blob: c7d4de9005495fef42d49d681bc9f6ab0408571c [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"
13#include "include/gpu/GrRenderTarget.h"
14#include "include/gpu/GrTexture.h"
15#include "include/private/GrImageContext.h"
16#include "include/private/GrResourceKey.h"
17#include "include/private/GrSingleOwner.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "include/private/SkImageInfoPriv.h"
19#include "src/core/SkAutoPixmapStorage.h"
20#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"
26#include "src/gpu/GrResourceProvider.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040027#include "src/gpu/GrSurfaceProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/gpu/GrSurfaceProxyPriv.h"
29#include "src/gpu/GrTextureProxyCacheAccess.h"
30#include "src/gpu/GrTextureRenderTargetProxy.h"
31#include "src/gpu/SkGr.h"
32#include "src/image/SkImage_Base.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050033
34#define ASSERT_SINGLE_OWNER \
Robert Phillipsa41c6852019-02-07 10:44:10 -050035 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fImageContext->priv().singleOwner());)
Robert Phillips1afd4cd2018-01-08 13:40:32 -050036
Robert Phillipsa9162df2019-02-11 14:12:03 -050037GrProxyProvider::GrProxyProvider(GrImageContext* imageContext) : fImageContext(imageContext) {}
Robert Phillips1afd4cd2018-01-08 13:40:32 -050038
39GrProxyProvider::~GrProxyProvider() {
Robert Phillipsa41c6852019-02-07 10:44:10 -050040 if (this->renderingDirectly()) {
Robert Phillips0790f8a2018-09-18 13:11:03 -040041 // In DDL-mode a proxy provider can still have extant uniquely keyed proxies (since
42 // they need their unique keys to, potentially, find a cached resource when the
43 // DDL is played) but, in non-DDL-mode they should all have been cleaned up by this point.
44 SkASSERT(!fUniquelyKeyedProxies.count());
45 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050046}
47
Robert Phillipsadbe1322018-01-17 13:35:46 -050048bool GrProxyProvider::assignUniqueKeyToProxy(const GrUniqueKey& key, GrTextureProxy* proxy) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050049 ASSERT_SINGLE_OWNER
50 SkASSERT(key.isValid());
51 if (this->isAbandoned() || !proxy) {
Robert Phillipsadbe1322018-01-17 13:35:46 -050052 return false;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050053 }
54
Robert Phillipsa41c6852019-02-07 10:44:10 -050055#ifdef SK_DEBUG
56 {
57 GrContext* direct = fImageContext->priv().asDirectContext();
58 if (direct) {
59 GrResourceCache* resourceCache = direct->priv().getResourceCache();
60 // If there is already a GrResource with this key then the caller has violated the
61 // normal usage pattern of uniquely keyed resources (e.g., they have created one w/o
62 // first seeing if it already existed in the cache).
63 SkASSERT(!resourceCache->findAndRefUniqueResource(key));
64 }
65 }
66#endif
Robert Phillips1afd4cd2018-01-08 13:40:32 -050067
Robert Phillips1afd4cd2018-01-08 13:40:32 -050068 SkASSERT(!fUniquelyKeyedProxies.find(key)); // multiple proxies can't get the same key
69
70 proxy->cacheAccess().setUniqueKey(this, key);
71 SkASSERT(proxy->getUniqueKey() == key);
72 fUniquelyKeyedProxies.add(proxy);
Robert Phillipsadbe1322018-01-17 13:35:46 -050073 return true;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050074}
75
76void GrProxyProvider::adoptUniqueKeyFromSurface(GrTextureProxy* proxy, const GrSurface* surf) {
77 SkASSERT(surf->getUniqueKey().isValid());
78 proxy->cacheAccess().setUniqueKey(this, surf->getUniqueKey());
79 SkASSERT(proxy->getUniqueKey() == surf->getUniqueKey());
80 // multiple proxies can't get the same key
81 SkASSERT(!fUniquelyKeyedProxies.find(surf->getUniqueKey()));
82 fUniquelyKeyedProxies.add(proxy);
83}
84
Chris Dalton2de13dd2019-01-03 15:11:59 -070085void GrProxyProvider::removeUniqueKeyFromProxy(GrTextureProxy* proxy) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050086 ASSERT_SINGLE_OWNER
Chris Dalton2de13dd2019-01-03 15:11:59 -070087 SkASSERT(proxy);
88 SkASSERT(proxy->getUniqueKey().isValid());
89
90 if (this->isAbandoned()) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050091 return;
92 }
Robert Phillips0790f8a2018-09-18 13:11:03 -040093
Chris Dalton2de13dd2019-01-03 15:11:59 -070094 this->processInvalidUniqueKey(proxy->getUniqueKey(), proxy, InvalidateGPUResource::kYes);
Robert Phillips1afd4cd2018-01-08 13:40:32 -050095}
96
97sk_sp<GrTextureProxy> GrProxyProvider::findProxyByUniqueKey(const GrUniqueKey& key,
98 GrSurfaceOrigin origin) {
99 ASSERT_SINGLE_OWNER
100
101 if (this->isAbandoned()) {
102 return nullptr;
103 }
104
Brian Salomon01ceae92019-04-02 11:49:54 -0400105 GrTextureProxy* proxy = fUniquelyKeyedProxies.find(key);
Brian Salomon01ceae92019-04-02 11:49:54 -0400106 if (proxy) {
Robert Phillipse5f73282019-06-18 17:15:04 -0400107 SkASSERT(proxy->getProxyRefCnt() >= 1);
108 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(
118 const GrSurfaceDesc& desc, GrSurfaceOrigin origin, SkBackingFit fit, SkBudgeted budgeted) {
119 GrContext* direct = fImageContext->priv().asDirectContext();
120 if (!direct) {
121 return nullptr;
122 }
123
124 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
125 sk_sp<GrTexture> tex;
126
127 if (SkBackingFit::kApprox == fit) {
Robert Phillips9313aa72019-04-09 18:41:27 -0400128 tex = resourceProvider->createApproxTexture(desc, GrResourceProvider::Flags::kNoPendingIO);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500129 } else {
Robert Phillips9313aa72019-04-09 18:41:27 -0400130 tex = resourceProvider->createTexture(desc, budgeted,
131 GrResourceProvider::Flags::kNoPendingIO);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500132 }
133 if (!tex) {
134 return nullptr;
135 }
136
137 return this->createWrapped(std::move(tex), origin);
138}
139
140sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createWrapped(sk_sp<GrTexture> tex,
141 GrSurfaceOrigin origin) {
142 return this->createWrapped(std::move(tex), origin);
143}
144#endif
145
Robert Phillipsadbe1322018-01-17 13:35:46 -0500146sk_sp<GrTextureProxy> GrProxyProvider::createWrapped(sk_sp<GrTexture> tex, GrSurfaceOrigin origin) {
147#ifdef SK_DEBUG
148 if (tex->getUniqueKey().isValid()) {
149 SkASSERT(!this->findProxyByUniqueKey(tex->getUniqueKey(), origin));
150 }
151#endif
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400152 GrColorType colorType = GrPixelConfigToColorType(tex->config());
153 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(tex->backendFormat(), colorType);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500154
155 if (tex->asRenderTarget()) {
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400156 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(tex->backendFormat(), colorType);
157 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin,
158 texSwizzle, outSwizzle));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500159 } else {
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400160 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin, texSwizzle));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500161 }
162}
163
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500164sk_sp<GrTextureProxy> GrProxyProvider::findOrCreateProxyByUniqueKey(const GrUniqueKey& key,
165 GrSurfaceOrigin origin) {
166 ASSERT_SINGLE_OWNER
167
168 if (this->isAbandoned()) {
169 return nullptr;
170 }
171
172 sk_sp<GrTextureProxy> result = this->findProxyByUniqueKey(key, origin);
173 if (result) {
174 return result;
175 }
176
Robert Phillipsa41c6852019-02-07 10:44:10 -0500177 GrContext* direct = fImageContext->priv().asDirectContext();
178 if (!direct) {
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500179 return nullptr;
180 }
181
Robert Phillipsa41c6852019-02-07 10:44:10 -0500182 GrResourceCache* resourceCache = direct->priv().getResourceCache();
183
184 GrGpuResource* resource = resourceCache->findAndRefUniqueResource(key);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500185 if (!resource) {
186 return nullptr;
187 }
188
189 sk_sp<GrTexture> texture(static_cast<GrSurface*>(resource)->asTexture());
190 SkASSERT(texture);
191
Robert Phillipsadbe1322018-01-17 13:35:46 -0500192 result = this->createWrapped(std::move(texture), origin);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500193 SkASSERT(result->getUniqueKey() == key);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500194 // createWrapped should've added this for us
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500195 SkASSERT(fUniquelyKeyedProxies.find(key));
196 return result;
197}
198
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500199sk_sp<GrTextureProxy> GrProxyProvider::createTextureProxy(sk_sp<SkImage> srcImage,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400200 GrSurfaceDescFlags descFlags,
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500201 int sampleCnt,
Greg Danielfb3abcd2018-02-02 15:48:33 -0500202 SkBudgeted budgeted,
Chris Daltond004e0b2018-09-27 09:28:03 -0600203 SkBackingFit fit,
204 GrInternalSurfaceFlags surfaceFlags) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500205 ASSERT_SINGLE_OWNER
206 SkASSERT(srcImage);
207
208 if (this->isAbandoned()) {
209 return nullptr;
210 }
211
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400212 const SkImageInfo& info = srcImage->imageInfo();
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400213 SkColorType ct = info.colorType();
Greg Danielf87651e2018-02-21 11:36:53 -0500214
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400215 GrBackendFormat format = this->caps()->getBackendFormatFromColorType(ct);
216 if (!format.isValid()) {
Greg Daniel0a7aa142018-02-21 13:02:32 -0500217 return nullptr;
218 }
219
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400220 if (!this->caps()->isFormatTexturable(ct, format)) {
Brian Osmand29dcd12018-09-13 15:04:29 -0400221 SkBitmap copy8888;
222 if (!copy8888.tryAllocPixels(info.makeColorType(kRGBA_8888_SkColorType)) ||
223 !srcImage->readPixels(copy8888.pixmap(), 0, 0)) {
224 return nullptr;
225 }
226 copy8888.setImmutable();
227 srcImage = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode);
Jim Van Verthc8098322019-04-16 10:50:01 -0400228 ct = kRGBA_8888_SkColorType;
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400229 format = this->caps()->getBackendFormatFromColorType(ct);
230 if (!format.isValid()) {
231 return nullptr;
232 }
Brian Osmand29dcd12018-09-13 15:04:29 -0400233 }
234
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400235 if (SkToBool(descFlags & kRenderTarget_GrSurfaceFlag)) {
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400236 sampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, ct, format);
Greg Danielf87651e2018-02-21 11:36:53 -0500237 if (!sampleCnt) {
238 return nullptr;
239 }
240 }
241
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400242 GrPixelConfig config = SkColorType2GrPixelConfig(ct);
243 if (kUnknown_GrPixelConfig == config) {
244 return nullptr;
245 }
246
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500247 GrSurfaceDesc desc;
248 desc.fWidth = srcImage->width();
249 desc.fHeight = srcImage->height();
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400250 desc.fFlags = descFlags;
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500251 desc.fSampleCnt = sampleCnt;
Greg Danielf87651e2018-02-21 11:36:53 -0500252 desc.fConfig = config;
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500253
254 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Robert Phillips10d17212019-04-24 14:09:10 -0400255 [desc, budgeted, srcImage, fit](GrResourceProvider* resourceProvider) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500256 SkPixmap pixMap;
257 SkAssertResult(srcImage->peekPixels(&pixMap));
258 GrMipLevel mipLevel = { pixMap.addr(), pixMap.rowBytes() };
259
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400260 return LazyInstantiationResult(resourceProvider->createTexture(
Robert Phillips10d17212019-04-24 14:09:10 -0400261 desc, budgeted, fit, mipLevel, GrResourceProvider::Flags::kNoPendingIO));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500262 },
Greg Daniel4065d452018-11-16 15:43:41 -0500263 format, desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, surfaceFlags, fit, budgeted);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500264
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400265 if (!proxy) {
266 return nullptr;
267 }
268
Robert Phillipsa41c6852019-02-07 10:44:10 -0500269 GrContext* direct = fImageContext->priv().asDirectContext();
270 if (direct) {
271 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
272
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500273 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
274 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500275 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500276 return nullptr;
277 }
278 }
Robert Phillipsc1b60662018-06-26 10:20:08 -0400279
280 SkASSERT(proxy->width() == desc.fWidth);
281 SkASSERT(proxy->height() == desc.fHeight);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500282 return proxy;
283}
284
Greg Daniel4065d452018-11-16 15:43:41 -0500285sk_sp<GrTextureProxy> GrProxyProvider::createMipMapProxy(const GrBackendFormat& format,
286 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500287 GrSurfaceOrigin origin,
Greg Daniel30815082018-02-09 16:08:30 -0500288 SkBudgeted budgeted) {
Robert Phillips579f0942018-01-08 14:53:35 -0500289 ASSERT_SINGLE_OWNER
290
291 if (this->isAbandoned()) {
292 return nullptr;
293 }
294
Greg Daniel4065d452018-11-16 15:43:41 -0500295 return this->createProxy(format, desc, origin, GrMipMapped::kYes, SkBackingFit::kExact,
296 budgeted, GrInternalSurfaceFlags::kNone);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500297}
298
Brian Osmande496652019-03-22 13:42:33 -0400299sk_sp<GrTextureProxy> GrProxyProvider::createProxyFromBitmap(const SkBitmap& bitmap,
300 GrMipMapped mipMapped) {
Brian Osman412674f2019-02-07 15:34:58 -0500301 ASSERT_SINGLE_OWNER
Brian Osman7dcc6162019-03-25 10:12:57 -0400302 SkASSERT(GrMipMapped::kNo == mipMapped || this->caps()->mipMapSupport());
Brian Osman412674f2019-02-07 15:34:58 -0500303
304 if (this->isAbandoned()) {
305 return nullptr;
306 }
307
Brian Osman2b23c4b2018-06-01 12:25:08 -0400308 if (!SkImageInfoIsValid(bitmap.info())) {
Greg Daniela4ead652018-02-07 10:21:48 -0500309 return nullptr;
310 }
311
Brian Osmande496652019-03-22 13:42:33 -0400312 ATRACE_ANDROID_FRAMEWORK("Upload %sTexture [%ux%u]",
313 GrMipMapped::kYes == mipMapped ? "MipMap " : "",
314 bitmap.width(), bitmap.height());
Greg Daniela4ead652018-02-07 10:21:48 -0500315
316 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
317 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
318 // upload of the data to the gpu can happen at anytime and the bitmap may change by then.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500319 SkCopyPixelsMode copyMode = this->renderingDirectly() ? kNever_SkCopyPixelsMode
320 : kIfMutable_SkCopyPixelsMode;
Greg Daniela4ead652018-02-07 10:21:48 -0500321 sk_sp<SkImage> baseLevel = SkMakeImageFromRasterBitmap(bitmap, copyMode);
Greg Daniela4ead652018-02-07 10:21:48 -0500322 if (!baseLevel) {
323 return nullptr;
324 }
325
Brian Osmande496652019-03-22 13:42:33 -0400326 // If mips weren't requested (or this was too small to have any), then take the fast path
327 if (GrMipMapped::kNo == mipMapped ||
328 0 == SkMipMap::ComputeLevelCount(baseLevel->width(), baseLevel->height())) {
Brian Osman7dcc6162019-03-25 10:12:57 -0400329 return this->createTextureProxy(std::move(baseLevel), kNone_GrSurfaceFlags, 1,
330 SkBudgeted::kYes, SkBackingFit::kExact);
Greg Daniela4ead652018-02-07 10:21:48 -0500331 }
332
Greg Daniel3fc5df42019-06-14 09:42:17 -0400333 SkColorType colorType = bitmap.info().colorType();
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400334 GrBackendFormat format = this->caps()->getBackendFormatFromColorType(colorType);
335 if (!format.isValid()) {
336 return nullptr;
337 }
Brian Osmand29dcd12018-09-13 15:04:29 -0400338 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info());
Greg Daniel3fc5df42019-06-14 09:42:17 -0400339
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400340 if (!this->caps()->isFormatTexturable(colorType, format)) {
Brian Osmand29dcd12018-09-13 15:04:29 -0400341 SkBitmap copy8888;
342 if (!copy8888.tryAllocPixels(bitmap.info().makeColorType(kRGBA_8888_SkColorType)) ||
343 !bitmap.readPixels(copy8888.pixmap())) {
344 return nullptr;
345 }
346 copy8888.setImmutable();
347 baseLevel = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode);
348 desc.fConfig = kRGBA_8888_GrPixelConfig;
Greg Daniel3fc5df42019-06-14 09:42:17 -0400349 colorType = kRGBA_8888_SkColorType;
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400350 format = this->caps()->getBackendFormatFromColorType(colorType);
351 if (!format.isValid()) {
352 return nullptr;
353 }
Greg Daniel3fc5df42019-06-14 09:42:17 -0400354 }
355
Brian Osmand29dcd12018-09-13 15:04:29 -0400356
357 SkPixmap pixmap;
358 SkAssertResult(baseLevel->peekPixels(&pixmap));
359 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(pixmap, nullptr));
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400360 if (!mipmaps) {
361 return nullptr;
362 }
363
Greg Daniela4ead652018-02-07 10:21:48 -0500364 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Osman1b97f132018-09-13 17:33:48 +0000365 [desc, baseLevel, mipmaps](GrResourceProvider* resourceProvider) {
Brian Osman1b97f132018-09-13 17:33:48 +0000366 const int mipLevelCount = mipmaps->countLevels() + 1;
367 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
368
Greg Daniela4ead652018-02-07 10:21:48 -0500369 SkPixmap pixmap;
370 SkAssertResult(baseLevel->peekPixels(&pixmap));
371
372 // DDL TODO: Instead of copying all this info into GrMipLevels we should just plumb
373 // the use of SkMipMap down through Ganesh.
374 texels[0].fPixels = pixmap.addr();
375 texels[0].fRowBytes = pixmap.rowBytes();
376
377 for (int i = 1; i < mipLevelCount; ++i) {
378 SkMipMap::Level generatedMipLevel;
379 mipmaps->getLevel(i - 1, &generatedMipLevel);
380 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
381 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
382 SkASSERT(texels[i].fPixels);
383 }
384
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400385 return LazyInstantiationResult(resourceProvider->createTexture(
386 desc, SkBudgeted::kYes, texels.get(), mipLevelCount));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500387 },
Greg Daniel4065d452018-11-16 15:43:41 -0500388 format, desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kYes, SkBackingFit::kExact,
389 SkBudgeted::kYes);
Greg Daniela4ead652018-02-07 10:21:48 -0500390
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400391 if (!proxy) {
392 return nullptr;
393 }
394
Robert Phillipsa41c6852019-02-07 10:44:10 -0500395 GrContext* direct = fImageContext->priv().asDirectContext();
396 if (direct) {
397 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Daniela4ead652018-02-07 10:21:48 -0500398 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
399 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500400 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Greg Daniela4ead652018-02-07 10:21:48 -0500401 return nullptr;
402 }
403 }
404 return proxy;
405}
406
Greg Danielfa55f2e2019-06-13 15:21:38 -0400407#ifdef SK_DEBUG
408static bool validate_backend_format_and_config(const GrCaps* caps,
409 const GrBackendFormat& format,
410 GrPixelConfig config) {
411 if (kUnknown_GrPixelConfig == config) {
412 return false;
413 }
Brian Salomonbb8dde82019-06-27 10:52:13 -0400414 if (GrPixelConfigIsCompressed(config)) {
415 // We have no way to verify these at the moment.
416 return true;
417 }
Greg Danielfa55f2e2019-06-13 15:21:38 -0400418 SkColorType colorType = GrColorTypeToSkColorType(GrPixelConfigToColorType(config));
419 if (colorType == kUnknown_SkColorType) {
420 // We should add support for validating GrColorType with a GrBackendFormat. Currently we
421 // only support SkColorType. For now we just assume things that don't have a corresponding
422 // SkColorType are correct.
423 return true;
424 }
Robert Phillipsc046ff02019-07-01 10:34:03 -0400425
426 return caps->areColorTypeAndFormatCompatible(colorType, format);
Greg Danielfa55f2e2019-06-13 15:21:38 -0400427}
428#endif
429
Greg Daniel4065d452018-11-16 15:43:41 -0500430sk_sp<GrTextureProxy> GrProxyProvider::createProxy(const GrBackendFormat& format,
431 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500432 GrSurfaceOrigin origin,
Greg Danielf6f7b672018-02-15 13:06:26 -0500433 GrMipMapped mipMapped,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500434 SkBackingFit fit,
435 SkBudgeted budgeted,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400436 GrInternalSurfaceFlags surfaceFlags) {
Greg Danielfa55f2e2019-06-13 15:21:38 -0400437 SkASSERT(validate_backend_format_and_config(this->caps(), format, desc.fConfig));
Brian Salomonbb8dde82019-06-27 10:52:13 -0400438 if (GrPixelConfigIsCompressed(desc.fConfig)) {
439 // Deferred proxies for compressed textures are not supported.
440 return nullptr;
441 }
Greg Danielf6f7b672018-02-15 13:06:26 -0500442 if (GrMipMapped::kYes == mipMapped) {
443 // SkMipMap doesn't include the base level in the level count so we have to add 1
444 int mipCount = SkMipMap::ComputeLevelCount(desc.fWidth, desc.fHeight) + 1;
445 if (1 == mipCount) {
446 mipMapped = GrMipMapped::kNo;
447 }
448 }
449
450 if (!this->caps()->validateSurfaceDesc(desc, mipMapped)) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500451 return nullptr;
452 }
Brian Salomon3a2cc2c2018-02-03 00:25:12 +0000453 GrSurfaceDesc copyDesc = desc;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500454 if (desc.fFlags & kRenderTarget_GrSurfaceFlag) {
455 copyDesc.fSampleCnt =
456 this->caps()->getRenderTargetSampleCount(desc.fSampleCnt, desc.fConfig);
457 }
Brian Salomon3a2cc2c2018-02-03 00:25:12 +0000458
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400459 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
460 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(format, colorType);
461
Brian Salomonbdecacf2018-02-02 20:32:49 -0500462 if (copyDesc.fFlags & kRenderTarget_GrSurfaceFlag) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500463 // We know anything we instantiate later from this deferred path will be
464 // both texturable and renderable
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400465 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(format, colorType);
Greg Daniel4065d452018-11-16 15:43:41 -0500466 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(*this->caps(), format, copyDesc,
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400467 origin, mipMapped, texSwizzle,
468 outSwizzle, fit, budgeted,
469 surfaceFlags));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500470 }
471
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400472 return sk_sp<GrTextureProxy>(new GrTextureProxy(format, copyDesc, origin, mipMapped, texSwizzle,
Brian Salomon7226c232018-07-30 13:13:17 -0400473 fit, budgeted, surfaceFlags));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500474}
475
Brian Salomonbb8dde82019-06-27 10:52:13 -0400476sk_sp<GrTextureProxy> GrProxyProvider::createCompressedTextureProxy(
477 int width, int height, SkBudgeted budgeted, SkImage::CompressionType compressionType,
478 sk_sp<SkData> data) {
479 GrBackendFormat format = this->caps()->getBackendFormatFromCompressionType(compressionType);
480
481 GrSurfaceDesc desc;
482 desc.fConfig = GrCompressionTypePixelConfig(compressionType);
483 desc.fWidth = width;
484 desc.fHeight = height;
485
Jim Van Verthee06b332019-01-18 10:36:32 -0500486 if (!this->caps()->isConfigTexturable(desc.fConfig)) {
487 return nullptr;
488 }
489
Jim Van Verthee06b332019-01-18 10:36:32 -0500490 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomonbb8dde82019-06-27 10:52:13 -0400491 [width, height, compressionType, budgeted, data](GrResourceProvider* resourceProvider) {
492 return LazyInstantiationResult(resourceProvider->createCompressedTexture(
493 width, height, compressionType, budgeted, data.get()));
494 },
495 format, desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, SkBackingFit::kExact,
496 SkBudgeted::kYes);
Jim Van Verthee06b332019-01-18 10:36:32 -0500497
498 if (!proxy) {
499 return nullptr;
500 }
501
Robert Phillipsa41c6852019-02-07 10:44:10 -0500502 GrContext* direct = fImageContext->priv().asDirectContext();
503 if (direct) {
504 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Jim Van Verthee06b332019-01-18 10:36:32 -0500505 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
506 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500507 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500508 return nullptr;
509 }
510 }
511 return proxy;
512}
513
Brian Salomon7578f3e2018-03-07 14:39:54 -0500514sk_sp<GrTextureProxy> GrProxyProvider::wrapBackendTexture(const GrBackendTexture& backendTex,
515 GrSurfaceOrigin origin,
516 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500517 GrWrapCacheable cacheable,
Brian Salomonc67c31c2018-12-06 10:00:03 -0500518 GrIOType ioType,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500519 ReleaseProc releaseProc,
520 ReleaseContext releaseCtx) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500521 SkASSERT(ioType != kWrite_GrIOType);
Robert Phillipsf9bec202018-01-16 09:21:01 -0500522 if (this->isAbandoned()) {
523 return nullptr;
524 }
525
Brian Salomonf7778972018-03-08 10:13:17 -0500526 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500527 GrContext* direct = fImageContext->priv().asDirectContext();
528 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500529 return nullptr;
530 }
531
Greg Danielfa55f2e2019-06-13 15:21:38 -0400532#ifdef SK_DEBUG
533 SkASSERT(validate_backend_format_and_config(this->caps(), backendTex.getBackendFormat(),
534 backendTex.config()));
535#endif
536
Robert Phillipsa41c6852019-02-07 10:44:10 -0500537 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
538
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500539 sk_sp<GrTexture> tex =
Robert Phillipsa41c6852019-02-07 10:44:10 -0500540 resourceProvider->wrapBackendTexture(backendTex, ownership, cacheable, ioType);
Brian Salomonf7778972018-03-08 10:13:17 -0500541 if (!tex) {
542 return nullptr;
543 }
Robert Phillipsadbe1322018-01-17 13:35:46 -0500544
Greg Daniel6a0176b2018-01-30 09:28:44 -0500545 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500546 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel6a0176b2018-01-30 09:28:44 -0500547 }
548
Brian Salomonf7778972018-03-08 10:13:17 -0500549 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
550 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500551 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500552
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400553 GrColorType colorType = GrPixelConfigToColorType(tex->config());
554 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(tex->backendFormat(), colorType);
555
556 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin, texSwizzle));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500557}
558
Brian Salomon7578f3e2018-03-07 14:39:54 -0500559sk_sp<GrTextureProxy> GrProxyProvider::wrapRenderableBackendTexture(
Brian Salomon02bd2952018-03-07 15:20:21 -0500560 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt,
Greg Daniel8ce79912019-02-05 10:08:43 -0500561 GrWrapOwnership ownership, GrWrapCacheable cacheable, ReleaseProc releaseProc,
562 ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500563 if (this->isAbandoned()) {
564 return nullptr;
565 }
566
Brian Salomonf7778972018-03-08 10:13:17 -0500567 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500568 GrContext* direct = fImageContext->priv().asDirectContext();
569 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500570 return nullptr;
571 }
572
Greg Danielfa55f2e2019-06-13 15:21:38 -0400573 SkASSERT(validate_backend_format_and_config(this->caps(), backendTex.getBackendFormat(),
574 backendTex.config()));
575
Robert Phillipsa41c6852019-02-07 10:44:10 -0500576 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
577
Greg Daniel6abda432018-02-15 14:55:00 -0500578 sampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config());
Greg Danielf87651e2018-02-21 11:36:53 -0500579 if (!sampleCnt) {
580 return nullptr;
581 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500582
Robert Phillipsa41c6852019-02-07 10:44:10 -0500583 sk_sp<GrTexture> tex = resourceProvider->wrapRenderableBackendTexture(backendTex, sampleCnt,
584 ownership, cacheable);
Brian Salomonf7778972018-03-08 10:13:17 -0500585 if (!tex) {
586 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500587 }
588
Greg Daniel8ce79912019-02-05 10:08:43 -0500589 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500590 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500591 }
592
Brian Salomonf7778972018-03-08 10:13:17 -0500593 SkASSERT(tex->asRenderTarget()); // A GrTextureRenderTarget
594 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500595 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Greg Daniel6abda432018-02-15 14:55:00 -0500596
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400597 GrColorType colorType = GrPixelConfigToColorType(tex->config());
598 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(tex->backendFormat(), colorType);
599 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(tex->backendFormat(), colorType);
600
601 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin, texSwizzle,
602 outSwizzle));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500603}
604
Brian Salomon7578f3e2018-03-07 14:39:54 -0500605sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget(
Greg Daniel8ce79912019-02-05 10:08:43 -0500606 const GrBackendRenderTarget& backendRT, GrSurfaceOrigin origin, ReleaseProc releaseProc,
607 ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500608 if (this->isAbandoned()) {
609 return nullptr;
610 }
611
Brian Salomonf7778972018-03-08 10:13:17 -0500612 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500613 GrContext* direct = fImageContext->priv().asDirectContext();
614 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500615 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500616 }
617
Brian Salomon3ec1f542019-06-17 17:54:57 +0000618 GrColorType colorType = GrPixelConfigToColorType(backendRT.config());
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400619#ifdef SK_DEBUG
Greg Danielfa55f2e2019-06-13 15:21:38 -0400620 GrPixelConfig testConfig =
621 this->caps()->validateBackendRenderTarget(backendRT,
622 GrColorTypeToSkColorType(colorType));
623 SkASSERT(testConfig != kUnknown_GrPixelConfig);
624#endif
625
Robert Phillipsa41c6852019-02-07 10:44:10 -0500626 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
627
628 sk_sp<GrRenderTarget> rt = resourceProvider->wrapBackendRenderTarget(backendRT);
Brian Salomonf7778972018-03-08 10:13:17 -0500629 if (!rt) {
630 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500631 }
Greg Daniel8ce79912019-02-05 10:08:43 -0500632
Greg Daniel8ce79912019-02-05 10:08:43 -0500633 if (releaseProc) {
Brian Salomon2ca31f82019-03-05 13:28:58 -0500634 rt->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500635 }
636
Brian Salomonf7778972018-03-08 10:13:17 -0500637 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
638 SkASSERT(!rt->getUniqueKey().isValid());
639 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500640 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Brian Salomonf7778972018-03-08 10:13:17 -0500641
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400642 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(rt->backendFormat(), colorType);
643 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(rt->backendFormat(), colorType);
644
645 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(std::move(rt), origin, texSwizzle,
646 outSwizzle));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500647}
648
Brian Salomon7578f3e2018-03-07 14:39:54 -0500649sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendTextureAsRenderTarget(
650 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500651 if (this->isAbandoned()) {
652 return nullptr;
653 }
654
Brian Salomonf7778972018-03-08 10:13:17 -0500655 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500656 GrContext* direct = fImageContext->priv().asDirectContext();
657 if (!direct) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500658 return nullptr;
659 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500660
Greg Danielfa55f2e2019-06-13 15:21:38 -0400661 SkASSERT(validate_backend_format_and_config(this->caps(), backendTex.getBackendFormat(),
662 backendTex.config()));
663
Robert Phillipsa41c6852019-02-07 10:44:10 -0500664 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
665
Brian Salomonf7778972018-03-08 10:13:17 -0500666 sk_sp<GrRenderTarget> rt =
Robert Phillipsa41c6852019-02-07 10:44:10 -0500667 resourceProvider->wrapBackendTextureAsRenderTarget(backendTex, sampleCnt);
Brian Salomonf7778972018-03-08 10:13:17 -0500668 if (!rt) {
669 return nullptr;
Greg Danielf87651e2018-02-21 11:36:53 -0500670 }
Brian Salomonf7778972018-03-08 10:13:17 -0500671 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
672 SkASSERT(!rt->getUniqueKey().isValid());
Greg Danielb46add82019-01-02 14:51:29 -0500673 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500674 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielf87651e2018-02-21 11:36:53 -0500675
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400676 GrColorType colorType = GrPixelConfigToColorType(rt->config());
677 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(rt->backendFormat(), colorType);
678 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(rt->backendFormat(), colorType);
679
680 return sk_sp<GrSurfaceProxy>(new GrRenderTargetProxy(std::move(rt), origin, texSwizzle,
681 outSwizzle));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500682}
683
Greg Danielb46add82019-01-02 14:51:29 -0500684sk_sp<GrRenderTargetProxy> GrProxyProvider::wrapVulkanSecondaryCBAsRenderTarget(
685 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
686 if (this->isAbandoned()) {
687 return nullptr;
688 }
689
690 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500691 GrContext* direct = fImageContext->priv().asDirectContext();
692 if (!direct) {
Greg Danielb46add82019-01-02 14:51:29 -0500693 return nullptr;
694 }
695
Robert Phillipsa41c6852019-02-07 10:44:10 -0500696 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Danielb46add82019-01-02 14:51:29 -0500697
Robert Phillipsa41c6852019-02-07 10:44:10 -0500698 sk_sp<GrRenderTarget> rt = resourceProvider->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
699 vkInfo);
Greg Danielb46add82019-01-02 14:51:29 -0500700 if (!rt) {
701 return nullptr;
702 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500703
Greg Danielb46add82019-01-02 14:51:29 -0500704 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
705 SkASSERT(!rt->getUniqueKey().isValid());
706 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500707 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielb46add82019-01-02 14:51:29 -0500708
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400709 GrColorType colorType = GrPixelConfigToColorType(rt->config());
710 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(rt->backendFormat(), colorType);
711 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(rt->backendFormat(), colorType);
712
Greg Danielb46add82019-01-02 14:51:29 -0500713 // All Vulkan surfaces uses top left origins.
714 return sk_sp<GrRenderTargetProxy>(
715 new GrRenderTargetProxy(std::move(rt),
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400716 kTopLeft_GrSurfaceOrigin, texSwizzle, outSwizzle,
Greg Danielb46add82019-01-02 14:51:29 -0500717 GrRenderTargetProxy::WrapsVkSecondaryCB::kYes));
718}
719
Robert Phillips777707b2018-01-17 11:40:14 -0500720sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500721 const GrBackendFormat& format,
Robert Phillips777707b2018-01-17 11:40:14 -0500722 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500723 GrSurfaceOrigin origin,
Brian Salomon7226c232018-07-30 13:13:17 -0400724 GrMipMapped mipMapped,
Brian Salomon7226c232018-07-30 13:13:17 -0400725 SkBackingFit fit,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500726 SkBudgeted budgeted) {
Greg Daniel4065d452018-11-16 15:43:41 -0500727 return this->createLazyProxy(std::move(callback), format, desc, origin, mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400728 GrInternalSurfaceFlags::kNone, fit, budgeted);
Greg Daniel2a303902018-02-20 10:25:54 -0500729}
730
731sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500732 const GrBackendFormat& format,
Greg Daniel2a303902018-02-20 10:25:54 -0500733 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500734 GrSurfaceOrigin origin,
Greg Daniel2a303902018-02-20 10:25:54 -0500735 GrMipMapped mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400736 GrInternalSurfaceFlags surfaceFlags,
Brian Salomon7226c232018-07-30 13:13:17 -0400737 SkBackingFit fit,
738 SkBudgeted budgeted) {
Greg Daniela8d92112018-03-09 12:05:04 -0500739 // For non-ddl draws always make lazy proxy's single use.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500740 LazyInstantiationType lazyType = this->renderingDirectly() ? LazyInstantiationType::kSingleUse
741 : LazyInstantiationType::kMultipleUse;
Greg Daniel4065d452018-11-16 15:43:41 -0500742 return this->createLazyProxy(std::move(callback), format, desc, origin, mipMapped, surfaceFlags,
743 fit, budgeted, lazyType);
Greg Daniela8d92112018-03-09 12:05:04 -0500744}
745
746sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500747 const GrBackendFormat& format,
Greg Daniela8d92112018-03-09 12:05:04 -0500748 const GrSurfaceDesc& desc,
749 GrSurfaceOrigin origin,
750 GrMipMapped mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400751 GrInternalSurfaceFlags surfaceFlags,
Brian Salomon7226c232018-07-30 13:13:17 -0400752 SkBackingFit fit,
753 SkBudgeted budgeted,
Greg Daniela8d92112018-03-09 12:05:04 -0500754 LazyInstantiationType lazyType) {
Robert Phillips777707b2018-01-17 11:40:14 -0500755 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
756 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400757
Robert Phillipsa41c6852019-02-07 10:44:10 -0500758 if (desc.fWidth > this->caps()->maxTextureSize() ||
759 desc.fHeight > this->caps()->maxTextureSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400760 return nullptr;
761 }
762
Greg Danielfa55f2e2019-06-13 15:21:38 -0400763 SkASSERT(validate_backend_format_and_config(this->caps(), format, desc.fConfig));
Greg Daniel457469c2018-02-08 15:05:44 -0500764
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400765 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
766 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(format, colorType);
767 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(format, colorType);
768
Brian Salomon2a4f9832018-03-03 22:43:43 -0500769 return sk_sp<GrTextureProxy>(
770 SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags)
Greg Daniel4065d452018-11-16 15:43:41 -0500771 ? new GrTextureRenderTargetProxy(std::move(callback), lazyType, format, desc,
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400772 origin, mipMapped, texSwizzle, outSwizzle, fit,
773 budgeted, surfaceFlags)
Greg Daniel4065d452018-11-16 15:43:41 -0500774 : new GrTextureProxy(std::move(callback), lazyType, format, desc, origin,
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400775 mipMapped, texSwizzle, fit, budgeted, surfaceFlags));
Robert Phillips777707b2018-01-17 11:40:14 -0500776}
777
Robert Phillipse8fabb22018-02-04 14:33:21 -0500778sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500779 LazyInstantiateCallback&& callback, const GrBackendFormat& format,
780 const GrSurfaceDesc& desc, GrSurfaceOrigin origin, GrInternalSurfaceFlags surfaceFlags,
Greg Danielb085fa92019-03-05 16:55:12 -0500781 const TextureInfo* textureInfo, SkBackingFit fit, SkBudgeted budgeted,
782 bool wrapsVkSecondaryCB) {
Robert Phillipse8fabb22018-02-04 14:33:21 -0500783 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
784 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400785
Robert Phillipsa41c6852019-02-07 10:44:10 -0500786 if (desc.fWidth > this->caps()->maxRenderTargetSize() ||
787 desc.fHeight > this->caps()->maxRenderTargetSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400788 return nullptr;
789 }
790
Greg Daniel2a303902018-02-20 10:25:54 -0500791 SkASSERT(SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags));
Greg Danielfa55f2e2019-06-13 15:21:38 -0400792 SkASSERT(validate_backend_format_and_config(this->caps(), format, desc.fConfig));
Greg Daniel457469c2018-02-08 15:05:44 -0500793
794 using LazyInstantiationType = GrSurfaceProxy::LazyInstantiationType;
795 // For non-ddl draws always make lazy proxy's single use.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500796 LazyInstantiationType lazyType = this->renderingDirectly() ? LazyInstantiationType::kSingleUse
797 : LazyInstantiationType::kMultipleUse;
Greg Daniel457469c2018-02-08 15:05:44 -0500798
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400799 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
800 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(format, colorType);
801 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(format, colorType);
802
Brian Salomon7226c232018-07-30 13:13:17 -0400803 if (textureInfo) {
Greg Danielb085fa92019-03-05 16:55:12 -0500804 // Wrapped vulkan secondary command buffers don't support texturing since we won't have an
805 // actual VkImage to texture from.
806 SkASSERT(!wrapsVkSecondaryCB);
Brian Salomon7226c232018-07-30 13:13:17 -0400807 return sk_sp<GrRenderTargetProxy>(new GrTextureRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500808 std::move(callback), lazyType, format, desc, origin, textureInfo->fMipMapped,
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400809 texSwizzle, outSwizzle, fit, budgeted, surfaceFlags));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500810 }
811
Greg Danielb085fa92019-03-05 16:55:12 -0500812 GrRenderTargetProxy::WrapsVkSecondaryCB vkSCB =
813 wrapsVkSecondaryCB ? GrRenderTargetProxy::WrapsVkSecondaryCB::kYes
814 : GrRenderTargetProxy::WrapsVkSecondaryCB::kNo;
815
Brian Salomon2a4f9832018-03-03 22:43:43 -0500816 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400817 std::move(callback), lazyType, format, desc, origin, texSwizzle, outSwizzle, fit,
818 budgeted, surfaceFlags, vkSCB));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500819}
820
Chris Dalton19cc00f2019-04-15 09:06:28 -0600821sk_sp<GrTextureProxy> GrProxyProvider::MakeFullyLazyProxy(
822 LazyInstantiateCallback&& callback, const GrBackendFormat& format, Renderable renderable,
823 GrSurfaceOrigin origin, GrPixelConfig config, const GrCaps& caps, int sampleCnt) {
Greg Danielfa55f2e2019-06-13 15:21:38 -0400824 SkASSERT(validate_backend_format_and_config(&caps, format, config));
Robert Phillips777707b2018-01-17 11:40:14 -0500825 GrSurfaceDesc desc;
Robert Phillips10d17212019-04-24 14:09:10 -0400826 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNone;
Robert Phillips777707b2018-01-17 11:40:14 -0500827 if (Renderable::kYes == renderable) {
828 desc.fFlags = kRenderTarget_GrSurfaceFlag;
829 }
Robert Phillips777707b2018-01-17 11:40:14 -0500830 desc.fWidth = -1;
831 desc.fHeight = -1;
832 desc.fConfig = config;
Chris Dalton19cc00f2019-04-15 09:06:28 -0600833 desc.fSampleCnt = sampleCnt;
Robert Phillips777707b2018-01-17 11:40:14 -0500834
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400835 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
836 GrSwizzle texSwizzle = caps.getTextureSwizzle(format, colorType);
837 GrSwizzle outSwizzle = caps.getOutputSwizzle(format, colorType);
838
Chris Dalton4c458b12018-06-16 17:22:59 -0600839 return sk_sp<GrTextureProxy>(
840 (Renderable::kYes == renderable)
Brian Salomon7226c232018-07-30 13:13:17 -0400841 ? new GrTextureRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500842 std::move(callback), LazyInstantiationType::kSingleUse, format, desc,
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400843 origin, GrMipMapped::kNo, texSwizzle, outSwizzle,
844 SkBackingFit::kApprox, SkBudgeted::kYes, surfaceFlags)
Chris Dalton4c458b12018-06-16 17:22:59 -0600845 : new GrTextureProxy(std::move(callback), LazyInstantiationType::kSingleUse,
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400846 format, desc, origin, GrMipMapped::kNo, texSwizzle,
Brian Salomon7226c232018-07-30 13:13:17 -0400847 SkBackingFit::kApprox, SkBudgeted::kYes, surfaceFlags));
Robert Phillips777707b2018-01-17 11:40:14 -0500848}
849
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500850bool GrProxyProvider::IsFunctionallyExact(GrSurfaceProxy* proxy) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400851 const bool isInstantiated = proxy->isInstantiated();
Robert Phillipsdb3b9792018-06-12 15:18:00 -0400852 // A proxy is functionally exact if:
853 // it is exact (obvs)
854 // when it is instantiated it will be exact (i.e., power of two dimensions)
855 // it is already instantiated and the proxy covers the entire backing surface
856 return proxy->priv().isExact() ||
857 (!isInstantiated && SkIsPow2(proxy->width()) && SkIsPow2(proxy->height())) ||
858 (isInstantiated && proxy->worstCaseWidth() == proxy->width() &&
859 proxy->worstCaseHeight() == proxy->height());
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500860}
861
Robert Phillips427966a2018-12-20 17:20:43 -0500862void GrProxyProvider::processInvalidUniqueKey(const GrUniqueKey& key, GrTextureProxy* proxy,
863 InvalidateGPUResource invalidateGPUResource) {
Chris Dalton2de13dd2019-01-03 15:11:59 -0700864 SkASSERT(key.isValid());
865
Robert Phillips427966a2018-12-20 17:20:43 -0500866 if (!proxy) {
867 proxy = fUniquelyKeyedProxies.find(key);
868 }
Chris Dalton2de13dd2019-01-03 15:11:59 -0700869 SkASSERT(!proxy || proxy->getUniqueKey() == key);
870
871 // Locate the corresponding GrGpuResource (if it needs to be invalidated) before clearing the
872 // proxy's unique key. We must do it in this order because 'key' may alias the proxy's key.
873 sk_sp<GrGpuResource> invalidGpuResource;
874 if (InvalidateGPUResource::kYes == invalidateGPUResource) {
Brian Salomon01ceae92019-04-02 11:49:54 -0400875 GrContext* direct = fImageContext->priv().asDirectContext();
876 if (direct) {
877 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
878 invalidGpuResource = resourceProvider->findByUniqueKey<GrGpuResource>(key);
Chris Dalton2de13dd2019-01-03 15:11:59 -0700879 }
880 SkASSERT(!invalidGpuResource || invalidGpuResource->getUniqueKey() == key);
881 }
Robert Phillips427966a2018-12-20 17:20:43 -0500882
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500883 // Note: this method is called for the whole variety of GrGpuResources so often 'key'
884 // will not be in 'fUniquelyKeyedProxies'.
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500885 if (proxy) {
Robert Phillips427966a2018-12-20 17:20:43 -0500886 fUniquelyKeyedProxies.remove(key);
887 proxy->cacheAccess().clearUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500888 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500889
Chris Dalton2de13dd2019-01-03 15:11:59 -0700890 if (invalidGpuResource) {
891 invalidGpuResource->resourcePriv().removeUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500892 }
893}
894
Robert Phillipsa41c6852019-02-07 10:44:10 -0500895uint32_t GrProxyProvider::contextID() const {
896 return fImageContext->priv().contextID();
897}
898
899const GrCaps* GrProxyProvider::caps() const {
900 return fImageContext->priv().caps();
901}
902
903sk_sp<const GrCaps> GrProxyProvider::refCaps() const {
904 return fImageContext->priv().refCaps();
905}
906
Robert Phillipsa9162df2019-02-11 14:12:03 -0500907bool GrProxyProvider::isAbandoned() const {
908 return fImageContext->priv().abandoned();
909}
910
Robert Phillips0790f8a2018-09-18 13:11:03 -0400911void GrProxyProvider::orphanAllUniqueKeys() {
912 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
913 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
914 GrTextureProxy& tmp = *iter;
915
916 tmp.fProxyProvider = nullptr;
917 }
918}
919
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500920void GrProxyProvider::removeAllUniqueKeys() {
921 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
922 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
923 GrTextureProxy& tmp = *iter;
924
Chris Dalton2de13dd2019-01-03 15:11:59 -0700925 this->processInvalidUniqueKey(tmp.getUniqueKey(), &tmp, InvalidateGPUResource::kNo);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500926 }
927 SkASSERT(!fUniquelyKeyedProxies.count());
928}
Robert Phillipsa41c6852019-02-07 10:44:10 -0500929
930bool GrProxyProvider::renderingDirectly() const {
931 return fImageContext->priv().asDirectContext();
932}