This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
Refactor MacOS to use Compositor to create backing stores using FlutterSurfaceManager #22500
Closed
RichardJCai
wants to merge
5
commits into
flutter-team-archive:master
from
RichardJCai:compositor_with_fsm
Closed
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0177f9e
Refactor MacOS to use FlutterMacOSCompositor for creating backing sto…
RichardJCai 1a3a6d2
Addressing PR comments
RichardJCai 48e4362
For MacOS, rasterizer will only use the compositor if there is a plat…
RichardJCai 3f3396e
Refactor FlutterSurfaceManager methods
RichardJCai 74a950a
Add licenses to FlutterViewControllerTestUtils files
RichardJCai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
shell/platform/darwin/macos/framework/Source/FlutterMacOSGLCompositor.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #include <vector> | ||
|
|
||
| #include "flutter/fml/macros.h" | ||
| #include "flutter/shell/platform/darwin/macos/framework/Source/FlutterSurfaceManager.h" | ||
| #include "flutter/shell/platform/darwin/macos/framework/Source/FlutterViewController_Internal.h" | ||
| #include "flutter/shell/platform/embedder/embedder.h" | ||
| #include "third_party/skia/include/gpu/GrDirectContext.h" | ||
|
|
||
| namespace flutter { | ||
|
|
||
| // FlutterMacOSGLCompositor creates and manages the backing stores used for | ||
| // rendering Flutter content and presents Flutter content and Platform views. | ||
| // Platform views are not yet supported. | ||
| // FlutterMacOSGLCompositor is created and destroyed by FlutterEngine. | ||
| class FlutterMacOSGLCompositor { | ||
|
RichardJCai marked this conversation as resolved.
|
||
| public: | ||
| FlutterMacOSGLCompositor(FlutterViewController* view_controller); | ||
|
|
||
| // Creates a FlutterSurfaceManager and uses the FlutterSurfaceManager's | ||
| // underlying FBO and texture in the backing store. | ||
| // Any additional state allocated for the backing store and | ||
| // saved as user_data in the backing store must be collected | ||
| // in the backing_store's desctruction_callback field which will | ||
| // be called when the embedder collects the backing store. | ||
| bool CreateBackingStore(const FlutterBackingStoreConfig* config, | ||
| FlutterBackingStore* backing_store_out); | ||
|
|
||
| // Releases the memory for any state used by the backing store. | ||
| bool CollectBackingStore(const FlutterBackingStore* backing_store); | ||
|
|
||
| // Presents the FlutterLayers by updating FlutterView(s) using the | ||
| // layer content. | ||
| bool Present(const FlutterLayer** layers, size_t layers_count); | ||
|
|
||
| using PresentCallback = std::function<bool()>; | ||
|
|
||
| // PresentCallback is called at the end of the Present function. | ||
| void SetPresentCallback(const PresentCallback& present_callback); | ||
|
|
||
| private: | ||
| FlutterViewController* view_controller_; | ||
| PresentCallback present_callback_; | ||
| NSOpenGLContext* open_gl_context_; | ||
|
|
||
|
RichardJCai marked this conversation as resolved.
|
||
| FML_DISALLOW_COPY_AND_ASSIGN(FlutterMacOSGLCompositor); | ||
| }; | ||
|
|
||
| } // namespace flutter | ||
89 changes: 89 additions & 0 deletions
89
shell/platform/darwin/macos/framework/Source/FlutterMacOSGLCompositor.mm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterMacOSGLCompositor.h" | ||
|
|
||
| #import <OpenGL/gl.h> | ||
| #import "flutter/fml/logging.h" | ||
| #import "flutter/fml/platform/darwin/cf_utils.h" | ||
| #import "third_party/skia/include/core/SkCanvas.h" | ||
| #import "third_party/skia/include/core/SkSurface.h" | ||
| #import "third_party/skia/include/gpu/gl/GrGLAssembleInterface.h" | ||
| #import "third_party/skia/include/utils/mac/SkCGUtils.h" | ||
|
|
||
| #include <unistd.h> | ||
|
|
||
| namespace flutter { | ||
|
|
||
| FlutterMacOSGLCompositor::FlutterMacOSGLCompositor(FlutterViewController* view_controller) | ||
| : view_controller_(view_controller), | ||
| open_gl_context_(view_controller.flutterView.openGLContext) {} | ||
|
|
||
| bool FlutterMacOSGLCompositor::CreateBackingStore(const FlutterBackingStoreConfig* config, | ||
| FlutterBackingStore* backing_store_out) { | ||
| FlutterSurfaceManager* surfaceManager = | ||
| [[FlutterSurfaceManager alloc] initWithLayer:view_controller_.flutterView.layer | ||
| openGLContext:open_gl_context_ | ||
| numFramebuffers:1]; | ||
|
|
||
| GLuint fbo = [surfaceManager getFramebuffer]; | ||
| GLuint texture = [surfaceManager getTexture]; | ||
|
|
||
| CGSize size = CGSizeMake(config->size.width, config->size.height); | ||
| size_t kFlutterSurfaceManagerFrontBuffer = 0; | ||
| [surfaceManager backTextureWithIOSurface:kFlutterSurfaceManagerFrontBuffer | ||
| size:size | ||
| backingTexture:texture | ||
| fbo:fbo]; | ||
|
|
||
| backing_store_out->type = kFlutterBackingStoreTypeOpenGL; | ||
| backing_store_out->open_gl.type = kFlutterOpenGLTargetTypeFramebuffer; | ||
| backing_store_out->open_gl.framebuffer.target = GL_RGBA8; | ||
| backing_store_out->open_gl.framebuffer.name = fbo; | ||
| backing_store_out->open_gl.framebuffer.user_data = (__bridge_retained void*)surfaceManager; | ||
| backing_store_out->open_gl.framebuffer.destruction_callback = [](void* user_data) { | ||
| if (user_data != nullptr) { | ||
| CFRelease(user_data); | ||
| } | ||
| }; | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| bool FlutterMacOSGLCompositor::CollectBackingStore(const FlutterBackingStore* backing_store) { | ||
| // The memory for FlutterSurfaceManager is handled in the destruction callback. | ||
| // No other memory has to be collected. | ||
| return true; | ||
| } | ||
|
|
||
| bool FlutterMacOSGLCompositor::Present(const FlutterLayer** layers, size_t layers_count) { | ||
| for (size_t i = 0; i < layers_count; ++i) { | ||
| const auto* layer = layers[i]; | ||
| FlutterBackingStore* backing_store = const_cast<FlutterBackingStore*>(layer->backing_store); | ||
| switch (layer->type) { | ||
| case kFlutterLayerContentTypeBackingStore: { | ||
| FlutterSurfaceManager* surfaceManager = | ||
| (__bridge FlutterSurfaceManager*)backing_store->open_gl.framebuffer.user_data; | ||
|
|
||
| CGSize size = CGSizeMake(layer->size.width, layer->size.height); | ||
| [view_controller_.flutterView frameBufferIDForSize:size]; | ||
| size_t kFlutterSurfaceManagerFrontBuffer = 0; | ||
| [surfaceManager setLayerContentWithIOSurface:kFlutterSurfaceManagerFrontBuffer]; | ||
| break; | ||
| } | ||
| case kFlutterLayerContentTypePlatformView: | ||
| // Add functionality in follow up PR. | ||
|
RichardJCai marked this conversation as resolved.
|
||
| FML_CHECK(false) << "Presenting PlatformViews not yet supported"; | ||
| break; | ||
| }; | ||
| } | ||
| return present_callback_(); | ||
| } | ||
|
|
||
| void FlutterMacOSGLCompositor::SetPresentCallback( | ||
| const FlutterMacOSGLCompositor::PresentCallback& present_callback) { | ||
| present_callback_ = present_callback; | ||
| } | ||
|
|
||
| } // namespace flutter | ||
29 changes: 29 additions & 0 deletions
29
shell/platform/darwin/macos/framework/Source/FlutterMacOSGLCompositorUnittests.mm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #import <Foundation/Foundation.h> | ||
|
|
||
| #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterMacOSGLCompositor.h" | ||
| #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterViewControllerTestUtils.h" | ||
| #import "flutter/testing/testing.h" | ||
|
|
||
| namespace flutter::testing { | ||
|
|
||
| TEST(FlutterMacOSGLCompositorTest, TestPresent) { | ||
| id mockViewController = CreateMockViewController(nil); | ||
|
|
||
| std::unique_ptr<flutter::FlutterMacOSGLCompositor> macos_compositor = | ||
| std::make_unique<FlutterMacOSGLCompositor>(mockViewController); | ||
|
|
||
| bool flag = false; | ||
| macos_compositor->SetPresentCallback([f = &flag]() { | ||
| *f = true; | ||
| return true; | ||
| }); | ||
|
|
||
| ASSERT_TRUE(macos_compositor->Present(nil, 0)); | ||
| ASSERT_TRUE(flag); | ||
| } | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add a test to ensure after collect backing store gets called, we invoke the destructor. |
||
| } // flutter::testing | ||
47 changes: 41 additions & 6 deletions
47
shell/platform/darwin/macos/framework/Source/FlutterSurfaceManager.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,53 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #import <Cocoa/Cocoa.h> | ||
|
|
||
| // Manages the IOSurfaces for FlutterView | ||
| @interface FlutterSurfaceManager : NSObject | ||
|
|
||
| - (nullable instancetype)initWithLayer:(nonnull CALayer*)containingLayer | ||
| openGLContext:(nonnull NSOpenGLContext*)opengLContext; | ||
| - (instancetype)initWithLayer:(CALayer*)containingLayer | ||
| openGLContext:(NSOpenGLContext*)openGLContext | ||
| numFramebuffers:(int)numFramebuffers; | ||
|
|
||
| - (void)ensureSurfaceSize:(CGSize)size; | ||
| - (void)swapBuffers; | ||
|
|
||
| - (uint32_t)glFrameBufferId; | ||
|
|
||
| /** | ||
| * Sets the CALayer content to the content of _ioSurface[kBack]. | ||
| */ | ||
| - (void)setLayerContent; | ||
|
RichardJCai marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * Sets the CALayer content to the content of ioSurface at ioSurfaceNum. | ||
| */ | ||
| // TODO(richardjcai): Fix this and remove setLayerContent. | ||
| - (void)setLayerContentWithIOSurface:(int)ioSurfaceNum; | ||
|
|
||
| /** | ||
| * Binds the IOSurface to the provided texture/framebuffer. | ||
| */ | ||
| - (void)backTextureWithIOSurface:(int)ioSurfaceNum | ||
| size:(CGSize)size | ||
| backingTexture:(GLuint)texture | ||
| fbo:(GLuint)fbo; | ||
|
|
||
| // Methods used by FlutterMacOSCompositor to render Flutter content | ||
| // using a single Framebuffer/Texture/IOSurface. | ||
|
|
||
| /** | ||
| * Returns the kFront framebuffer. | ||
| * The framebuffer is used by FlutterMacOSCompositor to create a backing store. | ||
| * The framebuffer is collected when the backing store that uses the | ||
| * framebuffer is collected. | ||
| */ | ||
| - (uint32_t)getFramebuffer; | ||
|
RichardJCai marked this conversation as resolved.
Outdated
|
||
|
|
||
| /** | ||
| * Returns the kFront texture. | ||
| * The texture is used by FlutterMacOSCompositor to create a backing store. | ||
| * The texture is collected when the backing store that uses the | ||
| * texture is collected. | ||
| */ | ||
| - (uint32_t)getTexture; | ||
|
|
||
| @end | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.