blob: f31b7110efabcc342eebbc7ab2958f454d3774c4 [file] [log] [blame]
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001/*
2 * Copyright 2018 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrProxyProvider.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkBitmap.h"
11#include "include/core/SkImage.h"
12#include "include/gpu/GrContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/private/GrImageContext.h"
14#include "include/private/GrResourceKey.h"
15#include "include/private/GrSingleOwner.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/private/SkImageInfoPriv.h"
17#include "src/core/SkAutoPixmapStorage.h"
Robert Phillips99dead92020-01-27 16:11:57 -050018#include "src/core/SkCompressedDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/core/SkImagePriv.h"
20#include "src/core/SkMipMap.h"
21#include "src/core/SkTraceEvent.h"
22#include "src/gpu/GrCaps.h"
23#include "src/gpu/GrContextPriv.h"
24#include "src/gpu/GrImageContextPriv.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040025#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/gpu/GrResourceProvider.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040027#include "src/gpu/GrSurfaceProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/gpu/GrSurfaceProxyPriv.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000029#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/gpu/GrTextureProxyCacheAccess.h"
31#include "src/gpu/GrTextureRenderTargetProxy.h"
32#include "src/gpu/SkGr.h"
33#include "src/image/SkImage_Base.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050034
35#define ASSERT_SINGLE_OWNER \
Robert Phillipsa41c6852019-02-07 10:44:10 -050036 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fImageContext->priv().singleOwner());)
Robert Phillips1afd4cd2018-01-08 13:40:32 -050037
Robert Phillipsa9162df2019-02-11 14:12:03 -050038GrProxyProvider::GrProxyProvider(GrImageContext* imageContext) : fImageContext(imageContext) {}
Robert Phillips1afd4cd2018-01-08 13:40:32 -050039
40GrProxyProvider::~GrProxyProvider() {
Robert Phillipsa41c6852019-02-07 10:44:10 -050041 if (this->renderingDirectly()) {
Robert Phillips0790f8a2018-09-18 13:11:03 -040042 // In DDL-mode a proxy provider can still have extant uniquely keyed proxies (since
43 // they need their unique keys to, potentially, find a cached resource when the
44 // DDL is played) but, in non-DDL-mode they should all have been cleaned up by this point.
45 SkASSERT(!fUniquelyKeyedProxies.count());
46 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050047}
48
Robert Phillipsadbe1322018-01-17 13:35:46 -050049bool GrProxyProvider::assignUniqueKeyToProxy(const GrUniqueKey& key, GrTextureProxy* proxy) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050050 ASSERT_SINGLE_OWNER
51 SkASSERT(key.isValid());
52 if (this->isAbandoned() || !proxy) {
Robert Phillipsadbe1322018-01-17 13:35:46 -050053 return false;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050054 }
55
Robert Phillipsa41c6852019-02-07 10:44:10 -050056#ifdef SK_DEBUG
57 {
58 GrContext* direct = fImageContext->priv().asDirectContext();
59 if (direct) {
60 GrResourceCache* resourceCache = direct->priv().getResourceCache();
61 // If there is already a GrResource with this key then the caller has violated the
62 // normal usage pattern of uniquely keyed resources (e.g., they have created one w/o
63 // first seeing if it already existed in the cache).
64 SkASSERT(!resourceCache->findAndRefUniqueResource(key));
65 }
66 }
67#endif
Robert Phillips1afd4cd2018-01-08 13:40:32 -050068
Robert Phillips1afd4cd2018-01-08 13:40:32 -050069 SkASSERT(!fUniquelyKeyedProxies.find(key)); // multiple proxies can't get the same key
70
71 proxy->cacheAccess().setUniqueKey(this, key);
72 SkASSERT(proxy->getUniqueKey() == key);
73 fUniquelyKeyedProxies.add(proxy);
Robert Phillipsadbe1322018-01-17 13:35:46 -050074 return true;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050075}
76
77void GrProxyProvider::adoptUniqueKeyFromSurface(GrTextureProxy* proxy, const GrSurface* surf) {
78 SkASSERT(surf->getUniqueKey().isValid());
79 proxy->cacheAccess().setUniqueKey(this, surf->getUniqueKey());
80 SkASSERT(proxy->getUniqueKey() == surf->getUniqueKey());
81 // multiple proxies can't get the same key
82 SkASSERT(!fUniquelyKeyedProxies.find(surf->getUniqueKey()));
83 fUniquelyKeyedProxies.add(proxy);
84}
85
Chris Dalton2de13dd2019-01-03 15:11:59 -070086void GrProxyProvider::removeUniqueKeyFromProxy(GrTextureProxy* proxy) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050087 ASSERT_SINGLE_OWNER
Chris Dalton2de13dd2019-01-03 15:11:59 -070088 SkASSERT(proxy);
89 SkASSERT(proxy->getUniqueKey().isValid());
90
91 if (this->isAbandoned()) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050092 return;
93 }
Robert Phillips0790f8a2018-09-18 13:11:03 -040094
Chris Dalton2de13dd2019-01-03 15:11:59 -070095 this->processInvalidUniqueKey(proxy->getUniqueKey(), proxy, InvalidateGPUResource::kYes);
Robert Phillips1afd4cd2018-01-08 13:40:32 -050096}
97
Greg Daniel3a365112020-02-14 10:47:18 -050098sk_sp<GrTextureProxy> GrProxyProvider::findProxyByUniqueKey(const GrUniqueKey& key) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050099 ASSERT_SINGLE_OWNER
100
101 if (this->isAbandoned()) {
102 return nullptr;
103 }
104
Brian Salomon01ceae92019-04-02 11:49:54 -0400105 GrTextureProxy* proxy = fUniquelyKeyedProxies.find(key);
Brian Salomon01ceae92019-04-02 11:49:54 -0400106 if (proxy) {
Robert Phillipse5f73282019-06-18 17:15:04 -0400107 return sk_ref_sp(proxy);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500108 }
Robert Phillipse5f73282019-06-18 17:15:04 -0400109 return nullptr;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500110}
111
Robert Phillipsa41c6852019-02-07 10:44:10 -0500112///////////////////////////////////////////////////////////////////////////////
113
114#if GR_TEST_UTILS
115sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createInstantiatedProxy(
Brian Salomona56a7462020-02-07 14:17:25 -0500116 SkISize dimensions,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400117 const GrBackendFormat& format,
118 GrRenderable renderable,
119 int renderTargetSampleCnt,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400120 SkBackingFit fit,
121 SkBudgeted budgeted,
122 GrProtected isProtected) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500123 ASSERT_SINGLE_OWNER
124 if (this->isAbandoned()) {
125 return nullptr;
126 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500127 GrContext* direct = fImageContext->priv().asDirectContext();
128 if (!direct) {
129 return nullptr;
130 }
131
Brian Salomon4eb38b72019-08-05 12:58:39 -0400132 if (this->caps()->isFormatCompressed(format)) {
133 // TODO: Allow this to go to GrResourceProvider::createCompressedTexture() once we no longer
Greg Daniel4cb29332020-01-23 10:07:02 -0500134 // rely on GrColorType to get a swizzle for the proxy.
Brian Salomon4eb38b72019-08-05 12:58:39 -0400135 return nullptr;
136 }
Brian Salomon4eb38b72019-08-05 12:58:39 -0400137
Robert Phillipsa41c6852019-02-07 10:44:10 -0500138 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
139 sk_sp<GrTexture> tex;
140
141 if (SkBackingFit::kApprox == fit) {
Brian Salomona56a7462020-02-07 14:17:25 -0500142 tex = resourceProvider->createApproxTexture(dimensions, format, renderable,
143 renderTargetSampleCnt, isProtected);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500144 } else {
Brian Salomona56a7462020-02-07 14:17:25 -0500145 tex = resourceProvider->createTexture(dimensions, format, renderable, renderTargetSampleCnt,
Brian Salomona90382f2019-09-17 09:01:56 -0400146 GrMipMapped::kNo, budgeted, isProtected);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500147 }
148 if (!tex) {
149 return nullptr;
150 }
151
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400152 return this->createWrapped(std::move(tex), UseAllocator::kYes);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500153}
154
Brian Salomon4eb38b72019-08-05 12:58:39 -0400155sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createInstantiatedProxy(
Brian Salomona56a7462020-02-07 14:17:25 -0500156 SkISize dimensions,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400157 GrColorType colorType,
158 GrRenderable renderable,
159 int renderTargetSampleCnt,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400160 SkBackingFit fit,
161 SkBudgeted budgeted,
162 GrProtected isProtected) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500163 ASSERT_SINGLE_OWNER
164 if (this->isAbandoned()) {
165 return nullptr;
166 }
Brian Salomon4eb38b72019-08-05 12:58:39 -0400167 auto format = this->caps()->getDefaultBackendFormat(colorType, renderable);
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400168 return this->testingOnly_createInstantiatedProxy(dimensions,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400169 format,
170 renderable,
171 renderTargetSampleCnt,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400172 fit,
173 budgeted,
174 isProtected);
175}
176
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400177sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createWrapped(sk_sp<GrTexture> tex) {
178 return this->createWrapped(std::move(tex), UseAllocator::kYes);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500179}
180#endif
181
Brian Salomonbeb7f522019-08-30 16:19:42 -0400182sk_sp<GrTextureProxy> GrProxyProvider::createWrapped(sk_sp<GrTexture> tex,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400183 UseAllocator useAllocator) {
Robert Phillipsadbe1322018-01-17 13:35:46 -0500184#ifdef SK_DEBUG
185 if (tex->getUniqueKey().isValid()) {
Greg Daniel3a365112020-02-14 10:47:18 -0500186 SkASSERT(!this->findProxyByUniqueKey(tex->getUniqueKey()));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500187 }
188#endif
189
190 if (tex->asRenderTarget()) {
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400191 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), useAllocator));
Greg Daniel3a365112020-02-14 10:47:18 -0500192 } else {
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400193 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), useAllocator));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500194 }
195}
196
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500197sk_sp<GrTextureProxy> GrProxyProvider::findOrCreateProxyByUniqueKey(const GrUniqueKey& key,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400198 UseAllocator useAllocator) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500199 ASSERT_SINGLE_OWNER
200
201 if (this->isAbandoned()) {
202 return nullptr;
203 }
204
Greg Daniel3a365112020-02-14 10:47:18 -0500205 sk_sp<GrTextureProxy> result = this->findProxyByUniqueKey(key);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500206 if (result) {
207 return result;
208 }
209
Robert Phillipsa41c6852019-02-07 10:44:10 -0500210 GrContext* direct = fImageContext->priv().asDirectContext();
211 if (!direct) {
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500212 return nullptr;
213 }
214
Robert Phillipsa41c6852019-02-07 10:44:10 -0500215 GrResourceCache* resourceCache = direct->priv().getResourceCache();
216
217 GrGpuResource* resource = resourceCache->findAndRefUniqueResource(key);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500218 if (!resource) {
219 return nullptr;
220 }
221
222 sk_sp<GrTexture> texture(static_cast<GrSurface*>(resource)->asTexture());
223 SkASSERT(texture);
224
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400225 result = this->createWrapped(std::move(texture), useAllocator);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500226 SkASSERT(result->getUniqueKey() == key);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500227 // createWrapped should've added this for us
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500228 SkASSERT(fUniquelyKeyedProxies.find(key));
229 return result;
230}
231
Brian Osmande496652019-03-22 13:42:33 -0400232sk_sp<GrTextureProxy> GrProxyProvider::createProxyFromBitmap(const SkBitmap& bitmap,
Greg Daniel6f5441a2020-01-28 17:02:49 -0500233 GrMipMapped mipMapped,
Brian Salomonbc074a62020-03-18 10:06:13 -0400234 SkBackingFit fit,
235 SkBudgeted budgeted) {
Brian Osman412674f2019-02-07 15:34:58 -0500236 ASSERT_SINGLE_OWNER
Greg Daniel6f5441a2020-01-28 17:02:49 -0500237 SkASSERT(fit == SkBackingFit::kExact || mipMapped == GrMipMapped::kNo);
Brian Osman412674f2019-02-07 15:34:58 -0500238
239 if (this->isAbandoned()) {
240 return nullptr;
241 }
242
Brian Osman2b23c4b2018-06-01 12:25:08 -0400243 if (!SkImageInfoIsValid(bitmap.info())) {
Greg Daniela4ead652018-02-07 10:21:48 -0500244 return nullptr;
245 }
246
Brian Osmande496652019-03-22 13:42:33 -0400247 ATRACE_ANDROID_FRAMEWORK("Upload %sTexture [%ux%u]",
248 GrMipMapped::kYes == mipMapped ? "MipMap " : "",
249 bitmap.width(), bitmap.height());
Greg Daniela4ead652018-02-07 10:21:48 -0500250
251 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
252 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
253 // upload of the data to the gpu can happen at anytime and the bitmap may change by then.
Greg Daniel6f5441a2020-01-28 17:02:49 -0500254 SkBitmap copyBitmap = bitmap;
255 if (!this->renderingDirectly() && !bitmap.isImmutable()) {
256 copyBitmap.allocPixels();
257 if (!bitmap.readPixels(copyBitmap.pixmap())) {
258 return nullptr;
259 }
260 copyBitmap.setImmutable();
Greg Daniela4ead652018-02-07 10:21:48 -0500261 }
262
Greg Daniel6f5441a2020-01-28 17:02:49 -0500263 sk_sp<GrTextureProxy> proxy;
264 if (mipMapped == GrMipMapped::kNo ||
265 0 == SkMipMap::ComputeLevelCount(copyBitmap.width(), copyBitmap.height())) {
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400266 proxy = this->createNonMippedProxyFromBitmap(copyBitmap, fit, budgeted);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500267 } else {
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400268 proxy = this->createMippedProxyFromBitmap(copyBitmap, budgeted);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500269 }
270
271 if (!proxy) {
272 return nullptr;
273 }
274
275 GrContext* direct = fImageContext->priv().asDirectContext();
276 if (direct) {
277 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
278
279 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
280 // we're better off instantiating the proxy immediately here.
281 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
282 return nullptr;
283 }
284 }
285 return proxy;
286}
287
288sk_sp<GrTextureProxy> GrProxyProvider::createNonMippedProxyFromBitmap(const SkBitmap& bitmap,
289 SkBackingFit fit,
Brian Salomonbc074a62020-03-18 10:06:13 -0400290 SkBudgeted budgeted) {
Brian Salomona56a7462020-02-07 14:17:25 -0500291 auto dims = bitmap.dimensions();
Greg Daniel6f5441a2020-01-28 17:02:49 -0500292
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400293 auto colorType = SkColorTypeToGrColorType(bitmap.colorType());
294 GrBackendFormat format = this->caps()->getDefaultBackendFormat(colorType, GrRenderable::kNo);
295 if (!format.isValid()) {
296 return nullptr;
297 }
298
Greg Daniel6f5441a2020-01-28 17:02:49 -0500299 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomonbc074a62020-03-18 10:06:13 -0400300 [dims, format, bitmap, fit, colorType, budgeted](GrResourceProvider* resourceProvider) {
Greg Daniel6f5441a2020-01-28 17:02:49 -0500301 GrMipLevel mipLevel = { bitmap.getPixels(), bitmap.rowBytes() };
302
303 return LazyCallbackResult(resourceProvider->createTexture(
Brian Salomonbc074a62020-03-18 10:06:13 -0400304 dims, format, colorType, GrRenderable::kNo, 1, budgeted, fit,
Greg Daniel6f5441a2020-01-28 17:02:49 -0500305 GrProtected::kNo, mipLevel));
306 },
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400307 format, dims, GrRenderable::kNo, 1, GrMipMapped::kNo, GrMipMapsStatus::kNotAllocated,
308 GrInternalSurfaceFlags::kNone, fit, budgeted, GrProtected::kNo, UseAllocator::kYes);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500309
310 if (!proxy) {
311 return nullptr;
312 }
Brian Salomona56a7462020-02-07 14:17:25 -0500313 SkASSERT(proxy->dimensions() == bitmap.dimensions());
Greg Daniel6f5441a2020-01-28 17:02:49 -0500314 return proxy;
315}
316
317sk_sp<GrTextureProxy> GrProxyProvider::createMippedProxyFromBitmap(const SkBitmap& bitmap,
Brian Salomonbc074a62020-03-18 10:06:13 -0400318 SkBudgeted budgeted) {
Greg Daniel6f5441a2020-01-28 17:02:49 -0500319 SkASSERT(this->caps()->mipMapSupport());
320
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400321 auto colorType = SkColorTypeToGrColorType(bitmap.colorType());
322 GrBackendFormat format = this->caps()->getDefaultBackendFormat(colorType, GrRenderable::kNo);
323 if (!format.isValid()) {
324 return nullptr;
325 }
Greg Daniel6f5441a2020-01-28 17:02:49 -0500326
327 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(bitmap.pixmap(), nullptr));
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400328 if (!mipmaps) {
329 return nullptr;
330 }
331
Brian Salomona56a7462020-02-07 14:17:25 -0500332 auto dims = bitmap.dimensions();
Greg Danielce3ddaa2020-01-22 16:58:15 -0500333
Greg Daniela4ead652018-02-07 10:21:48 -0500334 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomonbc074a62020-03-18 10:06:13 -0400335 [dims, format, bitmap, mipmaps, budgeted](GrResourceProvider* resourceProvider) {
Brian Osman1b97f132018-09-13 17:33:48 +0000336 const int mipLevelCount = mipmaps->countLevels() + 1;
337 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
338
Greg Daniel6f5441a2020-01-28 17:02:49 -0500339 texels[0].fPixels = bitmap.getPixels();
340 texels[0].fRowBytes = bitmap.rowBytes();
Greg Daniela4ead652018-02-07 10:21:48 -0500341
Greg Daniel6f5441a2020-01-28 17:02:49 -0500342 auto colorType = SkColorTypeToGrColorType(bitmap.colorType());
Greg Daniela4ead652018-02-07 10:21:48 -0500343 for (int i = 1; i < mipLevelCount; ++i) {
344 SkMipMap::Level generatedMipLevel;
345 mipmaps->getLevel(i - 1, &generatedMipLevel);
346 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
347 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
348 SkASSERT(texels[i].fPixels);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500349 SkASSERT(generatedMipLevel.fPixmap.colorType() == bitmap.colorType());
Greg Daniela4ead652018-02-07 10:21:48 -0500350 }
Brian Salomonbeb7f522019-08-30 16:19:42 -0400351 return LazyCallbackResult(resourceProvider->createTexture(
Brian Salomonbc074a62020-03-18 10:06:13 -0400352 dims, format, colorType, GrRenderable::kNo, 1, budgeted, GrProtected::kNo,
353 texels.get(), mipLevelCount));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500354 },
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400355 format, dims, GrRenderable::kNo, 1, GrMipMapped::kYes, GrMipMapsStatus::kValid,
356 GrInternalSurfaceFlags::kNone, SkBackingFit::kExact, budgeted, GrProtected::kNo,
357 UseAllocator::kYes);
Greg Daniela4ead652018-02-07 10:21:48 -0500358
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400359 if (!proxy) {
360 return nullptr;
361 }
362
Brian Salomona56a7462020-02-07 14:17:25 -0500363 SkASSERT(proxy->dimensions() == bitmap.dimensions());
Greg Daniel6f5441a2020-01-28 17:02:49 -0500364
Greg Daniela4ead652018-02-07 10:21:48 -0500365 return proxy;
366}
367
Greg Daniel4065d452018-11-16 15:43:41 -0500368sk_sp<GrTextureProxy> GrProxyProvider::createProxy(const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -0500369 SkISize dimensions,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400370 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400371 int renderTargetSampleCnt,
Greg Danielf6f7b672018-02-15 13:06:26 -0500372 GrMipMapped mipMapped,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500373 SkBackingFit fit,
374 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -0400375 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400376 GrInternalSurfaceFlags surfaceFlags,
377 GrSurfaceProxy::UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500378 ASSERT_SINGLE_OWNER
379 if (this->isAbandoned()) {
380 return nullptr;
381 }
382
Robert Phillips8ff8bcc2019-07-29 17:03:35 -0400383 const GrCaps* caps = this->caps();
384
385 if (caps->isFormatCompressed(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400386 // Deferred proxies for compressed textures are not supported.
387 return nullptr;
388 }
Robert Phillips0902c982019-07-16 07:47:56 -0400389
Greg Danielf6f7b672018-02-15 13:06:26 -0500390 if (GrMipMapped::kYes == mipMapped) {
391 // SkMipMap doesn't include the base level in the level count so we have to add 1
Brian Salomona56a7462020-02-07 14:17:25 -0500392 int mipCount = SkMipMap::ComputeLevelCount(dimensions.fWidth, dimensions.fHeight) + 1;
Greg Danielf6f7b672018-02-15 13:06:26 -0500393 if (1 == mipCount) {
394 mipMapped = GrMipMapped::kNo;
395 }
396 }
397
Brian Salomona56a7462020-02-07 14:17:25 -0500398 if (!caps->validateSurfaceParams(dimensions, format, renderable, renderTargetSampleCnt,
399 mipMapped)) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500400 return nullptr;
401 }
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600402 GrMipMapsStatus mipMapsStatus = (GrMipMapped::kYes == mipMapped)
403 ? GrMipMapsStatus::kDirty
404 : GrMipMapsStatus::kNotAllocated;
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400405 if (renderable == GrRenderable::kYes) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400406 renderTargetSampleCnt =
Greg Daniel6fa62e22019-08-07 15:52:37 -0400407 caps->getRenderTargetSampleCount(renderTargetSampleCnt, format);
408 SkASSERT(renderTargetSampleCnt);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500409 // We know anything we instantiate later from this deferred path will be
410 // both texturable and renderable
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400411 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400412 *caps, format, dimensions, renderTargetSampleCnt, mipMapped, mipMapsStatus, fit,
413 budgeted, isProtected, surfaceFlags, useAllocator));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500414 }
415
Greg Daniel3a365112020-02-14 10:47:18 -0500416 return sk_sp<GrTextureProxy>(new GrTextureProxy(format, dimensions, mipMapped, mipMapsStatus,
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400417 fit, budgeted, isProtected, surfaceFlags,
418 useAllocator));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500419}
420
Brian Salomonbb8dde82019-06-27 10:52:13 -0400421sk_sp<GrTextureProxy> GrProxyProvider::createCompressedTextureProxy(
Robert Phillips3a833922020-01-21 15:25:58 -0500422 SkISize dimensions, SkBudgeted budgeted, GrMipMapped mipMapped, GrProtected isProtected,
Robert Phillipse4720c62020-01-14 14:33:24 -0500423 SkImage::CompressionType compressionType, sk_sp<SkData> data) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500424 ASSERT_SINGLE_OWNER
425 if (this->isAbandoned()) {
426 return nullptr;
427 }
Brian Salomonbb8dde82019-06-27 10:52:13 -0400428
Robert Phillips8ff8bcc2019-07-29 17:03:35 -0400429 GrBackendFormat format = this->caps()->getBackendFormatFromCompressionType(compressionType);
430
Greg Daniel7bfc9132019-08-14 14:23:53 -0400431 if (!this->caps()->isFormatTexturable(format)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500432 return nullptr;
433 }
434
Robert Phillipse4720c62020-01-14 14:33:24 -0500435 GrMipMapsStatus mipMapsStatus = (GrMipMapped::kYes == mipMapped)
436 ? GrMipMapsStatus::kValid
437 : GrMipMapsStatus::kNotAllocated;
438
Jim Van Verthee06b332019-01-18 10:36:32 -0500439 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomona56a7462020-02-07 14:17:25 -0500440 [dimensions, format, budgeted, mipMapped, isProtected,
441 data](GrResourceProvider* resourceProvider) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400442 return LazyCallbackResult(resourceProvider->createCompressedTexture(
Robert Phillips3a833922020-01-21 15:25:58 -0500443 dimensions, format, budgeted, mipMapped, isProtected, data.get()));
Brian Salomonbb8dde82019-06-27 10:52:13 -0400444 },
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400445 format, dimensions, GrRenderable::kNo, 1, mipMapped, mipMapsStatus,
Greg Daniel3a365112020-02-14 10:47:18 -0500446 GrInternalSurfaceFlags::kReadOnly, SkBackingFit::kExact, SkBudgeted::kYes,
447 GrProtected::kNo, UseAllocator::kYes);
Jim Van Verthee06b332019-01-18 10:36:32 -0500448
449 if (!proxy) {
450 return nullptr;
451 }
452
Robert Phillipsa41c6852019-02-07 10:44:10 -0500453 GrContext* direct = fImageContext->priv().asDirectContext();
454 if (direct) {
455 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Jim Van Verthee06b332019-01-18 10:36:32 -0500456 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
457 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500458 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500459 return nullptr;
460 }
461 }
462 return proxy;
463}
464
Brian Salomon7578f3e2018-03-07 14:39:54 -0500465sk_sp<GrTextureProxy> GrProxyProvider::wrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500466 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500467 GrWrapCacheable cacheable,
Brian Salomonc67c31c2018-12-06 10:00:03 -0500468 GrIOType ioType,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500469 ReleaseProc releaseProc,
470 ReleaseContext releaseCtx) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500471 SkASSERT(ioType != kWrite_GrIOType);
Robert Phillipsf9bec202018-01-16 09:21:01 -0500472 if (this->isAbandoned()) {
473 return nullptr;
474 }
475
Brian Salomonf7778972018-03-08 10:13:17 -0500476 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500477 GrContext* direct = fImageContext->priv().asDirectContext();
478 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500479 return nullptr;
480 }
481
Robert Phillipsa41c6852019-02-07 10:44:10 -0500482 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
483
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500484 sk_sp<GrTexture> tex =
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400485 resourceProvider->wrapBackendTexture(backendTex, ownership, cacheable, ioType);
Brian Salomonf7778972018-03-08 10:13:17 -0500486 if (!tex) {
487 return nullptr;
488 }
Robert Phillipsadbe1322018-01-17 13:35:46 -0500489
Greg Daniel6a0176b2018-01-30 09:28:44 -0500490 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500491 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel6a0176b2018-01-30 09:28:44 -0500492 }
493
Brian Salomonf7778972018-03-08 10:13:17 -0500494 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
495 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500496 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500497
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400498 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500499}
500
Robert Phillipsead321b2019-12-19 10:16:32 -0500501sk_sp<GrTextureProxy> GrProxyProvider::wrapCompressedBackendTexture(const GrBackendTexture& beTex,
Robert Phillipsead321b2019-12-19 10:16:32 -0500502 GrWrapOwnership ownership,
503 GrWrapCacheable cacheable,
504 ReleaseProc releaseProc,
505 ReleaseContext releaseCtx) {
506 if (this->isAbandoned()) {
507 return nullptr;
508 }
509
510 // This is only supported on a direct GrContext.
511 GrContext* direct = fImageContext->priv().asDirectContext();
512 if (!direct) {
513 return nullptr;
514 }
515
516 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
517
518 sk_sp<GrTexture> tex = resourceProvider->wrapCompressedBackendTexture(beTex, ownership,
519 cacheable);
520 if (!tex) {
521 return nullptr;
522 }
523
524 if (releaseProc) {
525 tex->setRelease(releaseProc, releaseCtx);
526 }
527
528 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
529 // Make sure we match how we created the proxy with SkBudgeted::kNo
530 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
531
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400532 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), UseAllocator::kNo));
Robert Phillipsead321b2019-12-19 10:16:32 -0500533}
534
Brian Salomon7578f3e2018-03-07 14:39:54 -0500535sk_sp<GrTextureProxy> GrProxyProvider::wrapRenderableBackendTexture(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400536 const GrBackendTexture& backendTex,
537 int sampleCnt,
538 GrWrapOwnership ownership,
539 GrWrapCacheable cacheable,
540 ReleaseProc releaseProc,
Greg Daniel3a365112020-02-14 10:47:18 -0500541 ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500542 if (this->isAbandoned()) {
543 return nullptr;
544 }
545
Brian Salomonf7778972018-03-08 10:13:17 -0500546 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500547 GrContext* direct = fImageContext->priv().asDirectContext();
548 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500549 return nullptr;
550 }
551
Robert Phillips0902c982019-07-16 07:47:56 -0400552 const GrCaps* caps = this->caps();
553
Robert Phillipsa41c6852019-02-07 10:44:10 -0500554 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
555
Greg Daniel6fa62e22019-08-07 15:52:37 -0400556 sampleCnt = caps->getRenderTargetSampleCount(sampleCnt, backendTex.getBackendFormat());
557 SkASSERT(sampleCnt);
558
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400559 sk_sp<GrTexture> tex = resourceProvider->wrapRenderableBackendTexture(
560 backendTex, sampleCnt, ownership, cacheable);
Brian Salomonf7778972018-03-08 10:13:17 -0500561 if (!tex) {
562 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500563 }
564
Greg Daniel8ce79912019-02-05 10:08:43 -0500565 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500566 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500567 }
568
Brian Salomonf7778972018-03-08 10:13:17 -0500569 SkASSERT(tex->asRenderTarget()); // A GrTextureRenderTarget
570 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500571 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Greg Daniel6abda432018-02-15 14:55:00 -0500572
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400573 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500574}
575
Brian Salomon7578f3e2018-03-07 14:39:54 -0500576sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400577 const GrBackendRenderTarget& backendRT,
578 ReleaseProc releaseProc,
Greg Daniel3a365112020-02-14 10:47:18 -0500579 ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500580 if (this->isAbandoned()) {
581 return nullptr;
582 }
583
Brian Salomonf7778972018-03-08 10:13:17 -0500584 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500585 GrContext* direct = fImageContext->priv().asDirectContext();
586 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500587 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500588 }
589
Robert Phillipsa41c6852019-02-07 10:44:10 -0500590 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
591
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400592 sk_sp<GrRenderTarget> rt = resourceProvider->wrapBackendRenderTarget(backendRT);
Brian Salomonf7778972018-03-08 10:13:17 -0500593 if (!rt) {
594 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500595 }
Greg Daniel8ce79912019-02-05 10:08:43 -0500596
Greg Daniel8ce79912019-02-05 10:08:43 -0500597 if (releaseProc) {
Brian Salomon2ca31f82019-03-05 13:28:58 -0500598 rt->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500599 }
600
Brian Salomonf7778972018-03-08 10:13:17 -0500601 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
602 SkASSERT(!rt->getUniqueKey().isValid());
603 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500604 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Brian Salomonf7778972018-03-08 10:13:17 -0500605
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400606 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(std::move(rt), UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500607}
608
Brian Salomon7578f3e2018-03-07 14:39:54 -0500609sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendTextureAsRenderTarget(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400610 const GrBackendTexture& backendTex, int sampleCnt) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500611 if (this->isAbandoned()) {
612 return nullptr;
613 }
614
Brian Salomonf7778972018-03-08 10:13:17 -0500615 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500616 GrContext* direct = fImageContext->priv().asDirectContext();
617 if (!direct) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500618 return nullptr;
619 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500620
Robert Phillipsa41c6852019-02-07 10:44:10 -0500621 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
622
Brian Salomonf7778972018-03-08 10:13:17 -0500623 sk_sp<GrRenderTarget> rt =
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400624 resourceProvider->wrapBackendTextureAsRenderTarget(backendTex, sampleCnt);
Brian Salomonf7778972018-03-08 10:13:17 -0500625 if (!rt) {
626 return nullptr;
Greg Danielf87651e2018-02-21 11:36:53 -0500627 }
Brian Salomonf7778972018-03-08 10:13:17 -0500628 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
629 SkASSERT(!rt->getUniqueKey().isValid());
Greg Danielb46add82019-01-02 14:51:29 -0500630 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500631 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielf87651e2018-02-21 11:36:53 -0500632
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400633 return sk_sp<GrSurfaceProxy>(new GrRenderTargetProxy(std::move(rt), UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500634}
635
Greg Danielb46add82019-01-02 14:51:29 -0500636sk_sp<GrRenderTargetProxy> GrProxyProvider::wrapVulkanSecondaryCBAsRenderTarget(
637 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
638 if (this->isAbandoned()) {
639 return nullptr;
640 }
641
642 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500643 GrContext* direct = fImageContext->priv().asDirectContext();
644 if (!direct) {
Greg Danielb46add82019-01-02 14:51:29 -0500645 return nullptr;
646 }
647
Robert Phillipsa41c6852019-02-07 10:44:10 -0500648 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Danielb46add82019-01-02 14:51:29 -0500649
Robert Phillipsa41c6852019-02-07 10:44:10 -0500650 sk_sp<GrRenderTarget> rt = resourceProvider->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
651 vkInfo);
Greg Danielb46add82019-01-02 14:51:29 -0500652 if (!rt) {
653 return nullptr;
654 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500655
Greg Danielb46add82019-01-02 14:51:29 -0500656 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
657 SkASSERT(!rt->getUniqueKey().isValid());
658 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500659 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielb46add82019-01-02 14:51:29 -0500660
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400661 GrColorType colorType = SkColorTypeToGrColorType(imageInfo.colorType());
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400662
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400663 if (!this->caps()->isFormatAsColorTypeRenderable(colorType, rt->backendFormat(),
664 rt->numSamples())) {
665 return nullptr;
666 }
667
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400668 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
669 std::move(rt), UseAllocator::kNo, GrRenderTargetProxy::WrapsVkSecondaryCB::kYes));
Brian Salomone8a766b2019-07-19 14:24:36 -0400670}
671
672sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
673 const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -0500674 SkISize dimensions,
Brian Salomone8a766b2019-07-19 14:24:36 -0400675 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400676 int renderTargetSampleCnt,
Brian Salomone8a766b2019-07-19 14:24:36 -0400677 GrMipMapped mipMapped,
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600678 GrMipMapsStatus mipMapsStatus,
Brian Salomone8a766b2019-07-19 14:24:36 -0400679 GrInternalSurfaceFlags surfaceFlags,
680 SkBackingFit fit,
681 SkBudgeted budgeted,
682 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400683 GrSurfaceProxy::UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500684 ASSERT_SINGLE_OWNER
685 if (this->isAbandoned()) {
686 return nullptr;
687 }
Brian Salomona56a7462020-02-07 14:17:25 -0500688 SkASSERT((dimensions.fWidth <= 0 && dimensions.fHeight <= 0) ||
689 (dimensions.fWidth > 0 && dimensions.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400690
Robert Phillips0a15cc62019-07-30 12:49:10 -0400691 if (!format.isValid()) {
692 return nullptr;
693 }
694
Brian Salomona56a7462020-02-07 14:17:25 -0500695 if (dimensions.fWidth > this->caps()->maxTextureSize() ||
696 dimensions.fHeight > this->caps()->maxTextureSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400697 return nullptr;
698 }
699
Brian Salomonbeb7f522019-08-30 16:19:42 -0400700 if (renderable == GrRenderable::kYes) {
701 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(*this->caps(),
702 std::move(callback),
703 format,
Brian Salomona56a7462020-02-07 14:17:25 -0500704 dimensions,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400705 renderTargetSampleCnt,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400706 mipMapped,
707 mipMapsStatus,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400708 fit,
709 budgeted,
710 isProtected,
711 surfaceFlags,
712 useAllocator));
713 } else {
714 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(callback),
715 format,
Brian Salomona56a7462020-02-07 14:17:25 -0500716 dimensions,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400717 mipMapped,
718 mipMapsStatus,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400719 fit,
720 budgeted,
721 isProtected,
722 surfaceFlags,
723 useAllocator));
724 }
Robert Phillips777707b2018-01-17 11:40:14 -0500725}
726
Robert Phillipse8fabb22018-02-04 14:33:21 -0500727sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy(
Brian Salomonbeb7f522019-08-30 16:19:42 -0400728 LazyInstantiateCallback&& callback,
729 const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -0500730 SkISize dimensions,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400731 int sampleCnt,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400732 GrInternalSurfaceFlags surfaceFlags,
733 const TextureInfo* textureInfo,
734 GrMipMapsStatus mipMapsStatus,
735 SkBackingFit fit,
736 SkBudgeted budgeted,
737 GrProtected isProtected,
738 bool wrapsVkSecondaryCB,
739 UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500740 ASSERT_SINGLE_OWNER
741 if (this->isAbandoned()) {
742 return nullptr;
743 }
Brian Salomona56a7462020-02-07 14:17:25 -0500744 SkASSERT((dimensions.fWidth <= 0 && dimensions.fHeight <= 0) ||
745 (dimensions.fWidth > 0 && dimensions.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400746
Brian Salomona56a7462020-02-07 14:17:25 -0500747 if (dimensions.fWidth > this->caps()->maxRenderTargetSize() ||
748 dimensions.fHeight > this->caps()->maxRenderTargetSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400749 return nullptr;
750 }
751
Brian Salomon7226c232018-07-30 13:13:17 -0400752 if (textureInfo) {
Greg Danielb085fa92019-03-05 16:55:12 -0500753 // Wrapped vulkan secondary command buffers don't support texturing since we won't have an
754 // actual VkImage to texture from.
755 SkASSERT(!wrapsVkSecondaryCB);
Brian Salomon7226c232018-07-30 13:13:17 -0400756 return sk_sp<GrRenderTargetProxy>(new GrTextureRenderTargetProxy(
Greg Daniel3a365112020-02-14 10:47:18 -0500757 *this->caps(), std::move(callback), format, dimensions, sampleCnt,
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400758 textureInfo->fMipMapped, mipMapsStatus, fit, budgeted, isProtected, surfaceFlags,
759 useAllocator));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500760 }
761
Greg Danielb085fa92019-03-05 16:55:12 -0500762 GrRenderTargetProxy::WrapsVkSecondaryCB vkSCB =
763 wrapsVkSecondaryCB ? GrRenderTargetProxy::WrapsVkSecondaryCB::kYes
764 : GrRenderTargetProxy::WrapsVkSecondaryCB::kNo;
765
Greg Daniel3a365112020-02-14 10:47:18 -0500766 return sk_sp<GrRenderTargetProxy>(
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400767 new GrRenderTargetProxy(std::move(callback), format, dimensions, sampleCnt, fit,
768 budgeted, isProtected, surfaceFlags, useAllocator, vkSCB));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500769}
770
Brian Salomonbeb7f522019-08-30 16:19:42 -0400771sk_sp<GrTextureProxy> GrProxyProvider::MakeFullyLazyProxy(LazyInstantiateCallback&& callback,
772 const GrBackendFormat& format,
773 GrRenderable renderable,
774 int renderTargetSampleCnt,
775 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400776 const GrCaps& caps,
777 UseAllocator useAllocator) {
Robert Phillips0a15cc62019-07-30 12:49:10 -0400778 if (!format.isValid()) {
779 return nullptr;
780 }
781
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400782 SkASSERT(renderTargetSampleCnt == 1 || renderable == GrRenderable::kYes);
Robert Phillips10d17212019-04-24 14:09:10 -0400783 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNone;
Robert Phillips777707b2018-01-17 11:40:14 -0500784
Brian Salomona56a7462020-02-07 14:17:25 -0500785 static constexpr SkISize kLazyDims = {-1, -1};
Brian Salomonbeb7f522019-08-30 16:19:42 -0400786 if (GrRenderable::kYes == renderable) {
787 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
Greg Daniel3a365112020-02-14 10:47:18 -0500788 caps, std::move(callback), format, kLazyDims, renderTargetSampleCnt,
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400789 GrMipMapped::kNo, GrMipMapsStatus::kNotAllocated, SkBackingFit::kApprox,
Greg Daniel14b57212019-12-17 16:18:06 -0500790 SkBudgeted::kYes, isProtected, surfaceFlags, useAllocator));
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400791 } else {
792 return sk_sp<GrTextureProxy>(
793 new GrTextureProxy(std::move(callback), format, kLazyDims, GrMipMapped::kNo,
794 GrMipMapsStatus::kNotAllocated, SkBackingFit::kApprox,
795 SkBudgeted::kYes, isProtected, surfaceFlags, useAllocator));
Brian Salomonbeb7f522019-08-30 16:19:42 -0400796 }
Robert Phillips777707b2018-01-17 11:40:14 -0500797}
798
Robert Phillips427966a2018-12-20 17:20:43 -0500799void GrProxyProvider::processInvalidUniqueKey(const GrUniqueKey& key, GrTextureProxy* proxy,
800 InvalidateGPUResource invalidateGPUResource) {
Mike Klein04ef8102020-03-16 12:44:23 -0500801 this->processInvalidUniqueKeyImpl(key, proxy, invalidateGPUResource, RemoveTableEntry::kYes);
802}
803
804void GrProxyProvider::processInvalidUniqueKeyImpl(const GrUniqueKey& key, GrTextureProxy* proxy,
805 InvalidateGPUResource invalidateGPUResource,
806 RemoveTableEntry removeTableEntry) {
Chris Dalton2de13dd2019-01-03 15:11:59 -0700807 SkASSERT(key.isValid());
808
Robert Phillips427966a2018-12-20 17:20:43 -0500809 if (!proxy) {
810 proxy = fUniquelyKeyedProxies.find(key);
811 }
Chris Dalton2de13dd2019-01-03 15:11:59 -0700812 SkASSERT(!proxy || proxy->getUniqueKey() == key);
813
814 // Locate the corresponding GrGpuResource (if it needs to be invalidated) before clearing the
815 // proxy's unique key. We must do it in this order because 'key' may alias the proxy's key.
816 sk_sp<GrGpuResource> invalidGpuResource;
817 if (InvalidateGPUResource::kYes == invalidateGPUResource) {
Brian Salomon01ceae92019-04-02 11:49:54 -0400818 GrContext* direct = fImageContext->priv().asDirectContext();
819 if (direct) {
820 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
821 invalidGpuResource = resourceProvider->findByUniqueKey<GrGpuResource>(key);
Chris Dalton2de13dd2019-01-03 15:11:59 -0700822 }
823 SkASSERT(!invalidGpuResource || invalidGpuResource->getUniqueKey() == key);
824 }
Robert Phillips427966a2018-12-20 17:20:43 -0500825
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500826 // Note: this method is called for the whole variety of GrGpuResources so often 'key'
827 // will not be in 'fUniquelyKeyedProxies'.
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500828 if (proxy) {
Mike Klein04ef8102020-03-16 12:44:23 -0500829 if (removeTableEntry == RemoveTableEntry::kYes) {
830 fUniquelyKeyedProxies.remove(key);
831 }
Robert Phillips427966a2018-12-20 17:20:43 -0500832 proxy->cacheAccess().clearUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500833 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500834
Chris Dalton2de13dd2019-01-03 15:11:59 -0700835 if (invalidGpuResource) {
836 invalidGpuResource->resourcePriv().removeUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500837 }
838}
839
Robert Phillipsa41c6852019-02-07 10:44:10 -0500840uint32_t GrProxyProvider::contextID() const {
841 return fImageContext->priv().contextID();
842}
843
844const GrCaps* GrProxyProvider::caps() const {
845 return fImageContext->priv().caps();
846}
847
848sk_sp<const GrCaps> GrProxyProvider::refCaps() const {
849 return fImageContext->priv().refCaps();
850}
851
Robert Phillipsa9162df2019-02-11 14:12:03 -0500852bool GrProxyProvider::isAbandoned() const {
853 return fImageContext->priv().abandoned();
854}
855
Robert Phillips0790f8a2018-09-18 13:11:03 -0400856void GrProxyProvider::orphanAllUniqueKeys() {
Mike Kleincff63962020-03-14 16:22:45 -0500857 fUniquelyKeyedProxies.foreach([&](GrTextureProxy* proxy){
858 proxy->fProxyProvider = nullptr;
859 });
Robert Phillips0790f8a2018-09-18 13:11:03 -0400860}
861
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500862void GrProxyProvider::removeAllUniqueKeys() {
Mike Klein04ef8102020-03-16 12:44:23 -0500863 fUniquelyKeyedProxies.foreach([&](GrTextureProxy* proxy){
864 // It's not safe to remove table entries while iterating with foreach(),
865 // but since we're going to remove them all anyway, simply save that for the end.
866 this->processInvalidUniqueKeyImpl(proxy->getUniqueKey(), proxy,
867 InvalidateGPUResource::kNo,
868 RemoveTableEntry::kNo);
869 });
870 // Removing all those table entries is safe now.
871 fUniquelyKeyedProxies.reset();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500872}
Robert Phillipsa41c6852019-02-07 10:44:10 -0500873
874bool GrProxyProvider::renderingDirectly() const {
875 return fImageContext->priv().asDirectContext();
876}