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

Commit a4fff2f

Browse files
committed
Enabled metal on ios simulator
1 parent 11c6a18 commit a4fff2f

File tree

5 files changed

+44
-14
lines changed

5 files changed

+44
-14
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/platform/darwin/ios/ios_surface.mm

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,20 @@ bool IsIosEmbeddedViewsPreviewEnabled() {
3939
}
4040

4141
#if FLUTTER_SHELL_ENABLE_METAL
42-
if ([layer.get() isKindOfClass:[CAMetalLayer class]]) {
43-
return std::make_unique<IOSSurfaceMetal>(
44-
fml::scoped_nsobject<CAMetalLayer>(
45-
reinterpret_cast<CAMetalLayer*>([layer.get() retain])), // Metal layer
46-
std::move(context), // context
47-
platform_views_controller // platform views controller
48-
);
42+
#if TARGET_IPHONE_SIMULATOR
43+
if (@available(iOS 13.0, *)) {
44+
#endif // TARGET_IPHONE_SIMULATOR
45+
if ([layer.get() isKindOfClass:[CAMetalLayer class]]) {
46+
return std::make_unique<IOSSurfaceMetal>(
47+
fml::scoped_nsobject<CAMetalLayer>(
48+
reinterpret_cast<CAMetalLayer*>([layer.get() retain])), // Metal layer
49+
std::move(context), // context
50+
platform_views_controller // platform views controller
51+
);
52+
}
53+
#if TARGET_IPHONE_SIMULATOR
4954
}
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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@
1313

1414
namespace flutter {
1515

16+
#if TARGET_IPHONE_SIMULATOR
17+
class API_AVAILABLE(ios(13.0)) IOSSurfaceMetal final : public IOSSurface,
18+
public GPUSurfaceDelegate {
19+
#else
1620
class IOSSurfaceMetal final : public IOSSurface, public GPUSurfaceDelegate {
21+
#endif // TARGET_IPHONE_SIMULATOR
1722
public:
1823
IOSSurfaceMetal(fml::scoped_nsobject<CAMetalLayer> layer,
1924
std::shared_ptr<IOSContext> context,

shell/platform/darwin/ios/rendering_api_selection.mm

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,34 @@ bool ShouldUseMetalRenderer() {
2121
// past iOS 10.0. The processor was selected as it is the first version at which Metal was
2222
// supported. The iOS version floor was selected due to the availability of features used by Skia.
2323
bool ios_version_supports_metal = false;
24+
#if TARGET_IPHONE_SIMULATOR
25+
if (@available(iOS 13.0, *)) {
26+
#else
2427
if (@available(iOS 10.0, *)) {
28+
#endif // TARGET_IPHONE_SIMULATOR
2529
auto device = MTLCreateSystemDefaultDevice();
26-
ios_version_supports_metal = [device supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily1_v3];
30+
// We need to check if the device is here since an ios 13 simulator running on an older version
31+
// of macos (below 10.15) will not actually allow metal to be used.
32+
if (device != nil) {
33+
ios_version_supports_metal = [device supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily1_v3];
34+
}
2735
}
2836
return ios_version_supports_metal;
2937
}
3038
#endif // FLUTTER_SHELL_ENABLE_METAL
3139

3240
IOSRenderingAPI GetRenderingAPIForProcess() {
33-
#if TARGET_IPHONE_SIMULATOR
34-
return IOSRenderingAPI::kSoftware;
35-
#endif // TARGET_IPHONE_SIMULATOR
36-
3741
#if FLUTTER_SHELL_ENABLE_METAL
3842
static bool should_use_metal = ShouldUseMetalRenderer();
3943
if (should_use_metal) {
4044
return IOSRenderingAPI::kMetal;
4145
}
4246
#endif // FLUTTER_SHELL_ENABLE_METAL
47+
#if TARGET_IPHONE_SIMULATOR
48+
return IOSRenderingAPI::kSoftware;
49+
#else
4350
return IOSRenderingAPI::kOpenGLES;
51+
#endif // TARGET_IPHONE_SIMULATOR
4452
}
4553

4654
Class GetCoreAnimationLayerClassForRenderingAPI(IOSRenderingAPI rendering_api) {
@@ -49,8 +57,15 @@ Class GetCoreAnimationLayerClassForRenderingAPI(IOSRenderingAPI rendering_api) {
4957
return [CALayer class];
5058
case IOSRenderingAPI::kOpenGLES:
5159
return [CAEAGLLayer class];
52-
#if !TARGET_IPHONE_SIMULATOR
5360
case IOSRenderingAPI::kMetal:
61+
#if TARGET_IPHONE_SIMULATOR
62+
// This will always be true since we filter out this case when checking if the device
63+
// supports metal.
64+
if (@available(iOS 13.0, *)) {
65+
return [CAMetalLayer class];
66+
}
67+
break;
68+
#else
5469
return [CAMetalLayer class];
5570
#endif // !TARGET_IPHONE_SIMULATOR
5671
default:

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)