|
| 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 | + |
1 | 5 | #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizer.h" |
2 | 6 |
|
3 | | -#import <mutex> |
| 7 | +#include <mutex> |
4 | 8 |
|
5 | 9 | @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; |
7 | 26 |
|
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; |
11 | 29 |
|
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; |
18 | 32 |
|
19 | | - __weak id<FlutterResizeSynchronizerDelegate> delegate; |
| 33 | + __weak id<FlutterResizeSynchronizerDelegate> _delegate; |
20 | 34 | } |
21 | 35 | @end |
22 | 36 |
|
23 | 37 | @implementation FlutterResizeSynchronizer |
24 | 38 |
|
25 | | -- (instancetype)initWithDelegate:(id<FlutterResizeSynchronizerDelegate>)delegate_ { |
| 39 | +- (instancetype)initWithDelegate:(id<FlutterResizeSynchronizerDelegate>)delegate { |
26 | 40 | if (self = [super init]) { |
27 | | - acceptingCommit = true; |
28 | | - delegate = delegate_; |
| 41 | + _acceptingCommit = YES; |
| 42 | + _delegate = delegate; |
29 | 43 | } |
30 | 44 | return self; |
31 | 45 | } |
32 | 46 |
|
33 | 47 | - (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) { |
36 | 50 | return; |
37 | 51 | } |
38 | 52 |
|
39 | | - ++cookie; |
| 53 | + ++_cookie; |
40 | 54 |
|
41 | 55 | // from now on, ignore all incoming commits until the block below gets |
42 | 56 | // scheduled on raster thread |
43 | | - acceptingCommit = false; |
| 57 | + _acceptingCommit = NO; |
44 | 58 |
|
45 | 59 | // let pending commits finish to unblock the raster thread |
46 | | - pendingCommit = false; |
47 | | - condBlockBeginResize.notify_all(); |
| 60 | + _pendingCommit = NO; |
| 61 | + _condBlockBeginResize.notify_all(); |
48 | 62 |
|
49 | 63 | // let the engine send resize notification |
50 | 64 | notify(); |
51 | 65 |
|
52 | | - newSize = size; |
| 66 | + _newSize = size; |
53 | 67 |
|
54 | | - waiting = true; |
| 68 | + _waiting = YES; |
55 | 69 |
|
56 | | - condBlockRequestCommit.wait(lock, [&] { return pendingCommit; }); |
| 70 | + _condBlockRequestCommit.wait(lock, [&] { return _pendingCommit; }); |
57 | 71 |
|
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(); |
62 | 76 |
|
63 | | - waiting = false; |
| 77 | + _waiting = NO; |
64 | 78 | } |
65 | 79 |
|
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; |
71 | 85 | } |
72 | 86 | } |
73 | | - return acceptingCommit; |
| 87 | + return _acceptingCommit; |
74 | 88 | } |
75 | 89 |
|
76 | 90 | - (void)requestCommit { |
77 | | - std::unique_lock<std::mutex> lock(mutex); |
78 | | - if (!acceptingCommit) { |
| 91 | + std::unique_lock<std::mutex> lock(_mutex); |
| 92 | + if (!_acceptingCommit) { |
79 | 93 | return; |
80 | 94 | } |
81 | 95 |
|
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; }); |
86 | 100 | } else { |
87 | 101 | // No resize, schedule commit on platform thread and wait until either done |
88 | 102 | // 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]; |
95 | 109 | } |
96 | | - pendingCommit = false; |
97 | | - condBlockBeginResize.notify_all(); |
| 110 | + _pendingCommit = NO; |
| 111 | + _condBlockBeginResize.notify_all(); |
98 | 112 | } |
99 | 113 | }); |
100 | | - condBlockBeginResize.wait(lock, [&]() { return !pendingCommit; }); |
| 114 | + _condBlockBeginResize.wait(lock, [&]() { return !_pendingCommit; }); |
101 | 115 | } |
102 | 116 | } |
103 | 117 |
|
|
0 commit comments