| 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 |
#ifndef skgpu_graphite_MtlSharedContext_DEFINED |
| 9 |
#define skgpu_graphite_MtlSharedContext_DEFINED |
| 10 |
|
| 11 |
#include "src/gpu/graphite/SharedContext.h" |
| 12 |
|
| 13 |
#include "include/ports/SkCFObject.h" |
| 14 |
|
| 15 |
#include "include/gpu/graphite/mtl/MtlBackendContext.h" |
| 16 |
#include "src/gpu/graphite/ThreadSafeResourceProvider.h" |
| 17 |
#include "src/gpu/graphite/mtl/MtlCaps.h" |
| 18 |
|
| 19 |
#import <Metal/Metal.h> |
| 20 |
|
| 21 |
namespace skgpu { |
| 22 |
class MtlMemoryAllocator; |
| 23 |
} |
| 24 |
|
| 25 |
namespace skgpu::graphite { |
| 26 |
struct ContextOptions; |
| 27 |
class MtlGraphicsPipeline; |
| 28 |
|
| 29 |
class MtlThreadSafeResourceProvider final : public ThreadSafeResourceProvider { |
| 30 |
public: |
| 31 |
MtlThreadSafeResourceProvider(std::unique_ptr<ResourceProvider>); |
| 32 |
}; |
| 33 |
|
| 34 |
class MtlSharedContext final : public SharedContext { |
| 35 |
public: |
| 36 |
static sk_sp<SharedContext> Make(const MtlBackendContext&, const ContextOptions&); |
| 37 |
~MtlSharedContext() override; |
| 38 |
|
| 39 |
skgpu::MtlMemoryAllocator* memoryAllocator() const { return fMemoryAllocator.get(); } |
| 40 |
|
| 41 |
id<MTLDevice> device() const { return fDevice.get(); } |
| 42 |
|
| 43 |
const MtlCaps& mtlCaps() const { return static_cast<const MtlCaps&>(*this->caps()); } |
| 44 |
|
| 45 |
MtlThreadSafeResourceProvider* threadSafeResourceProvider() const; |
| 46 |
|
| 47 |
std::unique_ptr<ResourceProvider> makeResourceProvider(SingleOwner*, |
| 48 |
uint32_t recorderID, |
| 49 |
size_t resourceBudget) override; |
| 50 |
|
| 51 |
sk_cfp<id<MTLDepthStencilState>> getCompatibleDepthStencilState( |
| 52 |
const DepthStencilSettings&) const; |
| 53 |
|
| 54 |
|
| 55 |
private: |
| 56 |
|
| 57 |
MtlSharedContext(sk_cfp<id<MTLDevice>>, |
| 58 |
sk_sp<skgpu::MtlMemoryAllocator>, |
| 59 |
std::unique_ptr<const MtlCaps>, |
| 60 |
SkExecutor*, |
| 61 |
SkSpan<sk_sp<SkRuntimeEffect>> userDefinedKnownRuntimeEffects); |
| 62 |
|
| 63 |
void createCompatibleDepthStencilState(const DepthStencilSettings&); |
| 64 |
|
| 65 |
sk_sp<GraphicsPipeline> createGraphicsPipeline( |
| 66 |
const RuntimeEffectDictionary*, |
| 67 |
const UniqueKey&, |
| 68 |
const GraphicsPipelineDesc&, |
| 69 |
const RenderPassDesc&, |
| 70 |
SkEnumBitMask<PipelineCreationFlags>, |
| 71 |
uint32_t compilationID) override; |
| 72 |
|
| 73 |
sk_sp<skgpu::MtlMemoryAllocator> fMemoryAllocator; |
| 74 |
|
| 75 |
sk_cfp<id<MTLDevice>> fDevice; |
| 76 |
|
| 77 |
// In the current Graphite class structure 'fDepthStencilStates' would more appropriately |
| 78 |
// go in a new MtlGlobalCache class. However, GlobalCache may go away as a concept, in |
| 79 |
// which case, this would be a reasonable place for this cache. |
| 80 |
// TODO(robertphillips): Come up with a scheme to map from DepthStencilSettings to tightly |
| 81 |
// packed ints and switch this to be a std::array. |
| 82 |
skia_private::THashMap<DepthStencilSettings, sk_cfp<id<MTLDepthStencilState>>> |
| 83 |
fDepthStencilStates; |
| 84 |
}; |
| 85 |
|
| 86 |
} // namespace skgpu::graphite |
| 87 |
|
| 88 |
#endif // skgpu_graphite_MtlSharedContext_DEFINED |