blob: c950b3f9b1b08cda9f62ef569f660948282acaa7 [file] [log] [blame]
/*
* Copyright 2022 Google LLC
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "src/gpu/graphite/GlobalCache.h"
#include "src/gpu/graphite/ComputePipeline.h"
#include "src/gpu/graphite/ContextUtils.h"
#include "src/gpu/graphite/GraphicsPipeline.h"
namespace skgpu::graphite {
GlobalCache::GlobalCache()
: fGraphicsPipelineCache(16) // TODO: find a good value for these limits
, fComputePipelineCache(16) {}
GlobalCache::~GlobalCache() = default;
sk_sp<GraphicsPipeline> GlobalCache::findGraphicsPipeline(const UniqueKey& key) {
SkAutoSpinlock lock{fSpinLock};
sk_sp<GraphicsPipeline>* entry = fGraphicsPipelineCache.find(key);
return entry ? *entry : nullptr;
}
sk_sp<GraphicsPipeline> GlobalCache::addGraphicsPipeline(const UniqueKey& key,
sk_sp<GraphicsPipeline> pipeline) {
SkAutoSpinlock lock{fSpinLock};
sk_sp<GraphicsPipeline>* entry = fGraphicsPipelineCache.find(key);
if (!entry) {
// No equivalent pipeline was stored in the cache between a previous call to
// findGraphicsPipeline() that returned null (triggering the pipeline creation) and this
// later adding to the cache.
entry = fGraphicsPipelineCache.insert(key, std::move(pipeline));
} // else there was a race creating the same pipeline and this thread lost, so return the winner
return *entry;
}
sk_sp<ComputePipeline> GlobalCache::findComputePipeline(const UniqueKey& key) {
SkAutoSpinlock lock{fSpinLock};
sk_sp<ComputePipeline>* entry = fComputePipelineCache.find(key);
return entry ? *entry : nullptr;
}
sk_sp<ComputePipeline> GlobalCache::addComputePipeline(const UniqueKey& key,
sk_sp<ComputePipeline> pipeline) {
SkAutoSpinlock lock{fSpinLock};
sk_sp<ComputePipeline>* entry = fComputePipelineCache.find(key);
if (!entry) {
entry = fComputePipelineCache.insert(key, std::move(pipeline));
}
return *entry;
}
} // namespace skgpu::graphite