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
Closed
Changes from 1 commit
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
57 changes: 57 additions & 0 deletions
57
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,57 @@ | ||
// 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 backing stores used for | ||
RichardJCai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* rendering Flutter content and presents Flutter content and Platform views. | ||
*/ | ||
class FlutterMacOSGLCompositor { | ||
RichardJCai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public: | ||
FlutterMacOSGLCompositor(FlutterViewController* view_controller, | ||
RichardJCai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
NSOpenGLContext* open_gl_context); | ||
|
||
virtual ~FlutterMacOSGLCompositor(); | ||
RichardJCai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Creates a backing store according to FlutterBackingStoreConfig | ||
RichardJCai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// by modifying backing_store_out. | ||
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); | ||
|
||
protected: | ||
RichardJCai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
FlutterViewController* view_controller_; | ||
PresentCallback present_callback_; | ||
NSOpenGLContext* open_gl_context_; | ||
|
||
RichardJCai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Creates a FlutterSurfaceManager and uses the FlutterSurfaceManager's | ||
// underlying FBO and texture in the backing store. | ||
bool CreateBackingStoreUsingSurfaceManager( | ||
const FlutterBackingStoreConfig* config, | ||
FlutterBackingStore* backing_store_out); | ||
|
||
FML_DISALLOW_COPY_AND_ASSIGN(FlutterMacOSGLCompositor); | ||
}; | ||
|
||
} // namespace flutter |
94 changes: 94 additions & 0 deletions
94
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,94 @@ | ||
// 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, | ||
NSOpenGLContext* open_gl_context) | ||
: view_controller_(view_controller), open_gl_context_(open_gl_context) {} | ||
|
||
FlutterMacOSGLCompositor::~FlutterMacOSGLCompositor() = default; | ||
|
||
bool FlutterMacOSGLCompositor::CreateBackingStore(const FlutterBackingStoreConfig* config, | ||
FlutterBackingStore* backing_store_out) { | ||
return CreateBackingStoreUsingSurfaceManager(config, backing_store_out); | ||
} | ||
|
||
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->user_data; | ||
|
||
CGSize size = CGSizeMake(layer->size.width, layer->size.height); | ||
[view_controller_.flutterView frameBufferIDForSize:size]; | ||
[surfaceManager setLayerContentWithIOSurface:[surfaceManager getIOSurface]]; | ||
break; | ||
} | ||
case kFlutterLayerContentTypePlatformView: | ||
// Add functionality in follow up PR. | ||
RichardJCai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
FML_CHECK(false) << "Presenting PlatformViews not yet supported"; | ||
break; | ||
}; | ||
} | ||
return present_callback_(); | ||
} | ||
|
||
bool FlutterMacOSGLCompositor::CreateBackingStoreUsingSurfaceManager( | ||
const FlutterBackingStoreConfig* config, | ||
FlutterBackingStore* backing_store_out) { | ||
FlutterSurfaceManager* surfaceManager = | ||
[[FlutterSurfaceManager alloc] initWithLayer:view_controller_.flutterView.layer | ||
openGLContext:open_gl_context_]; | ||
|
||
GLuint fbo = [surfaceManager getFramebuffer]; | ||
RichardJCai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
GLuint texture = [surfaceManager getTexture]; | ||
IOSurfaceRef* io_surface_ref = [surfaceManager getIOSurface]; | ||
|
||
CGSize size = CGSizeMake(config->size.width, config->size.height); | ||
|
||
[surfaceManager backTextureWithIOSurface:io_surface_ref size:size backingTexture:texture fbo:fbo]; | ||
|
||
backing_store_out->type = kFlutterBackingStoreTypeOpenGL; | ||
backing_store_out->user_data = (__bridge_retained void*)surfaceManager; | ||
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 = backing_store_out->user_data; | ||
backing_store_out->open_gl.framebuffer.destruction_callback = [](void* user_data) { | ||
if (user_data != nullptr) { | ||
CFRelease(user_data); | ||
} | ||
}; | ||
|
||
return true; | ||
} | ||
|
||
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/FlutterViewControllerTestsUtils.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, nil); | ||
|
||
bool flag = false; | ||
macos_compositor->SetPresentCallback([f = &flag]() { | ||
*f = true; | ||
return true; | ||
}); | ||
|
||
ASSERT_TRUE(macos_compositor->Present(nil, 0)); | ||
ASSERT_TRUE(flag); | ||
} | ||
|
||
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 |
54 changes: 48 additions & 6 deletions
54
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,60 @@ | ||
// 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*)layer openGLContext:(NSOpenGLContext*)opengLContext; | ||
|
||
- (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.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* Sets the CALayer content to the content of the provided ioSurface. | ||
*/ | ||
- (void)setLayerContentWithIOSurface:(IOSurfaceRef*)ioSurface; | ||
|
||
/** | ||
* Binds the IOSurface to the provided texture/framebuffer. | ||
*/ | ||
- (void)backTextureWithIOSurface:(IOSurfaceRef*)ioSurface | ||
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.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* 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; | ||
|
||
/** | ||
* Returns the kFront IOSurfaceRef. | ||
* The IOSurface is backed by the FBO provided by getFramebuffer | ||
* and texture provided by getTexture. The IOSurface is used | ||
* in FlutterMacOSCompositor's Present call. | ||
* The IOSurface is collected when the backing store that uses the | ||
* IOSurface is collected. | ||
*/ | ||
- (IOSurfaceRef*)getIOSurface; | ||
|
||
@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.