Skip to content

Commit 263ab8b

Browse files
iskakaushikgspencergoog
authored andcommitted
Add support for rendering using Metal on macOS (flutter#23599)
1 parent 20cdf05 commit 263ab8b

30 files changed

+943
-164
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,6 +1062,8 @@ FILE: ../../../flutter/shell/platform/darwin/macos/framework/Headers/FlutterPlug
10621062
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Headers/FlutterViewController.h
10631063
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Info.plist
10641064
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterAppDelegate.mm
1065+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterBackingStore.h
1066+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterBackingStore.mm
10651067
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterBackingStoreData.h
10661068
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterBackingStoreData.mm
10671069
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterDartProject.mm
@@ -1080,11 +1082,18 @@ FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterIOSur
10801082
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterIOSurfaceHolder.mm
10811083
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterIntermediateKeyResponder.h
10821084
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterIntermediateKeyResponder.mm
1085+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterMetalRenderer.h
1086+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterMetalRenderer.mm
1087+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterMetalRendererTest.mm
1088+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterMetalSurfaceManagerTest.mm
10831089
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterMouseCursorPlugin.h
10841090
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterMouseCursorPlugin.mm
10851091
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterOpenGLRenderer.h
10861092
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterOpenGLRenderer.mm
10871093
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterOpenGLRendererTest.mm
1094+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterRenderer.h
1095+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterResizableBackingStoreProvider.h
1096+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterResizableBackingStoreProvider.mm
10881097
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizer.h
10891098
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizer.mm
10901099
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterSurfaceManager.h

shell/platform/darwin/graphics/FlutterDarwinContextMetal.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,19 @@ NS_ASSUME_NONNULL_BEGIN
2626
/**
2727
* Initializes a FlutterDarwinContextMetal with provided MTLDevice and MTLCommandQueue.
2828
*/
29-
- (instancetype)initWithMTLDevice:(id<MTLDevice>)mtlDevice
29+
- (instancetype)initWithMTLDevice:(id<MTLDevice>)device
3030
commandQueue:(id<MTLCommandQueue>)commandQueue;
3131

3232
/**
3333
* MTLDevice that is backing this context.s
3434
*/
35-
@property(nonatomic, readonly) id<MTLDevice> mtlDevice;
35+
@property(nonatomic, readonly) id<MTLDevice> device;
3636

3737
/**
38-
* MTLCommandQueue that is acquired from the `mtlDevice`. This queue is used both for rendering and
38+
* MTLCommandQueue that is acquired from the `device`. This queue is used both for rendering and
3939
* resource related commands.
4040
*/
41-
@property(nonatomic, readonly) id<MTLCommandQueue> mtlCommandQueue;
41+
@property(nonatomic, readonly) id<MTLCommandQueue> commandQueue;
4242

4343
/**
4444
* Skia GrContext that is used for rendering.

shell/platform/darwin/graphics/FlutterDarwinContextMetal.mm

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,40 +21,40 @@ static GrContextOptions CreateMetalGrContextOptions() {
2121
@implementation FlutterDarwinContextMetal
2222

2323
- (instancetype)initWithDefaultMTLDevice {
24-
id<MTLDevice> mtlDevice = MTLCreateSystemDefaultDevice();
25-
return [self initWithMTLDevice:mtlDevice commandQueue:[mtlDevice newCommandQueue]];
24+
id<MTLDevice> device = MTLCreateSystemDefaultDevice();
25+
return [self initWithMTLDevice:device commandQueue:[device newCommandQueue]];
2626
}
2727

28-
- (instancetype)initWithMTLDevice:(id<MTLDevice>)mtlDevice
28+
- (instancetype)initWithMTLDevice:(id<MTLDevice>)device
2929
commandQueue:(id<MTLCommandQueue>)commandQueue {
3030
self = [super init];
3131
if (self != nil) {
32-
_mtlDevice = mtlDevice;
32+
_device = device;
3333

34-
if (!_mtlDevice) {
34+
if (!_device) {
3535
FML_DLOG(ERROR) << "Could not acquire Metal device.";
3636
[self release];
3737
return nil;
3838
}
3939

40-
_mtlCommandQueue = commandQueue;
40+
_commandQueue = commandQueue;
4141

42-
if (!_mtlCommandQueue) {
42+
if (!_commandQueue) {
4343
FML_DLOG(ERROR) << "Could not create Metal command queue.";
4444
[self release];
4545
return nil;
4646
}
4747

48-
[_mtlCommandQueue setLabel:@"Flutter Main Queue"];
48+
[_commandQueue setLabel:@"Flutter Main Queue"];
4949

5050
auto contextOptions = CreateMetalGrContextOptions();
5151

5252
// Skia expect arguments to `MakeMetal` transfer ownership of the reference in for release later
5353
// when the GrDirectContext is collected.
5454
_mainContext =
55-
GrDirectContext::MakeMetal([_mtlDevice retain], [_mtlCommandQueue retain], contextOptions);
55+
GrDirectContext::MakeMetal([_device retain], [_commandQueue retain], contextOptions);
5656
_resourceContext =
57-
GrDirectContext::MakeMetal([_mtlDevice retain], [_mtlCommandQueue retain], contextOptions);
57+
GrDirectContext::MakeMetal([_device retain], [_commandQueue retain], contextOptions);
5858

5959
if (!_mainContext || !_resourceContext) {
6060
FML_DLOG(ERROR) << "Could not create Skia Metal contexts.";

shell/platform/darwin/ios/ios_context_metal.mm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
return;
2121
}
2222

23-
main_command_queue_.reset([darwin_context_metal_.get().mtlCommandQueue retain]);
23+
main_command_queue_.reset([darwin_context_metal_.get().commandQueue retain]);
2424

2525
CVMetalTextureCacheRef texture_cache_raw = NULL;
2626
auto cv_return = CVMetalTextureCacheCreate(kCFAllocatorDefault, // allocator
2727
NULL, // cache attributes (NULL default)
28-
darwin_context_metal_.get().mtlDevice, // metal device
28+
darwin_context_metal_.get().device, // metal device
2929
NULL, // texture attributes (NULL default)
3030
&texture_cache_raw // [out] cache
3131
);

shell/platform/darwin/ios/ios_surface_metal.mm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
is_valid_ = layer_;
2323
auto metal_context = CastToMetalContext(GetContext());
2424
auto darwin_context = metal_context->GetDarwinContext().get();
25-
command_queue_ = darwin_context.mtlCommandQueue;
26-
device_ = darwin_context.mtlDevice;
25+
command_queue_ = darwin_context.commandQueue;
26+
device_ = darwin_context.device;
2727
}
2828

2929
// |IOSSurface|

shell/platform/darwin/macos/BUILD.gn

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,18 @@
55
assert(is_mac)
66

77
import("//build/config/mac/mac_sdk.gni")
8-
import("//flutter/common/config.gni")
8+
import("//flutter/shell/gpu/gpu.gni")
99
import("//flutter/shell/platform/darwin/common/framework_shared.gni")
1010
import("//flutter/shell/platform/glfw/config.gni")
1111
import("//flutter/testing/testing.gni")
1212

13+
shell_gpu_configuration("macos_gpu_configuration") {
14+
enable_software = true
15+
enable_gl = true
16+
enable_vulkan = false
17+
enable_metal = shell_enable_metal
18+
}
19+
1320
group("macos") {
1421
deps = [ ":flutter_framework" ]
1522
if (build_glfw_shell) {
@@ -46,6 +53,8 @@ source_set("flutter_framework_source") {
4653

4754
sources = [
4855
"framework/Source/FlutterAppDelegate.mm",
56+
"framework/Source/FlutterBackingStore.h",
57+
"framework/Source/FlutterBackingStore.mm",
4958
"framework/Source/FlutterBackingStoreData.h",
5059
"framework/Source/FlutterBackingStoreData.mm",
5160
"framework/Source/FlutterDartProject.mm",
@@ -62,10 +71,15 @@ source_set("flutter_framework_source") {
6271
"framework/Source/FlutterIOSurfaceHolder.mm",
6372
"framework/Source/FlutterIntermediateKeyResponder.h",
6473
"framework/Source/FlutterIntermediateKeyResponder.mm",
74+
"framework/Source/FlutterMetalRenderer.h",
75+
"framework/Source/FlutterMetalRenderer.mm",
6576
"framework/Source/FlutterMouseCursorPlugin.h",
6677
"framework/Source/FlutterMouseCursorPlugin.mm",
6778
"framework/Source/FlutterOpenGLRenderer.h",
6879
"framework/Source/FlutterOpenGLRenderer.mm",
80+
"framework/Source/FlutterRenderer.h",
81+
"framework/Source/FlutterResizableBackingStoreProvider.h",
82+
"framework/Source/FlutterResizableBackingStoreProvider.mm",
6983
"framework/Source/FlutterResizeSynchronizer.h",
7084
"framework/Source/FlutterResizeSynchronizer.mm",
7185
"framework/Source/FlutterSurfaceManager.h",
@@ -85,6 +99,7 @@ source_set("flutter_framework_source") {
8599
sources += _flutter_framework_headers
86100

87101
deps = [
102+
":macos_gpu_configuration",
88103
"//flutter/flow:flow",
89104
"//flutter/fml",
90105
"//flutter/shell/platform/common/cpp:common_cpp_switches",
@@ -106,6 +121,7 @@ source_set("flutter_framework_source") {
106121
"Cocoa.framework",
107122
"CoreVideo.framework",
108123
"IOSurface.framework",
124+
"Metal.framework",
109125
"QuartzCore.framework",
110126
]
111127
}
@@ -131,12 +147,20 @@ executable("flutter_desktop_darwin_unittests") {
131147
sources = [
132148
"framework/Source/FlutterEngineTest.mm",
133149
"framework/Source/FlutterGLCompositorUnittests.mm",
134-
"framework/Source/FlutterOpenGLRendererTest.mm",
135150
"framework/Source/FlutterViewControllerTest.mm",
136151
"framework/Source/FlutterViewControllerTestUtils.h",
137152
"framework/Source/FlutterViewControllerTestUtils.mm",
138153
]
139154

155+
if (shell_enable_metal) {
156+
sources += [
157+
"framework/Source/FlutterMetalRendererTest.mm",
158+
"framework/Source/FlutterMetalSurfaceManagerTest.mm",
159+
]
160+
} else {
161+
sources += [ "framework/Source/FlutterOpenGLRendererTest.mm" ]
162+
}
163+
140164
cflags_objcc = [ "-fobjc-arc" ]
141165

142166
ldflags = [ "-ObjC" ]
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#import <Cocoa/Cocoa.h>
6+
#import <Metal/Metal.h>
7+
8+
/**
9+
* Interface for backing store handles. Typically contain references to the buffers that
10+
* are handed by the `FlutterView` to the `FlutterRenderer`.
11+
*/
12+
@interface FlutterRenderBackingStore : NSObject
13+
14+
@end
15+
16+
/**
17+
* Wraps an OpenGL frame buffer.
18+
*/
19+
@interface FlutterOpenGLRenderBackingStore : FlutterRenderBackingStore
20+
21+
/**
22+
* Frame buffer ID referenced by this backing store instance.
23+
*/
24+
@property(nonatomic, readonly) uint32_t frameBufferID;
25+
26+
/**
27+
* Initializes a backing store with the specified frame buffer id.
28+
*/
29+
- (nonnull instancetype)initWithFrameBufferID:(uint32_t)fboID;
30+
31+
@end
32+
33+
/**
34+
* Wraps a Metal texture.
35+
*/
36+
@interface FlutterMetalRenderBackingStore : FlutterRenderBackingStore
37+
38+
/**
39+
* MTLTexture referenced by this backing store instance.
40+
*/
41+
@property(nonnull, nonatomic, readonly) id<MTLTexture> texture;
42+
43+
/**
44+
* Initializes a backing store with the specified MTLTexture.
45+
*/
46+
- (nonnull instancetype)initWithTexture:(nonnull id<MTLTexture>)texture;
47+
48+
@end
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterBackingStore.h"
6+
7+
@implementation FlutterRenderBackingStore
8+
@end
9+
10+
@implementation FlutterOpenGLRenderBackingStore
11+
12+
- (instancetype)initWithFrameBufferID:(uint32_t)fboID {
13+
self = [super init];
14+
if (self) {
15+
_frameBufferID = fboID;
16+
}
17+
return self;
18+
}
19+
20+
@end
21+
22+
@implementation FlutterMetalRenderBackingStore
23+
24+
- (instancetype)initWithTexture:(id<MTLTexture>)texture {
25+
self = [super init];
26+
if (self) {
27+
_texture = texture;
28+
}
29+
return self;
30+
}
31+
32+
@end

shell/platform/darwin/macos/framework/Source/FlutterBackingStoreData.mm

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
// found in the LICENSE file.
44

55
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterBackingStoreData.h"
6+
7+
#import <OpenGL/gl.h>
8+
69
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterFrameBufferProvider.h"
710
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterIOSurfaceHolder.h"
811

9-
#include <OpenGL/gl.h>
10-
1112
@implementation FlutterBackingStoreData
1213

1314
- (nullable instancetype)initWithLayerId:(size_t)layerId

0 commit comments

Comments
 (0)