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 class GrMtlGpu;
22 class GrSurface;
23
24 #if defined(SK_BUILD_FOR_MAC)
25 #if __MAC_OS_X_VERSION_MAX_ALLOWED >= 101400
26 #define GR_METAL_SDK_VERSION 200
27 #else
28 #define GR_METAL_SDK_VERSION 100
29 #endif
30 #else
31 #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 120000 || __TV_OS_VERSION_MAX_ALLOWED >= 120000
32 #define GR_METAL_SDK_VERSION 200
33 #else
34 #define GR_METAL_SDK_VERSION 100
35 #endif
36 #endif
37
38 /**
39 * Returns the Metal texture format for the given GrPixelConfig
40 */
41 bool GrPixelConfigToMTLFormat(GrPixelConfig config, MTLPixelFormat* format);
42
43 /**
44 * Returns a id<MTLTexture> to the MTLTexture pointed at by the const void*.
45 */
GrGetMTLTexture(const void * mtlTexture)46 SK_ALWAYS_INLINE id<MTLTexture> GrGetMTLTexture(const void* mtlTexture) {
47 return (__bridge id<MTLTexture>)mtlTexture;
48 }
49
50 /**
51 * Returns a const void* to whatever the id object is pointing to.
52 */
GrGetPtrFromId(id idObject)53 SK_ALWAYS_INLINE const void* GrGetPtrFromId(id idObject) {
54 return (__bridge const void*)idObject;
55 }
56
57 /**
58 * Returns a const void* to whatever the id object is pointing to.
59 * Will call CFRetain on the object.
60 */
GrRetainPtrFromId(id idObject)61 SK_ALWAYS_INLINE const void* GrRetainPtrFromId(id idObject) {
62 return (__bridge_retained const void*)idObject;
63 }
64
65
66 /**
67 * Returns a MTLTextureDescriptor which describes the MTLTexture. Useful when creating a duplicate
68 * MTLTexture without the same storage allocation.
69 */
70 MTLTextureDescriptor* GrGetMTLTextureDescriptor(id<MTLTexture> mtlTexture);
71
72 /**
73 * Returns a compiled MTLLibrary created from MSL code generated by SkSLC
74 */
75 id<MTLLibrary> GrCompileMtlShaderLibrary(const GrMtlGpu* gpu,
76 const char* shaderString,
77 SkSL::Program::Kind kind,
78 const SkSL::Program::Settings& settings,
79 SkSL::Program::Inputs* outInputs);
80
81 /**
82 * Replacement for newLibraryWithSource:options:error that has a timeout.
83 */
84 id<MTLLibrary> GrMtlNewLibraryWithSource(id<MTLDevice>, NSString* mslCode,
85 MTLCompileOptions*, bool* timedout);
86
87 /**
88 * Replacement for newRenderPipelineStateWithDescriptor:error that has a timeout.
89 */
90 id<MTLRenderPipelineState> GrMtlNewRenderPipelineStateWithDescriptor(
91 id<MTLDevice>, MTLRenderPipelineDescriptor*, bool* timedout);
92
93 /**
94 * Returns a MTLTexture corresponding to the GrSurface.
95 */
96 id<MTLTexture> GrGetMTLTextureFromSurface(GrSurface* surface);
97
98 size_t GrMtlBytesPerFormat(MTLPixelFormat);
99
GrBackendFormatAsMTLPixelFormat(const GrBackendFormat & format)100 static inline MTLPixelFormat GrBackendFormatAsMTLPixelFormat(const GrBackendFormat& format) {
101 return static_cast<MTLPixelFormat>(format.asMtlFormat());
102 }
103
104 #endif
105