Skip to content

Commit

Permalink
[macos] Support smooth resizing for Metal (flutter#23924)
Browse files Browse the repository at this point in the history
Coordinate command buffer submission with Core Animation
scheduling to ensure smooth resizing.

Fixes: flutter/flutter#74056
  • Loading branch information
iskakaushik authored and hjfreyer committed Mar 22, 2021
1 parent 4cf93e3 commit 87da0c7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
Expand Up @@ -78,14 +78,19 @@ - (FlutterRenderBackingStore*)backingStore {
}

- (void)resizeSynchronizerFlush:(nonnull FlutterResizeSynchronizer*)synchronizer {
// TODO (kaushikiska): Support smooth resizing on Metal.
// no-op when using Metal rendering backend.
}

- (void)resizeSynchronizerCommit:(nonnull FlutterResizeSynchronizer*)synchronizer {
[CATransaction begin];
[CATransaction setDisableActions:YES];

id<MTLCommandBuffer> commandBuffer = [_commandQueue commandBuffer];
[commandBuffer commit];
[commandBuffer waitUntilScheduled];
[_surfaceManager swapBuffers];

[CATransaction commit];
}

@end
10 changes: 9 additions & 1 deletion shell/platform/darwin/macos/framework/Source/FlutterView.mm
Expand Up @@ -27,6 +27,7 @@ - (instancetype)initWithMTLDevice:(id<MTLDevice>)device
self = [super initWithFrame:NSZeroRect];
if (self) {
[self setWantsLayer:YES];
[self setLayerContentsRedrawPolicy:NSViewLayerContentsRedrawDuringViewResize];
_reshapeListener = reshapeListener;
_resizableBackingStoreProvider = [[FlutterMetalResizableBackingStoreProvider alloc]
initWithDevice:device
Expand All @@ -44,7 +45,14 @@ + (Class)layerClass {
}

- (CALayer*)makeBackingLayer {
return [CAMetalLayer layer];
CAMetalLayer* metalLayer = [CAMetalLayer layer];
// This is set to true to synchronize the presentation of the layer and its contents with Core
// Animation. When presenting the texture see `[FlutterMetalResizableBackingStoreProvider
// resizeSynchronizerCommit:]` we start a CATransaction and wait for the command buffer to be
// scheduled. This ensures that the resizing process is smooth.
metalLayer.presentsWithTransaction = YES;
metalLayer.autoresizingMask = kCALayerHeightSizable | kCALayerWidthSizable;
return metalLayer;
}
#endif

Expand Down

0 comments on commit 87da0c7

Please sign in to comment.