Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 62671fa

Browse files
committed
Rebase atop #29520
Previous commit: cdbb428 Add test context for loading swiftshader so update Remove unused things Prereqs to GPUSurfaceVulkan rewrite Comment typos
1 parent aabd53d commit 62671fa

22 files changed

+497
-66
lines changed

shell/gpu/gpu_surface_metal_delegate.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,14 @@ class GPUSurfaceMetalDelegate {
7575

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

8282
//------------------------------------------------------------------------------
8383
/// @brief Presents the texture with `texture_id` to the "screen".
8484
/// `texture_id` corresponds to a texture that has been obtained by an earlier
85-
/// call to `GetMTLTexture`. This is only called when the specefied render
85+
/// call to `GetMTLTexture`. This is only called when the specified render
8686
/// target type is `kMTLTexture`.
8787
///
8888
/// @see |GPUSurfaceMetalDelegate::GetMTLTexture|

shell/gpu/gpu_surface_vulkan.cc

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,19 @@
88

99
namespace flutter {
1010

11-
GPUSurfaceVulkan::GPUSurfaceVulkan(
12-
GPUSurfaceVulkanDelegate* delegate,
13-
std::unique_ptr<vulkan::VulkanNativeSurface> native_surface,
14-
bool render_to_surface)
15-
: GPUSurfaceVulkan(/*context=*/nullptr,
16-
delegate,
17-
std::move(native_surface),
18-
render_to_surface) {}
19-
20-
GPUSurfaceVulkan::GPUSurfaceVulkan(
21-
const sk_sp<GrDirectContext>& context,
22-
GPUSurfaceVulkanDelegate* delegate,
23-
std::unique_ptr<vulkan::VulkanNativeSurface> native_surface,
24-
bool render_to_surface)
25-
: window_(context,
26-
delegate->vk(),
27-
std::move(native_surface),
28-
render_to_surface),
29-
render_to_surface_(render_to_surface),
30-
weak_factory_(this) {}
11+
GPUSurfaceVulkan::GPUSurfaceVulkan(const sk_sp<GrDirectContext>& skia_context,
12+
GPUSurfaceVulkanDelegate* delegate)
13+
: skia_context_(skia_context), delegate_(delegate), weak_factory_(this) {}
3114

3215
GPUSurfaceVulkan::~GPUSurfaceVulkan() = default;
3316

3417
bool GPUSurfaceVulkan::IsValid() {
35-
return window_.IsValid();
18+
return image_ != nullptr;
3619
}
3720

3821
std::unique_ptr<SurfaceFrame> GPUSurfaceVulkan::AcquireFrame(
3922
const SkISize& size) {
40-
SurfaceFrame::FramebufferInfo framebuffer_info;
41-
framebuffer_info.supports_readback = true;
23+
VkImage image = delegate_->AcquireImage(size);
4224

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

53-
auto surface = window_.AcquireSurface();
35+
sk_sp<SkSurface> surface = window_.AcquireSurface();
5436

5537
if (surface == nullptr) {
5638
return nullptr;
@@ -65,12 +47,14 @@ std::unique_ptr<SurfaceFrame> GPUSurfaceVulkan::AcquireFrame(
6547
if (canvas == nullptr || !weak_this) {
6648
return false;
6749
}
68-
return weak_this->window_.SwapBuffers();
50+
return weak_this->Present();
6951
};
7052
return std::make_unique<SurfaceFrame>(
7153
std::move(surface), std::move(framebuffer_info), std::move(callback));
7254
}
7355

56+
bool GPUSurfaceVulkan::Present() {}
57+
7458
SkMatrix GPUSurfaceVulkan::GetRootTransformation() const {
7559
// This backend does not support delegating to the underlying platform to
7660
// query for root surface transformations. Just return identity.
@@ -80,7 +64,7 @@ SkMatrix GPUSurfaceVulkan::GetRootTransformation() const {
8064
}
8165

8266
GrDirectContext* GPUSurfaceVulkan::GetContext() {
83-
return window_.GetSkiaGrContext();
67+
return skia_context_;
8468
}
8569

8670
} // namespace flutter

shell/gpu/gpu_surface_vulkan.h

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,26 @@
1111
#include "flutter/fml/macros.h"
1212
#include "flutter/fml/memory/weak_ptr.h"
1313
#include "flutter/shell/gpu/gpu_surface_vulkan_delegate.h"
14+
#include "flutter/vulkan/vulkan_backbuffer.h"
1415
#include "flutter/vulkan/vulkan_native_surface.h"
1516
#include "flutter/vulkan/vulkan_window.h"
1617
#include "include/core/SkRefCnt.h"
1718

1819
namespace flutter {
1920

21+
22+
//------------------------------------------------------------------------------
23+
/// @brief A GPU surface backed by VkImages provided by a
24+
/// GPUSurfaceVulkanDelegate.
25+
///
2026
class GPUSurfaceVulkan : public Surface {
2127
public:
22-
//------------------------------------------------------------------------------
23-
/// @brief Create a GPUSurfaceVulkan which implicitly creates its own
24-
/// GrDirectContext for Skia.
25-
///
26-
GPUSurfaceVulkan(GPUSurfaceVulkanDelegate* delegate,
27-
std::unique_ptr<vulkan::VulkanNativeSurface> native_surface,
28-
bool render_to_surface);
29-
3028
//------------------------------------------------------------------------------
3129
/// @brief Create a GPUSurfaceVulkan while letting it reuse an existing
3230
/// GrDirectContext.
3331
///
3432
GPUSurfaceVulkan(const sk_sp<GrDirectContext>& context,
35-
GPUSurfaceVulkanDelegate* delegate,
36-
std::unique_ptr<vulkan::VulkanNativeSurface> native_surface,
37-
bool render_to_surface);
33+
GPUSurfaceVulkanDelegate* delegate);
3834

3935
~GPUSurfaceVulkan() override;
4036

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

43+
/// @brief Called when a frame is done rendering. It blocks on the render
44+
/// fence and then calls `GPUSurfaceVulkanDelegate::PresentImage` with
45+
/// the populated image.
46+
bool Present();
47+
4748
// |Surface|
4849
SkMatrix GetRootTransformation() const override;
4950

5051
// |Surface|
5152
GrDirectContext* GetContext() override;
5253

5354
private:
54-
vulkan::VulkanWindow window_;
55-
const bool render_to_surface_;
55+
sk_sp<GrDirectContext> skia_context_;
56+
GPUSurfaceVulkanDelegate* delegate_;
57+
58+
std::vector<std::unique_ptr<VulkanBackbuffer>> backbuffers_;
59+
std::vector<sk_sp<SkSurface>> surfaces_;
60+
size_t current_backbuffer_index_;
5661

5762
fml::WeakPtrFactory<GPUSurfaceVulkan> weak_factory_;
5863
FML_DISALLOW_COPY_AND_ASSIGN(GPUSurfaceVulkan);

shell/gpu/gpu_surface_vulkan_delegate.h

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,42 @@
66
#define FLUTTER_SHELL_GPU_GPU_SURFACE_VULKAN_DELEGATE_H_
77

88
#include "flutter/fml/memory/ref_ptr.h"
9+
#include "flutter/vulkan/vulkan_device.h"
10+
#include "flutter/vulkan/vulkan_image.h"
911
#include "flutter/vulkan/vulkan_proc_table.h"
12+
#include "third_party/skia/include/core/SkSize.h"
1013

1114
namespace flutter {
1215

16+
//------------------------------------------------------------------------------
17+
/// @brief Interface implemented by all platform surfaces that can present
18+
/// a Vulkan backing store to the "screen". The GPU surface
19+
/// abstraction (which abstracts the client rendering API) uses this
20+
/// delegation pattern to tell the platform surface (which abstracts
21+
/// how backing stores fulfilled by the selected client rendering
22+
/// API end up on the "screen" on a particular platform) when the
23+
/// rasterizer needs to allocate and present the Vulkan backing
24+
/// store.
25+
///
26+
/// @see |EmbedderSurfaceVulkan|.
27+
///
1328
class GPUSurfaceVulkanDelegate {
1429
public:
15-
~GPUSurfaceVulkanDelegate();
30+
virtual ~GPUSurfaceVulkanDelegate();
1631

17-
// Obtain a reference to the Vulkan implementation's proc table.
32+
/// @brief Obtain a reference to the Vulkan implementation's proc table.
33+
///
1834
virtual fml::RefPtr<vulkan::VulkanProcTable> vk() = 0;
35+
36+
/// @brief Called by the engine to fetch a VkImage for writing the next
37+
/// frame.
38+
///
39+
virtual VkImage AcquireImage(const SkISize& size);
40+
41+
/// @brief Called by the engine once a frame has been rendered to the image
42+
/// and it's ready to be bound for further reading/writing.
43+
///
44+
virtual bool PresentImage(VkImage image);
1945
};
2046

2147
} // namespace flutter

shell/platform/embedder/BUILD.gn

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,13 @@ template("embedder_source_set") {
104104
deps += [ "//flutter/shell/platform/darwin/graphics" ]
105105
}
106106

107+
if (embedder_enable_gl) {
108+
sources += [
109+
"embedder_surface_vulkan.cc",
110+
"embedder_surface_vulkan.h",
111+
]
112+
}
113+
107114
public_deps = [ ":embedder_headers" ]
108115

109116
public_configs += [
@@ -228,7 +235,9 @@ if (enable_unittests) {
228235
"tests/embedder_unittests_gl.cc",
229236
]
230237

231-
deps += [ "//flutter/testing:opengl" ]
238+
deps += [
239+
"//flutter/testing:opengl",
240+
]
232241
}
233242

234243
if (test_enable_metal) {
@@ -244,6 +253,11 @@ if (enable_unittests) {
244253
}
245254

246255
if (test_enable_vulkan) {
256+
sources += [
257+
"tests/embedder_test_context_vulkan.cc",
258+
"tests/embedder_test_context_vulkan.h",
259+
]
260+
247261
deps += [
248262
"//flutter/testing:vulkan",
249263
"//flutter/vulkan",

shell/platform/embedder/embedder.cc

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,41 @@ InferMetalPlatformViewCreationCallback(
368368
#endif
369369
}
370370

371+
static flutter::Shell::CreateCallback<flutter::PlatformView>
372+
InferVulkanPlatformViewCreationCallback(
373+
const FlutterRendererConfig* config,
374+
void* user_data,
375+
flutter::PlatformViewEmbedder::PlatformDispatchTable
376+
platform_dispatch_table,
377+
std::unique_ptr<flutter::EmbedderExternalViewEmbedder>
378+
external_view_embedder) {
379+
if (config->type != kVulkan) {
380+
return nullptr;
381+
}
382+
383+
#ifdef SHELL_ENABLE_VULKAN
384+
std::shared_ptr<flutter::EmbedderExternalViewEmbedder> view_embedder =
385+
std::move(external_view_embedder);
386+
387+
std::unique_ptr<flutter::EmbedderSurfaceVulkan> embedder_surface =
388+
std::make_unique<flutter::EmbedderSurfaceVulkan>(view_embedder);
389+
390+
return fml::MakeCopyable(
391+
[embedder_surface = std::move(embedder_surface), platform_dispatch_table,
392+
external_view_embedder = view_embedder](flutter::Shell& shell) mutable {
393+
return std::make_unique<flutter::PlatformViewEmbedder>(
394+
shell, // delegate
395+
shell.GetTaskRunners(), // task runners
396+
std::move(embedder_surface), // embedder surface
397+
platform_dispatch_table, // platform dispatch table
398+
std::move(external_view_embedder) // external view embedder
399+
);
400+
});
401+
#else
402+
return nullptr;
403+
#endif
404+
}
405+
371406
static flutter::Shell::CreateCallback<flutter::PlatformView>
372407
InferSoftwarePlatformViewCreationCallback(
373408
const FlutterRendererConfig* config,
@@ -430,6 +465,10 @@ InferPlatformViewCreationCallback(
430465
return InferMetalPlatformViewCreationCallback(
431466
config, user_data, platform_dispatch_table,
432467
std::move(external_view_embedder));
468+
case kVulkan:
469+
return InferVulkanPlatformViewCreationCallback(
470+
config, user_data, platform_dispatch_table,
471+
std::move(external_view_embedder));
433472
default:
434473
return nullptr;
435474
}
@@ -604,6 +643,14 @@ static sk_sp<SkSurface> MakeSkSurfaceFromBackingStore(
604643
#endif
605644
}
606645

646+
static sk_sp<SkSurface> MakeSkSurfaceFromBackingStore(
647+
GrDirectContext* context,
648+
const FlutterBackingStoreConfig& config,
649+
const FlutterVulkanBackingStore* vulkan) {
650+
assert(false); // TODO(bdero)
651+
return nullptr;
652+
}
653+
607654
static std::unique_ptr<flutter::EmbedderRenderTarget>
608655
CreateEmbedderRenderTarget(const FlutterCompositor* compositor,
609656
const FlutterBackingStoreConfig& config,
@@ -665,6 +712,11 @@ CreateEmbedderRenderTarget(const FlutterCompositor* compositor,
665712
render_surface =
666713
MakeSkSurfaceFromBackingStore(context, config, &backing_store.metal);
667714
break;
715+
716+
case kFlutterBackingStoreTypeVulkan:
717+
render_surface =
718+
MakeSkSurfaceFromBackingStore(context, config, &backing_store.vulkan);
719+
break;
668720
};
669721

670722
if (!render_surface) {

0 commit comments

Comments
 (0)