Fix code style issues in MacOS embedder (flutter/engine#22270)

This commit is contained in:
Matej Knopp
2020-11-04 23:49:03 +01:00
committed by GitHub
parent 68eedd5ba3
commit 309f003ea8
13 changed files with 260 additions and 192 deletions

View File

@@ -1068,8 +1068,8 @@ FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterView.
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterViewController.mm FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterViewController.mm
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterViewControllerTest.mm FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterViewControllerTest.mm
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterViewController_Internal.h FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterViewController_Internal.h
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/MacOSSwitchableGLContext.h FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/MacOSGLContextSwitch.h
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/MacOSSwitchableGLContext.mm FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/MacOSGLContextSwitch.mm
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/fixtures/flutter_desktop_test.dart FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/fixtures/flutter_desktop_test.dart
FILE: ../../../flutter/shell/platform/darwin/macos/framework/module.modulemap FILE: ../../../flutter/shell/platform/darwin/macos/framework/module.modulemap
FILE: ../../../flutter/shell/platform/embedder/assets/EmbedderInfo.plist FILE: ../../../flutter/shell/platform/embedder/assets/EmbedderInfo.plist

View File

@@ -66,15 +66,13 @@ source_set("flutter_framework_source") {
"framework/Source/FlutterView.mm", "framework/Source/FlutterView.mm",
"framework/Source/FlutterViewController.mm", "framework/Source/FlutterViewController.mm",
"framework/Source/FlutterViewController_Internal.h", "framework/Source/FlutterViewController_Internal.h",
"framework/Source/MacOSSwitchableGLContext.h", "framework/Source/MacOSGLContextSwitch.h",
"framework/Source/MacOSSwitchableGLContext.mm", "framework/Source/MacOSGLContextSwitch.mm",
] ]
sources += _flutter_framework_headers sources += _flutter_framework_headers
deps = [ deps = [
"//flutter/flow:flow",
"//flutter/fml:fml",
"//flutter/shell/platform/common/cpp:common_cpp_switches", "//flutter/shell/platform/common/cpp:common_cpp_switches",
"//flutter/shell/platform/darwin/common:framework_shared", "//flutter/shell/platform/darwin/common:framework_shared",
"//flutter/shell/platform/embedder:embedder_as_internal_library", "//flutter/shell/platform/embedder:embedder_as_internal_library",

View File

@@ -52,6 +52,11 @@ static FlutterLocale FlutterLocaleFromNSLocale(NSLocale* locale) {
*/ */
- (bool)engineCallbackOnPresent; - (bool)engineCallbackOnPresent;
/**
* Called by the engine when framebuffer object ID is requested.
*/
- (uint32_t)engineCallbackOnFBO:(const FlutterFrameInfo*)info;
/** /**
* Makes the resource context the current context. * Makes the resource context the current context.
*/ */
@@ -146,8 +151,7 @@ static bool OnPresent(FlutterEngine* engine) {
} }
static uint32_t OnFBO(FlutterEngine* engine, const FlutterFrameInfo* info) { static uint32_t OnFBO(FlutterEngine* engine, const FlutterFrameInfo* info) {
CGSize size = CGSizeMake(info->size.width, info->size.height); return [engine engineCallbackOnFBO:info];
return [engine.viewController.flutterView getFrameBufferIdForSize:size];
} }
static bool OnMakeResourceCurrent(FlutterEngine* engine) { static bool OnMakeResourceCurrent(FlutterEngine* engine) {
@@ -469,6 +473,11 @@ static bool OnAcquireExternalTexture(FlutterEngine* engine,
return true; return true;
} }
- (uint32_t)engineCallbackOnFBO:(const FlutterFrameInfo*)info {
CGSize size = CGSizeMake(info->size.width, info->size.height);
return [_viewController.flutterView frameBufferIDForSize:size];
}
- (bool)engineCallbackOnClearCurrent { - (bool)engineCallbackOnClearCurrent {
[NSOpenGLContext clearCurrentContext]; [NSOpenGLContext clearCurrentContext];
return true; return true;

View File

@@ -1,59 +1,78 @@
// 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 <Cocoa/Cocoa.h>
@class FlutterResizeSynchronizer; @class FlutterResizeSynchronizer;
/**
* Implemented by FlutterView.
*/
@protocol FlutterResizeSynchronizerDelegate @protocol FlutterResizeSynchronizerDelegate
// Invoked on raster thread; Delegate should flush the OpenGL context /**
- (void)resizeSynchronizerFlush:(FlutterResizeSynchronizer*)synchronizer; * Invoked on raster thread; Delegate should flush the OpenGL context.
*/
- (void)resizeSynchronizerFlush:(nonnull FlutterResizeSynchronizer*)synchronizer;
// Invoked on platform thread; Delegate should flip the surfaces /**
- (void)resizeSynchronizerCommit:(FlutterResizeSynchronizer*)synchronizer; * Invoked on platform thread; Delegate should flip the surfaces.
*/
- (void)resizeSynchronizerCommit:(nonnull FlutterResizeSynchronizer*)synchronizer;
@end @end
// Encapsulates the logic for blocking platform thread during window resize as /**
// well as synchronizing the raster and platform thread during commit (presenting frame) * Encapsulates the logic for blocking platform thread during window resize as
// * well as synchronizing the raster and platform thread during commit (presenting frame).
// Flow during window resize *
// * Flow during window resize
// 1. Platform thread calls [synchronizer beginResize:notify:] *
// This will hold the platform thread until we're ready to display contents. * 1. Platform thread calls [synchronizer beginResize:notify:]
// 2. Raster thread calls [synchronizer shouldEnsureSurfaceForSize:] with target size * This will hold the platform thread until we're ready to display contents.
// This will return false for any size other than target size * 2. Raster thread calls [synchronizer shouldEnsureSurfaceForSize:] with target size
// 3. Raster thread calls [synchronizer requestCommit] * This will return false for any size other than target size
// Any commit calls before shouldEnsureSurfaceForSize: is called with the right * 3. Raster thread calls [synchronizer requestCommit]
// size are simply ignored; There's no point rasterizing and displaying frames * Any commit calls before shouldEnsureSurfaceForSize: is called with the right
// with wrong size. * size are simply ignored; There's no point rasterizing and displaying frames
// Both delegate methods (flush/commit) will be invoked before beginResize returns * with wrong size.
// * Both delegate methods (flush/commit) will be invoked before beginResize returns
// Flow during regular operation (no resizing) *
// * Flow during regular operation (no resizing)
// 1. Raster thread calls [synchronizer requestCommit] *
// This will invoke [delegate flush:] on raster thread and * 1. Raster thread calls [synchronizer requestCommit]
// [delegate commit:] on platform thread. The requestCommit call will be blocked * This will invoke [delegate flush:] on raster thread and
// until this is done. This is necessary to ensure that rasterizer won't start * [delegate commit:] on platform thread. The requestCommit call will be blocked
// rasterizing next frame before we flipped the surface, which must be performed * until this is done. This is necessary to ensure that rasterizer won't start
// on platform thread * rasterizing next frame before we flipped the surface, which must be performed
* on platform thread
*/
@interface FlutterResizeSynchronizer : NSObject @interface FlutterResizeSynchronizer : NSObject
- (instancetype)initWithDelegate:(id<FlutterResizeSynchronizerDelegate>)delegate; - (nullable instancetype)initWithDelegate:(nonnull id<FlutterResizeSynchronizerDelegate>)delegate;
// Blocks the platform thread until /**
// - shouldEnsureSurfaceForSize is called with proper size and * Blocks the platform thread until
// - requestCommit is called * - shouldEnsureSurfaceForSize is called with proper size and
// All requestCommit calls before `shouldEnsureSurfaceForSize` is called with * - requestCommit is called
// expected size are ignored; * All requestCommit calls before `shouldEnsureSurfaceForSize` is called with
// The notify block is invoked immediately after synchronizer mutex is acquired * expected size are ignored;
- (void)beginResize:(CGSize)size notify:(dispatch_block_t)notify; * The notify block is invoked immediately after synchronizer mutex is acquired.
*/
- (void)beginResize:(CGSize)size notify:(nonnull dispatch_block_t)notify;
// Returns whether the view should ensure surfaces with given size; /**
// This will be false during resizing for any size other than size specified * Returns whether the view should ensure surfaces with given size;
// during beginResize * This will be false during resizing for any size other than size specified
- (bool)shouldEnsureSurfaceForSize:(CGSize)size; * during beginResize.
*/
- (BOOL)shouldEnsureSurfaceForSize:(CGSize)size;
// Called from rasterizer thread, will block until delegate resizeSynchronizerCommit: /**
// method is called (on platform thread) * Called from rasterizer thread, will block until delegate resizeSynchronizerCommit:
* method is called (on platform thread).
*/
- (void)requestCommit; - (void)requestCommit;
@end @end

View File

@@ -1,103 +1,117 @@
// 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/FlutterResizeSynchronizer.h" #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizer.h"
#import <mutex> #include <mutex>
@interface FlutterResizeSynchronizer () { @interface FlutterResizeSynchronizer () {
uint32_t cookie; // counter to detect stale callbacks // Counter to detect stale callbacks.
uint32_t _cookie;
std::mutex mutex; std::mutex _mutex;
std::condition_variable condBlockBeginResize; // used to block [beginResize:]
std::condition_variable condBlockRequestCommit; // used to block [requestCommit]
bool acceptingCommit; // if false, requestCommit calls are ignored until // Used to block [beginResize:].
// shouldEnsureSurfaceForSize is called with proper size std::condition_variable _condBlockBeginResize;
bool waiting; // waiting for resize to finish // Used to block [requestCommit].
bool pendingCommit; // requestCommit was called and [delegate commit:] must be performed on std::condition_variable _condBlockRequestCommit;
// platform thread
CGSize newSize; // target size for resizing
__weak id<FlutterResizeSynchronizerDelegate> delegate; // If NO, requestCommit calls are ignored until shouldEnsureSurfaceForSize is called with
// proper size.
BOOL _acceptingCommit;
// Waiting for resize to finish.
BOOL _waiting;
// RequestCommit was called and [delegate commit:] must be performed on platform thread.
BOOL _pendingCommit;
// Target size for resizing.
CGSize _newSize;
__weak id<FlutterResizeSynchronizerDelegate> _delegate;
} }
@end @end
@implementation FlutterResizeSynchronizer @implementation FlutterResizeSynchronizer
- (instancetype)initWithDelegate:(id<FlutterResizeSynchronizerDelegate>)delegate_ { - (instancetype)initWithDelegate:(id<FlutterResizeSynchronizerDelegate>)delegate {
if (self = [super init]) { if (self = [super init]) {
acceptingCommit = true; _acceptingCommit = YES;
delegate = delegate_; _delegate = delegate;
} }
return self; return self;
} }
- (void)beginResize:(CGSize)size notify:(dispatch_block_t)notify { - (void)beginResize:(CGSize)size notify:(dispatch_block_t)notify {
std::unique_lock<std::mutex> lock(mutex); std::unique_lock<std::mutex> lock(_mutex);
if (!delegate) { if (!_delegate) {
return; return;
} }
++cookie; ++_cookie;
// from now on, ignore all incoming commits until the block below gets // from now on, ignore all incoming commits until the block below gets
// scheduled on raster thread // scheduled on raster thread
acceptingCommit = false; _acceptingCommit = NO;
// let pending commits finish to unblock the raster thread // let pending commits finish to unblock the raster thread
pendingCommit = false; _pendingCommit = NO;
condBlockBeginResize.notify_all(); _condBlockBeginResize.notify_all();
// let the engine send resize notification // let the engine send resize notification
notify(); notify();
newSize = size; _newSize = size;
waiting = true; _waiting = YES;
condBlockRequestCommit.wait(lock, [&] { return pendingCommit; }); _condBlockRequestCommit.wait(lock, [&] { return _pendingCommit; });
[delegate resizeSynchronizerFlush:self]; [_delegate resizeSynchronizerFlush:self];
[delegate resizeSynchronizerCommit:self]; [_delegate resizeSynchronizerCommit:self];
pendingCommit = false; _pendingCommit = NO;
condBlockBeginResize.notify_all(); _condBlockBeginResize.notify_all();
waiting = false; _waiting = NO;
} }
- (bool)shouldEnsureSurfaceForSize:(CGSize)size { - (BOOL)shouldEnsureSurfaceForSize:(CGSize)size {
std::unique_lock<std::mutex> lock(mutex); std::unique_lock<std::mutex> lock(_mutex);
if (!acceptingCommit) { if (!_acceptingCommit) {
if (CGSizeEqualToSize(newSize, size)) { if (CGSizeEqualToSize(_newSize, size)) {
acceptingCommit = true; _acceptingCommit = YES;
} }
} }
return acceptingCommit; return _acceptingCommit;
} }
- (void)requestCommit { - (void)requestCommit {
std::unique_lock<std::mutex> lock(mutex); std::unique_lock<std::mutex> lock(_mutex);
if (!acceptingCommit) { if (!_acceptingCommit) {
return; return;
} }
pendingCommit = true; _pendingCommit = YES;
if (waiting) { // BeginResize is in progress, interrupt it and schedule commit call if (_waiting) { // BeginResize is in progress, interrupt it and schedule commit call
condBlockRequestCommit.notify_all(); _condBlockRequestCommit.notify_all();
condBlockBeginResize.wait(lock, [&]() { return !pendingCommit; }); _condBlockBeginResize.wait(lock, [&]() { return !_pendingCommit; });
} else { } else {
// No resize, schedule commit on platform thread and wait until either done // No resize, schedule commit on platform thread and wait until either done
// or interrupted by incoming BeginResize // or interrupted by incoming BeginResize
[delegate resizeSynchronizerFlush:self]; [_delegate resizeSynchronizerFlush:self];
dispatch_async(dispatch_get_main_queue(), [self, cookie_ = cookie] { dispatch_async(dispatch_get_main_queue(), [self, cookie = _cookie] {
std::unique_lock<std::mutex> lock(mutex); std::unique_lock<std::mutex> lock(_mutex);
if (cookie_ == cookie) { if (cookie == _cookie) {
if (delegate) { if (_delegate) {
[delegate resizeSynchronizerCommit:self]; [_delegate resizeSynchronizerCommit:self];
} }
pendingCommit = false; _pendingCommit = NO;
condBlockBeginResize.notify_all(); _condBlockBeginResize.notify_all();
} }
}); });
condBlockBeginResize.wait(lock, [&]() { return !pendingCommit; }); _condBlockBeginResize.wait(lock, [&]() { return !_pendingCommit; });
} }
} }

View File

@@ -1,9 +1,14 @@
// 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 <Cocoa/Cocoa.h>
// Manages the IOSurfaces for FlutterView // Manages the IOSurfaces for FlutterView
@interface FlutterSurfaceManager : NSObject @interface FlutterSurfaceManager : NSObject
- (instancetype)initWithLayer:(CALayer*)layer openGLContext:(NSOpenGLContext*)opengLContext; - (nullable instancetype)initWithLayer:(nonnull CALayer*)containingLayer
openGLContext:(nonnull NSOpenGLContext*)opengLContext;
- (void)ensureSurfaceSize:(CGSize)size; - (void)ensureSurfaceSize:(CGSize)size;
- (void)swapBuffers; - (void)swapBuffers;

View File

@@ -1,41 +1,45 @@
// 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/FlutterSurfaceManager.h" #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterSurfaceManager.h"
#import "flutter/fml/logging.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/MacOSSwitchableGLContext.h"
#include <OpenGL/gl.h> #include <OpenGL/gl.h>
#import "flutter/shell/platform/darwin/macos/framework/Source/MacOSGLContextSwitch.h"
enum { enum {
kFront = 0, kFlutterSurfaceManagerFrontBuffer = 0,
kBack = 1, kFlutterSurfaceManagerBackBuffer = 1,
kBufferCount, kFlutterSurfaceManagerBufferCount,
}; };
@interface FlutterSurfaceManager () { @interface FlutterSurfaceManager () {
CGSize surfaceSize; CGSize _surfaceSize;
CALayer* layer; // provided (parent layer) CALayer* _containingLayer; // provided (parent layer)
CALayer* contentLayer; CALayer* _contentLayer;
NSOpenGLContext* openGLContext; NSOpenGLContext* _openGLContext;
uint32_t _frameBufferId[kBufferCount]; uint32_t _frameBufferId[kFlutterSurfaceManagerBufferCount];
uint32_t _backingTexture[kBufferCount]; uint32_t _backingTexture[kFlutterSurfaceManagerBufferCount];
IOSurfaceRef _ioSurface[kBufferCount]; IOSurfaceRef _ioSurface[kFlutterSurfaceManagerBufferCount];
} }
@end @end
@implementation FlutterSurfaceManager @implementation FlutterSurfaceManager
- (instancetype)initWithLayer:(CALayer*)layer_ openGLContext:(NSOpenGLContext*)opengLContext_ { - (instancetype)initWithLayer:(CALayer*)containingLayer
openGLContext:(NSOpenGLContext*)openGLContext {
if (self = [super init]) { if (self = [super init]) {
layer = layer_; _containingLayer = containingLayer;
openGLContext = opengLContext_; _openGLContext = openGLContext;
// Layer for content. This is separate from provided layer, because it needs to be flipped // Layer for content. This is separate from provided layer, because it needs to be flipped
// vertically if we render to OpenGL texture // vertically if we render to OpenGL texture
contentLayer = [[CALayer alloc] init]; _contentLayer = [[CALayer alloc] init];
[layer_ addSublayer:contentLayer]; [_containingLayer addSublayer:_contentLayer];
flutter::GLContextSwitch context_switch( MacOSGLContextSwitch context_switch(openGLContext);
std::make_unique<MacOSSwitchableGLContext>(opengLContext_));
glGenFramebuffers(2, _frameBufferId); glGenFramebuffers(2, _frameBufferId);
glGenTextures(2, _backingTexture); glGenTextures(2, _backingTexture);
@@ -57,15 +61,14 @@ enum {
} }
- (void)ensureSurfaceSize:(CGSize)size { - (void)ensureSurfaceSize:(CGSize)size {
if (CGSizeEqualToSize(size, surfaceSize)) { if (CGSizeEqualToSize(size, _surfaceSize)) {
return; return;
} }
surfaceSize = size; _surfaceSize = size;
flutter::GLContextSwitch context_switch( MacOSGLContextSwitch context_switch(_openGLContext);
std::make_unique<MacOSSwitchableGLContext>(openGLContext));
for (int i = 0; i < kBufferCount; ++i) { for (int i = 0; i < kFlutterSurfaceManagerBufferCount; ++i) {
if (_ioSurface[i]) { if (_ioSurface[i]) {
CFRelease(_ioSurface[i]); CFRelease(_ioSurface[i]);
} }
@@ -96,29 +99,33 @@ enum {
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_RECTANGLE_ARB, glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_RECTANGLE_ARB,
_backingTexture[i], 0); _backingTexture[i], 0);
FML_DCHECK(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE); NSAssert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE,
@"Framebuffer status check failed");
} }
} }
- (void)swapBuffers { - (void)swapBuffers {
contentLayer.frame = layer.bounds; _contentLayer.frame = _containingLayer.bounds;
// The surface is an OpenGL texture, which means it has origin in bottom left corner // The surface is an OpenGL texture, which means it has origin in bottom left corner
// and needs to be flipped vertically // and needs to be flipped vertically
contentLayer.transform = CATransform3DMakeScale(1, -1, 1); _contentLayer.transform = CATransform3DMakeScale(1, -1, 1);
[contentLayer setContents:(__bridge id)_ioSurface[kBack]]; [_contentLayer setContents:(__bridge id)_ioSurface[kFlutterSurfaceManagerBackBuffer]];
std::swap(_ioSurface[kBack], _ioSurface[kFront]); std::swap(_ioSurface[kFlutterSurfaceManagerBackBuffer],
std::swap(_frameBufferId[kBack], _frameBufferId[kFront]); _ioSurface[kFlutterSurfaceManagerFrontBuffer]);
std::swap(_backingTexture[kBack], _backingTexture[kFront]); std::swap(_frameBufferId[kFlutterSurfaceManagerBackBuffer],
_frameBufferId[kFlutterSurfaceManagerFrontBuffer]);
std::swap(_backingTexture[kFlutterSurfaceManagerBackBuffer],
_backingTexture[kFlutterSurfaceManagerFrontBuffer]);
} }
- (uint32_t)glFrameBufferId { - (uint32_t)glFrameBufferId {
return _frameBufferId[kBack]; return _frameBufferId[kFlutterSurfaceManagerBackBuffer];
} }
- (void)dealloc { - (void)dealloc {
for (int i = 0; i < kBufferCount; ++i) { for (int i = 0; i < kFlutterSurfaceManagerBufferCount; ++i) {
if (_ioSurface[i]) { if (_ioSurface[i]) {
CFRelease(_ioSurface[i]); CFRelease(_ioSurface[i]);
} }

View File

@@ -20,7 +20,16 @@
*/ */
@interface FlutterView : NSView @interface FlutterView : NSView
/**
* The OpenGL context of backing surface.
*/
@property(readwrite, nonatomic, nonnull) NSOpenGLContext* openGLContext; @property(readwrite, nonatomic, nonnull) NSOpenGLContext* openGLContext;
/**
* Controls whether view resizing synchronously updates contents. This can only be enabled
* after the engine is running and producing frames, because during synchronous view resizing the
* platform thread is blocked until engine produces frame with requested size.
*/
@property(readwrite, nonatomic) BOOL synchronousResizing; @property(readwrite, nonatomic) BOOL synchronousResizing;
- (nullable instancetype)initWithFrame:(NSRect)frame - (nullable instancetype)initWithFrame:(NSRect)frame
@@ -38,7 +47,15 @@
- (nullable instancetype)initWithCoder:(nonnull NSCoder*)coder NS_UNAVAILABLE; - (nullable instancetype)initWithCoder:(nonnull NSCoder*)coder NS_UNAVAILABLE;
- (nonnull instancetype)init NS_UNAVAILABLE; - (nonnull instancetype)init NS_UNAVAILABLE;
/**
* Flushes the OpenGL context and flips the surfaces. Expected to be called on raster thread.
*/
- (void)present; - (void)present;
- (int)getFrameBufferIdForSize:(CGSize)size;
/**
* Ensures that framebuffer with requested size exists and returns the ID. Expected to be called on
* raster thread.
*/
- (int)frameBufferIDForSize:(CGSize)size;
@end @end

View File

@@ -3,17 +3,18 @@
// found in the LICENSE file. // found in the LICENSE file.
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterView.h" #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterView.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizer.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/FlutterSurfaceManager.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/MacOSSwitchableGLContext.h" #import "flutter/shell/platform/darwin/macos/framework/Source/MacOSGLContextSwitch.h"
#import <OpenGL/gl.h> #import <OpenGL/gl.h>
#import <QuartzCore/QuartzCore.h> #import <QuartzCore/QuartzCore.h>
@interface FlutterView () <FlutterResizeSynchronizerDelegate> { @interface FlutterView () <FlutterResizeSynchronizerDelegate> {
__weak id<FlutterViewReshapeListener> _reshapeListener; __weak id<FlutterViewReshapeListener> _reshapeListener;
FlutterResizeSynchronizer* resizeSynchronizer; FlutterResizeSynchronizer* _resizeSynchronizer;
FlutterSurfaceManager* surfaceManager; FlutterSurfaceManager* _surfaceManager;
} }
@end @end
@@ -35,9 +36,9 @@
[self setWantsLayer:YES]; [self setWantsLayer:YES];
resizeSynchronizer = [[FlutterResizeSynchronizer alloc] initWithDelegate:self]; _resizeSynchronizer = [[FlutterResizeSynchronizer alloc] initWithDelegate:self];
surfaceManager = [[FlutterSurfaceManager alloc] initWithLayer:self.layer _surfaceManager = [[FlutterSurfaceManager alloc] initWithLayer:self.layer
openGLContext:self.openGLContext]; openGLContext:self.openGLContext];
_reshapeListener = reshapeListener; _reshapeListener = reshapeListener;
} }
@@ -45,8 +46,7 @@
} }
- (void)resizeSynchronizerFlush:(FlutterResizeSynchronizer*)synchronizer { - (void)resizeSynchronizerFlush:(FlutterResizeSynchronizer*)synchronizer {
flutter::GLContextSwitch context_switch( MacOSGLContextSwitch context_switch(self.openGLContext);
std::make_unique<MacOSSwitchableGLContext>(self.openGLContext));
glFlush(); glFlush();
} }
@@ -54,29 +54,29 @@
[CATransaction begin]; [CATransaction begin];
[CATransaction setDisableActions:YES]; [CATransaction setDisableActions:YES];
[surfaceManager swapBuffers]; [_surfaceManager swapBuffers];
[CATransaction commit]; [CATransaction commit];
} }
- (int)getFrameBufferIdForSize:(CGSize)size { - (int)frameBufferIDForSize:(CGSize)size {
if ([resizeSynchronizer shouldEnsureSurfaceForSize:size]) { if ([_resizeSynchronizer shouldEnsureSurfaceForSize:size]) {
[surfaceManager ensureSurfaceSize:size]; [_surfaceManager ensureSurfaceSize:size];
} }
return [surfaceManager glFrameBufferId]; return [_surfaceManager glFrameBufferId];
} }
- (void)present { - (void)present {
[resizeSynchronizer requestCommit]; [_resizeSynchronizer requestCommit];
} }
- (void)reshaped { - (void)reshaped {
if (self.synchronousResizing) { if (self.synchronousResizing) {
CGSize scaledSize = [self convertSizeToBacking:self.bounds.size]; CGSize scaledSize = [self convertSizeToBacking:self.bounds.size];
[resizeSynchronizer beginResize:scaledSize [_resizeSynchronizer beginResize:scaledSize
notify:^{ notify:^{
[_reshapeListener viewDidReshape:self]; [_reshapeListener viewDidReshape:self];
}]; }];
} else { } else {
[_reshapeListener viewDidReshape:self]; [_reshapeListener viewDidReshape:self];
} }

View File

@@ -0,0 +1,21 @@
// 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(NSOpenGLContext* context);
~MacOSGLContextSwitch();
MacOSGLContextSwitch(const MacOSGLContextSwitch&) = delete;
MacOSGLContextSwitch(MacOSGLContextSwitch&&) = delete;
private:
NSOpenGLContext* previous_;
};

View File

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

View File

@@ -1,21 +0,0 @@
#include "flutter/flow/gl_context_switch.h"
#include "flutter/fml/memory/thread_checker.h"
#import <Cocoa/Cocoa.h>
class MacOSSwitchableGLContext final : public flutter::SwitchableGLContext {
public:
explicit MacOSSwitchableGLContext(NSOpenGLContext* context);
bool SetCurrent() override;
bool RemoveCurrent() override;
private:
NSOpenGLContext* context_;
NSOpenGLContext* previous_context_;
FML_DECLARE_THREAD_CHECKER(checker);
FML_DISALLOW_COPY_AND_ASSIGN(MacOSSwitchableGLContext);
};

View File

@@ -1,19 +0,0 @@
#import "flutter/shell/platform/darwin/macos/framework/Source/MacOSSwitchableGLContext.h"
MacOSSwitchableGLContext::MacOSSwitchableGLContext(NSOpenGLContext* context) : context_(context) {}
bool MacOSSwitchableGLContext::SetCurrent() {
FML_DCHECK_CREATION_THREAD_IS_CURRENT(checker);
previous_context_ = [NSOpenGLContext currentContext];
[context_ makeCurrentContext];
return true;
}
bool MacOSSwitchableGLContext::RemoveCurrent() {
if (previous_context_) {
[previous_context_ makeCurrentContext];
} else {
[NSOpenGLContext clearCurrentContext];
}
return true;
}