7 #include "flutter/fml/platform/darwin/cf_utils.h"
13 @property(nonatomic, weak) id<FlutterViewEngineDelegate> delegate;
17 BOOL _isWideGamutEnabled;
20 - (instancetype)init {
21 NSAssert(NO,
@"FlutterView must initWithDelegate");
26 NSAssert(NO,
@"FlutterView must initWithDelegate");
31 NSAssert(NO,
@"FlutterView must initWithDelegate");
36 if (@available(iOS 13.0, *)) {
37 return self.window.windowScene.screen;
39 return UIScreen.mainScreen;
42 - (MTLPixelFormat)pixelFormat {
43 if ([
self.layer isKindOfClass:[CAMetalLayer
class]]) {
46 #pragma clang diagnostic push
47 #pragma clang diagnostic ignored "-Wunguarded-availability-new"
48 CAMetalLayer* layer = (CAMetalLayer*)
self.layer;
49 return layer.pixelFormat;
51 return MTLPixelFormatBGRA8Unorm;
53 - (BOOL)isWideGamutSupported {
54 FML_DCHECK(
self.screen);
59 return self.screen.traitCollection.displayGamut != UIDisplayGamutSRGB;
64 enableWideGamut:(BOOL)isWideGamutEnabled {
65 if (delegate == nil) {
66 NSLog(
@"FlutterView delegate was nil.");
70 self = [
super initWithFrame:CGRectNull];
74 _isWideGamutEnabled = isWideGamutEnabled;
75 self.layer.opaque = opaque;
81 static void PrintWideGamutWarningOnce() {
82 static BOOL did_print = NO;
86 FML_DLOG(WARNING) <<
"Rendering wide gamut colors is turned on but isn't "
87 "supported, downgrading the color gamut to sRGB.";
91 - (void)layoutSubviews {
92 if ([
self.layer isKindOfClass:[CAMetalLayer
class]]) {
95 #pragma clang diagnostic push
96 #pragma clang diagnostic ignored "-Wunguarded-availability-new"
97 CAMetalLayer* layer = (CAMetalLayer*)
self.layer;
98 #pragma clang diagnostic pop
99 CGFloat screenScale =
self.screen.scale;
100 layer.allowsGroupOpacity = YES;
101 layer.contentsScale = screenScale;
102 layer.rasterizationScale = screenScale;
103 layer.framebufferOnly = flutter::Settings::kSurfaceDataAccessible ? NO : YES;
104 if (_isWideGamutEnabled &&
self.isWideGamutSupported) {
105 fml::CFRef<CGColorSpaceRef> srgb(CGColorSpaceCreateWithName(kCGColorSpaceExtendedSRGB));
106 layer.colorspace = srgb;
107 layer.pixelFormat = MTLPixelFormatBGRA10_XR;
108 }
else if (_isWideGamutEnabled && !
self.isWideGamutSupported) {
109 PrintWideGamutWarningOnce();
113 [
super layoutSubviews];
116 + (Class)layerClass {
121 - (void)drawLayer:(CALayer*)layer inContext:(CGContextRef)context {
122 TRACE_EVENT0(
"flutter",
"SnapshotFlutterView");
124 if (layer !=
self.layer || context ==
nullptr) {
128 auto screenshot = [_delegate takeScreenshot:flutter::Rasterizer::ScreenshotType::UncompressedImage
131 if (!screenshot.data || screenshot.data->isEmpty() || screenshot.frame_size.isEmpty()) {
135 NSData* data = [NSData dataWithBytes:const_cast<void*>(screenshot.data->data())
136 length:screenshot.data->size()];
138 fml::CFRef<CGDataProviderRef> image_data_provider(
139 CGDataProviderCreateWithCFData(
reinterpret_cast<CFDataRef
>(data)));
141 fml::CFRef<CGColorSpaceRef> colorspace(CGColorSpaceCreateDeviceRGB());
144 size_t bits_per_component = 8u;
145 size_t bits_per_pixel = 32u;
146 size_t bytes_per_row_multiplier = 4u;
147 CGBitmapInfo bitmap_info =
148 static_cast<CGBitmapInfo
>(
static_cast<uint32_t
>(kCGImageAlphaPremultipliedLast) |
149 static_cast<uint32_t
>(kCGBitmapByteOrder32Big));
151 switch (screenshot.pixel_format) {
152 case flutter::Rasterizer::ScreenshotFormat::kUnknown:
153 case flutter::Rasterizer::ScreenshotFormat::kR8G8B8A8UNormInt:
156 case flutter::Rasterizer::ScreenshotFormat::kB8G8R8A8UNormInt:
159 static_cast<CGBitmapInfo
>(
static_cast<uint32_t
>(kCGImageAlphaPremultipliedFirst) |
160 static_cast<uint32_t
>(kCGBitmapByteOrder32Little));
162 case flutter::Rasterizer::ScreenshotFormat::kR16G16B16A16Float:
163 bits_per_component = 16u;
164 bits_per_pixel = 64u;
165 bytes_per_row_multiplier = 8u;
167 static_cast<CGBitmapInfo
>(
static_cast<uint32_t
>(kCGImageAlphaPremultipliedLast) |
168 static_cast<uint32_t
>(kCGBitmapFloatComponents) |
169 static_cast<uint32_t
>(kCGBitmapByteOrder16Little));
173 fml::CFRef<CGImageRef> image(CGImageCreate(
174 screenshot.frame_size.width(),
175 screenshot.frame_size.height(),
178 bytes_per_row_multiplier * screenshot.frame_size.width(),
184 kCGRenderingIntentDefault
187 const CGRect frame_rect =
188 CGRectMake(0.0, 0.0, screenshot.frame_size.width(), screenshot.frame_size.height());
189 CGContextSaveGState(context);
191 CGFloat height = CGBitmapContextGetHeight(context);
193 height = CGFloat(screenshot.frame_size.height());
195 CGContextTranslateCTM(context, 0.0, height);
196 CGContextScaleCTM(context, 1.0, -1.0);
197 CGContextDrawImage(context, frame_rect, image);
198 CGContextRestoreGState(context);
201 - (BOOL)isAccessibilityElement {
210 [
self.delegate flutterViewAccessibilityDidCall];
224 - (NSArray<id<UIFocusItem>>*)focusItemsInRect:(CGRect)rect {
225 NSObject* rootAccessibilityElement =
226 [
self.accessibilityElements count] > 0 ?
self.accessibilityElements[0] : nil;
228 ? @[ [rootAccessibilityElement accessibilityElementAtIndex:0] ]
232 - (NSArray<id<UIFocusEnvironment>>*)preferredFocusEnvironments {