blob: 4fb2a64eafbdd93fa5c862ae5c36c6553ee06b82 [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,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400466 GrColorType grColorType,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500467 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500468 GrWrapCacheable cacheable,
Brian Salomonc67c31c2018-12-06 10:00:03 -0500469 GrIOType ioType,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500470 ReleaseProc releaseProc,
471 ReleaseContext releaseCtx) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500472 SkASSERT(ioType != kWrite_GrIOType);
Robert Phillipsf9bec202018-01-16 09:21:01 -0500473 if (this->isAbandoned()) {
474 return nullptr;
475 }
476
Brian Salomonf7778972018-03-08 10:13:17 -0500477 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500478 GrContext* direct = fImageContext->priv().asDirectContext();
479 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500480 return nullptr;
481 }
482
Robert Phillipsa41c6852019-02-07 10:44:10 -0500483 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
484
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500485 sk_sp<GrTexture> tex =
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400486 resourceProvider->wrapBackendTexture(backendTex, grColorType,
487 ownership, cacheable, ioType);
Brian Salomonf7778972018-03-08 10:13:17 -0500488 if (!tex) {
489 return nullptr;
490 }
Robert Phillipsadbe1322018-01-17 13:35:46 -0500491
Greg Daniel6a0176b2018-01-30 09:28:44 -0500492 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500493 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel6a0176b2018-01-30 09:28:44 -0500494 }
495
Brian Salomonf7778972018-03-08 10:13:17 -0500496 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
497 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500498 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500499
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400500 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500501}
502
Robert Phillipsead321b2019-12-19 10:16:32 -0500503sk_sp<GrTextureProxy> GrProxyProvider::wrapCompressedBackendTexture(const GrBackendTexture& beTex,
Robert Phillipsead321b2019-12-19 10:16:32 -0500504 GrWrapOwnership ownership,
505 GrWrapCacheable cacheable,
506 ReleaseProc releaseProc,
507 ReleaseContext releaseCtx) {
508 if (this->isAbandoned()) {
509 return nullptr;
510 }
511
512 // This is only supported on a direct GrContext.
513 GrContext* direct = fImageContext->priv().asDirectContext();
514 if (!direct) {
515 return nullptr;
516 }
517
518 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
519
520 sk_sp<GrTexture> tex = resourceProvider->wrapCompressedBackendTexture(beTex, ownership,
521 cacheable);
522 if (!tex) {
523 return nullptr;
524 }
525
526 if (releaseProc) {
527 tex->setRelease(releaseProc, releaseCtx);
528 }
529
530 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
531 // Make sure we match how we created the proxy with SkBudgeted::kNo
532 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
533
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400534 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), UseAllocator::kNo));
Robert Phillipsead321b2019-12-19 10:16:32 -0500535}
536
Brian Salomon7578f3e2018-03-07 14:39:54 -0500537sk_sp<GrTextureProxy> GrProxyProvider::wrapRenderableBackendTexture(
Greg Daniel3a365112020-02-14 10:47:18 -0500538 const GrBackendTexture& backendTex, int sampleCnt, GrColorType colorType,
539 GrWrapOwnership ownership, GrWrapCacheable cacheable, ReleaseProc releaseProc,
540 ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500541 if (this->isAbandoned()) {
542 return nullptr;
543 }
544
Brian Salomonf7778972018-03-08 10:13:17 -0500545 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500546 GrContext* direct = fImageContext->priv().asDirectContext();
547 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500548 return nullptr;
549 }
550
Robert Phillips0902c982019-07-16 07:47:56 -0400551 const GrCaps* caps = this->caps();
552
Robert Phillipsa41c6852019-02-07 10:44:10 -0500553 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
554
Greg Daniel6fa62e22019-08-07 15:52:37 -0400555 // TODO: This should have been checked and validated before getting into GrProxyProvider.
556 if (!caps->isFormatAsColorTypeRenderable(colorType, backendTex.getBackendFormat(), sampleCnt)) {
Greg Danielf87651e2018-02-21 11:36:53 -0500557 return nullptr;
558 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500559
Greg Daniel6fa62e22019-08-07 15:52:37 -0400560 sampleCnt = caps->getRenderTargetSampleCount(sampleCnt, backendTex.getBackendFormat());
561 SkASSERT(sampleCnt);
562
Robert Phillipsa41c6852019-02-07 10:44:10 -0500563 sk_sp<GrTexture> tex = resourceProvider->wrapRenderableBackendTexture(backendTex, sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400564 colorType, ownership,
565 cacheable);
Brian Salomonf7778972018-03-08 10:13:17 -0500566 if (!tex) {
567 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500568 }
569
Greg Daniel8ce79912019-02-05 10:08:43 -0500570 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500571 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500572 }
573
Brian Salomonf7778972018-03-08 10:13:17 -0500574 SkASSERT(tex->asRenderTarget()); // A GrTextureRenderTarget
575 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500576 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Greg Daniel6abda432018-02-15 14:55:00 -0500577
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400578 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500579}
580
Brian Salomon7578f3e2018-03-07 14:39:54 -0500581sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget(
Greg Daniel3a365112020-02-14 10:47:18 -0500582 const GrBackendRenderTarget& backendRT, GrColorType grColorType, ReleaseProc releaseProc,
583 ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500584 if (this->isAbandoned()) {
585 return nullptr;
586 }
587
Brian Salomonf7778972018-03-08 10:13:17 -0500588 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500589 GrContext* direct = fImageContext->priv().asDirectContext();
590 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500591 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500592 }
593
Robert Phillipsa41c6852019-02-07 10:44:10 -0500594 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
595
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400596 sk_sp<GrRenderTarget> rt = resourceProvider->wrapBackendRenderTarget(backendRT, grColorType);
Brian Salomonf7778972018-03-08 10:13:17 -0500597 if (!rt) {
598 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500599 }
Greg Daniel8ce79912019-02-05 10:08:43 -0500600
Greg Daniel8ce79912019-02-05 10:08:43 -0500601 if (releaseProc) {
Brian Salomon2ca31f82019-03-05 13:28:58 -0500602 rt->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500603 }
604
Brian Salomonf7778972018-03-08 10:13:17 -0500605 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
606 SkASSERT(!rt->getUniqueKey().isValid());
607 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500608 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Brian Salomonf7778972018-03-08 10:13:17 -0500609
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400610 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(std::move(rt), UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500611}
612
Brian Salomon7578f3e2018-03-07 14:39:54 -0500613sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendTextureAsRenderTarget(
Greg Daniel3a365112020-02-14 10:47:18 -0500614 const GrBackendTexture& backendTex, GrColorType grColorType, int sampleCnt) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500615 if (this->isAbandoned()) {
616 return nullptr;
617 }
618
Brian Salomonf7778972018-03-08 10:13:17 -0500619 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500620 GrContext* direct = fImageContext->priv().asDirectContext();
621 if (!direct) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500622 return nullptr;
623 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500624
Robert Phillipsa41c6852019-02-07 10:44:10 -0500625 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
626
Brian Salomonf7778972018-03-08 10:13:17 -0500627 sk_sp<GrRenderTarget> rt =
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400628 resourceProvider->wrapBackendTextureAsRenderTarget(backendTex, sampleCnt, grColorType);
Brian Salomonf7778972018-03-08 10:13:17 -0500629 if (!rt) {
630 return nullptr;
Greg Danielf87651e2018-02-21 11:36:53 -0500631 }
Brian Salomonf7778972018-03-08 10:13:17 -0500632 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
633 SkASSERT(!rt->getUniqueKey().isValid());
Greg Danielb46add82019-01-02 14:51:29 -0500634 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500635 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielf87651e2018-02-21 11:36:53 -0500636
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400637 return sk_sp<GrSurfaceProxy>(new GrRenderTargetProxy(std::move(rt), UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500638}
639
Greg Danielb46add82019-01-02 14:51:29 -0500640sk_sp<GrRenderTargetProxy> GrProxyProvider::wrapVulkanSecondaryCBAsRenderTarget(
641 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
642 if (this->isAbandoned()) {
643 return nullptr;
644 }
645
646 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500647 GrContext* direct = fImageContext->priv().asDirectContext();
648 if (!direct) {
Greg Danielb46add82019-01-02 14:51:29 -0500649 return nullptr;
650 }
651
Robert Phillipsa41c6852019-02-07 10:44:10 -0500652 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Danielb46add82019-01-02 14:51:29 -0500653
Robert Phillipsa41c6852019-02-07 10:44:10 -0500654 sk_sp<GrRenderTarget> rt = resourceProvider->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
655 vkInfo);
Greg Danielb46add82019-01-02 14:51:29 -0500656 if (!rt) {
657 return nullptr;
658 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500659
Greg Danielb46add82019-01-02 14:51:29 -0500660 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
661 SkASSERT(!rt->getUniqueKey().isValid());
662 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500663 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielb46add82019-01-02 14:51:29 -0500664
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400665 GrColorType colorType = SkColorTypeToGrColorType(imageInfo.colorType());
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400666
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400667 if (!this->caps()->isFormatAsColorTypeRenderable(colorType, rt->backendFormat(),
668 rt->numSamples())) {
669 return nullptr;
670 }
671
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400672 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
673 std::move(rt), UseAllocator::kNo, GrRenderTargetProxy::WrapsVkSecondaryCB::kYes));
Brian Salomone8a766b2019-07-19 14:24:36 -0400674}
675
676sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
677 const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -0500678 SkISize dimensions,
Brian Salomone8a766b2019-07-19 14:24:36 -0400679 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400680 int renderTargetSampleCnt,
Brian Salomone8a766b2019-07-19 14:24:36 -0400681 GrMipMapped mipMapped,
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600682 GrMipMapsStatus mipMapsStatus,
Brian Salomone8a766b2019-07-19 14:24:36 -0400683 GrInternalSurfaceFlags surfaceFlags,
684 SkBackingFit fit,
685 SkBudgeted budgeted,
686 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400687 GrSurfaceProxy::UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500688 ASSERT_SINGLE_OWNER
689 if (this->isAbandoned()) {
690 return nullptr;
691 }
Brian Salomona56a7462020-02-07 14:17:25 -0500692 SkASSERT((dimensions.fWidth <= 0 && dimensions.fHeight <= 0) ||
693 (dimensions.fWidth > 0 && dimensions.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400694
Robert Phillips0a15cc62019-07-30 12:49:10 -0400695 if (!format.isValid()) {
696 return nullptr;
697 }
698
Brian Salomona56a7462020-02-07 14:17:25 -0500699 if (dimensions.fWidth > this->caps()->maxTextureSize() ||
700 dimensions.fHeight > this->caps()->maxTextureSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400701 return nullptr;
702 }
703
Brian Salomonbeb7f522019-08-30 16:19:42 -0400704 if (renderable == GrRenderable::kYes) {
705 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(*this->caps(),
706 std::move(callback),
707 format,
Brian Salomona56a7462020-02-07 14:17:25 -0500708 dimensions,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400709 renderTargetSampleCnt,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400710 mipMapped,
711 mipMapsStatus,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400712 fit,
713 budgeted,
714 isProtected,
715 surfaceFlags,
716 useAllocator));
717 } else {
718 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(callback),
719 format,
Brian Salomona56a7462020-02-07 14:17:25 -0500720 dimensions,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400721 mipMapped,
722 mipMapsStatus,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400723 fit,
724 budgeted,
725 isProtected,
726 surfaceFlags,
727 useAllocator));
728 }
Robert Phillips777707b2018-01-17 11:40:14 -0500729}
730
Robert Phillipse8fabb22018-02-04 14:33:21 -0500731sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy(
Brian Salomonbeb7f522019-08-30 16:19:42 -0400732 LazyInstantiateCallback&& callback,
733 const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -0500734 SkISize dimensions,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400735 int sampleCnt,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400736 GrInternalSurfaceFlags surfaceFlags,
737 const TextureInfo* textureInfo,
738 GrMipMapsStatus mipMapsStatus,
739 SkBackingFit fit,
740 SkBudgeted budgeted,
741 GrProtected isProtected,
742 bool wrapsVkSecondaryCB,
743 UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500744 ASSERT_SINGLE_OWNER
745 if (this->isAbandoned()) {
746 return nullptr;
747 }
Brian Salomona56a7462020-02-07 14:17:25 -0500748 SkASSERT((dimensions.fWidth <= 0 && dimensions.fHeight <= 0) ||
749 (dimensions.fWidth > 0 && dimensions.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400750
Brian Salomona56a7462020-02-07 14:17:25 -0500751 if (dimensions.fWidth > this->caps()->maxRenderTargetSize() ||
752 dimensions.fHeight > this->caps()->maxRenderTargetSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400753 return nullptr;
754 }
755
Brian Salomon7226c232018-07-30 13:13:17 -0400756 if (textureInfo) {
Greg Danielb085fa92019-03-05 16:55:12 -0500757 // Wrapped vulkan secondary command buffers don't support texturing since we won't have an
758 // actual VkImage to texture from.
759 SkASSERT(!wrapsVkSecondaryCB);
Brian Salomon7226c232018-07-30 13:13:17 -0400760 return sk_sp<GrRenderTargetProxy>(new GrTextureRenderTargetProxy(
Greg Daniel3a365112020-02-14 10:47:18 -0500761 *this->caps(), std::move(callback), format, dimensions, sampleCnt,
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400762 textureInfo->fMipMapped, mipMapsStatus, fit, budgeted, isProtected, surfaceFlags,
763 useAllocator));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500764 }
765
Greg Danielb085fa92019-03-05 16:55:12 -0500766 GrRenderTargetProxy::WrapsVkSecondaryCB vkSCB =
767 wrapsVkSecondaryCB ? GrRenderTargetProxy::WrapsVkSecondaryCB::kYes
768 : GrRenderTargetProxy::WrapsVkSecondaryCB::kNo;
769
Greg Daniel3a365112020-02-14 10:47:18 -0500770 return sk_sp<GrRenderTargetProxy>(
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400771 new GrRenderTargetProxy(std::move(callback), format, dimensions, sampleCnt, fit,
772 budgeted, isProtected, surfaceFlags, useAllocator, vkSCB));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500773}
774
Brian Salomonbeb7f522019-08-30 16:19:42 -0400775sk_sp<GrTextureProxy> GrProxyProvider::MakeFullyLazyProxy(LazyInstantiateCallback&& callback,
776 const GrBackendFormat& format,
777 GrRenderable renderable,
778 int renderTargetSampleCnt,
779 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400780 const GrCaps& caps,
781 UseAllocator useAllocator) {
Robert Phillips0a15cc62019-07-30 12:49:10 -0400782 if (!format.isValid()) {
783 return nullptr;
784 }
785
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400786 SkASSERT(renderTargetSampleCnt == 1 || renderable == GrRenderable::kYes);
Robert Phillips10d17212019-04-24 14:09:10 -0400787 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNone;
Robert Phillips777707b2018-01-17 11:40:14 -0500788
Brian Salomona56a7462020-02-07 14:17:25 -0500789 static constexpr SkISize kLazyDims = {-1, -1};
Brian Salomonbeb7f522019-08-30 16:19:42 -0400790 if (GrRenderable::kYes == renderable) {
791 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
Greg Daniel3a365112020-02-14 10:47:18 -0500792 caps, std::move(callback), format, kLazyDims, renderTargetSampleCnt,
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400793 GrMipMapped::kNo, GrMipMapsStatus::kNotAllocated, SkBackingFit::kApprox,
Greg Daniel14b57212019-12-17 16:18:06 -0500794 SkBudgeted::kYes, isProtected, surfaceFlags, useAllocator));
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400795 } else {
796 return sk_sp<GrTextureProxy>(
797 new GrTextureProxy(std::move(callback), format, kLazyDims, GrMipMapped::kNo,
798 GrMipMapsStatus::kNotAllocated, SkBackingFit::kApprox,
799 SkBudgeted::kYes, isProtected, surfaceFlags, useAllocator));
Brian Salomonbeb7f522019-08-30 16:19:42 -0400800 }
Robert Phillips777707b2018-01-17 11:40:14 -0500801}
802
Robert Phillips427966a2018-12-20 17:20:43 -0500803void GrProxyProvider::processInvalidUniqueKey(const GrUniqueKey& key, GrTextureProxy* proxy,
804 InvalidateGPUResource invalidateGPUResource) {
Mike Klein04ef8102020-03-16 12:44:23 -0500805 this->processInvalidUniqueKeyImpl(key, proxy, invalidateGPUResource, RemoveTableEntry::kYes);
806}
807
808void GrProxyProvider::processInvalidUniqueKeyImpl(const GrUniqueKey& key, GrTextureProxy* proxy,
809 InvalidateGPUResource invalidateGPUResource,
810 RemoveTableEntry removeTableEntry) {
Chris Dalton2de13dd2019-01-03 15:11:59 -0700811 SkASSERT(key.isValid());
812
Robert Phillips427966a2018-12-20 17:20:43 -0500813 if (!proxy) {
814 proxy = fUniquelyKeyedProxies.find(key);
815 }
Chris Dalton2de13dd2019-01-03 15:11:59 -0700816 SkASSERT(!proxy || proxy->getUniqueKey() == key);
817
818 // Locate the corresponding GrGpuResource (if it needs to be invalidated) before clearing the
819 // proxy's unique key. We must do it in this order because 'key' may alias the proxy's key.
820 sk_sp<GrGpuResource> invalidGpuResource;
821 if (InvalidateGPUResource::kYes == invalidateGPUResource) {
Brian Salomon01ceae92019-04-02 11:49:54 -0400822 GrContext* direct = fImageContext->priv().asDirectContext();
823 if (direct) {
824 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
825 invalidGpuResource = resourceProvider->findByUniqueKey<GrGpuResource>(key);
Chris Dalton2de13dd2019-01-03 15:11:59 -0700826 }
827 SkASSERT(!invalidGpuResource || invalidGpuResource->getUniqueKey() == key);
828 }
Robert Phillips427966a2018-12-20 17:20:43 -0500829
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500830 // Note: this method is called for the whole variety of GrGpuResources so often 'key'
831 // will not be in 'fUniquelyKeyedProxies'.
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500832 if (proxy) {
Mike Klein04ef8102020-03-16 12:44:23 -0500833 if (removeTableEntry == RemoveTableEntry::kYes) {
834 fUniquelyKeyedProxies.remove(key);
835 }
Robert Phillips427966a2018-12-20 17:20:43 -0500836 proxy->cacheAccess().clearUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500837 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500838
Chris Dalton2de13dd2019-01-03 15:11:59 -0700839 if (invalidGpuResource) {
840 invalidGpuResource->resourcePriv().removeUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500841 }
842}
843
Robert Phillipsa41c6852019-02-07 10:44:10 -0500844uint32_t GrProxyProvider::contextID() const {
845 return fImageContext->priv().contextID();
846}
847
848const GrCaps* GrProxyProvider::caps() const {
849 return fImageContext->priv().caps();
850}
851
852sk_sp<const GrCaps> GrProxyProvider::refCaps() const {
853 return fImageContext->priv().refCaps();
854}
855
Robert Phillipsa9162df2019-02-11 14:12:03 -0500856bool GrProxyProvider::isAbandoned() const {
857 return fImageContext->priv().abandoned();
858}
859
Robert Phillips0790f8a2018-09-18 13:11:03 -0400860void GrProxyProvider::orphanAllUniqueKeys() {
Mike Kleincff63962020-03-14 16:22:45 -0500861 fUniquelyKeyedProxies.foreach([&](GrTextureProxy* proxy){
862 proxy->fProxyProvider = nullptr;
863 });
Robert Phillips0790f8a2018-09-18 13:11:03 -0400864}
865
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500866void GrProxyProvider::removeAllUniqueKeys() {
Mike Klein04ef8102020-03-16 12:44:23 -0500867 fUniquelyKeyedProxies.foreach([&](GrTextureProxy* proxy){
868 // It's not safe to remove table entries while iterating with foreach(),
869 // but since we're going to remove them all anyway, simply save that for the end.
870 this->processInvalidUniqueKeyImpl(proxy->getUniqueKey(), proxy,
871 InvalidateGPUResource::kNo,
872 RemoveTableEntry::kNo);
873 });
874 // Removing all those table entries is safe now.
875 fUniquelyKeyedProxies.reset();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500876}
Robert Phillipsa41c6852019-02-07 10:44:10 -0500877
878bool GrProxyProvider::renderingDirectly() const {
879 return fImageContext->priv().asDirectContext();
880}