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