blob: e6bf8349685b3a32c8a3a5ab823ecd8b0013d5b2 [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 SkASSERT(proxy->origin() == origin);
109 return sk_ref_sp(proxy);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500110 }
Robert Phillipse5f73282019-06-18 17:15:04 -0400111 return nullptr;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500112}
113
Robert Phillipsa41c6852019-02-07 10:44:10 -0500114///////////////////////////////////////////////////////////////////////////////
115
116#if GR_TEST_UTILS
117sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createInstantiatedProxy(
Brian Salomona56a7462020-02-07 14:17:25 -0500118 SkISize dimensions,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400119 GrColorType colorType,
120 const GrBackendFormat& format,
121 GrRenderable renderable,
122 int renderTargetSampleCnt,
123 GrSurfaceOrigin origin,
124 SkBackingFit fit,
125 SkBudgeted budgeted,
126 GrProtected isProtected) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500127 ASSERT_SINGLE_OWNER
128 if (this->isAbandoned()) {
129 return nullptr;
130 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500131 GrContext* direct = fImageContext->priv().asDirectContext();
132 if (!direct) {
133 return nullptr;
134 }
135
Brian Salomon4eb38b72019-08-05 12:58:39 -0400136 if (this->caps()->isFormatCompressed(format)) {
137 // TODO: Allow this to go to GrResourceProvider::createCompressedTexture() once we no longer
Greg Daniel4cb29332020-01-23 10:07:02 -0500138 // rely on GrColorType to get a swizzle for the proxy.
Brian Salomon4eb38b72019-08-05 12:58:39 -0400139 return nullptr;
140 }
Brian Salomon4eb38b72019-08-05 12:58:39 -0400141
Robert Phillipsa41c6852019-02-07 10:44:10 -0500142 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
143 sk_sp<GrTexture> tex;
144
145 if (SkBackingFit::kApprox == fit) {
Brian Salomona56a7462020-02-07 14:17:25 -0500146 tex = resourceProvider->createApproxTexture(dimensions, format, renderable,
147 renderTargetSampleCnt, isProtected);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500148 } else {
Brian Salomona56a7462020-02-07 14:17:25 -0500149 tex = resourceProvider->createTexture(dimensions, format, renderable, renderTargetSampleCnt,
Brian Salomona90382f2019-09-17 09:01:56 -0400150 GrMipMapped::kNo, budgeted, isProtected);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500151 }
152 if (!tex) {
153 return nullptr;
154 }
155
Brian Salomonbeb7f522019-08-30 16:19:42 -0400156 return this->createWrapped(std::move(tex), colorType, origin, UseAllocator::kYes);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500157}
158
Brian Salomon4eb38b72019-08-05 12:58:39 -0400159sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createInstantiatedProxy(
Brian Salomona56a7462020-02-07 14:17:25 -0500160 SkISize dimensions,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400161 GrColorType colorType,
162 GrRenderable renderable,
163 int renderTargetSampleCnt,
164 GrSurfaceOrigin origin,
165 SkBackingFit fit,
166 SkBudgeted budgeted,
167 GrProtected isProtected) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500168 ASSERT_SINGLE_OWNER
169 if (this->isAbandoned()) {
170 return nullptr;
171 }
Brian Salomon4eb38b72019-08-05 12:58:39 -0400172 auto format = this->caps()->getDefaultBackendFormat(colorType, renderable);
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400173 return this->testingOnly_createInstantiatedProxy(dimensions,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400174 colorType,
175 format,
176 renderable,
177 renderTargetSampleCnt,
178 origin,
179 fit,
180 budgeted,
181 isProtected);
182}
183
Robert Phillipsa41c6852019-02-07 10:44:10 -0500184sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createWrapped(sk_sp<GrTexture> tex,
Brian Salomon2af3e702019-08-11 19:10:31 -0400185 GrColorType colorType,
Robert Phillipsa41c6852019-02-07 10:44:10 -0500186 GrSurfaceOrigin origin) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400187 return this->createWrapped(std::move(tex), colorType, origin, UseAllocator::kYes);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500188}
189#endif
190
Brian Salomonbeb7f522019-08-30 16:19:42 -0400191sk_sp<GrTextureProxy> GrProxyProvider::createWrapped(sk_sp<GrTexture> tex,
192 GrColorType colorType,
193 GrSurfaceOrigin origin,
194 UseAllocator useAllocator) {
Robert Phillipsadbe1322018-01-17 13:35:46 -0500195#ifdef SK_DEBUG
196 if (tex->getUniqueKey().isValid()) {
197 SkASSERT(!this->findProxyByUniqueKey(tex->getUniqueKey(), origin));
198 }
199#endif
Greg Daniel14b57212019-12-17 16:18:06 -0500200 GrSwizzle readSwizzle = this->caps()->getReadSwizzle(tex->backendFormat(), colorType);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500201
202 if (tex->asRenderTarget()) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400203 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
Greg Daniel14b57212019-12-17 16:18:06 -0500204 std::move(tex), origin, readSwizzle, useAllocator));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500205 } else {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400206 return sk_sp<GrTextureProxy>(
Greg Daniel14b57212019-12-17 16:18:06 -0500207 new GrTextureProxy(std::move(tex), origin, readSwizzle, useAllocator));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500208 }
209}
210
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500211sk_sp<GrTextureProxy> GrProxyProvider::findOrCreateProxyByUniqueKey(const GrUniqueKey& key,
Brian Salomon2af3e702019-08-11 19:10:31 -0400212 GrColorType colorType,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400213 GrSurfaceOrigin origin,
214 UseAllocator useAllocator) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500215 ASSERT_SINGLE_OWNER
216
217 if (this->isAbandoned()) {
218 return nullptr;
219 }
220
221 sk_sp<GrTextureProxy> result = this->findProxyByUniqueKey(key, origin);
222 if (result) {
223 return result;
224 }
225
Robert Phillipsa41c6852019-02-07 10:44:10 -0500226 GrContext* direct = fImageContext->priv().asDirectContext();
227 if (!direct) {
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500228 return nullptr;
229 }
230
Robert Phillipsa41c6852019-02-07 10:44:10 -0500231 GrResourceCache* resourceCache = direct->priv().getResourceCache();
232
233 GrGpuResource* resource = resourceCache->findAndRefUniqueResource(key);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500234 if (!resource) {
235 return nullptr;
236 }
237
238 sk_sp<GrTexture> texture(static_cast<GrSurface*>(resource)->asTexture());
239 SkASSERT(texture);
240
Brian Salomonbeb7f522019-08-30 16:19:42 -0400241 result = this->createWrapped(std::move(texture), colorType, origin, useAllocator);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500242 SkASSERT(result->getUniqueKey() == key);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500243 // createWrapped should've added this for us
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500244 SkASSERT(fUniquelyKeyedProxies.find(key));
Brian Salomon2af3e702019-08-11 19:10:31 -0400245 SkASSERT(result->textureSwizzle() ==
Greg Daniel14b57212019-12-17 16:18:06 -0500246 this->caps()->getReadSwizzle(result->backendFormat(), colorType));
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500247 return result;
248}
249
Brian Osmande496652019-03-22 13:42:33 -0400250sk_sp<GrTextureProxy> GrProxyProvider::createProxyFromBitmap(const SkBitmap& bitmap,
Greg Daniel6f5441a2020-01-28 17:02:49 -0500251 GrMipMapped mipMapped,
252 SkBackingFit fit) {
Brian Osman412674f2019-02-07 15:34:58 -0500253 ASSERT_SINGLE_OWNER
Greg Daniel6f5441a2020-01-28 17:02:49 -0500254 SkASSERT(fit == SkBackingFit::kExact || mipMapped == GrMipMapped::kNo);
Brian Osman412674f2019-02-07 15:34:58 -0500255
256 if (this->isAbandoned()) {
257 return nullptr;
258 }
259
Brian Osman2b23c4b2018-06-01 12:25:08 -0400260 if (!SkImageInfoIsValid(bitmap.info())) {
Greg Daniela4ead652018-02-07 10:21:48 -0500261 return nullptr;
262 }
263
Brian Osmande496652019-03-22 13:42:33 -0400264 ATRACE_ANDROID_FRAMEWORK("Upload %sTexture [%ux%u]",
265 GrMipMapped::kYes == mipMapped ? "MipMap " : "",
266 bitmap.width(), bitmap.height());
Greg Daniela4ead652018-02-07 10:21:48 -0500267
268 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
269 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
270 // 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 -0500271 SkBitmap copyBitmap = bitmap;
272 if (!this->renderingDirectly() && !bitmap.isImmutable()) {
273 copyBitmap.allocPixels();
274 if (!bitmap.readPixels(copyBitmap.pixmap())) {
275 return nullptr;
276 }
277 copyBitmap.setImmutable();
Greg Daniela4ead652018-02-07 10:21:48 -0500278 }
279
Greg Daniel6f5441a2020-01-28 17:02:49 -0500280 GrColorType grCT = SkColorTypeToGrColorType(copyBitmap.info().colorType());
Greg Danielce3ddaa2020-01-22 16:58:15 -0500281 GrBackendFormat format = this->caps()->getDefaultBackendFormat(grCT, GrRenderable::kNo);
Robert Phillips0a15cc62019-07-30 12:49:10 -0400282 if (!format.isValid()) {
Greg Daniel82c6b102020-01-21 10:33:22 -0500283 return nullptr;
Greg Daniel3fc5df42019-06-14 09:42:17 -0400284 }
285
Greg Daniel6f5441a2020-01-28 17:02:49 -0500286 sk_sp<GrTextureProxy> proxy;
287 if (mipMapped == GrMipMapped::kNo ||
288 0 == SkMipMap::ComputeLevelCount(copyBitmap.width(), copyBitmap.height())) {
289 proxy = this->createNonMippedProxyFromBitmap(copyBitmap, fit, format, grCT);
290 } else {
291 proxy = this->createMippedProxyFromBitmap(copyBitmap, format, grCT);
292 }
293
294 if (!proxy) {
295 return nullptr;
296 }
297
298 GrContext* direct = fImageContext->priv().asDirectContext();
299 if (direct) {
300 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
301
302 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
303 // we're better off instantiating the proxy immediately here.
304 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
305 return nullptr;
306 }
307 }
308 return proxy;
309}
310
311sk_sp<GrTextureProxy> GrProxyProvider::createNonMippedProxyFromBitmap(const SkBitmap& bitmap,
312 SkBackingFit fit,
313 const GrBackendFormat& format,
314 GrColorType colorType) {
Greg Daniel6f5441a2020-01-28 17:02:49 -0500315 GrSwizzle swizzle = this->caps()->getReadSwizzle(format, colorType);
Brian Salomona56a7462020-02-07 14:17:25 -0500316 auto dims = bitmap.dimensions();
Greg Daniel6f5441a2020-01-28 17:02:49 -0500317
318 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomona56a7462020-02-07 14:17:25 -0500319 [dims, format, bitmap, fit, colorType](GrResourceProvider* resourceProvider) {
Greg Daniel6f5441a2020-01-28 17:02:49 -0500320 GrMipLevel mipLevel = { bitmap.getPixels(), bitmap.rowBytes() };
321
322 return LazyCallbackResult(resourceProvider->createTexture(
Brian Salomona56a7462020-02-07 14:17:25 -0500323 dims, format, colorType, GrRenderable::kNo, 1, SkBudgeted::kYes, fit,
Greg Daniel6f5441a2020-01-28 17:02:49 -0500324 GrProtected::kNo, mipLevel));
325 },
Brian Salomona56a7462020-02-07 14:17:25 -0500326 format, dims, swizzle, GrRenderable::kNo, 1, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo,
327 GrMipMapsStatus::kNotAllocated, GrInternalSurfaceFlags::kNone, fit, SkBudgeted::kYes,
328 GrProtected::kNo, UseAllocator::kYes);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500329
330 if (!proxy) {
331 return nullptr;
332 }
Brian Salomona56a7462020-02-07 14:17:25 -0500333 SkASSERT(proxy->dimensions() == bitmap.dimensions());
Greg Daniel6f5441a2020-01-28 17:02:49 -0500334 return proxy;
335}
336
337sk_sp<GrTextureProxy> GrProxyProvider::createMippedProxyFromBitmap(const SkBitmap& bitmap,
338 const GrBackendFormat& format,
339 GrColorType colorType) {
340 SkASSERT(this->caps()->mipMapSupport());
341
Greg Daniel6f5441a2020-01-28 17:02:49 -0500342
343 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(bitmap.pixmap(), nullptr));
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400344 if (!mipmaps) {
345 return nullptr;
346 }
347
Greg Daniel6f5441a2020-01-28 17:02:49 -0500348 GrSwizzle readSwizzle = this->caps()->getReadSwizzle(format, colorType);
Brian Salomona56a7462020-02-07 14:17:25 -0500349 auto dims = bitmap.dimensions();
Greg Danielce3ddaa2020-01-22 16:58:15 -0500350
Greg Daniela4ead652018-02-07 10:21:48 -0500351 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomona56a7462020-02-07 14:17:25 -0500352 [dims, format, bitmap, mipmaps](GrResourceProvider* resourceProvider) {
Brian Osman1b97f132018-09-13 17:33:48 +0000353 const int mipLevelCount = mipmaps->countLevels() + 1;
354 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
355
Greg Daniel6f5441a2020-01-28 17:02:49 -0500356 texels[0].fPixels = bitmap.getPixels();
357 texels[0].fRowBytes = bitmap.rowBytes();
Greg Daniela4ead652018-02-07 10:21:48 -0500358
Greg Daniel6f5441a2020-01-28 17:02:49 -0500359 auto colorType = SkColorTypeToGrColorType(bitmap.colorType());
Greg Daniela4ead652018-02-07 10:21:48 -0500360 for (int i = 1; i < mipLevelCount; ++i) {
361 SkMipMap::Level generatedMipLevel;
362 mipmaps->getLevel(i - 1, &generatedMipLevel);
363 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
364 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
365 SkASSERT(texels[i].fPixels);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500366 SkASSERT(generatedMipLevel.fPixmap.colorType() == bitmap.colorType());
Greg Daniela4ead652018-02-07 10:21:48 -0500367 }
Brian Salomonbeb7f522019-08-30 16:19:42 -0400368 return LazyCallbackResult(resourceProvider->createTexture(
Brian Salomona56a7462020-02-07 14:17:25 -0500369 dims, format, colorType, GrRenderable::kNo, 1, SkBudgeted::kYes,
Brian Salomona90382f2019-09-17 09:01:56 -0400370 GrProtected::kNo, texels.get(), mipLevelCount));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500371 },
Brian Salomona56a7462020-02-07 14:17:25 -0500372 format, dims, readSwizzle, GrRenderable::kNo, 1, kTopLeft_GrSurfaceOrigin,
Greg Danielce3ddaa2020-01-22 16:58:15 -0500373 GrMipMapped::kYes, GrMipMapsStatus::kValid, GrInternalSurfaceFlags::kNone,
374 SkBackingFit::kExact, SkBudgeted::kYes, GrProtected::kNo, UseAllocator::kYes);
Greg Daniela4ead652018-02-07 10:21:48 -0500375
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400376 if (!proxy) {
377 return nullptr;
378 }
379
Brian Salomona56a7462020-02-07 14:17:25 -0500380 SkASSERT(proxy->dimensions() == bitmap.dimensions());
Greg Daniel6f5441a2020-01-28 17:02:49 -0500381
Greg Daniela4ead652018-02-07 10:21:48 -0500382 return proxy;
383}
384
Greg Daniel4065d452018-11-16 15:43:41 -0500385sk_sp<GrTextureProxy> GrProxyProvider::createProxy(const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -0500386 SkISize dimensions,
Greg Daniel47c20e82020-01-21 14:29:57 -0500387 GrSwizzle readSwizzle,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400388 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400389 int renderTargetSampleCnt,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500390 GrSurfaceOrigin origin,
Greg Danielf6f7b672018-02-15 13:06:26 -0500391 GrMipMapped mipMapped,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500392 SkBackingFit fit,
393 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -0400394 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400395 GrInternalSurfaceFlags surfaceFlags,
396 GrSurfaceProxy::UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500397 ASSERT_SINGLE_OWNER
398 if (this->isAbandoned()) {
399 return nullptr;
400 }
401
Robert Phillips8ff8bcc2019-07-29 17:03:35 -0400402 const GrCaps* caps = this->caps();
403
404 if (caps->isFormatCompressed(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400405 // Deferred proxies for compressed textures are not supported.
406 return nullptr;
407 }
Robert Phillips0902c982019-07-16 07:47:56 -0400408
Greg Danielf6f7b672018-02-15 13:06:26 -0500409 if (GrMipMapped::kYes == mipMapped) {
410 // SkMipMap doesn't include the base level in the level count so we have to add 1
Brian Salomona56a7462020-02-07 14:17:25 -0500411 int mipCount = SkMipMap::ComputeLevelCount(dimensions.fWidth, dimensions.fHeight) + 1;
Greg Danielf6f7b672018-02-15 13:06:26 -0500412 if (1 == mipCount) {
413 mipMapped = GrMipMapped::kNo;
414 }
415 }
416
Brian Salomona56a7462020-02-07 14:17:25 -0500417 if (!caps->validateSurfaceParams(dimensions, format, renderable, renderTargetSampleCnt,
418 mipMapped)) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500419 return nullptr;
420 }
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600421 GrMipMapsStatus mipMapsStatus = (GrMipMapped::kYes == mipMapped)
422 ? GrMipMapsStatus::kDirty
423 : GrMipMapsStatus::kNotAllocated;
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400424 if (renderable == GrRenderable::kYes) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400425 renderTargetSampleCnt =
Greg Daniel6fa62e22019-08-07 15:52:37 -0400426 caps->getRenderTargetSampleCount(renderTargetSampleCnt, format);
427 SkASSERT(renderTargetSampleCnt);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500428 // We know anything we instantiate later from this deferred path will be
429 // both texturable and renderable
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400430 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
Brian Salomona56a7462020-02-07 14:17:25 -0500431 *caps, format, dimensions, renderTargetSampleCnt, origin, mipMapped, mipMapsStatus,
Greg Daniel14b57212019-12-17 16:18:06 -0500432 readSwizzle, fit, budgeted, isProtected, surfaceFlags, useAllocator));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500433 }
434
Brian Salomona56a7462020-02-07 14:17:25 -0500435 return sk_sp<GrTextureProxy>(new GrTextureProxy(format, dimensions, origin, mipMapped,
Greg Daniel14b57212019-12-17 16:18:06 -0500436 mipMapsStatus, readSwizzle, fit, budgeted,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400437 isProtected, surfaceFlags, useAllocator));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500438}
439
Brian Salomonbb8dde82019-06-27 10:52:13 -0400440sk_sp<GrTextureProxy> GrProxyProvider::createCompressedTextureProxy(
Robert Phillips3a833922020-01-21 15:25:58 -0500441 SkISize dimensions, SkBudgeted budgeted, GrMipMapped mipMapped, GrProtected isProtected,
Robert Phillipse4720c62020-01-14 14:33:24 -0500442 SkImage::CompressionType compressionType, sk_sp<SkData> data) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500443 ASSERT_SINGLE_OWNER
444 if (this->isAbandoned()) {
445 return nullptr;
446 }
Brian Salomonbb8dde82019-06-27 10:52:13 -0400447
Robert Phillips8ff8bcc2019-07-29 17:03:35 -0400448 GrBackendFormat format = this->caps()->getBackendFormatFromCompressionType(compressionType);
449
Greg Daniel7bfc9132019-08-14 14:23:53 -0400450 if (!this->caps()->isFormatTexturable(format)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500451 return nullptr;
452 }
453
Robert Phillipse4720c62020-01-14 14:33:24 -0500454 GrMipMapsStatus mipMapsStatus = (GrMipMapped::kYes == mipMapped)
455 ? GrMipMapsStatus::kValid
456 : GrMipMapsStatus::kNotAllocated;
457
Jim Van Verthee06b332019-01-18 10:36:32 -0500458 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomona56a7462020-02-07 14:17:25 -0500459 [dimensions, format, budgeted, mipMapped, isProtected,
460 data](GrResourceProvider* resourceProvider) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400461 return LazyCallbackResult(resourceProvider->createCompressedTexture(
Robert Phillips3a833922020-01-21 15:25:58 -0500462 dimensions, format, budgeted, mipMapped, isProtected, data.get()));
Brian Salomonbb8dde82019-06-27 10:52:13 -0400463 },
Brian Salomona56a7462020-02-07 14:17:25 -0500464 format, dimensions, GrSwizzle(), GrRenderable::kNo, 1, kTopLeft_GrSurfaceOrigin,
465 mipMapped, mipMapsStatus, GrInternalSurfaceFlags::kReadOnly, SkBackingFit::kExact,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400466 SkBudgeted::kYes, GrProtected::kNo, UseAllocator::kYes);
Jim Van Verthee06b332019-01-18 10:36:32 -0500467
468 if (!proxy) {
469 return nullptr;
470 }
471
Robert Phillipsa41c6852019-02-07 10:44:10 -0500472 GrContext* direct = fImageContext->priv().asDirectContext();
473 if (direct) {
474 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Jim Van Verthee06b332019-01-18 10:36:32 -0500475 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
476 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500477 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500478 return nullptr;
479 }
480 }
481 return proxy;
482}
483
Brian Salomon7578f3e2018-03-07 14:39:54 -0500484sk_sp<GrTextureProxy> GrProxyProvider::wrapBackendTexture(const GrBackendTexture& backendTex,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400485 GrColorType grColorType,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500486 GrSurfaceOrigin origin,
487 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500488 GrWrapCacheable cacheable,
Brian Salomonc67c31c2018-12-06 10:00:03 -0500489 GrIOType ioType,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500490 ReleaseProc releaseProc,
491 ReleaseContext releaseCtx) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500492 SkASSERT(ioType != kWrite_GrIOType);
Robert Phillipsf9bec202018-01-16 09:21:01 -0500493 if (this->isAbandoned()) {
494 return nullptr;
495 }
496
Brian Salomonf7778972018-03-08 10:13:17 -0500497 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500498 GrContext* direct = fImageContext->priv().asDirectContext();
499 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500500 return nullptr;
501 }
502
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400503 const GrCaps* caps = this->caps();
504
Robert Phillipsa41c6852019-02-07 10:44:10 -0500505 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
506
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500507 sk_sp<GrTexture> tex =
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400508 resourceProvider->wrapBackendTexture(backendTex, grColorType,
509 ownership, cacheable, ioType);
Brian Salomonf7778972018-03-08 10:13:17 -0500510 if (!tex) {
511 return nullptr;
512 }
Robert Phillipsadbe1322018-01-17 13:35:46 -0500513
Greg Daniel6a0176b2018-01-30 09:28:44 -0500514 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500515 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel6a0176b2018-01-30 09:28:44 -0500516 }
517
Brian Salomonf7778972018-03-08 10:13:17 -0500518 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
519 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500520 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500521
Greg Daniel14b57212019-12-17 16:18:06 -0500522 GrSwizzle readSwizzle = caps->getReadSwizzle(tex->backendFormat(), grColorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400523
Brian Salomonbeb7f522019-08-30 16:19:42 -0400524 return sk_sp<GrTextureProxy>(
Greg Daniel14b57212019-12-17 16:18:06 -0500525 new GrTextureProxy(std::move(tex), origin, readSwizzle, UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500526}
527
Robert Phillipsead321b2019-12-19 10:16:32 -0500528sk_sp<GrTextureProxy> GrProxyProvider::wrapCompressedBackendTexture(const GrBackendTexture& beTex,
529 GrSurfaceOrigin origin,
530 GrWrapOwnership ownership,
531 GrWrapCacheable cacheable,
532 ReleaseProc releaseProc,
533 ReleaseContext releaseCtx) {
534 if (this->isAbandoned()) {
535 return nullptr;
536 }
537
538 // This is only supported on a direct GrContext.
539 GrContext* direct = fImageContext->priv().asDirectContext();
540 if (!direct) {
541 return nullptr;
542 }
543
Robert Phillipsb0855272020-01-15 12:56:52 -0500544 const GrCaps* caps = this->caps();
545
Robert Phillipsead321b2019-12-19 10:16:32 -0500546 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
547
548 sk_sp<GrTexture> tex = resourceProvider->wrapCompressedBackendTexture(beTex, ownership,
549 cacheable);
550 if (!tex) {
551 return nullptr;
552 }
553
554 if (releaseProc) {
555 tex->setRelease(releaseProc, releaseCtx);
556 }
557
558 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
559 // Make sure we match how we created the proxy with SkBudgeted::kNo
560 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
561
Robert Phillipsb0855272020-01-15 12:56:52 -0500562 SkImage::CompressionType compressionType = caps->compressionType(beTex.getBackendFormat());
563
Robert Phillips99dead92020-01-27 16:11:57 -0500564 GrSwizzle texSwizzle = SkCompressionTypeIsOpaque(compressionType) ? GrSwizzle::RGB1()
Robert Phillipsb0855272020-01-15 12:56:52 -0500565 : GrSwizzle::RGBA();
Robert Phillipsead321b2019-12-19 10:16:32 -0500566
567 return sk_sp<GrTextureProxy>(
568 new GrTextureProxy(std::move(tex), origin, texSwizzle, UseAllocator::kNo));
569}
570
Brian Salomon7578f3e2018-03-07 14:39:54 -0500571sk_sp<GrTextureProxy> GrProxyProvider::wrapRenderableBackendTexture(
Brian Salomon02bd2952018-03-07 15:20:21 -0500572 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400573 GrColorType colorType, GrWrapOwnership ownership, GrWrapCacheable cacheable,
574 ReleaseProc releaseProc, ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500575 if (this->isAbandoned()) {
576 return nullptr;
577 }
578
Brian Salomonf7778972018-03-08 10:13:17 -0500579 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500580 GrContext* direct = fImageContext->priv().asDirectContext();
581 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500582 return nullptr;
583 }
584
Robert Phillips0902c982019-07-16 07:47:56 -0400585 const GrCaps* caps = this->caps();
586
Robert Phillipsa41c6852019-02-07 10:44:10 -0500587 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
588
Greg Daniel6fa62e22019-08-07 15:52:37 -0400589 // TODO: This should have been checked and validated before getting into GrProxyProvider.
590 if (!caps->isFormatAsColorTypeRenderable(colorType, backendTex.getBackendFormat(), sampleCnt)) {
Greg Danielf87651e2018-02-21 11:36:53 -0500591 return nullptr;
592 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500593
Greg Daniel6fa62e22019-08-07 15:52:37 -0400594 sampleCnt = caps->getRenderTargetSampleCount(sampleCnt, backendTex.getBackendFormat());
595 SkASSERT(sampleCnt);
596
Robert Phillipsa41c6852019-02-07 10:44:10 -0500597 sk_sp<GrTexture> tex = resourceProvider->wrapRenderableBackendTexture(backendTex, sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400598 colorType, ownership,
599 cacheable);
Brian Salomonf7778972018-03-08 10:13:17 -0500600 if (!tex) {
601 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500602 }
603
Greg Daniel8ce79912019-02-05 10:08:43 -0500604 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500605 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500606 }
607
Brian Salomonf7778972018-03-08 10:13:17 -0500608 SkASSERT(tex->asRenderTarget()); // A GrTextureRenderTarget
609 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500610 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Greg Daniel6abda432018-02-15 14:55:00 -0500611
Greg Daniel14b57212019-12-17 16:18:06 -0500612 GrSwizzle readSwizzle = caps->getReadSwizzle(tex->backendFormat(), colorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400613
Greg Daniel14b57212019-12-17 16:18:06 -0500614 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin, readSwizzle,
Greg Danielbaf8d992019-10-29 14:14:32 -0400615 UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500616}
617
Brian Salomon7578f3e2018-03-07 14:39:54 -0500618sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400619 const GrBackendRenderTarget& backendRT, GrColorType grColorType,
620 GrSurfaceOrigin origin, ReleaseProc releaseProc, ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500621 if (this->isAbandoned()) {
622 return nullptr;
623 }
624
Brian Salomonf7778972018-03-08 10:13:17 -0500625 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500626 GrContext* direct = fImageContext->priv().asDirectContext();
627 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500628 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500629 }
630
Robert Phillips62221e72019-07-24 15:07:38 -0400631 const GrCaps* caps = this->caps();
632
Robert Phillipsa41c6852019-02-07 10:44:10 -0500633 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
634
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400635 sk_sp<GrRenderTarget> rt = resourceProvider->wrapBackendRenderTarget(backendRT, grColorType);
Brian Salomonf7778972018-03-08 10:13:17 -0500636 if (!rt) {
637 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500638 }
Greg Daniel8ce79912019-02-05 10:08:43 -0500639
Greg Daniel8ce79912019-02-05 10:08:43 -0500640 if (releaseProc) {
Brian Salomon2ca31f82019-03-05 13:28:58 -0500641 rt->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500642 }
643
Brian Salomonf7778972018-03-08 10:13:17 -0500644 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
645 SkASSERT(!rt->getUniqueKey().isValid());
646 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500647 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Brian Salomonf7778972018-03-08 10:13:17 -0500648
Greg Daniel14b57212019-12-17 16:18:06 -0500649 GrSwizzle readSwizzle = caps->getReadSwizzle(rt->backendFormat(), grColorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400650
Greg Daniel14b57212019-12-17 16:18:06 -0500651 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(std::move(rt), origin, readSwizzle,
Greg Danielbaf8d992019-10-29 14:14:32 -0400652 UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500653}
654
Brian Salomon7578f3e2018-03-07 14:39:54 -0500655sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendTextureAsRenderTarget(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400656 const GrBackendTexture& backendTex, GrColorType grColorType,
657 GrSurfaceOrigin origin, int sampleCnt) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500658 if (this->isAbandoned()) {
659 return nullptr;
660 }
661
Brian Salomonf7778972018-03-08 10:13:17 -0500662 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500663 GrContext* direct = fImageContext->priv().asDirectContext();
664 if (!direct) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500665 return nullptr;
666 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500667
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400668 const GrCaps* caps = this->caps();
669
Robert Phillipsa41c6852019-02-07 10:44:10 -0500670 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
671
Brian Salomonf7778972018-03-08 10:13:17 -0500672 sk_sp<GrRenderTarget> rt =
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400673 resourceProvider->wrapBackendTextureAsRenderTarget(backendTex, sampleCnt, grColorType);
Brian Salomonf7778972018-03-08 10:13:17 -0500674 if (!rt) {
675 return nullptr;
Greg Danielf87651e2018-02-21 11:36:53 -0500676 }
Brian Salomonf7778972018-03-08 10:13:17 -0500677 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
678 SkASSERT(!rt->getUniqueKey().isValid());
Greg Danielb46add82019-01-02 14:51:29 -0500679 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500680 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielf87651e2018-02-21 11:36:53 -0500681
Greg Daniel14b57212019-12-17 16:18:06 -0500682 GrSwizzle readSwizzle = caps->getReadSwizzle(rt->backendFormat(), grColorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400683
Greg Daniel14b57212019-12-17 16:18:06 -0500684 return sk_sp<GrSurfaceProxy>(new GrRenderTargetProxy(std::move(rt), origin, readSwizzle,
Greg Danielbaf8d992019-10-29 14:14:32 -0400685 UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500686}
687
Greg Danielb46add82019-01-02 14:51:29 -0500688sk_sp<GrRenderTargetProxy> GrProxyProvider::wrapVulkanSecondaryCBAsRenderTarget(
689 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
690 if (this->isAbandoned()) {
691 return nullptr;
692 }
693
694 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500695 GrContext* direct = fImageContext->priv().asDirectContext();
696 if (!direct) {
Greg Danielb46add82019-01-02 14:51:29 -0500697 return nullptr;
698 }
699
Robert Phillipsa41c6852019-02-07 10:44:10 -0500700 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Danielb46add82019-01-02 14:51:29 -0500701
Robert Phillipsa41c6852019-02-07 10:44:10 -0500702 sk_sp<GrRenderTarget> rt = resourceProvider->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
703 vkInfo);
Greg Danielb46add82019-01-02 14:51:29 -0500704 if (!rt) {
705 return nullptr;
706 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500707
Greg Danielb46add82019-01-02 14:51:29 -0500708 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
709 SkASSERT(!rt->getUniqueKey().isValid());
710 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500711 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielb46add82019-01-02 14:51:29 -0500712
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400713 GrColorType colorType = SkColorTypeToGrColorType(imageInfo.colorType());
Greg Daniel14b57212019-12-17 16:18:06 -0500714 GrSwizzle readSwizzle = this->caps()->getReadSwizzle(rt->backendFormat(), colorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400715
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400716 if (!this->caps()->isFormatAsColorTypeRenderable(colorType, rt->backendFormat(),
717 rt->numSamples())) {
718 return nullptr;
719 }
720
Greg Danielb46add82019-01-02 14:51:29 -0500721 // All Vulkan surfaces uses top left origins.
Brian Salomonbeb7f522019-08-30 16:19:42 -0400722 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
Greg Daniel14b57212019-12-17 16:18:06 -0500723 std::move(rt), kTopLeft_GrSurfaceOrigin, readSwizzle, UseAllocator::kNo,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400724 GrRenderTargetProxy::WrapsVkSecondaryCB::kYes));
Brian Salomone8a766b2019-07-19 14:24:36 -0400725}
726
727sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
728 const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -0500729 SkISize dimensions,
Greg Danielce3ddaa2020-01-22 16:58:15 -0500730 GrSwizzle readSwizzle,
Brian Salomone8a766b2019-07-19 14:24:36 -0400731 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400732 int renderTargetSampleCnt,
Brian Salomone8a766b2019-07-19 14:24:36 -0400733 GrSurfaceOrigin origin,
734 GrMipMapped mipMapped,
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600735 GrMipMapsStatus mipMapsStatus,
Brian Salomone8a766b2019-07-19 14:24:36 -0400736 GrInternalSurfaceFlags surfaceFlags,
737 SkBackingFit fit,
738 SkBudgeted budgeted,
739 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400740 GrSurfaceProxy::UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500741 ASSERT_SINGLE_OWNER
742 if (this->isAbandoned()) {
743 return nullptr;
744 }
Brian Salomona56a7462020-02-07 14:17:25 -0500745 SkASSERT((dimensions.fWidth <= 0 && dimensions.fHeight <= 0) ||
746 (dimensions.fWidth > 0 && dimensions.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400747
Robert Phillips0a15cc62019-07-30 12:49:10 -0400748 if (!format.isValid()) {
749 return nullptr;
750 }
751
Brian Salomona56a7462020-02-07 14:17:25 -0500752 if (dimensions.fWidth > this->caps()->maxTextureSize() ||
753 dimensions.fHeight > this->caps()->maxTextureSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400754 return nullptr;
755 }
756
Brian Salomonbeb7f522019-08-30 16:19:42 -0400757 if (renderable == GrRenderable::kYes) {
758 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(*this->caps(),
759 std::move(callback),
760 format,
Brian Salomona56a7462020-02-07 14:17:25 -0500761 dimensions,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400762 renderTargetSampleCnt,
763 origin,
764 mipMapped,
765 mipMapsStatus,
Greg Daniel14b57212019-12-17 16:18:06 -0500766 readSwizzle,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400767 fit,
768 budgeted,
769 isProtected,
770 surfaceFlags,
771 useAllocator));
772 } else {
773 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(callback),
774 format,
Brian Salomona56a7462020-02-07 14:17:25 -0500775 dimensions,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400776 origin,
777 mipMapped,
778 mipMapsStatus,
Greg Daniel14b57212019-12-17 16:18:06 -0500779 readSwizzle,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400780 fit,
781 budgeted,
782 isProtected,
783 surfaceFlags,
784 useAllocator));
785 }
Robert Phillips777707b2018-01-17 11:40:14 -0500786}
787
Robert Phillipse8fabb22018-02-04 14:33:21 -0500788sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy(
Brian Salomonbeb7f522019-08-30 16:19:42 -0400789 LazyInstantiateCallback&& callback,
790 const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -0500791 SkISize dimensions,
Greg Danielce3ddaa2020-01-22 16:58:15 -0500792 GrSwizzle readSwizzle,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400793 int sampleCnt,
794 GrSurfaceOrigin origin,
795 GrInternalSurfaceFlags surfaceFlags,
796 const TextureInfo* textureInfo,
797 GrMipMapsStatus mipMapsStatus,
798 SkBackingFit fit,
799 SkBudgeted budgeted,
800 GrProtected isProtected,
801 bool wrapsVkSecondaryCB,
802 UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500803 ASSERT_SINGLE_OWNER
804 if (this->isAbandoned()) {
805 return nullptr;
806 }
Brian Salomona56a7462020-02-07 14:17:25 -0500807 SkASSERT((dimensions.fWidth <= 0 && dimensions.fHeight <= 0) ||
808 (dimensions.fWidth > 0 && dimensions.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400809
Brian Salomona56a7462020-02-07 14:17:25 -0500810 if (dimensions.fWidth > this->caps()->maxRenderTargetSize() ||
811 dimensions.fHeight > this->caps()->maxRenderTargetSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400812 return nullptr;
813 }
814
Brian Salomon7226c232018-07-30 13:13:17 -0400815 if (textureInfo) {
Greg Danielb085fa92019-03-05 16:55:12 -0500816 // Wrapped vulkan secondary command buffers don't support texturing since we won't have an
817 // actual VkImage to texture from.
818 SkASSERT(!wrapsVkSecondaryCB);
Brian Salomon7226c232018-07-30 13:13:17 -0400819 return sk_sp<GrRenderTargetProxy>(new GrTextureRenderTargetProxy(
Brian Salomona56a7462020-02-07 14:17:25 -0500820 *this->caps(), std::move(callback), format, dimensions, sampleCnt, origin,
Greg Daniel14b57212019-12-17 16:18:06 -0500821 textureInfo->fMipMapped, mipMapsStatus, readSwizzle, fit, budgeted, isProtected,
Greg Danielbaf8d992019-10-29 14:14:32 -0400822 surfaceFlags, useAllocator));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500823 }
824
Greg Danielb085fa92019-03-05 16:55:12 -0500825 GrRenderTargetProxy::WrapsVkSecondaryCB vkSCB =
826 wrapsVkSecondaryCB ? GrRenderTargetProxy::WrapsVkSecondaryCB::kYes
827 : GrRenderTargetProxy::WrapsVkSecondaryCB::kNo;
828
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400829 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
Brian Salomona56a7462020-02-07 14:17:25 -0500830 std::move(callback), format, dimensions, sampleCnt, origin, readSwizzle, fit, budgeted,
Greg Danielbaf8d992019-10-29 14:14:32 -0400831 isProtected, surfaceFlags, useAllocator, vkSCB));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500832}
833
Brian Salomonbeb7f522019-08-30 16:19:42 -0400834sk_sp<GrTextureProxy> GrProxyProvider::MakeFullyLazyProxy(LazyInstantiateCallback&& callback,
835 const GrBackendFormat& format,
Greg Danielce3ddaa2020-01-22 16:58:15 -0500836 GrSwizzle readSwizzle,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400837 GrRenderable renderable,
838 int renderTargetSampleCnt,
839 GrProtected isProtected,
840 GrSurfaceOrigin origin,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400841 const GrCaps& caps,
842 UseAllocator useAllocator) {
Robert Phillips0a15cc62019-07-30 12:49:10 -0400843 if (!format.isValid()) {
844 return nullptr;
845 }
846
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400847 SkASSERT(renderTargetSampleCnt == 1 || renderable == GrRenderable::kYes);
Robert Phillips10d17212019-04-24 14:09:10 -0400848 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNone;
Robert Phillips777707b2018-01-17 11:40:14 -0500849
Brian Salomona56a7462020-02-07 14:17:25 -0500850 static constexpr SkISize kLazyDims = {-1, -1};
Brian Salomonbeb7f522019-08-30 16:19:42 -0400851 if (GrRenderable::kYes == renderable) {
852 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
Brian Salomona56a7462020-02-07 14:17:25 -0500853 caps, std::move(callback), format, kLazyDims, renderTargetSampleCnt, origin,
Greg Daniel14b57212019-12-17 16:18:06 -0500854 GrMipMapped::kNo, GrMipMapsStatus::kNotAllocated, readSwizzle,
855 SkBackingFit::kApprox, SkBudgeted::kYes, isProtected, surfaceFlags, useAllocator));
Brian Salomonbeb7f522019-08-30 16:19:42 -0400856 } else {
857 return sk_sp<GrTextureProxy>(new GrTextureProxy(
Brian Salomona56a7462020-02-07 14:17:25 -0500858 std::move(callback), format, kLazyDims, origin, GrMipMapped::kNo,
Greg Daniel14b57212019-12-17 16:18:06 -0500859 GrMipMapsStatus::kNotAllocated, readSwizzle, SkBackingFit::kApprox,
860 SkBudgeted::kYes, isProtected, surfaceFlags, useAllocator));
Brian Salomonbeb7f522019-08-30 16:19:42 -0400861 }
Robert Phillips777707b2018-01-17 11:40:14 -0500862}
863
Robert Phillips427966a2018-12-20 17:20:43 -0500864void GrProxyProvider::processInvalidUniqueKey(const GrUniqueKey& key, GrTextureProxy* proxy,
865 InvalidateGPUResource invalidateGPUResource) {
Chris Dalton2de13dd2019-01-03 15:11:59 -0700866 SkASSERT(key.isValid());
867
Robert Phillips427966a2018-12-20 17:20:43 -0500868 if (!proxy) {
869 proxy = fUniquelyKeyedProxies.find(key);
870 }
Chris Dalton2de13dd2019-01-03 15:11:59 -0700871 SkASSERT(!proxy || proxy->getUniqueKey() == key);
872
873 // Locate the corresponding GrGpuResource (if it needs to be invalidated) before clearing the
874 // proxy's unique key. We must do it in this order because 'key' may alias the proxy's key.
875 sk_sp<GrGpuResource> invalidGpuResource;
876 if (InvalidateGPUResource::kYes == invalidateGPUResource) {
Brian Salomon01ceae92019-04-02 11:49:54 -0400877 GrContext* direct = fImageContext->priv().asDirectContext();
878 if (direct) {
879 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
880 invalidGpuResource = resourceProvider->findByUniqueKey<GrGpuResource>(key);
Chris Dalton2de13dd2019-01-03 15:11:59 -0700881 }
882 SkASSERT(!invalidGpuResource || invalidGpuResource->getUniqueKey() == key);
883 }
Robert Phillips427966a2018-12-20 17:20:43 -0500884
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500885 // Note: this method is called for the whole variety of GrGpuResources so often 'key'
886 // will not be in 'fUniquelyKeyedProxies'.
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500887 if (proxy) {
Robert Phillips427966a2018-12-20 17:20:43 -0500888 fUniquelyKeyedProxies.remove(key);
889 proxy->cacheAccess().clearUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500890 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500891
Chris Dalton2de13dd2019-01-03 15:11:59 -0700892 if (invalidGpuResource) {
893 invalidGpuResource->resourcePriv().removeUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500894 }
895}
896
Robert Phillipsa41c6852019-02-07 10:44:10 -0500897uint32_t GrProxyProvider::contextID() const {
898 return fImageContext->priv().contextID();
899}
900
901const GrCaps* GrProxyProvider::caps() const {
902 return fImageContext->priv().caps();
903}
904
905sk_sp<const GrCaps> GrProxyProvider::refCaps() const {
906 return fImageContext->priv().refCaps();
907}
908
Robert Phillipsa9162df2019-02-11 14:12:03 -0500909bool GrProxyProvider::isAbandoned() const {
910 return fImageContext->priv().abandoned();
911}
912
Robert Phillips0790f8a2018-09-18 13:11:03 -0400913void GrProxyProvider::orphanAllUniqueKeys() {
914 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
915 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
916 GrTextureProxy& tmp = *iter;
917
918 tmp.fProxyProvider = nullptr;
919 }
920}
921
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500922void GrProxyProvider::removeAllUniqueKeys() {
923 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
924 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
925 GrTextureProxy& tmp = *iter;
926
Chris Dalton2de13dd2019-01-03 15:11:59 -0700927 this->processInvalidUniqueKey(tmp.getUniqueKey(), &tmp, InvalidateGPUResource::kNo);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500928 }
929 SkASSERT(!fUniquelyKeyedProxies.count());
930}
Robert Phillipsa41c6852019-02-07 10:44:10 -0500931
932bool GrProxyProvider::renderingDirectly() const {
933 return fImageContext->priv().asDirectContext();
934}