Flutter Engine
The Flutter Engine
Instance Methods | Class Methods | Protected Attributes | Properties | List of all members
FlutterView Class Reference

#import <FlutterView.h>

Inheritance diagram for FlutterView:
<FlutterSurfaceManagerDelegate> TestFlutterView

Instance Methods

(instancetype) - NS_UNAVAILABLE
 
(instancetype) - initWithFrame:
 
(instancetype) - initWithCoder:
 
(instancetype) - initWithDelegate:opaque:enableWideGamut:
 
(UIScreen *) - screen
 
(MTLPixelFormat) - pixelFormat
 
(nullable instancetype) - initWithMTLDevice:commandQueue:delegate:threadSynchronizer:viewIdentifier:
 
(nullable instancetype) - initWithFrame:pixelFormat:
 
(nonnull instancetype) - initWithFrame:
 
(nullable instancetype) - initWithCoder:
 
(nonnull instancetype) - NS_UNAVAILABLE
 
(void) - setBackgroundColor:
 
(void) - didUpdateMouseCursor:
 
(instancetype) - init [implementation]
 
(BOOL- isWideGamutSupported [implementation]
 
(void) - layoutSubviews [implementation]
 
(void) - drawLayer:inContext: [implementation]
 
(BOOL- isAccessibilityElement [implementation]
 
(instancetype) - initWithMTLDevice:commandQueue:delegate:threadSynchronizer:viewIdentifier: [implementation]
 
(void) - onPresent:withBlock: [implementation]
 
(void) - reshaped [implementation]
 
(void) - setBackgroundColor: [implementation]
 
(void) - setFrameSize: [implementation]
 
(BOOL- isFlipped [implementation]
 
(BOOL- isOpaque [implementation]
 
(BOOL- acceptsFirstMouse: [implementation]
 
(BOOL- acceptsFirstResponder [implementation]
 
(void) - didUpdateMouseCursor: [implementation]
 
(void) - cursorUpdate: [implementation]
 
(void) - viewDidChangeBackingProperties [implementation]
 
(BOOL- layer:shouldInheritContentsScale:fromWindow: [implementation]
 
(NSAccessibilityRole) - accessibilityRole [implementation]
 
(NSString *) - accessibilityLabel [implementation]
 
- Instance Methods inherited from <FlutterSurfaceManagerDelegate>
(void) - onPresent:withBlock:
 

Class Methods

(instancetype) + NS_UNAVAILABLE
 
(static void) + PrintWideGamutWarningOnce [implementation]
 
(void) + setForceSoftwareRendering: [implementation]
 
(Class) + layerClass [implementation]
 

Protected Attributes

FlutterViewIdentifier _viewIdentifier
 
__weak id< FlutterViewDelegate_viewDelegate
 
FlutterThreadSynchronizer_threadSynchronizer
 
FlutterSurfaceManager_surfaceManager
 
NSCursor * _lastCursor
 

Properties

BOOL forceSoftwareRendering
 
FlutterSurfaceManagersurfaceManager
 
id< FlutterViewEngineDelegatedelegate [implementation]
 

Detailed Description

View capable of acting as a rendering target and input source for the Flutter engine.

Definition at line 34 of file FlutterView.h.

Method Documentation

◆ acceptsFirstMouse:

- (BOOL) acceptsFirstMouse: (NSEvent*)  event
implementation

Declares that the initial mouse-down when the view is not in focus will send an event to the view.

Definition at line 17 of file FlutterView.mm.

88 :(NSEvent*)event {
89 return YES;
90}
FlKeyEvent * event

◆ acceptsFirstResponder

- (BOOL) acceptsFirstResponder
implementation

Definition at line 17 of file FlutterView.mm.

92 {
93 // This is to ensure that FlutterView does not take first responder status from TextInputPlugin
94 // on mouse clicks.
95 return [_viewDelegate viewShouldAcceptFirstResponder:self];
96}

◆ accessibilityLabel

- (NSString *) accessibilityLabel
implementation

Definition at line 17 of file FlutterView.mm.

147 {
148 // TODO(chunhtai): Provides a way to let developer customize the accessibility
149 // label.
150 // https://github.com/flutter/flutter/issues/75446
151 NSString* applicationName =
152 [NSBundle.mainBundle objectForInfoDictionaryKey:@"CFBundleDisplayName"];
153 if (!applicationName) {
154 applicationName = [NSBundle.mainBundle objectForInfoDictionaryKey:@"CFBundleName"];
155 }
156 return applicationName;
157}

◆ accessibilityRole

- (NSAccessibilityRole) accessibilityRole
implementation

Definition at line 17 of file FlutterView.mm.

143 {
144 return NSAccessibilityGroupRole;
145}

◆ cursorUpdate:

- (void) cursorUpdate: (NSEvent*)  event
implementation

Definition at line 17 of file FlutterView.mm.

108 :(NSEvent*)event {
109 // Make sure to not override cursor when over a platform view.
110 NSPoint mouseLocation = [[self superview] convertPoint:event.locationInWindow fromView:nil];
111 NSView* hitTestView = [self hitTest:mouseLocation];
112 if (hitTestView != self) {
113 return;
114 }
115 [_lastCursor set];
116 // It is possible that there is a platform view with NSTrackingArea below flutter content.
117 // This could override the mouse cursor as a result of mouse move event. There is no good way
118 // to prevent that short of swizzling [NSCursor set], so as a workaround force flutter cursor
119 // in next runloop turn. This is not ideal, as it may cause the cursor flicker a bit.
120 [[NSRunLoop currentRunLoop] performBlock:^{
121 [_lastCursor set];
122 }];
123}

◆ didUpdateMouseCursor: [1/2]

- (void) didUpdateMouseCursor: (nonnull NSCursor *)  cursor

Called from the engine to notify the view that mouse cursor was updated while the mouse is over the view. The view is responsible from restoring the cursor when the mouse enters the view from another subview.

◆ didUpdateMouseCursor: [2/2]

- (void) didUpdateMouseCursor: (NSCursor*)  cursor
implementation

Definition at line 17 of file FlutterView.mm.

98 :(NSCursor*)cursor {
99 _lastCursor = cursor;
100}
NSCursor * _lastCursor
Definition: FlutterView.mm:17

◆ drawLayer:inContext:

- (void) drawLayer: (CALayer*)  layer
inContext: (CGContextRef)  context 
implementation

Definition at line 127 of file FlutterView.mm.

142 :(CALayer*)layer inContext:(CGContextRef)context {
143 TRACE_EVENT0("flutter", "SnapshotFlutterView");
144
145 if (layer != self.layer || context == nullptr) {
146 return;
147 }
148
149 auto screenshot = [_delegate takeScreenshot:flutter::Rasterizer::ScreenshotType::UncompressedImage
150 asBase64Encoded:NO];
151
152 if (!screenshot.data || screenshot.data->isEmpty() || screenshot.frame_size.isEmpty()) {
153 return;
154 }
155
156 NSData* data = [NSData dataWithBytes:const_cast<void*>(screenshot.data->data())
157 length:screenshot.data->size()];
158
159 fml::CFRef<CGDataProviderRef> image_data_provider(
160 CGDataProviderCreateWithCFData(reinterpret_cast<CFDataRef>(data)));
161
162 fml::CFRef<CGColorSpaceRef> colorspace(CGColorSpaceCreateDeviceRGB());
163
164 // Defaults for RGBA8888.
165 size_t bits_per_component = 8u;
166 size_t bits_per_pixel = 32u;
167 size_t bytes_per_row_multiplier = 4u;
168 CGBitmapInfo bitmap_info =
169 static_cast<CGBitmapInfo>(static_cast<uint32_t>(kCGImageAlphaPremultipliedLast) |
170 static_cast<uint32_t>(kCGBitmapByteOrder32Big));
171
172 switch (screenshot.pixel_format) {
175 // Assume unknown is Skia and is RGBA8888. Keep defaults.
176 break;
178 // Treat this as little endian with the alpha first so that it's read backwards.
179 bitmap_info =
180 static_cast<CGBitmapInfo>(static_cast<uint32_t>(kCGImageAlphaPremultipliedFirst) |
181 static_cast<uint32_t>(kCGBitmapByteOrder32Little));
182 break;
184 bits_per_component = 16u;
185 bits_per_pixel = 64u;
186 bytes_per_row_multiplier = 8u;
187 bitmap_info =
188 static_cast<CGBitmapInfo>(static_cast<uint32_t>(kCGImageAlphaPremultipliedLast) |
189 static_cast<uint32_t>(kCGBitmapFloatComponents) |
190 static_cast<uint32_t>(kCGBitmapByteOrder16Little));
191 break;
192 }
193
194 fml::CFRef<CGImageRef> image(CGImageCreate(
195 screenshot.frame_size.width(), // size_t width
196 screenshot.frame_size.height(), // size_t height
197 bits_per_component, // size_t bitsPerComponent
198 bits_per_pixel, // size_t bitsPerPixel,
199 bytes_per_row_multiplier * screenshot.frame_size.width(), // size_t bytesPerRow
200 colorspace, // CGColorSpaceRef space
201 bitmap_info, // CGBitmapInfo bitmapInfo
202 image_data_provider, // CGDataProviderRef provider
203 nullptr, // const CGFloat* decode
204 false, // bool shouldInterpolate
205 kCGRenderingIntentDefault // CGColorRenderingIntent intent
206 ));
207
208 const CGRect frame_rect =
209 CGRectMake(0.0, 0.0, screenshot.frame_size.width(), screenshot.frame_size.height());
210 CGContextSaveGState(context);
211 // If the CGContext is not a bitmap based context, this returns zero.
212 CGFloat height = CGBitmapContextGetHeight(context);
213 if (height == 0) {
214 height = CGFloat(screenshot.frame_size.height());
215 }
216 CGContextTranslateCTM(context, 0.0, height);
217 CGContextScaleCTM(context, 1.0, -1.0);
218 CGContextDrawImage(context, frame_rect, image);
219 CGContextRestoreGState(context);
220}
sk_sp< const SkImage > image
Definition: SkRecords.h:269
int32_t height
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63
#define TRACE_EVENT0(category_group, name)
Definition: trace_event.h:131

◆ init

- (instancetype) init
implementation

Definition at line 12 of file FlutterView.mm.

19 {
20 NSAssert(NO, @"FlutterView must initWithDelegate");
21 return nil;
22}

◆ initWithCoder: [1/2]

- (nullable instancetype) initWithCoder: (nonnull NSCoder *)  NS_UNAVAILABLE

◆ initWithCoder: [2/2]

- (instancetype) initWithCoder: (NSCoder*)  NS_UNAVAILABLE

Definition at line 12 of file FlutterView.mm.

29 :(NSCoder*)aDecoder {
30 NSAssert(NO, @"FlutterView must initWithDelegate");
31 return nil;
32}

◆ initWithDelegate:opaque:enableWideGamut:

- (instancetype) initWithDelegate: (id<FlutterViewEngineDelegate>)  delegate
opaque: (BOOL opaque
enableWideGamut: (BOOL NS_DESIGNATED_INITIALIZER 

Definition at line 12 of file FlutterView.mm.

65 :(id<FlutterViewEngineDelegate>)delegate
66 opaque:(BOOL)opaque
67 enableWideGamut:(BOOL)isWideGamutEnabled {
68 if (delegate == nil) {
69 NSLog(@"FlutterView delegate was nil.");
70 return nil;
71 }
72
73 self = [super initWithFrame:CGRectNull];
74
75 if (self) {
76 _delegate = delegate;
77 _isWideGamutEnabled = isWideGamutEnabled;
78 self.layer.opaque = opaque;
79
80 // This line is necessary. CoreAnimation(or UIKit) may take this to do
81 // something to compute the final frame presented on screen, if we don't set this,
82 // it will make it take long time for us to take next CAMetalDrawable and will
83 // cause constant junk during rendering.
84 self.backgroundColor = UIColor.clearColor;
85 }
86
87 return self;
88}
id< FlutterViewEngineDelegate > delegate
Definition: FlutterView.mm:12
int BOOL
Definition: windows_types.h:37

◆ initWithFrame: [1/2]

- (instancetype) initWithFrame: (CGRect)  NS_UNAVAILABLE

Definition at line 12 of file FlutterView.mm.

24 :(CGRect)frame {
25 NSAssert(NO, @"FlutterView must initWithDelegate");
26 return nil;
27}
double frame
Definition: examples.cpp:31

◆ initWithFrame: [2/2]

- (nonnull instancetype) initWithFrame: (NSRect)  NS_UNAVAILABLE

◆ initWithFrame:pixelFormat:

- (nullable instancetype) initWithFrame: (NSRect)  frameRect
pixelFormat: (nullable NSOpenGLPixelFormat *)  NS_UNAVAILABLE 

◆ initWithMTLDevice:commandQueue:delegate:threadSynchronizer:viewIdentifier: [1/2]

- (instancetype) initWithMTLDevice: (id<MTLDevice>)  device
commandQueue: (id<MTLCommandQueue>)  commandQueue
delegate: (id<FlutterViewDelegate>)  delegate
threadSynchronizer: (FlutterThreadSynchronizer*)  threadSynchronizer
viewIdentifier: (FlutterViewIdentifier viewIdentifier 
implementation

Definition at line 17 of file FlutterView.mm.

24 :(id<MTLDevice>)device
25 commandQueue:(id<MTLCommandQueue>)commandQueue
26 delegate:(id<FlutterViewDelegate>)delegate
27 threadSynchronizer:(FlutterThreadSynchronizer*)threadSynchronizer
28 viewIdentifier:(FlutterViewIdentifier)viewIdentifier {
29 self = [super initWithFrame:NSZeroRect];
30 if (self) {
31 [self setWantsLayer:YES];
32 [self setBackgroundColor:[NSColor blackColor]];
33 [self setLayerContentsRedrawPolicy:NSViewLayerContentsRedrawDuringViewResize];
34 _viewIdentifier = viewIdentifier;
36 _threadSynchronizer = threadSynchronizer;
37 _surfaceManager = [[FlutterSurfaceManager alloc] initWithDevice:device
38 commandQueue:commandQueue
39 layer:self.layer
40 delegate:self];
41 }
42 return self;
43}
VkDevice device
Definition: main.cc:53
__weak id< FlutterViewDelegate > _viewDelegate
Definition: FlutterView.mm:14
FlutterSurfaceManager * _surfaceManager
Definition: FlutterView.mm:16
FlutterThreadSynchronizer * _threadSynchronizer
Definition: FlutterView.mm:15
FlutterViewIdentifier _viewIdentifier
Definition: FlutterView.mm:13
int64_t FlutterViewIdentifier

◆ initWithMTLDevice:commandQueue:delegate:threadSynchronizer:viewIdentifier: [2/2]

- (nullable instancetype) initWithMTLDevice: (nonnull id< MTLDevice >)  device
commandQueue: (nonnull id< MTLCommandQueue >)  commandQueue
delegate: (nonnull id< FlutterViewDelegate >)  delegate
threadSynchronizer: (nonnull FlutterThreadSynchronizer *)  threadSynchronizer
viewIdentifier: (FlutterViewIdentifier NS_DESIGNATED_INITIALIZER 

Initialize a FlutterView that will be rendered to using Metal rendering apis.

◆ isAccessibilityElement

- (BOOL) isAccessibilityElement
implementation

Definition at line 127 of file FlutterView.mm.

222 {
223 // iOS does not provide an API to query whether the voice control
224 // is turned on or off. It is likely at least one of the assitive
225 // technologies is turned on if this method is called. If we do
226 // not catch it in notification center, we will catch it here.
227 //
228 // TODO(chunhtai): Remove this workaround once iOS provides an
229 // API to query whether voice control is enabled.
230 // https://github.com/flutter/flutter/issues/76808.
231 [self.delegate flutterViewAccessibilityDidCall];
232 return NO;
233}

◆ isFlipped

- (BOOL) isFlipped
implementation

Declares that the view uses a flipped coordinate system, consistent with Flutter conventions.

Definition at line 17 of file FlutterView.mm.

76 {
77 return YES;
78}

◆ isOpaque

- (BOOL) isOpaque
implementation

Definition at line 17 of file FlutterView.mm.

80 {
81 return YES;
82}

◆ isWideGamutSupported

- (BOOL) isWideGamutSupported
implementation

Definition at line 12 of file FlutterView.mm.

52 {
53 if (!self.delegate.isUsingImpeller) {
54 return NO;
55 }
56
57 FML_DCHECK(self.screen);
58
59 // This predicates the decision on the capabilities of the iOS device's
60 // display. This means external displays will not support wide gamut if the
61 // device's display doesn't support it. It practice that should be never.
62 return self.screen.traitCollection.displayGamut != UIDisplayGamutSRGB;
63}
#define FML_DCHECK(condition)
Definition: logging.h:103
UIScreen * screen()
Definition: FlutterView.mm:34

◆ layer:shouldInheritContentsScale:fromWindow:

- (BOOL) layer: (CALayer*)  layer
shouldInheritContentsScale: (CGFloat)  newScale
fromWindow: (NSWindow*)  window 
implementation

Definition at line 17 of file FlutterView.mm.

131 :(CALayer*)layer
132 shouldInheritContentsScale:(CGFloat)newScale
133 fromWindow:(NSWindow*)window {
134 return YES;
135}
GLFWwindow * window
Definition: main.cc:45

◆ layerClass

+ (Class) layerClass
implementation

Definition at line 127 of file FlutterView.mm.

137 {
140}
BOOL forceSoftwareRendering
Definition: FlutterView.h:49
IOSRenderingAPI GetRenderingAPIForProcess(bool force_software)
Class GetCoreAnimationLayerClassForRenderingAPI(IOSRenderingAPI rendering_api)

◆ layoutSubviews

- (void) layoutSubviews
implementation

Definition at line 90 of file FlutterView.mm.

100 {
101 if ([self.layer isKindOfClass:NSClassFromString(@"CAMetalLayer")]) {
102// It is a known Apple bug that CAMetalLayer incorrectly reports its supported
103// SDKs. It is, in fact, available since iOS 8.
104#pragma clang diagnostic push
105#pragma clang diagnostic ignored "-Wunguarded-availability-new"
106 CAMetalLayer* layer = (CAMetalLayer*)self.layer;
107#pragma clang diagnostic pop
108 CGFloat screenScale = self.screen.scale;
109 layer.allowsGroupOpacity = YES;
110 layer.contentsScale = screenScale;
111 layer.rasterizationScale = screenScale;
112 layer.framebufferOnly = flutter::Settings::kSurfaceDataAccessible ? NO : YES;
113 BOOL isWideGamutSupported = self.isWideGamutSupported;
114 if (_isWideGamutEnabled && isWideGamutSupported) {
115 CGColorSpaceRef srgb = CGColorSpaceCreateWithName(kCGColorSpaceExtendedSRGB);
116 layer.colorspace = srgb;
117 CFRelease(srgb);
118 layer.pixelFormat = MTLPixelFormatBGRA10_XR;
119 } else if (_isWideGamutEnabled && !isWideGamutSupported) {
121 }
122 }
123
124 [super layoutSubviews];
125}
BOOL isWideGamutSupported()
Definition: FlutterView.mm:52
static void PrintWideGamutWarningOnce()
Definition: FlutterView.mm:90
static constexpr bool kSurfaceDataAccessible
Definition: settings.h:113

◆ NS_UNAVAILABLE [1/3]

+ (instancetype) NS_UNAVAILABLE

◆ NS_UNAVAILABLE [2/3]

- (instancetype) NS_UNAVAILABLE

◆ NS_UNAVAILABLE [3/3]

- (nonnull instancetype) NS_UNAVAILABLE

◆ onPresent:withBlock:

- (void) onPresent: (CGSize)  frameSize
withBlock: (dispatch_block_t)  block 
implementation

Definition at line 17 of file FlutterView.mm.

45 :(CGSize)frameSize withBlock:(dispatch_block_t)block {
46 [_threadSynchronizer performCommitForView:_viewIdentifier size:frameSize notify:block];
47}

◆ pixelFormat

- (MTLPixelFormat) pixelFormat

Definition at line 12 of file FlutterView.mm.

41 {
42 if ([self.layer isKindOfClass:NSClassFromString(@"CAMetalLayer")]) {
43// It is a known Apple bug that CAMetalLayer incorrectly reports its supported
44// SDKs. It is, in fact, available since iOS 8.
45#pragma clang diagnostic push
46#pragma clang diagnostic ignored "-Wunguarded-availability-new"
47 CAMetalLayer* layer = (CAMetalLayer*)self.layer;
48 return layer.pixelFormat;
49 }
50 return MTLPixelFormatBGRA8Unorm;
51}

◆ PrintWideGamutWarningOnce

+ (static void) PrintWideGamutWarningOnce
implementation

Definition at line 90 of file FlutterView.mm.

90 {
91 static BOOL did_print = NO;
92 if (did_print) {
93 return;
94 }
95 FML_DLOG(WARNING) << "Rendering wide gamut colors is turned on but isn't "
96 "supported, downgrading the color gamut to sRGB.";
97 did_print = YES;
98}
#define FML_DLOG(severity)
Definition: logging.h:102

◆ reshaped

- (void) reshaped
implementation

Reimplemented in TestFlutterView.

Definition at line 17 of file FlutterView.mm.

53 {
54 CGSize scaledSize = [self convertSizeToBacking:self.bounds.size];
55 [_threadSynchronizer beginResizeForView:_viewIdentifier
56 size:scaledSize
57 notify:^{
58 [_viewDelegate viewDidReshape:self];
59 }];
60}

◆ screen

- (UIScreen *) screen

Definition at line 12 of file FlutterView.mm.

34 {
35 if (@available(iOS 13.0, *)) {
36 return self.window.windowScene.screen;
37 }
38 return UIScreen.mainScreen;
39}

◆ setBackgroundColor: [1/2]

- (void) setBackgroundColor: (nonnull NSColor *)  color

By default, the FlutterSurfaceManager creates two layers to manage Flutter content, the content layer and containing layer. To set the native background color, onto which the Flutter content is drawn, call this method with the NSColor which you would like to override the default, black background color with.

◆ setBackgroundColor: [2/2]

- (void) setBackgroundColor: (NSColor*)  color
implementation

Definition at line 17 of file FlutterView.mm.

62 :(NSColor*)color {
63 self.layer.backgroundColor = color.CGColor;
64}
DlColor color

◆ setForceSoftwareRendering:

+ (void) setForceSoftwareRendering: (BOOL forceSoftwareRendering
implementation

Definition at line 127 of file FlutterView.mm.

◆ setFrameSize:

- (void) setFrameSize: (NSSize)  newSize
implementation

Definition at line 17 of file FlutterView.mm.

68 :(NSSize)newSize {
69 [super setFrameSize:newSize];
70 [self reshaped];
71}

◆ viewDidChangeBackingProperties

- (void) viewDidChangeBackingProperties
implementation

Definition at line 17 of file FlutterView.mm.

125 {
126 [super viewDidChangeBackingProperties];
127 // Force redraw
128 [_viewDelegate viewDidReshape:self];
129}

Member Data Documentation

◆ _lastCursor

- (NSCursor*) _lastCursor
protected

Definition at line 17 of file FlutterView.mm.

◆ _surfaceManager

- (FlutterSurfaceManager*) _surfaceManager
protected

Definition at line 16 of file FlutterView.mm.

◆ _threadSynchronizer

- (FlutterThreadSynchronizer*) _threadSynchronizer
protected

Definition at line 15 of file FlutterView.mm.

◆ _viewDelegate

- (__weak id<FlutterViewDelegate>) _viewDelegate
protected

Definition at line 14 of file FlutterView.mm.

◆ _viewIdentifier

- (FlutterViewIdentifier) _viewIdentifier
protected

Definition at line 13 of file FlutterView.mm.

Property Documentation

◆ delegate

- (id<FlutterViewEngineDelegate>) delegate
readwritenonatomicweakimplementation

Definition at line 12 of file FlutterView.mm.

◆ forceSoftwareRendering

- (BOOL) forceSoftwareRendering
readwritenonatomicassign

Definition at line 49 of file FlutterView.h.

◆ surfaceManager

- (FlutterSurfaceManager *) surfaceManager
readnonatomicassign

Returns SurfaceManager for this view. SurfaceManager is responsible for providing and presenting render surfaces.

Definition at line 57 of file FlutterView.h.


The documentation for this class was generated from the following files: