1 /*
2 * Copyright 2022 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/GlobalCache.h"
9
10 #include "src/gpu/graphite/ComputePipeline.h"
11 #include "src/gpu/graphite/ContextUtils.h"
12 #include "src/gpu/graphite/GraphicsPipeline.h"
13 #include "src/gpu/graphite/Resource.h"
14
15 namespace skgpu::graphite {
16
GlobalCache()17 GlobalCache::GlobalCache()
18 : fGraphicsPipelineCache(256) // TODO: find a good value for these limits
19 , fComputePipelineCache(256) {}
20
~GlobalCache()21 GlobalCache::~GlobalCache() {
22 // These should have been cleared out earlier by deleteResources().
23 SkDEBUGCODE(SkAutoSpinlock lock{ fSpinLock });
24 SkASSERT(fGraphicsPipelineCache.count() == 0);
25 SkASSERT(fComputePipelineCache.count() == 0);
26 SkASSERT(fStaticResource.size() == 0);
27 }
28
deleteResources()29 void GlobalCache::deleteResources() {
30 SkAutoSpinlock lock{ fSpinLock };
31
32 fGraphicsPipelineCache.reset();
33 fComputePipelineCache.reset();
34 fStaticResource.clear();
35 }
36
findGraphicsPipeline(const UniqueKey & key)37 sk_sp<GraphicsPipeline> GlobalCache::findGraphicsPipeline(const UniqueKey& key) {
38 SkAutoSpinlock lock{fSpinLock};
39
40 sk_sp<GraphicsPipeline>* entry = fGraphicsPipelineCache.find(key);
41 return entry ? *entry : nullptr;
42 }
43
addGraphicsPipeline(const UniqueKey & key,sk_sp<GraphicsPipeline> pipeline)44 sk_sp<GraphicsPipeline> GlobalCache::addGraphicsPipeline(const UniqueKey& key,
45 sk_sp<GraphicsPipeline> pipeline) {
46 SkAutoSpinlock lock{fSpinLock};
47
48 sk_sp<GraphicsPipeline>* entry = fGraphicsPipelineCache.find(key);
49 if (!entry) {
50 // No equivalent pipeline was stored in the cache between a previous call to
51 // findGraphicsPipeline() that returned null (triggering the pipeline creation) and this
52 // later adding to the cache.
53 entry = fGraphicsPipelineCache.insert(key, std::move(pipeline));
54 } // else there was a race creating the same pipeline and this thread lost, so return the winner
55 return *entry;
56 }
57
58 #if defined(GRAPHITE_TEST_UTILS)
numGraphicsPipelines() const59 int GlobalCache::numGraphicsPipelines() const {
60 SkAutoSpinlock lock{fSpinLock};
61
62 return fGraphicsPipelineCache.count();
63 }
64
resetGraphicsPipelines()65 void GlobalCache::resetGraphicsPipelines() {
66 SkAutoSpinlock lock{fSpinLock};
67
68 fGraphicsPipelineCache.reset();
69 }
70
forEachGraphicsPipeline(const std::function<void (const UniqueKey &,const GraphicsPipeline *)> & fn)71 void GlobalCache::forEachGraphicsPipeline(
72 const std::function<void(const UniqueKey&, const GraphicsPipeline*)>& fn) {
73 SkAutoSpinlock lock{fSpinLock};
74
75 fGraphicsPipelineCache.foreach([&](const UniqueKey* k, const sk_sp<GraphicsPipeline>* v) {
76 fn(*k, v->get());
77 });
78 }
79 #endif // defined(GRAPHITE_TEST_UTILS)
80
findComputePipeline(const UniqueKey & key)81 sk_sp<ComputePipeline> GlobalCache::findComputePipeline(const UniqueKey& key) {
82 SkAutoSpinlock lock{fSpinLock};
83 sk_sp<ComputePipeline>* entry = fComputePipelineCache.find(key);
84 return entry ? *entry : nullptr;
85 }
86
addComputePipeline(const UniqueKey & key,sk_sp<ComputePipeline> pipeline)87 sk_sp<ComputePipeline> GlobalCache::addComputePipeline(const UniqueKey& key,
88 sk_sp<ComputePipeline> pipeline) {
89 SkAutoSpinlock lock{fSpinLock};
90 sk_sp<ComputePipeline>* entry = fComputePipelineCache.find(key);
91 if (!entry) {
92 entry = fComputePipelineCache.insert(key, std::move(pipeline));
93 }
94 return *entry;
95 }
96
addStaticResource(sk_sp<Resource> resource)97 void GlobalCache::addStaticResource(sk_sp<Resource> resource) {
98 SkAutoSpinlock lock{fSpinLock};
99 fStaticResource.push_back(std::move(resource));
100 }
101
102 } // namespace skgpu::graphite
103