* Copyright 2021 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/ResourceProvider.h"
#include "include/core/SkSamplingOptions.h"
#include "include/core/SkTileMode.h"
#include "include/gpu/graphite/BackendTexture.h"
#include "src/core/SkTraceEvent.h"
#include "src/gpu/graphite/Buffer.h"
#include "src/gpu/graphite/Caps.h"
#include "src/gpu/graphite/CommandBuffer.h"
#include "src/gpu/graphite/ComputePipeline.h"
#include "src/gpu/graphite/ContextPriv.h"
#include "src/gpu/graphite/ContextUtils.h"
#include "src/gpu/graphite/GlobalCache.h"
#include "src/gpu/graphite/GraphicsPipeline.h"
#include "src/gpu/graphite/GraphicsPipelineDesc.h"
#include "src/gpu/graphite/Log.h"
#include "src/gpu/graphite/RenderPassDesc.h"
#include "src/gpu/graphite/RendererProvider.h"
#include "src/gpu/graphite/ResourceCache.h"
#include "src/gpu/graphite/Sampler.h"
#include "src/gpu/graphite/SharedContext.h"
#include "src/gpu/graphite/Texture.h"
#include "src/sksl/SkSLCompiler.h"
namespace skgpu::graphite {
[[maybe_unused]] static std::string to_str(const SharedContext* ctx,
const GraphicsPipelineDesc& gpDesc,
const RenderPassDesc& rpDesc) {
const ShaderCodeDictionary* dict = ctx->shaderCodeDictionary();
const RenderStep* step = ctx->rendererProvider()->lookup(gpDesc.renderStepID());
return GetPipelineLabel(dict, rpDesc, step, gpDesc.paintParamsID());
}
ResourceProvider::ResourceProvider(SharedContext* sharedContext,\
SingleOwner* singleOwner,
uint32_t recorderID,
size_t resourceBudget)
: fSharedContext(sharedContext)
, fResourceCache(ResourceCache::Make(singleOwner, recorderID, resourceBudget)) {}
ResourceProvider::~ResourceProvider() {
fResourceCache->shutdown();
}
sk_sp<GraphicsPipeline> ResourceProvider::findOrCreateGraphicsPipeline(
const RuntimeEffectDictionary* runtimeDict,
const GraphicsPipelineDesc& pipelineDesc,
const RenderPassDesc& renderPassDesc,
SkEnumBitMask<PipelineCreationFlags> pipelineCreationFlags) {
auto globalCache = fSharedContext->globalCache();
UniqueKey pipelineKey = fSharedContext->caps()->makeGraphicsPipelineKey(pipelineDesc,
renderPassDesc);
uint32_t compilationID = 0;
sk_sp<GraphicsPipeline> pipeline = globalCache->findGraphicsPipeline(pipelineKey,
pipelineCreationFlags,
&compilationID);
if (!pipeline) {
TRACE_EVENT1_ALWAYS(
"skia.shaders", "createGraphicsPipeline", "desc",
TRACE_STR_COPY(to_str(fSharedContext, pipelineDesc, renderPassDesc).c_str()));
#if defined(SK_PIPELINE_LIFETIME_LOGGING)
bool forPrecompile =
SkToBool(pipelineCreationFlags & PipelineCreationFlags::kForPrecompilation);
static const char* kNames[2] = { "BeginBuildN", "BeginBuildP" };
TRACE_EVENT_INSTANT2("skia.gpu",
TRACE_STR_STATIC(kNames[forPrecompile]),
TRACE_EVENT_SCOPE_THREAD,
"key", pipelineKey.hash(),
"compilationID", compilationID);
#endif
pipeline = this->createGraphicsPipeline(runtimeDict, pipelineKey,
pipelineDesc, renderPassDesc,
pipelineCreationFlags,
compilationID);
if (pipeline) {
pipeline = globalCache->addGraphicsPipeline(pipelineKey, std::move(pipeline));
}
}
return pipeline;
}
sk_sp<ComputePipeline> ResourceProvider::findOrCreateComputePipeline(
const ComputePipelineDesc& pipelineDesc) {
auto globalCache = fSharedContext->globalCache();
UniqueKey pipelineKey = fSharedContext->caps()->makeComputePipelineKey(pipelineDesc);
sk_sp<ComputePipeline> pipeline = globalCache->findComputePipeline(pipelineKey);
if (!pipeline) {
pipeline = this->createComputePipeline(pipelineDesc);
if (pipeline) {
pipeline = globalCache->addComputePipeline(pipelineKey, std::move(pipeline));
}
}
return pipeline;
}
sk_sp<Texture> ResourceProvider::findOrCreateScratchTexture(SkISize dimensions,
const TextureInfo& info,
std::string_view label,
skgpu::Budgeted budgeted) {
SkASSERT(info.isValid());
static const ResourceType kType = GraphiteResourceKey::GenerateResourceType();
GraphiteResourceKey key;
fSharedContext->caps()->buildKeyForTexture(dimensions, info, kType, Shareable::kNo, &key);
return this->findOrCreateTextureWithKey(dimensions,
info,
key,
std::move(label),
budgeted);
}
sk_sp<Texture> ResourceProvider::findOrCreateDepthStencilAttachment(SkISize dimensions,
const TextureInfo& info) {
SkASSERT(info.isValid());
static const ResourceType kType = GraphiteResourceKey::GenerateResourceType();
GraphiteResourceKey key;
fSharedContext->caps()->buildKeyForTexture(dimensions, info, kType, Shareable::kYes, &key);
return this->findOrCreateTextureWithKey(dimensions,
info,
key,
"DepthStencilAttachment",
skgpu::Budgeted::kYes);
}
sk_sp<Texture> ResourceProvider::findOrCreateDiscardableMSAAAttachment(SkISize dimensions,
const TextureInfo& info) {
SkASSERT(info.isValid());
static const ResourceType kType = GraphiteResourceKey::GenerateResourceType();
GraphiteResourceKey key;
fSharedContext->caps()->buildKeyForTexture(dimensions, info, kType, Shareable::kYes, &key);
return this->findOrCreateTextureWithKey(dimensions,
info,
key,
"DiscardableMSAAAttachment",
skgpu::Budgeted::kYes);
}
sk_sp<Texture> ResourceProvider::findOrCreateTextureWithKey(SkISize dimensions,
const TextureInfo& info,
const GraphiteResourceKey& key,
std::string_view label,
skgpu::Budgeted budgeted) {
SkASSERT(key.shareable() == Shareable::kNo || budgeted == skgpu::Budgeted::kYes);
if (Resource* resource = fResourceCache->findAndRefResource(key, budgeted)) {
resource->setLabel(std::move(label));
return sk_sp<Texture>(static_cast<Texture*>(resource));
}
auto tex = this->createTexture(dimensions, info, budgeted);
if (!tex) {
return nullptr;
}
tex->setKey(key);
tex->setLabel(std::move(label));
fResourceCache->insertResource(tex.get());
return tex;
}
sk_sp<Texture> ResourceProvider::createWrappedTexture(const BackendTexture& backendTexture,
std::string_view label) {
sk_sp<Texture> texture = this->onCreateWrappedTexture(backendTexture);
if (texture) {
texture->setLabel(std::move(label));
}
return texture;
}
sk_sp<Sampler> ResourceProvider::findOrCreateCompatibleSampler(const SamplerDesc& samplerDesc) {
GraphiteResourceKey key = fSharedContext->caps()->makeSamplerKey(samplerDesc);
if (Resource* resource = fResourceCache->findAndRefResource(key, skgpu::Budgeted::kYes)) {
return sk_sp<Sampler>(static_cast<Sampler*>(resource));
}
sk_sp<Sampler> sampler = this->createSampler(samplerDesc);
if (!sampler) {
return nullptr;
}
sampler->setKey(key);
fResourceCache->insertResource(sampler.get());
return sampler;
}
sk_sp<Buffer> ResourceProvider::findOrCreateBuffer(size_t size,
BufferType type,
AccessPattern accessPattern,
std::string_view label) {
static const ResourceType kType = GraphiteResourceKey::GenerateResourceType();
GraphiteResourceKey key;
{
static_assert(sizeof(uint32_t) == 4);
static const int kSizeKeyNum32DataCnt = (sizeof(size_t) + 3) / 4;
static const int kKeyNum32DataCnt = kSizeKeyNum32DataCnt + 1;
SkASSERT(static_cast<uint32_t>(type) < (1u << 4));
SkASSERT(static_cast<uint32_t>(accessPattern) < (1u << 1));
GraphiteResourceKey::Builder builder(&key, kType, kKeyNum32DataCnt, Shareable::kNo);
builder[0] = (static_cast<uint32_t>(type) << 0) |
(static_cast<uint32_t>(accessPattern) << 4);
size_t szKey = size;
for (int i = 0; i < kSizeKeyNum32DataCnt; ++i) {
builder[i + 1] = (uint32_t) szKey;
if constexpr(kSizeKeyNum32DataCnt > 1) {
szKey = szKey >> 32;
}
}
}
skgpu::Budgeted budgeted = skgpu::Budgeted::kYes;
if (Resource* resource = fResourceCache->findAndRefResource(key, budgeted)) {
resource->setLabel(std::move(label));
return sk_sp<Buffer>(static_cast<Buffer*>(resource));
}
auto buffer = this->createBuffer(size, type, accessPattern);
if (!buffer) {
return nullptr;
}
buffer->setKey(key);
buffer->setLabel(std::move(label));
fResourceCache->insertResource(buffer.get());
return buffer;
}
namespace {
bool dimensions_are_valid(const int maxTextureSize, const SkISize& dimensions) {
if (dimensions.isEmpty() ||
dimensions.width() > maxTextureSize ||
dimensions.height() > maxTextureSize) {
SKGPU_LOG_W("Call to createBackendTexture has requested dimensions (%d, %d) larger than the"
" supported gpu max texture size: %d. Or the dimensions are empty.",
dimensions.fWidth, dimensions.fHeight, maxTextureSize);
return false;
}
return true;
}
}
BackendTexture ResourceProvider::createBackendTexture(SkISize dimensions, const TextureInfo& info) {
if (!dimensions_are_valid(fSharedContext->caps()->maxTextureSize(), dimensions)) {
return {};
}
return this->onCreateBackendTexture(dimensions, info);
}
#ifdef SK_BUILD_FOR_ANDROID
BackendTexture ResourceProvider::createBackendTexture(AHardwareBuffer* hardwareBuffer,
bool isRenderable,
bool isProtectedContent,
SkISize dimensions,
bool fromAndroidWindow) const {
if (!dimensions_are_valid(fSharedContext->caps()->maxTextureSize(), dimensions)) {
return {};
}
return this->onCreateBackendTexture(hardwareBuffer,
isRenderable,
isProtectedContent,
dimensions,
fromAndroidWindow);
}
BackendTexture ResourceProvider::onCreateBackendTexture(AHardwareBuffer*,
bool isRenderable,
bool isProtectedContent,
SkISize dimensions,
bool fromAndroidWindow) const {
return {};
}
#endif
void ResourceProvider::deleteBackendTexture(const BackendTexture& texture) {
this->onDeleteBackendTexture(texture);
}
void ResourceProvider::freeGpuResources() {
this->onFreeGpuResources();
fResourceCache->purgeResources();
}
void ResourceProvider::purgeResourcesNotUsedSince(StdSteadyClock::time_point purgeTime) {
this->onPurgeResourcesNotUsedSince(purgeTime);
fResourceCache->purgeResourcesNotUsedSince(purgeTime);
}
}