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 the Metal texture format for the given GrPixelConfig
36 */
37 bool GrPixelConfigToMTLFormat(GrPixelConfig config, MTLPixelFormat* format);
38
39 /**
40 * Returns a id<MTLTexture> to the MTLTexture pointed at by the const void*.
41 */
GrGetMTLTexture(const void * mtlTexture)42 SK_ALWAYS_INLINE id<MTLTexture> GrGetMTLTexture(const void* mtlTexture) {
43 return (__bridge id<MTLTexture>)mtlTexture;
44 }
45
46 /**
47 * Returns a const void* to whatever the id object is pointing to.
48 */
GrGetPtrFromId(id idObject)49 SK_ALWAYS_INLINE const void* GrGetPtrFromId(id idObject) {
50 return (__bridge const void*)idObject;
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 const void* GrRetainPtrFromId(id idObject) {
58 return (__bridge_retained const void*)idObject;
59 }
60
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> GrCompileMtlShaderLibrary(const GrMtlGpu* gpu,
72 const char* shaderString,
73 SkSL::Program::Kind kind,
74 const SkSL::Program::Settings& settings,
75 SkSL::Program::Inputs* outInputs);
76
77 /**
78 * Replacement for newLibraryWithSource:options:error that has a timeout.
79 */
80 id<MTLLibrary> GrMtlNewLibraryWithSource(id<MTLDevice>, NSString* mslCode,
81 MTLCompileOptions*, bool* timedout);
82
83 /**
84 * Replacement for newRenderPipelineStateWithDescriptor:error that has a timeout.
85 */
86 id<MTLRenderPipelineState> GrMtlNewRenderPipelineStateWithDescriptor(
87 id<MTLDevice>, MTLRenderPipelineDescriptor*, bool* timedout);
88
89 /**
90 * Returns a MTLTexture corresponding to the GrSurface.
91 */
92 id<MTLTexture> GrGetMTLTextureFromSurface(GrSurface* surface);
93
GrBackendFormatAsMTLPixelFormat(const GrBackendFormat & format)94 static inline MTLPixelFormat GrBackendFormatAsMTLPixelFormat(const GrBackendFormat& format) {
95 return static_cast<MTLPixelFormat>(format.asMtlFormat());
96 }
97
98 /**
99 * Returns true if the format is compressed.
100 */
101 bool GrMtlFormatIsCompressed(MTLPixelFormat mtlFormat);
102
103 /**
104 * Maps a MTLPixelFormat into the CompressionType enum if applicable.
105 */
106 bool GrMtlFormatToCompressionType(MTLPixelFormat mtlFormat,
107 SkImage::CompressionType* compressionType);
108 #endif
109