Flutter Engine
The Flutter Engine
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
VkSurfaceKHR surface
Definition: main.cc:49
static void ReleaseSurface(void *surface)
FlutterMetalTexture asFlutterMetalTexture()
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
static bool init()
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
FlutterMetalTextureHandle texture
Definition: embedder.h:661
size_t struct_size
The size of this struct. Must be sizeof(FlutterMetalTexture).
Definition: embedder.h:651
int64_t texture_id
Definition: embedder.h:656
VoidCallback destruction_callback
Definition: embedder.h:668
const uintptr_t id
int BOOL
Definition: windows_types.h:37