Skip to content

Commit

Permalink
Rebase atop flutter#29520
Browse files Browse the repository at this point in the history
Previous commit: flutter@cdbb428

Add test context for loading swiftshader so

update

Remove unused things

Prereqs to GPUSurfaceVulkan rewrite

Comment typos
  • Loading branch information
bdero committed Dec 13, 2021
1 parent 71f9af2 commit b933684
Show file tree
Hide file tree
Showing 22 changed files with 497 additions and 66 deletions.
4 changes: 2 additions & 2 deletions shell/gpu/gpu_surface_metal_delegate.h
Expand Up @@ -75,14 +75,14 @@ class GPUSurfaceMetalDelegate {

//------------------------------------------------------------------------------
/// @brief Returns the handle to the MTLTexture to render to. This is only
/// called when the specefied render target type is `kMTLTexture`.
/// called when the specified render target type is `kMTLTexture`.
///
virtual GPUMTLTextureInfo GetMTLTexture(const SkISize& frame_info) const = 0;

//------------------------------------------------------------------------------
/// @brief Presents the texture with `texture_id` to the "screen".
/// `texture_id` corresponds to a texture that has been obtained by an earlier
/// call to `GetMTLTexture`. This is only called when the specefied render
/// call to `GetMTLTexture`. This is only called when the specified render
/// target type is `kMTLTexture`.
///
/// @see |GPUSurfaceMetalDelegate::GetMTLTexture|
Expand Down
36 changes: 10 additions & 26 deletions shell/gpu/gpu_surface_vulkan.cc
Expand Up @@ -8,37 +8,19 @@

namespace flutter {

GPUSurfaceVulkan::GPUSurfaceVulkan(
GPUSurfaceVulkanDelegate* delegate,
std::unique_ptr<vulkan::VulkanNativeSurface> native_surface,
bool render_to_surface)
: GPUSurfaceVulkan(/*context=*/nullptr,
delegate,
std::move(native_surface),
render_to_surface) {}

GPUSurfaceVulkan::GPUSurfaceVulkan(
const sk_sp<GrDirectContext>& context,
GPUSurfaceVulkanDelegate* delegate,
std::unique_ptr<vulkan::VulkanNativeSurface> native_surface,
bool render_to_surface)
: window_(context,
delegate->vk(),
std::move(native_surface),
render_to_surface),
render_to_surface_(render_to_surface),
weak_factory_(this) {}
GPUSurfaceVulkan::GPUSurfaceVulkan(const sk_sp<GrDirectContext>& skia_context,
GPUSurfaceVulkanDelegate* delegate)
: skia_context_(skia_context), delegate_(delegate), weak_factory_(this) {}

GPUSurfaceVulkan::~GPUSurfaceVulkan() = default;

bool GPUSurfaceVulkan::IsValid() {
return window_.IsValid();
return image_ != nullptr;
}

std::unique_ptr<SurfaceFrame> GPUSurfaceVulkan::AcquireFrame(
const SkISize& size) {
SurfaceFrame::FramebufferInfo framebuffer_info;
framebuffer_info.supports_readback = true;
VkImage image = delegate_->AcquireImage(size);

// TODO(38466): Refactor GPU surface APIs take into account the fact that an
// external view embedder may want to render to the root surface.
Expand All @@ -50,7 +32,7 @@ std::unique_ptr<SurfaceFrame> GPUSurfaceVulkan::AcquireFrame(
});
}

auto surface = window_.AcquireSurface();
sk_sp<SkSurface> surface = window_.AcquireSurface();

if (surface == nullptr) {
return nullptr;
Expand All @@ -65,12 +47,14 @@ std::unique_ptr<SurfaceFrame> GPUSurfaceVulkan::AcquireFrame(
if (canvas == nullptr || !weak_this) {
return false;
}
return weak_this->window_.SwapBuffers();
return weak_this->Present();
};
return std::make_unique<SurfaceFrame>(
std::move(surface), std::move(framebuffer_info), std::move(callback));
}

bool GPUSurfaceVulkan::Present() {}

SkMatrix GPUSurfaceVulkan::GetRootTransformation() const {
// This backend does not support delegating to the underlying platform to
// query for root surface transformations. Just return identity.
Expand All @@ -80,7 +64,7 @@ SkMatrix GPUSurfaceVulkan::GetRootTransformation() const {
}

GrDirectContext* GPUSurfaceVulkan::GetContext() {
return window_.GetSkiaGrContext();
return skia_context_;
}

} // namespace flutter
31 changes: 18 additions & 13 deletions shell/gpu/gpu_surface_vulkan.h
Expand Up @@ -11,30 +11,26 @@
#include "flutter/fml/macros.h"
#include "flutter/fml/memory/weak_ptr.h"
#include "flutter/shell/gpu/gpu_surface_vulkan_delegate.h"
#include "flutter/vulkan/vulkan_backbuffer.h"
#include "flutter/vulkan/vulkan_native_surface.h"
#include "flutter/vulkan/vulkan_window.h"
#include "include/core/SkRefCnt.h"

namespace flutter {


//------------------------------------------------------------------------------
/// @brief A GPU surface backed by VkImages provided by a
/// GPUSurfaceVulkanDelegate.
///
class GPUSurfaceVulkan : public Surface {
public:
//------------------------------------------------------------------------------
/// @brief Create a GPUSurfaceVulkan which implicitly creates its own
/// GrDirectContext for Skia.
///
GPUSurfaceVulkan(GPUSurfaceVulkanDelegate* delegate,
std::unique_ptr<vulkan::VulkanNativeSurface> native_surface,
bool render_to_surface);

//------------------------------------------------------------------------------
/// @brief Create a GPUSurfaceVulkan while letting it reuse an existing
/// GrDirectContext.
///
GPUSurfaceVulkan(const sk_sp<GrDirectContext>& context,
GPUSurfaceVulkanDelegate* delegate,
std::unique_ptr<vulkan::VulkanNativeSurface> native_surface,
bool render_to_surface);
GPUSurfaceVulkanDelegate* delegate);

~GPUSurfaceVulkan() override;

Expand All @@ -44,15 +40,24 @@ class GPUSurfaceVulkan : public Surface {
// |Surface|
std::unique_ptr<SurfaceFrame> AcquireFrame(const SkISize& size) override;

/// @brief Called when a frame is done rendering. It blocks on the render
/// fence and then calls `GPUSurfaceVulkanDelegate::PresentImage` with
/// the populated image.
bool Present();

// |Surface|
SkMatrix GetRootTransformation() const override;

// |Surface|
GrDirectContext* GetContext() override;

private:
vulkan::VulkanWindow window_;
const bool render_to_surface_;
sk_sp<GrDirectContext> skia_context_;
GPUSurfaceVulkanDelegate* delegate_;

std::vector<std::unique_ptr<VulkanBackbuffer>> backbuffers_;
std::vector<sk_sp<SkSurface>> surfaces_;
size_t current_backbuffer_index_;

fml::WeakPtrFactory<GPUSurfaceVulkan> weak_factory_;
FML_DISALLOW_COPY_AND_ASSIGN(GPUSurfaceVulkan);
Expand Down
30 changes: 28 additions & 2 deletions shell/gpu/gpu_surface_vulkan_delegate.h
Expand Up @@ -6,16 +6,42 @@
#define FLUTTER_SHELL_GPU_GPU_SURFACE_VULKAN_DELEGATE_H_

#include "flutter/fml/memory/ref_ptr.h"
#include "flutter/vulkan/vulkan_device.h"
#include "flutter/vulkan/vulkan_image.h"
#include "flutter/vulkan/vulkan_proc_table.h"
#include "third_party/skia/include/core/SkSize.h"

namespace flutter {

//------------------------------------------------------------------------------
/// @brief Interface implemented by all platform surfaces that can present
/// a Vulkan backing store to the "screen". The GPU surface
/// abstraction (which abstracts the client rendering API) uses this
/// delegation pattern to tell the platform surface (which abstracts
/// how backing stores fulfilled by the selected client rendering
/// API end up on the "screen" on a particular platform) when the
/// rasterizer needs to allocate and present the Vulkan backing
/// store.
///
/// @see |EmbedderSurfaceVulkan|.
///
class GPUSurfaceVulkanDelegate {
public:
~GPUSurfaceVulkanDelegate();
virtual ~GPUSurfaceVulkanDelegate();

// Obtain a reference to the Vulkan implementation's proc table.
/// @brief Obtain a reference to the Vulkan implementation's proc table.
///
virtual fml::RefPtr<vulkan::VulkanProcTable> vk() = 0;

/// @brief Called by the engine to fetch a VkImage for writing the next
/// frame.
///
virtual VkImage AcquireImage(const SkISize& size);

/// @brief Called by the engine once a frame has been rendered to the image
/// and it's ready to be bound for further reading/writing.
///
virtual bool PresentImage(VkImage image);
};

} // namespace flutter
Expand Down
16 changes: 15 additions & 1 deletion shell/platform/embedder/BUILD.gn
Expand Up @@ -104,6 +104,13 @@ template("embedder_source_set") {
deps += [ "//flutter/shell/platform/darwin/graphics" ]
}

if (embedder_enable_gl) {
sources += [
"embedder_surface_vulkan.cc",
"embedder_surface_vulkan.h",
]
}

public_deps = [ ":embedder_headers" ]

public_configs += [
Expand Down Expand Up @@ -228,7 +235,9 @@ if (enable_unittests) {
"tests/embedder_unittests_gl.cc",
]

deps += [ "//flutter/testing:opengl" ]
deps += [
"//flutter/testing:opengl",
]
}

if (test_enable_metal) {
Expand All @@ -244,6 +253,11 @@ if (enable_unittests) {
}

if (test_enable_vulkan) {
sources += [
"tests/embedder_test_context_vulkan.cc",
"tests/embedder_test_context_vulkan.h",
]

deps += [
"//flutter/testing:vulkan",
"//flutter/vulkan",
Expand Down
52 changes: 52 additions & 0 deletions shell/platform/embedder/embedder.cc
Expand Up @@ -388,6 +388,41 @@ InferMetalPlatformViewCreationCallback(
#endif
}

static flutter::Shell::CreateCallback<flutter::PlatformView>
InferVulkanPlatformViewCreationCallback(
const FlutterRendererConfig* config,
void* user_data,
flutter::PlatformViewEmbedder::PlatformDispatchTable
platform_dispatch_table,
std::unique_ptr<flutter::EmbedderExternalViewEmbedder>
external_view_embedder) {
if (config->type != kVulkan) {
return nullptr;
}

#ifdef SHELL_ENABLE_VULKAN
std::shared_ptr<flutter::EmbedderExternalViewEmbedder> view_embedder =
std::move(external_view_embedder);

std::unique_ptr<flutter::EmbedderSurfaceVulkan> embedder_surface =
std::make_unique<flutter::EmbedderSurfaceVulkan>(view_embedder);

return fml::MakeCopyable(
[embedder_surface = std::move(embedder_surface), platform_dispatch_table,
external_view_embedder = view_embedder](flutter::Shell& shell) mutable {
return std::make_unique<flutter::PlatformViewEmbedder>(
shell, // delegate
shell.GetTaskRunners(), // task runners
std::move(embedder_surface), // embedder surface
platform_dispatch_table, // platform dispatch table
std::move(external_view_embedder) // external view embedder
);
});
#else
return nullptr;
#endif
}

static flutter::Shell::CreateCallback<flutter::PlatformView>
InferSoftwarePlatformViewCreationCallback(
const FlutterRendererConfig* config,
Expand Down Expand Up @@ -450,6 +485,10 @@ InferPlatformViewCreationCallback(
return InferMetalPlatformViewCreationCallback(
config, user_data, platform_dispatch_table,
std::move(external_view_embedder));
case kVulkan:
return InferVulkanPlatformViewCreationCallback(
config, user_data, platform_dispatch_table,
std::move(external_view_embedder));
default:
return nullptr;
}
Expand Down Expand Up @@ -624,6 +663,14 @@ static sk_sp<SkSurface> MakeSkSurfaceFromBackingStore(
#endif
}

static sk_sp<SkSurface> MakeSkSurfaceFromBackingStore(
GrDirectContext* context,
const FlutterBackingStoreConfig& config,
const FlutterVulkanBackingStore* vulkan) {
assert(false); // TODO(bdero)
return nullptr;
}

static std::unique_ptr<flutter::EmbedderRenderTarget>
CreateEmbedderRenderTarget(const FlutterCompositor* compositor,
const FlutterBackingStoreConfig& config,
Expand Down Expand Up @@ -685,6 +732,11 @@ CreateEmbedderRenderTarget(const FlutterCompositor* compositor,
render_surface =
MakeSkSurfaceFromBackingStore(context, config, &backing_store.metal);
break;

case kFlutterBackingStoreTypeVulkan:
render_surface =
MakeSkSurfaceFromBackingStore(context, config, &backing_store.vulkan);
break;
};

if (!render_surface) {
Expand Down

0 comments on commit b933684

Please sign in to comment.