7 #include <IOSurface/IOSurfaceObjC.h>
8 #include <Metal/Metal.h>
9 #include <UIKit/UIKit.h>
11 #include "flutter/fml/logging.h"
64 @property(readonly, nonatomic) id<MTLTexture> texture;
65 @property(readonly, nonatomic) IOSurface* surface;
66 @property(readwrite, nonatomic) CFTimeInterval presentedTime;
67 @property(readwrite, atomic) BOOL waitingForCompletion;
78 - (instancetype)initWithTexture:(
id<MTLTexture>)texture surface:(IOSurface*)surface {
79 if (
self = [super init]) {
97 drawableId:(NSUInteger)drawableId;
105 drawableId:(NSUInteger)drawableId {
106 if (
self = [super init]) {
114 - (id<MTLTexture>)texture {
118 #pragma clang diagnostic push
119 #pragma clang diagnostic ignored "-Wunguarded-availability-new"
120 - (CAMetalLayer*)layer {
121 return (
id)
self->_layer;
123 #pragma clang diagnostic pop
125 - (NSUInteger)drawableID {
126 return self->_drawableId;
129 - (CFTimeInterval)presentedTime {
134 [_layer presentTexture:self->_texture];
135 self->_presented = YES;
140 [_layer returnTexture:self->_texture];
144 - (void)addPresentedHandler:(nonnull MTLDrawablePresentedHandler)block {
145 FML_LOG(WARNING) <<
"FlutterMetalLayer drawable does not implement addPresentedHandler:";
148 - (void)presentAtTime:(CFTimeInterval)presentationTime {
149 FML_LOG(WARNING) <<
"FlutterMetalLayer drawable does not implement presentAtTime:";
152 - (void)presentAfterMinimumDuration:(CFTimeInterval)duration {
153 FML_LOG(WARNING) <<
"FlutterMetalLayer drawable does not implement presentAfterMinimumDuration:";
156 - (void)flutterPrepareForPresent:(nonnull
id<MTLCommandBuffer>)commandBuffer {
159 [commandBuffer addCompletedHandler:^(id<MTLCommandBuffer> buffer) {
160 texture.waitingForCompletion = NO;
169 @synthesize device = _device;
175 - (instancetype)init {
176 if (
self = [super init]) {
177 _preferredDevice = MTLCreateSystemDefaultDevice();
178 self.device =
self.preferredDevice;
179 self.pixelFormat = MTLPixelFormatBGRA8Unorm;
180 _availableTextures = [[NSMutableSet alloc] init];
182 _displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(onDisplayLink:)];
183 [
self setMaxRefreshRate:DisplayLinkManager.displayRefreshRate forceMax:NO];
184 [_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
185 [[NSNotificationCenter defaultCenter] addObserver:self
186 selector:@selector(didEnterBackground:)
187 name:UIApplicationDidEnterBackgroundNotification
193 - (void)setMaxRefreshRate:(
double)refreshRate forceMax:(BOOL)forceMax {
201 double maxFrameRate = fmax(refreshRate, 60);
202 double minFrameRate = fmax(maxFrameRate / 2, 60);
203 if (@available(iOS 15.0, *)) {
205 CAFrameRateRangeMake(forceMax ? maxFrameRate : minFrameRate, maxFrameRate, maxFrameRate);
211 - (void)onDisplayLink:(CADisplayLink*)link {
212 _didSetContentsDuringThisDisplayLinkPeriod = NO;
214 if (_displayLinkPauseCountdown == 3) {
216 if (_displayLinkForcedMaxRate) {
217 [
self setMaxRefreshRate:DisplayLinkManager.displayRefreshRate forceMax:NO];
218 _displayLinkForcedMaxRate = NO;
221 ++_displayLinkPauseCountdown;
225 - (BOOL)isKindOfClass:(Class)aClass {
226 #pragma clang diagnostic push
227 #pragma clang diagnostic ignored "-Wunguarded-availability-new"
229 if ([aClass isEqual:[CAMetalLayer
class]]) {
232 #pragma clang diagnostic pop
233 return [
super isKindOfClass:aClass];
236 - (void)setDrawableSize:(CGSize)drawableSize {
237 [_availableTextures removeAllObjects];
243 - (void)didEnterBackground:(
id)notification {
244 [_availableTextures removeAllObjects];
245 _totalTextures = _front != nil ? 1 : 0;
250 return _drawableSize;
253 - (IOSurface*)createIOSurface {
255 unsigned bytesPerElement;
256 if (
self.
pixelFormat == MTLPixelFormatRGBA16Float) {
259 }
else if (
self.
pixelFormat == MTLPixelFormatBGRA8Unorm) {
262 }
else if (
self.
pixelFormat == MTLPixelFormatBGRA10_XR) {
263 pixelFormat = kCVPixelFormatType_40ARGBLEWideGamut;
266 FML_LOG(ERROR) <<
"Unsupported pixel format: " <<
self.pixelFormat;
270 IOSurfaceAlignProperty(kIOSurfaceBytesPerRow, _drawableSize.width * bytesPerElement);
272 IOSurfaceAlignProperty(kIOSurfaceAllocSize, _drawableSize.height * bytesPerRow);
273 NSDictionary* options = @{
274 (id)kIOSurfaceWidth : @(_drawableSize.width),
275 (id)kIOSurfaceHeight : @(_drawableSize.height),
277 (id)kIOSurfaceBytesPerElement : @(bytesPerElement),
278 (id)kIOSurfaceBytesPerRow : @(bytesPerRow),
279 (id)kIOSurfaceAllocSize : @(totalBytes),
282 IOSurfaceRef res = IOSurfaceCreate((CFDictionaryRef)options);
284 FML_LOG(ERROR) <<
"Failed to create IOSurface with options "
285 << options.debugDescription.UTF8String;
290 CFStringRef name = CGColorSpaceGetName(
self.
colorspace);
291 IOSurfaceSetValue(res, CFSTR(
"IOSurfaceColorSpace"), name);
293 IOSurfaceSetValue(res, CFSTR(
"IOSurfaceColorSpace"), kCGColorSpaceSRGB);
295 return (__bridge_transfer IOSurface*)res;
299 CFTimeInterval start = CACurrentMediaTime();
302 if (texture != nil) {
305 CFTimeInterval elapsed = CACurrentMediaTime() - start;
307 NSLog(
@"Waited %f seconds for a drawable, giving up.", elapsed);
314 @
synchronized(
self) {
315 if (_front != nil && _front.waitingForCompletion) {
318 if (_totalTextures < 3) {
320 IOSurface* surface = [
self createIOSurface];
321 if (surface == nil) {
324 MTLTextureDescriptor* textureDescriptor =
325 [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:_pixelFormat
326 width:_drawableSize.width
327 height:_drawableSize.height
330 if (_framebufferOnly) {
331 textureDescriptor.usage = MTLTextureUsageRenderTarget;
333 textureDescriptor.usage =
334 MTLTextureUsageRenderTarget | MTLTextureUsageShaderRead | MTLTextureUsageShaderWrite;
336 id<MTLTexture> texture = [
self.device newTextureWithDescriptor:textureDescriptor
337 iosurface:(__bridge IOSurfaceRef)surface
341 return flutterTexture;
365 [_availableTextures removeObject:res];
374 if (texture == nil) {
379 drawableId:_nextDrawableId++];
386 [
self setNeedsDisplay];
388 [CATransaction begin];
389 [CATransaction setDisableActions:YES];
390 self.contents = texture.
surface;
391 [CATransaction commit];
393 _displayLinkPauseCountdown = 0;
394 if (!_didSetContentsDuringThisDisplayLinkPeriod) {
395 _didSetContentsDuringThisDisplayLinkPeriod = YES;
396 }
else if (!_displayLinkForcedMaxRate) {
397 _displayLinkForcedMaxRate = YES;
398 [
self setMaxRefreshRate:DisplayLinkManager.displayRefreshRate forceMax:YES];
403 @
synchronized(
self) {
405 [_availableTextures addObject:_front];
409 if ([NSThread isMainThread]) {
410 [
self presentOnMainThread:texture];
413 dispatch_async(dispatch_get_main_queue(), ^{
414 [
self presentOnMainThread:texture];
421 @
synchronized(
self) {
422 [_availableTextures addObject:texture];
428 static BOOL didCheckInfoPlist = NO;
429 if (!didCheckInfoPlist) {
430 didCheckInfoPlist = YES;
431 NSNumber* use_flutter_metal_layer =
432 [[NSBundle mainBundle] objectForInfoDictionaryKey:@"FLTUseFlutterMetalLayer"];
433 if (use_flutter_metal_layer != nil && [use_flutter_metal_layer boolValue]) {
435 FML_LOG(WARNING) <<
"Using FlutterMetalLayer. This is an experimental feature.";