[macOS] Remove OpenGL rendering backend (flutter/engine#37448)

In flutter/buildroot#648 (rolled to the engine in flutter/engine#37380)
the minimum macOS SDK was bumped from 10.13 to 10.14. As of macOS 10.14,
Metal is available for all users. This eliminates the macOS OpenGL
rendering support code.

Note that this only removes the GL-specific code in the embedder. A
followup patch will land refactorings to clean up the remaining code,
for example, cases where a parent interface exists only because we had
both a GL and a Metal implementation. This is being does in two patches
in order to keep review clear and simple and to simplify rebases of any
outstanding patches that also touches this code.

Issue: https://github.com/flutter/flutter/issues/114445
This commit is contained in:
Chris Bracken
2022-11-09 15:39:48 +01:00
committed by GitHub
parent 7d1510c7b5
commit 57d02219ab
33 changed files with 18 additions and 1255 deletions

View File

@@ -2589,8 +2589,6 @@ FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/Accessibilit
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterAppDelegate.mm
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterBackingStore.h
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterBackingStore.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/FlutterChannelKeyResponder.h
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterChannelKeyResponder.mm
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterChannelKeyResponderUnittests.mm
@@ -2607,16 +2605,8 @@ FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterEngin
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterEngineTestUtils.h
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterEngineTestUtils.mm
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine_Internal.h
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureGL.h
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureGL.mm
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureMetal.h
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureMetal.mm
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterFrameBufferProvider.h
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterFrameBufferProvider.mm
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterFrameBufferProviderUnittests.mm
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterGLCompositor.h
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterGLCompositor.mm
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterGLCompositorUnittests.mm
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterIOSurfaceHolder.h
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterIOSurfaceHolder.mm
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterKeyPrimaryResponder.h
@@ -2638,9 +2628,6 @@ FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterMetal
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterMetalSurfaceManagerTest.mm
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterMouseCursorPlugin.h
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterMouseCursorPlugin.mm
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterOpenGLRenderer.h
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterOpenGLRenderer.mm
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterOpenGLRendererTest.mm
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformNodeDelegateMac.h
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformNodeDelegateMac.mm
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformNodeDelegateMacTest.mm
@@ -2648,8 +2635,6 @@ FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatf
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewController.mm
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewControllerTest.mm
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterRenderer.h
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterRenderingBackend.h
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterRenderingBackend.mm
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterResizableBackingStoreProvider.h
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterResizableBackingStoreProvider.mm
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizer.h
@@ -2677,8 +2662,6 @@ FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterViewE
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterViewProvider.h
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/KeyCodeMap.g.mm
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/KeyCodeMap_Internal.h
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/MacOSGLContextSwitch.h
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/MacOSGLContextSwitch.mm
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/TestFlutterPlatformView.h
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/TestFlutterPlatformView.mm
FILE: ../../../flutter/shell/platform/darwin/macos/framework/module.modulemap

View File

@@ -59,8 +59,6 @@ source_set("flutter_framework_source") {
"framework/Source/FlutterAppDelegate.mm",
"framework/Source/FlutterBackingStore.h",
"framework/Source/FlutterBackingStore.mm",
"framework/Source/FlutterBackingStoreData.h",
"framework/Source/FlutterBackingStoreData.mm",
"framework/Source/FlutterChannelKeyResponder.h",
"framework/Source/FlutterChannelKeyResponder.mm",
"framework/Source/FlutterCompositor.h",
@@ -71,14 +69,8 @@ source_set("flutter_framework_source") {
"framework/Source/FlutterEmbedderKeyResponder.mm",
"framework/Source/FlutterEngine.mm",
"framework/Source/FlutterEngine_Internal.h",
"framework/Source/FlutterExternalTextureGL.h",
"framework/Source/FlutterExternalTextureGL.mm",
"framework/Source/FlutterExternalTextureMetal.h",
"framework/Source/FlutterExternalTextureMetal.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/FlutterKeyPrimaryResponder.h",
@@ -96,15 +88,11 @@ source_set("flutter_framework_source") {
"framework/Source/FlutterMetalRenderer.mm",
"framework/Source/FlutterMouseCursorPlugin.h",
"framework/Source/FlutterMouseCursorPlugin.mm",
"framework/Source/FlutterOpenGLRenderer.h",
"framework/Source/FlutterOpenGLRenderer.mm",
"framework/Source/FlutterPlatformNodeDelegateMac.h",
"framework/Source/FlutterPlatformNodeDelegateMac.mm",
"framework/Source/FlutterPlatformViewController.h",
"framework/Source/FlutterPlatformViewController.mm",
"framework/Source/FlutterRenderer.h",
"framework/Source/FlutterRenderingBackend.h",
"framework/Source/FlutterRenderingBackend.mm",
"framework/Source/FlutterResizableBackingStoreProvider.h",
"framework/Source/FlutterResizableBackingStoreProvider.mm",
"framework/Source/FlutterResizeSynchronizer.h",
@@ -125,8 +113,6 @@ source_set("flutter_framework_source") {
"framework/Source/FlutterViewEngineProvider.mm",
"framework/Source/FlutterViewProvider.h",
"framework/Source/KeyCodeMap.g.mm",
"framework/Source/MacOSGLContextSwitch.h",
"framework/Source/MacOSGLContextSwitch.mm",
]
sources += _flutter_framework_headers
@@ -190,14 +176,11 @@ executable("flutter_desktop_darwin_unittests") {
"framework/Source/FlutterEngineTest.mm",
"framework/Source/FlutterEngineTestUtils.h",
"framework/Source/FlutterEngineTestUtils.mm",
"framework/Source/FlutterFrameBufferProviderUnittests.mm",
"framework/Source/FlutterGLCompositorUnittests.mm",
"framework/Source/FlutterKeyboardManagerUnittests.mm",
"framework/Source/FlutterMenuPluginTest.mm",
"framework/Source/FlutterMetalCompositorUnittests.mm",
"framework/Source/FlutterMetalRendererTest.mm",
"framework/Source/FlutterMetalSurfaceManagerTest.mm",
"framework/Source/FlutterOpenGLRendererTest.mm",
"framework/Source/FlutterPlatformNodeDelegateMacTest.mm",
"framework/Source/FlutterPlatformViewControllerTest.mm",
"framework/Source/FlutterTextInputPluginTest.mm",

View File

@@ -13,23 +13,6 @@
@end
/**
* Wraps an OpenGL frame buffer.
*/
@interface FlutterOpenGLRenderBackingStore : FlutterRenderBackingStore
/**
* Frame buffer ID referenced by this backing store instance.
*/
@property(nonatomic, readonly) uint32_t frameBufferID;
/**
* Initializes a backing store with the specified frame buffer id.
*/
- (nonnull instancetype)initWithFrameBufferID:(uint32_t)fboID;
@end
/**
* Wraps a Metal texture.
*/

View File

@@ -7,18 +7,6 @@
@implementation FlutterRenderBackingStore
@end
@implementation FlutterOpenGLRenderBackingStore
- (instancetype)initWithFrameBufferID:(uint32_t)fboID {
self = [super init];
if (self) {
_frameBufferID = fboID;
}
return self;
}
@end
@implementation FlutterMetalRenderBackingStore
- (instancetype)initWithTexture:(id<MTLTexture>)texture {

View File

@@ -1,30 +0,0 @@
// 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/FlutterFrameBufferProvider.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterIOSurfaceHolder.h"
#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>
/**
* FlutterBackingStoreData holds data to be stored in the
* BackingStore's user_data.
*/
@interface FlutterBackingStoreData : NSObject
- (nullable instancetype)initWithFbProvider:(nonnull FlutterFrameBufferProvider*)fbProvider
ioSurfaceHolder:(nonnull FlutterIOSurfaceHolder*)ioSurfaceHolder;
/**
* Provides the fbo for rendering the layer.
*/
@property(nonnull, nonatomic, readonly) FlutterFrameBufferProvider* frameBufferProvider;
/**
* Contains the IOSurfaceRef with the layer contents.
*/
@property(nonnull, nonatomic, readonly) FlutterIOSurfaceHolder* ioSurfaceHolder;
@end

View File

@@ -1,23 +0,0 @@
// 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"
#import <OpenGL/gl.h>
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterFrameBufferProvider.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterIOSurfaceHolder.h"
@implementation FlutterBackingStoreData
- (nullable instancetype)initWithFbProvider:(nonnull FlutterFrameBufferProvider*)fbProvider
ioSurfaceHolder:(nonnull FlutterIOSurfaceHolder*)ioSurfaceHolder {
if (self = [super init]) {
_frameBufferProvider = fbProvider;
_ioSurfaceHolder = ioSurfaceHolder;
}
return self;
}
@end

View File

@@ -10,15 +10,11 @@
#include <vector>
#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/FlutterGLCompositor.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterMenuPlugin.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterMetalCompositor.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterMetalRenderer.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterMouseCursorPlugin.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterOpenGLRenderer.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewController.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterRenderingBackend.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterViewController_Internal.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterViewEngineProvider.h"
#include "flutter/shell/platform/embedder/embedder.h"
@@ -251,11 +247,7 @@ static void OnPlatformMessage(const FlutterPlatformMessage* message, FlutterEngi
_embedderAPI.struct_size = sizeof(FlutterEngineProcTable);
FlutterEngineGetProcAddresses(&_embedderAPI);
if ([FlutterRenderingBackend renderUsingMetal]) {
_renderer = [[FlutterMetalRenderer alloc] initWithFlutterEngine:self];
} else {
_renderer = [[FlutterOpenGLRenderer alloc] initWithFlutterEngine:self];
}
_renderer = [[FlutterMetalRenderer alloc] initWithFlutterEngine:self];
NSNotificationCenter* notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self
@@ -436,16 +428,9 @@ static void OnPlatformMessage(const FlutterPlatformMessage* message, FlutterEngi
__weak FlutterEngine* weakSelf = self;
if ([FlutterRenderingBackend renderUsingMetal]) {
FlutterMetalRenderer* metalRenderer = reinterpret_cast<FlutterMetalRenderer*>(_renderer);
_macOSCompositor = std::make_unique<flutter::FlutterMetalCompositor>(
_viewProvider, _platformViewController, metalRenderer.device);
} else {
FlutterOpenGLRenderer* openGLRenderer = reinterpret_cast<FlutterOpenGLRenderer*>(_renderer);
[openGLRenderer.openGLContext makeCurrentContext];
_macOSCompositor =
std::make_unique<flutter::FlutterGLCompositor>(_viewProvider, openGLRenderer.openGLContext);
}
FlutterMetalRenderer* metalRenderer = reinterpret_cast<FlutterMetalRenderer*>(_renderer);
_macOSCompositor = std::make_unique<flutter::FlutterMetalCompositor>(
_viewProvider, _platformViewController, metalRenderer.device);
_macOSCompositor->SetPresentCallback([weakSelf](bool has_flutter_content) {
if (has_flutter_content) {
return [weakSelf.renderer present] == YES;

View File

@@ -23,8 +23,7 @@
@interface FlutterEngine (Test)
/**
* The FlutterCompositor object currently in use by the FlutterEngine. This is
* either a FlutterOpenGLCompositor or a FlutterMetalCompositor.
* The FlutterCompositor object currently in use by the FlutterEngine.
*
* May be nil if the compositor has not been initialized yet.
*/

View File

@@ -1,28 +0,0 @@
// 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/FlutterMacOSExternalTexture.h"
/**
* Used to bridge FlutterTexture object and handle the texture copy request the
* Flutter engine.
*/
@interface FlutterExternalTextureGL : NSObject <FlutterMacOSExternalTexture>
/**
* Initializes a texture adapter with |texture|.
*/
- (nonnull instancetype)initWithFlutterTexture:(nonnull id<FlutterTexture>)texture;
/**
* Accepts texture buffer copy request from the Flutter engine.
* When the user side marks the textureID as available, the Flutter engine will
* callback to this method and ask for populate the |openGLTexture| object,
* such as the texture type and the format of the pixel buffer and the texture object.
*/
- (BOOL)populateTexture:(nonnull FlutterOpenGLTexture*)openGLTexture;
@end

View File

@@ -1,85 +0,0 @@
// 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/FlutterExternalTextureGL.h"
#import <AppKit/AppKit.h>
#import <CoreVideo/CoreVideo.h>
#import <OpenGL/gl.h>
static void OnCVOpenGLTextureRelease(CVOpenGLTextureRef cvOpenGLTexture) {
CVOpenGLTextureRelease(cvOpenGLTexture);
}
@implementation FlutterExternalTextureGL {
/**
* OpenGL texture cache.
*/
CVOpenGLTextureCacheRef _openGLTextureCache;
/**
* User side texture object, used to copy pixel buffer.
*/
id<FlutterTexture> _texture;
}
- (instancetype)initWithFlutterTexture:(id<FlutterTexture>)texture {
self = [super init];
if (self) {
_texture = texture;
}
return self;
}
- (int64_t)textureID {
return reinterpret_cast<int64_t>(_texture);
}
- (BOOL)populateTexture:(FlutterOpenGLTexture*)openGLTexture {
// Copy the pixel buffer from the FlutterTexture instance implemented on the user side.
CVPixelBufferRef pixelBuffer = [_texture copyPixelBuffer];
if (!pixelBuffer) {
return NO;
}
// Create the opengl texture cache if necessary.
if (!_openGLTextureCache) {
CGLContextObj context = [NSOpenGLContext currentContext].CGLContextObj;
CGLPixelFormatObj format = CGLGetPixelFormat(context);
if (CVOpenGLTextureCacheCreate(kCFAllocatorDefault, NULL, context, format, NULL,
&_openGLTextureCache) != kCVReturnSuccess) {
NSLog(@"Could not create texture cache.");
CVPixelBufferRelease(pixelBuffer);
return NO;
}
}
// Try to clear the cache of OpenGL textures to save memory.
CVOpenGLTextureCacheFlush(_openGLTextureCache, 0);
CVOpenGLTextureRef cvOpenGLTexture = NULL;
if (CVOpenGLTextureCacheCreateTextureFromImage(kCFAllocatorDefault, _openGLTextureCache,
pixelBuffer, NULL,
&cvOpenGLTexture) != kCVReturnSuccess) {
CVPixelBufferRelease(pixelBuffer);
return NO;
}
openGLTexture->target = static_cast<uint32_t>(CVOpenGLTextureGetTarget(cvOpenGLTexture));
openGLTexture->name = static_cast<uint32_t>(CVOpenGLTextureGetName(cvOpenGLTexture));
openGLTexture->format = static_cast<uint32_t>(GL_RGBA8);
openGLTexture->destruction_callback = (VoidCallback)OnCVOpenGLTextureRelease;
openGLTexture->user_data = cvOpenGLTexture;
openGLTexture->width = CVPixelBufferGetWidth(pixelBuffer);
openGLTexture->height = CVPixelBufferGetHeight(pixelBuffer);
CVPixelBufferRelease(pixelBuffer);
return YES;
}
- (void)dealloc {
CVOpenGLTextureCacheRelease(_openGLTextureCache);
}
@end

View File

@@ -1,24 +0,0 @@
// 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>
/**
* Creates framebuffers and their backing textures.
*/
@interface FlutterFrameBufferProvider : NSObject
- (nullable instancetype)initWithOpenGLContext:(nonnull const NSOpenGLContext*)openGLContext;
/**
* Returns the id of the framebuffer.
*/
- (uint32_t)glFrameBufferId;
/**
* Returns the id of the backing texture..
*/
- (uint32_t)glTextureId;
@end

View File

@@ -1,57 +0,0 @@
// 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/FlutterFrameBufferProvider.h"
#import <OpenGL/gl.h>
#import "flutter/shell/platform/darwin/macos/framework/Source/MacOSGLContextSwitch.h"
@interface FlutterFrameBufferProvider () {
const NSOpenGLContext* _openGLContext;
uint32_t _frameBufferId;
uint32_t _backingTexture;
}
@end
@implementation FlutterFrameBufferProvider
- (instancetype)initWithOpenGLContext:(const NSOpenGLContext*)openGLContext {
if (self = [super init]) {
_openGLContext = openGLContext;
MacOSGLContextSwitch context_switch(_openGLContext);
glGenFramebuffers(1, &_frameBufferId);
glGenTextures(1, &_backingTexture);
[self createFramebuffer];
}
return self;
}
- (void)createFramebuffer {
glBindFramebuffer(GL_FRAMEBUFFER, _frameBufferId);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, _backingTexture);
glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);
}
- (uint32_t)glFrameBufferId {
return _frameBufferId;
}
- (uint32_t)glTextureId {
return _backingTexture;
}
- (void)dealloc {
MacOSGLContextSwitch context_switch(_openGLContext);
glDeleteFramebuffers(1, &_frameBufferId);
glDeleteTextures(1, &_backingTexture);
}
@end

View File

@@ -1,41 +0,0 @@
// 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/FlutterFrameBufferProvider.h"
#import "flutter/testing/testing.h"
#import <OpenGL/gl.h>
namespace flutter::testing {
TEST(FlutterFrameBufferProviderTest, TestCreate) {
NSOpenGLPixelFormatAttribute attributes[] = {
NSOpenGLPFAColorSize, 24, NSOpenGLPFAAlphaSize, 8, 0,
};
NSOpenGLPixelFormat* pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes];
NSOpenGLContext* context = [[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext:nil];
[context makeCurrentContext];
FlutterFrameBufferProvider* framebufferProvider =
[[FlutterFrameBufferProvider alloc] initWithOpenGLContext:context];
GLuint fbo = [framebufferProvider glFrameBufferId];
GLuint texture = [framebufferProvider glTextureId];
// Normally we'd back this using an IOSurface but for this test let's just create a TexImage2D
// with no backing data.
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, texture);
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_RECTANGLE_ARB, texture,
0);
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
EXPECT_TRUE(status == GL_FRAMEBUFFER_COMPLETE);
}
} // namespace flutter::testing

View File

@@ -1,57 +0,0 @@
// 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.
#ifndef FLUTTER_GL_COMPOSITOR_H_
#define FLUTTER_GL_COMPOSITOR_H_
#include "flutter/fml/macros.h"
#include "flutter/shell/platform/darwin/macos/framework/Source/FlutterCompositor.h"
#include "flutter/shell/platform/darwin/macos/framework/Source/FlutterSurfaceManager.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 FlutterCompositor {
public:
FlutterGLCompositor(id<FlutterViewProvider> view_provider,
NSOpenGLContext* opengl_context);
virtual ~FlutterGLCompositor() = default;
// Creates a BackingStore and saves updates the backing_store_out
// data with the new BackingStore data.
// If the backing store is being requested for the first time
// for a given frame, this compositor does not create a new backing
// store but rather returns the backing store associated with the
// FlutterView'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 destruction_callback field which will
// be called when the embedder collects the backing store.
bool CreateBackingStore(const FlutterBackingStoreConfig* config,
FlutterBackingStore* backing_store_out) override;
// Releases the memory for any state used by the backing store.
bool CollectBackingStore(const FlutterBackingStore* backing_store) override;
// Presents the FlutterLayers by updating FlutterView(s) using the
// layer content.
// Present sets frame_started_ to false.
bool Present(const FlutterLayer** layers, size_t layers_count) override;
private:
const NSOpenGLContext* open_gl_context_;
FML_DISALLOW_COPY_AND_ASSIGN(FlutterGLCompositor);
};
} // namespace flutter
#endif // FLUTTER_GL_COMPOSITOR_H_

View File

@@ -1,122 +0,0 @@
// 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 <OpenGL/gl.h>
#include "flutter/fml/logging.h"
#include "flutter/fml/platform/darwin/cf_utils.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterBackingStore.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"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkSurface.h"
#include "third_party/skia/include/gpu/gl/GrGLAssembleInterface.h"
#include "third_party/skia/include/utils/mac/SkCGUtils.h"
namespace flutter {
FlutterGLCompositor::FlutterGLCompositor(id<FlutterViewProvider> view_provider,
NSOpenGLContext* opengl_context)
: FlutterCompositor(view_provider), open_gl_context_(opengl_context) {}
bool FlutterGLCompositor::CreateBackingStore(const FlutterBackingStoreConfig* config,
FlutterBackingStore* backing_store_out) {
// TODO(dkwingsmt): This class only supports single-view for now. As more
// classes are gradually converted to multi-view, it should get the view ID
// from somewhere.
FlutterView* view = GetView(kFlutterDefaultViewId);
if (!view) {
return false;
}
CGSize size = CGSizeMake(config->size.width, config->size.height);
if (GetFrameStatus() != FrameStatus::kStarted) {
StartFrame();
// If the backing store is for the first layer, return the fbo for the
// FlutterView.
FlutterOpenGLRenderBackingStore* backingStore =
reinterpret_cast<FlutterOpenGLRenderBackingStore*>([view backingStoreForSize:size]);
backing_store_out->open_gl.framebuffer.name = backingStore.frameBufferID;
} else {
FlutterFrameBufferProvider* fb_provider =
[[FlutterFrameBufferProvider alloc] initWithOpenGLContext:open_gl_context_];
FlutterIOSurfaceHolder* io_surface_holder = [FlutterIOSurfaceHolder alloc];
GLuint fbo = [fb_provider glFrameBufferId];
GLuint texture = [fb_provider glTextureId];
[io_surface_holder bindSurfaceToTexture:texture fbo:fbo size:size];
FlutterBackingStoreData* data =
[[FlutterBackingStoreData alloc] initWithFbProvider:fb_provider
ioSurfaceHolder:io_surface_holder];
backing_store_out->open_gl.framebuffer.name = fbo;
backing_store_out->open_gl.framebuffer.user_data = (__bridge_retained void*)data;
}
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.destruction_callback = [](void* user_data) {
if (user_data != nullptr) {
// This deletes the OpenGL framebuffer object and texture backing it.
CFRelease(user_data);
}
};
return true;
}
bool FlutterGLCompositor::CollectBackingStore(const FlutterBackingStore* backing_store) {
return true;
}
bool FlutterGLCompositor::Present(const FlutterLayer** layers, size_t layers_count) {
// TODO(dkwingsmt): This class only supports single-view for now. As more
// classes are gradually converted to multi-view, it should get the view ID
// from somewhere.
FlutterView* view = GetView(kFlutterDefaultViewId);
if (!view) {
return false;
}
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<FlutterBackingStore*>(layer->backing_store);
switch (layer->type) {
case kFlutterLayerContentTypeBackingStore: {
if (backing_store->open_gl.framebuffer.user_data) {
FlutterBackingStoreData* backing_store_data =
(__bridge FlutterBackingStoreData*)backing_store->open_gl.framebuffer.user_data;
FlutterIOSurfaceHolder* io_surface_holder = [backing_store_data ioSurfaceHolder];
IOSurfaceRef io_surface = [io_surface_holder ioSurface];
// The surface is an OpenGL texture, which means it has origin in bottom left corner
// and needs to be flipped vertically
InsertCALayerForIOSurface(view, io_surface, CATransform3DMakeScale(1, -1, 1));
}
has_flutter_content = true;
break;
}
case kFlutterLayerContentTypePlatformView:
// Add functionality in follow up PR.
FML_LOG(WARNING) << "Presenting PlatformViews not yet supported";
break;
};
}
return EndFrame(has_flutter_content);
}
} // namespace flutter

View File

@@ -1,63 +0,0 @@
// 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 <OCMock/OCMock.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"
@interface FlutterViewMockProviderGL : NSObject <FlutterViewProvider> {
FlutterView* _defaultView;
}
/**
* Create a FlutterViewMockProviderGL with the provided view as the default view.
*/
- (nonnull instancetype)initWithDefaultView:(nonnull FlutterView*)view;
@end
@implementation FlutterViewMockProviderGL
- (nonnull instancetype)initWithDefaultView:(nonnull FlutterView*)view {
self = [super init];
if (self != nil) {
_defaultView = view;
}
return self;
}
- (nullable FlutterView*)getView:(uint64_t)viewId {
if (viewId == kFlutterDefaultViewId) {
return _defaultView;
}
return nil;
}
@end
namespace flutter::testing {
namespace {
id<FlutterViewProvider> MockViewProvider() {
id viewMock = OCMClassMock([FlutterView class]);
return [[FlutterViewMockProviderGL alloc] initWithDefaultView:viewMock];
}
} // namespace
TEST(FlutterGLCompositorTest, TestPresent) {
std::unique_ptr<flutter::FlutterGLCompositor> macos_compositor =
std::make_unique<FlutterGLCompositor>(MockViewProvider(), nullptr);
bool flag = false;
macos_compositor->SetPresentCallback([f = &flag](bool has_flutter_content) {
*f = true;
return true;
});
ASSERT_TRUE(macos_compositor->Present(nil, 0));
ASSERT_TRUE(flag);
}
} // namespace flutter::testing

View File

@@ -10,11 +10,6 @@
*/
@interface FlutterIOSurfaceHolder : NSObject
/**
* Bind the IOSurface to the provided texture and fbo.
*/
- (void)bindSurfaceToTexture:(GLuint)texture fbo:(GLuint)fbo size:(CGSize)size;
/**
* Releases the current IOSurface if one exists
* and creates a new IOSurface with the specified size.

View File

@@ -4,8 +4,6 @@
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterIOSurfaceHolder.h"
#import <OpenGL/gl.h>
@interface FlutterIOSurfaceHolder () {
IOSurfaceRef _ioSurface;
}
@@ -13,24 +11,6 @@
@implementation FlutterIOSurfaceHolder
- (void)bindSurfaceToTexture:(GLuint)texture fbo:(GLuint)fbo size:(CGSize)size {
[self recreateIOSurfaceWithSize:size];
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, texture);
CGLTexImageIOSurface2D(CGLGetCurrentContext(), GL_TEXTURE_RECTANGLE_ARB, GL_RGBA, int(size.width),
int(size.height), GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, _ioSurface,
0 /* plane */);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_RECTANGLE_ARB, texture,
0);
NSAssert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE,
@"Framebuffer status check failed");
}
- (void)recreateIOSurfaceWithSize:(CGSize)size {
if (_ioSurface) {
CFRelease(_ioSurface);

View File

@@ -1,53 +0,0 @@
// 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>
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterRenderer.h"
/**
* Provides the renderer config needed to initialize the embedder engine and also handles external
* texture management. This is initialized during FlutterEngine creation and then attached to the
* FlutterView once the FlutterViewController is initializer.
*/
@interface FlutterOpenGLRenderer : FlutterTextureRegistrar <FlutterRenderer>
/**
* The resource context used by the engine for texture uploads. FlutterViews associated with this
* engine should be created to share with this context.
*/
@property(nonatomic, readonly, nullable) NSOpenGLContext* resourceContext;
/**
* The main OpenGL which will be used for rendering contents to the FlutterView.
*/
@property(readwrite, nonatomic, nonnull) NSOpenGLContext* openGLContext;
/**
* Called by the engine to make the context the engine should draw into current.
*/
- (BOOL)makeCurrent;
/**
* Called by the engine to clear the context the engine should draw into.
*/
- (BOOL)clearCurrent;
/**
* Called by the engine when framebuffer object ID is requested.
*/
- (uint32_t)fboForFrameInfo:(nonnull const FlutterFrameInfo*)info;
/**
* Makes the resource context the current context.
*/
- (BOOL)makeResourceCurrent;
/**
* Populates the texture registry with the provided openGLTexture.
*/
- (BOOL)populateTextureWithIdentifier:(int64_t)textureID
openGLTexture:(nonnull FlutterOpenGLTexture*)openGLTexture;
@end

View File

@@ -1,161 +0,0 @@
// 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/FlutterOpenGLRenderer.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine_Internal.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureGL.h"
#pragma mark - Static methods for openGL callbacks that require the engine.
static bool OnMakeCurrent(FlutterEngine* engine) {
FlutterOpenGLRenderer* openGLRenderer = reinterpret_cast<FlutterOpenGLRenderer*>(engine.renderer);
return [openGLRenderer makeCurrent];
}
static bool OnClearCurrent(FlutterEngine* engine) {
FlutterOpenGLRenderer* openGLRenderer = reinterpret_cast<FlutterOpenGLRenderer*>(engine.renderer);
return [openGLRenderer clearCurrent];
}
static bool OnPresent(FlutterEngine* engine) {
return [engine.renderer present];
}
static uint32_t OnFBO(FlutterEngine* engine, const FlutterFrameInfo* info) {
FlutterOpenGLRenderer* openGLRenderer = reinterpret_cast<FlutterOpenGLRenderer*>(engine.renderer);
return [openGLRenderer fboForFrameInfo:info];
}
static bool OnMakeResourceCurrent(FlutterEngine* engine) {
FlutterOpenGLRenderer* openGLRenderer = reinterpret_cast<FlutterOpenGLRenderer*>(engine.renderer);
return [openGLRenderer makeResourceCurrent];
}
static bool OnAcquireExternalTexture(FlutterEngine* engine,
int64_t textureIdentifier,
size_t width,
size_t height,
FlutterOpenGLTexture* openGlTexture) {
FlutterOpenGLRenderer* openGLRenderer = reinterpret_cast<FlutterOpenGLRenderer*>(engine.renderer);
return [openGLRenderer populateTextureWithIdentifier:textureIdentifier
openGLTexture:openGlTexture];
}
#pragma mark - FlutterOpenGLRenderer implementation.
@implementation FlutterOpenGLRenderer {
FlutterView* _flutterView;
// The context provided to the Flutter engine for rendering to the FlutterView. This is lazily
// created during initialization of the FlutterView. This is used to render content into the
// FlutterView.
NSOpenGLContext* _openGLContext;
// The context provided to the Flutter engine for resource loading.
NSOpenGLContext* _resourceContext;
}
- (instancetype)initWithFlutterEngine:(FlutterEngine*)flutterEngine {
self = [super initWithDelegate:self engine:flutterEngine];
return self;
}
- (void)setFlutterView:(FlutterView*)view {
_flutterView = view;
if (!view) {
_resourceContext = nil;
}
}
- (BOOL)makeCurrent {
if (!_openGLContext) {
return false;
}
[_openGLContext makeCurrentContext];
return true;
}
- (BOOL)clearCurrent {
[NSOpenGLContext clearCurrentContext];
return true;
}
- (BOOL)present {
if (!_openGLContext || !_flutterView) {
return NO;
}
[_flutterView present];
return YES;
}
- (void)presentWithoutContent {
if (!_flutterView) {
return;
}
[_flutterView presentWithoutContent];
}
- (uint32_t)fboForFrameInfo:(const FlutterFrameInfo*)info {
CGSize size = CGSizeMake(info->size.width, info->size.height);
FlutterOpenGLRenderBackingStore* backingStore =
reinterpret_cast<FlutterOpenGLRenderBackingStore*>([_flutterView backingStoreForSize:size]);
return backingStore.frameBufferID;
}
- (NSOpenGLContext*)resourceContext {
if (!_resourceContext) {
NSOpenGLPixelFormatAttribute attributes[] = {
NSOpenGLPFAColorSize, 24, NSOpenGLPFAAlphaSize, 8, 0,
};
NSOpenGLPixelFormat* pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes];
_resourceContext = [[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext:nil];
}
return _resourceContext;
}
- (NSOpenGLContext*)openGLContext {
if (!_openGLContext) {
NSOpenGLContext* shareContext = [self resourceContext];
_openGLContext = [[NSOpenGLContext alloc] initWithFormat:shareContext.pixelFormat
shareContext:shareContext];
}
return _openGLContext;
}
- (BOOL)makeResourceCurrent {
[self.resourceContext makeCurrentContext];
return YES;
}
#pragma mark - FlutterTextureRegistrar
- (BOOL)populateTextureWithIdentifier:(int64_t)textureID
openGLTexture:(FlutterOpenGLTexture*)openGLTexture {
id<FlutterMacOSExternalTexture> texture = [self getTextureWithID:textureID];
FlutterExternalTextureGL* glTexture = reinterpret_cast<FlutterExternalTextureGL*>(texture);
return [glTexture populateTexture:openGLTexture];
}
- (id<FlutterMacOSExternalTexture>)onRegisterTexture:(id<FlutterTexture>)texture {
return [[FlutterExternalTextureGL alloc] initWithFlutterTexture:texture];
}
- (FlutterRendererConfig)createRendererConfig {
const FlutterRendererConfig rendererConfig = {
.type = kOpenGL,
.open_gl.struct_size = sizeof(FlutterOpenGLRendererConfig),
.open_gl.make_current = reinterpret_cast<BoolCallback>(OnMakeCurrent),
.open_gl.clear_current = reinterpret_cast<BoolCallback>(OnClearCurrent),
.open_gl.present = reinterpret_cast<BoolCallback>(OnPresent),
.open_gl.fbo_with_frame_info_callback = reinterpret_cast<UIntFrameInfoCallback>(OnFBO),
.open_gl.fbo_reset_after_present = true,
.open_gl.make_resource_current = reinterpret_cast<BoolCallback>(OnMakeResourceCurrent),
.open_gl.gl_external_texture_frame_callback =
reinterpret_cast<TextureFrameCallback>(OnAcquireExternalTexture),
};
return rendererConfig;
}
@end

View File

@@ -1,141 +0,0 @@
// 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 <OCMock/OCMock.h>
#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/FlutterOpenGLRenderer.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterView.h"
#include "flutter/shell/platform/embedder/embedder.h"
#include "flutter/shell/platform/embedder/test_utils/proc_table_replacement.h"
#include "flutter/testing/testing.h"
// MOCK_ENGINE_PROC is leaky by design
// NOLINTBEGIN(clang-analyzer-core.StackAddressEscape)
@interface TestOpenGLEngine : FlutterEngine
@property(nonatomic, readwrite) id<FlutterRenderer> renderer;
- (nullable instancetype)initWithGLRenderer;
@end
@implementation TestOpenGLEngine
@synthesize renderer;
- (nullable instancetype)initWithGLRenderer {
NSString* fixtures = @(flutter::testing::GetFixturesPath());
FlutterDartProject* project = [[FlutterDartProject alloc]
initWithAssetsPath:fixtures
ICUDataPath:[fixtures stringByAppendingString:@"/icudtl.dat"]];
self = [self initWithName:@"test" project:project allowHeadlessExecution:true];
if (self) {
renderer = [[FlutterOpenGLRenderer alloc] initWithFlutterEngine:self];
}
return self;
}
@end
namespace flutter::testing {
TEST(FlutterOpenGLRenderer, RegisterExternalTexture) {
FlutterEngine* engine = [[TestOpenGLEngine alloc] initWithGLRenderer];
EXPECT_TRUE([engine runWithEntrypoint:@"main"]);
id<FlutterTexture> flutterTexture = OCMProtocolMock(@protocol(FlutterTexture));
bool called = false;
engine.embedderAPI.RegisterExternalTexture =
MOCK_ENGINE_PROC(RegisterExternalTexture, [&](auto engine, int64_t textureIdentifier) {
called = true;
EXPECT_EQ(textureIdentifier, reinterpret_cast<int64_t>(flutterTexture));
return kSuccess;
});
FlutterOpenGLRenderer* openGLRenderer = reinterpret_cast<FlutterOpenGLRenderer*>(engine.renderer);
[openGLRenderer registerTexture:flutterTexture];
EXPECT_TRUE(called);
[engine shutDownEngine];
}
TEST(FlutterOpenGLRenderer, UnregisterExternalTexture) {
FlutterEngine* engine = [[TestOpenGLEngine alloc] initWithGLRenderer];
EXPECT_TRUE([engine runWithEntrypoint:@"main"]);
id<FlutterTexture> flutterTexture = OCMProtocolMock(@protocol(FlutterTexture));
bool called = false;
FlutterOpenGLRenderer* openGLRenderer = reinterpret_cast<FlutterOpenGLRenderer*>(engine.renderer);
int64_t registeredTextureId = [openGLRenderer registerTexture:flutterTexture];
engine.embedderAPI.UnregisterExternalTexture =
MOCK_ENGINE_PROC(UnregisterExternalTexture, [&](auto engine, int64_t textureIdentifier) {
called = true;
EXPECT_EQ(textureIdentifier, registeredTextureId);
return kSuccess;
});
[openGLRenderer unregisterTexture:registeredTextureId];
EXPECT_TRUE(called);
[engine shutDownEngine];
}
TEST(FlutterOpenGLRenderer, MarkExternalTextureFrameAvailable) {
FlutterEngine* engine = [[TestOpenGLEngine alloc] initWithGLRenderer];
EXPECT_TRUE([engine runWithEntrypoint:@"main"]);
id<FlutterTexture> flutterTexture = OCMProtocolMock(@protocol(FlutterTexture));
bool called = false;
FlutterOpenGLRenderer* openGLRenderer = reinterpret_cast<FlutterOpenGLRenderer*>(engine.renderer);
int64_t registeredTextureId = [openGLRenderer registerTexture:flutterTexture];
engine.embedderAPI.MarkExternalTextureFrameAvailable = MOCK_ENGINE_PROC(
MarkExternalTextureFrameAvailable, [&](auto engine, int64_t textureIdentifier) {
called = true;
EXPECT_EQ(textureIdentifier, registeredTextureId);
return kSuccess;
});
[openGLRenderer textureFrameAvailable:registeredTextureId];
EXPECT_TRUE(called);
[engine shutDownEngine];
}
TEST(FlutterOpenGLRenderer, PresetDelegatesToFlutterView) {
FlutterEngine* engine = [[TestOpenGLEngine alloc] initWithGLRenderer];
FlutterOpenGLRenderer* renderer = [[FlutterOpenGLRenderer alloc] initWithFlutterEngine:engine];
id mockFlutterView = OCMClassMock([FlutterView class]);
[(FlutterView*)[mockFlutterView expect] present];
[renderer setFlutterView:mockFlutterView];
[renderer openGLContext];
[renderer present];
}
TEST(FlutterOpenGLRenderer, FBOReturnedByFlutterView) {
FlutterEngine* engine = [[TestOpenGLEngine alloc] initWithGLRenderer];
FlutterOpenGLRenderer* renderer = [[FlutterOpenGLRenderer alloc] initWithFlutterEngine:engine];
id mockFlutterView = OCMClassMock([FlutterView class]);
FlutterFrameInfo frameInfo;
frameInfo.struct_size = sizeof(FlutterFrameInfo);
FlutterUIntSize dimensions;
dimensions.width = 100;
dimensions.height = 200;
frameInfo.size = dimensions;
CGSize size = CGSizeMake(dimensions.width, dimensions.height);
[[mockFlutterView expect] backingStoreForSize:size];
[renderer setFlutterView:mockFlutterView];
[renderer openGLContext];
[renderer fboForFrameInfo:&frameInfo];
}
} // namespace flutter::testing
// NOLINTEND(clang-analyzer-core.StackAddressEscape)

View File

@@ -1,19 +0,0 @@
// 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>
/**
* Flutter on macOS currently supports both OpenGL and Metal rendering backends. This class provides
* utilities for determining the rendering backend and the corresponging layer properties.
*/
@interface FlutterRenderingBackend : NSObject
/**
* Returns YES if the engine is supposed to use Metal as the rendering backend. On macOS versions
* >= 10.4 this is YES.
*/
+ (BOOL)renderUsingMetal;
@end

View File

@@ -1,22 +0,0 @@
// 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/FlutterRenderingBackend.h"
#import <Metal/Metal.h>
#import <QuartzCore/QuartzCore.h>
@implementation FlutterRenderingBackend
+ (BOOL)renderUsingMetal {
#ifdef SHELL_ENABLE_METAL
if (@available(macOS 10.14, *)) {
BOOL systemSupportsMetal = MTLCreateSystemDefaultDevice() != nil;
return systemSupportsMetal;
}
#endif
return NO;
}
@end

View File

@@ -26,21 +26,6 @@
@end
/**
* OpenGL-backed FlutterResizableBackingStoreProvider. Backing store in this context implies a frame
* buffer.
*/
@interface FlutterOpenGLResizableBackingStoreProvider
: NSObject <FlutterResizableBackingStoreProvider>
/**
* Creates a resizable backing store provider for the given CALayer.
*/
- (nonnull instancetype)initWithMainContext:(nonnull NSOpenGLContext*)mainContext
layer:(nonnull CALayer*)layer;
@end
/**
* Metal-backed FlutterResizableBackingStoreProvider. Backing store in this context implies a
* MTLTexture.

View File

@@ -4,50 +4,9 @@
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterResizableBackingStoreProvider.h"
#import <OpenGL/gl.h>
#import <QuartzCore/QuartzCore.h>
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterSurfaceManager.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/MacOSGLContextSwitch.h"
@implementation FlutterOpenGLResizableBackingStoreProvider {
NSOpenGLContext* _mainContext;
id<FlutterSurfaceManager> _surfaceManager;
}
- (instancetype)initWithMainContext:(NSOpenGLContext*)mainContext layer:(CALayer*)layer {
self = [super init];
if (self) {
_mainContext = mainContext;
_surfaceManager = [[FlutterGLSurfaceManager alloc] initWithLayer:layer
openGLContext:_mainContext];
}
return self;
}
- (void)onBackingStoreResized:(CGSize)size {
[_surfaceManager ensureSurfaceSize:size];
}
- (FlutterRenderBackingStore*)backingStore {
return [_surfaceManager renderBuffer];
}
- (void)resizeSynchronizerFlush:(nonnull FlutterResizeSynchronizer*)synchronizer {
MacOSGLContextSwitch context_switch(_mainContext);
glFlush();
}
- (void)resizeSynchronizerCommit:(nonnull FlutterResizeSynchronizer*)synchronizer {
[CATransaction begin];
[CATransaction setDisableActions:YES];
[_surfaceManager swapBuffers];
[CATransaction commit];
}
@end
@implementation FlutterMetalResizableBackingStoreProvider {
id<MTLDevice> _device;

View File

@@ -12,7 +12,7 @@
@protocol FlutterResizeSynchronizerDelegate
/**
* Invoked on raster thread; Delegate should flush the OpenGL context.
* Invoked on raster thread; Delegate should flush the graphics context.
*/
- (void)resizeSynchronizerFlush:(nonnull FlutterResizeSynchronizer*)synchronizer;

View File

@@ -60,9 +60,8 @@
/**
* Manages IOSurfaces for the FlutterEngine to render to.
*
* The backing store when rendering with OpenGL is a frame buffer backed by a texture, on Metal its
* a Metal texture. There are two IOSurfaces created during initialization, FlutterSurfaceManager
* manages the lifecycle of these.
* The backing store when rendering with on Metal its a Metal texture. There are two IOSurfaces
* created during initialization, FlutterSurfaceManager manages the lifecycle of these.
*/
@interface FlutterIOSurfaceManager : NSObject <FlutterSurfaceManager>
@@ -82,20 +81,6 @@
@end
/**
* FlutterSurfaceManager implementation where the IOSurfaces managed are backed by a frame buffers
* which are bound to offscreen textures.
*/
@interface FlutterGLSurfaceManager : FlutterIOSurfaceManager <FlutterIOSurfaceManagerDelegate>
/**
* Creates two IOSurfaces backed by frame buffers and their backing textures.
*/
- (nullable instancetype)initWithLayer:(nonnull CALayer*)containingLayer
openGLContext:(nonnull NSOpenGLContext*)openGLContext;
@end
/**
* FlutterSurfaceManager implementation where the IOSurfaces managed are backed by a Metal textures.
*/

View File

@@ -5,13 +5,10 @@
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterSurfaceManager.h"
#import <Metal/Metal.h>
#import <OpenGL/gl.h>
#include <algorithm>
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterFrameBufferProvider.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterIOSurfaceHolder.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/MacOSGLContextSwitch.h"
enum {
kFlutterSurfaceManagerFrontBuffer = 0,
@@ -128,53 +125,6 @@ static const double kIdleDelay = 1.0;
@end
@implementation FlutterGLSurfaceManager {
NSOpenGLContext* _openGLContext;
FlutterFrameBufferProvider* _frameBuffers[kFlutterSurfaceManagerBufferCount];
}
- (instancetype)initWithLayer:(CALayer*)containingLayer
openGLContext:(NSOpenGLContext*)openGLContext {
self = [super initWithLayer:containingLayer contentTransform:CATransform3DMakeScale(1, -1, 1)];
if (self) {
super.delegate = self;
_openGLContext = openGLContext;
}
return self;
}
- (FlutterRenderBackingStore*)renderBuffer {
[self ensureBackBuffer];
uint32_t fboID = [_frameBuffers[kFlutterSurfaceManagerBackBuffer] glFrameBufferId];
return [[FlutterOpenGLRenderBackingStore alloc] initWithFrameBufferID:fboID];
}
- (void)onSwapBuffers {
std::swap(_frameBuffers[kFlutterSurfaceManagerBackBuffer],
_frameBuffers[kFlutterSurfaceManagerFrontBuffer]);
}
- (void)onUpdateSurface:(FlutterIOSurfaceHolder*)surface
bufferIndex:(size_t)index
size:(CGSize)size {
if (_frameBuffers[index] == nil) {
_frameBuffers[index] =
[[FlutterFrameBufferProvider alloc] initWithOpenGLContext:_openGLContext];
}
MacOSGLContextSwitch context_switch(_openGLContext);
GLuint fbo = [_frameBuffers[index] glFrameBufferId];
GLuint texture = [_frameBuffers[index] glTextureId];
[surface bindSurfaceToTexture:texture fbo:fbo size:size];
}
- (void)onSurfaceReleased:(size_t)index {
_frameBuffers[index] = nil;
}
@end
@implementation FlutterMetalSurfaceManager {
id<MTLDevice> _device;
id<MTLCommandQueue> _commandQueue;

View File

@@ -42,15 +42,6 @@ constexpr uint64_t kFlutterDefaultViewId = 0;
reshapeListener:(nonnull id<FlutterViewReshapeListener>)reshapeListener
NS_DESIGNATED_INITIALIZER;
- (nullable instancetype)initWithFrame:(NSRect)frame
mainContext:(nonnull NSOpenGLContext*)mainContext
reshapeListener:(nonnull id<FlutterViewReshapeListener>)reshapeListener
NS_DESIGNATED_INITIALIZER;
- (nullable instancetype)initWithMainContext:(nonnull NSOpenGLContext*)mainContext
reshapeListener:
(nonnull id<FlutterViewReshapeListener>)reshapeListener;
- (nullable instancetype)initWithFrame:(NSRect)frameRect
pixelFormat:(nullable NSOpenGLPixelFormat*)format NS_UNAVAILABLE;
- (nonnull instancetype)initWithFrame:(NSRect)frameRect NS_UNAVAILABLE;
@@ -58,7 +49,7 @@ constexpr uint64_t kFlutterDefaultViewId = 0;
- (nonnull instancetype)init NS_UNAVAILABLE;
/**
* Flushes the OpenGL context and flips the surfaces. Expected to be called on raster thread.
* Flushes the graphics context and flips the surfaces. Expected to be called on raster thread.
*/
- (void)present;

View File

@@ -4,12 +4,9 @@
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterView.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterRenderingBackend.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizer.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterSurfaceManager.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/MacOSGLContextSwitch.h"
#import <OpenGL/gl.h>
#import <QuartzCore/QuartzCore.h>
@interface FlutterView () {
@@ -41,28 +38,6 @@
return self;
}
- (instancetype)initWithMainContext:(NSOpenGLContext*)mainContext
reshapeListener:(id<FlutterViewReshapeListener>)reshapeListener {
return [self initWithFrame:NSZeroRect mainContext:mainContext reshapeListener:reshapeListener];
}
- (instancetype)initWithFrame:(NSRect)frame
mainContext:(NSOpenGLContext*)mainContext
reshapeListener:(id<FlutterViewReshapeListener>)reshapeListener {
self = [super initWithFrame:frame];
if (self) {
[self setWantsLayer:YES];
[self setBackgroundColor:[NSColor blackColor]];
_reshapeListener = reshapeListener;
_resizableBackingStoreProvider =
[[FlutterOpenGLResizableBackingStoreProvider alloc] initWithMainContext:mainContext
layer:self.layer];
_resizeSynchronizer =
[[FlutterResizeSynchronizer alloc] initWithDelegate:_resizableBackingStoreProvider];
}
return self;
}
- (FlutterRenderBackingStore*)backingStoreForSize:(CGSize)size {
if ([_resizeSynchronizer shouldEnsureSurfaceForSize:size]) {
[_resizableBackingStoreProvider onBackingStoreResized:size];

View File

@@ -15,8 +15,6 @@
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterKeyPrimaryResponder.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterKeyboardManager.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterMetalRenderer.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterOpenGLRenderer.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterRenderingBackend.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterTextInputSemanticsObject.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterView.h"
#import "flutter/shell/platform/embedder/embedder.h"
@@ -361,27 +359,16 @@ static void CommonInit(FlutterViewController* controller) {
- (void)loadView {
FlutterView* flutterView;
if ([FlutterRenderingBackend renderUsingMetal]) {
FlutterMetalRenderer* metalRenderer = reinterpret_cast<FlutterMetalRenderer*>(_engine.renderer);
id<MTLDevice> device = metalRenderer.device;
id<MTLCommandQueue> commandQueue = metalRenderer.commandQueue;
if (!device || !commandQueue) {
NSLog(@"Unable to create FlutterView; no MTLDevice or MTLCommandQueue available.");
return;
}
flutterView = [[FlutterView alloc] initWithMTLDevice:device
commandQueue:commandQueue
reshapeListener:self];
} else {
FlutterOpenGLRenderer* openGLRenderer =
reinterpret_cast<FlutterOpenGLRenderer*>(_engine.renderer);
NSOpenGLContext* mainContext = openGLRenderer.openGLContext;
if (!mainContext) {
NSLog(@"Unable to create FlutterView; no GL context available.");
return;
}
flutterView = [[FlutterView alloc] initWithMainContext:mainContext reshapeListener:self];
FlutterMetalRenderer* metalRenderer = reinterpret_cast<FlutterMetalRenderer*>(_engine.renderer);
id<MTLDevice> device = metalRenderer.device;
id<MTLCommandQueue> commandQueue = metalRenderer.commandQueue;
if (!device || !commandQueue) {
NSLog(@"Unable to create FlutterView; no MTLDevice or MTLCommandQueue available.");
return;
}
flutterView = [[FlutterView alloc] initWithMTLDevice:device
commandQueue:commandQueue
reshapeListener:self];
if (_backgroundColor != nil) {
[flutterView setBackgroundColor:_backgroundColor];
}

View File

@@ -1,21 +0,0 @@
// 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>
/**
* RAII wrapper that sets provided NSOpenGLContext as current and restores
* original context on scope exit.
*/
class MacOSGLContextSwitch {
public:
explicit MacOSGLContextSwitch(const NSOpenGLContext* context);
~MacOSGLContextSwitch();
MacOSGLContextSwitch(const MacOSGLContextSwitch&) = delete;
MacOSGLContextSwitch(MacOSGLContextSwitch&&) = delete;
private:
NSOpenGLContext* previous_;
};

View File

@@ -1,18 +0,0 @@
// 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/MacOSGLContextSwitch.h"
MacOSGLContextSwitch::MacOSGLContextSwitch(const NSOpenGLContext* context) {
previous_ = [NSOpenGLContext currentContext];
[context makeCurrentContext];
}
MacOSGLContextSwitch::~MacOSGLContextSwitch() {
if (previous_) {
[previous_ makeCurrentContext];
} else {
[NSOpenGLContext clearCurrentContext];
}
}