| Flutter macOS Embedder |

FlutterSurfaceManagerTest.mm
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 #import <Cocoa/Cocoa.h>
6 #import <Metal/Metal.h>
7
8 #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterSurface.h"
9 #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterSurfaceManager.h"
10
11 #include "flutter/testing/testing.h"
12 #include "gtest/gtest.h"
13
14 @interface TestView : NSView <FlutterSurfaceManagerDelegate>
15
16 @property(readwrite, nonatomic) CGSize presentedFrameSize;
17 - (nonnull instancetype)init;
18
19 @end
20
21 @implementation TestView
22
23 - (instancetype)init {
24 self = [super initWithFrame:NSZeroRect];
25 if (self) {
26 [self setWantsLayer:YES];
27 self.layer.contentsScale = 2.0;
28 }
29 return self;
30 }
31
32 - (void)onPresent:(CGSize)frameSize withBlock:(nonnull dispatch_block_t)block {
33 self.presentedFrameSize = frameSize;
34 block();
35 }
36
37 @end
38
39 namespace flutter::testing {
40
41 static FlutterSurfaceManager* CreateSurfaceManager(TestView* testView) {
42 id<MTLDevice> device = MTLCreateSystemDefaultDevice();
43 id<MTLCommandQueue> commandQueue = [device newCommandQueue];
44 CALayer* layer = reinterpret_cast<CALayer*>(testView.layer);
45 return [[FlutterSurfaceManager alloc] initWithDevice:device
46 commandQueue:commandQueue
47 layer:layer
48 delegate:testView];
49 }
50
51 static FlutterSurfacePresentInfo* CreatePresentInfo(
52 FlutterSurface* surface,
53 CGPoint offset = CGPointZero,
54 size_t index = 0,
55 const std::vector<FlutterRect>& paintRegion = {}) {
56 FlutterSurfacePresentInfo* res = [[FlutterSurfacePresentInfo alloc] init];
57 res.surface = surface;
58 res.offset = offset;
59 res.zIndex = index;
60 res.paintRegion = paintRegion;
61 return res;
62 }
63
64 TEST(FlutterSurfaceManager, MetalTextureSizeMatchesSurfaceSize) {
65 TestView* testView = [[TestView alloc] init];
66 FlutterSurfaceManager* surfaceManager = CreateSurfaceManager(testView);
67
68 // Get back buffer, lookup should work for borrowed surfaces util present.
69 auto surface = [surfaceManager surfaceForSize:CGSizeMake(100, 50)];
70 auto texture = surface.asFlutterMetalTexture;
71 id<MTLTexture> metalTexture = (__bridge id)texture.texture;
72 EXPECT_EQ(metalTexture.width, 100ul);
73 EXPECT_EQ(metalTexture.height, 50ul);
74 texture.destruction_callback(texture.user_data);
75 }
76
77 TEST(FlutterSurfaceManager, TestSurfaceLookupFromTexture) {
78 TestView* testView = [[TestView alloc] init];
79 FlutterSurfaceManager* surfaceManager = CreateSurfaceManager(testView);
80
81 // Get back buffer, lookup should work for borrowed surfaces util present.
82 auto surface = [surfaceManager surfaceForSize:CGSizeMake(100, 50)];
83
84 // SurfaceManager should keep texture alive while borrowed.
85 auto texture = surface.asFlutterMetalTexture;
86 texture.destruction_callback(texture.user_data);
87
88 FlutterMetalTexture dummyTexture{.texture_id = 1, .texture = nullptr, .user_data = nullptr};
89 auto surface1 = [FlutterSurface fromFlutterMetalTexture:&dummyTexture];
90 EXPECT_EQ(surface1, nil);
91
92 auto surface2 = [FlutterSurface fromFlutterMetalTexture:&texture];
93 EXPECT_EQ(surface2, surface);
94 }
95
96 TEST(FlutterSurfaceManager, BackBufferCacheDoesNotLeak) {
97 TestView* testView = [[TestView alloc] init];
98 FlutterSurfaceManager* surfaceManager = CreateSurfaceManager(testView);
99 EXPECT_EQ(surfaceManager.backBufferCache.count, 0ul);
100
101 auto surface1 = [surfaceManager surfaceForSize:CGSizeMake(100, 100)];
102 [surfaceManager presentSurfaces:@[ CreatePresentInfo(surface1) ] atTime:0 notify:nil];
103
104 EXPECT_EQ(surfaceManager.backBufferCache.count, 0ul);
105
106 auto surface2 = [surfaceManager surfaceForSize:CGSizeMake(110, 110)];
107 [surfaceManager presentSurfaces:@[ CreatePresentInfo(surface2) ] atTime:0 notify:nil];
108
109 EXPECT_EQ(surfaceManager.backBufferCache.count, 1ul);
110
111 auto surface3 = [surfaceManager surfaceForSize:CGSizeMake(120, 120)];
112 [surfaceManager presentSurfaces:@[ CreatePresentInfo(surface3) ] atTime:0 notify:nil];
113
114 // Cache should be cleaned during present and only contain the last visible
115 // surface(s).
116 EXPECT_EQ(surfaceManager.backBufferCache.count, 1ul);
117 auto surfaceFromCache = [surfaceManager surfaceForSize:CGSizeMake(110, 110)];
118 EXPECT_EQ(surfaceFromCache, surface2);
119
120 [surfaceManager presentSurfaces:@[] atTime:0 notify:nil];
121 EXPECT_EQ(surfaceManager.backBufferCache.count, 1ul);
122
123 [surfaceManager presentSurfaces:@[] atTime:0 notify:nil];
124 EXPECT_EQ(surfaceManager.backBufferCache.count, 0ul);
125 }
126
127 TEST(FlutterSurfaceManager, SurfacesAreRecycled) {
128 TestView* testView = [[TestView alloc] init];
129 FlutterSurfaceManager* surfaceManager = CreateSurfaceManager(testView);
130
131 EXPECT_EQ(surfaceManager.frontSurfaces.count, 0ul);
132
133 // Get first surface and present it.
134
135 auto surface1 = [surfaceManager surfaceForSize:CGSizeMake(100, 100)];
136 EXPECT_TRUE(CGSizeEqualToSize(surface1.size, CGSizeMake(100, 100)));
137
138 EXPECT_EQ(surfaceManager.backBufferCache.count, 0ul);
139 EXPECT_EQ(surfaceManager.frontSurfaces.count, 0ul);
140
141 [surfaceManager presentSurfaces:@[ CreatePresentInfo(surface1) ] atTime:0 notify:nil];
142
143 EXPECT_EQ(surfaceManager.backBufferCache.count, 0ul);
144 EXPECT_EQ(surfaceManager.frontSurfaces.count, 1ul);
145 EXPECT_EQ(testView.layer.sublayers.count, 1ul);
146
147 // Get second surface and present it.
148
149 auto surface2 = [surfaceManager surfaceForSize:CGSizeMake(100, 100)];
150 EXPECT_TRUE(CGSizeEqualToSize(surface2.size, CGSizeMake(100, 100)));
151
152 EXPECT_EQ(surfaceManager.backBufferCache.count, 0ul);
153
154 [surfaceManager presentSurfaces:@[ CreatePresentInfo(surface2) ] atTime:0 notify:nil];
155
156 // Check that current front surface returns to cache.
157 EXPECT_EQ(surfaceManager.backBufferCache.count, 1ul);
158 EXPECT_EQ(surfaceManager.frontSurfaces.count, 1ul);
159 EXPECT_EQ(testView.layer.sublayers.count, 1ull);
160
161 // Check that surface is properly reused.
162 auto surface3 = [surfaceManager surfaceForSize:CGSizeMake(100, 100)];
163 EXPECT_EQ(surface3, surface1);
164 }
165
166 inline bool operator==(const CGRect& lhs, const CGRect& rhs) {
167 return CGRectEqualToRect(lhs, rhs);
168 }
169
170 TEST(FlutterSurfaceManager, LayerManagement) {
171 TestView* testView = [[TestView alloc] init];
172 FlutterSurfaceManager* surfaceManager = CreateSurfaceManager(testView);
173
174 EXPECT_EQ(testView.layer.sublayers.count, 0ul);
175
176 auto surface1_1 = [surfaceManager surfaceForSize:CGSizeMake(50, 30)];
177 [surfaceManager presentSurfaces:@[ CreatePresentInfo(surface1_1, CGPointMake(20, 10)) ]
178 atTime:0
179 notify:nil];
180
181 EXPECT_EQ(testView.layer.sublayers.count, 1ul);
182 EXPECT_TRUE(CGSizeEqualToSize(testView.presentedFrameSize, CGSizeMake(70, 40)));
183
184 auto surface2_1 = [surfaceManager surfaceForSize:CGSizeMake(50, 30)];
185 auto surface2_2 = [surfaceManager surfaceForSize:CGSizeMake(20, 20)];
186 [surfaceManager presentSurfaces:@[
187 CreatePresentInfo(surface2_1, CGPointMake(20, 10), 1),
188 CreatePresentInfo(surface2_2, CGPointMake(40, 50), 2,
189 {
190 FlutterRect{0, 0, 20, 20},
191 FlutterRect{40, 0, 60, 20},
192 })
193 ]
194 atTime:0
195 notify:nil];
196
197 EXPECT_EQ(testView.layer.sublayers.count, 2ul);
198 EXPECT_EQ(testView.layer.sublayers[0].zPosition, 1.0);
199 EXPECT_EQ(testView.layer.sublayers[1].zPosition, 2.0);
200 CALayer* firstOverlaySublayer;
201 {
202 NSArray<CALayer*>* sublayers = testView.layer.sublayers[1].sublayers;
203 EXPECT_EQ(sublayers.count, 2ul);
204 EXPECT_TRUE(CGRectEqualToRect(sublayers[0].frame, CGRectMake(0, 0, 10, 10)));
205 EXPECT_TRUE(CGRectEqualToRect(sublayers[1].frame, CGRectMake(20, 0, 10, 10)));
206 EXPECT_TRUE(CGRectEqualToRect(sublayers[0].contentsRect, CGRectMake(0, 0, 1, 1)));
207 EXPECT_TRUE(CGRectEqualToRect(sublayers[1].contentsRect, CGRectMake(2, 0, 1, 1)));
208 EXPECT_EQ(sublayers[0].contents, sublayers[1].contents);
209 firstOverlaySublayer = sublayers[0];
210 }
211 EXPECT_TRUE(CGSizeEqualToSize(testView.presentedFrameSize, CGSizeMake(70, 70)));
212
213 // Check second overlay sublayer is removed while first is reused and updated
214 [surfaceManager presentSurfaces:@[
215 CreatePresentInfo(surface2_1, CGPointMake(20, 10), 1),
216 CreatePresentInfo(surface2_2, CGPointMake(40, 50), 2,
217 {
218 FlutterRect{0, 10, 20, 20},
219 })
220 ]
221 atTime:0
222 notify:nil];
223 EXPECT_EQ(testView.layer.sublayers.count, 2ul);
224 {
225 NSArray<CALayer*>* sublayers = testView.layer.sublayers[1].sublayers;
226 EXPECT_EQ(sublayers.count, 1ul);
227 EXPECT_EQ(sublayers[0], firstOverlaySublayer);
228 EXPECT_TRUE(CGRectEqualToRect(sublayers[0].frame, CGRectMake(0, 5, 10, 5)));
229 }
230
231 // Check that second overlay sublayer is added back while first is reused and updated
232 [surfaceManager presentSurfaces:@[
233 CreatePresentInfo(surface2_1, CGPointMake(20, 10), 1),
234 CreatePresentInfo(surface2_2, CGPointMake(40, 50), 2,
235 {
236 FlutterRect{0, 0, 20, 20},
237 FlutterRect{40, 0, 60, 20},
238 })
239 ]
240 atTime:0
241 notify:nil];
242
243 EXPECT_EQ(testView.layer.sublayers.count, 2ul);
244 {
245 NSArray<CALayer*>* sublayers = testView.layer.sublayers[1].sublayers;
246 EXPECT_EQ(sublayers.count, 2ul);
247 EXPECT_EQ(sublayers[0], firstOverlaySublayer);
248 EXPECT_TRUE(CGRectEqualToRect(sublayers[0].frame, CGRectMake(0, 0, 10, 10)));
249 EXPECT_TRUE(CGRectEqualToRect(sublayers[1].frame, CGRectMake(20, 0, 10, 10)));
250 EXPECT_EQ(sublayers[0].contents, sublayers[1].contents);
251 }
252
253 auto surface3_1 = [surfaceManager surfaceForSize:CGSizeMake(50, 30)];
254 [surfaceManager presentSurfaces:@[ CreatePresentInfo(surface3_1, CGPointMake(20, 10)) ]
255 atTime:0
256 notify:nil];
257
258 EXPECT_EQ(testView.layer.sublayers.count, 1ul);
259 EXPECT_TRUE(CGSizeEqualToSize(testView.presentedFrameSize, CGSizeMake(70, 40)));
260
261 // Check removal of all surfaces.
262 [surfaceManager presentSurfaces:@[] atTime:0 notify:nil];
263 EXPECT_EQ(testView.layer.sublayers.count, 0ul);
264 EXPECT_TRUE(CGSizeEqualToSize(testView.presentedFrameSize, CGSizeMake(0, 0)));
265 }
266
267 } // namespace flutter::testing
TestView
Definition: FlutterSurfaceManagerTest.mm:14
+[FlutterSurface fromFlutterMetalTexture:]
nullable FlutterSurface * fromFlutterMetalTexture:(nonnull const FlutterMetalTexture *texture)
-[FlutterSurface asFlutterMetalTexture]
FlutterMetalTexture asFlutterMetalTexture()
Definition: FlutterSurface.mm:45
flutter::testing::CreatePresentInfo
static FlutterSurfacePresentInfo * CreatePresentInfo(FlutterSurface *surface, CGPoint offset=CGPointZero, size_t index=0, const std::vector< FlutterRect > &paintRegion={})
Definition: FlutterSurfaceManagerTest.mm:51
FlutterSurfaceManager.h
-[FlutterBackBufferCache count]
NSUInteger count()
Definition: FlutterSurfaceManager.mm:309
FlutterSurfaceManager
Definition: FlutterSurfaceManager.h:44
flutter::testing
Definition: AccessibilityBridgeMacTest.mm:13
FlutterSurface.h
FlutterSurface
Definition: FlutterSurface.h:16
FlutterSurfaceManagerDelegate-p
Definition: FlutterSurfaceManager.h:27
FlutterSurfacePresentInfo::surface
FlutterSurface * surface
Definition: FlutterSurfaceManager.h:20
FlutterSurfaceManager::frontSurfaces
NSArray< FlutterSurface * > * frontSurfaces
Definition: FlutterSurfaceManager.h:106
FlutterSurfacePresentInfo::offset
CGPoint offset
Definition: FlutterSurfaceManager.h:21
FlutterSurfaceManager::backBufferCache
FlutterBackBufferCache * backBufferCache
Definition: FlutterSurfaceManager.h:105
flutter::testing::operator==
bool operator==(const CGRect &lhs, const CGRect &rhs)
Definition: FlutterSurfaceManagerTest.mm:166
FlutterSurfacePresentInfo
Definition: FlutterSurfaceManager.h:18
-[FlutterSurfaceManager presentSurfaces:atTime:notify:]
void presentSurfaces:atTime:notify:(nonnull NSArray< FlutterSurfacePresentInfo * > *surfaces,[atTime] CFTimeInterval presentationTime,[notify] nullable dispatch_block_t notify)
FlutterSurfacePresentInfo::zIndex
size_t zIndex
Definition: FlutterSurfaceManager.h:22
-[FlutterSurfaceManager surfaceForSize:]
nonnull FlutterSurface * surfaceForSize:(CGSize size)
Definition: FlutterSurfaceManager.mm:133
TestView::presentedFrameSize
CGSize presentedFrameSize
Definition: FlutterSurfaceManagerTest.mm:16
flutter::testing::CreateSurfaceManager
static FlutterSurfaceManager * CreateSurfaceManager(TestView *testView)
Definition: FlutterSurfaceManagerTest.mm:41
flutter::testing::TEST
TEST(FlutterSurfaceManager, LayerManagement)
Definition: FlutterSurfaceManagerTest.mm:170
FlutterSurfacePresentInfo::paintRegion
std::vector< FlutterRect > paintRegion
Definition: FlutterSurfaceManager.h:23
-[TestView init]
nonnull instancetype init()
Definition: FlutterSurfaceManagerTest.mm:23