7 #include <CoreMedia/CoreMedia.h>
8 #include <IOSurface/IOSurfaceObjC.h>
9 #include <Metal/Metal.h>
10 #include <UIKit/UIKit.h>
12 #import "flutter/shell/platform/darwin/common/InternalFlutterSwiftCommon/InternalFlutterSwiftCommon.h"
14 #import "flutter/shell/platform/darwin/ios/InternalFlutterSwift/InternalFlutterSwift.h"
40 @property(readonly, nonatomic) id<MTLTexture>
texture;
41 @property(readonly, nonatomic) IOSurface*
surface;
49 - (instancetype)initWithTexture:(
id<MTLTexture>)texture surface:(IOSurface*)surface {
50 if (
self = [super init]) {
68 drawableId:(NSUInteger)drawableId;
76 drawableId:(NSUInteger)drawableId {
77 if (
self = [super init]) {
86 return self->_texture.texture;
89 #pragma clang diagnostic push
90 #pragma clang diagnostic ignored "-Wunguarded-availability-new"
91 - (CAMetalLayer*)layer {
92 return (
id)
self->_layer;
94 #pragma clang diagnostic pop
96 - (NSUInteger)drawableID {
97 return self->_drawableId;
105 [_layer presentTexture:self->_texture];
106 self->_presented = YES;
111 [_layer returnTexture:self->_texture];
115 - (void)addPresentedHandler:(nonnull MTLDrawablePresentedHandler)block {
116 [FlutterLogger logWarning:@"FlutterMetalLayer drawable does not implement addPresentedHandler:"];
119 - (void)presentAtTime:(CFTimeInterval)presentationTime {
120 [FlutterLogger logWarning:@"FlutterMetalLayer drawable does not implement presentAtTime:"];
123 - (void)presentAfterMinimumDuration:(CFTimeInterval)duration {
125 logWarning:@"FlutterMetalLayer drawable does not implement presentAfterMinimumDuration:"];
128 - (void)flutterPrepareForPresent:(nonnull
id<MTLCommandBuffer>)commandBuffer {
130 texture.waitingForCompletion = YES;
131 [commandBuffer addCompletedHandler:^(id<MTLCommandBuffer> buffer) {
132 texture.waitingForCompletion = NO;
140 - (instancetype)init {
141 if (
self = [super init]) {
142 _preferredDevice = MTLCreateSystemDefaultDevice();
143 self.device =
self.preferredDevice;
144 self.pixelFormat = MTLPixelFormatBGRA8Unorm;
145 _availableTextures = [[NSMutableSet alloc] init];
147 [[NSNotificationCenter defaultCenter] addObserver:self
148 selector:@selector(didEnterBackground:)
149 name:UIApplicationDidEnterBackgroundNotification
156 [[NSNotificationCenter defaultCenter] removeObserver:self];
159 - (BOOL)isKindOfClass:(Class)aClass {
160 #pragma clang diagnostic push
161 #pragma clang diagnostic ignored "-Wunguarded-availability-new"
163 if ([aClass isEqual:[CAMetalLayer
class]]) {
166 #pragma clang diagnostic pop
167 return [
super isKindOfClass:aClass];
170 - (void)setDrawableSize:(CGSize)drawableSize {
171 @
synchronized(
self) {
172 [_availableTextures removeAllObjects];
179 - (void)didEnterBackground:(
id)notification {
180 @
synchronized(
self) {
181 [_availableTextures removeAllObjects];
182 _totalTextures = _front != nil ? 1 : 0;
187 @
synchronized(
self) {
188 return _drawableSize;
192 - (IOSurface*)createIOSurface {
194 unsigned bytesPerElement;
195 if (
self.
pixelFormat == MTLPixelFormatRGBA16Float) {
198 }
else if (
self.
pixelFormat == MTLPixelFormatBGRA8Unorm) {
201 }
else if (
self.
pixelFormat == MTLPixelFormatBGRA10_XR) {
202 pixelFormat = kCVPixelFormatType_40ARGBLEWideGamut;
205 NSString* errorMessage =
206 [NSString stringWithFormat:@"Unsupported pixel format: %lu", self.pixelFormat];
207 [FlutterLogger logError:errorMessage];
211 IOSurfaceAlignProperty(kIOSurfaceBytesPerRow, _drawableSize.width * bytesPerElement);
213 IOSurfaceAlignProperty(kIOSurfaceAllocSize, _drawableSize.height * bytesPerRow);
214 NSDictionary* options = @{
215 (id)kIOSurfaceWidth : @(_drawableSize.width),
216 (id)kIOSurfaceHeight : @(_drawableSize.height),
218 (id)kIOSurfaceBytesPerElement : @(bytesPerElement),
219 (id)kIOSurfaceBytesPerRow : @(bytesPerRow),
220 (id)kIOSurfaceAllocSize : @(totalBytes),
223 IOSurfaceRef res = IOSurfaceCreate((CFDictionaryRef)options);
225 NSString* errorMessage = [NSString
226 stringWithFormat:@"Failed to create IOSurface with options %@", options.debugDescription];
227 [FlutterLogger logError:errorMessage];
232 CFStringRef name = CGColorSpaceGetName(
self.
colorspace);
233 IOSurfaceSetValue(res, kIOSurfaceColorSpace, name);
235 IOSurfaceSetValue(res, kIOSurfaceColorSpace, kCGColorSpaceSRGB);
237 return (__bridge_transfer IOSurface*)res;
241 CFTimeInterval start = CACurrentMediaTime();
247 CFTimeInterval elapsed = CACurrentMediaTime() - start;
249 NSLog(
@"Waited %f seconds for a drawable, giving up.", elapsed);
256 @
synchronized(
self) {
257 if (_front != nil && _front.waitingForCompletion) {
260 if (_totalTextures < 3) {
262 IOSurface*
surface = [
self createIOSurface];
266 MTLTextureDescriptor* textureDescriptor =
267 [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:_pixelFormat
268 width:_drawableSize.width
269 height:_drawableSize.height
272 if (_framebufferOnly) {
273 textureDescriptor.usage = MTLTextureUsageRenderTarget;
275 textureDescriptor.usage =
276 MTLTextureUsageRenderTarget | MTLTextureUsageShaderRead | MTLTextureUsageShaderWrite;
278 id<MTLTexture>
texture = [
self.device newTextureWithDescriptor:textureDescriptor
279 iosurface:(__bridge IOSurfaceRef)surface
283 return flutterTexture;
307 [_availableTextures removeObject:res];
321 drawableId:_nextDrawableId++];
326 if (
texture.texture.width != _drawableSize.width ||
327 texture.texture.height != _drawableSize.height) {
336 [
self setNeedsDisplay];
338 [CATransaction begin];
339 [CATransaction setDisableActions:YES];
340 self.contents =
texture.surface;
341 [CATransaction commit];
345 @
synchronized(
self) {
346 if (
texture.texture.width != _drawableSize.width ||
347 texture.texture.height != _drawableSize.height) {
351 [_availableTextures addObject:_front];
354 texture.presentedTime = CACurrentMediaTime();
355 if ([NSThread isMainThread]) {
356 [
self presentOnMainThread:texture];
359 dispatch_async(dispatch_get_main_queue(), ^{
360 [
self presentOnMainThread:texture];
370 @
synchronized(
self) {
371 if (
texture.texture.width == _drawableSize.width &&
372 texture.texture.height == _drawableSize.height) {
373 [_availableTextures addObject:texture];
380 static BOOL didCheckInfoPlist = NO;
381 if (!didCheckInfoPlist) {
382 didCheckInfoPlist = YES;
383 NSNumber* use_flutter_metal_layer =
384 [[NSBundle mainBundle] objectForInfoDictionaryKey:@"FLTUseFlutterMetalLayer"];
385 if (use_flutter_metal_layer != nil && ![use_flutter_metal_layer boolValue]) {
FlutterTexture * _texture
__weak FlutterMetalLayer * _layer
BOOL waitingForCompletion
CFTimeInterval presentedTime