Debian Codegraph @

src/gpu/graphite/ResourceProvider.cpp

raw
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/ResourceProvider.h"
9
10 #include "include/core/SkSamplingOptions.h"
11 #include "include/core/SkTileMode.h"
12 #include "include/gpu/graphite/BackendTexture.h"
13 #include "src/gpu/graphite/Buffer.h"
14 #include "src/gpu/graphite/Caps.h"
15 #include "src/gpu/graphite/CommandBuffer.h"
16 #include "src/gpu/graphite/ComputePipeline.h"
17 #include "src/gpu/graphite/ContextPriv.h"
18 #include "src/gpu/graphite/ContextUtils.h"
19 #include "src/gpu/graphite/GlobalCache.h"
20 #include "src/gpu/graphite/GraphicsPipeline.h"
21 #include "src/gpu/graphite/GraphicsPipelineHandle.h"
22 #include "src/gpu/graphite/Log.h"
23 #include "src/gpu/graphite/PipelineCreationTask.h"
24 #include "src/gpu/graphite/PipelineManager.h"
25 #include "src/gpu/graphite/RenderPassDesc.h"
26 #include "src/gpu/graphite/RendererProvider.h"
27 #include "src/gpu/graphite/ResourceCache.h"
28 #include "src/gpu/graphite/RuntimeEffectDictionary.h"
29 #include "src/gpu/graphite/Sampler.h"
30 #include "src/gpu/graphite/SharedContext.h"
31 #include "src/gpu/graphite/Texture.h"
32 #include "src/sksl/SkSLCompiler.h"
33
34 namespace skgpu::graphite {
35
36 ResourceProvider::ResourceProvider(SharedContext* sharedContext,
37 SingleOwner* singleOwner,
38 uint32_t recorderID,
39 size_t resourceBudget)
40 : fSharedContext(sharedContext)
41 , fResourceCache(ResourceCache::Make(singleOwner, recorderID, resourceBudget)) {}
42
43 ResourceProvider::~ResourceProvider() {
44 fResourceCache->shutdown();
45 }
46
47 GraphicsPipelineHandle ResourceProvider::createGraphicsPipelineHandle(
48 const GraphicsPipelineDesc& pipelineDesc,
49 const RenderPassDesc& renderPassDesc,
50 SkEnumBitMask<PipelineCreationFlags> pipelineCreationFlags) {
51
52 PipelineManager* pipelineManager = fSharedContext->pipelineManager();
53
54 return pipelineManager->createHandle(fSharedContext,
55 pipelineDesc,
56 renderPassDesc,
57 pipelineCreationFlags);
58 }
59
60 void ResourceProvider::startPipelineCreationTask(sk_sp<const RuntimeEffectDictionary> runtimeDict,
61 const GraphicsPipelineHandle& handle) {
62 PipelineManager* pipelineManager = fSharedContext->pipelineManager();
63
64 pipelineManager->startPipelineCreationTask(fSharedContext,
65 std::move(runtimeDict),
66 handle);
67 }
68
69 sk_sp<GraphicsPipeline> ResourceProvider::resolveHandle(const GraphicsPipelineHandle& handle) {
70 PipelineManager* pipelineManager = fSharedContext->pipelineManager();
71
72 return pipelineManager->resolveHandle(handle);
73 }
74
75 sk_sp<ComputePipeline> ResourceProvider::findOrCreateComputePipeline(
76 const ComputePipelineDesc& pipelineDesc) {
77 auto globalCache = fSharedContext->globalCache();
78 UniqueKey pipelineKey = fSharedContext->caps()->makeComputePipelineKey(pipelineDesc);
79 sk_sp<ComputePipeline> pipeline = globalCache->findComputePipeline(pipelineKey);
80 if (!pipeline) {
81 pipeline = this->createComputePipeline(pipelineDesc);
82 if (pipeline) {
83 pipeline = globalCache->addComputePipeline(pipelineKey, std::move(pipeline));
84 }
85 }
86 return pipeline;
87 }
88
89 ////////////////////////////////////////////////////////////////////////////////////////////////
90
91 sk_sp<Texture> ResourceProvider::findOrCreateShareableTexture(SkISize dimensions,
92 const TextureInfo& info,
93 std::string_view label) {
94 return this->findOrCreateTexture(dimensions, info, label, Budgeted::kYes, Shareable::kYes);
95 }
96
97 sk_sp<Texture> ResourceProvider::findOrCreateNonShareableTexture(SkISize dimensions,
98 const TextureInfo& info,
99 std::string_view label,
100 Budgeted budgeted) {
101 return this->findOrCreateTexture(dimensions, info, label, budgeted, Shareable::kNo);
102 }
103
104 sk_sp<Texture> ResourceProvider::findOrCreateScratchTexture(
105 SkISize dimensions,
106 const TextureInfo& info,
107 std::string_view label,
108 const ResourceCache::ScratchResourceSet& unavailable) {
109 return this->findOrCreateTexture(
110 dimensions, info, label, Budgeted::kYes, Shareable::kScratch, &unavailable);
111 }
112
113 sk_sp<Texture> ResourceProvider::findOrCreateTexture(
114 SkISize dimensions,
115 const TextureInfo& info,
116 std::string_view label,
117 Budgeted budgeted,
118 Shareable shareable,
119 const ResourceCache::ScratchResourceSet* unavailable) {
120 // If the resource is shareable it should be budgeted since it shouldn't be backing any client
121 // owned object.
122 SkASSERT(shareable == Shareable::kNo || budgeted == Budgeted::kYes);
123 SkASSERT(shareable != Shareable::kScratch || SkToBool(unavailable));
124
125 static const ResourceType kType = GraphiteResourceKey::GenerateResourceType();
126
127 if (!info.isValid()) {
128 // Checking for a valid TextureInfo here allows callers to consolidate error checking for
129 // both TextureInfo and Texture creation to checking for a null returned Texture.
130 return nullptr;
131 }
132
133 GraphiteResourceKey key;
134 fSharedContext->caps()->buildKeyForTexture(dimensions, info, kType, &key);
135
136 if (Resource* resource =
137 fResourceCache->findAndRefResource(key, budgeted, shareable, unavailable)) {
138 // Shareable resource labels should only be set upon creation.
139 if (shareable == Shareable::kYes) {
140 SkASSERT(resource->getLabel() == label);
141 } else {
142 resource->setLabel(label);
143 }
144 return sk_sp<Texture>(static_cast<Texture*>(resource));
145 }
146
147 auto tex = this->createTexture(dimensions, info);
148 if (!tex) {
149 return nullptr;
150 }
151
152 tex->setLabel(label);
153 fResourceCache->insertResource(tex.get(), key, budgeted, shareable);
154
155 return tex;
156 }
157
158 sk_sp<Texture> ResourceProvider::createWrappedTexture(const BackendTexture& backendTexture,
159 std::string_view label) {
160 sk_sp<Texture> texture = this->onCreateWrappedTexture(backendTexture);
161 if (texture) {
162 texture->setLabel(label);
163 SkASSERT(texture->ownership() == Ownership::kWrapped);
164 }
165 return texture;
166 }
167
168 sk_sp<Sampler> ResourceProvider::findOrCreateCompatibleSampler(const SamplerDesc& samplerDesc) {
169 static constexpr Budgeted kBudgeted = Budgeted::kYes;
170 static constexpr Shareable kShareable = Shareable::kYes;
171 static const ResourceType kType = GraphiteResourceKey::GenerateResourceType();
172
173 GraphiteResourceKey key;
174 {
175 // The size of the returned span accurately captures the quantity of uint32s needed whether
176 // the sampler is immutable or not. Each backend will already have encoded any specific
177 // immutable sampler details into the SamplerDesc, so there is no need to delegate to Caps
178 // to create a specific key.
179 const SkSpan<const uint32_t>& samplerData = samplerDesc.asSpan();
180 GraphiteResourceKey::Builder builder(&key, kType, SkTo<uint16_t>(samplerData.size()));
181
182 for (size_t i = 0; i < samplerData.size(); i++) {
183 builder[i] = samplerData[i];
184 }
185 }
186
187 if (Resource* resource = fResourceCache->findAndRefResource(key, kBudgeted, kShareable)) {
188 return sk_sp<Sampler>(static_cast<Sampler*>(resource));
189 }
190
191 sk_sp<Sampler> sampler = this->createSampler(samplerDesc);
192 if (!sampler) {
193 return nullptr;
194 }
195
196 fResourceCache->insertResource(sampler.get(), key, kBudgeted, kShareable);
197 return sampler;
198 }
199
200 sk_sp<Buffer> ResourceProvider::findOrCreateNonShareableBuffer(size_t size,
201 BufferType type,
202 AccessPattern accessPattern,
203 std::string_view label) {
204 return this->findOrCreateBuffer(size, type, accessPattern, label, Shareable::kNo);
205 }
206
207 sk_sp<Buffer> ResourceProvider::findOrCreateScratchBuffer(
208 size_t size,
209 BufferType type,
210 AccessPattern access,
211 std::string_view label,
212 const ResourceCache::ScratchResourceSet& unvailable) {
213 // Scratch buffers must be GPU only, mapped access makes it too difficult to scope their
214 // reads and writes within the actual command buffer execution.
215 SkASSERT(access != AccessPattern::kHostVisible);
216 return this->findOrCreateBuffer(size, type, access, label, Shareable::kScratch, &unvailable);
217 }
218
219 sk_sp<Buffer> ResourceProvider::findOrCreateBuffer(
220 size_t size,
221 BufferType type,
222 AccessPattern accessPattern,
223 std::string_view label,
224 Shareable shareable,
225 const ResourceCache::ScratchResourceSet* unavailable) {
226 static constexpr Budgeted kBudgeted = Budgeted::kYes;
227 static const ResourceType kType = GraphiteResourceKey::GenerateResourceType();
228
229 GraphiteResourceKey key;
230 {
231 // For the key we need ((sizeof(size_t) + (sizeof(uint32_t) - 1)) / (sizeof(uint32_t))
232 // uint32_t's for the size and one uint32_t for the rest.
233 static_assert(sizeof(uint32_t) == 4);
234 static const uint16_t kSizeKeyNum32DataCnt = (sizeof(size_t) + 3) / 4;
235 static const uint16_t kKeyNum32DataCnt = kSizeKeyNum32DataCnt + 1;
236
237 SkASSERT(static_cast<uint32_t>(type) < (1u << 4));
238 SkASSERT(static_cast<uint32_t>(accessPattern) < (1u << 2));
239
240 GraphiteResourceKey::Builder builder(&key, kType, kKeyNum32DataCnt);
241 builder[0] = (static_cast<uint32_t>(type) << 0) |
242 (static_cast<uint32_t>(accessPattern) << 4);
243 size_t szKey = size;
244 for (int i = 0; i < kSizeKeyNum32DataCnt; ++i) {
245 builder[i + 1] = (uint32_t) szKey;
246
247 // If size_t is 4 bytes, we cannot do a shift of 32 or else we get a warning/error that
248 // shift amount is >= width of the type.
249 if constexpr(kSizeKeyNum32DataCnt > 1) {
250 szKey = szKey >> 32;
251 }
252 }
253 }
254
255 if (Resource* resource =
256 fResourceCache->findAndRefResource(key, kBudgeted, shareable, unavailable)) {
257 // Shareable resource labels should only be set upon creation.
258 if (shareable == Shareable::kYes) {
259 SkASSERT(resource->getLabel() == label);
260 } else {
261 resource->setLabel(label);
262 }
263 return sk_sp<Buffer>(static_cast<Buffer*>(resource));
264 }
265 auto buffer = this->createBuffer(size, type, accessPattern);
266 if (!buffer) {
267 return nullptr;
268 }
269
270 buffer->setLabel(label);
271 fResourceCache->insertResource(buffer.get(), key, kBudgeted, shareable);
272 return buffer;
273 }
274
275 namespace {
276 bool dimensions_are_valid(const int maxTextureSize, const SkISize& dimensions) {
277 if (dimensions.isEmpty() ||
278 dimensions.width() > maxTextureSize ||
279 dimensions.height() > maxTextureSize) {
280 SKGPU_LOG_W("Call to createBackendTexture has requested dimensions (%d, %d) larger than the"
281 " supported gpu max texture size: %d. Or the dimensions are empty.",
282 dimensions.fWidth, dimensions.fHeight, maxTextureSize);
283 return false;
284 }
285 return true;
286 }
287 }
288
289 BackendTexture ResourceProvider::createBackendTexture(SkISize dimensions, const TextureInfo& info) {
290 if (!dimensions_are_valid(fSharedContext->caps()->maxTextureSize(), dimensions)) {
291 return {};
292 }
293 return this->onCreateBackendTexture(dimensions, info);
294 }
295
296 #ifdef SK_BUILD_FOR_ANDROID
297 BackendTexture ResourceProvider::createBackendTexture(AHardwareBuffer* hardwareBuffer,
298 bool isRenderable,
299 bool isProtectedContent,
300 SkISize dimensions,
301 bool fromAndroidWindow) const {
302 if (!dimensions_are_valid(fSharedContext->caps()->maxTextureSize(), dimensions)) {
303 return {};
304 }
305 return this->onCreateBackendTexture(hardwareBuffer,
306 isRenderable,
307 isProtectedContent,
308 dimensions,
309 fromAndroidWindow);
310 }
311
312 BackendTexture ResourceProvider::onCreateBackendTexture(AHardwareBuffer*,
313 bool isRenderable,
314 bool isProtectedContent,
315 SkISize dimensions,
316 bool fromAndroidWindow) const {
317 return {};
318 }
319 #endif
320
321 void ResourceProvider::deleteBackendTexture(const BackendTexture& texture) {
322 this->onDeleteBackendTexture(texture);
323 }
324
325 void ResourceProvider::freeGpuResources() {
326 this->onFreeGpuResources();
327
328 // TODO: Are there Resources that are ref'd by the ResourceProvider or its subclasses that need
329 // be released? If we ever find that we're holding things directly on the ResourceProviders we
330 // call down into the subclasses to allow them to release things.
331
332 fResourceCache->purgeResources();
333 }
334
335 void ResourceProvider::purgeResourcesNotUsedSince(StdSteadyClock::time_point purgeTime) {
336 this->onPurgeResourcesNotUsedSince(purgeTime);
337 fResourceCache->purgeResourcesNotUsedSince(purgeTime);
338 }
339
340 const Caps* ResourceProvider::caps() const {
341 return fSharedContext->caps();
342 }
343
344 } // namespace skgpu::graphite