Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Create FlutterGLCompositor.
Refactor rendering one layer to use the FlutterGLCompositor.

In this commit, FlutterGLCompositor only supports rendering one view
and does not support rendering platform views.
  • Loading branch information
RichardJCai committed Nov 23, 2020
commit 8f1c21b984300403aa227e19174bfd447c151af0
2 changes: 2 additions & 0 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,8 @@ FILE: ../../../flutter/shell/platform/darwin/macos/framework/Headers/FlutterPlug
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Headers/FlutterViewController.h
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Info.plist
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterAppDelegate.mm
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterBackingStoreData.h
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterBackingStoreData.mm
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterDartProject.mm
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterDartProject_Internal.h
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm
Expand Down
10 changes: 10 additions & 0 deletions shell/platform/darwin/macos/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ source_set("flutter_framework_source") {

sources = [
"framework/Source/FlutterAppDelegate.mm",
"framework/Source/FlutterBackingStoreData.h",
"framework/Source/FlutterBackingStoreData.mm",
"framework/Source/FlutterDartProject.mm",
"framework/Source/FlutterDartProject_Internal.h",
"framework/Source/FlutterEngine.mm",
Expand All @@ -54,6 +56,8 @@ source_set("flutter_framework_source") {
"framework/Source/FlutterExternalTextureGL.mm",
"framework/Source/FlutterFrameBufferProvider.h",
"framework/Source/FlutterFrameBufferProvider.mm",
"framework/Source/FlutterGLCompositor.h",
"framework/Source/FlutterGLCompositor.mm",
"framework/Source/FlutterIOSurfaceHolder.h",
"framework/Source/FlutterIOSurfaceHolder.mm",
"framework/Source/FlutterMouseCursorPlugin.h",
Expand All @@ -77,9 +81,12 @@ source_set("flutter_framework_source") {
sources += _flutter_framework_headers

deps = [
"//flutter/flow:flow",
"//flutter/fml",
"//flutter/shell/platform/common/cpp:common_cpp_switches",
"//flutter/shell/platform/darwin/common:framework_shared",
"//flutter/shell/platform/embedder:embedder_as_internal_library",
"//third_party/skia",
]

public_configs = [ "//flutter:config" ]
Expand Down Expand Up @@ -119,7 +126,10 @@ executable("flutter_desktop_darwin_unittests") {

sources = [
"framework/Source/FlutterEngineTest.mm",
"framework/Source/FlutterGLCompositorUnittests.mm",
"framework/Source/FlutterViewControllerTest.mm",
"framework/Source/FlutterViewControllerTestUtils.h",
"framework/Source/FlutterViewControllerTestUtils.mm",
]

cflags_objcc = [ "-fobjc-arc" ]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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>

/**
* FlutterBackingStoreData holds data to be stored in the
* BackingStore's user_data.
*/
@interface FlutterBackingStoreData : NSObject

- (nullable instancetype)initWithIsRootView:(bool)isRootView;

- (bool)isRootView;

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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/FlutterBackingStoreData.h"

#include <OpenGL/gl.h>

@interface FlutterBackingStoreData () {
bool _isRootView;
}
@end

@implementation FlutterBackingStoreData

- (nullable instancetype)initWithIsRootView:(bool)isRootView {
if (self = [super init]) {
_isRootView = isRootView;
}
return self;
}

- (bool)isRootView {
return _isRootView;
}

@end
50 changes: 49 additions & 1 deletion shell/platform/darwin/macos/framework/Source/FlutterEngine.mm
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterDartProject_Internal.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureGL.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterViewController_Internal.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterGLCompositor.h"
#import "flutter/shell/platform/embedder/embedder.h"

/**
Expand Down Expand Up @@ -197,6 +197,13 @@ @implementation FlutterEngine {

// Pointer to the Dart AOT snapshot and instruction data.
_FlutterEngineAOTData* _aotData;

// _macOSGLCompositor is created when the engine is created and
// it's destruction is handled by ARC when the engine is destroyed.
std::unique_ptr<flutter::FlutterGLCompositor> _macOSGLCompositor;

// FlutterCompositor is copied and used in embedder.cc.
FlutterCompositor _compositor;
}

- (instancetype)initWithName:(NSString*)labelPrefix project:(FlutterDartProject*)project {
Expand Down Expand Up @@ -306,6 +313,8 @@ - (BOOL)runWithEntrypoint:(NSString*)entrypoint {
flutterArguments.aot_data = _aotData;
}

flutterArguments.compositor = [self createFlutterCompositor];

FlutterEngineResult result = _embedderAPI.Initialize(
FLUTTER_ENGINE_VERSION, &rendererConfig, &flutterArguments, (__bridge void*)(self), &_engine);
if (result != kSuccess) {
Expand Down Expand Up @@ -360,6 +369,45 @@ - (void)setViewController:(FlutterViewController*)controller {
}
}

- (FlutterCompositor*)createFlutterCompositor {
[_mainOpenGLContext makeCurrentContext];

_macOSGLCompositor = std::make_unique<flutter::FlutterGLCompositor>(_viewController);

_compositor = {};
_compositor.struct_size = sizeof(FlutterCompositor);
_compositor.user_data = _macOSGLCompositor.get();

_compositor.create_backing_store_callback = [](const FlutterBackingStoreConfig* config, //
FlutterBackingStore* backing_store_out, //
void* user_data //
) {
return reinterpret_cast<flutter::FlutterGLCompositor*>(user_data)->CreateBackingStore(
config, backing_store_out);
};

_compositor.collect_backing_store_callback = [](const FlutterBackingStore* backing_store, //
void* user_data //
) {
return reinterpret_cast<flutter::FlutterGLCompositor*>(user_data)->CollectBackingStore(
backing_store);
};

_compositor.present_layers_callback = [](const FlutterLayer** layers, //
size_t layers_count, //
void* user_data //
) {
return reinterpret_cast<flutter::FlutterGLCompositor*>(user_data)->Present(layers,
layers_count);
};

__weak FlutterEngine* weak_self = self;
_macOSGLCompositor->SetPresentCallback(
[weak_self]() { return [weak_self engineCallbackOnPresent]; });

return &_compositor;
}

- (id<FlutterBinaryMessenger>)binaryMessenger {
// TODO(stuartmorgan): Switch to FlutterBinaryMessengerRelay to avoid plugins
// keeping the engine alive.
Expand Down
56 changes: 56 additions & 0 deletions shell/platform/darwin/macos/framework/Source/FlutterGLCompositor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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 {

// FlutterGLCompositor creates and manages the backing stores used for
// rendering Flutter content and presents Flutter content and Platform views.
// Platform views are not yet supported.
// FlutterGLCompositor is created and destroyed by FlutterEngine.
class FlutterGLCompositor {
public:
FlutterGLCompositor(FlutterViewController* view_controller);

// Creates a BackingStore and saves updates the backing_store_out
// data with the new BackingStore data.
// If the view requesting the backing store is the root view,
// we do not create a new backing store but rather return the
// backing store associated with the root view's FlutterSurfaceManager.
//
// 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_;

FML_DISALLOW_COPY_AND_ASSIGN(FlutterGLCompositor);
};

} // namespace flutter
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// 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/FlutterGLCompositor.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterBackingStoreData.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterFrameBufferProvider.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterIOSurfaceHolder.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 {

FlutterGLCompositor::FlutterGLCompositor(FlutterViewController* view_controller)
: view_controller_(view_controller),
open_gl_context_(view_controller.flutterView.openGLContext) {}

bool FlutterGLCompositor::CreateBackingStore(const FlutterBackingStoreConfig* config,
FlutterBackingStore* backing_store_out) {
CGSize size = CGSizeMake(config->size.width, config->size.height);
FlutterBackingStoreData* data =
[[FlutterBackingStoreData alloc] initWithIsRootView:config->is_root_view];

backing_store_out->type = kFlutterBackingStoreTypeOpenGL;
backing_store_out->is_cacheable = true;
backing_store_out->open_gl.type = kFlutterOpenGLTargetTypeFramebuffer;
backing_store_out->open_gl.framebuffer.target = GL_RGBA8;

if (config->is_root_view) {
// The root view uses FlutterSurfaceManager and is not cacheable since
// the fbo id changes on every present.
backing_store_out->is_cacheable = false;
auto fbo = [view_controller_.flutterView frameBufferIDForSize:size];
backing_store_out->open_gl.framebuffer.name = fbo;
} else {
FML_CHECK(false) << "Compositor only supports creating a backing store for the root view";
}

backing_store_out->open_gl.framebuffer.user_data = (__bridge_retained void*)data;
backing_store_out->open_gl.framebuffer.destruction_callback = [](void* user_data) {
if (user_data != nullptr) {
CFRelease(user_data);
}
};

return true;
}

bool FlutterGLCompositor::CollectBackingStore(const FlutterBackingStore* backing_store) {
return true;
}

bool FlutterGLCompositor::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: {
FlutterBackingStoreData* data =
(__bridge FlutterBackingStoreData*)(backing_store->open_gl.framebuffer.user_data);
if (![data isRootView]) {
FML_CHECK(false) << "Compositor only supports presenting the root view.";
}
break;
}
case kFlutterLayerContentTypePlatformView:
// Add functionality in follow up PR.
FML_CHECK(false) << "Presenting PlatformViews not yet supported";
break;
};
}
return present_callback_();
}

void FlutterGLCompositor::SetPresentCallback(
const FlutterGLCompositor::PresentCallback& present_callback) {
present_callback_ = present_callback;
}

} // namespace flutter
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/FlutterGLCompositor.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterViewControllerTestUtils.h"
#import "flutter/testing/testing.h"

namespace flutter::testing {

TEST(FlutterGLCompositorTest, TestPresent) {
id mockViewController = CreateMockViewController(nil);

std::unique_ptr<flutter::FlutterGLCompositor> macos_compositor =
std::make_unique<FlutterGLCompositor>(mockViewController);

bool flag = false;
macos_compositor->SetPresentCallback([f = &flag]() {
*f = true;
return true;
});

ASSERT_TRUE(macos_compositor->Present(nil, 0));
ASSERT_TRUE(flag);
}

} // flutter::testing
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,14 @@
#import "flutter/shell/platform/darwin/macos/framework/Headers/FlutterEngine.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterDartProject_Internal.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine_Internal.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterViewControllerTestUtils.h"
#include "flutter/testing/testing.h"

namespace flutter::testing {

// Returns a mock FlutterViewController that is able to work in environments
// without a real pasteboard.
id mockViewController(NSString* pasteboardString) {
NSString* fixtures = @(testing::GetFixturesPath());
FlutterDartProject* project = [[FlutterDartProject alloc]
initWithAssetsPath:fixtures
ICUDataPath:[fixtures stringByAppendingString:@"/icudtl.dat"]];
FlutterViewController* viewController = [[FlutterViewController alloc] initWithProject:project];

// Mock pasteboard so that this test will work in environments without a
// real pasteboard.
id pasteboardMock = OCMClassMock([NSPasteboard class]);
OCMExpect([pasteboardMock stringForType:[OCMArg any]]).andDo(^(NSInvocation* invocation) {
NSString* returnValue = pasteboardString.length > 0 ? pasteboardString : nil;
[invocation setReturnValue:&returnValue];
});
id viewControllerMock = OCMPartialMock(viewController);
OCMStub([viewControllerMock pasteboard]).andReturn(pasteboardMock);
return viewControllerMock;
}

TEST(FlutterViewController, HasStringsWhenPasteboardEmpty) {
// Mock FlutterViewController so that it behaves like the pasteboard is empty.
id viewControllerMock = mockViewController(nil);
id viewControllerMock = CreateMockViewController(nil);

// Call hasStrings and expect it to be false.
__block bool calledAfterClear = false;
Expand All @@ -56,7 +36,7 @@ id mockViewController(NSString* pasteboardString) {
TEST(FlutterViewController, HasStringsWhenPasteboardFull) {
// Mock FlutterViewController so that it behaves like the pasteboard has a
// valid string.
id viewControllerMock = mockViewController(@"some string");
id viewControllerMock = CreateMockViewController(@"some string");

// Call hasStrings and expect it to be true.
__block bool called = false;
Expand Down
Loading