blob: a05e84ab4393887b58b3d484373a59ac91781214 [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
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) {
Greg Daniel0e9d34d2021-08-13 16:20:18 -0400145 tex = resourceProvider->createApproxTexture(dimensions,
146 format,
147 format.textureType(),
148 renderable,
149 renderTargetSampleCnt,
150 isProtected);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500151 } else {
Greg Daniel0e9d34d2021-08-13 16:20:18 -0400152 tex = resourceProvider->createTexture(dimensions,
153 format,
154 format.textureType(),
155 renderable,
156 renderTargetSampleCnt,
157 GrMipmapped::kNo,
158 budgeted,
159 isProtected);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500160 }
161 if (!tex) {
162 return nullptr;
163 }
164
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400165 return this->createWrapped(std::move(tex), UseAllocator::kYes);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500166}
167
Brian Salomon4eb38b72019-08-05 12:58:39 -0400168sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createInstantiatedProxy(
Brian Salomona56a7462020-02-07 14:17:25 -0500169 SkISize dimensions,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400170 GrColorType colorType,
171 GrRenderable renderable,
172 int renderTargetSampleCnt,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400173 SkBackingFit fit,
174 SkBudgeted budgeted,
175 GrProtected isProtected) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500176 ASSERT_SINGLE_OWNER
177 if (this->isAbandoned()) {
178 return nullptr;
179 }
Brian Salomon4eb38b72019-08-05 12:58:39 -0400180 auto format = this->caps()->getDefaultBackendFormat(colorType, renderable);
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400181 return this->testingOnly_createInstantiatedProxy(dimensions,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400182 format,
183 renderable,
184 renderTargetSampleCnt,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400185 fit,
186 budgeted,
187 isProtected);
188}
189
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400190sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createWrapped(sk_sp<GrTexture> tex) {
191 return this->createWrapped(std::move(tex), UseAllocator::kYes);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500192}
193#endif
194
Brian Salomonbeb7f522019-08-30 16:19:42 -0400195sk_sp<GrTextureProxy> GrProxyProvider::createWrapped(sk_sp<GrTexture> tex,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400196 UseAllocator useAllocator) {
Robert Phillipsadbe1322018-01-17 13:35:46 -0500197#ifdef SK_DEBUG
198 if (tex->getUniqueKey().isValid()) {
Greg Daniel3a365112020-02-14 10:47:18 -0500199 SkASSERT(!this->findProxyByUniqueKey(tex->getUniqueKey()));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500200 }
201#endif
202
203 if (tex->asRenderTarget()) {
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400204 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), useAllocator,
205 this->isDDLProvider()));
Greg Daniel3a365112020-02-14 10:47:18 -0500206 } else {
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400207 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), useAllocator,
208 this->isDDLProvider()));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500209 }
210}
211
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500212sk_sp<GrTextureProxy> GrProxyProvider::findOrCreateProxyByUniqueKey(const GrUniqueKey& key,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400213 UseAllocator useAllocator) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500214 ASSERT_SINGLE_OWNER
215
216 if (this->isAbandoned()) {
217 return nullptr;
218 }
219
Greg Daniel3a365112020-02-14 10:47:18 -0500220 sk_sp<GrTextureProxy> result = this->findProxyByUniqueKey(key);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500221 if (result) {
222 return result;
223 }
224
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400225 auto direct = fImageContext->asDirectContext();
Robert Phillipsa41c6852019-02-07 10:44:10 -0500226 if (!direct) {
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500227 return nullptr;
228 }
229
Robert Phillipsa41c6852019-02-07 10:44:10 -0500230 GrResourceCache* resourceCache = direct->priv().getResourceCache();
231
232 GrGpuResource* resource = resourceCache->findAndRefUniqueResource(key);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500233 if (!resource) {
234 return nullptr;
235 }
236
237 sk_sp<GrTexture> texture(static_cast<GrSurface*>(resource)->asTexture());
238 SkASSERT(texture);
239
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400240 result = this->createWrapped(std::move(texture), useAllocator);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500241 SkASSERT(result->getUniqueKey() == key);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500242 // createWrapped should've added this for us
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500243 SkASSERT(fUniquelyKeyedProxies.find(key));
244 return result;
245}
246
Brian Salomond005b692020-04-01 15:47:05 -0400247GrSurfaceProxyView GrProxyProvider::findCachedProxyWithColorTypeFallback(const GrUniqueKey& key,
248 GrSurfaceOrigin origin,
Brian Salomon0029db02020-04-03 10:41:24 -0400249 GrColorType ct,
250 int sampleCnt) {
Brian Salomond005b692020-04-01 15:47:05 -0400251 auto proxy = this->findOrCreateProxyByUniqueKey(key);
252 if (!proxy) {
253 return {};
254 }
Robert Phillips87b57fa2021-06-08 13:10:04 -0400255 const GrCaps* caps = fImageContext->priv().caps();
256
Brian Salomond005b692020-04-01 15:47:05 -0400257 // Assume that we used a fallback color type if and only if the proxy is renderable.
258 if (proxy->asRenderTargetProxy()) {
259 GrBackendFormat expectedFormat;
Robert Phillips87b57fa2021-06-08 13:10:04 -0400260 std::tie(ct, expectedFormat) = caps->getFallbackColorTypeAndFormat(ct, sampleCnt);
Brian Salomond005b692020-04-01 15:47:05 -0400261 SkASSERT(expectedFormat == proxy->backendFormat());
262 }
Robert Phillips87b57fa2021-06-08 13:10:04 -0400263 GrSwizzle swizzle = caps->getReadSwizzle(proxy->backendFormat(), ct);
Brian Salomond005b692020-04-01 15:47:05 -0400264 return {std::move(proxy), origin, swizzle};
265}
266
Brian Osmande496652019-03-22 13:42:33 -0400267sk_sp<GrTextureProxy> GrProxyProvider::createProxyFromBitmap(const SkBitmap& bitmap,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400268 GrMipmapped mipMapped,
Brian Salomonbc074a62020-03-18 10:06:13 -0400269 SkBackingFit fit,
270 SkBudgeted budgeted) {
Brian Osman412674f2019-02-07 15:34:58 -0500271 ASSERT_SINGLE_OWNER
Brian Salomon7e67dca2020-07-21 09:27:25 -0400272 SkASSERT(fit == SkBackingFit::kExact || mipMapped == GrMipmapped::kNo);
Brian Osman412674f2019-02-07 15:34:58 -0500273
274 if (this->isAbandoned()) {
275 return nullptr;
276 }
277
Brian Osman2b23c4b2018-06-01 12:25:08 -0400278 if (!SkImageInfoIsValid(bitmap.info())) {
Greg Daniela4ead652018-02-07 10:21:48 -0500279 return nullptr;
280 }
281
Brian Osmande496652019-03-22 13:42:33 -0400282 ATRACE_ANDROID_FRAMEWORK("Upload %sTexture [%ux%u]",
Brian Salomon7e67dca2020-07-21 09:27:25 -0400283 GrMipmapped::kYes == mipMapped ? "MipMap " : "",
Brian Osmande496652019-03-22 13:42:33 -0400284 bitmap.width(), bitmap.height());
Greg Daniela4ead652018-02-07 10:21:48 -0500285
286 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
287 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
288 // 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 -0500289 SkBitmap copyBitmap = bitmap;
290 if (!this->renderingDirectly() && !bitmap.isImmutable()) {
291 copyBitmap.allocPixels();
292 if (!bitmap.readPixels(copyBitmap.pixmap())) {
293 return nullptr;
294 }
295 copyBitmap.setImmutable();
Greg Daniela4ead652018-02-07 10:21:48 -0500296 }
297
Greg Daniel6f5441a2020-01-28 17:02:49 -0500298 sk_sp<GrTextureProxy> proxy;
Brian Salomon7e67dca2020-07-21 09:27:25 -0400299 if (mipMapped == GrMipmapped::kNo ||
Mike Reed13711eb2020-07-14 17:16:32 -0400300 0 == SkMipmap::ComputeLevelCount(copyBitmap.width(), copyBitmap.height())) {
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400301 proxy = this->createNonMippedProxyFromBitmap(copyBitmap, fit, budgeted);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500302 } else {
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400303 proxy = this->createMippedProxyFromBitmap(copyBitmap, budgeted);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500304 }
305
306 if (!proxy) {
307 return nullptr;
308 }
309
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400310 auto direct = fImageContext->asDirectContext();
Greg Daniel6f5441a2020-01-28 17:02:49 -0500311 if (direct) {
312 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
313
314 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
315 // we're better off instantiating the proxy immediately here.
316 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
317 return nullptr;
318 }
319 }
320 return proxy;
321}
322
323sk_sp<GrTextureProxy> GrProxyProvider::createNonMippedProxyFromBitmap(const SkBitmap& bitmap,
324 SkBackingFit fit,
Brian Salomonbc074a62020-03-18 10:06:13 -0400325 SkBudgeted budgeted) {
Brian Salomona56a7462020-02-07 14:17:25 -0500326 auto dims = bitmap.dimensions();
Greg Daniel6f5441a2020-01-28 17:02:49 -0500327
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400328 auto colorType = SkColorTypeToGrColorType(bitmap.colorType());
329 GrBackendFormat format = this->caps()->getDefaultBackendFormat(colorType, GrRenderable::kNo);
330 if (!format.isValid()) {
331 return nullptr;
332 }
333
Greg Daniel6f5441a2020-01-28 17:02:49 -0500334 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomon63410e92020-03-23 18:32:50 -0400335 [bitmap](GrResourceProvider* resourceProvider, const LazySurfaceDesc& desc) {
Brian Salomon40a40622020-07-21 10:32:07 -0400336 SkASSERT(desc.fMipmapped == GrMipmapped::kNo);
Brian Salomon2c673402021-04-02 14:36:58 -0400337 GrMipLevel mipLevel = {bitmap.getPixels(), bitmap.rowBytes(), nullptr};
Brian Salomon63410e92020-03-23 18:32:50 -0400338 auto colorType = SkColorTypeToGrColorType(bitmap.colorType());
Greg Daniel6f5441a2020-01-28 17:02:49 -0500339 return LazyCallbackResult(resourceProvider->createTexture(
Greg Daniel0e9d34d2021-08-13 16:20:18 -0400340 desc.fDimensions,
341 desc.fFormat,
342 desc.fFormat.textureType(),
343 colorType,
344 desc.fRenderable,
345 desc.fSampleCnt,
346 desc.fBudgeted,
347 desc.fFit,
348 desc.fProtected,
349 mipLevel));
Greg Daniel6f5441a2020-01-28 17:02:49 -0500350 },
Greg Danielc113f002020-08-26 13:28:22 -0400351 format, dims, GrMipmapped::kNo, GrMipmapStatus::kNotAllocated,
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400352 GrInternalSurfaceFlags::kNone, fit, budgeted, GrProtected::kNo, UseAllocator::kYes);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500353
354 if (!proxy) {
355 return nullptr;
356 }
Brian Salomona56a7462020-02-07 14:17:25 -0500357 SkASSERT(proxy->dimensions() == bitmap.dimensions());
Greg Daniel6f5441a2020-01-28 17:02:49 -0500358 return proxy;
359}
360
361sk_sp<GrTextureProxy> GrProxyProvider::createMippedProxyFromBitmap(const SkBitmap& bitmap,
Brian Salomonbc074a62020-03-18 10:06:13 -0400362 SkBudgeted budgeted) {
Brian Salomon69100f02020-07-21 10:49:25 -0400363 SkASSERT(this->caps()->mipmapSupport());
Greg Daniel6f5441a2020-01-28 17:02:49 -0500364
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400365 auto colorType = SkColorTypeToGrColorType(bitmap.colorType());
366 GrBackendFormat format = this->caps()->getDefaultBackendFormat(colorType, GrRenderable::kNo);
367 if (!format.isValid()) {
368 return nullptr;
369 }
Greg Daniel6f5441a2020-01-28 17:02:49 -0500370
Mike Reed13711eb2020-07-14 17:16:32 -0400371 sk_sp<SkMipmap> mipmaps(SkMipmap::Build(bitmap.pixmap(), nullptr));
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400372 if (!mipmaps) {
373 return nullptr;
374 }
375
Brian Salomona56a7462020-02-07 14:17:25 -0500376 auto dims = bitmap.dimensions();
Greg Danielce3ddaa2020-01-22 16:58:15 -0500377
Greg Daniela4ead652018-02-07 10:21:48 -0500378 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomon63410e92020-03-23 18:32:50 -0400379 [bitmap, mipmaps](GrResourceProvider* resourceProvider, const LazySurfaceDesc& desc) {
Brian Osman1b97f132018-09-13 17:33:48 +0000380 const int mipLevelCount = mipmaps->countLevels() + 1;
381 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
Brian Salomon63410e92020-03-23 18:32:50 -0400382 auto colorType = SkColorTypeToGrColorType(bitmap.colorType());
Brian Osman1b97f132018-09-13 17:33:48 +0000383
Greg Daniel6f5441a2020-01-28 17:02:49 -0500384 texels[0].fPixels = bitmap.getPixels();
385 texels[0].fRowBytes = bitmap.rowBytes();
Greg Daniela4ead652018-02-07 10:21:48 -0500386
Greg Daniela4ead652018-02-07 10:21:48 -0500387 for (int i = 1; i < mipLevelCount; ++i) {
Mike Reed13711eb2020-07-14 17:16:32 -0400388 SkMipmap::Level generatedMipLevel;
Greg Daniela4ead652018-02-07 10:21:48 -0500389 mipmaps->getLevel(i - 1, &generatedMipLevel);
390 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
391 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
392 SkASSERT(texels[i].fPixels);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500393 SkASSERT(generatedMipLevel.fPixmap.colorType() == bitmap.colorType());
Greg Daniela4ead652018-02-07 10:21:48 -0500394 }
Brian Salomonbeb7f522019-08-30 16:19:42 -0400395 return LazyCallbackResult(resourceProvider->createTexture(
Greg Daniel0e9d34d2021-08-13 16:20:18 -0400396 desc.fDimensions,
397 desc.fFormat,
398 desc.fFormat.textureType(),
399 colorType,
400 GrRenderable::kNo,
401 1,
402 desc.fBudgeted,
403 GrMipMapped::kYes,
404 GrProtected::kNo,
405 texels.get()));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500406 },
Greg Danielc113f002020-08-26 13:28:22 -0400407 format, dims, GrMipmapped::kYes, GrMipmapStatus::kValid, GrInternalSurfaceFlags::kNone,
408 SkBackingFit::kExact, budgeted, GrProtected::kNo, UseAllocator::kYes);
Greg Daniela4ead652018-02-07 10:21:48 -0500409
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400410 if (!proxy) {
411 return nullptr;
412 }
413
Brian Salomona56a7462020-02-07 14:17:25 -0500414 SkASSERT(proxy->dimensions() == bitmap.dimensions());
Greg Daniel6f5441a2020-01-28 17:02:49 -0500415
Greg Daniela4ead652018-02-07 10:21:48 -0500416 return proxy;
417}
418
Greg Daniel4065d452018-11-16 15:43:41 -0500419sk_sp<GrTextureProxy> GrProxyProvider::createProxy(const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -0500420 SkISize dimensions,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400421 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400422 int renderTargetSampleCnt,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400423 GrMipmapped mipMapped,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500424 SkBackingFit fit,
425 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -0400426 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400427 GrInternalSurfaceFlags surfaceFlags,
428 GrSurfaceProxy::UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500429 ASSERT_SINGLE_OWNER
430 if (this->isAbandoned()) {
431 return nullptr;
432 }
433
Robert Phillips8ff8bcc2019-07-29 17:03:35 -0400434 const GrCaps* caps = this->caps();
435
436 if (caps->isFormatCompressed(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400437 // Deferred proxies for compressed textures are not supported.
438 return nullptr;
439 }
Robert Phillips0902c982019-07-16 07:47:56 -0400440
Brian Salomon7e67dca2020-07-21 09:27:25 -0400441 if (GrMipmapped::kYes == mipMapped) {
Mike Reed13711eb2020-07-14 17:16:32 -0400442 // SkMipmap doesn't include the base level in the level count so we have to add 1
443 int mipCount = SkMipmap::ComputeLevelCount(dimensions.fWidth, dimensions.fHeight) + 1;
Greg Danielf6f7b672018-02-15 13:06:26 -0500444 if (1 == mipCount) {
Brian Salomon7e67dca2020-07-21 09:27:25 -0400445 mipMapped = GrMipmapped::kNo;
Greg Danielf6f7b672018-02-15 13:06:26 -0500446 }
447 }
448
Greg Daniel0e9d34d2021-08-13 16:20:18 -0400449 if (!caps->validateSurfaceParams(dimensions,
450 format,
451 renderable,
452 renderTargetSampleCnt,
453 mipMapped,
454 GrTextureType::k2D)) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500455 return nullptr;
456 }
Brian Salomona6db5102020-07-21 09:56:23 -0400457 GrMipmapStatus mipmapStatus = (GrMipmapped::kYes == mipMapped)
458 ? GrMipmapStatus::kDirty
459 : GrMipmapStatus::kNotAllocated;
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400460 if (renderable == GrRenderable::kYes) {
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400461 renderTargetSampleCnt = caps->getRenderTargetSampleCount(renderTargetSampleCnt, format);
Greg Daniel6fa62e22019-08-07 15:52:37 -0400462 SkASSERT(renderTargetSampleCnt);
Greg Daniel638b2e82020-08-27 14:29:00 -0400463 GrInternalSurfaceFlags extraFlags = caps->getExtraSurfaceFlagsForDeferredRT();
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500464 // We know anything we instantiate later from this deferred path will be
465 // both texturable and renderable
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400466 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
Brian Salomona6db5102020-07-21 09:56:23 -0400467 *caps, format, dimensions, renderTargetSampleCnt, mipMapped, mipmapStatus, fit,
Greg Daniel638b2e82020-08-27 14:29:00 -0400468 budgeted, isProtected, surfaceFlags | extraFlags, useAllocator,
469 this->isDDLProvider()));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500470 }
471
Brian Salomona6db5102020-07-21 09:56:23 -0400472 return sk_sp<GrTextureProxy>(new GrTextureProxy(format, dimensions, mipMapped, mipmapStatus,
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400473 fit, budgeted, isProtected, surfaceFlags,
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400474 useAllocator, this->isDDLProvider()));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500475}
476
Brian Salomonbb8dde82019-06-27 10:52:13 -0400477sk_sp<GrTextureProxy> GrProxyProvider::createCompressedTextureProxy(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400478 SkISize dimensions, SkBudgeted budgeted, GrMipmapped mipMapped, GrProtected isProtected,
Robert Phillipse4720c62020-01-14 14:33:24 -0500479 SkImage::CompressionType compressionType, sk_sp<SkData> data) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500480 ASSERT_SINGLE_OWNER
481 if (this->isAbandoned()) {
482 return nullptr;
483 }
Brian Salomonbb8dde82019-06-27 10:52:13 -0400484
Robert Phillips8ff8bcc2019-07-29 17:03:35 -0400485 GrBackendFormat format = this->caps()->getBackendFormatFromCompressionType(compressionType);
486
Greg Daniel0e9d34d2021-08-13 16:20:18 -0400487 if (!this->caps()->isFormatTexturable(format, GrTextureType::k2D)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500488 return nullptr;
489 }
490
Brian Salomona6db5102020-07-21 09:56:23 -0400491 GrMipmapStatus mipmapStatus = (GrMipmapped::kYes == mipMapped) ? GrMipmapStatus::kValid
492 : GrMipmapStatus::kNotAllocated;
Robert Phillipse4720c62020-01-14 14:33:24 -0500493
Jim Van Verthee06b332019-01-18 10:36:32 -0500494 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomon63410e92020-03-23 18:32:50 -0400495 [data](GrResourceProvider* resourceProvider, const LazySurfaceDesc& desc) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400496 return LazyCallbackResult(resourceProvider->createCompressedTexture(
Brian Salomon40a40622020-07-21 10:32:07 -0400497 desc.fDimensions, desc.fFormat, desc.fBudgeted, desc.fMipmapped,
Brian Salomon63410e92020-03-23 18:32:50 -0400498 desc.fProtected, data.get()));
Brian Salomonbb8dde82019-06-27 10:52:13 -0400499 },
Greg Danielc113f002020-08-26 13:28:22 -0400500 format, dimensions, mipMapped, mipmapStatus,GrInternalSurfaceFlags::kReadOnly,
501 SkBackingFit::kExact, SkBudgeted::kYes, GrProtected::kNo, UseAllocator::kYes);
Jim Van Verthee06b332019-01-18 10:36:32 -0500502
503 if (!proxy) {
504 return nullptr;
505 }
506
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400507 auto direct = fImageContext->asDirectContext();
Robert Phillipsa41c6852019-02-07 10:44:10 -0500508 if (direct) {
509 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Jim Van Verthee06b332019-01-18 10:36:32 -0500510 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
511 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500512 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500513 return nullptr;
514 }
515 }
516 return proxy;
517}
518
Brian Salomon7578f3e2018-03-07 14:39:54 -0500519sk_sp<GrTextureProxy> GrProxyProvider::wrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500520 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500521 GrWrapCacheable cacheable,
Brian Salomonc67c31c2018-12-06 10:00:03 -0500522 GrIOType ioType,
Robert Phillipse22c5ca2020-06-19 12:29:57 -0400523 sk_sp<GrRefCntedCallback> releaseHelper) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500524 SkASSERT(ioType != kWrite_GrIOType);
Robert Phillipse22c5ca2020-06-19 12:29:57 -0400525
Robert Phillipsf9bec202018-01-16 09:21:01 -0500526 if (this->isAbandoned()) {
527 return nullptr;
528 }
529
Brian Salomonf7778972018-03-08 10:13:17 -0500530 // This is only supported on a direct GrContext.
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400531 auto direct = fImageContext->asDirectContext();
Robert Phillipsa41c6852019-02-07 10:44:10 -0500532 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500533 return nullptr;
534 }
535
Robert Phillipsa41c6852019-02-07 10:44:10 -0500536 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
537
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500538 sk_sp<GrTexture> tex =
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400539 resourceProvider->wrapBackendTexture(backendTex, ownership, cacheable, ioType);
Brian Salomonf7778972018-03-08 10:13:17 -0500540 if (!tex) {
541 return nullptr;
542 }
Robert Phillipsadbe1322018-01-17 13:35:46 -0500543
Robert Phillipse22c5ca2020-06-19 12:29:57 -0400544 if (releaseHelper) {
545 tex->setRelease(std::move(releaseHelper));
Greg Daniel6a0176b2018-01-30 09:28:44 -0500546 }
547
Brian Salomonf7778972018-03-08 10:13:17 -0500548 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
549 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500550 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500551
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400552 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), UseAllocator::kNo,
553 this->isDDLProvider()));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500554}
555
Robert Phillipsa1121332020-06-29 13:05:29 -0400556sk_sp<GrTextureProxy> GrProxyProvider::wrapCompressedBackendTexture(
557 const GrBackendTexture& beTex,
558 GrWrapOwnership ownership,
559 GrWrapCacheable cacheable,
560 sk_sp<GrRefCntedCallback> releaseHelper) {
Robert Phillipsead321b2019-12-19 10:16:32 -0500561 if (this->isAbandoned()) {
562 return nullptr;
563 }
564
565 // This is only supported on a direct GrContext.
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400566 auto direct = fImageContext->asDirectContext();
Robert Phillipsead321b2019-12-19 10:16:32 -0500567 if (!direct) {
568 return nullptr;
569 }
570
571 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
572
573 sk_sp<GrTexture> tex = resourceProvider->wrapCompressedBackendTexture(beTex, ownership,
574 cacheable);
575 if (!tex) {
576 return nullptr;
577 }
578
Robert Phillipsa1121332020-06-29 13:05:29 -0400579 if (releaseHelper) {
580 tex->setRelease(std::move(releaseHelper));
Robert Phillipsead321b2019-12-19 10:16:32 -0500581 }
582
583 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
584 // Make sure we match how we created the proxy with SkBudgeted::kNo
585 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
586
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400587 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), UseAllocator::kNo,
588 this->isDDLProvider()));
Robert Phillipsead321b2019-12-19 10:16:32 -0500589}
590
Brian Salomon7578f3e2018-03-07 14:39:54 -0500591sk_sp<GrTextureProxy> GrProxyProvider::wrapRenderableBackendTexture(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400592 const GrBackendTexture& backendTex,
593 int sampleCnt,
594 GrWrapOwnership ownership,
595 GrWrapCacheable cacheable,
Robert Phillipsa1121332020-06-29 13:05:29 -0400596 sk_sp<GrRefCntedCallback> releaseHelper) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500597 if (this->isAbandoned()) {
598 return nullptr;
599 }
600
Brian Salomonf7778972018-03-08 10:13:17 -0500601 // This is only supported on a direct GrContext.
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400602 auto direct = fImageContext->asDirectContext();
Robert Phillipsa41c6852019-02-07 10:44:10 -0500603 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500604 return nullptr;
605 }
606
Robert Phillips0902c982019-07-16 07:47:56 -0400607 const GrCaps* caps = this->caps();
608
Robert Phillipsa41c6852019-02-07 10:44:10 -0500609 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
610
Greg Daniel6fa62e22019-08-07 15:52:37 -0400611 sampleCnt = caps->getRenderTargetSampleCount(sampleCnt, backendTex.getBackendFormat());
612 SkASSERT(sampleCnt);
613
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400614 sk_sp<GrTexture> tex = resourceProvider->wrapRenderableBackendTexture(
615 backendTex, sampleCnt, ownership, cacheable);
Brian Salomonf7778972018-03-08 10:13:17 -0500616 if (!tex) {
617 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500618 }
619
Robert Phillipsa1121332020-06-29 13:05:29 -0400620 if (releaseHelper) {
621 tex->setRelease(std::move(releaseHelper));
Greg Daniel8ce79912019-02-05 10:08:43 -0500622 }
623
Brian Salomonf7778972018-03-08 10:13:17 -0500624 SkASSERT(tex->asRenderTarget()); // A GrTextureRenderTarget
625 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500626 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Greg Daniel6abda432018-02-15 14:55:00 -0500627
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400628 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), UseAllocator::kNo,
629 this->isDDLProvider()));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500630}
631
Brian Salomon7578f3e2018-03-07 14:39:54 -0500632sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400633 const GrBackendRenderTarget& backendRT,
Robert Phillipsa1121332020-06-29 13:05:29 -0400634 sk_sp<GrRefCntedCallback> releaseHelper) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500635 if (this->isAbandoned()) {
636 return nullptr;
637 }
638
Brian Salomonf7778972018-03-08 10:13:17 -0500639 // This is only supported on a direct GrContext.
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400640 auto direct = fImageContext->asDirectContext();
Robert Phillipsa41c6852019-02-07 10:44:10 -0500641 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500642 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500643 }
644
Robert Phillipsa41c6852019-02-07 10:44:10 -0500645 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
646
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400647 sk_sp<GrRenderTarget> rt = resourceProvider->wrapBackendRenderTarget(backendRT);
Brian Salomonf7778972018-03-08 10:13:17 -0500648 if (!rt) {
649 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500650 }
Greg Daniel8ce79912019-02-05 10:08:43 -0500651
Robert Phillipsa1121332020-06-29 13:05:29 -0400652 if (releaseHelper) {
653 rt->setRelease(std::move(releaseHelper));
Greg Daniel8ce79912019-02-05 10:08:43 -0500654 }
655
Brian Salomonf7778972018-03-08 10:13:17 -0500656 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
657 SkASSERT(!rt->getUniqueKey().isValid());
658 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500659 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Brian Salomonf7778972018-03-08 10:13:17 -0500660
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400661 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(std::move(rt), UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500662}
663
Greg Danielb46add82019-01-02 14:51:29 -0500664sk_sp<GrRenderTargetProxy> GrProxyProvider::wrapVulkanSecondaryCBAsRenderTarget(
665 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
666 if (this->isAbandoned()) {
667 return nullptr;
668 }
669
670 // This is only supported on a direct GrContext.
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400671 auto direct = fImageContext->asDirectContext();
Robert Phillipsa41c6852019-02-07 10:44:10 -0500672 if (!direct) {
Greg Danielb46add82019-01-02 14:51:29 -0500673 return nullptr;
674 }
675
Robert Phillipsa41c6852019-02-07 10:44:10 -0500676 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Danielb46add82019-01-02 14:51:29 -0500677
Robert Phillipsa41c6852019-02-07 10:44:10 -0500678 sk_sp<GrRenderTarget> rt = resourceProvider->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
679 vkInfo);
Greg Danielb46add82019-01-02 14:51:29 -0500680 if (!rt) {
681 return nullptr;
682 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500683
Greg Danielb46add82019-01-02 14:51:29 -0500684 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
685 SkASSERT(!rt->getUniqueKey().isValid());
686 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500687 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielb46add82019-01-02 14:51:29 -0500688
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400689 GrColorType colorType = SkColorTypeToGrColorType(imageInfo.colorType());
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400690
Greg Danielde4bbdb2021-04-13 14:23:23 -0400691 if (!this->caps()->isFormatAsColorTypeRenderable(
692 colorType, GrBackendFormat::MakeVk(vkInfo.fFormat), /*sampleCount=*/1)) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400693 return nullptr;
694 }
695
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400696 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
697 std::move(rt), UseAllocator::kNo, GrRenderTargetProxy::WrapsVkSecondaryCB::kYes));
Brian Salomone8a766b2019-07-19 14:24:36 -0400698}
699
Adlai Hollerdced31f2021-02-19 12:33:46 -0500700sk_sp<GrTextureProxy> GrProxyProvider::CreatePromiseProxy(GrContextThreadSafeProxy* threadSafeProxy,
701 LazyInstantiateCallback&& callback,
702 const GrBackendFormat& format,
703 SkISize dimensions,
704 GrMipmapped mipMapped) {
705 if (threadSafeProxy->priv().abandoned()) {
706 return nullptr;
707 }
708 SkASSERT((dimensions.fWidth <= 0 && dimensions.fHeight <= 0) ||
709 (dimensions.fWidth > 0 && dimensions.fHeight > 0));
710
711 if (dimensions.fWidth > threadSafeProxy->priv().caps()->maxTextureSize() ||
712 dimensions.fHeight > threadSafeProxy->priv().caps()->maxTextureSize()) {
713 return nullptr;
714 }
715 // Ganesh assumes that, when wrapping a mipmapped backend texture from a client, that its
716 // mipmaps are fully fleshed out.
717 GrMipmapStatus mipmapStatus = (GrMipmapped::kYes == mipMapped) ? GrMipmapStatus::kValid
718 : GrMipmapStatus::kNotAllocated;
719
720 // We pass kReadOnly here since we should treat content of the client's texture as immutable.
721 // The promise API provides no way for the client to indicate that the texture is protected.
Adlai Holler55aaefe2021-03-03 16:12:56 -0700722 auto proxy = sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(callback),
723 format,
724 dimensions,
725 mipMapped,
726 mipmapStatus,
727 SkBackingFit::kExact,
728 SkBudgeted::kNo,
729 GrProtected::kNo,
730 GrInternalSurfaceFlags::kReadOnly,
731 GrSurfaceProxy::UseAllocator::kYes,
732 GrDDLProvider::kYes));
733 proxy->priv().setIsPromiseProxy();
734 return proxy;
Adlai Hollerdced31f2021-02-19 12:33:46 -0500735}
736
Brian Salomone8a766b2019-07-19 14:24:36 -0400737sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
738 const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -0500739 SkISize dimensions,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400740 GrMipmapped mipMapped,
Brian Salomona6db5102020-07-21 09:56:23 -0400741 GrMipmapStatus mipmapStatus,
Brian Salomone8a766b2019-07-19 14:24:36 -0400742 GrInternalSurfaceFlags surfaceFlags,
743 SkBackingFit fit,
744 SkBudgeted budgeted,
745 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400746 GrSurfaceProxy::UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500747 ASSERT_SINGLE_OWNER
748 if (this->isAbandoned()) {
749 return nullptr;
750 }
Brian Salomona56a7462020-02-07 14:17:25 -0500751 SkASSERT((dimensions.fWidth <= 0 && dimensions.fHeight <= 0) ||
752 (dimensions.fWidth > 0 && dimensions.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400753
Brian Salomonbd3792d2020-11-10 14:17:58 -0500754 if (!format.isValid() || format.backend() != fImageContext->backend()) {
Robert Phillips0a15cc62019-07-30 12:49:10 -0400755 return nullptr;
756 }
757
Brian Salomona56a7462020-02-07 14:17:25 -0500758 if (dimensions.fWidth > this->caps()->maxTextureSize() ||
759 dimensions.fHeight > this->caps()->maxTextureSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400760 return nullptr;
761 }
762
Greg Danielc113f002020-08-26 13:28:22 -0400763 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(callback),
764 format,
765 dimensions,
766 mipMapped,
767 mipmapStatus,
768 fit,
769 budgeted,
770 isProtected,
771 surfaceFlags,
772 useAllocator,
773 this->isDDLProvider()));
Robert Phillips777707b2018-01-17 11:40:14 -0500774}
775
Robert Phillipse8fabb22018-02-04 14:33:21 -0500776sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy(
Brian Salomonbeb7f522019-08-30 16:19:42 -0400777 LazyInstantiateCallback&& callback,
778 const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -0500779 SkISize dimensions,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400780 int sampleCnt,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400781 GrInternalSurfaceFlags surfaceFlags,
782 const TextureInfo* textureInfo,
Brian Salomona6db5102020-07-21 09:56:23 -0400783 GrMipmapStatus mipmapStatus,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400784 SkBackingFit fit,
785 SkBudgeted budgeted,
786 GrProtected isProtected,
787 bool wrapsVkSecondaryCB,
788 UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500789 ASSERT_SINGLE_OWNER
790 if (this->isAbandoned()) {
791 return nullptr;
792 }
Brian Salomona56a7462020-02-07 14:17:25 -0500793 SkASSERT((dimensions.fWidth <= 0 && dimensions.fHeight <= 0) ||
794 (dimensions.fWidth > 0 && dimensions.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400795
Brian Salomona56a7462020-02-07 14:17:25 -0500796 if (dimensions.fWidth > this->caps()->maxRenderTargetSize() ||
797 dimensions.fHeight > this->caps()->maxRenderTargetSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400798 return nullptr;
799 }
800
Brian Salomon7226c232018-07-30 13:13:17 -0400801 if (textureInfo) {
Greg Danielb085fa92019-03-05 16:55:12 -0500802 // Wrapped vulkan secondary command buffers don't support texturing since we won't have an
803 // actual VkImage to texture from.
804 SkASSERT(!wrapsVkSecondaryCB);
Brian Salomon7226c232018-07-30 13:13:17 -0400805 return sk_sp<GrRenderTargetProxy>(new GrTextureRenderTargetProxy(
Greg Daniel3a365112020-02-14 10:47:18 -0500806 *this->caps(), std::move(callback), format, dimensions, sampleCnt,
Brian Salomon40a40622020-07-21 10:32:07 -0400807 textureInfo->fMipmapped, mipmapStatus, fit, budgeted, isProtected, surfaceFlags,
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400808 useAllocator, this->isDDLProvider()));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500809 }
810
Greg Danielb085fa92019-03-05 16:55:12 -0500811 GrRenderTargetProxy::WrapsVkSecondaryCB vkSCB =
812 wrapsVkSecondaryCB ? GrRenderTargetProxy::WrapsVkSecondaryCB::kYes
813 : GrRenderTargetProxy::WrapsVkSecondaryCB::kNo;
814
Greg Daniel3a365112020-02-14 10:47:18 -0500815 return sk_sp<GrRenderTargetProxy>(
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400816 new GrRenderTargetProxy(std::move(callback), format, dimensions, sampleCnt, fit,
817 budgeted, isProtected, surfaceFlags, useAllocator, vkSCB));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500818}
819
Brian Salomonbeb7f522019-08-30 16:19:42 -0400820sk_sp<GrTextureProxy> GrProxyProvider::MakeFullyLazyProxy(LazyInstantiateCallback&& callback,
821 const GrBackendFormat& format,
822 GrRenderable renderable,
823 int renderTargetSampleCnt,
824 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400825 const GrCaps& caps,
826 UseAllocator useAllocator) {
Robert Phillips0a15cc62019-07-30 12:49:10 -0400827 if (!format.isValid()) {
828 return nullptr;
829 }
830
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400831 SkASSERT(renderTargetSampleCnt == 1 || renderable == GrRenderable::kYes);
Greg Daniel638b2e82020-08-27 14:29:00 -0400832 // TODO: If we ever have callers requesting specific surface flags then we shouldn't use the
833 // extra deferred flags here. Instead those callers should all pass in exactly what they want.
834 // However, as of today all uses of this essentially create a deferred proxy in the end.
835 GrInternalSurfaceFlags surfaceFlags = caps.getExtraSurfaceFlagsForDeferredRT();
Robert Phillips777707b2018-01-17 11:40:14 -0500836
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400837 // MakeFullyLazyProxy is only called at flush time so we know these texture proxies are
838 // not being created by a DDL provider.
Brian Salomona56a7462020-02-07 14:17:25 -0500839 static constexpr SkISize kLazyDims = {-1, -1};
Brian Salomonbeb7f522019-08-30 16:19:42 -0400840 if (GrRenderable::kYes == renderable) {
841 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
Greg Daniel3a365112020-02-14 10:47:18 -0500842 caps, std::move(callback), format, kLazyDims, renderTargetSampleCnt,
Brian Salomona6db5102020-07-21 09:56:23 -0400843 GrMipmapped::kNo, GrMipmapStatus::kNotAllocated, SkBackingFit::kApprox,
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400844 SkBudgeted::kYes, isProtected, surfaceFlags, useAllocator, GrDDLProvider::kNo));
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400845 } else {
846 return sk_sp<GrTextureProxy>(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400847 new GrTextureProxy(std::move(callback), format, kLazyDims, GrMipmapped::kNo,
Brian Salomona6db5102020-07-21 09:56:23 -0400848 GrMipmapStatus::kNotAllocated, SkBackingFit::kApprox,
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400849 SkBudgeted::kYes, isProtected, surfaceFlags, useAllocator,
850 GrDDLProvider::kNo));
Brian Salomonbeb7f522019-08-30 16:19:42 -0400851 }
Robert Phillips777707b2018-01-17 11:40:14 -0500852}
853
Robert Phillips427966a2018-12-20 17:20:43 -0500854void GrProxyProvider::processInvalidUniqueKey(const GrUniqueKey& key, GrTextureProxy* proxy,
855 InvalidateGPUResource invalidateGPUResource) {
Mike Klein04ef8102020-03-16 12:44:23 -0500856 this->processInvalidUniqueKeyImpl(key, proxy, invalidateGPUResource, RemoveTableEntry::kYes);
857}
858
859void GrProxyProvider::processInvalidUniqueKeyImpl(const GrUniqueKey& key, GrTextureProxy* proxy,
860 InvalidateGPUResource invalidateGPUResource,
861 RemoveTableEntry removeTableEntry) {
Chris Dalton2de13dd2019-01-03 15:11:59 -0700862 SkASSERT(key.isValid());
863
Robert Phillips427966a2018-12-20 17:20:43 -0500864 if (!proxy) {
865 proxy = fUniquelyKeyedProxies.find(key);
866 }
Chris Dalton2de13dd2019-01-03 15:11:59 -0700867 SkASSERT(!proxy || proxy->getUniqueKey() == key);
868
869 // Locate the corresponding GrGpuResource (if it needs to be invalidated) before clearing the
870 // proxy's unique key. We must do it in this order because 'key' may alias the proxy's key.
871 sk_sp<GrGpuResource> invalidGpuResource;
872 if (InvalidateGPUResource::kYes == invalidateGPUResource) {
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400873 auto direct = fImageContext->asDirectContext();
Brian Salomon01ceae92019-04-02 11:49:54 -0400874 if (direct) {
875 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
876 invalidGpuResource = resourceProvider->findByUniqueKey<GrGpuResource>(key);
Chris Dalton2de13dd2019-01-03 15:11:59 -0700877 }
878 SkASSERT(!invalidGpuResource || invalidGpuResource->getUniqueKey() == key);
879 }
Robert Phillips427966a2018-12-20 17:20:43 -0500880
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500881 // Note: this method is called for the whole variety of GrGpuResources so often 'key'
882 // will not be in 'fUniquelyKeyedProxies'.
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500883 if (proxy) {
Mike Klein04ef8102020-03-16 12:44:23 -0500884 if (removeTableEntry == RemoveTableEntry::kYes) {
885 fUniquelyKeyedProxies.remove(key);
886 }
Robert Phillips427966a2018-12-20 17:20:43 -0500887 proxy->cacheAccess().clearUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500888 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500889
Chris Dalton2de13dd2019-01-03 15:11:59 -0700890 if (invalidGpuResource) {
891 invalidGpuResource->resourcePriv().removeUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500892 }
893}
894
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400895GrDDLProvider GrProxyProvider::isDDLProvider() const {
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400896 return fImageContext->asDirectContext() ? GrDDLProvider::kNo : GrDDLProvider::kYes;
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400897}
898
Robert Phillipsa41c6852019-02-07 10:44:10 -0500899uint32_t GrProxyProvider::contextID() const {
900 return fImageContext->priv().contextID();
901}
902
903const GrCaps* GrProxyProvider::caps() const {
904 return fImageContext->priv().caps();
905}
906
907sk_sp<const GrCaps> GrProxyProvider::refCaps() const {
908 return fImageContext->priv().refCaps();
909}
910
Robert Phillipsa9162df2019-02-11 14:12:03 -0500911bool GrProxyProvider::isAbandoned() const {
912 return fImageContext->priv().abandoned();
913}
914
Robert Phillips0790f8a2018-09-18 13:11:03 -0400915void GrProxyProvider::orphanAllUniqueKeys() {
Mike Kleincff63962020-03-14 16:22:45 -0500916 fUniquelyKeyedProxies.foreach([&](GrTextureProxy* proxy){
917 proxy->fProxyProvider = nullptr;
918 });
Robert Phillips0790f8a2018-09-18 13:11:03 -0400919}
920
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500921void GrProxyProvider::removeAllUniqueKeys() {
Mike Klein04ef8102020-03-16 12:44:23 -0500922 fUniquelyKeyedProxies.foreach([&](GrTextureProxy* proxy){
923 // It's not safe to remove table entries while iterating with foreach(),
924 // but since we're going to remove them all anyway, simply save that for the end.
925 this->processInvalidUniqueKeyImpl(proxy->getUniqueKey(), proxy,
926 InvalidateGPUResource::kNo,
927 RemoveTableEntry::kNo);
928 });
929 // Removing all those table entries is safe now.
930 fUniquelyKeyedProxies.reset();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500931}
Robert Phillipsa41c6852019-02-07 10:44:10 -0500932
933bool GrProxyProvider::renderingDirectly() const {
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400934 return fImageContext->asDirectContext();
Robert Phillipsa41c6852019-02-07 10:44:10 -0500935}