blob: 5e0fd0e546fd77c3b04f8a252e3cd8b4219c28f5 [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,
524 GrSurfaceOrigin origin,
525 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500526 GrWrapCacheable cacheable,
Brian Salomonc67c31c2018-12-06 10:00:03 -0500527 GrIOType ioType,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500528 ReleaseProc releaseProc,
529 ReleaseContext releaseCtx) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500530 SkASSERT(ioType != kWrite_GrIOType);
Robert Phillipsf9bec202018-01-16 09:21:01 -0500531 if (this->isAbandoned()) {
532 return nullptr;
533 }
534
Brian Salomonf7778972018-03-08 10:13:17 -0500535 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500536 GrContext* direct = fImageContext->priv().asDirectContext();
537 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500538 return nullptr;
539 }
540
Robert Phillipsdd399802019-07-18 12:28:00 +0000541 SkASSERT(validate_backend_format_and_config(this->caps(), backendTex.getBackendFormat(),
542 backendTex.config()));
Greg Danielfa55f2e2019-06-13 15:21:38 -0400543
Robert Phillipsa41c6852019-02-07 10:44:10 -0500544 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
545
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500546 sk_sp<GrTexture> tex =
Robert Phillipsdd399802019-07-18 12:28:00 +0000547 resourceProvider->wrapBackendTexture(backendTex, ownership, cacheable, ioType);
Brian Salomonf7778972018-03-08 10:13:17 -0500548 if (!tex) {
549 return nullptr;
550 }
Robert Phillipsadbe1322018-01-17 13:35:46 -0500551
Greg Daniel6a0176b2018-01-30 09:28:44 -0500552 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500553 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel6a0176b2018-01-30 09:28:44 -0500554 }
555
Brian Salomonf7778972018-03-08 10:13:17 -0500556 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
557 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500558 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500559
Robert Phillipsdd399802019-07-18 12:28:00 +0000560 GrColorType colorType = GrPixelConfigToColorType(tex->config());
561 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(tex->backendFormat(), colorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400562
563 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin, texSwizzle));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500564}
565
Brian Salomon7578f3e2018-03-07 14:39:54 -0500566sk_sp<GrTextureProxy> GrProxyProvider::wrapRenderableBackendTexture(
Brian Salomon02bd2952018-03-07 15:20:21 -0500567 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400568 GrColorType colorType, GrWrapOwnership ownership, GrWrapCacheable cacheable,
569 ReleaseProc releaseProc, ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500570 if (this->isAbandoned()) {
571 return nullptr;
572 }
573
Brian Salomonf7778972018-03-08 10:13:17 -0500574 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500575 GrContext* direct = fImageContext->priv().asDirectContext();
576 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500577 return nullptr;
578 }
579
Robert Phillips0902c982019-07-16 07:47:56 -0400580 const GrCaps* caps = this->caps();
581
582 SkASSERT(GrCaps::AreConfigsCompatible(backendTex.config(),
583 caps->getConfigFromBackendFormat(
584 backendTex.getBackendFormat(),
585 colorType)));
586 SkASSERT(validate_backend_format_and_colortype(caps, colorType, backendTex.getBackendFormat()));
Greg Danielfa55f2e2019-06-13 15:21:38 -0400587
Robert Phillipsa41c6852019-02-07 10:44:10 -0500588 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
589
Robert Phillips0902c982019-07-16 07:47:56 -0400590 sampleCnt = caps->getRenderTargetSampleCount(sampleCnt, colorType,
591 backendTex.getBackendFormat());
Greg Danielf87651e2018-02-21 11:36:53 -0500592 if (!sampleCnt) {
593 return nullptr;
594 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500595
Robert Phillipsa41c6852019-02-07 10:44:10 -0500596 sk_sp<GrTexture> tex = resourceProvider->wrapRenderableBackendTexture(backendTex, sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400597 colorType, ownership,
598 cacheable);
Brian Salomonf7778972018-03-08 10:13:17 -0500599 if (!tex) {
600 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500601 }
602
Greg Daniel8ce79912019-02-05 10:08:43 -0500603 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500604 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500605 }
606
Brian Salomonf7778972018-03-08 10:13:17 -0500607 SkASSERT(tex->asRenderTarget()); // A GrTextureRenderTarget
608 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500609 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Greg Daniel6abda432018-02-15 14:55:00 -0500610
Robert Phillips0902c982019-07-16 07:47:56 -0400611 GrSwizzle texSwizzle = caps->getTextureSwizzle(tex->backendFormat(), colorType);
612 GrSwizzle outSwizzle = caps->getOutputSwizzle(tex->backendFormat(), colorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400613
614 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin, texSwizzle,
615 outSwizzle));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500616}
617
Brian Salomon7578f3e2018-03-07 14:39:54 -0500618sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget(
Robert Phillipsdd399802019-07-18 12:28:00 +0000619 const GrBackendRenderTarget& backendRT, GrSurfaceOrigin origin, ReleaseProc releaseProc,
620 ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500621 if (this->isAbandoned()) {
622 return nullptr;
623 }
624
Brian Salomonf7778972018-03-08 10:13:17 -0500625 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500626 GrContext* direct = fImageContext->priv().asDirectContext();
627 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500628 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500629 }
630
Robert Phillipsdd399802019-07-18 12:28:00 +0000631 GrColorType colorType = GrPixelConfigToColorType(backendRT.config());
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400632#ifdef SK_DEBUG
Greg Danielfa55f2e2019-06-13 15:21:38 -0400633 GrPixelConfig testConfig =
Robert Phillipsdd399802019-07-18 12:28:00 +0000634 this->caps()->validateBackendRenderTarget(backendRT, colorType);
Greg Danielfa55f2e2019-06-13 15:21:38 -0400635 SkASSERT(testConfig != kUnknown_GrPixelConfig);
636#endif
637
Robert Phillipsa41c6852019-02-07 10:44:10 -0500638 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
639
Robert Phillipsdd399802019-07-18 12:28:00 +0000640 sk_sp<GrRenderTarget> rt = resourceProvider->wrapBackendRenderTarget(backendRT);
Brian Salomonf7778972018-03-08 10:13:17 -0500641 if (!rt) {
642 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500643 }
Greg Daniel8ce79912019-02-05 10:08:43 -0500644
Greg Daniel8ce79912019-02-05 10:08:43 -0500645 if (releaseProc) {
Brian Salomon2ca31f82019-03-05 13:28:58 -0500646 rt->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500647 }
648
Brian Salomonf7778972018-03-08 10:13:17 -0500649 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
650 SkASSERT(!rt->getUniqueKey().isValid());
651 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500652 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Brian Salomonf7778972018-03-08 10:13:17 -0500653
Robert Phillipsdd399802019-07-18 12:28:00 +0000654 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(rt->backendFormat(), colorType);
655 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(rt->backendFormat(), colorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400656
657 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(std::move(rt), origin, texSwizzle,
658 outSwizzle));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500659}
660
Brian Salomon7578f3e2018-03-07 14:39:54 -0500661sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendTextureAsRenderTarget(
Robert Phillipsdd399802019-07-18 12:28:00 +0000662 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500663 if (this->isAbandoned()) {
664 return nullptr;
665 }
666
Brian Salomonf7778972018-03-08 10:13:17 -0500667 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500668 GrContext* direct = fImageContext->priv().asDirectContext();
669 if (!direct) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500670 return nullptr;
671 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500672
Robert Phillipsdd399802019-07-18 12:28:00 +0000673 SkASSERT(validate_backend_format_and_config(this->caps(), backendTex.getBackendFormat(),
674 backendTex.config()));
Greg Danielfa55f2e2019-06-13 15:21:38 -0400675
Robert Phillipsa41c6852019-02-07 10:44:10 -0500676 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
677
Brian Salomonf7778972018-03-08 10:13:17 -0500678 sk_sp<GrRenderTarget> rt =
Robert Phillipsdd399802019-07-18 12:28:00 +0000679 resourceProvider->wrapBackendTextureAsRenderTarget(backendTex, sampleCnt);
Brian Salomonf7778972018-03-08 10:13:17 -0500680 if (!rt) {
681 return nullptr;
Greg Danielf87651e2018-02-21 11:36:53 -0500682 }
Brian Salomonf7778972018-03-08 10:13:17 -0500683 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
684 SkASSERT(!rt->getUniqueKey().isValid());
Greg Danielb46add82019-01-02 14:51:29 -0500685 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500686 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielf87651e2018-02-21 11:36:53 -0500687
Robert Phillipsdd399802019-07-18 12:28:00 +0000688 GrColorType colorType = GrPixelConfigToColorType(rt->config());
689 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(rt->backendFormat(), colorType);
690 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(rt->backendFormat(), colorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400691
692 return sk_sp<GrSurfaceProxy>(new GrRenderTargetProxy(std::move(rt), origin, texSwizzle,
693 outSwizzle));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500694}
695
Greg Danielb46add82019-01-02 14:51:29 -0500696sk_sp<GrRenderTargetProxy> GrProxyProvider::wrapVulkanSecondaryCBAsRenderTarget(
697 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
698 if (this->isAbandoned()) {
699 return nullptr;
700 }
701
702 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500703 GrContext* direct = fImageContext->priv().asDirectContext();
704 if (!direct) {
Greg Danielb46add82019-01-02 14:51:29 -0500705 return nullptr;
706 }
707
Robert Phillipsa41c6852019-02-07 10:44:10 -0500708 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Danielb46add82019-01-02 14:51:29 -0500709
Robert Phillipsa41c6852019-02-07 10:44:10 -0500710 sk_sp<GrRenderTarget> rt = resourceProvider->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
711 vkInfo);
Greg Danielb46add82019-01-02 14:51:29 -0500712 if (!rt) {
713 return nullptr;
714 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500715
Greg Danielb46add82019-01-02 14:51:29 -0500716 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
717 SkASSERT(!rt->getUniqueKey().isValid());
718 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500719 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielb46add82019-01-02 14:51:29 -0500720
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400721 GrColorType colorType = GrPixelConfigToColorType(rt->config());
722 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(rt->backendFormat(), colorType);
723 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(rt->backendFormat(), colorType);
724
Greg Danielb46add82019-01-02 14:51:29 -0500725 // All Vulkan surfaces uses top left origins.
726 return sk_sp<GrRenderTargetProxy>(
727 new GrRenderTargetProxy(std::move(rt),
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400728 kTopLeft_GrSurfaceOrigin, texSwizzle, outSwizzle,
Greg Danielb46add82019-01-02 14:51:29 -0500729 GrRenderTargetProxy::WrapsVkSecondaryCB::kYes));
730}
731
Robert Phillips777707b2018-01-17 11:40:14 -0500732sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500733 const GrBackendFormat& format,
Robert Phillips777707b2018-01-17 11:40:14 -0500734 const GrSurfaceDesc& desc,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400735 GrRenderable renderable,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500736 GrSurfaceOrigin origin,
Brian Salomon7226c232018-07-30 13:13:17 -0400737 GrMipMapped mipMapped,
Brian Salomon7226c232018-07-30 13:13:17 -0400738 SkBackingFit fit,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500739 SkBudgeted budgeted) {
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400740 return this->createLazyProxy(std::move(callback), format, desc, renderable, origin, mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400741 GrInternalSurfaceFlags::kNone, fit, budgeted);
Greg Daniel2a303902018-02-20 10:25:54 -0500742}
743
744sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500745 const GrBackendFormat& format,
Greg Daniel2a303902018-02-20 10:25:54 -0500746 const GrSurfaceDesc& desc,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400747 GrRenderable renderable,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500748 GrSurfaceOrigin origin,
Greg Daniel2a303902018-02-20 10:25:54 -0500749 GrMipMapped mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400750 GrInternalSurfaceFlags surfaceFlags,
Brian Salomon7226c232018-07-30 13:13:17 -0400751 SkBackingFit fit,
752 SkBudgeted budgeted) {
Greg Daniela8d92112018-03-09 12:05:04 -0500753 // For non-ddl draws always make lazy proxy's single use.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500754 LazyInstantiationType lazyType = this->renderingDirectly() ? LazyInstantiationType::kSingleUse
755 : LazyInstantiationType::kMultipleUse;
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400756 return this->createLazyProxy(std::move(callback), format, desc, renderable, origin, mipMapped,
757 surfaceFlags, fit, budgeted, lazyType);
Greg Daniela8d92112018-03-09 12:05:04 -0500758}
759
760sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500761 const GrBackendFormat& format,
Greg Daniela8d92112018-03-09 12:05:04 -0500762 const GrSurfaceDesc& desc,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400763 GrRenderable renderable,
Greg Daniela8d92112018-03-09 12:05:04 -0500764 GrSurfaceOrigin origin,
765 GrMipMapped mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400766 GrInternalSurfaceFlags surfaceFlags,
Brian Salomon7226c232018-07-30 13:13:17 -0400767 SkBackingFit fit,
768 SkBudgeted budgeted,
Greg Daniela8d92112018-03-09 12:05:04 -0500769 LazyInstantiationType lazyType) {
Robert Phillips777707b2018-01-17 11:40:14 -0500770 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
771 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400772
Robert Phillipsa41c6852019-02-07 10:44:10 -0500773 if (desc.fWidth > this->caps()->maxTextureSize() ||
774 desc.fHeight > this->caps()->maxTextureSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400775 return nullptr;
776 }
777
Greg Danielfa55f2e2019-06-13 15:21:38 -0400778 SkASSERT(validate_backend_format_and_config(this->caps(), format, desc.fConfig));
Greg Daniel457469c2018-02-08 15:05:44 -0500779
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400780 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
781 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(format, colorType);
782 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(format, colorType);
783
Brian Salomon2a4f9832018-03-03 22:43:43 -0500784 return sk_sp<GrTextureProxy>(
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400785 renderable == GrRenderable::kYes
Greg Daniel4065d452018-11-16 15:43:41 -0500786 ? new GrTextureRenderTargetProxy(std::move(callback), lazyType, format, desc,
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400787 origin, mipMapped, texSwizzle, outSwizzle, fit,
788 budgeted, surfaceFlags)
Greg Daniel4065d452018-11-16 15:43:41 -0500789 : new GrTextureProxy(std::move(callback), lazyType, format, desc, origin,
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400790 mipMapped, texSwizzle, fit, budgeted, surfaceFlags));
Robert Phillips777707b2018-01-17 11:40:14 -0500791}
792
Robert Phillipse8fabb22018-02-04 14:33:21 -0500793sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500794 LazyInstantiateCallback&& callback, const GrBackendFormat& format,
795 const GrSurfaceDesc& desc, GrSurfaceOrigin origin, GrInternalSurfaceFlags surfaceFlags,
Greg Danielb085fa92019-03-05 16:55:12 -0500796 const TextureInfo* textureInfo, SkBackingFit fit, SkBudgeted budgeted,
797 bool wrapsVkSecondaryCB) {
Robert Phillipse8fabb22018-02-04 14:33:21 -0500798 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
799 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400800
Robert Phillipsa41c6852019-02-07 10:44:10 -0500801 if (desc.fWidth > this->caps()->maxRenderTargetSize() ||
802 desc.fHeight > this->caps()->maxRenderTargetSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400803 return nullptr;
804 }
805
Greg Danielfa55f2e2019-06-13 15:21:38 -0400806 SkASSERT(validate_backend_format_and_config(this->caps(), format, desc.fConfig));
Greg Daniel457469c2018-02-08 15:05:44 -0500807
808 using LazyInstantiationType = GrSurfaceProxy::LazyInstantiationType;
809 // For non-ddl draws always make lazy proxy's single use.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500810 LazyInstantiationType lazyType = this->renderingDirectly() ? LazyInstantiationType::kSingleUse
811 : LazyInstantiationType::kMultipleUse;
Greg Daniel457469c2018-02-08 15:05:44 -0500812
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400813 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
814 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(format, colorType);
815 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(format, colorType);
816
Brian Salomon7226c232018-07-30 13:13:17 -0400817 if (textureInfo) {
Greg Danielb085fa92019-03-05 16:55:12 -0500818 // Wrapped vulkan secondary command buffers don't support texturing since we won't have an
819 // actual VkImage to texture from.
820 SkASSERT(!wrapsVkSecondaryCB);
Brian Salomon7226c232018-07-30 13:13:17 -0400821 return sk_sp<GrRenderTargetProxy>(new GrTextureRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500822 std::move(callback), lazyType, format, desc, origin, textureInfo->fMipMapped,
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400823 texSwizzle, outSwizzle, fit, budgeted, surfaceFlags));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500824 }
825
Greg Danielb085fa92019-03-05 16:55:12 -0500826 GrRenderTargetProxy::WrapsVkSecondaryCB vkSCB =
827 wrapsVkSecondaryCB ? GrRenderTargetProxy::WrapsVkSecondaryCB::kYes
828 : GrRenderTargetProxy::WrapsVkSecondaryCB::kNo;
829
Brian Salomon2a4f9832018-03-03 22:43:43 -0500830 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400831 std::move(callback), lazyType, format, desc, origin, texSwizzle, outSwizzle, fit,
832 budgeted, surfaceFlags, vkSCB));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500833}
834
Chris Dalton19cc00f2019-04-15 09:06:28 -0600835sk_sp<GrTextureProxy> GrProxyProvider::MakeFullyLazyProxy(
836 LazyInstantiateCallback&& callback, const GrBackendFormat& format, Renderable renderable,
837 GrSurfaceOrigin origin, GrPixelConfig config, const GrCaps& caps, int sampleCnt) {
Greg Danielfa55f2e2019-06-13 15:21:38 -0400838 SkASSERT(validate_backend_format_and_config(&caps, format, config));
Robert Phillips777707b2018-01-17 11:40:14 -0500839 GrSurfaceDesc desc;
Robert Phillips10d17212019-04-24 14:09:10 -0400840 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNone;
Robert Phillips777707b2018-01-17 11:40:14 -0500841 desc.fWidth = -1;
842 desc.fHeight = -1;
843 desc.fConfig = config;
Chris Dalton19cc00f2019-04-15 09:06:28 -0600844 desc.fSampleCnt = sampleCnt;
Robert Phillips777707b2018-01-17 11:40:14 -0500845
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400846 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
847 GrSwizzle texSwizzle = caps.getTextureSwizzle(format, colorType);
848 GrSwizzle outSwizzle = caps.getOutputSwizzle(format, colorType);
849
Chris Dalton4c458b12018-06-16 17:22:59 -0600850 return sk_sp<GrTextureProxy>(
851 (Renderable::kYes == renderable)
Brian Salomon7226c232018-07-30 13:13:17 -0400852 ? new GrTextureRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500853 std::move(callback), LazyInstantiationType::kSingleUse, format, desc,
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400854 origin, GrMipMapped::kNo, texSwizzle, outSwizzle,
855 SkBackingFit::kApprox, SkBudgeted::kYes, surfaceFlags)
Chris Dalton4c458b12018-06-16 17:22:59 -0600856 : new GrTextureProxy(std::move(callback), LazyInstantiationType::kSingleUse,
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400857 format, desc, origin, GrMipMapped::kNo, texSwizzle,
Brian Salomon7226c232018-07-30 13:13:17 -0400858 SkBackingFit::kApprox, SkBudgeted::kYes, surfaceFlags));
Robert Phillips777707b2018-01-17 11:40:14 -0500859}
860
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500861bool GrProxyProvider::IsFunctionallyExact(GrSurfaceProxy* proxy) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400862 const bool isInstantiated = proxy->isInstantiated();
Robert Phillipsdb3b9792018-06-12 15:18:00 -0400863 // A proxy is functionally exact if:
864 // it is exact (obvs)
865 // when it is instantiated it will be exact (i.e., power of two dimensions)
866 // it is already instantiated and the proxy covers the entire backing surface
867 return proxy->priv().isExact() ||
868 (!isInstantiated && SkIsPow2(proxy->width()) && SkIsPow2(proxy->height())) ||
869 (isInstantiated && proxy->worstCaseWidth() == proxy->width() &&
870 proxy->worstCaseHeight() == proxy->height());
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500871}
872
Robert Phillips427966a2018-12-20 17:20:43 -0500873void GrProxyProvider::processInvalidUniqueKey(const GrUniqueKey& key, GrTextureProxy* proxy,
874 InvalidateGPUResource invalidateGPUResource) {
Chris Dalton2de13dd2019-01-03 15:11:59 -0700875 SkASSERT(key.isValid());
876
Robert Phillips427966a2018-12-20 17:20:43 -0500877 if (!proxy) {
878 proxy = fUniquelyKeyedProxies.find(key);
879 }
Chris Dalton2de13dd2019-01-03 15:11:59 -0700880 SkASSERT(!proxy || proxy->getUniqueKey() == key);
881
882 // Locate the corresponding GrGpuResource (if it needs to be invalidated) before clearing the
883 // proxy's unique key. We must do it in this order because 'key' may alias the proxy's key.
884 sk_sp<GrGpuResource> invalidGpuResource;
885 if (InvalidateGPUResource::kYes == invalidateGPUResource) {
Brian Salomon01ceae92019-04-02 11:49:54 -0400886 GrContext* direct = fImageContext->priv().asDirectContext();
887 if (direct) {
888 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
889 invalidGpuResource = resourceProvider->findByUniqueKey<GrGpuResource>(key);
Chris Dalton2de13dd2019-01-03 15:11:59 -0700890 }
891 SkASSERT(!invalidGpuResource || invalidGpuResource->getUniqueKey() == key);
892 }
Robert Phillips427966a2018-12-20 17:20:43 -0500893
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500894 // Note: this method is called for the whole variety of GrGpuResources so often 'key'
895 // will not be in 'fUniquelyKeyedProxies'.
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500896 if (proxy) {
Robert Phillips427966a2018-12-20 17:20:43 -0500897 fUniquelyKeyedProxies.remove(key);
898 proxy->cacheAccess().clearUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500899 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500900
Chris Dalton2de13dd2019-01-03 15:11:59 -0700901 if (invalidGpuResource) {
902 invalidGpuResource->resourcePriv().removeUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500903 }
904}
905
Robert Phillipsa41c6852019-02-07 10:44:10 -0500906uint32_t GrProxyProvider::contextID() const {
907 return fImageContext->priv().contextID();
908}
909
910const GrCaps* GrProxyProvider::caps() const {
911 return fImageContext->priv().caps();
912}
913
914sk_sp<const GrCaps> GrProxyProvider::refCaps() const {
915 return fImageContext->priv().refCaps();
916}
917
Robert Phillipsa9162df2019-02-11 14:12:03 -0500918bool GrProxyProvider::isAbandoned() const {
919 return fImageContext->priv().abandoned();
920}
921
Robert Phillips0790f8a2018-09-18 13:11:03 -0400922void GrProxyProvider::orphanAllUniqueKeys() {
923 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
924 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
925 GrTextureProxy& tmp = *iter;
926
927 tmp.fProxyProvider = nullptr;
928 }
929}
930
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500931void GrProxyProvider::removeAllUniqueKeys() {
932 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
933 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
934 GrTextureProxy& tmp = *iter;
935
Chris Dalton2de13dd2019-01-03 15:11:59 -0700936 this->processInvalidUniqueKey(tmp.getUniqueKey(), &tmp, InvalidateGPUResource::kNo);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500937 }
938 SkASSERT(!fUniquelyKeyedProxies.count());
939}
Robert Phillipsa41c6852019-02-07 10:44:10 -0500940
941bool GrProxyProvider::renderingDirectly() const {
942 return fImageContext->priv().asDirectContext();
943}