blob: bc4e3ead64cf3da2e0896818ec8639728424bb05 [file] [log] [blame]
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrProxyProvider.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkBitmap.h"
11#include "include/core/SkImage.h"
12#include "include/gpu/GrContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/private/GrImageContext.h"
14#include "include/private/GrResourceKey.h"
15#include "include/private/GrSingleOwner.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/private/SkImageInfoPriv.h"
17#include "src/core/SkAutoPixmapStorage.h"
Robert Phillips99dead92020-01-27 16:11:57 -050018#include "src/core/SkCompressedDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/core/SkImagePriv.h"
20#include "src/core/SkMipMap.h"
21#include "src/core/SkTraceEvent.h"
22#include "src/gpu/GrCaps.h"
23#include "src/gpu/GrContextPriv.h"
24#include "src/gpu/GrImageContextPriv.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040025#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/gpu/GrResourceProvider.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040027#include "src/gpu/GrSurfaceProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/gpu/GrSurfaceProxyPriv.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000029#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/gpu/GrTextureProxyCacheAccess.h"
31#include "src/gpu/GrTextureRenderTargetProxy.h"
32#include "src/gpu/SkGr.h"
33#include "src/image/SkImage_Base.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050034
35#define ASSERT_SINGLE_OWNER \
Robert Phillipsa41c6852019-02-07 10:44:10 -050036 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fImageContext->priv().singleOwner());)
Robert Phillips1afd4cd2018-01-08 13:40:32 -050037
Robert Phillipsa9162df2019-02-11 14:12:03 -050038GrProxyProvider::GrProxyProvider(GrImageContext* imageContext) : fImageContext(imageContext) {}
Robert Phillips1afd4cd2018-01-08 13:40:32 -050039
40GrProxyProvider::~GrProxyProvider() {
Robert Phillipsa41c6852019-02-07 10:44:10 -050041 if (this->renderingDirectly()) {
Robert Phillips0790f8a2018-09-18 13:11:03 -040042 // In DDL-mode a proxy provider can still have extant uniquely keyed proxies (since
43 // they need their unique keys to, potentially, find a cached resource when the
44 // DDL is played) but, in non-DDL-mode they should all have been cleaned up by this point.
45 SkASSERT(!fUniquelyKeyedProxies.count());
46 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050047}
48
Robert Phillipsadbe1322018-01-17 13:35:46 -050049bool GrProxyProvider::assignUniqueKeyToProxy(const GrUniqueKey& key, GrTextureProxy* proxy) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050050 ASSERT_SINGLE_OWNER
51 SkASSERT(key.isValid());
52 if (this->isAbandoned() || !proxy) {
Robert Phillipsadbe1322018-01-17 13:35:46 -050053 return false;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050054 }
55
Robert Phillipsa41c6852019-02-07 10:44:10 -050056#ifdef SK_DEBUG
57 {
58 GrContext* direct = fImageContext->priv().asDirectContext();
59 if (direct) {
60 GrResourceCache* resourceCache = direct->priv().getResourceCache();
61 // If there is already a GrResource with this key then the caller has violated the
62 // normal usage pattern of uniquely keyed resources (e.g., they have created one w/o
63 // first seeing if it already existed in the cache).
64 SkASSERT(!resourceCache->findAndRefUniqueResource(key));
65 }
66 }
67#endif
Robert Phillips1afd4cd2018-01-08 13:40:32 -050068
Robert Phillips1afd4cd2018-01-08 13:40:32 -050069 SkASSERT(!fUniquelyKeyedProxies.find(key)); // multiple proxies can't get the same key
70
71 proxy->cacheAccess().setUniqueKey(this, key);
72 SkASSERT(proxy->getUniqueKey() == key);
73 fUniquelyKeyedProxies.add(proxy);
Robert Phillipsadbe1322018-01-17 13:35:46 -050074 return true;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050075}
76
77void GrProxyProvider::adoptUniqueKeyFromSurface(GrTextureProxy* proxy, const GrSurface* surf) {
78 SkASSERT(surf->getUniqueKey().isValid());
79 proxy->cacheAccess().setUniqueKey(this, surf->getUniqueKey());
80 SkASSERT(proxy->getUniqueKey() == surf->getUniqueKey());
81 // multiple proxies can't get the same key
82 SkASSERT(!fUniquelyKeyedProxies.find(surf->getUniqueKey()));
83 fUniquelyKeyedProxies.add(proxy);
84}
85
Chris Dalton2de13dd2019-01-03 15:11:59 -070086void GrProxyProvider::removeUniqueKeyFromProxy(GrTextureProxy* proxy) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050087 ASSERT_SINGLE_OWNER
Chris Dalton2de13dd2019-01-03 15:11:59 -070088 SkASSERT(proxy);
89 SkASSERT(proxy->getUniqueKey().isValid());
90
91 if (this->isAbandoned()) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050092 return;
93 }
Robert Phillips0790f8a2018-09-18 13:11:03 -040094
Chris Dalton2de13dd2019-01-03 15:11:59 -070095 this->processInvalidUniqueKey(proxy->getUniqueKey(), proxy, InvalidateGPUResource::kYes);
Robert Phillips1afd4cd2018-01-08 13:40:32 -050096}
97
Greg Daniel3a365112020-02-14 10:47:18 -050098sk_sp<GrTextureProxy> GrProxyProvider::findProxyByUniqueKey(const GrUniqueKey& key) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050099 ASSERT_SINGLE_OWNER
100
101 if (this->isAbandoned()) {
102 return nullptr;
103 }
104
Brian Salomon01ceae92019-04-02 11:49:54 -0400105 GrTextureProxy* proxy = fUniquelyKeyedProxies.find(key);
Brian Salomon01ceae92019-04-02 11:49:54 -0400106 if (proxy) {
Robert Phillipse5f73282019-06-18 17:15:04 -0400107 return sk_ref_sp(proxy);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500108 }
Robert Phillipse5f73282019-06-18 17:15:04 -0400109 return nullptr;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500110}
111
Robert Phillipsa41c6852019-02-07 10:44:10 -0500112///////////////////////////////////////////////////////////////////////////////
113
114#if GR_TEST_UTILS
115sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createInstantiatedProxy(
Brian Salomona56a7462020-02-07 14:17:25 -0500116 SkISize dimensions,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400117 GrColorType colorType,
118 const GrBackendFormat& format,
119 GrRenderable renderable,
120 int renderTargetSampleCnt,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400121 SkBackingFit fit,
122 SkBudgeted budgeted,
123 GrProtected isProtected) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500124 ASSERT_SINGLE_OWNER
125 if (this->isAbandoned()) {
126 return nullptr;
127 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500128 GrContext* direct = fImageContext->priv().asDirectContext();
129 if (!direct) {
130 return nullptr;
131 }
132
Brian Salomon4eb38b72019-08-05 12:58:39 -0400133 if (this->caps()->isFormatCompressed(format)) {
134 // TODO: Allow this to go to GrResourceProvider::createCompressedTexture() once we no longer
Greg Daniel4cb29332020-01-23 10:07:02 -0500135 // rely on GrColorType to get a swizzle for the proxy.
Brian Salomon4eb38b72019-08-05 12:58:39 -0400136 return nullptr;
137 }
Brian Salomon4eb38b72019-08-05 12:58:39 -0400138
Robert Phillipsa41c6852019-02-07 10:44:10 -0500139 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
140 sk_sp<GrTexture> tex;
141
142 if (SkBackingFit::kApprox == fit) {
Brian Salomona56a7462020-02-07 14:17:25 -0500143 tex = resourceProvider->createApproxTexture(dimensions, format, renderable,
144 renderTargetSampleCnt, isProtected);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500145 } else {
Brian Salomona56a7462020-02-07 14:17:25 -0500146 tex = resourceProvider->createTexture(dimensions, format, renderable, renderTargetSampleCnt,
Brian Salomona90382f2019-09-17 09:01:56 -0400147 GrMipMapped::kNo, budgeted, isProtected);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500148 }
149 if (!tex) {
150 return nullptr;
151 }
152
Greg Daniel3a365112020-02-14 10:47:18 -0500153 return this->createWrapped(std::move(tex), colorType, UseAllocator::kYes);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500154}
155
Brian Salomon4eb38b72019-08-05 12:58:39 -0400156sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createInstantiatedProxy(
Brian Salomona56a7462020-02-07 14:17:25 -0500157 SkISize dimensions,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400158 GrColorType colorType,
159 GrRenderable renderable,
160 int renderTargetSampleCnt,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400161 SkBackingFit fit,
162 SkBudgeted budgeted,
163 GrProtected isProtected) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500164 ASSERT_SINGLE_OWNER
165 if (this->isAbandoned()) {
166 return nullptr;
167 }
Brian Salomon4eb38b72019-08-05 12:58:39 -0400168 auto format = this->caps()->getDefaultBackendFormat(colorType, renderable);
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400169 return this->testingOnly_createInstantiatedProxy(dimensions,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400170 colorType,
171 format,
172 renderable,
173 renderTargetSampleCnt,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400174 fit,
175 budgeted,
176 isProtected);
177}
178
Robert Phillipsa41c6852019-02-07 10:44:10 -0500179sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createWrapped(sk_sp<GrTexture> tex,
Greg Daniel3a365112020-02-14 10:47:18 -0500180 GrColorType colorType) {
181 return this->createWrapped(std::move(tex), colorType, UseAllocator::kYes);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500182}
183#endif
184
Brian Salomonbeb7f522019-08-30 16:19:42 -0400185sk_sp<GrTextureProxy> GrProxyProvider::createWrapped(sk_sp<GrTexture> tex,
186 GrColorType colorType,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400187 UseAllocator useAllocator) {
Robert Phillipsadbe1322018-01-17 13:35:46 -0500188#ifdef SK_DEBUG
189 if (tex->getUniqueKey().isValid()) {
Greg Daniel3a365112020-02-14 10:47:18 -0500190 SkASSERT(!this->findProxyByUniqueKey(tex->getUniqueKey()));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500191 }
192#endif
Greg Daniel14b57212019-12-17 16:18:06 -0500193 GrSwizzle readSwizzle = this->caps()->getReadSwizzle(tex->backendFormat(), colorType);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500194
195 if (tex->asRenderTarget()) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400196 return sk_sp<GrTextureProxy>(
Greg Daniel3a365112020-02-14 10:47:18 -0500197 new GrTextureRenderTargetProxy(std::move(tex), readSwizzle, useAllocator));
198 } else {
199 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), readSwizzle, useAllocator));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500200 }
201}
202
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500203sk_sp<GrTextureProxy> GrProxyProvider::findOrCreateProxyByUniqueKey(const GrUniqueKey& key,
Brian Salomon2af3e702019-08-11 19:10:31 -0400204 GrColorType colorType,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400205 UseAllocator useAllocator) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500206 ASSERT_SINGLE_OWNER
207
208 if (this->isAbandoned()) {
209 return nullptr;
210 }
211
Greg Daniel3a365112020-02-14 10:47:18 -0500212 sk_sp<GrTextureProxy> result = this->findProxyByUniqueKey(key);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500213 if (result) {
214 return result;
215 }
216
Robert Phillipsa41c6852019-02-07 10:44:10 -0500217 GrContext* direct = fImageContext->priv().asDirectContext();
218 if (!direct) {
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500219 return nullptr;
220 }
221
Robert Phillipsa41c6852019-02-07 10:44:10 -0500222 GrResourceCache* resourceCache = direct->priv().getResourceCache();
223
224 GrGpuResource* resource = resourceCache->findAndRefUniqueResource(key);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500225 if (!resource) {
226 return nullptr;
227 }
228
229 sk_sp<GrTexture> texture(static_cast<GrSurface*>(resource)->asTexture());
230 SkASSERT(texture);
231
Greg Daniel3a365112020-02-14 10:47:18 -0500232 result = this->createWrapped(std::move(texture), colorType, useAllocator);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500233 SkASSERT(result->getUniqueKey() == key);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500234 // createWrapped should've added this for us
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500235 SkASSERT(fUniquelyKeyedProxies.find(key));
Greg Daniel87506ab2020-02-12 13:05:42 -0500236 SkASSERT(result->textureSwizzleDoNotUse() ==
Greg Daniel14b57212019-12-17 16:18:06 -0500237 this->caps()->getReadSwizzle(result->backendFormat(), colorType));
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500238 return result;
239}
240
Brian Osmande496652019-03-22 13:42:33 -0400241sk_sp<GrTextureProxy> GrProxyProvider::createProxyFromBitmap(const SkBitmap& bitmap,
Greg Daniel6f5441a2020-01-28 17:02:49 -0500242 GrMipMapped mipMapped,
243 SkBackingFit fit) {
Brian Osman412674f2019-02-07 15:34:58 -0500244 ASSERT_SINGLE_OWNER
Greg Daniel6f5441a2020-01-28 17:02:49 -0500245 SkASSERT(fit == SkBackingFit::kExact || mipMapped == GrMipMapped::kNo);
Brian Osman412674f2019-02-07 15:34:58 -0500246
247 if (this->isAbandoned()) {
248 return nullptr;
249 }
250
Brian Osman2b23c4b2018-06-01 12:25:08 -0400251 if (!SkImageInfoIsValid(bitmap.info())) {
Greg Daniela4ead652018-02-07 10:21:48 -0500252 return nullptr;
253 }
254
Brian Osmande496652019-03-22 13:42:33 -0400255 ATRACE_ANDROID_FRAMEWORK("Upload %sTexture [%ux%u]",
256 GrMipMapped::kYes == mipMapped ? "MipMap " : "",
257 bitmap.width(), bitmap.height());
Greg Daniela4ead652018-02-07 10:21:48 -0500258
259 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
260 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
261 // upload of the data to the gpu can happen at anytime and the bitmap may change by then.
Greg Daniel6f5441a2020-01-28 17:02:49 -0500262 SkBitmap copyBitmap = bitmap;
263 if (!this->renderingDirectly() && !bitmap.isImmutable()) {
264 copyBitmap.allocPixels();
265 if (!bitmap.readPixels(copyBitmap.pixmap())) {
266 return nullptr;
267 }
268 copyBitmap.setImmutable();
Greg Daniela4ead652018-02-07 10:21:48 -0500269 }
270
Greg Daniel6f5441a2020-01-28 17:02:49 -0500271 GrColorType grCT = SkColorTypeToGrColorType(copyBitmap.info().colorType());
Greg Danielce3ddaa2020-01-22 16:58:15 -0500272 GrBackendFormat format = this->caps()->getDefaultBackendFormat(grCT, GrRenderable::kNo);
Robert Phillips0a15cc62019-07-30 12:49:10 -0400273 if (!format.isValid()) {
Greg Daniel82c6b102020-01-21 10:33:22 -0500274 return nullptr;
Greg Daniel3fc5df42019-06-14 09:42:17 -0400275 }
276
Greg Daniel6f5441a2020-01-28 17:02:49 -0500277 sk_sp<GrTextureProxy> proxy;
278 if (mipMapped == GrMipMapped::kNo ||
279 0 == SkMipMap::ComputeLevelCount(copyBitmap.width(), copyBitmap.height())) {
280 proxy = this->createNonMippedProxyFromBitmap(copyBitmap, fit, format, grCT);
281 } else {
282 proxy = this->createMippedProxyFromBitmap(copyBitmap, format, grCT);
283 }
284
285 if (!proxy) {
286 return nullptr;
287 }
288
289 GrContext* direct = fImageContext->priv().asDirectContext();
290 if (direct) {
291 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
292
293 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
294 // we're better off instantiating the proxy immediately here.
295 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
296 return nullptr;
297 }
298 }
299 return proxy;
300}
301
302sk_sp<GrTextureProxy> GrProxyProvider::createNonMippedProxyFromBitmap(const SkBitmap& bitmap,
303 SkBackingFit fit,
304 const GrBackendFormat& format,
305 GrColorType colorType) {
Greg Daniel6f5441a2020-01-28 17:02:49 -0500306 GrSwizzle swizzle = this->caps()->getReadSwizzle(format, colorType);
Brian Salomona56a7462020-02-07 14:17:25 -0500307 auto dims = bitmap.dimensions();
Greg Daniel6f5441a2020-01-28 17:02:49 -0500308
309 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomona56a7462020-02-07 14:17:25 -0500310 [dims, format, bitmap, fit, colorType](GrResourceProvider* resourceProvider) {
Greg Daniel6f5441a2020-01-28 17:02:49 -0500311 GrMipLevel mipLevel = { bitmap.getPixels(), bitmap.rowBytes() };
312
313 return LazyCallbackResult(resourceProvider->createTexture(
Brian Salomona56a7462020-02-07 14:17:25 -0500314 dims, format, colorType, GrRenderable::kNo, 1, SkBudgeted::kYes, fit,
Greg Daniel6f5441a2020-01-28 17:02:49 -0500315 GrProtected::kNo, mipLevel));
316 },
Greg Daniel3a365112020-02-14 10:47:18 -0500317 format, dims, swizzle, GrRenderable::kNo, 1, GrMipMapped::kNo,
Brian Salomona56a7462020-02-07 14:17:25 -0500318 GrMipMapsStatus::kNotAllocated, GrInternalSurfaceFlags::kNone, fit, SkBudgeted::kYes,
319 GrProtected::kNo, UseAllocator::kYes);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500320
321 if (!proxy) {
322 return nullptr;
323 }
Brian Salomona56a7462020-02-07 14:17:25 -0500324 SkASSERT(proxy->dimensions() == bitmap.dimensions());
Greg Daniel6f5441a2020-01-28 17:02:49 -0500325 return proxy;
326}
327
328sk_sp<GrTextureProxy> GrProxyProvider::createMippedProxyFromBitmap(const SkBitmap& bitmap,
329 const GrBackendFormat& format,
330 GrColorType colorType) {
331 SkASSERT(this->caps()->mipMapSupport());
332
Greg Daniel6f5441a2020-01-28 17:02:49 -0500333
334 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(bitmap.pixmap(), nullptr));
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400335 if (!mipmaps) {
336 return nullptr;
337 }
338
Greg Daniel6f5441a2020-01-28 17:02:49 -0500339 GrSwizzle readSwizzle = this->caps()->getReadSwizzle(format, colorType);
Brian Salomona56a7462020-02-07 14:17:25 -0500340 auto dims = bitmap.dimensions();
Greg Danielce3ddaa2020-01-22 16:58:15 -0500341
Greg Daniela4ead652018-02-07 10:21:48 -0500342 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomona56a7462020-02-07 14:17:25 -0500343 [dims, format, bitmap, mipmaps](GrResourceProvider* resourceProvider) {
Brian Osman1b97f132018-09-13 17:33:48 +0000344 const int mipLevelCount = mipmaps->countLevels() + 1;
345 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
346
Greg Daniel6f5441a2020-01-28 17:02:49 -0500347 texels[0].fPixels = bitmap.getPixels();
348 texels[0].fRowBytes = bitmap.rowBytes();
Greg Daniela4ead652018-02-07 10:21:48 -0500349
Greg Daniel6f5441a2020-01-28 17:02:49 -0500350 auto colorType = SkColorTypeToGrColorType(bitmap.colorType());
Greg Daniela4ead652018-02-07 10:21:48 -0500351 for (int i = 1; i < mipLevelCount; ++i) {
352 SkMipMap::Level generatedMipLevel;
353 mipmaps->getLevel(i - 1, &generatedMipLevel);
354 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
355 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
356 SkASSERT(texels[i].fPixels);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500357 SkASSERT(generatedMipLevel.fPixmap.colorType() == bitmap.colorType());
Greg Daniela4ead652018-02-07 10:21:48 -0500358 }
Brian Salomonbeb7f522019-08-30 16:19:42 -0400359 return LazyCallbackResult(resourceProvider->createTexture(
Brian Salomona56a7462020-02-07 14:17:25 -0500360 dims, format, colorType, GrRenderable::kNo, 1, SkBudgeted::kYes,
Brian Salomona90382f2019-09-17 09:01:56 -0400361 GrProtected::kNo, texels.get(), mipLevelCount));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500362 },
Greg Daniel3a365112020-02-14 10:47:18 -0500363 format, dims, readSwizzle, GrRenderable::kNo, 1, GrMipMapped::kYes,
364 GrMipMapsStatus::kValid, GrInternalSurfaceFlags::kNone, SkBackingFit::kExact,
365 SkBudgeted::kYes, GrProtected::kNo, UseAllocator::kYes);
Greg Daniela4ead652018-02-07 10:21:48 -0500366
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400367 if (!proxy) {
368 return nullptr;
369 }
370
Brian Salomona56a7462020-02-07 14:17:25 -0500371 SkASSERT(proxy->dimensions() == bitmap.dimensions());
Greg Daniel6f5441a2020-01-28 17:02:49 -0500372
Greg Daniela4ead652018-02-07 10:21:48 -0500373 return proxy;
374}
375
Greg Daniel4065d452018-11-16 15:43:41 -0500376sk_sp<GrTextureProxy> GrProxyProvider::createProxy(const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -0500377 SkISize dimensions,
Greg Daniel47c20e82020-01-21 14:29:57 -0500378 GrSwizzle readSwizzle,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400379 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400380 int renderTargetSampleCnt,
Greg Danielf6f7b672018-02-15 13:06:26 -0500381 GrMipMapped mipMapped,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500382 SkBackingFit fit,
383 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -0400384 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400385 GrInternalSurfaceFlags surfaceFlags,
386 GrSurfaceProxy::UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500387 ASSERT_SINGLE_OWNER
388 if (this->isAbandoned()) {
389 return nullptr;
390 }
391
Robert Phillips8ff8bcc2019-07-29 17:03:35 -0400392 const GrCaps* caps = this->caps();
393
394 if (caps->isFormatCompressed(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400395 // Deferred proxies for compressed textures are not supported.
396 return nullptr;
397 }
Robert Phillips0902c982019-07-16 07:47:56 -0400398
Greg Danielf6f7b672018-02-15 13:06:26 -0500399 if (GrMipMapped::kYes == mipMapped) {
400 // SkMipMap doesn't include the base level in the level count so we have to add 1
Brian Salomona56a7462020-02-07 14:17:25 -0500401 int mipCount = SkMipMap::ComputeLevelCount(dimensions.fWidth, dimensions.fHeight) + 1;
Greg Danielf6f7b672018-02-15 13:06:26 -0500402 if (1 == mipCount) {
403 mipMapped = GrMipMapped::kNo;
404 }
405 }
406
Brian Salomona56a7462020-02-07 14:17:25 -0500407 if (!caps->validateSurfaceParams(dimensions, format, renderable, renderTargetSampleCnt,
408 mipMapped)) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500409 return nullptr;
410 }
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600411 GrMipMapsStatus mipMapsStatus = (GrMipMapped::kYes == mipMapped)
412 ? GrMipMapsStatus::kDirty
413 : GrMipMapsStatus::kNotAllocated;
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400414 if (renderable == GrRenderable::kYes) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400415 renderTargetSampleCnt =
Greg Daniel6fa62e22019-08-07 15:52:37 -0400416 caps->getRenderTargetSampleCount(renderTargetSampleCnt, format);
417 SkASSERT(renderTargetSampleCnt);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500418 // We know anything we instantiate later from this deferred path will be
419 // both texturable and renderable
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400420 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
Greg Daniel3a365112020-02-14 10:47:18 -0500421 *caps, format, dimensions, renderTargetSampleCnt, mipMapped, mipMapsStatus,
Greg Daniel14b57212019-12-17 16:18:06 -0500422 readSwizzle, fit, budgeted, isProtected, surfaceFlags, useAllocator));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500423 }
424
Greg Daniel3a365112020-02-14 10:47:18 -0500425 return sk_sp<GrTextureProxy>(new GrTextureProxy(format, dimensions, mipMapped, mipMapsStatus,
426 readSwizzle, fit, budgeted, isProtected,
427 surfaceFlags, useAllocator));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500428}
429
Brian Salomonbb8dde82019-06-27 10:52:13 -0400430sk_sp<GrTextureProxy> GrProxyProvider::createCompressedTextureProxy(
Robert Phillips3a833922020-01-21 15:25:58 -0500431 SkISize dimensions, SkBudgeted budgeted, GrMipMapped mipMapped, GrProtected isProtected,
Robert Phillipse4720c62020-01-14 14:33:24 -0500432 SkImage::CompressionType compressionType, sk_sp<SkData> data) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500433 ASSERT_SINGLE_OWNER
434 if (this->isAbandoned()) {
435 return nullptr;
436 }
Brian Salomonbb8dde82019-06-27 10:52:13 -0400437
Robert Phillips8ff8bcc2019-07-29 17:03:35 -0400438 GrBackendFormat format = this->caps()->getBackendFormatFromCompressionType(compressionType);
439
Greg Daniel7bfc9132019-08-14 14:23:53 -0400440 if (!this->caps()->isFormatTexturable(format)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500441 return nullptr;
442 }
443
Robert Phillipse4720c62020-01-14 14:33:24 -0500444 GrMipMapsStatus mipMapsStatus = (GrMipMapped::kYes == mipMapped)
445 ? GrMipMapsStatus::kValid
446 : GrMipMapsStatus::kNotAllocated;
447
Jim Van Verthee06b332019-01-18 10:36:32 -0500448 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomona56a7462020-02-07 14:17:25 -0500449 [dimensions, format, budgeted, mipMapped, isProtected,
450 data](GrResourceProvider* resourceProvider) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400451 return LazyCallbackResult(resourceProvider->createCompressedTexture(
Robert Phillips3a833922020-01-21 15:25:58 -0500452 dimensions, format, budgeted, mipMapped, isProtected, data.get()));
Brian Salomonbb8dde82019-06-27 10:52:13 -0400453 },
Greg Daniel3a365112020-02-14 10:47:18 -0500454 format, dimensions, GrSwizzle(), GrRenderable::kNo, 1, mipMapped, mipMapsStatus,
455 GrInternalSurfaceFlags::kReadOnly, SkBackingFit::kExact, SkBudgeted::kYes,
456 GrProtected::kNo, UseAllocator::kYes);
Jim Van Verthee06b332019-01-18 10:36:32 -0500457
458 if (!proxy) {
459 return nullptr;
460 }
461
Robert Phillipsa41c6852019-02-07 10:44:10 -0500462 GrContext* direct = fImageContext->priv().asDirectContext();
463 if (direct) {
464 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Jim Van Verthee06b332019-01-18 10:36:32 -0500465 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
466 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500467 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500468 return nullptr;
469 }
470 }
471 return proxy;
472}
473
Brian Salomon7578f3e2018-03-07 14:39:54 -0500474sk_sp<GrTextureProxy> GrProxyProvider::wrapBackendTexture(const GrBackendTexture& backendTex,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400475 GrColorType grColorType,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500476 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500477 GrWrapCacheable cacheable,
Brian Salomonc67c31c2018-12-06 10:00:03 -0500478 GrIOType ioType,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500479 ReleaseProc releaseProc,
480 ReleaseContext releaseCtx) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500481 SkASSERT(ioType != kWrite_GrIOType);
Robert Phillipsf9bec202018-01-16 09:21:01 -0500482 if (this->isAbandoned()) {
483 return nullptr;
484 }
485
Brian Salomonf7778972018-03-08 10:13:17 -0500486 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500487 GrContext* direct = fImageContext->priv().asDirectContext();
488 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500489 return nullptr;
490 }
491
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400492 const GrCaps* caps = this->caps();
493
Robert Phillipsa41c6852019-02-07 10:44:10 -0500494 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
495
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500496 sk_sp<GrTexture> tex =
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400497 resourceProvider->wrapBackendTexture(backendTex, grColorType,
498 ownership, cacheable, ioType);
Brian Salomonf7778972018-03-08 10:13:17 -0500499 if (!tex) {
500 return nullptr;
501 }
Robert Phillipsadbe1322018-01-17 13:35:46 -0500502
Greg Daniel6a0176b2018-01-30 09:28:44 -0500503 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500504 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel6a0176b2018-01-30 09:28:44 -0500505 }
506
Brian Salomonf7778972018-03-08 10:13:17 -0500507 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
508 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500509 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500510
Greg Daniel14b57212019-12-17 16:18:06 -0500511 GrSwizzle readSwizzle = caps->getReadSwizzle(tex->backendFormat(), grColorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400512
Brian Salomonbeb7f522019-08-30 16:19:42 -0400513 return sk_sp<GrTextureProxy>(
Greg Daniel3a365112020-02-14 10:47:18 -0500514 new GrTextureProxy(std::move(tex), readSwizzle, UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500515}
516
Robert Phillipsead321b2019-12-19 10:16:32 -0500517sk_sp<GrTextureProxy> GrProxyProvider::wrapCompressedBackendTexture(const GrBackendTexture& beTex,
Robert Phillipsead321b2019-12-19 10:16:32 -0500518 GrWrapOwnership ownership,
519 GrWrapCacheable cacheable,
520 ReleaseProc releaseProc,
521 ReleaseContext releaseCtx) {
522 if (this->isAbandoned()) {
523 return nullptr;
524 }
525
526 // This is only supported on a direct GrContext.
527 GrContext* direct = fImageContext->priv().asDirectContext();
528 if (!direct) {
529 return nullptr;
530 }
531
Robert Phillipsb0855272020-01-15 12:56:52 -0500532 const GrCaps* caps = this->caps();
533
Robert Phillipsead321b2019-12-19 10:16:32 -0500534 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
535
536 sk_sp<GrTexture> tex = resourceProvider->wrapCompressedBackendTexture(beTex, ownership,
537 cacheable);
538 if (!tex) {
539 return nullptr;
540 }
541
542 if (releaseProc) {
543 tex->setRelease(releaseProc, releaseCtx);
544 }
545
546 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
547 // Make sure we match how we created the proxy with SkBudgeted::kNo
548 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
549
Robert Phillipsb0855272020-01-15 12:56:52 -0500550 SkImage::CompressionType compressionType = caps->compressionType(beTex.getBackendFormat());
551
Robert Phillips99dead92020-01-27 16:11:57 -0500552 GrSwizzle texSwizzle = SkCompressionTypeIsOpaque(compressionType) ? GrSwizzle::RGB1()
Robert Phillipsb0855272020-01-15 12:56:52 -0500553 : GrSwizzle::RGBA();
Robert Phillipsead321b2019-12-19 10:16:32 -0500554
Greg Daniel3a365112020-02-14 10:47:18 -0500555 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), texSwizzle, UseAllocator::kNo));
Robert Phillipsead321b2019-12-19 10:16:32 -0500556}
557
Brian Salomon7578f3e2018-03-07 14:39:54 -0500558sk_sp<GrTextureProxy> GrProxyProvider::wrapRenderableBackendTexture(
Greg Daniel3a365112020-02-14 10:47:18 -0500559 const GrBackendTexture& backendTex, int sampleCnt, GrColorType colorType,
560 GrWrapOwnership ownership, GrWrapCacheable cacheable, ReleaseProc releaseProc,
561 ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500562 if (this->isAbandoned()) {
563 return nullptr;
564 }
565
Brian Salomonf7778972018-03-08 10:13:17 -0500566 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500567 GrContext* direct = fImageContext->priv().asDirectContext();
568 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500569 return nullptr;
570 }
571
Robert Phillips0902c982019-07-16 07:47:56 -0400572 const GrCaps* caps = this->caps();
573
Robert Phillipsa41c6852019-02-07 10:44:10 -0500574 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
575
Greg Daniel6fa62e22019-08-07 15:52:37 -0400576 // TODO: This should have been checked and validated before getting into GrProxyProvider.
577 if (!caps->isFormatAsColorTypeRenderable(colorType, backendTex.getBackendFormat(), sampleCnt)) {
Greg Danielf87651e2018-02-21 11:36:53 -0500578 return nullptr;
579 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500580
Greg Daniel6fa62e22019-08-07 15:52:37 -0400581 sampleCnt = caps->getRenderTargetSampleCount(sampleCnt, backendTex.getBackendFormat());
582 SkASSERT(sampleCnt);
583
Robert Phillipsa41c6852019-02-07 10:44:10 -0500584 sk_sp<GrTexture> tex = resourceProvider->wrapRenderableBackendTexture(backendTex, sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400585 colorType, ownership,
586 cacheable);
Brian Salomonf7778972018-03-08 10:13:17 -0500587 if (!tex) {
588 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500589 }
590
Greg Daniel8ce79912019-02-05 10:08:43 -0500591 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500592 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500593 }
594
Brian Salomonf7778972018-03-08 10:13:17 -0500595 SkASSERT(tex->asRenderTarget()); // A GrTextureRenderTarget
596 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500597 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Greg Daniel6abda432018-02-15 14:55:00 -0500598
Greg Daniel14b57212019-12-17 16:18:06 -0500599 GrSwizzle readSwizzle = caps->getReadSwizzle(tex->backendFormat(), colorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400600
Greg Daniel3a365112020-02-14 10:47:18 -0500601 return sk_sp<GrTextureProxy>(
602 new GrTextureRenderTargetProxy(std::move(tex), readSwizzle, UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500603}
604
Brian Salomon7578f3e2018-03-07 14:39:54 -0500605sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget(
Greg Daniel3a365112020-02-14 10:47:18 -0500606 const GrBackendRenderTarget& backendRT, GrColorType grColorType, ReleaseProc releaseProc,
607 ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500608 if (this->isAbandoned()) {
609 return nullptr;
610 }
611
Brian Salomonf7778972018-03-08 10:13:17 -0500612 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500613 GrContext* direct = fImageContext->priv().asDirectContext();
614 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500615 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500616 }
617
Robert Phillips62221e72019-07-24 15:07:38 -0400618 const GrCaps* caps = this->caps();
619
Robert Phillipsa41c6852019-02-07 10:44:10 -0500620 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
621
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400622 sk_sp<GrRenderTarget> rt = resourceProvider->wrapBackendRenderTarget(backendRT, grColorType);
Brian Salomonf7778972018-03-08 10:13:17 -0500623 if (!rt) {
624 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500625 }
Greg Daniel8ce79912019-02-05 10:08:43 -0500626
Greg Daniel8ce79912019-02-05 10:08:43 -0500627 if (releaseProc) {
Brian Salomon2ca31f82019-03-05 13:28:58 -0500628 rt->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500629 }
630
Brian Salomonf7778972018-03-08 10:13:17 -0500631 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
632 SkASSERT(!rt->getUniqueKey().isValid());
633 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500634 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Brian Salomonf7778972018-03-08 10:13:17 -0500635
Greg Daniel14b57212019-12-17 16:18:06 -0500636 GrSwizzle readSwizzle = caps->getReadSwizzle(rt->backendFormat(), grColorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400637
Greg Daniel3a365112020-02-14 10:47:18 -0500638 return sk_sp<GrRenderTargetProxy>(
639 new GrRenderTargetProxy(std::move(rt), readSwizzle, UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500640}
641
Brian Salomon7578f3e2018-03-07 14:39:54 -0500642sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendTextureAsRenderTarget(
Greg Daniel3a365112020-02-14 10:47:18 -0500643 const GrBackendTexture& backendTex, GrColorType grColorType, int sampleCnt) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500644 if (this->isAbandoned()) {
645 return nullptr;
646 }
647
Brian Salomonf7778972018-03-08 10:13:17 -0500648 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500649 GrContext* direct = fImageContext->priv().asDirectContext();
650 if (!direct) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500651 return nullptr;
652 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500653
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400654 const GrCaps* caps = this->caps();
655
Robert Phillipsa41c6852019-02-07 10:44:10 -0500656 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
657
Brian Salomonf7778972018-03-08 10:13:17 -0500658 sk_sp<GrRenderTarget> rt =
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400659 resourceProvider->wrapBackendTextureAsRenderTarget(backendTex, sampleCnt, grColorType);
Brian Salomonf7778972018-03-08 10:13:17 -0500660 if (!rt) {
661 return nullptr;
Greg Danielf87651e2018-02-21 11:36:53 -0500662 }
Brian Salomonf7778972018-03-08 10:13:17 -0500663 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
664 SkASSERT(!rt->getUniqueKey().isValid());
Greg Danielb46add82019-01-02 14:51:29 -0500665 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500666 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielf87651e2018-02-21 11:36:53 -0500667
Greg Daniel14b57212019-12-17 16:18:06 -0500668 GrSwizzle readSwizzle = caps->getReadSwizzle(rt->backendFormat(), grColorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400669
Greg Daniel3a365112020-02-14 10:47:18 -0500670 return sk_sp<GrSurfaceProxy>(
671 new GrRenderTargetProxy(std::move(rt), readSwizzle, UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500672}
673
Greg Danielb46add82019-01-02 14:51:29 -0500674sk_sp<GrRenderTargetProxy> GrProxyProvider::wrapVulkanSecondaryCBAsRenderTarget(
675 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
676 if (this->isAbandoned()) {
677 return nullptr;
678 }
679
680 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500681 GrContext* direct = fImageContext->priv().asDirectContext();
682 if (!direct) {
Greg Danielb46add82019-01-02 14:51:29 -0500683 return nullptr;
684 }
685
Robert Phillipsa41c6852019-02-07 10:44:10 -0500686 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Danielb46add82019-01-02 14:51:29 -0500687
Robert Phillipsa41c6852019-02-07 10:44:10 -0500688 sk_sp<GrRenderTarget> rt = resourceProvider->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
689 vkInfo);
Greg Danielb46add82019-01-02 14:51:29 -0500690 if (!rt) {
691 return nullptr;
692 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500693
Greg Danielb46add82019-01-02 14:51:29 -0500694 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
695 SkASSERT(!rt->getUniqueKey().isValid());
696 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500697 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielb46add82019-01-02 14:51:29 -0500698
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400699 GrColorType colorType = SkColorTypeToGrColorType(imageInfo.colorType());
Greg Daniel14b57212019-12-17 16:18:06 -0500700 GrSwizzle readSwizzle = this->caps()->getReadSwizzle(rt->backendFormat(), colorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400701
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400702 if (!this->caps()->isFormatAsColorTypeRenderable(colorType, rt->backendFormat(),
703 rt->numSamples())) {
704 return nullptr;
705 }
706
Greg Daniel3a365112020-02-14 10:47:18 -0500707 return sk_sp<GrRenderTargetProxy>(
708 new GrRenderTargetProxy(std::move(rt), readSwizzle, UseAllocator::kNo,
709 GrRenderTargetProxy::WrapsVkSecondaryCB::kYes));
Brian Salomone8a766b2019-07-19 14:24:36 -0400710}
711
712sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
713 const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -0500714 SkISize dimensions,
Greg Danielce3ddaa2020-01-22 16:58:15 -0500715 GrSwizzle readSwizzle,
Brian Salomone8a766b2019-07-19 14:24:36 -0400716 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400717 int renderTargetSampleCnt,
Brian Salomone8a766b2019-07-19 14:24:36 -0400718 GrMipMapped mipMapped,
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600719 GrMipMapsStatus mipMapsStatus,
Brian Salomone8a766b2019-07-19 14:24:36 -0400720 GrInternalSurfaceFlags surfaceFlags,
721 SkBackingFit fit,
722 SkBudgeted budgeted,
723 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400724 GrSurfaceProxy::UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500725 ASSERT_SINGLE_OWNER
726 if (this->isAbandoned()) {
727 return nullptr;
728 }
Brian Salomona56a7462020-02-07 14:17:25 -0500729 SkASSERT((dimensions.fWidth <= 0 && dimensions.fHeight <= 0) ||
730 (dimensions.fWidth > 0 && dimensions.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400731
Robert Phillips0a15cc62019-07-30 12:49:10 -0400732 if (!format.isValid()) {
733 return nullptr;
734 }
735
Brian Salomona56a7462020-02-07 14:17:25 -0500736 if (dimensions.fWidth > this->caps()->maxTextureSize() ||
737 dimensions.fHeight > this->caps()->maxTextureSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400738 return nullptr;
739 }
740
Brian Salomonbeb7f522019-08-30 16:19:42 -0400741 if (renderable == GrRenderable::kYes) {
742 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(*this->caps(),
743 std::move(callback),
744 format,
Brian Salomona56a7462020-02-07 14:17:25 -0500745 dimensions,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400746 renderTargetSampleCnt,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400747 mipMapped,
748 mipMapsStatus,
Greg Daniel14b57212019-12-17 16:18:06 -0500749 readSwizzle,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400750 fit,
751 budgeted,
752 isProtected,
753 surfaceFlags,
754 useAllocator));
755 } else {
756 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(callback),
757 format,
Brian Salomona56a7462020-02-07 14:17:25 -0500758 dimensions,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400759 mipMapped,
760 mipMapsStatus,
Greg Daniel14b57212019-12-17 16:18:06 -0500761 readSwizzle,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400762 fit,
763 budgeted,
764 isProtected,
765 surfaceFlags,
766 useAllocator));
767 }
Robert Phillips777707b2018-01-17 11:40:14 -0500768}
769
Robert Phillipse8fabb22018-02-04 14:33:21 -0500770sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy(
Brian Salomonbeb7f522019-08-30 16:19:42 -0400771 LazyInstantiateCallback&& callback,
772 const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -0500773 SkISize dimensions,
Greg Danielce3ddaa2020-01-22 16:58:15 -0500774 GrSwizzle readSwizzle,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400775 int sampleCnt,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400776 GrInternalSurfaceFlags surfaceFlags,
777 const TextureInfo* textureInfo,
778 GrMipMapsStatus mipMapsStatus,
779 SkBackingFit fit,
780 SkBudgeted budgeted,
781 GrProtected isProtected,
782 bool wrapsVkSecondaryCB,
783 UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500784 ASSERT_SINGLE_OWNER
785 if (this->isAbandoned()) {
786 return nullptr;
787 }
Brian Salomona56a7462020-02-07 14:17:25 -0500788 SkASSERT((dimensions.fWidth <= 0 && dimensions.fHeight <= 0) ||
789 (dimensions.fWidth > 0 && dimensions.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400790
Brian Salomona56a7462020-02-07 14:17:25 -0500791 if (dimensions.fWidth > this->caps()->maxRenderTargetSize() ||
792 dimensions.fHeight > this->caps()->maxRenderTargetSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400793 return nullptr;
794 }
795
Brian Salomon7226c232018-07-30 13:13:17 -0400796 if (textureInfo) {
Greg Danielb085fa92019-03-05 16:55:12 -0500797 // Wrapped vulkan secondary command buffers don't support texturing since we won't have an
798 // actual VkImage to texture from.
799 SkASSERT(!wrapsVkSecondaryCB);
Brian Salomon7226c232018-07-30 13:13:17 -0400800 return sk_sp<GrRenderTargetProxy>(new GrTextureRenderTargetProxy(
Greg Daniel3a365112020-02-14 10:47:18 -0500801 *this->caps(), std::move(callback), format, dimensions, sampleCnt,
Greg Daniel14b57212019-12-17 16:18:06 -0500802 textureInfo->fMipMapped, mipMapsStatus, readSwizzle, fit, budgeted, isProtected,
Greg Danielbaf8d992019-10-29 14:14:32 -0400803 surfaceFlags, useAllocator));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500804 }
805
Greg Danielb085fa92019-03-05 16:55:12 -0500806 GrRenderTargetProxy::WrapsVkSecondaryCB vkSCB =
807 wrapsVkSecondaryCB ? GrRenderTargetProxy::WrapsVkSecondaryCB::kYes
808 : GrRenderTargetProxy::WrapsVkSecondaryCB::kNo;
809
Greg Daniel3a365112020-02-14 10:47:18 -0500810 return sk_sp<GrRenderTargetProxy>(
811 new GrRenderTargetProxy(std::move(callback), format, dimensions, sampleCnt, readSwizzle,
812 fit, budgeted, isProtected, surfaceFlags, useAllocator, vkSCB));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500813}
814
Brian Salomonbeb7f522019-08-30 16:19:42 -0400815sk_sp<GrTextureProxy> GrProxyProvider::MakeFullyLazyProxy(LazyInstantiateCallback&& callback,
816 const GrBackendFormat& format,
Greg Danielce3ddaa2020-01-22 16:58:15 -0500817 GrSwizzle readSwizzle,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400818 GrRenderable renderable,
819 int renderTargetSampleCnt,
820 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400821 const GrCaps& caps,
822 UseAllocator useAllocator) {
Robert Phillips0a15cc62019-07-30 12:49:10 -0400823 if (!format.isValid()) {
824 return nullptr;
825 }
826
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400827 SkASSERT(renderTargetSampleCnt == 1 || renderable == GrRenderable::kYes);
Robert Phillips10d17212019-04-24 14:09:10 -0400828 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNone;
Robert Phillips777707b2018-01-17 11:40:14 -0500829
Brian Salomona56a7462020-02-07 14:17:25 -0500830 static constexpr SkISize kLazyDims = {-1, -1};
Brian Salomonbeb7f522019-08-30 16:19:42 -0400831 if (GrRenderable::kYes == renderable) {
832 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
Greg Daniel3a365112020-02-14 10:47:18 -0500833 caps, std::move(callback), format, kLazyDims, renderTargetSampleCnt,
Greg Daniel14b57212019-12-17 16:18:06 -0500834 GrMipMapped::kNo, GrMipMapsStatus::kNotAllocated, readSwizzle,
835 SkBackingFit::kApprox, SkBudgeted::kYes, isProtected, surfaceFlags, useAllocator));
Brian Salomonbeb7f522019-08-30 16:19:42 -0400836 } else {
837 return sk_sp<GrTextureProxy>(new GrTextureProxy(
Greg Daniel3a365112020-02-14 10:47:18 -0500838 std::move(callback), format, kLazyDims, GrMipMapped::kNo,
Greg Daniel14b57212019-12-17 16:18:06 -0500839 GrMipMapsStatus::kNotAllocated, readSwizzle, SkBackingFit::kApprox,
840 SkBudgeted::kYes, isProtected, surfaceFlags, useAllocator));
Brian Salomonbeb7f522019-08-30 16:19:42 -0400841 }
Robert Phillips777707b2018-01-17 11:40:14 -0500842}
843
Robert Phillips427966a2018-12-20 17:20:43 -0500844void GrProxyProvider::processInvalidUniqueKey(const GrUniqueKey& key, GrTextureProxy* proxy,
845 InvalidateGPUResource invalidateGPUResource) {
Chris Dalton2de13dd2019-01-03 15:11:59 -0700846 SkASSERT(key.isValid());
847
Robert Phillips427966a2018-12-20 17:20:43 -0500848 if (!proxy) {
849 proxy = fUniquelyKeyedProxies.find(key);
850 }
Chris Dalton2de13dd2019-01-03 15:11:59 -0700851 SkASSERT(!proxy || proxy->getUniqueKey() == key);
852
853 // Locate the corresponding GrGpuResource (if it needs to be invalidated) before clearing the
854 // proxy's unique key. We must do it in this order because 'key' may alias the proxy's key.
855 sk_sp<GrGpuResource> invalidGpuResource;
856 if (InvalidateGPUResource::kYes == invalidateGPUResource) {
Brian Salomon01ceae92019-04-02 11:49:54 -0400857 GrContext* direct = fImageContext->priv().asDirectContext();
858 if (direct) {
859 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
860 invalidGpuResource = resourceProvider->findByUniqueKey<GrGpuResource>(key);
Chris Dalton2de13dd2019-01-03 15:11:59 -0700861 }
862 SkASSERT(!invalidGpuResource || invalidGpuResource->getUniqueKey() == key);
863 }
Robert Phillips427966a2018-12-20 17:20:43 -0500864
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500865 // Note: this method is called for the whole variety of GrGpuResources so often 'key'
866 // will not be in 'fUniquelyKeyedProxies'.
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500867 if (proxy) {
Robert Phillips427966a2018-12-20 17:20:43 -0500868 fUniquelyKeyedProxies.remove(key);
869 proxy->cacheAccess().clearUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500870 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500871
Chris Dalton2de13dd2019-01-03 15:11:59 -0700872 if (invalidGpuResource) {
873 invalidGpuResource->resourcePriv().removeUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500874 }
875}
876
Robert Phillipsa41c6852019-02-07 10:44:10 -0500877uint32_t GrProxyProvider::contextID() const {
878 return fImageContext->priv().contextID();
879}
880
881const GrCaps* GrProxyProvider::caps() const {
882 return fImageContext->priv().caps();
883}
884
885sk_sp<const GrCaps> GrProxyProvider::refCaps() const {
886 return fImageContext->priv().refCaps();
887}
888
Robert Phillipsa9162df2019-02-11 14:12:03 -0500889bool GrProxyProvider::isAbandoned() const {
890 return fImageContext->priv().abandoned();
891}
892
Robert Phillips0790f8a2018-09-18 13:11:03 -0400893void GrProxyProvider::orphanAllUniqueKeys() {
Mike Kleincff63962020-03-14 16:22:45 -0500894 fUniquelyKeyedProxies.foreach([&](GrTextureProxy* proxy){
895 proxy->fProxyProvider = nullptr;
896 });
Robert Phillips0790f8a2018-09-18 13:11:03 -0400897}
898
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500899void GrProxyProvider::removeAllUniqueKeys() {
900 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
901 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
902 GrTextureProxy& tmp = *iter;
903
Chris Dalton2de13dd2019-01-03 15:11:59 -0700904 this->processInvalidUniqueKey(tmp.getUniqueKey(), &tmp, InvalidateGPUResource::kNo);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500905 }
906 SkASSERT(!fUniquelyKeyedProxies.count());
907}
Robert Phillipsa41c6852019-02-07 10:44:10 -0500908
909bool GrProxyProvider::renderingDirectly() const {
910 return fImageContext->priv().asDirectContext();
911}