Skip to content

Commit 6760858

Browse files
authored
EmbedderTest: Extract backend-specific user_data (flutter/engine#56642)
Replace the top-level public EmbedderBackingStoreProducer::UserData struct with backend-specific UserData classes, and make them internal to those backends. UserData internals shouldn't be visible to/manipulated by unit tests and compositors, but instead constrained to the backing store producer itself. Issue: flutter#158998 [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
1 parent 2840d49 commit 6760858

15 files changed

+148
-76
lines changed

engine/src/flutter/shell/platform/embedder/tests/embedder_test_backingstore_producer.h

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,6 @@ namespace flutter::testing {
2121

2222
class EmbedderTestBackingStoreProducer {
2323
public:
24-
struct UserData {
25-
UserData() : surface(nullptr), image(nullptr){};
26-
27-
explicit UserData(sk_sp<SkSurface> surface)
28-
: surface(std::move(surface)), image(nullptr){};
29-
30-
UserData(sk_sp<SkSurface> surface, FlutterVulkanImage* vk_image)
31-
: surface(std::move(surface)), image(vk_image){};
32-
33-
sk_sp<SkSurface> surface;
34-
FlutterVulkanImage* image;
35-
#ifdef SHELL_ENABLE_GL
36-
UserData(sk_sp<SkSurface> surface,
37-
FlutterVulkanImage* vk_image,
38-
std::unique_ptr<TestGLOnscreenOnlySurface> gl_surface)
39-
: surface(std::move(surface)),
40-
image(vk_image),
41-
gl_surface(std::move(gl_surface)){};
42-
43-
std::unique_ptr<TestGLOnscreenOnlySurface> gl_surface;
44-
#endif
45-
};
46-
4724
enum class RenderTargetType {
4825
kSoftwareBuffer,
4926
kSoftwareBuffer2,
@@ -61,6 +38,12 @@ class EmbedderTestBackingStoreProducer {
6138
virtual bool Create(const FlutterBackingStoreConfig* config,
6239
FlutterBackingStore* backing_store_out) = 0;
6340

41+
virtual sk_sp<SkSurface> GetSurface(
42+
const FlutterBackingStore* backing_store) const = 0;
43+
44+
virtual sk_sp<SkImage> MakeImageSnapshot(
45+
const FlutterBackingStore* backing_store) const = 0;
46+
6447
protected:
6548
sk_sp<GrDirectContext> context_;
6649
RenderTargetType type_;

engine/src/flutter/shell/platform/embedder/tests/embedder_test_backingstore_producer_gl.cc

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414

1515
namespace flutter::testing {
1616

17+
namespace {
18+
struct UserData {
19+
sk_sp<SkSurface> surface;
20+
std::unique_ptr<TestGLOnscreenOnlySurface> gl_surface;
21+
};
22+
} // namespace
23+
1724
EmbedderTestBackingStoreProducerGL::EmbedderTestBackingStoreProducerGL(
1825
sk_sp<GrDirectContext> context,
1926
RenderTargetType type,
@@ -39,6 +46,30 @@ bool EmbedderTestBackingStoreProducerGL::Create(
3946
};
4047
}
4148

49+
sk_sp<SkSurface> EmbedderTestBackingStoreProducerGL::GetSurface(
50+
const FlutterBackingStore* backing_store) const {
51+
UserData* user_data = reinterpret_cast<UserData*>(backing_store->user_data);
52+
return user_data->surface;
53+
}
54+
55+
sk_sp<SkImage> EmbedderTestBackingStoreProducerGL::MakeImageSnapshot(
56+
const FlutterBackingStore* backing_store) const {
57+
UserData* user_data = reinterpret_cast<UserData*>(backing_store->user_data);
58+
if (user_data->gl_surface != nullptr) {
59+
// This backing store is an OpenGL Surface.
60+
// We need to make it current so we can snapshot it.
61+
user_data->gl_surface->MakeCurrent();
62+
63+
// GetRasterSurfaceSnapshot() does two
64+
// gl_surface->makeImageSnapshot()'s. Doing a single
65+
// ->makeImageSnapshot() will not work.
66+
return user_data->gl_surface->GetRasterSurfaceSnapshot();
67+
}
68+
69+
// Otherwise, it's a GL Texture or FrameBuffer.
70+
return user_data->surface->makeImageSnapshot();
71+
}
72+
4273
bool EmbedderTestBackingStoreProducerGL::CreateFramebuffer(
4374
const FlutterBackingStoreConfig* config,
4475
FlutterBackingStore* backing_store_out) {
@@ -75,8 +106,7 @@ bool EmbedderTestBackingStoreProducerGL::CreateFramebuffer(
75106
return false;
76107
}
77108

78-
auto user_data = new UserData(surface);
79-
109+
auto user_data = new UserData{.surface = surface};
80110
backing_store_out->type = kFlutterBackingStoreTypeOpenGL;
81111
backing_store_out->user_data = user_data;
82112
backing_store_out->open_gl.type = kFlutterOpenGLTargetTypeFramebuffer;
@@ -124,8 +154,7 @@ bool EmbedderTestBackingStoreProducerGL::CreateTexture(
124154
return false;
125155
}
126156

127-
auto user_data = new UserData(surface);
128-
157+
auto user_data = new UserData{.surface = surface};
129158
backing_store_out->type = kFlutterBackingStoreTypeOpenGL;
130159
backing_store_out->user_data = user_data;
131160
backing_store_out->open_gl.type = kFlutterOpenGLTargetTypeTexture;
@@ -165,8 +194,10 @@ bool EmbedderTestBackingStoreProducerGL::CreateSurface(
165194

166195
auto sk_surface = surface->GetOnscreenSurface();
167196

168-
auto user_data = new UserData(sk_surface, nullptr, std::move(surface));
169-
197+
auto user_data = new UserData{
198+
.surface = sk_surface,
199+
.gl_surface = std::move(surface),
200+
};
170201
backing_store_out->type = kFlutterBackingStoreTypeOpenGL;
171202
backing_store_out->user_data = user_data;
172203
backing_store_out->open_gl.type = kFlutterOpenGLTargetTypeSurface;

engine/src/flutter/shell/platform/embedder/tests/embedder_test_backingstore_producer_gl.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,14 @@ class EmbedderTestBackingStoreProducerGL
2323

2424
virtual ~EmbedderTestBackingStoreProducerGL();
2525

26-
virtual bool Create(const FlutterBackingStoreConfig* config,
27-
FlutterBackingStore* backing_store_out);
26+
bool Create(const FlutterBackingStoreConfig* config,
27+
FlutterBackingStore* backing_store_out) override;
28+
29+
sk_sp<SkSurface> GetSurface(
30+
const FlutterBackingStore* backing_store) const override;
31+
32+
sk_sp<SkImage> MakeImageSnapshot(
33+
const FlutterBackingStore* backing_store) const override;
2834

2935
private:
3036
bool CreateFramebuffer(const FlutterBackingStoreConfig* config,

engine/src/flutter/shell/platform/embedder/tests/embedder_test_backingstore_producer_metal.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,14 @@ class EmbedderTestBackingStoreProducerMetal
1818

1919
virtual ~EmbedderTestBackingStoreProducerMetal();
2020

21-
virtual bool Create(const FlutterBackingStoreConfig* config,
22-
FlutterBackingStore* backing_store_out);
21+
bool Create(const FlutterBackingStoreConfig* config,
22+
FlutterBackingStore* backing_store_out) override;
23+
24+
sk_sp<SkSurface> GetSurface(
25+
const FlutterBackingStore* backing_store) const override;
26+
27+
sk_sp<SkImage> MakeImageSnapshot(
28+
const FlutterBackingStore* backing_store) const override;
2329

2430
private:
2531
std::unique_ptr<TestMetalContext> test_metal_context_;

engine/src/flutter/shell/platform/embedder/tests/embedder_test_backingstore_producer_metal.mm

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include "flutter/shell/platform/embedder/tests/embedder_test_backingstore_producer_metal.h"
66

7+
#include <exception>
8+
79
#include "flutter/fml/logging.h"
810
#include "third_party/skia/include/core/SkColorSpace.h"
911
#include "third_party/skia/include/gpu/ganesh/GrBackendSurface.h"
@@ -56,4 +58,16 @@
5658
return true;
5759
}
5860

61+
sk_sp<SkSurface> EmbedderTestBackingStoreProducerMetal::GetSurface(
62+
const FlutterBackingStore* backing_store) const {
63+
FML_LOG(FATAL) << "Unimplemented.";
64+
std::terminate();
65+
}
66+
67+
sk_sp<SkImage> EmbedderTestBackingStoreProducerMetal::MakeImageSnapshot(
68+
const FlutterBackingStore* backing_store) const {
69+
FML_LOG(FATAL) << "Unimplemented.";
70+
std::terminate();
71+
}
72+
5973
} // namespace flutter::testing

engine/src/flutter/shell/platform/embedder/tests/embedder_test_backingstore_producer_software.cc

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212

1313
namespace flutter::testing {
1414

15+
namespace {
16+
struct UserData {
17+
sk_sp<SkSurface> surface;
18+
};
19+
} // namespace
20+
1521
EmbedderTestBackingStoreProducerSoftware::
1622
EmbedderTestBackingStoreProducerSoftware(
1723
sk_sp<GrDirectContext> context,
@@ -46,6 +52,18 @@ bool EmbedderTestBackingStoreProducerSoftware::Create(
4652
}
4753
}
4854

55+
sk_sp<SkSurface> EmbedderTestBackingStoreProducerSoftware::GetSurface(
56+
const FlutterBackingStore* backing_store) const {
57+
UserData* user_data = reinterpret_cast<UserData*>(backing_store->user_data);
58+
return user_data->surface;
59+
}
60+
61+
sk_sp<SkImage> EmbedderTestBackingStoreProducerSoftware::MakeImageSnapshot(
62+
const FlutterBackingStore* backing_store) const {
63+
auto user_data = reinterpret_cast<UserData*>(backing_store->user_data);
64+
return user_data->surface->makeImageSnapshot();
65+
}
66+
4967
bool EmbedderTestBackingStoreProducerSoftware::CreateSoftware(
5068
const FlutterBackingStoreConfig* config,
5169
FlutterBackingStore* backing_store_out) {
@@ -64,8 +82,7 @@ bool EmbedderTestBackingStoreProducerSoftware::CreateSoftware(
6482
return false;
6583
}
6684

67-
auto user_data = new UserData(surface);
68-
85+
auto user_data = new UserData{.surface = surface};
6986
backing_store_out->type = kFlutterBackingStoreTypeSoftware;
7087
backing_store_out->user_data = user_data;
7188
backing_store_out->software.allocation = pixmap.addr();
@@ -101,8 +118,7 @@ bool EmbedderTestBackingStoreProducerSoftware::CreateSoftware2(
101118
return false;
102119
}
103120

104-
auto user_data = new UserData(surface);
105-
121+
auto user_data = new UserData{.surface = surface};
106122
backing_store_out->type = kFlutterBackingStoreTypeSoftware2;
107123
backing_store_out->user_data = user_data;
108124
backing_store_out->software2.struct_size =

engine/src/flutter/shell/platform/embedder/tests/embedder_test_backingstore_producer_software.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,14 @@ class EmbedderTestBackingStoreProducerSoftware
2020

2121
virtual ~EmbedderTestBackingStoreProducerSoftware();
2222

23-
virtual bool Create(const FlutterBackingStoreConfig* config,
24-
FlutterBackingStore* backing_store_out);
23+
bool Create(const FlutterBackingStoreConfig* config,
24+
FlutterBackingStore* backing_store_out) override;
25+
26+
sk_sp<SkSurface> GetSurface(
27+
const FlutterBackingStore* backing_store) const override;
28+
29+
sk_sp<SkImage> MakeImageSnapshot(
30+
const FlutterBackingStore* backing_store) const override;
2531

2632
private:
2733
bool CreateSoftware(const FlutterBackingStoreConfig* config,

engine/src/flutter/shell/platform/embedder/tests/embedder_test_backingstore_producer_vulkan.cc

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313

1414
namespace flutter::testing {
1515

16+
namespace {
17+
struct UserData {
18+
sk_sp<SkSurface> surface;
19+
FlutterVulkanImage* image;
20+
};
21+
} // namespace
22+
1623
EmbedderTestBackingStoreProducerVulkan::EmbedderTestBackingStoreProducerVulkan(
1724
sk_sp<GrDirectContext> context,
1825
RenderTargetType type)
@@ -83,7 +90,7 @@ bool EmbedderTestBackingStoreProducerVulkan::Create(
8390

8491
// Collect all allocated resources in the destruction_callback.
8592
{
86-
auto user_data = new UserData(surface, image);
93+
auto user_data = new UserData{.surface = surface, .image = image};
8794
backing_store_out->user_data = user_data;
8895
backing_store_out->vulkan.user_data = user_data;
8996
backing_store_out->vulkan.destruction_callback = [](void* user_data) {
@@ -96,4 +103,16 @@ bool EmbedderTestBackingStoreProducerVulkan::Create(
96103
return true;
97104
}
98105

106+
sk_sp<SkSurface> EmbedderTestBackingStoreProducerVulkan::GetSurface(
107+
const FlutterBackingStore* backing_store) const {
108+
UserData* user_data = reinterpret_cast<UserData*>(backing_store->user_data);
109+
return user_data->surface;
110+
}
111+
112+
sk_sp<SkImage> EmbedderTestBackingStoreProducerVulkan::MakeImageSnapshot(
113+
const FlutterBackingStore* backing_store) const {
114+
UserData* user_data = reinterpret_cast<UserData*>(backing_store->user_data);
115+
return user_data->surface->makeImageSnapshot();
116+
}
117+
99118
} // namespace flutter::testing

engine/src/flutter/shell/platform/embedder/tests/embedder_test_backingstore_producer_vulkan.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,14 @@ class EmbedderTestBackingStoreProducerVulkan
1919

2020
virtual ~EmbedderTestBackingStoreProducerVulkan();
2121

22-
virtual bool Create(const FlutterBackingStoreConfig* config,
23-
FlutterBackingStore* backing_store_out);
22+
bool Create(const FlutterBackingStoreConfig* config,
23+
FlutterBackingStore* backing_store_out) override;
24+
25+
sk_sp<SkSurface> GetSurface(
26+
const FlutterBackingStore* backing_store) const override;
27+
28+
sk_sp<SkImage> MakeImageSnapshot(
29+
const FlutterBackingStore* backing_store) const override;
2430

2531
private:
2632
fml::RefPtr<TestVulkanContext> test_vulkan_context_;

engine/src/flutter/shell/platform/embedder/tests/embedder_test_compositor.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ bool EmbedderTestCompositor::CollectBackingStore(
5151
return true;
5252
}
5353

54+
sk_sp<SkSurface> EmbedderTestCompositor::GetSurface(
55+
const FlutterBackingStore* backing_store) const {
56+
return backingstore_producer_->GetSurface(backing_store);
57+
}
58+
5459
sk_sp<SkImage> EmbedderTestCompositor::GetLastComposition() {
5560
return last_composition_;
5661
}

engine/src/flutter/shell/platform/embedder/tests/embedder_test_compositor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ class EmbedderTestCompositor {
4545
void SetPlatformViewRendererCallback(
4646
const PlatformViewRendererCallback& callback);
4747

48+
sk_sp<SkSurface> GetSurface(const FlutterBackingStore* backing_store) const;
49+
4850
//----------------------------------------------------------------------------
4951
/// @brief Allows tests to install a callback to notify them when the
5052
/// entire render tree has been finalized so they can run their

engine/src/flutter/shell/platform/embedder/tests/embedder_test_compositor_gl.cc

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -89,24 +89,8 @@ bool EmbedderTestCompositorGL::UpdateOffscrenComposition(
8989

9090
switch (layer->type) {
9191
case kFlutterLayerContentTypeBackingStore: {
92-
auto gl_user_data =
93-
reinterpret_cast<EmbedderTestBackingStoreProducer::UserData*>(
94-
layer->backing_store->user_data);
95-
96-
if (gl_user_data->gl_surface != nullptr) {
97-
// This backing store is a OpenGL Surface.
98-
// We need to make it current so we can snapshot it.
99-
100-
gl_user_data->gl_surface->MakeCurrent();
101-
102-
// GetRasterSurfaceSnapshot() does two
103-
// gl_surface->makeImageSnapshot()'s. Doing a single
104-
// ->makeImageSnapshot() will not work.
105-
layer_image = gl_user_data->gl_surface->GetRasterSurfaceSnapshot();
106-
} else {
107-
layer_image = gl_user_data->surface->makeImageSnapshot();
108-
}
109-
92+
layer_image =
93+
backingstore_producer_->MakeImageSnapshot(layer->backing_store);
11094
// We don't clear the current surface here because we need the
11195
// EGL context to be current for surface->makeImageSnapshot() below.
11296
break;

engine/src/flutter/shell/platform/embedder/tests/embedder_test_compositor_software.cc

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,11 @@ bool EmbedderTestCompositorSoftware::UpdateOffscrenComposition(
6868
SkIPoint canvas_offset = SkIPoint::Make(0, 0);
6969

7070
switch (layer->type) {
71-
case kFlutterLayerContentTypeBackingStore:
71+
case kFlutterLayerContentTypeBackingStore: {
7272
layer_image =
73-
reinterpret_cast<EmbedderTestBackingStoreProducer::UserData*>(
74-
layer->backing_store->user_data)
75-
->surface->makeImageSnapshot();
76-
73+
backingstore_producer_->MakeImageSnapshot(layer->backing_store);
7774
break;
75+
}
7876
case kFlutterLayerContentTypePlatformView:
7977
layer_image = platform_view_renderer_callback_
8078
? platform_view_renderer_callback_(*layer, nullptr)

engine/src/flutter/shell/platform/embedder/tests/embedder_test_compositor_vulkan.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,7 @@ bool EmbedderTestCompositorVulkan::UpdateOffscrenComposition(
8282
switch (layer->type) {
8383
case kFlutterLayerContentTypeBackingStore:
8484
layer_image =
85-
reinterpret_cast<EmbedderTestBackingStoreProducer::UserData*>(
86-
layer->backing_store->user_data)
87-
->surface->makeImageSnapshot();
85+
backingstore_producer_->MakeImageSnapshot(layer->backing_store);
8886
break;
8987
case kFlutterLayerContentTypePlatformView:
9088
layer_image =

0 commit comments

Comments
 (0)