blob: 10cb35360bd459611f8b7a063db8a625187c4ade [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/gpu/GrResourceProvider.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040027#include "src/gpu/GrSurfaceProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/gpu/GrSurfaceProxyPriv.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000029#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/gpu/GrTextureProxyCacheAccess.h"
31#include "src/gpu/GrTextureRenderTargetProxy.h"
32#include "src/gpu/SkGr.h"
33#include "src/image/SkImage_Base.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050034
35#define ASSERT_SINGLE_OWNER \
Robert Phillipsa41c6852019-02-07 10:44:10 -050036 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fImageContext->priv().singleOwner());)
Robert Phillips1afd4cd2018-01-08 13:40:32 -050037
Robert Phillipsa9162df2019-02-11 14:12:03 -050038GrProxyProvider::GrProxyProvider(GrImageContext* imageContext) : fImageContext(imageContext) {}
Robert Phillips1afd4cd2018-01-08 13:40:32 -050039
40GrProxyProvider::~GrProxyProvider() {
Robert Phillipsa41c6852019-02-07 10:44:10 -050041 if (this->renderingDirectly()) {
Robert Phillips0790f8a2018-09-18 13:11:03 -040042 // In DDL-mode a proxy provider can still have extant uniquely keyed proxies (since
43 // they need their unique keys to, potentially, find a cached resource when the
44 // DDL is played) but, in non-DDL-mode they should all have been cleaned up by this point.
45 SkASSERT(!fUniquelyKeyedProxies.count());
46 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050047}
48
Robert Phillipsadbe1322018-01-17 13:35:46 -050049bool GrProxyProvider::assignUniqueKeyToProxy(const GrUniqueKey& key, GrTextureProxy* proxy) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050050 ASSERT_SINGLE_OWNER
51 SkASSERT(key.isValid());
52 if (this->isAbandoned() || !proxy) {
Robert Phillipsadbe1322018-01-17 13:35:46 -050053 return false;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050054 }
55
Robert Phillipsa41c6852019-02-07 10:44:10 -050056#ifdef SK_DEBUG
57 {
58 GrContext* direct = fImageContext->priv().asDirectContext();
59 if (direct) {
60 GrResourceCache* resourceCache = direct->priv().getResourceCache();
61 // If there is already a GrResource with this key then the caller has violated the
62 // normal usage pattern of uniquely keyed resources (e.g., they have created one w/o
63 // first seeing if it already existed in the cache).
64 SkASSERT(!resourceCache->findAndRefUniqueResource(key));
65 }
66 }
67#endif
Robert Phillips1afd4cd2018-01-08 13:40:32 -050068
Robert Phillips1afd4cd2018-01-08 13:40:32 -050069 SkASSERT(!fUniquelyKeyedProxies.find(key)); // multiple proxies can't get the same key
70
71 proxy->cacheAccess().setUniqueKey(this, key);
72 SkASSERT(proxy->getUniqueKey() == key);
73 fUniquelyKeyedProxies.add(proxy);
Robert Phillipsadbe1322018-01-17 13:35:46 -050074 return true;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050075}
76
77void GrProxyProvider::adoptUniqueKeyFromSurface(GrTextureProxy* proxy, const GrSurface* surf) {
78 SkASSERT(surf->getUniqueKey().isValid());
79 proxy->cacheAccess().setUniqueKey(this, surf->getUniqueKey());
80 SkASSERT(proxy->getUniqueKey() == surf->getUniqueKey());
81 // multiple proxies can't get the same key
82 SkASSERT(!fUniquelyKeyedProxies.find(surf->getUniqueKey()));
83 fUniquelyKeyedProxies.add(proxy);
84}
85
Chris Dalton2de13dd2019-01-03 15:11:59 -070086void GrProxyProvider::removeUniqueKeyFromProxy(GrTextureProxy* proxy) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050087 ASSERT_SINGLE_OWNER
Chris Dalton2de13dd2019-01-03 15:11:59 -070088 SkASSERT(proxy);
89 SkASSERT(proxy->getUniqueKey().isValid());
90
91 if (this->isAbandoned()) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050092 return;
93 }
Robert Phillips0790f8a2018-09-18 13:11:03 -040094
Chris Dalton2de13dd2019-01-03 15:11:59 -070095 this->processInvalidUniqueKey(proxy->getUniqueKey(), proxy, InvalidateGPUResource::kYes);
Robert Phillips1afd4cd2018-01-08 13:40:32 -050096}
97
Greg Daniel3a365112020-02-14 10:47:18 -050098sk_sp<GrTextureProxy> GrProxyProvider::findProxyByUniqueKey(const GrUniqueKey& key) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050099 ASSERT_SINGLE_OWNER
100
101 if (this->isAbandoned()) {
102 return nullptr;
103 }
104
Brian Salomon01ceae92019-04-02 11:49:54 -0400105 GrTextureProxy* proxy = fUniquelyKeyedProxies.find(key);
Brian Salomon01ceae92019-04-02 11:49:54 -0400106 if (proxy) {
Robert Phillipse5f73282019-06-18 17:15:04 -0400107 return sk_ref_sp(proxy);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500108 }
Robert Phillipse5f73282019-06-18 17:15:04 -0400109 return nullptr;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500110}
111
Robert Phillipsa41c6852019-02-07 10:44:10 -0500112///////////////////////////////////////////////////////////////////////////////
113
114#if GR_TEST_UTILS
115sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createInstantiatedProxy(
Brian Salomona56a7462020-02-07 14:17:25 -0500116 SkISize dimensions,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400117 GrColorType colorType,
118 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
Greg Daniel3a365112020-02-14 10:47:18 -0500153 return this->createWrapped(std::move(tex), colorType, 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 colorType,
171 format,
172 renderable,
173 renderTargetSampleCnt,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400174 fit,
175 budgeted,
176 isProtected);
177}
178
Robert Phillipsa41c6852019-02-07 10:44:10 -0500179sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createWrapped(sk_sp<GrTexture> tex,
Greg Daniel3a365112020-02-14 10:47:18 -0500180 GrColorType colorType) {
181 return this->createWrapped(std::move(tex), colorType, UseAllocator::kYes);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500182}
183#endif
184
Brian Salomonbeb7f522019-08-30 16:19:42 -0400185sk_sp<GrTextureProxy> GrProxyProvider::createWrapped(sk_sp<GrTexture> tex,
186 GrColorType colorType,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400187 UseAllocator useAllocator) {
Robert Phillipsadbe1322018-01-17 13:35:46 -0500188#ifdef SK_DEBUG
189 if (tex->getUniqueKey().isValid()) {
Greg Daniel3a365112020-02-14 10:47:18 -0500190 SkASSERT(!this->findProxyByUniqueKey(tex->getUniqueKey()));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500191 }
192#endif
Greg Daniel14b57212019-12-17 16:18:06 -0500193 GrSwizzle readSwizzle = this->caps()->getReadSwizzle(tex->backendFormat(), colorType);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500194
195 if (tex->asRenderTarget()) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400196 return sk_sp<GrTextureProxy>(
Greg Daniel3a365112020-02-14 10:47:18 -0500197 new GrTextureRenderTargetProxy(std::move(tex), readSwizzle, useAllocator));
198 } else {
199 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), readSwizzle, useAllocator));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500200 }
201}
202
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500203sk_sp<GrTextureProxy> GrProxyProvider::findOrCreateProxyByUniqueKey(const GrUniqueKey& key,
Brian Salomon2af3e702019-08-11 19:10:31 -0400204 GrColorType colorType,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400205 UseAllocator useAllocator) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500206 ASSERT_SINGLE_OWNER
207
208 if (this->isAbandoned()) {
209 return nullptr;
210 }
211
Greg Daniel3a365112020-02-14 10:47:18 -0500212 sk_sp<GrTextureProxy> result = this->findProxyByUniqueKey(key);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500213 if (result) {
214 return result;
215 }
216
Robert Phillipsa41c6852019-02-07 10:44:10 -0500217 GrContext* direct = fImageContext->priv().asDirectContext();
218 if (!direct) {
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500219 return nullptr;
220 }
221
Robert Phillipsa41c6852019-02-07 10:44:10 -0500222 GrResourceCache* resourceCache = direct->priv().getResourceCache();
223
224 GrGpuResource* resource = resourceCache->findAndRefUniqueResource(key);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500225 if (!resource) {
226 return nullptr;
227 }
228
229 sk_sp<GrTexture> texture(static_cast<GrSurface*>(resource)->asTexture());
230 SkASSERT(texture);
231
Greg Daniel3a365112020-02-14 10:47:18 -0500232 result = this->createWrapped(std::move(texture), colorType, useAllocator);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500233 SkASSERT(result->getUniqueKey() == key);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500234 // createWrapped should've added this for us
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500235 SkASSERT(fUniquelyKeyedProxies.find(key));
Greg Daniel87506ab2020-02-12 13:05:42 -0500236 SkASSERT(result->textureSwizzleDoNotUse() ==
Greg Daniel14b57212019-12-17 16:18:06 -0500237 this->caps()->getReadSwizzle(result->backendFormat(), colorType));
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500238 return result;
239}
240
Brian Osmande496652019-03-22 13:42:33 -0400241sk_sp<GrTextureProxy> GrProxyProvider::createProxyFromBitmap(const SkBitmap& bitmap,
Greg Daniel6f5441a2020-01-28 17:02:49 -0500242 GrMipMapped mipMapped,
Brian Salomonbc074a62020-03-18 10:06:13 -0400243 SkBackingFit fit,
244 SkBudgeted budgeted) {
Brian Osman412674f2019-02-07 15:34:58 -0500245 ASSERT_SINGLE_OWNER
Greg Daniel6f5441a2020-01-28 17:02:49 -0500246 SkASSERT(fit == SkBackingFit::kExact || mipMapped == GrMipMapped::kNo);
Brian Osman412674f2019-02-07 15:34:58 -0500247
248 if (this->isAbandoned()) {
249 return nullptr;
250 }
251
Brian Osman2b23c4b2018-06-01 12:25:08 -0400252 if (!SkImageInfoIsValid(bitmap.info())) {
Greg Daniela4ead652018-02-07 10:21:48 -0500253 return nullptr;
254 }
255
Brian Osmande496652019-03-22 13:42:33 -0400256 ATRACE_ANDROID_FRAMEWORK("Upload %sTexture [%ux%u]",
257 GrMipMapped::kYes == mipMapped ? "MipMap " : "",
258 bitmap.width(), bitmap.height());
Greg Daniela4ead652018-02-07 10:21:48 -0500259
260 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
261 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
262 // 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 -0500263 SkBitmap copyBitmap = bitmap;
264 if (!this->renderingDirectly() && !bitmap.isImmutable()) {
265 copyBitmap.allocPixels();
266 if (!bitmap.readPixels(copyBitmap.pixmap())) {
267 return nullptr;
268 }
269 copyBitmap.setImmutable();
Greg Daniela4ead652018-02-07 10:21:48 -0500270 }
271
Greg Daniel6f5441a2020-01-28 17:02:49 -0500272 GrColorType grCT = SkColorTypeToGrColorType(copyBitmap.info().colorType());
Greg Danielce3ddaa2020-01-22 16:58:15 -0500273 GrBackendFormat format = this->caps()->getDefaultBackendFormat(grCT, GrRenderable::kNo);
Robert Phillips0a15cc62019-07-30 12:49:10 -0400274 if (!format.isValid()) {
Greg Daniel82c6b102020-01-21 10:33:22 -0500275 return nullptr;
Greg Daniel3fc5df42019-06-14 09:42:17 -0400276 }
277
Greg Daniel6f5441a2020-01-28 17:02:49 -0500278 sk_sp<GrTextureProxy> proxy;
279 if (mipMapped == GrMipMapped::kNo ||
280 0 == SkMipMap::ComputeLevelCount(copyBitmap.width(), copyBitmap.height())) {
Brian Salomonbc074a62020-03-18 10:06:13 -0400281 proxy = this->createNonMippedProxyFromBitmap(copyBitmap, fit, format, grCT, budgeted);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500282 } else {
Brian Salomonbc074a62020-03-18 10:06:13 -0400283 proxy = this->createMippedProxyFromBitmap(copyBitmap, format, grCT, budgeted);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500284 }
285
286 if (!proxy) {
287 return nullptr;
288 }
289
290 GrContext* direct = fImageContext->priv().asDirectContext();
291 if (direct) {
292 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
293
294 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
295 // we're better off instantiating the proxy immediately here.
296 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
297 return nullptr;
298 }
299 }
300 return proxy;
301}
302
303sk_sp<GrTextureProxy> GrProxyProvider::createNonMippedProxyFromBitmap(const SkBitmap& bitmap,
304 SkBackingFit fit,
305 const GrBackendFormat& format,
Brian Salomonbc074a62020-03-18 10:06:13 -0400306 GrColorType colorType,
307 SkBudgeted budgeted) {
Greg Daniel6f5441a2020-01-28 17:02:49 -0500308 GrSwizzle swizzle = this->caps()->getReadSwizzle(format, colorType);
Brian Salomona56a7462020-02-07 14:17:25 -0500309 auto dims = bitmap.dimensions();
Greg Daniel6f5441a2020-01-28 17:02:49 -0500310
311 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomonbc074a62020-03-18 10:06:13 -0400312 [dims, format, bitmap, fit, colorType, budgeted](GrResourceProvider* resourceProvider) {
Greg Daniel6f5441a2020-01-28 17:02:49 -0500313 GrMipLevel mipLevel = { bitmap.getPixels(), bitmap.rowBytes() };
314
315 return LazyCallbackResult(resourceProvider->createTexture(
Brian Salomonbc074a62020-03-18 10:06:13 -0400316 dims, format, colorType, GrRenderable::kNo, 1, budgeted, fit,
Greg Daniel6f5441a2020-01-28 17:02:49 -0500317 GrProtected::kNo, mipLevel));
318 },
Greg Daniel3a365112020-02-14 10:47:18 -0500319 format, dims, swizzle, GrRenderable::kNo, 1, GrMipMapped::kNo,
Brian Salomonbc074a62020-03-18 10:06:13 -0400320 GrMipMapsStatus::kNotAllocated, GrInternalSurfaceFlags::kNone, fit, budgeted,
Brian Salomona56a7462020-02-07 14:17:25 -0500321 GrProtected::kNo, UseAllocator::kYes);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500322
323 if (!proxy) {
324 return nullptr;
325 }
Brian Salomona56a7462020-02-07 14:17:25 -0500326 SkASSERT(proxy->dimensions() == bitmap.dimensions());
Greg Daniel6f5441a2020-01-28 17:02:49 -0500327 return proxy;
328}
329
330sk_sp<GrTextureProxy> GrProxyProvider::createMippedProxyFromBitmap(const SkBitmap& bitmap,
331 const GrBackendFormat& format,
Brian Salomonbc074a62020-03-18 10:06:13 -0400332 GrColorType colorType,
333 SkBudgeted budgeted) {
Greg Daniel6f5441a2020-01-28 17:02:49 -0500334 SkASSERT(this->caps()->mipMapSupport());
335
Greg Daniel6f5441a2020-01-28 17:02:49 -0500336
337 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(bitmap.pixmap(), nullptr));
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400338 if (!mipmaps) {
339 return nullptr;
340 }
341
Greg Daniel6f5441a2020-01-28 17:02:49 -0500342 GrSwizzle readSwizzle = this->caps()->getReadSwizzle(format, colorType);
Brian Salomona56a7462020-02-07 14:17:25 -0500343 auto dims = bitmap.dimensions();
Greg Danielce3ddaa2020-01-22 16:58:15 -0500344
Greg Daniela4ead652018-02-07 10:21:48 -0500345 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomonbc074a62020-03-18 10:06:13 -0400346 [dims, format, bitmap, mipmaps, budgeted](GrResourceProvider* resourceProvider) {
Brian Osman1b97f132018-09-13 17:33:48 +0000347 const int mipLevelCount = mipmaps->countLevels() + 1;
348 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
349
Greg Daniel6f5441a2020-01-28 17:02:49 -0500350 texels[0].fPixels = bitmap.getPixels();
351 texels[0].fRowBytes = bitmap.rowBytes();
Greg Daniela4ead652018-02-07 10:21:48 -0500352
Greg Daniel6f5441a2020-01-28 17:02:49 -0500353 auto colorType = SkColorTypeToGrColorType(bitmap.colorType());
Greg Daniela4ead652018-02-07 10:21:48 -0500354 for (int i = 1; i < mipLevelCount; ++i) {
355 SkMipMap::Level generatedMipLevel;
356 mipmaps->getLevel(i - 1, &generatedMipLevel);
357 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
358 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
359 SkASSERT(texels[i].fPixels);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500360 SkASSERT(generatedMipLevel.fPixmap.colorType() == bitmap.colorType());
Greg Daniela4ead652018-02-07 10:21:48 -0500361 }
Brian Salomonbeb7f522019-08-30 16:19:42 -0400362 return LazyCallbackResult(resourceProvider->createTexture(
Brian Salomonbc074a62020-03-18 10:06:13 -0400363 dims, format, colorType, GrRenderable::kNo, 1, budgeted, GrProtected::kNo,
364 texels.get(), mipLevelCount));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500365 },
Greg Daniel3a365112020-02-14 10:47:18 -0500366 format, dims, readSwizzle, GrRenderable::kNo, 1, GrMipMapped::kYes,
Brian Salomonbc074a62020-03-18 10:06:13 -0400367 GrMipMapsStatus::kValid, GrInternalSurfaceFlags::kNone, SkBackingFit::kExact, budgeted,
368 GrProtected::kNo, UseAllocator::kYes);
Greg Daniela4ead652018-02-07 10:21:48 -0500369
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400370 if (!proxy) {
371 return nullptr;
372 }
373
Brian Salomona56a7462020-02-07 14:17:25 -0500374 SkASSERT(proxy->dimensions() == bitmap.dimensions());
Greg Daniel6f5441a2020-01-28 17:02:49 -0500375
Greg Daniela4ead652018-02-07 10:21:48 -0500376 return proxy;
377}
378
Greg Daniel4065d452018-11-16 15:43:41 -0500379sk_sp<GrTextureProxy> GrProxyProvider::createProxy(const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -0500380 SkISize dimensions,
Greg Daniel47c20e82020-01-21 14:29:57 -0500381 GrSwizzle readSwizzle,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400382 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400383 int renderTargetSampleCnt,
Greg Danielf6f7b672018-02-15 13:06:26 -0500384 GrMipMapped mipMapped,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500385 SkBackingFit fit,
386 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -0400387 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400388 GrInternalSurfaceFlags surfaceFlags,
389 GrSurfaceProxy::UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500390 ASSERT_SINGLE_OWNER
391 if (this->isAbandoned()) {
392 return nullptr;
393 }
394
Robert Phillips8ff8bcc2019-07-29 17:03:35 -0400395 const GrCaps* caps = this->caps();
396
397 if (caps->isFormatCompressed(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400398 // Deferred proxies for compressed textures are not supported.
399 return nullptr;
400 }
Robert Phillips0902c982019-07-16 07:47:56 -0400401
Greg Danielf6f7b672018-02-15 13:06:26 -0500402 if (GrMipMapped::kYes == mipMapped) {
403 // SkMipMap doesn't include the base level in the level count so we have to add 1
Brian Salomona56a7462020-02-07 14:17:25 -0500404 int mipCount = SkMipMap::ComputeLevelCount(dimensions.fWidth, dimensions.fHeight) + 1;
Greg Danielf6f7b672018-02-15 13:06:26 -0500405 if (1 == mipCount) {
406 mipMapped = GrMipMapped::kNo;
407 }
408 }
409
Brian Salomona56a7462020-02-07 14:17:25 -0500410 if (!caps->validateSurfaceParams(dimensions, format, renderable, renderTargetSampleCnt,
411 mipMapped)) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500412 return nullptr;
413 }
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600414 GrMipMapsStatus mipMapsStatus = (GrMipMapped::kYes == mipMapped)
415 ? GrMipMapsStatus::kDirty
416 : GrMipMapsStatus::kNotAllocated;
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400417 if (renderable == GrRenderable::kYes) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400418 renderTargetSampleCnt =
Greg Daniel6fa62e22019-08-07 15:52:37 -0400419 caps->getRenderTargetSampleCount(renderTargetSampleCnt, format);
420 SkASSERT(renderTargetSampleCnt);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500421 // We know anything we instantiate later from this deferred path will be
422 // both texturable and renderable
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400423 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
Greg Daniel3a365112020-02-14 10:47:18 -0500424 *caps, format, dimensions, renderTargetSampleCnt, mipMapped, mipMapsStatus,
Greg Daniel14b57212019-12-17 16:18:06 -0500425 readSwizzle, fit, budgeted, isProtected, surfaceFlags, useAllocator));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500426 }
427
Greg Daniel3a365112020-02-14 10:47:18 -0500428 return sk_sp<GrTextureProxy>(new GrTextureProxy(format, dimensions, mipMapped, mipMapsStatus,
429 readSwizzle, fit, budgeted, isProtected,
430 surfaceFlags, useAllocator));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500431}
432
Brian Salomonbb8dde82019-06-27 10:52:13 -0400433sk_sp<GrTextureProxy> GrProxyProvider::createCompressedTextureProxy(
Robert Phillips3a833922020-01-21 15:25:58 -0500434 SkISize dimensions, SkBudgeted budgeted, GrMipMapped mipMapped, GrProtected isProtected,
Robert Phillipse4720c62020-01-14 14:33:24 -0500435 SkImage::CompressionType compressionType, sk_sp<SkData> data) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500436 ASSERT_SINGLE_OWNER
437 if (this->isAbandoned()) {
438 return nullptr;
439 }
Brian Salomonbb8dde82019-06-27 10:52:13 -0400440
Robert Phillips8ff8bcc2019-07-29 17:03:35 -0400441 GrBackendFormat format = this->caps()->getBackendFormatFromCompressionType(compressionType);
442
Greg Daniel7bfc9132019-08-14 14:23:53 -0400443 if (!this->caps()->isFormatTexturable(format)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500444 return nullptr;
445 }
446
Robert Phillipse4720c62020-01-14 14:33:24 -0500447 GrMipMapsStatus mipMapsStatus = (GrMipMapped::kYes == mipMapped)
448 ? GrMipMapsStatus::kValid
449 : GrMipMapsStatus::kNotAllocated;
450
Jim Van Verthee06b332019-01-18 10:36:32 -0500451 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomona56a7462020-02-07 14:17:25 -0500452 [dimensions, format, budgeted, mipMapped, isProtected,
453 data](GrResourceProvider* resourceProvider) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400454 return LazyCallbackResult(resourceProvider->createCompressedTexture(
Robert Phillips3a833922020-01-21 15:25:58 -0500455 dimensions, format, budgeted, mipMapped, isProtected, data.get()));
Brian Salomonbb8dde82019-06-27 10:52:13 -0400456 },
Greg Daniel3a365112020-02-14 10:47:18 -0500457 format, dimensions, GrSwizzle(), GrRenderable::kNo, 1, mipMapped, mipMapsStatus,
458 GrInternalSurfaceFlags::kReadOnly, SkBackingFit::kExact, SkBudgeted::kYes,
459 GrProtected::kNo, UseAllocator::kYes);
Jim Van Verthee06b332019-01-18 10:36:32 -0500460
461 if (!proxy) {
462 return nullptr;
463 }
464
Robert Phillipsa41c6852019-02-07 10:44:10 -0500465 GrContext* direct = fImageContext->priv().asDirectContext();
466 if (direct) {
467 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Jim Van Verthee06b332019-01-18 10:36:32 -0500468 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
469 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500470 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500471 return nullptr;
472 }
473 }
474 return proxy;
475}
476
Brian Salomon7578f3e2018-03-07 14:39:54 -0500477sk_sp<GrTextureProxy> GrProxyProvider::wrapBackendTexture(const GrBackendTexture& backendTex,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400478 GrColorType grColorType,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500479 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500480 GrWrapCacheable cacheable,
Brian Salomonc67c31c2018-12-06 10:00:03 -0500481 GrIOType ioType,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500482 ReleaseProc releaseProc,
483 ReleaseContext releaseCtx) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500484 SkASSERT(ioType != kWrite_GrIOType);
Robert Phillipsf9bec202018-01-16 09:21:01 -0500485 if (this->isAbandoned()) {
486 return nullptr;
487 }
488
Brian Salomonf7778972018-03-08 10:13:17 -0500489 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500490 GrContext* direct = fImageContext->priv().asDirectContext();
491 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500492 return nullptr;
493 }
494
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400495 const GrCaps* caps = this->caps();
496
Robert Phillipsa41c6852019-02-07 10:44:10 -0500497 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
498
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500499 sk_sp<GrTexture> tex =
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400500 resourceProvider->wrapBackendTexture(backendTex, grColorType,
501 ownership, cacheable, ioType);
Brian Salomonf7778972018-03-08 10:13:17 -0500502 if (!tex) {
503 return nullptr;
504 }
Robert Phillipsadbe1322018-01-17 13:35:46 -0500505
Greg Daniel6a0176b2018-01-30 09:28:44 -0500506 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500507 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel6a0176b2018-01-30 09:28:44 -0500508 }
509
Brian Salomonf7778972018-03-08 10:13:17 -0500510 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
511 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500512 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500513
Greg Daniel14b57212019-12-17 16:18:06 -0500514 GrSwizzle readSwizzle = caps->getReadSwizzle(tex->backendFormat(), grColorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400515
Brian Salomonbeb7f522019-08-30 16:19:42 -0400516 return sk_sp<GrTextureProxy>(
Greg Daniel3a365112020-02-14 10:47:18 -0500517 new GrTextureProxy(std::move(tex), readSwizzle, 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
Robert Phillipsb0855272020-01-15 12:56:52 -0500535 const GrCaps* caps = this->caps();
536
Robert Phillipsead321b2019-12-19 10:16:32 -0500537 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
538
539 sk_sp<GrTexture> tex = resourceProvider->wrapCompressedBackendTexture(beTex, ownership,
540 cacheable);
541 if (!tex) {
542 return nullptr;
543 }
544
545 if (releaseProc) {
546 tex->setRelease(releaseProc, releaseCtx);
547 }
548
549 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
550 // Make sure we match how we created the proxy with SkBudgeted::kNo
551 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
552
Robert Phillipsb0855272020-01-15 12:56:52 -0500553 SkImage::CompressionType compressionType = caps->compressionType(beTex.getBackendFormat());
554
Robert Phillips99dead92020-01-27 16:11:57 -0500555 GrSwizzle texSwizzle = SkCompressionTypeIsOpaque(compressionType) ? GrSwizzle::RGB1()
Robert Phillipsb0855272020-01-15 12:56:52 -0500556 : GrSwizzle::RGBA();
Robert Phillipsead321b2019-12-19 10:16:32 -0500557
Greg Daniel3a365112020-02-14 10:47:18 -0500558 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), texSwizzle, UseAllocator::kNo));
Robert Phillipsead321b2019-12-19 10:16:32 -0500559}
560
Brian Salomon7578f3e2018-03-07 14:39:54 -0500561sk_sp<GrTextureProxy> GrProxyProvider::wrapRenderableBackendTexture(
Greg Daniel3a365112020-02-14 10:47:18 -0500562 const GrBackendTexture& backendTex, int sampleCnt, GrColorType colorType,
563 GrWrapOwnership ownership, GrWrapCacheable cacheable, ReleaseProc releaseProc,
564 ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500565 if (this->isAbandoned()) {
566 return nullptr;
567 }
568
Brian Salomonf7778972018-03-08 10:13:17 -0500569 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500570 GrContext* direct = fImageContext->priv().asDirectContext();
571 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500572 return nullptr;
573 }
574
Robert Phillips0902c982019-07-16 07:47:56 -0400575 const GrCaps* caps = this->caps();
576
Robert Phillipsa41c6852019-02-07 10:44:10 -0500577 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
578
Greg Daniel6fa62e22019-08-07 15:52:37 -0400579 // TODO: This should have been checked and validated before getting into GrProxyProvider.
580 if (!caps->isFormatAsColorTypeRenderable(colorType, backendTex.getBackendFormat(), sampleCnt)) {
Greg Danielf87651e2018-02-21 11:36:53 -0500581 return nullptr;
582 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500583
Greg Daniel6fa62e22019-08-07 15:52:37 -0400584 sampleCnt = caps->getRenderTargetSampleCount(sampleCnt, backendTex.getBackendFormat());
585 SkASSERT(sampleCnt);
586
Robert Phillipsa41c6852019-02-07 10:44:10 -0500587 sk_sp<GrTexture> tex = resourceProvider->wrapRenderableBackendTexture(backendTex, sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400588 colorType, ownership,
589 cacheable);
Brian Salomonf7778972018-03-08 10:13:17 -0500590 if (!tex) {
591 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500592 }
593
Greg Daniel8ce79912019-02-05 10:08:43 -0500594 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500595 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500596 }
597
Brian Salomonf7778972018-03-08 10:13:17 -0500598 SkASSERT(tex->asRenderTarget()); // A GrTextureRenderTarget
599 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500600 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Greg Daniel6abda432018-02-15 14:55:00 -0500601
Greg Daniel14b57212019-12-17 16:18:06 -0500602 GrSwizzle readSwizzle = caps->getReadSwizzle(tex->backendFormat(), colorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400603
Greg Daniel3a365112020-02-14 10:47:18 -0500604 return sk_sp<GrTextureProxy>(
605 new GrTextureRenderTargetProxy(std::move(tex), readSwizzle, UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500606}
607
Brian Salomon7578f3e2018-03-07 14:39:54 -0500608sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget(
Greg Daniel3a365112020-02-14 10:47:18 -0500609 const GrBackendRenderTarget& backendRT, GrColorType grColorType, ReleaseProc releaseProc,
610 ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500611 if (this->isAbandoned()) {
612 return nullptr;
613 }
614
Brian Salomonf7778972018-03-08 10:13:17 -0500615 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500616 GrContext* direct = fImageContext->priv().asDirectContext();
617 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500618 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500619 }
620
Robert Phillips62221e72019-07-24 15:07:38 -0400621 const GrCaps* caps = this->caps();
622
Robert Phillipsa41c6852019-02-07 10:44:10 -0500623 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
624
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400625 sk_sp<GrRenderTarget> rt = resourceProvider->wrapBackendRenderTarget(backendRT, grColorType);
Brian Salomonf7778972018-03-08 10:13:17 -0500626 if (!rt) {
627 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500628 }
Greg Daniel8ce79912019-02-05 10:08:43 -0500629
Greg Daniel8ce79912019-02-05 10:08:43 -0500630 if (releaseProc) {
Brian Salomon2ca31f82019-03-05 13:28:58 -0500631 rt->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500632 }
633
Brian Salomonf7778972018-03-08 10:13:17 -0500634 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
635 SkASSERT(!rt->getUniqueKey().isValid());
636 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500637 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Brian Salomonf7778972018-03-08 10:13:17 -0500638
Greg Daniel14b57212019-12-17 16:18:06 -0500639 GrSwizzle readSwizzle = caps->getReadSwizzle(rt->backendFormat(), grColorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400640
Greg Daniel3a365112020-02-14 10:47:18 -0500641 return sk_sp<GrRenderTargetProxy>(
642 new GrRenderTargetProxy(std::move(rt), readSwizzle, UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500643}
644
Brian Salomon7578f3e2018-03-07 14:39:54 -0500645sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendTextureAsRenderTarget(
Greg Daniel3a365112020-02-14 10:47:18 -0500646 const GrBackendTexture& backendTex, GrColorType grColorType, int sampleCnt) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500647 if (this->isAbandoned()) {
648 return nullptr;
649 }
650
Brian Salomonf7778972018-03-08 10:13:17 -0500651 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500652 GrContext* direct = fImageContext->priv().asDirectContext();
653 if (!direct) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500654 return nullptr;
655 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500656
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400657 const GrCaps* caps = this->caps();
658
Robert Phillipsa41c6852019-02-07 10:44:10 -0500659 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
660
Brian Salomonf7778972018-03-08 10:13:17 -0500661 sk_sp<GrRenderTarget> rt =
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400662 resourceProvider->wrapBackendTextureAsRenderTarget(backendTex, sampleCnt, grColorType);
Brian Salomonf7778972018-03-08 10:13:17 -0500663 if (!rt) {
664 return nullptr;
Greg Danielf87651e2018-02-21 11:36:53 -0500665 }
Brian Salomonf7778972018-03-08 10:13:17 -0500666 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
667 SkASSERT(!rt->getUniqueKey().isValid());
Greg Danielb46add82019-01-02 14:51:29 -0500668 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500669 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielf87651e2018-02-21 11:36:53 -0500670
Greg Daniel14b57212019-12-17 16:18:06 -0500671 GrSwizzle readSwizzle = caps->getReadSwizzle(rt->backendFormat(), grColorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400672
Greg Daniel3a365112020-02-14 10:47:18 -0500673 return sk_sp<GrSurfaceProxy>(
674 new GrRenderTargetProxy(std::move(rt), readSwizzle, UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500675}
676
Greg Danielb46add82019-01-02 14:51:29 -0500677sk_sp<GrRenderTargetProxy> GrProxyProvider::wrapVulkanSecondaryCBAsRenderTarget(
678 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
679 if (this->isAbandoned()) {
680 return nullptr;
681 }
682
683 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500684 GrContext* direct = fImageContext->priv().asDirectContext();
685 if (!direct) {
Greg Danielb46add82019-01-02 14:51:29 -0500686 return nullptr;
687 }
688
Robert Phillipsa41c6852019-02-07 10:44:10 -0500689 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Danielb46add82019-01-02 14:51:29 -0500690
Robert Phillipsa41c6852019-02-07 10:44:10 -0500691 sk_sp<GrRenderTarget> rt = resourceProvider->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
692 vkInfo);
Greg Danielb46add82019-01-02 14:51:29 -0500693 if (!rt) {
694 return nullptr;
695 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500696
Greg Danielb46add82019-01-02 14:51:29 -0500697 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
698 SkASSERT(!rt->getUniqueKey().isValid());
699 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500700 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielb46add82019-01-02 14:51:29 -0500701
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400702 GrColorType colorType = SkColorTypeToGrColorType(imageInfo.colorType());
Greg Daniel14b57212019-12-17 16:18:06 -0500703 GrSwizzle readSwizzle = this->caps()->getReadSwizzle(rt->backendFormat(), colorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400704
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400705 if (!this->caps()->isFormatAsColorTypeRenderable(colorType, rt->backendFormat(),
706 rt->numSamples())) {
707 return nullptr;
708 }
709
Greg Daniel3a365112020-02-14 10:47:18 -0500710 return sk_sp<GrRenderTargetProxy>(
711 new GrRenderTargetProxy(std::move(rt), readSwizzle, UseAllocator::kNo,
712 GrRenderTargetProxy::WrapsVkSecondaryCB::kYes));
Brian Salomone8a766b2019-07-19 14:24:36 -0400713}
714
715sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
716 const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -0500717 SkISize dimensions,
Greg Danielce3ddaa2020-01-22 16:58:15 -0500718 GrSwizzle readSwizzle,
Brian Salomone8a766b2019-07-19 14:24:36 -0400719 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400720 int renderTargetSampleCnt,
Brian Salomone8a766b2019-07-19 14:24:36 -0400721 GrMipMapped mipMapped,
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600722 GrMipMapsStatus mipMapsStatus,
Brian Salomone8a766b2019-07-19 14:24:36 -0400723 GrInternalSurfaceFlags surfaceFlags,
724 SkBackingFit fit,
725 SkBudgeted budgeted,
726 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400727 GrSurfaceProxy::UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500728 ASSERT_SINGLE_OWNER
729 if (this->isAbandoned()) {
730 return nullptr;
731 }
Brian Salomona56a7462020-02-07 14:17:25 -0500732 SkASSERT((dimensions.fWidth <= 0 && dimensions.fHeight <= 0) ||
733 (dimensions.fWidth > 0 && dimensions.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400734
Robert Phillips0a15cc62019-07-30 12:49:10 -0400735 if (!format.isValid()) {
736 return nullptr;
737 }
738
Brian Salomona56a7462020-02-07 14:17:25 -0500739 if (dimensions.fWidth > this->caps()->maxTextureSize() ||
740 dimensions.fHeight > this->caps()->maxTextureSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400741 return nullptr;
742 }
743
Brian Salomonbeb7f522019-08-30 16:19:42 -0400744 if (renderable == GrRenderable::kYes) {
745 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(*this->caps(),
746 std::move(callback),
747 format,
Brian Salomona56a7462020-02-07 14:17:25 -0500748 dimensions,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400749 renderTargetSampleCnt,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400750 mipMapped,
751 mipMapsStatus,
Greg Daniel14b57212019-12-17 16:18:06 -0500752 readSwizzle,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400753 fit,
754 budgeted,
755 isProtected,
756 surfaceFlags,
757 useAllocator));
758 } else {
759 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(callback),
760 format,
Brian Salomona56a7462020-02-07 14:17:25 -0500761 dimensions,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400762 mipMapped,
763 mipMapsStatus,
Greg Daniel14b57212019-12-17 16:18:06 -0500764 readSwizzle,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400765 fit,
766 budgeted,
767 isProtected,
768 surfaceFlags,
769 useAllocator));
770 }
Robert Phillips777707b2018-01-17 11:40:14 -0500771}
772
Robert Phillipse8fabb22018-02-04 14:33:21 -0500773sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy(
Brian Salomonbeb7f522019-08-30 16:19:42 -0400774 LazyInstantiateCallback&& callback,
775 const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -0500776 SkISize dimensions,
Greg Danielce3ddaa2020-01-22 16:58:15 -0500777 GrSwizzle readSwizzle,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400778 int sampleCnt,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400779 GrInternalSurfaceFlags surfaceFlags,
780 const TextureInfo* textureInfo,
781 GrMipMapsStatus mipMapsStatus,
782 SkBackingFit fit,
783 SkBudgeted budgeted,
784 GrProtected isProtected,
785 bool wrapsVkSecondaryCB,
786 UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500787 ASSERT_SINGLE_OWNER
788 if (this->isAbandoned()) {
789 return nullptr;
790 }
Brian Salomona56a7462020-02-07 14:17:25 -0500791 SkASSERT((dimensions.fWidth <= 0 && dimensions.fHeight <= 0) ||
792 (dimensions.fWidth > 0 && dimensions.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400793
Brian Salomona56a7462020-02-07 14:17:25 -0500794 if (dimensions.fWidth > this->caps()->maxRenderTargetSize() ||
795 dimensions.fHeight > this->caps()->maxRenderTargetSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400796 return nullptr;
797 }
798
Brian Salomon7226c232018-07-30 13:13:17 -0400799 if (textureInfo) {
Greg Danielb085fa92019-03-05 16:55:12 -0500800 // Wrapped vulkan secondary command buffers don't support texturing since we won't have an
801 // actual VkImage to texture from.
802 SkASSERT(!wrapsVkSecondaryCB);
Brian Salomon7226c232018-07-30 13:13:17 -0400803 return sk_sp<GrRenderTargetProxy>(new GrTextureRenderTargetProxy(
Greg Daniel3a365112020-02-14 10:47:18 -0500804 *this->caps(), std::move(callback), format, dimensions, sampleCnt,
Greg Daniel14b57212019-12-17 16:18:06 -0500805 textureInfo->fMipMapped, mipMapsStatus, readSwizzle, fit, budgeted, isProtected,
Greg Danielbaf8d992019-10-29 14:14:32 -0400806 surfaceFlags, useAllocator));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500807 }
808
Greg Danielb085fa92019-03-05 16:55:12 -0500809 GrRenderTargetProxy::WrapsVkSecondaryCB vkSCB =
810 wrapsVkSecondaryCB ? GrRenderTargetProxy::WrapsVkSecondaryCB::kYes
811 : GrRenderTargetProxy::WrapsVkSecondaryCB::kNo;
812
Greg Daniel3a365112020-02-14 10:47:18 -0500813 return sk_sp<GrRenderTargetProxy>(
814 new GrRenderTargetProxy(std::move(callback), format, dimensions, sampleCnt, readSwizzle,
815 fit, budgeted, isProtected, surfaceFlags, useAllocator, vkSCB));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500816}
817
Brian Salomonbeb7f522019-08-30 16:19:42 -0400818sk_sp<GrTextureProxy> GrProxyProvider::MakeFullyLazyProxy(LazyInstantiateCallback&& callback,
819 const GrBackendFormat& format,
Greg Danielce3ddaa2020-01-22 16:58:15 -0500820 GrSwizzle readSwizzle,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400821 GrRenderable renderable,
822 int renderTargetSampleCnt,
823 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400824 const GrCaps& caps,
825 UseAllocator useAllocator) {
Robert Phillips0a15cc62019-07-30 12:49:10 -0400826 if (!format.isValid()) {
827 return nullptr;
828 }
829
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400830 SkASSERT(renderTargetSampleCnt == 1 || renderable == GrRenderable::kYes);
Robert Phillips10d17212019-04-24 14:09:10 -0400831 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNone;
Robert Phillips777707b2018-01-17 11:40:14 -0500832
Brian Salomona56a7462020-02-07 14:17:25 -0500833 static constexpr SkISize kLazyDims = {-1, -1};
Brian Salomonbeb7f522019-08-30 16:19:42 -0400834 if (GrRenderable::kYes == renderable) {
835 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
Greg Daniel3a365112020-02-14 10:47:18 -0500836 caps, std::move(callback), format, kLazyDims, renderTargetSampleCnt,
Greg Daniel14b57212019-12-17 16:18:06 -0500837 GrMipMapped::kNo, GrMipMapsStatus::kNotAllocated, readSwizzle,
838 SkBackingFit::kApprox, SkBudgeted::kYes, isProtected, surfaceFlags, useAllocator));
Brian Salomonbeb7f522019-08-30 16:19:42 -0400839 } else {
840 return sk_sp<GrTextureProxy>(new GrTextureProxy(
Greg Daniel3a365112020-02-14 10:47:18 -0500841 std::move(callback), format, kLazyDims, GrMipMapped::kNo,
Greg Daniel14b57212019-12-17 16:18:06 -0500842 GrMipMapsStatus::kNotAllocated, readSwizzle, SkBackingFit::kApprox,
843 SkBudgeted::kYes, isProtected, surfaceFlags, useAllocator));
Brian Salomonbeb7f522019-08-30 16:19:42 -0400844 }
Robert Phillips777707b2018-01-17 11:40:14 -0500845}
846
Robert Phillips427966a2018-12-20 17:20:43 -0500847void GrProxyProvider::processInvalidUniqueKey(const GrUniqueKey& key, GrTextureProxy* proxy,
848 InvalidateGPUResource invalidateGPUResource) {
Mike Klein04ef8102020-03-16 12:44:23 -0500849 this->processInvalidUniqueKeyImpl(key, proxy, invalidateGPUResource, RemoveTableEntry::kYes);
850}
851
852void GrProxyProvider::processInvalidUniqueKeyImpl(const GrUniqueKey& key, GrTextureProxy* proxy,
853 InvalidateGPUResource invalidateGPUResource,
854 RemoveTableEntry removeTableEntry) {
Chris Dalton2de13dd2019-01-03 15:11:59 -0700855 SkASSERT(key.isValid());
856
Robert Phillips427966a2018-12-20 17:20:43 -0500857 if (!proxy) {
858 proxy = fUniquelyKeyedProxies.find(key);
859 }
Chris Dalton2de13dd2019-01-03 15:11:59 -0700860 SkASSERT(!proxy || proxy->getUniqueKey() == key);
861
862 // Locate the corresponding GrGpuResource (if it needs to be invalidated) before clearing the
863 // proxy's unique key. We must do it in this order because 'key' may alias the proxy's key.
864 sk_sp<GrGpuResource> invalidGpuResource;
865 if (InvalidateGPUResource::kYes == invalidateGPUResource) {
Brian Salomon01ceae92019-04-02 11:49:54 -0400866 GrContext* direct = fImageContext->priv().asDirectContext();
867 if (direct) {
868 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
869 invalidGpuResource = resourceProvider->findByUniqueKey<GrGpuResource>(key);
Chris Dalton2de13dd2019-01-03 15:11:59 -0700870 }
871 SkASSERT(!invalidGpuResource || invalidGpuResource->getUniqueKey() == key);
872 }
Robert Phillips427966a2018-12-20 17:20:43 -0500873
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500874 // Note: this method is called for the whole variety of GrGpuResources so often 'key'
875 // will not be in 'fUniquelyKeyedProxies'.
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500876 if (proxy) {
Mike Klein04ef8102020-03-16 12:44:23 -0500877 if (removeTableEntry == RemoveTableEntry::kYes) {
878 fUniquelyKeyedProxies.remove(key);
879 }
Robert Phillips427966a2018-12-20 17:20:43 -0500880 proxy->cacheAccess().clearUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500881 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500882
Chris Dalton2de13dd2019-01-03 15:11:59 -0700883 if (invalidGpuResource) {
884 invalidGpuResource->resourcePriv().removeUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500885 }
886}
887
Robert Phillipsa41c6852019-02-07 10:44:10 -0500888uint32_t GrProxyProvider::contextID() const {
889 return fImageContext->priv().contextID();
890}
891
892const GrCaps* GrProxyProvider::caps() const {
893 return fImageContext->priv().caps();
894}
895
896sk_sp<const GrCaps> GrProxyProvider::refCaps() const {
897 return fImageContext->priv().refCaps();
898}
899
Robert Phillipsa9162df2019-02-11 14:12:03 -0500900bool GrProxyProvider::isAbandoned() const {
901 return fImageContext->priv().abandoned();
902}
903
Robert Phillips0790f8a2018-09-18 13:11:03 -0400904void GrProxyProvider::orphanAllUniqueKeys() {
Mike Kleincff63962020-03-14 16:22:45 -0500905 fUniquelyKeyedProxies.foreach([&](GrTextureProxy* proxy){
906 proxy->fProxyProvider = nullptr;
907 });
Robert Phillips0790f8a2018-09-18 13:11:03 -0400908}
909
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500910void GrProxyProvider::removeAllUniqueKeys() {
Mike Klein04ef8102020-03-16 12:44:23 -0500911 fUniquelyKeyedProxies.foreach([&](GrTextureProxy* proxy){
912 // It's not safe to remove table entries while iterating with foreach(),
913 // but since we're going to remove them all anyway, simply save that for the end.
914 this->processInvalidUniqueKeyImpl(proxy->getUniqueKey(), proxy,
915 InvalidateGPUResource::kNo,
916 RemoveTableEntry::kNo);
917 });
918 // Removing all those table entries is safe now.
919 fUniquelyKeyedProxies.reset();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500920}
Robert Phillipsa41c6852019-02-07 10:44:10 -0500921
922bool GrProxyProvider::renderingDirectly() const {
923 return fImageContext->priv().asDirectContext();
924}