Debian Codegraph @

src/gpu/graphite/PipelineManager.cpp

raw
1 /*
2 * Copyright 2025 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/PipelineManager.h"
9
10 #include "src/core/SkTaskGroup.h"
11 #include "src/gpu/graphite/GraphicsPipelineDesc.h"
12 #include "src/gpu/graphite/GraphicsPipelineHandle.h"
13 #include "src/gpu/graphite/Log.h"
14 #include "src/gpu/graphite/PipelineCreationTask.h"
15 #include "src/gpu/graphite/RenderPassDesc.h"
16 #include "src/gpu/graphite/RuntimeEffectDictionary.h"
17 #include "src/gpu/graphite/SharedContext.h"
18
19 namespace skgpu::graphite {
20
21 GraphicsPipelineHandle::GraphicsPipelineHandle(sk_sp<PipelineCreationTask> task)
22 : fTaskOrPipeline(std::move(task)) {}
23
24 GraphicsPipelineHandle::GraphicsPipelineHandle(sk_sp<GraphicsPipeline> pipeline)
25 : fTaskOrPipeline(std::move(pipeline)) {}
26
27 PipelineManager::PipelineManager() {}
28 PipelineManager::~PipelineManager() {}
29
30 const UniqueKey& PipelineManager::Traits::GetKey(const sk_sp<PipelineCreationTask>& task) {
31 return task->fPipelineKey;
32 }
33
34 uint32_t PipelineManager::Traits::Hash(const UniqueKey& pipelineKey) {
35 return pipelineKey.hash();
36 }
37
38 GraphicsPipelineHandle PipelineManager::createHandle(
39 SharedContext* sharedContext,
40 const GraphicsPipelineDesc& pipelineDesc,
41 const RenderPassDesc& renderPassDesc,
42 SkEnumBitMask<PipelineCreationFlags> pipelineCreationFlags) {
43 GlobalCache* globalCache = sharedContext->globalCache();
44 const Caps* caps = sharedContext->caps();
45
46 UniqueKey pipelineKey = caps->makeGraphicsPipelineKey(pipelineDesc, renderPassDesc);
47
48 if (sk_sp<PipelineCreationTask> task = this->findTask(pipelineKey)) {
49 // There is a task in progress to compile this Pipeline so it can't be ready yet (i.e.,
50 // it isn't in the Pipeline Cache).
51 return GraphicsPipelineHandle(std::move(task));
52 }
53
54 sk_sp<GraphicsPipeline> pipeline = globalCache->findGraphicsPipeline(
55 pipelineKey,
56 pipelineCreationFlags);
57 if (pipeline) {
58 return GraphicsPipelineHandle(std::move(pipeline));
59 }
60
61 sk_sp<PipelineCreationTask> task = this->findOrCreateTask(pipelineKey,
62 pipelineDesc,
63 renderPassDesc,
64 pipelineCreationFlags);
65 return GraphicsPipelineHandle(std::move(task));
66 }
67
68 void PipelineManager::startPipelineCreationTask(SharedContext* sharedContext,
69 sk_sp<const RuntimeEffectDictionary> runtimeDict,
70 const GraphicsPipelineHandle& handle) {
71 if (std::holds_alternative<sk_sp<GraphicsPipeline>>(handle.fTaskOrPipeline)) {
72 return;
73 }
74
75 sk_sp<PipelineCreationTask> task =
76 std::get<sk_sp<PipelineCreationTask>>(handle.fTaskOrPipeline);
77
78 sk_sp<GraphicsPipeline> pipeline = sharedContext->findOrCreateGraphicsPipeline(
79 runtimeDict.get(),
80 task->fPipelineKey,
81 task->fGraphicsPipelineDesc,
82 task->fRenderPassDesc,
83 task->fPipelineCreationFlags);
84
85 if (!pipeline) {
86 SKGPU_LOG_W("Failed to create GraphicsPipeline!");
87 }
88
89 if (!task->fCompleted.exchange(true)) {
90 task->fPipeline = pipeline;
91 this->removeTask(task.get());
92 }
93 }
94
95 sk_sp<GraphicsPipeline> PipelineManager::resolveHandle(const GraphicsPipelineHandle& handle) {
96 if (std::holds_alternative<sk_sp<GraphicsPipeline>>(handle.fTaskOrPipeline)) {
97 return std::get<sk_sp<GraphicsPipeline>>(handle.fTaskOrPipeline);
98 }
99
100 // Since 'fTaskOrPipeline' doesn't hold a pipeline the pipeline must not have existed when
101 // the handle was created so a compilation task must've been created to compile it
102 sk_sp<PipelineCreationTask> task =
103 std::get<sk_sp<PipelineCreationTask>>(handle.fTaskOrPipeline);
104
105 // For the non-threaded version of the PipelineManager, whenever a thread gets here it
106 // will already have blindly executed the task (in DrawPass::prepareResources).
107 SkASSERT(task->fCompleted);
108 return task->fPipeline;
109 }
110
111 #if defined(GPU_TEST_UTILS)
112 PipelineManager::Stats PipelineManager::getStats() const {
113 SkAutoSpinlock lock{fSpinLock};
114
115 return fStats;
116 }
117 #endif
118
119 sk_sp<PipelineCreationTask> PipelineManager::findTask(const UniqueKey& pipelineKey) {
120 SkAutoSpinlock lock{fSpinLock};
121
122 sk_sp<PipelineCreationTask> task = fActiveTasks.findOrNull(pipelineKey);
123
124 #if defined(GPU_TEST_UTILS)
125 if (task) {
126 fStats.fNumPreemptivelyFoundTasks++;
127 }
128 #endif
129
130 return task;
131 }
132
133 sk_sp<PipelineCreationTask> PipelineManager::findOrCreateTask(
134 const UniqueKey& pipelineKey,
135 const GraphicsPipelineDesc& pipelineDesc,
136 const RenderPassDesc& renderPassDesc,
137 SkEnumBitMask<PipelineCreationFlags> pipelineCreationFlags) {
138 SkAutoSpinlock lock{fSpinLock};
139
140 sk_sp<PipelineCreationTask>* task = fActiveTasks.find(pipelineKey);
141 if (task) {
142 // There is a race in createHandle from when we first check for a task; then, failing that,
143 // check for an existing pipeline; then, failing that, try to create a new task. Thus,
144 // we can sometimes find our task here.
145 #if defined(GPU_TEST_UTILS)
146 fStats.fNumTaskCreationRaces++;
147 #endif
148 return *task;
149 }
150
151 #if defined(GPU_TEST_UTILS)
152 fStats.fNumTasksCreated++;
153 #endif
154
155 sk_sp<PipelineCreationTask> newTask = sk_sp<PipelineCreationTask>(
156 new PipelineCreationTask(pipelineKey,
157 pipelineDesc,
158 renderPassDesc,
159 pipelineCreationFlags));
160 fActiveTasks.set(newTask);
161 return newTask;
162 }
163
164 void PipelineManager::removeTask(PipelineCreationTask* task) {
165 SkAutoSpinlock lock{fSpinLock};
166
167 // TODO(robertphillips): this guard is only necessary in the non-threaded version of
168 // the PipelineManager
169 if (fActiveTasks.findOrNull(task->fPipelineKey)) {
170 fActiveTasks.remove(task->fPipelineKey);
171 }
172 }
173
174 } // namespace skgpu::graphite