FlutterResizeSynchronizer.h 2.8 KB
Newer Older
1 2 3 4
// 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.

5 6 7 8
#import <Cocoa/Cocoa.h>

@class FlutterResizeSynchronizer;

9 10 11
/**
 * Implemented by FlutterView.
 */
12 13
@protocol FlutterResizeSynchronizerDelegate

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

19 20 21 22
/**
 * Invoked on platform thread; Delegate should flip the surfaces.
 */
- (void)resizeSynchronizerCommit:(nonnull FlutterResizeSynchronizer*)synchronizer;
23 24 25

@end

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
/**
 * 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
 */
51 52
@interface FlutterResizeSynchronizer : NSObject

53
- (nullable instancetype)initWithDelegate:(nonnull id<FlutterResizeSynchronizerDelegate>)delegate;
54

55 56 57 58 59 60 61 62 63
/**
 * 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;
64

65 66 67 68 69 70
/**
 * 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;
71

72 73 74 75
/**
 * Called from rasterizer thread, will block until delegate resizeSynchronizerCommit:
 * method is called (on platform thread).
 */
76 77 78
- (void)requestCommit;

@end