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