blob: 2bb1708e17f4e88cde70a9828ed5ecada4551bec [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"
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040012#include "include/gpu/GrDirectContext.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"
Mike Reed13711eb2020-07-14 17:16:32 -040020#include "src/core/SkMipmap.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#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
Adlai Holler33dbd652020-06-01 12:35:42 -040036#define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(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 Phillipsf10b2a52020-05-15 10:20:04 -040056 // Only the proxyProvider that created a proxy should be assigning unique keys to it.
57 SkASSERT(this->isDDLProvider() == proxy->creatingProvider());
58
Robert Phillipsa41c6852019-02-07 10:44:10 -050059#ifdef SK_DEBUG
60 {
Robert Phillipsf8f45d92020-07-01 11:11:18 -040061 auto direct = fImageContext->asDirectContext();
Robert Phillipsa41c6852019-02-07 10:44:10 -050062 if (direct) {
63 GrResourceCache* resourceCache = direct->priv().getResourceCache();
64 // If there is already a GrResource with this key then the caller has violated the
65 // normal usage pattern of uniquely keyed resources (e.g., they have created one w/o
66 // first seeing if it already existed in the cache).
67 SkASSERT(!resourceCache->findAndRefUniqueResource(key));
68 }
69 }
70#endif
Robert Phillips1afd4cd2018-01-08 13:40:32 -050071
Robert Phillips1afd4cd2018-01-08 13:40:32 -050072 SkASSERT(!fUniquelyKeyedProxies.find(key)); // multiple proxies can't get the same key
73
74 proxy->cacheAccess().setUniqueKey(this, key);
75 SkASSERT(proxy->getUniqueKey() == key);
76 fUniquelyKeyedProxies.add(proxy);
Robert Phillipsadbe1322018-01-17 13:35:46 -050077 return true;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050078}
79
80void GrProxyProvider::adoptUniqueKeyFromSurface(GrTextureProxy* proxy, const GrSurface* surf) {
81 SkASSERT(surf->getUniqueKey().isValid());
82 proxy->cacheAccess().setUniqueKey(this, surf->getUniqueKey());
83 SkASSERT(proxy->getUniqueKey() == surf->getUniqueKey());
84 // multiple proxies can't get the same key
85 SkASSERT(!fUniquelyKeyedProxies.find(surf->getUniqueKey()));
86 fUniquelyKeyedProxies.add(proxy);
87}
88
Chris Dalton2de13dd2019-01-03 15:11:59 -070089void GrProxyProvider::removeUniqueKeyFromProxy(GrTextureProxy* proxy) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050090 ASSERT_SINGLE_OWNER
Chris Dalton2de13dd2019-01-03 15:11:59 -070091 SkASSERT(proxy);
92 SkASSERT(proxy->getUniqueKey().isValid());
93
94 if (this->isAbandoned()) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050095 return;
96 }
Robert Phillips0790f8a2018-09-18 13:11:03 -040097
Chris Dalton2de13dd2019-01-03 15:11:59 -070098 this->processInvalidUniqueKey(proxy->getUniqueKey(), proxy, InvalidateGPUResource::kYes);
Robert Phillips1afd4cd2018-01-08 13:40:32 -050099}
100
Greg Daniel3a365112020-02-14 10:47:18 -0500101sk_sp<GrTextureProxy> GrProxyProvider::findProxyByUniqueKey(const GrUniqueKey& key) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500102 ASSERT_SINGLE_OWNER
103
104 if (this->isAbandoned()) {
105 return nullptr;
106 }
107
Brian Salomon01ceae92019-04-02 11:49:54 -0400108 GrTextureProxy* proxy = fUniquelyKeyedProxies.find(key);
Brian Salomon01ceae92019-04-02 11:49:54 -0400109 if (proxy) {
Robert Phillipse5f73282019-06-18 17:15:04 -0400110 return sk_ref_sp(proxy);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500111 }
Robert Phillipse5f73282019-06-18 17:15:04 -0400112 return nullptr;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500113}
114
Robert Phillipsa41c6852019-02-07 10:44:10 -0500115///////////////////////////////////////////////////////////////////////////////
116
117#if GR_TEST_UTILS
118sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createInstantiatedProxy(
Brian Salomona56a7462020-02-07 14:17:25 -0500119 SkISize dimensions,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400120 const GrBackendFormat& format,
121 GrRenderable renderable,
122 int renderTargetSampleCnt,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400123 SkBackingFit fit,
124 SkBudgeted budgeted,
125 GrProtected isProtected) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500126 ASSERT_SINGLE_OWNER
127 if (this->isAbandoned()) {
128 return nullptr;
129 }
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400130 auto direct = fImageContext->asDirectContext();
Robert Phillipsa41c6852019-02-07 10:44:10 -0500131 if (!direct) {
132 return nullptr;
133 }
134
Brian Salomon4eb38b72019-08-05 12:58:39 -0400135 if (this->caps()->isFormatCompressed(format)) {
136 // TODO: Allow this to go to GrResourceProvider::createCompressedTexture() once we no longer
Greg Daniel4cb29332020-01-23 10:07:02 -0500137 // rely on GrColorType to get a swizzle for the proxy.
Brian Salomon4eb38b72019-08-05 12:58:39 -0400138 return nullptr;
139 }
Brian Salomon4eb38b72019-08-05 12:58:39 -0400140
Robert Phillipsa41c6852019-02-07 10:44:10 -0500141 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
142 sk_sp<GrTexture> tex;
143
144 if (SkBackingFit::kApprox == fit) {
Brian Salomona56a7462020-02-07 14:17:25 -0500145 tex = resourceProvider->createApproxTexture(dimensions, format, renderable,
146 renderTargetSampleCnt, isProtected);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500147 } else {
Brian Salomona56a7462020-02-07 14:17:25 -0500148 tex = resourceProvider->createTexture(dimensions, format, renderable, renderTargetSampleCnt,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400149 GrMipmapped::kNo, budgeted, isProtected);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500150 }
151 if (!tex) {
152 return nullptr;
153 }
154
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400155 return this->createWrapped(std::move(tex), UseAllocator::kYes);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500156}
157
Brian Salomon4eb38b72019-08-05 12:58:39 -0400158sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createInstantiatedProxy(
Brian Salomona56a7462020-02-07 14:17:25 -0500159 SkISize dimensions,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400160 GrColorType colorType,
161 GrRenderable renderable,
162 int renderTargetSampleCnt,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400163 SkBackingFit fit,
164 SkBudgeted budgeted,
165 GrProtected isProtected) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500166 ASSERT_SINGLE_OWNER
167 if (this->isAbandoned()) {
168 return nullptr;
169 }
Brian Salomon4eb38b72019-08-05 12:58:39 -0400170 auto format = this->caps()->getDefaultBackendFormat(colorType, renderable);
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400171 return this->testingOnly_createInstantiatedProxy(dimensions,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400172 format,
173 renderable,
174 renderTargetSampleCnt,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400175 fit,
176 budgeted,
177 isProtected);
178}
179
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400180sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createWrapped(sk_sp<GrTexture> tex) {
181 return this->createWrapped(std::move(tex), UseAllocator::kYes);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500182}
183#endif
184
Brian Salomonbeb7f522019-08-30 16:19:42 -0400185sk_sp<GrTextureProxy> GrProxyProvider::createWrapped(sk_sp<GrTexture> tex,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400186 UseAllocator useAllocator) {
Robert Phillipsadbe1322018-01-17 13:35:46 -0500187#ifdef SK_DEBUG
188 if (tex->getUniqueKey().isValid()) {
Greg Daniel3a365112020-02-14 10:47:18 -0500189 SkASSERT(!this->findProxyByUniqueKey(tex->getUniqueKey()));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500190 }
191#endif
192
193 if (tex->asRenderTarget()) {
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400194 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), useAllocator,
195 this->isDDLProvider()));
Greg Daniel3a365112020-02-14 10:47:18 -0500196 } else {
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400197 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), useAllocator,
198 this->isDDLProvider()));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500199 }
200}
201
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500202sk_sp<GrTextureProxy> GrProxyProvider::findOrCreateProxyByUniqueKey(const GrUniqueKey& key,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400203 UseAllocator useAllocator) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500204 ASSERT_SINGLE_OWNER
205
206 if (this->isAbandoned()) {
207 return nullptr;
208 }
209
Greg Daniel3a365112020-02-14 10:47:18 -0500210 sk_sp<GrTextureProxy> result = this->findProxyByUniqueKey(key);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500211 if (result) {
212 return result;
213 }
214
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400215 auto direct = fImageContext->asDirectContext();
Robert Phillipsa41c6852019-02-07 10:44:10 -0500216 if (!direct) {
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500217 return nullptr;
218 }
219
Robert Phillipsa41c6852019-02-07 10:44:10 -0500220 GrResourceCache* resourceCache = direct->priv().getResourceCache();
221
222 GrGpuResource* resource = resourceCache->findAndRefUniqueResource(key);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500223 if (!resource) {
224 return nullptr;
225 }
226
227 sk_sp<GrTexture> texture(static_cast<GrSurface*>(resource)->asTexture());
228 SkASSERT(texture);
229
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400230 result = this->createWrapped(std::move(texture), useAllocator);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500231 SkASSERT(result->getUniqueKey() == key);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500232 // createWrapped should've added this for us
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500233 SkASSERT(fUniquelyKeyedProxies.find(key));
234 return result;
235}
236
Brian Salomond005b692020-04-01 15:47:05 -0400237GrSurfaceProxyView GrProxyProvider::findCachedProxyWithColorTypeFallback(const GrUniqueKey& key,
238 GrSurfaceOrigin origin,
Brian Salomon0029db02020-04-03 10:41:24 -0400239 GrColorType ct,
240 int sampleCnt) {
Brian Salomond005b692020-04-01 15:47:05 -0400241 auto proxy = this->findOrCreateProxyByUniqueKey(key);
242 if (!proxy) {
243 return {};
244 }
245 // Assume that we used a fallback color type if and only if the proxy is renderable.
246 if (proxy->asRenderTargetProxy()) {
247 GrBackendFormat expectedFormat;
248 std::tie(ct, expectedFormat) =
Brian Salomon0029db02020-04-03 10:41:24 -0400249 GrRenderTargetContext::GetFallbackColorTypeAndFormat(fImageContext, ct, sampleCnt);
Brian Salomond005b692020-04-01 15:47:05 -0400250 SkASSERT(expectedFormat == proxy->backendFormat());
251 }
252 GrSwizzle swizzle = fImageContext->priv().caps()->getReadSwizzle(proxy->backendFormat(), ct);
253 return {std::move(proxy), origin, swizzle};
254}
255
Brian Osmande496652019-03-22 13:42:33 -0400256sk_sp<GrTextureProxy> GrProxyProvider::createProxyFromBitmap(const SkBitmap& bitmap,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400257 GrMipmapped mipMapped,
Brian Salomonbc074a62020-03-18 10:06:13 -0400258 SkBackingFit fit,
259 SkBudgeted budgeted) {
Brian Osman412674f2019-02-07 15:34:58 -0500260 ASSERT_SINGLE_OWNER
Brian Salomon7e67dca2020-07-21 09:27:25 -0400261 SkASSERT(fit == SkBackingFit::kExact || mipMapped == GrMipmapped::kNo);
Brian Osman412674f2019-02-07 15:34:58 -0500262
263 if (this->isAbandoned()) {
264 return nullptr;
265 }
266
Brian Osman2b23c4b2018-06-01 12:25:08 -0400267 if (!SkImageInfoIsValid(bitmap.info())) {
Greg Daniela4ead652018-02-07 10:21:48 -0500268 return nullptr;
269 }
270
Brian Osmande496652019-03-22 13:42:33 -0400271 ATRACE_ANDROID_FRAMEWORK("Upload %sTexture [%ux%u]",
Brian Salomon7e67dca2020-07-21 09:27:25 -0400272 GrMipmapped::kYes == mipMapped ? "MipMap " : "",
Brian Osmande496652019-03-22 13:42:33 -0400273 bitmap.width(), bitmap.height());
Greg Daniela4ead652018-02-07 10:21:48 -0500274
275 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
276 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
277 // 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 -0500278 SkBitmap copyBitmap = bitmap;
279 if (!this->renderingDirectly() && !bitmap.isImmutable()) {
280 copyBitmap.allocPixels();
281 if (!bitmap.readPixels(copyBitmap.pixmap())) {
282 return nullptr;
283 }
284 copyBitmap.setImmutable();
Greg Daniela4ead652018-02-07 10:21:48 -0500285 }
286
Greg Daniel6f5441a2020-01-28 17:02:49 -0500287 sk_sp<GrTextureProxy> proxy;
Brian Salomon7e67dca2020-07-21 09:27:25 -0400288 if (mipMapped == GrMipmapped::kNo ||
Mike Reed13711eb2020-07-14 17:16:32 -0400289 0 == SkMipmap::ComputeLevelCount(copyBitmap.width(), copyBitmap.height())) {
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400290 proxy = this->createNonMippedProxyFromBitmap(copyBitmap, fit, budgeted);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500291 } else {
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400292 proxy = this->createMippedProxyFromBitmap(copyBitmap, budgeted);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500293 }
294
295 if (!proxy) {
296 return nullptr;
297 }
298
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400299 auto direct = fImageContext->asDirectContext();
Greg Daniel6f5441a2020-01-28 17:02:49 -0500300 if (direct) {
301 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
302
303 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
304 // we're better off instantiating the proxy immediately here.
305 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
306 return nullptr;
307 }
308 }
309 return proxy;
310}
311
312sk_sp<GrTextureProxy> GrProxyProvider::createNonMippedProxyFromBitmap(const SkBitmap& bitmap,
313 SkBackingFit fit,
Brian Salomonbc074a62020-03-18 10:06:13 -0400314 SkBudgeted budgeted) {
Brian Salomona56a7462020-02-07 14:17:25 -0500315 auto dims = bitmap.dimensions();
Greg Daniel6f5441a2020-01-28 17:02:49 -0500316
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400317 auto colorType = SkColorTypeToGrColorType(bitmap.colorType());
318 GrBackendFormat format = this->caps()->getDefaultBackendFormat(colorType, GrRenderable::kNo);
319 if (!format.isValid()) {
320 return nullptr;
321 }
322
Greg Daniel6f5441a2020-01-28 17:02:49 -0500323 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomon63410e92020-03-23 18:32:50 -0400324 [bitmap](GrResourceProvider* resourceProvider, const LazySurfaceDesc& desc) {
Brian Salomon40a40622020-07-21 10:32:07 -0400325 SkASSERT(desc.fMipmapped == GrMipmapped::kNo);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500326 GrMipLevel mipLevel = { bitmap.getPixels(), bitmap.rowBytes() };
Brian Salomon63410e92020-03-23 18:32:50 -0400327 auto colorType = SkColorTypeToGrColorType(bitmap.colorType());
Greg Daniel6f5441a2020-01-28 17:02:49 -0500328 return LazyCallbackResult(resourceProvider->createTexture(
Brian Salomon63410e92020-03-23 18:32:50 -0400329 desc.fDimensions, desc.fFormat, colorType, desc.fRenderable,
330 desc.fSampleCnt, desc.fBudgeted, desc.fFit, desc.fProtected, mipLevel));
Greg Daniel6f5441a2020-01-28 17:02:49 -0500331 },
Greg Danielc113f002020-08-26 13:28:22 -0400332 format, dims, GrMipmapped::kNo, GrMipmapStatus::kNotAllocated,
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400333 GrInternalSurfaceFlags::kNone, fit, budgeted, GrProtected::kNo, UseAllocator::kYes);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500334
335 if (!proxy) {
336 return nullptr;
337 }
Brian Salomona56a7462020-02-07 14:17:25 -0500338 SkASSERT(proxy->dimensions() == bitmap.dimensions());
Greg Daniel6f5441a2020-01-28 17:02:49 -0500339 return proxy;
340}
341
342sk_sp<GrTextureProxy> GrProxyProvider::createMippedProxyFromBitmap(const SkBitmap& bitmap,
Brian Salomonbc074a62020-03-18 10:06:13 -0400343 SkBudgeted budgeted) {
Brian Salomon69100f02020-07-21 10:49:25 -0400344 SkASSERT(this->caps()->mipmapSupport());
Greg Daniel6f5441a2020-01-28 17:02:49 -0500345
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400346 auto colorType = SkColorTypeToGrColorType(bitmap.colorType());
347 GrBackendFormat format = this->caps()->getDefaultBackendFormat(colorType, GrRenderable::kNo);
348 if (!format.isValid()) {
349 return nullptr;
350 }
Greg Daniel6f5441a2020-01-28 17:02:49 -0500351
Mike Reed13711eb2020-07-14 17:16:32 -0400352 sk_sp<SkMipmap> mipmaps(SkMipmap::Build(bitmap.pixmap(), nullptr));
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400353 if (!mipmaps) {
354 return nullptr;
355 }
356
Brian Salomona56a7462020-02-07 14:17:25 -0500357 auto dims = bitmap.dimensions();
Greg Danielce3ddaa2020-01-22 16:58:15 -0500358
Greg Daniela4ead652018-02-07 10:21:48 -0500359 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomon63410e92020-03-23 18:32:50 -0400360 [bitmap, mipmaps](GrResourceProvider* resourceProvider, const LazySurfaceDesc& desc) {
Brian Osman1b97f132018-09-13 17:33:48 +0000361 const int mipLevelCount = mipmaps->countLevels() + 1;
362 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
Brian Salomon63410e92020-03-23 18:32:50 -0400363 auto colorType = SkColorTypeToGrColorType(bitmap.colorType());
Brian Osman1b97f132018-09-13 17:33:48 +0000364
Greg Daniel6f5441a2020-01-28 17:02:49 -0500365 texels[0].fPixels = bitmap.getPixels();
366 texels[0].fRowBytes = bitmap.rowBytes();
Greg Daniela4ead652018-02-07 10:21:48 -0500367
Greg Daniela4ead652018-02-07 10:21:48 -0500368 for (int i = 1; i < mipLevelCount; ++i) {
Mike Reed13711eb2020-07-14 17:16:32 -0400369 SkMipmap::Level generatedMipLevel;
Greg Daniela4ead652018-02-07 10:21:48 -0500370 mipmaps->getLevel(i - 1, &generatedMipLevel);
371 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
372 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
373 SkASSERT(texels[i].fPixels);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500374 SkASSERT(generatedMipLevel.fPixmap.colorType() == bitmap.colorType());
Greg Daniela4ead652018-02-07 10:21:48 -0500375 }
Brian Salomonbeb7f522019-08-30 16:19:42 -0400376 return LazyCallbackResult(resourceProvider->createTexture(
Brian Salomon63410e92020-03-23 18:32:50 -0400377 desc.fDimensions, desc.fFormat, colorType, GrRenderable::kNo, 1,
378 desc.fBudgeted, GrProtected::kNo, texels.get(), mipLevelCount));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500379 },
Greg Danielc113f002020-08-26 13:28:22 -0400380 format, dims, GrMipmapped::kYes, GrMipmapStatus::kValid, GrInternalSurfaceFlags::kNone,
381 SkBackingFit::kExact, budgeted, GrProtected::kNo, UseAllocator::kYes);
Greg Daniela4ead652018-02-07 10:21:48 -0500382
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400383 if (!proxy) {
384 return nullptr;
385 }
386
Brian Salomona56a7462020-02-07 14:17:25 -0500387 SkASSERT(proxy->dimensions() == bitmap.dimensions());
Greg Daniel6f5441a2020-01-28 17:02:49 -0500388
Greg Daniela4ead652018-02-07 10:21:48 -0500389 return proxy;
390}
391
Greg Daniel4065d452018-11-16 15:43:41 -0500392sk_sp<GrTextureProxy> GrProxyProvider::createProxy(const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -0500393 SkISize dimensions,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400394 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400395 int renderTargetSampleCnt,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400396 GrMipmapped mipMapped,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500397 SkBackingFit fit,
398 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -0400399 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400400 GrInternalSurfaceFlags surfaceFlags,
401 GrSurfaceProxy::UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500402 ASSERT_SINGLE_OWNER
403 if (this->isAbandoned()) {
404 return nullptr;
405 }
406
Robert Phillips8ff8bcc2019-07-29 17:03:35 -0400407 const GrCaps* caps = this->caps();
408
409 if (caps->isFormatCompressed(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400410 // Deferred proxies for compressed textures are not supported.
411 return nullptr;
412 }
Robert Phillips0902c982019-07-16 07:47:56 -0400413
Brian Salomon7e67dca2020-07-21 09:27:25 -0400414 if (GrMipmapped::kYes == mipMapped) {
Mike Reed13711eb2020-07-14 17:16:32 -0400415 // SkMipmap doesn't include the base level in the level count so we have to add 1
416 int mipCount = SkMipmap::ComputeLevelCount(dimensions.fWidth, dimensions.fHeight) + 1;
Greg Danielf6f7b672018-02-15 13:06:26 -0500417 if (1 == mipCount) {
Brian Salomon7e67dca2020-07-21 09:27:25 -0400418 mipMapped = GrMipmapped::kNo;
Greg Danielf6f7b672018-02-15 13:06:26 -0500419 }
420 }
421
Brian Salomona56a7462020-02-07 14:17:25 -0500422 if (!caps->validateSurfaceParams(dimensions, format, renderable, renderTargetSampleCnt,
423 mipMapped)) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500424 return nullptr;
425 }
Brian Salomona6db5102020-07-21 09:56:23 -0400426 GrMipmapStatus mipmapStatus = (GrMipmapped::kYes == mipMapped)
427 ? GrMipmapStatus::kDirty
428 : GrMipmapStatus::kNotAllocated;
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400429 if (renderable == GrRenderable::kYes) {
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400430 renderTargetSampleCnt = caps->getRenderTargetSampleCount(renderTargetSampleCnt, format);
Greg Daniel6fa62e22019-08-07 15:52:37 -0400431 SkASSERT(renderTargetSampleCnt);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500432 // We know anything we instantiate later from this deferred path will be
433 // both texturable and renderable
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400434 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
Brian Salomona6db5102020-07-21 09:56:23 -0400435 *caps, format, dimensions, renderTargetSampleCnt, mipMapped, mipmapStatus, fit,
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400436 budgeted, isProtected, surfaceFlags, useAllocator, this->isDDLProvider()));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500437 }
438
Brian Salomona6db5102020-07-21 09:56:23 -0400439 return sk_sp<GrTextureProxy>(new GrTextureProxy(format, dimensions, mipMapped, mipmapStatus,
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400440 fit, budgeted, isProtected, surfaceFlags,
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400441 useAllocator, this->isDDLProvider()));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500442}
443
Brian Salomonbb8dde82019-06-27 10:52:13 -0400444sk_sp<GrTextureProxy> GrProxyProvider::createCompressedTextureProxy(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400445 SkISize dimensions, SkBudgeted budgeted, GrMipmapped mipMapped, GrProtected isProtected,
Robert Phillipse4720c62020-01-14 14:33:24 -0500446 SkImage::CompressionType compressionType, sk_sp<SkData> data) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500447 ASSERT_SINGLE_OWNER
448 if (this->isAbandoned()) {
449 return nullptr;
450 }
Brian Salomonbb8dde82019-06-27 10:52:13 -0400451
Robert Phillips8ff8bcc2019-07-29 17:03:35 -0400452 GrBackendFormat format = this->caps()->getBackendFormatFromCompressionType(compressionType);
453
Greg Daniel7bfc9132019-08-14 14:23:53 -0400454 if (!this->caps()->isFormatTexturable(format)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500455 return nullptr;
456 }
457
Brian Salomona6db5102020-07-21 09:56:23 -0400458 GrMipmapStatus mipmapStatus = (GrMipmapped::kYes == mipMapped) ? GrMipmapStatus::kValid
459 : GrMipmapStatus::kNotAllocated;
Robert Phillipse4720c62020-01-14 14:33:24 -0500460
Jim Van Verthee06b332019-01-18 10:36:32 -0500461 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomon63410e92020-03-23 18:32:50 -0400462 [data](GrResourceProvider* resourceProvider, const LazySurfaceDesc& desc) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400463 return LazyCallbackResult(resourceProvider->createCompressedTexture(
Brian Salomon40a40622020-07-21 10:32:07 -0400464 desc.fDimensions, desc.fFormat, desc.fBudgeted, desc.fMipmapped,
Brian Salomon63410e92020-03-23 18:32:50 -0400465 desc.fProtected, data.get()));
Brian Salomonbb8dde82019-06-27 10:52:13 -0400466 },
Greg Danielc113f002020-08-26 13:28:22 -0400467 format, dimensions, mipMapped, mipmapStatus,GrInternalSurfaceFlags::kReadOnly,
468 SkBackingFit::kExact, SkBudgeted::kYes, GrProtected::kNo, UseAllocator::kYes);
Jim Van Verthee06b332019-01-18 10:36:32 -0500469
470 if (!proxy) {
471 return nullptr;
472 }
473
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400474 auto direct = fImageContext->asDirectContext();
Robert Phillipsa41c6852019-02-07 10:44:10 -0500475 if (direct) {
476 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Jim Van Verthee06b332019-01-18 10:36:32 -0500477 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
478 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500479 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500480 return nullptr;
481 }
482 }
483 return proxy;
484}
485
Brian Salomon7578f3e2018-03-07 14:39:54 -0500486sk_sp<GrTextureProxy> GrProxyProvider::wrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500487 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500488 GrWrapCacheable cacheable,
Brian Salomonc67c31c2018-12-06 10:00:03 -0500489 GrIOType ioType,
Robert Phillipse22c5ca2020-06-19 12:29:57 -0400490 sk_sp<GrRefCntedCallback> releaseHelper) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500491 SkASSERT(ioType != kWrite_GrIOType);
Robert Phillipse22c5ca2020-06-19 12:29:57 -0400492
Robert Phillipsf9bec202018-01-16 09:21:01 -0500493 if (this->isAbandoned()) {
494 return nullptr;
495 }
496
Brian Salomonf7778972018-03-08 10:13:17 -0500497 // This is only supported on a direct GrContext.
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400498 auto direct = fImageContext->asDirectContext();
Robert Phillipsa41c6852019-02-07 10:44:10 -0500499 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500500 return nullptr;
501 }
502
Robert Phillipsa41c6852019-02-07 10:44:10 -0500503 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
504
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500505 sk_sp<GrTexture> tex =
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400506 resourceProvider->wrapBackendTexture(backendTex, ownership, cacheable, ioType);
Brian Salomonf7778972018-03-08 10:13:17 -0500507 if (!tex) {
508 return nullptr;
509 }
Robert Phillipsadbe1322018-01-17 13:35:46 -0500510
Robert Phillipse22c5ca2020-06-19 12:29:57 -0400511 if (releaseHelper) {
512 tex->setRelease(std::move(releaseHelper));
Greg Daniel6a0176b2018-01-30 09:28:44 -0500513 }
514
Brian Salomonf7778972018-03-08 10:13:17 -0500515 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
516 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500517 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500518
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400519 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), UseAllocator::kNo,
520 this->isDDLProvider()));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500521}
522
Robert Phillipsa1121332020-06-29 13:05:29 -0400523sk_sp<GrTextureProxy> GrProxyProvider::wrapCompressedBackendTexture(
524 const GrBackendTexture& beTex,
525 GrWrapOwnership ownership,
526 GrWrapCacheable cacheable,
527 sk_sp<GrRefCntedCallback> releaseHelper) {
Robert Phillipsead321b2019-12-19 10:16:32 -0500528 if (this->isAbandoned()) {
529 return nullptr;
530 }
531
532 // This is only supported on a direct GrContext.
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400533 auto direct = fImageContext->asDirectContext();
Robert Phillipsead321b2019-12-19 10:16:32 -0500534 if (!direct) {
535 return nullptr;
536 }
537
538 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
539
540 sk_sp<GrTexture> tex = resourceProvider->wrapCompressedBackendTexture(beTex, ownership,
541 cacheable);
542 if (!tex) {
543 return nullptr;
544 }
545
Robert Phillipsa1121332020-06-29 13:05:29 -0400546 if (releaseHelper) {
547 tex->setRelease(std::move(releaseHelper));
Robert Phillipsead321b2019-12-19 10:16:32 -0500548 }
549
550 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
551 // Make sure we match how we created the proxy with SkBudgeted::kNo
552 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
553
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400554 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), UseAllocator::kNo,
555 this->isDDLProvider()));
Robert Phillipsead321b2019-12-19 10:16:32 -0500556}
557
Brian Salomon7578f3e2018-03-07 14:39:54 -0500558sk_sp<GrTextureProxy> GrProxyProvider::wrapRenderableBackendTexture(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400559 const GrBackendTexture& backendTex,
560 int sampleCnt,
561 GrWrapOwnership ownership,
562 GrWrapCacheable cacheable,
Robert Phillipsa1121332020-06-29 13:05:29 -0400563 sk_sp<GrRefCntedCallback> releaseHelper) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500564 if (this->isAbandoned()) {
565 return nullptr;
566 }
567
Brian Salomonf7778972018-03-08 10:13:17 -0500568 // This is only supported on a direct GrContext.
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400569 auto direct = fImageContext->asDirectContext();
Robert Phillipsa41c6852019-02-07 10:44:10 -0500570 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500571 return nullptr;
572 }
573
Robert Phillips0902c982019-07-16 07:47:56 -0400574 const GrCaps* caps = this->caps();
575
Robert Phillipsa41c6852019-02-07 10:44:10 -0500576 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
577
Greg Daniel6fa62e22019-08-07 15:52:37 -0400578 sampleCnt = caps->getRenderTargetSampleCount(sampleCnt, backendTex.getBackendFormat());
579 SkASSERT(sampleCnt);
580
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400581 sk_sp<GrTexture> tex = resourceProvider->wrapRenderableBackendTexture(
582 backendTex, sampleCnt, ownership, cacheable);
Brian Salomonf7778972018-03-08 10:13:17 -0500583 if (!tex) {
584 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500585 }
586
Robert Phillipsa1121332020-06-29 13:05:29 -0400587 if (releaseHelper) {
588 tex->setRelease(std::move(releaseHelper));
Greg Daniel8ce79912019-02-05 10:08:43 -0500589 }
590
Brian Salomonf7778972018-03-08 10:13:17 -0500591 SkASSERT(tex->asRenderTarget()); // A GrTextureRenderTarget
592 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500593 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Greg Daniel6abda432018-02-15 14:55:00 -0500594
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400595 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), UseAllocator::kNo,
596 this->isDDLProvider()));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500597}
598
Brian Salomon7578f3e2018-03-07 14:39:54 -0500599sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400600 const GrBackendRenderTarget& backendRT,
Robert Phillipsa1121332020-06-29 13:05:29 -0400601 sk_sp<GrRefCntedCallback> releaseHelper) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500602 if (this->isAbandoned()) {
603 return nullptr;
604 }
605
Brian Salomonf7778972018-03-08 10:13:17 -0500606 // This is only supported on a direct GrContext.
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400607 auto direct = fImageContext->asDirectContext();
Robert Phillipsa41c6852019-02-07 10:44:10 -0500608 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500609 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500610 }
611
Robert Phillipsa41c6852019-02-07 10:44:10 -0500612 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
613
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400614 sk_sp<GrRenderTarget> rt = resourceProvider->wrapBackendRenderTarget(backendRT);
Brian Salomonf7778972018-03-08 10:13:17 -0500615 if (!rt) {
616 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500617 }
Greg Daniel8ce79912019-02-05 10:08:43 -0500618
Robert Phillipsa1121332020-06-29 13:05:29 -0400619 if (releaseHelper) {
620 rt->setRelease(std::move(releaseHelper));
Greg Daniel8ce79912019-02-05 10:08:43 -0500621 }
622
Brian Salomonf7778972018-03-08 10:13:17 -0500623 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
624 SkASSERT(!rt->getUniqueKey().isValid());
625 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500626 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Brian Salomonf7778972018-03-08 10:13:17 -0500627
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400628 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(std::move(rt), UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500629}
630
Brian Salomon7578f3e2018-03-07 14:39:54 -0500631sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendTextureAsRenderTarget(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400632 const GrBackendTexture& backendTex, int sampleCnt) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500633 if (this->isAbandoned()) {
634 return nullptr;
635 }
636
Brian Salomonf7778972018-03-08 10:13:17 -0500637 // This is only supported on a direct GrContext.
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400638 auto direct = fImageContext->asDirectContext();
Robert Phillipsa41c6852019-02-07 10:44:10 -0500639 if (!direct) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500640 return nullptr;
641 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500642
Robert Phillipsa41c6852019-02-07 10:44:10 -0500643 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
644
Brian Salomonf7778972018-03-08 10:13:17 -0500645 sk_sp<GrRenderTarget> rt =
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400646 resourceProvider->wrapBackendTextureAsRenderTarget(backendTex, sampleCnt);
Brian Salomonf7778972018-03-08 10:13:17 -0500647 if (!rt) {
648 return nullptr;
Greg Danielf87651e2018-02-21 11:36:53 -0500649 }
Brian Salomonf7778972018-03-08 10:13:17 -0500650 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
651 SkASSERT(!rt->getUniqueKey().isValid());
Greg Danielb46add82019-01-02 14:51:29 -0500652 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500653 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielf87651e2018-02-21 11:36:53 -0500654
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400655 return sk_sp<GrSurfaceProxy>(new GrRenderTargetProxy(std::move(rt), UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500656}
657
Greg Danielb46add82019-01-02 14:51:29 -0500658sk_sp<GrRenderTargetProxy> GrProxyProvider::wrapVulkanSecondaryCBAsRenderTarget(
659 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
660 if (this->isAbandoned()) {
661 return nullptr;
662 }
663
664 // This is only supported on a direct GrContext.
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400665 auto direct = fImageContext->asDirectContext();
Robert Phillipsa41c6852019-02-07 10:44:10 -0500666 if (!direct) {
Greg Danielb46add82019-01-02 14:51:29 -0500667 return nullptr;
668 }
669
Robert Phillipsa41c6852019-02-07 10:44:10 -0500670 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Danielb46add82019-01-02 14:51:29 -0500671
Robert Phillipsa41c6852019-02-07 10:44:10 -0500672 sk_sp<GrRenderTarget> rt = resourceProvider->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
673 vkInfo);
Greg Danielb46add82019-01-02 14:51:29 -0500674 if (!rt) {
675 return nullptr;
676 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500677
Greg Danielb46add82019-01-02 14:51:29 -0500678 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
679 SkASSERT(!rt->getUniqueKey().isValid());
680 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500681 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielb46add82019-01-02 14:51:29 -0500682
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400683 GrColorType colorType = SkColorTypeToGrColorType(imageInfo.colorType());
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400684
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400685 if (!this->caps()->isFormatAsColorTypeRenderable(colorType, rt->backendFormat(),
686 rt->numSamples())) {
687 return nullptr;
688 }
689
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400690 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
691 std::move(rt), UseAllocator::kNo, GrRenderTargetProxy::WrapsVkSecondaryCB::kYes));
Brian Salomone8a766b2019-07-19 14:24:36 -0400692}
693
694sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
695 const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -0500696 SkISize dimensions,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400697 GrMipmapped mipMapped,
Brian Salomona6db5102020-07-21 09:56:23 -0400698 GrMipmapStatus mipmapStatus,
Brian Salomone8a766b2019-07-19 14:24:36 -0400699 GrInternalSurfaceFlags surfaceFlags,
700 SkBackingFit fit,
701 SkBudgeted budgeted,
702 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400703 GrSurfaceProxy::UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500704 ASSERT_SINGLE_OWNER
705 if (this->isAbandoned()) {
706 return nullptr;
707 }
Brian Salomona56a7462020-02-07 14:17:25 -0500708 SkASSERT((dimensions.fWidth <= 0 && dimensions.fHeight <= 0) ||
709 (dimensions.fWidth > 0 && dimensions.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400710
Robert Phillips0a15cc62019-07-30 12:49:10 -0400711 if (!format.isValid()) {
712 return nullptr;
713 }
714
Brian Salomona56a7462020-02-07 14:17:25 -0500715 if (dimensions.fWidth > this->caps()->maxTextureSize() ||
716 dimensions.fHeight > this->caps()->maxTextureSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400717 return nullptr;
718 }
719
Greg Danielc113f002020-08-26 13:28:22 -0400720 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(callback),
721 format,
722 dimensions,
723 mipMapped,
724 mipmapStatus,
725 fit,
726 budgeted,
727 isProtected,
728 surfaceFlags,
729 useAllocator,
730 this->isDDLProvider()));
Robert Phillips777707b2018-01-17 11:40:14 -0500731}
732
Robert Phillipse8fabb22018-02-04 14:33:21 -0500733sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy(
Brian Salomonbeb7f522019-08-30 16:19:42 -0400734 LazyInstantiateCallback&& callback,
735 const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -0500736 SkISize dimensions,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400737 int sampleCnt,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400738 GrInternalSurfaceFlags surfaceFlags,
739 const TextureInfo* textureInfo,
Brian Salomona6db5102020-07-21 09:56:23 -0400740 GrMipmapStatus mipmapStatus,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400741 SkBackingFit fit,
742 SkBudgeted budgeted,
743 GrProtected isProtected,
744 bool wrapsVkSecondaryCB,
745 UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500746 ASSERT_SINGLE_OWNER
747 if (this->isAbandoned()) {
748 return nullptr;
749 }
Brian Salomona56a7462020-02-07 14:17:25 -0500750 SkASSERT((dimensions.fWidth <= 0 && dimensions.fHeight <= 0) ||
751 (dimensions.fWidth > 0 && dimensions.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400752
Brian Salomona56a7462020-02-07 14:17:25 -0500753 if (dimensions.fWidth > this->caps()->maxRenderTargetSize() ||
754 dimensions.fHeight > this->caps()->maxRenderTargetSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400755 return nullptr;
756 }
757
Brian Salomon7226c232018-07-30 13:13:17 -0400758 if (textureInfo) {
Greg Danielb085fa92019-03-05 16:55:12 -0500759 // Wrapped vulkan secondary command buffers don't support texturing since we won't have an
760 // actual VkImage to texture from.
761 SkASSERT(!wrapsVkSecondaryCB);
Brian Salomon7226c232018-07-30 13:13:17 -0400762 return sk_sp<GrRenderTargetProxy>(new GrTextureRenderTargetProxy(
Greg Daniel3a365112020-02-14 10:47:18 -0500763 *this->caps(), std::move(callback), format, dimensions, sampleCnt,
Brian Salomon40a40622020-07-21 10:32:07 -0400764 textureInfo->fMipmapped, mipmapStatus, fit, budgeted, isProtected, surfaceFlags,
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400765 useAllocator, this->isDDLProvider()));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500766 }
767
Greg Danielb085fa92019-03-05 16:55:12 -0500768 GrRenderTargetProxy::WrapsVkSecondaryCB vkSCB =
769 wrapsVkSecondaryCB ? GrRenderTargetProxy::WrapsVkSecondaryCB::kYes
770 : GrRenderTargetProxy::WrapsVkSecondaryCB::kNo;
771
Greg Daniel3a365112020-02-14 10:47:18 -0500772 return sk_sp<GrRenderTargetProxy>(
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400773 new GrRenderTargetProxy(std::move(callback), format, dimensions, sampleCnt, fit,
774 budgeted, isProtected, surfaceFlags, useAllocator, vkSCB));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500775}
776
Brian Salomonbeb7f522019-08-30 16:19:42 -0400777sk_sp<GrTextureProxy> GrProxyProvider::MakeFullyLazyProxy(LazyInstantiateCallback&& callback,
778 const GrBackendFormat& format,
779 GrRenderable renderable,
780 int renderTargetSampleCnt,
781 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400782 const GrCaps& caps,
783 UseAllocator useAllocator) {
Robert Phillips0a15cc62019-07-30 12:49:10 -0400784 if (!format.isValid()) {
785 return nullptr;
786 }
787
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400788 SkASSERT(renderTargetSampleCnt == 1 || renderable == GrRenderable::kYes);
Robert Phillips10d17212019-04-24 14:09:10 -0400789 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNone;
Robert Phillips777707b2018-01-17 11:40:14 -0500790
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400791 // MakeFullyLazyProxy is only called at flush time so we know these texture proxies are
792 // not being created by a DDL provider.
Brian Salomona56a7462020-02-07 14:17:25 -0500793 static constexpr SkISize kLazyDims = {-1, -1};
Brian Salomonbeb7f522019-08-30 16:19:42 -0400794 if (GrRenderable::kYes == renderable) {
795 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
Greg Daniel3a365112020-02-14 10:47:18 -0500796 caps, std::move(callback), format, kLazyDims, renderTargetSampleCnt,
Brian Salomona6db5102020-07-21 09:56:23 -0400797 GrMipmapped::kNo, GrMipmapStatus::kNotAllocated, SkBackingFit::kApprox,
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400798 SkBudgeted::kYes, isProtected, surfaceFlags, useAllocator, GrDDLProvider::kNo));
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400799 } else {
800 return sk_sp<GrTextureProxy>(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400801 new GrTextureProxy(std::move(callback), format, kLazyDims, GrMipmapped::kNo,
Brian Salomona6db5102020-07-21 09:56:23 -0400802 GrMipmapStatus::kNotAllocated, SkBackingFit::kApprox,
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400803 SkBudgeted::kYes, isProtected, surfaceFlags, useAllocator,
804 GrDDLProvider::kNo));
Brian Salomonbeb7f522019-08-30 16:19:42 -0400805 }
Robert Phillips777707b2018-01-17 11:40:14 -0500806}
807
Robert Phillips427966a2018-12-20 17:20:43 -0500808void GrProxyProvider::processInvalidUniqueKey(const GrUniqueKey& key, GrTextureProxy* proxy,
809 InvalidateGPUResource invalidateGPUResource) {
Mike Klein04ef8102020-03-16 12:44:23 -0500810 this->processInvalidUniqueKeyImpl(key, proxy, invalidateGPUResource, RemoveTableEntry::kYes);
811}
812
813void GrProxyProvider::processInvalidUniqueKeyImpl(const GrUniqueKey& key, GrTextureProxy* proxy,
814 InvalidateGPUResource invalidateGPUResource,
815 RemoveTableEntry removeTableEntry) {
Chris Dalton2de13dd2019-01-03 15:11:59 -0700816 SkASSERT(key.isValid());
817
Robert Phillips427966a2018-12-20 17:20:43 -0500818 if (!proxy) {
819 proxy = fUniquelyKeyedProxies.find(key);
820 }
Chris Dalton2de13dd2019-01-03 15:11:59 -0700821 SkASSERT(!proxy || proxy->getUniqueKey() == key);
822
823 // Locate the corresponding GrGpuResource (if it needs to be invalidated) before clearing the
824 // proxy's unique key. We must do it in this order because 'key' may alias the proxy's key.
825 sk_sp<GrGpuResource> invalidGpuResource;
826 if (InvalidateGPUResource::kYes == invalidateGPUResource) {
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400827 auto direct = fImageContext->asDirectContext();
Brian Salomon01ceae92019-04-02 11:49:54 -0400828 if (direct) {
829 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
830 invalidGpuResource = resourceProvider->findByUniqueKey<GrGpuResource>(key);
Chris Dalton2de13dd2019-01-03 15:11:59 -0700831 }
832 SkASSERT(!invalidGpuResource || invalidGpuResource->getUniqueKey() == key);
833 }
Robert Phillips427966a2018-12-20 17:20:43 -0500834
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500835 // Note: this method is called for the whole variety of GrGpuResources so often 'key'
836 // will not be in 'fUniquelyKeyedProxies'.
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500837 if (proxy) {
Mike Klein04ef8102020-03-16 12:44:23 -0500838 if (removeTableEntry == RemoveTableEntry::kYes) {
839 fUniquelyKeyedProxies.remove(key);
840 }
Robert Phillips427966a2018-12-20 17:20:43 -0500841 proxy->cacheAccess().clearUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500842 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500843
Chris Dalton2de13dd2019-01-03 15:11:59 -0700844 if (invalidGpuResource) {
845 invalidGpuResource->resourcePriv().removeUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500846 }
847}
848
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400849GrDDLProvider GrProxyProvider::isDDLProvider() const {
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400850 return fImageContext->asDirectContext() ? GrDDLProvider::kNo : GrDDLProvider::kYes;
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400851}
852
Robert Phillipsa41c6852019-02-07 10:44:10 -0500853uint32_t GrProxyProvider::contextID() const {
854 return fImageContext->priv().contextID();
855}
856
857const GrCaps* GrProxyProvider::caps() const {
858 return fImageContext->priv().caps();
859}
860
861sk_sp<const GrCaps> GrProxyProvider::refCaps() const {
862 return fImageContext->priv().refCaps();
863}
864
Robert Phillipsa9162df2019-02-11 14:12:03 -0500865bool GrProxyProvider::isAbandoned() const {
866 return fImageContext->priv().abandoned();
867}
868
Robert Phillips0790f8a2018-09-18 13:11:03 -0400869void GrProxyProvider::orphanAllUniqueKeys() {
Mike Kleincff63962020-03-14 16:22:45 -0500870 fUniquelyKeyedProxies.foreach([&](GrTextureProxy* proxy){
871 proxy->fProxyProvider = nullptr;
872 });
Robert Phillips0790f8a2018-09-18 13:11:03 -0400873}
874
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500875void GrProxyProvider::removeAllUniqueKeys() {
Mike Klein04ef8102020-03-16 12:44:23 -0500876 fUniquelyKeyedProxies.foreach([&](GrTextureProxy* proxy){
877 // It's not safe to remove table entries while iterating with foreach(),
878 // but since we're going to remove them all anyway, simply save that for the end.
879 this->processInvalidUniqueKeyImpl(proxy->getUniqueKey(), proxy,
880 InvalidateGPUResource::kNo,
881 RemoveTableEntry::kNo);
882 });
883 // Removing all those table entries is safe now.
884 fUniquelyKeyedProxies.reset();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500885}
Robert Phillipsa41c6852019-02-07 10:44:10 -0500886
887bool GrProxyProvider::renderingDirectly() const {
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400888 return fImageContext->asDirectContext();
Robert Phillipsa41c6852019-02-07 10:44:10 -0500889}