blob: 5edcdc5e29756807a291d8e38fc6c3b081f26313 [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"
Brian Salomond005b692020-04-01 15:47:05 -040026#include "src/gpu/GrRenderTargetContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/GrResourceProvider.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040028#include "src/gpu/GrSurfaceProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/gpu/GrSurfaceProxyPriv.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000030#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "src/gpu/GrTextureProxyCacheAccess.h"
32#include "src/gpu/GrTextureRenderTargetProxy.h"
33#include "src/gpu/SkGr.h"
34#include "src/image/SkImage_Base.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050035
36#define ASSERT_SINGLE_OWNER \
Robert Phillipsa41c6852019-02-07 10:44:10 -050037 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fImageContext->priv().singleOwner());)
Robert Phillips1afd4cd2018-01-08 13:40:32 -050038
Robert Phillipsa9162df2019-02-11 14:12:03 -050039GrProxyProvider::GrProxyProvider(GrImageContext* imageContext) : fImageContext(imageContext) {}
Robert Phillips1afd4cd2018-01-08 13:40:32 -050040
41GrProxyProvider::~GrProxyProvider() {
Robert Phillipsa41c6852019-02-07 10:44:10 -050042 if (this->renderingDirectly()) {
Robert Phillips0790f8a2018-09-18 13:11:03 -040043 // In DDL-mode a proxy provider can still have extant uniquely keyed proxies (since
44 // they need their unique keys to, potentially, find a cached resource when the
45 // DDL is played) but, in non-DDL-mode they should all have been cleaned up by this point.
46 SkASSERT(!fUniquelyKeyedProxies.count());
47 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050048}
49
Robert Phillipsadbe1322018-01-17 13:35:46 -050050bool GrProxyProvider::assignUniqueKeyToProxy(const GrUniqueKey& key, GrTextureProxy* proxy) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050051 ASSERT_SINGLE_OWNER
52 SkASSERT(key.isValid());
53 if (this->isAbandoned() || !proxy) {
Robert Phillipsadbe1322018-01-17 13:35:46 -050054 return false;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050055 }
56
Robert Phillipsf10b2a52020-05-15 10:20:04 -040057 // Only the proxyProvider that created a proxy should be assigning unique keys to it.
58 SkASSERT(this->isDDLProvider() == proxy->creatingProvider());
59
Robert Phillipsa41c6852019-02-07 10:44:10 -050060#ifdef SK_DEBUG
61 {
62 GrContext* direct = fImageContext->priv().asDirectContext();
63 if (direct) {
64 GrResourceCache* resourceCache = direct->priv().getResourceCache();
65 // If there is already a GrResource with this key then the caller has violated the
66 // normal usage pattern of uniquely keyed resources (e.g., they have created one w/o
67 // first seeing if it already existed in the cache).
68 SkASSERT(!resourceCache->findAndRefUniqueResource(key));
69 }
70 }
71#endif
Robert Phillips1afd4cd2018-01-08 13:40:32 -050072
Robert Phillips1afd4cd2018-01-08 13:40:32 -050073 SkASSERT(!fUniquelyKeyedProxies.find(key)); // multiple proxies can't get the same key
74
75 proxy->cacheAccess().setUniqueKey(this, key);
76 SkASSERT(proxy->getUniqueKey() == key);
77 fUniquelyKeyedProxies.add(proxy);
Robert Phillipsadbe1322018-01-17 13:35:46 -050078 return true;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050079}
80
81void GrProxyProvider::adoptUniqueKeyFromSurface(GrTextureProxy* proxy, const GrSurface* surf) {
82 SkASSERT(surf->getUniqueKey().isValid());
83 proxy->cacheAccess().setUniqueKey(this, surf->getUniqueKey());
84 SkASSERT(proxy->getUniqueKey() == surf->getUniqueKey());
85 // multiple proxies can't get the same key
86 SkASSERT(!fUniquelyKeyedProxies.find(surf->getUniqueKey()));
87 fUniquelyKeyedProxies.add(proxy);
88}
89
Chris Dalton2de13dd2019-01-03 15:11:59 -070090void GrProxyProvider::removeUniqueKeyFromProxy(GrTextureProxy* proxy) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050091 ASSERT_SINGLE_OWNER
Chris Dalton2de13dd2019-01-03 15:11:59 -070092 SkASSERT(proxy);
93 SkASSERT(proxy->getUniqueKey().isValid());
94
95 if (this->isAbandoned()) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050096 return;
97 }
Robert Phillips0790f8a2018-09-18 13:11:03 -040098
Chris Dalton2de13dd2019-01-03 15:11:59 -070099 this->processInvalidUniqueKey(proxy->getUniqueKey(), proxy, InvalidateGPUResource::kYes);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500100}
101
Greg Daniel3a365112020-02-14 10:47:18 -0500102sk_sp<GrTextureProxy> GrProxyProvider::findProxyByUniqueKey(const GrUniqueKey& key) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500103 ASSERT_SINGLE_OWNER
104
105 if (this->isAbandoned()) {
106 return nullptr;
107 }
108
Brian Salomon01ceae92019-04-02 11:49:54 -0400109 GrTextureProxy* proxy = fUniquelyKeyedProxies.find(key);
Brian Salomon01ceae92019-04-02 11:49:54 -0400110 if (proxy) {
Robert Phillipse5f73282019-06-18 17:15:04 -0400111 return sk_ref_sp(proxy);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500112 }
Robert Phillipse5f73282019-06-18 17:15:04 -0400113 return nullptr;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500114}
115
Robert Phillipsa41c6852019-02-07 10:44:10 -0500116///////////////////////////////////////////////////////////////////////////////
117
118#if GR_TEST_UTILS
119sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createInstantiatedProxy(
Brian Salomona56a7462020-02-07 14:17:25 -0500120 SkISize dimensions,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400121 const GrBackendFormat& format,
122 GrRenderable renderable,
123 int renderTargetSampleCnt,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400124 SkBackingFit fit,
125 SkBudgeted budgeted,
126 GrProtected isProtected) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500127 ASSERT_SINGLE_OWNER
128 if (this->isAbandoned()) {
129 return nullptr;
130 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500131 GrContext* direct = fImageContext->priv().asDirectContext();
132 if (!direct) {
133 return nullptr;
134 }
135
Brian Salomon4eb38b72019-08-05 12:58:39 -0400136 if (this->caps()->isFormatCompressed(format)) {
137 // TODO: Allow this to go to GrResourceProvider::createCompressedTexture() once we no longer
Greg Daniel4cb29332020-01-23 10:07:02 -0500138 // rely on GrColorType to get a swizzle for the proxy.
Brian Salomon4eb38b72019-08-05 12:58:39 -0400139 return nullptr;
140 }
Brian Salomon4eb38b72019-08-05 12:58:39 -0400141
Robert Phillipsa41c6852019-02-07 10:44:10 -0500142 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
143 sk_sp<GrTexture> tex;
144
145 if (SkBackingFit::kApprox == fit) {
Brian Salomona56a7462020-02-07 14:17:25 -0500146 tex = resourceProvider->createApproxTexture(dimensions, format, renderable,
147 renderTargetSampleCnt, isProtected);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500148 } else {
Brian Salomona56a7462020-02-07 14:17:25 -0500149 tex = resourceProvider->createTexture(dimensions, format, renderable, renderTargetSampleCnt,
Brian Salomona90382f2019-09-17 09:01:56 -0400150 GrMipMapped::kNo, budgeted, isProtected);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500151 }
152 if (!tex) {
153 return nullptr;
154 }
155
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400156 return this->createWrapped(std::move(tex), UseAllocator::kYes);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500157}
158
Brian Salomon4eb38b72019-08-05 12:58:39 -0400159sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createInstantiatedProxy(
Brian Salomona56a7462020-02-07 14:17:25 -0500160 SkISize dimensions,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400161 GrColorType colorType,
162 GrRenderable renderable,
163 int renderTargetSampleCnt,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400164 SkBackingFit fit,
165 SkBudgeted budgeted,
166 GrProtected isProtected) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500167 ASSERT_SINGLE_OWNER
168 if (this->isAbandoned()) {
169 return nullptr;
170 }
Brian Salomon4eb38b72019-08-05 12:58:39 -0400171 auto format = this->caps()->getDefaultBackendFormat(colorType, renderable);
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400172 return this->testingOnly_createInstantiatedProxy(dimensions,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400173 format,
174 renderable,
175 renderTargetSampleCnt,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400176 fit,
177 budgeted,
178 isProtected);
179}
180
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400181sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createWrapped(sk_sp<GrTexture> tex) {
182 return this->createWrapped(std::move(tex), UseAllocator::kYes);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500183}
184#endif
185
Brian Salomonbeb7f522019-08-30 16:19:42 -0400186sk_sp<GrTextureProxy> GrProxyProvider::createWrapped(sk_sp<GrTexture> tex,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400187 UseAllocator useAllocator) {
Robert Phillipsadbe1322018-01-17 13:35:46 -0500188#ifdef SK_DEBUG
189 if (tex->getUniqueKey().isValid()) {
Greg Daniel3a365112020-02-14 10:47:18 -0500190 SkASSERT(!this->findProxyByUniqueKey(tex->getUniqueKey()));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500191 }
192#endif
193
194 if (tex->asRenderTarget()) {
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400195 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), useAllocator,
196 this->isDDLProvider()));
Greg Daniel3a365112020-02-14 10:47:18 -0500197 } else {
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400198 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), useAllocator,
199 this->isDDLProvider()));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500200 }
201}
202
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500203sk_sp<GrTextureProxy> GrProxyProvider::findOrCreateProxyByUniqueKey(const GrUniqueKey& key,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400204 UseAllocator useAllocator) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500205 ASSERT_SINGLE_OWNER
206
207 if (this->isAbandoned()) {
208 return nullptr;
209 }
210
Greg Daniel3a365112020-02-14 10:47:18 -0500211 sk_sp<GrTextureProxy> result = this->findProxyByUniqueKey(key);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500212 if (result) {
213 return result;
214 }
215
Robert Phillipsa41c6852019-02-07 10:44:10 -0500216 GrContext* direct = fImageContext->priv().asDirectContext();
217 if (!direct) {
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500218 return nullptr;
219 }
220
Robert Phillipsa41c6852019-02-07 10:44:10 -0500221 GrResourceCache* resourceCache = direct->priv().getResourceCache();
222
223 GrGpuResource* resource = resourceCache->findAndRefUniqueResource(key);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500224 if (!resource) {
225 return nullptr;
226 }
227
228 sk_sp<GrTexture> texture(static_cast<GrSurface*>(resource)->asTexture());
229 SkASSERT(texture);
230
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400231 result = this->createWrapped(std::move(texture), useAllocator);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500232 SkASSERT(result->getUniqueKey() == key);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500233 // createWrapped should've added this for us
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500234 SkASSERT(fUniquelyKeyedProxies.find(key));
235 return result;
236}
237
Brian Salomond005b692020-04-01 15:47:05 -0400238GrSurfaceProxyView GrProxyProvider::findCachedProxyWithColorTypeFallback(const GrUniqueKey& key,
239 GrSurfaceOrigin origin,
Brian Salomon0029db02020-04-03 10:41:24 -0400240 GrColorType ct,
241 int sampleCnt) {
Brian Salomond005b692020-04-01 15:47:05 -0400242 auto proxy = this->findOrCreateProxyByUniqueKey(key);
243 if (!proxy) {
244 return {};
245 }
246 // Assume that we used a fallback color type if and only if the proxy is renderable.
247 if (proxy->asRenderTargetProxy()) {
248 GrBackendFormat expectedFormat;
249 std::tie(ct, expectedFormat) =
Brian Salomon0029db02020-04-03 10:41:24 -0400250 GrRenderTargetContext::GetFallbackColorTypeAndFormat(fImageContext, ct, sampleCnt);
Brian Salomond005b692020-04-01 15:47:05 -0400251 SkASSERT(expectedFormat == proxy->backendFormat());
252 }
253 GrSwizzle swizzle = fImageContext->priv().caps()->getReadSwizzle(proxy->backendFormat(), ct);
254 return {std::move(proxy), origin, swizzle};
255}
256
Brian Osmande496652019-03-22 13:42:33 -0400257sk_sp<GrTextureProxy> GrProxyProvider::createProxyFromBitmap(const SkBitmap& bitmap,
Greg Daniel6f5441a2020-01-28 17:02:49 -0500258 GrMipMapped mipMapped,
Brian Salomonbc074a62020-03-18 10:06:13 -0400259 SkBackingFit fit,
260 SkBudgeted budgeted) {
Brian Osman412674f2019-02-07 15:34:58 -0500261 ASSERT_SINGLE_OWNER
Greg Daniel6f5441a2020-01-28 17:02:49 -0500262 SkASSERT(fit == SkBackingFit::kExact || mipMapped == GrMipMapped::kNo);
Brian Osman412674f2019-02-07 15:34:58 -0500263
264 if (this->isAbandoned()) {
265 return nullptr;
266 }
267
Brian Osman2b23c4b2018-06-01 12:25:08 -0400268 if (!SkImageInfoIsValid(bitmap.info())) {
Greg Daniela4ead652018-02-07 10:21:48 -0500269 return nullptr;
270 }
271
Brian Osmande496652019-03-22 13:42:33 -0400272 ATRACE_ANDROID_FRAMEWORK("Upload %sTexture [%ux%u]",
273 GrMipMapped::kYes == mipMapped ? "MipMap " : "",
274 bitmap.width(), bitmap.height());
Greg Daniela4ead652018-02-07 10:21:48 -0500275
276 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
277 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
278 // 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 -0500279 SkBitmap copyBitmap = bitmap;
280 if (!this->renderingDirectly() && !bitmap.isImmutable()) {
281 copyBitmap.allocPixels();
282 if (!bitmap.readPixels(copyBitmap.pixmap())) {
283 return nullptr;
284 }
285 copyBitmap.setImmutable();
Greg Daniela4ead652018-02-07 10:21:48 -0500286 }
287
Greg Daniel6f5441a2020-01-28 17:02:49 -0500288 sk_sp<GrTextureProxy> proxy;
289 if (mipMapped == GrMipMapped::kNo ||
290 0 == SkMipMap::ComputeLevelCount(copyBitmap.width(), copyBitmap.height())) {
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400291 proxy = this->createNonMippedProxyFromBitmap(copyBitmap, fit, budgeted);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500292 } else {
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400293 proxy = this->createMippedProxyFromBitmap(copyBitmap, budgeted);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500294 }
295
296 if (!proxy) {
297 return nullptr;
298 }
299
300 GrContext* direct = fImageContext->priv().asDirectContext();
301 if (direct) {
302 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
303
304 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
305 // we're better off instantiating the proxy immediately here.
306 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
307 return nullptr;
308 }
309 }
310 return proxy;
311}
312
313sk_sp<GrTextureProxy> GrProxyProvider::createNonMippedProxyFromBitmap(const SkBitmap& bitmap,
314 SkBackingFit fit,
Brian Salomonbc074a62020-03-18 10:06:13 -0400315 SkBudgeted budgeted) {
Brian Salomona56a7462020-02-07 14:17:25 -0500316 auto dims = bitmap.dimensions();
Greg Daniel6f5441a2020-01-28 17:02:49 -0500317
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400318 auto colorType = SkColorTypeToGrColorType(bitmap.colorType());
319 GrBackendFormat format = this->caps()->getDefaultBackendFormat(colorType, GrRenderable::kNo);
320 if (!format.isValid()) {
321 return nullptr;
322 }
323
Greg Daniel6f5441a2020-01-28 17:02:49 -0500324 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomon63410e92020-03-23 18:32:50 -0400325 [bitmap](GrResourceProvider* resourceProvider, const LazySurfaceDesc& desc) {
326 SkASSERT(desc.fMipMapped == GrMipMapped::kNo);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500327 GrMipLevel mipLevel = { bitmap.getPixels(), bitmap.rowBytes() };
Brian Salomon63410e92020-03-23 18:32:50 -0400328 auto colorType = SkColorTypeToGrColorType(bitmap.colorType());
Greg Daniel6f5441a2020-01-28 17:02:49 -0500329 return LazyCallbackResult(resourceProvider->createTexture(
Brian Salomon63410e92020-03-23 18:32:50 -0400330 desc.fDimensions, desc.fFormat, colorType, desc.fRenderable,
331 desc.fSampleCnt, desc.fBudgeted, desc.fFit, desc.fProtected, mipLevel));
Greg Daniel6f5441a2020-01-28 17:02:49 -0500332 },
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400333 format, dims, GrRenderable::kNo, 1, GrMipMapped::kNo, GrMipMapsStatus::kNotAllocated,
334 GrInternalSurfaceFlags::kNone, fit, budgeted, GrProtected::kNo, UseAllocator::kYes);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500335
336 if (!proxy) {
337 return nullptr;
338 }
Brian Salomona56a7462020-02-07 14:17:25 -0500339 SkASSERT(proxy->dimensions() == bitmap.dimensions());
Greg Daniel6f5441a2020-01-28 17:02:49 -0500340 return proxy;
341}
342
343sk_sp<GrTextureProxy> GrProxyProvider::createMippedProxyFromBitmap(const SkBitmap& bitmap,
Brian Salomonbc074a62020-03-18 10:06:13 -0400344 SkBudgeted budgeted) {
Greg Daniel6f5441a2020-01-28 17:02:49 -0500345 SkASSERT(this->caps()->mipMapSupport());
346
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400347 auto colorType = SkColorTypeToGrColorType(bitmap.colorType());
348 GrBackendFormat format = this->caps()->getDefaultBackendFormat(colorType, GrRenderable::kNo);
349 if (!format.isValid()) {
350 return nullptr;
351 }
Greg Daniel6f5441a2020-01-28 17:02:49 -0500352
353 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(bitmap.pixmap(), nullptr));
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400354 if (!mipmaps) {
355 return nullptr;
356 }
357
Brian Salomona56a7462020-02-07 14:17:25 -0500358 auto dims = bitmap.dimensions();
Greg Danielce3ddaa2020-01-22 16:58:15 -0500359
Greg Daniela4ead652018-02-07 10:21:48 -0500360 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomon63410e92020-03-23 18:32:50 -0400361 [bitmap, mipmaps](GrResourceProvider* resourceProvider, const LazySurfaceDesc& desc) {
Brian Osman1b97f132018-09-13 17:33:48 +0000362 const int mipLevelCount = mipmaps->countLevels() + 1;
363 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
Brian Salomon63410e92020-03-23 18:32:50 -0400364 auto colorType = SkColorTypeToGrColorType(bitmap.colorType());
Brian Osman1b97f132018-09-13 17:33:48 +0000365
Greg Daniel6f5441a2020-01-28 17:02:49 -0500366 texels[0].fPixels = bitmap.getPixels();
367 texels[0].fRowBytes = bitmap.rowBytes();
Greg Daniela4ead652018-02-07 10:21:48 -0500368
Greg Daniela4ead652018-02-07 10:21:48 -0500369 for (int i = 1; i < mipLevelCount; ++i) {
370 SkMipMap::Level generatedMipLevel;
371 mipmaps->getLevel(i - 1, &generatedMipLevel);
372 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
373 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
374 SkASSERT(texels[i].fPixels);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500375 SkASSERT(generatedMipLevel.fPixmap.colorType() == bitmap.colorType());
Greg Daniela4ead652018-02-07 10:21:48 -0500376 }
Brian Salomonbeb7f522019-08-30 16:19:42 -0400377 return LazyCallbackResult(resourceProvider->createTexture(
Brian Salomon63410e92020-03-23 18:32:50 -0400378 desc.fDimensions, desc.fFormat, colorType, GrRenderable::kNo, 1,
379 desc.fBudgeted, GrProtected::kNo, texels.get(), mipLevelCount));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500380 },
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400381 format, dims, GrRenderable::kNo, 1, GrMipMapped::kYes, GrMipMapsStatus::kValid,
382 GrInternalSurfaceFlags::kNone, SkBackingFit::kExact, budgeted, GrProtected::kNo,
383 UseAllocator::kYes);
Greg Daniela4ead652018-02-07 10:21:48 -0500384
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400385 if (!proxy) {
386 return nullptr;
387 }
388
Brian Salomona56a7462020-02-07 14:17:25 -0500389 SkASSERT(proxy->dimensions() == bitmap.dimensions());
Greg Daniel6f5441a2020-01-28 17:02:49 -0500390
Greg Daniela4ead652018-02-07 10:21:48 -0500391 return proxy;
392}
393
Greg Daniel4065d452018-11-16 15:43:41 -0500394sk_sp<GrTextureProxy> GrProxyProvider::createProxy(const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -0500395 SkISize dimensions,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400396 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400397 int renderTargetSampleCnt,
Greg Danielf6f7b672018-02-15 13:06:26 -0500398 GrMipMapped mipMapped,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500399 SkBackingFit fit,
400 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -0400401 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400402 GrInternalSurfaceFlags surfaceFlags,
403 GrSurfaceProxy::UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500404 ASSERT_SINGLE_OWNER
405 if (this->isAbandoned()) {
406 return nullptr;
407 }
408
Robert Phillips8ff8bcc2019-07-29 17:03:35 -0400409 const GrCaps* caps = this->caps();
410
411 if (caps->isFormatCompressed(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400412 // Deferred proxies for compressed textures are not supported.
413 return nullptr;
414 }
Robert Phillips0902c982019-07-16 07:47:56 -0400415
Greg Danielf6f7b672018-02-15 13:06:26 -0500416 if (GrMipMapped::kYes == mipMapped) {
417 // SkMipMap doesn't include the base level in the level count so we have to add 1
Brian Salomona56a7462020-02-07 14:17:25 -0500418 int mipCount = SkMipMap::ComputeLevelCount(dimensions.fWidth, dimensions.fHeight) + 1;
Greg Danielf6f7b672018-02-15 13:06:26 -0500419 if (1 == mipCount) {
420 mipMapped = GrMipMapped::kNo;
421 }
422 }
423
Brian Salomona56a7462020-02-07 14:17:25 -0500424 if (!caps->validateSurfaceParams(dimensions, format, renderable, renderTargetSampleCnt,
425 mipMapped)) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500426 return nullptr;
427 }
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600428 GrMipMapsStatus mipMapsStatus = (GrMipMapped::kYes == mipMapped)
429 ? GrMipMapsStatus::kDirty
430 : GrMipMapsStatus::kNotAllocated;
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400431 if (renderable == GrRenderable::kYes) {
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400432 renderTargetSampleCnt = caps->getRenderTargetSampleCount(renderTargetSampleCnt, format);
Greg Daniel6fa62e22019-08-07 15:52:37 -0400433 SkASSERT(renderTargetSampleCnt);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500434 // We know anything we instantiate later from this deferred path will be
435 // both texturable and renderable
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400436 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400437 *caps, format, dimensions, renderTargetSampleCnt, mipMapped, mipMapsStatus, fit,
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400438 budgeted, isProtected, surfaceFlags, useAllocator, this->isDDLProvider()));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500439 }
440
Greg Daniel3a365112020-02-14 10:47:18 -0500441 return sk_sp<GrTextureProxy>(new GrTextureProxy(format, dimensions, mipMapped, mipMapsStatus,
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400442 fit, budgeted, isProtected, surfaceFlags,
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400443 useAllocator, this->isDDLProvider()));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500444}
445
Brian Salomonbb8dde82019-06-27 10:52:13 -0400446sk_sp<GrTextureProxy> GrProxyProvider::createCompressedTextureProxy(
Robert Phillips3a833922020-01-21 15:25:58 -0500447 SkISize dimensions, SkBudgeted budgeted, GrMipMapped mipMapped, GrProtected isProtected,
Robert Phillipse4720c62020-01-14 14:33:24 -0500448 SkImage::CompressionType compressionType, sk_sp<SkData> data) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500449 ASSERT_SINGLE_OWNER
450 if (this->isAbandoned()) {
451 return nullptr;
452 }
Brian Salomonbb8dde82019-06-27 10:52:13 -0400453
Robert Phillips8ff8bcc2019-07-29 17:03:35 -0400454 GrBackendFormat format = this->caps()->getBackendFormatFromCompressionType(compressionType);
455
Greg Daniel7bfc9132019-08-14 14:23:53 -0400456 if (!this->caps()->isFormatTexturable(format)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500457 return nullptr;
458 }
459
Robert Phillipse4720c62020-01-14 14:33:24 -0500460 GrMipMapsStatus mipMapsStatus = (GrMipMapped::kYes == mipMapped)
461 ? GrMipMapsStatus::kValid
462 : GrMipMapsStatus::kNotAllocated;
463
Jim Van Verthee06b332019-01-18 10:36:32 -0500464 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomon63410e92020-03-23 18:32:50 -0400465 [data](GrResourceProvider* resourceProvider, const LazySurfaceDesc& desc) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400466 return LazyCallbackResult(resourceProvider->createCompressedTexture(
Brian Salomon63410e92020-03-23 18:32:50 -0400467 desc.fDimensions, desc.fFormat, desc.fBudgeted, desc.fMipMapped,
468 desc.fProtected, data.get()));
Brian Salomonbb8dde82019-06-27 10:52:13 -0400469 },
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400470 format, dimensions, GrRenderable::kNo, 1, mipMapped, mipMapsStatus,
Greg Daniel3a365112020-02-14 10:47:18 -0500471 GrInternalSurfaceFlags::kReadOnly, SkBackingFit::kExact, SkBudgeted::kYes,
472 GrProtected::kNo, UseAllocator::kYes);
Jim Van Verthee06b332019-01-18 10:36:32 -0500473
474 if (!proxy) {
475 return nullptr;
476 }
477
Robert Phillipsa41c6852019-02-07 10:44:10 -0500478 GrContext* direct = fImageContext->priv().asDirectContext();
479 if (direct) {
480 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Jim Van Verthee06b332019-01-18 10:36:32 -0500481 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
482 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500483 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500484 return nullptr;
485 }
486 }
487 return proxy;
488}
489
Brian Salomon7578f3e2018-03-07 14:39:54 -0500490sk_sp<GrTextureProxy> GrProxyProvider::wrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500491 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500492 GrWrapCacheable cacheable,
Brian Salomonc67c31c2018-12-06 10:00:03 -0500493 GrIOType ioType,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500494 ReleaseProc releaseProc,
495 ReleaseContext releaseCtx) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500496 SkASSERT(ioType != kWrite_GrIOType);
Robert Phillipsf9bec202018-01-16 09:21:01 -0500497 if (this->isAbandoned()) {
498 return nullptr;
499 }
500
Brian Salomonf7778972018-03-08 10:13:17 -0500501 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500502 GrContext* direct = fImageContext->priv().asDirectContext();
503 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500504 return nullptr;
505 }
506
Robert Phillipsa41c6852019-02-07 10:44:10 -0500507 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
508
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500509 sk_sp<GrTexture> tex =
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400510 resourceProvider->wrapBackendTexture(backendTex, ownership, cacheable, ioType);
Brian Salomonf7778972018-03-08 10:13:17 -0500511 if (!tex) {
512 return nullptr;
513 }
Robert Phillipsadbe1322018-01-17 13:35:46 -0500514
Greg Daniel6a0176b2018-01-30 09:28:44 -0500515 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500516 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel6a0176b2018-01-30 09:28:44 -0500517 }
518
Brian Salomonf7778972018-03-08 10:13:17 -0500519 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
520 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500521 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500522
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400523 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), UseAllocator::kNo,
524 this->isDDLProvider()));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500525}
526
Robert Phillipsead321b2019-12-19 10:16:32 -0500527sk_sp<GrTextureProxy> GrProxyProvider::wrapCompressedBackendTexture(const GrBackendTexture& beTex,
Robert Phillipsead321b2019-12-19 10:16:32 -0500528 GrWrapOwnership ownership,
529 GrWrapCacheable cacheable,
530 ReleaseProc releaseProc,
531 ReleaseContext releaseCtx) {
532 if (this->isAbandoned()) {
533 return nullptr;
534 }
535
536 // This is only supported on a direct GrContext.
537 GrContext* direct = fImageContext->priv().asDirectContext();
538 if (!direct) {
539 return nullptr;
540 }
541
542 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
543
544 sk_sp<GrTexture> tex = resourceProvider->wrapCompressedBackendTexture(beTex, ownership,
545 cacheable);
546 if (!tex) {
547 return nullptr;
548 }
549
550 if (releaseProc) {
551 tex->setRelease(releaseProc, releaseCtx);
552 }
553
554 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
555 // Make sure we match how we created the proxy with SkBudgeted::kNo
556 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
557
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400558 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), UseAllocator::kNo,
559 this->isDDLProvider()));
Robert Phillipsead321b2019-12-19 10:16:32 -0500560}
561
Brian Salomon7578f3e2018-03-07 14:39:54 -0500562sk_sp<GrTextureProxy> GrProxyProvider::wrapRenderableBackendTexture(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400563 const GrBackendTexture& backendTex,
564 int sampleCnt,
565 GrWrapOwnership ownership,
566 GrWrapCacheable cacheable,
567 ReleaseProc releaseProc,
Greg Daniel3a365112020-02-14 10:47:18 -0500568 ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500569 if (this->isAbandoned()) {
570 return nullptr;
571 }
572
Brian Salomonf7778972018-03-08 10:13:17 -0500573 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500574 GrContext* direct = fImageContext->priv().asDirectContext();
575 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500576 return nullptr;
577 }
578
Robert Phillips0902c982019-07-16 07:47:56 -0400579 const GrCaps* caps = this->caps();
580
Robert Phillipsa41c6852019-02-07 10:44:10 -0500581 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
582
Greg Daniel6fa62e22019-08-07 15:52:37 -0400583 sampleCnt = caps->getRenderTargetSampleCount(sampleCnt, backendTex.getBackendFormat());
584 SkASSERT(sampleCnt);
585
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400586 sk_sp<GrTexture> tex = resourceProvider->wrapRenderableBackendTexture(
587 backendTex, sampleCnt, ownership, cacheable);
Brian Salomonf7778972018-03-08 10:13:17 -0500588 if (!tex) {
589 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500590 }
591
Greg Daniel8ce79912019-02-05 10:08:43 -0500592 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500593 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500594 }
595
Brian Salomonf7778972018-03-08 10:13:17 -0500596 SkASSERT(tex->asRenderTarget()); // A GrTextureRenderTarget
597 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500598 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Greg Daniel6abda432018-02-15 14:55:00 -0500599
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400600 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), UseAllocator::kNo,
601 this->isDDLProvider()));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500602}
603
Brian Salomon7578f3e2018-03-07 14:39:54 -0500604sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400605 const GrBackendRenderTarget& backendRT,
606 ReleaseProc releaseProc,
Greg Daniel3a365112020-02-14 10:47:18 -0500607 ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500608 if (this->isAbandoned()) {
609 return nullptr;
610 }
611
Brian Salomonf7778972018-03-08 10:13:17 -0500612 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500613 GrContext* direct = fImageContext->priv().asDirectContext();
614 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500615 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500616 }
617
Robert Phillipsa41c6852019-02-07 10:44:10 -0500618 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
619
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400620 sk_sp<GrRenderTarget> rt = resourceProvider->wrapBackendRenderTarget(backendRT);
Brian Salomonf7778972018-03-08 10:13:17 -0500621 if (!rt) {
622 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500623 }
Greg Daniel8ce79912019-02-05 10:08:43 -0500624
Greg Daniel8ce79912019-02-05 10:08:43 -0500625 if (releaseProc) {
Brian Salomon2ca31f82019-03-05 13:28:58 -0500626 rt->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500627 }
628
Brian Salomonf7778972018-03-08 10:13:17 -0500629 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
630 SkASSERT(!rt->getUniqueKey().isValid());
631 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500632 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Brian Salomonf7778972018-03-08 10:13:17 -0500633
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400634 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(std::move(rt), UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500635}
636
Brian Salomon7578f3e2018-03-07 14:39:54 -0500637sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendTextureAsRenderTarget(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400638 const GrBackendTexture& backendTex, int sampleCnt) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500639 if (this->isAbandoned()) {
640 return nullptr;
641 }
642
Brian Salomonf7778972018-03-08 10:13:17 -0500643 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500644 GrContext* direct = fImageContext->priv().asDirectContext();
645 if (!direct) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500646 return nullptr;
647 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500648
Robert Phillipsa41c6852019-02-07 10:44:10 -0500649 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
650
Brian Salomonf7778972018-03-08 10:13:17 -0500651 sk_sp<GrRenderTarget> rt =
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400652 resourceProvider->wrapBackendTextureAsRenderTarget(backendTex, sampleCnt);
Brian Salomonf7778972018-03-08 10:13:17 -0500653 if (!rt) {
654 return nullptr;
Greg Danielf87651e2018-02-21 11:36:53 -0500655 }
Brian Salomonf7778972018-03-08 10:13:17 -0500656 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
657 SkASSERT(!rt->getUniqueKey().isValid());
Greg Danielb46add82019-01-02 14:51:29 -0500658 // 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 Danielf87651e2018-02-21 11:36:53 -0500660
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400661 return sk_sp<GrSurfaceProxy>(new GrRenderTargetProxy(std::move(rt), UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500662}
663
Greg Danielb46add82019-01-02 14:51:29 -0500664sk_sp<GrRenderTargetProxy> GrProxyProvider::wrapVulkanSecondaryCBAsRenderTarget(
665 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
666 if (this->isAbandoned()) {
667 return nullptr;
668 }
669
670 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500671 GrContext* direct = fImageContext->priv().asDirectContext();
672 if (!direct) {
Greg Danielb46add82019-01-02 14:51:29 -0500673 return nullptr;
674 }
675
Robert Phillipsa41c6852019-02-07 10:44:10 -0500676 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Danielb46add82019-01-02 14:51:29 -0500677
Robert Phillipsa41c6852019-02-07 10:44:10 -0500678 sk_sp<GrRenderTarget> rt = resourceProvider->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
679 vkInfo);
Greg Danielb46add82019-01-02 14:51:29 -0500680 if (!rt) {
681 return nullptr;
682 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500683
Greg Danielb46add82019-01-02 14:51:29 -0500684 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
685 SkASSERT(!rt->getUniqueKey().isValid());
686 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500687 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielb46add82019-01-02 14:51:29 -0500688
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400689 GrColorType colorType = SkColorTypeToGrColorType(imageInfo.colorType());
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400690
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400691 if (!this->caps()->isFormatAsColorTypeRenderable(colorType, rt->backendFormat(),
692 rt->numSamples())) {
693 return nullptr;
694 }
695
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400696 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
697 std::move(rt), UseAllocator::kNo, GrRenderTargetProxy::WrapsVkSecondaryCB::kYes));
Brian Salomone8a766b2019-07-19 14:24:36 -0400698}
699
700sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
701 const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -0500702 SkISize dimensions,
Brian Salomone8a766b2019-07-19 14:24:36 -0400703 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400704 int renderTargetSampleCnt,
Brian Salomone8a766b2019-07-19 14:24:36 -0400705 GrMipMapped mipMapped,
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600706 GrMipMapsStatus mipMapsStatus,
Brian Salomone8a766b2019-07-19 14:24:36 -0400707 GrInternalSurfaceFlags surfaceFlags,
708 SkBackingFit fit,
709 SkBudgeted budgeted,
710 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400711 GrSurfaceProxy::UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500712 ASSERT_SINGLE_OWNER
713 if (this->isAbandoned()) {
714 return nullptr;
715 }
Brian Salomona56a7462020-02-07 14:17:25 -0500716 SkASSERT((dimensions.fWidth <= 0 && dimensions.fHeight <= 0) ||
717 (dimensions.fWidth > 0 && dimensions.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400718
Robert Phillips0a15cc62019-07-30 12:49:10 -0400719 if (!format.isValid()) {
720 return nullptr;
721 }
722
Brian Salomona56a7462020-02-07 14:17:25 -0500723 if (dimensions.fWidth > this->caps()->maxTextureSize() ||
724 dimensions.fHeight > this->caps()->maxTextureSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400725 return nullptr;
726 }
727
Brian Salomonbeb7f522019-08-30 16:19:42 -0400728 if (renderable == GrRenderable::kYes) {
729 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(*this->caps(),
730 std::move(callback),
731 format,
Brian Salomona56a7462020-02-07 14:17:25 -0500732 dimensions,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400733 renderTargetSampleCnt,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400734 mipMapped,
735 mipMapsStatus,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400736 fit,
737 budgeted,
738 isProtected,
739 surfaceFlags,
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400740 useAllocator,
741 this->isDDLProvider()));
Brian Salomonbeb7f522019-08-30 16:19:42 -0400742 } else {
743 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(callback),
744 format,
Brian Salomona56a7462020-02-07 14:17:25 -0500745 dimensions,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400746 mipMapped,
747 mipMapsStatus,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400748 fit,
749 budgeted,
750 isProtected,
751 surfaceFlags,
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400752 useAllocator,
753 this->isDDLProvider()));
Brian Salomonbeb7f522019-08-30 16:19:42 -0400754 }
Robert Phillips777707b2018-01-17 11:40:14 -0500755}
756
Robert Phillipse8fabb22018-02-04 14:33:21 -0500757sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy(
Brian Salomonbeb7f522019-08-30 16:19:42 -0400758 LazyInstantiateCallback&& callback,
759 const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -0500760 SkISize dimensions,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400761 int sampleCnt,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400762 GrInternalSurfaceFlags surfaceFlags,
763 const TextureInfo* textureInfo,
764 GrMipMapsStatus mipMapsStatus,
765 SkBackingFit fit,
766 SkBudgeted budgeted,
767 GrProtected isProtected,
768 bool wrapsVkSecondaryCB,
769 UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500770 ASSERT_SINGLE_OWNER
771 if (this->isAbandoned()) {
772 return nullptr;
773 }
Brian Salomona56a7462020-02-07 14:17:25 -0500774 SkASSERT((dimensions.fWidth <= 0 && dimensions.fHeight <= 0) ||
775 (dimensions.fWidth > 0 && dimensions.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400776
Brian Salomona56a7462020-02-07 14:17:25 -0500777 if (dimensions.fWidth > this->caps()->maxRenderTargetSize() ||
778 dimensions.fHeight > this->caps()->maxRenderTargetSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400779 return nullptr;
780 }
781
Brian Salomon7226c232018-07-30 13:13:17 -0400782 if (textureInfo) {
Greg Danielb085fa92019-03-05 16:55:12 -0500783 // Wrapped vulkan secondary command buffers don't support texturing since we won't have an
784 // actual VkImage to texture from.
785 SkASSERT(!wrapsVkSecondaryCB);
Brian Salomon7226c232018-07-30 13:13:17 -0400786 return sk_sp<GrRenderTargetProxy>(new GrTextureRenderTargetProxy(
Greg Daniel3a365112020-02-14 10:47:18 -0500787 *this->caps(), std::move(callback), format, dimensions, sampleCnt,
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400788 textureInfo->fMipMapped, mipMapsStatus, fit, budgeted, isProtected, surfaceFlags,
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400789 useAllocator, this->isDDLProvider()));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500790 }
791
Greg Danielb085fa92019-03-05 16:55:12 -0500792 GrRenderTargetProxy::WrapsVkSecondaryCB vkSCB =
793 wrapsVkSecondaryCB ? GrRenderTargetProxy::WrapsVkSecondaryCB::kYes
794 : GrRenderTargetProxy::WrapsVkSecondaryCB::kNo;
795
Greg Daniel3a365112020-02-14 10:47:18 -0500796 return sk_sp<GrRenderTargetProxy>(
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400797 new GrRenderTargetProxy(std::move(callback), format, dimensions, sampleCnt, fit,
798 budgeted, isProtected, surfaceFlags, useAllocator, vkSCB));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500799}
800
Brian Salomonbeb7f522019-08-30 16:19:42 -0400801sk_sp<GrTextureProxy> GrProxyProvider::MakeFullyLazyProxy(LazyInstantiateCallback&& callback,
802 const GrBackendFormat& format,
803 GrRenderable renderable,
804 int renderTargetSampleCnt,
805 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400806 const GrCaps& caps,
807 UseAllocator useAllocator) {
Robert Phillips0a15cc62019-07-30 12:49:10 -0400808 if (!format.isValid()) {
809 return nullptr;
810 }
811
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400812 SkASSERT(renderTargetSampleCnt == 1 || renderable == GrRenderable::kYes);
Robert Phillips10d17212019-04-24 14:09:10 -0400813 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNone;
Robert Phillips777707b2018-01-17 11:40:14 -0500814
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400815 // MakeFullyLazyProxy is only called at flush time so we know these texture proxies are
816 // not being created by a DDL provider.
Brian Salomona56a7462020-02-07 14:17:25 -0500817 static constexpr SkISize kLazyDims = {-1, -1};
Brian Salomonbeb7f522019-08-30 16:19:42 -0400818 if (GrRenderable::kYes == renderable) {
819 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
Greg Daniel3a365112020-02-14 10:47:18 -0500820 caps, std::move(callback), format, kLazyDims, renderTargetSampleCnt,
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400821 GrMipMapped::kNo, GrMipMapsStatus::kNotAllocated, SkBackingFit::kApprox,
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400822 SkBudgeted::kYes, isProtected, surfaceFlags, useAllocator, GrDDLProvider::kNo));
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400823 } else {
824 return sk_sp<GrTextureProxy>(
825 new GrTextureProxy(std::move(callback), format, kLazyDims, GrMipMapped::kNo,
826 GrMipMapsStatus::kNotAllocated, SkBackingFit::kApprox,
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400827 SkBudgeted::kYes, isProtected, surfaceFlags, useAllocator,
828 GrDDLProvider::kNo));
Brian Salomonbeb7f522019-08-30 16:19:42 -0400829 }
Robert Phillips777707b2018-01-17 11:40:14 -0500830}
831
Robert Phillips427966a2018-12-20 17:20:43 -0500832void GrProxyProvider::processInvalidUniqueKey(const GrUniqueKey& key, GrTextureProxy* proxy,
833 InvalidateGPUResource invalidateGPUResource) {
Mike Klein04ef8102020-03-16 12:44:23 -0500834 this->processInvalidUniqueKeyImpl(key, proxy, invalidateGPUResource, RemoveTableEntry::kYes);
835}
836
837void GrProxyProvider::processInvalidUniqueKeyImpl(const GrUniqueKey& key, GrTextureProxy* proxy,
838 InvalidateGPUResource invalidateGPUResource,
839 RemoveTableEntry removeTableEntry) {
Chris Dalton2de13dd2019-01-03 15:11:59 -0700840 SkASSERT(key.isValid());
841
Robert Phillips427966a2018-12-20 17:20:43 -0500842 if (!proxy) {
843 proxy = fUniquelyKeyedProxies.find(key);
844 }
Chris Dalton2de13dd2019-01-03 15:11:59 -0700845 SkASSERT(!proxy || proxy->getUniqueKey() == key);
846
847 // Locate the corresponding GrGpuResource (if it needs to be invalidated) before clearing the
848 // proxy's unique key. We must do it in this order because 'key' may alias the proxy's key.
849 sk_sp<GrGpuResource> invalidGpuResource;
850 if (InvalidateGPUResource::kYes == invalidateGPUResource) {
Brian Salomon01ceae92019-04-02 11:49:54 -0400851 GrContext* direct = fImageContext->priv().asDirectContext();
852 if (direct) {
853 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
854 invalidGpuResource = resourceProvider->findByUniqueKey<GrGpuResource>(key);
Chris Dalton2de13dd2019-01-03 15:11:59 -0700855 }
856 SkASSERT(!invalidGpuResource || invalidGpuResource->getUniqueKey() == key);
857 }
Robert Phillips427966a2018-12-20 17:20:43 -0500858
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500859 // Note: this method is called for the whole variety of GrGpuResources so often 'key'
860 // will not be in 'fUniquelyKeyedProxies'.
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500861 if (proxy) {
Mike Klein04ef8102020-03-16 12:44:23 -0500862 if (removeTableEntry == RemoveTableEntry::kYes) {
863 fUniquelyKeyedProxies.remove(key);
864 }
Robert Phillips427966a2018-12-20 17:20:43 -0500865 proxy->cacheAccess().clearUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500866 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500867
Chris Dalton2de13dd2019-01-03 15:11:59 -0700868 if (invalidGpuResource) {
869 invalidGpuResource->resourcePriv().removeUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500870 }
871}
872
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400873GrDDLProvider GrProxyProvider::isDDLProvider() const {
874 return fImageContext->priv().asDirectContext() ? GrDDLProvider::kNo : GrDDLProvider::kYes;
875}
876
Robert Phillipsa41c6852019-02-07 10:44:10 -0500877uint32_t GrProxyProvider::contextID() const {
878 return fImageContext->priv().contextID();
879}
880
881const GrCaps* GrProxyProvider::caps() const {
882 return fImageContext->priv().caps();
883}
884
885sk_sp<const GrCaps> GrProxyProvider::refCaps() const {
886 return fImageContext->priv().refCaps();
887}
888
Robert Phillipsa9162df2019-02-11 14:12:03 -0500889bool GrProxyProvider::isAbandoned() const {
890 return fImageContext->priv().abandoned();
891}
892
Robert Phillips0790f8a2018-09-18 13:11:03 -0400893void GrProxyProvider::orphanAllUniqueKeys() {
Mike Kleincff63962020-03-14 16:22:45 -0500894 fUniquelyKeyedProxies.foreach([&](GrTextureProxy* proxy){
895 proxy->fProxyProvider = nullptr;
896 });
Robert Phillips0790f8a2018-09-18 13:11:03 -0400897}
898
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500899void GrProxyProvider::removeAllUniqueKeys() {
Mike Klein04ef8102020-03-16 12:44:23 -0500900 fUniquelyKeyedProxies.foreach([&](GrTextureProxy* proxy){
901 // It's not safe to remove table entries while iterating with foreach(),
902 // but since we're going to remove them all anyway, simply save that for the end.
903 this->processInvalidUniqueKeyImpl(proxy->getUniqueKey(), proxy,
904 InvalidateGPUResource::kNo,
905 RemoveTableEntry::kNo);
906 });
907 // Removing all those table entries is safe now.
908 fUniquelyKeyedProxies.reset();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500909}
Robert Phillipsa41c6852019-02-07 10:44:10 -0500910
911bool GrProxyProvider::renderingDirectly() const {
912 return fImageContext->priv().asDirectContext();
913}