| 1 |
/* |
| 2 |
* Copyright 2024 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 |
#include "include/gpu/graphite/PrecompileContext.h" |
| 8 |
|
| 9 |
#include "include/core/SkTypes.h" |
| 10 |
#include "src/gpu/GpuTypesPriv.h" |
| 11 |
#include "src/gpu/graphite/GlobalCache.h" |
| 12 |
#include "src/gpu/graphite/ResourceProvider.h" |
| 13 |
#include "src/gpu/graphite/SerializationUtils.h" |
| 14 |
#include "src/gpu/graphite/SharedContext.h" |
| 15 |
|
| 16 |
#if defined(SK_ENABLE_PRECOMPILE) |
| 17 |
#include "src/gpu/graphite/ContextUtils.h" |
| 18 |
#include "src/gpu/graphite/GraphicsPipelineDesc.h" |
| 19 |
#include "src/gpu/graphite/GraphicsPipelineHandle.h" |
| 20 |
#include "src/gpu/graphite/Log.h" |
| 21 |
#include "src/gpu/graphite/PipelineCreationTask.h" |
| 22 |
#include "src/gpu/graphite/RenderPassDesc.h" |
| 23 |
#include "src/gpu/graphite/RendererProvider.h" |
| 24 |
#include "src/gpu/graphite/RuntimeEffectDictionary.h" |
| 25 |
#endif |
| 26 |
|
| 27 |
#include <cstddef> |
| 28 |
|
| 29 |
namespace skgpu::graphite { |
| 30 |
|
| 31 |
#define ASSERT_SINGLE_OWNER SKGPU_ASSERT_SINGLE_OWNER(&fSingleOwner) |
| 32 |
|
| 33 |
PrecompileContext::~PrecompileContext() { |
| 34 |
ASSERT_SINGLE_OWNER |
| 35 |
} |
| 36 |
|
| 37 |
PrecompileContext::PrecompileContext(sk_sp<SharedContext> sharedContext) |
| 38 |
: fSharedContext(sharedContext) { |
| 39 |
|
| 40 |
// ResourceProviders are not thread-safe. Here we create a ResourceProvider |
| 41 |
// specifically for the thread on which precompilation will occur. |
| 42 |
static constexpr size_t kEmptyBudget = 0; |
| 43 |
fResourceProvider = |
| 44 |
fSharedContext->makeResourceProvider(&fSingleOwner, SK_InvalidGenID, kEmptyBudget); |
| 45 |
} |
| 46 |
|
| 47 |
void PrecompileContext::purgePipelinesNotUsedInMs(std::chrono::milliseconds msNotUsed) { |
| 48 |
ASSERT_SINGLE_OWNER |
| 49 |
|
| 50 |
auto purgeTime = skgpu::StdSteadyClock::now() - msNotUsed; |
| 51 |
|
| 52 |
fSharedContext->globalCache()->purgePipelinesNotUsedSince(purgeTime); |
| 53 |
} |
| 54 |
|
| 55 |
void PrecompileContext::reportPipelineStats(StatOptions option) { |
| 56 |
ASSERT_SINGLE_OWNER |
| 57 |
|
| 58 |
if (option == StatOptions::kPrecompile) { |
| 59 |
fSharedContext->globalCache()->reportPrecompileStats(); |
| 60 |
} else { |
| 61 |
fSharedContext->globalCache()->reportCacheStats(); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
bool PrecompileContext::precompile(sk_sp<SkData> serializedPipelineKey) { |
| 66 |
#if defined(SK_ENABLE_PRECOMPILE) |
| 67 |
sk_sp<RuntimeEffectDictionary> rtEffectDict = sk_make_sp<RuntimeEffectDictionary>(); |
| 68 |
const Caps* caps = fSharedContext->caps(); |
| 69 |
|
| 70 |
GraphicsPipelineDesc pipelineDesc; |
| 71 |
RenderPassDesc renderPassDesc; |
| 72 |
|
| 73 |
if (!DataToPipelineDesc(caps, |
| 74 |
fSharedContext->shaderCodeDictionary(), |
| 75 |
serializedPipelineKey.get(), |
| 76 |
&pipelineDesc, |
| 77 |
&renderPassDesc)) { |
| 78 |
return false; |
| 79 |
} |
| 80 |
|
| 81 |
GraphicsPipelineHandle handle = fResourceProvider->createGraphicsPipelineHandle( |
| 82 |
pipelineDesc, |
| 83 |
renderPassDesc, |
| 84 |
PipelineCreationFlags::kForPrecompilation); |
| 85 |
fResourceProvider->startPipelineCreationTask(rtEffectDict, handle); |
| 86 |
|
| 87 |
return true; |
| 88 |
#else |
| 89 |
return false; |
| 90 |
#endif |
| 91 |
} |
| 92 |
|
| 93 |
std::string PrecompileContext::getPipelineLabel(sk_sp<SkData> serializedPipelineKey) { |
| 94 |
#if defined(SK_ENABLE_PRECOMPILE) |
| 95 |
GraphicsPipelineDesc pipelineDesc; |
| 96 |
RenderPassDesc renderPassDesc; |
| 97 |
|
| 98 |
if (!DataToPipelineDesc(fSharedContext->caps(), |
| 99 |
fSharedContext->shaderCodeDictionary(), |
| 100 |
serializedPipelineKey.get(), |
| 101 |
&pipelineDesc, |
| 102 |
&renderPassDesc)) { |
| 103 |
return ""; |
| 104 |
} |
| 105 |
|
| 106 |
const RendererProvider* rendererProvider = fSharedContext->rendererProvider(); |
| 107 |
|
| 108 |
const RenderStep* renderStep = rendererProvider->lookup(pipelineDesc.renderStepID()); |
| 109 |
if (!renderStep) { |
| 110 |
return ""; |
| 111 |
} |
| 112 |
|
| 113 |
return GetPipelineLabel(fSharedContext->caps(), |
| 114 |
fSharedContext->shaderCodeDictionary(), |
| 115 |
renderPassDesc, |
| 116 |
renderStep, |
| 117 |
pipelineDesc.paintParamsID()); |
| 118 |
#else |
| 119 |
return ""; |
| 120 |
#endif |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
} // namespace skgpu::graphite |