Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
GlobalCache.cpp
Go to the documentation of this file.
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
9
14
15namespace skgpu::graphite {
16
18 : fGraphicsPipelineCache(256) // TODO: find a good value for these limits
19 , fComputePipelineCache(256) {}
20
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
30 SkAutoSpinlock lock{ fSpinLock };
31
32 fGraphicsPipelineCache.reset();
33 fComputePipelineCache.reset();
34 fStaticResource.clear();
35}
36
38 SkAutoSpinlock lock{fSpinLock};
39
40 sk_sp<GraphicsPipeline>* entry = fGraphicsPipelineCache.find(key);
41 return entry ? *entry : nullptr;
42}
43
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)
59int GlobalCache::numGraphicsPipelines() const {
60 SkAutoSpinlock lock{fSpinLock};
61
62 return fGraphicsPipelineCache.count();
63}
64
65void GlobalCache::resetGraphicsPipelines() {
66 SkAutoSpinlock lock{fSpinLock};
67
68 fGraphicsPipelineCache.reset();
69}
70
71void 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
82 SkAutoSpinlock lock{fSpinLock};
83 sk_sp<ComputePipeline>* entry = fComputePipelineCache.find(key);
84 return entry ? *entry : nullptr;
85}
86
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
98 SkAutoSpinlock lock{fSpinLock};
99 fStaticResource.push_back(std::move(resource));
100}
101
102} // namespace skgpu::graphite
#define SkASSERT(cond)
Definition SkAssert.h:116
#define SkDEBUGCODE(...)
Definition SkDebug.h:23
T * get() const
Definition SkRefCnt.h:303
sk_sp< ComputePipeline > addComputePipeline(const UniqueKey &, sk_sp< ComputePipeline >) SK_EXCLUDES(fSpinLock)
sk_sp< ComputePipeline > findComputePipeline(const UniqueKey &) SK_EXCLUDES(fSpinLock)
void addStaticResource(sk_sp< Resource >) SK_EXCLUDES(fSpinLock)
sk_sp< GraphicsPipeline > addGraphicsPipeline(const UniqueKey &, sk_sp< GraphicsPipeline >) SK_EXCLUDES(fSpinLock)
sk_sp< GraphicsPipeline > findGraphicsPipeline(const UniqueKey &) SK_EXCLUDES(fSpinLock)