| 1 |
/* |
| 2 |
* Copyright 2021 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/SharedContext.h" |
| 9 |
|
| 10 |
#include "include/gpu/graphite/BackendTexture.h" |
| 11 |
#include "include/gpu/graphite/TextureInfo.h" |
| 12 |
#include "src/core/SkTraceEvent.h" |
| 13 |
#include "src/gpu/graphite/Caps.h" |
| 14 |
#include "src/gpu/graphite/CommandBuffer.h" |
| 15 |
#include "src/gpu/graphite/ContextUtils.h" |
| 16 |
#include "src/gpu/graphite/GpuWorkSubmission.h" |
| 17 |
#include "src/gpu/graphite/GraphicsPipelineDesc.h" |
| 18 |
#include "src/gpu/graphite/RendererProvider.h" |
| 19 |
#include "src/gpu/graphite/ResourceProvider.h" |
| 20 |
#include "src/gpu/graphite/SerializationUtils.h" |
| 21 |
#include "src/gpu/graphite/ThreadSafeResourceProvider.h" |
| 22 |
|
| 23 |
namespace skgpu::graphite { |
| 24 |
|
| 25 |
static Layout get_binding_layout(const Caps* caps) { |
| 26 |
ResourceBindingRequirements reqs = caps->resourceBindingRequirements(); |
| 27 |
return caps->storageBufferSupport() ? reqs.fStorageBufferLayout : reqs.fUniformBufferLayout; |
| 28 |
} |
| 29 |
|
| 30 |
// TODO (robertphillips): make use of executor here |
| 31 |
SharedContext::SharedContext(std::unique_ptr<const Caps> caps, |
| 32 |
BackendApi backend, |
| 33 |
SkExecutor* executor, |
| 34 |
SkSpan<sk_sp<SkRuntimeEffect>> userDefinedKnownRuntimeEffects) |
| 35 |
: fCaps(std::move(caps)) |
| 36 |
, fBackend(backend) |
| 37 |
, fGlobalCache() |
| 38 |
, fPipelineManager() // TODO(robertphillips): pass in executor here |
| 39 |
, fShaderDictionary(get_binding_layout(fCaps.get()), userDefinedKnownRuntimeEffects) {} |
| 40 |
|
| 41 |
SharedContext::~SharedContext() { |
| 42 |
// TODO: add disconnect? |
| 43 |
|
| 44 |
// TODO: destroyResources instead? |
| 45 |
} |
| 46 |
|
| 47 |
Protected SharedContext::isProtected() const { return Protected(fCaps->protectedSupport()); } |
| 48 |
|
| 49 |
void SharedContext::setRendererProvider(std::unique_ptr<RendererProvider> rendererProvider) { |
| 50 |
// Should only be called once and be non-null |
| 51 |
SkASSERT(rendererProvider && !fRendererProvider); |
| 52 |
fRendererProvider = std::move(rendererProvider); |
| 53 |
} |
| 54 |
|
| 55 |
void SharedContext::setCaptureManager(sk_sp<SkCaptureManager> captureManager) { |
| 56 |
// Should only be called once and be non-null |
| 57 |
SkASSERT(captureManager && !fCaptureManager); |
| 58 |
fCaptureManager = captureManager; |
| 59 |
} |
| 60 |
|
| 61 |
// This is only used when tracing is enabled at compile time. |
| 62 |
[[maybe_unused]] static std::string to_str(const SharedContext* ctx, |
| 63 |
const GraphicsPipelineDesc& gpDesc, |
| 64 |
const RenderPassDesc& rpDesc) { |
| 65 |
const ShaderCodeDictionary* dict = ctx->shaderCodeDictionary(); |
| 66 |
const RenderStep* step = ctx->rendererProvider()->lookup(gpDesc.renderStepID()); |
| 67 |
return GetPipelineLabel(ctx->caps(), dict, rpDesc, step, gpDesc.paintParamsID()); |
| 68 |
} |
| 69 |
|
| 70 |
sk_sp<GraphicsPipeline> SharedContext::findOrCreateGraphicsPipeline( |
| 71 |
const RuntimeEffectDictionary* runtimeDict, |
| 72 |
const UniqueKey& pipelineKey, |
| 73 |
const GraphicsPipelineDesc& pipelineDesc, |
| 74 |
const RenderPassDesc& renderPassDesc, |
| 75 |
SkEnumBitMask<PipelineCreationFlags> pipelineCreationFlags) { |
| 76 |
auto globalCache = this->globalCache(); |
| 77 |
|
| 78 |
uint32_t compilationID = 0; |
| 79 |
sk_sp<GraphicsPipeline> pipeline = globalCache->findGraphicsPipeline(pipelineKey, |
| 80 |
pipelineCreationFlags, |
| 81 |
&compilationID); |
| 82 |
if (!pipeline) { |
| 83 |
// Haven't encountered this pipeline, so create a new one. Since pipelines are shared |
| 84 |
// across Recorders, we could theoretically create equivalent pipelines on different |
| 85 |
// threads. If this happens, GlobalCache returns the first-through-gate pipeline and we |
| 86 |
// discard the redundant pipeline. While this is wasted effort in the rare event of a race, |
| 87 |
// it allows pipeline creation to be performed without locking the global cache. |
| 88 |
// NOTE: The parameters to TRACE_EVENT are only evaluated inside an if-block when the |
| 89 |
// category is enabled. |
| 90 |
TRACE_EVENT1_ALWAYS( |
| 91 |
"skia.shaders", "createGraphicsPipeline", "desc", |
| 92 |
TRACE_STR_COPY(to_str(this, pipelineDesc, renderPassDesc).c_str())); |
| 93 |
|
| 94 |
#if defined(SK_PIPELINE_LIFETIME_LOGGING) |
| 95 |
bool forPrecompile = |
| 96 |
SkToBool(pipelineCreationFlags & PipelineCreationFlags::kForPrecompilation); |
| 97 |
|
| 98 |
static const char* kNames[2] = { "BeginBuildN", "BeginBuildP" }; |
| 99 |
TRACE_EVENT_INSTANT2("skia.gpu", |
| 100 |
TRACE_STR_STATIC(kNames[forPrecompile]), |
| 101 |
TRACE_EVENT_SCOPE_THREAD, |
| 102 |
"key", pipelineKey.hash(), |
| 103 |
"compilationID", compilationID); |
| 104 |
#endif |
| 105 |
|
| 106 |
pipeline = this->createGraphicsPipeline(runtimeDict, pipelineKey, |
| 107 |
pipelineDesc, renderPassDesc, |
| 108 |
pipelineCreationFlags, |
| 109 |
compilationID); |
| 110 |
if (pipeline) { |
| 111 |
// TODO: Should we store a null pipeline if we failed to create one so that subsequent |
| 112 |
// usage immediately sees that the pipeline cannot be created, vs. retrying every time? |
| 113 |
bool addedToCache; |
| 114 |
std::tie(pipeline, addedToCache) = globalCache->addGraphicsPipeline(pipelineKey, |
| 115 |
pipeline); |
| 116 |
|
| 117 |
if (addedToCache && globalCache->hasPipelineCallback()) { |
| 118 |
sk_sp<SkData> data = PipelineDescToData(this->caps(), |
| 119 |
this->shaderCodeDictionary(), |
| 120 |
pipelineDesc, |
| 121 |
renderPassDesc); |
| 122 |
globalCache->invokePipelineCallback( |
| 123 |
ContextOptions::PipelineCacheOp::kAddingPipeline, |
| 124 |
pipeline.get(), |
| 125 |
std::move(data)); |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
return pipeline; |
| 130 |
} |
| 131 |
|
| 132 |
#if defined(SK_DEBUG) |
| 133 |
size_t SharedContext::getResourceCacheLimit() const { |
| 134 |
return fThreadSafeResourceProvider->getResourceCacheLimit(); |
| 135 |
} |
| 136 |
size_t SharedContext::getResourceCacheCurrentBudgetedBytes() const { |
| 137 |
return fThreadSafeResourceProvider->getResourceCacheCurrentBudgetedBytes(); |
| 138 |
} |
| 139 |
size_t SharedContext::getResourceCacheCurrentPurgeableBytes() const { |
| 140 |
return fThreadSafeResourceProvider->getResourceCacheCurrentPurgeableBytes(); |
| 141 |
} |
| 142 |
#endif |
| 143 |
|
| 144 |
void SharedContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const { |
| 145 |
fThreadSafeResourceProvider->dumpMemoryStatistics(traceMemoryDump); |
| 146 |
} |
| 147 |
void SharedContext::freeGpuResources() { |
| 148 |
fThreadSafeResourceProvider->freeGpuResources(); |
| 149 |
} |
| 150 |
void SharedContext::purgeResourcesNotUsedSince(StdSteadyClock::time_point purgeTime) { |
| 151 |
fThreadSafeResourceProvider->purgeResourcesNotUsedSince(purgeTime); |
| 152 |
} |
| 153 |
void SharedContext::forceProcessReturnedResources() { |
| 154 |
fThreadSafeResourceProvider->forceProcessReturnedResources(); |
| 155 |
} |
| 156 |
|
| 157 |
} // namespace skgpu::graphite |