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 "experimental/graphite/src/ResourceProvider.h"
9
10 #include "experimental/graphite/src/Buffer.h"
11 #include "experimental/graphite/src/CommandBuffer.h"
12 #include "experimental/graphite/src/GraphicsPipeline.h"
13 #include "experimental/graphite/src/Texture.h"
14
15 namespace skgpu {
16
ResourceProvider(const Gpu * gpu)17 ResourceProvider::ResourceProvider(const Gpu* gpu) : fGpu(gpu) {
18 fGraphicsPipelineCache.reset(new GraphicsPipelineCache(this));
19 }
20
~ResourceProvider()21 ResourceProvider::~ResourceProvider() {
22 fGraphicsPipelineCache.release();
23 }
24
findOrCreateGraphicsPipeline(const GraphicsPipelineDesc & desc)25 sk_sp<GraphicsPipeline> ResourceProvider::findOrCreateGraphicsPipeline(
26 const GraphicsPipelineDesc& desc) {
27 return fGraphicsPipelineCache->refPipeline(desc);
28 }
29
30 ////////////////////////////////////////////////////////////////////////////////////////////////
31
32 struct ResourceProvider::GraphicsPipelineCache::Entry {
Entryskgpu::ResourceProvider::GraphicsPipelineCache::Entry33 Entry(sk_sp<GraphicsPipeline> pipeline) : fPipeline(std::move(pipeline)) {}
34
35 sk_sp<GraphicsPipeline> fPipeline;
36 };
37
GraphicsPipelineCache(ResourceProvider * resourceProvider)38 ResourceProvider::GraphicsPipelineCache::GraphicsPipelineCache(ResourceProvider* resourceProvider)
39 : fMap(16) // TODO: find a good value for this
40 , fResourceProvider(resourceProvider) {}
41
~GraphicsPipelineCache()42 ResourceProvider::GraphicsPipelineCache::~GraphicsPipelineCache() {
43 SkASSERT(0 == fMap.count());
44 }
45
release()46 void ResourceProvider::GraphicsPipelineCache::release() {
47 fMap.reset();
48 }
49
refPipeline(const GraphicsPipelineDesc & desc)50 sk_sp<GraphicsPipeline> ResourceProvider::GraphicsPipelineCache::refPipeline(
51 const GraphicsPipelineDesc& desc) {
52 std::unique_ptr<Entry>* entry = fMap.find(desc);
53
54 if (!entry) {
55 auto pipeline = fResourceProvider->onCreateGraphicsPipeline(desc);
56 if (!pipeline) {
57 return nullptr;
58 }
59 entry = fMap.insert(desc, std::unique_ptr<Entry>(new Entry(std::move(pipeline))));
60 }
61 return (*entry)->fPipeline;
62 }
63
findOrCreateTexture(SkISize dimensions,const TextureInfo & info)64 sk_sp<Texture> ResourceProvider::findOrCreateTexture(SkISize dimensions, const TextureInfo& info) {
65 return this->createTexture(dimensions, info);
66 }
67
findOrCreateBuffer(size_t size,BufferType type,PrioritizeGpuReads prioritizeGpuReads)68 sk_sp<Buffer> ResourceProvider::findOrCreateBuffer(size_t size,
69 BufferType type,
70 PrioritizeGpuReads prioritizeGpuReads) {
71 return this->createBuffer(size, type, prioritizeGpuReads);
72 }
73
74
75 } // namespace skgpu
76