Flutter macOS Embedder
FlutterSurfaceManager.h
Go to the documentation of this file.
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 #ifndef FLUTTER_SHELL_PLATFORM_DARWIN_MACOS_FRAMEWORK_SOURCE_FLUTTERSURFACEMANAGER_H_
6 #define FLUTTER_SHELL_PLATFORM_DARWIN_MACOS_FRAMEWORK_SOURCE_FLUTTERSURFACEMANAGER_H_
7 
8 #import <Cocoa/Cocoa.h>
9 #import <QuartzCore/QuartzCore.h>
10 
11 #include <vector>
12 
14 
15 /**
16  * Surface with additional properties needed for presenting.
17  */
18 @interface FlutterSurfacePresentInfo : NSObject
19 
20 @property(readwrite, strong, nonatomic, nonnull) FlutterSurface* surface;
21 @property(readwrite, nonatomic) CGPoint offset;
22 @property(readwrite, nonatomic) size_t zIndex;
23 @property(readwrite, nonatomic) std::vector<FlutterRect> paintRegion;
24 
25 @end
26 
27 @protocol FlutterSurfaceManagerDelegate <NSObject>
28 
29 /*
30  * Schedules the block on the platform thread.
31  * Provided `frameSize` is used to unblock the platform thread if it waits for
32  * a certain frame size during resizing.
33  */
34 - (void)onPresent:(CGSize)frameSize
35  withBlock:(nonnull dispatch_block_t)block
36  delay:(NSTimeInterval)delay;
37 
38 @end
39 
40 /**
41  * FlutterSurfaceManager is responsible for providing and presenting Core Animation render
42  * surfaces and managing sublayers.
43  *
44  * Owned by `FlutterView`.
45  */
46 @interface FlutterSurfaceManager : NSObject
47 
48 /**
49  * Initializes and returns a surface manager that renders to a child layer (referred to as the
50  * content layer) of the containing layer.
51  */
52 - (nullable instancetype)initWithDevice:(nonnull id<MTLDevice>)device
53  commandQueue:(nonnull id<MTLCommandQueue>)commandQueue
54  layer:(nonnull CALayer*)containingLayer
55  delegate:(nonnull id<FlutterSurfaceManagerDelegate>)delegate
56  wideGamut:(BOOL)wideGamut;
57 
58 /**
59  * Updates the wide gamut setting. Flushes cached surfaces so new surfaces
60  * will be created with the updated pixel format.
61  *
62  * Must be called on the platform thread.
63  */
64 - (void)setEnableWideGamut:(BOOL)enableWideGamut;
65 
66 /**
67  * Returns a back buffer surface of the given size to which Flutter can render content.
68  * A cached surface will be returned if available; otherwise a new one will be created.
69  *
70  * Must be called on raster thread.
71  */
72 - (nonnull FlutterSurface*)surfaceForSize:(CGSize)size;
73 
74 /**
75  * Sets the provided surfaces as contents of FlutterView. Will create, update and
76  * remove sublayers as needed.
77  *
78  * Must be called on raster thread. This will schedule a commit on the platform thread and block the
79  * raster thread until the commit is done. The `notify` block will be invoked on the platform thread
80  * and can be used to perform additional work, such as mutating platform views. It is guaranteed be
81  * called in the same CATransaction.
82  */
83 - (void)presentSurfaces:(nonnull NSArray<FlutterSurfacePresentInfo*>*)surfaces
84  atTime:(CFTimeInterval)presentationTime
85  notify:(nullable dispatch_block_t)notify;
86 
87 @end
88 
89 /**
90  * Cache of back buffers to prevent unnecessary IOSurface allocations.
91  */
92 @interface FlutterBackBufferCache : NSObject
93 
94 /**
95  * Removes surface with given size from cache (if available) and returns it.
96  */
97 - (nullable FlutterSurface*)removeSurfaceForSize:(CGSize)size;
98 
99 /**
100  * Removes all cached surfaces replacing them with new ones.
101  */
102 - (void)returnSurfaces:(nonnull NSArray<FlutterSurface*>*)surfaces;
103 
104 /**
105  * Removes all cached surfaces.
106  */
107 - (void)flush;
108 
109 /**
110  * Returns number of surfaces currently in cache. Used for tests.
111  */
112 - (NSUInteger)count;
113 
114 @end
115 
116 /**
117  * Interface to internal properties used for testing.
118  */
120 
121 @property(readonly, nonatomic, nonnull) FlutterBackBufferCache* backBufferCache;
122 @property(readonly, nonatomic, nonnull) NSArray<FlutterSurface*>* frontSurfaces;
123 @property(readonly, nonatomic, nonnull) NSArray<CALayer*>* layers;
124 
125 @end
126 
127 #endif // FLUTTER_SHELL_PLATFORM_DARWIN_MACOS_FRAMEWORK_SOURCE_FLUTTERSURFACEMANAGER_H_
std::vector< FlutterRect > paintRegion