blob: 1fa39c4bfc048fbab5f5ec945e7ac06e94969203 [file] [log] [blame]
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001/*
2 * Copyright 2018 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrProxyProvider.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkBitmap.h"
11#include "include/core/SkImage.h"
12#include "include/gpu/GrContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/private/GrImageContext.h"
14#include "include/private/GrResourceKey.h"
15#include "include/private/GrSingleOwner.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/private/SkImageInfoPriv.h"
17#include "src/core/SkAutoPixmapStorage.h"
Robert Phillips99dead92020-01-27 16:11:57 -050018#include "src/core/SkCompressedDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/core/SkImagePriv.h"
20#include "src/core/SkMipMap.h"
21#include "src/core/SkTraceEvent.h"
22#include "src/gpu/GrCaps.h"
23#include "src/gpu/GrContextPriv.h"
24#include "src/gpu/GrImageContextPriv.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040025#include "src/gpu/GrRenderTarget.h"
Brian Salomonb16f30b2020-03-31 09:17:56 -040026#include "src/gpu/GrRenderTargetContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/GrResourceProvider.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040028#include "src/gpu/GrSurfaceProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/gpu/GrSurfaceProxyPriv.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000030#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "src/gpu/GrTextureProxyCacheAccess.h"
32#include "src/gpu/GrTextureRenderTargetProxy.h"
33#include "src/gpu/SkGr.h"
34#include "src/image/SkImage_Base.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050035
36#define ASSERT_SINGLE_OWNER \
Robert Phillipsa41c6852019-02-07 10:44:10 -050037 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fImageContext->priv().singleOwner());)
Robert Phillips1afd4cd2018-01-08 13:40:32 -050038
Robert Phillipsa9162df2019-02-11 14:12:03 -050039GrProxyProvider::GrProxyProvider(GrImageContext* imageContext) : fImageContext(imageContext) {}
Robert Phillips1afd4cd2018-01-08 13:40:32 -050040
41GrProxyProvider::~GrProxyProvider() {
Robert Phillipsa41c6852019-02-07 10:44:10 -050042 if (this->renderingDirectly()) {
Robert Phillips0790f8a2018-09-18 13:11:03 -040043 // In DDL-mode a proxy provider can still have extant uniquely keyed proxies (since
44 // they need their unique keys to, potentially, find a cached resource when the
45 // DDL is played) but, in non-DDL-mode they should all have been cleaned up by this point.
46 SkASSERT(!fUniquelyKeyedProxies.count());
47 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050048}
49
Robert Phillipsadbe1322018-01-17 13:35:46 -050050bool GrProxyProvider::assignUniqueKeyToProxy(const GrUniqueKey& key, GrTextureProxy* proxy) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050051 ASSERT_SINGLE_OWNER
52 SkASSERT(key.isValid());
53 if (this->isAbandoned() || !proxy) {
Robert Phillipsadbe1322018-01-17 13:35:46 -050054 return false;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050055 }
56
Robert Phillipsa41c6852019-02-07 10:44:10 -050057#ifdef SK_DEBUG
58 {
59 GrContext* direct = fImageContext->priv().asDirectContext();
60 if (direct) {
61 GrResourceCache* resourceCache = direct->priv().getResourceCache();
62 // If there is already a GrResource with this key then the caller has violated the
63 // normal usage pattern of uniquely keyed resources (e.g., they have created one w/o
64 // first seeing if it already existed in the cache).
65 SkASSERT(!resourceCache->findAndRefUniqueResource(key));
66 }
67 }
68#endif
Robert Phillips1afd4cd2018-01-08 13:40:32 -050069
Robert Phillips1afd4cd2018-01-08 13:40:32 -050070 SkASSERT(!fUniquelyKeyedProxies.find(key)); // multiple proxies can't get the same key
71
72 proxy->cacheAccess().setUniqueKey(this, key);
73 SkASSERT(proxy->getUniqueKey() == key);
74 fUniquelyKeyedProxies.add(proxy);
Robert Phillipsadbe1322018-01-17 13:35:46 -050075 return true;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050076}
77
78void GrProxyProvider::adoptUniqueKeyFromSurface(GrTextureProxy* proxy, const GrSurface* surf) {
79 SkASSERT(surf->getUniqueKey().isValid());
80 proxy->cacheAccess().setUniqueKey(this, surf->getUniqueKey());
81 SkASSERT(proxy->getUniqueKey() == surf->getUniqueKey());
82 // multiple proxies can't get the same key
83 SkASSERT(!fUniquelyKeyedProxies.find(surf->getUniqueKey()));
84 fUniquelyKeyedProxies.add(proxy);
85}
86
Chris Dalton2de13dd2019-01-03 15:11:59 -070087void GrProxyProvider::removeUniqueKeyFromProxy(GrTextureProxy* proxy) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050088 ASSERT_SINGLE_OWNER
Chris Dalton2de13dd2019-01-03 15:11:59 -070089 SkASSERT(proxy);
90 SkASSERT(proxy->getUniqueKey().isValid());
91
92 if (this->isAbandoned()) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050093 return;
94 }
Robert Phillips0790f8a2018-09-18 13:11:03 -040095
Chris Dalton2de13dd2019-01-03 15:11:59 -070096 this->processInvalidUniqueKey(proxy->getUniqueKey(), proxy, InvalidateGPUResource::kYes);
Robert Phillips1afd4cd2018-01-08 13:40:32 -050097}
98
Greg Daniel3a365112020-02-14 10:47:18 -050099sk_sp<GrTextureProxy> GrProxyProvider::findProxyByUniqueKey(const GrUniqueKey& key) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500100 ASSERT_SINGLE_OWNER
101
102 if (this->isAbandoned()) {
103 return nullptr;
104 }
105
Brian Salomon01ceae92019-04-02 11:49:54 -0400106 GrTextureProxy* proxy = fUniquelyKeyedProxies.find(key);
Brian Salomon01ceae92019-04-02 11:49:54 -0400107 if (proxy) {
Robert Phillipse5f73282019-06-18 17:15:04 -0400108 return sk_ref_sp(proxy);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500109 }
Robert Phillipse5f73282019-06-18 17:15:04 -0400110 return nullptr;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500111}
112
Robert Phillipsa41c6852019-02-07 10:44:10 -0500113///////////////////////////////////////////////////////////////////////////////
114
115#if GR_TEST_UTILS
116sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createInstantiatedProxy(
Brian Salomona56a7462020-02-07 14:17:25 -0500117 SkISize dimensions,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400118 const GrBackendFormat& format,
119 GrRenderable renderable,
120 int renderTargetSampleCnt,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400121 SkBackingFit fit,
122 SkBudgeted budgeted,
123 GrProtected isProtected) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500124 ASSERT_SINGLE_OWNER
125 if (this->isAbandoned()) {
126 return nullptr;
127 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500128 GrContext* direct = fImageContext->priv().asDirectContext();
129 if (!direct) {
130 return nullptr;
131 }
132
Brian Salomon4eb38b72019-08-05 12:58:39 -0400133 if (this->caps()->isFormatCompressed(format)) {
134 // TODO: Allow this to go to GrResourceProvider::createCompressedTexture() once we no longer
Greg Daniel4cb29332020-01-23 10:07:02 -0500135 // rely on GrColorType to get a swizzle for the proxy.
Brian Salomon4eb38b72019-08-05 12:58:39 -0400136 return nullptr;
137 }
Brian Salomon4eb38b72019-08-05 12:58:39 -0400138
Robert Phillipsa41c6852019-02-07 10:44:10 -0500139 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
140 sk_sp<GrTexture> tex;
141
142 if (SkBackingFit::kApprox == fit) {
Brian Salomona56a7462020-02-07 14:17:25 -0500143 tex = resourceProvider->createApproxTexture(dimensions, format, renderable,
144 renderTargetSampleCnt, isProtected);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500145 } else {
Brian Salomona56a7462020-02-07 14:17:25 -0500146 tex = resourceProvider->createTexture(dimensions, format, renderable, renderTargetSampleCnt,
Brian Salomona90382f2019-09-17 09:01:56 -0400147 GrMipMapped::kNo, budgeted, isProtected);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500148 }
149 if (!tex) {
150 return nullptr;
151 }
152
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400153 return this->createWrapped(std::move(tex), UseAllocator::kYes);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500154}
155
Brian Salomon4eb38b72019-08-05 12:58:39 -0400156sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createInstantiatedProxy(
Brian Salomona56a7462020-02-07 14:17:25 -0500157 SkISize dimensions,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400158 GrColorType colorType,
159 GrRenderable renderable,
160 int renderTargetSampleCnt,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400161 SkBackingFit fit,
162 SkBudgeted budgeted,
163 GrProtected isProtected) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500164 ASSERT_SINGLE_OWNER
165 if (this->isAbandoned()) {
166 return nullptr;
167 }
Brian Salomon4eb38b72019-08-05 12:58:39 -0400168 auto format = this->caps()->getDefaultBackendFormat(colorType, renderable);
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400169 return this->testingOnly_createInstantiatedProxy(dimensions,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400170 format,
171 renderable,
172 renderTargetSampleCnt,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400173 fit,
174 budgeted,
175 isProtected);
176}
177
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400178sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createWrapped(sk_sp<GrTexture> tex) {
179 return this->createWrapped(std::move(tex), UseAllocator::kYes);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500180}
181#endif
182
Brian Salomonbeb7f522019-08-30 16:19:42 -0400183sk_sp<GrTextureProxy> GrProxyProvider::createWrapped(sk_sp<GrTexture> tex,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400184 UseAllocator useAllocator) {
Robert Phillipsadbe1322018-01-17 13:35:46 -0500185#ifdef SK_DEBUG
186 if (tex->getUniqueKey().isValid()) {
Greg Daniel3a365112020-02-14 10:47:18 -0500187 SkASSERT(!this->findProxyByUniqueKey(tex->getUniqueKey()));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500188 }
189#endif
190
191 if (tex->asRenderTarget()) {
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400192 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), useAllocator));
Greg Daniel3a365112020-02-14 10:47:18 -0500193 } else {
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400194 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), useAllocator));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500195 }
196}
197
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500198sk_sp<GrTextureProxy> GrProxyProvider::findOrCreateProxyByUniqueKey(const GrUniqueKey& key,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400199 UseAllocator useAllocator) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500200 ASSERT_SINGLE_OWNER
201
202 if (this->isAbandoned()) {
203 return nullptr;
204 }
205
Greg Daniel3a365112020-02-14 10:47:18 -0500206 sk_sp<GrTextureProxy> result = this->findProxyByUniqueKey(key);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500207 if (result) {
208 return result;
209 }
210
Robert Phillipsa41c6852019-02-07 10:44:10 -0500211 GrContext* direct = fImageContext->priv().asDirectContext();
212 if (!direct) {
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500213 return nullptr;
214 }
215
Robert Phillipsa41c6852019-02-07 10:44:10 -0500216 GrResourceCache* resourceCache = direct->priv().getResourceCache();
217
218 GrGpuResource* resource = resourceCache->findAndRefUniqueResource(key);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500219 if (!resource) {
220 return nullptr;
221 }
222
223 sk_sp<GrTexture> texture(static_cast<GrSurface*>(resource)->asTexture());
224 SkASSERT(texture);
225
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400226 result = this->createWrapped(std::move(texture), useAllocator);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500227 SkASSERT(result->getUniqueKey() == key);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500228 // createWrapped should've added this for us
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500229 SkASSERT(fUniquelyKeyedProxies.find(key));
230 return result;
231}
232
Brian Salomonb16f30b2020-03-31 09:17:56 -0400233GrSurfaceProxyView GrProxyProvider::findCachedProxyWithColorTypeFallback(const GrUniqueKey& key,
234 GrSurfaceOrigin origin,
235 GrColorType ct) {
236 auto proxy = this->findOrCreateProxyByUniqueKey(key);
237 if (!proxy) {
238 return {};
239 }
240 // Assume that we used a fallback color type if and only if the proxy is renderable.
241 if (proxy->asRenderTargetProxy()) {
242 GrBackendFormat expectedFormat;
243 std::tie(ct, expectedFormat) =
244 GrRenderTargetContext::GetFallbackColorTypeAndFormat(fImageContext, ct);
245 SkASSERT(expectedFormat == proxy->backendFormat());
246 }
247 GrSwizzle swizzle = fImageContext->priv().caps()->getReadSwizzle(proxy->backendFormat(), ct);
248 return {std::move(proxy), origin, swizzle};
249}
250
Brian Osmande496652019-03-22 13:42:33 -0400251sk_sp<GrTextureProxy> GrProxyProvider::createProxyFromBitmap(const SkBitmap& bitmap,
Greg Daniel6f5441a2020-01-28 17:02:49 -0500252 GrMipMapped mipMapped,
Brian Salomonbc074a62020-03-18 10:06:13 -0400253 SkBackingFit fit,
254 SkBudgeted budgeted) {
Brian Osman412674f2019-02-07 15:34:58 -0500255 ASSERT_SINGLE_OWNER
Greg Daniel6f5441a2020-01-28 17:02:49 -0500256 SkASSERT(fit == SkBackingFit::kExact || mipMapped == GrMipMapped::kNo);
Brian Osman412674f2019-02-07 15:34:58 -0500257
258 if (this->isAbandoned()) {
259 return nullptr;
260 }
261
Brian Osman2b23c4b2018-06-01 12:25:08 -0400262 if (!SkImageInfoIsValid(bitmap.info())) {
Greg Daniela4ead652018-02-07 10:21:48 -0500263 return nullptr;
264 }
265
Brian Osmande496652019-03-22 13:42:33 -0400266 ATRACE_ANDROID_FRAMEWORK("Upload %sTexture [%ux%u]",
267 GrMipMapped::kYes == mipMapped ? "MipMap " : "",
268 bitmap.width(), bitmap.height());
Greg Daniela4ead652018-02-07 10:21:48 -0500269
270 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
271 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
272 // 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 -0500273 SkBitmap copyBitmap = bitmap;
274 if (!this->renderingDirectly() && !bitmap.isImmutable()) {
275 copyBitmap.allocPixels();
276 if (!bitmap.readPixels(copyBitmap.pixmap())) {
277 return nullptr;
278 }
279 copyBitmap.setImmutable();
Greg Daniela4ead652018-02-07 10:21:48 -0500280 }
281
Greg Daniel6f5441a2020-01-28 17:02:49 -0500282 sk_sp<GrTextureProxy> proxy;
283 if (mipMapped == GrMipMapped::kNo ||
284 0 == SkMipMap::ComputeLevelCount(copyBitmap.width(), copyBitmap.height())) {
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400285 proxy = this->createNonMippedProxyFromBitmap(copyBitmap, fit, budgeted);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500286 } else {
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400287 proxy = this->createMippedProxyFromBitmap(copyBitmap, budgeted);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500288 }
289
290 if (!proxy) {
291 return nullptr;
292 }
293
294 GrContext* direct = fImageContext->priv().asDirectContext();
295 if (direct) {
296 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
297
298 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
299 // we're better off instantiating the proxy immediately here.
300 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
301 return nullptr;
302 }
303 }
304 return proxy;
305}
306
307sk_sp<GrTextureProxy> GrProxyProvider::createNonMippedProxyFromBitmap(const SkBitmap& bitmap,
308 SkBackingFit fit,
Brian Salomonbc074a62020-03-18 10:06:13 -0400309 SkBudgeted budgeted) {
Brian Salomona56a7462020-02-07 14:17:25 -0500310 auto dims = bitmap.dimensions();
Greg Daniel6f5441a2020-01-28 17:02:49 -0500311
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400312 auto colorType = SkColorTypeToGrColorType(bitmap.colorType());
313 GrBackendFormat format = this->caps()->getDefaultBackendFormat(colorType, GrRenderable::kNo);
314 if (!format.isValid()) {
315 return nullptr;
316 }
317
Greg Daniel6f5441a2020-01-28 17:02:49 -0500318 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomonbc074a62020-03-18 10:06:13 -0400319 [dims, format, bitmap, fit, colorType, budgeted](GrResourceProvider* resourceProvider) {
Greg Daniel6f5441a2020-01-28 17:02:49 -0500320 GrMipLevel mipLevel = { bitmap.getPixels(), bitmap.rowBytes() };
321
322 return LazyCallbackResult(resourceProvider->createTexture(
Brian Salomonbc074a62020-03-18 10:06:13 -0400323 dims, format, colorType, GrRenderable::kNo, 1, budgeted, fit,
Greg Daniel6f5441a2020-01-28 17:02:49 -0500324 GrProtected::kNo, mipLevel));
325 },
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400326 format, dims, GrRenderable::kNo, 1, GrMipMapped::kNo, GrMipMapsStatus::kNotAllocated,
327 GrInternalSurfaceFlags::kNone, fit, budgeted, GrProtected::kNo, UseAllocator::kYes);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500328
329 if (!proxy) {
330 return nullptr;
331 }
Brian Salomona56a7462020-02-07 14:17:25 -0500332 SkASSERT(proxy->dimensions() == bitmap.dimensions());
Greg Daniel6f5441a2020-01-28 17:02:49 -0500333 return proxy;
334}
335
336sk_sp<GrTextureProxy> GrProxyProvider::createMippedProxyFromBitmap(const SkBitmap& bitmap,
Brian Salomonbc074a62020-03-18 10:06:13 -0400337 SkBudgeted budgeted) {
Greg Daniel6f5441a2020-01-28 17:02:49 -0500338 SkASSERT(this->caps()->mipMapSupport());
339
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400340 auto colorType = SkColorTypeToGrColorType(bitmap.colorType());
341 GrBackendFormat format = this->caps()->getDefaultBackendFormat(colorType, GrRenderable::kNo);
342 if (!format.isValid()) {
343 return nullptr;
344 }
Greg Daniel6f5441a2020-01-28 17:02:49 -0500345
346 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(bitmap.pixmap(), nullptr));
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400347 if (!mipmaps) {
348 return nullptr;
349 }
350
Brian Salomona56a7462020-02-07 14:17:25 -0500351 auto dims = bitmap.dimensions();
Greg Danielce3ddaa2020-01-22 16:58:15 -0500352
Greg Daniela4ead652018-02-07 10:21:48 -0500353 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomonbc074a62020-03-18 10:06:13 -0400354 [dims, format, bitmap, mipmaps, budgeted](GrResourceProvider* resourceProvider) {
Brian Osman1b97f132018-09-13 17:33:48 +0000355 const int mipLevelCount = mipmaps->countLevels() + 1;
356 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
357
Greg Daniel6f5441a2020-01-28 17:02:49 -0500358 texels[0].fPixels = bitmap.getPixels();
359 texels[0].fRowBytes = bitmap.rowBytes();
Greg Daniela4ead652018-02-07 10:21:48 -0500360
Greg Daniel6f5441a2020-01-28 17:02:49 -0500361 auto colorType = SkColorTypeToGrColorType(bitmap.colorType());
Greg Daniela4ead652018-02-07 10:21:48 -0500362 for (int i = 1; i < mipLevelCount; ++i) {
363 SkMipMap::Level generatedMipLevel;
364 mipmaps->getLevel(i - 1, &generatedMipLevel);
365 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
366 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
367 SkASSERT(texels[i].fPixels);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500368 SkASSERT(generatedMipLevel.fPixmap.colorType() == bitmap.colorType());
Greg Daniela4ead652018-02-07 10:21:48 -0500369 }
Brian Salomonbeb7f522019-08-30 16:19:42 -0400370 return LazyCallbackResult(resourceProvider->createTexture(
Brian Salomonbc074a62020-03-18 10:06:13 -0400371 dims, format, colorType, GrRenderable::kNo, 1, budgeted, GrProtected::kNo,
372 texels.get(), mipLevelCount));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500373 },
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400374 format, dims, GrRenderable::kNo, 1, GrMipMapped::kYes, GrMipMapsStatus::kValid,
375 GrInternalSurfaceFlags::kNone, SkBackingFit::kExact, budgeted, GrProtected::kNo,
376 UseAllocator::kYes);
Greg Daniela4ead652018-02-07 10:21:48 -0500377
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400378 if (!proxy) {
379 return nullptr;
380 }
381
Brian Salomona56a7462020-02-07 14:17:25 -0500382 SkASSERT(proxy->dimensions() == bitmap.dimensions());
Greg Daniel6f5441a2020-01-28 17:02:49 -0500383
Greg Daniela4ead652018-02-07 10:21:48 -0500384 return proxy;
385}
386
Greg Daniel4065d452018-11-16 15:43:41 -0500387sk_sp<GrTextureProxy> GrProxyProvider::createProxy(const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -0500388 SkISize dimensions,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400389 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400390 int renderTargetSampleCnt,
Greg Danielf6f7b672018-02-15 13:06:26 -0500391 GrMipMapped mipMapped,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500392 SkBackingFit fit,
393 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -0400394 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400395 GrInternalSurfaceFlags surfaceFlags,
396 GrSurfaceProxy::UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500397 ASSERT_SINGLE_OWNER
398 if (this->isAbandoned()) {
399 return nullptr;
400 }
401
Robert Phillips8ff8bcc2019-07-29 17:03:35 -0400402 const GrCaps* caps = this->caps();
403
404 if (caps->isFormatCompressed(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400405 // Deferred proxies for compressed textures are not supported.
406 return nullptr;
407 }
Robert Phillips0902c982019-07-16 07:47:56 -0400408
Greg Danielf6f7b672018-02-15 13:06:26 -0500409 if (GrMipMapped::kYes == mipMapped) {
410 // SkMipMap doesn't include the base level in the level count so we have to add 1
Brian Salomona56a7462020-02-07 14:17:25 -0500411 int mipCount = SkMipMap::ComputeLevelCount(dimensions.fWidth, dimensions.fHeight) + 1;
Greg Danielf6f7b672018-02-15 13:06:26 -0500412 if (1 == mipCount) {
413 mipMapped = GrMipMapped::kNo;
414 }
415 }
416
Brian Salomona56a7462020-02-07 14:17:25 -0500417 if (!caps->validateSurfaceParams(dimensions, format, renderable, renderTargetSampleCnt,
418 mipMapped)) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500419 return nullptr;
420 }
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600421 GrMipMapsStatus mipMapsStatus = (GrMipMapped::kYes == mipMapped)
422 ? GrMipMapsStatus::kDirty
423 : GrMipMapsStatus::kNotAllocated;
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400424 if (renderable == GrRenderable::kYes) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400425 renderTargetSampleCnt =
Greg Daniel6fa62e22019-08-07 15:52:37 -0400426 caps->getRenderTargetSampleCount(renderTargetSampleCnt, format);
427 SkASSERT(renderTargetSampleCnt);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500428 // We know anything we instantiate later from this deferred path will be
429 // both texturable and renderable
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400430 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400431 *caps, format, dimensions, renderTargetSampleCnt, mipMapped, mipMapsStatus, fit,
432 budgeted, isProtected, surfaceFlags, useAllocator));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500433 }
434
Greg Daniel3a365112020-02-14 10:47:18 -0500435 return sk_sp<GrTextureProxy>(new GrTextureProxy(format, dimensions, mipMapped, mipMapsStatus,
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400436 fit, budgeted, isProtected, surfaceFlags,
437 useAllocator));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500438}
439
Brian Salomonbb8dde82019-06-27 10:52:13 -0400440sk_sp<GrTextureProxy> GrProxyProvider::createCompressedTextureProxy(
Robert Phillips3a833922020-01-21 15:25:58 -0500441 SkISize dimensions, SkBudgeted budgeted, GrMipMapped mipMapped, GrProtected isProtected,
Robert Phillipse4720c62020-01-14 14:33:24 -0500442 SkImage::CompressionType compressionType, sk_sp<SkData> data) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500443 ASSERT_SINGLE_OWNER
444 if (this->isAbandoned()) {
445 return nullptr;
446 }
Brian Salomonbb8dde82019-06-27 10:52:13 -0400447
Robert Phillips8ff8bcc2019-07-29 17:03:35 -0400448 GrBackendFormat format = this->caps()->getBackendFormatFromCompressionType(compressionType);
449
Greg Daniel7bfc9132019-08-14 14:23:53 -0400450 if (!this->caps()->isFormatTexturable(format)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500451 return nullptr;
452 }
453
Robert Phillipse4720c62020-01-14 14:33:24 -0500454 GrMipMapsStatus mipMapsStatus = (GrMipMapped::kYes == mipMapped)
455 ? GrMipMapsStatus::kValid
456 : GrMipMapsStatus::kNotAllocated;
457
Jim Van Verthee06b332019-01-18 10:36:32 -0500458 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomona56a7462020-02-07 14:17:25 -0500459 [dimensions, format, budgeted, mipMapped, isProtected,
460 data](GrResourceProvider* resourceProvider) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400461 return LazyCallbackResult(resourceProvider->createCompressedTexture(
Robert Phillips3a833922020-01-21 15:25:58 -0500462 dimensions, format, budgeted, mipMapped, isProtected, data.get()));
Brian Salomonbb8dde82019-06-27 10:52:13 -0400463 },
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400464 format, dimensions, GrRenderable::kNo, 1, mipMapped, mipMapsStatus,
Greg Daniel3a365112020-02-14 10:47:18 -0500465 GrInternalSurfaceFlags::kReadOnly, SkBackingFit::kExact, SkBudgeted::kYes,
466 GrProtected::kNo, UseAllocator::kYes);
Jim Van Verthee06b332019-01-18 10:36:32 -0500467
468 if (!proxy) {
469 return nullptr;
470 }
471
Robert Phillipsa41c6852019-02-07 10:44:10 -0500472 GrContext* direct = fImageContext->priv().asDirectContext();
473 if (direct) {
474 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Jim Van Verthee06b332019-01-18 10:36:32 -0500475 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
476 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500477 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500478 return nullptr;
479 }
480 }
481 return proxy;
482}
483
Brian Salomon7578f3e2018-03-07 14:39:54 -0500484sk_sp<GrTextureProxy> GrProxyProvider::wrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500485 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500486 GrWrapCacheable cacheable,
Brian Salomonc67c31c2018-12-06 10:00:03 -0500487 GrIOType ioType,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500488 ReleaseProc releaseProc,
489 ReleaseContext releaseCtx) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500490 SkASSERT(ioType != kWrite_GrIOType);
Robert Phillipsf9bec202018-01-16 09:21:01 -0500491 if (this->isAbandoned()) {
492 return nullptr;
493 }
494
Brian Salomonf7778972018-03-08 10:13:17 -0500495 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500496 GrContext* direct = fImageContext->priv().asDirectContext();
497 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500498 return nullptr;
499 }
500
Robert Phillipsa41c6852019-02-07 10:44:10 -0500501 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
502
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500503 sk_sp<GrTexture> tex =
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400504 resourceProvider->wrapBackendTexture(backendTex, ownership, cacheable, ioType);
Brian Salomonf7778972018-03-08 10:13:17 -0500505 if (!tex) {
506 return nullptr;
507 }
Robert Phillipsadbe1322018-01-17 13:35:46 -0500508
Greg Daniel6a0176b2018-01-30 09:28:44 -0500509 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500510 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel6a0176b2018-01-30 09:28:44 -0500511 }
512
Brian Salomonf7778972018-03-08 10:13:17 -0500513 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
514 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500515 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500516
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400517 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500518}
519
Robert Phillipsead321b2019-12-19 10:16:32 -0500520sk_sp<GrTextureProxy> GrProxyProvider::wrapCompressedBackendTexture(const GrBackendTexture& beTex,
Robert Phillipsead321b2019-12-19 10:16:32 -0500521 GrWrapOwnership ownership,
522 GrWrapCacheable cacheable,
523 ReleaseProc releaseProc,
524 ReleaseContext releaseCtx) {
525 if (this->isAbandoned()) {
526 return nullptr;
527 }
528
529 // This is only supported on a direct GrContext.
530 GrContext* direct = fImageContext->priv().asDirectContext();
531 if (!direct) {
532 return nullptr;
533 }
534
535 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
536
537 sk_sp<GrTexture> tex = resourceProvider->wrapCompressedBackendTexture(beTex, ownership,
538 cacheable);
539 if (!tex) {
540 return nullptr;
541 }
542
543 if (releaseProc) {
544 tex->setRelease(releaseProc, releaseCtx);
545 }
546
547 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
548 // Make sure we match how we created the proxy with SkBudgeted::kNo
549 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
550
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400551 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), UseAllocator::kNo));
Robert Phillipsead321b2019-12-19 10:16:32 -0500552}
553
Brian Salomon7578f3e2018-03-07 14:39:54 -0500554sk_sp<GrTextureProxy> GrProxyProvider::wrapRenderableBackendTexture(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400555 const GrBackendTexture& backendTex,
556 int sampleCnt,
557 GrWrapOwnership ownership,
558 GrWrapCacheable cacheable,
559 ReleaseProc releaseProc,
Greg Daniel3a365112020-02-14 10:47:18 -0500560 ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500561 if (this->isAbandoned()) {
562 return nullptr;
563 }
564
Brian Salomonf7778972018-03-08 10:13:17 -0500565 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500566 GrContext* direct = fImageContext->priv().asDirectContext();
567 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500568 return nullptr;
569 }
570
Robert Phillips0902c982019-07-16 07:47:56 -0400571 const GrCaps* caps = this->caps();
572
Robert Phillipsa41c6852019-02-07 10:44:10 -0500573 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
574
Greg Daniel6fa62e22019-08-07 15:52:37 -0400575 sampleCnt = caps->getRenderTargetSampleCount(sampleCnt, backendTex.getBackendFormat());
576 SkASSERT(sampleCnt);
577
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400578 sk_sp<GrTexture> tex = resourceProvider->wrapRenderableBackendTexture(
579 backendTex, sampleCnt, ownership, cacheable);
Brian Salomonf7778972018-03-08 10:13:17 -0500580 if (!tex) {
581 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500582 }
583
Greg Daniel8ce79912019-02-05 10:08:43 -0500584 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500585 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500586 }
587
Brian Salomonf7778972018-03-08 10:13:17 -0500588 SkASSERT(tex->asRenderTarget()); // A GrTextureRenderTarget
589 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500590 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Greg Daniel6abda432018-02-15 14:55:00 -0500591
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400592 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500593}
594
Brian Salomon7578f3e2018-03-07 14:39:54 -0500595sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400596 const GrBackendRenderTarget& backendRT,
597 ReleaseProc releaseProc,
Greg Daniel3a365112020-02-14 10:47:18 -0500598 ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500599 if (this->isAbandoned()) {
600 return nullptr;
601 }
602
Brian Salomonf7778972018-03-08 10:13:17 -0500603 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500604 GrContext* direct = fImageContext->priv().asDirectContext();
605 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500606 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500607 }
608
Robert Phillipsa41c6852019-02-07 10:44:10 -0500609 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
610
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400611 sk_sp<GrRenderTarget> rt = resourceProvider->wrapBackendRenderTarget(backendRT);
Brian Salomonf7778972018-03-08 10:13:17 -0500612 if (!rt) {
613 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500614 }
Greg Daniel8ce79912019-02-05 10:08:43 -0500615
Greg Daniel8ce79912019-02-05 10:08:43 -0500616 if (releaseProc) {
Brian Salomon2ca31f82019-03-05 13:28:58 -0500617 rt->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500618 }
619
Brian Salomonf7778972018-03-08 10:13:17 -0500620 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
621 SkASSERT(!rt->getUniqueKey().isValid());
622 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500623 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Brian Salomonf7778972018-03-08 10:13:17 -0500624
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400625 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(std::move(rt), UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500626}
627
Brian Salomon7578f3e2018-03-07 14:39:54 -0500628sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendTextureAsRenderTarget(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400629 const GrBackendTexture& backendTex, int sampleCnt) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500630 if (this->isAbandoned()) {
631 return nullptr;
632 }
633
Brian Salomonf7778972018-03-08 10:13:17 -0500634 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500635 GrContext* direct = fImageContext->priv().asDirectContext();
636 if (!direct) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500637 return nullptr;
638 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500639
Robert Phillipsa41c6852019-02-07 10:44:10 -0500640 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
641
Brian Salomonf7778972018-03-08 10:13:17 -0500642 sk_sp<GrRenderTarget> rt =
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400643 resourceProvider->wrapBackendTextureAsRenderTarget(backendTex, sampleCnt);
Brian Salomonf7778972018-03-08 10:13:17 -0500644 if (!rt) {
645 return nullptr;
Greg Danielf87651e2018-02-21 11:36:53 -0500646 }
Brian Salomonf7778972018-03-08 10:13:17 -0500647 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
648 SkASSERT(!rt->getUniqueKey().isValid());
Greg Danielb46add82019-01-02 14:51:29 -0500649 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500650 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielf87651e2018-02-21 11:36:53 -0500651
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400652 return sk_sp<GrSurfaceProxy>(new GrRenderTargetProxy(std::move(rt), UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500653}
654
Greg Danielb46add82019-01-02 14:51:29 -0500655sk_sp<GrRenderTargetProxy> GrProxyProvider::wrapVulkanSecondaryCBAsRenderTarget(
656 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
657 if (this->isAbandoned()) {
658 return nullptr;
659 }
660
661 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500662 GrContext* direct = fImageContext->priv().asDirectContext();
663 if (!direct) {
Greg Danielb46add82019-01-02 14:51:29 -0500664 return nullptr;
665 }
666
Robert Phillipsa41c6852019-02-07 10:44:10 -0500667 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Danielb46add82019-01-02 14:51:29 -0500668
Robert Phillipsa41c6852019-02-07 10:44:10 -0500669 sk_sp<GrRenderTarget> rt = resourceProvider->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
670 vkInfo);
Greg Danielb46add82019-01-02 14:51:29 -0500671 if (!rt) {
672 return nullptr;
673 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500674
Greg Danielb46add82019-01-02 14:51:29 -0500675 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
676 SkASSERT(!rt->getUniqueKey().isValid());
677 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500678 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielb46add82019-01-02 14:51:29 -0500679
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400680 GrColorType colorType = SkColorTypeToGrColorType(imageInfo.colorType());
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400681
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400682 if (!this->caps()->isFormatAsColorTypeRenderable(colorType, rt->backendFormat(),
683 rt->numSamples())) {
684 return nullptr;
685 }
686
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400687 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
688 std::move(rt), UseAllocator::kNo, GrRenderTargetProxy::WrapsVkSecondaryCB::kYes));
Brian Salomone8a766b2019-07-19 14:24:36 -0400689}
690
691sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
692 const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -0500693 SkISize dimensions,
Brian Salomone8a766b2019-07-19 14:24:36 -0400694 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400695 int renderTargetSampleCnt,
Brian Salomone8a766b2019-07-19 14:24:36 -0400696 GrMipMapped mipMapped,
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600697 GrMipMapsStatus mipMapsStatus,
Brian Salomone8a766b2019-07-19 14:24:36 -0400698 GrInternalSurfaceFlags surfaceFlags,
699 SkBackingFit fit,
700 SkBudgeted budgeted,
701 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400702 GrSurfaceProxy::UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500703 ASSERT_SINGLE_OWNER
704 if (this->isAbandoned()) {
705 return nullptr;
706 }
Brian Salomona56a7462020-02-07 14:17:25 -0500707 SkASSERT((dimensions.fWidth <= 0 && dimensions.fHeight <= 0) ||
708 (dimensions.fWidth > 0 && dimensions.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400709
Robert Phillips0a15cc62019-07-30 12:49:10 -0400710 if (!format.isValid()) {
711 return nullptr;
712 }
713
Brian Salomona56a7462020-02-07 14:17:25 -0500714 if (dimensions.fWidth > this->caps()->maxTextureSize() ||
715 dimensions.fHeight > this->caps()->maxTextureSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400716 return nullptr;
717 }
718
Brian Salomonbeb7f522019-08-30 16:19:42 -0400719 if (renderable == GrRenderable::kYes) {
720 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(*this->caps(),
721 std::move(callback),
722 format,
Brian Salomona56a7462020-02-07 14:17:25 -0500723 dimensions,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400724 renderTargetSampleCnt,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400725 mipMapped,
726 mipMapsStatus,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400727 fit,
728 budgeted,
729 isProtected,
730 surfaceFlags,
731 useAllocator));
732 } else {
733 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(callback),
734 format,
Brian Salomona56a7462020-02-07 14:17:25 -0500735 dimensions,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400736 mipMapped,
737 mipMapsStatus,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400738 fit,
739 budgeted,
740 isProtected,
741 surfaceFlags,
742 useAllocator));
743 }
Robert Phillips777707b2018-01-17 11:40:14 -0500744}
745
Robert Phillipse8fabb22018-02-04 14:33:21 -0500746sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy(
Brian Salomonbeb7f522019-08-30 16:19:42 -0400747 LazyInstantiateCallback&& callback,
748 const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -0500749 SkISize dimensions,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400750 int sampleCnt,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400751 GrInternalSurfaceFlags surfaceFlags,
752 const TextureInfo* textureInfo,
753 GrMipMapsStatus mipMapsStatus,
754 SkBackingFit fit,
755 SkBudgeted budgeted,
756 GrProtected isProtected,
757 bool wrapsVkSecondaryCB,
758 UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500759 ASSERT_SINGLE_OWNER
760 if (this->isAbandoned()) {
761 return nullptr;
762 }
Brian Salomona56a7462020-02-07 14:17:25 -0500763 SkASSERT((dimensions.fWidth <= 0 && dimensions.fHeight <= 0) ||
764 (dimensions.fWidth > 0 && dimensions.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400765
Brian Salomona56a7462020-02-07 14:17:25 -0500766 if (dimensions.fWidth > this->caps()->maxRenderTargetSize() ||
767 dimensions.fHeight > this->caps()->maxRenderTargetSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400768 return nullptr;
769 }
770
Brian Salomon7226c232018-07-30 13:13:17 -0400771 if (textureInfo) {
Greg Danielb085fa92019-03-05 16:55:12 -0500772 // Wrapped vulkan secondary command buffers don't support texturing since we won't have an
773 // actual VkImage to texture from.
774 SkASSERT(!wrapsVkSecondaryCB);
Brian Salomon7226c232018-07-30 13:13:17 -0400775 return sk_sp<GrRenderTargetProxy>(new GrTextureRenderTargetProxy(
Greg Daniel3a365112020-02-14 10:47:18 -0500776 *this->caps(), std::move(callback), format, dimensions, sampleCnt,
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400777 textureInfo->fMipMapped, mipMapsStatus, fit, budgeted, isProtected, surfaceFlags,
778 useAllocator));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500779 }
780
Greg Danielb085fa92019-03-05 16:55:12 -0500781 GrRenderTargetProxy::WrapsVkSecondaryCB vkSCB =
782 wrapsVkSecondaryCB ? GrRenderTargetProxy::WrapsVkSecondaryCB::kYes
783 : GrRenderTargetProxy::WrapsVkSecondaryCB::kNo;
784
Greg Daniel3a365112020-02-14 10:47:18 -0500785 return sk_sp<GrRenderTargetProxy>(
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400786 new GrRenderTargetProxy(std::move(callback), format, dimensions, sampleCnt, fit,
787 budgeted, isProtected, surfaceFlags, useAllocator, vkSCB));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500788}
789
Brian Salomonbeb7f522019-08-30 16:19:42 -0400790sk_sp<GrTextureProxy> GrProxyProvider::MakeFullyLazyProxy(LazyInstantiateCallback&& callback,
791 const GrBackendFormat& format,
792 GrRenderable renderable,
793 int renderTargetSampleCnt,
794 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400795 const GrCaps& caps,
796 UseAllocator useAllocator) {
Robert Phillips0a15cc62019-07-30 12:49:10 -0400797 if (!format.isValid()) {
798 return nullptr;
799 }
800
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400801 SkASSERT(renderTargetSampleCnt == 1 || renderable == GrRenderable::kYes);
Robert Phillips10d17212019-04-24 14:09:10 -0400802 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNone;
Robert Phillips777707b2018-01-17 11:40:14 -0500803
Brian Salomona56a7462020-02-07 14:17:25 -0500804 static constexpr SkISize kLazyDims = {-1, -1};
Brian Salomonbeb7f522019-08-30 16:19:42 -0400805 if (GrRenderable::kYes == renderable) {
806 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
Greg Daniel3a365112020-02-14 10:47:18 -0500807 caps, std::move(callback), format, kLazyDims, renderTargetSampleCnt,
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400808 GrMipMapped::kNo, GrMipMapsStatus::kNotAllocated, SkBackingFit::kApprox,
Greg Daniel14b57212019-12-17 16:18:06 -0500809 SkBudgeted::kYes, isProtected, surfaceFlags, useAllocator));
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400810 } else {
811 return sk_sp<GrTextureProxy>(
812 new GrTextureProxy(std::move(callback), format, kLazyDims, GrMipMapped::kNo,
813 GrMipMapsStatus::kNotAllocated, SkBackingFit::kApprox,
814 SkBudgeted::kYes, isProtected, surfaceFlags, useAllocator));
Brian Salomonbeb7f522019-08-30 16:19:42 -0400815 }
Robert Phillips777707b2018-01-17 11:40:14 -0500816}
817
Robert Phillips427966a2018-12-20 17:20:43 -0500818void GrProxyProvider::processInvalidUniqueKey(const GrUniqueKey& key, GrTextureProxy* proxy,
819 InvalidateGPUResource invalidateGPUResource) {
Mike Klein04ef8102020-03-16 12:44:23 -0500820 this->processInvalidUniqueKeyImpl(key, proxy, invalidateGPUResource, RemoveTableEntry::kYes);
821}
822
823void GrProxyProvider::processInvalidUniqueKeyImpl(const GrUniqueKey& key, GrTextureProxy* proxy,
824 InvalidateGPUResource invalidateGPUResource,
825 RemoveTableEntry removeTableEntry) {
Chris Dalton2de13dd2019-01-03 15:11:59 -0700826 SkASSERT(key.isValid());
827
Robert Phillips427966a2018-12-20 17:20:43 -0500828 if (!proxy) {
829 proxy = fUniquelyKeyedProxies.find(key);
830 }
Chris Dalton2de13dd2019-01-03 15:11:59 -0700831 SkASSERT(!proxy || proxy->getUniqueKey() == key);
832
833 // Locate the corresponding GrGpuResource (if it needs to be invalidated) before clearing the
834 // proxy's unique key. We must do it in this order because 'key' may alias the proxy's key.
835 sk_sp<GrGpuResource> invalidGpuResource;
836 if (InvalidateGPUResource::kYes == invalidateGPUResource) {
Brian Salomon01ceae92019-04-02 11:49:54 -0400837 GrContext* direct = fImageContext->priv().asDirectContext();
838 if (direct) {
839 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
840 invalidGpuResource = resourceProvider->findByUniqueKey<GrGpuResource>(key);
Chris Dalton2de13dd2019-01-03 15:11:59 -0700841 }
842 SkASSERT(!invalidGpuResource || invalidGpuResource->getUniqueKey() == key);
843 }
Robert Phillips427966a2018-12-20 17:20:43 -0500844
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500845 // Note: this method is called for the whole variety of GrGpuResources so often 'key'
846 // will not be in 'fUniquelyKeyedProxies'.
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500847 if (proxy) {
Mike Klein04ef8102020-03-16 12:44:23 -0500848 if (removeTableEntry == RemoveTableEntry::kYes) {
849 fUniquelyKeyedProxies.remove(key);
850 }
Robert Phillips427966a2018-12-20 17:20:43 -0500851 proxy->cacheAccess().clearUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500852 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500853
Chris Dalton2de13dd2019-01-03 15:11:59 -0700854 if (invalidGpuResource) {
855 invalidGpuResource->resourcePriv().removeUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500856 }
857}
858
Robert Phillipsa41c6852019-02-07 10:44:10 -0500859uint32_t GrProxyProvider::contextID() const {
860 return fImageContext->priv().contextID();
861}
862
863const GrCaps* GrProxyProvider::caps() const {
864 return fImageContext->priv().caps();
865}
866
867sk_sp<const GrCaps> GrProxyProvider::refCaps() const {
868 return fImageContext->priv().refCaps();
869}
870
Robert Phillipsa9162df2019-02-11 14:12:03 -0500871bool GrProxyProvider::isAbandoned() const {
872 return fImageContext->priv().abandoned();
873}
874
Robert Phillips0790f8a2018-09-18 13:11:03 -0400875void GrProxyProvider::orphanAllUniqueKeys() {
Mike Kleincff63962020-03-14 16:22:45 -0500876 fUniquelyKeyedProxies.foreach([&](GrTextureProxy* proxy){
877 proxy->fProxyProvider = nullptr;
878 });
Robert Phillips0790f8a2018-09-18 13:11:03 -0400879}
880
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500881void GrProxyProvider::removeAllUniqueKeys() {
Mike Klein04ef8102020-03-16 12:44:23 -0500882 fUniquelyKeyedProxies.foreach([&](GrTextureProxy* proxy){
883 // It's not safe to remove table entries while iterating with foreach(),
884 // but since we're going to remove them all anyway, simply save that for the end.
885 this->processInvalidUniqueKeyImpl(proxy->getUniqueKey(), proxy,
886 InvalidateGPUResource::kNo,
887 RemoveTableEntry::kNo);
888 });
889 // Removing all those table entries is safe now.
890 fUniquelyKeyedProxies.reset();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500891}
Robert Phillipsa41c6852019-02-07 10:44:10 -0500892
893bool GrProxyProvider::renderingDirectly() const {
894 return fImageContext->priv().asDirectContext();
895}