blob: 60ada6c5ecf6e21866ebf0655c7ddd35cd226752 [file] [log] [blame]
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001/*
2 * Copyright 2018 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrProxyProvider.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkBitmap.h"
11#include "include/core/SkImage.h"
12#include "include/gpu/GrContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/private/GrImageContext.h"
14#include "include/private/GrResourceKey.h"
15#include "include/private/GrSingleOwner.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/private/SkImageInfoPriv.h"
17#include "src/core/SkAutoPixmapStorage.h"
Robert Phillips99dead92020-01-27 16:11:57 -050018#include "src/core/SkCompressedDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/core/SkImagePriv.h"
20#include "src/core/SkMipMap.h"
21#include "src/core/SkTraceEvent.h"
22#include "src/gpu/GrCaps.h"
23#include "src/gpu/GrContextPriv.h"
24#include "src/gpu/GrImageContextPriv.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040025#include "src/gpu/GrRenderTarget.h"
Brian Salomond005b692020-04-01 15:47:05 -040026#include "src/gpu/GrRenderTargetContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/GrResourceProvider.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040028#include "src/gpu/GrSurfaceProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/gpu/GrSurfaceProxyPriv.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000030#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "src/gpu/GrTextureProxyCacheAccess.h"
32#include "src/gpu/GrTextureRenderTargetProxy.h"
33#include "src/gpu/SkGr.h"
34#include "src/image/SkImage_Base.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050035
36#define ASSERT_SINGLE_OWNER \
Robert Phillipsa41c6852019-02-07 10:44:10 -050037 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fImageContext->priv().singleOwner());)
Robert Phillips1afd4cd2018-01-08 13:40:32 -050038
Robert Phillipsa9162df2019-02-11 14:12:03 -050039GrProxyProvider::GrProxyProvider(GrImageContext* imageContext) : fImageContext(imageContext) {}
Robert Phillips1afd4cd2018-01-08 13:40:32 -050040
41GrProxyProvider::~GrProxyProvider() {
Robert Phillipsa41c6852019-02-07 10:44:10 -050042 if (this->renderingDirectly()) {
Robert Phillips0790f8a2018-09-18 13:11:03 -040043 // In DDL-mode a proxy provider can still have extant uniquely keyed proxies (since
44 // they need their unique keys to, potentially, find a cached resource when the
45 // DDL is played) but, in non-DDL-mode they should all have been cleaned up by this point.
46 SkASSERT(!fUniquelyKeyedProxies.count());
47 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050048}
49
Robert Phillipsadbe1322018-01-17 13:35:46 -050050bool GrProxyProvider::assignUniqueKeyToProxy(const GrUniqueKey& key, GrTextureProxy* proxy) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050051 ASSERT_SINGLE_OWNER
52 SkASSERT(key.isValid());
53 if (this->isAbandoned() || !proxy) {
Robert Phillipsadbe1322018-01-17 13:35:46 -050054 return false;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050055 }
56
Robert 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 Salomond005b692020-04-01 15:47:05 -0400233GrSurfaceProxyView GrProxyProvider::findCachedProxyWithColorTypeFallback(const GrUniqueKey& key,
234 GrSurfaceOrigin origin,
Brian Salomon0029db02020-04-03 10:41:24 -0400235 GrColorType ct,
236 int sampleCnt) {
Brian Salomond005b692020-04-01 15:47:05 -0400237 auto proxy = this->findOrCreateProxyByUniqueKey(key);
238 if (!proxy) {
239 return {};
240 }
241 // Assume that we used a fallback color type if and only if the proxy is renderable.
242 if (proxy->asRenderTargetProxy()) {
243 GrBackendFormat expectedFormat;
244 std::tie(ct, expectedFormat) =
Brian Salomon0029db02020-04-03 10:41:24 -0400245 GrRenderTargetContext::GetFallbackColorTypeAndFormat(fImageContext, ct, sampleCnt);
Brian Salomond005b692020-04-01 15:47:05 -0400246 SkASSERT(expectedFormat == proxy->backendFormat());
247 }
248 GrSwizzle swizzle = fImageContext->priv().caps()->getReadSwizzle(proxy->backendFormat(), ct);
249 return {std::move(proxy), origin, swizzle};
250}
251
Brian Osmande496652019-03-22 13:42:33 -0400252sk_sp<GrTextureProxy> GrProxyProvider::createProxyFromBitmap(const SkBitmap& bitmap,
Greg Daniel6f5441a2020-01-28 17:02:49 -0500253 GrMipMapped mipMapped,
Brian Salomonbc074a62020-03-18 10:06:13 -0400254 SkBackingFit fit,
255 SkBudgeted budgeted) {
Brian Osman412674f2019-02-07 15:34:58 -0500256 ASSERT_SINGLE_OWNER
Greg Daniel6f5441a2020-01-28 17:02:49 -0500257 SkASSERT(fit == SkBackingFit::kExact || mipMapped == GrMipMapped::kNo);
Brian Osman412674f2019-02-07 15:34:58 -0500258
259 if (this->isAbandoned()) {
260 return nullptr;
261 }
262
Brian Osman2b23c4b2018-06-01 12:25:08 -0400263 if (!SkImageInfoIsValid(bitmap.info())) {
Greg Daniela4ead652018-02-07 10:21:48 -0500264 return nullptr;
265 }
266
Brian Osmande496652019-03-22 13:42:33 -0400267 ATRACE_ANDROID_FRAMEWORK("Upload %sTexture [%ux%u]",
268 GrMipMapped::kYes == mipMapped ? "MipMap " : "",
269 bitmap.width(), bitmap.height());
Greg Daniela4ead652018-02-07 10:21:48 -0500270
271 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
272 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
273 // 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 -0500274 SkBitmap copyBitmap = bitmap;
275 if (!this->renderingDirectly() && !bitmap.isImmutable()) {
276 copyBitmap.allocPixels();
277 if (!bitmap.readPixels(copyBitmap.pixmap())) {
278 return nullptr;
279 }
280 copyBitmap.setImmutable();
Greg Daniela4ead652018-02-07 10:21:48 -0500281 }
282
Greg Daniel6f5441a2020-01-28 17:02:49 -0500283 sk_sp<GrTextureProxy> proxy;
284 if (mipMapped == GrMipMapped::kNo ||
285 0 == SkMipMap::ComputeLevelCount(copyBitmap.width(), copyBitmap.height())) {
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400286 proxy = this->createNonMippedProxyFromBitmap(copyBitmap, fit, budgeted);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500287 } else {
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400288 proxy = this->createMippedProxyFromBitmap(copyBitmap, budgeted);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500289 }
290
291 if (!proxy) {
292 return nullptr;
293 }
294
295 GrContext* direct = fImageContext->priv().asDirectContext();
296 if (direct) {
297 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
298
299 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
300 // we're better off instantiating the proxy immediately here.
301 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
302 return nullptr;
303 }
304 }
305 return proxy;
306}
307
308sk_sp<GrTextureProxy> GrProxyProvider::createNonMippedProxyFromBitmap(const SkBitmap& bitmap,
309 SkBackingFit fit,
Brian Salomonbc074a62020-03-18 10:06:13 -0400310 SkBudgeted budgeted) {
Brian Salomona56a7462020-02-07 14:17:25 -0500311 auto dims = bitmap.dimensions();
Greg Daniel6f5441a2020-01-28 17:02:49 -0500312
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400313 auto colorType = SkColorTypeToGrColorType(bitmap.colorType());
314 GrBackendFormat format = this->caps()->getDefaultBackendFormat(colorType, GrRenderable::kNo);
315 if (!format.isValid()) {
316 return nullptr;
317 }
318
Greg Daniel6f5441a2020-01-28 17:02:49 -0500319 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomonbc074a62020-03-18 10:06:13 -0400320 [dims, format, bitmap, fit, colorType, budgeted](GrResourceProvider* resourceProvider) {
Greg Daniel6f5441a2020-01-28 17:02:49 -0500321 GrMipLevel mipLevel = { bitmap.getPixels(), bitmap.rowBytes() };
322
323 return LazyCallbackResult(resourceProvider->createTexture(
Brian Salomonbc074a62020-03-18 10:06:13 -0400324 dims, format, colorType, GrRenderable::kNo, 1, budgeted, fit,
Greg Daniel6f5441a2020-01-28 17:02:49 -0500325 GrProtected::kNo, mipLevel));
326 },
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400327 format, dims, GrRenderable::kNo, 1, GrMipMapped::kNo, GrMipMapsStatus::kNotAllocated,
328 GrInternalSurfaceFlags::kNone, fit, budgeted, GrProtected::kNo, UseAllocator::kYes);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500329
330 if (!proxy) {
331 return nullptr;
332 }
Brian Salomona56a7462020-02-07 14:17:25 -0500333 SkASSERT(proxy->dimensions() == bitmap.dimensions());
Greg Daniel6f5441a2020-01-28 17:02:49 -0500334 return proxy;
335}
336
337sk_sp<GrTextureProxy> GrProxyProvider::createMippedProxyFromBitmap(const SkBitmap& bitmap,
Brian Salomonbc074a62020-03-18 10:06:13 -0400338 SkBudgeted budgeted) {
Greg Daniel6f5441a2020-01-28 17:02:49 -0500339 SkASSERT(this->caps()->mipMapSupport());
340
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400341 auto colorType = SkColorTypeToGrColorType(bitmap.colorType());
342 GrBackendFormat format = this->caps()->getDefaultBackendFormat(colorType, GrRenderable::kNo);
343 if (!format.isValid()) {
344 return nullptr;
345 }
Greg Daniel6f5441a2020-01-28 17:02:49 -0500346
347 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(bitmap.pixmap(), nullptr));
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400348 if (!mipmaps) {
349 return nullptr;
350 }
351
Brian Salomona56a7462020-02-07 14:17:25 -0500352 auto dims = bitmap.dimensions();
Greg Danielce3ddaa2020-01-22 16:58:15 -0500353
Greg Daniela4ead652018-02-07 10:21:48 -0500354 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomonbc074a62020-03-18 10:06:13 -0400355 [dims, format, bitmap, mipmaps, budgeted](GrResourceProvider* resourceProvider) {
Brian Osman1b97f132018-09-13 17:33:48 +0000356 const int mipLevelCount = mipmaps->countLevels() + 1;
357 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
358
Greg Daniel6f5441a2020-01-28 17:02:49 -0500359 texels[0].fPixels = bitmap.getPixels();
360 texels[0].fRowBytes = bitmap.rowBytes();
Greg Daniela4ead652018-02-07 10:21:48 -0500361
Greg Daniel6f5441a2020-01-28 17:02:49 -0500362 auto colorType = SkColorTypeToGrColorType(bitmap.colorType());
Greg Daniela4ead652018-02-07 10:21:48 -0500363 for (int i = 1; i < mipLevelCount; ++i) {
364 SkMipMap::Level generatedMipLevel;
365 mipmaps->getLevel(i - 1, &generatedMipLevel);
366 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
367 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
368 SkASSERT(texels[i].fPixels);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500369 SkASSERT(generatedMipLevel.fPixmap.colorType() == bitmap.colorType());
Greg Daniela4ead652018-02-07 10:21:48 -0500370 }
Brian Salomonbeb7f522019-08-30 16:19:42 -0400371 return LazyCallbackResult(resourceProvider->createTexture(
Brian Salomonbc074a62020-03-18 10:06:13 -0400372 dims, format, colorType, GrRenderable::kNo, 1, budgeted, GrProtected::kNo,
373 texels.get(), mipLevelCount));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500374 },
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400375 format, dims, GrRenderable::kNo, 1, GrMipMapped::kYes, GrMipMapsStatus::kValid,
376 GrInternalSurfaceFlags::kNone, SkBackingFit::kExact, budgeted, GrProtected::kNo,
377 UseAllocator::kYes);
Greg Daniela4ead652018-02-07 10:21:48 -0500378
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400379 if (!proxy) {
380 return nullptr;
381 }
382
Brian Salomona56a7462020-02-07 14:17:25 -0500383 SkASSERT(proxy->dimensions() == bitmap.dimensions());
Greg Daniel6f5441a2020-01-28 17:02:49 -0500384
Greg Daniela4ead652018-02-07 10:21:48 -0500385 return proxy;
386}
387
Greg Daniel4065d452018-11-16 15:43:41 -0500388sk_sp<GrTextureProxy> GrProxyProvider::createProxy(const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -0500389 SkISize dimensions,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400390 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400391 int renderTargetSampleCnt,
Greg Danielf6f7b672018-02-15 13:06:26 -0500392 GrMipMapped mipMapped,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500393 SkBackingFit fit,
394 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -0400395 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400396 GrInternalSurfaceFlags surfaceFlags,
397 GrSurfaceProxy::UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500398 ASSERT_SINGLE_OWNER
399 if (this->isAbandoned()) {
400 return nullptr;
401 }
402
Robert Phillips8ff8bcc2019-07-29 17:03:35 -0400403 const GrCaps* caps = this->caps();
404
405 if (caps->isFormatCompressed(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400406 // Deferred proxies for compressed textures are not supported.
407 return nullptr;
408 }
Robert Phillips0902c982019-07-16 07:47:56 -0400409
Greg Danielf6f7b672018-02-15 13:06:26 -0500410 if (GrMipMapped::kYes == mipMapped) {
411 // SkMipMap doesn't include the base level in the level count so we have to add 1
Brian Salomona56a7462020-02-07 14:17:25 -0500412 int mipCount = SkMipMap::ComputeLevelCount(dimensions.fWidth, dimensions.fHeight) + 1;
Greg Danielf6f7b672018-02-15 13:06:26 -0500413 if (1 == mipCount) {
414 mipMapped = GrMipMapped::kNo;
415 }
416 }
417
Brian Salomona56a7462020-02-07 14:17:25 -0500418 if (!caps->validateSurfaceParams(dimensions, format, renderable, renderTargetSampleCnt,
419 mipMapped)) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500420 return nullptr;
421 }
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600422 GrMipMapsStatus mipMapsStatus = (GrMipMapped::kYes == mipMapped)
423 ? GrMipMapsStatus::kDirty
424 : GrMipMapsStatus::kNotAllocated;
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400425 if (renderable == GrRenderable::kYes) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400426 renderTargetSampleCnt =
Greg Daniel6fa62e22019-08-07 15:52:37 -0400427 caps->getRenderTargetSampleCount(renderTargetSampleCnt, format);
428 SkASSERT(renderTargetSampleCnt);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500429 // We know anything we instantiate later from this deferred path will be
430 // both texturable and renderable
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400431 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400432 *caps, format, dimensions, renderTargetSampleCnt, mipMapped, mipMapsStatus, fit,
433 budgeted, isProtected, surfaceFlags, useAllocator));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500434 }
435
Greg Daniel3a365112020-02-14 10:47:18 -0500436 return sk_sp<GrTextureProxy>(new GrTextureProxy(format, dimensions, mipMapped, mipMapsStatus,
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400437 fit, budgeted, isProtected, surfaceFlags,
438 useAllocator));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500439}
440
Brian Salomonbb8dde82019-06-27 10:52:13 -0400441sk_sp<GrTextureProxy> GrProxyProvider::createCompressedTextureProxy(
Robert Phillips3a833922020-01-21 15:25:58 -0500442 SkISize dimensions, SkBudgeted budgeted, GrMipMapped mipMapped, GrProtected isProtected,
Robert Phillipse4720c62020-01-14 14:33:24 -0500443 SkImage::CompressionType compressionType, sk_sp<SkData> data) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500444 ASSERT_SINGLE_OWNER
445 if (this->isAbandoned()) {
446 return nullptr;
447 }
Brian Salomonbb8dde82019-06-27 10:52:13 -0400448
Robert Phillips8ff8bcc2019-07-29 17:03:35 -0400449 GrBackendFormat format = this->caps()->getBackendFormatFromCompressionType(compressionType);
450
Greg Daniel7bfc9132019-08-14 14:23:53 -0400451 if (!this->caps()->isFormatTexturable(format)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500452 return nullptr;
453 }
454
Robert Phillipse4720c62020-01-14 14:33:24 -0500455 GrMipMapsStatus mipMapsStatus = (GrMipMapped::kYes == mipMapped)
456 ? GrMipMapsStatus::kValid
457 : GrMipMapsStatus::kNotAllocated;
458
Jim Van Verthee06b332019-01-18 10:36:32 -0500459 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomona56a7462020-02-07 14:17:25 -0500460 [dimensions, format, budgeted, mipMapped, isProtected,
461 data](GrResourceProvider* resourceProvider) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400462 return LazyCallbackResult(resourceProvider->createCompressedTexture(
Robert Phillips3a833922020-01-21 15:25:58 -0500463 dimensions, format, budgeted, mipMapped, isProtected, data.get()));
Brian Salomonbb8dde82019-06-27 10:52:13 -0400464 },
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400465 format, dimensions, GrRenderable::kNo, 1, mipMapped, mipMapsStatus,
Greg Daniel3a365112020-02-14 10:47:18 -0500466 GrInternalSurfaceFlags::kReadOnly, SkBackingFit::kExact, SkBudgeted::kYes,
467 GrProtected::kNo, UseAllocator::kYes);
Jim Van Verthee06b332019-01-18 10:36:32 -0500468
469 if (!proxy) {
470 return nullptr;
471 }
472
Robert Phillipsa41c6852019-02-07 10:44:10 -0500473 GrContext* direct = fImageContext->priv().asDirectContext();
474 if (direct) {
475 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Jim Van Verthee06b332019-01-18 10:36:32 -0500476 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
477 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500478 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500479 return nullptr;
480 }
481 }
482 return proxy;
483}
484
Brian Salomon7578f3e2018-03-07 14:39:54 -0500485sk_sp<GrTextureProxy> GrProxyProvider::wrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500486 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500487 GrWrapCacheable cacheable,
Brian Salomonc67c31c2018-12-06 10:00:03 -0500488 GrIOType ioType,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500489 ReleaseProc releaseProc,
490 ReleaseContext releaseCtx) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500491 SkASSERT(ioType != kWrite_GrIOType);
Robert Phillipsf9bec202018-01-16 09:21:01 -0500492 if (this->isAbandoned()) {
493 return nullptr;
494 }
495
Brian Salomonf7778972018-03-08 10:13:17 -0500496 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500497 GrContext* direct = fImageContext->priv().asDirectContext();
498 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500499 return nullptr;
500 }
501
Robert Phillipsa41c6852019-02-07 10:44:10 -0500502 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
503
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500504 sk_sp<GrTexture> tex =
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400505 resourceProvider->wrapBackendTexture(backendTex, ownership, cacheable, ioType);
Brian Salomonf7778972018-03-08 10:13:17 -0500506 if (!tex) {
507 return nullptr;
508 }
Robert Phillipsadbe1322018-01-17 13:35:46 -0500509
Greg Daniel6a0176b2018-01-30 09:28:44 -0500510 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500511 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel6a0176b2018-01-30 09:28:44 -0500512 }
513
Brian Salomonf7778972018-03-08 10:13:17 -0500514 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
515 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500516 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500517
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400518 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500519}
520
Robert Phillipsead321b2019-12-19 10:16:32 -0500521sk_sp<GrTextureProxy> GrProxyProvider::wrapCompressedBackendTexture(const GrBackendTexture& beTex,
Robert Phillipsead321b2019-12-19 10:16:32 -0500522 GrWrapOwnership ownership,
523 GrWrapCacheable cacheable,
524 ReleaseProc releaseProc,
525 ReleaseContext releaseCtx) {
526 if (this->isAbandoned()) {
527 return nullptr;
528 }
529
530 // This is only supported on a direct GrContext.
531 GrContext* direct = fImageContext->priv().asDirectContext();
532 if (!direct) {
533 return nullptr;
534 }
535
536 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
537
538 sk_sp<GrTexture> tex = resourceProvider->wrapCompressedBackendTexture(beTex, ownership,
539 cacheable);
540 if (!tex) {
541 return nullptr;
542 }
543
544 if (releaseProc) {
545 tex->setRelease(releaseProc, releaseCtx);
546 }
547
548 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
549 // Make sure we match how we created the proxy with SkBudgeted::kNo
550 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
551
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400552 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), UseAllocator::kNo));
Robert Phillipsead321b2019-12-19 10:16:32 -0500553}
554
Brian Salomon7578f3e2018-03-07 14:39:54 -0500555sk_sp<GrTextureProxy> GrProxyProvider::wrapRenderableBackendTexture(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400556 const GrBackendTexture& backendTex,
557 int sampleCnt,
558 GrWrapOwnership ownership,
559 GrWrapCacheable cacheable,
560 ReleaseProc releaseProc,
Greg Daniel3a365112020-02-14 10:47:18 -0500561 ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500562 if (this->isAbandoned()) {
563 return nullptr;
564 }
565
Brian Salomonf7778972018-03-08 10:13:17 -0500566 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500567 GrContext* direct = fImageContext->priv().asDirectContext();
568 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500569 return nullptr;
570 }
571
Robert Phillips0902c982019-07-16 07:47:56 -0400572 const GrCaps* caps = this->caps();
573
Robert Phillipsa41c6852019-02-07 10:44:10 -0500574 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
575
Greg Daniel6fa62e22019-08-07 15:52:37 -0400576 sampleCnt = caps->getRenderTargetSampleCount(sampleCnt, backendTex.getBackendFormat());
577 SkASSERT(sampleCnt);
578
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400579 sk_sp<GrTexture> tex = resourceProvider->wrapRenderableBackendTexture(
580 backendTex, sampleCnt, ownership, cacheable);
Brian Salomonf7778972018-03-08 10:13:17 -0500581 if (!tex) {
582 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500583 }
584
Greg Daniel8ce79912019-02-05 10:08:43 -0500585 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500586 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500587 }
588
Brian Salomonf7778972018-03-08 10:13:17 -0500589 SkASSERT(tex->asRenderTarget()); // A GrTextureRenderTarget
590 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500591 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Greg Daniel6abda432018-02-15 14:55:00 -0500592
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400593 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500594}
595
Brian Salomon7578f3e2018-03-07 14:39:54 -0500596sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400597 const GrBackendRenderTarget& backendRT,
598 ReleaseProc releaseProc,
Greg Daniel3a365112020-02-14 10:47:18 -0500599 ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500600 if (this->isAbandoned()) {
601 return nullptr;
602 }
603
Brian Salomonf7778972018-03-08 10:13:17 -0500604 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500605 GrContext* direct = fImageContext->priv().asDirectContext();
606 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500607 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500608 }
609
Robert Phillipsa41c6852019-02-07 10:44:10 -0500610 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
611
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400612 sk_sp<GrRenderTarget> rt = resourceProvider->wrapBackendRenderTarget(backendRT);
Brian Salomonf7778972018-03-08 10:13:17 -0500613 if (!rt) {
614 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500615 }
Greg Daniel8ce79912019-02-05 10:08:43 -0500616
Greg Daniel8ce79912019-02-05 10:08:43 -0500617 if (releaseProc) {
Brian Salomon2ca31f82019-03-05 13:28:58 -0500618 rt->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500619 }
620
Brian Salomonf7778972018-03-08 10:13:17 -0500621 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
622 SkASSERT(!rt->getUniqueKey().isValid());
623 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500624 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Brian Salomonf7778972018-03-08 10:13:17 -0500625
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400626 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(std::move(rt), UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500627}
628
Brian Salomon7578f3e2018-03-07 14:39:54 -0500629sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendTextureAsRenderTarget(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400630 const GrBackendTexture& backendTex, int sampleCnt) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500631 if (this->isAbandoned()) {
632 return nullptr;
633 }
634
Brian Salomonf7778972018-03-08 10:13:17 -0500635 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500636 GrContext* direct = fImageContext->priv().asDirectContext();
637 if (!direct) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500638 return nullptr;
639 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500640
Robert Phillipsa41c6852019-02-07 10:44:10 -0500641 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
642
Brian Salomonf7778972018-03-08 10:13:17 -0500643 sk_sp<GrRenderTarget> rt =
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400644 resourceProvider->wrapBackendTextureAsRenderTarget(backendTex, sampleCnt);
Brian Salomonf7778972018-03-08 10:13:17 -0500645 if (!rt) {
646 return nullptr;
Greg Danielf87651e2018-02-21 11:36:53 -0500647 }
Brian Salomonf7778972018-03-08 10:13:17 -0500648 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
649 SkASSERT(!rt->getUniqueKey().isValid());
Greg Danielb46add82019-01-02 14:51:29 -0500650 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500651 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielf87651e2018-02-21 11:36:53 -0500652
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400653 return sk_sp<GrSurfaceProxy>(new GrRenderTargetProxy(std::move(rt), UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500654}
655
Greg Danielb46add82019-01-02 14:51:29 -0500656sk_sp<GrRenderTargetProxy> GrProxyProvider::wrapVulkanSecondaryCBAsRenderTarget(
657 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
658 if (this->isAbandoned()) {
659 return nullptr;
660 }
661
662 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500663 GrContext* direct = fImageContext->priv().asDirectContext();
664 if (!direct) {
Greg Danielb46add82019-01-02 14:51:29 -0500665 return nullptr;
666 }
667
Robert Phillipsa41c6852019-02-07 10:44:10 -0500668 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Danielb46add82019-01-02 14:51:29 -0500669
Robert Phillipsa41c6852019-02-07 10:44:10 -0500670 sk_sp<GrRenderTarget> rt = resourceProvider->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
671 vkInfo);
Greg Danielb46add82019-01-02 14:51:29 -0500672 if (!rt) {
673 return nullptr;
674 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500675
Greg Danielb46add82019-01-02 14:51:29 -0500676 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
677 SkASSERT(!rt->getUniqueKey().isValid());
678 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500679 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielb46add82019-01-02 14:51:29 -0500680
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400681 GrColorType colorType = SkColorTypeToGrColorType(imageInfo.colorType());
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400682
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400683 if (!this->caps()->isFormatAsColorTypeRenderable(colorType, rt->backendFormat(),
684 rt->numSamples())) {
685 return nullptr;
686 }
687
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400688 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
689 std::move(rt), UseAllocator::kNo, GrRenderTargetProxy::WrapsVkSecondaryCB::kYes));
Brian Salomone8a766b2019-07-19 14:24:36 -0400690}
691
692sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
693 const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -0500694 SkISize dimensions,
Brian Salomone8a766b2019-07-19 14:24:36 -0400695 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400696 int renderTargetSampleCnt,
Brian Salomone8a766b2019-07-19 14:24:36 -0400697 GrMipMapped mipMapped,
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600698 GrMipMapsStatus mipMapsStatus,
Brian Salomone8a766b2019-07-19 14:24:36 -0400699 GrInternalSurfaceFlags surfaceFlags,
700 SkBackingFit fit,
701 SkBudgeted budgeted,
702 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400703 GrSurfaceProxy::UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500704 ASSERT_SINGLE_OWNER
705 if (this->isAbandoned()) {
706 return nullptr;
707 }
Brian Salomona56a7462020-02-07 14:17:25 -0500708 SkASSERT((dimensions.fWidth <= 0 && dimensions.fHeight <= 0) ||
709 (dimensions.fWidth > 0 && dimensions.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400710
Robert Phillips0a15cc62019-07-30 12:49:10 -0400711 if (!format.isValid()) {
712 return nullptr;
713 }
714
Brian Salomona56a7462020-02-07 14:17:25 -0500715 if (dimensions.fWidth > this->caps()->maxTextureSize() ||
716 dimensions.fHeight > this->caps()->maxTextureSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400717 return nullptr;
718 }
719
Brian Salomonbeb7f522019-08-30 16:19:42 -0400720 if (renderable == GrRenderable::kYes) {
721 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(*this->caps(),
722 std::move(callback),
723 format,
Brian Salomona56a7462020-02-07 14:17:25 -0500724 dimensions,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400725 renderTargetSampleCnt,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400726 mipMapped,
727 mipMapsStatus,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400728 fit,
729 budgeted,
730 isProtected,
731 surfaceFlags,
732 useAllocator));
733 } else {
734 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(callback),
735 format,
Brian Salomona56a7462020-02-07 14:17:25 -0500736 dimensions,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400737 mipMapped,
738 mipMapsStatus,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400739 fit,
740 budgeted,
741 isProtected,
742 surfaceFlags,
743 useAllocator));
744 }
Robert Phillips777707b2018-01-17 11:40:14 -0500745}
746
Robert Phillipse8fabb22018-02-04 14:33:21 -0500747sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy(
Brian Salomonbeb7f522019-08-30 16:19:42 -0400748 LazyInstantiateCallback&& callback,
749 const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -0500750 SkISize dimensions,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400751 int sampleCnt,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400752 GrInternalSurfaceFlags surfaceFlags,
753 const TextureInfo* textureInfo,
754 GrMipMapsStatus mipMapsStatus,
755 SkBackingFit fit,
756 SkBudgeted budgeted,
757 GrProtected isProtected,
758 bool wrapsVkSecondaryCB,
759 UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500760 ASSERT_SINGLE_OWNER
761 if (this->isAbandoned()) {
762 return nullptr;
763 }
Brian Salomona56a7462020-02-07 14:17:25 -0500764 SkASSERT((dimensions.fWidth <= 0 && dimensions.fHeight <= 0) ||
765 (dimensions.fWidth > 0 && dimensions.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400766
Brian Salomona56a7462020-02-07 14:17:25 -0500767 if (dimensions.fWidth > this->caps()->maxRenderTargetSize() ||
768 dimensions.fHeight > this->caps()->maxRenderTargetSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400769 return nullptr;
770 }
771
Brian Salomon7226c232018-07-30 13:13:17 -0400772 if (textureInfo) {
Greg Danielb085fa92019-03-05 16:55:12 -0500773 // Wrapped vulkan secondary command buffers don't support texturing since we won't have an
774 // actual VkImage to texture from.
775 SkASSERT(!wrapsVkSecondaryCB);
Brian Salomon7226c232018-07-30 13:13:17 -0400776 return sk_sp<GrRenderTargetProxy>(new GrTextureRenderTargetProxy(
Greg Daniel3a365112020-02-14 10:47:18 -0500777 *this->caps(), std::move(callback), format, dimensions, sampleCnt,
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400778 textureInfo->fMipMapped, mipMapsStatus, fit, budgeted, isProtected, surfaceFlags,
779 useAllocator));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500780 }
781
Greg Danielb085fa92019-03-05 16:55:12 -0500782 GrRenderTargetProxy::WrapsVkSecondaryCB vkSCB =
783 wrapsVkSecondaryCB ? GrRenderTargetProxy::WrapsVkSecondaryCB::kYes
784 : GrRenderTargetProxy::WrapsVkSecondaryCB::kNo;
785
Greg Daniel3a365112020-02-14 10:47:18 -0500786 return sk_sp<GrRenderTargetProxy>(
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400787 new GrRenderTargetProxy(std::move(callback), format, dimensions, sampleCnt, fit,
788 budgeted, isProtected, surfaceFlags, useAllocator, vkSCB));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500789}
790
Brian Salomonbeb7f522019-08-30 16:19:42 -0400791sk_sp<GrTextureProxy> GrProxyProvider::MakeFullyLazyProxy(LazyInstantiateCallback&& callback,
792 const GrBackendFormat& format,
793 GrRenderable renderable,
794 int renderTargetSampleCnt,
795 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400796 const GrCaps& caps,
797 UseAllocator useAllocator) {
Robert Phillips0a15cc62019-07-30 12:49:10 -0400798 if (!format.isValid()) {
799 return nullptr;
800 }
801
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400802 SkASSERT(renderTargetSampleCnt == 1 || renderable == GrRenderable::kYes);
Robert Phillips10d17212019-04-24 14:09:10 -0400803 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNone;
Robert Phillips777707b2018-01-17 11:40:14 -0500804
Brian Salomona56a7462020-02-07 14:17:25 -0500805 static constexpr SkISize kLazyDims = {-1, -1};
Brian Salomonbeb7f522019-08-30 16:19:42 -0400806 if (GrRenderable::kYes == renderable) {
807 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
Greg Daniel3a365112020-02-14 10:47:18 -0500808 caps, std::move(callback), format, kLazyDims, renderTargetSampleCnt,
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400809 GrMipMapped::kNo, GrMipMapsStatus::kNotAllocated, SkBackingFit::kApprox,
Greg Daniel14b57212019-12-17 16:18:06 -0500810 SkBudgeted::kYes, isProtected, surfaceFlags, useAllocator));
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400811 } else {
812 return sk_sp<GrTextureProxy>(
813 new GrTextureProxy(std::move(callback), format, kLazyDims, GrMipMapped::kNo,
814 GrMipMapsStatus::kNotAllocated, SkBackingFit::kApprox,
815 SkBudgeted::kYes, isProtected, surfaceFlags, useAllocator));
Brian Salomonbeb7f522019-08-30 16:19:42 -0400816 }
Robert Phillips777707b2018-01-17 11:40:14 -0500817}
818
Robert Phillips427966a2018-12-20 17:20:43 -0500819void GrProxyProvider::processInvalidUniqueKey(const GrUniqueKey& key, GrTextureProxy* proxy,
820 InvalidateGPUResource invalidateGPUResource) {
Mike Klein04ef8102020-03-16 12:44:23 -0500821 this->processInvalidUniqueKeyImpl(key, proxy, invalidateGPUResource, RemoveTableEntry::kYes);
822}
823
824void GrProxyProvider::processInvalidUniqueKeyImpl(const GrUniqueKey& key, GrTextureProxy* proxy,
825 InvalidateGPUResource invalidateGPUResource,
826 RemoveTableEntry removeTableEntry) {
Chris Dalton2de13dd2019-01-03 15:11:59 -0700827 SkASSERT(key.isValid());
828
Robert Phillips427966a2018-12-20 17:20:43 -0500829 if (!proxy) {
830 proxy = fUniquelyKeyedProxies.find(key);
831 }
Chris Dalton2de13dd2019-01-03 15:11:59 -0700832 SkASSERT(!proxy || proxy->getUniqueKey() == key);
833
834 // Locate the corresponding GrGpuResource (if it needs to be invalidated) before clearing the
835 // proxy's unique key. We must do it in this order because 'key' may alias the proxy's key.
836 sk_sp<GrGpuResource> invalidGpuResource;
837 if (InvalidateGPUResource::kYes == invalidateGPUResource) {
Brian Salomon01ceae92019-04-02 11:49:54 -0400838 GrContext* direct = fImageContext->priv().asDirectContext();
839 if (direct) {
840 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
841 invalidGpuResource = resourceProvider->findByUniqueKey<GrGpuResource>(key);
Chris Dalton2de13dd2019-01-03 15:11:59 -0700842 }
843 SkASSERT(!invalidGpuResource || invalidGpuResource->getUniqueKey() == key);
844 }
Robert Phillips427966a2018-12-20 17:20:43 -0500845
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500846 // Note: this method is called for the whole variety of GrGpuResources so often 'key'
847 // will not be in 'fUniquelyKeyedProxies'.
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500848 if (proxy) {
Mike Klein04ef8102020-03-16 12:44:23 -0500849 if (removeTableEntry == RemoveTableEntry::kYes) {
850 fUniquelyKeyedProxies.remove(key);
851 }
Robert Phillips427966a2018-12-20 17:20:43 -0500852 proxy->cacheAccess().clearUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500853 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500854
Chris Dalton2de13dd2019-01-03 15:11:59 -0700855 if (invalidGpuResource) {
856 invalidGpuResource->resourcePriv().removeUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500857 }
858}
859
Robert Phillipsa41c6852019-02-07 10:44:10 -0500860uint32_t GrProxyProvider::contextID() const {
861 return fImageContext->priv().contextID();
862}
863
864const GrCaps* GrProxyProvider::caps() const {
865 return fImageContext->priv().caps();
866}
867
868sk_sp<const GrCaps> GrProxyProvider::refCaps() const {
869 return fImageContext->priv().refCaps();
870}
871
Robert Phillipsa9162df2019-02-11 14:12:03 -0500872bool GrProxyProvider::isAbandoned() const {
873 return fImageContext->priv().abandoned();
874}
875
Robert Phillips0790f8a2018-09-18 13:11:03 -0400876void GrProxyProvider::orphanAllUniqueKeys() {
Mike Kleincff63962020-03-14 16:22:45 -0500877 fUniquelyKeyedProxies.foreach([&](GrTextureProxy* proxy){
878 proxy->fProxyProvider = nullptr;
879 });
Robert Phillips0790f8a2018-09-18 13:11:03 -0400880}
881
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500882void GrProxyProvider::removeAllUniqueKeys() {
Mike Klein04ef8102020-03-16 12:44:23 -0500883 fUniquelyKeyedProxies.foreach([&](GrTextureProxy* proxy){
884 // It's not safe to remove table entries while iterating with foreach(),
885 // but since we're going to remove them all anyway, simply save that for the end.
886 this->processInvalidUniqueKeyImpl(proxy->getUniqueKey(), proxy,
887 InvalidateGPUResource::kNo,
888 RemoveTableEntry::kNo);
889 });
890 // Removing all those table entries is safe now.
891 fUniquelyKeyedProxies.reset();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500892}
Robert Phillipsa41c6852019-02-07 10:44:10 -0500893
894bool GrProxyProvider::renderingDirectly() const {
895 return fImageContext->priv().asDirectContext();
896}