[macOS] Do not block main thread during resizing if there is no content (flutter/engine#35409)

* [macOS] Do not block main thread during resizing if there is no content

Fixes: https://github.com/flutter/flutter/issues/92673
This commit is contained in:
Matej Knopp
2022-09-22 22:45:43 +02:00
committed by GitHub
parent f70c0410dc
commit f56c06b371
9 changed files with 53 additions and 8 deletions

View File

@@ -439,11 +439,12 @@ static void OnPlatformMessage(const FlutterPlatformMessage* message, FlutterEngi
_macOSCompositor = std::make_unique<flutter::FlutterMetalCompositor>(
_viewController, _platformViewController, metalRenderer.device);
_macOSCompositor->SetPresentCallback([weakSelf](bool has_flutter_content) {
FlutterMetalRenderer* metalRenderer =
reinterpret_cast<FlutterMetalRenderer*>(weakSelf.renderer);
if (has_flutter_content) {
FlutterMetalRenderer* metalRenderer =
reinterpret_cast<FlutterMetalRenderer*>(weakSelf.renderer);
return [metalRenderer present:0 /*=textureID*/] == YES;
} else {
[metalRenderer presentWithoutContent];
return true;
}
});
@@ -454,11 +455,12 @@ static void OnPlatformMessage(const FlutterPlatformMessage* message, FlutterEngi
openGLRenderer.openGLContext);
_macOSCompositor->SetPresentCallback([weakSelf](bool has_flutter_content) {
FlutterOpenGLRenderer* openGLRenderer =
reinterpret_cast<FlutterOpenGLRenderer*>(weakSelf.renderer);
if (has_flutter_content) {
FlutterOpenGLRenderer* openGLRenderer =
reinterpret_cast<FlutterOpenGLRenderer*>(weakSelf.renderer);
return [openGLRenderer glPresent] == YES;
} else {
[openGLRenderer presentWithoutContent];
return true;
}
});

View File

@@ -34,6 +34,11 @@
*/
- (BOOL)present:(int64_t)textureID;
/**
* Tells the renderer that there is no Flutter content available for this frame.
*/
- (void)presentWithoutContent;
/**
* Populates the texture registry with the provided metalTexture.
*/

View File

@@ -107,6 +107,10 @@ static bool OnAcquireExternalTexture(FlutterEngine* engine,
return YES;
}
- (void)presentWithoutContent {
[_flutterView presentWithoutContent];
}
#pragma mark - FlutterTextureRegistrar methods.
- (BOOL)populateTextureWithIdentifier:(int64_t)textureID

View File

@@ -39,6 +39,11 @@
*/
- (BOOL)glPresent;
/**
* Tells the renderer that there is no Flutter content available for this frame.
*/
- (void)presentWithoutContent;
/**
* Called by the engine when framebuffer object ID is requested.
*/

View File

@@ -96,6 +96,10 @@ static bool OnAcquireExternalTexture(FlutterEngine* engine,
return true;
}
- (void)presentWithoutContent {
[_flutterView presentWithoutContent];
}
- (uint32_t)fboForFrameInfo:(const FlutterFrameInfo*)info {
CGSize size = CGSizeMake(info->size.width, info->size.height);
FlutterOpenGLRenderBackingStore* backingStore =

View File

@@ -75,6 +75,13 @@
*/
- (void)requestCommit;
/**
* Called from view to notify the synchronizer that there are no Flutter frames
* coming. Synchronizer must unblock main thread and not block until another
* frame is available.
*/
- (void)noFlutterContent;
/**
* Called when shutting down. Unblocks everything and prevents any further synchronization.
*/

View File

@@ -19,7 +19,7 @@
// Whether a frame was received; the synchronizer doesn't block platform thread during resize
// until it knows that framework is running and producing frames
BOOL _receivedFirstFrame;
BOOL _hasFlutterContent;
// If NO, requestCommit calls are ignored until shouldEnsureSurfaceForSize is called with
// proper size.
@@ -57,7 +57,7 @@
return;
}
if (!_receivedFirstFrame || _shuttingDown) {
if (!_hasFlutterContent || _shuttingDown) {
// No blocking until framework produces at least one frame
notify();
return;
@@ -93,7 +93,7 @@
- (BOOL)shouldEnsureSurfaceForSize:(CGSize)size {
std::unique_lock<std::mutex> lock(_mutex);
if (!_receivedFirstFrame) {
if (!_hasFlutterContent) {
return YES;
}
@@ -107,11 +107,12 @@
- (void)requestCommit {
std::unique_lock<std::mutex> lock(_mutex);
if (!_acceptingCommit || _shuttingDown) {
return;
}
_receivedFirstFrame = YES;
_hasFlutterContent = YES;
_pendingCommit = YES;
if (_waiting) { // BeginResize is in progress, interrupt it and schedule commit call
@@ -135,6 +136,13 @@
}
}
- (void)noFlutterContent {
std::unique_lock<std::mutex> lock(_mutex);
_hasFlutterContent = NO;
_acceptingCommit = YES;
_condBlockBeginResize.notify_all();
}
- (void)shutdown {
std::unique_lock<std::mutex> lock(_mutex);
_shuttingDown = YES;

View File

@@ -50,6 +50,12 @@
*/
- (void)present;
/**
* Called when there is no Flutter content available to render. This must be passed to resize
* synchronizer.
*/
- (void)presentWithoutContent;
/**
* Ensures that a backing store with requested size exists and returns the descriptor. Expected to
* be called on raster thread.

View File

@@ -72,6 +72,10 @@
[_resizeSynchronizer requestCommit];
}
- (void)presentWithoutContent {
[_resizeSynchronizer noFlutterContent];
}
- (void)reshaped {
CGSize scaledSize = [self convertSizeToBacking:self.bounds.size];
[_resizeSynchronizer beginResize:scaledSize