blob: 9a2da8857d41d1cef7d3b17d00122b64472d477a [file] [log] [blame]
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001/*
2 * Copyright 2018 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrProxyProvider.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkBitmap.h"
11#include "include/core/SkImage.h"
12#include "include/gpu/GrContext.h"
13#include "include/gpu/GrRenderTarget.h"
14#include "include/gpu/GrTexture.h"
15#include "include/private/GrImageContext.h"
16#include "include/private/GrResourceKey.h"
17#include "include/private/GrSingleOwner.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "include/private/SkImageInfoPriv.h"
19#include "src/core/SkAutoPixmapStorage.h"
20#include "src/core/SkImagePriv.h"
21#include "src/core/SkMipMap.h"
22#include "src/core/SkTraceEvent.h"
23#include "src/gpu/GrCaps.h"
24#include "src/gpu/GrContextPriv.h"
25#include "src/gpu/GrImageContextPriv.h"
26#include "src/gpu/GrResourceProvider.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040027#include "src/gpu/GrSurfaceProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/gpu/GrSurfaceProxyPriv.h"
29#include "src/gpu/GrTextureProxyCacheAccess.h"
30#include "src/gpu/GrTextureRenderTargetProxy.h"
31#include "src/gpu/SkGr.h"
32#include "src/image/SkImage_Base.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050033
34#define ASSERT_SINGLE_OWNER \
Robert Phillipsa41c6852019-02-07 10:44:10 -050035 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fImageContext->priv().singleOwner());)
Robert Phillips1afd4cd2018-01-08 13:40:32 -050036
Robert Phillipsa9162df2019-02-11 14:12:03 -050037GrProxyProvider::GrProxyProvider(GrImageContext* imageContext) : fImageContext(imageContext) {}
Robert Phillips1afd4cd2018-01-08 13:40:32 -050038
39GrProxyProvider::~GrProxyProvider() {
Robert Phillipsa41c6852019-02-07 10:44:10 -050040 if (this->renderingDirectly()) {
Robert Phillips0790f8a2018-09-18 13:11:03 -040041 // In DDL-mode a proxy provider can still have extant uniquely keyed proxies (since
42 // they need their unique keys to, potentially, find a cached resource when the
43 // DDL is played) but, in non-DDL-mode they should all have been cleaned up by this point.
44 SkASSERT(!fUniquelyKeyedProxies.count());
45 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050046}
47
Robert Phillipsadbe1322018-01-17 13:35:46 -050048bool GrProxyProvider::assignUniqueKeyToProxy(const GrUniqueKey& key, GrTextureProxy* proxy) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050049 ASSERT_SINGLE_OWNER
50 SkASSERT(key.isValid());
51 if (this->isAbandoned() || !proxy) {
Robert Phillipsadbe1322018-01-17 13:35:46 -050052 return false;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050053 }
54
Robert Phillipsa41c6852019-02-07 10:44:10 -050055#ifdef SK_DEBUG
56 {
57 GrContext* direct = fImageContext->priv().asDirectContext();
58 if (direct) {
59 GrResourceCache* resourceCache = direct->priv().getResourceCache();
60 // If there is already a GrResource with this key then the caller has violated the
61 // normal usage pattern of uniquely keyed resources (e.g., they have created one w/o
62 // first seeing if it already existed in the cache).
63 SkASSERT(!resourceCache->findAndRefUniqueResource(key));
64 }
65 }
66#endif
Robert Phillips1afd4cd2018-01-08 13:40:32 -050067
Robert Phillips1afd4cd2018-01-08 13:40:32 -050068 SkASSERT(!fUniquelyKeyedProxies.find(key)); // multiple proxies can't get the same key
69
70 proxy->cacheAccess().setUniqueKey(this, key);
71 SkASSERT(proxy->getUniqueKey() == key);
72 fUniquelyKeyedProxies.add(proxy);
Robert Phillipsadbe1322018-01-17 13:35:46 -050073 return true;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050074}
75
76void GrProxyProvider::adoptUniqueKeyFromSurface(GrTextureProxy* proxy, const GrSurface* surf) {
77 SkASSERT(surf->getUniqueKey().isValid());
78 proxy->cacheAccess().setUniqueKey(this, surf->getUniqueKey());
79 SkASSERT(proxy->getUniqueKey() == surf->getUniqueKey());
80 // multiple proxies can't get the same key
81 SkASSERT(!fUniquelyKeyedProxies.find(surf->getUniqueKey()));
82 fUniquelyKeyedProxies.add(proxy);
83}
84
Chris Dalton2de13dd2019-01-03 15:11:59 -070085void GrProxyProvider::removeUniqueKeyFromProxy(GrTextureProxy* proxy) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050086 ASSERT_SINGLE_OWNER
Chris Dalton2de13dd2019-01-03 15:11:59 -070087 SkASSERT(proxy);
88 SkASSERT(proxy->getUniqueKey().isValid());
89
90 if (this->isAbandoned()) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050091 return;
92 }
Robert Phillips0790f8a2018-09-18 13:11:03 -040093
Chris Dalton2de13dd2019-01-03 15:11:59 -070094 this->processInvalidUniqueKey(proxy->getUniqueKey(), proxy, InvalidateGPUResource::kYes);
Robert Phillips1afd4cd2018-01-08 13:40:32 -050095}
96
97sk_sp<GrTextureProxy> GrProxyProvider::findProxyByUniqueKey(const GrUniqueKey& key,
98 GrSurfaceOrigin origin) {
99 ASSERT_SINGLE_OWNER
100
101 if (this->isAbandoned()) {
102 return nullptr;
103 }
104
Brian Salomon01ceae92019-04-02 11:49:54 -0400105 GrTextureProxy* proxy = fUniquelyKeyedProxies.find(key);
Brian Salomon01ceae92019-04-02 11:49:54 -0400106 if (proxy) {
Robert Phillipse5f73282019-06-18 17:15:04 -0400107 SkASSERT(proxy->getProxyRefCnt() >= 1);
108 SkASSERT(proxy->origin() == origin);
109 return sk_ref_sp(proxy);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500110 }
Robert Phillipse5f73282019-06-18 17:15:04 -0400111 return nullptr;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500112}
113
Robert Phillipsa41c6852019-02-07 10:44:10 -0500114///////////////////////////////////////////////////////////////////////////////
115
116#if GR_TEST_UTILS
117sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createInstantiatedProxy(
118 const GrSurfaceDesc& desc, GrSurfaceOrigin origin, SkBackingFit fit, SkBudgeted budgeted) {
119 GrContext* direct = fImageContext->priv().asDirectContext();
120 if (!direct) {
121 return nullptr;
122 }
123
124 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
125 sk_sp<GrTexture> tex;
126
127 if (SkBackingFit::kApprox == fit) {
Robert Phillips9313aa72019-04-09 18:41:27 -0400128 tex = resourceProvider->createApproxTexture(desc, GrResourceProvider::Flags::kNoPendingIO);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500129 } else {
Robert Phillips9313aa72019-04-09 18:41:27 -0400130 tex = resourceProvider->createTexture(desc, budgeted,
131 GrResourceProvider::Flags::kNoPendingIO);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500132 }
133 if (!tex) {
134 return nullptr;
135 }
136
137 return this->createWrapped(std::move(tex), origin);
138}
139
140sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createWrapped(sk_sp<GrTexture> tex,
141 GrSurfaceOrigin origin) {
142 return this->createWrapped(std::move(tex), origin);
143}
144#endif
145
Robert Phillipsadbe1322018-01-17 13:35:46 -0500146sk_sp<GrTextureProxy> GrProxyProvider::createWrapped(sk_sp<GrTexture> tex, GrSurfaceOrigin origin) {
147#ifdef SK_DEBUG
148 if (tex->getUniqueKey().isValid()) {
149 SkASSERT(!this->findProxyByUniqueKey(tex->getUniqueKey(), origin));
150 }
151#endif
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400152 GrColorType colorType = GrPixelConfigToColorType(tex->config());
153 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(tex->backendFormat(), colorType);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500154
155 if (tex->asRenderTarget()) {
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400156 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(tex->backendFormat(), colorType);
157 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin,
158 texSwizzle, outSwizzle));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500159 } else {
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400160 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin, texSwizzle));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500161 }
162}
163
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500164sk_sp<GrTextureProxy> GrProxyProvider::findOrCreateProxyByUniqueKey(const GrUniqueKey& key,
165 GrSurfaceOrigin origin) {
166 ASSERT_SINGLE_OWNER
167
168 if (this->isAbandoned()) {
169 return nullptr;
170 }
171
172 sk_sp<GrTextureProxy> result = this->findProxyByUniqueKey(key, origin);
173 if (result) {
174 return result;
175 }
176
Robert Phillipsa41c6852019-02-07 10:44:10 -0500177 GrContext* direct = fImageContext->priv().asDirectContext();
178 if (!direct) {
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500179 return nullptr;
180 }
181
Robert Phillipsa41c6852019-02-07 10:44:10 -0500182 GrResourceCache* resourceCache = direct->priv().getResourceCache();
183
184 GrGpuResource* resource = resourceCache->findAndRefUniqueResource(key);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500185 if (!resource) {
186 return nullptr;
187 }
188
189 sk_sp<GrTexture> texture(static_cast<GrSurface*>(resource)->asTexture());
190 SkASSERT(texture);
191
Robert Phillipsadbe1322018-01-17 13:35:46 -0500192 result = this->createWrapped(std::move(texture), origin);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500193 SkASSERT(result->getUniqueKey() == key);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500194 // createWrapped should've added this for us
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500195 SkASSERT(fUniquelyKeyedProxies.find(key));
196 return result;
197}
198
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500199sk_sp<GrTextureProxy> GrProxyProvider::createTextureProxy(sk_sp<SkImage> srcImage,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400200 GrSurfaceDescFlags descFlags,
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500201 int sampleCnt,
Greg Danielfb3abcd2018-02-02 15:48:33 -0500202 SkBudgeted budgeted,
Chris Daltond004e0b2018-09-27 09:28:03 -0600203 SkBackingFit fit,
204 GrInternalSurfaceFlags surfaceFlags) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500205 ASSERT_SINGLE_OWNER
206 SkASSERT(srcImage);
207
208 if (this->isAbandoned()) {
209 return nullptr;
210 }
211
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400212 const SkImageInfo& info = srcImage->imageInfo();
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400213 SkColorType ct = info.colorType();
Greg Daniel2f2caea2019-07-08 14:24:47 -0400214 GrColorType grCT = SkColorTypeToGrColorType(ct);
Greg Danielf87651e2018-02-21 11:36:53 -0500215
Greg Daniel627d0532019-07-08 16:48:14 -0400216 GrBackendFormat format = this->caps()->getBackendFormatFromColorType(grCT);
Greg Daniel0a7aa142018-02-21 13:02:32 -0500217
Greg Daniel7b1bebf2019-07-16 17:09:59 -0400218 if (!format.isValid() || !this->caps()->isFormatTexturable(grCT, format)) {
Brian Osmand29dcd12018-09-13 15:04:29 -0400219 SkBitmap copy8888;
220 if (!copy8888.tryAllocPixels(info.makeColorType(kRGBA_8888_SkColorType)) ||
221 !srcImage->readPixels(copy8888.pixmap(), 0, 0)) {
222 return nullptr;
223 }
224 copy8888.setImmutable();
225 srcImage = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode);
Jim Van Verthc8098322019-04-16 10:50:01 -0400226 ct = kRGBA_8888_SkColorType;
Greg Daniel627d0532019-07-08 16:48:14 -0400227 grCT = GrColorType::kRGBA_8888;
228 format = this->caps()->getBackendFormatFromColorType(grCT);
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400229 if (!format.isValid()) {
230 return nullptr;
231 }
Brian Osmand29dcd12018-09-13 15:04:29 -0400232 }
233
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400234 if (SkToBool(descFlags & kRenderTarget_GrSurfaceFlag)) {
Greg Daniel5c96db82019-07-09 14:06:58 -0400235 sampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, grCT, format);
Greg Danielf87651e2018-02-21 11:36:53 -0500236 if (!sampleCnt) {
237 return nullptr;
238 }
239 }
240
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400241 GrPixelConfig config = SkColorType2GrPixelConfig(ct);
242 if (kUnknown_GrPixelConfig == config) {
243 return nullptr;
244 }
245
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500246 GrSurfaceDesc desc;
247 desc.fWidth = srcImage->width();
248 desc.fHeight = srcImage->height();
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400249 desc.fFlags = descFlags;
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500250 desc.fSampleCnt = sampleCnt;
Greg Danielf87651e2018-02-21 11:36:53 -0500251 desc.fConfig = config;
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500252
253 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Robert Phillips10d17212019-04-24 14:09:10 -0400254 [desc, budgeted, srcImage, fit](GrResourceProvider* resourceProvider) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500255 SkPixmap pixMap;
256 SkAssertResult(srcImage->peekPixels(&pixMap));
257 GrMipLevel mipLevel = { pixMap.addr(), pixMap.rowBytes() };
258
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400259 return LazyInstantiationResult(resourceProvider->createTexture(
Robert Phillips10d17212019-04-24 14:09:10 -0400260 desc, budgeted, fit, mipLevel, GrResourceProvider::Flags::kNoPendingIO));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500261 },
Greg Daniel4065d452018-11-16 15:43:41 -0500262 format, desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, surfaceFlags, fit, budgeted);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500263
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400264 if (!proxy) {
265 return nullptr;
266 }
267
Robert Phillipsa41c6852019-02-07 10:44:10 -0500268 GrContext* direct = fImageContext->priv().asDirectContext();
269 if (direct) {
270 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
271
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500272 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
273 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500274 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500275 return nullptr;
276 }
277 }
Robert Phillipsc1b60662018-06-26 10:20:08 -0400278
279 SkASSERT(proxy->width() == desc.fWidth);
280 SkASSERT(proxy->height() == desc.fHeight);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500281 return proxy;
282}
283
Greg Daniel4065d452018-11-16 15:43:41 -0500284sk_sp<GrTextureProxy> GrProxyProvider::createMipMapProxy(const GrBackendFormat& format,
285 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500286 GrSurfaceOrigin origin,
Greg Daniel30815082018-02-09 16:08:30 -0500287 SkBudgeted budgeted) {
Robert Phillips579f0942018-01-08 14:53:35 -0500288 ASSERT_SINGLE_OWNER
289
290 if (this->isAbandoned()) {
291 return nullptr;
292 }
293
Greg Daniel4065d452018-11-16 15:43:41 -0500294 return this->createProxy(format, desc, origin, GrMipMapped::kYes, SkBackingFit::kExact,
295 budgeted, GrInternalSurfaceFlags::kNone);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500296}
297
Brian Osmande496652019-03-22 13:42:33 -0400298sk_sp<GrTextureProxy> GrProxyProvider::createProxyFromBitmap(const SkBitmap& bitmap,
299 GrMipMapped mipMapped) {
Brian Osman412674f2019-02-07 15:34:58 -0500300 ASSERT_SINGLE_OWNER
Brian Osman7dcc6162019-03-25 10:12:57 -0400301 SkASSERT(GrMipMapped::kNo == mipMapped || this->caps()->mipMapSupport());
Brian Osman412674f2019-02-07 15:34:58 -0500302
303 if (this->isAbandoned()) {
304 return nullptr;
305 }
306
Brian Osman2b23c4b2018-06-01 12:25:08 -0400307 if (!SkImageInfoIsValid(bitmap.info())) {
Greg Daniela4ead652018-02-07 10:21:48 -0500308 return nullptr;
309 }
310
Brian Osmande496652019-03-22 13:42:33 -0400311 ATRACE_ANDROID_FRAMEWORK("Upload %sTexture [%ux%u]",
312 GrMipMapped::kYes == mipMapped ? "MipMap " : "",
313 bitmap.width(), bitmap.height());
Greg Daniela4ead652018-02-07 10:21:48 -0500314
315 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
316 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
317 // 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 -0500318 SkCopyPixelsMode copyMode = this->renderingDirectly() ? kNever_SkCopyPixelsMode
319 : kIfMutable_SkCopyPixelsMode;
Greg Daniela4ead652018-02-07 10:21:48 -0500320 sk_sp<SkImage> baseLevel = SkMakeImageFromRasterBitmap(bitmap, copyMode);
Greg Daniela4ead652018-02-07 10:21:48 -0500321 if (!baseLevel) {
322 return nullptr;
323 }
324
Brian Osmande496652019-03-22 13:42:33 -0400325 // If mips weren't requested (or this was too small to have any), then take the fast path
326 if (GrMipMapped::kNo == mipMapped ||
327 0 == SkMipMap::ComputeLevelCount(baseLevel->width(), baseLevel->height())) {
Brian Osman7dcc6162019-03-25 10:12:57 -0400328 return this->createTextureProxy(std::move(baseLevel), kNone_GrSurfaceFlags, 1,
329 SkBudgeted::kYes, SkBackingFit::kExact);
Greg Daniela4ead652018-02-07 10:21:48 -0500330 }
331
Greg Daniel627d0532019-07-08 16:48:14 -0400332 GrColorType grColorType = SkColorTypeToGrColorType(bitmap.info().colorType());
333 GrBackendFormat format = this->caps()->getBackendFormatFromColorType(grColorType);
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400334 if (!format.isValid()) {
335 return nullptr;
336 }
Brian Osmand29dcd12018-09-13 15:04:29 -0400337 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info());
Greg Daniel3fc5df42019-06-14 09:42:17 -0400338
Greg Daniel2f2caea2019-07-08 14:24:47 -0400339 if (!this->caps()->isFormatTexturable(grColorType, format)) {
Brian Osmand29dcd12018-09-13 15:04:29 -0400340 SkBitmap copy8888;
341 if (!copy8888.tryAllocPixels(bitmap.info().makeColorType(kRGBA_8888_SkColorType)) ||
342 !bitmap.readPixels(copy8888.pixmap())) {
343 return nullptr;
344 }
345 copy8888.setImmutable();
346 baseLevel = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode);
347 desc.fConfig = kRGBA_8888_GrPixelConfig;
Greg Daniel627d0532019-07-08 16:48:14 -0400348 grColorType = GrColorType::kRGBA_8888;
349 format = this->caps()->getBackendFormatFromColorType(grColorType);
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400350 if (!format.isValid()) {
351 return nullptr;
352 }
Greg Daniel3fc5df42019-06-14 09:42:17 -0400353 }
354
Brian Osmand29dcd12018-09-13 15:04:29 -0400355
356 SkPixmap pixmap;
357 SkAssertResult(baseLevel->peekPixels(&pixmap));
358 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(pixmap, nullptr));
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400359 if (!mipmaps) {
360 return nullptr;
361 }
362
Greg Daniela4ead652018-02-07 10:21:48 -0500363 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Osman1b97f132018-09-13 17:33:48 +0000364 [desc, baseLevel, mipmaps](GrResourceProvider* resourceProvider) {
Brian Osman1b97f132018-09-13 17:33:48 +0000365 const int mipLevelCount = mipmaps->countLevels() + 1;
366 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
367
Greg Daniela4ead652018-02-07 10:21:48 -0500368 SkPixmap pixmap;
369 SkAssertResult(baseLevel->peekPixels(&pixmap));
370
371 // DDL TODO: Instead of copying all this info into GrMipLevels we should just plumb
372 // the use of SkMipMap down through Ganesh.
373 texels[0].fPixels = pixmap.addr();
374 texels[0].fRowBytes = pixmap.rowBytes();
375
376 for (int i = 1; i < mipLevelCount; ++i) {
377 SkMipMap::Level generatedMipLevel;
378 mipmaps->getLevel(i - 1, &generatedMipLevel);
379 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
380 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
381 SkASSERT(texels[i].fPixels);
382 }
383
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400384 return LazyInstantiationResult(resourceProvider->createTexture(
385 desc, SkBudgeted::kYes, texels.get(), mipLevelCount));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500386 },
Greg Daniel4065d452018-11-16 15:43:41 -0500387 format, desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kYes, SkBackingFit::kExact,
388 SkBudgeted::kYes);
Greg Daniela4ead652018-02-07 10:21:48 -0500389
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400390 if (!proxy) {
391 return nullptr;
392 }
393
Robert Phillipsa41c6852019-02-07 10:44:10 -0500394 GrContext* direct = fImageContext->priv().asDirectContext();
395 if (direct) {
396 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Daniela4ead652018-02-07 10:21:48 -0500397 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
398 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500399 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Greg Daniela4ead652018-02-07 10:21:48 -0500400 return nullptr;
401 }
402 }
403 return proxy;
404}
405
Greg Danielfa55f2e2019-06-13 15:21:38 -0400406#ifdef SK_DEBUG
407static bool validate_backend_format_and_config(const GrCaps* caps,
408 const GrBackendFormat& format,
409 GrPixelConfig config) {
410 if (kUnknown_GrPixelConfig == config) {
411 return false;
412 }
Brian Salomonbb8dde82019-06-27 10:52:13 -0400413 if (GrPixelConfigIsCompressed(config)) {
414 // We have no way to verify these at the moment.
415 return true;
416 }
Robert Phillipsc046ff02019-07-01 10:34:03 -0400417
Robert Phillips1e2cb442019-07-02 15:51:28 -0400418 GrColorType grCT = GrPixelConfigToColorType(config);
419
420 return caps->areColorTypeAndFormatCompatible(grCT, format);
Greg Danielfa55f2e2019-06-13 15:21:38 -0400421}
Robert Phillips0902c982019-07-16 07:47:56 -0400422
423static bool validate_backend_format_and_colortype(const GrCaps* caps,
424 GrColorType colorType,
425 const GrBackendFormat& format) {
426 return caps->areColorTypeAndFormatCompatible(colorType, format);
427}
Greg Danielfa55f2e2019-06-13 15:21:38 -0400428#endif
429
Greg Daniel4065d452018-11-16 15:43:41 -0500430sk_sp<GrTextureProxy> GrProxyProvider::createProxy(const GrBackendFormat& format,
431 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500432 GrSurfaceOrigin origin,
Greg Danielf6f7b672018-02-15 13:06:26 -0500433 GrMipMapped mipMapped,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500434 SkBackingFit fit,
435 SkBudgeted budgeted,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400436 GrInternalSurfaceFlags surfaceFlags) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400437 if (GrPixelConfigIsCompressed(desc.fConfig)) {
438 // Deferred proxies for compressed textures are not supported.
439 return nullptr;
440 }
Robert Phillips0902c982019-07-16 07:47:56 -0400441
442 const GrCaps* caps = this->caps();
443 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
444
445 SkASSERT(GrCaps::AreConfigsCompatible(desc.fConfig,
446 caps->getConfigFromBackendFormat(format, colorType)));
447 SkASSERT(validate_backend_format_and_colortype(caps, colorType, format));
Greg Danielf6f7b672018-02-15 13:06:26 -0500448 if (GrMipMapped::kYes == mipMapped) {
449 // SkMipMap doesn't include the base level in the level count so we have to add 1
450 int mipCount = SkMipMap::ComputeLevelCount(desc.fWidth, desc.fHeight) + 1;
451 if (1 == mipCount) {
452 mipMapped = GrMipMapped::kNo;
453 }
454 }
455
Robert Phillips0902c982019-07-16 07:47:56 -0400456 if (!caps->validateSurfaceDesc(desc, mipMapped)) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500457 return nullptr;
458 }
Brian Salomon3a2cc2c2018-02-03 00:25:12 +0000459 GrSurfaceDesc copyDesc = desc;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500460 if (desc.fFlags & kRenderTarget_GrSurfaceFlag) {
Robert Phillips0902c982019-07-16 07:47:56 -0400461 copyDesc.fSampleCnt = caps->getRenderTargetSampleCount(desc.fSampleCnt, colorType, format);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500462 }
Brian Salomon3a2cc2c2018-02-03 00:25:12 +0000463
Robert Phillips0902c982019-07-16 07:47:56 -0400464 GrSwizzle texSwizzle = caps->getTextureSwizzle(format, colorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400465
Brian Salomonbdecacf2018-02-02 20:32:49 -0500466 if (copyDesc.fFlags & kRenderTarget_GrSurfaceFlag) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500467 // We know anything we instantiate later from this deferred path will be
468 // both texturable and renderable
Robert Phillips0902c982019-07-16 07:47:56 -0400469 GrSwizzle outSwizzle = caps->getOutputSwizzle(format, colorType);
470 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(*caps, format, copyDesc,
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400471 origin, mipMapped, texSwizzle,
472 outSwizzle, fit, budgeted,
473 surfaceFlags));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500474 }
475
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400476 return sk_sp<GrTextureProxy>(new GrTextureProxy(format, copyDesc, origin, mipMapped, texSwizzle,
Brian Salomon7226c232018-07-30 13:13:17 -0400477 fit, budgeted, surfaceFlags));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500478}
479
Brian Salomonbb8dde82019-06-27 10:52:13 -0400480sk_sp<GrTextureProxy> GrProxyProvider::createCompressedTextureProxy(
481 int width, int height, SkBudgeted budgeted, SkImage::CompressionType compressionType,
482 sk_sp<SkData> data) {
483 GrBackendFormat format = this->caps()->getBackendFormatFromCompressionType(compressionType);
484
485 GrSurfaceDesc desc;
486 desc.fConfig = GrCompressionTypePixelConfig(compressionType);
487 desc.fWidth = width;
488 desc.fHeight = height;
489
Jim Van Verthee06b332019-01-18 10:36:32 -0500490 if (!this->caps()->isConfigTexturable(desc.fConfig)) {
491 return nullptr;
492 }
493
Jim Van Verthee06b332019-01-18 10:36:32 -0500494 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomonbb8dde82019-06-27 10:52:13 -0400495 [width, height, compressionType, budgeted, data](GrResourceProvider* resourceProvider) {
496 return LazyInstantiationResult(resourceProvider->createCompressedTexture(
497 width, height, compressionType, budgeted, data.get()));
498 },
499 format, desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, SkBackingFit::kExact,
500 SkBudgeted::kYes);
Jim Van Verthee06b332019-01-18 10:36:32 -0500501
502 if (!proxy) {
503 return nullptr;
504 }
505
Robert Phillipsa41c6852019-02-07 10:44:10 -0500506 GrContext* direct = fImageContext->priv().asDirectContext();
507 if (direct) {
508 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Jim Van Verthee06b332019-01-18 10:36:32 -0500509 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
510 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500511 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500512 return nullptr;
513 }
514 }
515 return proxy;
516}
517
Brian Salomon7578f3e2018-03-07 14:39:54 -0500518sk_sp<GrTextureProxy> GrProxyProvider::wrapBackendTexture(const GrBackendTexture& backendTex,
519 GrSurfaceOrigin origin,
520 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500521 GrWrapCacheable cacheable,
Brian Salomonc67c31c2018-12-06 10:00:03 -0500522 GrIOType ioType,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500523 ReleaseProc releaseProc,
524 ReleaseContext releaseCtx) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500525 SkASSERT(ioType != kWrite_GrIOType);
Robert Phillipsf9bec202018-01-16 09:21:01 -0500526 if (this->isAbandoned()) {
527 return nullptr;
528 }
529
Brian Salomonf7778972018-03-08 10:13:17 -0500530 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500531 GrContext* direct = fImageContext->priv().asDirectContext();
532 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500533 return nullptr;
534 }
535
Greg Danielfa55f2e2019-06-13 15:21:38 -0400536 SkASSERT(validate_backend_format_and_config(this->caps(), backendTex.getBackendFormat(),
537 backendTex.config()));
Greg Danielfa55f2e2019-06-13 15:21:38 -0400538
Robert Phillipsa41c6852019-02-07 10:44:10 -0500539 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
540
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500541 sk_sp<GrTexture> tex =
Robert Phillipsa41c6852019-02-07 10:44:10 -0500542 resourceProvider->wrapBackendTexture(backendTex, ownership, cacheable, ioType);
Brian Salomonf7778972018-03-08 10:13:17 -0500543 if (!tex) {
544 return nullptr;
545 }
Robert Phillipsadbe1322018-01-17 13:35:46 -0500546
Greg Daniel6a0176b2018-01-30 09:28:44 -0500547 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500548 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel6a0176b2018-01-30 09:28:44 -0500549 }
550
Brian Salomonf7778972018-03-08 10:13:17 -0500551 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
552 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500553 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500554
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400555 GrColorType colorType = GrPixelConfigToColorType(tex->config());
556 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(tex->backendFormat(), colorType);
557
558 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin, texSwizzle));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500559}
560
Brian Salomon7578f3e2018-03-07 14:39:54 -0500561sk_sp<GrTextureProxy> GrProxyProvider::wrapRenderableBackendTexture(
Brian Salomon02bd2952018-03-07 15:20:21 -0500562 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400563 GrColorType colorType, GrWrapOwnership ownership, GrWrapCacheable cacheable,
564 ReleaseProc releaseProc, ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500565 if (this->isAbandoned()) {
566 return nullptr;
567 }
568
Brian Salomonf7778972018-03-08 10:13:17 -0500569 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500570 GrContext* direct = fImageContext->priv().asDirectContext();
571 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500572 return nullptr;
573 }
574
Robert Phillips0902c982019-07-16 07:47:56 -0400575 const GrCaps* caps = this->caps();
576
577 SkASSERT(GrCaps::AreConfigsCompatible(backendTex.config(),
578 caps->getConfigFromBackendFormat(
579 backendTex.getBackendFormat(),
580 colorType)));
581 SkASSERT(validate_backend_format_and_colortype(caps, colorType, backendTex.getBackendFormat()));
Greg Danielfa55f2e2019-06-13 15:21:38 -0400582
Robert Phillipsa41c6852019-02-07 10:44:10 -0500583 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
584
Robert Phillips0902c982019-07-16 07:47:56 -0400585 sampleCnt = caps->getRenderTargetSampleCount(sampleCnt, colorType,
586 backendTex.getBackendFormat());
Greg Danielf87651e2018-02-21 11:36:53 -0500587 if (!sampleCnt) {
588 return nullptr;
589 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500590
Robert Phillipsa41c6852019-02-07 10:44:10 -0500591 sk_sp<GrTexture> tex = resourceProvider->wrapRenderableBackendTexture(backendTex, sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400592 colorType, ownership,
593 cacheable);
Brian Salomonf7778972018-03-08 10:13:17 -0500594 if (!tex) {
595 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500596 }
597
Greg Daniel8ce79912019-02-05 10:08:43 -0500598 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500599 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500600 }
601
Brian Salomonf7778972018-03-08 10:13:17 -0500602 SkASSERT(tex->asRenderTarget()); // A GrTextureRenderTarget
603 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500604 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Greg Daniel6abda432018-02-15 14:55:00 -0500605
Robert Phillips0902c982019-07-16 07:47:56 -0400606 GrSwizzle texSwizzle = caps->getTextureSwizzle(tex->backendFormat(), colorType);
607 GrSwizzle outSwizzle = caps->getOutputSwizzle(tex->backendFormat(), colorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400608
609 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin, texSwizzle,
610 outSwizzle));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500611}
612
Brian Salomon7578f3e2018-03-07 14:39:54 -0500613sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget(
Greg Daniel8ce79912019-02-05 10:08:43 -0500614 const GrBackendRenderTarget& backendRT, GrSurfaceOrigin origin, ReleaseProc releaseProc,
615 ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500616 if (this->isAbandoned()) {
617 return nullptr;
618 }
619
Brian Salomonf7778972018-03-08 10:13:17 -0500620 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500621 GrContext* direct = fImageContext->priv().asDirectContext();
622 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500623 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500624 }
625
Brian Salomon3ec1f542019-06-17 17:54:57 +0000626 GrColorType colorType = GrPixelConfigToColorType(backendRT.config());
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400627#ifdef SK_DEBUG
Greg Danielfa55f2e2019-06-13 15:21:38 -0400628 GrPixelConfig testConfig =
Robert Phillips1e2cb442019-07-02 15:51:28 -0400629 this->caps()->validateBackendRenderTarget(backendRT, colorType);
Greg Danielfa55f2e2019-06-13 15:21:38 -0400630 SkASSERT(testConfig != kUnknown_GrPixelConfig);
631#endif
632
Robert Phillipsa41c6852019-02-07 10:44:10 -0500633 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
634
635 sk_sp<GrRenderTarget> rt = resourceProvider->wrapBackendRenderTarget(backendRT);
Brian Salomonf7778972018-03-08 10:13:17 -0500636 if (!rt) {
637 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500638 }
Greg Daniel8ce79912019-02-05 10:08:43 -0500639
Greg Daniel8ce79912019-02-05 10:08:43 -0500640 if (releaseProc) {
Brian Salomon2ca31f82019-03-05 13:28:58 -0500641 rt->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500642 }
643
Brian Salomonf7778972018-03-08 10:13:17 -0500644 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
645 SkASSERT(!rt->getUniqueKey().isValid());
646 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500647 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Brian Salomonf7778972018-03-08 10:13:17 -0500648
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400649 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(rt->backendFormat(), colorType);
650 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(rt->backendFormat(), colorType);
651
652 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(std::move(rt), origin, texSwizzle,
653 outSwizzle));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500654}
655
Brian Salomon7578f3e2018-03-07 14:39:54 -0500656sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendTextureAsRenderTarget(
657 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500658 if (this->isAbandoned()) {
659 return nullptr;
660 }
661
Brian Salomonf7778972018-03-08 10:13:17 -0500662 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500663 GrContext* direct = fImageContext->priv().asDirectContext();
664 if (!direct) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500665 return nullptr;
666 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500667
Greg Danielfa55f2e2019-06-13 15:21:38 -0400668 SkASSERT(validate_backend_format_and_config(this->caps(), backendTex.getBackendFormat(),
669 backendTex.config()));
670
Robert Phillipsa41c6852019-02-07 10:44:10 -0500671 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
672
Brian Salomonf7778972018-03-08 10:13:17 -0500673 sk_sp<GrRenderTarget> rt =
Robert Phillipsa41c6852019-02-07 10:44:10 -0500674 resourceProvider->wrapBackendTextureAsRenderTarget(backendTex, sampleCnt);
Brian Salomonf7778972018-03-08 10:13:17 -0500675 if (!rt) {
676 return nullptr;
Greg Danielf87651e2018-02-21 11:36:53 -0500677 }
Brian Salomonf7778972018-03-08 10:13:17 -0500678 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
679 SkASSERT(!rt->getUniqueKey().isValid());
Greg Danielb46add82019-01-02 14:51:29 -0500680 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500681 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielf87651e2018-02-21 11:36:53 -0500682
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400683 GrColorType colorType = GrPixelConfigToColorType(rt->config());
684 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(rt->backendFormat(), colorType);
685 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(rt->backendFormat(), colorType);
686
687 return sk_sp<GrSurfaceProxy>(new GrRenderTargetProxy(std::move(rt), origin, texSwizzle,
688 outSwizzle));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500689}
690
Greg Danielb46add82019-01-02 14:51:29 -0500691sk_sp<GrRenderTargetProxy> GrProxyProvider::wrapVulkanSecondaryCBAsRenderTarget(
692 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
693 if (this->isAbandoned()) {
694 return nullptr;
695 }
696
697 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500698 GrContext* direct = fImageContext->priv().asDirectContext();
699 if (!direct) {
Greg Danielb46add82019-01-02 14:51:29 -0500700 return nullptr;
701 }
702
Robert Phillipsa41c6852019-02-07 10:44:10 -0500703 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Danielb46add82019-01-02 14:51:29 -0500704
Robert Phillipsa41c6852019-02-07 10:44:10 -0500705 sk_sp<GrRenderTarget> rt = resourceProvider->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
706 vkInfo);
Greg Danielb46add82019-01-02 14:51:29 -0500707 if (!rt) {
708 return nullptr;
709 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500710
Greg Danielb46add82019-01-02 14:51:29 -0500711 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
712 SkASSERT(!rt->getUniqueKey().isValid());
713 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500714 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielb46add82019-01-02 14:51:29 -0500715
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400716 GrColorType colorType = GrPixelConfigToColorType(rt->config());
717 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(rt->backendFormat(), colorType);
718 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(rt->backendFormat(), colorType);
719
Greg Danielb46add82019-01-02 14:51:29 -0500720 // All Vulkan surfaces uses top left origins.
721 return sk_sp<GrRenderTargetProxy>(
722 new GrRenderTargetProxy(std::move(rt),
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400723 kTopLeft_GrSurfaceOrigin, texSwizzle, outSwizzle,
Greg Danielb46add82019-01-02 14:51:29 -0500724 GrRenderTargetProxy::WrapsVkSecondaryCB::kYes));
725}
726
Robert Phillips777707b2018-01-17 11:40:14 -0500727sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500728 const GrBackendFormat& format,
Robert Phillips777707b2018-01-17 11:40:14 -0500729 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500730 GrSurfaceOrigin origin,
Brian Salomon7226c232018-07-30 13:13:17 -0400731 GrMipMapped mipMapped,
Brian Salomon7226c232018-07-30 13:13:17 -0400732 SkBackingFit fit,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500733 SkBudgeted budgeted) {
Greg Daniel4065d452018-11-16 15:43:41 -0500734 return this->createLazyProxy(std::move(callback), format, desc, origin, mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400735 GrInternalSurfaceFlags::kNone, fit, budgeted);
Greg Daniel2a303902018-02-20 10:25:54 -0500736}
737
738sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500739 const GrBackendFormat& format,
Greg Daniel2a303902018-02-20 10:25:54 -0500740 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500741 GrSurfaceOrigin origin,
Greg Daniel2a303902018-02-20 10:25:54 -0500742 GrMipMapped mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400743 GrInternalSurfaceFlags surfaceFlags,
Brian Salomon7226c232018-07-30 13:13:17 -0400744 SkBackingFit fit,
745 SkBudgeted budgeted) {
Greg Daniela8d92112018-03-09 12:05:04 -0500746 // For non-ddl draws always make lazy proxy's single use.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500747 LazyInstantiationType lazyType = this->renderingDirectly() ? LazyInstantiationType::kSingleUse
748 : LazyInstantiationType::kMultipleUse;
Greg Daniel4065d452018-11-16 15:43:41 -0500749 return this->createLazyProxy(std::move(callback), format, desc, origin, mipMapped, surfaceFlags,
750 fit, budgeted, lazyType);
Greg Daniela8d92112018-03-09 12:05:04 -0500751}
752
753sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500754 const GrBackendFormat& format,
Greg Daniela8d92112018-03-09 12:05:04 -0500755 const GrSurfaceDesc& desc,
756 GrSurfaceOrigin origin,
757 GrMipMapped mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400758 GrInternalSurfaceFlags surfaceFlags,
Brian Salomon7226c232018-07-30 13:13:17 -0400759 SkBackingFit fit,
760 SkBudgeted budgeted,
Greg Daniela8d92112018-03-09 12:05:04 -0500761 LazyInstantiationType lazyType) {
Robert Phillips777707b2018-01-17 11:40:14 -0500762 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
763 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400764
Robert Phillipsa41c6852019-02-07 10:44:10 -0500765 if (desc.fWidth > this->caps()->maxTextureSize() ||
766 desc.fHeight > this->caps()->maxTextureSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400767 return nullptr;
768 }
769
Greg Danielfa55f2e2019-06-13 15:21:38 -0400770 SkASSERT(validate_backend_format_and_config(this->caps(), format, desc.fConfig));
Greg Daniel457469c2018-02-08 15:05:44 -0500771
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400772 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
773 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(format, colorType);
774 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(format, colorType);
775
Brian Salomon2a4f9832018-03-03 22:43:43 -0500776 return sk_sp<GrTextureProxy>(
777 SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags)
Greg Daniel4065d452018-11-16 15:43:41 -0500778 ? new GrTextureRenderTargetProxy(std::move(callback), lazyType, format, desc,
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400779 origin, mipMapped, texSwizzle, outSwizzle, fit,
780 budgeted, surfaceFlags)
Greg Daniel4065d452018-11-16 15:43:41 -0500781 : new GrTextureProxy(std::move(callback), lazyType, format, desc, origin,
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400782 mipMapped, texSwizzle, fit, budgeted, surfaceFlags));
Robert Phillips777707b2018-01-17 11:40:14 -0500783}
784
Robert Phillipse8fabb22018-02-04 14:33:21 -0500785sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500786 LazyInstantiateCallback&& callback, const GrBackendFormat& format,
787 const GrSurfaceDesc& desc, GrSurfaceOrigin origin, GrInternalSurfaceFlags surfaceFlags,
Greg Danielb085fa92019-03-05 16:55:12 -0500788 const TextureInfo* textureInfo, SkBackingFit fit, SkBudgeted budgeted,
789 bool wrapsVkSecondaryCB) {
Robert Phillipse8fabb22018-02-04 14:33:21 -0500790 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
791 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400792
Robert Phillipsa41c6852019-02-07 10:44:10 -0500793 if (desc.fWidth > this->caps()->maxRenderTargetSize() ||
794 desc.fHeight > this->caps()->maxRenderTargetSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400795 return nullptr;
796 }
797
Greg Daniel2a303902018-02-20 10:25:54 -0500798 SkASSERT(SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags));
Greg Danielfa55f2e2019-06-13 15:21:38 -0400799 SkASSERT(validate_backend_format_and_config(this->caps(), format, desc.fConfig));
Greg Daniel457469c2018-02-08 15:05:44 -0500800
801 using LazyInstantiationType = GrSurfaceProxy::LazyInstantiationType;
802 // For non-ddl draws always make lazy proxy's single use.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500803 LazyInstantiationType lazyType = this->renderingDirectly() ? LazyInstantiationType::kSingleUse
804 : LazyInstantiationType::kMultipleUse;
Greg Daniel457469c2018-02-08 15:05:44 -0500805
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400806 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
807 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(format, colorType);
808 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(format, colorType);
809
Brian Salomon7226c232018-07-30 13:13:17 -0400810 if (textureInfo) {
Greg Danielb085fa92019-03-05 16:55:12 -0500811 // Wrapped vulkan secondary command buffers don't support texturing since we won't have an
812 // actual VkImage to texture from.
813 SkASSERT(!wrapsVkSecondaryCB);
Brian Salomon7226c232018-07-30 13:13:17 -0400814 return sk_sp<GrRenderTargetProxy>(new GrTextureRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500815 std::move(callback), lazyType, format, desc, origin, textureInfo->fMipMapped,
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400816 texSwizzle, outSwizzle, fit, budgeted, surfaceFlags));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500817 }
818
Greg Danielb085fa92019-03-05 16:55:12 -0500819 GrRenderTargetProxy::WrapsVkSecondaryCB vkSCB =
820 wrapsVkSecondaryCB ? GrRenderTargetProxy::WrapsVkSecondaryCB::kYes
821 : GrRenderTargetProxy::WrapsVkSecondaryCB::kNo;
822
Brian Salomon2a4f9832018-03-03 22:43:43 -0500823 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400824 std::move(callback), lazyType, format, desc, origin, texSwizzle, outSwizzle, fit,
825 budgeted, surfaceFlags, vkSCB));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500826}
827
Chris Dalton19cc00f2019-04-15 09:06:28 -0600828sk_sp<GrTextureProxy> GrProxyProvider::MakeFullyLazyProxy(
829 LazyInstantiateCallback&& callback, const GrBackendFormat& format, Renderable renderable,
830 GrSurfaceOrigin origin, GrPixelConfig config, const GrCaps& caps, int sampleCnt) {
Greg Danielfa55f2e2019-06-13 15:21:38 -0400831 SkASSERT(validate_backend_format_and_config(&caps, format, config));
Robert Phillips777707b2018-01-17 11:40:14 -0500832 GrSurfaceDesc desc;
Robert Phillips10d17212019-04-24 14:09:10 -0400833 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNone;
Robert Phillips777707b2018-01-17 11:40:14 -0500834 if (Renderable::kYes == renderable) {
835 desc.fFlags = kRenderTarget_GrSurfaceFlag;
836 }
Robert Phillips777707b2018-01-17 11:40:14 -0500837 desc.fWidth = -1;
838 desc.fHeight = -1;
839 desc.fConfig = config;
Chris Dalton19cc00f2019-04-15 09:06:28 -0600840 desc.fSampleCnt = sampleCnt;
Robert Phillips777707b2018-01-17 11:40:14 -0500841
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400842 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
843 GrSwizzle texSwizzle = caps.getTextureSwizzle(format, colorType);
844 GrSwizzle outSwizzle = caps.getOutputSwizzle(format, colorType);
845
Chris Dalton4c458b12018-06-16 17:22:59 -0600846 return sk_sp<GrTextureProxy>(
847 (Renderable::kYes == renderable)
Brian Salomon7226c232018-07-30 13:13:17 -0400848 ? new GrTextureRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500849 std::move(callback), LazyInstantiationType::kSingleUse, format, desc,
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400850 origin, GrMipMapped::kNo, texSwizzle, outSwizzle,
851 SkBackingFit::kApprox, SkBudgeted::kYes, surfaceFlags)
Chris Dalton4c458b12018-06-16 17:22:59 -0600852 : new GrTextureProxy(std::move(callback), LazyInstantiationType::kSingleUse,
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400853 format, desc, origin, GrMipMapped::kNo, texSwizzle,
Brian Salomon7226c232018-07-30 13:13:17 -0400854 SkBackingFit::kApprox, SkBudgeted::kYes, surfaceFlags));
Robert Phillips777707b2018-01-17 11:40:14 -0500855}
856
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500857bool GrProxyProvider::IsFunctionallyExact(GrSurfaceProxy* proxy) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400858 const bool isInstantiated = proxy->isInstantiated();
Robert Phillipsdb3b9792018-06-12 15:18:00 -0400859 // A proxy is functionally exact if:
860 // it is exact (obvs)
861 // when it is instantiated it will be exact (i.e., power of two dimensions)
862 // it is already instantiated and the proxy covers the entire backing surface
863 return proxy->priv().isExact() ||
864 (!isInstantiated && SkIsPow2(proxy->width()) && SkIsPow2(proxy->height())) ||
865 (isInstantiated && proxy->worstCaseWidth() == proxy->width() &&
866 proxy->worstCaseHeight() == proxy->height());
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500867}
868
Robert Phillips427966a2018-12-20 17:20:43 -0500869void GrProxyProvider::processInvalidUniqueKey(const GrUniqueKey& key, GrTextureProxy* proxy,
870 InvalidateGPUResource invalidateGPUResource) {
Chris Dalton2de13dd2019-01-03 15:11:59 -0700871 SkASSERT(key.isValid());
872
Robert Phillips427966a2018-12-20 17:20:43 -0500873 if (!proxy) {
874 proxy = fUniquelyKeyedProxies.find(key);
875 }
Chris Dalton2de13dd2019-01-03 15:11:59 -0700876 SkASSERT(!proxy || proxy->getUniqueKey() == key);
877
878 // Locate the corresponding GrGpuResource (if it needs to be invalidated) before clearing the
879 // proxy's unique key. We must do it in this order because 'key' may alias the proxy's key.
880 sk_sp<GrGpuResource> invalidGpuResource;
881 if (InvalidateGPUResource::kYes == invalidateGPUResource) {
Brian Salomon01ceae92019-04-02 11:49:54 -0400882 GrContext* direct = fImageContext->priv().asDirectContext();
883 if (direct) {
884 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
885 invalidGpuResource = resourceProvider->findByUniqueKey<GrGpuResource>(key);
Chris Dalton2de13dd2019-01-03 15:11:59 -0700886 }
887 SkASSERT(!invalidGpuResource || invalidGpuResource->getUniqueKey() == key);
888 }
Robert Phillips427966a2018-12-20 17:20:43 -0500889
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500890 // Note: this method is called for the whole variety of GrGpuResources so often 'key'
891 // will not be in 'fUniquelyKeyedProxies'.
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500892 if (proxy) {
Robert Phillips427966a2018-12-20 17:20:43 -0500893 fUniquelyKeyedProxies.remove(key);
894 proxy->cacheAccess().clearUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500895 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500896
Chris Dalton2de13dd2019-01-03 15:11:59 -0700897 if (invalidGpuResource) {
898 invalidGpuResource->resourcePriv().removeUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500899 }
900}
901
Robert Phillipsa41c6852019-02-07 10:44:10 -0500902uint32_t GrProxyProvider::contextID() const {
903 return fImageContext->priv().contextID();
904}
905
906const GrCaps* GrProxyProvider::caps() const {
907 return fImageContext->priv().caps();
908}
909
910sk_sp<const GrCaps> GrProxyProvider::refCaps() const {
911 return fImageContext->priv().refCaps();
912}
913
Robert Phillipsa9162df2019-02-11 14:12:03 -0500914bool GrProxyProvider::isAbandoned() const {
915 return fImageContext->priv().abandoned();
916}
917
Robert Phillips0790f8a2018-09-18 13:11:03 -0400918void GrProxyProvider::orphanAllUniqueKeys() {
919 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
920 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
921 GrTextureProxy& tmp = *iter;
922
923 tmp.fProxyProvider = nullptr;
924 }
925}
926
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500927void GrProxyProvider::removeAllUniqueKeys() {
928 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
929 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
930 GrTextureProxy& tmp = *iter;
931
Chris Dalton2de13dd2019-01-03 15:11:59 -0700932 this->processInvalidUniqueKey(tmp.getUniqueKey(), &tmp, InvalidateGPUResource::kNo);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500933 }
934 SkASSERT(!fUniquelyKeyedProxies.count());
935}
Robert Phillipsa41c6852019-02-07 10:44:10 -0500936
937bool GrProxyProvider::renderingDirectly() const {
938 return fImageContext->priv().asDirectContext();
939}