Flutter Engine
The Flutter Engine
Namespaces | Enumerations | Functions
GrMtlUtil.h File Reference
#import <Metal/Metal.h>
#include "include/gpu/GrBackendSurface.h"
#include "include/gpu/GrContextOptions.h"
#include "include/gpu/ganesh/mtl/GrMtlBackendSurface.h"
#include "include/private/gpu/ganesh/GrTypesPriv.h"
#include "src/gpu/ganesh/mtl/GrMtlTypesPriv.h"
#include "src/sksl/ir/SkSLProgram.h"

Go to the source code of this file.

Namespaces

namespace  SkSL
 

Enumerations

enum class  GrMtlErrorCode { kTimeout = 1 }
 

Functions

GR_NORETAIN_BEGIN SK_ALWAYS_INLINE id< MTLTexture > GrGetMTLTexture (const void *mtlTexture)
 
SK_ALWAYS_INLINE const void * GrGetPtrFromId (id idObject)
 
SK_ALWAYS_INLINE CF_RETURNS_RETAINED const void * GrRetainPtrFromId (id idObject)
 
NSError * GrCreateMtlError (NSString *description, GrMtlErrorCode errorCode)
 
MTLTextureDescriptor * GrGetMTLTextureDescriptor (id< MTLTexture > mtlTexture)
 
id< MTLLibrary > GrCompileMtlShaderLibrary (const GrMtlGpu *gpu, const std::string &msl, GrContextOptions::ShaderErrorHandler *errorHandler)
 
void GrPrecompileMtlShaderLibrary (const GrMtlGpu *gpu, const std::string &msl)
 
id< MTLLibrary > GrMtlNewLibraryWithSource (id< MTLDevice >, NSString *mslCode, MTLCompileOptions *, NSError **)
 
id< MTLRenderPipelineState > GrMtlNewRenderPipelineStateWithDescriptor (id< MTLDevice >, MTLRenderPipelineDescriptor *, NSError **)
 
id< MTLTexture > GrGetMTLTextureFromSurface (GrSurface *surface)
 
static MTLPixelFormat GrBackendFormatAsMTLPixelFormat (const GrBackendFormat &format)
 
SkTextureCompressionType GrMtlFormatToCompressionType (MTLPixelFormat)
 
int GrMtlFormatStencilBits (MTLPixelFormat)
 
GrColorFormatDesc GrMtlFormatDesc (MTLPixelFormat)
 

Enumeration Type Documentation

◆ GrMtlErrorCode

enum class GrMtlErrorCode
strong
Enumerator
kTimeout 

Definition at line 63 of file GrMtlUtil.h.

63 {
64 kTimeout = 1,
65};

Function Documentation

◆ GrBackendFormatAsMTLPixelFormat()

static MTLPixelFormat GrBackendFormatAsMTLPixelFormat ( const GrBackendFormat format)
inlinestatic

Definition at line 106 of file GrMtlUtil.h.

106 {
107 return static_cast<MTLPixelFormat>(GrBackendFormats::AsMtlFormat(format));
108}
uint32_t uint32_t * format
SK_API GrMTLPixelFormat AsMtlFormat(const GrBackendFormat &)

◆ GrCompileMtlShaderLibrary()

id< MTLLibrary > GrCompileMtlShaderLibrary ( const GrMtlGpu gpu,
const std::string &  msl,
GrContextOptions::ShaderErrorHandler errorHandler 
)

Returns a compiled MTLLibrary created from MSL code

Definition at line 52 of file GrMtlUtil.mm.

