blob: 8cadf8f35e59c36204531e8b692b33bcc9b9569c [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"
19#include "src/core/SkImagePriv.h"
20#include "src/core/SkMipMap.h"
21#include "src/core/SkTraceEvent.h"
22#include "src/gpu/GrCaps.h"
23#include "src/gpu/GrContextPriv.h"
24#include "src/gpu/GrImageContextPriv.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040025#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/gpu/GrResourceProvider.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040027#include "src/gpu/GrSurfaceProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/gpu/GrSurfaceProxyPriv.h"
29#include "src/gpu/GrTextureProxyCacheAccess.h"
30#include "src/gpu/GrTextureRenderTargetProxy.h"
31#include "src/gpu/SkGr.h"
32#include "src/image/SkImage_Base.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050033
34#define ASSERT_SINGLE_OWNER \
Robert Phillipsa41c6852019-02-07 10:44:10 -050035 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fImageContext->priv().singleOwner());)
Robert Phillips1afd4cd2018-01-08 13:40:32 -050036
Robert Phillipsa9162df2019-02-11 14:12:03 -050037GrProxyProvider::GrProxyProvider(GrImageContext* imageContext) : fImageContext(imageContext) {}
Robert Phillips1afd4cd2018-01-08 13:40:32 -050038
39GrProxyProvider::~GrProxyProvider() {
Robert Phillipsa41c6852019-02-07 10:44:10 -050040 if (this->renderingDirectly()) {
Robert Phillips0790f8a2018-09-18 13:11:03 -040041 // In DDL-mode a proxy provider can still have extant uniquely keyed proxies (since
42 // they need their unique keys to, potentially, find a cached resource when the
43 // DDL is played) but, in non-DDL-mode they should all have been cleaned up by this point.
44 SkASSERT(!fUniquelyKeyedProxies.count());
45 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050046}
47
Robert Phillipsadbe1322018-01-17 13:35:46 -050048bool GrProxyProvider::assignUniqueKeyToProxy(const GrUniqueKey& key, GrTextureProxy* proxy) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050049 ASSERT_SINGLE_OWNER
50 SkASSERT(key.isValid());
51 if (this->isAbandoned() || !proxy) {
Robert Phillipsadbe1322018-01-17 13:35:46 -050052 return false;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050053 }
54
Robert Phillipsa41c6852019-02-07 10:44:10 -050055#ifdef SK_DEBUG
56 {
57 GrContext* direct = fImageContext->priv().asDirectContext();
58 if (direct) {
59 GrResourceCache* resourceCache = direct->priv().getResourceCache();
60 // If there is already a GrResource with this key then the caller has violated the
61 // normal usage pattern of uniquely keyed resources (e.g., they have created one w/o
62 // first seeing if it already existed in the cache).
63 SkASSERT(!resourceCache->findAndRefUniqueResource(key));
64 }
65 }
66#endif
Robert Phillips1afd4cd2018-01-08 13:40:32 -050067
Robert Phillips1afd4cd2018-01-08 13:40:32 -050068 SkASSERT(!fUniquelyKeyedProxies.find(key)); // multiple proxies can't get the same key
69
70 proxy->cacheAccess().setUniqueKey(this, key);
71 SkASSERT(proxy->getUniqueKey() == key);
72 fUniquelyKeyedProxies.add(proxy);
Robert Phillipsadbe1322018-01-17 13:35:46 -050073 return true;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050074}
75
76void GrProxyProvider::adoptUniqueKeyFromSurface(GrTextureProxy* proxy, const GrSurface* surf) {
77 SkASSERT(surf->getUniqueKey().isValid());
78 proxy->cacheAccess().setUniqueKey(this, surf->getUniqueKey());
79 SkASSERT(proxy->getUniqueKey() == surf->getUniqueKey());
80 // multiple proxies can't get the same key
81 SkASSERT(!fUniquelyKeyedProxies.find(surf->getUniqueKey()));
82 fUniquelyKeyedProxies.add(proxy);
83}
84
Chris Dalton2de13dd2019-01-03 15:11:59 -070085void GrProxyProvider::removeUniqueKeyFromProxy(GrTextureProxy* proxy) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050086 ASSERT_SINGLE_OWNER
Chris Dalton2de13dd2019-01-03 15:11:59 -070087 SkASSERT(proxy);
88 SkASSERT(proxy->getUniqueKey().isValid());
89
90 if (this->isAbandoned()) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050091 return;
92 }
Robert Phillips0790f8a2018-09-18 13:11:03 -040093
Chris Dalton2de13dd2019-01-03 15:11:59 -070094 this->processInvalidUniqueKey(proxy->getUniqueKey(), proxy, InvalidateGPUResource::kYes);
Robert Phillips1afd4cd2018-01-08 13:40:32 -050095}
96
97sk_sp<GrTextureProxy> GrProxyProvider::findProxyByUniqueKey(const GrUniqueKey& key,
98 GrSurfaceOrigin origin) {
99 ASSERT_SINGLE_OWNER
100
101 if (this->isAbandoned()) {
102 return nullptr;
103 }
104
Brian Salomon01ceae92019-04-02 11:49:54 -0400105 GrTextureProxy* proxy = fUniquelyKeyedProxies.find(key);
Brian Salomon01ceae92019-04-02 11:49:54 -0400106 if (proxy) {
Robert Phillipse5f73282019-06-18 17:15:04 -0400107 SkASSERT(proxy->origin() == origin);
108 return sk_ref_sp(proxy);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500109 }
Robert Phillipse5f73282019-06-18 17:15:04 -0400110 return nullptr;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500111}
112
Robert Phillipsa41c6852019-02-07 10:44:10 -0500113///////////////////////////////////////////////////////////////////////////////
114
115#if GR_TEST_UTILS
116sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createInstantiatedProxy(
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400117 const SkISize& dimensions,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400118 GrColorType colorType,
119 const GrBackendFormat& format,
120 GrRenderable renderable,
121 int renderTargetSampleCnt,
122 GrSurfaceOrigin origin,
123 SkBackingFit fit,
124 SkBudgeted budgeted,
125 GrProtected isProtected) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500126 ASSERT_SINGLE_OWNER
127 if (this->isAbandoned()) {
128 return nullptr;
129 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500130 GrContext* direct = fImageContext->priv().asDirectContext();
131 if (!direct) {
132 return nullptr;
133 }
134
Brian Salomon4eb38b72019-08-05 12:58:39 -0400135 if (this->caps()->isFormatCompressed(format)) {
136 // TODO: Allow this to go to GrResourceProvider::createCompressedTexture() once we no longer
137 // rely on GrColorType to get to GrPixelConfig. Currently this will cause
138 // makeConfigSpecific() to assert because GrColorTypeToPixelConfig() never returns a
139 // compressed GrPixelConfig.
140 return nullptr;
141 }
142 GrSurfaceDesc desc;
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400143 desc.fWidth = dimensions.width();
144 desc.fHeight = dimensions.height();
Brian Salomon4eb38b72019-08-05 12:58:39 -0400145
Robert Phillipsa41c6852019-02-07 10:44:10 -0500146 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
147 sk_sp<GrTexture> tex;
148
149 if (SkBackingFit::kApprox == fit) {
Brian Salomon4eb38b72019-08-05 12:58:39 -0400150 tex = resourceProvider->createApproxTexture(desc, format, renderable, renderTargetSampleCnt,
Robert Phillipsaee18c92019-09-06 11:48:27 -0400151 isProtected);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500152 } else {
Brian Salomon4eb38b72019-08-05 12:58:39 -0400153 tex = resourceProvider->createTexture(desc, format, renderable, renderTargetSampleCnt,
Brian Salomona90382f2019-09-17 09:01:56 -0400154 GrMipMapped::kNo, budgeted, isProtected);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500155 }
156 if (!tex) {
157 return nullptr;
158 }
159
Brian Salomonbeb7f522019-08-30 16:19:42 -0400160 return this->createWrapped(std::move(tex), colorType, origin, UseAllocator::kYes);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500161}
162
Brian Salomon4eb38b72019-08-05 12:58:39 -0400163sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createInstantiatedProxy(
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400164 const SkISize& dimensions,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400165 GrColorType colorType,
166 GrRenderable renderable,
167 int renderTargetSampleCnt,
168 GrSurfaceOrigin origin,
169 SkBackingFit fit,
170 SkBudgeted budgeted,
171 GrProtected isProtected) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500172 ASSERT_SINGLE_OWNER
173 if (this->isAbandoned()) {
174 return nullptr;
175 }
Brian Salomon4eb38b72019-08-05 12:58:39 -0400176 auto format = this->caps()->getDefaultBackendFormat(colorType, renderable);
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400177 return this->testingOnly_createInstantiatedProxy(dimensions,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400178 colorType,
179 format,
180 renderable,
181 renderTargetSampleCnt,
182 origin,
183 fit,
184 budgeted,
185 isProtected);
186}
187
Robert Phillipsa41c6852019-02-07 10:44:10 -0500188sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createWrapped(sk_sp<GrTexture> tex,
Brian Salomon2af3e702019-08-11 19:10:31 -0400189 GrColorType colorType,
Robert Phillipsa41c6852019-02-07 10:44:10 -0500190 GrSurfaceOrigin origin) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400191 return this->createWrapped(std::move(tex), colorType, origin, UseAllocator::kYes);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500192}
193#endif
194
Brian Salomonbeb7f522019-08-30 16:19:42 -0400195sk_sp<GrTextureProxy> GrProxyProvider::createWrapped(sk_sp<GrTexture> tex,
196 GrColorType colorType,
197 GrSurfaceOrigin origin,
198 UseAllocator useAllocator) {
Robert Phillipsadbe1322018-01-17 13:35:46 -0500199#ifdef SK_DEBUG
200 if (tex->getUniqueKey().isValid()) {
201 SkASSERT(!this->findProxyByUniqueKey(tex->getUniqueKey(), origin));
202 }
203#endif
Greg Daniel14b57212019-12-17 16:18:06 -0500204 GrSwizzle readSwizzle = this->caps()->getReadSwizzle(tex->backendFormat(), colorType);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500205
206 if (tex->asRenderTarget()) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400207 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
Greg Daniel14b57212019-12-17 16:18:06 -0500208 std::move(tex), origin, readSwizzle, useAllocator));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500209 } else {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400210 return sk_sp<GrTextureProxy>(
Greg Daniel14b57212019-12-17 16:18:06 -0500211 new GrTextureProxy(std::move(tex), origin, readSwizzle, useAllocator));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500212 }
213}
214
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500215sk_sp<GrTextureProxy> GrProxyProvider::findOrCreateProxyByUniqueKey(const GrUniqueKey& key,
Brian Salomon2af3e702019-08-11 19:10:31 -0400216 GrColorType colorType,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400217 GrSurfaceOrigin origin,
218 UseAllocator useAllocator) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500219 ASSERT_SINGLE_OWNER
220
221 if (this->isAbandoned()) {
222 return nullptr;
223 }
224
225 sk_sp<GrTextureProxy> result = this->findProxyByUniqueKey(key, origin);
226 if (result) {
227 return result;
228 }
229
Robert Phillipsa41c6852019-02-07 10:44:10 -0500230 GrContext* direct = fImageContext->priv().asDirectContext();
231 if (!direct) {
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500232 return nullptr;
233 }
234
Robert Phillipsa41c6852019-02-07 10:44:10 -0500235 GrResourceCache* resourceCache = direct->priv().getResourceCache();
236
237 GrGpuResource* resource = resourceCache->findAndRefUniqueResource(key);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500238 if (!resource) {
239 return nullptr;
240 }
241
242 sk_sp<GrTexture> texture(static_cast<GrSurface*>(resource)->asTexture());
243 SkASSERT(texture);
244
Brian Salomonbeb7f522019-08-30 16:19:42 -0400245 result = this->createWrapped(std::move(texture), colorType, origin, useAllocator);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500246 SkASSERT(result->getUniqueKey() == key);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500247 // createWrapped should've added this for us
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500248 SkASSERT(fUniquelyKeyedProxies.find(key));
Brian Salomon2af3e702019-08-11 19:10:31 -0400249 SkASSERT(result->textureSwizzle() ==
Greg Daniel14b57212019-12-17 16:18:06 -0500250 this->caps()->getReadSwizzle(result->backendFormat(), colorType));
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500251 return result;
252}
253
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500254sk_sp<GrTextureProxy> GrProxyProvider::createTextureProxy(sk_sp<SkImage> srcImage,
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500255 int sampleCnt,
Greg Danielfb3abcd2018-02-02 15:48:33 -0500256 SkBudgeted budgeted,
Chris Daltond004e0b2018-09-27 09:28:03 -0600257 SkBackingFit fit,
258 GrInternalSurfaceFlags surfaceFlags) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500259 ASSERT_SINGLE_OWNER
260 SkASSERT(srcImage);
261
262 if (this->isAbandoned()) {
263 return nullptr;
264 }
265
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400266 const SkImageInfo& info = srcImage->imageInfo();
Brian Salomon2af3e702019-08-11 19:10:31 -0400267 GrColorType ct = SkColorTypeToGrColorType(info.colorType());
Greg Danielf87651e2018-02-21 11:36:53 -0500268
Brian Salomon96b383a2019-08-13 16:55:41 -0400269 GrBackendFormat format = this->caps()->getDefaultBackendFormat(ct, GrRenderable::kNo);
Greg Daniel0a7aa142018-02-21 13:02:32 -0500270
Robert Phillips0a15cc62019-07-30 12:49:10 -0400271 if (!format.isValid()) {
Brian Osmand29dcd12018-09-13 15:04:29 -0400272 SkBitmap copy8888;
273 if (!copy8888.tryAllocPixels(info.makeColorType(kRGBA_8888_SkColorType)) ||
274 !srcImage->readPixels(copy8888.pixmap(), 0, 0)) {
275 return nullptr;
276 }
277 copy8888.setImmutable();
278 srcImage = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode);
Brian Salomon2af3e702019-08-11 19:10:31 -0400279 ct = GrColorType::kRGBA_8888;
Brian Salomon96b383a2019-08-13 16:55:41 -0400280 format = this->caps()->getDefaultBackendFormat(ct, GrRenderable::kNo);
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400281 if (!format.isValid()) {
282 return nullptr;
283 }
Brian Osmand29dcd12018-09-13 15:04:29 -0400284 }
285
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500286 GrSurfaceDesc desc;
287 desc.fWidth = srcImage->width();
288 desc.fHeight = srcImage->height();
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500289
Greg Danielce3ddaa2020-01-22 16:58:15 -0500290 GrSwizzle swizzle = this->caps()->getReadSwizzle(format, ct);
291
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500292 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomon96b383a2019-08-13 16:55:41 -0400293 [desc, format, sampleCnt, budgeted, srcImage, fit,
Brian Salomon2af3e702019-08-11 19:10:31 -0400294 ct](GrResourceProvider* resourceProvider) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500295 SkPixmap pixMap;
296 SkAssertResult(srcImage->peekPixels(&pixMap));
297 GrMipLevel mipLevel = { pixMap.addr(), pixMap.rowBytes() };
298
Brian Salomonbeb7f522019-08-30 16:19:42 -0400299 return LazyCallbackResult(resourceProvider->createTexture(
Brian Salomona90382f2019-09-17 09:01:56 -0400300 desc, format, ct, GrRenderable::kNo, sampleCnt, budgeted, fit,
301 GrProtected::kNo, mipLevel));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500302 },
Greg Danielce3ddaa2020-01-22 16:58:15 -0500303 format, desc, swizzle, GrRenderable::kNo, sampleCnt, kTopLeft_GrSurfaceOrigin,
304 GrMipMapped::kNo, GrMipMapsStatus::kNotAllocated, surfaceFlags, fit, budgeted,
305 GrProtected::kNo, UseAllocator::kYes);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500306
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400307 if (!proxy) {
308 return nullptr;
309 }
310
Robert Phillipsa41c6852019-02-07 10:44:10 -0500311 GrContext* direct = fImageContext->priv().asDirectContext();
312 if (direct) {
313 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
314
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500315 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
316 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500317 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500318 return nullptr;
319 }
320 }
Robert Phillipsc1b60662018-06-26 10:20:08 -0400321
322 SkASSERT(proxy->width() == desc.fWidth);
323 SkASSERT(proxy->height() == desc.fHeight);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500324 return proxy;
325}
326
Brian Osmande496652019-03-22 13:42:33 -0400327sk_sp<GrTextureProxy> GrProxyProvider::createProxyFromBitmap(const SkBitmap& bitmap,
328 GrMipMapped mipMapped) {
Brian Osman412674f2019-02-07 15:34:58 -0500329 ASSERT_SINGLE_OWNER
Brian Osman7dcc6162019-03-25 10:12:57 -0400330 SkASSERT(GrMipMapped::kNo == mipMapped || this->caps()->mipMapSupport());
Brian Osman412674f2019-02-07 15:34:58 -0500331
332 if (this->isAbandoned()) {
333 return nullptr;
334 }
335
Brian Osman2b23c4b2018-06-01 12:25:08 -0400336 if (!SkImageInfoIsValid(bitmap.info())) {
Greg Daniela4ead652018-02-07 10:21:48 -0500337 return nullptr;
338 }
339
Brian Osmande496652019-03-22 13:42:33 -0400340 ATRACE_ANDROID_FRAMEWORK("Upload %sTexture [%ux%u]",
341 GrMipMapped::kYes == mipMapped ? "MipMap " : "",
342 bitmap.width(), bitmap.height());
Greg Daniela4ead652018-02-07 10:21:48 -0500343
344 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
345 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
346 // 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 -0500347 SkCopyPixelsMode copyMode = this->renderingDirectly() ? kNever_SkCopyPixelsMode
348 : kIfMutable_SkCopyPixelsMode;
Greg Daniela4ead652018-02-07 10:21:48 -0500349 sk_sp<SkImage> baseLevel = SkMakeImageFromRasterBitmap(bitmap, copyMode);
Greg Daniela4ead652018-02-07 10:21:48 -0500350 if (!baseLevel) {
351 return nullptr;
352 }
353
Brian Osmande496652019-03-22 13:42:33 -0400354 // If mips weren't requested (or this was too small to have any), then take the fast path
355 if (GrMipMapped::kNo == mipMapped ||
356 0 == SkMipMap::ComputeLevelCount(baseLevel->width(), baseLevel->height())) {
Brian Salomon96b383a2019-08-13 16:55:41 -0400357 return this->createTextureProxy(std::move(baseLevel), 1, SkBudgeted::kYes,
358 SkBackingFit::kExact);
Greg Daniela4ead652018-02-07 10:21:48 -0500359 }
360
Brian Osmand29dcd12018-09-13 15:04:29 -0400361 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info());
Greg Daniel3fc5df42019-06-14 09:42:17 -0400362
Greg Danielce3ddaa2020-01-22 16:58:15 -0500363 GrColorType grCT = SkColorTypeToGrColorType(bitmap.info().colorType());
364 GrBackendFormat format = this->caps()->getDefaultBackendFormat(grCT, GrRenderable::kNo);
Robert Phillips0a15cc62019-07-30 12:49:10 -0400365 if (!format.isValid()) {
Greg Daniel82c6b102020-01-21 10:33:22 -0500366 return nullptr;
Greg Daniel3fc5df42019-06-14 09:42:17 -0400367 }
368
Brian Osmand29dcd12018-09-13 15:04:29 -0400369 SkPixmap pixmap;
370 SkAssertResult(baseLevel->peekPixels(&pixmap));
371 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(pixmap, nullptr));
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400372 if (!mipmaps) {
373 return nullptr;
374 }
375
Greg Danielce3ddaa2020-01-22 16:58:15 -0500376 GrSwizzle readSwizzle = this->caps()->getReadSwizzle(format, grCT);
377
Greg Daniela4ead652018-02-07 10:21:48 -0500378 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomon4eb38b72019-08-05 12:58:39 -0400379 [desc, format, baseLevel, mipmaps](GrResourceProvider* resourceProvider) {
Brian Osman1b97f132018-09-13 17:33:48 +0000380 const int mipLevelCount = mipmaps->countLevels() + 1;
381 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
382
Greg Daniela4ead652018-02-07 10:21:48 -0500383 SkPixmap pixmap;
384 SkAssertResult(baseLevel->peekPixels(&pixmap));
385
Greg Daniela4ead652018-02-07 10:21:48 -0500386 texels[0].fPixels = pixmap.addr();
387 texels[0].fRowBytes = pixmap.rowBytes();
388
Brian Salomona90382f2019-09-17 09:01:56 -0400389 auto colorType = SkColorTypeToGrColorType(pixmap.colorType());
Greg Daniela4ead652018-02-07 10:21:48 -0500390 for (int i = 1; i < mipLevelCount; ++i) {
391 SkMipMap::Level generatedMipLevel;
392 mipmaps->getLevel(i - 1, &generatedMipLevel);
393 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
394 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
395 SkASSERT(texels[i].fPixels);
Brian Salomona90382f2019-09-17 09:01:56 -0400396 SkASSERT(generatedMipLevel.fPixmap.colorType() == pixmap.colorType());
Greg Daniela4ead652018-02-07 10:21:48 -0500397 }
Brian Salomonbeb7f522019-08-30 16:19:42 -0400398 return LazyCallbackResult(resourceProvider->createTexture(
Brian Salomona90382f2019-09-17 09:01:56 -0400399 desc, format, colorType, GrRenderable::kNo, 1, SkBudgeted::kYes,
400 GrProtected::kNo, texels.get(), mipLevelCount));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500401 },
Greg Danielce3ddaa2020-01-22 16:58:15 -0500402 format, desc, readSwizzle, GrRenderable::kNo, 1, kTopLeft_GrSurfaceOrigin,
403 GrMipMapped::kYes, GrMipMapsStatus::kValid, GrInternalSurfaceFlags::kNone,
404 SkBackingFit::kExact, SkBudgeted::kYes, GrProtected::kNo, UseAllocator::kYes);
Greg Daniela4ead652018-02-07 10:21:48 -0500405
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400406 if (!proxy) {
407 return nullptr;
408 }
409
Robert Phillipsa41c6852019-02-07 10:44:10 -0500410 GrContext* direct = fImageContext->priv().asDirectContext();
411 if (direct) {
412 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Daniela4ead652018-02-07 10:21:48 -0500413 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
414 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500415 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Greg Daniela4ead652018-02-07 10:21:48 -0500416 return nullptr;
417 }
418 }
419 return proxy;
420}
421
Greg Daniel4065d452018-11-16 15:43:41 -0500422sk_sp<GrTextureProxy> GrProxyProvider::createProxy(const GrBackendFormat& format,
423 const GrSurfaceDesc& desc,
Greg Daniel47c20e82020-01-21 14:29:57 -0500424 GrSwizzle readSwizzle,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400425 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400426 int renderTargetSampleCnt,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500427 GrSurfaceOrigin origin,
Greg Danielf6f7b672018-02-15 13:06:26 -0500428 GrMipMapped mipMapped,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500429 SkBackingFit fit,
430 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -0400431 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400432 GrInternalSurfaceFlags surfaceFlags,
433 GrSurfaceProxy::UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500434 ASSERT_SINGLE_OWNER
435 if (this->isAbandoned()) {
436 return nullptr;
437 }
438
Robert Phillips8ff8bcc2019-07-29 17:03:35 -0400439 const GrCaps* caps = this->caps();
440
441 if (caps->isFormatCompressed(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400442 // Deferred proxies for compressed textures are not supported.
443 return nullptr;
444 }
Robert Phillips0902c982019-07-16 07:47:56 -0400445
Greg Danielf6f7b672018-02-15 13:06:26 -0500446 if (GrMipMapped::kYes == mipMapped) {
447 // SkMipMap doesn't include the base level in the level count so we have to add 1
448 int mipCount = SkMipMap::ComputeLevelCount(desc.fWidth, desc.fHeight) + 1;
449 if (1 == mipCount) {
450 mipMapped = GrMipMapped::kNo;
451 }
452 }
453
Greg Daniel9a48beb2020-01-16 16:57:16 -0500454 if (!caps->validateSurfaceParams({desc.fWidth, desc.fHeight}, format, renderable,
Greg Daniel6fa62e22019-08-07 15:52:37 -0400455 renderTargetSampleCnt, mipMapped)) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500456 return nullptr;
457 }
Brian Salomon3a2cc2c2018-02-03 00:25:12 +0000458 GrSurfaceDesc copyDesc = desc;
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600459 GrMipMapsStatus mipMapsStatus = (GrMipMapped::kYes == mipMapped)
460 ? GrMipMapsStatus::kDirty
461 : GrMipMapsStatus::kNotAllocated;
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400462 if (renderable == GrRenderable::kYes) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400463 renderTargetSampleCnt =
Greg Daniel6fa62e22019-08-07 15:52:37 -0400464 caps->getRenderTargetSampleCount(renderTargetSampleCnt, format);
465 SkASSERT(renderTargetSampleCnt);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500466 // We know anything we instantiate later from this deferred path will be
467 // both texturable and renderable
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400468 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600469 *caps, format, copyDesc, renderTargetSampleCnt, origin, mipMapped, mipMapsStatus,
Greg Daniel14b57212019-12-17 16:18:06 -0500470 readSwizzle, fit, budgeted, isProtected, surfaceFlags, useAllocator));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500471 }
472
Brian Salomonbeb7f522019-08-30 16:19:42 -0400473 return sk_sp<GrTextureProxy>(new GrTextureProxy(format, copyDesc, origin, mipMapped,
Greg Daniel14b57212019-12-17 16:18:06 -0500474 mipMapsStatus, readSwizzle, fit, budgeted,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400475 isProtected, surfaceFlags, useAllocator));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500476}
477
Brian Salomonbb8dde82019-06-27 10:52:13 -0400478sk_sp<GrTextureProxy> GrProxyProvider::createCompressedTextureProxy(
Robert Phillips3a833922020-01-21 15:25:58 -0500479 SkISize dimensions, SkBudgeted budgeted, GrMipMapped mipMapped, GrProtected isProtected,
Robert Phillipse4720c62020-01-14 14:33:24 -0500480 SkImage::CompressionType compressionType, sk_sp<SkData> data) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500481 ASSERT_SINGLE_OWNER
482 if (this->isAbandoned()) {
483 return nullptr;
484 }
Brian Salomonbb8dde82019-06-27 10:52:13 -0400485
486 GrSurfaceDesc desc;
Robert Phillips9f744f72019-12-19 19:14:33 -0500487 desc.fWidth = dimensions.width();
488 desc.fHeight = dimensions.height();
Brian Salomonbb8dde82019-06-27 10:52:13 -0400489
Robert Phillips8ff8bcc2019-07-29 17:03:35 -0400490 GrBackendFormat format = this->caps()->getBackendFormatFromCompressionType(compressionType);
491
Greg Daniel7bfc9132019-08-14 14:23:53 -0400492 if (!this->caps()->isFormatTexturable(format)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500493 return nullptr;
494 }
495
Robert Phillipse4720c62020-01-14 14:33:24 -0500496 GrMipMapsStatus mipMapsStatus = (GrMipMapped::kYes == mipMapped)
497 ? GrMipMapsStatus::kValid
498 : GrMipMapsStatus::kNotAllocated;
499
Jim Van Verthee06b332019-01-18 10:36:32 -0500500 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Robert Phillips3a833922020-01-21 15:25:58 -0500501 [dimensions, format, budgeted, mipMapped, isProtected, data]
502 (GrResourceProvider* resourceProvider) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400503 return LazyCallbackResult(resourceProvider->createCompressedTexture(
Robert Phillips3a833922020-01-21 15:25:58 -0500504 dimensions, format, budgeted, mipMapped, isProtected, data.get()));
Brian Salomonbb8dde82019-06-27 10:52:13 -0400505 },
Greg Danielce3ddaa2020-01-22 16:58:15 -0500506 format, desc, GrSwizzle(), GrRenderable::kNo, 1, kTopLeft_GrSurfaceOrigin, mipMapped,
Robert Phillipse4720c62020-01-14 14:33:24 -0500507 mipMapsStatus, GrInternalSurfaceFlags::kReadOnly, SkBackingFit::kExact,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400508 SkBudgeted::kYes, GrProtected::kNo, UseAllocator::kYes);
Jim Van Verthee06b332019-01-18 10:36:32 -0500509
510 if (!proxy) {
511 return nullptr;
512 }
513
Robert Phillipsa41c6852019-02-07 10:44:10 -0500514 GrContext* direct = fImageContext->priv().asDirectContext();
515 if (direct) {
516 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Jim Van Verthee06b332019-01-18 10:36:32 -0500517 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
518 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500519 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500520 return nullptr;
521 }
522 }
523 return proxy;
524}
525
Brian Salomon7578f3e2018-03-07 14:39:54 -0500526sk_sp<GrTextureProxy> GrProxyProvider::wrapBackendTexture(const GrBackendTexture& backendTex,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400527 GrColorType grColorType,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500528 GrSurfaceOrigin origin,
529 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500530 GrWrapCacheable cacheable,
Brian Salomonc67c31c2018-12-06 10:00:03 -0500531 GrIOType ioType,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500532 ReleaseProc releaseProc,
533 ReleaseContext releaseCtx) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500534 SkASSERT(ioType != kWrite_GrIOType);
Robert Phillipsf9bec202018-01-16 09:21:01 -0500535 if (this->isAbandoned()) {
536 return nullptr;
537 }
538
Brian Salomonf7778972018-03-08 10:13:17 -0500539 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500540 GrContext* direct = fImageContext->priv().asDirectContext();
541 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500542 return nullptr;
543 }
544
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400545 const GrCaps* caps = this->caps();
546
Robert Phillipsa41c6852019-02-07 10:44:10 -0500547 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
548
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500549 sk_sp<GrTexture> tex =
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400550 resourceProvider->wrapBackendTexture(backendTex, grColorType,
551 ownership, cacheable, ioType);
Brian Salomonf7778972018-03-08 10:13:17 -0500552 if (!tex) {
553 return nullptr;
554 }
Robert Phillipsadbe1322018-01-17 13:35:46 -0500555
Greg Daniel6a0176b2018-01-30 09:28:44 -0500556 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500557 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel6a0176b2018-01-30 09:28:44 -0500558 }
559
Brian Salomonf7778972018-03-08 10:13:17 -0500560 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
561 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500562 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500563
Greg Daniel14b57212019-12-17 16:18:06 -0500564 GrSwizzle readSwizzle = caps->getReadSwizzle(tex->backendFormat(), grColorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400565
Brian Salomonbeb7f522019-08-30 16:19:42 -0400566 return sk_sp<GrTextureProxy>(
Greg Daniel14b57212019-12-17 16:18:06 -0500567 new GrTextureProxy(std::move(tex), origin, readSwizzle, UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500568}
569
Robert Phillipsead321b2019-12-19 10:16:32 -0500570sk_sp<GrTextureProxy> GrProxyProvider::wrapCompressedBackendTexture(const GrBackendTexture& beTex,
571 GrSurfaceOrigin origin,
572 GrWrapOwnership ownership,
573 GrWrapCacheable cacheable,
574 ReleaseProc releaseProc,
575 ReleaseContext releaseCtx) {
576 if (this->isAbandoned()) {
577 return nullptr;
578 }
579
580 // This is only supported on a direct GrContext.
581 GrContext* direct = fImageContext->priv().asDirectContext();
582 if (!direct) {
583 return nullptr;
584 }
585
Robert Phillipsb0855272020-01-15 12:56:52 -0500586 const GrCaps* caps = this->caps();
587
Robert Phillipsead321b2019-12-19 10:16:32 -0500588 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
589
590 sk_sp<GrTexture> tex = resourceProvider->wrapCompressedBackendTexture(beTex, ownership,
591 cacheable);
592 if (!tex) {
593 return nullptr;
594 }
595
596 if (releaseProc) {
597 tex->setRelease(releaseProc, releaseCtx);
598 }
599
600 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
601 // Make sure we match how we created the proxy with SkBudgeted::kNo
602 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
603
Robert Phillipsb0855272020-01-15 12:56:52 -0500604 SkImage::CompressionType compressionType = caps->compressionType(beTex.getBackendFormat());
605
606 GrSwizzle texSwizzle = GrCompressionTypeIsOpaque(compressionType) ? GrSwizzle::RGB1()
607 : GrSwizzle::RGBA();
Robert Phillipsead321b2019-12-19 10:16:32 -0500608
609 return sk_sp<GrTextureProxy>(
610 new GrTextureProxy(std::move(tex), origin, texSwizzle, UseAllocator::kNo));
611}
612
Brian Salomon7578f3e2018-03-07 14:39:54 -0500613sk_sp<GrTextureProxy> GrProxyProvider::wrapRenderableBackendTexture(
Brian Salomon02bd2952018-03-07 15:20:21 -0500614 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400615 GrColorType colorType, GrWrapOwnership ownership, GrWrapCacheable cacheable,
616 ReleaseProc releaseProc, ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500617 if (this->isAbandoned()) {
618 return nullptr;
619 }
620
Brian Salomonf7778972018-03-08 10:13:17 -0500621 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500622 GrContext* direct = fImageContext->priv().asDirectContext();
623 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500624 return nullptr;
625 }
626
Robert Phillips0902c982019-07-16 07:47:56 -0400627 const GrCaps* caps = this->caps();
628
Robert Phillipsa41c6852019-02-07 10:44:10 -0500629 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
630
Greg Daniel6fa62e22019-08-07 15:52:37 -0400631 // TODO: This should have been checked and validated before getting into GrProxyProvider.
632 if (!caps->isFormatAsColorTypeRenderable(colorType, backendTex.getBackendFormat(), sampleCnt)) {
Greg Danielf87651e2018-02-21 11:36:53 -0500633 return nullptr;
634 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500635
Greg Daniel6fa62e22019-08-07 15:52:37 -0400636 sampleCnt = caps->getRenderTargetSampleCount(sampleCnt, backendTex.getBackendFormat());
637 SkASSERT(sampleCnt);
638
Robert Phillipsa41c6852019-02-07 10:44:10 -0500639 sk_sp<GrTexture> tex = resourceProvider->wrapRenderableBackendTexture(backendTex, sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400640 colorType, ownership,
641 cacheable);
Brian Salomonf7778972018-03-08 10:13:17 -0500642 if (!tex) {
643 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500644 }
645
Greg Daniel8ce79912019-02-05 10:08:43 -0500646 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500647 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500648 }
649
Brian Salomonf7778972018-03-08 10:13:17 -0500650 SkASSERT(tex->asRenderTarget()); // A GrTextureRenderTarget
651 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500652 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Greg Daniel6abda432018-02-15 14:55:00 -0500653
Greg Daniel14b57212019-12-17 16:18:06 -0500654 GrSwizzle readSwizzle = caps->getReadSwizzle(tex->backendFormat(), colorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400655
Greg Daniel14b57212019-12-17 16:18:06 -0500656 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin, readSwizzle,
Greg Danielbaf8d992019-10-29 14:14:32 -0400657 UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500658}
659
Brian Salomon7578f3e2018-03-07 14:39:54 -0500660sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400661 const GrBackendRenderTarget& backendRT, GrColorType grColorType,
662 GrSurfaceOrigin origin, ReleaseProc releaseProc, ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500663 if (this->isAbandoned()) {
664 return nullptr;
665 }
666
Brian Salomonf7778972018-03-08 10:13:17 -0500667 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500668 GrContext* direct = fImageContext->priv().asDirectContext();
669 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500670 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500671 }
672
Robert Phillips62221e72019-07-24 15:07:38 -0400673 const GrCaps* caps = this->caps();
674
Robert Phillipsa41c6852019-02-07 10:44:10 -0500675 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
676
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400677 sk_sp<GrRenderTarget> rt = resourceProvider->wrapBackendRenderTarget(backendRT, grColorType);
Brian Salomonf7778972018-03-08 10:13:17 -0500678 if (!rt) {
679 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500680 }
Greg Daniel8ce79912019-02-05 10:08:43 -0500681
Greg Daniel8ce79912019-02-05 10:08:43 -0500682 if (releaseProc) {
Brian Salomon2ca31f82019-03-05 13:28:58 -0500683 rt->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500684 }
685
Brian Salomonf7778972018-03-08 10:13:17 -0500686 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
687 SkASSERT(!rt->getUniqueKey().isValid());
688 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500689 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Brian Salomonf7778972018-03-08 10:13:17 -0500690
Greg Daniel14b57212019-12-17 16:18:06 -0500691 GrSwizzle readSwizzle = caps->getReadSwizzle(rt->backendFormat(), grColorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400692
Greg Daniel14b57212019-12-17 16:18:06 -0500693 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(std::move(rt), origin, readSwizzle,
Greg Danielbaf8d992019-10-29 14:14:32 -0400694 UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500695}
696
Brian Salomon7578f3e2018-03-07 14:39:54 -0500697sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendTextureAsRenderTarget(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400698 const GrBackendTexture& backendTex, GrColorType grColorType,
699 GrSurfaceOrigin origin, int sampleCnt) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500700 if (this->isAbandoned()) {
701 return nullptr;
702 }
703
Brian Salomonf7778972018-03-08 10:13:17 -0500704 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500705 GrContext* direct = fImageContext->priv().asDirectContext();
706 if (!direct) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500707 return nullptr;
708 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500709
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400710 const GrCaps* caps = this->caps();
711
Robert Phillipsa41c6852019-02-07 10:44:10 -0500712 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
713
Brian Salomonf7778972018-03-08 10:13:17 -0500714 sk_sp<GrRenderTarget> rt =
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400715 resourceProvider->wrapBackendTextureAsRenderTarget(backendTex, sampleCnt, grColorType);
Brian Salomonf7778972018-03-08 10:13:17 -0500716 if (!rt) {
717 return nullptr;
Greg Danielf87651e2018-02-21 11:36:53 -0500718 }
Brian Salomonf7778972018-03-08 10:13:17 -0500719 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
720 SkASSERT(!rt->getUniqueKey().isValid());
Greg Danielb46add82019-01-02 14:51:29 -0500721 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500722 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielf87651e2018-02-21 11:36:53 -0500723
Greg Daniel14b57212019-12-17 16:18:06 -0500724 GrSwizzle readSwizzle = caps->getReadSwizzle(rt->backendFormat(), grColorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400725
Greg Daniel14b57212019-12-17 16:18:06 -0500726 return sk_sp<GrSurfaceProxy>(new GrRenderTargetProxy(std::move(rt), origin, readSwizzle,
Greg Danielbaf8d992019-10-29 14:14:32 -0400727 UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500728}
729
Greg Danielb46add82019-01-02 14:51:29 -0500730sk_sp<GrRenderTargetProxy> GrProxyProvider::wrapVulkanSecondaryCBAsRenderTarget(
731 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
732 if (this->isAbandoned()) {
733 return nullptr;
734 }
735
736 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500737 GrContext* direct = fImageContext->priv().asDirectContext();
738 if (!direct) {
Greg Danielb46add82019-01-02 14:51:29 -0500739 return nullptr;
740 }
741
Robert Phillipsa41c6852019-02-07 10:44:10 -0500742 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Danielb46add82019-01-02 14:51:29 -0500743
Robert Phillipsa41c6852019-02-07 10:44:10 -0500744 sk_sp<GrRenderTarget> rt = resourceProvider->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
745 vkInfo);
Greg Danielb46add82019-01-02 14:51:29 -0500746 if (!rt) {
747 return nullptr;
748 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500749
Greg Danielb46add82019-01-02 14:51:29 -0500750 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
751 SkASSERT(!rt->getUniqueKey().isValid());
752 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500753 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielb46add82019-01-02 14:51:29 -0500754
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400755 GrColorType colorType = SkColorTypeToGrColorType(imageInfo.colorType());
Greg Daniel14b57212019-12-17 16:18:06 -0500756 GrSwizzle readSwizzle = this->caps()->getReadSwizzle(rt->backendFormat(), colorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400757
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400758 if (!this->caps()->isFormatAsColorTypeRenderable(colorType, rt->backendFormat(),
759 rt->numSamples())) {
760 return nullptr;
761 }
762
Greg Danielb46add82019-01-02 14:51:29 -0500763 // All Vulkan surfaces uses top left origins.
Brian Salomonbeb7f522019-08-30 16:19:42 -0400764 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
Greg Daniel14b57212019-12-17 16:18:06 -0500765 std::move(rt), kTopLeft_GrSurfaceOrigin, readSwizzle, UseAllocator::kNo,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400766 GrRenderTargetProxy::WrapsVkSecondaryCB::kYes));
Brian Salomone8a766b2019-07-19 14:24:36 -0400767}
768
769sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
770 const GrBackendFormat& format,
771 const GrSurfaceDesc& desc,
Greg Danielce3ddaa2020-01-22 16:58:15 -0500772 GrSwizzle readSwizzle,
Brian Salomone8a766b2019-07-19 14:24:36 -0400773 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400774 int renderTargetSampleCnt,
Brian Salomone8a766b2019-07-19 14:24:36 -0400775 GrSurfaceOrigin origin,
776 GrMipMapped mipMapped,
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600777 GrMipMapsStatus mipMapsStatus,
Brian Salomone8a766b2019-07-19 14:24:36 -0400778 GrInternalSurfaceFlags surfaceFlags,
779 SkBackingFit fit,
780 SkBudgeted budgeted,
781 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400782 GrSurfaceProxy::UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500783 ASSERT_SINGLE_OWNER
784 if (this->isAbandoned()) {
785 return nullptr;
786 }
Robert Phillips777707b2018-01-17 11:40:14 -0500787 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
788 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400789
Robert Phillips0a15cc62019-07-30 12:49:10 -0400790 if (!format.isValid()) {
791 return nullptr;
792 }
793
Robert Phillipsa41c6852019-02-07 10:44:10 -0500794 if (desc.fWidth > this->caps()->maxTextureSize() ||
795 desc.fHeight > this->caps()->maxTextureSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400796 return nullptr;
797 }
798
Brian Salomonbeb7f522019-08-30 16:19:42 -0400799 if (renderable == GrRenderable::kYes) {
800 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(*this->caps(),
801 std::move(callback),
802 format,
803 desc,
804 renderTargetSampleCnt,
805 origin,
806 mipMapped,
807 mipMapsStatus,
Greg Daniel14b57212019-12-17 16:18:06 -0500808 readSwizzle,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400809 fit,
810 budgeted,
811 isProtected,
812 surfaceFlags,
813 useAllocator));
814 } else {
815 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(callback),
816 format,
817 desc,
818 origin,
819 mipMapped,
820 mipMapsStatus,
Greg Daniel14b57212019-12-17 16:18:06 -0500821 readSwizzle,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400822 fit,
823 budgeted,
824 isProtected,
825 surfaceFlags,
826 useAllocator));
827 }
Robert Phillips777707b2018-01-17 11:40:14 -0500828}
829
Robert Phillipse8fabb22018-02-04 14:33:21 -0500830sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy(
Brian Salomonbeb7f522019-08-30 16:19:42 -0400831 LazyInstantiateCallback&& callback,
832 const GrBackendFormat& format,
833 const GrSurfaceDesc& desc,
Greg Danielce3ddaa2020-01-22 16:58:15 -0500834 GrSwizzle readSwizzle,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400835 int sampleCnt,
836 GrSurfaceOrigin origin,
837 GrInternalSurfaceFlags surfaceFlags,
838 const TextureInfo* textureInfo,
839 GrMipMapsStatus mipMapsStatus,
840 SkBackingFit fit,
841 SkBudgeted budgeted,
842 GrProtected isProtected,
843 bool wrapsVkSecondaryCB,
844 UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500845 ASSERT_SINGLE_OWNER
846 if (this->isAbandoned()) {
847 return nullptr;
848 }
Robert Phillipse8fabb22018-02-04 14:33:21 -0500849 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
850 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400851
Robert Phillipsa41c6852019-02-07 10:44:10 -0500852 if (desc.fWidth > this->caps()->maxRenderTargetSize() ||
853 desc.fHeight > this->caps()->maxRenderTargetSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400854 return nullptr;
855 }
856
Brian Salomon7226c232018-07-30 13:13:17 -0400857 if (textureInfo) {
Greg Danielb085fa92019-03-05 16:55:12 -0500858 // Wrapped vulkan secondary command buffers don't support texturing since we won't have an
859 // actual VkImage to texture from.
860 SkASSERT(!wrapsVkSecondaryCB);
Brian Salomon7226c232018-07-30 13:13:17 -0400861 return sk_sp<GrRenderTargetProxy>(new GrTextureRenderTargetProxy(
Brian Salomonbeb7f522019-08-30 16:19:42 -0400862 *this->caps(), std::move(callback), format, desc, sampleCnt, origin,
Greg Daniel14b57212019-12-17 16:18:06 -0500863 textureInfo->fMipMapped, mipMapsStatus, readSwizzle, fit, budgeted, isProtected,
Greg Danielbaf8d992019-10-29 14:14:32 -0400864 surfaceFlags, useAllocator));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500865 }
866
Greg Danielb085fa92019-03-05 16:55:12 -0500867 GrRenderTargetProxy::WrapsVkSecondaryCB vkSCB =
868 wrapsVkSecondaryCB ? GrRenderTargetProxy::WrapsVkSecondaryCB::kYes
869 : GrRenderTargetProxy::WrapsVkSecondaryCB::kNo;
870
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400871 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
Greg Daniel14b57212019-12-17 16:18:06 -0500872 std::move(callback), format, desc, sampleCnt, origin, readSwizzle, fit, budgeted,
Greg Danielbaf8d992019-10-29 14:14:32 -0400873 isProtected, surfaceFlags, useAllocator, vkSCB));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500874}
875
Brian Salomonbeb7f522019-08-30 16:19:42 -0400876sk_sp<GrTextureProxy> GrProxyProvider::MakeFullyLazyProxy(LazyInstantiateCallback&& callback,
877 const GrBackendFormat& format,
Greg Danielce3ddaa2020-01-22 16:58:15 -0500878 GrSwizzle readSwizzle,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400879 GrRenderable renderable,
880 int renderTargetSampleCnt,
881 GrProtected isProtected,
882 GrSurfaceOrigin origin,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400883 const GrCaps& caps,
884 UseAllocator useAllocator) {
Robert Phillips0a15cc62019-07-30 12:49:10 -0400885 if (!format.isValid()) {
886 return nullptr;
887 }
888
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400889 SkASSERT(renderTargetSampleCnt == 1 || renderable == GrRenderable::kYes);
Robert Phillips777707b2018-01-17 11:40:14 -0500890 GrSurfaceDesc desc;
Robert Phillips10d17212019-04-24 14:09:10 -0400891 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNone;
Robert Phillips777707b2018-01-17 11:40:14 -0500892 desc.fWidth = -1;
893 desc.fHeight = -1;
Robert Phillips777707b2018-01-17 11:40:14 -0500894
Brian Salomonbeb7f522019-08-30 16:19:42 -0400895 if (GrRenderable::kYes == renderable) {
896 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
897 caps, std::move(callback), format, desc, renderTargetSampleCnt, origin,
Greg Daniel14b57212019-12-17 16:18:06 -0500898 GrMipMapped::kNo, GrMipMapsStatus::kNotAllocated, readSwizzle,
899 SkBackingFit::kApprox, SkBudgeted::kYes, isProtected, surfaceFlags, useAllocator));
Brian Salomonbeb7f522019-08-30 16:19:42 -0400900 } else {
901 return sk_sp<GrTextureProxy>(new GrTextureProxy(
902 std::move(callback), format, desc, origin, GrMipMapped::kNo,
Greg Daniel14b57212019-12-17 16:18:06 -0500903 GrMipMapsStatus::kNotAllocated, readSwizzle, SkBackingFit::kApprox,
904 SkBudgeted::kYes, isProtected, surfaceFlags, useAllocator));
Brian Salomonbeb7f522019-08-30 16:19:42 -0400905 }
Robert Phillips777707b2018-01-17 11:40:14 -0500906}
907
Robert Phillips427966a2018-12-20 17:20:43 -0500908void GrProxyProvider::processInvalidUniqueKey(const GrUniqueKey& key, GrTextureProxy* proxy,
909 InvalidateGPUResource invalidateGPUResource) {
Chris Dalton2de13dd2019-01-03 15:11:59 -0700910 SkASSERT(key.isValid());
911
Robert Phillips427966a2018-12-20 17:20:43 -0500912 if (!proxy) {
913 proxy = fUniquelyKeyedProxies.find(key);
914 }
Chris Dalton2de13dd2019-01-03 15:11:59 -0700915 SkASSERT(!proxy || proxy->getUniqueKey() == key);
916
917 // Locate the corresponding GrGpuResource (if it needs to be invalidated) before clearing the
918 // proxy's unique key. We must do it in this order because 'key' may alias the proxy's key.
919 sk_sp<GrGpuResource> invalidGpuResource;
920 if (InvalidateGPUResource::kYes == invalidateGPUResource) {
Brian Salomon01ceae92019-04-02 11:49:54 -0400921 GrContext* direct = fImageContext->priv().asDirectContext();
922 if (direct) {
923 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
924 invalidGpuResource = resourceProvider->findByUniqueKey<GrGpuResource>(key);
Chris Dalton2de13dd2019-01-03 15:11:59 -0700925 }
926 SkASSERT(!invalidGpuResource || invalidGpuResource->getUniqueKey() == key);
927 }
Robert Phillips427966a2018-12-20 17:20:43 -0500928
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500929 // Note: this method is called for the whole variety of GrGpuResources so often 'key'
930 // will not be in 'fUniquelyKeyedProxies'.
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500931 if (proxy) {
Robert Phillips427966a2018-12-20 17:20:43 -0500932 fUniquelyKeyedProxies.remove(key);
933 proxy->cacheAccess().clearUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500934 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500935
Chris Dalton2de13dd2019-01-03 15:11:59 -0700936 if (invalidGpuResource) {
937 invalidGpuResource->resourcePriv().removeUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500938 }
939}
940
Robert Phillipsa41c6852019-02-07 10:44:10 -0500941uint32_t GrProxyProvider::contextID() const {
942 return fImageContext->priv().contextID();
943}
944
945const GrCaps* GrProxyProvider::caps() const {
946 return fImageContext->priv().caps();
947}
948
949sk_sp<const GrCaps> GrProxyProvider::refCaps() const {
950 return fImageContext->priv().refCaps();
951}
952
Robert Phillipsa9162df2019-02-11 14:12:03 -0500953bool GrProxyProvider::isAbandoned() const {
954 return fImageContext->priv().abandoned();
955}
956
Robert Phillips0790f8a2018-09-18 13:11:03 -0400957void GrProxyProvider::orphanAllUniqueKeys() {
958 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
959 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
960 GrTextureProxy& tmp = *iter;
961
962 tmp.fProxyProvider = nullptr;
963 }
964}
965
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500966void GrProxyProvider::removeAllUniqueKeys() {
967 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
968 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
969 GrTextureProxy& tmp = *iter;
970
Chris Dalton2de13dd2019-01-03 15:11:59 -0700971 this->processInvalidUniqueKey(tmp.getUniqueKey(), &tmp, InvalidateGPUResource::kNo);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500972 }
973 SkASSERT(!fUniquelyKeyedProxies.count());
974}
Robert Phillipsa41c6852019-02-07 10:44:10 -0500975
976bool GrProxyProvider::renderingDirectly() const {
977 return fImageContext->priv().asDirectContext();
978}