blob: 7bbf53d28e583646763659685113560332e420c1 [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
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500253sk_sp<GrTextureProxy> GrProxyProvider::createTextureProxy(sk_sp<SkImage> srcImage,
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500254 int sampleCnt,
Greg Danielfb3abcd2018-02-02 15:48:33 -0500255 SkBudgeted budgeted,
Chris Daltond004e0b2018-09-27 09:28:03 -0600256 SkBackingFit fit,
257 GrInternalSurfaceFlags surfaceFlags) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500258 ASSERT_SINGLE_OWNER
259 SkASSERT(srcImage);
260
261 if (this->isAbandoned()) {
262 return nullptr;
263 }
264
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400265 const SkImageInfo& info = srcImage->imageInfo();
Brian Salomon2af3e702019-08-11 19:10:31 -0400266 GrColorType ct = SkColorTypeToGrColorType(info.colorType());
Greg Danielf87651e2018-02-21 11:36:53 -0500267
Brian Salomon96b383a2019-08-13 16:55:41 -0400268 GrBackendFormat format = this->caps()->getDefaultBackendFormat(ct, GrRenderable::kNo);
Greg Daniel0a7aa142018-02-21 13:02:32 -0500269
Robert Phillips0a15cc62019-07-30 12:49:10 -0400270 if (!format.isValid()) {
Brian Osmand29dcd12018-09-13 15:04:29 -0400271 SkBitmap copy8888;
272 if (!copy8888.tryAllocPixels(info.makeColorType(kRGBA_8888_SkColorType)) ||
273 !srcImage->readPixels(copy8888.pixmap(), 0, 0)) {
274 return nullptr;
275 }
276 copy8888.setImmutable();
277 srcImage = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode);
Brian Salomon2af3e702019-08-11 19:10:31 -0400278 ct = GrColorType::kRGBA_8888;
Brian Salomon96b383a2019-08-13 16:55:41 -0400279 format = this->caps()->getDefaultBackendFormat(ct, GrRenderable::kNo);
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400280 if (!format.isValid()) {
281 return nullptr;
282 }
Brian Osmand29dcd12018-09-13 15:04:29 -0400283 }
284
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500285 GrSurfaceDesc desc;
286 desc.fWidth = srcImage->width();
287 desc.fHeight = srcImage->height();
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500288
Greg Danielce3ddaa2020-01-22 16:58:15 -0500289 GrSwizzle swizzle = this->caps()->getReadSwizzle(format, ct);
290
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500291 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomon96b383a2019-08-13 16:55:41 -0400292 [desc, format, sampleCnt, budgeted, srcImage, fit,
Brian Salomon2af3e702019-08-11 19:10:31 -0400293 ct](GrResourceProvider* resourceProvider) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500294 SkPixmap pixMap;
295 SkAssertResult(srcImage->peekPixels(&pixMap));
296 GrMipLevel mipLevel = { pixMap.addr(), pixMap.rowBytes() };
297
Brian Salomonbeb7f522019-08-30 16:19:42 -0400298 return LazyCallbackResult(resourceProvider->createTexture(
Brian Salomona90382f2019-09-17 09:01:56 -0400299 desc, format, ct, GrRenderable::kNo, sampleCnt, budgeted, fit,
300 GrProtected::kNo, mipLevel));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500301 },
Greg Danielce3ddaa2020-01-22 16:58:15 -0500302 format, desc, swizzle, GrRenderable::kNo, sampleCnt, kTopLeft_GrSurfaceOrigin,
303 GrMipMapped::kNo, GrMipMapsStatus::kNotAllocated, surfaceFlags, fit, budgeted,
304 GrProtected::kNo, UseAllocator::kYes);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500305
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400306 if (!proxy) {
307 return nullptr;
308 }
309
Robert Phillipsa41c6852019-02-07 10:44:10 -0500310 GrContext* direct = fImageContext->priv().asDirectContext();
311 if (direct) {
312 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
313
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500314 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
315 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500316 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500317 return nullptr;
318 }
319 }
Robert Phillipsc1b60662018-06-26 10:20:08 -0400320
321 SkASSERT(proxy->width() == desc.fWidth);
322 SkASSERT(proxy->height() == desc.fHeight);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500323 return proxy;
324}
325
Brian Osmande496652019-03-22 13:42:33 -0400326sk_sp<GrTextureProxy> GrProxyProvider::createProxyFromBitmap(const SkBitmap& bitmap,
327 GrMipMapped mipMapped) {
Brian Osman412674f2019-02-07 15:34:58 -0500328 ASSERT_SINGLE_OWNER
Brian Osman7dcc6162019-03-25 10:12:57 -0400329 SkASSERT(GrMipMapped::kNo == mipMapped || this->caps()->mipMapSupport());
Brian Osman412674f2019-02-07 15:34:58 -0500330
331 if (this->isAbandoned()) {
332 return nullptr;
333 }
334
Brian Osman2b23c4b2018-06-01 12:25:08 -0400335 if (!SkImageInfoIsValid(bitmap.info())) {
Greg Daniela4ead652018-02-07 10:21:48 -0500336 return nullptr;
337 }
338
Brian Osmande496652019-03-22 13:42:33 -0400339 ATRACE_ANDROID_FRAMEWORK("Upload %sTexture [%ux%u]",
340 GrMipMapped::kYes == mipMapped ? "MipMap " : "",
341 bitmap.width(), bitmap.height());
Greg Daniela4ead652018-02-07 10:21:48 -0500342
343 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
344 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
345 // upload of the data to the gpu can happen at anytime and the bitmap may change by then.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500346 SkCopyPixelsMode copyMode = this->renderingDirectly() ? kNever_SkCopyPixelsMode
347 : kIfMutable_SkCopyPixelsMode;
Greg Daniela4ead652018-02-07 10:21:48 -0500348 sk_sp<SkImage> baseLevel = SkMakeImageFromRasterBitmap(bitmap, copyMode);
Greg Daniela4ead652018-02-07 10:21:48 -0500349 if (!baseLevel) {
350 return nullptr;
351 }
352
Brian Osmande496652019-03-22 13:42:33 -0400353 // If mips weren't requested (or this was too small to have any), then take the fast path
354 if (GrMipMapped::kNo == mipMapped ||
355 0 == SkMipMap::ComputeLevelCount(baseLevel->width(), baseLevel->height())) {
Brian Salomon96b383a2019-08-13 16:55:41 -0400356 return this->createTextureProxy(std::move(baseLevel), 1, SkBudgeted::kYes,
357 SkBackingFit::kExact);
Greg Daniela4ead652018-02-07 10:21:48 -0500358 }
359
Brian Osmand29dcd12018-09-13 15:04:29 -0400360 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info());
Greg Daniel3fc5df42019-06-14 09:42:17 -0400361
Greg Danielce3ddaa2020-01-22 16:58:15 -0500362 GrColorType grCT = SkColorTypeToGrColorType(bitmap.info().colorType());
363 GrBackendFormat format = this->caps()->getDefaultBackendFormat(grCT, GrRenderable::kNo);
Robert Phillips0a15cc62019-07-30 12:49:10 -0400364 if (!format.isValid()) {
Greg Daniel82c6b102020-01-21 10:33:22 -0500365 return nullptr;
Greg Daniel3fc5df42019-06-14 09:42:17 -0400366 }
367
Brian Osmand29dcd12018-09-13 15:04:29 -0400368 SkPixmap pixmap;
369 SkAssertResult(baseLevel->peekPixels(&pixmap));
370 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(pixmap, nullptr));
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400371 if (!mipmaps) {
372 return nullptr;
373 }
374
Greg Danielce3ddaa2020-01-22 16:58:15 -0500375 GrSwizzle readSwizzle = this->caps()->getReadSwizzle(format, grCT);
376
Greg Daniela4ead652018-02-07 10:21:48 -0500377 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomon4eb38b72019-08-05 12:58:39 -0400378 [desc, format, baseLevel, mipmaps](GrResourceProvider* resourceProvider) {
Brian Osman1b97f132018-09-13 17:33:48 +0000379 const int mipLevelCount = mipmaps->countLevels() + 1;
380 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
381
Greg Daniela4ead652018-02-07 10:21:48 -0500382 SkPixmap pixmap;
383 SkAssertResult(baseLevel->peekPixels(&pixmap));
384
Greg Daniela4ead652018-02-07 10:21:48 -0500385 texels[0].fPixels = pixmap.addr();
386 texels[0].fRowBytes = pixmap.rowBytes();
387
Brian Salomona90382f2019-09-17 09:01:56 -0400388 auto colorType = SkColorTypeToGrColorType(pixmap.colorType());
Greg Daniela4ead652018-02-07 10:21:48 -0500389 for (int i = 1; i < mipLevelCount; ++i) {
390 SkMipMap::Level generatedMipLevel;
391 mipmaps->getLevel(i - 1, &generatedMipLevel);
392 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
393 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
394 SkASSERT(texels[i].fPixels);
Brian Salomona90382f2019-09-17 09:01:56 -0400395 SkASSERT(generatedMipLevel.fPixmap.colorType() == pixmap.colorType());
Greg Daniela4ead652018-02-07 10:21:48 -0500396 }
Brian Salomonbeb7f522019-08-30 16:19:42 -0400397 return LazyCallbackResult(resourceProvider->createTexture(
Brian Salomona90382f2019-09-17 09:01:56 -0400398 desc, format, colorType, GrRenderable::kNo, 1, SkBudgeted::kYes,
399 GrProtected::kNo, texels.get(), mipLevelCount));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500400 },
Greg Danielce3ddaa2020-01-22 16:58:15 -0500401 format, desc, readSwizzle, GrRenderable::kNo, 1, kTopLeft_GrSurfaceOrigin,
402 GrMipMapped::kYes, GrMipMapsStatus::kValid, GrInternalSurfaceFlags::kNone,
403 SkBackingFit::kExact, SkBudgeted::kYes, GrProtected::kNo, UseAllocator::kYes);
Greg Daniela4ead652018-02-07 10:21:48 -0500404
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400405 if (!proxy) {
406 return nullptr;
407 }
408
Robert Phillipsa41c6852019-02-07 10:44:10 -0500409 GrContext* direct = fImageContext->priv().asDirectContext();
410 if (direct) {
411 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Daniela4ead652018-02-07 10:21:48 -0500412 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
413 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500414 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Greg Daniela4ead652018-02-07 10:21:48 -0500415 return nullptr;
416 }
417 }
418 return proxy;
419}
420
Greg Daniel4065d452018-11-16 15:43:41 -0500421sk_sp<GrTextureProxy> GrProxyProvider::createProxy(const GrBackendFormat& format,
422 const GrSurfaceDesc& desc,
Greg Daniel47c20e82020-01-21 14:29:57 -0500423 GrSwizzle readSwizzle,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400424 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400425 int renderTargetSampleCnt,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500426 GrSurfaceOrigin origin,
Greg Danielf6f7b672018-02-15 13:06:26 -0500427 GrMipMapped mipMapped,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500428 SkBackingFit fit,
429 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -0400430 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400431 GrInternalSurfaceFlags surfaceFlags,
432 GrSurfaceProxy::UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500433 ASSERT_SINGLE_OWNER
434 if (this->isAbandoned()) {
435 return nullptr;
436 }
437
Robert Phillips8ff8bcc2019-07-29 17:03:35 -0400438 const GrCaps* caps = this->caps();
439
440 if (caps->isFormatCompressed(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400441 // Deferred proxies for compressed textures are not supported.
442 return nullptr;
443 }
Robert Phillips0902c982019-07-16 07:47:56 -0400444
Greg Danielf6f7b672018-02-15 13:06:26 -0500445 if (GrMipMapped::kYes == mipMapped) {
446 // SkMipMap doesn't include the base level in the level count so we have to add 1
447 int mipCount = SkMipMap::ComputeLevelCount(desc.fWidth, desc.fHeight) + 1;
448 if (1 == mipCount) {
449 mipMapped = GrMipMapped::kNo;
450 }
451 }
452
Greg Daniel9a48beb2020-01-16 16:57:16 -0500453 if (!caps->validateSurfaceParams({desc.fWidth, desc.fHeight}, format, renderable,
Greg Daniel6fa62e22019-08-07 15:52:37 -0400454 renderTargetSampleCnt, mipMapped)) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500455 return nullptr;
456 }
Brian Salomon3a2cc2c2018-02-03 00:25:12 +0000457 GrSurfaceDesc copyDesc = desc;
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600458 GrMipMapsStatus mipMapsStatus = (GrMipMapped::kYes == mipMapped)
459 ? GrMipMapsStatus::kDirty
460 : GrMipMapsStatus::kNotAllocated;
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400461 if (renderable == GrRenderable::kYes) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400462 renderTargetSampleCnt =
Greg Daniel6fa62e22019-08-07 15:52:37 -0400463 caps->getRenderTargetSampleCount(renderTargetSampleCnt, format);
464 SkASSERT(renderTargetSampleCnt);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500465 // We know anything we instantiate later from this deferred path will be
466 // both texturable and renderable
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400467 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600468 *caps, format, copyDesc, renderTargetSampleCnt, origin, mipMapped, mipMapsStatus,
Greg Daniel14b57212019-12-17 16:18:06 -0500469 readSwizzle, fit, budgeted, isProtected, surfaceFlags, useAllocator));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500470 }
471
Brian Salomonbeb7f522019-08-30 16:19:42 -0400472 return sk_sp<GrTextureProxy>(new GrTextureProxy(format, copyDesc, origin, mipMapped,
Greg Daniel14b57212019-12-17 16:18:06 -0500473 mipMapsStatus, readSwizzle, fit, budgeted,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400474 isProtected, surfaceFlags, useAllocator));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500475}
476
Brian Salomonbb8dde82019-06-27 10:52:13 -0400477sk_sp<GrTextureProxy> GrProxyProvider::createCompressedTextureProxy(
Robert Phillips3a833922020-01-21 15:25:58 -0500478 SkISize dimensions, SkBudgeted budgeted, GrMipMapped mipMapped, GrProtected isProtected,
Robert Phillipse4720c62020-01-14 14:33:24 -0500479 SkImage::CompressionType compressionType, sk_sp<SkData> data) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500480 ASSERT_SINGLE_OWNER
481 if (this->isAbandoned()) {
482 return nullptr;
483 }
Brian Salomonbb8dde82019-06-27 10:52:13 -0400484
485 GrSurfaceDesc desc;
Robert Phillips9f744f72019-12-19 19:14:33 -0500486 desc.fWidth = dimensions.width();
487 desc.fHeight = dimensions.height();
Brian Salomonbb8dde82019-06-27 10:52:13 -0400488
Robert Phillips8ff8bcc2019-07-29 17:03:35 -0400489 GrBackendFormat format = this->caps()->getBackendFormatFromCompressionType(compressionType);
490
Greg Daniel7bfc9132019-08-14 14:23:53 -0400491 if (!this->caps()->isFormatTexturable(format)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500492 return nullptr;
493 }
494
Robert Phillipse4720c62020-01-14 14:33:24 -0500495 GrMipMapsStatus mipMapsStatus = (GrMipMapped::kYes == mipMapped)
496 ? GrMipMapsStatus::kValid
497 : GrMipMapsStatus::kNotAllocated;
498
Jim Van Verthee06b332019-01-18 10:36:32 -0500499 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Robert Phillips3a833922020-01-21 15:25:58 -0500500 [dimensions, format, budgeted, mipMapped, isProtected, data]
501 (GrResourceProvider* resourceProvider) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400502 return LazyCallbackResult(resourceProvider->createCompressedTexture(
Robert Phillips3a833922020-01-21 15:25:58 -0500503 dimensions, format, budgeted, mipMapped, isProtected, data.get()));
Brian Salomonbb8dde82019-06-27 10:52:13 -0400504 },
Greg Danielce3ddaa2020-01-22 16:58:15 -0500505 format, desc, GrSwizzle(), GrRenderable::kNo, 1, kTopLeft_GrSurfaceOrigin, mipMapped,
Robert Phillipse4720c62020-01-14 14:33:24 -0500506 mipMapsStatus, GrInternalSurfaceFlags::kReadOnly, SkBackingFit::kExact,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400507 SkBudgeted::kYes, GrProtected::kNo, UseAllocator::kYes);
Jim Van Verthee06b332019-01-18 10:36:32 -0500508
509 if (!proxy) {
510 return nullptr;
511 }
512
Robert Phillipsa41c6852019-02-07 10:44:10 -0500513 GrContext* direct = fImageContext->priv().asDirectContext();
514 if (direct) {
515 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Jim Van Verthee06b332019-01-18 10:36:32 -0500516 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
517 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500518 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500519 return nullptr;
520 }
521 }
522 return proxy;
523}
524
Brian Salomon7578f3e2018-03-07 14:39:54 -0500525sk_sp<GrTextureProxy> GrProxyProvider::wrapBackendTexture(const GrBackendTexture& backendTex,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400526 GrColorType grColorType,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500527 GrSurfaceOrigin origin,
528 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500529 GrWrapCacheable cacheable,
Brian Salomonc67c31c2018-12-06 10:00:03 -0500530 GrIOType ioType,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500531 ReleaseProc releaseProc,
532 ReleaseContext releaseCtx) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500533 SkASSERT(ioType != kWrite_GrIOType);
Robert Phillipsf9bec202018-01-16 09:21:01 -0500534 if (this->isAbandoned()) {
535 return nullptr;
536 }
537
Brian Salomonf7778972018-03-08 10:13:17 -0500538 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500539 GrContext* direct = fImageContext->priv().asDirectContext();
540 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500541 return nullptr;
542 }
543
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400544 const GrCaps* caps = this->caps();
545
Robert Phillipsa41c6852019-02-07 10:44:10 -0500546 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
547
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500548 sk_sp<GrTexture> tex =
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400549 resourceProvider->wrapBackendTexture(backendTex, grColorType,
550 ownership, cacheable, ioType);
Brian Salomonf7778972018-03-08 10:13:17 -0500551 if (!tex) {
552 return nullptr;
553 }
Robert Phillipsadbe1322018-01-17 13:35:46 -0500554
Greg Daniel6a0176b2018-01-30 09:28:44 -0500555 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500556 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel6a0176b2018-01-30 09:28:44 -0500557 }
558
Brian Salomonf7778972018-03-08 10:13:17 -0500559 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
560 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500561 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500562
Greg Daniel14b57212019-12-17 16:18:06 -0500563 GrSwizzle readSwizzle = caps->getReadSwizzle(tex->backendFormat(), grColorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400564
Brian Salomonbeb7f522019-08-30 16:19:42 -0400565 return sk_sp<GrTextureProxy>(
Greg Daniel14b57212019-12-17 16:18:06 -0500566 new GrTextureProxy(std::move(tex), origin, readSwizzle, UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500567}
568
Robert Phillipsead321b2019-12-19 10:16:32 -0500569sk_sp<GrTextureProxy> GrProxyProvider::wrapCompressedBackendTexture(const GrBackendTexture& beTex,
570 GrSurfaceOrigin origin,
571 GrWrapOwnership ownership,
572 GrWrapCacheable cacheable,
573 ReleaseProc releaseProc,
574 ReleaseContext releaseCtx) {
575 if (this->isAbandoned()) {
576 return nullptr;
577 }
578
579 // This is only supported on a direct GrContext.
580 GrContext* direct = fImageContext->priv().asDirectContext();
581 if (!direct) {
582 return nullptr;
583 }
584
Robert Phillipsb0855272020-01-15 12:56:52 -0500585 const GrCaps* caps = this->caps();
586
Robert Phillipsead321b2019-12-19 10:16:32 -0500587 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
588
589 sk_sp<GrTexture> tex = resourceProvider->wrapCompressedBackendTexture(beTex, ownership,
590 cacheable);
591 if (!tex) {
592 return nullptr;
593 }
594
595 if (releaseProc) {
596 tex->setRelease(releaseProc, releaseCtx);
597 }
598
599 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
600 // Make sure we match how we created the proxy with SkBudgeted::kNo
601 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
602
Robert Phillipsb0855272020-01-15 12:56:52 -0500603 SkImage::CompressionType compressionType = caps->compressionType(beTex.getBackendFormat());
604
Robert Phillips99dead92020-01-27 16:11:57 -0500605 GrSwizzle texSwizzle = SkCompressionTypeIsOpaque(compressionType) ? GrSwizzle::RGB1()
Robert Phillipsb0855272020-01-15 12:56:52 -0500606 : GrSwizzle::RGBA();
Robert Phillipsead321b2019-12-19 10:16:32 -0500607
608 return sk_sp<GrTextureProxy>(
609 new GrTextureProxy(std::move(tex), origin, texSwizzle, UseAllocator::kNo));
610}
611
Brian Salomon7578f3e2018-03-07 14:39:54 -0500612sk_sp<GrTextureProxy> GrProxyProvider::wrapRenderableBackendTexture(
Brian Salomon02bd2952018-03-07 15:20:21 -0500613 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400614 GrColorType colorType, GrWrapOwnership ownership, GrWrapCacheable cacheable,
615 ReleaseProc releaseProc, ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500616 if (this->isAbandoned()) {
617 return nullptr;
618 }
619
Brian Salomonf7778972018-03-08 10:13:17 -0500620 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500621 GrContext* direct = fImageContext->priv().asDirectContext();
622 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500623 return nullptr;
624 }
625
Robert Phillips0902c982019-07-16 07:47:56 -0400626 const GrCaps* caps = this->caps();
627
Robert Phillipsa41c6852019-02-07 10:44:10 -0500628 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
629
Greg Daniel6fa62e22019-08-07 15:52:37 -0400630 // TODO: This should have been checked and validated before getting into GrProxyProvider.
631 if (!caps->isFormatAsColorTypeRenderable(colorType, backendTex.getBackendFormat(), sampleCnt)) {
Greg Danielf87651e2018-02-21 11:36:53 -0500632 return nullptr;
633 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500634
Greg Daniel6fa62e22019-08-07 15:52:37 -0400635 sampleCnt = caps->getRenderTargetSampleCount(sampleCnt, backendTex.getBackendFormat());
636 SkASSERT(sampleCnt);
637
Robert Phillipsa41c6852019-02-07 10:44:10 -0500638 sk_sp<GrTexture> tex = resourceProvider->wrapRenderableBackendTexture(backendTex, sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400639 colorType, ownership,
640 cacheable);
Brian Salomonf7778972018-03-08 10:13:17 -0500641 if (!tex) {
642 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500643 }
644
Greg Daniel8ce79912019-02-05 10:08:43 -0500645 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500646 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500647 }
648
Brian Salomonf7778972018-03-08 10:13:17 -0500649 SkASSERT(tex->asRenderTarget()); // A GrTextureRenderTarget
650 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500651 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Greg Daniel6abda432018-02-15 14:55:00 -0500652
Greg Daniel14b57212019-12-17 16:18:06 -0500653 GrSwizzle readSwizzle = caps->getReadSwizzle(tex->backendFormat(), colorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400654
Greg Daniel14b57212019-12-17 16:18:06 -0500655 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin, readSwizzle,
Greg Danielbaf8d992019-10-29 14:14:32 -0400656 UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500657}
658
Brian Salomon7578f3e2018-03-07 14:39:54 -0500659sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400660 const GrBackendRenderTarget& backendRT, GrColorType grColorType,
661 GrSurfaceOrigin origin, ReleaseProc releaseProc, ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500662 if (this->isAbandoned()) {
663 return nullptr;
664 }
665
Brian Salomonf7778972018-03-08 10:13:17 -0500666 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500667 GrContext* direct = fImageContext->priv().asDirectContext();
668 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500669 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500670 }
671
Robert Phillips62221e72019-07-24 15:07:38 -0400672 const GrCaps* caps = this->caps();
673
Robert Phillipsa41c6852019-02-07 10:44:10 -0500674 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
675
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400676 sk_sp<GrRenderTarget> rt = resourceProvider->wrapBackendRenderTarget(backendRT, grColorType);
Brian Salomonf7778972018-03-08 10:13:17 -0500677 if (!rt) {
678 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500679 }
Greg Daniel8ce79912019-02-05 10:08:43 -0500680
Greg Daniel8ce79912019-02-05 10:08:43 -0500681 if (releaseProc) {
Brian Salomon2ca31f82019-03-05 13:28:58 -0500682 rt->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500683 }
684
Brian Salomonf7778972018-03-08 10:13:17 -0500685 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
686 SkASSERT(!rt->getUniqueKey().isValid());
687 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500688 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Brian Salomonf7778972018-03-08 10:13:17 -0500689
Greg Daniel14b57212019-12-17 16:18:06 -0500690 GrSwizzle readSwizzle = caps->getReadSwizzle(rt->backendFormat(), grColorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400691
Greg Daniel14b57212019-12-17 16:18:06 -0500692 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(std::move(rt), origin, readSwizzle,
Greg Danielbaf8d992019-10-29 14:14:32 -0400693 UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500694}
695
Brian Salomon7578f3e2018-03-07 14:39:54 -0500696sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendTextureAsRenderTarget(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400697 const GrBackendTexture& backendTex, GrColorType grColorType,
698 GrSurfaceOrigin origin, int sampleCnt) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500699 if (this->isAbandoned()) {
700 return nullptr;
701 }
702
Brian Salomonf7778972018-03-08 10:13:17 -0500703 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500704 GrContext* direct = fImageContext->priv().asDirectContext();
705 if (!direct) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500706 return nullptr;
707 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500708
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400709 const GrCaps* caps = this->caps();
710
Robert Phillipsa41c6852019-02-07 10:44:10 -0500711 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
712
Brian Salomonf7778972018-03-08 10:13:17 -0500713 sk_sp<GrRenderTarget> rt =
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400714 resourceProvider->wrapBackendTextureAsRenderTarget(backendTex, sampleCnt, grColorType);
Brian Salomonf7778972018-03-08 10:13:17 -0500715 if (!rt) {
716 return nullptr;
Greg Danielf87651e2018-02-21 11:36:53 -0500717 }
Brian Salomonf7778972018-03-08 10:13:17 -0500718 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
719 SkASSERT(!rt->getUniqueKey().isValid());
Greg Danielb46add82019-01-02 14:51:29 -0500720 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500721 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielf87651e2018-02-21 11:36:53 -0500722
Greg Daniel14b57212019-12-17 16:18:06 -0500723 GrSwizzle readSwizzle = caps->getReadSwizzle(rt->backendFormat(), grColorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400724
Greg Daniel14b57212019-12-17 16:18:06 -0500725 return sk_sp<GrSurfaceProxy>(new GrRenderTargetProxy(std::move(rt), origin, readSwizzle,
Greg Danielbaf8d992019-10-29 14:14:32 -0400726 UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500727}
728
Greg Danielb46add82019-01-02 14:51:29 -0500729sk_sp<GrRenderTargetProxy> GrProxyProvider::wrapVulkanSecondaryCBAsRenderTarget(
730 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
731 if (this->isAbandoned()) {
732 return nullptr;
733 }
734
735 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500736 GrContext* direct = fImageContext->priv().asDirectContext();
737 if (!direct) {
Greg Danielb46add82019-01-02 14:51:29 -0500738 return nullptr;
739 }
740
Robert Phillipsa41c6852019-02-07 10:44:10 -0500741 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Danielb46add82019-01-02 14:51:29 -0500742
Robert Phillipsa41c6852019-02-07 10:44:10 -0500743 sk_sp<GrRenderTarget> rt = resourceProvider->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
744 vkInfo);
Greg Danielb46add82019-01-02 14:51:29 -0500745 if (!rt) {
746 return nullptr;
747 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500748
Greg Danielb46add82019-01-02 14:51:29 -0500749 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
750 SkASSERT(!rt->getUniqueKey().isValid());
751 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500752 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielb46add82019-01-02 14:51:29 -0500753
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400754 GrColorType colorType = SkColorTypeToGrColorType(imageInfo.colorType());
Greg Daniel14b57212019-12-17 16:18:06 -0500755 GrSwizzle readSwizzle = this->caps()->getReadSwizzle(rt->backendFormat(), colorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400756
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400757 if (!this->caps()->isFormatAsColorTypeRenderable(colorType, rt->backendFormat(),
758 rt->numSamples())) {
759 return nullptr;
760 }
761
Greg Danielb46add82019-01-02 14:51:29 -0500762 // All Vulkan surfaces uses top left origins.
Brian Salomonbeb7f522019-08-30 16:19:42 -0400763 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
Greg Daniel14b57212019-12-17 16:18:06 -0500764 std::move(rt), kTopLeft_GrSurfaceOrigin, readSwizzle, UseAllocator::kNo,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400765 GrRenderTargetProxy::WrapsVkSecondaryCB::kYes));
Brian Salomone8a766b2019-07-19 14:24:36 -0400766}
767
768sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
769 const GrBackendFormat& format,
770 const GrSurfaceDesc& desc,
Greg Danielce3ddaa2020-01-22 16:58:15 -0500771 GrSwizzle readSwizzle,
Brian Salomone8a766b2019-07-19 14:24:36 -0400772 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400773 int renderTargetSampleCnt,
Brian Salomone8a766b2019-07-19 14:24:36 -0400774 GrSurfaceOrigin origin,
775 GrMipMapped mipMapped,
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600776 GrMipMapsStatus mipMapsStatus,
Brian Salomone8a766b2019-07-19 14:24:36 -0400777 GrInternalSurfaceFlags surfaceFlags,
778 SkBackingFit fit,
779 SkBudgeted budgeted,
780 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400781 GrSurfaceProxy::UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500782 ASSERT_SINGLE_OWNER
783 if (this->isAbandoned()) {
784 return nullptr;
785 }
Robert Phillips777707b2018-01-17 11:40:14 -0500786 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
787 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400788
Robert Phillips0a15cc62019-07-30 12:49:10 -0400789 if (!format.isValid()) {
790 return nullptr;
791 }
792
Robert Phillipsa41c6852019-02-07 10:44:10 -0500793 if (desc.fWidth > this->caps()->maxTextureSize() ||
794 desc.fHeight > this->caps()->maxTextureSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400795 return nullptr;
796 }
797
Brian Salomonbeb7f522019-08-30 16:19:42 -0400798 if (renderable == GrRenderable::kYes) {
799 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(*this->caps(),
800 std::move(callback),
801 format,
802 desc,
803 renderTargetSampleCnt,
804 origin,
805 mipMapped,
806 mipMapsStatus,
Greg Daniel14b57212019-12-17 16:18:06 -0500807 readSwizzle,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400808 fit,
809 budgeted,
810 isProtected,
811 surfaceFlags,
812 useAllocator));
813 } else {
814 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(callback),
815 format,
816 desc,
817 origin,
818 mipMapped,
819 mipMapsStatus,
Greg Daniel14b57212019-12-17 16:18:06 -0500820 readSwizzle,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400821 fit,
822 budgeted,
823 isProtected,
824 surfaceFlags,
825 useAllocator));
826 }
Robert Phillips777707b2018-01-17 11:40:14 -0500827}
828
Robert Phillipse8fabb22018-02-04 14:33:21 -0500829sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy(
Brian Salomonbeb7f522019-08-30 16:19:42 -0400830 LazyInstantiateCallback&& callback,
831 const GrBackendFormat& format,
832 const GrSurfaceDesc& desc,
Greg Danielce3ddaa2020-01-22 16:58:15 -0500833 GrSwizzle readSwizzle,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400834 int sampleCnt,
835 GrSurfaceOrigin origin,
836 GrInternalSurfaceFlags surfaceFlags,
837 const TextureInfo* textureInfo,
838 GrMipMapsStatus mipMapsStatus,
839 SkBackingFit fit,
840 SkBudgeted budgeted,
841 GrProtected isProtected,
842 bool wrapsVkSecondaryCB,
843 UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500844 ASSERT_SINGLE_OWNER
845 if (this->isAbandoned()) {
846 return nullptr;
847 }
Robert Phillipse8fabb22018-02-04 14:33:21 -0500848 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
849 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400850
Robert Phillipsa41c6852019-02-07 10:44:10 -0500851 if (desc.fWidth > this->caps()->maxRenderTargetSize() ||
852 desc.fHeight > this->caps()->maxRenderTargetSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400853 return nullptr;
854 }
855
Brian Salomon7226c232018-07-30 13:13:17 -0400856 if (textureInfo) {
Greg Danielb085fa92019-03-05 16:55:12 -0500857 // Wrapped vulkan secondary command buffers don't support texturing since we won't have an
858 // actual VkImage to texture from.
859 SkASSERT(!wrapsVkSecondaryCB);
Brian Salomon7226c232018-07-30 13:13:17 -0400860 return sk_sp<GrRenderTargetProxy>(new GrTextureRenderTargetProxy(
Brian Salomonbeb7f522019-08-30 16:19:42 -0400861 *this->caps(), std::move(callback), format, desc, sampleCnt, origin,
Greg Daniel14b57212019-12-17 16:18:06 -0500862 textureInfo->fMipMapped, mipMapsStatus, readSwizzle, fit, budgeted, isProtected,
Greg Danielbaf8d992019-10-29 14:14:32 -0400863 surfaceFlags, useAllocator));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500864 }
865
Greg Danielb085fa92019-03-05 16:55:12 -0500866 GrRenderTargetProxy::WrapsVkSecondaryCB vkSCB =
867 wrapsVkSecondaryCB ? GrRenderTargetProxy::WrapsVkSecondaryCB::kYes
868 : GrRenderTargetProxy::WrapsVkSecondaryCB::kNo;
869
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400870 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
Greg Daniel14b57212019-12-17 16:18:06 -0500871 std::move(callback), format, desc, sampleCnt, origin, readSwizzle, fit, budgeted,
Greg Danielbaf8d992019-10-29 14:14:32 -0400872 isProtected, surfaceFlags, useAllocator, vkSCB));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500873}
874
Brian Salomonbeb7f522019-08-30 16:19:42 -0400875sk_sp<GrTextureProxy> GrProxyProvider::MakeFullyLazyProxy(LazyInstantiateCallback&& callback,
876 const GrBackendFormat& format,
Greg Danielce3ddaa2020-01-22 16:58:15 -0500877 GrSwizzle readSwizzle,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400878 GrRenderable renderable,
879 int renderTargetSampleCnt,
880 GrProtected isProtected,
881 GrSurfaceOrigin origin,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400882 const GrCaps& caps,
883 UseAllocator useAllocator) {
Robert Phillips0a15cc62019-07-30 12:49:10 -0400884 if (!format.isValid()) {
885 return nullptr;
886 }
887
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400888 SkASSERT(renderTargetSampleCnt == 1 || renderable == GrRenderable::kYes);
Robert Phillips777707b2018-01-17 11:40:14 -0500889 GrSurfaceDesc desc;
Robert Phillips10d17212019-04-24 14:09:10 -0400890 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNone;
Robert Phillips777707b2018-01-17 11:40:14 -0500891 desc.fWidth = -1;
892 desc.fHeight = -1;
Robert Phillips777707b2018-01-17 11:40:14 -0500893
Brian Salomonbeb7f522019-08-30 16:19:42 -0400894 if (GrRenderable::kYes == renderable) {
895 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
896 caps, std::move(callback), format, desc, renderTargetSampleCnt, origin,
Greg Daniel14b57212019-12-17 16:18:06 -0500897 GrMipMapped::kNo, GrMipMapsStatus::kNotAllocated, readSwizzle,
898 SkBackingFit::kApprox, SkBudgeted::kYes, isProtected, surfaceFlags, useAllocator));
Brian Salomonbeb7f522019-08-30 16:19:42 -0400899 } else {
900 return sk_sp<GrTextureProxy>(new GrTextureProxy(
901 std::move(callback), format, desc, origin, GrMipMapped::kNo,
Greg Daniel14b57212019-12-17 16:18:06 -0500902 GrMipMapsStatus::kNotAllocated, readSwizzle, SkBackingFit::kApprox,
903 SkBudgeted::kYes, isProtected, surfaceFlags, useAllocator));
Brian Salomonbeb7f522019-08-30 16:19:42 -0400904 }
Robert Phillips777707b2018-01-17 11:40:14 -0500905}
906
Robert Phillips427966a2018-12-20 17:20:43 -0500907void GrProxyProvider::processInvalidUniqueKey(const GrUniqueKey& key, GrTextureProxy* proxy,
908 InvalidateGPUResource invalidateGPUResource) {
Chris Dalton2de13dd2019-01-03 15:11:59 -0700909 SkASSERT(key.isValid());
910
Robert Phillips427966a2018-12-20 17:20:43 -0500911 if (!proxy) {
912 proxy = fUniquelyKeyedProxies.find(key);
913 }
Chris Dalton2de13dd2019-01-03 15:11:59 -0700914 SkASSERT(!proxy || proxy->getUniqueKey() == key);
915
916 // Locate the corresponding GrGpuResource (if it needs to be invalidated) before clearing the
917 // proxy's unique key. We must do it in this order because 'key' may alias the proxy's key.
918 sk_sp<GrGpuResource> invalidGpuResource;
919 if (InvalidateGPUResource::kYes == invalidateGPUResource) {
Brian Salomon01ceae92019-04-02 11:49:54 -0400920 GrContext* direct = fImageContext->priv().asDirectContext();
921 if (direct) {
922 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
923 invalidGpuResource = resourceProvider->findByUniqueKey<GrGpuResource>(key);
Chris Dalton2de13dd2019-01-03 15:11:59 -0700924 }
925 SkASSERT(!invalidGpuResource || invalidGpuResource->getUniqueKey() == key);
926 }
Robert Phillips427966a2018-12-20 17:20:43 -0500927
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500928 // Note: this method is called for the whole variety of GrGpuResources so often 'key'
929 // will not be in 'fUniquelyKeyedProxies'.
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500930 if (proxy) {
Robert Phillips427966a2018-12-20 17:20:43 -0500931 fUniquelyKeyedProxies.remove(key);
932 proxy->cacheAccess().clearUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500933 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500934
Chris Dalton2de13dd2019-01-03 15:11:59 -0700935 if (invalidGpuResource) {
936 invalidGpuResource->resourcePriv().removeUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500937 }
938}
939
Robert Phillipsa41c6852019-02-07 10:44:10 -0500940uint32_t GrProxyProvider::contextID() const {
941 return fImageContext->priv().contextID();
942}
943
944const GrCaps* GrProxyProvider::caps() const {
945 return fImageContext->priv().caps();
946}
947
948sk_sp<const GrCaps> GrProxyProvider::refCaps() const {
949 return fImageContext->priv().refCaps();
950}
951
Robert Phillipsa9162df2019-02-11 14:12:03 -0500952bool GrProxyProvider::isAbandoned() const {
953 return fImageContext->priv().abandoned();
954}
955
Robert Phillips0790f8a2018-09-18 13:11:03 -0400956void GrProxyProvider::orphanAllUniqueKeys() {
957 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
958 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
959 GrTextureProxy& tmp = *iter;
960
961 tmp.fProxyProvider = nullptr;
962 }
963}
964
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500965void GrProxyProvider::removeAllUniqueKeys() {
966 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
967 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
968 GrTextureProxy& tmp = *iter;
969
Chris Dalton2de13dd2019-01-03 15:11:59 -0700970 this->processInvalidUniqueKey(tmp.getUniqueKey(), &tmp, InvalidateGPUResource::kNo);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500971 }
972 SkASSERT(!fUniquelyKeyedProxies.count());
973}
Robert Phillipsa41c6852019-02-07 10:44:10 -0500974
975bool GrProxyProvider::renderingDirectly() const {
976 return fImageContext->priv().asDirectContext();
977}