7 #import <CoreMedia/CoreMedia.h>
8 #import <Metal/Metal.h>
10 #import "flutter/fml/platform/darwin/cf_utils.h"
34 return reinterpret_cast<int64_t
>(
_texture);
38 return _isInUseOverride || IOSurfaceIsInUse(_ioSurface);
46 return _isInUseOverride;
49 - (void)setIsInUseOverride:(BOOL)isInUseOverride {
53 - (instancetype)initWithSize:(CGSize)size
54 device:(
id<MTLDevice>)device
55 enableWideGamut:(BOOL)enableWideGamut {
56 if (
self = [super init]) {
58 self->_isWideGamut = enableWideGamut;
60 enableWideGamut:enableWideGamut]);
61 MTLPixelFormat pixelFormat =
62 enableWideGamut ? MTLPixelFormatBGRA10_XR : MTLPixelFormatBGRA8Unorm;
63 self->_texture = [
FlutterSurface createTextureForIOSurface:_ioSurface
66 pixelFormat:pixelFormat];
72 return FlutterMetalTexture{
73 .struct_size =
sizeof(FlutterMetalTexture),
78 .user_data = (__bridge_retained
void*)
self,
79 .destruction_callback =
88 + (
FlutterSurface*)fromFlutterMetalTexture:(const FlutterMetalTexture*)texture {
92 + (IOSurfaceRef)createIOSurfaceWithSize:(CGSize)size enableWideGamut:(BOOL)enableWideGamut {
94 unsigned bytesPerElement;
95 if (enableWideGamut) {
97 pixelFormat = kCVPixelFormatType_40ARGBLEWideGamut;
100 pixelFormat = kCVPixelFormatType_32BGRA;
104 size_t bytesPerRow = IOSurfaceAlignProperty(kIOSurfaceBytesPerRow,
size.width * bytesPerElement);
105 size_t totalBytes = IOSurfaceAlignProperty(kIOSurfaceAllocSize,
size.height * bytesPerRow);
106 NSDictionary* options = @{
107 (id)kIOSurfaceWidth : @(
size.width),
108 (id)kIOSurfaceHeight : @(
size.height),
109 (id)kIOSurfacePixelFormat : @(pixelFormat),
110 (id)kIOSurfaceBytesPerElement : @(bytesPerElement),
111 (id)kIOSurfaceBytesPerRow : @(bytesPerRow),
112 (id)kIOSurfaceAllocSize : @(totalBytes),
115 IOSurfaceRef res = IOSurfaceCreate((CFDictionaryRef)options);
116 if (enableWideGamut) {
117 IOSurfaceSetValue(res, kIOSurfaceColorSpace, kCGColorSpaceExtendedSRGB);
119 IOSurfaceSetValue(res, kIOSurfaceColorSpace, kCGColorSpaceSRGB);
124 + (id<MTLTexture>)createTextureForIOSurface:(IOSurfaceRef)surface
126 device:(
id<MTLDevice>)device
127 pixelFormat:(MTLPixelFormat)pixelFormat {
128 MTLTextureDescriptor* textureDescriptor =
129 [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:pixelFormat
133 textureDescriptor.usage =
134 MTLTextureUsageShaderRead | MTLTextureUsageRenderTarget | MTLTextureUsageShaderWrite;
136 return [device newTextureWithDescriptor:textureDescriptor iosurface:surface plane:0];
id< FlutterTexture > _texture
fml::CFRef< IOSurfaceRef > _ioSurface
id< MTLTexture > _texture
FlutterMetalTexture asFlutterMetalTexture()