| Flutter macOS Embedder |

FlutterSurface.mm
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterSurface.h"
6
7 #import <CoreMedia/CoreMedia.h>
8 #import <Metal/Metal.h>
9
10 #import "flutter/fml/platform/darwin/cf_utils.h"
11
12 @interface FlutterSurface () {
13 CGSize _size;
14 fml::CFRef<IOSurfaceRef> _ioSurface;
15 id<MTLTexture> _texture;
16 // Used for testing.
17 BOOL _isInUseOverride;
18 }
19 @end
20
21 @implementation FlutterSurface
22
23 - (IOSurfaceRef)ioSurface {
24 return _ioSurface;
25 }
26
27 - (CGSize)size {
28 return _size;
29 }
30
31 - (int64_t)textureId {
32 return reinterpret_cast<int64_t>(_texture);
33 }
34
35 - (BOOL)isInUse {
36 return _isInUseOverride || IOSurfaceIsInUse(_ioSurface);
37 }
38
39 - (BOOL)isInUseOverride {
40 return _isInUseOverride;
41 }
42
43 - (void)setIsInUseOverride:(BOOL)isInUseOverride {
44 _isInUseOverride = isInUseOverride;
45 }
46
47 - (instancetype)initWithSize:(CGSize)size device:(id<MTLDevice>)device {
48 if (self = [super init]) {
49 self->_size = size;
50 self->_ioSurface.Reset([FlutterSurface createIOSurfaceWithSize:size]);
51 self->_texture = [FlutterSurface createTextureForIOSurface:_ioSurface size:size device:device];
52 }
53 return self;
54 }
55
56 - (FlutterMetalTexture)asFlutterMetalTexture {
57 return FlutterMetalTexture{
58 .struct_size = sizeof(FlutterMetalTexture),
59 .texture_id = self.textureId,
60 .texture = (__bridge void*)_texture,
61 // Retain for use in [FlutterSurface fromFlutterMetalTexture]. Released in
62 // destruction_callback.
63 .user_data = (__bridge_retained void*)self,
64 .destruction_callback =
65 [](void* user_data) {
66 // Balancing release for the retain when setting user_data above.
67 FlutterSurface* surface = (__bridge_transfer FlutterSurface*)user_data;
68 surface = nil;
69 },
70 };
71 }
72
73 + (FlutterSurface*)fromFlutterMetalTexture:(const FlutterMetalTexture*)texture {
74 return (__bridge FlutterSurface*)texture->user_data;
75 }
76
77 + (IOSurfaceRef)createIOSurfaceWithSize:(CGSize)size {
78 unsigned pixelFormat = kCVPixelFormatType_32BGRA;
79 unsigned bytesPerElement = 4;
80
81 size_t bytesPerRow = IOSurfaceAlignProperty(kIOSurfaceBytesPerRow, size.width * bytesPerElement);
82 size_t totalBytes = IOSurfaceAlignProperty(kIOSurfaceAllocSize, size.height * bytesPerRow);
83 NSDictionary* options = @{
84 (id)kIOSurfaceWidth : @(size.width),
85 (id)kIOSurfaceHeight : @(size.height),
86 (id)kIOSurfacePixelFormat : @(pixelFormat),
87 (id)kIOSurfaceBytesPerElement : @(bytesPerElement),
88 (id)kIOSurfaceBytesPerRow : @(bytesPerRow),
89 (id)kIOSurfaceAllocSize : @(totalBytes),
90 };
91
92 IOSurfaceRef res = IOSurfaceCreate((CFDictionaryRef)options);
93 IOSurfaceSetValue(res, kIOSurfaceColorSpace, kCGColorSpaceSRGB);
94 return res;
95 }
96
97 + (id<MTLTexture>)createTextureForIOSurface:(IOSurfaceRef)surface
98 size:(CGSize)size
99 device:(id<MTLDevice>)device {
100 MTLTextureDescriptor* textureDescriptor =
101 [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatBGRA8Unorm
102 width:size.width
103 height:size.height
104 mipmapped:NO];
105 textureDescriptor.usage =
106 MTLTextureUsageShaderRead | MTLTextureUsageRenderTarget | MTLTextureUsageShaderWrite;
107 // plane = 0 for BGRA.
108 return [device newTextureWithDescriptor:textureDescriptor iosurface:surface plane:0];
109 }
110
111 @end
-[FlutterSurface asFlutterMetalTexture]
FlutterMetalTexture asFlutterMetalTexture()
Definition: FlutterSurface.mm:56
FlutterSurface(Private)::textureId
int64_t textureId
Definition: FlutterSurface.h:34
FlutterSurface()::_size
CGSize _size
Definition: FlutterSurface.mm:13
user_data
void * user_data
Definition: texture_registrar_unittests.cc:27
FlutterSurface(Private)::isInUse
BOOL isInUse
Definition: FlutterSurface.h:36
FlutterSurface(Private)::ioSurface
IOSurfaceRef ioSurface
Definition: FlutterSurface.h:32
FlutterSurface()::_ioSurface
fml::CFRef< IOSurfaceRef > _ioSurface
Definition: FlutterSurface.mm:14
FlutterSurface.h
_texture
id< FlutterTexture > _texture
Definition: FlutterExternalTexture.mm:14
FlutterSurface
Definition: FlutterSurface.h:16
FlutterSurface()::_isInUseOverride
BOOL _isInUseOverride
Definition: FlutterSurface.mm:17
FlutterSurface(Testing)::isInUseOverride
BOOL isInUseOverride
Definition: FlutterSurface.h:41
FlutterSurface(Private)::size
CGSize size
Definition: FlutterSurface.h:33
texture_id
int64_t texture_id
Definition: texture_registrar_unittests.cc:24
FlutterSurface()::_texture
id< MTLTexture > _texture
Definition: FlutterSurface.mm:15