54 {
55 TRACE_EVENT0("skia.shaders", "driver_compile_shader");
56 NSString* nsSource = [[NSString alloc] initWithBytesNoCopy:const_cast<char*>(msl.c_str())
57 length:msl.size()
58 encoding:NSUTF8StringEncoding
59 freeWhenDone:NO];
60 if (!nsSource) {
61 return nil;
62 }
63 MTLCompileOptions* options = [[MTLCompileOptions alloc] init];
64 // array<> is supported in MSL 2.0 on MacOS 10.13+ and iOS 11+,
65 // and in MSL 1.2 on iOS 10+ (but not MacOS).
66 if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) {
67 options.languageVersion = MTLLanguageVersion2_0;
68#if defined(SK_BUILD_FOR_IOS)
69 } else if (@available(macOS 10.12, iOS 10.0, tvOS 10.0, *)) {
70 options.languageVersion = MTLLanguageVersion1_2;
71#endif
72 }
73 options.fastMathEnabled = YES;
74
75 NSError* error = nil;
76 id<MTLLibrary> compiledLibrary;
77 if (@available(macOS 10.15, *)) {
78 compiledLibrary = [gpu->device() newLibraryWithSource:(NSString* _Nonnull)nsSource
80 error:&error];
81 } else {
82 compiledLibrary = GrMtlNewLibraryWithSource(gpu->device(), (NSString* _Nonnull)nsSource,
83 options, &error);
84 }
85 if (!compiledLibrary) {
86 errorHandler->compileError(
87 msl.c_str(), error.debugDescription.UTF8String, /*shaderWasCached=*/false);
88 return nil;
89 }
90
91 return compiledLibrary;
92}
const char * options
id< MTLLibrary > GrMtlNewLibraryWithSource(id< MTLDevice > device, NSString *mslCode, MTLCompileOptions *options, NSError **error)
Definition: GrMtlUtil.mm:130
id< MTLDevice > device() const
Definition: GrMtlGpu.h:49
virtual void compileError(const char *shader, const char *errors)
const uint8_t uint32_t uint32_t GError ** error
size_t length
static bool init()
#define TRACE_EVENT0(category_group, name)
Definition: trace_event.h:131

◆ GrCreateMtlError()

NSError * GrCreateMtlError ( NSString *  description,
GrMtlErrorCode  errorCode 
)

Definition at line 28 of file GrMtlUtil.mm.

28 {
29 NSDictionary* userInfo = [NSDictionary dictionaryWithObject:description
30 forKey:NSLocalizedDescriptionKey];
31 return [NSError errorWithDomain:@"org.skia.ganesh"
32 code:(NSInteger)errorCode
33 userInfo:userInfo];
34}

◆ GrGetMTLTexture()

GR_NORETAIN_BEGIN SK_ALWAYS_INLINE id< MTLTexture > GrGetMTLTexture ( const void *  mtlTexture)

Returns a id<MTLTexture> to the MTLTexture pointed at by the const void*.

TODO: Remove this and the other bridging functions? It's better to cast on the calling side so ARC has more context, and they don't add much value.

Definition at line 36 of file GrMtlUtil.h.

36 {
37#if __has_feature(objc_arc)
38 return (__bridge id<MTLTexture>)mtlTexture;
39#else
40 return (id<MTLTexture>)mtlTexture;
41#endif
42}

◆ GrGetMTLTextureDescriptor()

MTLTextureDescriptor * GrGetMTLTextureDescriptor ( id< MTLTexture >  mtlTexture)

Returns a MTLTextureDescriptor which describes the MTLTexture. Useful when creating a duplicate MTLTexture without the same storage allocation.

Definition at line 36 of file GrMtlUtil.mm.

36 {
37 MTLTextureDescriptor* texDesc = [[MTLTextureDescriptor alloc] init];
38 texDesc.textureType = mtlTexture.textureType;
39 texDesc.pixelFormat = mtlTexture.pixelFormat;
40 texDesc.width = mtlTexture.width;
41 texDesc.height = mtlTexture.height;
42 texDesc.depth = mtlTexture.depth;
43 texDesc.mipmapLevelCount = mtlTexture.mipmapLevelCount;
44 texDesc.arrayLength = mtlTexture.arrayLength;
45 texDesc.sampleCount = mtlTexture.sampleCount;
46 if (@available(macOS 10.11, iOS 9.0, tvOS 9.0, *)) {
47 texDesc.usage = mtlTexture.usage;
48 }
49 return texDesc;
50}

◆ GrGetMTLTextureFromSurface()

id< MTLTexture > GrGetMTLTextureFromSurface ( GrSurface surface)

Returns a MTLTexture corresponding to the GrSurface.

Definition at line 201 of file GrMtlUtil.mm.

201 {
202 id<MTLTexture> mtlTexture = nil;
203
204 GrMtlRenderTarget* renderTarget = static_cast<GrMtlRenderTarget*>(surface->asRenderTarget());
206 if (renderTarget) {
207 // We should not be using this for multisampled rendertargets with a separate resolve
208 // texture.
209 if (renderTarget->resolveAttachment()) {
210 SkASSERT(renderTarget->numSamples() > 1);
211 SkASSERT(false);
212 return nil;
213 }
214 mtlTexture = renderTarget->colorMTLTexture();
215 } else {
216 texture = static_cast<GrMtlTexture*>(surface->asTexture());
217 if (texture) {
218 mtlTexture = texture->mtlTexture();
219 }
220 }
221 return mtlTexture;
222}
#define SkASSERT(cond)
Definition: SkAssert.h:116
id< MTLTexture > colorMTLTexture() const
GrMtlAttachment * resolveAttachment() const
int numSamples() const
VkSurfaceKHR surface
Definition: main.cc:49
FlTexture * texture

