| Flutter iOS Embedder |

FlutterView.mm
Go to the documentation of this file.
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
5 #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterView.h"
6
7 #include "flutter/fml/platform/darwin/cf_utils.h"
8 #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterSharedApplication.h"
9 #import "flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject.h"
10
11 FLUTTER_ASSERT_ARC
12
13 @interface FlutterView ()
14 @property(nonatomic, weak) id<FlutterViewEngineDelegate> delegate;
15 @end
16
17 @implementation FlutterView {
18 BOOL _isWideGamutEnabled;
19 }
20
21 - (instancetype)init {
22 NSAssert(NO, @"FlutterView must initWithDelegate");
23 return nil;
24 }
25
26 - (instancetype)initWithFrame:(CGRect)frame {
27 NSAssert(NO, @"FlutterView must initWithDelegate");
28 return nil;
29 }
30
31 - (instancetype)initWithCoder:(NSCoder*)aDecoder {
32 NSAssert(NO, @"FlutterView must initWithDelegate");
33 return nil;
34 }
35
36 - (UIScreen*)screen {
37 return self.window.windowScene.screen;
38 }
39
40 - (MTLPixelFormat)pixelFormat {
41 if ([self.layer isKindOfClass:[CAMetalLayer class]]) {
42 // It is a known Apple bug that CAMetalLayer incorrectly reports its supported
43 // SDKs. It is, in fact, available since iOS 8.
44 #pragma clang diagnostic push
45 #pragma clang diagnostic ignored "-Wunguarded-availability-new"
46 CAMetalLayer* layer = (CAMetalLayer*)self.layer;
47 return layer.pixelFormat;
48 }
49 return MTLPixelFormatBGRA8Unorm;
50 }
51 - (BOOL)isWideGamutSupported {
52 FML_DCHECK(self.screen);
53
54 // Wide Gamut is not supported for iOS Extensions due to memory limitations
55 // (see https://github.com/flutter/flutter/issues/165086).
56 if (FlutterSharedApplication.isAppExtension) {
57 return NO;
58 }
59
60 // This predicates the decision on the capabilities of the iOS device's
61 // display. This means external displays will not support wide gamut if the
62 // device's display doesn't support it. It practice that should be never.
63 return self.screen.traitCollection.displayGamut != UIDisplayGamutSRGB;
64 }
65
66 - (instancetype)initWithDelegate:(id<FlutterViewEngineDelegate>)delegate
67 opaque:(BOOL)opaque
68 enableWideGamut:(BOOL)isWideGamutEnabled {
69 if (delegate == nil) {
70 NSLog(@"FlutterView delegate was nil.");
71 return nil;
72 }
73
74 self = [super initWithFrame:CGRectNull];
75
76 if (self) {
77 _delegate = delegate;
78 _isWideGamutEnabled = isWideGamutEnabled;
79 self.layer.opaque = opaque;
80 }
81
82 return self;
83 }
84
85 static void PrintWideGamutWarningOnce() {
86 static BOOL did_print = NO;
87 if (did_print) {
88 return;
89 }
90 FML_DLOG(WARNING) << "Rendering wide gamut colors is turned on but isn't "
91 "supported, downgrading the color gamut to sRGB.";
92 did_print = YES;
93 }
94
95 - (void)layoutSubviews {
96 if ([self.layer isKindOfClass:[CAMetalLayer class]]) {
97 // It is a known Apple bug that CAMetalLayer incorrectly reports its supported
98 // SDKs. It is, in fact, available since iOS 8.
99 #pragma clang diagnostic push
100 #pragma clang diagnostic ignored "-Wunguarded-availability-new"
101 CAMetalLayer* layer = (CAMetalLayer*)self.layer;
102 #pragma clang diagnostic pop
103 CGFloat screenScale = self.screen.scale;
104 layer.allowsGroupOpacity = YES;
105 layer.contentsScale = screenScale;
106 layer.rasterizationScale = screenScale;
107 layer.framebufferOnly = flutter::Settings::kSurfaceDataAccessible ? NO : YES;
108 if (_isWideGamutEnabled && self.isWideGamutSupported) {
109 fml::CFRef<CGColorSpaceRef> srgb(CGColorSpaceCreateWithName(kCGColorSpaceExtendedSRGB));
110 layer.colorspace = srgb;
111 layer.pixelFormat = MTLPixelFormatBGRA10_XR;
112 } else if (_isWideGamutEnabled && !self.isWideGamutSupported) {
113 PrintWideGamutWarningOnce();
114 }
115 }
116
117 [super layoutSubviews];
118 }
119
120 + (Class)layerClass {
121 return flutter::GetCoreAnimationLayerClassForRenderingAPI(
122 flutter::GetRenderingAPIForProcess(/*force_software=*/false));
123 }
124
125 - (void)drawLayer:(CALayer*)layer inContext:(CGContextRef)context {
126 TRACE_EVENT0("flutter", "SnapshotFlutterView");
127
128 if (layer != self.layer || context == nullptr) {
129 return;
130 }
131
132 auto screenshot = [_delegate takeScreenshot:flutter::Rasterizer::ScreenshotType::UncompressedImage
133 asBase64Encoded:NO];
134
135 if (!screenshot.data || screenshot.data->isEmpty() || screenshot.frame_size.isEmpty()) {
136 return;
137 }
138
139 NSData* data = [NSData dataWithBytes:const_cast<void*>(screenshot.data->data())
140 length:screenshot.data->size()];
141
142 fml::CFRef<CGDataProviderRef> image_data_provider(
143 CGDataProviderCreateWithCFData(reinterpret_cast<CFDataRef>(data)));
144
145 fml::CFRef<CGColorSpaceRef> colorspace(CGColorSpaceCreateDeviceRGB());
146
147 // Defaults for RGBA8888.
148 size_t bits_per_component = 8u;
149 size_t bits_per_pixel = 32u;
150 size_t bytes_per_row_multiplier = 4u;
151 CGBitmapInfo bitmap_info =
152 static_cast<CGBitmapInfo>(static_cast<uint32_t>(kCGImageAlphaPremultipliedLast) |
153 static_cast<uint32_t>(kCGBitmapByteOrder32Big));
154
155 switch (screenshot.pixel_format) {
156 case flutter::Rasterizer::ScreenshotFormat::kUnknown:
157 case flutter::Rasterizer::ScreenshotFormat::kR8G8B8A8UNormInt:
158 // Assume unknown is Skia and is RGBA8888. Keep defaults.
159 break;
160 case flutter::Rasterizer::ScreenshotFormat::kB8G8R8A8UNormInt:
161 // Treat this as little endian with the alpha first so that it's read backwards.
162 bitmap_info =
163 static_cast<CGBitmapInfo>(static_cast<uint32_t>(kCGImageAlphaPremultipliedFirst) |
164 static_cast<uint32_t>(kCGBitmapByteOrder32Little));
165 break;
166 case flutter::Rasterizer::ScreenshotFormat::kR16G16B16A16Float:
167 bits_per_component = 16u;
168 bits_per_pixel = 64u;
169 bytes_per_row_multiplier = 8u;
170 bitmap_info =
171 static_cast<CGBitmapInfo>(static_cast<uint32_t>(kCGImageAlphaPremultipliedLast) |
172 static_cast<uint32_t>(kCGBitmapFloatComponents) |
173 static_cast<uint32_t>(kCGBitmapByteOrder16Little));
174 break;
175 }
176
177 fml::CFRef<CGImageRef> image(CGImageCreate(
178 screenshot.frame_size.width(), // size_t width
179 screenshot.frame_size.height(), // size_t height
180 bits_per_component, // size_t bitsPerComponent
181 bits_per_pixel, // size_t bitsPerPixel,
182 bytes_per_row_multiplier * screenshot.frame_size.width(), // size_t bytesPerRow
183 colorspace, // CGColorSpaceRef space
184 bitmap_info, // CGBitmapInfo bitmapInfo
185 image_data_provider, // CGDataProviderRef provider
186 nullptr, // const CGFloat* decode
187 false, // bool shouldInterpolate
188 kCGRenderingIntentDefault // CGColorRenderingIntent intent
189 ));
190
191 const CGRect frame_rect =
192 CGRectMake(0.0, 0.0, screenshot.frame_size.width(), screenshot.frame_size.height());
193 CGContextSaveGState(context);
194 // If the CGContext is not a bitmap based context, this returns zero.
195 CGFloat height = CGBitmapContextGetHeight(context);
196 if (height == 0) {
197 height = CGFloat(screenshot.frame_size.height());
198 }
199 CGContextTranslateCTM(context, 0.0, height);
200 CGContextScaleCTM(context, 1.0, -1.0);
201 CGContextDrawImage(context, frame_rect, image);
202 CGContextRestoreGState(context);
203 }
204
205 - (BOOL)isAccessibilityElement {
206 // iOS does not provide an API to query whether the voice control
207 // is turned on or off. It is likely at least one of the assitive
208 // technologies is turned on if this method is called. If we do
209 // not catch it in notification center, we will catch it here.
210 //
211 // TODO(chunhtai): Remove this workaround once iOS provides an
212 // API to query whether voice control is enabled.
213 // https://github.com/flutter/flutter/issues/76808.
214 [self.delegate flutterViewAccessibilityDidCall];
215 return NO;
216 }
217
218 // Enables keyboard-based navigation when the user turns on
219 // full keyboard access (FKA), using existing accessibility information.
220 //
221 // iOS does not provide any API for monitoring or querying whether FKA is on,
222 // but it does call isAccessibilityElement if FKA is on,
223 // so the isAccessibilityElement implementation above will be called
224 // when the view appears and the accessibility information will most likely
225 // be available by the time the user starts to interact with the app using FKA.
226 //
227 // See SemanticsObject+UIFocusSystem.mm for more details.
228 - (NSArray<id<UIFocusItem>>*)focusItemsInRect:(CGRect)rect {
229 NSObject* rootAccessibilityElement =
230 [self.accessibilityElements count] > 0 ? self.accessibilityElements[0] : nil;
231 return [rootAccessibilityElement isKindOfClass:[SemanticsObjectContainer class]]
232 ? @[ [rootAccessibilityElement accessibilityElementAtIndex:0] ]
233 : nil;
234 }
235
236 - (NSArray<id<UIFocusEnvironment>>*)preferredFocusEnvironments {
237 // Occasionally we add subviews to FlutterView (text fields for example).
238 // These views shouldn't be directly visible to the iOS focus engine, instead
239 // the focus engine should only interact with the designated focus items
240 // (SemanticsObjects).
241 return nil;
242 }
243
244 @end
FlutterSharedApplication.h
initWithFrame
instancetype initWithFrame
Definition: FlutterTextInputPlugin.h:179
initWithCoder
instancetype initWithCoder
Definition: FlutterTextInputPlugin.h:178
FlutterView.h
SemanticsObject.h
FlutterSharedApplication
Definition: FlutterSharedApplication.h:11
FlutterSharedApplication::isAppExtension
BOOL isAppExtension
Definition: FlutterSharedApplication.h:15
FlutterView
Definition: FlutterView.h:33
SemanticsObjectContainer
Definition: SemanticsObject.h:228
FLUTTER_ASSERT_ARC
Definition: FlutterChannelKeyResponder.mm:13
flutter::GetRenderingAPIForProcess
IOSRenderingAPI GetRenderingAPIForProcess(bool force_software)
Definition: rendering_api_selection.mm:31
flutter::GetCoreAnimationLayerClassForRenderingAPI
Class GetCoreAnimationLayerClassForRenderingAPI(IOSRenderingAPI rendering_api)
Definition: rendering_api_selection.mm:59
FlutterViewEngineDelegate-p
Definition: FlutterView.h:14