19
19
// BackBuffer will be released after kIdleDelay if there is no activity.
20
20
static const double kIdleDelay = 1.0 ;
21
21
22
- @implementation FlutterIOSurfaceManager {
22
+ @interface FlutterSurfaceManager ()
23
+
24
+ /* *
25
+ * Cancels any previously-scheduled onIdle requests.
26
+ */
27
+ - (void )cancelIdle ;
28
+
29
+ /* *
30
+ * Creates a backing textures for the specified surface with the specified size.
31
+ */
32
+ - (id <MTLTexture >)createTextureForSurface : (FlutterIOSurfaceHolder*)surface size : (CGSize )size ;
33
+
34
+ @end
35
+
36
+ @implementation FlutterSurfaceManager {
23
37
CALayer * _containingLayer; // provided (parent layer)
24
38
CALayer * _contentLayer;
25
39
CATransform3D _contentTransform;
26
40
27
41
CGSize _surfaceSize;
28
42
FlutterIOSurfaceHolder* _ioSurfaces[kFlutterSurfaceManagerBufferCount ];
29
43
BOOL _frameInProgress;
44
+
45
+ id <MTLDevice > _device;
46
+ id <MTLCommandQueue > _commandQueue;
47
+ id <MTLTexture > _textures[kFlutterSurfaceManagerBufferCount ];
30
48
}
31
49
32
- - (instancetype )initWithLayer : (CALayer *)containingLayer contentTransform : (CATransform3D)transform {
50
+ - (nullable instancetype )initWithDevice : (nonnull id <MTLDevice >)device
51
+ commandQueue : (nonnull id <MTLCommandQueue >)commandQueue
52
+ layer : (nonnull CALayer *)containingLayer {
33
53
self = [super init ];
34
54
if (self) {
35
55
_containingLayer = containingLayer;
36
- _contentTransform = transform ;
56
+ _contentTransform = CATransform3DIdentity ;
37
57
_contentLayer = [[CALayer alloc ] init ];
38
58
[_containingLayer addSublayer: _contentLayer];
39
59
40
60
_ioSurfaces[0 ] = [[FlutterIOSurfaceHolder alloc ] init ];
41
61
_ioSurfaces[1 ] = [[FlutterIOSurfaceHolder alloc ] init ];
62
+
63
+ _device = device;
64
+ _commandQueue = commandQueue;
42
65
}
43
66
return self;
44
67
}
@@ -51,7 +74,7 @@ - (void)ensureSurfaceSize:(CGSize)size {
51
74
for (int i = 0 ; i < kFlutterSurfaceManagerBufferCount ; ++i) {
52
75
if (_ioSurfaces[i] != nil ) {
53
76
[_ioSurfaces[i] recreateIOSurfaceWithSize: size];
54
- [_delegate onUpdateSurface : _ioSurfaces[i] bufferIndex: i size: size];
77
+ _textures[i] = [ self createTextureForSurface : _ioSurfaces[i] size: size];
55
78
}
56
79
}
57
80
}
@@ -71,7 +94,8 @@ - (void)swapBuffers {
71
94
72
95
std::swap (_ioSurfaces[kFlutterSurfaceManagerBackBuffer ],
73
96
_ioSurfaces[kFlutterSurfaceManagerFrontBuffer ]);
74
- [_delegate onSwapBuffers ];
97
+ std::swap (_textures[kFlutterSurfaceManagerBackBuffer ],
98
+ _textures[kFlutterSurfaceManagerFrontBuffer ]);
75
99
76
100
// performSelector:withObject:afterDelay needs to be performed on RunLoop thread
77
101
[self performSelectorOnMainThread: @selector (reschedule ) withObject: nil waitUntilDone: NO ];
@@ -92,7 +116,7 @@ - (void)onIdle {
92
116
// Release the back buffer and notify delegate. The buffer will be restored
93
117
// on demand in ensureBackBuffer
94
118
_ioSurfaces[kFlutterSurfaceManagerBackBuffer ] = nil ;
95
- [ self .delegate onSurfaceReleased: kFlutterSurfaceManagerBackBuffer ];
119
+ _textures[ kFlutterSurfaceManagerBackBuffer ] = nil ;
96
120
}
97
121
}
98
122
}
@@ -104,9 +128,9 @@ - (void)ensureBackBuffer {
104
128
// Restore previously released backbuffer
105
129
_ioSurfaces[kFlutterSurfaceManagerBackBuffer ] = [[FlutterIOSurfaceHolder alloc ] init ];
106
130
[_ioSurfaces[kFlutterSurfaceManagerBackBuffer ] recreateIOSurfaceWithSize: _surfaceSize];
107
- [_delegate onUpdateSurface: _ioSurfaces[ kFlutterSurfaceManagerBackBuffer ]
108
- bufferIndex: kFlutterSurfaceManagerBackBuffer
109
- size: _surfaceSize];
131
+ _textures[ kFlutterSurfaceManagerBackBuffer ] =
132
+ [ self createTextureForSurface: _ioSurfaces[ kFlutterSurfaceManagerBackBuffer ]
133
+ size: _surfaceSize];
110
134
}
111
135
};
112
136
[self performSelectorOnMainThread: @selector (cancelIdle ) withObject: nil waitUntilDone: NO ];
@@ -117,47 +141,12 @@ - (void)cancelIdle {
117
141
}
118
142
119
143
- (nonnull FlutterRenderBackingStore*)renderBuffer {
120
- @throw ([NSException exceptionWithName: @" Sub-classes FlutterIOSurfaceManager of"
121
- " must override renderBuffer."
122
- reason: nil
123
- userInfo: nil ]);
124
- }
125
-
126
- @end
127
-
128
- @implementation FlutterMetalSurfaceManager {
129
- id <MTLDevice > _device;
130
- id <MTLCommandQueue > _commandQueue;
131
-
132
- id <MTLTexture > _textures[kFlutterSurfaceManagerBufferCount ];
133
- }
134
-
135
- - (nullable instancetype )initWithDevice : (nonnull id <MTLDevice >)device
136
- commandQueue : (nonnull id <MTLCommandQueue >)commandQueue
137
- layer : (nonnull CALayer *)containingLayer {
138
- self = [super initWithLayer: containingLayer contentTransform: CATransform3DIdentity];
139
- if (self) {
140
- super.delegate = self;
141
- _device = device;
142
- _commandQueue = commandQueue;
143
- }
144
- return self;
145
- }
146
-
147
- - (FlutterRenderBackingStore*)renderBuffer {
148
144
[self ensureBackBuffer ];
149
145
id <MTLTexture > texture = _textures[kFlutterSurfaceManagerBackBuffer ];
150
146
return [[FlutterMetalRenderBackingStore alloc ] initWithTexture: texture];
151
147
}
152
148
153
- - (void )onSwapBuffers {
154
- std::swap (_textures[kFlutterSurfaceManagerBackBuffer ],
155
- _textures[kFlutterSurfaceManagerFrontBuffer ]);
156
- }
157
-
158
- - (void )onUpdateSurface : (FlutterIOSurfaceHolder*)surface
159
- bufferIndex : (size_t )index
160
- size : (CGSize )size {
149
+ - (id <MTLTexture >)createTextureForSurface : (FlutterIOSurfaceHolder*)surface size : (CGSize )size {
161
150
MTLTextureDescriptor * textureDescriptor =
162
151
[MTLTextureDescriptor texture2DDescriptorWithPixelFormat: MTLPixelFormatBGRA8Unorm
163
152
width: size.width
@@ -166,13 +155,7 @@ - (void)onUpdateSurface:(FlutterIOSurfaceHolder*)surface
166
155
textureDescriptor.usage =
167
156
MTLTextureUsageShaderRead | MTLTextureUsageRenderTarget | MTLTextureUsageShaderWrite ;
168
157
// plane = 0 for BGRA.
169
- _textures[index ] = [_device newTextureWithDescriptor: textureDescriptor
170
- iosurface: [surface ioSurface ]
171
- plane: 0 ];
172
- }
173
-
174
- - (void )onSurfaceReleased : (size_t )index {
175
- _textures[index ] = nil ;
158
+ return [_device newTextureWithDescriptor: textureDescriptor iosurface: [surface ioSurface ] plane: 0 ];
176
159
}
177
160
178
161
@end
0 commit comments