◆ GrGetPtrFromId()

SK_ALWAYS_INLINE const void * GrGetPtrFromId ( id  idObject)

Returns a const void* to whatever the id object is pointing to.

Definition at line 47 of file GrMtlUtil.h.

47 {
48#if __has_feature(objc_arc)
49 return (__bridge const void*)idObject;
50#else
51 return (const void*)idObject;
52#endif
53}

◆ GrMtlFormatDesc()

GrColorFormatDesc GrMtlFormatDesc ( MTLPixelFormat  mtlFormat)

Definition at line 233 of file GrMtlUtil.mm.

233 {
234 switch (mtlFormat) {
235 case MTLPixelFormatRGBA8Unorm:
237 case MTLPixelFormatR8Unorm:
239 case MTLPixelFormatA8Unorm:
241 case MTLPixelFormatBGRA8Unorm:
243 case MTLPixelFormatB5G6R5Unorm:
245 case MTLPixelFormatRGBA16Float:
247 case MTLPixelFormatR16Float:
249 case MTLPixelFormatRG8Unorm:
251 case MTLPixelFormatRGB10A2Unorm:
253 case MTLPixelFormatBGR10A2Unorm:
255 case MTLPixelFormatABGR4Unorm:
257 case MTLPixelFormatRGBA8Unorm_sRGB:
259 case MTLPixelFormatR16Unorm:
261 case MTLPixelFormatRG16Unorm:
263 case MTLPixelFormatRGBA16Unorm:
265 case MTLPixelFormatRG16Float:
267
268 // Compressed texture formats are not expected to have a description.
269 case MTLPixelFormatETC2_RGB8: return GrColorFormatDesc::MakeInvalid();
270#ifdef SK_BUILD_FOR_MAC
271 case MTLPixelFormatBC1_RGBA: return GrColorFormatDesc::MakeInvalid();
272#endif
273
274 // This type only describes color channels.
275 case MTLPixelFormatStencil8: return GrColorFormatDesc::MakeInvalid();
276
277 default:
279 }
280}
static constexpr GrColorFormatDesc MakeInvalid()
Definition: GrTypesPriv.h:756
static constexpr GrColorFormatDesc MakeRGBA(int rgba, GrColorTypeEncoding e)
Definition: GrTypesPriv.h:720
static constexpr GrColorFormatDesc MakeAlpha(int a, GrColorTypeEncoding e)
Definition: GrTypesPriv.h:736
static constexpr GrColorFormatDesc MakeRG(int rg, GrColorTypeEncoding e)
Definition: GrTypesPriv.h:744
static constexpr GrColorFormatDesc MakeRGB(int rgb, GrColorTypeEncoding e)
Definition: GrTypesPriv.h:728
static constexpr GrColorFormatDesc MakeR(int r, GrColorTypeEncoding e)
Definition: GrTypesPriv.h:740

◆ GrMtlFormatStencilBits()

int GrMtlFormatStencilBits ( MTLPixelFormat  format)

Definition at line 302 of file GrMtlUtil.mm.

302 {
303 switch (format) {
304 case MTLPixelFormatStencil8:
305 return 8;
306 default:
307 return 0;
308 }
309}

◆ GrMtlFormatToCompressionType()

SkTextureCompressionType GrMtlFormatToCompressionType ( MTLPixelFormat  format)

Maps a MTLPixelFormat into the CompressionType enum if applicable.

Definition at line 282 of file GrMtlUtil.mm.

282 {
283 switch (format) {
284 case MTLPixelFormatETC2_RGB8: return SkTextureCompressionType::kETC2_RGB8_UNORM;
285#ifdef SK_BUILD_FOR_MAC
286 case MTLPixelFormatBC1_RGBA: return SkTextureCompressionType::kBC1_RGBA8_UNORM;
287#endif
288 default: return SkTextureCompressionType::kNone;
289 }
290
292}
#define SkUNREACHABLE
Definition: SkAssert.h:135

◆ GrMtlNewLibraryWithSource()

id< MTLLibrary > GrMtlNewLibraryWithSource ( id< MTLDevice >  device,
NSString *  mslCode,
MTLCompileOptions *  options,
NSError **  error 
)

Replacement for newLibraryWithSource:options:error that has a timeout.

Definition at line 130 of file GrMtlUtil.mm.

