5#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterMetalLayer.h"
7#include <IOSurface/IOSurfaceObjC.h>
8#include <Metal/Metal.h>
9#include <UIKit/UIKit.h>
11#include "flutter/fml/logging.h"
12#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.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 {
97 drawableId:(NSUInteger)drawableId;
105 drawableId:(NSUInteger)drawableId {
115 return self->_texture.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
126 return self->_drawableId;
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 {
158 texture.waitingForCompletion = YES;
159 [commandBuffer addCompletedHandler:^(id<MTLCommandBuffer> buffer) {
160 texture.waitingForCompletion = NO;
169@synthesize device = _device;
175- (instancetype)
init {
179 self.pixelFormat = MTLPixelFormatBGRA8Unorm;
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 {
217 [
self setMaxRefreshRate:DisplayLinkManager.displayRefreshRate forceMax:NO];
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];
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;
270 IOSurfaceAlignProperty(kIOSurfaceBytesPerRow,
_drawableSize.width * bytesPerElement);
272 IOSurfaceAlignProperty(kIOSurfaceAllocSize,
_drawableSize.height * bytesPerRow);
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;
289 if (
self.colorspace != nil) {
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();
305 CFTimeInterval elapsed = CACurrentMediaTime() -
start;
307 NSLog(
@"Waited %f seconds for a drawable, giving up.", elapsed);
314 @
synchronized(
self) {
320 IOSurface*
surface = [
self createIOSurface];
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];
379 drawableId:_nextDrawableId++];
386 [
self setNeedsDisplay];
388 [CATransaction begin];
389 [CATransaction setDisableActions:YES];
391 [CATransaction commit];
398 [
self setMaxRefreshRate:DisplayLinkManager.displayRefreshRate forceMax:YES];
403 @
synchronized(
self) {
405 [_availableTextures addObject:_front];
408 texture.presentedTime = CACurrentMediaTime();
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.";
id< FlutterTexture > _texture
#define FML_LOG(severity)
BOOL maxRefreshRateEnabledOnIPhone
double displayRefreshRate
The display refresh rate used for reporting purposes. The engine does not care about this for frame s...
FlutterTexture * _texture
CFTimeInterval presentedTime()
__weak FlutterMetalLayer * _layer
CFTimeInterval _presentedTime
id< MTLTexture > _texture
BOOL waitingForCompletion
CFTimeInterval presentedTime
DEF_SWITCHES_START aot vmservice shared library name
CADisplayLink * _displayLink