blob: 24057b0b4a0035e306229dd5186669eba88a781d [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/gpu/GrTexture.h"
14#include "include/private/GrImageContext.h"
15#include "include/private/GrResourceKey.h"
16#include "include/private/GrSingleOwner.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "include/private/SkImageInfoPriv.h"
18#include "src/core/SkAutoPixmapStorage.h"
Robert Phillips99dead92020-01-27 16:11:57 -050019#include "src/core/SkCompressedDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/core/SkImagePriv.h"
21#include "src/core/SkMipMap.h"
22#include "src/core/SkTraceEvent.h"
23#include "src/gpu/GrCaps.h"
24#include "src/gpu/GrContextPriv.h"
25#include "src/gpu/GrImageContextPriv.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040026#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/GrResourceProvider.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040028#include "src/gpu/GrSurfaceProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/gpu/GrSurfaceProxyPriv.h"
30#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
98sk_sp<GrTextureProxy> GrProxyProvider::findProxyByUniqueKey(const GrUniqueKey& key,
99 GrSurfaceOrigin origin) {
100 ASSERT_SINGLE_OWNER
101
102 if (this->isAbandoned()) {
103 return nullptr;
104 }
105
Brian Salomon01ceae92019-04-02 11:49:54 -0400106 GrTextureProxy* proxy = fUniquelyKeyedProxies.find(key);
Brian Salomon01ceae92019-04-02 11:49:54 -0400107 if (proxy) {
Robert Phillipse5f73282019-06-18 17:15:04 -0400108 SkASSERT(proxy->origin() == origin);
109 return sk_ref_sp(proxy);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500110 }
Robert Phillipse5f73282019-06-18 17:15:04 -0400111 return nullptr;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500112}
113
Robert Phillipsa41c6852019-02-07 10:44:10 -0500114///////////////////////////////////////////////////////////////////////////////
115
116#if GR_TEST_UTILS
117sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createInstantiatedProxy(
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400118 const SkISize& dimensions,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400119 GrColorType colorType,
120 const GrBackendFormat& format,
121 GrRenderable renderable,
122 int renderTargetSampleCnt,
123 GrSurfaceOrigin origin,
124 SkBackingFit fit,
125 SkBudgeted budgeted,
126 GrProtected isProtected) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500127 ASSERT_SINGLE_OWNER
128 if (this->isAbandoned()) {
129 return nullptr;
130 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500131 GrContext* direct = fImageContext->priv().asDirectContext();
132 if (!direct) {
133 return nullptr;
134 }
135
Brian Salomon4eb38b72019-08-05 12:58:39 -0400136 if (this->caps()->isFormatCompressed(format)) {
137 // TODO: Allow this to go to GrResourceProvider::createCompressedTexture() once we no longer
Greg Daniel4cb29332020-01-23 10:07:02 -0500138 // rely on GrColorType to get a swizzle for the proxy.
Brian Salomon4eb38b72019-08-05 12:58:39 -0400139 return nullptr;
140 }
141 GrSurfaceDesc desc;
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400142 desc.fWidth = dimensions.width();
143 desc.fHeight = dimensions.height();
Brian Salomon4eb38b72019-08-05 12:58:39 -0400144
Robert Phillipsa41c6852019-02-07 10:44:10 -0500145 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
146 sk_sp<GrTexture> tex;
147
148 if (SkBackingFit::kApprox == fit) {
Brian Salomon4eb38b72019-08-05 12:58:39 -0400149 tex = resourceProvider->createApproxTexture(desc, format, renderable, renderTargetSampleCnt,
Robert Phillipsaee18c92019-09-06 11:48:27 -0400150 isProtected);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500151 } else {
Brian Salomon4eb38b72019-08-05 12:58:39 -0400152 tex = resourceProvider->createTexture(desc, format, renderable, renderTargetSampleCnt,
Brian Salomona90382f2019-09-17 09:01:56 -0400153 GrMipMapped::kNo, budgeted, isProtected);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500154 }
155 if (!tex) {
156 return nullptr;
157 }
158
Brian Salomonbeb7f522019-08-30 16:19:42 -0400159 return this->createWrapped(std::move(tex), colorType, origin, UseAllocator::kYes);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500160}
161
Brian Salomon4eb38b72019-08-05 12:58:39 -0400162sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createInstantiatedProxy(
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400163 const SkISize& dimensions,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400164 GrColorType colorType,
165 GrRenderable renderable,
166 int renderTargetSampleCnt,
167 GrSurfaceOrigin origin,
168 SkBackingFit fit,
169 SkBudgeted budgeted,
170 GrProtected isProtected) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500171 ASSERT_SINGLE_OWNER
172 if (this->isAbandoned()) {
173 return nullptr;
174 }
Brian Salomon4eb38b72019-08-05 12:58:39 -0400175 auto format = this->caps()->getDefaultBackendFormat(colorType, renderable);
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400176 return this->testingOnly_createInstantiatedProxy(dimensions,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400177 colorType,
178 format,
179 renderable,
180 renderTargetSampleCnt,
181 origin,
182 fit,
183 budgeted,
184 isProtected);
185}
186
Robert Phillipsa41c6852019-02-07 10:44:10 -0500187sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createWrapped(sk_sp<GrTexture> tex,
Brian Salomon2af3e702019-08-11 19:10:31 -0400188 GrColorType colorType,
Robert Phillipsa41c6852019-02-07 10:44:10 -0500189 GrSurfaceOrigin origin) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400190 return this->createWrapped(std::move(tex), colorType, origin, UseAllocator::kYes);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500191}
192#endif
193
Brian Salomonbeb7f522019-08-30 16:19:42 -0400194sk_sp<GrTextureProxy> GrProxyProvider::createWrapped(sk_sp<GrTexture> tex,
195 GrColorType colorType,
196 GrSurfaceOrigin origin,
197 UseAllocator useAllocator) {
Robert Phillipsadbe1322018-01-17 13:35:46 -0500198#ifdef SK_DEBUG
199 if (tex->getUniqueKey().isValid()) {
200 SkASSERT(!this->findProxyByUniqueKey(tex->getUniqueKey(), origin));
201 }
202#endif
Greg Daniel14b57212019-12-17 16:18:06 -0500203 GrSwizzle readSwizzle = this->caps()->getReadSwizzle(tex->backendFormat(), colorType);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500204
205 if (tex->asRenderTarget()) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400206 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
Greg Daniel14b57212019-12-17 16:18:06 -0500207 std::move(tex), origin, readSwizzle, useAllocator));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500208 } else {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400209 return sk_sp<GrTextureProxy>(
Greg Daniel14b57212019-12-17 16:18:06 -0500210 new GrTextureProxy(std::move(tex), origin, readSwizzle, useAllocator));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500211 }
212}
213
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500214sk_sp<GrTextureProxy> GrProxyProvider::findOrCreateProxyByUniqueKey(const GrUniqueKey& key,
Brian Salomon2af3e702019-08-11 19:10:31 -0400215 GrColorType colorType,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400216 GrSurfaceOrigin origin,
217 UseAllocator useAllocator) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500218 ASSERT_SINGLE_OWNER
219
220 if (this->isAbandoned()) {
221 return nullptr;
222 }
223
224 sk_sp<GrTextureProxy> result = this->findProxyByUniqueKey(key, origin);
225 if (result) {
226 return result;
227 }
228
Robert Phillipsa41c6852019-02-07 10:44:10 -0500229 GrContext* direct = fImageContext->priv().asDirectContext();
230 if (!direct) {
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500231 return nullptr;
232 }
233
Robert Phillipsa41c6852019-02-07 10:44:10 -0500234 GrResourceCache* resourceCache = direct->priv().getResourceCache();
235
236 GrGpuResource* resource = resourceCache->findAndRefUniqueResource(key);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500237 if (!resource) {
238 return nullptr;
239 }
240
241 sk_sp<GrTexture> texture(static_cast<GrSurface*>(resource)->asTexture());
242 SkASSERT(texture);
243
Brian Salomonbeb7f522019-08-30 16:19:42 -0400244 result = this->createWrapped(std::move(texture), colorType, origin, useAllocator);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500245 SkASSERT(result->getUniqueKey() == key);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500246 // createWrapped should've added this for us
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500247 SkASSERT(fUniquelyKeyedProxies.find(key));
Brian Salomon2af3e702019-08-11 19:10:31 -0400248 SkASSERT(result->textureSwizzle() ==
Greg Daniel14b57212019-12-17 16:18:06 -0500249 this->caps()->getReadSwizzle(result->backendFormat(), colorType));
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500250 return result;
251}
252
Brian Osmande496652019-03-22 13:42:33 -0400253sk_sp<GrTextureProxy> GrProxyProvider::createProxyFromBitmap(const SkBitmap& bitmap,
Greg Daniel6f5441a2020-01-28 17:02:49 -0500254 GrMipMapped mipMapped,
255 SkBackingFit fit) {
Brian Osman412674f2019-02-07 15:34:58 -0500256 ASSERT_SINGLE_OWNER
Greg Daniel6f5441a2020-01-28 17:02:49 -0500257 SkASSERT(fit == SkBackingFit::kExact || mipMapped == GrMipMapped::kNo);
Brian Osman412674f2019-02-07 15:34:58 -0500258
259 if (this->isAbandoned()) {
260 return nullptr;
261 }
262
Brian Osman2b23c4b2018-06-01 12:25:08 -0400263 if (!SkImageInfoIsValid(bitmap.info())) {
Greg Daniela4ead652018-02-07 10:21:48 -0500264 return nullptr;
265 }
266
Brian Osmande496652019-03-22 13:42:33 -0400267 ATRACE_ANDROID_FRAMEWORK("Upload %sTexture [%ux%u]",
268 GrMipMapped::kYes == mipMapped ? "MipMap " : "",
269 bitmap.width(), bitmap.height());
Greg Daniela4ead652018-02-07 10:21:48 -0500270
271 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
272 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
273 // 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 -0500274 SkBitmap copyBitmap = bitmap;
275 if (!this->renderingDirectly() && !bitmap.isImmutable()) {
276 copyBitmap.allocPixels();
277 if (!bitmap.readPixels(copyBitmap.pixmap())) {
278 return nullptr;
279 }
280 copyBitmap.setImmutable();
Greg Daniela4ead652018-02-07 10:21:48 -0500281 }
282
Greg Daniel6f5441a2020-01-28 17:02:49 -0500283 GrColorType grCT = SkColorTypeToGrColorType(copyBitmap.info().colorType());
Greg Danielce3ddaa2020-01-22 16:58:15 -0500284 GrBackendFormat format = this->caps()->getDefaultBackendFormat(grCT, GrRenderable::kNo);
Robert Phillips0a15cc62019-07-30 12:49:10 -0400285 if (!format.isValid()) {
Greg Daniel82c6b102020-01-21 10:33:22 -0500286 return nullptr;
Greg Daniel3fc5df42019-06-14 09:42:17 -0400287 }
288
Greg Daniel6f5441a2020-01-28 17:02:49 -0500289 sk_sp<GrTextureProxy> proxy;
290 if (mipMapped == GrMipMapped::kNo ||
291 0 == SkMipMap::ComputeLevelCount(copyBitmap.width(), copyBitmap.height())) {
292 proxy = this->createNonMippedProxyFromBitmap(copyBitmap, fit, format, grCT);
293 } else {
294 proxy = this->createMippedProxyFromBitmap(copyBitmap, format, grCT);
295 }
296
297 if (!proxy) {
298 return nullptr;
299 }
300
301 GrContext* direct = fImageContext->priv().asDirectContext();
302 if (direct) {
303 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
304
305 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
306 // we're better off instantiating the proxy immediately here.
307 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
308 return nullptr;
309 }
310 }
311 return proxy;
312}
313
314sk_sp<GrTextureProxy> GrProxyProvider::createNonMippedProxyFromBitmap(const SkBitmap& bitmap,
315 SkBackingFit fit,
316 const GrBackendFormat& format,
317 GrColorType colorType) {
318 GrSurfaceDesc desc;
319 desc.fWidth = bitmap.width();
320 desc.fHeight = bitmap.height();
321
322 GrSwizzle swizzle = this->caps()->getReadSwizzle(format, colorType);
323
324 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
325 [desc, format, bitmap, fit, colorType]
326 (GrResourceProvider* resourceProvider) {
327 GrMipLevel mipLevel = { bitmap.getPixels(), bitmap.rowBytes() };
328
329 return LazyCallbackResult(resourceProvider->createTexture(
330 desc, format, colorType, GrRenderable::kNo, 1, SkBudgeted::kYes, fit,
331 GrProtected::kNo, mipLevel));
332 },
333 format, desc, swizzle, GrRenderable::kNo, 1, kTopLeft_GrSurfaceOrigin,
334 GrMipMapped::kNo, GrMipMapsStatus::kNotAllocated, GrInternalSurfaceFlags::kNone, fit,
335 SkBudgeted::kYes, GrProtected::kNo, UseAllocator::kYes);
336
337 if (!proxy) {
338 return nullptr;
339 }
340 SkASSERT(proxy->width() == desc.fWidth);
341 SkASSERT(proxy->height() == desc.fHeight);
342 return proxy;
343}
344
345sk_sp<GrTextureProxy> GrProxyProvider::createMippedProxyFromBitmap(const SkBitmap& bitmap,
346 const GrBackendFormat& format,
347 GrColorType colorType) {
348 SkASSERT(this->caps()->mipMapSupport());
349
350 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info());
351
352 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(bitmap.pixmap(), nullptr));
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400353 if (!mipmaps) {
354 return nullptr;
355 }
356
Greg Daniel6f5441a2020-01-28 17:02:49 -0500357 GrSwizzle readSwizzle = this->caps()->getReadSwizzle(format, colorType);
Greg Danielce3ddaa2020-01-22 16:58:15 -0500358
Greg Daniela4ead652018-02-07 10:21:48 -0500359 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Greg Daniel6f5441a2020-01-28 17:02:49 -0500360 [desc, format, bitmap, mipmaps](GrResourceProvider* resourceProvider) {
Brian Osman1b97f132018-09-13 17:33:48 +0000361 const int mipLevelCount = mipmaps->countLevels() + 1;
362 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
363
Greg Daniel6f5441a2020-01-28 17:02:49 -0500364 texels[0].fPixels = bitmap.getPixels();
365 texels[0].fRowBytes = bitmap.rowBytes();
Greg Daniela4ead652018-02-07 10:21:48 -0500366
Greg Daniel6f5441a2020-01-28 17:02:49 -0500367 auto colorType = SkColorTypeToGrColorType(bitmap.colorType());
Greg Daniela4ead652018-02-07 10:21:48 -0500368 for (int i = 1; i < mipLevelCount; ++i) {
369 SkMipMap::Level generatedMipLevel;
370 mipmaps->getLevel(i - 1, &generatedMipLevel);
371 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
372 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
373 SkASSERT(texels[i].fPixels);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500374 SkASSERT(generatedMipLevel.fPixmap.colorType() == bitmap.colorType());
Greg Daniela4ead652018-02-07 10:21:48 -0500375 }
Brian Salomonbeb7f522019-08-30 16:19:42 -0400376 return LazyCallbackResult(resourceProvider->createTexture(
Brian Salomona90382f2019-09-17 09:01:56 -0400377 desc, format, colorType, GrRenderable::kNo, 1, SkBudgeted::kYes,
378 GrProtected::kNo, texels.get(), mipLevelCount));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500379 },
Greg Danielce3ddaa2020-01-22 16:58:15 -0500380 format, desc, readSwizzle, GrRenderable::kNo, 1, kTopLeft_GrSurfaceOrigin,
381 GrMipMapped::kYes, GrMipMapsStatus::kValid, GrInternalSurfaceFlags::kNone,
382 SkBackingFit::kExact, SkBudgeted::kYes, GrProtected::kNo, UseAllocator::kYes);
Greg Daniela4ead652018-02-07 10:21:48 -0500383
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400384 if (!proxy) {
385 return nullptr;
386 }
387
Greg Daniel6f5441a2020-01-28 17:02:49 -0500388 SkASSERT(proxy->width() == desc.fWidth);
389 SkASSERT(proxy->height() == desc.fHeight);
390
Greg Daniela4ead652018-02-07 10:21:48 -0500391 return proxy;
392}
393
Greg Daniel4065d452018-11-16 15:43:41 -0500394sk_sp<GrTextureProxy> GrProxyProvider::createProxy(const GrBackendFormat& format,
395 const GrSurfaceDesc& desc,
Greg Daniel47c20e82020-01-21 14:29:57 -0500396 GrSwizzle readSwizzle,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400397 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400398 int renderTargetSampleCnt,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500399 GrSurfaceOrigin origin,
Greg Danielf6f7b672018-02-15 13:06:26 -0500400 GrMipMapped mipMapped,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500401 SkBackingFit fit,
402 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -0400403 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400404 GrInternalSurfaceFlags surfaceFlags,
405 GrSurfaceProxy::UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500406 ASSERT_SINGLE_OWNER
407 if (this->isAbandoned()) {
408 return nullptr;
409 }
410
Robert Phillips8ff8bcc2019-07-29 17:03:35 -0400411 const GrCaps* caps = this->caps();
412
413 if (caps->isFormatCompressed(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400414 // Deferred proxies for compressed textures are not supported.
415 return nullptr;
416 }
Robert Phillips0902c982019-07-16 07:47:56 -0400417
Greg Danielf6f7b672018-02-15 13:06:26 -0500418 if (GrMipMapped::kYes == mipMapped) {
419 // SkMipMap doesn't include the base level in the level count so we have to add 1
420 int mipCount = SkMipMap::ComputeLevelCount(desc.fWidth, desc.fHeight) + 1;
421 if (1 == mipCount) {
422 mipMapped = GrMipMapped::kNo;
423 }
424 }
425
Greg Daniel9a48beb2020-01-16 16:57:16 -0500426 if (!caps->validateSurfaceParams({desc.fWidth, desc.fHeight}, format, renderable,
Greg Daniel6fa62e22019-08-07 15:52:37 -0400427 renderTargetSampleCnt, mipMapped)) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500428 return nullptr;
429 }
Brian Salomon3a2cc2c2018-02-03 00:25:12 +0000430 GrSurfaceDesc copyDesc = desc;
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600431 GrMipMapsStatus mipMapsStatus = (GrMipMapped::kYes == mipMapped)
432 ? GrMipMapsStatus::kDirty
433 : GrMipMapsStatus::kNotAllocated;
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400434 if (renderable == GrRenderable::kYes) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400435 renderTargetSampleCnt =
Greg Daniel6fa62e22019-08-07 15:52:37 -0400436 caps->getRenderTargetSampleCount(renderTargetSampleCnt, format);
437 SkASSERT(renderTargetSampleCnt);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500438 // We know anything we instantiate later from this deferred path will be
439 // both texturable and renderable
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400440 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600441 *caps, format, copyDesc, renderTargetSampleCnt, origin, mipMapped, mipMapsStatus,
Greg Daniel14b57212019-12-17 16:18:06 -0500442 readSwizzle, fit, budgeted, isProtected, surfaceFlags, useAllocator));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500443 }
444
Brian Salomonbeb7f522019-08-30 16:19:42 -0400445 return sk_sp<GrTextureProxy>(new GrTextureProxy(format, copyDesc, origin, mipMapped,
Greg Daniel14b57212019-12-17 16:18:06 -0500446 mipMapsStatus, readSwizzle, fit, budgeted,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400447 isProtected, surfaceFlags, useAllocator));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500448}
449
Brian Salomonbb8dde82019-06-27 10:52:13 -0400450sk_sp<GrTextureProxy> GrProxyProvider::createCompressedTextureProxy(
Robert Phillips3a833922020-01-21 15:25:58 -0500451 SkISize dimensions, SkBudgeted budgeted, GrMipMapped mipMapped, GrProtected isProtected,
Robert Phillipse4720c62020-01-14 14:33:24 -0500452 SkImage::CompressionType compressionType, sk_sp<SkData> data) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500453 ASSERT_SINGLE_OWNER
454 if (this->isAbandoned()) {
455 return nullptr;
456 }
Brian Salomonbb8dde82019-06-27 10:52:13 -0400457
458 GrSurfaceDesc desc;
Robert Phillips9f744f72019-12-19 19:14:33 -0500459 desc.fWidth = dimensions.width();
460 desc.fHeight = dimensions.height();
Brian Salomonbb8dde82019-06-27 10:52:13 -0400461
Robert Phillips8ff8bcc2019-07-29 17:03:35 -0400462 GrBackendFormat format = this->caps()->getBackendFormatFromCompressionType(compressionType);
463
Greg Daniel7bfc9132019-08-14 14:23:53 -0400464 if (!this->caps()->isFormatTexturable(format)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500465 return nullptr;
466 }
467
Robert Phillipse4720c62020-01-14 14:33:24 -0500468 GrMipMapsStatus mipMapsStatus = (GrMipMapped::kYes == mipMapped)
469 ? GrMipMapsStatus::kValid
470 : GrMipMapsStatus::kNotAllocated;
471
Jim Van Verthee06b332019-01-18 10:36:32 -0500472 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Robert Phillips3a833922020-01-21 15:25:58 -0500473 [dimensions, format, budgeted, mipMapped, isProtected, data]
474 (GrResourceProvider* resourceProvider) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400475 return LazyCallbackResult(resourceProvider->createCompressedTexture(
Robert Phillips3a833922020-01-21 15:25:58 -0500476 dimensions, format, budgeted, mipMapped, isProtected, data.get()));
Brian Salomonbb8dde82019-06-27 10:52:13 -0400477 },
Greg Danielce3ddaa2020-01-22 16:58:15 -0500478 format, desc, GrSwizzle(), GrRenderable::kNo, 1, kTopLeft_GrSurfaceOrigin, mipMapped,
Robert Phillipse4720c62020-01-14 14:33:24 -0500479 mipMapsStatus, GrInternalSurfaceFlags::kReadOnly, SkBackingFit::kExact,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400480 SkBudgeted::kYes, GrProtected::kNo, UseAllocator::kYes);
Jim Van Verthee06b332019-01-18 10:36:32 -0500481
482 if (!proxy) {
483 return nullptr;
484 }
485
Robert Phillipsa41c6852019-02-07 10:44:10 -0500486 GrContext* direct = fImageContext->priv().asDirectContext();
487 if (direct) {
488 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Jim Van Verthee06b332019-01-18 10:36:32 -0500489 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
490 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500491 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500492 return nullptr;
493 }
494 }
495 return proxy;
496}
497
Brian Salomon7578f3e2018-03-07 14:39:54 -0500498sk_sp<GrTextureProxy> GrProxyProvider::wrapBackendTexture(const GrBackendTexture& backendTex,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400499 GrColorType grColorType,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500500 GrSurfaceOrigin origin,
501 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500502 GrWrapCacheable cacheable,
Brian Salomonc67c31c2018-12-06 10:00:03 -0500503 GrIOType ioType,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500504 ReleaseProc releaseProc,
505 ReleaseContext releaseCtx) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500506 SkASSERT(ioType != kWrite_GrIOType);
Robert Phillipsf9bec202018-01-16 09:21:01 -0500507 if (this->isAbandoned()) {
508 return nullptr;
509 }
510
Brian Salomonf7778972018-03-08 10:13:17 -0500511 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500512 GrContext* direct = fImageContext->priv().asDirectContext();
513 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500514 return nullptr;
515 }
516
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400517 const GrCaps* caps = this->caps();
518
Robert Phillipsa41c6852019-02-07 10:44:10 -0500519 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
520
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500521 sk_sp<GrTexture> tex =
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400522 resourceProvider->wrapBackendTexture(backendTex, grColorType,
523 ownership, cacheable, ioType);
Brian Salomonf7778972018-03-08 10:13:17 -0500524 if (!tex) {
525 return nullptr;
526 }
Robert Phillipsadbe1322018-01-17 13:35:46 -0500527
Greg Daniel6a0176b2018-01-30 09:28:44 -0500528 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500529 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel6a0176b2018-01-30 09:28:44 -0500530 }
531
Brian Salomonf7778972018-03-08 10:13:17 -0500532 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
533 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500534 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500535
Greg Daniel14b57212019-12-17 16:18:06 -0500536 GrSwizzle readSwizzle = caps->getReadSwizzle(tex->backendFormat(), grColorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400537
Brian Salomonbeb7f522019-08-30 16:19:42 -0400538 return sk_sp<GrTextureProxy>(
Greg Daniel14b57212019-12-17 16:18:06 -0500539 new GrTextureProxy(std::move(tex), origin, readSwizzle, UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500540}
541
Robert Phillipsead321b2019-12-19 10:16:32 -0500542sk_sp<GrTextureProxy> GrProxyProvider::wrapCompressedBackendTexture(const GrBackendTexture& beTex,
543 GrSurfaceOrigin origin,
544 GrWrapOwnership ownership,
545 GrWrapCacheable cacheable,
546 ReleaseProc releaseProc,
547 ReleaseContext releaseCtx) {
548 if (this->isAbandoned()) {
549 return nullptr;
550 }
551
552 // This is only supported on a direct GrContext.
553 GrContext* direct = fImageContext->priv().asDirectContext();
554 if (!direct) {
555 return nullptr;
556 }
557
Robert Phillipsb0855272020-01-15 12:56:52 -0500558 const GrCaps* caps = this->caps();
559
Robert Phillipsead321b2019-12-19 10:16:32 -0500560 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
561
562 sk_sp<GrTexture> tex = resourceProvider->wrapCompressedBackendTexture(beTex, ownership,
563 cacheable);
564 if (!tex) {
565 return nullptr;
566 }
567
568 if (releaseProc) {
569 tex->setRelease(releaseProc, releaseCtx);
570 }
571
572 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
573 // Make sure we match how we created the proxy with SkBudgeted::kNo
574 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
575
Robert Phillipsb0855272020-01-15 12:56:52 -0500576 SkImage::CompressionType compressionType = caps->compressionType(beTex.getBackendFormat());
577
Robert Phillips99dead92020-01-27 16:11:57 -0500578 GrSwizzle texSwizzle = SkCompressionTypeIsOpaque(compressionType) ? GrSwizzle::RGB1()
Robert Phillipsb0855272020-01-15 12:56:52 -0500579 : GrSwizzle::RGBA();
Robert Phillipsead321b2019-12-19 10:16:32 -0500580
581 return sk_sp<GrTextureProxy>(
582 new GrTextureProxy(std::move(tex), origin, texSwizzle, UseAllocator::kNo));
583}
584
Brian Salomon7578f3e2018-03-07 14:39:54 -0500585sk_sp<GrTextureProxy> GrProxyProvider::wrapRenderableBackendTexture(
Brian Salomon02bd2952018-03-07 15:20:21 -0500586 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400587 GrColorType colorType, GrWrapOwnership ownership, GrWrapCacheable cacheable,
588 ReleaseProc releaseProc, ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500589 if (this->isAbandoned()) {
590 return nullptr;
591 }
592
Brian Salomonf7778972018-03-08 10:13:17 -0500593 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500594 GrContext* direct = fImageContext->priv().asDirectContext();
595 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500596 return nullptr;
597 }
598
Robert Phillips0902c982019-07-16 07:47:56 -0400599 const GrCaps* caps = this->caps();
600
Robert Phillipsa41c6852019-02-07 10:44:10 -0500601 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
602
Greg Daniel6fa62e22019-08-07 15:52:37 -0400603 // TODO: This should have been checked and validated before getting into GrProxyProvider.
604 if (!caps->isFormatAsColorTypeRenderable(colorType, backendTex.getBackendFormat(), sampleCnt)) {
Greg Danielf87651e2018-02-21 11:36:53 -0500605 return nullptr;
606 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500607
Greg Daniel6fa62e22019-08-07 15:52:37 -0400608 sampleCnt = caps->getRenderTargetSampleCount(sampleCnt, backendTex.getBackendFormat());
609 SkASSERT(sampleCnt);
610
Robert Phillipsa41c6852019-02-07 10:44:10 -0500611 sk_sp<GrTexture> tex = resourceProvider->wrapRenderableBackendTexture(backendTex, sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400612 colorType, ownership,
613 cacheable);
Brian Salomonf7778972018-03-08 10:13:17 -0500614 if (!tex) {
615 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500616 }
617
Greg Daniel8ce79912019-02-05 10:08:43 -0500618 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500619 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500620 }
621
Brian Salomonf7778972018-03-08 10:13:17 -0500622 SkASSERT(tex->asRenderTarget()); // A GrTextureRenderTarget
623 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500624 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Greg Daniel6abda432018-02-15 14:55:00 -0500625
Greg Daniel14b57212019-12-17 16:18:06 -0500626 GrSwizzle readSwizzle = caps->getReadSwizzle(tex->backendFormat(), colorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400627
Greg Daniel14b57212019-12-17 16:18:06 -0500628 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin, readSwizzle,
Greg Danielbaf8d992019-10-29 14:14:32 -0400629 UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500630}
631
Brian Salomon7578f3e2018-03-07 14:39:54 -0500632sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400633 const GrBackendRenderTarget& backendRT, GrColorType grColorType,
634 GrSurfaceOrigin origin, ReleaseProc releaseProc, ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500635 if (this->isAbandoned()) {
636 return nullptr;
637 }
638
Brian Salomonf7778972018-03-08 10:13:17 -0500639 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500640 GrContext* direct = fImageContext->priv().asDirectContext();
641 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500642 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500643 }
644
Robert Phillips62221e72019-07-24 15:07:38 -0400645 const GrCaps* caps = this->caps();
646
Robert Phillipsa41c6852019-02-07 10:44:10 -0500647 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
648
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400649 sk_sp<GrRenderTarget> rt = resourceProvider->wrapBackendRenderTarget(backendRT, grColorType);
Brian Salomonf7778972018-03-08 10:13:17 -0500650 if (!rt) {
651 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500652 }
Greg Daniel8ce79912019-02-05 10:08:43 -0500653
Greg Daniel8ce79912019-02-05 10:08:43 -0500654 if (releaseProc) {
Brian Salomon2ca31f82019-03-05 13:28:58 -0500655 rt->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500656 }
657
Brian Salomonf7778972018-03-08 10:13:17 -0500658 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
659 SkASSERT(!rt->getUniqueKey().isValid());
660 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500661 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Brian Salomonf7778972018-03-08 10:13:17 -0500662
Greg Daniel14b57212019-12-17 16:18:06 -0500663 GrSwizzle readSwizzle = caps->getReadSwizzle(rt->backendFormat(), grColorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400664
Greg Daniel14b57212019-12-17 16:18:06 -0500665 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(std::move(rt), origin, readSwizzle,
Greg Danielbaf8d992019-10-29 14:14:32 -0400666 UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500667}
668
Brian Salomon7578f3e2018-03-07 14:39:54 -0500669sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendTextureAsRenderTarget(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400670 const GrBackendTexture& backendTex, GrColorType grColorType,
671 GrSurfaceOrigin origin, int sampleCnt) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500672 if (this->isAbandoned()) {
673 return nullptr;
674 }
675
Brian Salomonf7778972018-03-08 10:13:17 -0500676 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500677 GrContext* direct = fImageContext->priv().asDirectContext();
678 if (!direct) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500679 return nullptr;
680 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500681
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400682 const GrCaps* caps = this->caps();
683
Robert Phillipsa41c6852019-02-07 10:44:10 -0500684 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
685
Brian Salomonf7778972018-03-08 10:13:17 -0500686 sk_sp<GrRenderTarget> rt =
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400687 resourceProvider->wrapBackendTextureAsRenderTarget(backendTex, sampleCnt, grColorType);
Brian Salomonf7778972018-03-08 10:13:17 -0500688 if (!rt) {
689 return nullptr;
Greg Danielf87651e2018-02-21 11:36:53 -0500690 }
Brian Salomonf7778972018-03-08 10:13:17 -0500691 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
692 SkASSERT(!rt->getUniqueKey().isValid());
Greg Danielb46add82019-01-02 14:51:29 -0500693 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500694 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielf87651e2018-02-21 11:36:53 -0500695
Greg Daniel14b57212019-12-17 16:18:06 -0500696 GrSwizzle readSwizzle = caps->getReadSwizzle(rt->backendFormat(), grColorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400697
Greg Daniel14b57212019-12-17 16:18:06 -0500698 return sk_sp<GrSurfaceProxy>(new GrRenderTargetProxy(std::move(rt), origin, readSwizzle,
Greg Danielbaf8d992019-10-29 14:14:32 -0400699 UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500700}
701
Greg Danielb46add82019-01-02 14:51:29 -0500702sk_sp<GrRenderTargetProxy> GrProxyProvider::wrapVulkanSecondaryCBAsRenderTarget(
703 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
704 if (this->isAbandoned()) {
705 return nullptr;
706 }
707
708 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500709 GrContext* direct = fImageContext->priv().asDirectContext();
710 if (!direct) {
Greg Danielb46add82019-01-02 14:51:29 -0500711 return nullptr;
712 }
713
Robert Phillipsa41c6852019-02-07 10:44:10 -0500714 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Danielb46add82019-01-02 14:51:29 -0500715
Robert Phillipsa41c6852019-02-07 10:44:10 -0500716 sk_sp<GrRenderTarget> rt = resourceProvider->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
717 vkInfo);
Greg Danielb46add82019-01-02 14:51:29 -0500718 if (!rt) {
719 return nullptr;
720 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500721
Greg Danielb46add82019-01-02 14:51:29 -0500722 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
723 SkASSERT(!rt->getUniqueKey().isValid());
724 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500725 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielb46add82019-01-02 14:51:29 -0500726
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400727 GrColorType colorType = SkColorTypeToGrColorType(imageInfo.colorType());
Greg Daniel14b57212019-12-17 16:18:06 -0500728 GrSwizzle readSwizzle = this->caps()->getReadSwizzle(rt->backendFormat(), colorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400729
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400730 if (!this->caps()->isFormatAsColorTypeRenderable(colorType, rt->backendFormat(),
731 rt->numSamples())) {
732 return nullptr;
733 }
734
Greg Danielb46add82019-01-02 14:51:29 -0500735 // All Vulkan surfaces uses top left origins.
Brian Salomonbeb7f522019-08-30 16:19:42 -0400736 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
Greg Daniel14b57212019-12-17 16:18:06 -0500737 std::move(rt), kTopLeft_GrSurfaceOrigin, readSwizzle, UseAllocator::kNo,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400738 GrRenderTargetProxy::WrapsVkSecondaryCB::kYes));
Brian Salomone8a766b2019-07-19 14:24:36 -0400739}
740
741sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
742 const GrBackendFormat& format,
743 const GrSurfaceDesc& desc,
Greg Danielce3ddaa2020-01-22 16:58:15 -0500744 GrSwizzle readSwizzle,
Brian Salomone8a766b2019-07-19 14:24:36 -0400745 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400746 int renderTargetSampleCnt,
Brian Salomone8a766b2019-07-19 14:24:36 -0400747 GrSurfaceOrigin origin,
748 GrMipMapped mipMapped,
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600749 GrMipMapsStatus mipMapsStatus,
Brian Salomone8a766b2019-07-19 14:24:36 -0400750 GrInternalSurfaceFlags surfaceFlags,
751 SkBackingFit fit,
752 SkBudgeted budgeted,
753 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400754 GrSurfaceProxy::UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500755 ASSERT_SINGLE_OWNER
756 if (this->isAbandoned()) {
757 return nullptr;
758 }
Robert Phillips777707b2018-01-17 11:40:14 -0500759 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
760 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400761
Robert Phillips0a15cc62019-07-30 12:49:10 -0400762 if (!format.isValid()) {
763 return nullptr;
764 }
765
Robert Phillipsa41c6852019-02-07 10:44:10 -0500766 if (desc.fWidth > this->caps()->maxTextureSize() ||
767 desc.fHeight > this->caps()->maxTextureSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400768 return nullptr;
769 }
770
Brian Salomonbeb7f522019-08-30 16:19:42 -0400771 if (renderable == GrRenderable::kYes) {
772 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(*this->caps(),
773 std::move(callback),
774 format,
775 desc,
776 renderTargetSampleCnt,
777 origin,
778 mipMapped,
779 mipMapsStatus,
Greg Daniel14b57212019-12-17 16:18:06 -0500780 readSwizzle,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400781 fit,
782 budgeted,
783 isProtected,
784 surfaceFlags,
785 useAllocator));
786 } else {
787 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(callback),
788 format,
789 desc,
790 origin,
791 mipMapped,
792 mipMapsStatus,
Greg Daniel14b57212019-12-17 16:18:06 -0500793 readSwizzle,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400794 fit,
795 budgeted,
796 isProtected,
797 surfaceFlags,
798 useAllocator));
799 }
Robert Phillips777707b2018-01-17 11:40:14 -0500800}
801
Robert Phillipse8fabb22018-02-04 14:33:21 -0500802sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy(
Brian Salomonbeb7f522019-08-30 16:19:42 -0400803 LazyInstantiateCallback&& callback,
804 const GrBackendFormat& format,
805 const GrSurfaceDesc& desc,
Greg Danielce3ddaa2020-01-22 16:58:15 -0500806 GrSwizzle readSwizzle,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400807 int sampleCnt,
808 GrSurfaceOrigin origin,
809 GrInternalSurfaceFlags surfaceFlags,
810 const TextureInfo* textureInfo,
811 GrMipMapsStatus mipMapsStatus,
812 SkBackingFit fit,
813 SkBudgeted budgeted,
814 GrProtected isProtected,
815 bool wrapsVkSecondaryCB,
816 UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500817 ASSERT_SINGLE_OWNER
818 if (this->isAbandoned()) {
819 return nullptr;
820 }
Robert Phillipse8fabb22018-02-04 14:33:21 -0500821 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
822 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400823
Robert Phillipsa41c6852019-02-07 10:44:10 -0500824 if (desc.fWidth > this->caps()->maxRenderTargetSize() ||
825 desc.fHeight > this->caps()->maxRenderTargetSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400826 return nullptr;
827 }
828
Brian Salomon7226c232018-07-30 13:13:17 -0400829 if (textureInfo) {
Greg Danielb085fa92019-03-05 16:55:12 -0500830 // Wrapped vulkan secondary command buffers don't support texturing since we won't have an
831 // actual VkImage to texture from.
832 SkASSERT(!wrapsVkSecondaryCB);
Brian Salomon7226c232018-07-30 13:13:17 -0400833 return sk_sp<GrRenderTargetProxy>(new GrTextureRenderTargetProxy(
Brian Salomonbeb7f522019-08-30 16:19:42 -0400834 *this->caps(), std::move(callback), format, desc, sampleCnt, origin,
Greg Daniel14b57212019-12-17 16:18:06 -0500835 textureInfo->fMipMapped, mipMapsStatus, readSwizzle, fit, budgeted, isProtected,
Greg Danielbaf8d992019-10-29 14:14:32 -0400836 surfaceFlags, useAllocator));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500837 }
838
Greg Danielb085fa92019-03-05 16:55:12 -0500839 GrRenderTargetProxy::WrapsVkSecondaryCB vkSCB =
840 wrapsVkSecondaryCB ? GrRenderTargetProxy::WrapsVkSecondaryCB::kYes
841 : GrRenderTargetProxy::WrapsVkSecondaryCB::kNo;
842
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400843 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
Greg Daniel14b57212019-12-17 16:18:06 -0500844 std::move(callback), format, desc, sampleCnt, origin, readSwizzle, fit, budgeted,
Greg Danielbaf8d992019-10-29 14:14:32 -0400845 isProtected, surfaceFlags, useAllocator, vkSCB));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500846}
847
Brian Salomonbeb7f522019-08-30 16:19:42 -0400848sk_sp<GrTextureProxy> GrProxyProvider::MakeFullyLazyProxy(LazyInstantiateCallback&& callback,
849 const GrBackendFormat& format,
Greg Danielce3ddaa2020-01-22 16:58:15 -0500850 GrSwizzle readSwizzle,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400851 GrRenderable renderable,
852 int renderTargetSampleCnt,
853 GrProtected isProtected,
854 GrSurfaceOrigin origin,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400855 const GrCaps& caps,
856 UseAllocator useAllocator) {
Robert Phillips0a15cc62019-07-30 12:49:10 -0400857 if (!format.isValid()) {
858 return nullptr;
859 }
860
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400861 SkASSERT(renderTargetSampleCnt == 1 || renderable == GrRenderable::kYes);
Robert Phillips777707b2018-01-17 11:40:14 -0500862 GrSurfaceDesc desc;
Robert Phillips10d17212019-04-24 14:09:10 -0400863 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNone;
Robert Phillips777707b2018-01-17 11:40:14 -0500864 desc.fWidth = -1;
865 desc.fHeight = -1;
Robert Phillips777707b2018-01-17 11:40:14 -0500866
Brian Salomonbeb7f522019-08-30 16:19:42 -0400867 if (GrRenderable::kYes == renderable) {
868 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
869 caps, std::move(callback), format, desc, renderTargetSampleCnt, origin,
Greg Daniel14b57212019-12-17 16:18:06 -0500870 GrMipMapped::kNo, GrMipMapsStatus::kNotAllocated, readSwizzle,
871 SkBackingFit::kApprox, SkBudgeted::kYes, isProtected, surfaceFlags, useAllocator));
Brian Salomonbeb7f522019-08-30 16:19:42 -0400872 } else {
873 return sk_sp<GrTextureProxy>(new GrTextureProxy(
874 std::move(callback), format, desc, origin, GrMipMapped::kNo,
Greg Daniel14b57212019-12-17 16:18:06 -0500875 GrMipMapsStatus::kNotAllocated, readSwizzle, SkBackingFit::kApprox,
876 SkBudgeted::kYes, isProtected, surfaceFlags, useAllocator));
Brian Salomonbeb7f522019-08-30 16:19:42 -0400877 }
Robert Phillips777707b2018-01-17 11:40:14 -0500878}
879
Robert Phillips427966a2018-12-20 17:20:43 -0500880void GrProxyProvider::processInvalidUniqueKey(const GrUniqueKey& key, GrTextureProxy* proxy,
881 InvalidateGPUResource invalidateGPUResource) {
Chris Dalton2de13dd2019-01-03 15:11:59 -0700882 SkASSERT(key.isValid());
883
Robert Phillips427966a2018-12-20 17:20:43 -0500884 if (!proxy) {
885 proxy = fUniquelyKeyedProxies.find(key);
886 }
Chris Dalton2de13dd2019-01-03 15:11:59 -0700887 SkASSERT(!proxy || proxy->getUniqueKey() == key);
888
889 // Locate the corresponding GrGpuResource (if it needs to be invalidated) before clearing the
890 // proxy's unique key. We must do it in this order because 'key' may alias the proxy's key.
891 sk_sp<GrGpuResource> invalidGpuResource;
892 if (InvalidateGPUResource::kYes == invalidateGPUResource) {
Brian Salomon01ceae92019-04-02 11:49:54 -0400893 GrContext* direct = fImageContext->priv().asDirectContext();
894 if (direct) {
895 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
896 invalidGpuResource = resourceProvider->findByUniqueKey<GrGpuResource>(key);
Chris Dalton2de13dd2019-01-03 15:11:59 -0700897 }
898 SkASSERT(!invalidGpuResource || invalidGpuResource->getUniqueKey() == key);
899 }
Robert Phillips427966a2018-12-20 17:20:43 -0500900
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500901 // Note: this method is called for the whole variety of GrGpuResources so often 'key'
902 // will not be in 'fUniquelyKeyedProxies'.
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500903 if (proxy) {
Robert Phillips427966a2018-12-20 17:20:43 -0500904 fUniquelyKeyedProxies.remove(key);
905 proxy->cacheAccess().clearUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500906 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500907
Chris Dalton2de13dd2019-01-03 15:11:59 -0700908 if (invalidGpuResource) {
909 invalidGpuResource->resourcePriv().removeUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500910 }
911}
912
Robert Phillipsa41c6852019-02-07 10:44:10 -0500913uint32_t GrProxyProvider::contextID() const {
914 return fImageContext->priv().contextID();
915}
916
917const GrCaps* GrProxyProvider::caps() const {
918 return fImageContext->priv().caps();
919}
920
921sk_sp<const GrCaps> GrProxyProvider::refCaps() const {
922 return fImageContext->priv().refCaps();
923}
924
Robert Phillipsa9162df2019-02-11 14:12:03 -0500925bool GrProxyProvider::isAbandoned() const {
926 return fImageContext->priv().abandoned();
927}
928
Robert Phillips0790f8a2018-09-18 13:11:03 -0400929void GrProxyProvider::orphanAllUniqueKeys() {
930 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
931 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
932 GrTextureProxy& tmp = *iter;
933
934 tmp.fProxyProvider = nullptr;
935 }
936}
937
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500938void GrProxyProvider::removeAllUniqueKeys() {
939 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
940 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
941 GrTextureProxy& tmp = *iter;
942
Chris Dalton2de13dd2019-01-03 15:11:59 -0700943 this->processInvalidUniqueKey(tmp.getUniqueKey(), &tmp, InvalidateGPUResource::kNo);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500944 }
945 SkASSERT(!fUniquelyKeyedProxies.count());
946}
Robert Phillipsa41c6852019-02-07 10:44:10 -0500947
948bool GrProxyProvider::renderingDirectly() const {
949 return fImageContext->priv().asDirectContext();
950}