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
6
7#import <CoreMedia/CoreMedia.h>
8#import <Metal/Metal.h>
9
11
12@interface FlutterSurface () {
13 CGSize _size;
15 id<MTLTexture> _texture;
16 // Used for testing.
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
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
id< FlutterTexture > _texture
fml::CFRef< IOSurfaceRef > _ioSurface
id< MTLTexture > _texture
VkSurfaceKHR surface
Definition main.cc:65
FlutterMetalTexture asFlutterMetalTexture()
FlTexture * texture
it will be possible to load the file into Perfetto s trace viewer use test Running tests that layout and measure text will not yield consistent results across various platforms Enabling this option will make font resolution default to the Ahem test font on all 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
int32_t height
int32_t width
size_t struct_size
The size of this struct. Must be sizeof(FlutterMetalTexture).
Definition embedder.h:855
int64_t texture_id
const uintptr_t id
int BOOL