blob: a2c896cfbbaeec6ab0ba4c466394e43ff40d59f9 [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/gpu/GrTexture.h"
14#include "include/private/GrImageContext.h"
15#include "include/private/GrResourceKey.h"
16#include "include/private/GrSingleOwner.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "include/private/SkImageInfoPriv.h"
18#include "src/core/SkAutoPixmapStorage.h"
Robert Phillips99dead92020-01-27 16:11:57 -050019#include "src/core/SkCompressedDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/core/SkImagePriv.h"
21#include "src/core/SkMipMap.h"
22#include "src/core/SkTraceEvent.h"
23#include "src/gpu/GrCaps.h"
24#include "src/gpu/GrContextPriv.h"
25#include "src/gpu/GrImageContextPriv.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040026#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/GrResourceProvider.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040028#include "src/gpu/GrSurfaceProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/gpu/GrSurfaceProxyPriv.h"
30#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
98sk_sp<GrTextureProxy> GrProxyProvider::findProxyByUniqueKey(const GrUniqueKey& key,
99 GrSurfaceOrigin origin) {
100 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 GrColorType colorType,
119 const GrBackendFormat& format,
120 GrRenderable renderable,
121 int renderTargetSampleCnt,
122 GrSurfaceOrigin origin,
123 SkBackingFit fit,
124 SkBudgeted budgeted,
125 GrProtected isProtected) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500126 ASSERT_SINGLE_OWNER
127 if (this->isAbandoned()) {
128 return nullptr;
129 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500130 GrContext* direct = fImageContext->priv().asDirectContext();
131 if (!direct) {
132 return nullptr;
133 }
134
Brian Salomon4eb38b72019-08-05 12:58:39 -0400135 if (this->caps()->isFormatCompressed(format)) {
136 // TODO: Allow this to go to GrResourceProvider::createCompressedTexture() once we no longer
Greg Daniel4cb29332020-01-23 10:07:02 -0500137 // rely on GrColorType to get a swizzle for the proxy.
Brian Salomon4eb38b72019-08-05 12:58:39 -0400138 return nullptr;
139 }
Brian Salomon4eb38b72019-08-05 12:58:39 -0400140
Robert Phillipsa41c6852019-02-07 10:44:10 -0500141 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
142 sk_sp<GrTexture> tex;
143
144 if (SkBackingFit::kApprox == fit) {
Brian Salomona56a7462020-02-07 14:17:25 -0500145 tex = resourceProvider->createApproxTexture(dimensions, format, renderable,
146 renderTargetSampleCnt, isProtected);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500147 } else {
Brian Salomona56a7462020-02-07 14:17:25 -0500148 tex = resourceProvider->createTexture(dimensions, format, renderable, renderTargetSampleCnt,
Brian Salomona90382f2019-09-17 09:01:56 -0400149 GrMipMapped::kNo, budgeted, isProtected);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500150 }
151 if (!tex) {
152 return nullptr;
153 }
154
Brian Salomonbeb7f522019-08-30 16:19:42 -0400155 return this->createWrapped(std::move(tex), colorType, origin, UseAllocator::kYes);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500156}
157
Brian Salomon4eb38b72019-08-05 12:58:39 -0400158sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createInstantiatedProxy(
Brian Salomona56a7462020-02-07 14:17:25 -0500159 SkISize dimensions,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400160 GrColorType colorType,
161 GrRenderable renderable,
162 int renderTargetSampleCnt,
163 GrSurfaceOrigin origin,
164 SkBackingFit fit,
165 SkBudgeted budgeted,
166 GrProtected isProtected) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500167 ASSERT_SINGLE_OWNER
168 if (this->isAbandoned()) {
169 return nullptr;
170 }
Brian Salomon4eb38b72019-08-05 12:58:39 -0400171 auto format = this->caps()->getDefaultBackendFormat(colorType, renderable);
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400172 return this->testingOnly_createInstantiatedProxy(dimensions,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400173 colorType,
174 format,
175 renderable,
176 renderTargetSampleCnt,
177 origin,
178 fit,
179 budgeted,
180 isProtected);
181}
182
Robert Phillipsa41c6852019-02-07 10:44:10 -0500183sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createWrapped(sk_sp<GrTexture> tex,
Brian Salomon2af3e702019-08-11 19:10:31 -0400184 GrColorType colorType,
Robert Phillipsa41c6852019-02-07 10:44:10 -0500185 GrSurfaceOrigin origin) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400186 return this->createWrapped(std::move(tex), colorType, origin, UseAllocator::kYes);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500187}
188#endif
189
Brian Salomonbeb7f522019-08-30 16:19:42 -0400190sk_sp<GrTextureProxy> GrProxyProvider::createWrapped(sk_sp<GrTexture> tex,
191 GrColorType colorType,
192 GrSurfaceOrigin origin,
193 UseAllocator useAllocator) {
Robert Phillipsadbe1322018-01-17 13:35:46 -0500194#ifdef SK_DEBUG
195 if (tex->getUniqueKey().isValid()) {
196 SkASSERT(!this->findProxyByUniqueKey(tex->getUniqueKey(), origin));
197 }
198#endif
Greg Daniel14b57212019-12-17 16:18:06 -0500199 GrSwizzle readSwizzle = this->caps()->getReadSwizzle(tex->backendFormat(), colorType);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500200
201 if (tex->asRenderTarget()) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400202 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
Greg Daniel14b57212019-12-17 16:18:06 -0500203 std::move(tex), origin, readSwizzle, useAllocator));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500204 } else {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400205 return sk_sp<GrTextureProxy>(
Greg Daniel14b57212019-12-17 16:18:06 -0500206 new GrTextureProxy(std::move(tex), origin, readSwizzle, useAllocator));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500207 }
208}
209
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500210sk_sp<GrTextureProxy> GrProxyProvider::findOrCreateProxyByUniqueKey(const GrUniqueKey& key,
Brian Salomon2af3e702019-08-11 19:10:31 -0400211 GrColorType colorType,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400212 GrSurfaceOrigin origin,
213 UseAllocator useAllocator) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500214 ASSERT_SINGLE_OWNER
215
216 if (this->isAbandoned()) {
217 return nullptr;
218 }
219
220 sk_sp<GrTextureProxy> result = this->findProxyByUniqueKey(key, origin);
221 if (result) {
222 return result;
223 }
224
Robert Phillipsa41c6852019-02-07 10:44:10 -0500225 GrContext* direct = fImageContext->priv().asDirectContext();
226 if (!direct) {
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500227 return nullptr;
228 }
229
Robert Phillipsa41c6852019-02-07 10:44:10 -0500230 GrResourceCache* resourceCache = direct->priv().getResourceCache();
231
232 GrGpuResource* resource = resourceCache->findAndRefUniqueResource(key);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500233 if (!resource) {
234 return nullptr;
235 }
236
237 sk_sp<GrTexture> texture(static_cast<GrSurface*>(resource)->asTexture());
238 SkASSERT(texture);
239
Brian Salomonbeb7f522019-08-30 16:19:42 -0400240 result = this->createWrapped(std::move(texture), colorType, origin, useAllocator);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500241 SkASSERT(result->getUniqueKey() == key);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500242 // createWrapped should've added this for us
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500243 SkASSERT(fUniquelyKeyedProxies.find(key));
Greg Daniel87506ab2020-02-12 13:05:42 -0500244 SkASSERT(result->textureSwizzleDoNotUse() ==
Greg Daniel14b57212019-12-17 16:18:06 -0500245 this->caps()->getReadSwizzle(result->backendFormat(), colorType));
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500246 return result;
247}
248
Brian Osmande496652019-03-22 13:42:33 -0400249sk_sp<GrTextureProxy> GrProxyProvider::createProxyFromBitmap(const SkBitmap& bitmap,
Greg Daniel6f5441a2020-01-28 17:02:49 -0500250 GrMipMapped mipMapped,
251 SkBackingFit fit) {
Brian Osman412674f2019-02-07 15:34:58 -0500252 ASSERT_SINGLE_OWNER
Greg Daniel6f5441a2020-01-28 17:02:49 -0500253 SkASSERT(fit == SkBackingFit::kExact || mipMapped == GrMipMapped::kNo);
Brian Osman412674f2019-02-07 15:34:58 -0500254
255 if (this->isAbandoned()) {
256 return nullptr;
257 }
258
Brian Osman2b23c4b2018-06-01 12:25:08 -0400259 if (!SkImageInfoIsValid(bitmap.info())) {
Greg Daniela4ead652018-02-07 10:21:48 -0500260 return nullptr;
261 }
262
Brian Osmande496652019-03-22 13:42:33 -0400263 ATRACE_ANDROID_FRAMEWORK("Upload %sTexture [%ux%u]",
264 GrMipMapped::kYes == mipMapped ? "MipMap " : "",
265 bitmap.width(), bitmap.height());
Greg Daniela4ead652018-02-07 10:21:48 -0500266
267 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
268 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
269 // 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 -0500270 SkBitmap copyBitmap = bitmap;
271 if (!this->renderingDirectly() && !bitmap.isImmutable()) {
272 copyBitmap.allocPixels();
273 if (!bitmap.readPixels(copyBitmap.pixmap())) {
274 return nullptr;
275 }
276 copyBitmap.setImmutable();
Greg Daniela4ead652018-02-07 10:21:48 -0500277 }
278
Greg Daniel6f5441a2020-01-28 17:02:49 -0500279 GrColorType grCT = SkColorTypeToGrColorType(copyBitmap.info().colorType());
Greg Danielce3ddaa2020-01-22 16:58:15 -0500280 GrBackendFormat format = this->caps()->getDefaultBackendFormat(grCT, GrRenderable::kNo);
Robert Phillips0a15cc62019-07-30 12:49:10 -0400281 if (!format.isValid()) {
Greg Daniel82c6b102020-01-21 10:33:22 -0500282 return nullptr;
Greg Daniel3fc5df42019-06-14 09:42:17 -0400283 }
284
Greg Daniel6f5441a2020-01-28 17:02:49 -0500285 sk_sp<GrTextureProxy> proxy;
286 if (mipMapped == GrMipMapped::kNo ||
287 0 == SkMipMap::ComputeLevelCount(copyBitmap.width(), copyBitmap.height())) {
288 proxy = this->createNonMippedProxyFromBitmap(copyBitmap, fit, format, grCT);
289 } else {
290 proxy = this->createMippedProxyFromBitmap(copyBitmap, format, grCT);
291 }
292
293 if (!proxy) {
294 return nullptr;
295 }
296
297 GrContext* direct = fImageContext->priv().asDirectContext();
298 if (direct) {
299 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
300
301 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
302 // we're better off instantiating the proxy immediately here.
303 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
304 return nullptr;
305 }
306 }
307 return proxy;
308}
309
310sk_sp<GrTextureProxy> GrProxyProvider::createNonMippedProxyFromBitmap(const SkBitmap& bitmap,
311 SkBackingFit fit,
312 const GrBackendFormat& format,
313 GrColorType colorType) {
Greg Daniel6f5441a2020-01-28 17:02:49 -0500314 GrSwizzle swizzle = this->caps()->getReadSwizzle(format, colorType);
Brian Salomona56a7462020-02-07 14:17:25 -0500315 auto dims = bitmap.dimensions();
Greg Daniel6f5441a2020-01-28 17:02:49 -0500316
317 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomona56a7462020-02-07 14:17:25 -0500318 [dims, format, bitmap, fit, colorType](GrResourceProvider* resourceProvider) {
Greg Daniel6f5441a2020-01-28 17:02:49 -0500319 GrMipLevel mipLevel = { bitmap.getPixels(), bitmap.rowBytes() };
320
321 return LazyCallbackResult(resourceProvider->createTexture(
Brian Salomona56a7462020-02-07 14:17:25 -0500322 dims, format, colorType, GrRenderable::kNo, 1, SkBudgeted::kYes, fit,
Greg Daniel6f5441a2020-01-28 17:02:49 -0500323 GrProtected::kNo, mipLevel));
324 },
Brian Salomona56a7462020-02-07 14:17:25 -0500325 format, dims, swizzle, GrRenderable::kNo, 1, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo,
326 GrMipMapsStatus::kNotAllocated, GrInternalSurfaceFlags::kNone, fit, SkBudgeted::kYes,
327 GrProtected::kNo, UseAllocator::kYes);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500328
329 if (!proxy) {
330 return nullptr;
331 }
Brian Salomona56a7462020-02-07 14:17:25 -0500332 SkASSERT(proxy->dimensions() == bitmap.dimensions());
Greg Daniel6f5441a2020-01-28 17:02:49 -0500333 return proxy;
334}
335
336sk_sp<GrTextureProxy> GrProxyProvider::createMippedProxyFromBitmap(const SkBitmap& bitmap,
337 const GrBackendFormat& format,
338 GrColorType colorType) {
339 SkASSERT(this->caps()->mipMapSupport());
340
Greg Daniel6f5441a2020-01-28 17:02:49 -0500341
342 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(bitmap.pixmap(), nullptr));
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400343 if (!mipmaps) {
344 return nullptr;
345 }
346
Greg Daniel6f5441a2020-01-28 17:02:49 -0500347 GrSwizzle readSwizzle = this->caps()->getReadSwizzle(format, colorType);
Brian Salomona56a7462020-02-07 14:17:25 -0500348 auto dims = bitmap.dimensions();
Greg Danielce3ddaa2020-01-22 16:58:15 -0500349
Greg Daniela4ead652018-02-07 10:21:48 -0500350 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomona56a7462020-02-07 14:17:25 -0500351 [dims, format, bitmap, mipmaps](GrResourceProvider* resourceProvider) {
Brian Osman1b97f132018-09-13 17:33:48 +0000352 const int mipLevelCount = mipmaps->countLevels() + 1;
353 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
354
Greg Daniel6f5441a2020-01-28 17:02:49 -0500355 texels[0].fPixels = bitmap.getPixels();
356 texels[0].fRowBytes = bitmap.rowBytes();
Greg Daniela4ead652018-02-07 10:21:48 -0500357
Greg Daniel6f5441a2020-01-28 17:02:49 -0500358 auto colorType = SkColorTypeToGrColorType(bitmap.colorType());
Greg Daniela4ead652018-02-07 10:21:48 -0500359 for (int i = 1; i < mipLevelCount; ++i) {
360 SkMipMap::Level generatedMipLevel;
361 mipmaps->getLevel(i - 1, &generatedMipLevel);
362 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
363 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
364 SkASSERT(texels[i].fPixels);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500365 SkASSERT(generatedMipLevel.fPixmap.colorType() == bitmap.colorType());
Greg Daniela4ead652018-02-07 10:21:48 -0500366 }
Brian Salomonbeb7f522019-08-30 16:19:42 -0400367 return LazyCallbackResult(resourceProvider->createTexture(
Brian Salomona56a7462020-02-07 14:17:25 -0500368 dims, format, colorType, GrRenderable::kNo, 1, SkBudgeted::kYes,
Brian Salomona90382f2019-09-17 09:01:56 -0400369 GrProtected::kNo, texels.get(), mipLevelCount));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500370 },
Brian Salomona56a7462020-02-07 14:17:25 -0500371 format, dims, readSwizzle, GrRenderable::kNo, 1, kTopLeft_GrSurfaceOrigin,
Greg Danielce3ddaa2020-01-22 16:58:15 -0500372 GrMipMapped::kYes, GrMipMapsStatus::kValid, GrInternalSurfaceFlags::kNone,
373 SkBackingFit::kExact, SkBudgeted::kYes, GrProtected::kNo, UseAllocator::kYes);
Greg Daniela4ead652018-02-07 10:21:48 -0500374
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400375 if (!proxy) {
376 return nullptr;
377 }
378
Brian Salomona56a7462020-02-07 14:17:25 -0500379 SkASSERT(proxy->dimensions() == bitmap.dimensions());
Greg Daniel6f5441a2020-01-28 17:02:49 -0500380
Greg Daniela4ead652018-02-07 10:21:48 -0500381 return proxy;
382}
383
Greg Daniel4065d452018-11-16 15:43:41 -0500384sk_sp<GrTextureProxy> GrProxyProvider::createProxy(const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -0500385 SkISize dimensions,
Greg Daniel47c20e82020-01-21 14:29:57 -0500386 GrSwizzle readSwizzle,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400387 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400388 int renderTargetSampleCnt,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500389 GrSurfaceOrigin origin,
Greg Danielf6f7b672018-02-15 13:06:26 -0500390 GrMipMapped mipMapped,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500391 SkBackingFit fit,
392 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -0400393 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400394 GrInternalSurfaceFlags surfaceFlags,
395 GrSurfaceProxy::UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500396 ASSERT_SINGLE_OWNER
397 if (this->isAbandoned()) {
398 return nullptr;
399 }
400
Robert Phillips8ff8bcc2019-07-29 17:03:35 -0400401 const GrCaps* caps = this->caps();
402
403 if (caps->isFormatCompressed(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400404 // Deferred proxies for compressed textures are not supported.
405 return nullptr;
406 }
Robert Phillips0902c982019-07-16 07:47:56 -0400407
Greg Danielf6f7b672018-02-15 13:06:26 -0500408 if (GrMipMapped::kYes == mipMapped) {
409 // SkMipMap doesn't include the base level in the level count so we have to add 1
Brian Salomona56a7462020-02-07 14:17:25 -0500410 int mipCount = SkMipMap::ComputeLevelCount(dimensions.fWidth, dimensions.fHeight) + 1;
Greg Danielf6f7b672018-02-15 13:06:26 -0500411 if (1 == mipCount) {
412 mipMapped = GrMipMapped::kNo;
413 }
414 }
415
Brian Salomona56a7462020-02-07 14:17:25 -0500416 if (!caps->validateSurfaceParams(dimensions, format, renderable, renderTargetSampleCnt,
417 mipMapped)) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500418 return nullptr;
419 }
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600420 GrMipMapsStatus mipMapsStatus = (GrMipMapped::kYes == mipMapped)
421 ? GrMipMapsStatus::kDirty
422 : GrMipMapsStatus::kNotAllocated;
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400423 if (renderable == GrRenderable::kYes) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400424 renderTargetSampleCnt =
Greg Daniel6fa62e22019-08-07 15:52:37 -0400425 caps->getRenderTargetSampleCount(renderTargetSampleCnt, format);
426 SkASSERT(renderTargetSampleCnt);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500427 // We know anything we instantiate later from this deferred path will be
428 // both texturable and renderable
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400429 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
Brian Salomona56a7462020-02-07 14:17:25 -0500430 *caps, format, dimensions, renderTargetSampleCnt, origin, mipMapped, mipMapsStatus,
Greg Daniel14b57212019-12-17 16:18:06 -0500431 readSwizzle, fit, budgeted, isProtected, surfaceFlags, useAllocator));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500432 }
433
Brian Salomona56a7462020-02-07 14:17:25 -0500434 return sk_sp<GrTextureProxy>(new GrTextureProxy(format, dimensions, origin, mipMapped,
Greg Daniel14b57212019-12-17 16:18:06 -0500435 mipMapsStatus, readSwizzle, fit, budgeted,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400436 isProtected, surfaceFlags, useAllocator));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500437}
438
Brian Salomonbb8dde82019-06-27 10:52:13 -0400439sk_sp<GrTextureProxy> GrProxyProvider::createCompressedTextureProxy(
Robert Phillips3a833922020-01-21 15:25:58 -0500440 SkISize dimensions, SkBudgeted budgeted, GrMipMapped mipMapped, GrProtected isProtected,
Robert Phillipse4720c62020-01-14 14:33:24 -0500441 SkImage::CompressionType compressionType, sk_sp<SkData> data) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500442 ASSERT_SINGLE_OWNER
443 if (this->isAbandoned()) {
444 return nullptr;
445 }
Brian Salomonbb8dde82019-06-27 10:52:13 -0400446
Robert Phillips8ff8bcc2019-07-29 17:03:35 -0400447 GrBackendFormat format = this->caps()->getBackendFormatFromCompressionType(compressionType);
448
Greg Daniel7bfc9132019-08-14 14:23:53 -0400449 if (!this->caps()->isFormatTexturable(format)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500450 return nullptr;
451 }
452
Robert Phillipse4720c62020-01-14 14:33:24 -0500453 GrMipMapsStatus mipMapsStatus = (GrMipMapped::kYes == mipMapped)
454 ? GrMipMapsStatus::kValid
455 : GrMipMapsStatus::kNotAllocated;
456
Jim Van Verthee06b332019-01-18 10:36:32 -0500457 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomona56a7462020-02-07 14:17:25 -0500458 [dimensions, format, budgeted, mipMapped, isProtected,
459 data](GrResourceProvider* resourceProvider) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400460 return LazyCallbackResult(resourceProvider->createCompressedTexture(
Robert Phillips3a833922020-01-21 15:25:58 -0500461 dimensions, format, budgeted, mipMapped, isProtected, data.get()));
Brian Salomonbb8dde82019-06-27 10:52:13 -0400462 },
Brian Salomona56a7462020-02-07 14:17:25 -0500463 format, dimensions, GrSwizzle(), GrRenderable::kNo, 1, kTopLeft_GrSurfaceOrigin,
464 mipMapped, mipMapsStatus, GrInternalSurfaceFlags::kReadOnly, SkBackingFit::kExact,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400465 SkBudgeted::kYes, GrProtected::kNo, UseAllocator::kYes);
Jim Van Verthee06b332019-01-18 10:36:32 -0500466
467 if (!proxy) {
468 return nullptr;
469 }
470
Robert Phillipsa41c6852019-02-07 10:44:10 -0500471 GrContext* direct = fImageContext->priv().asDirectContext();
472 if (direct) {
473 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Jim Van Verthee06b332019-01-18 10:36:32 -0500474 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
475 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500476 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500477 return nullptr;
478 }
479 }
480 return proxy;
481}
482
Brian Salomon7578f3e2018-03-07 14:39:54 -0500483sk_sp<GrTextureProxy> GrProxyProvider::wrapBackendTexture(const GrBackendTexture& backendTex,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400484 GrColorType grColorType,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500485 GrSurfaceOrigin origin,
486 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 Phillipsc80b0e92019-07-23 10:27:09 -0400502 const GrCaps* caps = this->caps();
503
Robert Phillipsa41c6852019-02-07 10:44:10 -0500504 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
505
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500506 sk_sp<GrTexture> tex =
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400507 resourceProvider->wrapBackendTexture(backendTex, grColorType,
508 ownership, cacheable, ioType);
Brian Salomonf7778972018-03-08 10:13:17 -0500509 if (!tex) {
510 return nullptr;
511 }
Robert Phillipsadbe1322018-01-17 13:35:46 -0500512
Greg Daniel6a0176b2018-01-30 09:28:44 -0500513 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500514 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel6a0176b2018-01-30 09:28:44 -0500515 }
516
Brian Salomonf7778972018-03-08 10:13:17 -0500517 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
518 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500519 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500520
Greg Daniel14b57212019-12-17 16:18:06 -0500521 GrSwizzle readSwizzle = caps->getReadSwizzle(tex->backendFormat(), grColorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400522
Brian Salomonbeb7f522019-08-30 16:19:42 -0400523 return sk_sp<GrTextureProxy>(
Greg Daniel14b57212019-12-17 16:18:06 -0500524 new GrTextureProxy(std::move(tex), origin, readSwizzle, UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500525}
526
Robert Phillipsead321b2019-12-19 10:16:32 -0500527sk_sp<GrTextureProxy> GrProxyProvider::wrapCompressedBackendTexture(const GrBackendTexture& beTex,
528 GrSurfaceOrigin origin,
529 GrWrapOwnership ownership,
530 GrWrapCacheable cacheable,
531 ReleaseProc releaseProc,
532 ReleaseContext releaseCtx) {
533 if (this->isAbandoned()) {
534 return nullptr;
535 }
536
537 // This is only supported on a direct GrContext.
538 GrContext* direct = fImageContext->priv().asDirectContext();
539 if (!direct) {
540 return nullptr;
541 }
542
Robert Phillipsb0855272020-01-15 12:56:52 -0500543 const GrCaps* caps = this->caps();
544
Robert Phillipsead321b2019-12-19 10:16:32 -0500545 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
546
547 sk_sp<GrTexture> tex = resourceProvider->wrapCompressedBackendTexture(beTex, ownership,
548 cacheable);
549 if (!tex) {
550 return nullptr;
551 }
552
553 if (releaseProc) {
554 tex->setRelease(releaseProc, releaseCtx);
555 }
556
557 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
558 // Make sure we match how we created the proxy with SkBudgeted::kNo
559 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
560
Robert Phillipsb0855272020-01-15 12:56:52 -0500561 SkImage::CompressionType compressionType = caps->compressionType(beTex.getBackendFormat());
562
Robert Phillips99dead92020-01-27 16:11:57 -0500563 GrSwizzle texSwizzle = SkCompressionTypeIsOpaque(compressionType) ? GrSwizzle::RGB1()
Robert Phillipsb0855272020-01-15 12:56:52 -0500564 : GrSwizzle::RGBA();
Robert Phillipsead321b2019-12-19 10:16:32 -0500565
566 return sk_sp<GrTextureProxy>(
567 new GrTextureProxy(std::move(tex), origin, texSwizzle, UseAllocator::kNo));
568}
569
Brian Salomon7578f3e2018-03-07 14:39:54 -0500570sk_sp<GrTextureProxy> GrProxyProvider::wrapRenderableBackendTexture(
Brian Salomon02bd2952018-03-07 15:20:21 -0500571 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400572 GrColorType colorType, GrWrapOwnership ownership, GrWrapCacheable cacheable,
573 ReleaseProc releaseProc, ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500574 if (this->isAbandoned()) {
575 return nullptr;
576 }
577
Brian Salomonf7778972018-03-08 10:13:17 -0500578 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500579 GrContext* direct = fImageContext->priv().asDirectContext();
580 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500581 return nullptr;
582 }
583
Robert Phillips0902c982019-07-16 07:47:56 -0400584 const GrCaps* caps = this->caps();
585
Robert Phillipsa41c6852019-02-07 10:44:10 -0500586 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
587
Greg Daniel6fa62e22019-08-07 15:52:37 -0400588 // TODO: This should have been checked and validated before getting into GrProxyProvider.
589 if (!caps->isFormatAsColorTypeRenderable(colorType, backendTex.getBackendFormat(), sampleCnt)) {
Greg Danielf87651e2018-02-21 11:36:53 -0500590 return nullptr;
591 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500592
Greg Daniel6fa62e22019-08-07 15:52:37 -0400593 sampleCnt = caps->getRenderTargetSampleCount(sampleCnt, backendTex.getBackendFormat());
594 SkASSERT(sampleCnt);
595
Robert Phillipsa41c6852019-02-07 10:44:10 -0500596 sk_sp<GrTexture> tex = resourceProvider->wrapRenderableBackendTexture(backendTex, sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400597 colorType, ownership,
598 cacheable);
Brian Salomonf7778972018-03-08 10:13:17 -0500599 if (!tex) {
600 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500601 }
602
Greg Daniel8ce79912019-02-05 10:08:43 -0500603 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500604 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500605 }
606
Brian Salomonf7778972018-03-08 10:13:17 -0500607 SkASSERT(tex->asRenderTarget()); // A GrTextureRenderTarget
608 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500609 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Greg Daniel6abda432018-02-15 14:55:00 -0500610
Greg Daniel14b57212019-12-17 16:18:06 -0500611 GrSwizzle readSwizzle = caps->getReadSwizzle(tex->backendFormat(), colorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400612
Greg Daniel14b57212019-12-17 16:18:06 -0500613 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin, readSwizzle,
Greg Danielbaf8d992019-10-29 14:14:32 -0400614 UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500615}
616
Brian Salomon7578f3e2018-03-07 14:39:54 -0500617sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400618 const GrBackendRenderTarget& backendRT, GrColorType grColorType,
619 GrSurfaceOrigin origin, ReleaseProc releaseProc, ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500620 if (this->isAbandoned()) {
621 return nullptr;
622 }
623
Brian Salomonf7778972018-03-08 10:13:17 -0500624 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500625 GrContext* direct = fImageContext->priv().asDirectContext();
626 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500627 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500628 }
629
Robert Phillips62221e72019-07-24 15:07:38 -0400630 const GrCaps* caps = this->caps();
631
Robert Phillipsa41c6852019-02-07 10:44:10 -0500632 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
633
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400634 sk_sp<GrRenderTarget> rt = resourceProvider->wrapBackendRenderTarget(backendRT, grColorType);
Brian Salomonf7778972018-03-08 10:13:17 -0500635 if (!rt) {
636 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500637 }
Greg Daniel8ce79912019-02-05 10:08:43 -0500638
Greg Daniel8ce79912019-02-05 10:08:43 -0500639 if (releaseProc) {
Brian Salomon2ca31f82019-03-05 13:28:58 -0500640 rt->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500641 }
642
Brian Salomonf7778972018-03-08 10:13:17 -0500643 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
644 SkASSERT(!rt->getUniqueKey().isValid());
645 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500646 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Brian Salomonf7778972018-03-08 10:13:17 -0500647
Greg Daniel14b57212019-12-17 16:18:06 -0500648 GrSwizzle readSwizzle = caps->getReadSwizzle(rt->backendFormat(), grColorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400649
Greg Daniel14b57212019-12-17 16:18:06 -0500650 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(std::move(rt), origin, readSwizzle,
Greg Danielbaf8d992019-10-29 14:14:32 -0400651 UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500652}
653
Brian Salomon7578f3e2018-03-07 14:39:54 -0500654sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendTextureAsRenderTarget(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400655 const GrBackendTexture& backendTex, GrColorType grColorType,
656 GrSurfaceOrigin origin, int sampleCnt) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500657 if (this->isAbandoned()) {
658 return nullptr;
659 }
660
Brian Salomonf7778972018-03-08 10:13:17 -0500661 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500662 GrContext* direct = fImageContext->priv().asDirectContext();
663 if (!direct) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500664 return nullptr;
665 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500666
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400667 const GrCaps* caps = this->caps();
668
Robert Phillipsa41c6852019-02-07 10:44:10 -0500669 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
670
Brian Salomonf7778972018-03-08 10:13:17 -0500671 sk_sp<GrRenderTarget> rt =
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400672 resourceProvider->wrapBackendTextureAsRenderTarget(backendTex, sampleCnt, grColorType);
Brian Salomonf7778972018-03-08 10:13:17 -0500673 if (!rt) {
674 return nullptr;
Greg Danielf87651e2018-02-21 11:36:53 -0500675 }
Brian Salomonf7778972018-03-08 10:13:17 -0500676 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
677 SkASSERT(!rt->getUniqueKey().isValid());
Greg Danielb46add82019-01-02 14:51:29 -0500678 // 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 Danielf87651e2018-02-21 11:36:53 -0500680
Greg Daniel14b57212019-12-17 16:18:06 -0500681 GrSwizzle readSwizzle = caps->getReadSwizzle(rt->backendFormat(), grColorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400682
Greg Daniel14b57212019-12-17 16:18:06 -0500683 return sk_sp<GrSurfaceProxy>(new GrRenderTargetProxy(std::move(rt), origin, readSwizzle,
Greg Danielbaf8d992019-10-29 14:14:32 -0400684 UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500685}
686
Greg Danielb46add82019-01-02 14:51:29 -0500687sk_sp<GrRenderTargetProxy> GrProxyProvider::wrapVulkanSecondaryCBAsRenderTarget(
688 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
689 if (this->isAbandoned()) {
690 return nullptr;
691 }
692
693 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500694 GrContext* direct = fImageContext->priv().asDirectContext();
695 if (!direct) {
Greg Danielb46add82019-01-02 14:51:29 -0500696 return nullptr;
697 }
698
Robert Phillipsa41c6852019-02-07 10:44:10 -0500699 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Danielb46add82019-01-02 14:51:29 -0500700
Robert Phillipsa41c6852019-02-07 10:44:10 -0500701 sk_sp<GrRenderTarget> rt = resourceProvider->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
702 vkInfo);
Greg Danielb46add82019-01-02 14:51:29 -0500703 if (!rt) {
704 return nullptr;
705 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500706
Greg Danielb46add82019-01-02 14:51:29 -0500707 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
708 SkASSERT(!rt->getUniqueKey().isValid());
709 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500710 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielb46add82019-01-02 14:51:29 -0500711
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400712 GrColorType colorType = SkColorTypeToGrColorType(imageInfo.colorType());
Greg Daniel14b57212019-12-17 16:18:06 -0500713 GrSwizzle readSwizzle = this->caps()->getReadSwizzle(rt->backendFormat(), colorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400714
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400715 if (!this->caps()->isFormatAsColorTypeRenderable(colorType, rt->backendFormat(),
716 rt->numSamples())) {
717 return nullptr;
718 }
719
Greg Danielb46add82019-01-02 14:51:29 -0500720 // All Vulkan surfaces uses top left origins.
Brian Salomonbeb7f522019-08-30 16:19:42 -0400721 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
Greg Daniel14b57212019-12-17 16:18:06 -0500722 std::move(rt), kTopLeft_GrSurfaceOrigin, readSwizzle, UseAllocator::kNo,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400723 GrRenderTargetProxy::WrapsVkSecondaryCB::kYes));
Brian Salomone8a766b2019-07-19 14:24:36 -0400724}
725
726sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
727 const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -0500728 SkISize dimensions,
Greg Danielce3ddaa2020-01-22 16:58:15 -0500729 GrSwizzle readSwizzle,
Brian Salomone8a766b2019-07-19 14:24:36 -0400730 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400731 int renderTargetSampleCnt,
Brian Salomone8a766b2019-07-19 14:24:36 -0400732 GrSurfaceOrigin origin,
733 GrMipMapped mipMapped,
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600734 GrMipMapsStatus mipMapsStatus,
Brian Salomone8a766b2019-07-19 14:24:36 -0400735 GrInternalSurfaceFlags surfaceFlags,
736 SkBackingFit fit,
737 SkBudgeted budgeted,
738 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400739 GrSurfaceProxy::UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500740 ASSERT_SINGLE_OWNER
741 if (this->isAbandoned()) {
742 return nullptr;
743 }
Brian Salomona56a7462020-02-07 14:17:25 -0500744 SkASSERT((dimensions.fWidth <= 0 && dimensions.fHeight <= 0) ||
745 (dimensions.fWidth > 0 && dimensions.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400746
Robert Phillips0a15cc62019-07-30 12:49:10 -0400747 if (!format.isValid()) {
748 return nullptr;
749 }
750
Brian Salomona56a7462020-02-07 14:17:25 -0500751 if (dimensions.fWidth > this->caps()->maxTextureSize() ||
752 dimensions.fHeight > this->caps()->maxTextureSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400753 return nullptr;
754 }
755
Brian Salomonbeb7f522019-08-30 16:19:42 -0400756 if (renderable == GrRenderable::kYes) {
757 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(*this->caps(),
758 std::move(callback),
759 format,
Brian Salomona56a7462020-02-07 14:17:25 -0500760 dimensions,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400761 renderTargetSampleCnt,
762 origin,
763 mipMapped,
764 mipMapsStatus,
Greg Daniel14b57212019-12-17 16:18:06 -0500765 readSwizzle,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400766 fit,
767 budgeted,
768 isProtected,
769 surfaceFlags,
770 useAllocator));
771 } else {
772 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(callback),
773 format,
Brian Salomona56a7462020-02-07 14:17:25 -0500774 dimensions,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400775 origin,
776 mipMapped,
777 mipMapsStatus,
Greg Daniel14b57212019-12-17 16:18:06 -0500778 readSwizzle,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400779 fit,
780 budgeted,
781 isProtected,
782 surfaceFlags,
783 useAllocator));
784 }
Robert Phillips777707b2018-01-17 11:40:14 -0500785}
786
Robert Phillipse8fabb22018-02-04 14:33:21 -0500787sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy(
Brian Salomonbeb7f522019-08-30 16:19:42 -0400788 LazyInstantiateCallback&& callback,
789 const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -0500790 SkISize dimensions,
Greg Danielce3ddaa2020-01-22 16:58:15 -0500791 GrSwizzle readSwizzle,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400792 int sampleCnt,
793 GrSurfaceOrigin origin,
794 GrInternalSurfaceFlags surfaceFlags,
795 const TextureInfo* textureInfo,
796 GrMipMapsStatus mipMapsStatus,
797 SkBackingFit fit,
798 SkBudgeted budgeted,
799 GrProtected isProtected,
800 bool wrapsVkSecondaryCB,
801 UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500802 ASSERT_SINGLE_OWNER
803 if (this->isAbandoned()) {
804 return nullptr;
805 }
Brian Salomona56a7462020-02-07 14:17:25 -0500806 SkASSERT((dimensions.fWidth <= 0 && dimensions.fHeight <= 0) ||
807 (dimensions.fWidth > 0 && dimensions.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400808
Brian Salomona56a7462020-02-07 14:17:25 -0500809 if (dimensions.fWidth > this->caps()->maxRenderTargetSize() ||
810 dimensions.fHeight > this->caps()->maxRenderTargetSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400811 return nullptr;
812 }
813
Brian Salomon7226c232018-07-30 13:13:17 -0400814 if (textureInfo) {
Greg Danielb085fa92019-03-05 16:55:12 -0500815 // Wrapped vulkan secondary command buffers don't support texturing since we won't have an
816 // actual VkImage to texture from.
817 SkASSERT(!wrapsVkSecondaryCB);
Brian Salomon7226c232018-07-30 13:13:17 -0400818 return sk_sp<GrRenderTargetProxy>(new GrTextureRenderTargetProxy(
Brian Salomona56a7462020-02-07 14:17:25 -0500819 *this->caps(), std::move(callback), format, dimensions, sampleCnt, origin,
Greg Daniel14b57212019-12-17 16:18:06 -0500820 textureInfo->fMipMapped, mipMapsStatus, readSwizzle, fit, budgeted, isProtected,
Greg Danielbaf8d992019-10-29 14:14:32 -0400821 surfaceFlags, useAllocator));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500822 }
823
Greg Danielb085fa92019-03-05 16:55:12 -0500824 GrRenderTargetProxy::WrapsVkSecondaryCB vkSCB =
825 wrapsVkSecondaryCB ? GrRenderTargetProxy::WrapsVkSecondaryCB::kYes
826 : GrRenderTargetProxy::WrapsVkSecondaryCB::kNo;
827
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400828 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
Brian Salomona56a7462020-02-07 14:17:25 -0500829 std::move(callback), format, dimensions, sampleCnt, origin, readSwizzle, fit, budgeted,
Greg Danielbaf8d992019-10-29 14:14:32 -0400830 isProtected, surfaceFlags, useAllocator, vkSCB));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500831}
832
Brian Salomonbeb7f522019-08-30 16:19:42 -0400833sk_sp<GrTextureProxy> GrProxyProvider::MakeFullyLazyProxy(LazyInstantiateCallback&& callback,
834 const GrBackendFormat& format,
Greg Danielce3ddaa2020-01-22 16:58:15 -0500835 GrSwizzle readSwizzle,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400836 GrRenderable renderable,
837 int renderTargetSampleCnt,
838 GrProtected isProtected,
839 GrSurfaceOrigin origin,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400840 const GrCaps& caps,
841 UseAllocator useAllocator) {
Robert Phillips0a15cc62019-07-30 12:49:10 -0400842 if (!format.isValid()) {
843 return nullptr;
844 }
845
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400846 SkASSERT(renderTargetSampleCnt == 1 || renderable == GrRenderable::kYes);
Robert Phillips10d17212019-04-24 14:09:10 -0400847 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNone;
Robert Phillips777707b2018-01-17 11:40:14 -0500848
Brian Salomona56a7462020-02-07 14:17:25 -0500849 static constexpr SkISize kLazyDims = {-1, -1};
Brian Salomonbeb7f522019-08-30 16:19:42 -0400850 if (GrRenderable::kYes == renderable) {
851 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
Brian Salomona56a7462020-02-07 14:17:25 -0500852 caps, std::move(callback), format, kLazyDims, renderTargetSampleCnt, origin,
Greg Daniel14b57212019-12-17 16:18:06 -0500853 GrMipMapped::kNo, GrMipMapsStatus::kNotAllocated, readSwizzle,
854 SkBackingFit::kApprox, SkBudgeted::kYes, isProtected, surfaceFlags, useAllocator));
Brian Salomonbeb7f522019-08-30 16:19:42 -0400855 } else {
856 return sk_sp<GrTextureProxy>(new GrTextureProxy(
Brian Salomona56a7462020-02-07 14:17:25 -0500857 std::move(callback), format, kLazyDims, origin, GrMipMapped::kNo,
Greg Daniel14b57212019-12-17 16:18:06 -0500858 GrMipMapsStatus::kNotAllocated, readSwizzle, SkBackingFit::kApprox,
859 SkBudgeted::kYes, isProtected, surfaceFlags, useAllocator));
Brian Salomonbeb7f522019-08-30 16:19:42 -0400860 }
Robert Phillips777707b2018-01-17 11:40:14 -0500861}
862
Robert Phillips427966a2018-12-20 17:20:43 -0500863void GrProxyProvider::processInvalidUniqueKey(const GrUniqueKey& key, GrTextureProxy* proxy,
864 InvalidateGPUResource invalidateGPUResource) {
Chris Dalton2de13dd2019-01-03 15:11:59 -0700865 SkASSERT(key.isValid());
866
Robert Phillips427966a2018-12-20 17:20:43 -0500867 if (!proxy) {
868 proxy = fUniquelyKeyedProxies.find(key);
869 }
Chris Dalton2de13dd2019-01-03 15:11:59 -0700870 SkASSERT(!proxy || proxy->getUniqueKey() == key);
871
872 // Locate the corresponding GrGpuResource (if it needs to be invalidated) before clearing the
873 // proxy's unique key. We must do it in this order because 'key' may alias the proxy's key.
874 sk_sp<GrGpuResource> invalidGpuResource;
875 if (InvalidateGPUResource::kYes == invalidateGPUResource) {
Brian Salomon01ceae92019-04-02 11:49:54 -0400876 GrContext* direct = fImageContext->priv().asDirectContext();
877 if (direct) {
878 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
879 invalidGpuResource = resourceProvider->findByUniqueKey<GrGpuResource>(key);
Chris Dalton2de13dd2019-01-03 15:11:59 -0700880 }
881 SkASSERT(!invalidGpuResource || invalidGpuResource->getUniqueKey() == key);
882 }
Robert Phillips427966a2018-12-20 17:20:43 -0500883
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500884 // Note: this method is called for the whole variety of GrGpuResources so often 'key'
885 // will not be in 'fUniquelyKeyedProxies'.
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500886 if (proxy) {
Robert Phillips427966a2018-12-20 17:20:43 -0500887 fUniquelyKeyedProxies.remove(key);
888 proxy->cacheAccess().clearUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500889 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500890
Chris Dalton2de13dd2019-01-03 15:11:59 -0700891 if (invalidGpuResource) {
892 invalidGpuResource->resourcePriv().removeUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500893 }
894}
895
Robert Phillipsa41c6852019-02-07 10:44:10 -0500896uint32_t GrProxyProvider::contextID() const {
897 return fImageContext->priv().contextID();
898}
899
900const GrCaps* GrProxyProvider::caps() const {
901 return fImageContext->priv().caps();
902}
903
904sk_sp<const GrCaps> GrProxyProvider::refCaps() const {
905 return fImageContext->priv().refCaps();
906}
907
Robert Phillipsa9162df2019-02-11 14:12:03 -0500908bool GrProxyProvider::isAbandoned() const {
909 return fImageContext->priv().abandoned();
910}
911
Robert Phillips0790f8a2018-09-18 13:11:03 -0400912void GrProxyProvider::orphanAllUniqueKeys() {
913 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
914 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
915 GrTextureProxy& tmp = *iter;
916
917 tmp.fProxyProvider = nullptr;
918 }
919}
920
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500921void GrProxyProvider::removeAllUniqueKeys() {
922 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
923 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
924 GrTextureProxy& tmp = *iter;
925
Chris Dalton2de13dd2019-01-03 15:11:59 -0700926 this->processInvalidUniqueKey(tmp.getUniqueKey(), &tmp, InvalidateGPUResource::kNo);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500927 }
928 SkASSERT(!fUniquelyKeyedProxies.count());
929}
Robert Phillipsa41c6852019-02-07 10:44:10 -0500930
931bool GrProxyProvider::renderingDirectly() const {
932 return fImageContext->priv().asDirectContext();
933}