Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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.
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
37 return _isInUseOverride;
38}
39
40- (void)setIsInUseOverride:(BOOL)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
53static void ReleaseSurface(void* surface) {
54 if (surface != nullptr) {
55 CFBridgingRelease(surface);
56 }
57}
58
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);
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
const char * options
id< FlutterTexture > _texture
VkSurfaceKHR surface
Definition main.cc:49
static void ReleaseSurface(void *surface)
FlutterMetalTexture asFlutterMetalTexture()
IOSurfaceRef ioSurface()
IOSurfaceRef createIOSurfaceWithSize:(CGSize size)
IOSurfaceRef _ioSurface
id< MTLTexture > _texture
id< MTLTexture > createTextureForIOSurface:size:device:(IOSurfaceRef surface, [size] CGSize size, [device] id< MTLDevice > device)
FlTexture * texture
int32_t height
int32_t width
FlutterMetalTextureHandle texture
Definition embedder.h:659
size_t struct_size
The size of this struct. Must be sizeof(FlutterMetalTexture).
Definition embedder.h:649
VoidCallback destruction_callback
Definition embedder.h:666
const uintptr_t id
int BOOL