From 41f40a15ac1e401fcac0edef567d933ced315a94 Mon Sep 17 00:00:00 2001 From: Matej Knopp Date: Mon, 23 Aug 2021 18:02:02 +0200 Subject: [PATCH] macOS: Do not swap surface if nothing was painted (flutter/engine#28136) --- .../framework/Source/FlutterCompositor.h | 4 ++-- .../framework/Source/FlutterCompositor.mm | 4 ++-- .../macos/framework/Source/FlutterEngine.mm | 24 ++++++++++++------- .../framework/Source/FlutterEngineTest.mm | 2 +- .../framework/Source/FlutterGLCompositor.mm | 5 +++- .../Source/FlutterGLCompositorUnittests.mm | 2 +- .../Source/FlutterMetalCompositor.mm | 5 +++- .../Source/FlutterMetalCompositorUnittests.mm | 2 +- .../Source/FlutterMetalSurfaceManagerTest.mm | 1 + .../framework/Source/FlutterSurfaceManager.mm | 7 ++++++ 10 files changed, 39 insertions(+), 17 deletions(-) diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterCompositor.h b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterCompositor.h index 8e3bcff066..aca395edac 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterCompositor.h +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterCompositor.h @@ -46,7 +46,7 @@ class FlutterCompositor { // Present sets frame_started_ to false. virtual bool Present(const FlutterLayer** layers, size_t layers_count) = 0; - using PresentCallback = std::function; + using PresentCallback = std::function; // PresentCallback is called at the end of the Present function. void SetPresentCallback(const PresentCallback& present_callback); @@ -73,7 +73,7 @@ class FlutterCompositor { // Calls the present callback and ensures the frame status is updated // to frame ended, returning whether the present was successful or not. - bool EndFrame(); + bool EndFrame(bool has_flutter_content); // Creates a CALayer object which is backed by the supplied IOSurface, and // adds it to the root CALayer for this FlutterViewController's view. diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterCompositor.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterCompositor.mm index 487cc41b76..3015a716c6 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterCompositor.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterCompositor.mm @@ -29,8 +29,8 @@ void FlutterCompositor::StartFrame() { SetFrameStatus(FrameStatus::kStarted); } -bool FlutterCompositor::EndFrame() { - bool status = present_callback_(); +bool FlutterCompositor::EndFrame(bool has_flutter_content) { + bool status = present_callback_(has_flutter_content); SetFrameStatus(FrameStatus::kEnded); return status; } diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm index 66b5fa88f4..ab196fa2f7 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm @@ -340,10 +340,14 @@ static void OnPlatformMessage(const FlutterPlatformMessage* message, FlutterEngi FlutterMetalRenderer* metalRenderer = reinterpret_cast(_renderer); _macOSCompositor = std::make_unique(_viewController, metalRenderer.device); - _macOSCompositor->SetPresentCallback([weakSelf]() { - FlutterMetalRenderer* metalRenderer = - reinterpret_cast(weakSelf.renderer); - return [metalRenderer present:0 /*=textureID*/]; + _macOSCompositor->SetPresentCallback([weakSelf](bool has_flutter_content) { + if (has_flutter_content) { + FlutterMetalRenderer* metalRenderer = + reinterpret_cast(weakSelf.renderer); + return [metalRenderer present:0 /*=textureID*/] == YES; + } else { + return true; + } }); } else { FlutterOpenGLRenderer* openGLRenderer = reinterpret_cast(_renderer); @@ -351,10 +355,14 @@ static void OnPlatformMessage(const FlutterPlatformMessage* message, FlutterEngi _macOSCompositor = std::make_unique(_viewController, openGLRenderer.openGLContext); - _macOSCompositor->SetPresentCallback([weakSelf]() { - FlutterOpenGLRenderer* openGLRenderer = - reinterpret_cast(weakSelf.renderer); - return [openGLRenderer glPresent]; + _macOSCompositor->SetPresentCallback([weakSelf](bool has_flutter_content) { + if (has_flutter_content) { + FlutterOpenGLRenderer* openGLRenderer = + reinterpret_cast(weakSelf.renderer); + return [openGLRenderer glPresent] == YES; + } else { + return true; + } }); } diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngineTest.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngineTest.mm index 3414daa48b..56d6a62072 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngineTest.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngineTest.mm @@ -355,7 +355,7 @@ TEST(FlutterEngine, Compositor) { // Latch to ensure the entire layer tree has been generated and presented. fml::AutoResetWaitableEvent latch; auto compositor = engine.macOSCompositor; - compositor->SetPresentCallback([&]() { + compositor->SetPresentCallback([&](bool has_flutter_content) { latch.Signal(); return true; }); diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterGLCompositor.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterGLCompositor.mm index 89f9b55b32..c8c6ce7f93 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterGLCompositor.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterGLCompositor.mm @@ -77,6 +77,8 @@ bool FlutterGLCompositor::CollectBackingStore(const FlutterBackingStore* backing bool FlutterGLCompositor::Present(const FlutterLayer** layers, size_t layers_count) { SetFrameStatus(FrameStatus::kPresenting); + bool has_flutter_content = false; + for (size_t i = 0; i < layers_count; ++i) { const auto* layer = layers[i]; FlutterBackingStore* backing_store = const_cast(layer->backing_store); @@ -93,6 +95,7 @@ bool FlutterGLCompositor::Present(const FlutterLayer** layers, size_t layers_cou // and needs to be flipped vertically InsertCALayerForIOSurface(io_surface, CATransform3DMakeScale(1, -1, 1)); } + has_flutter_content = true; break; } case kFlutterLayerContentTypePlatformView: @@ -102,7 +105,7 @@ bool FlutterGLCompositor::Present(const FlutterLayer** layers, size_t layers_cou }; } - return EndFrame(); + return EndFrame(has_flutter_content); } } // namespace flutter diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterGLCompositorUnittests.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterGLCompositorUnittests.mm index 25fc355751..049c3ed7cd 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterGLCompositorUnittests.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterGLCompositorUnittests.mm @@ -17,7 +17,7 @@ TEST(FlutterGLCompositorTest, TestPresent) { std::make_unique(mockViewController, nullptr); bool flag = false; - macos_compositor->SetPresentCallback([f = &flag]() { + macos_compositor->SetPresentCallback([f = &flag](bool has_flutter_content) { *f = true; return true; }); diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterMetalCompositor.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterMetalCompositor.mm index aa36b4ae29..101df075db 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterMetalCompositor.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterMetalCompositor.mm @@ -76,6 +76,8 @@ bool FlutterMetalCompositor::CollectBackingStore(const FlutterBackingStore* back bool FlutterMetalCompositor::Present(const FlutterLayer** layers, size_t layers_count) { SetFrameStatus(FrameStatus::kPresenting); + bool has_flutter_content = false; + for (size_t i = 0; i < layers_count; ++i) { const auto* layer = layers[i]; FlutterBackingStore* backing_store = const_cast(layer->backing_store); @@ -88,6 +90,7 @@ bool FlutterMetalCompositor::Present(const FlutterLayer** layers, size_t layers_ IOSurfaceRef io_surface = [io_surface_holder ioSurface]; InsertCALayerForIOSurface(io_surface); } + has_flutter_content = true; break; } case kFlutterLayerContentTypePlatformView: @@ -97,7 +100,7 @@ bool FlutterMetalCompositor::Present(const FlutterLayer** layers, size_t layers_ }; } - return EndFrame(); + return EndFrame(has_flutter_content); } } // namespace flutter diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterMetalCompositorUnittests.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterMetalCompositorUnittests.mm index 0f4dc409fb..766acc6255 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterMetalCompositorUnittests.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterMetalCompositorUnittests.mm @@ -17,7 +17,7 @@ TEST(FlutterMetalCompositorTest, TestPresent) { std::make_unique(mockViewController, nullptr); bool flag = false; - macos_compositor->SetPresentCallback([f = &flag]() { + macos_compositor->SetPresentCallback([f = &flag](bool has_flutter_content) { *f = true; return true; }); diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterMetalSurfaceManagerTest.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterMetalSurfaceManagerTest.mm index 7a739e7535..7776077638 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterMetalSurfaceManagerTest.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterMetalSurfaceManagerTest.mm @@ -53,6 +53,7 @@ TEST(FlutterMetalSurfaceManager, EnsureSizeUpdatesSizeForBackBuffer) { FlutterMetalSurfaceManager* surfaceManager = CreateSurfaceManager(); CGSize size = CGSizeMake(100, 50); [surfaceManager ensureSurfaceSize:size]; + [surfaceManager renderBuffer]; // make sure we have back buffer [surfaceManager swapBuffers]; id texture = (reinterpret_cast([surfaceManager renderBuffer])).texture; diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterSurfaceManager.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterSurfaceManager.mm index 054aaa22ff..9c212ba104 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterSurfaceManager.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterSurfaceManager.mm @@ -58,6 +58,13 @@ static const double kIdleDelay = 1.0; } - (void)swapBuffers { +#ifndef NDEBUG + // swapBuffers should not be called unless a frame was drawn + @synchronized(self) { + assert(_frameInProgress); + } +#endif + _contentLayer.frame = _containingLayer.bounds; _contentLayer.transform = _contentTransform; IOSurfaceRef contentIOSurface = [_ioSurfaces[kFlutterSurfaceManagerBackBuffer] ioSurface];