Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit acc2466

Browse files
authored
Fix code style issues in MacOS embedder (#22270)
1 parent 122f242 commit acc2466

13 files changed

Lines changed: 262 additions & 194 deletions

ci/licenses_golden/licenses_flutter

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,8 +1068,8 @@ FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterView.
10681068
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterViewController.mm
10691069
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterViewControllerTest.mm
10701070
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterViewController_Internal.h
1071-
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/MacOSSwitchableGLContext.h
1072-
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/MacOSSwitchableGLContext.mm
1071+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/MacOSGLContextSwitch.h
1072+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/MacOSGLContextSwitch.mm
10731073
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/fixtures/flutter_desktop_test.dart
10741074
FILE: ../../../flutter/shell/platform/darwin/macos/framework/module.modulemap
10751075
FILE: ../../../flutter/shell/platform/embedder/assets/EmbedderInfo.plist

shell/platform/darwin/macos/BUILD.gn

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,13 @@ source_set("flutter_framework_source") {
6666
"framework/Source/FlutterView.mm",
6767
"framework/Source/FlutterViewController.mm",
6868
"framework/Source/FlutterViewController_Internal.h",
69-
"framework/Source/MacOSSwitchableGLContext.h",
70-
"framework/Source/MacOSSwitchableGLContext.mm",
69+
"framework/Source/MacOSGLContextSwitch.h",
70+
"framework/Source/MacOSGLContextSwitch.mm",
7171
]
7272

7373
sources += _flutter_framework_headers
7474

7575
deps = [
76-
"//flutter/flow:flow",
77-
"//flutter/fml:fml",
7876
"//flutter/shell/platform/common/cpp:common_cpp_switches",
7977
"//flutter/shell/platform/darwin/common:framework_shared",
8078
"//flutter/shell/platform/embedder:embedder_as_internal_library",

shell/platform/darwin/macos/framework/Source/FlutterEngine.mm

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ - (bool)engineCallbackOnClearCurrent;
5252
*/
5353
- (bool)engineCallbackOnPresent;
5454

55+
/**
56+
* Called by the engine when framebuffer object ID is requested.
57+
*/
58+
- (uint32_t)engineCallbackOnFBO:(const FlutterFrameInfo*)info;
59+
5560
/**
5661
* Makes the resource context the current context.
5762
*/
@@ -146,8 +151,7 @@ static bool OnPresent(FlutterEngine* engine) {
146151
}
147152

148153
static uint32_t OnFBO(FlutterEngine* engine, const FlutterFrameInfo* info) {
149-
CGSize size = CGSizeMake(info->size.width, info->size.height);
150-
return [engine.viewController.flutterView getFrameBufferIdForSize:size];
154+
return [engine engineCallbackOnFBO:info];
151155
}
152156

153157
static bool OnMakeResourceCurrent(FlutterEngine* engine) {
@@ -469,6 +473,11 @@ - (bool)engineCallbackOnMakeCurrent {
469473
return true;
470474
}
471475

476+
- (uint32_t)engineCallbackOnFBO:(const FlutterFrameInfo*)info {
477+
CGSize size = CGSizeMake(info->size.width, info->size.height);
478+
return [_viewController.flutterView frameBufferIDForSize:size];
479+
}
480+
472481
- (bool)engineCallbackOnClearCurrent {
473482
[NSOpenGLContext clearCurrentContext];
474483
return true;
Lines changed: 60 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,78 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
15
#import <Cocoa/Cocoa.h>
26

37
@class FlutterResizeSynchronizer;
48

9+
/**
10+
* Implemented by FlutterView.
11+
*/
512
@protocol FlutterResizeSynchronizerDelegate
613

7-
// Invoked on raster thread; Delegate should flush the OpenGL context
8-
- (void)resizeSynchronizerFlush:(FlutterResizeSynchronizer*)synchronizer;
14+
/**
15+
* Invoked on raster thread; Delegate should flush the OpenGL context.
16+
*/
17+
- (void)resizeSynchronizerFlush:(nonnull FlutterResizeSynchronizer*)synchronizer;
918

10-
// Invoked on platform thread; Delegate should flip the surfaces
11-
- (void)resizeSynchronizerCommit:(FlutterResizeSynchronizer*)synchronizer;
19+
/**
20+
* Invoked on platform thread; Delegate should flip the surfaces.
21+
*/
22+
- (void)resizeSynchronizerCommit:(nonnull FlutterResizeSynchronizer*)synchronizer;
1223

1324
@end
1425

15-
// Encapsulates the logic for blocking platform thread during window resize as
16-
// well as synchronizing the raster and platform thread during commit (presenting frame)
17-
//
18-
// Flow during window resize
19-
//
20-
// 1. Platform thread calls [synchronizer beginResize:notify:]
21-
// This will hold the platform thread until we're ready to display contents.
22-
// 2. Raster thread calls [synchronizer shouldEnsureSurfaceForSize:] with target size
23-
// This will return false for any size other than target size
24-
// 3. Raster thread calls [synchronizer requestCommit]
25-
// Any commit calls before shouldEnsureSurfaceForSize: is called with the right
26-
// size are simply ignored; There's no point rasterizing and displaying frames
27-
// with wrong size.
28-
// Both delegate methods (flush/commit) will be invoked before beginResize returns
29-
//
30-
// Flow during regular operation (no resizing)
31-
//
32-
// 1. Raster thread calls [synchronizer requestCommit]
33-
// This will invoke [delegate flush:] on raster thread and
34-
// [delegate commit:] on platform thread. The requestCommit call will be blocked
35-
// until this is done. This is necessary to ensure that rasterizer won't start
36-
// rasterizing next frame before we flipped the surface, which must be performed
37-
// on platform thread
26+
/**
27+
* Encapsulates the logic for blocking platform thread during window resize as
28+
* well as synchronizing the raster and platform thread during commit (presenting frame).
29+
*
30+
* Flow during window resize
31+
*
32+
* 1. Platform thread calls [synchronizer beginResize:notify:]
33+
* This will hold the platform thread until we're ready to display contents.
34+
* 2. Raster thread calls [synchronizer shouldEnsureSurfaceForSize:] with target size
35+
* This will return false for any size other than target size
36+
* 3. Raster thread calls [synchronizer requestCommit]
37+
* Any commit calls before shouldEnsureSurfaceForSize: is called with the right
38+
* size are simply ignored; There's no point rasterizing and displaying frames
39+
* with wrong size.
40+
* Both delegate methods (flush/commit) will be invoked before beginResize returns
41+
*
42+
* Flow during regular operation (no resizing)
43+
*
44+
* 1. Raster thread calls [synchronizer requestCommit]
45+
* This will invoke [delegate flush:] on raster thread and
46+
* [delegate commit:] on platform thread. The requestCommit call will be blocked
47+
* until this is done. This is necessary to ensure that rasterizer won't start
48+
* rasterizing next frame before we flipped the surface, which must be performed
49+
* on platform thread
50+
*/
3851
@interface FlutterResizeSynchronizer : NSObject
3952

40-
- (instancetype)initWithDelegate:(id<FlutterResizeSynchronizerDelegate>)delegate;
53+
- (nullable instancetype)initWithDelegate:(nonnull id<FlutterResizeSynchronizerDelegate>)delegate;
4154

42-
// Blocks the platform thread until
43-
// - shouldEnsureSurfaceForSize is called with proper size and
44-
// - requestCommit is called
45-
// All requestCommit calls before `shouldEnsureSurfaceForSize` is called with
46-
// expected size are ignored;
47-
// The notify block is invoked immediately after synchronizer mutex is acquired
48-
- (void)beginResize:(CGSize)size notify:(dispatch_block_t)notify;
55+
/**
56+
* Blocks the platform thread until
57+
* - shouldEnsureSurfaceForSize is called with proper size and
58+
* - requestCommit is called
59+
* All requestCommit calls before `shouldEnsureSurfaceForSize` is called with
60+
* expected size are ignored;
61+
* The notify block is invoked immediately after synchronizer mutex is acquired.
62+
*/
63+
- (void)beginResize:(CGSize)size notify:(nonnull dispatch_block_t)notify;
4964

50-
// Returns whether the view should ensure surfaces with given size;
51-
// This will be false during resizing for any size other than size specified
52-
// during beginResize
53-
- (bool)shouldEnsureSurfaceForSize:(CGSize)size;
65+
/**
66+
* Returns whether the view should ensure surfaces with given size;
67+
* This will be false during resizing for any size other than size specified
68+
* during beginResize.
69+
*/
70+
- (BOOL)shouldEnsureSurfaceForSize:(CGSize)size;
5471

55-
// Called from rasterizer thread, will block until delegate resizeSynchronizerCommit:
56-
// method is called (on platform thread)
72+
/**
73+
* Called from rasterizer thread, will block until delegate resizeSynchronizerCommit:
74+
* method is called (on platform thread).
75+
*/
5776
- (void)requestCommit;
5877

5978
@end

shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizer.mm

Lines changed: 64 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,117 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
15
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizer.h"
26

3-
#import <mutex>
7+
#include <mutex>
48

59
@interface FlutterResizeSynchronizer () {
6-
uint32_t cookie; // counter to detect stale callbacks
10+
// Counter to detect stale callbacks.
11+
uint32_t _cookie;
12+
13+
std::mutex _mutex;
14+
15+
// Used to block [beginResize:].
16+
std::condition_variable _condBlockBeginResize;
17+
// Used to block [requestCommit].
18+
std::condition_variable _condBlockRequestCommit;
19+
20+
// If NO, requestCommit calls are ignored until shouldEnsureSurfaceForSize is called with
21+
// proper size.
22+
BOOL _acceptingCommit;
23+
24+
// Waiting for resize to finish.
25+
BOOL _waiting;
726

8-
std::mutex mutex;
9-
std::condition_variable condBlockBeginResize; // used to block [beginResize:]
10-
std::condition_variable condBlockRequestCommit; // used to block [requestCommit]
27+
// RequestCommit was called and [delegate commit:] must be performed on platform thread.
28+
BOOL _pendingCommit;
1129

12-
bool acceptingCommit; // if false, requestCommit calls are ignored until
13-
// shouldEnsureSurfaceForSize is called with proper size
14-
bool waiting; // waiting for resize to finish
15-
bool pendingCommit; // requestCommit was called and [delegate commit:] must be performed on
16-
// platform thread
17-
CGSize newSize; // target size for resizing
30+
// Target size for resizing.
31+
CGSize _newSize;
1832

19-
__weak id<FlutterResizeSynchronizerDelegate> delegate;
33+
__weak id<FlutterResizeSynchronizerDelegate> _delegate;
2034
}
2135
@end
2236

2337
@implementation FlutterResizeSynchronizer
2438

25-
- (instancetype)initWithDelegate:(id<FlutterResizeSynchronizerDelegate>)delegate_ {
39+
- (instancetype)initWithDelegate:(id<FlutterResizeSynchronizerDelegate>)delegate {
2640
if (self = [super init]) {
27-
acceptingCommit = true;
28-
delegate = delegate_;
41+
_acceptingCommit = YES;
42+
_delegate = delegate;
2943
}
3044
return self;
3145
}
3246

3347
- (void)beginResize:(CGSize)size notify:(dispatch_block_t)notify {
34-
std::unique_lock<std::mutex> lock(mutex);
35-
if (!delegate) {
48+
std::unique_lock<std::mutex> lock(_mutex);
49+
if (!_delegate) {
3650
return;
3751
}
3852

39-
++cookie;
53+
++_cookie;
4054

4155
// from now on, ignore all incoming commits until the block below gets
4256
// scheduled on raster thread
43-
acceptingCommit = false;
57+
_acceptingCommit = NO;
4458

4559
// let pending commits finish to unblock the raster thread
46-
pendingCommit = false;
47-
condBlockBeginResize.notify_all();
60+
_pendingCommit = NO;
61+
_condBlockBeginResize.notify_all();
4862

4963
// let the engine send resize notification
5064
notify();
5165

52-
newSize = size;
66+
_newSize = size;
5367

54-
waiting = true;
68+
_waiting = YES;
5569

56-
condBlockRequestCommit.wait(lock, [&] { return pendingCommit; });
70+
_condBlockRequestCommit.wait(lock, [&] { return _pendingCommit; });
5771

58-
[delegate resizeSynchronizerFlush:self];
59-
[delegate resizeSynchronizerCommit:self];
60-
pendingCommit = false;
61-
condBlockBeginResize.notify_all();
72+
[_delegate resizeSynchronizerFlush:self];
73+
[_delegate resizeSynchronizerCommit:self];
74+
_pendingCommit = NO;
75+
_condBlockBeginResize.notify_all();
6276

63-
waiting = false;
77+
_waiting = NO;
6478
}
6579

66-
- (bool)shouldEnsureSurfaceForSize:(CGSize)size {
67-
std::unique_lock<std::mutex> lock(mutex);
68-
if (!acceptingCommit) {
69-
if (CGSizeEqualToSize(newSize, size)) {
70-
acceptingCommit = true;
80+
- (BOOL)shouldEnsureSurfaceForSize:(CGSize)size {
81+
std::unique_lock<std::mutex> lock(_mutex);
82+
if (!_acceptingCommit) {
83+
if (CGSizeEqualToSize(_newSize, size)) {
84+
_acceptingCommit = YES;
7185
}
7286
}
73-
return acceptingCommit;
87+
return _acceptingCommit;
7488
}
7589

7690
- (void)requestCommit {
77-
std::unique_lock<std::mutex> lock(mutex);
78-
if (!acceptingCommit) {
91+
std::unique_lock<std::mutex> lock(_mutex);
92+
if (!_acceptingCommit) {
7993
return;
8094
}
8195

82-
pendingCommit = true;
83-
if (waiting) { // BeginResize is in progress, interrupt it and schedule commit call
84-
condBlockRequestCommit.notify_all();
85-
condBlockBeginResize.wait(lock, [&]() { return !pendingCommit; });
96+
_pendingCommit = YES;
97+
if (_waiting) { // BeginResize is in progress, interrupt it and schedule commit call
98+
_condBlockRequestCommit.notify_all();
99+
_condBlockBeginResize.wait(lock, [&]() { return !_pendingCommit; });
86100
} else {
87101
// No resize, schedule commit on platform thread and wait until either done
88102
// or interrupted by incoming BeginResize
89-
[delegate resizeSynchronizerFlush:self];
90-
dispatch_async(dispatch_get_main_queue(), [self, cookie_ = cookie] {
91-
std::unique_lock<std::mutex> lock(mutex);
92-
if (cookie_ == cookie) {
93-
if (delegate) {
94-
[delegate resizeSynchronizerCommit:self];
103+
[_delegate resizeSynchronizerFlush:self];
104+
dispatch_async(dispatch_get_main_queue(), [self, cookie = _cookie] {
105+
std::unique_lock<std::mutex> lock(_mutex);
106+
if (cookie == _cookie) {
107+
if (_delegate) {
108+
[_delegate resizeSynchronizerCommit:self];
95109
}
96-
pendingCommit = false;
97-
condBlockBeginResize.notify_all();
110+
_pendingCommit = NO;
111+
_condBlockBeginResize.notify_all();
98112
}
99113
});
100-
condBlockBeginResize.wait(lock, [&]() { return !pendingCommit; });
114+
_condBlockBeginResize.wait(lock, [&]() { return !_pendingCommit; });
101115
}
102116
}
103117

shell/platform/darwin/macos/framework/Source/FlutterSurfaceManager.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
15
#import <Cocoa/Cocoa.h>
26

37
// Manages the IOSurfaces for FlutterView
48
@interface FlutterSurfaceManager : NSObject
59

6-
- (instancetype)initWithLayer:(CALayer*)layer openGLContext:(NSOpenGLContext*)opengLContext;
10+
- (nullable instancetype)initWithLayer:(nonnull CALayer*)containingLayer
11+
openGLContext:(nonnull NSOpenGLContext*)opengLContext;
712

813
- (void)ensureSurfaceSize:(CGSize)size;
914
- (void)swapBuffers;

0 commit comments

Comments
 (0)