blob: d4b811b51a1eea8afdb16614b728ff386dedaa60 [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(
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400118 const GrSurfaceDesc& desc, GrRenderable renderable, GrSurfaceOrigin origin,
119 SkBackingFit fit, SkBudgeted budgeted) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500120 GrContext* direct = fImageContext->priv().asDirectContext();
121 if (!direct) {
122 return nullptr;
123 }
124
125 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
126 sk_sp<GrTexture> tex;
127
128 if (SkBackingFit::kApprox == fit) {
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400129 tex = resourceProvider->createApproxTexture(desc, renderable,
130 GrResourceProvider::Flags::kNoPendingIO);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500131 } else {
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400132 tex = resourceProvider->createTexture(desc, renderable, budgeted,
Robert Phillips9313aa72019-04-09 18:41:27 -0400133 GrResourceProvider::Flags::kNoPendingIO);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500134 }
135 if (!tex) {
136 return nullptr;
137 }
138
139 return this->createWrapped(std::move(tex), origin);
140}
141
142sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createWrapped(sk_sp<GrTexture> tex,
143 GrSurfaceOrigin origin) {
144 return this->createWrapped(std::move(tex), origin);
145}
146#endif
147
Robert Phillipsadbe1322018-01-17 13:35:46 -0500148sk_sp<GrTextureProxy> GrProxyProvider::createWrapped(sk_sp<GrTexture> tex, GrSurfaceOrigin origin) {
149#ifdef SK_DEBUG
150 if (tex->getUniqueKey().isValid()) {
151 SkASSERT(!this->findProxyByUniqueKey(tex->getUniqueKey(), origin));
152 }
153#endif
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400154 GrColorType colorType = GrPixelConfigToColorType(tex->config());
155 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(tex->backendFormat(), colorType);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500156
157 if (tex->asRenderTarget()) {
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400158 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(tex->backendFormat(), colorType);
159 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin,
160 texSwizzle, outSwizzle));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500161 } else {
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400162 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin, texSwizzle));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500163 }
164}
165
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500166sk_sp<GrTextureProxy> GrProxyProvider::findOrCreateProxyByUniqueKey(const GrUniqueKey& key,
167 GrSurfaceOrigin origin) {
168 ASSERT_SINGLE_OWNER
169
170 if (this->isAbandoned()) {
171 return nullptr;
172 }
173
174 sk_sp<GrTextureProxy> result = this->findProxyByUniqueKey(key, origin);
175 if (result) {
176 return result;
177 }
178
Robert Phillipsa41c6852019-02-07 10:44:10 -0500179 GrContext* direct = fImageContext->priv().asDirectContext();
180 if (!direct) {
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500181 return nullptr;
182 }
183
Robert Phillipsa41c6852019-02-07 10:44:10 -0500184 GrResourceCache* resourceCache = direct->priv().getResourceCache();
185
186 GrGpuResource* resource = resourceCache->findAndRefUniqueResource(key);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500187 if (!resource) {
188 return nullptr;
189 }
190
191 sk_sp<GrTexture> texture(static_cast<GrSurface*>(resource)->asTexture());
192 SkASSERT(texture);
193
Robert Phillipsadbe1322018-01-17 13:35:46 -0500194 result = this->createWrapped(std::move(texture), origin);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500195 SkASSERT(result->getUniqueKey() == key);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500196 // createWrapped should've added this for us
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500197 SkASSERT(fUniquelyKeyedProxies.find(key));
198 return result;
199}
200
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500201sk_sp<GrTextureProxy> GrProxyProvider::createTextureProxy(sk_sp<SkImage> srcImage,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400202 GrRenderable renderable,
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500203 int sampleCnt,
Greg Danielfb3abcd2018-02-02 15:48:33 -0500204 SkBudgeted budgeted,
Chris Daltond004e0b2018-09-27 09:28:03 -0600205 SkBackingFit fit,
206 GrInternalSurfaceFlags surfaceFlags) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500207 ASSERT_SINGLE_OWNER
208 SkASSERT(srcImage);
209
210 if (this->isAbandoned()) {
211 return nullptr;
212 }
213
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400214 const SkImageInfo& info = srcImage->imageInfo();
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400215 SkColorType ct = info.colorType();
Greg Daniel2f2caea2019-07-08 14:24:47 -0400216 GrColorType grCT = SkColorTypeToGrColorType(ct);
Greg Danielf87651e2018-02-21 11:36:53 -0500217
Greg Daniel627d0532019-07-08 16:48:14 -0400218 GrBackendFormat format = this->caps()->getBackendFormatFromColorType(grCT);
Greg Daniel0a7aa142018-02-21 13:02:32 -0500219
Greg Daniel7b1bebf2019-07-16 17:09:59 -0400220 if (!format.isValid() || !this->caps()->isFormatTexturable(grCT, 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;
Greg Daniel627d0532019-07-08 16:48:14 -0400229 grCT = GrColorType::kRGBA_8888;
230 format = this->caps()->getBackendFormatFromColorType(grCT);
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400231 if (!format.isValid()) {
232 return nullptr;
233 }
Brian Osmand29dcd12018-09-13 15:04:29 -0400234 }
235
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400236 if (renderable == GrRenderable::kYes) {
Greg Daniel5c96db82019-07-09 14:06:58 -0400237 sampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, grCT, format);
Greg Danielf87651e2018-02-21 11:36:53 -0500238 if (!sampleCnt) {
239 return nullptr;
240 }
241 }
242
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400243 GrPixelConfig config = SkColorType2GrPixelConfig(ct);
244 if (kUnknown_GrPixelConfig == config) {
245 return nullptr;
246 }
247
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500248 GrSurfaceDesc desc;
249 desc.fWidth = srcImage->width();
250 desc.fHeight = srcImage->height();
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(
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400255 [desc, renderable, 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 Salomonf2c2ba92019-07-17 09:59:59 -0400260 return LazyInstantiationResult(
261 resourceProvider->createTexture(desc, renderable, budgeted, fit, mipLevel,
262 GrResourceProvider::Flags::kNoPendingIO));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500263 },
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400264 format, desc, renderable, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, surfaceFlags, fit,
265 budgeted);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500266
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400267 if (!proxy) {
268 return nullptr;
269 }
270
Robert Phillipsa41c6852019-02-07 10:44:10 -0500271 GrContext* direct = fImageContext->priv().asDirectContext();
272 if (direct) {
273 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
274
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500275 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
276 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500277 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500278 return nullptr;
279 }
280 }
Robert Phillipsc1b60662018-06-26 10:20:08 -0400281
282 SkASSERT(proxy->width() == desc.fWidth);
283 SkASSERT(proxy->height() == desc.fHeight);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500284 return proxy;
285}
286
Greg Daniel4065d452018-11-16 15:43:41 -0500287sk_sp<GrTextureProxy> GrProxyProvider::createMipMapProxy(const GrBackendFormat& format,
288 const GrSurfaceDesc& desc,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400289 GrRenderable renderable,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500290 GrSurfaceOrigin origin,
Greg Daniel30815082018-02-09 16:08:30 -0500291 SkBudgeted budgeted) {
Robert Phillips579f0942018-01-08 14:53:35 -0500292 ASSERT_SINGLE_OWNER
293
294 if (this->isAbandoned()) {
295 return nullptr;
296 }
297
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400298 return this->createProxy(format, desc, renderable, origin, GrMipMapped::kYes,
299 SkBackingFit::kExact, budgeted, GrInternalSurfaceFlags::kNone);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500300}
301
Brian Osmande496652019-03-22 13:42:33 -0400302sk_sp<GrTextureProxy> GrProxyProvider::createProxyFromBitmap(const SkBitmap& bitmap,
303 GrMipMapped mipMapped) {
Brian Osman412674f2019-02-07 15:34:58 -0500304 ASSERT_SINGLE_OWNER
Brian Osman7dcc6162019-03-25 10:12:57 -0400305 SkASSERT(GrMipMapped::kNo == mipMapped || this->caps()->mipMapSupport());
Brian Osman412674f2019-02-07 15:34:58 -0500306
307 if (this->isAbandoned()) {
308 return nullptr;
309 }
310
Brian Osman2b23c4b2018-06-01 12:25:08 -0400311 if (!SkImageInfoIsValid(bitmap.info())) {
Greg Daniela4ead652018-02-07 10:21:48 -0500312 return nullptr;
313 }
314
Brian Osmande496652019-03-22 13:42:33 -0400315 ATRACE_ANDROID_FRAMEWORK("Upload %sTexture [%ux%u]",
316 GrMipMapped::kYes == mipMapped ? "MipMap " : "",
317 bitmap.width(), bitmap.height());
Greg Daniela4ead652018-02-07 10:21:48 -0500318
319 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
320 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
321 // 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 -0500322 SkCopyPixelsMode copyMode = this->renderingDirectly() ? kNever_SkCopyPixelsMode
323 : kIfMutable_SkCopyPixelsMode;
Greg Daniela4ead652018-02-07 10:21:48 -0500324 sk_sp<SkImage> baseLevel = SkMakeImageFromRasterBitmap(bitmap, copyMode);
Greg Daniela4ead652018-02-07 10:21:48 -0500325 if (!baseLevel) {
326 return nullptr;
327 }
328
Brian Osmande496652019-03-22 13:42:33 -0400329 // If mips weren't requested (or this was too small to have any), then take the fast path
330 if (GrMipMapped::kNo == mipMapped ||
331 0 == SkMipMap::ComputeLevelCount(baseLevel->width(), baseLevel->height())) {
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400332 return this->createTextureProxy(std::move(baseLevel), GrRenderable::kNo, 1,
Brian Osman7dcc6162019-03-25 10:12:57 -0400333 SkBudgeted::kYes, SkBackingFit::kExact);
Greg Daniela4ead652018-02-07 10:21:48 -0500334 }
335
Greg Daniel627d0532019-07-08 16:48:14 -0400336 GrColorType grColorType = SkColorTypeToGrColorType(bitmap.info().colorType());
337 GrBackendFormat format = this->caps()->getBackendFormatFromColorType(grColorType);
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400338 if (!format.isValid()) {
339 return nullptr;
340 }
Brian Osmand29dcd12018-09-13 15:04:29 -0400341 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info());
Greg Daniel3fc5df42019-06-14 09:42:17 -0400342
Greg Daniel2f2caea2019-07-08 14:24:47 -0400343 if (!this->caps()->isFormatTexturable(grColorType, format)) {
Brian Osmand29dcd12018-09-13 15:04:29 -0400344 SkBitmap copy8888;
345 if (!copy8888.tryAllocPixels(bitmap.info().makeColorType(kRGBA_8888_SkColorType)) ||
346 !bitmap.readPixels(copy8888.pixmap())) {
347 return nullptr;
348 }
349 copy8888.setImmutable();
350 baseLevel = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode);
351 desc.fConfig = kRGBA_8888_GrPixelConfig;
Greg Daniel627d0532019-07-08 16:48:14 -0400352 grColorType = GrColorType::kRGBA_8888;
353 format = this->caps()->getBackendFormatFromColorType(grColorType);
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400354 if (!format.isValid()) {
355 return nullptr;
356 }
Greg Daniel3fc5df42019-06-14 09:42:17 -0400357 }
358
Brian Osmand29dcd12018-09-13 15:04:29 -0400359
360 SkPixmap pixmap;
361 SkAssertResult(baseLevel->peekPixels(&pixmap));
362 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(pixmap, nullptr));
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400363 if (!mipmaps) {
364 return nullptr;
365 }
366
Greg Daniela4ead652018-02-07 10:21:48 -0500367 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Osman1b97f132018-09-13 17:33:48 +0000368 [desc, baseLevel, mipmaps](GrResourceProvider* resourceProvider) {
Brian Osman1b97f132018-09-13 17:33:48 +0000369 const int mipLevelCount = mipmaps->countLevels() + 1;
370 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
371
Greg Daniela4ead652018-02-07 10:21:48 -0500372 SkPixmap pixmap;
373 SkAssertResult(baseLevel->peekPixels(&pixmap));
374
375 // DDL TODO: Instead of copying all this info into GrMipLevels we should just plumb
376 // the use of SkMipMap down through Ganesh.
377 texels[0].fPixels = pixmap.addr();
378 texels[0].fRowBytes = pixmap.rowBytes();
379
380 for (int i = 1; i < mipLevelCount; ++i) {
381 SkMipMap::Level generatedMipLevel;
382 mipmaps->getLevel(i - 1, &generatedMipLevel);
383 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
384 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
385 SkASSERT(texels[i].fPixels);
386 }
387
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400388 return LazyInstantiationResult(resourceProvider->createTexture(
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400389 desc, GrRenderable::kNo, SkBudgeted::kYes, texels.get(), mipLevelCount));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500390 },
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400391 format, desc, GrRenderable::kNo, kTopLeft_GrSurfaceOrigin, GrMipMapped::kYes,
392 SkBackingFit::kExact, SkBudgeted::kYes);
Greg Daniela4ead652018-02-07 10:21:48 -0500393
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400394 if (!proxy) {
395 return nullptr;
396 }
397
Robert Phillipsa41c6852019-02-07 10:44:10 -0500398 GrContext* direct = fImageContext->priv().asDirectContext();
399 if (direct) {
400 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Daniela4ead652018-02-07 10:21:48 -0500401 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
402 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500403 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Greg Daniela4ead652018-02-07 10:21:48 -0500404 return nullptr;
405 }
406 }
407 return proxy;
408}
409
Greg Danielfa55f2e2019-06-13 15:21:38 -0400410#ifdef SK_DEBUG
411static bool validate_backend_format_and_config(const GrCaps* caps,
412 const GrBackendFormat& format,
413 GrPixelConfig config) {
414 if (kUnknown_GrPixelConfig == config) {
415 return false;
416 }
Brian Salomonbb8dde82019-06-27 10:52:13 -0400417 if (GrPixelConfigIsCompressed(config)) {
418 // We have no way to verify these at the moment.
419 return true;
420 }
Robert Phillipsc046ff02019-07-01 10:34:03 -0400421
Robert Phillips1e2cb442019-07-02 15:51:28 -0400422 GrColorType grCT = GrPixelConfigToColorType(config);
423
424 return caps->areColorTypeAndFormatCompatible(grCT, format);
Greg Danielfa55f2e2019-06-13 15:21:38 -0400425}
Robert Phillips0902c982019-07-16 07:47:56 -0400426
427static bool validate_backend_format_and_colortype(const GrCaps* caps,
428 GrColorType colorType,
429 const GrBackendFormat& format) {
430 return caps->areColorTypeAndFormatCompatible(colorType, format);
431}
Greg Danielfa55f2e2019-06-13 15:21:38 -0400432#endif
433
Greg Daniel4065d452018-11-16 15:43:41 -0500434sk_sp<GrTextureProxy> GrProxyProvider::createProxy(const GrBackendFormat& format,
435 const GrSurfaceDesc& desc,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400436 GrRenderable renderable,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500437 GrSurfaceOrigin origin,
Greg Danielf6f7b672018-02-15 13:06:26 -0500438 GrMipMapped mipMapped,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500439 SkBackingFit fit,
440 SkBudgeted budgeted,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400441 GrInternalSurfaceFlags surfaceFlags) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400442 if (GrPixelConfigIsCompressed(desc.fConfig)) {
443 // Deferred proxies for compressed textures are not supported.
444 return nullptr;
445 }
Robert Phillips0902c982019-07-16 07:47:56 -0400446
447 const GrCaps* caps = this->caps();
448 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
449
450 SkASSERT(GrCaps::AreConfigsCompatible(desc.fConfig,
451 caps->getConfigFromBackendFormat(format, colorType)));
452 SkASSERT(validate_backend_format_and_colortype(caps, colorType, format));
Greg Danielf6f7b672018-02-15 13:06:26 -0500453 if (GrMipMapped::kYes == mipMapped) {
454 // SkMipMap doesn't include the base level in the level count so we have to add 1
455 int mipCount = SkMipMap::ComputeLevelCount(desc.fWidth, desc.fHeight) + 1;
456 if (1 == mipCount) {
457 mipMapped = GrMipMapped::kNo;
458 }
459 }
460
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400461 if (!caps->validateSurfaceDesc(desc, renderable, mipMapped)) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500462 return nullptr;
463 }
Brian Salomon3a2cc2c2018-02-03 00:25:12 +0000464 GrSurfaceDesc copyDesc = desc;
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400465 if (renderable == GrRenderable::kYes) {
Robert Phillips0902c982019-07-16 07:47:56 -0400466 copyDesc.fSampleCnt = caps->getRenderTargetSampleCount(desc.fSampleCnt, colorType, format);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500467 }
Brian Salomon3a2cc2c2018-02-03 00:25:12 +0000468
Robert Phillips0902c982019-07-16 07:47:56 -0400469 GrSwizzle texSwizzle = caps->getTextureSwizzle(format, colorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400470
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400471 if (renderable == GrRenderable::kYes) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500472 // We know anything we instantiate later from this deferred path will be
473 // both texturable and renderable
Robert Phillips0902c982019-07-16 07:47:56 -0400474 GrSwizzle outSwizzle = caps->getOutputSwizzle(format, colorType);
475 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(*caps, format, copyDesc,
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400476 origin, mipMapped, texSwizzle,
477 outSwizzle, fit, budgeted,
478 surfaceFlags));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500479 }
480
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400481 return sk_sp<GrTextureProxy>(new GrTextureProxy(format, copyDesc, origin, mipMapped, texSwizzle,
Brian Salomon7226c232018-07-30 13:13:17 -0400482 fit, budgeted, surfaceFlags));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500483}
484
Brian Salomonbb8dde82019-06-27 10:52:13 -0400485sk_sp<GrTextureProxy> GrProxyProvider::createCompressedTextureProxy(
486 int width, int height, SkBudgeted budgeted, SkImage::CompressionType compressionType,
487 sk_sp<SkData> data) {
488 GrBackendFormat format = this->caps()->getBackendFormatFromCompressionType(compressionType);
489
490 GrSurfaceDesc desc;
491 desc.fConfig = GrCompressionTypePixelConfig(compressionType);
492 desc.fWidth = width;
493 desc.fHeight = height;
494
Jim Van Verthee06b332019-01-18 10:36:32 -0500495 if (!this->caps()->isConfigTexturable(desc.fConfig)) {
496 return nullptr;
497 }
498
Jim Van Verthee06b332019-01-18 10:36:32 -0500499 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomonbb8dde82019-06-27 10:52:13 -0400500 [width, height, compressionType, budgeted, data](GrResourceProvider* resourceProvider) {
501 return LazyInstantiationResult(resourceProvider->createCompressedTexture(
502 width, height, compressionType, budgeted, data.get()));
503 },
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400504 format, desc, GrRenderable::kNo, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo,
505 SkBackingFit::kExact, SkBudgeted::kYes);
Jim Van Verthee06b332019-01-18 10:36:32 -0500506
507 if (!proxy) {
508 return nullptr;
509 }
510
Robert Phillipsa41c6852019-02-07 10:44:10 -0500511 GrContext* direct = fImageContext->priv().asDirectContext();
512 if (direct) {
513 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Jim Van Verthee06b332019-01-18 10:36:32 -0500514 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
515 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500516 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500517 return nullptr;
518 }
519 }
520 return proxy;
521}
522
Brian Salomon7578f3e2018-03-07 14:39:54 -0500523sk_sp<GrTextureProxy> GrProxyProvider::wrapBackendTexture(const GrBackendTexture& backendTex,
Robert Phillips97256382019-07-17 15:26:36 -0400524 GrColorType grColorType,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500525 GrSurfaceOrigin origin,
526 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500527 GrWrapCacheable cacheable,
Brian Salomonc67c31c2018-12-06 10:00:03 -0500528 GrIOType ioType,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500529 ReleaseProc releaseProc,
530 ReleaseContext releaseCtx) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500531 SkASSERT(ioType != kWrite_GrIOType);
Robert Phillipsf9bec202018-01-16 09:21:01 -0500532 if (this->isAbandoned()) {
533 return nullptr;
534 }
535
Brian Salomonf7778972018-03-08 10:13:17 -0500536 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500537 GrContext* direct = fImageContext->priv().asDirectContext();
538 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500539 return nullptr;
540 }
541
Robert Phillips97256382019-07-17 15:26:36 -0400542 const GrCaps* caps = this->caps();
543
544 SkASSERT(GrCaps::AreConfigsCompatible(backendTex.config(),
545 caps->getConfigFromBackendFormat(
546 backendTex.getBackendFormat(),
547 grColorType)));
548
549 SkASSERT(validate_backend_format_and_colortype(caps, grColorType,
550 backendTex.getBackendFormat()));
Greg Danielfa55f2e2019-06-13 15:21:38 -0400551
Robert Phillipsa41c6852019-02-07 10:44:10 -0500552 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
553
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500554 sk_sp<GrTexture> tex =
Robert Phillips97256382019-07-17 15:26:36 -0400555 resourceProvider->wrapBackendTexture(backendTex, grColorType,
556 ownership, cacheable, ioType);
Brian Salomonf7778972018-03-08 10:13:17 -0500557 if (!tex) {
558 return nullptr;
559 }
Robert Phillipsadbe1322018-01-17 13:35:46 -0500560
Greg Daniel6a0176b2018-01-30 09:28:44 -0500561 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500562 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel6a0176b2018-01-30 09:28:44 -0500563 }
564
Brian Salomonf7778972018-03-08 10:13:17 -0500565 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
566 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500567 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500568
Robert Phillips97256382019-07-17 15:26:36 -0400569 GrSwizzle texSwizzle = caps->getTextureSwizzle(tex->backendFormat(), grColorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400570
571 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin, texSwizzle));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500572}
573
Brian Salomon7578f3e2018-03-07 14:39:54 -0500574sk_sp<GrTextureProxy> GrProxyProvider::wrapRenderableBackendTexture(
Brian Salomon02bd2952018-03-07 15:20:21 -0500575 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400576 GrColorType colorType, GrWrapOwnership ownership, GrWrapCacheable cacheable,
577 ReleaseProc releaseProc, ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500578 if (this->isAbandoned()) {
579 return nullptr;
580 }
581
Brian Salomonf7778972018-03-08 10:13:17 -0500582 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500583 GrContext* direct = fImageContext->priv().asDirectContext();
584 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500585 return nullptr;
586 }
587
Robert Phillips0902c982019-07-16 07:47:56 -0400588 const GrCaps* caps = this->caps();
589
590 SkASSERT(GrCaps::AreConfigsCompatible(backendTex.config(),
591 caps->getConfigFromBackendFormat(
592 backendTex.getBackendFormat(),
593 colorType)));
594 SkASSERT(validate_backend_format_and_colortype(caps, colorType, backendTex.getBackendFormat()));
Greg Danielfa55f2e2019-06-13 15:21:38 -0400595
Robert Phillipsa41c6852019-02-07 10:44:10 -0500596 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
597
Robert Phillips0902c982019-07-16 07:47:56 -0400598 sampleCnt = caps->getRenderTargetSampleCount(sampleCnt, colorType,
599 backendTex.getBackendFormat());
Greg Danielf87651e2018-02-21 11:36:53 -0500600 if (!sampleCnt) {
601 return nullptr;
602 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500603
Robert Phillipsa41c6852019-02-07 10:44:10 -0500604 sk_sp<GrTexture> tex = resourceProvider->wrapRenderableBackendTexture(backendTex, sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400605 colorType, ownership,
606 cacheable);
Brian Salomonf7778972018-03-08 10:13:17 -0500607 if (!tex) {
608 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500609 }
610
Greg Daniel8ce79912019-02-05 10:08:43 -0500611 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500612 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500613 }
614
Brian Salomonf7778972018-03-08 10:13:17 -0500615 SkASSERT(tex->asRenderTarget()); // A GrTextureRenderTarget
616 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500617 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Greg Daniel6abda432018-02-15 14:55:00 -0500618
Robert Phillips0902c982019-07-16 07:47:56 -0400619 GrSwizzle texSwizzle = caps->getTextureSwizzle(tex->backendFormat(), colorType);
620 GrSwizzle outSwizzle = caps->getOutputSwizzle(tex->backendFormat(), colorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400621
622 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin, texSwizzle,
623 outSwizzle));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500624}
625
Brian Salomon7578f3e2018-03-07 14:39:54 -0500626sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget(
Robert Phillips97256382019-07-17 15:26:36 -0400627 const GrBackendRenderTarget& backendRT, GrColorType grColorType,
628 GrSurfaceOrigin origin, ReleaseProc releaseProc, ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500629 if (this->isAbandoned()) {
630 return nullptr;
631 }
632
Brian Salomonf7778972018-03-08 10:13:17 -0500633 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500634 GrContext* direct = fImageContext->priv().asDirectContext();
635 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500636 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500637 }
638
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400639#ifdef SK_DEBUG
Greg Danielfa55f2e2019-06-13 15:21:38 -0400640 GrPixelConfig testConfig =
Robert Phillips97256382019-07-17 15:26:36 -0400641 this->caps()->validateBackendRenderTarget(backendRT, grColorType);
Greg Danielfa55f2e2019-06-13 15:21:38 -0400642 SkASSERT(testConfig != kUnknown_GrPixelConfig);
643#endif
644
Robert Phillipsa41c6852019-02-07 10:44:10 -0500645 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
646
Robert Phillips97256382019-07-17 15:26:36 -0400647 sk_sp<GrRenderTarget> rt = resourceProvider->wrapBackendRenderTarget(backendRT, grColorType);
Brian Salomonf7778972018-03-08 10:13:17 -0500648 if (!rt) {
649 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500650 }
Greg Daniel8ce79912019-02-05 10:08:43 -0500651
Greg Daniel8ce79912019-02-05 10:08:43 -0500652 if (releaseProc) {
Brian Salomon2ca31f82019-03-05 13:28:58 -0500653 rt->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500654 }
655
Brian Salomonf7778972018-03-08 10:13:17 -0500656 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
657 SkASSERT(!rt->getUniqueKey().isValid());
658 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500659 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Brian Salomonf7778972018-03-08 10:13:17 -0500660
Robert Phillips97256382019-07-17 15:26:36 -0400661 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(rt->backendFormat(), grColorType);
662 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(rt->backendFormat(), grColorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400663
664 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(std::move(rt), origin, texSwizzle,
665 outSwizzle));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500666}
667
Brian Salomon7578f3e2018-03-07 14:39:54 -0500668sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendTextureAsRenderTarget(
Robert Phillips97256382019-07-17 15:26:36 -0400669 const GrBackendTexture& backendTex, GrColorType grColorType,
670 GrSurfaceOrigin origin, int sampleCnt) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500671 if (this->isAbandoned()) {
672 return nullptr;
673 }
674
Brian Salomonf7778972018-03-08 10:13:17 -0500675 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500676 GrContext* direct = fImageContext->priv().asDirectContext();
677 if (!direct) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500678 return nullptr;
679 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500680
Robert Phillips97256382019-07-17 15:26:36 -0400681 const GrCaps* caps = this->caps();
682
683 SkASSERT(GrCaps::AreConfigsCompatible(backendTex.config(),
684 caps->getConfigFromBackendFormat(
685 backendTex.getBackendFormat(),
686 grColorType)));
687
688 SkASSERT(validate_backend_format_and_colortype(caps, grColorType,
689 backendTex.getBackendFormat()));
Greg Danielfa55f2e2019-06-13 15:21:38 -0400690
Robert Phillipsa41c6852019-02-07 10:44:10 -0500691 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
692
Brian Salomonf7778972018-03-08 10:13:17 -0500693 sk_sp<GrRenderTarget> rt =
Robert Phillips97256382019-07-17 15:26:36 -0400694 resourceProvider->wrapBackendTextureAsRenderTarget(backendTex, sampleCnt, grColorType);
Brian Salomonf7778972018-03-08 10:13:17 -0500695 if (!rt) {
696 return nullptr;
Greg Danielf87651e2018-02-21 11:36:53 -0500697 }
Brian Salomonf7778972018-03-08 10:13:17 -0500698 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
699 SkASSERT(!rt->getUniqueKey().isValid());
Greg Danielb46add82019-01-02 14:51:29 -0500700 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500701 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielf87651e2018-02-21 11:36:53 -0500702
Robert Phillips97256382019-07-17 15:26:36 -0400703 GrSwizzle texSwizzle = caps->getTextureSwizzle(rt->backendFormat(), grColorType);
704 GrSwizzle outSwizzle = caps->getOutputSwizzle(rt->backendFormat(), grColorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400705
706 return sk_sp<GrSurfaceProxy>(new GrRenderTargetProxy(std::move(rt), origin, texSwizzle,
707 outSwizzle));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500708}
709
Greg Danielb46add82019-01-02 14:51:29 -0500710sk_sp<GrRenderTargetProxy> GrProxyProvider::wrapVulkanSecondaryCBAsRenderTarget(
711 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
712 if (this->isAbandoned()) {
713 return nullptr;
714 }
715
716 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500717 GrContext* direct = fImageContext->priv().asDirectContext();
718 if (!direct) {
Greg Danielb46add82019-01-02 14:51:29 -0500719 return nullptr;
720 }
721
Robert Phillipsa41c6852019-02-07 10:44:10 -0500722 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Danielb46add82019-01-02 14:51:29 -0500723
Robert Phillipsa41c6852019-02-07 10:44:10 -0500724 sk_sp<GrRenderTarget> rt = resourceProvider->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
725 vkInfo);
Greg Danielb46add82019-01-02 14:51:29 -0500726 if (!rt) {
727 return nullptr;
728 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500729
Greg Danielb46add82019-01-02 14:51:29 -0500730 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
731 SkASSERT(!rt->getUniqueKey().isValid());
732 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500733 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielb46add82019-01-02 14:51:29 -0500734
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400735 GrColorType colorType = GrPixelConfigToColorType(rt->config());
736 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(rt->backendFormat(), colorType);
737 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(rt->backendFormat(), colorType);
738
Greg Danielb46add82019-01-02 14:51:29 -0500739 // All Vulkan surfaces uses top left origins.
740 return sk_sp<GrRenderTargetProxy>(
741 new GrRenderTargetProxy(std::move(rt),
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400742 kTopLeft_GrSurfaceOrigin, texSwizzle, outSwizzle,
Greg Danielb46add82019-01-02 14:51:29 -0500743 GrRenderTargetProxy::WrapsVkSecondaryCB::kYes));
744}
745
Robert Phillips777707b2018-01-17 11:40:14 -0500746sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500747 const GrBackendFormat& format,
Robert Phillips777707b2018-01-17 11:40:14 -0500748 const GrSurfaceDesc& desc,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400749 GrRenderable renderable,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500750 GrSurfaceOrigin origin,
Brian Salomon7226c232018-07-30 13:13:17 -0400751 GrMipMapped mipMapped,
Brian Salomon7226c232018-07-30 13:13:17 -0400752 SkBackingFit fit,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500753 SkBudgeted budgeted) {
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400754 return this->createLazyProxy(std::move(callback), format, desc, renderable, origin, mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400755 GrInternalSurfaceFlags::kNone, fit, budgeted);
Greg Daniel2a303902018-02-20 10:25:54 -0500756}
757
758sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500759 const GrBackendFormat& format,
Greg Daniel2a303902018-02-20 10:25:54 -0500760 const GrSurfaceDesc& desc,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400761 GrRenderable renderable,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500762 GrSurfaceOrigin origin,
Greg Daniel2a303902018-02-20 10:25:54 -0500763 GrMipMapped mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400764 GrInternalSurfaceFlags surfaceFlags,
Brian Salomon7226c232018-07-30 13:13:17 -0400765 SkBackingFit fit,
766 SkBudgeted budgeted) {
Greg Daniela8d92112018-03-09 12:05:04 -0500767 // For non-ddl draws always make lazy proxy's single use.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500768 LazyInstantiationType lazyType = this->renderingDirectly() ? LazyInstantiationType::kSingleUse
769 : LazyInstantiationType::kMultipleUse;
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400770 return this->createLazyProxy(std::move(callback), format, desc, renderable, origin, mipMapped,
771 surfaceFlags, fit, budgeted, lazyType);
Greg Daniela8d92112018-03-09 12:05:04 -0500772}
773
774sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500775 const GrBackendFormat& format,
Greg Daniela8d92112018-03-09 12:05:04 -0500776 const GrSurfaceDesc& desc,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400777 GrRenderable renderable,
Greg Daniela8d92112018-03-09 12:05:04 -0500778 GrSurfaceOrigin origin,
779 GrMipMapped mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400780 GrInternalSurfaceFlags surfaceFlags,
Brian Salomon7226c232018-07-30 13:13:17 -0400781 SkBackingFit fit,
782 SkBudgeted budgeted,
Greg Daniela8d92112018-03-09 12:05:04 -0500783 LazyInstantiationType lazyType) {
Robert Phillips777707b2018-01-17 11:40:14 -0500784 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
785 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400786
Robert Phillipsa41c6852019-02-07 10:44:10 -0500787 if (desc.fWidth > this->caps()->maxTextureSize() ||
788 desc.fHeight > this->caps()->maxTextureSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400789 return nullptr;
790 }
791
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
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400794 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
795 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(format, colorType);
796 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(format, colorType);
797
Brian Salomon2a4f9832018-03-03 22:43:43 -0500798 return sk_sp<GrTextureProxy>(
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400799 renderable == GrRenderable::kYes
Greg Daniel4065d452018-11-16 15:43:41 -0500800 ? new GrTextureRenderTargetProxy(std::move(callback), lazyType, format, desc,
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400801 origin, mipMapped, texSwizzle, outSwizzle, fit,
802 budgeted, surfaceFlags)
Greg Daniel4065d452018-11-16 15:43:41 -0500803 : new GrTextureProxy(std::move(callback), lazyType, format, desc, origin,
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400804 mipMapped, texSwizzle, fit, budgeted, surfaceFlags));
Robert Phillips777707b2018-01-17 11:40:14 -0500805}
806
Robert Phillipse8fabb22018-02-04 14:33:21 -0500807sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500808 LazyInstantiateCallback&& callback, const GrBackendFormat& format,
809 const GrSurfaceDesc& desc, GrSurfaceOrigin origin, GrInternalSurfaceFlags surfaceFlags,
Greg Danielb085fa92019-03-05 16:55:12 -0500810 const TextureInfo* textureInfo, SkBackingFit fit, SkBudgeted budgeted,
811 bool wrapsVkSecondaryCB) {
Robert Phillipse8fabb22018-02-04 14:33:21 -0500812 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
813 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400814
Robert Phillipsa41c6852019-02-07 10:44:10 -0500815 if (desc.fWidth > this->caps()->maxRenderTargetSize() ||
816 desc.fHeight > this->caps()->maxRenderTargetSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400817 return nullptr;
818 }
819
Greg Danielfa55f2e2019-06-13 15:21:38 -0400820 SkASSERT(validate_backend_format_and_config(this->caps(), format, desc.fConfig));
Greg Daniel457469c2018-02-08 15:05:44 -0500821
822 using LazyInstantiationType = GrSurfaceProxy::LazyInstantiationType;
823 // For non-ddl draws always make lazy proxy's single use.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500824 LazyInstantiationType lazyType = this->renderingDirectly() ? LazyInstantiationType::kSingleUse
825 : LazyInstantiationType::kMultipleUse;
Greg Daniel457469c2018-02-08 15:05:44 -0500826
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400827 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
828 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(format, colorType);
829 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(format, colorType);
830
Brian Salomon7226c232018-07-30 13:13:17 -0400831 if (textureInfo) {
Greg Danielb085fa92019-03-05 16:55:12 -0500832 // Wrapped vulkan secondary command buffers don't support texturing since we won't have an
833 // actual VkImage to texture from.
834 SkASSERT(!wrapsVkSecondaryCB);
Brian Salomon7226c232018-07-30 13:13:17 -0400835 return sk_sp<GrRenderTargetProxy>(new GrTextureRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500836 std::move(callback), lazyType, format, desc, origin, textureInfo->fMipMapped,
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400837 texSwizzle, outSwizzle, fit, budgeted, surfaceFlags));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500838 }
839
Greg Danielb085fa92019-03-05 16:55:12 -0500840 GrRenderTargetProxy::WrapsVkSecondaryCB vkSCB =
841 wrapsVkSecondaryCB ? GrRenderTargetProxy::WrapsVkSecondaryCB::kYes
842 : GrRenderTargetProxy::WrapsVkSecondaryCB::kNo;
843
Brian Salomon2a4f9832018-03-03 22:43:43 -0500844 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400845 std::move(callback), lazyType, format, desc, origin, texSwizzle, outSwizzle, fit,
846 budgeted, surfaceFlags, vkSCB));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500847}
848
Chris Dalton19cc00f2019-04-15 09:06:28 -0600849sk_sp<GrTextureProxy> GrProxyProvider::MakeFullyLazyProxy(
850 LazyInstantiateCallback&& callback, const GrBackendFormat& format, Renderable renderable,
851 GrSurfaceOrigin origin, GrPixelConfig config, const GrCaps& caps, int sampleCnt) {
Greg Danielfa55f2e2019-06-13 15:21:38 -0400852 SkASSERT(validate_backend_format_and_config(&caps, format, config));
Robert Phillips777707b2018-01-17 11:40:14 -0500853 GrSurfaceDesc desc;
Robert Phillips10d17212019-04-24 14:09:10 -0400854 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNone;
Robert Phillips777707b2018-01-17 11:40:14 -0500855 desc.fWidth = -1;
856 desc.fHeight = -1;
857 desc.fConfig = config;
Chris Dalton19cc00f2019-04-15 09:06:28 -0600858 desc.fSampleCnt = sampleCnt;
Robert Phillips777707b2018-01-17 11:40:14 -0500859
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400860 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
861 GrSwizzle texSwizzle = caps.getTextureSwizzle(format, colorType);
862 GrSwizzle outSwizzle = caps.getOutputSwizzle(format, colorType);
863
Chris Dalton4c458b12018-06-16 17:22:59 -0600864 return sk_sp<GrTextureProxy>(
865 (Renderable::kYes == renderable)
Brian Salomon7226c232018-07-30 13:13:17 -0400866 ? new GrTextureRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500867 std::move(callback), LazyInstantiationType::kSingleUse, format, desc,
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400868 origin, GrMipMapped::kNo, texSwizzle, outSwizzle,
869 SkBackingFit::kApprox, SkBudgeted::kYes, surfaceFlags)
Chris Dalton4c458b12018-06-16 17:22:59 -0600870 : new GrTextureProxy(std::move(callback), LazyInstantiationType::kSingleUse,
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400871 format, desc, origin, GrMipMapped::kNo, texSwizzle,
Brian Salomon7226c232018-07-30 13:13:17 -0400872 SkBackingFit::kApprox, SkBudgeted::kYes, surfaceFlags));
Robert Phillips777707b2018-01-17 11:40:14 -0500873}
874
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500875bool GrProxyProvider::IsFunctionallyExact(GrSurfaceProxy* proxy) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400876 const bool isInstantiated = proxy->isInstantiated();
Robert Phillipsdb3b9792018-06-12 15:18:00 -0400877 // A proxy is functionally exact if:
878 // it is exact (obvs)
879 // when it is instantiated it will be exact (i.e., power of two dimensions)
880 // it is already instantiated and the proxy covers the entire backing surface
881 return proxy->priv().isExact() ||
882 (!isInstantiated && SkIsPow2(proxy->width()) && SkIsPow2(proxy->height())) ||
883 (isInstantiated && proxy->worstCaseWidth() == proxy->width() &&
884 proxy->worstCaseHeight() == proxy->height());
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500885}
886
Robert Phillips427966a2018-12-20 17:20:43 -0500887void GrProxyProvider::processInvalidUniqueKey(const GrUniqueKey& key, GrTextureProxy* proxy,
888 InvalidateGPUResource invalidateGPUResource) {
Chris Dalton2de13dd2019-01-03 15:11:59 -0700889 SkASSERT(key.isValid());
890
Robert Phillips427966a2018-12-20 17:20:43 -0500891 if (!proxy) {
892 proxy = fUniquelyKeyedProxies.find(key);
893 }
Chris Dalton2de13dd2019-01-03 15:11:59 -0700894 SkASSERT(!proxy || proxy->getUniqueKey() == key);
895
896 // Locate the corresponding GrGpuResource (if it needs to be invalidated) before clearing the
897 // proxy's unique key. We must do it in this order because 'key' may alias the proxy's key.
898 sk_sp<GrGpuResource> invalidGpuResource;
899 if (InvalidateGPUResource::kYes == invalidateGPUResource) {
Brian Salomon01ceae92019-04-02 11:49:54 -0400900 GrContext* direct = fImageContext->priv().asDirectContext();
901 if (direct) {
902 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
903 invalidGpuResource = resourceProvider->findByUniqueKey<GrGpuResource>(key);
Chris Dalton2de13dd2019-01-03 15:11:59 -0700904 }
905 SkASSERT(!invalidGpuResource || invalidGpuResource->getUniqueKey() == key);
906 }
Robert Phillips427966a2018-12-20 17:20:43 -0500907
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500908 // Note: this method is called for the whole variety of GrGpuResources so often 'key'
909 // will not be in 'fUniquelyKeyedProxies'.
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500910 if (proxy) {
Robert Phillips427966a2018-12-20 17:20:43 -0500911 fUniquelyKeyedProxies.remove(key);
912 proxy->cacheAccess().clearUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500913 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500914
Chris Dalton2de13dd2019-01-03 15:11:59 -0700915 if (invalidGpuResource) {
916 invalidGpuResource->resourcePriv().removeUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500917 }
918}
919
Robert Phillipsa41c6852019-02-07 10:44:10 -0500920uint32_t GrProxyProvider::contextID() const {
921 return fImageContext->priv().contextID();
922}
923
924const GrCaps* GrProxyProvider::caps() const {
925 return fImageContext->priv().caps();
926}
927
928sk_sp<const GrCaps> GrProxyProvider::refCaps() const {
929 return fImageContext->priv().refCaps();
930}
931
Robert Phillipsa9162df2019-02-11 14:12:03 -0500932bool GrProxyProvider::isAbandoned() const {
933 return fImageContext->priv().abandoned();
934}
935
Robert Phillips0790f8a2018-09-18 13:11:03 -0400936void GrProxyProvider::orphanAllUniqueKeys() {
937 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
938 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
939 GrTextureProxy& tmp = *iter;
940
941 tmp.fProxyProvider = nullptr;
942 }
943}
944
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500945void GrProxyProvider::removeAllUniqueKeys() {
946 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
947 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
948 GrTextureProxy& tmp = *iter;
949
Chris Dalton2de13dd2019-01-03 15:11:59 -0700950 this->processInvalidUniqueKey(tmp.getUniqueKey(), &tmp, InvalidateGPUResource::kNo);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500951 }
952 SkASSERT(!fUniquelyKeyedProxies.count());
953}
Robert Phillipsa41c6852019-02-07 10:44:10 -0500954
955bool GrProxyProvider::renderingDirectly() const {
956 return fImageContext->priv().asDirectContext();
957}