blob: aa23eb745f3c73f87dbcdc64b5aa714d161f2f77 [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;
143 desc.fConfig = GrColorTypeToPixelConfig(colorType);
144 desc.fConfig = this->caps()->makeConfigSpecific(desc.fConfig, format);
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400145 desc.fWidth = dimensions.width();
146 desc.fHeight = dimensions.height();
Brian Salomon4eb38b72019-08-05 12:58:39 -0400147
Robert Phillipsa41c6852019-02-07 10:44:10 -0500148 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
149 sk_sp<GrTexture> tex;
150
151 if (SkBackingFit::kApprox == fit) {
Brian Salomon4eb38b72019-08-05 12:58:39 -0400152 tex = resourceProvider->createApproxTexture(desc, format, renderable, renderTargetSampleCnt,
Robert Phillipsaee18c92019-09-06 11:48:27 -0400153 isProtected);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500154 } else {
Brian Salomon4eb38b72019-08-05 12:58:39 -0400155 tex = resourceProvider->createTexture(desc, format, renderable, renderTargetSampleCnt,
Brian Salomona90382f2019-09-17 09:01:56 -0400156 GrMipMapped::kNo, budgeted, isProtected);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500157 }
158 if (!tex) {
159 return nullptr;
160 }
161
Brian Salomonbeb7f522019-08-30 16:19:42 -0400162 return this->createWrapped(std::move(tex), colorType, origin, UseAllocator::kYes);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500163}
164
Brian Salomon4eb38b72019-08-05 12:58:39 -0400165sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createInstantiatedProxy(
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400166 const SkISize& dimensions,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400167 GrColorType colorType,
168 GrRenderable renderable,
169 int renderTargetSampleCnt,
170 GrSurfaceOrigin origin,
171 SkBackingFit fit,
172 SkBudgeted budgeted,
173 GrProtected isProtected) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500174 ASSERT_SINGLE_OWNER
175 if (this->isAbandoned()) {
176 return nullptr;
177 }
Brian Salomon4eb38b72019-08-05 12:58:39 -0400178 auto format = this->caps()->getDefaultBackendFormat(colorType, renderable);
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400179 return this->testingOnly_createInstantiatedProxy(dimensions,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400180 colorType,
181 format,
182 renderable,
183 renderTargetSampleCnt,
184 origin,
185 fit,
186 budgeted,
187 isProtected);
188}
189
Robert Phillipsa41c6852019-02-07 10:44:10 -0500190sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createWrapped(sk_sp<GrTexture> tex,
Brian Salomon2af3e702019-08-11 19:10:31 -0400191 GrColorType colorType,
Robert Phillipsa41c6852019-02-07 10:44:10 -0500192 GrSurfaceOrigin origin) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400193 return this->createWrapped(std::move(tex), colorType, origin, UseAllocator::kYes);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500194}
195#endif
196
Brian Salomonbeb7f522019-08-30 16:19:42 -0400197sk_sp<GrTextureProxy> GrProxyProvider::createWrapped(sk_sp<GrTexture> tex,
198 GrColorType colorType,
199 GrSurfaceOrigin origin,
200 UseAllocator useAllocator) {
Robert Phillipsadbe1322018-01-17 13:35:46 -0500201#ifdef SK_DEBUG
202 if (tex->getUniqueKey().isValid()) {
203 SkASSERT(!this->findProxyByUniqueKey(tex->getUniqueKey(), origin));
204 }
205#endif
Greg Daniel14b57212019-12-17 16:18:06 -0500206 GrSwizzle readSwizzle = this->caps()->getReadSwizzle(tex->backendFormat(), colorType);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500207
208 if (tex->asRenderTarget()) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400209 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
Greg Daniel14b57212019-12-17 16:18:06 -0500210 std::move(tex), origin, readSwizzle, useAllocator));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500211 } else {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400212 return sk_sp<GrTextureProxy>(
Greg Daniel14b57212019-12-17 16:18:06 -0500213 new GrTextureProxy(std::move(tex), origin, readSwizzle, useAllocator));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500214 }
215}
216
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500217sk_sp<GrTextureProxy> GrProxyProvider::findOrCreateProxyByUniqueKey(const GrUniqueKey& key,
Brian Salomon2af3e702019-08-11 19:10:31 -0400218 GrColorType colorType,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400219 GrSurfaceOrigin origin,
220 UseAllocator useAllocator) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500221 ASSERT_SINGLE_OWNER
222
223 if (this->isAbandoned()) {
224 return nullptr;
225 }
226
227 sk_sp<GrTextureProxy> result = this->findProxyByUniqueKey(key, origin);
228 if (result) {
229 return result;
230 }
231
Robert Phillipsa41c6852019-02-07 10:44:10 -0500232 GrContext* direct = fImageContext->priv().asDirectContext();
233 if (!direct) {
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500234 return nullptr;
235 }
236
Robert Phillipsa41c6852019-02-07 10:44:10 -0500237 GrResourceCache* resourceCache = direct->priv().getResourceCache();
238
239 GrGpuResource* resource = resourceCache->findAndRefUniqueResource(key);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500240 if (!resource) {
241 return nullptr;
242 }
243
244 sk_sp<GrTexture> texture(static_cast<GrSurface*>(resource)->asTexture());
245 SkASSERT(texture);
246
Brian Salomonbeb7f522019-08-30 16:19:42 -0400247 result = this->createWrapped(std::move(texture), colorType, origin, useAllocator);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500248 SkASSERT(result->getUniqueKey() == key);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500249 // createWrapped should've added this for us
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500250 SkASSERT(fUniquelyKeyedProxies.find(key));
Brian Salomon2af3e702019-08-11 19:10:31 -0400251 SkASSERT(result->textureSwizzle() ==
Greg Daniel14b57212019-12-17 16:18:06 -0500252 this->caps()->getReadSwizzle(result->backendFormat(), colorType));
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500253 return result;
254}
255
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500256sk_sp<GrTextureProxy> GrProxyProvider::createTextureProxy(sk_sp<SkImage> srcImage,
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500257 int sampleCnt,
Greg Danielfb3abcd2018-02-02 15:48:33 -0500258 SkBudgeted budgeted,
Chris Daltond004e0b2018-09-27 09:28:03 -0600259 SkBackingFit fit,
260 GrInternalSurfaceFlags surfaceFlags) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500261 ASSERT_SINGLE_OWNER
262 SkASSERT(srcImage);
263
264 if (this->isAbandoned()) {
265 return nullptr;
266 }
267
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400268 const SkImageInfo& info = srcImage->imageInfo();
Brian Salomon2af3e702019-08-11 19:10:31 -0400269 GrColorType ct = SkColorTypeToGrColorType(info.colorType());
Greg Danielf87651e2018-02-21 11:36:53 -0500270
Brian Salomon96b383a2019-08-13 16:55:41 -0400271 GrBackendFormat format = this->caps()->getDefaultBackendFormat(ct, GrRenderable::kNo);
Greg Daniel0a7aa142018-02-21 13:02:32 -0500272
Robert Phillips0a15cc62019-07-30 12:49:10 -0400273 if (!format.isValid()) {
Brian Osmand29dcd12018-09-13 15:04:29 -0400274 SkBitmap copy8888;
275 if (!copy8888.tryAllocPixels(info.makeColorType(kRGBA_8888_SkColorType)) ||
276 !srcImage->readPixels(copy8888.pixmap(), 0, 0)) {
277 return nullptr;
278 }
279 copy8888.setImmutable();
280 srcImage = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode);
Brian Salomon2af3e702019-08-11 19:10:31 -0400281 ct = GrColorType::kRGBA_8888;
Brian Salomon96b383a2019-08-13 16:55:41 -0400282 format = this->caps()->getDefaultBackendFormat(ct, GrRenderable::kNo);
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400283 if (!format.isValid()) {
284 return nullptr;
285 }
Brian Osmand29dcd12018-09-13 15:04:29 -0400286 }
287
Brian Salomon2af3e702019-08-11 19:10:31 -0400288 GrPixelConfig config = GrColorTypeToPixelConfig(ct);
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400289 if (kUnknown_GrPixelConfig == config) {
290 return nullptr;
291 }
292
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500293 GrSurfaceDesc desc;
294 desc.fWidth = srcImage->width();
295 desc.fHeight = srcImage->height();
Greg Danielf87651e2018-02-21 11:36:53 -0500296 desc.fConfig = config;
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500297
298 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomon96b383a2019-08-13 16:55:41 -0400299 [desc, format, sampleCnt, budgeted, srcImage, fit,
Brian Salomon2af3e702019-08-11 19:10:31 -0400300 ct](GrResourceProvider* resourceProvider) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500301 SkPixmap pixMap;
302 SkAssertResult(srcImage->peekPixels(&pixMap));
303 GrMipLevel mipLevel = { pixMap.addr(), pixMap.rowBytes() };
304
Brian Salomonbeb7f522019-08-30 16:19:42 -0400305 return LazyCallbackResult(resourceProvider->createTexture(
Brian Salomona90382f2019-09-17 09:01:56 -0400306 desc, format, ct, GrRenderable::kNo, sampleCnt, budgeted, fit,
307 GrProtected::kNo, mipLevel));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500308 },
Brian Salomon96b383a2019-08-13 16:55:41 -0400309 format, desc, GrRenderable::kNo, sampleCnt, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400310 GrMipMapsStatus::kNotAllocated, surfaceFlags, fit, budgeted, GrProtected::kNo,
311 UseAllocator::kYes);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500312
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400313 if (!proxy) {
314 return nullptr;
315 }
316
Robert Phillipsa41c6852019-02-07 10:44:10 -0500317 GrContext* direct = fImageContext->priv().asDirectContext();
318 if (direct) {
319 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
320
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500321 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
322 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500323 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500324 return nullptr;
325 }
326 }
Robert Phillipsc1b60662018-06-26 10:20:08 -0400327
328 SkASSERT(proxy->width() == desc.fWidth);
329 SkASSERT(proxy->height() == desc.fHeight);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500330 return proxy;
331}
332
Brian Osmande496652019-03-22 13:42:33 -0400333sk_sp<GrTextureProxy> GrProxyProvider::createProxyFromBitmap(const SkBitmap& bitmap,
334 GrMipMapped mipMapped) {
Brian Osman412674f2019-02-07 15:34:58 -0500335 ASSERT_SINGLE_OWNER
Brian Osman7dcc6162019-03-25 10:12:57 -0400336 SkASSERT(GrMipMapped::kNo == mipMapped || this->caps()->mipMapSupport());
Brian Osman412674f2019-02-07 15:34:58 -0500337
338 if (this->isAbandoned()) {
339 return nullptr;
340 }
341
Brian Osman2b23c4b2018-06-01 12:25:08 -0400342 if (!SkImageInfoIsValid(bitmap.info())) {
Greg Daniela4ead652018-02-07 10:21:48 -0500343 return nullptr;
344 }
345
Brian Osmande496652019-03-22 13:42:33 -0400346 ATRACE_ANDROID_FRAMEWORK("Upload %sTexture [%ux%u]",
347 GrMipMapped::kYes == mipMapped ? "MipMap " : "",
348 bitmap.width(), bitmap.height());
Greg Daniela4ead652018-02-07 10:21:48 -0500349
350 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
351 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
352 // 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 -0500353 SkCopyPixelsMode copyMode = this->renderingDirectly() ? kNever_SkCopyPixelsMode
354 : kIfMutable_SkCopyPixelsMode;
Greg Daniela4ead652018-02-07 10:21:48 -0500355 sk_sp<SkImage> baseLevel = SkMakeImageFromRasterBitmap(bitmap, copyMode);
Greg Daniela4ead652018-02-07 10:21:48 -0500356 if (!baseLevel) {
357 return nullptr;
358 }
359
Brian Osmande496652019-03-22 13:42:33 -0400360 // If mips weren't requested (or this was too small to have any), then take the fast path
361 if (GrMipMapped::kNo == mipMapped ||
362 0 == SkMipMap::ComputeLevelCount(baseLevel->width(), baseLevel->height())) {
Brian Salomon96b383a2019-08-13 16:55:41 -0400363 return this->createTextureProxy(std::move(baseLevel), 1, SkBudgeted::kYes,
364 SkBackingFit::kExact);
Greg Daniela4ead652018-02-07 10:21:48 -0500365 }
366
Brian Osmand29dcd12018-09-13 15:04:29 -0400367 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info());
Greg Daniel3fc5df42019-06-14 09:42:17 -0400368
Brian Salomona90382f2019-09-17 09:01:56 -0400369 GrBackendFormat format = this->caps()->getDefaultBackendFormat(
370 SkColorTypeToGrColorType(bitmap.info().colorType()), GrRenderable::kNo);
Robert Phillips0a15cc62019-07-30 12:49:10 -0400371 if (!format.isValid()) {
Brian Osmand29dcd12018-09-13 15:04:29 -0400372 SkBitmap copy8888;
373 if (!copy8888.tryAllocPixels(bitmap.info().makeColorType(kRGBA_8888_SkColorType)) ||
374 !bitmap.readPixels(copy8888.pixmap())) {
375 return nullptr;
376 }
377 copy8888.setImmutable();
378 baseLevel = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode);
379 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Salomona90382f2019-09-17 09:01:56 -0400380 format = this->caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888, GrRenderable::kNo);
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400381 if (!format.isValid()) {
382 return nullptr;
383 }
Greg Daniel3fc5df42019-06-14 09:42:17 -0400384 }
385
Brian Osmand29dcd12018-09-13 15:04:29 -0400386 SkPixmap pixmap;
387 SkAssertResult(baseLevel->peekPixels(&pixmap));
388 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(pixmap, nullptr));
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400389 if (!mipmaps) {
390 return nullptr;
391 }
392
Greg Daniela4ead652018-02-07 10:21:48 -0500393 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomon4eb38b72019-08-05 12:58:39 -0400394 [desc, format, baseLevel, mipmaps](GrResourceProvider* resourceProvider) {
Brian Osman1b97f132018-09-13 17:33:48 +0000395 const int mipLevelCount = mipmaps->countLevels() + 1;
396 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
397
Greg Daniela4ead652018-02-07 10:21:48 -0500398 SkPixmap pixmap;
399 SkAssertResult(baseLevel->peekPixels(&pixmap));
400
Greg Daniela4ead652018-02-07 10:21:48 -0500401 texels[0].fPixels = pixmap.addr();
402 texels[0].fRowBytes = pixmap.rowBytes();
403
Brian Salomona90382f2019-09-17 09:01:56 -0400404 auto colorType = SkColorTypeToGrColorType(pixmap.colorType());
Greg Daniela4ead652018-02-07 10:21:48 -0500405 for (int i = 1; i < mipLevelCount; ++i) {
406 SkMipMap::Level generatedMipLevel;
407 mipmaps->getLevel(i - 1, &generatedMipLevel);
408 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
409 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
410 SkASSERT(texels[i].fPixels);
Brian Salomona90382f2019-09-17 09:01:56 -0400411 SkASSERT(generatedMipLevel.fPixmap.colorType() == pixmap.colorType());
Greg Daniela4ead652018-02-07 10:21:48 -0500412 }
Brian Salomonbeb7f522019-08-30 16:19:42 -0400413 return LazyCallbackResult(resourceProvider->createTexture(
Brian Salomona90382f2019-09-17 09:01:56 -0400414 desc, format, colorType, GrRenderable::kNo, 1, SkBudgeted::kYes,
415 GrProtected::kNo, texels.get(), mipLevelCount));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500416 },
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400417 format, desc, GrRenderable::kNo, 1, kTopLeft_GrSurfaceOrigin, GrMipMapped::kYes,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400418 GrMipMapsStatus::kValid, GrInternalSurfaceFlags::kNone, SkBackingFit::kExact,
419 SkBudgeted::kYes, GrProtected::kNo, UseAllocator::kYes);
Greg Daniela4ead652018-02-07 10:21:48 -0500420
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400421 if (!proxy) {
422 return nullptr;
423 }
424
Robert Phillipsa41c6852019-02-07 10:44:10 -0500425 GrContext* direct = fImageContext->priv().asDirectContext();
426 if (direct) {
427 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Daniela4ead652018-02-07 10:21:48 -0500428 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
429 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500430 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Greg Daniela4ead652018-02-07 10:21:48 -0500431 return nullptr;
432 }
433 }
434 return proxy;
435}
436
Greg Daniel4065d452018-11-16 15:43:41 -0500437sk_sp<GrTextureProxy> GrProxyProvider::createProxy(const GrBackendFormat& format,
438 const GrSurfaceDesc& desc,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400439 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400440 int renderTargetSampleCnt,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500441 GrSurfaceOrigin origin,
Greg Danielf6f7b672018-02-15 13:06:26 -0500442 GrMipMapped mipMapped,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500443 SkBackingFit fit,
444 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -0400445 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400446 GrInternalSurfaceFlags surfaceFlags,
447 GrSurfaceProxy::UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500448 ASSERT_SINGLE_OWNER
449 if (this->isAbandoned()) {
450 return nullptr;
451 }
452
Robert Phillips8ff8bcc2019-07-29 17:03:35 -0400453 const GrCaps* caps = this->caps();
454
455 if (caps->isFormatCompressed(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400456 // Deferred proxies for compressed textures are not supported.
457 return nullptr;
458 }
Robert Phillips0902c982019-07-16 07:47:56 -0400459
Robert Phillips0902c982019-07-16 07:47:56 -0400460 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
461
462 SkASSERT(GrCaps::AreConfigsCompatible(desc.fConfig,
463 caps->getConfigFromBackendFormat(format, colorType)));
Robert Phillips62221e72019-07-24 15:07:38 -0400464
Greg Danielf6f7b672018-02-15 13:06:26 -0500465 if (GrMipMapped::kYes == mipMapped) {
466 // SkMipMap doesn't include the base level in the level count so we have to add 1
467 int mipCount = SkMipMap::ComputeLevelCount(desc.fWidth, desc.fHeight) + 1;
468 if (1 == mipCount) {
469 mipMapped = GrMipMapped::kNo;
470 }
471 }
472
Greg Daniel6fa62e22019-08-07 15:52:37 -0400473 if (!caps->validateSurfaceParams({desc.fWidth, desc.fHeight}, format, desc.fConfig, renderable,
474 renderTargetSampleCnt, mipMapped)) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500475 return nullptr;
476 }
Brian Salomon3a2cc2c2018-02-03 00:25:12 +0000477 GrSurfaceDesc copyDesc = desc;
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600478 GrMipMapsStatus mipMapsStatus = (GrMipMapped::kYes == mipMapped)
479 ? GrMipMapsStatus::kDirty
480 : GrMipMapsStatus::kNotAllocated;
Greg Daniel14b57212019-12-17 16:18:06 -0500481 GrSwizzle readSwizzle = caps->getReadSwizzle(format, colorType);
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400482 if (renderable == GrRenderable::kYes) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400483 renderTargetSampleCnt =
Greg Daniel6fa62e22019-08-07 15:52:37 -0400484 caps->getRenderTargetSampleCount(renderTargetSampleCnt, format);
485 SkASSERT(renderTargetSampleCnt);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500486 // We know anything we instantiate later from this deferred path will be
487 // both texturable and renderable
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400488 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600489 *caps, format, copyDesc, renderTargetSampleCnt, origin, mipMapped, mipMapsStatus,
Greg Daniel14b57212019-12-17 16:18:06 -0500490 readSwizzle, fit, budgeted, isProtected, surfaceFlags, useAllocator));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500491 }
492
Brian Salomonbeb7f522019-08-30 16:19:42 -0400493 return sk_sp<GrTextureProxy>(new GrTextureProxy(format, copyDesc, origin, mipMapped,
Greg Daniel14b57212019-12-17 16:18:06 -0500494 mipMapsStatus, readSwizzle, fit, budgeted,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400495 isProtected, surfaceFlags, useAllocator));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500496}
497
Brian Salomonbb8dde82019-06-27 10:52:13 -0400498sk_sp<GrTextureProxy> GrProxyProvider::createCompressedTextureProxy(
Robert Phillips9f744f72019-12-19 19:14:33 -0500499 SkISize dimensions, SkBudgeted budgeted, SkImage::CompressionType compressionType,
Brian Salomonbb8dde82019-06-27 10:52:13 -0400500 sk_sp<SkData> data) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500501 ASSERT_SINGLE_OWNER
502 if (this->isAbandoned()) {
503 return nullptr;
504 }
Brian Salomonbb8dde82019-06-27 10:52:13 -0400505
506 GrSurfaceDesc desc;
Robert Phillipsa27d6252019-12-10 14:48:36 -0500507 desc.fConfig = GrCompressionTypeToPixelConfig(compressionType);
Robert Phillips9f744f72019-12-19 19:14:33 -0500508 desc.fWidth = dimensions.width();
509 desc.fHeight = dimensions.height();
Brian Salomonbb8dde82019-06-27 10:52:13 -0400510
Robert Phillips8ff8bcc2019-07-29 17:03:35 -0400511 GrBackendFormat format = this->caps()->getBackendFormatFromCompressionType(compressionType);
512
Greg Daniel7bfc9132019-08-14 14:23:53 -0400513 if (!this->caps()->isFormatTexturable(format)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500514 return nullptr;
515 }
516
Jim Van Verthee06b332019-01-18 10:36:32 -0500517 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Robert Phillips9f744f72019-12-19 19:14:33 -0500518 [dimensions, format, budgeted,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400519 data](GrResourceProvider* resourceProvider) {
520 return LazyCallbackResult(resourceProvider->createCompressedTexture(
Robert Phillips9f744f72019-12-19 19:14:33 -0500521 dimensions, format, budgeted, data.get()));
Brian Salomonbb8dde82019-06-27 10:52:13 -0400522 },
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400523 format, desc, GrRenderable::kNo, 1, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo,
Greg Danielc3a927f2019-10-16 12:03:50 -0400524 GrMipMapsStatus::kNotAllocated, GrInternalSurfaceFlags::kReadOnly, SkBackingFit::kExact,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400525 SkBudgeted::kYes, GrProtected::kNo, UseAllocator::kYes);
Jim Van Verthee06b332019-01-18 10:36:32 -0500526
527 if (!proxy) {
528 return nullptr;
529 }
530
Robert Phillipsa41c6852019-02-07 10:44:10 -0500531 GrContext* direct = fImageContext->priv().asDirectContext();
532 if (direct) {
533 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Jim Van Verthee06b332019-01-18 10:36:32 -0500534 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
535 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500536 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500537 return nullptr;
538 }
539 }
540 return proxy;
541}
542
Brian Salomon7578f3e2018-03-07 14:39:54 -0500543sk_sp<GrTextureProxy> GrProxyProvider::wrapBackendTexture(const GrBackendTexture& backendTex,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400544 GrColorType grColorType,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500545 GrSurfaceOrigin origin,
546 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500547 GrWrapCacheable cacheable,
Brian Salomonc67c31c2018-12-06 10:00:03 -0500548 GrIOType ioType,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500549 ReleaseProc releaseProc,
550 ReleaseContext releaseCtx) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500551 SkASSERT(ioType != kWrite_GrIOType);
Robert Phillipsf9bec202018-01-16 09:21:01 -0500552 if (this->isAbandoned()) {
553 return nullptr;
554 }
555
Brian Salomonf7778972018-03-08 10:13:17 -0500556 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500557 GrContext* direct = fImageContext->priv().asDirectContext();
558 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500559 return nullptr;
560 }
561
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400562 const GrCaps* caps = this->caps();
563
Robert Phillipsa41c6852019-02-07 10:44:10 -0500564 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
565
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500566 sk_sp<GrTexture> tex =
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400567 resourceProvider->wrapBackendTexture(backendTex, grColorType,
568 ownership, cacheable, ioType);
Brian Salomonf7778972018-03-08 10:13:17 -0500569 if (!tex) {
570 return nullptr;
571 }
Robert Phillipsadbe1322018-01-17 13:35:46 -0500572
Greg Daniel6a0176b2018-01-30 09:28:44 -0500573 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500574 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel6a0176b2018-01-30 09:28:44 -0500575 }
576
Brian Salomonf7778972018-03-08 10:13:17 -0500577 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
578 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500579 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500580
Greg Daniel14b57212019-12-17 16:18:06 -0500581 GrSwizzle readSwizzle = caps->getReadSwizzle(tex->backendFormat(), grColorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400582
Brian Salomonbeb7f522019-08-30 16:19:42 -0400583 return sk_sp<GrTextureProxy>(
Greg Daniel14b57212019-12-17 16:18:06 -0500584 new GrTextureProxy(std::move(tex), origin, readSwizzle, UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500585}
586
Robert Phillipsead321b2019-12-19 10:16:32 -0500587sk_sp<GrTextureProxy> GrProxyProvider::wrapCompressedBackendTexture(const GrBackendTexture& beTex,
588 GrSurfaceOrigin origin,
589 GrWrapOwnership ownership,
590 GrWrapCacheable cacheable,
591 ReleaseProc releaseProc,
592 ReleaseContext releaseCtx) {
593 if (this->isAbandoned()) {
594 return nullptr;
595 }
596
597 // This is only supported on a direct GrContext.
598 GrContext* direct = fImageContext->priv().asDirectContext();
599 if (!direct) {
600 return nullptr;
601 }
602
603 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
604
605 sk_sp<GrTexture> tex = resourceProvider->wrapCompressedBackendTexture(beTex, ownership,
606 cacheable);
607 if (!tex) {
608 return nullptr;
609 }
610
611 if (releaseProc) {
612 tex->setRelease(releaseProc, releaseCtx);
613 }
614
615 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
616 // Make sure we match how we created the proxy with SkBudgeted::kNo
617 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
618
619 // TODO: this will need to be changed for compressed RGBA formats
620 GrSwizzle texSwizzle = GrSwizzle::RGB1();
621
622 return sk_sp<GrTextureProxy>(
623 new GrTextureProxy(std::move(tex), origin, texSwizzle, UseAllocator::kNo));
624}
625
Brian Salomon7578f3e2018-03-07 14:39:54 -0500626sk_sp<GrTextureProxy> GrProxyProvider::wrapRenderableBackendTexture(
Brian Salomon02bd2952018-03-07 15:20:21 -0500627 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400628 GrColorType colorType, GrWrapOwnership ownership, GrWrapCacheable cacheable,
629 ReleaseProc releaseProc, ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500630 if (this->isAbandoned()) {
631 return nullptr;
632 }
633
Brian Salomonf7778972018-03-08 10:13:17 -0500634 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500635 GrContext* direct = fImageContext->priv().asDirectContext();
636 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500637 return nullptr;
638 }
639
Robert Phillips0902c982019-07-16 07:47:56 -0400640 const GrCaps* caps = this->caps();
641
Robert Phillipsa41c6852019-02-07 10:44:10 -0500642 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
643
Greg Daniel6fa62e22019-08-07 15:52:37 -0400644 // TODO: This should have been checked and validated before getting into GrProxyProvider.
645 if (!caps->isFormatAsColorTypeRenderable(colorType, backendTex.getBackendFormat(), sampleCnt)) {
Greg Danielf87651e2018-02-21 11:36:53 -0500646 return nullptr;
647 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500648
Greg Daniel6fa62e22019-08-07 15:52:37 -0400649 sampleCnt = caps->getRenderTargetSampleCount(sampleCnt, backendTex.getBackendFormat());
650 SkASSERT(sampleCnt);
651
Robert Phillipsa41c6852019-02-07 10:44:10 -0500652 sk_sp<GrTexture> tex = resourceProvider->wrapRenderableBackendTexture(backendTex, sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400653 colorType, ownership,
654 cacheable);
Brian Salomonf7778972018-03-08 10:13:17 -0500655 if (!tex) {
656 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500657 }
658
Greg Daniel8ce79912019-02-05 10:08:43 -0500659 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500660 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500661 }
662
Brian Salomonf7778972018-03-08 10:13:17 -0500663 SkASSERT(tex->asRenderTarget()); // A GrTextureRenderTarget
664 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500665 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Greg Daniel6abda432018-02-15 14:55:00 -0500666
Greg Daniel14b57212019-12-17 16:18:06 -0500667 GrSwizzle readSwizzle = caps->getReadSwizzle(tex->backendFormat(), colorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400668
Greg Daniel14b57212019-12-17 16:18:06 -0500669 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin, readSwizzle,
Greg Danielbaf8d992019-10-29 14:14:32 -0400670 UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500671}
672
Brian Salomon7578f3e2018-03-07 14:39:54 -0500673sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400674 const GrBackendRenderTarget& backendRT, GrColorType grColorType,
675 GrSurfaceOrigin origin, ReleaseProc releaseProc, ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500676 if (this->isAbandoned()) {
677 return nullptr;
678 }
679
Brian Salomonf7778972018-03-08 10:13:17 -0500680 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500681 GrContext* direct = fImageContext->priv().asDirectContext();
682 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500683 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500684 }
685
Robert Phillips62221e72019-07-24 15:07:38 -0400686 const GrCaps* caps = this->caps();
687
Robert Phillipsa41c6852019-02-07 10:44:10 -0500688 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
689
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400690 sk_sp<GrRenderTarget> rt = resourceProvider->wrapBackendRenderTarget(backendRT, grColorType);
Brian Salomonf7778972018-03-08 10:13:17 -0500691 if (!rt) {
692 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500693 }
Greg Daniel8ce79912019-02-05 10:08:43 -0500694
Greg Daniel8ce79912019-02-05 10:08:43 -0500695 if (releaseProc) {
Brian Salomon2ca31f82019-03-05 13:28:58 -0500696 rt->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500697 }
698
Brian Salomonf7778972018-03-08 10:13:17 -0500699 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
700 SkASSERT(!rt->getUniqueKey().isValid());
701 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500702 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Brian Salomonf7778972018-03-08 10:13:17 -0500703
Greg Daniel14b57212019-12-17 16:18:06 -0500704 GrSwizzle readSwizzle = caps->getReadSwizzle(rt->backendFormat(), grColorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400705
Greg Daniel14b57212019-12-17 16:18:06 -0500706 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(std::move(rt), origin, readSwizzle,
Greg Danielbaf8d992019-10-29 14:14:32 -0400707 UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500708}
709
Brian Salomon7578f3e2018-03-07 14:39:54 -0500710sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendTextureAsRenderTarget(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400711 const GrBackendTexture& backendTex, GrColorType grColorType,
712 GrSurfaceOrigin origin, int sampleCnt) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500713 if (this->isAbandoned()) {
714 return nullptr;
715 }
716
Brian Salomonf7778972018-03-08 10:13:17 -0500717 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500718 GrContext* direct = fImageContext->priv().asDirectContext();
719 if (!direct) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500720 return nullptr;
721 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500722
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400723 const GrCaps* caps = this->caps();
724
Robert Phillipsa41c6852019-02-07 10:44:10 -0500725 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
726
Brian Salomonf7778972018-03-08 10:13:17 -0500727 sk_sp<GrRenderTarget> rt =
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400728 resourceProvider->wrapBackendTextureAsRenderTarget(backendTex, sampleCnt, grColorType);
Brian Salomonf7778972018-03-08 10:13:17 -0500729 if (!rt) {
730 return nullptr;
Greg Danielf87651e2018-02-21 11:36:53 -0500731 }
Brian Salomonf7778972018-03-08 10:13:17 -0500732 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
733 SkASSERT(!rt->getUniqueKey().isValid());
Greg Danielb46add82019-01-02 14:51:29 -0500734 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500735 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielf87651e2018-02-21 11:36:53 -0500736
Greg Daniel14b57212019-12-17 16:18:06 -0500737 GrSwizzle readSwizzle = caps->getReadSwizzle(rt->backendFormat(), grColorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400738
Greg Daniel14b57212019-12-17 16:18:06 -0500739 return sk_sp<GrSurfaceProxy>(new GrRenderTargetProxy(std::move(rt), origin, readSwizzle,
Greg Danielbaf8d992019-10-29 14:14:32 -0400740 UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500741}
742
Greg Danielb46add82019-01-02 14:51:29 -0500743sk_sp<GrRenderTargetProxy> GrProxyProvider::wrapVulkanSecondaryCBAsRenderTarget(
744 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
745 if (this->isAbandoned()) {
746 return nullptr;
747 }
748
749 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500750 GrContext* direct = fImageContext->priv().asDirectContext();
751 if (!direct) {
Greg Danielb46add82019-01-02 14:51:29 -0500752 return nullptr;
753 }
754
Robert Phillipsa41c6852019-02-07 10:44:10 -0500755 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Danielb46add82019-01-02 14:51:29 -0500756
Robert Phillipsa41c6852019-02-07 10:44:10 -0500757 sk_sp<GrRenderTarget> rt = resourceProvider->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
758 vkInfo);
Greg Danielb46add82019-01-02 14:51:29 -0500759 if (!rt) {
760 return nullptr;
761 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500762
Greg Danielb46add82019-01-02 14:51:29 -0500763 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
764 SkASSERT(!rt->getUniqueKey().isValid());
765 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500766 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielb46add82019-01-02 14:51:29 -0500767
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400768 GrColorType colorType = SkColorTypeToGrColorType(imageInfo.colorType());
Greg Daniel14b57212019-12-17 16:18:06 -0500769 GrSwizzle readSwizzle = this->caps()->getReadSwizzle(rt->backendFormat(), colorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400770
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400771 if (!this->caps()->isFormatAsColorTypeRenderable(colorType, rt->backendFormat(),
772 rt->numSamples())) {
773 return nullptr;
774 }
775
Greg Danielb46add82019-01-02 14:51:29 -0500776 // All Vulkan surfaces uses top left origins.
Brian Salomonbeb7f522019-08-30 16:19:42 -0400777 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
Greg Daniel14b57212019-12-17 16:18:06 -0500778 std::move(rt), kTopLeft_GrSurfaceOrigin, readSwizzle, UseAllocator::kNo,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400779 GrRenderTargetProxy::WrapsVkSecondaryCB::kYes));
Brian Salomone8a766b2019-07-19 14:24:36 -0400780}
781
782sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
783 const GrBackendFormat& format,
784 const GrSurfaceDesc& desc,
785 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400786 int renderTargetSampleCnt,
Brian Salomone8a766b2019-07-19 14:24:36 -0400787 GrSurfaceOrigin origin,
788 GrMipMapped mipMapped,
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600789 GrMipMapsStatus mipMapsStatus,
Brian Salomone8a766b2019-07-19 14:24:36 -0400790 GrInternalSurfaceFlags surfaceFlags,
791 SkBackingFit fit,
792 SkBudgeted budgeted,
793 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400794 GrSurfaceProxy::UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500795 ASSERT_SINGLE_OWNER
796 if (this->isAbandoned()) {
797 return nullptr;
798 }
Robert Phillips777707b2018-01-17 11:40:14 -0500799 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
800 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400801
Robert Phillips0a15cc62019-07-30 12:49:10 -0400802 if (!format.isValid()) {
803 return nullptr;
804 }
805
Robert Phillipsa41c6852019-02-07 10:44:10 -0500806 if (desc.fWidth > this->caps()->maxTextureSize() ||
807 desc.fHeight > this->caps()->maxTextureSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400808 return nullptr;
809 }
810
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400811 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
Greg Daniel14b57212019-12-17 16:18:06 -0500812 GrSwizzle readSwizzle = this->caps()->getReadSwizzle(format, colorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400813
Brian Salomonbeb7f522019-08-30 16:19:42 -0400814 if (renderable == GrRenderable::kYes) {
815 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(*this->caps(),
816 std::move(callback),
817 format,
818 desc,
819 renderTargetSampleCnt,
820 origin,
821 mipMapped,
822 mipMapsStatus,
Greg Daniel14b57212019-12-17 16:18:06 -0500823 readSwizzle,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400824 fit,
825 budgeted,
826 isProtected,
827 surfaceFlags,
828 useAllocator));
829 } else {
830 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(callback),
831 format,
832 desc,
833 origin,
834 mipMapped,
835 mipMapsStatus,
Greg Daniel14b57212019-12-17 16:18:06 -0500836 readSwizzle,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400837 fit,
838 budgeted,
839 isProtected,
840 surfaceFlags,
841 useAllocator));
842 }
Robert Phillips777707b2018-01-17 11:40:14 -0500843}
844
Robert Phillipse8fabb22018-02-04 14:33:21 -0500845sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy(
Brian Salomonbeb7f522019-08-30 16:19:42 -0400846 LazyInstantiateCallback&& callback,
847 const GrBackendFormat& format,
848 const GrSurfaceDesc& desc,
849 int sampleCnt,
850 GrSurfaceOrigin origin,
851 GrInternalSurfaceFlags surfaceFlags,
852 const TextureInfo* textureInfo,
853 GrMipMapsStatus mipMapsStatus,
854 SkBackingFit fit,
855 SkBudgeted budgeted,
856 GrProtected isProtected,
857 bool wrapsVkSecondaryCB,
858 UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500859 ASSERT_SINGLE_OWNER
860 if (this->isAbandoned()) {
861 return nullptr;
862 }
Robert Phillipse8fabb22018-02-04 14:33:21 -0500863 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
864 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400865
Robert Phillipsa41c6852019-02-07 10:44:10 -0500866 if (desc.fWidth > this->caps()->maxRenderTargetSize() ||
867 desc.fHeight > this->caps()->maxRenderTargetSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400868 return nullptr;
869 }
870
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400871 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
Greg Daniel14b57212019-12-17 16:18:06 -0500872 GrSwizzle readSwizzle = this->caps()->getReadSwizzle(format, colorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400873
Brian Salomon7226c232018-07-30 13:13:17 -0400874 if (textureInfo) {
Greg Danielb085fa92019-03-05 16:55:12 -0500875 // Wrapped vulkan secondary command buffers don't support texturing since we won't have an
876 // actual VkImage to texture from.
877 SkASSERT(!wrapsVkSecondaryCB);
Brian Salomon7226c232018-07-30 13:13:17 -0400878 return sk_sp<GrRenderTargetProxy>(new GrTextureRenderTargetProxy(
Brian Salomonbeb7f522019-08-30 16:19:42 -0400879 *this->caps(), std::move(callback), format, desc, sampleCnt, origin,
Greg Daniel14b57212019-12-17 16:18:06 -0500880 textureInfo->fMipMapped, mipMapsStatus, readSwizzle, fit, budgeted, isProtected,
Greg Danielbaf8d992019-10-29 14:14:32 -0400881 surfaceFlags, useAllocator));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500882 }
883
Greg Danielb085fa92019-03-05 16:55:12 -0500884 GrRenderTargetProxy::WrapsVkSecondaryCB vkSCB =
885 wrapsVkSecondaryCB ? GrRenderTargetProxy::WrapsVkSecondaryCB::kYes
886 : GrRenderTargetProxy::WrapsVkSecondaryCB::kNo;
887
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400888 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
Greg Daniel14b57212019-12-17 16:18:06 -0500889 std::move(callback), format, desc, sampleCnt, origin, readSwizzle, fit, budgeted,
Greg Danielbaf8d992019-10-29 14:14:32 -0400890 isProtected, surfaceFlags, useAllocator, vkSCB));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500891}
892
Brian Salomonbeb7f522019-08-30 16:19:42 -0400893sk_sp<GrTextureProxy> GrProxyProvider::MakeFullyLazyProxy(LazyInstantiateCallback&& callback,
894 const GrBackendFormat& format,
895 GrRenderable renderable,
896 int renderTargetSampleCnt,
897 GrProtected isProtected,
898 GrSurfaceOrigin origin,
899 GrPixelConfig config,
900 const GrCaps& caps,
901 UseAllocator useAllocator) {
Robert Phillips0a15cc62019-07-30 12:49:10 -0400902 if (!format.isValid()) {
903 return nullptr;
904 }
905
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400906 SkASSERT(renderTargetSampleCnt == 1 || renderable == GrRenderable::kYes);
Robert Phillips777707b2018-01-17 11:40:14 -0500907 GrSurfaceDesc desc;
Robert Phillips10d17212019-04-24 14:09:10 -0400908 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNone;
Robert Phillips777707b2018-01-17 11:40:14 -0500909 desc.fWidth = -1;
910 desc.fHeight = -1;
911 desc.fConfig = config;
Robert Phillips777707b2018-01-17 11:40:14 -0500912
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400913 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
Greg Daniel14b57212019-12-17 16:18:06 -0500914 GrSwizzle readSwizzle = caps.getReadSwizzle(format, colorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400915
Brian Salomonbeb7f522019-08-30 16:19:42 -0400916 if (GrRenderable::kYes == renderable) {
917 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
918 caps, std::move(callback), format, desc, renderTargetSampleCnt, origin,
Greg Daniel14b57212019-12-17 16:18:06 -0500919 GrMipMapped::kNo, GrMipMapsStatus::kNotAllocated, readSwizzle,
920 SkBackingFit::kApprox, SkBudgeted::kYes, isProtected, surfaceFlags, useAllocator));
Brian Salomonbeb7f522019-08-30 16:19:42 -0400921 } else {
922 return sk_sp<GrTextureProxy>(new GrTextureProxy(
923 std::move(callback), format, desc, origin, GrMipMapped::kNo,
Greg Daniel14b57212019-12-17 16:18:06 -0500924 GrMipMapsStatus::kNotAllocated, readSwizzle, SkBackingFit::kApprox,
925 SkBudgeted::kYes, isProtected, surfaceFlags, useAllocator));
Brian Salomonbeb7f522019-08-30 16:19:42 -0400926 }
Robert Phillips777707b2018-01-17 11:40:14 -0500927}
928
Robert Phillips427966a2018-12-20 17:20:43 -0500929void GrProxyProvider::processInvalidUniqueKey(const GrUniqueKey& key, GrTextureProxy* proxy,
930 InvalidateGPUResource invalidateGPUResource) {
Chris Dalton2de13dd2019-01-03 15:11:59 -0700931 SkASSERT(key.isValid());
932
Robert Phillips427966a2018-12-20 17:20:43 -0500933 if (!proxy) {
934 proxy = fUniquelyKeyedProxies.find(key);
935 }
Chris Dalton2de13dd2019-01-03 15:11:59 -0700936 SkASSERT(!proxy || proxy->getUniqueKey() == key);
937
938 // Locate the corresponding GrGpuResource (if it needs to be invalidated) before clearing the
939 // proxy's unique key. We must do it in this order because 'key' may alias the proxy's key.
940 sk_sp<GrGpuResource> invalidGpuResource;
941 if (InvalidateGPUResource::kYes == invalidateGPUResource) {
Brian Salomon01ceae92019-04-02 11:49:54 -0400942 GrContext* direct = fImageContext->priv().asDirectContext();
943 if (direct) {
944 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
945 invalidGpuResource = resourceProvider->findByUniqueKey<GrGpuResource>(key);
Chris Dalton2de13dd2019-01-03 15:11:59 -0700946 }
947 SkASSERT(!invalidGpuResource || invalidGpuResource->getUniqueKey() == key);
948 }
Robert Phillips427966a2018-12-20 17:20:43 -0500949
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500950 // Note: this method is called for the whole variety of GrGpuResources so often 'key'
951 // will not be in 'fUniquelyKeyedProxies'.
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500952 if (proxy) {
Robert Phillips427966a2018-12-20 17:20:43 -0500953 fUniquelyKeyedProxies.remove(key);
954 proxy->cacheAccess().clearUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500955 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500956
Chris Dalton2de13dd2019-01-03 15:11:59 -0700957 if (invalidGpuResource) {
958 invalidGpuResource->resourcePriv().removeUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500959 }
960}
961
Robert Phillipsa41c6852019-02-07 10:44:10 -0500962uint32_t GrProxyProvider::contextID() const {
963 return fImageContext->priv().contextID();
964}
965
966const GrCaps* GrProxyProvider::caps() const {
967 return fImageContext->priv().caps();
968}
969
970sk_sp<const GrCaps> GrProxyProvider::refCaps() const {
971 return fImageContext->priv().refCaps();
972}
973
Robert Phillipsa9162df2019-02-11 14:12:03 -0500974bool GrProxyProvider::isAbandoned() const {
975 return fImageContext->priv().abandoned();
976}
977
Robert Phillips0790f8a2018-09-18 13:11:03 -0400978void GrProxyProvider::orphanAllUniqueKeys() {
979 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
980 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
981 GrTextureProxy& tmp = *iter;
982
983 tmp.fProxyProvider = nullptr;
984 }
985}
986
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500987void GrProxyProvider::removeAllUniqueKeys() {
988 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
989 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
990 GrTextureProxy& tmp = *iter;
991
Chris Dalton2de13dd2019-01-03 15:11:59 -0700992 this->processInvalidUniqueKey(tmp.getUniqueKey(), &tmp, InvalidateGPUResource::kNo);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500993 }
994 SkASSERT(!fUniquelyKeyedProxies.count());
995}
Robert Phillipsa41c6852019-02-07 10:44:10 -0500996
997bool GrProxyProvider::renderingDirectly() const {
998 return fImageContext->priv().asDirectContext();
999}