| 1 |
/* |
| 2 |
* Copyright 2021 Google LLC |
| 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 |
#include "src/gpu/graphite/mtl/MtlResourceProvider.h" |
| 9 |
|
| 10 |
#include "include/gpu/ShaderErrorHandler.h" |
| 11 |
#include "include/gpu/graphite/BackendTexture.h" |
| 12 |
#include "include/gpu/graphite/mtl/MtlGraphiteTypes.h" |
| 13 |
#include "src/sksl/SkSLProgramKind.h" |
| 14 |
|
| 15 |
#include "src/core/SkSLTypeShared.h" |
| 16 |
#include "src/gpu/Blend.h" |
| 17 |
#include "src/gpu/Swizzle.h" |
| 18 |
#include "src/gpu/graphite/GlobalCache.h" |
| 19 |
#include "src/gpu/graphite/RenderPassDesc.h" |
| 20 |
#include "src/gpu/graphite/Renderer.h" |
| 21 |
#include "src/gpu/graphite/mtl/MtlBuffer.h" |
| 22 |
#include "src/gpu/graphite/mtl/MtlCommandBuffer.h" |
| 23 |
#include "src/gpu/graphite/mtl/MtlComputePipeline.h" |
| 24 |
#include "src/gpu/graphite/mtl/MtlGraphicsPipeline.h" |
| 25 |
#include "src/gpu/graphite/mtl/MtlSampler.h" |
| 26 |
#include "src/gpu/graphite/mtl/MtlSharedContext.h" |
| 27 |
#include "src/gpu/graphite/mtl/MtlTexture.h" |
| 28 |
#include "src/gpu/mtl/MtlUtilsPriv.h" |
| 29 |
|
| 30 |
#import <Metal/Metal.h> |
| 31 |
|
| 32 |
namespace skgpu::graphite { |
| 33 |
|
| 34 |
MtlResourceProvider::MtlResourceProvider(SharedContext* sharedContext, |
| 35 |
SingleOwner* singleOwner, |
| 36 |
uint32_t recorderID, |
| 37 |
size_t resourceBudget) |
| 38 |
: ResourceProvider(sharedContext, singleOwner, recorderID, resourceBudget) {} |
| 39 |
|
| 40 |
const MtlSharedContext* MtlResourceProvider::mtlSharedContext() { |
| 41 |
return static_cast<const MtlSharedContext*>(fSharedContext); |
| 42 |
} |
| 43 |
|
| 44 |
sk_sp<MtlGraphicsPipeline> MtlResourceProvider::findOrCreateLoadMSAAPipeline( |
| 45 |
const RenderPassDesc& renderPassDesc) { |
| 46 |
uint32_t renderPassKey = |
| 47 |
this->mtlSharedContext()->mtlCaps().getRenderPassDescKey(renderPassDesc); |
| 48 |
sk_sp<MtlGraphicsPipeline> pipeline = fLoadMSAAPipelines[renderPassKey]; |
| 49 |
if (!pipeline) { |
| 50 |
pipeline = MtlGraphicsPipeline::MakeLoadMSAAPipeline(this->mtlSharedContext(), |
| 51 |
renderPassDesc); |
| 52 |
if (pipeline) { |
| 53 |
fLoadMSAAPipelines.set(renderPassKey, pipeline); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
return pipeline; |
| 58 |
} |
| 59 |
|
| 60 |
sk_sp<ComputePipeline> MtlResourceProvider::createComputePipeline( |
| 61 |
const ComputePipelineDesc& pipelineDesc) { |
| 62 |
return MtlComputePipeline::Make(this->mtlSharedContext(), pipelineDesc); |
| 63 |
} |
| 64 |
|
| 65 |
sk_sp<Texture> MtlResourceProvider::createTexture(SkISize dimensions, |
| 66 |
const TextureInfo& info) { |
| 67 |
return MtlTexture::Make(this->mtlSharedContext(), dimensions, info); |
| 68 |
} |
| 69 |
|
| 70 |
sk_sp<Texture> MtlResourceProvider::onCreateWrappedTexture(const BackendTexture& texture) { |
| 71 |
CFTypeRef mtlHandleTexture = BackendTextures::GetMtlTexture(texture); |
| 72 |
if (!mtlHandleTexture) { |
| 73 |
return nullptr; |
| 74 |
} |
| 75 |
sk_cfp<id<MTLTexture>> mtlTexture = sk_ret_cfp((id<MTLTexture>)mtlHandleTexture); |
| 76 |
return MtlTexture::MakeWrapped(this->mtlSharedContext(), texture.dimensions(), texture.info(), |
| 77 |
std::move(mtlTexture)); |
| 78 |
} |
| 79 |
|
| 80 |
sk_sp<Buffer> MtlResourceProvider::createBuffer(size_t size, |
| 81 |
BufferType type, |
| 82 |
AccessPattern accessPattern) { |
| 83 |
return MtlBuffer::Make(this->mtlSharedContext(), size, type, accessPattern); |
| 84 |
} |
| 85 |
|
| 86 |
sk_sp<Sampler> MtlResourceProvider::createSampler(const SamplerDesc& samplerDesc) { |
| 87 |
return MtlSampler::Make(this->mtlSharedContext(), |
| 88 |
samplerDesc.samplingOptions(), |
| 89 |
samplerDesc.tileModeX(), |
| 90 |
samplerDesc.tileModeY()); |
| 91 |
} |
| 92 |
|
| 93 |
BackendTexture MtlResourceProvider::onCreateBackendTexture(SkISize dimensions, |
| 94 |
const TextureInfo& info) { |
| 95 |
sk_cfp<id<MTLTexture>> texture = MtlTexture::MakeMtlTexture(this->mtlSharedContext(), |
| 96 |
dimensions, |
| 97 |
info); |
| 98 |
if (!texture) { |
| 99 |
return {}; |
| 100 |
} |
| 101 |
return BackendTextures::MakeMetal(dimensions, (CFTypeRef)texture.release()); |
| 102 |
} |
| 103 |
|
| 104 |
void MtlResourceProvider::onDeleteBackendTexture(const BackendTexture& texture) { |
| 105 |
SkASSERT(texture.backend() == BackendApi::kMetal); |
| 106 |
CFTypeRef texHandle = BackendTextures::GetMtlTexture(texture); |
| 107 |
SkCFSafeRelease(texHandle); |
| 108 |
} |
| 109 |
|
| 110 |
MtlThreadSafeResourceProvider::MtlThreadSafeResourceProvider( |
| 111 |
std::unique_ptr<ResourceProvider> resourceProvider) |
| 112 |
: ThreadSafeResourceProvider(std::move(resourceProvider)) {} |
| 113 |
|
| 114 |
} // namespace skgpu::graphite |