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 
6 
7 #include "flutter/fml/platform/darwin/cf_utils.h"
12 
14 
15 @interface FlutterView ()
16 @property(nonatomic, weak) id<FlutterViewEngineDelegate> delegate;
17 @property(nonatomic, weak) UIWindowScene* previousScene;
18 @end
19 
21 @end
22 
23 @implementation FlutterView {
24  BOOL _isWideGamutEnabled;
26 }
27 
28 - (instancetype)init {
29  NSAssert(NO, @"FlutterView must initWithDelegate");
30  return nil;
31 }
32 
33 - (instancetype)initWithFrame:(CGRect)frame {
34  NSAssert(NO, @"FlutterView must initWithDelegate");
35  return nil;
36 }
37 
38 - (instancetype)initWithCoder:(NSCoder*)aDecoder {
39  NSAssert(NO, @"FlutterView must initWithDelegate");
40  return nil;
41 }
42 
43 - (UIScreen*)screen {
44  return self.window.windowScene.screen;
45 }
46 
47 // iOS has a concept of "intrinsicContentSize", which indicates the size a view would like to be
48 // based on its content. When an intrinsicContentSize is set, iOS will automatically add Auto Layout
49 // constraints for the width and/or height. However, the constraints use a private API. There are
50 // situations where we may want to filter these constraints. To avoid using a private API, Flutter
51 // creates a custom constraint called FlutterAutoResizeLayoutConstraint to add a width/height
52 // constraint that reflects the intrinsicContentSize.
53 - (void)setIntrinsicContentSize:(CGSize)size {
54  if (!self.autoResizable) {
55  return;
56  }
57 
58  UIWindow* window = self.window;
59  CGFloat scale = window ? self.window.windowScene.screen.scale : self.traitCollection.displayScale;
60  CGSize scaledSize = CGSizeMake(size.width / scale, size.height / scale);
61 
62  CGSize roundedScaleSize = CGSizeMake(roundf(scaledSize.width), roundf(scaledSize.height));
63  CGSize roundedIntrinsicSize =
64  CGSizeMake(roundf(_intrinsicSize.width), roundf(_intrinsicSize.height));
65 
66  // If the size has not changed, don't update constraints.
67  if (CGSizeEqualToSize(roundedIntrinsicSize, roundedScaleSize)) {
68  return;
69  }
70  _intrinsicSize = scaledSize;
71 
72  self.translatesAutoresizingMaskIntoConstraints = false;
73 
74  // Remove any existing FlutterAutoResizeLayoutConstraint
75  [self removeAutoResizeLayoutConstraints];
76 
77  FlutterAutoResizeLayoutConstraint* widthConstraint =
78  [FlutterAutoResizeLayoutConstraint constraintWithItem:self
79  attribute:NSLayoutAttributeWidth
80  relatedBy:NSLayoutRelationEqual
81  toItem:nil
82  attribute:NSLayoutAttributeNotAnAttribute
83  multiplier:1.0
84  constant:scaledSize.width];
85 
86  FlutterAutoResizeLayoutConstraint* heightConstraint =
87  [FlutterAutoResizeLayoutConstraint constraintWithItem:self
88  attribute:NSLayoutAttributeHeight
89  relatedBy:NSLayoutRelationEqual
90  toItem:nil
91  attribute:NSLayoutAttributeNotAnAttribute
92  multiplier:1.0
93  constant:scaledSize.height];
94 
95  [NSLayoutConstraint activateConstraints:@[ widthConstraint, heightConstraint ]];
96  [self setNeedsLayout];
97 }
98 
99 - (void)resetIntrinsicContentSize {
100  _intrinsicSize = CGSizeMake(UIViewNoIntrinsicMetric, UIViewNoIntrinsicMetric);
101  [self removeAutoResizeLayoutConstraints];
102 }
103 
104 - (void)removeAutoResizeLayoutConstraints {
105  for (NSLayoutConstraint* constraint in self.constraints) {
106  if ([constraint isKindOfClass:[FlutterAutoResizeLayoutConstraint class]]) {
107  constraint.active = NO;
108  }
109  }
110 }
111 
112 - (MTLPixelFormat)pixelFormat {
113  if ([self.layer isKindOfClass:[CAMetalLayer class]]) {
114 // It is a known Apple bug that CAMetalLayer incorrectly reports its supported
115 // SDKs. It is, in fact, available since iOS 8.
116 #pragma clang diagnostic push
117 #pragma clang diagnostic ignored "-Wunguarded-availability-new"
118  CAMetalLayer* layer = (CAMetalLayer*)self.layer;
119  return layer.pixelFormat;
120  }
121  return MTLPixelFormatBGRA8Unorm;
122 }
123 - (BOOL)isWideGamutSupported {
124  FML_DCHECK(self.screen);
125 
126  // Wide Gamut is not supported for iOS Extensions due to memory limitations
127  // (see https://github.com/flutter/flutter/issues/165086).
129  return NO;
130  }
131 
132  // This predicates the decision on the capabilities of the iOS device's
133  // display. This means external displays will not support wide gamut if the
134  // device's display doesn't support it. It practice that should be never.
135  return self.screen.traitCollection.displayGamut != UIDisplayGamutSRGB;
136 }
137 
138 - (instancetype)initWithDelegate:(id<FlutterViewEngineDelegate>)delegate
139  opaque:(BOOL)opaque
140  enableWideGamut:(BOOL)isWideGamutEnabled {
141  if (delegate == nil) {
142  NSLog(@"FlutterView delegate was nil.");
143  return nil;
144  }
145 
146  self = [super initWithFrame:CGRectNull];
147 
148  if (self) {
149  _delegate = delegate;
150  _isWideGamutEnabled = isWideGamutEnabled;
151  self.layer.opaque = opaque;
152  _autoResizable = NO;
153  _intrinsicSize = CGSizeMake(UIViewNoIntrinsicMetric, UIViewNoIntrinsicMetric);
154  }
155 
156  return self;
157 }
158 
159 static void PrintWideGamutWarningOnce() {
160  static BOOL did_print = NO;
161  if (did_print) {
162  return;
163  }
164  FML_DLOG(WARNING) << "Rendering wide gamut colors is turned on but isn't "
165  "supported, downgrading the color gamut to sRGB.";
166  did_print = YES;
167 }
168 
169 - (void)layoutSubviews {
170  if ([self.layer isKindOfClass:[CAMetalLayer class]]) {
171 // It is a known Apple bug that CAMetalLayer incorrectly reports its supported
172 // SDKs. It is, in fact, available since iOS 8.
173 #pragma clang diagnostic push
174 #pragma clang diagnostic ignored "-Wunguarded-availability-new"
175  CAMetalLayer* layer = (CAMetalLayer*)self.layer;
176 #pragma clang diagnostic pop
177  CGFloat screenScale = self.screen.scale;
178  layer.allowsGroupOpacity = YES;
179  layer.contentsScale = screenScale;
180  layer.rasterizationScale = screenScale;
181  layer.framebufferOnly = flutter::Settings::kSurfaceDataAccessible ? NO : YES;
182  if (_isWideGamutEnabled && self.isWideGamutSupported) {
183  fml::CFRef<CGColorSpaceRef> srgb(CGColorSpaceCreateWithName(kCGColorSpaceExtendedSRGB));
184  layer.colorspace = srgb;
185  layer.pixelFormat = MTLPixelFormatBGRA10_XR;
186  } else if (_isWideGamutEnabled && !self.isWideGamutSupported) {
187  PrintWideGamutWarningOnce();
188  }
189  }
190 
191  [super layoutSubviews];
192 }
193 
194 + (Class)layerClass {
196 }
197 
198 - (void)drawLayer:(CALayer*)layer inContext:(CGContextRef)context {
199  TRACE_EVENT0("flutter", "SnapshotFlutterView");
200 
201  if (layer != self.layer || context == nullptr) {
202  return;
203  }
204 
205  auto screenshot = [_delegate takeScreenshot:flutter::Rasterizer::ScreenshotType::UncompressedImage
206  asBase64Encoded:NO];
207 
208  if (!screenshot.data || screenshot.data->isEmpty() || screenshot.frame_size.IsEmpty()) {
209  return;
210  }
211 
212  NSData* data = [NSData dataWithBytes:const_cast<void*>(screenshot.data->data())
213  length:screenshot.data->size()];
214 
215  fml::CFRef<CGDataProviderRef> image_data_provider(
216  CGDataProviderCreateWithCFData(reinterpret_cast<CFDataRef>(data)));
217 
218  fml::CFRef<CGColorSpaceRef> colorspace(CGColorSpaceCreateDeviceRGB());
219 
220  // Defaults for RGBA8888.
221  size_t bits_per_component = 8u;
222  size_t bits_per_pixel = 32u;
223  size_t bytes_per_row_multiplier = 4u;
224  CGBitmapInfo bitmap_info =
225  static_cast<CGBitmapInfo>(static_cast<uint32_t>(kCGImageAlphaPremultipliedLast) |
226  static_cast<uint32_t>(kCGBitmapByteOrder32Big));
227 
228  switch (screenshot.pixel_format) {
229  case flutter::Rasterizer::ScreenshotFormat::kUnknown:
230  case flutter::Rasterizer::ScreenshotFormat::kR8G8B8A8UNormInt:
231  // Assume unknown is Skia and is RGBA8888. Keep defaults.
232  break;
233  case flutter::Rasterizer::ScreenshotFormat::kB8G8R8A8UNormInt:
234  // Treat this as little endian with the alpha first so that it's read backwards.
235  bitmap_info =
236  static_cast<CGBitmapInfo>(static_cast<uint32_t>(kCGImageAlphaPremultipliedFirst) |
237  static_cast<uint32_t>(kCGBitmapByteOrder32Little));
238  break;
239  case flutter::Rasterizer::ScreenshotFormat::kR16G16B16A16Float:
240  bits_per_component = 16u;
241  bits_per_pixel = 64u;
242  bytes_per_row_multiplier = 8u;
243  bitmap_info =
244  static_cast<CGBitmapInfo>(static_cast<uint32_t>(kCGImageAlphaPremultipliedLast) |
245  static_cast<uint32_t>(kCGBitmapFloatComponents) |
246  static_cast<uint32_t>(kCGBitmapByteOrder16Little));
247  break;
248  }
249 
250  fml::CFRef<CGImageRef> image(CGImageCreate(
251  screenshot.frame_size.width, // size_t width
252  screenshot.frame_size.height, // size_t height
253  bits_per_component, // size_t bitsPerComponent
254  bits_per_pixel, // size_t bitsPerPixel,
255  bytes_per_row_multiplier * screenshot.frame_size.width, // size_t bytesPerRow
256  colorspace, // CGColorSpaceRef space
257  bitmap_info, // CGBitmapInfo bitmapInfo
258  image_data_provider, // CGDataProviderRef provider
259  nullptr, // const CGFloat* decode
260  false, // bool shouldInterpolate
261  kCGRenderingIntentDefault // CGColorRenderingIntent intent
262  ));
263 
264  const CGRect frame_rect =
265  CGRectMake(0.0, 0.0, screenshot.frame_size.width, screenshot.frame_size.height);
266  CGContextSaveGState(context);
267  // If the CGContext is not a bitmap based context, this returns zero.
268  CGFloat height = CGBitmapContextGetHeight(context);
269  if (height == 0) {
270  height = CGFloat(screenshot.frame_size.height);
271  }
272  CGContextTranslateCTM(context, 0.0, height);
273  CGContextScaleCTM(context, 1.0, -1.0);
274  CGContextDrawImage(context, frame_rect, image);
275  CGContextRestoreGState(context);
276 }
277 
278 - (BOOL)isAccessibilityElement {
279  // iOS does not provide an API to query whether the voice control
280  // is turned on or off. It is likely at least one of the assitive
281  // technologies is turned on if this method is called. If we do
282  // not catch it in notification center, we will catch it here.
283  //
284  // TODO(chunhtai): Remove this workaround once iOS provides an
285  // API to query whether voice control is enabled.
286  // https://github.com/flutter/flutter/issues/76808.
287  [self.delegate flutterViewAccessibilityDidCall];
288  return NO;
289 }
290 
291 // Enables keyboard-based navigation when the user turns on
292 // full keyboard access (FKA), using existing accessibility information.
293 //
294 // iOS does not provide any API for monitoring or querying whether FKA is on,
295 // but it does call isAccessibilityElement if FKA is on,
296 // so the isAccessibilityElement implementation above will be called
297 // when the view appears and the accessibility information will most likely
298 // be available by the time the user starts to interact with the app using FKA.
299 //
300 // See SemanticsObject+UIFocusSystem.mm for more details.
301 - (NSArray<id<UIFocusItem>>*)focusItemsInRect:(CGRect)rect {
302  NSObject* rootAccessibilityElement =
303  [self.accessibilityElements count] > 0 ? self.accessibilityElements[0] : nil;
304  return [rootAccessibilityElement isKindOfClass:[SemanticsObjectContainer class]]
305  ? @[ [rootAccessibilityElement accessibilityElementAtIndex:0] ]
306  : nil;
307 }
308 
309 - (NSArray<id<UIFocusEnvironment>>*)preferredFocusEnvironments {
310  // Occasionally we add subviews to FlutterView (text fields for example).
311  // These views shouldn't be directly visible to the iOS focus engine, instead
312  // the focus engine should only interact with the designated focus items
313  // (SemanticsObjects).
314  return nil;
315 }
316 
317 - (void)willMoveToWindow:(UIWindow*)newWindow {
318  // When a FlutterView moves windows, it may also be moving scenes. Add/remove the FlutterEngine
319  // from the FlutterSceneLifeCycleProvider.sceneLifeCycleDelegate if it changes scenes.
320  UIWindowScene* newScene = newWindow.windowScene;
321  UIWindowScene* currentScene = self.window.windowScene;
322 
323  if (newScene == currentScene) {
324  return;
325  }
326 
327  // Remove the engine from the previous scene if it's no longer in that window and scene.
328  FlutterPluginSceneLifeCycleDelegate* previousSceneLifeCycleDelegate =
329  [FlutterPluginSceneLifeCycleDelegate fromScene:self.previousScene];
330  if (previousSceneLifeCycleDelegate) {
331  [previousSceneLifeCycleDelegate removeFlutterManagedEngine:(FlutterEngine*)self.delegate];
332  self.previousScene = nil;
333  }
334 
335  if (newScene) {
336  // Add the engine to the new scene's lifecycle delegate.
337  FlutterPluginSceneLifeCycleDelegate* newSceneLifeCycleDelegate =
338  [FlutterPluginSceneLifeCycleDelegate fromScene:newScene];
339  if (newSceneLifeCycleDelegate) {
340  [newSceneLifeCycleDelegate addFlutterManagedEngine:(FlutterEngine*)self.delegate];
341  }
342  } else {
343  // If the view is being removed from a window, store the current scene to remove the engine
344  // from it later when the view is added to a new window.
345  self.previousScene = currentScene;
346  }
347 }
348 @end
instancetype initWithFrame
instancetype initWithCoder
CGSize _intrinsicSize
Definition: FlutterView.mm:23
Class GetCoreAnimationLayerClass()