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

Commit 3bc00c7

Browse files
committed
document
1 parent a2c2866 commit 3bc00c7

File tree

7 files changed

+58
-24
lines changed

7 files changed

+58
-24
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
FlutterLayer* layer = (FlutterLayer*)layers[i];
5252
if (layer->type == kFlutterLayerContentTypeBackingStore) {
5353
FlutterSurface* surface =
54-
[view.surfaceManager backBufferForMetalTexture:&layer->backing_store->metal.texture];
54+
[view.surfaceManager lookupSurface:&layer->backing_store->metal.texture];
5555
if (surface) {
5656
FlutterSurfacePresentInfo* info = [[FlutterSurfacePresentInfo alloc] init];
5757
info.surface = surface;

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,17 +111,15 @@ - (FlutterMetalTexture)createTextureForView:(uint64_t)viewId size:(CGSize)size {
111111
- (BOOL)present:(uint64_t)viewId texture:(const FlutterMetalTexture*)texture {
112112
FlutterView* view = [_viewProvider getView:viewId];
113113
if (view != nil) {
114-
FlutterSurface* surface = [view.surfaceManager backBufferForMetalTexture:texture];
114+
FlutterSurface* surface = [view.surfaceManager lookupSurface:texture];
115115
if (surface == nil) {
116116
return NO;
117117
}
118118
FlutterSurfacePresentInfo* info = [[FlutterSurfacePresentInfo alloc] init];
119119
info.offset = CGPointMake(0, 0);
120120
info.zIndex = 0;
121121
info.surface = surface;
122-
[view.surfaceManager present:@[ info ]
123-
notify:^{
124-
}];
122+
[view.surfaceManager present:@[ info ] notify:nil];
125123
}
126124
return YES;
127125
}

shell/platform/darwin/macos/framework/Source/FlutterSurfaceManager.h

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
@end
1616

17+
/**
18+
* Surface with additional properties needed for presenting.
19+
*/
1720
@interface FlutterSurfacePresentInfo : NSObject
1821

1922
@property(readwrite, strong, nonatomic, nonnull) FlutterSurface* surface;
@@ -22,40 +25,48 @@
2225

2326
@end
2427

28+
/**
29+
* Owned by `FlutterView`.
30+
* Responsible for providing surfaces for Flutter to render into and
31+
* as well as subsequent presentation of these surfaces.
32+
* The Presentation of surfaces includes management of core animation sublayers.
33+
*/
2534
@interface FlutterSurfaceManager : NSObject
2635

2736
/**
2837
* Initializes and returns a surface manager that renders to a child layer (referred to as the
29-
* content layer) of the containing layer and applies the transform to the contents of the content
30-
* layer.
38+
* content layer) of the containing layer.
3139
*/
3240
- (nullable instancetype)initWithDevice:(nonnull id<MTLDevice>)device
3341
commandQueue:(nonnull id<MTLCommandQueue>)commandQueue
3442
layer:(nonnull CALayer*)containingLayer
3543
threadSynchronizer:(nonnull FlutterThreadSynchronizer*)synchronizer;
3644

3745
/**
38-
* Looks up surface for given metal texture. Can be called for surfaces obtained
39-
* through backBufferForSize: until `present` is called.
46+
* Returns back buffer surface for given size that Flutter can render content to.
47+
* Will return cached surface if one is available, or create new one otherwise.
48+
*
49+
* Must be called on raster thread.
4050
*/
41-
- (nullable FlutterSurface*)backBufferForMetalTexture:(nonnull const FlutterMetalTexture*)texture;
51+
- (nonnull FlutterSurface*)backBufferForSize:(CGSize)size;
4252

4353
/**
44-
* Returns back buffer surface for given size.
45-
*
46-
* Must be called on raster threads.
54+
* Looks up surface for given metal texture. Can only be called for surfaces
55+
* obtained through `backBufferForSize:` until `present:` is called.
4756
*/
48-
- (nonnull FlutterSurface*)backBufferForSize:(CGSize)size;
57+
- (nullable FlutterSurface*)lookupSurface:(nonnull const FlutterMetalTexture*)texture;
4958

5059
/**
51-
* Sets the provided surfaces as contents of views containing layer sublayers.
60+
* Sets the provided surfaces as contents of FlutterView. Will create, update and
61+
* remove sublayers as needed.
5262
*
5363
* Must be called on raster thread. This will schedule a commit on platform
5464
* thread and block raster thread until the commit is done.
5565
* `notify` block will be invoked on platform thread and can be used to perform
56-
* additional work after commit is done. It will be called in same CATransaction.
66+
* additional work after commit is done, such as mutating platform views.
67+
* It is guaranteed be called in same CATransaction.
5768
*/
5869
- (void)present:(nonnull NSArray<FlutterSurfacePresentInfo*>*)surfaces
59-
notify:(nonnull dispatch_block_t)notify;
70+
notify:(nullable dispatch_block_t)notify;
6071

6172
@end

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,19 @@ @interface FlutterSurfaceManager () {
100100
CALayer* _containingLayer;
101101
FlutterThreadSynchronizer* _synchronizer;
102102

103+
// Available (cached) back buffer surfaces. These will be removed during
104+
// present and replaced by current frong surfaces.
103105
NSMutableArray<FlutterSurface*>* _backBufferSurfaces;
106+
107+
// Surfaces that currently obtained through backBufferForSize waiting to be
108+
// presented. This is used to keep the surface alive because the non compositor
109+
// codepath doesn't properly retain the surface.
104110
NSMutableArray<FlutterSurface*>* _borrowedSurfaces;
111+
112+
// Surfaces currently used to back visible layers.
105113
NSMutableArray<FlutterSurface*>* _frontSurfaces;
114+
115+
// Currently visible layers.
106116
NSMutableArray<CALayer*>* _layers;
107117
}
108118
@end
@@ -127,7 +137,7 @@ - (instancetype)initWithDevice:(id<MTLDevice>)device
127137
return self;
128138
}
129139

130-
- (FlutterSurface*)backBufferForMetalTexture:(nonnull const FlutterMetalTexture*)texture {
140+
- (FlutterSurface*)lookupSurface:(nonnull const FlutterMetalTexture*)texture {
131141
for (FlutterSurface* surface in _borrowedSurfaces) {
132142
if (surface.textureId == texture->texture_id) {
133143
return surface;
@@ -153,28 +163,34 @@ - (FlutterSurface*)backBufferForSize:(CGSize)size {
153163
}
154164

155165
- (void)commit:(NSArray<FlutterSurfacePresentInfo*>*)surfaces {
166+
assert([NSThread isMainThread]);
167+
156168
[_borrowedSurfaces removeAllObjects];
157169

158-
// release all unused back buffer surfaces
170+
// Release all unused back buffer surfaces and replace them with front surfaces.
159171
[_backBufferSurfaces removeAllObjects];
160172
[_backBufferSurfaces addObjectsFromArray:_frontSurfaces];
161173

174+
// Front surfaces will be replaced by currently presented surfaces.
162175
[_frontSurfaces removeAllObjects];
163176
for (FlutterSurfacePresentInfo* info in surfaces) {
164177
[_frontSurfaces addObject:info.surface];
165178
}
166179

180+
// Remove excess layers.
167181
while (_layers.count > _frontSurfaces.count) {
168182
[_layers.lastObject removeFromSuperlayer];
169183
[_layers removeLastObject];
170184
}
171185

186+
// Add missing layers.
172187
while (_layers.count < _frontSurfaces.count) {
173188
CALayer* layer = [CALayer layer];
174189
[_containingLayer addSublayer:layer];
175190
[_layers addObject:layer];
176191
}
177192

193+
// Update contents of surface.
178194
for (size_t i = 0; i < surfaces.count; ++i) {
179195
FlutterSurfacePresentInfo* info = surfaces[i];
180196
CALayer* layer = _layers[i];
@@ -190,15 +206,19 @@ - (void)present:(NSArray<FlutterSurfacePresentInfo*>*)surfaces notify:(dispatch_
190206
[commandBuffer commit];
191207
[commandBuffer waitUntilScheduled];
192208

209+
// Get the actual dimensions of the frame (relevant for resize synchronizer).
193210
CGSize size = CGSizeZero;
194211
for (FlutterSurfacePresentInfo* info in surfaces) {
195212
size = CGSizeMake(std::max(size.width, info.offset.x + info.surface.size.width),
196213
std::max(size.height, info.offset.y + info.surface.size.height));
197214
}
215+
198216
[_synchronizer performCommit:size
199217
notify:^{
200218
[self commit:surfaces];
201-
notify();
219+
if (notify != nil) {
220+
notify();
221+
}
202222
}];
203223
}
204224

shell/platform/darwin/macos/framework/Source/FlutterThreadSynchronizer.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#import <Cocoa/Cocoa.h>
22

33
/**
4-
* Takes care of synchronizing raster and platform thread.
4+
* Takes care of synchronization between raster and platform thread.
55
*/
66
@interface FlutterThreadSynchronizer : NSObject
77

@@ -14,8 +14,11 @@
1414
/**
1515
* Called from raster thread. Schedules the given block on platform thread
1616
* and blocks until it is performed.
17-
* If platform thread is blocked in beginResize for given size (or size is empty),
17+
*
18+
* If platform thread is blocked in `beginResize:` for given size (or size is empty),
1819
* unblocks platform thread.
20+
*
21+
* The notify block is guaranteed to be called within a core animation transaction.
1922
*/
2023
- (void)performCommit:(CGSize)size notify:(nonnull dispatch_block_t)notify;
2124

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ void Wait() {
3838
private:
3939
std::condition_variable cv_;
4040
std::mutex mutex_;
41-
42-
// True if this event is in the signaled state.
4341
bool signaled_ = false;
4442
};
4543
} // namespace

shell/platform/darwin/macos/framework/Source/FlutterView.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ constexpr uint64_t kFlutterDefaultViewId = 0;
4848
- (nullable instancetype)initWithCoder:(nonnull NSCoder*)coder NS_UNAVAILABLE;
4949
- (nonnull instancetype)init NS_UNAVAILABLE;
5050

51+
/**
52+
* Returns SurfaceManager for this view. SurfaceManager is responsible for
53+
* providing and presenting render surfaces.
54+
*/
5155
- (nonnull FlutterSurfaceManager*)surfaceManager;
5256

5357
/**

0 commit comments

Comments
 (0)