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

Commit 2e3d02a

Browse files
committed
Enabled metal on ios simulator
1 parent ace381d commit 2e3d02a

File tree

7 files changed

+51
-7
lines changed

7 files changed

+51
-7
lines changed

shell/gpu/gpu_surface_metal.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717

1818
namespace flutter {
1919

20+
#if TARGET_IPHONE_SIMULATOR
21+
class API_AVAILABLE(ios(13.0)) GPUSurfaceMetal : public Surface {
22+
#else
2023
class GPUSurfaceMetal : public Surface {
24+
#endif // TARGET_IPHONE_SIMULATOR
2125
public:
2226
GPUSurfaceMetal(GPUSurfaceDelegate* delegate,
2327
fml::scoped_nsobject<CAMetalLayer> layer,

shell/gpu/gpu_surface_metal.mm

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515

1616
namespace flutter {
1717

18+
#if TARGET_IPHONE_SIMULATOR
19+
API_AVAILABLE(ios(13.0))
20+
#endif // TARGET_IPHONE_SIMULATOR
1821
GPUSurfaceMetal::GPUSurfaceMetal(GPUSurfaceDelegate* delegate,
1922
fml::scoped_nsobject<CAMetalLayer> layer,
2023
sk_sp<GrContext> context,
@@ -40,6 +43,9 @@
4043

4144
// |Surface|
4245
std::unique_ptr<SurfaceFrame> GPUSurfaceMetal::AcquireFrame(const SkISize& frame_size) {
46+
#if TARGET_IPHONE_SIMULATOR
47+
if (@available(iOS 13.0, *)) {
48+
#endif // TARGET_IPHONE_SIMULATOR
4349
if (!IsValid()) {
4450
FML_LOG(ERROR) << "Metal surface was invalid.";
4551
return nullptr;
@@ -97,6 +103,10 @@
97103
};
98104

99105
return std::make_unique<SurfaceFrame>(std::move(surface), true, submit_callback);
106+
#if TARGET_IPHONE_SIMULATOR
107+
}
108+
return nil;
109+
#endif // TARGET_IPHONE_SIMULATOR
100110
}
101111

102112
// |Surface|

shell/platform/darwin/ios/ios_surface.mm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ bool IsIosEmbeddedViewsPreviewEnabled() {
3939
}
4040

4141
#if FLUTTER_SHELL_ENABLE_METAL
42+
#if TARGET_IPHONE_SIMULATOR
43+
if (@available(iOS 13.0, *)) {
44+
#endif // TARGET_IPHONE_SIMULATOR
4245
if ([layer.get() isKindOfClass:[CAMetalLayer class]]) {
4346
return std::make_unique<IOSSurfaceMetal>(
4447
fml::scoped_nsobject<CAMetalLayer>(
@@ -47,6 +50,9 @@ bool IsIosEmbeddedViewsPreviewEnabled() {
4750
platform_views_controller // platform views controller
4851
);
4952
}
53+
#if TARGET_IPHONE_SIMULATOR
54+
}
55+
#endif // TARGET_IPHONE_SIMULATOR
5056
#endif // FLUTTER_SHELL_ENABLE_METAL
5157

5258
return std::make_unique<IOSSurfaceSoftware>(

shell/platform/darwin/ios/ios_surface_metal.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313

1414
namespace flutter {
1515

16+
#if TARGET_IPHONE_SIMULATOR
17+
class API_AVAILABLE(ios(13.0)) IOSSurfaceMetal final : public IOSSurface, public GPUSurfaceDelegate {
18+
#else
1619
class IOSSurfaceMetal final : public IOSSurface, public GPUSurfaceDelegate {
20+
#endif // TARGET_IPHONE_SIMULATOR
1721
public:
1822
IOSSurfaceMetal(fml::scoped_nsobject<CAMetalLayer> layer,
1923
std::shared_ptr<IOSContext> context,

shell/platform/darwin/ios/ios_surface_metal.mm

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
return reinterpret_cast<IOSContextMetal*>(context.get());
1414
}
1515

16+
17+
#if TARGET_IPHONE_SIMULATOR
18+
API_AVAILABLE(ios(13.0))
19+
#endif // TARGET_IPHONE_SIMULATOR
1620
IOSSurfaceMetal::IOSSurfaceMetal(fml::scoped_nsobject<CAMetalLayer> layer,
1721
std::shared_ptr<IOSContext> context,
1822
FlutterPlatformViewsController* platform_views_controller)

shell/platform/darwin/ios/rendering_api_selection.mm

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,30 @@ bool ShouldUseMetalRenderer() {
2323
bool ios_version_supports_metal = false;
2424
if (@available(iOS 10.0, *)) {
2525
auto device = MTLCreateSystemDefaultDevice();
26-
ios_version_supports_metal = [device supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily1_v3];
26+
// We need to check if the device is here since an ios 13 simulator running on an older version
27+
// of macos (below 10.15) will not actually allow metal to be used
28+
if (device != nil) {
29+
ios_version_supports_metal = [device supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily1_v3];
30+
}
2731
}
2832
return ios_version_supports_metal;
2933
}
3034
#endif // FLUTTER_SHELL_ENABLE_METAL
3135

3236
IOSRenderingAPI GetRenderingAPIForProcess() {
33-
#if TARGET_IPHONE_SIMULATOR
34-
return IOSRenderingAPI::kSoftware;
35-
#endif // TARGET_IPHONE_SIMULATOR
3637

3738
#if FLUTTER_SHELL_ENABLE_METAL
3839
static bool should_use_metal = ShouldUseMetalRenderer();
3940
if (should_use_metal) {
40-
return IOSRenderingAPI::kMetal;
41+
#if TARGET_IPHONE_SIMULATOR
42+
if (@available(iOS 13.0, *)) {
43+
return IOSRenderingAPI::kMetal;
44+
} else {
45+
return IOSRenderingAPI::kSoftware;
46+
}
47+
#else
48+
return IOSRenderingAPI::kMetal;
49+
#endif // TARGET_IPHONE_SIMULATOR
4150
}
4251
#endif // FLUTTER_SHELL_ENABLE_METAL
4352
return IOSRenderingAPI::kOpenGLES;
@@ -49,9 +58,16 @@ Class GetCoreAnimationLayerClassForRenderingAPI(IOSRenderingAPI rendering_api) {
4958
return [CALayer class];
5059
case IOSRenderingAPI::kOpenGLES:
5160
return [CAEAGLLayer class];
52-
#if !TARGET_IPHONE_SIMULATOR
5361
case IOSRenderingAPI::kMetal:
62+
#if !TARGET_IPHONE_SIMULATOR
5463
return [CAMetalLayer class];
64+
#else
65+
// This will always be true since we filter out this case when checking if the device
66+
// supports metal
67+
if (@available(iOS 13.0, *)) {
68+
return [CAMetalLayer class];
69+
}
70+
break;
5571
#endif // !TARGET_IPHONE_SIMULATOR
5672
default:
5773
break;

tools/gn

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def to_gn_args(args):
217217
gn_args['goma_dir'] = None
218218

219219
# Enable Metal on non-simulator iOS builds.
220-
if args.target_os == 'ios' and not args.simulator:
220+
if args.target_os == 'ios':
221221
gn_args['skia_use_metal'] = True
222222
gn_args['shell_enable_metal'] = True
223223
# Bitcode enabled builds using the current version of the toolchain leak

0 commit comments

Comments
 (0)