| 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 <Metal/Metal.h>
8
9 @interface FlutterSurface () {
10 CGSize _size;
11 IOSurfaceRef _ioSurface;
12 id<MTLTexture> _texture;
13 // Used for testing.
14 BOOL _isInUseOverride;
15 }
16 @end
17
18 @implementation FlutterSurface
19
20 - (IOSurfaceRef)ioSurface {
21 return _ioSurface;
22 }
23
24 - (CGSize)size {
25 return _size;
26 }
27
28 - (int64_t)textureId {
29 return reinterpret_cast<int64_t>(_texture);
30 }
31
32 - (BOOL)isInUse {
33 return _isInUseOverride || IOSurfaceIsInUse(_ioSurface);
34 }
35
36 - (BOOL)isInUseOverride {
37 return _isInUseOverride;
38 }
39
40 - (void)setIsInUseOverride:(BOOL)isInUseOverride {
41 _isInUseOverride = isInUseOverride;
42 }
43
44 - (instancetype)initWithSize:(CGSize)size device:(id<MTLDevice>)device {
45 if (self = [super init]) {
46 self->_size = size;
47 self->_ioSurface = [FlutterSurface createIOSurfaceWithSize:size];
48 self->_texture = [FlutterSurface createTextureForIOSurface:_ioSurface size:size device:device];
49 }
50 return self;
51 }
52
53 static void ReleaseSurface(void* surface) {
54 if (surface != nullptr) {
55 CFBridgingRelease(surface);
56 }
57 }
58
59 - (FlutterMetalTexture)asFlutterMetalTexture {
60 FlutterMetalTexture res;
61 memset(&res, 0, sizeof(FlutterMetalTexture));
62 res.struct_size = sizeof(FlutterMetalTexture);
63 res.texture = (__bridge void*)_texture;
64 res.texture_id = self.textureId;
65 res.user_data = (void*)CFBridgingRetain(self);
66 res.destruction_callback = ReleaseSurface;
67 return res;
68 }
69
70 + (FlutterSurface*)fromFlutterMetalTexture:(const FlutterMetalTexture*)texture {
71 return (__bridge FlutterSurface*)texture->user_data;
72 }
73
74 - (void)dealloc {
75 CFRelease(_ioSurface);
76 }
77
78 + (IOSurfaceRef)createIOSurfaceWithSize:(CGSize)size {
79 unsigned pixelFormat = 'BGRA';
80 unsigned bytesPerElement = 4;
81
82 size_t bytesPerRow = IOSurfaceAlignProperty(kIOSurfaceBytesPerRow, size.width * bytesPerElement);
83 size_t totalBytes = IOSurfaceAlignProperty(kIOSurfaceAllocSize, size.height * bytesPerRow);
84 NSDictionary* options = @{
85 (id)kIOSurfaceWidth : @(size.width),
86 (id)kIOSurfaceHeight : @(size.height),
87 (id)kIOSurfacePixelFormat : @(pixelFormat),
88 (id)kIOSurfaceBytesPerElement : @(bytesPerElement),
89 (id)kIOSurfaceBytesPerRow : @(bytesPerRow),
90 (id)kIOSurfaceAllocSize : @(totalBytes),
91 };
92
93 IOSurfaceRef res = IOSurfaceCreate((CFDictionaryRef)options);
94 IOSurfaceSetValue(res, CFSTR("IOSurfaceColorSpace"), kCGColorSpaceSRGB);
95 return res;
96 }
97
98 + (id<MTLTexture>)createTextureForIOSurface:(IOSurfaceRef)surface
99 size:(CGSize)size
100 device:(id<MTLDevice>)device {
101 MTLTextureDescriptor* textureDescriptor =
102 [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatBGRA8Unorm
103 width:size.width
104 height:size.height
105 mipmapped:NO];
106 textureDescriptor.usage =
107 MTLTextureUsageShaderRead | MTLTextureUsageRenderTarget | MTLTextureUsageShaderWrite;
108 // plane = 0 for BGRA.
109 return [device newTextureWithDescriptor:textureDescriptor iosurface:surface plane:0];
110 }
111
112 @end
-[FlutterSurface asFlutterMetalTexture]
FlutterMetalTexture asFlutterMetalTexture()
Definition: FlutterSurface.mm:59
FlutterSurface(Private)::textureId
int64_t textureId
Definition: FlutterSurface.h:34
FlutterSurface()::_size
CGSize _size
Definition: FlutterSurface.mm:10
FlutterSurface(Private)::isInUse
BOOL isInUse
Definition: FlutterSurface.h:36
FlutterSurface(Private)::ioSurface
IOSurfaceRef ioSurface
Definition: FlutterSurface.h:32
FlutterSurface()::_ioSurface
IOSurfaceRef _ioSurface
Definition: FlutterSurface.mm:11
FlutterSurface.h
_texture
id< FlutterTexture > _texture
Definition: FlutterExternalTexture.mm:14
FlutterSurface
Definition: FlutterSurface.h:16
FlutterSurface()::_isInUseOverride
BOOL _isInUseOverride
Definition: FlutterSurface.mm:14
FlutterSurface(Testing)::isInUseOverride
BOOL isInUseOverride
Definition: FlutterSurface.h:41
FlutterSurface(Private)::size
CGSize size
Definition: FlutterSurface.h:33
FlutterSurface()::_texture
id< MTLTexture > _texture
Definition: FlutterSurface.mm:12