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/GrTypesPriv.h"
15 #include "src/sksl/ir/SkSLProgram.h"
16
17 #if !__has_feature(objc_arc)
18 #error This file must be compiled with Arc. Use -fobjc-arc flag
19 #endif
20
21 #if defined(SK_BUILD_FOR_MAC)
22 #if __MAC_OS_X_VERSION_MAX_ALLOWED < 101400
23 #error Must use at least 10.14 SDK to build Metal backend for MacOS
24 #endif
25 #else
26 #if __IPHONE_OS_VERSION_MAX_ALLOWED < 120000 && __TV_OS_VERSION_MAX_ALLOWED < 120000
27 #error Must use at least 12.00 SDK to build Metal backend for iOS
28 #endif
29 #endif
30
31 class GrMtlGpu;
32 class GrSurface;
33
34 /**
35 * Returns a id<MTLTexture> to the MTLTexture pointed at by the const void*.
36 */
GrGetMTLTexture(const void * mtlTexture)37 SK_ALWAYS_INLINE id<MTLTexture> GrGetMTLTexture(const void* mtlTexture) {
38 return (__bridge id<MTLTexture>)mtlTexture;
39 }
40
41 /**
42 * Returns a const void* to whatever the id object is pointing to.
43 */
GrGetPtrFromId(id idObject)44 SK_ALWAYS_INLINE const void* GrGetPtrFromId(id idObject) {
45 return (__bridge const void*)idObject;
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 const void* GrRetainPtrFromId(id idObject) {
53 return (__bridge_retained const void*)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 * Returns a compiled MTLLibrary created from MSL code generated by SkSLC
70 */
71 id<MTLLibrary> GrGenerateMtlShaderLibrary(const GrMtlGpu* gpu,
72 const SkSL::String& shaderString,
73 SkSL::Program::Kind kind,
74 const SkSL::Program::Settings& settings,
75 SkSL::String* mslShader,
76 SkSL::Program::Inputs* outInputs);
77
78 /**
79 * Returns a compiled MTLLibrary created from MSL code
80 */
81 id<MTLLibrary> GrCompileMtlShaderLibrary(const GrMtlGpu* gpu,
82 const SkSL::String& shaderString);
83
84 /**
85 * Replacement for newLibraryWithSource:options:error that has a timeout.
86 */
87 id<MTLLibrary> GrMtlNewLibraryWithSource(id<MTLDevice>, NSString* mslCode,
88 MTLCompileOptions*, NSError**);
89
90 /**
91 * Replacement for newRenderPipelineStateWithDescriptor:error that has a timeout.
92 */
93 id<MTLRenderPipelineState> GrMtlNewRenderPipelineStateWithDescriptor(
94 id<MTLDevice>, MTLRenderPipelineDescriptor*, NSError**);
95
96 /**
97 * Returns a MTLTexture corresponding to the GrSurface.
98 */
99 id<MTLTexture> GrGetMTLTextureFromSurface(GrSurface* surface);
100
GrBackendFormatAsMTLPixelFormat(const GrBackendFormat & format)101 static inline MTLPixelFormat GrBackendFormatAsMTLPixelFormat(const GrBackendFormat& format) {
102 return static_cast<MTLPixelFormat>(format.asMtlFormat());
103 }
104
105 /**
106 * Returns true if the format is compressed.
107 */
108 bool GrMtlFormatIsCompressed(MTLPixelFormat mtlFormat);
109
110 /**
111 * Maps a MTLPixelFormat into the CompressionType enum if applicable.
112 */
113 SkImage::CompressionType GrMtlFormatToCompressionType(MTLPixelFormat mtlFormat);
114
115 #endif
116