This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
Fix code style issues in MacOS embedder #22270
Merged
fluttergithubbot
merged 6 commits into
flutter:master
from
knopp:macos_embedder_code_style_fixes
Nov 4, 2020
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 59 additions & 40 deletions
99
shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizer.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,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> | ||
|
|
||
| @class FlutterResizeSynchronizer; | ||
|
|
||
| /** | ||
| * Implemented by FlutterView. | ||
| */ | ||
| @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 | ||
|
|
||
| // 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 | ||
| // | ||
| // 1. Platform thread calls [synchronizer beginResize:notify:] | ||
| // This will hold the platform thread until we're ready to display contents. | ||
| // 2. Raster thread calls [synchronizer shouldEnsureSurfaceForSize:] with target size | ||
| // This will return false for any size other than target size | ||
| // 3. Raster thread calls [synchronizer requestCommit] | ||
| // Any commit calls before shouldEnsureSurfaceForSize: is called with the right | ||
| // size are simply ignored; There's no point rasterizing and displaying frames | ||
| // with wrong size. | ||
| // Both delegate methods (flush/commit) will be invoked before beginResize returns | ||
| // | ||
| // Flow during regular operation (no resizing) | ||
| // | ||
| // 1. Raster thread calls [synchronizer requestCommit] | ||
| // This will invoke [delegate flush:] on raster thread and | ||
| // [delegate commit:] on platform thread. The requestCommit call will be blocked | ||
| // until this is done. This is necessary to ensure that rasterizer won't start | ||
| // rasterizing next frame before we flipped the surface, which must be performed | ||
| // on platform thread | ||
| /** | ||
| * 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 | ||
| * | ||
| * 1. Platform thread calls [synchronizer beginResize:notify:] | ||
| * This will hold the platform thread until we're ready to display contents. | ||
| * 2. Raster thread calls [synchronizer shouldEnsureSurfaceForSize:] with target size | ||
| * This will return false for any size other than target size | ||
| * 3. Raster thread calls [synchronizer requestCommit] | ||
| * Any commit calls before shouldEnsureSurfaceForSize: is called with the right | ||
| * size are simply ignored; There's no point rasterizing and displaying frames | ||
| * with wrong size. | ||
| * Both delegate methods (flush/commit) will be invoked before beginResize returns | ||
| * | ||
| * Flow during regular operation (no resizing) | ||
| * | ||
| * 1. Raster thread calls [synchronizer requestCommit] | ||
| * This will invoke [delegate flush:] on raster thread and | ||
| * [delegate commit:] on platform thread. The requestCommit call will be blocked | ||
| * until this is done. This is necessary to ensure that rasterizer won't start | ||
| * rasterizing next frame before we flipped the surface, which must be performed | ||
| * on platform thread | ||
| */ | ||
| @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 | ||
| // - requestCommit is called | ||
| // All requestCommit calls before `shouldEnsureSurfaceForSize` is called with | ||
| // expected size are ignored; | ||
| // The notify block is invoked immediately after synchronizer mutex is acquired | ||
| - (void)beginResize:(CGSize)size notify:(dispatch_block_t)notify; | ||
| /** | ||
| * Blocks the platform thread until | ||
| * - shouldEnsureSurfaceForSize is called with proper size and | ||
| * - requestCommit is called | ||
| * All requestCommit calls before `shouldEnsureSurfaceForSize` is called with | ||
| * expected size are ignored; | ||
| * 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 | ||
| // during beginResize | ||
| /** | ||
| * Returns whether the view should ensure surfaces with given size; | ||
| * This will be false during resizing for any size other than size specified | ||
| * 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; | ||
|
|
||
| @end | ||
112 changes: 63 additions & 49 deletions
112
shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizer.mm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 <mutex> | ||
| #include <mutex> | ||
|
|
||
| @interface FlutterResizeSynchronizer () { | ||
| uint32_t cookie; // counter to detect stale callbacks | ||
| // counter to detect stale callbacks | ||
|
||
| uint32_t _cookie; | ||
|
|
||
| std::mutex _mutex; | ||
|
|
||
| // used to block [beginResize:] | ||
| std::condition_variable _condBlockBeginResize; | ||
| // used to block [requestCommit] | ||
| std::condition_variable _condBlockRequestCommit; | ||
|
|
||
| // if NO, requestCommit calls are ignored until shouldEnsureSurfaceForSize is called with | ||
| // proper size | ||
| BOOL _acceptingCommit; | ||
|
|
||
| // waiting for resize to finish | ||
| BOOL _waiting; | ||
|
|
||
| std::mutex mutex; | ||
| std::condition_variable condBlockBeginResize; // used to block [beginResize:] | ||
| std::condition_variable condBlockRequestCommit; // used to block [requestCommit] | ||
| // requestCommit was called and [delegate commit:] must be performed on platform thread | ||
| BOOL _pendingCommit; | ||
|
|
||
| bool acceptingCommit; // if false, requestCommit calls are ignored until | ||
| // shouldEnsureSurfaceForSize is called with proper size | ||
| bool waiting; // waiting for resize to finish | ||
| bool pendingCommit; // requestCommit was called and [delegate commit:] must be performed on | ||
| // platform thread | ||
| CGSize newSize; // target size for resizing | ||
| // target size for resizing | ||
| CGSize _newSize; | ||
|
|
||
| __weak id<FlutterResizeSynchronizerDelegate> delegate; | ||
| __weak id<FlutterResizeSynchronizerDelegate> _delegate; | ||
| } | ||
| @end | ||
|
|
||
| @implementation FlutterResizeSynchronizer | ||
|
|
||
| - (instancetype)initWithDelegate:(id<FlutterResizeSynchronizerDelegate>)delegate_ { | ||
| - (instancetype)initWithDelegate:(id<FlutterResizeSynchronizerDelegate>)delegate { | ||
| if (self = [super init]) { | ||
| acceptingCommit = true; | ||
| delegate = delegate_; | ||
| _acceptingCommit = YES; | ||
| _delegate = delegate; | ||
| } | ||
| return self; | ||
| } | ||
|
|
||
| - (void)beginResize:(CGSize)size notify:(dispatch_block_t)notify { | ||
| std::unique_lock<std::mutex> lock(mutex); | ||
| if (!delegate) { | ||
| std::unique_lock<std::mutex> lock(_mutex); | ||
| if (!_delegate) { | ||
| return; | ||
| } | ||
|
|
||
| ++cookie; | ||
| ++_cookie; | ||
|
|
||
| // from now on, ignore all incoming commits until the block below gets | ||
| // scheduled on raster thread | ||
| acceptingCommit = false; | ||
| _acceptingCommit = NO; | ||
|
|
||
| // let pending commits finish to unblock the raster thread | ||
| pendingCommit = false; | ||
| condBlockBeginResize.notify_all(); | ||
| _pendingCommit = NO; | ||
| _condBlockBeginResize.notify_all(); | ||
|
|
||
| // let the engine send resize notification | ||
| notify(); | ||
|
|
||
| newSize = size; | ||
| _newSize = size; | ||
|
|
||
| waiting = true; | ||
| _waiting = YES; | ||
|
|
||
| condBlockRequestCommit.wait(lock, [&] { return pendingCommit; }); | ||
| _condBlockRequestCommit.wait(lock, [&] { return _pendingCommit; }); | ||
|
|
||
| [delegate resizeSynchronizerFlush:self]; | ||
| [delegate resizeSynchronizerCommit:self]; | ||
| pendingCommit = false; | ||
| condBlockBeginResize.notify_all(); | ||
| [_delegate resizeSynchronizerFlush:self]; | ||
| [_delegate resizeSynchronizerCommit:self]; | ||
| _pendingCommit = NO; | ||
| _condBlockBeginResize.notify_all(); | ||
|
|
||
| waiting = false; | ||
| _waiting = NO; | ||
| } | ||
|
|
||
| - (bool)shouldEnsureSurfaceForSize:(CGSize)size { | ||
| std::unique_lock<std::mutex> lock(mutex); | ||
| if (!acceptingCommit) { | ||
| if (CGSizeEqualToSize(newSize, size)) { | ||
| acceptingCommit = true; | ||
| std::unique_lock<std::mutex> lock(_mutex); | ||
| if (!_acceptingCommit) { | ||
| if (CGSizeEqualToSize(_newSize, size)) { | ||
| _acceptingCommit = YES; | ||
| } | ||
| } | ||
| return acceptingCommit; | ||
| return _acceptingCommit; | ||
| } | ||
|
|
||
| - (void)requestCommit { | ||
| std::unique_lock<std::mutex> lock(mutex); | ||
| if (!acceptingCommit) { | ||
| std::unique_lock<std::mutex> lock(_mutex); | ||
| if (!_acceptingCommit) { | ||
| return; | ||
| } | ||
|
|
||
| pendingCommit = true; | ||
| if (waiting) { // BeginResize is in progress, interrupt it and schedule commit call | ||
| condBlockRequestCommit.notify_all(); | ||
| condBlockBeginResize.wait(lock, [&]() { return !pendingCommit; }); | ||
| _pendingCommit = YES; | ||
| if (_waiting) { // BeginResize is in progress, interrupt it and schedule commit call | ||
| _condBlockRequestCommit.notify_all(); | ||
| _condBlockBeginResize.wait(lock, [&]() { return !_pendingCommit; }); | ||
| } else { | ||
| // No resize, schedule commit on platform thread and wait until either done | ||
| // or interrupted by incoming BeginResize | ||
| [delegate resizeSynchronizerFlush:self]; | ||
| dispatch_async(dispatch_get_main_queue(), [self, cookie_ = cookie] { | ||
| std::unique_lock<std::mutex> lock(mutex); | ||
| if (cookie_ == cookie) { | ||
| if (delegate) { | ||
| [delegate resizeSynchronizerCommit:self]; | ||
| [_delegate resizeSynchronizerFlush:self]; | ||
| dispatch_async(dispatch_get_main_queue(), [self, cookie = _cookie] { | ||
| std::unique_lock<std::mutex> lock(_mutex); | ||
| if (cookie == _cookie) { | ||
| if (_delegate) { | ||
| [_delegate resizeSynchronizerCommit:self]; | ||
| } | ||
| pendingCommit = false; | ||
| condBlockBeginResize.notify_all(); | ||
| _pendingCommit = NO; | ||
| _condBlockBeginResize.notify_all(); | ||
| } | ||
| }); | ||
| condBlockBeginResize.wait(lock, [&]() { return !pendingCommit; }); | ||
| _condBlockBeginResize.wait(lock, [&]() { return !_pendingCommit; }); | ||
| } | ||
| } | ||
|
|
||
|
|
||
7 changes: 6 additions & 1 deletion
7
shell/platform/darwin/macos/framework/Source/FlutterSurfaceManager.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,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> | ||
|
|
||
| // Manages the IOSurfaces for FlutterView | ||
| @interface FlutterSurfaceManager : NSObject | ||
|
|
||
| - (instancetype)initWithLayer:(CALayer*)layer openGLContext:(NSOpenGLContext*)opengLContext; | ||
| - (instancetype)initWithLayer:(CALayer*)containingLayer | ||
| openGLContext:(NSOpenGLContext*)opengLContext; | ||
|
||
|
|
||
| - (void)ensureSurfaceSize:(CGSize)size; | ||
| - (void)swapBuffers; | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BOOL