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()17GlobalCache::GlobalCache() 18 : fGraphicsPipelineCache(256) // TODO: find a good value for these limits 19 , fComputePipelineCache(16) {} 20 21 GlobalCache::~GlobalCache() = default; 22 findGraphicsPipeline(const UniqueKey & key)23sk_sp<GraphicsPipeline> GlobalCache::findGraphicsPipeline(const UniqueKey& key) { 24 SkAutoSpinlock lock{fSpinLock}; 25 26 sk_sp<GraphicsPipeline>* entry = fGraphicsPipelineCache.find(key); 27 return entry ? *entry : nullptr; 28 } 29 addGraphicsPipeline(const UniqueKey & key,sk_sp<GraphicsPipeline> pipeline)30sk_sp<GraphicsPipeline> GlobalCache::addGraphicsPipeline(const UniqueKey& key, 31 sk_sp<GraphicsPipeline> pipeline) { 32 SkAutoSpinlock lock{fSpinLock}; 33 34 sk_sp<GraphicsPipeline>* entry = fGraphicsPipelineCache.find(key); 35 if (!entry) { 36 // No equivalent pipeline was stored in the cache between a previous call to 37 // findGraphicsPipeline() that returned null (triggering the pipeline creation) and this 38 // later adding to the cache. 39 entry = fGraphicsPipelineCache.insert(key, std::move(pipeline)); 40 } // else there was a race creating the same pipeline and this thread lost, so return the winner 41 return *entry; 42 } 43 44 #if GRAPHITE_TEST_UTILS numGraphicsPipelines() const45int GlobalCache::numGraphicsPipelines() const { 46 SkAutoSpinlock lock{fSpinLock}; 47 48 return fGraphicsPipelineCache.count(); 49 } 50 resetGraphicsPipelines()51void GlobalCache::resetGraphicsPipelines() { 52 SkAutoSpinlock lock{fSpinLock}; 53 54 fGraphicsPipelineCache.reset(); 55 } 56 #endif // GRAPHITE_TEST_UTILS 57 findComputePipeline(const UniqueKey & key)58sk_sp<ComputePipeline> GlobalCache::findComputePipeline(const UniqueKey& key) { 59 SkAutoSpinlock lock{fSpinLock}; 60 sk_sp<ComputePipeline>* entry = fComputePipelineCache.find(key); 61 return entry ? *entry : nullptr; 62 } 63 addComputePipeline(const UniqueKey & key,sk_sp<ComputePipeline> pipeline)64sk_sp<ComputePipeline> GlobalCache::addComputePipeline(const UniqueKey& key, 65 sk_sp<ComputePipeline> pipeline) { 66 SkAutoSpinlock lock{fSpinLock}; 67 sk_sp<ComputePipeline>* entry = fComputePipelineCache.find(key); 68 if (!entry) { 69 entry = fComputePipelineCache.insert(key, std::move(pipeline)); 70 } 71 return *entry; 72 } 73 addStaticResource(sk_sp<Resource> resource)74void GlobalCache::addStaticResource(sk_sp<Resource> resource) { 75 SkAutoSpinlock lock{fSpinLock}; 76 fStaticResource.push_back(std::move(resource)); 77 } 78 79 } // namespace skgpu::graphite 80