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

Commit 54d9b3d

Browse files
committed
Extract backend-specific code in ShellTestPlatformView
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/flutter#158998 Issue: flutter/flutter#137801
1 parent 03eb3db commit 54d9b3d

7 files changed

+160
-91
lines changed

shell/common/shell_test_platform_view.cc

Lines changed: 54 additions & 25 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
PlatformView::Delegate& delegate,
@@ -34,29 +25,68 @@ std::unique_ptr<ShellTestPlatformView> ShellTestPlatformView::Create(
3425
case BackendType::kDefaultBackend:
3526
#ifdef SHELL_ENABLE_GL
3627
case BackendType::kGLBackend:
37-
return std::make_unique<ShellTestPlatformViewGL>(
38-
delegate, task_runners, vsync_clock, create_vsync_waiter,
39-
shell_test_external_view_embedder);
28+
return CreateGL(delegate, task_runners, vsync_clock, create_vsync_waiter,
29+
shell_test_external_view_embedder,
30+
is_gpu_disabled_sync_switch);
4031
#endif // SHELL_ENABLE_GL
41-
#ifdef SHELL_ENABLE_VULKAN
42-
case BackendType::kVulkanBackend:
43-
return std::make_unique<ShellTestPlatformViewVulkan>(
44-
delegate, task_runners, vsync_clock, create_vsync_waiter,
45-
shell_test_external_view_embedder);
46-
#endif // SHELL_ENABLE_VULKAN
4732
#ifdef SHELL_ENABLE_METAL
4833
case BackendType::kMetalBackend:
49-
return std::make_unique<ShellTestPlatformViewMetal>(
34+
return CreateMetal(delegate, task_runners, vsync_clock,
35+
create_vsync_waiter, shell_test_external_view_embedder,
36+
is_gpu_disabled_sync_switch);
37+
#endif // SHELL_ENABLE_METAL
38+
#ifdef SHELL_ENABLE_VULKAN
39+
case BackendType::kVulkanBackend:
40+
return CreateVulkan(
5041
delegate, task_runners, vsync_clock, create_vsync_waiter,
5142
shell_test_external_view_embedder, is_gpu_disabled_sync_switch);
52-
#endif // SHELL_ENABLE_METAL
53-
43+
#endif // SHELL_ENABLE_VULKAN
5444
default:
5545
FML_LOG(FATAL) << "No backends supported for ShellTestPlatformView";
5646
return nullptr;
5747
}
5848
}
5949

50+
#ifndef SHELL_ENABLE_GL
51+
std::unique_ptr<ShellTestPlatformView> ShellTestPlatformView::CreateGL(
52+
PlatformView::Delegate& delegate,
53+
const TaskRunners& task_runners,
54+
const std::shared_ptr<ShellTestVsyncClock>& vsync_clock,
55+
const CreateVsyncWaiter& create_vsync_waiter,
56+
const std::shared_ptr<ShellTestExternalViewEmbedder>&
57+
shell_test_external_view_embedder,
58+
const std::shared_ptr<const fml::SyncSwitch>& is_gpu_disabled_sync_switch) {
59+
FML_LOG(FATAL) << "OpenGL backend not enabled in this build";
60+
return nullptr;
61+
}
62+
#endif // SHELL_ENABLE_GL
63+
#ifndef SHELL_ENABLE_METAL
64+
std::unique_ptr<ShellTestPlatformView> 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>& is_gpu_disabled_sync_switch) {
72+
FML_LOG(FATAL) << "Metal backend not enabled in this build";
73+
return nullptr;
74+
}
75+
#endif // SHELL_ENABLE_METAL
76+
#ifndef SHELL_ENABLE_VULKAN
77+
std::unique_ptr<ShellTestPlatformView> ShellTestPlatformView::CreateVulkan(
78+
PlatformView::Delegate& delegate,
79+
const TaskRunners& task_runners,
80+
const std::shared_ptr<ShellTestVsyncClock>& vsync_clock,
81+
const CreateVsyncWaiter& create_vsync_waiter,
82+
const std::shared_ptr<ShellTestExternalViewEmbedder>&
83+
shell_test_external_view_embedder,
84+
const std::shared_ptr<const fml::SyncSwitch>& is_gpu_disabled_sync_switch) {
85+
FML_LOG(FATAL) << "Vulkan backend not enabled in this build";
86+
return nullptr;
87+
}
88+
#endif // SHELL_ENABLE_VULKAN
89+
6090
ShellTestPlatformViewBuilder::ShellTestPlatformViewBuilder(Config config)
6191
: config_(std::move(config)) {}
6292

@@ -86,5 +116,4 @@ std::unique_ptr<PlatformView> ShellTestPlatformViewBuilder::operator()(
86116
);
87117
}
88118

89-
} // namespace testing
90-
} // namespace flutter
119+
} // namespace flutter::testing

shell/common/shell_test_platform_view.h

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

12-
namespace flutter {
13-
namespace testing {
12+
namespace flutter::testing {
1413

1514
class ShellTestPlatformView : public PlatformView {
1615
public:
@@ -39,6 +38,34 @@ class ShellTestPlatformView : public PlatformView {
3938
const TaskRunners& task_runners)
4039
: PlatformView(delegate, task_runners) {}
4140

41+
static std::unique_ptr<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>&
49+
is_gpu_disabled_sync_switch);
50+
static std::unique_ptr<ShellTestPlatformView> CreateMetal(
51+
PlatformView::Delegate& delegate,
52+
const TaskRunners& task_runners,
53+
const std::shared_ptr<ShellTestVsyncClock>& vsync_clock,
54+
const CreateVsyncWaiter& create_vsync_waiter,
55+
const std::shared_ptr<ShellTestExternalViewEmbedder>&
56+
shell_test_external_view_embedder,
57+
const std::shared_ptr<const fml::SyncSwitch>&
58+
is_gpu_disabled_sync_switch);
59+
static std::unique_ptr<ShellTestPlatformView> CreateVulkan(
60+
PlatformView::Delegate& delegate,
61+
const TaskRunners& task_runners,
62+
const std::shared_ptr<ShellTestVsyncClock>& vsync_clock,
63+
const CreateVsyncWaiter& create_vsync_waiter,
64+
const std::shared_ptr<ShellTestExternalViewEmbedder>&
65+
shell_test_external_view_embedder,
66+
const std::shared_ptr<const fml::SyncSwitch>&
67+
is_gpu_disabled_sync_switch);
68+
4269
FML_DISALLOW_COPY_AND_ASSIGN(ShellTestPlatformView);
4370
};
4471

@@ -63,7 +90,6 @@ class ShellTestPlatformViewBuilder {
6390
Config config_;
6491
};
6592

66-
} // namespace testing
67-
} // namespace flutter
93+
} // namespace flutter::testing
6894

6995
#endif // FLUTTER_SHELL_COMMON_SHELL_TEST_PLATFORM_VIEW_H_

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,

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_

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

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)