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