Skip to content

Commit 39db174

Browse files
authored
Extract backend-specific code in ShellTestPlatformView (flutter/engine#56722)
Moves code specific to each graphics backend into the (existing) translation unit associated with that backend. Previously, we could not include any Objective-C types in `shell_test_platform_view_metal.h`, since that file was included into `shell_test_platform_view.cc`, which is pure C++. To work around this, we had encapsulated Objective-C Metal types in a `DarwinContextMetal` struct hidden in the implementation file with a pointer to it forward-declared in the header. We now use Metal types directly in the header, without the workarounds. Issue: flutter#158998 Issue: flutter#137801 [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
1 parent 5883c1a commit 39db174

7 files changed

+157
-102
lines changed

engine/src/flutter/shell/common/shell_test_platform_view.cc

Lines changed: 51 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,11 @@
44

55
#include "flutter/shell/common/shell_test_platform_view.h"
66

7-
#ifdef SHELL_ENABLE_GL
8-
#include "flutter/shell/common/shell_test_platform_view_gl.h"
9-
#endif // SHELL_ENABLE_GL
10-
#ifdef SHELL_ENABLE_VULKAN
11-
#include "flutter/shell/common/shell_test_platform_view_vulkan.h"
12-
#endif // SHELL_ENABLE_VULKAN
13-
#ifdef SHELL_ENABLE_METAL
14-
#include "flutter/shell/common/shell_test_platform_view_metal.h"
15-
#endif // SHELL_ENABLE_METAL
7+
#include <memory>
168

179
#include "flutter/shell/common/vsync_waiter_fallback.h"
1810

19-
namespace flutter {
20-
namespace testing {
11+
namespace flutter::testing {
2112

2213
std::unique_ptr<ShellTestPlatformView> ShellTestPlatformView::Create(
2314
BackendType backend,
@@ -32,35 +23,60 @@ std::unique_ptr<ShellTestPlatformView> ShellTestPlatformView::Create(
3223
// Make this fully runtime configurable
3324
switch (backend) {
3425
case BackendType::kGLBackend:
35-
#ifdef SHELL_ENABLE_GL
36-
return std::make_unique<ShellTestPlatformViewGL>(
37-
delegate, task_runners, vsync_clock, create_vsync_waiter,
38-
shell_test_external_view_embedder);
39-
#else
40-
FML_LOG(FATAL) << "OpenGL not enabled in this build";
41-
return nullptr;
42-
#endif // SHELL_ENABLE_GL
43-
case BackendType::kVulkanBackend:
44-
#ifdef SHELL_ENABLE_VULKAN
45-
return std::make_unique<ShellTestPlatformViewVulkan>(
46-
delegate, task_runners, vsync_clock, create_vsync_waiter,
47-
shell_test_external_view_embedder);
48-
#else
49-
FML_LOG(FATAL) << "Vulkan not enabled in this build";
50-
return nullptr;
51-
#endif // SHELL_ENABLE_VULKAN
26+
return CreateGL(delegate, task_runners, vsync_clock, create_vsync_waiter,
27+
shell_test_external_view_embedder,
28+
is_gpu_disabled_sync_switch);
5229
case BackendType::kMetalBackend:
53-
#ifdef SHELL_ENABLE_METAL
54-
return std::make_unique<ShellTestPlatformViewMetal>(
30+
return CreateMetal(delegate, task_runners, vsync_clock,
31+
create_vsync_waiter, shell_test_external_view_embedder,
32+
is_gpu_disabled_sync_switch);
33+
case BackendType::kVulkanBackend:
34+
return CreateVulkan(
5535
delegate, task_runners, vsync_clock, create_vsync_waiter,
5636
shell_test_external_view_embedder, is_gpu_disabled_sync_switch);
57-
#else
58-
FML_LOG(FATAL) << "Metal not enabled in this build";
59-
return nullptr;
60-
#endif // SHELL_ENABLE_METAL
6137
}
6238
}
6339

40+
#ifndef SHELL_ENABLE_GL
41+
std::unique_ptr<ShellTestPlatformView> ShellTestPlatformView::CreateGL(
42+
PlatformView::Delegate& delegate,
43+
const TaskRunners& task_runners,
44+
const std::shared_ptr<ShellTestVsyncClock>& vsync_clock,
45+
const CreateVsyncWaiter& create_vsync_waiter,
46+
const std::shared_ptr<ShellTestExternalViewEmbedder>&
47+
shell_test_external_view_embedder,
48+
const std::shared_ptr<const fml::SyncSwitch>& is_gpu_disabled_sync_switch) {
49+
FML_LOG(FATAL) << "OpenGL backend not enabled in this build";
50+
return nullptr;
51+
}
52+
#endif // SHELL_ENABLE_GL
53+
#ifndef SHELL_ENABLE_METAL
54+
std::unique_ptr<ShellTestPlatformView> ShellTestPlatformView::CreateMetal(
55+
PlatformView::Delegate& delegate,
56+
const TaskRunners& task_runners,
57+
const std::shared_ptr<ShellTestVsyncClock>& vsync_clock,
58+
const CreateVsyncWaiter& create_vsync_waiter,
59+
const std::shared_ptr<ShellTestExternalViewEmbedder>&
60+
shell_test_external_view_embedder,
61+
const std::shared_ptr<const fml::SyncSwitch>& is_gpu_disabled_sync_switch) {
62+
FML_LOG(FATAL) << "Metal backend not enabled in this build";
63+
return nullptr;
64+
}
65+
#endif // SHELL_ENABLE_METAL
66+
#ifndef SHELL_ENABLE_VULKAN
67+
std::unique_ptr<ShellTestPlatformView> ShellTestPlatformView::CreateVulkan(
68+
PlatformView::Delegate& delegate,
69+
const TaskRunners& task_runners,
70+
const std::shared_ptr<ShellTestVsyncClock>& vsync_clock,
71+
const CreateVsyncWaiter& create_vsync_waiter,
72+
const std::shared_ptr<ShellTestExternalViewEmbedder>&
73+
shell_test_external_view_embedder,
74+
const std::shared_ptr<const fml::SyncSwitch>& is_gpu_disabled_sync_switch) {
75+
FML_LOG(FATAL) << "Vulkan backend not enabled in this build";
76+
return nullptr;
77+
}
78+
#endif // SHELL_ENABLE_VULKAN
79+
6480
ShellTestPlatformViewBuilder::ShellTestPlatformViewBuilder(Config config)
6581
: config_(std::move(config)) {}
6682

@@ -90,5 +106,4 @@ std::unique_ptr<PlatformView> ShellTestPlatformViewBuilder::operator()(
90106
);
91107
}
92108

93-
} // namespace testing
94-
} // namespace flutter
109+
} // namespace flutter::testing

engine/src/flutter/shell/common/shell_test_platform_view.h

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
#include "flutter/shell/common/shell_test_external_view_embedder.h"
1212
#include "flutter/shell/common/vsync_waiters_test.h"
1313

14-
namespace flutter {
15-
namespace testing {
14+
namespace flutter::testing {
1615

1716
class ShellTestPlatformView : public PlatformView {
1817
public:
@@ -53,6 +52,34 @@ class ShellTestPlatformView : public PlatformView {
5352
const TaskRunners& task_runners)
5453
: PlatformView(delegate, task_runners) {}
5554

55+
static std::unique_ptr<ShellTestPlatformView> CreateGL(
56+
PlatformView::Delegate& delegate,
57+
const TaskRunners& task_runners,
58+
const std::shared_ptr<ShellTestVsyncClock>& vsync_clock,
59+
const CreateVsyncWaiter& create_vsync_waiter,
60+
const std::shared_ptr<ShellTestExternalViewEmbedder>&
61+
shell_test_external_view_embedder,
62+
const std::shared_ptr<const fml::SyncSwitch>&
63+
is_gpu_disabled_sync_switch);
64+
static std::unique_ptr<ShellTestPlatformView> CreateMetal(
65+
PlatformView::Delegate& delegate,
66+
const TaskRunners& task_runners,
67+
const std::shared_ptr<ShellTestVsyncClock>& vsync_clock,
68+
const CreateVsyncWaiter& create_vsync_waiter,
69+
const std::shared_ptr<ShellTestExternalViewEmbedder>&
70+
shell_test_external_view_embedder,
71+
const std::shared_ptr<const fml::SyncSwitch>&
72+
is_gpu_disabled_sync_switch);
73+
static std::unique_ptr<ShellTestPlatformView> CreateVulkan(
74+
PlatformView::Delegate& delegate,
75+
const TaskRunners& task_runners,
76+
const std::shared_ptr<ShellTestVsyncClock>& vsync_clock,
77+
const CreateVsyncWaiter& create_vsync_waiter,
78+
const std::shared_ptr<ShellTestExternalViewEmbedder>&
79+
shell_test_external_view_embedder,
80+
const std::shared_ptr<const fml::SyncSwitch>&
81+
is_gpu_disabled_sync_switch);
82+
5683
FML_DISALLOW_COPY_AND_ASSIGN(ShellTestPlatformView);
5784
};
5885

@@ -77,7 +104,6 @@ class ShellTestPlatformViewBuilder {
77104
Config config_;
78105
};
79106

80-
} // namespace testing
81-
} // namespace flutter
107+
} // namespace flutter::testing
82108

83109
#endif // FLUTTER_SHELL_COMMON_SHELL_TEST_PLATFORM_VIEW_H_

engine/src/flutter/shell/common/shell_test_platform_view_gl.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@ static std::vector<std::shared_ptr<fml::Mapping>> ShaderLibraryMappings() {
2222
};
2323
}
2424

25+
std::unique_ptr<ShellTestPlatformView> ShellTestPlatformView::CreateGL(
26+
PlatformView::Delegate& delegate,
27+
const TaskRunners& task_runners,
28+
const std::shared_ptr<ShellTestVsyncClock>& vsync_clock,
29+
const CreateVsyncWaiter& create_vsync_waiter,
30+
const std::shared_ptr<ShellTestExternalViewEmbedder>&
31+
shell_test_external_view_embedder,
32+
const std::shared_ptr<const fml::SyncSwitch>& is_gpu_disabled_sync_switch) {
33+
return std::make_unique<ShellTestPlatformViewGL>(
34+
delegate, task_runners, vsync_clock, create_vsync_waiter,
35+
shell_test_external_view_embedder);
36+
}
37+
2538
ShellTestPlatformViewGL::ShellTestPlatformViewGL(
2639
PlatformView::Delegate& delegate,
2740
const TaskRunners& task_runners,

engine/src/flutter/shell/common/shell_test_platform_view_metal.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55
#ifndef FLUTTER_SHELL_COMMON_SHELL_TEST_PLATFORM_VIEW_METAL_H_
66
#define FLUTTER_SHELL_COMMON_SHELL_TEST_PLATFORM_VIEW_METAL_H_
77

8-
#include "flutter/fml/macros.h"
98
#include "flutter/shell/common/shell_test_platform_view.h"
10-
#include "flutter/shell/gpu/gpu_surface_metal_delegate.h"
119

12-
namespace flutter {
13-
namespace testing {
10+
#import <Metal/Metal.h>
11+
12+
#include "flutter/fml/macros.h"
13+
#include "flutter/shell/gpu/gpu_surface_metal_delegate.h"
14+
#include "flutter/shell/platform/darwin/graphics/FlutterDarwinContextMetalImpeller.h"
15+
#include "flutter/shell/platform/darwin/graphics/FlutterDarwinContextMetalSkia.h"
1416

15-
struct DarwinContextMetal;
17+
namespace flutter::testing {
1618

1719
class ShellTestPlatformViewMetal final : public ShellTestPlatformView,
1820
public GPUSurfaceMetalDelegate {
@@ -30,7 +32,9 @@ class ShellTestPlatformViewMetal final : public ShellTestPlatformView,
3032
virtual ~ShellTestPlatformViewMetal() override;
3133

3234
private:
33-
const std::unique_ptr<DarwinContextMetal> metal_context_;
35+
FlutterDarwinContextMetalSkia* skia_context_;
36+
FlutterDarwinContextMetalImpeller* impeller_context_;
37+
id<MTLTexture> offscreen_texture_;
3438
const CreateVsyncWaiter create_vsync_waiter_;
3539
const std::shared_ptr<ShellTestVsyncClock> vsync_clock_;
3640
const std::shared_ptr<ShellTestExternalViewEmbedder>
@@ -70,7 +74,6 @@ class ShellTestPlatformViewMetal final : public ShellTestPlatformView,
7074
FML_DISALLOW_COPY_AND_ASSIGN(ShellTestPlatformViewMetal);
7175
};
7276

73-
} // namespace testing
74-
} // namespace flutter
77+
} // namespace flutter::testing
7578

7679
#endif // FLUTTER_SHELL_COMMON_SHELL_TEST_PLATFORM_VIEW_METAL_H_

engine/src/flutter/shell/common/shell_test_platform_view_metal.mm

Lines changed: 35 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,52 +4,25 @@
44

55
#include "flutter/shell/common/shell_test_platform_view_metal.h"
66

7-
#import <Metal/Metal.h>
8-
97
#include <utility>
108

119
#include "flutter/shell/gpu/gpu_surface_metal_impeller.h"
1210
#include "flutter/shell/gpu/gpu_surface_metal_skia.h"
13-
#include "flutter/shell/platform/darwin/graphics/FlutterDarwinContextMetalImpeller.h"
14-
#include "flutter/shell/platform/darwin/graphics/FlutterDarwinContextMetalSkia.h"
1511

1612
FLUTTER_ASSERT_ARC
1713

18-
namespace flutter {
19-
namespace testing {
20-
21-
// This is out of the header so that shell_test_platform_view_metal.h can be included in
22-
// non-Objective-C TUs.
23-
struct DarwinContextMetal {
24-
FlutterDarwinContextMetalSkia* skia_context;
25-
FlutterDarwinContextMetalImpeller* impeller_context;
26-
id<MTLTexture> offscreen_texture;
27-
};
14+
namespace flutter::testing {
2815

29-
static std::unique_ptr<DarwinContextMetal> CreateDarwinContext(
30-
bool impeller,
16+
std::unique_ptr<ShellTestPlatformView> ShellTestPlatformView::CreateMetal(
17+
PlatformView::Delegate& delegate,
18+
const TaskRunners& task_runners,
19+
const std::shared_ptr<ShellTestVsyncClock>& vsync_clock,
20+
const CreateVsyncWaiter& create_vsync_waiter,
21+
const std::shared_ptr<ShellTestExternalViewEmbedder>& shell_test_external_view_embedder,
3122
const std::shared_ptr<const fml::SyncSwitch>& is_gpu_disabled_sync_switch) {
32-
FlutterDarwinContextMetalSkia* skia_context = nil;
33-
FlutterDarwinContextMetalImpeller* impeller_context = nil;
34-
id<MTLDevice> device = nil;
35-
if (impeller) {
36-
impeller_context = [[FlutterDarwinContextMetalImpeller alloc] init:is_gpu_disabled_sync_switch];
37-
FML_CHECK(impeller_context.context);
38-
device = impeller_context.context->GetMTLDevice();
39-
} else {
40-
skia_context = [[FlutterDarwinContextMetalSkia alloc] initWithDefaultMTLDevice];
41-
FML_CHECK(skia_context.mainContext);
42-
device = skia_context.device;
43-
}
44-
auto descriptor =
45-
[MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatBGRA8Unorm
46-
width:800
47-
height:600
48-
mipmapped:NO];
49-
descriptor.usage = MTLTextureUsageRenderTarget | MTLTextureUsageShaderRead;
50-
id<MTLTexture> offscreen_texture = [device newTextureWithDescriptor:descriptor];
51-
return std::make_unique<DarwinContextMetal>(
52-
DarwinContextMetal{skia_context, impeller_context, offscreen_texture});
23+
return std::make_unique<ShellTestPlatformViewMetal>(
24+
delegate, task_runners, vsync_clock, create_vsync_waiter, shell_test_external_view_embedder,
25+
is_gpu_disabled_sync_switch);
5326
}
5427

5528
ShellTestPlatformViewMetal::ShellTestPlatformViewMetal(
@@ -61,11 +34,28 @@
6134
const std::shared_ptr<const fml::SyncSwitch>& is_gpu_disabled_sync_switch)
6235
: ShellTestPlatformView(delegate, task_runners),
6336
GPUSurfaceMetalDelegate(MTLRenderTargetType::kMTLTexture),
64-
metal_context_(
65-
CreateDarwinContext(GetSettings().enable_impeller, is_gpu_disabled_sync_switch)),
6637
create_vsync_waiter_(std::move(create_vsync_waiter)),
6738
vsync_clock_(std::move(vsync_clock)),
68-
shell_test_external_view_embedder_(std::move(shell_test_external_view_embedder)) {}
39+
shell_test_external_view_embedder_(std::move(shell_test_external_view_embedder)) {
40+
id<MTLDevice> device = nil;
41+
if (GetSettings().enable_impeller) {
42+
impeller_context_ =
43+
[[FlutterDarwinContextMetalImpeller alloc] init:is_gpu_disabled_sync_switch];
44+
FML_CHECK(impeller_context_.context);
45+
device = impeller_context_.context->GetMTLDevice();
46+
} else {
47+
skia_context_ = [[FlutterDarwinContextMetalSkia alloc] initWithDefaultMTLDevice];
48+
FML_CHECK(skia_context_.mainContext);
49+
device = skia_context_.device;
50+
}
51+
auto descriptor =
52+
[MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatBGRA8Unorm
53+
width:800
54+
height:600
55+
mipmapped:NO];
56+
descriptor.usage = MTLTextureUsageRenderTarget | MTLTextureUsageShaderRead;
57+
offscreen_texture_ = [device newTextureWithDescriptor:descriptor];
58+
}
6959

7060
ShellTestPlatformViewMetal::~ShellTestPlatformViewMetal() = default;
7161

@@ -93,16 +83,16 @@
9383
// |PlatformView|
9484
std::unique_ptr<Surface> ShellTestPlatformViewMetal::CreateRenderingSurface() {
9585
if (GetSettings().enable_impeller) {
96-
auto context = metal_context_->impeller_context.context;
86+
auto context = impeller_context_.context;
9787
return std::make_unique<GPUSurfaceMetalImpeller>(
9888
this, std::make_shared<impeller::AiksContext>(context, nullptr));
9989
}
100-
return std::make_unique<GPUSurfaceMetalSkia>(this, metal_context_->skia_context.mainContext);
90+
return std::make_unique<GPUSurfaceMetalSkia>(this, skia_context_.mainContext);
10191
}
10292

10393
// |PlatformView|
10494
std::shared_ptr<impeller::Context> ShellTestPlatformViewMetal::GetImpellerContext() const {
105-
return metal_context_->impeller_context.context;
95+
return impeller_context_.context;
10696
}
10797

10898
// |GPUSurfaceMetalDelegate|
@@ -123,7 +113,7 @@
123113
GPUMTLTextureInfo ShellTestPlatformViewMetal::GetMTLTexture(const SkISize& frame_info) const {
124114
return {
125115
.texture_id = 0,
126-
.texture = (__bridge GPUMTLTextureHandle)metal_context_->offscreen_texture,
116+
.texture = (__bridge GPUMTLTextureHandle)offscreen_texture_,
127117
};
128118
}
129119

@@ -133,5 +123,4 @@
133123
return true;
134124
}
135125

136-
} // namespace testing
137-
} // namespace flutter
126+
} // namespace flutter::testing

engine/src/flutter/shell/common/shell_test_platform_view_vulkan.cc

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,20 @@
2323
#include "flutter/vulkan/swiftshader_path.h"
2424
#endif
2525

26-
namespace flutter {
27-
namespace testing {
26+
namespace flutter::testing {
27+
28+
std::unique_ptr<ShellTestPlatformView> ShellTestPlatformView::CreateVulkan(
29+
PlatformView::Delegate& delegate,
30+
const TaskRunners& task_runners,
31+
const std::shared_ptr<ShellTestVsyncClock>& vsync_clock,
32+
const CreateVsyncWaiter& create_vsync_waiter,
33+
const std::shared_ptr<ShellTestExternalViewEmbedder>&
34+
shell_test_external_view_embedder,
35+
const std::shared_ptr<const fml::SyncSwitch>& is_gpu_disabled_sync_switch) {
36+
return std::make_unique<ShellTestPlatformViewVulkan>(
37+
delegate, task_runners, vsync_clock, create_vsync_waiter,
38+
shell_test_external_view_embedder);
39+
}
2840

2941
ShellTestPlatformViewVulkan::ShellTestPlatformViewVulkan(
3042
PlatformView::Delegate& delegate,
@@ -232,5 +244,4 @@ SkMatrix ShellTestPlatformViewVulkan::OffScreenSurface::GetRootTransformation()
232244
return matrix;
233245
}
234246

235-
} // namespace testing
236-
} // namespace flutter
247+
} // namespace flutter::testing

0 commit comments

Comments
 (0)