7 #include "flutter/fml/platform/darwin/cf_utils.h"
12 @property(nonatomic, weak) id<FlutterViewEngineDelegate> delegate;
16 BOOL _isWideGamutEnabled;
19 - (instancetype)init {
20 NSAssert(NO,
@"FlutterView must initWithDelegate");
25 NSAssert(NO,
@"FlutterView must initWithDelegate");
30 NSAssert(NO,
@"FlutterView must initWithDelegate");
35 if (@available(iOS 13.0, *)) {
36 return self.window.windowScene.screen;
38 return UIScreen.mainScreen;
41 - (MTLPixelFormat)pixelFormat {
42 if ([
self.layer isKindOfClass:NSClassFromString(
@"CAMetalLayer")]) {
45 #pragma clang diagnostic push
46 #pragma clang diagnostic ignored "-Wunguarded-availability-new"
47 CAMetalLayer* layer = (CAMetalLayer*)
self.layer;
48 return layer.pixelFormat;
50 return MTLPixelFormatBGRA8Unorm;
52 - (BOOL)isWideGamutSupported {
53 if (!
self.delegate.isUsingImpeller) {
57 FML_DCHECK(
self.screen);
62 return self.screen.traitCollection.displayGamut != UIDisplayGamutSRGB;
67 enableWideGamut:(BOOL)isWideGamutEnabled {
68 if (delegate == nil) {
69 NSLog(
@"FlutterView delegate was nil.");
73 self = [
super initWithFrame:CGRectNull];
77 _isWideGamutEnabled = isWideGamutEnabled;
78 self.layer.opaque = opaque;
84 self.backgroundColor = UIColor.clearColor;
90 static void PrintWideGamutWarningOnce() {
91 static BOOL did_print = NO;
95 FML_DLOG(WARNING) <<
"Rendering wide gamut colors is turned on but isn't "
96 "supported, downgrading the color gamut to sRGB.";
100 - (void)layoutSubviews {
101 if ([
self.layer isKindOfClass:NSClassFromString(
@"CAMetalLayer")]) {
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;
118 layer.pixelFormat = MTLPixelFormatBGRA10_XR;
119 }
else if (_isWideGamutEnabled && !isWideGamutSupported) {
120 PrintWideGamutWarningOnce();
124 [
super layoutSubviews];
129 + (BOOL)forceSoftwareRendering {
133 + (void)setForceSoftwareRendering:(BOOL)forceSoftwareRendering {
137 + (Class)layerClass {
142 - (void)drawLayer:(CALayer*)layer inContext:(CGContextRef)context {
143 TRACE_EVENT0(
"flutter",
"SnapshotFlutterView");
145 if (layer !=
self.layer || context ==
nullptr) {
149 auto screenshot = [_delegate takeScreenshot:flutter::Rasterizer::ScreenshotType::UncompressedImage
152 if (!screenshot.data || screenshot.data->isEmpty() || screenshot.frame_size.isEmpty()) {
156 NSData* data = [NSData dataWithBytes:const_cast<void*>(screenshot.data->data())
157 length:screenshot.data->size()];
159 fml::CFRef<CGDataProviderRef> image_data_provider(
160 CGDataProviderCreateWithCFData(
reinterpret_cast<CFDataRef
>(data)));
162 fml::CFRef<CGColorSpaceRef> colorspace(CGColorSpaceCreateDeviceRGB());
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));
172 switch (screenshot.pixel_format) {
173 case flutter::Rasterizer::ScreenshotFormat::kUnknown:
174 case flutter::Rasterizer::ScreenshotFormat::kR8G8B8A8UNormInt:
177 case flutter::Rasterizer::ScreenshotFormat::kB8G8R8A8UNormInt:
180 static_cast<CGBitmapInfo
>(
static_cast<uint32_t
>(kCGImageAlphaPremultipliedFirst) |
181 static_cast<uint32_t
>(kCGBitmapByteOrder32Little));
183 case flutter::Rasterizer::ScreenshotFormat::kR16G16B16A16Float:
184 bits_per_component = 16u;
185 bits_per_pixel = 64u;
186 bytes_per_row_multiplier = 8u;
188 static_cast<CGBitmapInfo
>(
static_cast<uint32_t
>(kCGImageAlphaPremultipliedLast) |
189 static_cast<uint32_t
>(kCGBitmapFloatComponents) |
190 static_cast<uint32_t
>(kCGBitmapByteOrder16Little));
194 fml::CFRef<CGImageRef> image(CGImageCreate(
195 screenshot.frame_size.width(),
196 screenshot.frame_size.height(),
199 bytes_per_row_multiplier * screenshot.frame_size.width(),
205 kCGRenderingIntentDefault
208 const CGRect frame_rect =
209 CGRectMake(0.0, 0.0, screenshot.frame_size.width(), screenshot.frame_size.height());
210 CGContextSaveGState(context);
212 CGFloat height = CGBitmapContextGetHeight(context);
214 height = CGFloat(screenshot.frame_size.height());
216 CGContextTranslateCTM(context, 0.0, height);
217 CGContextScaleCTM(context, 1.0, -1.0);
218 CGContextDrawImage(context, frame_rect, image);
219 CGContextRestoreGState(context);
222 - (BOOL)isAccessibilityElement {
231 [
self.delegate flutterViewAccessibilityDidCall];