Skip to content

Commit a8073db

Browse files
[macOS] Keep the back buffer cache single-sized by discarding stale surfaces on return
Per review: instead of filtering wrong-size surfaces when a surface is requested, keep the existing invariant that every cached surface has the size of the last request and refuse returned surfaces of a different size. They are from before the size changed and cannot be reused.
1 parent 8816bbc commit a8073db

3 files changed

Lines changed: 25 additions & 24 deletions

File tree

‎engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterSurfaceManager.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@
9292
@interface FlutterBackBufferCache : NSObject
9393

9494
/**
95-
* Removes a surface with the given size from the cache and returns it, or nil
96-
* if no idle surface of that exact size is cached. Cached surfaces of any
97-
* other size are evicted. The returned surface is guaranteed to have `size`.
95+
* Removes surface with given size from cache (if available) and returns it.
96+
* All cached surfaces have the size of the last request; a request for a
97+
* different size purges the cache first.
9898
*/
9999
- (nullable FlutterSurface*)removeSurfaceForSize:(CGSize)size;
100100

‎engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterSurfaceManager.mm‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -318,16 +318,9 @@ - (void)setAge:(int)age forSurface:(FlutterSurface*)surface {
318318

319319
- (nullable FlutterSurface*)removeSurfaceForSize:(CGSize)size {
320320
@synchronized(self) {
321-
// Surfaces of a different size can never be reused. Drop them so a later
322-
// request cannot receive a surface whose Metal texture disagrees with the
323-
// backing store size it was asked for (flutter/flutter#185394). This is
324-
// checked per surface: the cache can hold mixed sizes when the front
325-
// surfaces of a frame with a different size are returned while a surface
326-
// of the current size is still held by the window server.
327-
for (NSInteger i = static_cast<NSInteger>(_surfaces.count) - 1; i >= 0; i--) {
328-
if (!CGSizeEqualToSize(_surfaces[i].size, size)) {
329-
[_surfaces removeObjectAtIndex:i];
330-
}
321+
// Purge all cached surfaces if the size has changed.
322+
if (_surfaces.firstObject != nil && !CGSizeEqualToSize(_surfaces.firstObject.size, size)) {
323+
[_surfaces removeAllObjects];
331324
}
332325

333326
FlutterSurface* res;
@@ -344,13 +337,20 @@ - (nullable FlutterSurface*)removeSurfaceForSize:(CGSize)size {
344337
if (res != nil) {
345338
[_surfaces removeObject:res];
346339
}
347-
FML_DCHECK(res == nil || CGSizeEqualToSize(res.size, size));
348340
return res;
349341
}
350342
}
351343

352344
- (void)returnSurfaces:(nonnull NSArray<FlutterSurface*>*)returnedSurfaces {
353345
@synchronized(self) {
346+
if (_surfaces.count > 0 && returnedSurfaces.count > 0 &&
347+
!CGSizeEqualToSize(returnedSurfaces.firstObject.size, _surfaces.firstObject.size)) {
348+
// Any cached surface size will match last requested size (see removeSurfaceForSize:). If the
349+
// size of incoming surfaces don't match cached surface size it means they are stale and
350+
// should be discarded. Keeping them would let removeSurfaceForSize: hand out a surface of
351+
// the wrong size (https://github.com/flutter/flutter/issues/185394).
352+
return;
353+
}
354354
for (FlutterSurface* surface in returnedSurfaces) {
355355
[self setAge:0 forSurface:surface];
356356
}

‎engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterSurfaceManagerTest.mm‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -200,14 +200,14 @@ - (void)onPresent:(CGSize)frameSize
200200

201201
// Regression test for https://github.com/flutter/flutter/issues/185394.
202202
//
203-
// The cache purged itself only when the *first* cached surface had a different
204-
// size than the request, then returned the youngest idle surface without
205-
// checking *that* surface's size. A size flip-flop (A -> B -> A) while the
206-
// previous A surface is still held by the window server leaves a mixed-size
207-
// cache, and the next request for A gets the B surface. Impeller then wraps a
208-
// Metal texture whose real size disagrees with the descriptor, the texture
209-
// invalidates itself, the color attachment is dropped, and the raster thread
210-
// crashes in impeller::Canvas::SetupRenderPass.
203+
// The cache is meant to hold surfaces of a single size (the last requested
204+
// one), but returnSurfaces: appended the previous front surfaces without
205+
// checking their size. A size flip-flop (A -> B -> A) while the previous A
206+
// surface is still held by the window server therefore left a mixed-size
207+
// cache, and the next request for A got the B surface. Impeller then wrapped a
208+
// Metal texture whose real size disagreed with the descriptor, the texture
209+
// invalidated itself, the color attachment was dropped, and the raster thread
210+
// crashed in impeller::Canvas::SetupRenderPass.
211211
TEST(FlutterSurfaceManager, BackBufferCacheNeverReturnsSurfaceOfDifferentSize) {
212212
TestView* testView = [[TestView alloc] init];
213213
FlutterSurfaceManager* surfaceManager = CreateSurfaceManager(testView);
@@ -232,9 +232,10 @@ - (void)onPresent:(CGSize)frameSize
232232
auto surfaceA2 = [surfaceManager surfaceForSize:sizeA];
233233
EXPECT_NE(surfaceA2, surfaceA1);
234234
EXPECT_TRUE(CGSizeEqualToSize(surfaceA2.size, sizeA));
235-
// Presenting returns B1 into a cache whose head is A1 -> mixed sizes.
235+
// Presenting A2 returns B1, whose size differs from the cached A1. The cache
236+
// must drop it rather than hold two sizes.
236237
[surfaceManager presentSurfaces:@[ CreatePresentInfo(surfaceA2) ] atTime:0 notify:nil];
237-
EXPECT_EQ(surfaceManager.backBufferCache.count, 2ul);
238+
EXPECT_EQ(surfaceManager.backBufferCache.count, 1ul);
238239

239240
surfaceA1.isInUseOverride = NO;
240241

0 commit comments

Comments
 (0)