blob: c18c4d6cba3467d05062c7418e8bb3f15e64cc3c [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"
Adlai Hollerdced31f2021-02-19 12:33:46 -050023#include "src/gpu/GrContextThreadSafeProxyPriv.h"
Adlai Hollera0693042020-10-14 11:23:11 -040024#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/GrImageContextPriv.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040026#include "src/gpu/GrRenderTarget.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
Greg Daniel84261652021-09-19 17:53:40 -040036#ifdef SK_VULKAN
37#include "include/gpu/vk/GrVkTypes.h"
38#endif
39
Adlai Holler33dbd652020-06-01 12:35:42 -040040#define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(fImageContext->priv().singleOwner())
Robert Phillips1afd4cd2018-01-08 13:40:32 -050041
Robert Phillipsa9162df2019-02-11 14:12:03 -050042GrProxyProvider::GrProxyProvider(GrImageContext* imageContext) : fImageContext(imageContext) {}
Robert Phillips1afd4cd2018-01-08 13:40:32 -050043
44GrProxyProvider::~GrProxyProvider() {
Robert Phillipsa41c6852019-02-07 10:44:10 -050045 if (this->renderingDirectly()) {
Robert Phillips0790f8a2018-09-18 13:11:03 -040046 // In DDL-mode a proxy provider can still have extant uniquely keyed proxies (since
47 // they need their unique keys to, potentially, find a cached resource when the
48 // DDL is played) but, in non-DDL-mode they should all have been cleaned up by this point.
49 SkASSERT(!fUniquelyKeyedProxies.count());
50 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050051}
52
Robert Phillipsadbe1322018-01-17 13:35:46 -050053bool GrProxyProvider::assignUniqueKeyToProxy(const GrUniqueKey& key, GrTextureProxy* proxy) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050054 ASSERT_SINGLE_OWNER
55 SkASSERT(key.isValid());
56 if (this->isAbandoned() || !proxy) {
Robert Phillipsadbe1322018-01-17 13:35:46 -050057 return false;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050058 }
59
Robert Phillipsf10b2a52020-05-15 10:20:04 -040060 // Only the proxyProvider that created a proxy should be assigning unique keys to it.
61 SkASSERT(this->isDDLProvider() == proxy->creatingProvider());
62
Robert Phillipsa41c6852019-02-07 10:44:10 -050063#ifdef SK_DEBUG
64 {
Robert Phillipsf8f45d92020-07-01 11:11:18 -040065 auto direct = fImageContext->asDirectContext();
Robert Phillipsa41c6852019-02-07 10:44:10 -050066 if (direct) {
67 GrResourceCache* resourceCache = direct->priv().getResourceCache();
68 // If there is already a GrResource with this key then the caller has violated the
69 // normal usage pattern of uniquely keyed resources (e.g., they have created one w/o
70 // first seeing if it already existed in the cache).
71 SkASSERT(!resourceCache->findAndRefUniqueResource(key));
72 }
73 }
74#endif
Robert Phillips1afd4cd2018-01-08 13:40:32 -050075
Robert Phillips1afd4cd2018-01-08 13:40:32 -050076 SkASSERT(!fUniquelyKeyedProxies.find(key)); // multiple proxies can't get the same key
77
78 proxy->cacheAccess().setUniqueKey(this, key);
79 SkASSERT(proxy->getUniqueKey() == key);
80 fUniquelyKeyedProxies.add(proxy);
Robert Phillipsadbe1322018-01-17 13:35:46 -050081 return true;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050082}
83
84void GrProxyProvider::adoptUniqueKeyFromSurface(GrTextureProxy* proxy, const GrSurface* surf) {
85 SkASSERT(surf->getUniqueKey().isValid());
86 proxy->cacheAccess().setUniqueKey(this, surf->getUniqueKey());
87 SkASSERT(proxy->getUniqueKey() == surf->getUniqueKey());
88 // multiple proxies can't get the same key
89 SkASSERT(!fUniquelyKeyedProxies.find(surf->getUniqueKey()));
90 fUniquelyKeyedProxies.add(proxy);
91}
92
Chris Dalton2de13dd2019-01-03 15:11:59 -070093void GrProxyProvider::removeUniqueKeyFromProxy(GrTextureProxy* proxy) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050094 ASSERT_SINGLE_OWNER
Chris Dalton2de13dd2019-01-03 15:11:59 -070095 SkASSERT(proxy);
96 SkASSERT(proxy->getUniqueKey().isValid());
97
98 if (this->isAbandoned()) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050099 return;
100 }
Robert Phillips0790f8a2018-09-18 13:11:03 -0400101
Chris Dalton2de13dd2019-01-03 15:11:59 -0700102 this->processInvalidUniqueKey(proxy->getUniqueKey(), proxy, InvalidateGPUResource::kYes);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500103}
104
Greg Daniel3a365112020-02-14 10:47:18 -0500105sk_sp<GrTextureProxy> GrProxyProvider::findProxyByUniqueKey(const GrUniqueKey& key) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500106 ASSERT_SINGLE_OWNER
107
108 if (this->isAbandoned()) {
109 return nullptr;
110 }
111
Brian Salomon01ceae92019-04-02 11:49:54 -0400112 GrTextureProxy* proxy = fUniquelyKeyedProxies.find(key);
Brian Salomon01ceae92019-04-02 11:49:54 -0400113 if (proxy) {
Robert Phillipse5f73282019-06-18 17:15:04 -0400114 return sk_ref_sp(proxy);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500115 }
Robert Phillipse5f73282019-06-18 17:15:04 -0400116 return nullptr;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500117}
118
Robert Phillipsa41c6852019-02-07 10:44:10 -0500119///////////////////////////////////////////////////////////////////////////////
120
121#if GR_TEST_UTILS
122sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createInstantiatedProxy(
Brian Salomona56a7462020-02-07 14:17:25 -0500123 SkISize dimensions,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400124 const GrBackendFormat& format,
125 GrRenderable renderable,
126 int renderTargetSampleCnt,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400127 SkBackingFit fit,
128 SkBudgeted budgeted,
129 GrProtected isProtected) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500130 ASSERT_SINGLE_OWNER
131 if (this->isAbandoned()) {
132 return nullptr;
133 }
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400134 auto direct = fImageContext->asDirectContext();
Robert Phillipsa41c6852019-02-07 10:44:10 -0500135 if (!direct) {
136 return nullptr;
137 }
138
Brian Salomon4eb38b72019-08-05 12:58:39 -0400139 if (this->caps()->isFormatCompressed(format)) {
140 // TODO: Allow this to go to GrResourceProvider::createCompressedTexture() once we no longer
Greg Daniel4cb29332020-01-23 10:07:02 -0500141 // rely on GrColorType to get a swizzle for the proxy.
Brian Salomon4eb38b72019-08-05 12:58:39 -0400142 return nullptr;
143 }
Brian Salomon4eb38b72019-08-05 12:58:39 -0400144
Robert Phillipsa41c6852019-02-07 10:44:10 -0500145 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
146 sk_sp<GrTexture> tex;
147
148 if (SkBackingFit::kApprox == fit) {
Greg Daniel0e9d34d2021-08-13 16:20:18 -0400149 tex = resourceProvider->createApproxTexture(dimensions,
150 format,
151 format.textureType(),
152 renderable,
153 renderTargetSampleCnt,
154 isProtected);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500155 } else {
Greg Daniel0e9d34d2021-08-13 16:20:18 -0400156 tex = resourceProvider->createTexture(dimensions,
157 format,
158 format.textureType(),
159 renderable,
160 renderTargetSampleCnt,
161 GrMipmapped::kNo,
162 budgeted,
163 isProtected);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500164 }
165 if (!tex) {
166 return nullptr;
167 }
168
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400169 return this->createWrapped(std::move(tex), UseAllocator::kYes);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500170}
171
Brian Salomon4eb38b72019-08-05 12:58:39 -0400172sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createInstantiatedProxy(
Brian Salomona56a7462020-02-07 14:17:25 -0500173 SkISize dimensions,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400174 GrColorType colorType,
175 GrRenderable renderable,
176 int renderTargetSampleCnt,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400177 SkBackingFit fit,
178 SkBudgeted budgeted,
179 GrProtected isProtected) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500180 ASSERT_SINGLE_OWNER
181 if (this->isAbandoned()) {
182 return nullptr;
183 }
Brian Salomon4eb38b72019-08-05 12:58:39 -0400184 auto format = this->caps()->getDefaultBackendFormat(colorType, renderable);
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400185 return this->testingOnly_createInstantiatedProxy(dimensions,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400186 format,
187 renderable,
188 renderTargetSampleCnt,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400189 fit,
190 budgeted,
191 isProtected);
192}
193
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400194sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createWrapped(sk_sp<GrTexture> tex) {
195 return this->createWrapped(std::move(tex), UseAllocator::kYes);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500196}
197#endif
198
Brian Salomonbeb7f522019-08-30 16:19:42 -0400199sk_sp<GrTextureProxy> GrProxyProvider::createWrapped(sk_sp<GrTexture> tex,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400200 UseAllocator useAllocator) {
Robert Phillipsadbe1322018-01-17 13:35:46 -0500201#ifdef SK_DEBUG
202 if (tex->getUniqueKey().isValid()) {
Greg Daniel3a365112020-02-14 10:47:18 -0500203 SkASSERT(!this->findProxyByUniqueKey(tex->getUniqueKey()));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500204 }
205#endif
206
207 if (tex->asRenderTarget()) {
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400208 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), useAllocator,
209 this->isDDLProvider()));
Greg Daniel3a365112020-02-14 10:47:18 -0500210 } else {
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400211 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), useAllocator,
212 this->isDDLProvider()));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500213 }
214}
215
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500216sk_sp<GrTextureProxy> GrProxyProvider::findOrCreateProxyByUniqueKey(const GrUniqueKey& key,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400217 UseAllocator useAllocator) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500218 ASSERT_SINGLE_OWNER
219
220 if (this->isAbandoned()) {
221 return nullptr;
222 }
223
Greg Daniel3a365112020-02-14 10:47:18 -0500224 sk_sp<GrTextureProxy> result = this->findProxyByUniqueKey(key);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500225 if (result) {
226 return result;
227 }
228
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400229 auto direct = fImageContext->asDirectContext();
Robert Phillipsa41c6852019-02-07 10:44:10 -0500230 if (!direct) {
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500231 return nullptr;
232 }
233
Robert Phillipsa41c6852019-02-07 10:44:10 -0500234 GrResourceCache* resourceCache = direct->priv().getResourceCache();
235
236 GrGpuResource* resource = resourceCache->findAndRefUniqueResource(key);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500237 if (!resource) {
238 return nullptr;
239 }
240
241 sk_sp<GrTexture> texture(static_cast<GrSurface*>(resource)->asTexture());
242 SkASSERT(texture);
243
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400244 result = this->createWrapped(std::move(texture), useAllocator);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500245 SkASSERT(result->getUniqueKey() == key);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500246 // createWrapped should've added this for us
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500247 SkASSERT(fUniquelyKeyedProxies.find(key));
248 return result;
249}
250
Brian Salomond005b692020-04-01 15:47:05 -0400251GrSurfaceProxyView GrProxyProvider::findCachedProxyWithColorTypeFallback(const GrUniqueKey& key,
252 GrSurfaceOrigin origin,
Brian Salomon0029db02020-04-03 10:41:24 -0400253 GrColorType ct,
254 int sampleCnt) {
Brian Salomond005b692020-04-01 15:47:05 -0400255 auto proxy = this->findOrCreateProxyByUniqueKey(key);
256 if (!proxy) {
257 return {};
258 }
Robert Phillips87b57fa2021-06-08 13:10:04 -0400259 const GrCaps* caps = fImageContext->priv().caps();
260
Brian Salomond005b692020-04-01 15:47:05 -0400261 // Assume that we used a fallback color type if and only if the proxy is renderable.
262 if (proxy->asRenderTargetProxy()) {
263 GrBackendFormat expectedFormat;
Robert Phillips87b57fa2021-06-08 13:10:04 -0400264 std::tie(ct, expectedFormat) = caps->getFallbackColorTypeAndFormat(ct, sampleCnt);
Brian Salomond005b692020-04-01 15:47:05 -0400265 SkASSERT(expectedFormat == proxy->backendFormat());
266 }
Robert Phillips87b57fa2021-06-08 13:10:04 -0400267 GrSwizzle swizzle = caps->getReadSwizzle(proxy->backendFormat(), ct);
Brian Salomond005b692020-04-01 15:47:05 -0400268 return {std::move(proxy), origin, swizzle};
269}
270
Brian Osmande496652019-03-22 13:42:33 -0400271sk_sp<GrTextureProxy> GrProxyProvider::createProxyFromBitmap(const SkBitmap& bitmap,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400272 GrMipmapped mipMapped,
Brian Salomonbc074a62020-03-18 10:06:13 -0400273 SkBackingFit fit,
274 SkBudgeted budgeted) {
Brian Osman412674f2019-02-07 15:34:58 -0500275 ASSERT_SINGLE_OWNER
Brian Salomon7e67dca2020-07-21 09:27:25 -0400276 SkASSERT(fit == SkBackingFit::kExact || mipMapped == GrMipmapped::kNo);
Brian Osman412674f2019-02-07 15:34:58 -0500277
278 if (this->isAbandoned()) {
279 return nullptr;
280 }
281
Brian Osman2b23c4b2018-06-01 12:25:08 -0400282 if (!SkImageInfoIsValid(bitmap.info())) {
Greg Daniela4ead652018-02-07 10:21:48 -0500283 return nullptr;
284 }
285
Brian Osmande496652019-03-22 13:42:33 -0400286 ATRACE_ANDROID_FRAMEWORK("Upload %sTexture [%ux%u]",
Brian Salomon7e67dca2020-07-21 09:27:25 -0400287 GrMipmapped::kYes == mipMapped ? "MipMap " : "",
Brian Osmande496652019-03-22 13:42:33 -0400288 bitmap.width(), bitmap.height());
Greg Daniela4ead652018-02-07 10:21:48 -0500289
290 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
291 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
292 // 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 -0500293 SkBitmap copyBitmap = bitmap;
294 if (!this->renderingDirectly() && !bitmap.isImmutable()) {
295 copyBitmap.allocPixels();
296 if (!bitmap.readPixels(copyBitmap.pixmap())) {
297 return nullptr;
298 }
299 copyBitmap.setImmutable();
Greg Daniela4ead652018-02-07 10:21:48 -0500300 }
301
Greg Daniel6f5441a2020-01-28 17:02:49 -0500302 sk_sp<GrTextureProxy> proxy;
Brian Salomon7e67dca2020-07-21 09:27:25 -0400303 if (mipMapped == GrMipmapped::kNo ||
Mike Reed13711eb2020-07-14 17:16:32 -0400304 0 == SkMipmap::ComputeLevelCount(copyBitmap.width(), copyBitmap.height())) {
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400305 proxy = this->createNonMippedProxyFromBitmap(copyBitmap, fit, budgeted);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500306 } else {
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400307 proxy = this->createMippedProxyFromBitmap(copyBitmap, budgeted);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500308 }
309
310 if (!proxy) {
311 return nullptr;
312 }
313
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400314 auto direct = fImageContext->asDirectContext();
Greg Daniel6f5441a2020-01-28 17:02:49 -0500315 if (direct) {
316 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
317
318 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
319 // we're better off instantiating the proxy immediately here.
320 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
321 return nullptr;
322 }
323 }
324 return proxy;
325}
326
327sk_sp<GrTextureProxy> GrProxyProvider::createNonMippedProxyFromBitmap(const SkBitmap& bitmap,
328 SkBackingFit fit,
Brian Salomonbc074a62020-03-18 10:06:13 -0400329 SkBudgeted budgeted) {
Brian Salomona56a7462020-02-07 14:17:25 -0500330 auto dims = bitmap.dimensions();
Greg Daniel6f5441a2020-01-28 17:02:49 -0500331
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400332 auto colorType = SkColorTypeToGrColorType(bitmap.colorType());
333 GrBackendFormat format = this->caps()->getDefaultBackendFormat(colorType, GrRenderable::kNo);
334 if (!format.isValid()) {
335 return nullptr;
336 }
337
Greg Daniel6f5441a2020-01-28 17:02:49 -0500338 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomon63410e92020-03-23 18:32:50 -0400339 [bitmap](GrResourceProvider* resourceProvider, const LazySurfaceDesc& desc) {
Brian Salomon40a40622020-07-21 10:32:07 -0400340 SkASSERT(desc.fMipmapped == GrMipmapped::kNo);
Brian Salomon2c673402021-04-02 14:36:58 -0400341 GrMipLevel mipLevel = {bitmap.getPixels(), bitmap.rowBytes(), nullptr};
Brian Salomon63410e92020-03-23 18:32:50 -0400342 auto colorType = SkColorTypeToGrColorType(bitmap.colorType());
Greg Daniel6f5441a2020-01-28 17:02:49 -0500343 return LazyCallbackResult(resourceProvider->createTexture(
Greg Daniel0e9d34d2021-08-13 16:20:18 -0400344 desc.fDimensions,
345 desc.fFormat,
Greg Daniele56d31f2021-08-16 09:29:56 -0400346 desc.fTextureType,
Greg Daniel0e9d34d2021-08-13 16:20:18 -0400347 colorType,
348 desc.fRenderable,
349 desc.fSampleCnt,
350 desc.fBudgeted,
351 desc.fFit,
352 desc.fProtected,
353 mipLevel));
Greg Daniel6f5441a2020-01-28 17:02:49 -0500354 },
Greg Danielc113f002020-08-26 13:28:22 -0400355 format, dims, GrMipmapped::kNo, GrMipmapStatus::kNotAllocated,
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400356 GrInternalSurfaceFlags::kNone, fit, budgeted, GrProtected::kNo, UseAllocator::kYes);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500357
358 if (!proxy) {
359 return nullptr;
360 }
Brian Salomona56a7462020-02-07 14:17:25 -0500361 SkASSERT(proxy->dimensions() == bitmap.dimensions());
Greg Daniel6f5441a2020-01-28 17:02:49 -0500362 return proxy;
363}
364
365sk_sp<GrTextureProxy> GrProxyProvider::createMippedProxyFromBitmap(const SkBitmap& bitmap,
Brian Salomonbc074a62020-03-18 10:06:13 -0400366 SkBudgeted budgeted) {
Brian Salomon69100f02020-07-21 10:49:25 -0400367 SkASSERT(this->caps()->mipmapSupport());
Greg Daniel6f5441a2020-01-28 17:02:49 -0500368
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400369 auto colorType = SkColorTypeToGrColorType(bitmap.colorType());
370 GrBackendFormat format = this->caps()->getDefaultBackendFormat(colorType, GrRenderable::kNo);
371 if (!format.isValid()) {
372 return nullptr;
373 }
Greg Daniel6f5441a2020-01-28 17:02:49 -0500374
Mike Reed13711eb2020-07-14 17:16:32 -0400375 sk_sp<SkMipmap> mipmaps(SkMipmap::Build(bitmap.pixmap(), nullptr));
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400376 if (!mipmaps) {
377 return nullptr;
378 }
379
Brian Salomona56a7462020-02-07 14:17:25 -0500380 auto dims = bitmap.dimensions();
Greg Danielce3ddaa2020-01-22 16:58:15 -0500381
Greg Daniela4ead652018-02-07 10:21:48 -0500382 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomon63410e92020-03-23 18:32:50 -0400383 [bitmap, mipmaps](GrResourceProvider* resourceProvider, const LazySurfaceDesc& desc) {
Brian Osman1b97f132018-09-13 17:33:48 +0000384 const int mipLevelCount = mipmaps->countLevels() + 1;
385 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
Brian Salomon63410e92020-03-23 18:32:50 -0400386 auto colorType = SkColorTypeToGrColorType(bitmap.colorType());
Brian Osman1b97f132018-09-13 17:33:48 +0000387
Greg Daniel6f5441a2020-01-28 17:02:49 -0500388 texels[0].fPixels = bitmap.getPixels();
389 texels[0].fRowBytes = bitmap.rowBytes();
Greg Daniela4ead652018-02-07 10:21:48 -0500390
Greg Daniela4ead652018-02-07 10:21:48 -0500391 for (int i = 1; i < mipLevelCount; ++i) {
Mike Reed13711eb2020-07-14 17:16:32 -0400392 SkMipmap::Level generatedMipLevel;
Greg Daniela4ead652018-02-07 10:21:48 -0500393 mipmaps->getLevel(i - 1, &generatedMipLevel);
394 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
395 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
396 SkASSERT(texels[i].fPixels);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500397 SkASSERT(generatedMipLevel.fPixmap.colorType() == bitmap.colorType());
Greg Daniela4ead652018-02-07 10:21:48 -0500398 }
Brian Salomonbeb7f522019-08-30 16:19:42 -0400399 return LazyCallbackResult(resourceProvider->createTexture(
Greg Daniel0e9d34d2021-08-13 16:20:18 -0400400 desc.fDimensions,
401 desc.fFormat,
Greg Daniele56d31f2021-08-16 09:29:56 -0400402 desc.fTextureType,
Greg Daniel0e9d34d2021-08-13 16:20:18 -0400403 colorType,
404 GrRenderable::kNo,
405 1,
406 desc.fBudgeted,
407 GrMipMapped::kYes,
408 GrProtected::kNo,
409 texels.get()));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500410 },
Greg Danielc113f002020-08-26 13:28:22 -0400411 format, dims, GrMipmapped::kYes, GrMipmapStatus::kValid, GrInternalSurfaceFlags::kNone,
412 SkBackingFit::kExact, budgeted, GrProtected::kNo, UseAllocator::kYes);
Greg Daniela4ead652018-02-07 10:21:48 -0500413
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400414 if (!proxy) {
415 return nullptr;
416 }
417
Brian Salomona56a7462020-02-07 14:17:25 -0500418 SkASSERT(proxy->dimensions() == bitmap.dimensions());
Greg Daniel6f5441a2020-01-28 17:02:49 -0500419
Greg Daniela4ead652018-02-07 10:21:48 -0500420 return proxy;
421}
422
Greg Daniel4065d452018-11-16 15:43:41 -0500423sk_sp<GrTextureProxy> GrProxyProvider::createProxy(const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -0500424 SkISize dimensions,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400425 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400426 int renderTargetSampleCnt,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400427 GrMipmapped mipMapped,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500428 SkBackingFit fit,
429 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -0400430 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400431 GrInternalSurfaceFlags surfaceFlags,
432 GrSurfaceProxy::UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500433 ASSERT_SINGLE_OWNER
434 if (this->isAbandoned()) {
435 return nullptr;
436 }
437
Robert Phillips8ff8bcc2019-07-29 17:03:35 -0400438 const GrCaps* caps = this->caps();
439
440 if (caps->isFormatCompressed(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400441 // Deferred proxies for compressed textures are not supported.
442 return nullptr;
443 }
Robert Phillips0902c982019-07-16 07:47:56 -0400444
Brian Salomon7e67dca2020-07-21 09:27:25 -0400445 if (GrMipmapped::kYes == mipMapped) {
Mike Reed13711eb2020-07-14 17:16:32 -0400446 // SkMipmap doesn't include the base level in the level count so we have to add 1
447 int mipCount = SkMipmap::ComputeLevelCount(dimensions.fWidth, dimensions.fHeight) + 1;
Greg Danielf6f7b672018-02-15 13:06:26 -0500448 if (1 == mipCount) {
Brian Salomon7e67dca2020-07-21 09:27:25 -0400449 mipMapped = GrMipmapped::kNo;
Greg Danielf6f7b672018-02-15 13:06:26 -0500450 }
451 }
452
Greg Daniel0e9d34d2021-08-13 16:20:18 -0400453 if (!caps->validateSurfaceParams(dimensions,
454 format,
455 renderable,
456 renderTargetSampleCnt,
457 mipMapped,
458 GrTextureType::k2D)) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500459 return nullptr;
460 }
Brian Salomona6db5102020-07-21 09:56:23 -0400461 GrMipmapStatus mipmapStatus = (GrMipmapped::kYes == mipMapped)
462 ? GrMipmapStatus::kDirty
463 : GrMipmapStatus::kNotAllocated;
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400464 if (renderable == GrRenderable::kYes) {
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400465 renderTargetSampleCnt = caps->getRenderTargetSampleCount(renderTargetSampleCnt, format);
Greg Daniel6fa62e22019-08-07 15:52:37 -0400466 SkASSERT(renderTargetSampleCnt);
Greg Daniel638b2e82020-08-27 14:29:00 -0400467 GrInternalSurfaceFlags extraFlags = caps->getExtraSurfaceFlagsForDeferredRT();
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500468 // We know anything we instantiate later from this deferred path will be
469 // both texturable and renderable
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400470 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
Brian Salomona6db5102020-07-21 09:56:23 -0400471 *caps, format, dimensions, renderTargetSampleCnt, mipMapped, mipmapStatus, fit,
Greg Daniel638b2e82020-08-27 14:29:00 -0400472 budgeted, isProtected, surfaceFlags | extraFlags, useAllocator,
473 this->isDDLProvider()));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500474 }
475
Brian Salomona6db5102020-07-21 09:56:23 -0400476 return sk_sp<GrTextureProxy>(new GrTextureProxy(format, dimensions, mipMapped, mipmapStatus,
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400477 fit, budgeted, isProtected, surfaceFlags,
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400478 useAllocator, this->isDDLProvider()));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500479}
480
Brian Salomonbb8dde82019-06-27 10:52:13 -0400481sk_sp<GrTextureProxy> GrProxyProvider::createCompressedTextureProxy(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400482 SkISize dimensions, SkBudgeted budgeted, GrMipmapped mipMapped, GrProtected isProtected,
Robert Phillipse4720c62020-01-14 14:33:24 -0500483 SkImage::CompressionType compressionType, sk_sp<SkData> data) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500484 ASSERT_SINGLE_OWNER
485 if (this->isAbandoned()) {
486 return nullptr;
487 }
Brian Salomonbb8dde82019-06-27 10:52:13 -0400488
Robert Phillips8ff8bcc2019-07-29 17:03:35 -0400489 GrBackendFormat format = this->caps()->getBackendFormatFromCompressionType(compressionType);
490
Greg Daniel0e9d34d2021-08-13 16:20:18 -0400491 if (!this->caps()->isFormatTexturable(format, GrTextureType::k2D)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500492 return nullptr;
493 }
494
Brian Salomona6db5102020-07-21 09:56:23 -0400495 GrMipmapStatus mipmapStatus = (GrMipmapped::kYes == mipMapped) ? GrMipmapStatus::kValid
496 : GrMipmapStatus::kNotAllocated;
Robert Phillipse4720c62020-01-14 14:33:24 -0500497
Jim Van Verthee06b332019-01-18 10:36:32 -0500498 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomon63410e92020-03-23 18:32:50 -0400499 [data](GrResourceProvider* resourceProvider, const LazySurfaceDesc& desc) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400500 return LazyCallbackResult(resourceProvider->createCompressedTexture(
Brian Salomon40a40622020-07-21 10:32:07 -0400501 desc.fDimensions, desc.fFormat, desc.fBudgeted, desc.fMipmapped,
Brian Salomon63410e92020-03-23 18:32:50 -0400502 desc.fProtected, data.get()));
Brian Salomonbb8dde82019-06-27 10:52:13 -0400503 },
Greg Danielc113f002020-08-26 13:28:22 -0400504 format, dimensions, mipMapped, mipmapStatus,GrInternalSurfaceFlags::kReadOnly,
505 SkBackingFit::kExact, SkBudgeted::kYes, GrProtected::kNo, UseAllocator::kYes);
Jim Van Verthee06b332019-01-18 10:36:32 -0500506
507 if (!proxy) {
508 return nullptr;
509 }
510
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400511 auto direct = fImageContext->asDirectContext();
Robert Phillipsa41c6852019-02-07 10:44:10 -0500512 if (direct) {
513 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Jim Van Verthee06b332019-01-18 10:36:32 -0500514 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
515 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500516 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500517 return nullptr;
518 }
519 }
520 return proxy;
521}
522
Brian Salomon7578f3e2018-03-07 14:39:54 -0500523sk_sp<GrTextureProxy> GrProxyProvider::wrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500524 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500525 GrWrapCacheable cacheable,
Brian Salomonc67c31c2018-12-06 10:00:03 -0500526 GrIOType ioType,
Robert Phillipse22c5ca2020-06-19 12:29:57 -0400527 sk_sp<GrRefCntedCallback> releaseHelper) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500528 SkASSERT(ioType != kWrite_GrIOType);
Robert Phillipse22c5ca2020-06-19 12:29:57 -0400529
Robert Phillipsf9bec202018-01-16 09:21:01 -0500530 if (this->isAbandoned()) {
531 return nullptr;
532 }
533
Brian Salomonf7778972018-03-08 10:13:17 -0500534 // This is only supported on a direct GrContext.
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400535 auto direct = fImageContext->asDirectContext();
Robert Phillipsa41c6852019-02-07 10:44:10 -0500536 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500537 return nullptr;
538 }
539
Robert Phillipsa41c6852019-02-07 10:44:10 -0500540 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
541
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500542 sk_sp<GrTexture> tex =
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400543 resourceProvider->wrapBackendTexture(backendTex, ownership, cacheable, ioType);
Brian Salomonf7778972018-03-08 10:13:17 -0500544 if (!tex) {
545 return nullptr;
546 }
Robert Phillipsadbe1322018-01-17 13:35:46 -0500547
Robert Phillipse22c5ca2020-06-19 12:29:57 -0400548 if (releaseHelper) {
549 tex->setRelease(std::move(releaseHelper));
Greg Daniel6a0176b2018-01-30 09:28:44 -0500550 }
551
Brian Salomonf7778972018-03-08 10:13:17 -0500552 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
553 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500554 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500555
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400556 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), UseAllocator::kNo,
557 this->isDDLProvider()));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500558}
559
Robert Phillipsa1121332020-06-29 13:05:29 -0400560sk_sp<GrTextureProxy> GrProxyProvider::wrapCompressedBackendTexture(
561 const GrBackendTexture& beTex,
562 GrWrapOwnership ownership,
563 GrWrapCacheable cacheable,
564 sk_sp<GrRefCntedCallback> releaseHelper) {
Robert Phillipsead321b2019-12-19 10:16:32 -0500565 if (this->isAbandoned()) {
566 return nullptr;
567 }
568
569 // This is only supported on a direct GrContext.
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400570 auto direct = fImageContext->asDirectContext();
Robert Phillipsead321b2019-12-19 10:16:32 -0500571 if (!direct) {
572 return nullptr;
573 }
574
575 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
576
577 sk_sp<GrTexture> tex = resourceProvider->wrapCompressedBackendTexture(beTex, ownership,
578 cacheable);
579 if (!tex) {
580 return nullptr;
581 }
582
Robert Phillipsa1121332020-06-29 13:05:29 -0400583 if (releaseHelper) {
584 tex->setRelease(std::move(releaseHelper));
Robert Phillipsead321b2019-12-19 10:16:32 -0500585 }
586
587 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
588 // Make sure we match how we created the proxy with SkBudgeted::kNo
589 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
590
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400591 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), UseAllocator::kNo,
592 this->isDDLProvider()));
Robert Phillipsead321b2019-12-19 10:16:32 -0500593}
594
Brian Salomon7578f3e2018-03-07 14:39:54 -0500595sk_sp<GrTextureProxy> GrProxyProvider::wrapRenderableBackendTexture(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400596 const GrBackendTexture& backendTex,
597 int sampleCnt,
598 GrWrapOwnership ownership,
599 GrWrapCacheable cacheable,
Robert Phillipsa1121332020-06-29 13:05:29 -0400600 sk_sp<GrRefCntedCallback> releaseHelper) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500601 if (this->isAbandoned()) {
602 return nullptr;
603 }
604
Brian Salomonf7778972018-03-08 10:13:17 -0500605 // This is only supported on a direct GrContext.
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400606 auto direct = fImageContext->asDirectContext();
Robert Phillipsa41c6852019-02-07 10:44:10 -0500607 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500608 return nullptr;
609 }
610
Robert Phillips0902c982019-07-16 07:47:56 -0400611 const GrCaps* caps = this->caps();
612
Robert Phillipsa41c6852019-02-07 10:44:10 -0500613 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
614
Greg Daniel6fa62e22019-08-07 15:52:37 -0400615 sampleCnt = caps->getRenderTargetSampleCount(sampleCnt, backendTex.getBackendFormat());
616 SkASSERT(sampleCnt);
617
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400618 sk_sp<GrTexture> tex = resourceProvider->wrapRenderableBackendTexture(
619 backendTex, sampleCnt, ownership, cacheable);
Brian Salomonf7778972018-03-08 10:13:17 -0500620 if (!tex) {
621 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500622 }
623
Robert Phillipsa1121332020-06-29 13:05:29 -0400624 if (releaseHelper) {
625 tex->setRelease(std::move(releaseHelper));
Greg Daniel8ce79912019-02-05 10:08:43 -0500626 }
627
Brian Salomonf7778972018-03-08 10:13:17 -0500628 SkASSERT(tex->asRenderTarget()); // A GrTextureRenderTarget
629 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500630 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Greg Daniel6abda432018-02-15 14:55:00 -0500631
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400632 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), UseAllocator::kNo,
633 this->isDDLProvider()));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500634}
635
Brian Salomon7578f3e2018-03-07 14:39:54 -0500636sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400637 const GrBackendRenderTarget& backendRT,
Robert Phillipsa1121332020-06-29 13:05:29 -0400638 sk_sp<GrRefCntedCallback> releaseHelper) {
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 Phillipsf8f45d92020-07-01 11:11:18 -0400644 auto direct = fImageContext->asDirectContext();
Robert Phillipsa41c6852019-02-07 10:44:10 -0500645 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500646 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500647 }
648
Robert Phillipsa41c6852019-02-07 10:44:10 -0500649 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
650
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400651 sk_sp<GrRenderTarget> rt = resourceProvider->wrapBackendRenderTarget(backendRT);
Brian Salomonf7778972018-03-08 10:13:17 -0500652 if (!rt) {
653 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500654 }
Greg Daniel8ce79912019-02-05 10:08:43 -0500655
Robert Phillipsa1121332020-06-29 13:05:29 -0400656 if (releaseHelper) {
657 rt->setRelease(std::move(releaseHelper));
Greg Daniel8ce79912019-02-05 10:08:43 -0500658 }
659
Brian Salomonf7778972018-03-08 10:13:17 -0500660 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
661 SkASSERT(!rt->getUniqueKey().isValid());
662 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500663 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Brian Salomonf7778972018-03-08 10:13:17 -0500664
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400665 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(std::move(rt), UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500666}
667
Greg Daniel84261652021-09-19 17:53:40 -0400668#ifdef SK_VULKAN
Greg Danielb46add82019-01-02 14:51:29 -0500669sk_sp<GrRenderTargetProxy> GrProxyProvider::wrapVulkanSecondaryCBAsRenderTarget(
670 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
671 if (this->isAbandoned()) {
672 return nullptr;
673 }
674
675 // This is only supported on a direct GrContext.
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400676 auto direct = fImageContext->asDirectContext();
Robert Phillipsa41c6852019-02-07 10:44:10 -0500677 if (!direct) {
Greg Danielb46add82019-01-02 14:51:29 -0500678 return nullptr;
679 }
680
Robert Phillipsa41c6852019-02-07 10:44:10 -0500681 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Danielb46add82019-01-02 14:51:29 -0500682
Robert Phillipsa41c6852019-02-07 10:44:10 -0500683 sk_sp<GrRenderTarget> rt = resourceProvider->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
684 vkInfo);
Greg Danielb46add82019-01-02 14:51:29 -0500685 if (!rt) {
686 return nullptr;
687 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500688
Greg Danielb46add82019-01-02 14:51:29 -0500689 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
690 SkASSERT(!rt->getUniqueKey().isValid());
691 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500692 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielb46add82019-01-02 14:51:29 -0500693
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400694 GrColorType colorType = SkColorTypeToGrColorType(imageInfo.colorType());
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400695
Greg Danielde4bbdb2021-04-13 14:23:23 -0400696 if (!this->caps()->isFormatAsColorTypeRenderable(
697 colorType, GrBackendFormat::MakeVk(vkInfo.fFormat), /*sampleCount=*/1)) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400698 return nullptr;
699 }
700
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400701 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
702 std::move(rt), UseAllocator::kNo, GrRenderTargetProxy::WrapsVkSecondaryCB::kYes));
Brian Salomone8a766b2019-07-19 14:24:36 -0400703}
Greg Daniel84261652021-09-19 17:53:40 -0400704#else
705sk_sp<GrRenderTargetProxy> GrProxyProvider::wrapVulkanSecondaryCBAsRenderTarget(
706 const SkImageInfo&, const GrVkDrawableInfo&) {
707 return nullptr;
708}
709#endif
Brian Salomone8a766b2019-07-19 14:24:36 -0400710
Adlai Hollerdced31f2021-02-19 12:33:46 -0500711sk_sp<GrTextureProxy> GrProxyProvider::CreatePromiseProxy(GrContextThreadSafeProxy* threadSafeProxy,
712 LazyInstantiateCallback&& callback,
713 const GrBackendFormat& format,
714 SkISize dimensions,
715 GrMipmapped mipMapped) {
716 if (threadSafeProxy->priv().abandoned()) {
717 return nullptr;
718 }
719 SkASSERT((dimensions.fWidth <= 0 && dimensions.fHeight <= 0) ||
720 (dimensions.fWidth > 0 && dimensions.fHeight > 0));
721
722 if (dimensions.fWidth > threadSafeProxy->priv().caps()->maxTextureSize() ||
723 dimensions.fHeight > threadSafeProxy->priv().caps()->maxTextureSize()) {
724 return nullptr;
725 }
726 // Ganesh assumes that, when wrapping a mipmapped backend texture from a client, that its
727 // mipmaps are fully fleshed out.
728 GrMipmapStatus mipmapStatus = (GrMipmapped::kYes == mipMapped) ? GrMipmapStatus::kValid
729 : GrMipmapStatus::kNotAllocated;
730
731 // We pass kReadOnly here since we should treat content of the client's texture as immutable.
732 // The promise API provides no way for the client to indicate that the texture is protected.
Adlai Holler55aaefe2021-03-03 16:12:56 -0700733 auto proxy = sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(callback),
734 format,
735 dimensions,
736 mipMapped,
737 mipmapStatus,
738 SkBackingFit::kExact,
739 SkBudgeted::kNo,
740 GrProtected::kNo,
741 GrInternalSurfaceFlags::kReadOnly,
742 GrSurfaceProxy::UseAllocator::kYes,
743 GrDDLProvider::kYes));
744 proxy->priv().setIsPromiseProxy();
745 return proxy;
Adlai Hollerdced31f2021-02-19 12:33:46 -0500746}
747
Brian Salomone8a766b2019-07-19 14:24:36 -0400748sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
749 const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -0500750 SkISize dimensions,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400751 GrMipmapped mipMapped,
Brian Salomona6db5102020-07-21 09:56:23 -0400752 GrMipmapStatus mipmapStatus,
Brian Salomone8a766b2019-07-19 14:24:36 -0400753 GrInternalSurfaceFlags surfaceFlags,
754 SkBackingFit fit,
755 SkBudgeted budgeted,
756 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400757 GrSurfaceProxy::UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500758 ASSERT_SINGLE_OWNER
759 if (this->isAbandoned()) {
760 return nullptr;
761 }
Brian Salomona56a7462020-02-07 14:17:25 -0500762 SkASSERT((dimensions.fWidth <= 0 && dimensions.fHeight <= 0) ||
763 (dimensions.fWidth > 0 && dimensions.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400764
Brian Salomonbd3792d2020-11-10 14:17:58 -0500765 if (!format.isValid() || format.backend() != fImageContext->backend()) {
Robert Phillips0a15cc62019-07-30 12:49:10 -0400766 return nullptr;
767 }
768
Brian Salomona56a7462020-02-07 14:17:25 -0500769 if (dimensions.fWidth > this->caps()->maxTextureSize() ||
770 dimensions.fHeight > this->caps()->maxTextureSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400771 return nullptr;
772 }
773
Greg Danielc113f002020-08-26 13:28:22 -0400774 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(callback),
775 format,
776 dimensions,
777 mipMapped,
778 mipmapStatus,
779 fit,
780 budgeted,
781 isProtected,
782 surfaceFlags,
783 useAllocator,
784 this->isDDLProvider()));
Robert Phillips777707b2018-01-17 11:40:14 -0500785}
786
Robert Phillipse8fabb22018-02-04 14:33:21 -0500787sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy(
Brian Salomonbeb7f522019-08-30 16:19:42 -0400788 LazyInstantiateCallback&& callback,
789 const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -0500790 SkISize dimensions,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400791 int sampleCnt,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400792 GrInternalSurfaceFlags surfaceFlags,
793 const TextureInfo* textureInfo,
Brian Salomona6db5102020-07-21 09:56:23 -0400794 GrMipmapStatus mipmapStatus,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400795 SkBackingFit fit,
796 SkBudgeted budgeted,
797 GrProtected isProtected,
798 bool wrapsVkSecondaryCB,
799 UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500800 ASSERT_SINGLE_OWNER
801 if (this->isAbandoned()) {
802 return nullptr;
803 }
Brian Salomona56a7462020-02-07 14:17:25 -0500804 SkASSERT((dimensions.fWidth <= 0 && dimensions.fHeight <= 0) ||
805 (dimensions.fWidth > 0 && dimensions.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400806
Brian Salomona56a7462020-02-07 14:17:25 -0500807 if (dimensions.fWidth > this->caps()->maxRenderTargetSize() ||
808 dimensions.fHeight > this->caps()->maxRenderTargetSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400809 return nullptr;
810 }
811
Brian Salomon7226c232018-07-30 13:13:17 -0400812 if (textureInfo) {
Greg Danielb085fa92019-03-05 16:55:12 -0500813 // Wrapped vulkan secondary command buffers don't support texturing since we won't have an
814 // actual VkImage to texture from.
815 SkASSERT(!wrapsVkSecondaryCB);
Brian Salomon7226c232018-07-30 13:13:17 -0400816 return sk_sp<GrRenderTargetProxy>(new GrTextureRenderTargetProxy(
Greg Daniel3a365112020-02-14 10:47:18 -0500817 *this->caps(), std::move(callback), format, dimensions, sampleCnt,
Brian Salomon40a40622020-07-21 10:32:07 -0400818 textureInfo->fMipmapped, mipmapStatus, fit, budgeted, isProtected, surfaceFlags,
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400819 useAllocator, this->isDDLProvider()));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500820 }
821
Greg Danielb085fa92019-03-05 16:55:12 -0500822 GrRenderTargetProxy::WrapsVkSecondaryCB vkSCB =
823 wrapsVkSecondaryCB ? GrRenderTargetProxy::WrapsVkSecondaryCB::kYes
824 : GrRenderTargetProxy::WrapsVkSecondaryCB::kNo;
825
Greg Daniel3a365112020-02-14 10:47:18 -0500826 return sk_sp<GrRenderTargetProxy>(
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400827 new GrRenderTargetProxy(std::move(callback), format, dimensions, sampleCnt, fit,
828 budgeted, isProtected, surfaceFlags, useAllocator, vkSCB));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500829}
830
Brian Salomonbeb7f522019-08-30 16:19:42 -0400831sk_sp<GrTextureProxy> GrProxyProvider::MakeFullyLazyProxy(LazyInstantiateCallback&& callback,
832 const GrBackendFormat& format,
833 GrRenderable renderable,
834 int renderTargetSampleCnt,
835 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400836 const GrCaps& caps,
837 UseAllocator useAllocator) {
Robert Phillips0a15cc62019-07-30 12:49:10 -0400838 if (!format.isValid()) {
839 return nullptr;
840 }
841
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400842 SkASSERT(renderTargetSampleCnt == 1 || renderable == GrRenderable::kYes);
Greg Daniel638b2e82020-08-27 14:29:00 -0400843 // TODO: If we ever have callers requesting specific surface flags then we shouldn't use the
844 // extra deferred flags here. Instead those callers should all pass in exactly what they want.
845 // However, as of today all uses of this essentially create a deferred proxy in the end.
846 GrInternalSurfaceFlags surfaceFlags = caps.getExtraSurfaceFlagsForDeferredRT();
Robert Phillips777707b2018-01-17 11:40:14 -0500847
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400848 // MakeFullyLazyProxy is only called at flush time so we know these texture proxies are
849 // not being created by a DDL provider.
Brian Salomona56a7462020-02-07 14:17:25 -0500850 static constexpr SkISize kLazyDims = {-1, -1};
Brian Salomonbeb7f522019-08-30 16:19:42 -0400851 if (GrRenderable::kYes == renderable) {
852 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
Greg Daniel3a365112020-02-14 10:47:18 -0500853 caps, std::move(callback), format, kLazyDims, renderTargetSampleCnt,
Brian Salomona6db5102020-07-21 09:56:23 -0400854 GrMipmapped::kNo, GrMipmapStatus::kNotAllocated, SkBackingFit::kApprox,
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400855 SkBudgeted::kYes, isProtected, surfaceFlags, useAllocator, GrDDLProvider::kNo));
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400856 } else {
857 return sk_sp<GrTextureProxy>(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400858 new GrTextureProxy(std::move(callback), format, kLazyDims, GrMipmapped::kNo,
Brian Salomona6db5102020-07-21 09:56:23 -0400859 GrMipmapStatus::kNotAllocated, SkBackingFit::kApprox,
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400860 SkBudgeted::kYes, isProtected, surfaceFlags, useAllocator,
861 GrDDLProvider::kNo));
Brian Salomonbeb7f522019-08-30 16:19:42 -0400862 }
Robert Phillips777707b2018-01-17 11:40:14 -0500863}
864
Robert Phillips427966a2018-12-20 17:20:43 -0500865void GrProxyProvider::processInvalidUniqueKey(const GrUniqueKey& key, GrTextureProxy* proxy,
866 InvalidateGPUResource invalidateGPUResource) {
Mike Klein04ef8102020-03-16 12:44:23 -0500867 this->processInvalidUniqueKeyImpl(key, proxy, invalidateGPUResource, RemoveTableEntry::kYes);
868}
869
870void GrProxyProvider::processInvalidUniqueKeyImpl(const GrUniqueKey& key, GrTextureProxy* proxy,
871 InvalidateGPUResource invalidateGPUResource,
872 RemoveTableEntry removeTableEntry) {
Chris Dalton2de13dd2019-01-03 15:11:59 -0700873 SkASSERT(key.isValid());
874
Robert Phillips427966a2018-12-20 17:20:43 -0500875 if (!proxy) {
876 proxy = fUniquelyKeyedProxies.find(key);
877 }
Chris Dalton2de13dd2019-01-03 15:11:59 -0700878 SkASSERT(!proxy || proxy->getUniqueKey() == key);
879
880 // Locate the corresponding GrGpuResource (if it needs to be invalidated) before clearing the
881 // proxy's unique key. We must do it in this order because 'key' may alias the proxy's key.
882 sk_sp<GrGpuResource> invalidGpuResource;
883 if (InvalidateGPUResource::kYes == invalidateGPUResource) {
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400884 auto direct = fImageContext->asDirectContext();
Brian Salomon01ceae92019-04-02 11:49:54 -0400885 if (direct) {
886 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
887 invalidGpuResource = resourceProvider->findByUniqueKey<GrGpuResource>(key);
Chris Dalton2de13dd2019-01-03 15:11:59 -0700888 }
889 SkASSERT(!invalidGpuResource || invalidGpuResource->getUniqueKey() == key);
890 }
Robert Phillips427966a2018-12-20 17:20:43 -0500891
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500892 // Note: this method is called for the whole variety of GrGpuResources so often 'key'
893 // will not be in 'fUniquelyKeyedProxies'.
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500894 if (proxy) {
Mike Klein04ef8102020-03-16 12:44:23 -0500895 if (removeTableEntry == RemoveTableEntry::kYes) {
896 fUniquelyKeyedProxies.remove(key);
897 }
Robert Phillips427966a2018-12-20 17:20:43 -0500898 proxy->cacheAccess().clearUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500899 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500900
Chris Dalton2de13dd2019-01-03 15:11:59 -0700901 if (invalidGpuResource) {
902 invalidGpuResource->resourcePriv().removeUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500903 }
904}
905
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400906GrDDLProvider GrProxyProvider::isDDLProvider() const {
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400907 return fImageContext->asDirectContext() ? GrDDLProvider::kNo : GrDDLProvider::kYes;
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400908}
909
Robert Phillipsa41c6852019-02-07 10:44:10 -0500910uint32_t GrProxyProvider::contextID() const {
911 return fImageContext->priv().contextID();
912}
913
914const GrCaps* GrProxyProvider::caps() const {
915 return fImageContext->priv().caps();
916}
917
918sk_sp<const GrCaps> GrProxyProvider::refCaps() const {
919 return fImageContext->priv().refCaps();
920}
921
Robert Phillipsa9162df2019-02-11 14:12:03 -0500922bool GrProxyProvider::isAbandoned() const {
923 return fImageContext->priv().abandoned();
924}
925
Robert Phillips0790f8a2018-09-18 13:11:03 -0400926void GrProxyProvider::orphanAllUniqueKeys() {
Mike Kleincff63962020-03-14 16:22:45 -0500927 fUniquelyKeyedProxies.foreach([&](GrTextureProxy* proxy){
928 proxy->fProxyProvider = nullptr;
929 });
Robert Phillips0790f8a2018-09-18 13:11:03 -0400930}
931
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500932void GrProxyProvider::removeAllUniqueKeys() {
Mike Klein04ef8102020-03-16 12:44:23 -0500933 fUniquelyKeyedProxies.foreach([&](GrTextureProxy* proxy){
934 // It's not safe to remove table entries while iterating with foreach(),
935 // but since we're going to remove them all anyway, simply save that for the end.
936 this->processInvalidUniqueKeyImpl(proxy->getUniqueKey(), proxy,
937 InvalidateGPUResource::kNo,
938 RemoveTableEntry::kNo);
939 });
940 // Removing all those table entries is safe now.
941 fUniquelyKeyedProxies.reset();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500942}
Robert Phillipsa41c6852019-02-07 10:44:10 -0500943
944bool GrProxyProvider::renderingDirectly() const {
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400945 return fImageContext->asDirectContext();
Robert Phillipsa41c6852019-02-07 10:44:10 -0500946}