131 {
132 dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
133 sk_sp<MtlCompileResult> compileResult(new MtlCompileResult);
134 // We have to increment the ref for the Obj-C block manually because it won't do it for us
135 compileResult->ref();
136 MTLNewLibraryCompletionHandler completionHandler =
137 ^(id<MTLLibrary> library, NSError* compileError) {
138 compileResult->set(library, compileError);
139 dispatch_semaphore_signal(semaphore);
140 compileResult->unref();
141 };
142
143 [device newLibraryWithSource: mslCode
145 completionHandler: completionHandler];
146
147 // Wait 1 second for the compiler
148 constexpr auto kTimeoutNS = 1000000000UL;
149 if (dispatch_semaphore_wait(semaphore, dispatch_time(DISPATCH_TIME_NOW, kTimeoutNS))) {
150 if (error) {
151 constexpr auto kTimeoutMS = kTimeoutNS/1000000UL;
152 NSString* description =
153 [NSString stringWithFormat:@"Compilation took longer than %lu ms",
154 kTimeoutMS];
156 }
157 return nil;
158 }
159
160 id<MTLLibrary> compiledLibrary;
161 std::tie(compiledLibrary, *error) = compileResult->get();
162
163 return compiledLibrary;
164}
GR_NORETAIN_BEGIN NSError * GrCreateMtlError(NSString *description, GrMtlErrorCode errorCode)
Definition: GrMtlUtil.mm:28
VkDevice device
Definition: main.cc:53

◆ GrMtlNewRenderPipelineStateWithDescriptor()

id< MTLRenderPipelineState > GrMtlNewRenderPipelineStateWithDescriptor ( id< MTLDevice >  device,
MTLRenderPipelineDescriptor *  pipelineDescriptor,
NSError **  error 
)

Replacement for newRenderPipelineStateWithDescriptor:error that has a timeout.

Definition at line 166 of file GrMtlUtil.mm.

167 {
168 dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
169 sk_sp<MtlCompileResult> compileResult(new MtlCompileResult);
170 // We have to increment the ref for the Obj-C block manually because it won't do it for us
171 compileResult->ref();
172 MTLNewRenderPipelineStateCompletionHandler completionHandler =
173 ^(id<MTLRenderPipelineState> state, NSError* compileError) {
174 compileResult->set(state, compileError);
175 dispatch_semaphore_signal(semaphore);
176 compileResult->unref();
177 };
178
179 [device newRenderPipelineStateWithDescriptor: pipelineDescriptor
180 completionHandler: completionHandler];
181
182 // Wait 1 second for pipeline creation
183 constexpr auto kTimeoutNS = 1000000000UL;
184 if (dispatch_semaphore_wait(semaphore, dispatch_time(DISPATCH_TIME_NOW, kTimeoutNS))) {
185 if (error) {
186 constexpr auto kTimeoutMS = kTimeoutNS/1000000UL;
187 NSString* description =
188 [NSString stringWithFormat:@"Pipeline creation took longer than %lu ms",
189 kTimeoutMS];
191 }
192 return nil;
193 }
194
195 id<MTLRenderPipelineState> pipelineState;
196 std::tie(pipelineState, *error) = compileResult->get();
197
198 return pipelineState;
199}
AtkStateType state

◆ GrPrecompileMtlShaderLibrary()

void GrPrecompileMtlShaderLibrary ( const GrMtlGpu gpu,
const std::string &  msl 
)

Attempts to compile an MSL shader asynchronously. We are not concerned about the result, which will be cached in the Apple shader cache.

Definition at line 94 of file GrMtlUtil.mm.

95 {
96 NSString* nsSource = [[NSString alloc] initWithBytesNoCopy:const_cast<char*>(msl.c_str())
97 length:msl.size()
98 encoding:NSUTF8StringEncoding
99 freeWhenDone:NO];
100 if (!nsSource) {
101 return;
102 }
103 // Do nothing after completion for now.
104 // TODO: cache the result somewhere so we can use it later.
105 MTLNewLibraryCompletionHandler completionHandler = ^(id<MTLLibrary> library, NSError* error) {};
106 [gpu->device() newLibraryWithSource:(NSString* _Nonnull)nsSource
107 options:nil
108 completionHandler:completionHandler];
109}

◆ GrRetainPtrFromId()

SK_ALWAYS_INLINE CF_RETURNS_RETAINED const void * GrRetainPtrFromId ( id  idObject)

Returns a const void* to whatever the id object is pointing to. Will call CFRetain on the object.

Definition at line 59 of file GrMtlUtil.h.

59 {
60 return CFBridgingRetain(idObject);
61}