• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef GrMtlUtil_DEFINED
9 #define GrMtlUtil_DEFINED
10 
11 #import <Metal/Metal.h>
12 
13 #include "include/gpu/GrBackendSurface.h"
14 #include "include/private/GrMtlTypesPriv.h"
15 #include "include/private/GrTypesPriv.h"
16 #include "src/sksl/ir/SkSLProgram.h"
17 
18 class GrMtlGpu;
19 class GrSurface;
20 
21 GR_NORETAIN_BEGIN
22 
23 /**
24  * Returns a id<MTLTexture> to the MTLTexture pointed at by the const void*.
25  *
26  * TODO: Remove this and the other bridging functions? It's better to cast on the calling
27  * side so ARC has more context, and they don't add much value.
28  */
GrGetMTLTexture(const void * mtlTexture)29 SK_ALWAYS_INLINE id<MTLTexture> GrGetMTLTexture(const void* mtlTexture) {
30 #if __has_feature(objc_arc)
31     return (__bridge id<MTLTexture>)mtlTexture;
32 #else
33     return (id<MTLTexture>)mtlTexture;
34 #endif
35 }
36 
37 /**
38  * Returns a const void* to whatever the id object is pointing to.
39  */
GrGetPtrFromId(id idObject)40 SK_ALWAYS_INLINE const void* GrGetPtrFromId(id idObject) {
41 #if __has_feature(objc_arc)
42     return (__bridge const void*)idObject;
43 #else
44     return (const void*)idObject;
45 #endif
46 }
47 
48 /**
49  * Returns a const void* to whatever the id object is pointing to.
50  * Will call CFRetain on the object.
51  */
GrRetainPtrFromId(id idObject)52 SK_ALWAYS_INLINE CF_RETURNS_RETAINED const void* GrRetainPtrFromId(id idObject) {
53     return CFBridgingRetain(idObject);
54 }
55 
56 enum class GrMtlErrorCode {
57     kTimeout = 1,
58 };
59 
60 NSError* GrCreateMtlError(NSString* description, GrMtlErrorCode errorCode);
61 
62 /**
63  * Returns a MTLTextureDescriptor which describes the MTLTexture. Useful when creating a duplicate
64  * MTLTexture without the same storage allocation.
65  */
66 MTLTextureDescriptor* GrGetMTLTextureDescriptor(id<MTLTexture> mtlTexture);
67 
68 /**
69  * Produces MSL code generated by SkSLC
70  */
71 bool GrSkSLToMSL(const GrMtlGpu* gpu,
72                  const SkSL::String& sksl,
73                  SkSL::ProgramKind kind,
74                  const SkSL::Program::Settings& settings,
75                  SkSL::String* msl,
76                  SkSL::Program::Inputs* outInputs,
77                  GrContextOptions::ShaderErrorHandler* errorHandler);
78 
79 /**
80  * Returns a compiled MTLLibrary created from MSL code
81  */
82 id<MTLLibrary> GrCompileMtlShaderLibrary(const GrMtlGpu* gpu,
83                                          const SkSL::String& msl,
84                                          GrContextOptions::ShaderErrorHandler* errorHandler);
85 
86 /**
87  * Attempts to compile an MSL shader asynchronously. We are not concerned about the result, which
88  * will be cached in the Apple shader cache.
89  */
90 void GrPrecompileMtlShaderLibrary(const GrMtlGpu* gpu,
91                                   const SkSL::String& msl);
92 
93 /**
94  * Replacement for newLibraryWithSource:options:error that has a timeout.
95  */
96 id<MTLLibrary> GrMtlNewLibraryWithSource(id<MTLDevice>, NSString* mslCode,
97                                          MTLCompileOptions*, NSError**);
98 
99 /**
100  * Replacement for newRenderPipelineStateWithDescriptor:error that has a timeout.
101  */
102 id<MTLRenderPipelineState> GrMtlNewRenderPipelineStateWithDescriptor(
103         id<MTLDevice>, MTLRenderPipelineDescriptor*, NSError**);
104 
105 /**
106  * Returns a MTLTexture corresponding to the GrSurface.
107  */
108 id<MTLTexture> GrGetMTLTextureFromSurface(GrSurface* surface);
109 
GrBackendFormatAsMTLPixelFormat(const GrBackendFormat & format)110 static inline MTLPixelFormat GrBackendFormatAsMTLPixelFormat(const GrBackendFormat& format) {
111     return static_cast<MTLPixelFormat>(format.asMtlFormat());
112 }
113 
114 /**
115  * Returns true if the format is compressed.
116  */
117 bool GrMtlFormatIsCompressed(MTLPixelFormat mtlFormat);
118 
119 /**
120  * Maps a MTLPixelFormat into the CompressionType enum if applicable.
121  */
122 SkImage::CompressionType GrMtlFormatToCompressionType(MTLPixelFormat mtlFormat);
123 
124 size_t GrMtlFormatBytesPerBlock(MTLPixelFormat);
125 
126 int GrMtlFormatStencilBits(MTLPixelFormat);
127 
128 #ifdef SK_BUILD_FOR_IOS
129 bool GrMtlIsAppInBackground();
130 #endif
131 
132 GR_NORETAIN_END
133 
134 #endif
135