Debian Codegraph @

src/gpu/ganesh/GrProxyProvider.cpp

raw
1 /*
2 * Copyright 2018 Google Inc.
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 "src/gpu/ganesh/GrProxyProvider.h"
8
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkData.h"
11 #include "include/core/SkPixmap.h"
12 #include "include/core/SkSize.h"
13 #include "include/core/SkTypes.h"
14 #include "include/gpu/GpuTypes.h"
15 #include "include/gpu/ganesh/GrBackendSurface.h"
16 #include "include/gpu/ganesh/GrContextThreadSafeProxy.h"
17 #include "include/gpu/ganesh/GrDirectContext.h"
18 #include "include/private/base/SingleOwner.h"
19 #include "include/private/gpu/ganesh/GrImageContext.h"
20 #include "src/core/SkImageInfoPriv.h"
21 #include "src/core/SkMipmap.h"
22 #include "src/core/SkTraceEvent.h"
23 #include "src/gpu/SkBackingFit.h"
24 #include "src/gpu/Swizzle.h"
25 #include "src/gpu/ganesh/GrCaps.h"
26 #include "src/gpu/ganesh/GrContextThreadSafeProxyPriv.h"
27 #include "src/gpu/ganesh/GrDirectContextPriv.h"
28 #include "src/gpu/ganesh/GrGpuResource.h"
29 #include "src/gpu/ganesh/GrGpuResourcePriv.h"
30 #include "src/gpu/ganesh/GrImageContextPriv.h"
31 #include "src/gpu/ganesh/GrRenderTarget.h"
32 #include "src/gpu/ganesh/GrRenderTargetProxy.h"
33 #include "src/gpu/ganesh/GrResourceCache.h"
34 #include "src/gpu/ganesh/GrResourceProvider.h"
35 #include "src/gpu/ganesh/GrSurface.h"
36 #include "src/gpu/ganesh/GrSurfaceProxy.h"
37 #include "src/gpu/ganesh/GrSurfaceProxyPriv.h"
38 #include "src/gpu/ganesh/GrSurfaceProxyView.h"
39 #include "src/gpu/ganesh/GrTexture.h"
40 #include "src/gpu/ganesh/GrTextureProxyCacheAccess.h"
41 #include "src/gpu/ganesh/GrTextureRenderTargetProxy.h"
42 #include "src/gpu/ganesh/image/GrMippedBitmap.h"
43
44 #include <functional>
45 #include <memory>
46 #include <tuple>
47 #include <utility>
48
49 #define ASSERT_SINGLE_OWNER SKGPU_ASSERT_SINGLE_OWNER(fImageContext->priv().singleOwner())
50
51 GrProxyProvider::GrProxyProvider(GrImageContext* imageContext) : fImageContext(imageContext) {}
52
53 GrProxyProvider::~GrProxyProvider() {
54 if (this->renderingDirectly()) {
55 // In DDL-mode a proxy provider can still have extant uniquely keyed proxies (since
56 // they need their unique keys to, potentially, find a cached resource when the
57 // DDL is played) but, in non-DDL-mode they should all have been cleaned up by this point.
58 SkASSERT(!fUniquelyKeyedProxies.count());
59 }
60 }
61
62 bool GrProxyProvider::assignUniqueKeyToProxy(const skgpu::UniqueKey& key, GrTextureProxy* proxy) {
63 ASSERT_SINGLE_OWNER
64 SkASSERT(key.isValid());
65 if (this->isAbandoned() || !proxy) {
66 return false;
67 }
68
69 // Only the proxyProvider that created a proxy should be assigning unique keys to it.
70 SkASSERT(this->isDDLProvider() == proxy->creatingProvider());
71
72 #ifdef SK_DEBUG
73 {
74 auto direct = fImageContext->asDirectContext();
75 if (direct) {
76 GrResourceCache* resourceCache = direct->priv().getResourceCache();
77 // If there is already a GrResource with this key then the caller has violated the
78 // normal usage pattern of uniquely keyed resources (e.g., they have created one w/o
79 // first seeing if it already existed in the cache).
80 SkASSERT(!resourceCache->findAndRefUniqueResource(key));
81 }
82 }
83 #endif
84
85 SkASSERT(!fUniquelyKeyedProxies.find(key)); // multiple proxies can't get the same key
86
87 proxy->cacheAccess().setUniqueKey(this, key);
88 SkASSERT(proxy->getUniqueKey() == key);
89 fUniquelyKeyedProxies.add(proxy);
90 return true;
91 }
92
93 void GrProxyProvider::adoptUniqueKeyFromSurface(GrTextureProxy* proxy, const GrSurface* surf) {
94 SkASSERT(surf->getUniqueKey().isValid());
95 proxy->cacheAccess().setUniqueKey(this, surf->getUniqueKey());
96 SkASSERT(proxy->getUniqueKey() == surf->getUniqueKey());
97 // multiple proxies can't get the same key
98 SkASSERT(!fUniquelyKeyedProxies.find(surf->getUniqueKey()));
99 fUniquelyKeyedProxies.add(proxy);
100 }
101
102 void GrProxyProvider::removeUniqueKeyFromProxy(GrTextureProxy* proxy) {
103 ASSERT_SINGLE_OWNER
104 SkASSERT(proxy);
105 SkASSERT(proxy->getUniqueKey().isValid());
106
107 if (this->isAbandoned()) {
108 return;
109 }
110
111 this->processInvalidUniqueKey(proxy->getUniqueKey(), proxy, InvalidateGPUResource::kYes);
112 }
113
114 sk_sp<GrTextureProxy> GrProxyProvider::findProxyByUniqueKey(const skgpu::UniqueKey& key) {
115 ASSERT_SINGLE_OWNER
116
117 if (this->isAbandoned()) {
118 return nullptr;
119 }
120
121 GrTextureProxy* proxy = fUniquelyKeyedProxies.find(key);
122 if (proxy) {
123 return sk_ref_sp(proxy);
124 }
125 return nullptr;
126 }
127
128 ///////////////////////////////////////////////////////////////////////////////
129
130 #if defined(GPU_TEST_UTILS)
131 sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createInstantiatedProxy(
132 SkISize dimensions,
133 const GrBackendFormat& format,
134 GrRenderable renderable,
135 int renderTargetSampleCnt,
136 SkBackingFit fit,
137 skgpu::Budgeted budgeted,
138 GrProtected isProtected) {
139 ASSERT_SINGLE_OWNER
140 if (this->isAbandoned()) {
141 return nullptr;
142 }
143 auto direct = fImageContext->asDirectContext();
144 if (!direct) {
145 return nullptr;
146 }
147
148 if (this->caps()->isFormatCompressed(format)) {
149 // TODO: Allow this to go to GrResourceProvider::createCompressedTexture() once we no longer
150 // rely on GrColorType to get a swizzle for the proxy.
151 return nullptr;
152 }
153
154 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
155 sk_sp<GrTexture> tex;
156
157 if (SkBackingFit::kApprox == fit) {
158 tex = resourceProvider->createApproxTexture(
159 dimensions,
160 format,
161 format.textureType(),
162 renderable,
163 renderTargetSampleCnt,
164 isProtected,
165 /*label=*/"InstantiatedProxyViaApproxTexture_Test");
166 } else {
167 tex = resourceProvider->createTexture(dimensions,
168 format,
169 format.textureType(),
170 renderable,
171 renderTargetSampleCnt,
172 skgpu::Mipmapped::kNo,
173 budgeted,
174 isProtected,
175 /*label=*/"InstantiatedProxyViaTexture_Test");
176 }
177 if (!tex) {
178 return nullptr;
179 }
180
181 return this->createWrapped(std::move(tex), UseAllocator::kYes);
182 }
183
184 sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createInstantiatedProxy(
185 SkISize dimensions,
186 GrColorType colorType,
187 GrRenderable renderable,
188 int renderTargetSampleCnt,
189 SkBackingFit fit,
190 skgpu::Budgeted budgeted,
191 GrProtected isProtected) {
192 ASSERT_SINGLE_OWNER
193 if (this->isAbandoned()) {
194 return nullptr;
195 }
196 auto format = this->caps()->getDefaultBackendFormat(colorType, renderable);
197 return this->testingOnly_createInstantiatedProxy(dimensions,
198 format,
199 renderable,
200 renderTargetSampleCnt,
201 fit,
202 budgeted,
203 isProtected);
204 }
205
206 sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createWrapped(sk_sp<GrTexture> tex) {
207 return this->createWrapped(std::move(tex), UseAllocator::kYes);
208 }
209 #endif
210
211 sk_sp<GrTextureProxy> GrProxyProvider::createWrapped(sk_sp<GrTexture> tex,
212 UseAllocator useAllocator) {
213 #ifdef SK_DEBUG
214 if (tex->getUniqueKey().isValid()) {
215 SkASSERT(!this->findProxyByUniqueKey(tex->getUniqueKey()));
216 }
217 #endif
218
219 if (tex->asRenderTarget()) {
220 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
221 std::move(tex), useAllocator, this->isDDLProvider()));
222 } else {
223 return sk_sp<GrTextureProxy>(
224 new GrTextureProxy(std::move(tex), useAllocator, this->isDDLProvider()));
225 }
226 }
227
228 sk_sp<GrTextureProxy> GrProxyProvider::findOrCreateProxyByUniqueKey(const skgpu::UniqueKey& key,
229 UseAllocator useAllocator) {
230 ASSERT_SINGLE_OWNER
231
232 if (this->isAbandoned()) {
233 return nullptr;
234 }
235
236 sk_sp<GrTextureProxy> result = this->findProxyByUniqueKey(key);
237 if (result) {
238 return result;
239 }
240
241 auto direct = fImageContext->asDirectContext();
242 if (!direct) {
243 return nullptr;
244 }
245
246 GrResourceCache* resourceCache = direct->priv().getResourceCache();
247
248 GrGpuResource* resource = resourceCache->findAndRefUniqueResource(key);
249 if (!resource) {
250 return nullptr;
251 }
252
253 sk_sp<GrTexture> texture(static_cast<GrSurface*>(resource)->asTexture());
254 SkASSERT(texture);
255
256 result = this->createWrapped(std::move(texture), useAllocator);
257 SkASSERT(result->getUniqueKey() == key);
258 // createWrapped should've added this for us
259 SkASSERT(fUniquelyKeyedProxies.find(key));
260 return result;
261 }
262
263 GrSurfaceProxyView GrProxyProvider::findCachedProxyWithColorTypeFallback(
264 const skgpu::UniqueKey& key,
265 GrSurfaceOrigin origin,
266 GrColorType ct,
267 int sampleCnt) {
268 auto proxy = this->findOrCreateProxyByUniqueKey(key);
269 if (!proxy) {
270 return {};
271 }
272 const GrCaps* caps = fImageContext->priv().caps();
273
274 // Assume that we used a fallback color type if and only if the proxy is renderable.
275 if (proxy->asRenderTargetProxy()) {
276 GrBackendFormat expectedFormat;
277 std::tie(ct, expectedFormat) = caps->getFallbackColorTypeAndFormat(ct, sampleCnt);
278 SkASSERT(expectedFormat == proxy->backendFormat());
279 }
280 skgpu::Swizzle swizzle = caps->getReadSwizzle(proxy->backendFormat(), ct);
281 return {std::move(proxy), origin, swizzle};
282 }
283
284 sk_sp<GrTextureProxy> GrProxyProvider::createProxyFromBitmap(const GrMippedBitmap& bm,
285 skgpu::Mipmapped mipmapped,
286 SkBackingFit fit,
287 skgpu::Budgeted budgeted) {
288 ASSERT_SINGLE_OWNER
289 SkASSERT(fit == SkBackingFit::kExact || mipmapped == skgpu::Mipmapped::kNo);
290
291 if (this->isAbandoned()) {
292 return nullptr;
293 }
294
295 SkBitmap bitmap = bm.bitmap();
296 if (!SkImageInfoIsValid(bitmap.info())) {
297 return nullptr;
298 }
299
300 ATRACE_ANDROID_FRAMEWORK("Upload %sTexture [%dx%d]",
301 skgpu::Mipmapped::kYes == mipmapped ? "MipMap " : "",
302 bitmap.width(),
303 bitmap.height());
304
305 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
306 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
307 // upload of the data to the gpu can happen at anytime and the bitmap may change by then.
308 SkBitmap copyBitmap = bitmap;
309 sk_sp<const SkMipmap> mips = bm.mips();
310 if (!this->renderingDirectly() && !bitmap.isImmutable()) {
311 copyBitmap.allocPixels();
312 if (!bitmap.readPixels(copyBitmap.pixmap())) {
313 return nullptr;
314 }
315 if (mipmapped == skgpu::Mipmapped::kYes && bm.mips()) {
316 mips = sk_sp<SkMipmap>(SkMipmap::Build(copyBitmap.pixmap(),
317 /* factoryProc= */ nullptr,
318 /* computeContents= */ false));
319 if (mips) {
320 for (int i = 0; i < mips->countLevels(); ++i) {
321 SkMipmap::Level src, dst;
322 bm.mips()->getLevel(i, &src);
323 mips->getLevel(i, &dst);
324 src.fPixmap.readPixels(dst.fPixmap);
325 }
326 }
327 }
328 copyBitmap.setImmutable();
329 }
330
331 sk_sp<GrTextureProxy> proxy;
332 if (mipmapped == skgpu::Mipmapped::kNo ||
333 !SkMipmap::ComputeLevelCount(copyBitmap.dimensions())) {
334 proxy = this->createNonMippedProxyFromBitmap(copyBitmap, fit, budgeted);
335 } else {
336 proxy = this->createMippedProxyFromBitmap(GrMippedBitmap(copyBitmap, mips), budgeted);
337 }
338
339 if (!proxy) {
340 return nullptr;
341 }
342
343 auto direct = fImageContext->asDirectContext();
344 if (direct) {
345 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
346
347 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
348 // we're better off instantiating the proxy immediately here.
349 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
350 return nullptr;
351 }
352 }
353 return proxy;
354 }
355
356 sk_sp<GrTextureProxy> GrProxyProvider::createNonMippedProxyFromBitmap(const SkBitmap& bitmap,
357 SkBackingFit fit,
358 skgpu::Budgeted budgeted) {
359 auto dims = bitmap.dimensions();
360
361 auto colorType = SkColorTypeToGrColorType(bitmap.colorType());
362 GrBackendFormat format = this->caps()->getDefaultBackendFormat(colorType, GrRenderable::kNo);
363 if (!format.isValid()) {
364 return nullptr;
365 }
366
367 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
368 [bitmap](GrResourceProvider* resourceProvider, const LazySurfaceDesc& desc) {
369 SkASSERT(desc.fMipmapped == skgpu::Mipmapped::kNo);
370 GrMipLevel mipLevel = {bitmap.getPixels(), bitmap.rowBytes(), nullptr};
371 auto colorType = SkColorTypeToGrColorType(bitmap.colorType());
372 return LazyCallbackResult(resourceProvider->createTexture(desc.fDimensions,
373 desc.fFormat,
374 desc.fTextureType,
375 colorType,
376 desc.fRenderable,
377 desc.fSampleCnt,
378 desc.fBudgeted,
379 desc.fFit,
380 desc.fProtected,
381 mipLevel,
382 desc.fLabel));
383 },
384 format,
385 dims,
386 skgpu::Mipmapped::kNo,
387 GrMipmapStatus::kNotAllocated,
388 GrInternalSurfaceFlags::kNone,
389 fit,
390 budgeted,
391 GrProtected::kNo,
392 UseAllocator::kYes,
393 "ProxyProvider_CreateNonMippedProxyFromBitmap");
394
395 if (!proxy) {
396 return nullptr;
397 }
398 SkASSERT(proxy->dimensions() == bitmap.dimensions());
399 return proxy;
400 }
401
402 sk_sp<GrTextureProxy> GrProxyProvider::createMippedProxyFromBitmap(const GrMippedBitmap& bm,
403 skgpu::Budgeted budgeted) {
404 SkASSERT(this->caps()->mipmapSupport());
405 SkBitmap bitmap = bm.bitmap();
406 auto colorType = SkColorTypeToGrColorType(bitmap.colorType());
407 GrBackendFormat format = this->caps()->getDefaultBackendFormat(colorType, GrRenderable::kNo);
408 if (!format.isValid()) {
409 return nullptr;
410 }
411
412 sk_sp<const SkMipmap> mipmaps = bm.mips();
413 if (!mipmaps) {
414 mipmaps.reset(SkMipmap::Build(bitmap.pixmap(), /* factoryProc= */ nullptr));
415 if (!mipmaps) {
416 return nullptr;
417 }
418 }
419
420 auto dims = bitmap.dimensions();
421
422 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
423 [bitmap, mipmaps](GrResourceProvider* resourceProvider, const LazySurfaceDesc& desc) {
424 const int mipLevelCount = mipmaps->countLevels() + 1;
425 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
426 auto colorType = SkColorTypeToGrColorType(bitmap.colorType());
427
428 texels[0].fPixels = bitmap.getPixels();
429 texels[0].fRowBytes = bitmap.rowBytes();
430
431 for (int i = 1; i < mipLevelCount; ++i) {
432 SkMipmap::Level generatedMipLevel;
433 mipmaps->getLevel(i - 1, &generatedMipLevel);
434 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
435 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
436 SkASSERT(texels[i].fPixels);
437 SkASSERT(generatedMipLevel.fPixmap.colorType() == bitmap.colorType());
438 }
439 return LazyCallbackResult(resourceProvider->createTexture(desc.fDimensions,
440 desc.fFormat,
441 desc.fTextureType,
442 colorType,
443 GrRenderable::kNo,
444 1,
445 desc.fBudgeted,
446 skgpu::Mipmapped::kYes,
447 GrProtected::kNo,
448 texels.get(),
449 desc.fLabel));
450 },
451 format,
452 dims,
453 skgpu::Mipmapped::kYes,
454 GrMipmapStatus::kValid,
455 GrInternalSurfaceFlags::kNone,
456 SkBackingFit::kExact,
457 budgeted,
458 GrProtected::kNo,
459 UseAllocator::kYes,
460 "ProxyProvider_CreateMippedProxyFromBitmap");
461
462 if (!proxy) {
463 return nullptr;
464 }
465
466 SkASSERT(proxy->dimensions() == bitmap.dimensions());
467
468 return proxy;
469 }
470
471 sk_sp<GrTextureProxy> GrProxyProvider::createProxy(const GrBackendFormat& format,
472 SkISize dimensions,
473 GrRenderable renderable,
474 int renderTargetSampleCnt,
475 skgpu::Mipmapped mipmapped,
476 SkBackingFit fit,
477 skgpu::Budgeted budgeted,
478 GrProtected isProtected,
479 std::string_view label,
480 GrInternalSurfaceFlags surfaceFlags,
481 GrSurfaceProxy::UseAllocator useAllocator) {
482 ASSERT_SINGLE_OWNER
483 if (this->isAbandoned()) {
484 return nullptr;
485 }
486
487 const GrCaps* caps = this->caps();
488
489 if (caps->isFormatCompressed(format)) {
490 // Deferred proxies for compressed textures are not supported.
491 return nullptr;
492 }
493
494 if (skgpu::Mipmapped::kYes == mipmapped) {
495 // SkMipmap doesn't include the base level in the level count so we have to add 1
496 int mipCount = SkMipmap::ComputeLevelCount(dimensions) + 1;
497 if (1 == mipCount) {
498 mipmapped = skgpu::Mipmapped::kNo;
499 }
500 }
501
502 if (!caps->validateSurfaceParams(dimensions,
503 format,
504 renderable,
505 renderTargetSampleCnt,
506 mipmapped,
507 GrTextureType::k2D)) {
508 return nullptr;
509 }
510 GrMipmapStatus mipmapStatus = (skgpu::Mipmapped::kYes == mipmapped)
511 ? GrMipmapStatus::kDirty
512 : GrMipmapStatus::kNotAllocated;
513 if (renderable == GrRenderable::kYes) {
514 renderTargetSampleCnt = caps->getRenderTargetSampleCount(renderTargetSampleCnt, format);
515 SkASSERT(renderTargetSampleCnt);
516 GrInternalSurfaceFlags extraFlags = caps->getExtraSurfaceFlagsForDeferredRT();
517 // We know anything we instantiate later from this deferred path will be
518 // both texturable and renderable
519 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(*caps,
520 format,
521 dimensions,
522 renderTargetSampleCnt,
523 mipmapped,
524 mipmapStatus,
525 fit,
526 budgeted,
527 isProtected,
528 surfaceFlags | extraFlags,
529 useAllocator,
530 this->isDDLProvider(),
531 label));
532 }
533
534 return sk_sp<GrTextureProxy>(new GrTextureProxy(format,
535 dimensions,
536 mipmapped,
537 mipmapStatus,
538 fit,
539 budgeted,
540 isProtected,
541 surfaceFlags,
542 useAllocator,
543 this->isDDLProvider(),
544 label));
545 }
546
547 sk_sp<GrTextureProxy> GrProxyProvider::createCompressedTextureProxy(
548 SkISize dimensions,
549 skgpu::Budgeted budgeted,
550 skgpu::Mipmapped mipmapped,
551 GrProtected isProtected,
552 SkTextureCompressionType compressionType,
553 sk_sp<SkData> data) {
554 ASSERT_SINGLE_OWNER
555 if (this->isAbandoned()) {
556 return nullptr;
557 }
558
559 GrBackendFormat format = this->caps()->getBackendFormatFromCompressionType(compressionType);
560
561 if (!this->caps()->isFormatTexturable(format, GrTextureType::k2D)) {
562 return nullptr;
563 }
564
565 GrMipmapStatus mipmapStatus = (skgpu::Mipmapped::kYes == mipmapped)
566 ? GrMipmapStatus::kValid
567 : GrMipmapStatus::kNotAllocated;
568
569 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
570 [data](GrResourceProvider* resourceProvider, const LazySurfaceDesc& desc) {
571 return LazyCallbackResult(
572 resourceProvider->createCompressedTexture(desc.fDimensions,
573 desc.fFormat,
574 desc.fBudgeted,
575 desc.fMipmapped,
576 desc.fProtected,
577 data.get(),
578 desc.fLabel));
579 },
580 format,
581 dimensions,
582 mipmapped,
583 mipmapStatus,
584 GrInternalSurfaceFlags::kReadOnly,
585 SkBackingFit::kExact,
586 skgpu::Budgeted::kYes,
587 GrProtected::kNo,
588 UseAllocator::kYes,
589 "ProxyProvider_CreateCompressedTextureProxy");
590
591 if (!proxy) {
592 return nullptr;
593 }
594
595 auto direct = fImageContext->asDirectContext();
596 if (direct) {
597 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
598 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
599 // we're better off instantiating the proxy immediately here.
600 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
601 return nullptr;
602 }
603 }
604 return proxy;
605 }
606
607 sk_sp<GrTextureProxy> GrProxyProvider::wrapBackendTexture(
608 const GrBackendTexture& backendTex,
609 GrWrapOwnership ownership,
610 GrWrapCacheable cacheable,
611 GrIOType ioType,
612 sk_sp<skgpu::RefCntedCallback> releaseHelper) {
613 SkASSERT(ioType != kWrite_GrIOType);
614
615 if (this->isAbandoned()) {
616 return nullptr;
617 }
618
619 // This is only supported on a direct GrContext.
620 auto direct = fImageContext->asDirectContext();
621 if (!direct) {
622 return nullptr;
623 }
624
625 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
626
627 sk_sp<GrTexture> tex =
628 resourceProvider->wrapBackendTexture(backendTex, ownership, cacheable, ioType);
629 if (!tex) {
630 return nullptr;
631 }
632
633 if (releaseHelper) {
634 tex->setRelease(std::move(releaseHelper));
635 }
636
637 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
638 // Make sure we match how we created the proxy with skgpu::Budgeted::kNo
639 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
640
641 return sk_sp<GrTextureProxy>(
642 new GrTextureProxy(std::move(tex), UseAllocator::kNo, this->isDDLProvider()));
643 }
644
645 sk_sp<GrTextureProxy> GrProxyProvider::wrapCompressedBackendTexture(
646 const GrBackendTexture& beTex,
647 GrWrapOwnership ownership,
648 GrWrapCacheable cacheable,
649 sk_sp<skgpu::RefCntedCallback> releaseHelper) {
650 if (this->isAbandoned()) {
651 return nullptr;
652 }
653
654 // This is only supported on a direct GrContext.
655 auto direct = fImageContext->asDirectContext();
656 if (!direct) {
657 return nullptr;
658 }
659
660 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
661
662 sk_sp<GrTexture> tex = resourceProvider->wrapCompressedBackendTexture(beTex, ownership,
663 cacheable);
664 if (!tex) {
665 return nullptr;
666 }
667
668 if (releaseHelper) {
669 tex->setRelease(std::move(releaseHelper));
670 }
671
672 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
673 // Make sure we match how we created the proxy with skgpu::Budgeted::kNo
674 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
675
676 return sk_sp<GrTextureProxy>(
677 new GrTextureProxy(std::move(tex), UseAllocator::kNo, this->isDDLProvider()));
678 }
679
680 sk_sp<GrTextureProxy> GrProxyProvider::wrapRenderableBackendTexture(
681 const GrBackendTexture& backendTex,
682 int sampleCnt,
683 GrWrapOwnership ownership,
684 GrWrapCacheable cacheable,
685 sk_sp<skgpu::RefCntedCallback> releaseHelper) {
686 if (this->isAbandoned()) {
687 return nullptr;
688 }
689
690 // This is only supported on a direct GrContext.
691 auto direct = fImageContext->asDirectContext();
692 if (!direct) {
693 return nullptr;
694 }
695
696 const GrCaps* caps = this->caps();
697
698 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
699
700 sampleCnt = caps->getRenderTargetSampleCount(sampleCnt, backendTex.getBackendFormat());
701 SkASSERT(sampleCnt);
702
703 sk_sp<GrTexture> tex = resourceProvider->wrapRenderableBackendTexture(
704 backendTex, sampleCnt, ownership, cacheable);
705 if (!tex) {
706 return nullptr;
707 }
708
709 if (releaseHelper) {
710 tex->setRelease(std::move(releaseHelper));
711 }
712
713 SkASSERT(tex->asRenderTarget()); // A GrTextureRenderTarget
714 // Make sure we match how we created the proxy with skgpu::Budgeted::kNo
715 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
716
717 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
718 std::move(tex), UseAllocator::kNo, this->isDDLProvider()));
719 }
720
721 GrResourceProvider* GrProxyProvider::resourceProvider() const {
722 GrDirectContext* direct = fImageContext->asDirectContext();
723 if (!direct) {
724 return nullptr;
725 }
726
727 return direct->priv().resourceProvider();
728 }
729
730 sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget(
731 const GrBackendRenderTarget& backendRT,
732 sk_sp<skgpu::RefCntedCallback> releaseHelper) {
733 if (this->isAbandoned()) {
734 return nullptr;
735 }
736
737 // This is only supported on a direct GrContext.
738 auto direct = fImageContext->asDirectContext();
739 if (!direct) {
740 return nullptr;
741 }
742
743 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
744
745 sk_sp<GrRenderTarget> rt = resourceProvider->wrapBackendRenderTarget(backendRT);
746 if (!rt) {
747 return nullptr;
748 }
749
750 if (releaseHelper) {
751 rt->setRelease(std::move(releaseHelper));
752 }
753
754 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
755 SkASSERT(!rt->getUniqueKey().isValid());
756 // Make sure we match how we created the proxy with skgpu::Budgeted::kNo
757 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
758
759 return sk_sp<GrRenderTargetProxy>(
760 new GrRenderTargetProxy(std::move(rt), UseAllocator::kNo, {}));
761 }
762
763 sk_sp<GrTextureProxy> GrProxyProvider::CreatePromiseProxy(GrContextThreadSafeProxy* threadSafeProxy,
764 LazyInstantiateCallback&& callback,
765 const GrBackendFormat& format,
766 SkISize dimensions,
767 skgpu::Mipmapped mipmapped) {
768 if (threadSafeProxy->priv().abandoned()) {
769 return nullptr;
770 }
771 SkASSERT((dimensions.fWidth <= 0 && dimensions.fHeight <= 0) ||
772 (dimensions.fWidth > 0 && dimensions.fHeight > 0));
773
774 if (dimensions.fWidth > threadSafeProxy->priv().caps()->maxTextureSize() ||
775 dimensions.fHeight > threadSafeProxy->priv().caps()->maxTextureSize()) {
776 return nullptr;
777 }
778 if (!threadSafeProxy->priv().caps()->isFormatTexturable(format, format.textureType())) {
779 return nullptr;
780 }
781 // Ganesh assumes that, when wrapping a mipmapped backend texture from a client, that its
782 // mipmaps are fully fleshed out.
783 GrMipmapStatus mipmapStatus = (skgpu::Mipmapped::kYes == mipmapped)
784 ? GrMipmapStatus::kValid
785 : GrMipmapStatus::kNotAllocated;
786
787 // We pass kReadOnly here since we should treat content of the client's texture as immutable.
788 // The promise API provides no way for the client to indicate that the texture is protected.
789 auto proxy = sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(callback),
790 format,
791 dimensions,
792 mipmapped,
793 mipmapStatus,
794 SkBackingFit::kExact,
795 skgpu::Budgeted::kNo,
796 GrProtected::kNo,
797 GrInternalSurfaceFlags::kReadOnly,
798 GrSurfaceProxy::UseAllocator::kYes,
799 GrDDLProvider::kYes,
800 /*label=*/"PromiseProxy"));
801 proxy->priv().setIsPromiseProxy();
802 return proxy;
803 }
804
805 sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
806 const GrBackendFormat& format,
807 SkISize dimensions,
808 skgpu::Mipmapped mipmapped,
809 GrMipmapStatus mipmapStatus,
810 GrInternalSurfaceFlags surfaceFlags,
811 SkBackingFit fit,
812 skgpu::Budgeted budgeted,
813 GrProtected isProtected,
814 GrSurfaceProxy::UseAllocator useAllocator,
815 std::string_view label) {
816 ASSERT_SINGLE_OWNER
817 if (this->isAbandoned()) {
818 return nullptr;
819 }
820 SkASSERT((dimensions.fWidth <= 0 && dimensions.fHeight <= 0) ||
821 (dimensions.fWidth > 0 && dimensions.fHeight > 0));
822
823 if (!format.isValid() || format.backend() != fImageContext->backend()) {
824 return nullptr;
825 }
826
827 if (dimensions.fWidth > this->caps()->maxTextureSize() ||
828 dimensions.fHeight > this->caps()->maxTextureSize()) {
829 return nullptr;
830 }
831
832 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(callback),
833 format,
834 dimensions,
835 mipmapped,
836 mipmapStatus,
837 fit,
838 budgeted,
839 isProtected,
840 surfaceFlags,
841 useAllocator,
842 this->isDDLProvider(),
843 label));
844 }
845
846 sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy(
847 LazyInstantiateCallback&& callback,
848 const GrBackendFormat& format,
849 SkISize dimensions,
850 int sampleCnt,
851 GrInternalSurfaceFlags surfaceFlags,
852 const TextureInfo* textureInfo,
853 GrMipmapStatus mipmapStatus,
854 SkBackingFit fit,
855 skgpu::Budgeted budgeted,
856 GrProtected isProtected,
857 bool wrapsVkSecondaryCB,
858 UseAllocator useAllocator) {
859 ASSERT_SINGLE_OWNER
860 if (this->isAbandoned()) {
861 return nullptr;
862 }
863 SkASSERT((dimensions.fWidth <= 0 && dimensions.fHeight <= 0) ||
864 (dimensions.fWidth > 0 && dimensions.fHeight > 0));
865
866 if (dimensions.fWidth > this->caps()->maxRenderTargetSize() ||
867 dimensions.fHeight > this->caps()->maxRenderTargetSize()) {
868 return nullptr;
869 }
870
871 if (textureInfo) {
872 // Wrapped vulkan secondary command buffers don't support texturing since we won't have an
873 // actual VkImage to texture from.
874 SkASSERT(!wrapsVkSecondaryCB);
875 return sk_sp<GrRenderTargetProxy>(new GrTextureRenderTargetProxy(
876 *this->caps(),
877 std::move(callback),
878 format,
879 dimensions,
880 sampleCnt,
881 textureInfo->fMipmapped,
882 mipmapStatus,
883 fit,
884 budgeted,
885 isProtected,
886 surfaceFlags,
887 useAllocator,
888 this->isDDLProvider(),
889 /*label=*/"TextureRenderTarget_LazyRenderTargetProxy"));
890 }
891
892 GrRenderTargetProxy::WrapsVkSecondaryCB vkSCB =
893 wrapsVkSecondaryCB ? GrRenderTargetProxy::WrapsVkSecondaryCB::kYes
894 : GrRenderTargetProxy::WrapsVkSecondaryCB::kNo;
895
896 return sk_sp<GrRenderTargetProxy>(
897 new GrRenderTargetProxy(std::move(callback),
898 format,
899 dimensions,
900 sampleCnt,
901 fit,
902 budgeted,
903 isProtected,
904 surfaceFlags,
905 useAllocator,
906 vkSCB,
907 /*label=*/"RenderTargetProxy_LazyRenderTargetProxy"));
908 }
909
910 sk_sp<GrTextureProxy> GrProxyProvider::MakeFullyLazyProxy(LazyInstantiateCallback&& callback,
911 const GrBackendFormat& format,
912 GrRenderable renderable,
913 int renderTargetSampleCnt,
914 GrProtected isProtected,
915 const GrCaps& caps,
916 UseAllocator useAllocator) {
917 if (!format.isValid()) {
918 return nullptr;
919 }
920
921 SkASSERT(renderTargetSampleCnt == 1 || renderable == GrRenderable::kYes);
922 // TODO: If we ever have callers requesting specific surface flags then we shouldn't use the
923 // extra deferred flags here. Instead those callers should all pass in exactly what they want.
924 // However, as of today all uses of this essentially create a deferred proxy in the end.
925 GrInternalSurfaceFlags surfaceFlags = caps.getExtraSurfaceFlagsForDeferredRT();
926
927 // MakeFullyLazyProxy is only called at flush time so we know these texture proxies are
928 // not being created by a DDL provider.
929 static constexpr SkISize kLazyDims = {-1, -1};
930 if (GrRenderable::kYes == renderable) {
931 return sk_sp<GrTextureProxy>(
932 new GrTextureRenderTargetProxy(caps,
933 std::move(callback),
934 format,
935 kLazyDims,
936 renderTargetSampleCnt,
937 skgpu::Mipmapped::kNo,
938 GrMipmapStatus::kNotAllocated,
939 SkBackingFit::kApprox,
940 skgpu::Budgeted::kYes,
941 isProtected,
942 surfaceFlags,
943 useAllocator,
944 GrDDLProvider::kNo,
945 /*label=*/"TextureRenderTarget_FullyLazyProxy"));
946 } else {
947 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(callback),
948 format,
949 kLazyDims,
950 skgpu::Mipmapped::kNo,
951 GrMipmapStatus::kNotAllocated,
952 SkBackingFit::kApprox,
953 skgpu::Budgeted::kYes,
954 isProtected,
955 surfaceFlags,
956 useAllocator,
957 GrDDLProvider::kNo,
958 /*label=*/"Texture_FullyLazyProxy"));
959 }
960 }
961
962 void GrProxyProvider::processInvalidUniqueKey(const skgpu::UniqueKey& key,
963 GrTextureProxy* proxy,
964 InvalidateGPUResource invalidateGPUResource) {
965 this->processInvalidUniqueKeyImpl(key, proxy, invalidateGPUResource, RemoveTableEntry::kYes);
966 }
967
968 void GrProxyProvider::processInvalidUniqueKeyImpl(const skgpu::UniqueKey& key,
969 GrTextureProxy* proxy,
970 InvalidateGPUResource invalidateGPUResource,
971 RemoveTableEntry removeTableEntry) {
972 SkASSERT(key.isValid());
973
974 if (!proxy) {
975 proxy = fUniquelyKeyedProxies.find(key);
976 }
977 SkASSERT(!proxy || proxy->getUniqueKey() == key);
978
979 // Locate the corresponding GrGpuResource (if it needs to be invalidated) before clearing the
980 // proxy's unique key. We must do it in this order because 'key' may alias the proxy's key.
981 sk_sp<GrGpuResource> invalidGpuResource;
982 if (InvalidateGPUResource::kYes == invalidateGPUResource) {
983 auto direct = fImageContext->asDirectContext();
984 if (direct) {
985 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
986 invalidGpuResource = resourceProvider->findByUniqueKey<GrGpuResource>(key);
987 }
988 SkASSERT(!invalidGpuResource || invalidGpuResource->getUniqueKey() == key);
989 }
990
991 // Note: this method is called for the whole variety of GrGpuResources so often 'key'
992 // will not be in 'fUniquelyKeyedProxies'.
993 if (proxy) {
994 if (removeTableEntry == RemoveTableEntry::kYes) {
995 fUniquelyKeyedProxies.remove(key);
996 }
997 proxy->cacheAccess().clearUniqueKey();
998 }
999
1000 if (invalidGpuResource) {
1001 invalidGpuResource->resourcePriv().removeUniqueKey();
1002 }
1003 }
1004
1005 GrDDLProvider GrProxyProvider::isDDLProvider() const {
1006 return fImageContext->asDirectContext() ? GrDDLProvider::kNo : GrDDLProvider::kYes;
1007 }
1008
1009 uint32_t GrProxyProvider::contextID() const {
1010 return fImageContext->priv().contextID();
1011 }
1012
1013 const GrCaps* GrProxyProvider::caps() const {
1014 return fImageContext->priv().caps();
1015 }
1016
1017 sk_sp<const GrCaps> GrProxyProvider::refCaps() const {
1018 return fImageContext->priv().refCaps();
1019 }
1020
1021 bool GrProxyProvider::isAbandoned() const {
1022 return fImageContext->priv().abandoned();
1023 }
1024
1025 void GrProxyProvider::orphanAllUniqueKeys() {
1026 fUniquelyKeyedProxies.foreach([&](GrTextureProxy* proxy){
1027 proxy->fProxyProvider = nullptr;
1028 });
1029 }
1030
1031 void GrProxyProvider::removeAllUniqueKeys() {
1032 fUniquelyKeyedProxies.foreach([&](GrTextureProxy* proxy){
1033 // It's not safe to remove table entries while iterating with foreach(),
1034 // but since we're going to remove them all anyway, simply save that for the end.
1035 this->processInvalidUniqueKeyImpl(proxy->getUniqueKey(), proxy,
1036 InvalidateGPUResource::kNo,
1037 RemoveTableEntry::kNo);
1038 });
1039 // Removing all those table entries is safe now.
1040 fUniquelyKeyedProxies.reset();
1041 }
1042
1043 bool GrProxyProvider::renderingDirectly() const {
1044 return fImageContext->asDirectContext();
1045 }