| 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/codegen/SkSLNativeShader.h" |
| 19 |
#include "src/sksl/ir/SkSLProgram.h" |
| 20 |
|
| 21 |
class GrMtlGpu; |
| 22 |
class GrSurface; |
| 23 |
enum class SkTextureCompressionType; |
| 24 |
namespace SkSL { |
| 25 |
enum class ProgramKind : int8_t; |
| 26 |
struct ProgramSettings; |
| 27 |
} |
| 28 |
|
| 29 |
GR_NORETAIN_BEGIN |
| 30 |
|
| 31 |
/** |
| 32 |
* Returns a id<MTLTexture> to the MTLTexture pointed at by the const void*. |
| 33 |
* |
| 34 |
* TODO: Remove this and the other bridging functions? It's better to cast on the calling |
| 35 |
* side so ARC has more context, and they don't add much value. |
| 36 |
*/ |
| 37 |
SK_ALWAYS_INLINE id<MTLTexture> GrGetMTLTexture(const void* mtlTexture) { |
| 38 |
#if __has_feature(objc_arc) |
| 39 |
return (__bridge id<MTLTexture>)mtlTexture; |
| 40 |
#else |
| 41 |
return (id<MTLTexture>)mtlTexture; |
| 42 |
#endif |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Returns a const void* to whatever the id object is pointing to. |
| 47 |
*/ |
| 48 |
SK_ALWAYS_INLINE const void* GrGetPtrFromId(id idObject) { |
| 49 |
#if __has_feature(objc_arc) |
| 50 |
return (__bridge const void*)idObject; |
| 51 |
#else |
| 52 |
return (const void*)idObject; |
| 53 |
#endif |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Returns a const void* to whatever the id object is pointing to. |
| 58 |
* Will call CFRetain on the object. |
| 59 |
*/ |
| 60 |
SK_ALWAYS_INLINE CF_RETURNS_RETAINED const void* GrRetainPtrFromId(id idObject) { |
| 61 |
return CFBridgingRetain(idObject); |
| 62 |
} |
| 63 |
|
| 64 |
enum class GrMtlErrorCode { |
| 65 |
kTimeout = 1, |
| 66 |
}; |
| 67 |
|
| 68 |
NSError* GrCreateMtlError(NSString* description, GrMtlErrorCode errorCode); |
| 69 |
|
| 70 |
/** |
| 71 |
* Returns a MTLTextureDescriptor which describes the MTLTexture. Useful when creating a duplicate |
| 72 |
* MTLTexture without the same storage allocation. |
| 73 |
*/ |
| 74 |
MTLTextureDescriptor* GrGetMTLTextureDescriptor(id<MTLTexture> mtlTexture); |
| 75 |
|
| 76 |
/** |
| 77 |
* Returns a compiled MTLLibrary created from MSL code |
| 78 |
*/ |
| 79 |
id<MTLLibrary> GrCompileMtlShaderLibrary(const GrMtlGpu* gpu, |
| 80 |
const SkSL::NativeShader& msl, |
| 81 |
GrContextOptions::ShaderErrorHandler* errorHandler); |
| 82 |
|
| 83 |
/** |
| 84 |
* Attempts to compile an MSL shader asynchronously. We are not concerned about the result, which |
| 85 |
* will be cached in the Apple shader cache. |
| 86 |
*/ |
| 87 |
void GrPrecompileMtlShaderLibrary(const GrMtlGpu* gpu, const SkSL::NativeShader& 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 |
|
| 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 |