blob: ee8ab36f26fc9896777575f5e12fa45a41926c05 [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);
106 sk_sp<GrTextureProxy> result;
107 if (proxy) {
Brian Salomon2c791fc2019-04-02 11:52:03 -0400108 GrResourceCache* cache = nullptr;
109 if (auto directContext = fImageContext->priv().asDirectContext()) {
110 cache = directContext->priv().getResourceCache();
111 }
112 proxy->firstRefAccess().ref(cache);
Brian Salomon01ceae92019-04-02 11:49:54 -0400113 result.reset(proxy);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500114 SkASSERT(result->origin() == origin);
115 }
116 return result;
117}
118
Robert Phillipsa41c6852019-02-07 10:44:10 -0500119///////////////////////////////////////////////////////////////////////////////
120
121#if GR_TEST_UTILS
122sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createInstantiatedProxy(
123 const GrSurfaceDesc& desc, GrSurfaceOrigin origin, SkBackingFit fit, SkBudgeted budgeted) {
124 GrContext* direct = fImageContext->priv().asDirectContext();
125 if (!direct) {
126 return nullptr;
127 }
128
129 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
130 sk_sp<GrTexture> tex;
131
132 if (SkBackingFit::kApprox == fit) {
Robert Phillips9313aa72019-04-09 18:41:27 -0400133 tex = resourceProvider->createApproxTexture(desc, GrResourceProvider::Flags::kNoPendingIO);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500134 } else {
Robert Phillips9313aa72019-04-09 18:41:27 -0400135 tex = resourceProvider->createTexture(desc, budgeted,
136 GrResourceProvider::Flags::kNoPendingIO);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500137 }
138 if (!tex) {
139 return nullptr;
140 }
141
142 return this->createWrapped(std::move(tex), origin);
143}
144
145sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createWrapped(sk_sp<GrTexture> tex,
146 GrSurfaceOrigin origin) {
147 return this->createWrapped(std::move(tex), origin);
148}
149#endif
150
Robert Phillipsadbe1322018-01-17 13:35:46 -0500151sk_sp<GrTextureProxy> GrProxyProvider::createWrapped(sk_sp<GrTexture> tex, GrSurfaceOrigin origin) {
152#ifdef SK_DEBUG
153 if (tex->getUniqueKey().isValid()) {
154 SkASSERT(!this->findProxyByUniqueKey(tex->getUniqueKey(), origin));
155 }
156#endif
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400157 GrColorType colorType = GrPixelConfigToColorType(tex->config());
158 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(tex->backendFormat(), colorType);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500159
160 if (tex->asRenderTarget()) {
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400161 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(tex->backendFormat(), colorType);
162 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin,
163 texSwizzle, outSwizzle));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500164 } else {
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400165 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin, texSwizzle));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500166 }
167}
168
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500169sk_sp<GrTextureProxy> GrProxyProvider::findOrCreateProxyByUniqueKey(const GrUniqueKey& key,
170 GrSurfaceOrigin origin) {
171 ASSERT_SINGLE_OWNER
172
173 if (this->isAbandoned()) {
174 return nullptr;
175 }
176
177 sk_sp<GrTextureProxy> result = this->findProxyByUniqueKey(key, origin);
178 if (result) {
179 return result;
180 }
181
Robert Phillipsa41c6852019-02-07 10:44:10 -0500182 GrContext* direct = fImageContext->priv().asDirectContext();
183 if (!direct) {
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500184 return nullptr;
185 }
186
Robert Phillipsa41c6852019-02-07 10:44:10 -0500187 GrResourceCache* resourceCache = direct->priv().getResourceCache();
188
189 GrGpuResource* resource = resourceCache->findAndRefUniqueResource(key);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500190 if (!resource) {
191 return nullptr;
192 }
193
194 sk_sp<GrTexture> texture(static_cast<GrSurface*>(resource)->asTexture());
195 SkASSERT(texture);
196
Robert Phillipsadbe1322018-01-17 13:35:46 -0500197 result = this->createWrapped(std::move(texture), origin);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500198 SkASSERT(result->getUniqueKey() == key);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500199 // createWrapped should've added this for us
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500200 SkASSERT(fUniquelyKeyedProxies.find(key));
201 return result;
202}
203
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500204sk_sp<GrTextureProxy> GrProxyProvider::createTextureProxy(sk_sp<SkImage> srcImage,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400205 GrSurfaceDescFlags descFlags,
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500206 int sampleCnt,
Greg Danielfb3abcd2018-02-02 15:48:33 -0500207 SkBudgeted budgeted,
Chris Daltond004e0b2018-09-27 09:28:03 -0600208 SkBackingFit fit,
209 GrInternalSurfaceFlags surfaceFlags) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500210 ASSERT_SINGLE_OWNER
211 SkASSERT(srcImage);
212
213 if (this->isAbandoned()) {
214 return nullptr;
215 }
216
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400217 const SkImageInfo& info = srcImage->imageInfo();
Brian Osmand29dcd12018-09-13 15:04:29 -0400218 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
Greg Danielf87651e2018-02-21 11:36:53 -0500219
Greg Daniel0a7aa142018-02-21 13:02:32 -0500220 if (kUnknown_GrPixelConfig == config) {
221 return nullptr;
222 }
223
Jim Van Verthc8098322019-04-16 10:50:01 -0400224 SkColorType ct = info.colorType();
Brian Osmand29dcd12018-09-13 15:04:29 -0400225 if (!this->caps()->isConfigTexturable(config)) {
226 SkBitmap copy8888;
227 if (!copy8888.tryAllocPixels(info.makeColorType(kRGBA_8888_SkColorType)) ||
228 !srcImage->readPixels(copy8888.pixmap(), 0, 0)) {
229 return nullptr;
230 }
231 copy8888.setImmutable();
232 srcImage = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode);
233 config = kRGBA_8888_GrPixelConfig;
Jim Van Verthc8098322019-04-16 10:50:01 -0400234 ct = kRGBA_8888_SkColorType;
235 }
236
237 GrBackendFormat format = this->caps()->getBackendFormatFromColorType(ct);
238 if (!format.isValid()) {
239 return nullptr;
Brian Osmand29dcd12018-09-13 15:04:29 -0400240 }
241
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400242 if (SkToBool(descFlags & kRenderTarget_GrSurfaceFlag)) {
Greg Danielf87651e2018-02-21 11:36:53 -0500243 sampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, config);
244 if (!sampleCnt) {
245 return nullptr;
246 }
247 }
248
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400249 if (SkToBool(descFlags & kRenderTarget_GrSurfaceFlag)) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500250 if (this->caps()->usesMixedSamples() && sampleCnt > 1) {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400251 surfaceFlags |= GrInternalSurfaceFlags::kMixedSampled;
Greg Daniel2a303902018-02-20 10:25:54 -0500252 }
Greg Daniel2a303902018-02-20 10:25:54 -0500253 }
254
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500255 GrSurfaceDesc desc;
256 desc.fWidth = srcImage->width();
257 desc.fHeight = srcImage->height();
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400258 desc.fFlags = descFlags;
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500259 desc.fSampleCnt = sampleCnt;
Greg Danielf87651e2018-02-21 11:36:53 -0500260 desc.fConfig = config;
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500261
262 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Robert Phillips10d17212019-04-24 14:09:10 -0400263 [desc, budgeted, srcImage, fit](GrResourceProvider* resourceProvider) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500264 SkPixmap pixMap;
265 SkAssertResult(srcImage->peekPixels(&pixMap));
266 GrMipLevel mipLevel = { pixMap.addr(), pixMap.rowBytes() };
267
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400268 return LazyInstantiationResult(resourceProvider->createTexture(
Robert Phillips10d17212019-04-24 14:09:10 -0400269 desc, budgeted, fit, mipLevel, GrResourceProvider::Flags::kNoPendingIO));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500270 },
Greg Daniel4065d452018-11-16 15:43:41 -0500271 format, desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, surfaceFlags, fit, budgeted);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500272
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400273 if (!proxy) {
274 return nullptr;
275 }
276
Robert Phillipsa41c6852019-02-07 10:44:10 -0500277 GrContext* direct = fImageContext->priv().asDirectContext();
278 if (direct) {
279 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
280
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500281 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
282 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500283 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500284 return nullptr;
285 }
286 }
Robert Phillipsc1b60662018-06-26 10:20:08 -0400287
288 SkASSERT(proxy->width() == desc.fWidth);
289 SkASSERT(proxy->height() == desc.fHeight);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500290 return proxy;
291}
292
Greg Daniel4065d452018-11-16 15:43:41 -0500293sk_sp<GrTextureProxy> GrProxyProvider::createMipMapProxy(const GrBackendFormat& format,
294 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500295 GrSurfaceOrigin origin,
Greg Daniel30815082018-02-09 16:08:30 -0500296 SkBudgeted budgeted) {
Robert Phillips579f0942018-01-08 14:53:35 -0500297 ASSERT_SINGLE_OWNER
298
299 if (this->isAbandoned()) {
300 return nullptr;
301 }
302
Greg Daniel4065d452018-11-16 15:43:41 -0500303 return this->createProxy(format, desc, origin, GrMipMapped::kYes, SkBackingFit::kExact,
304 budgeted, GrInternalSurfaceFlags::kNone);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500305}
306
Brian Osmande496652019-03-22 13:42:33 -0400307sk_sp<GrTextureProxy> GrProxyProvider::createProxyFromBitmap(const SkBitmap& bitmap,
308 GrMipMapped mipMapped) {
Brian Osman412674f2019-02-07 15:34:58 -0500309 ASSERT_SINGLE_OWNER
Brian Osman7dcc6162019-03-25 10:12:57 -0400310 SkASSERT(GrMipMapped::kNo == mipMapped || this->caps()->mipMapSupport());
Brian Osman412674f2019-02-07 15:34:58 -0500311
312 if (this->isAbandoned()) {
313 return nullptr;
314 }
315
Brian Osman2b23c4b2018-06-01 12:25:08 -0400316 if (!SkImageInfoIsValid(bitmap.info())) {
Greg Daniela4ead652018-02-07 10:21:48 -0500317 return nullptr;
318 }
319
Brian Osmande496652019-03-22 13:42:33 -0400320 ATRACE_ANDROID_FRAMEWORK("Upload %sTexture [%ux%u]",
321 GrMipMapped::kYes == mipMapped ? "MipMap " : "",
322 bitmap.width(), bitmap.height());
Greg Daniela4ead652018-02-07 10:21:48 -0500323
324 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
325 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
326 // 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 -0500327 SkCopyPixelsMode copyMode = this->renderingDirectly() ? kNever_SkCopyPixelsMode
328 : kIfMutable_SkCopyPixelsMode;
Greg Daniela4ead652018-02-07 10:21:48 -0500329 sk_sp<SkImage> baseLevel = SkMakeImageFromRasterBitmap(bitmap, copyMode);
Greg Daniela4ead652018-02-07 10:21:48 -0500330 if (!baseLevel) {
331 return nullptr;
332 }
333
Brian Osmande496652019-03-22 13:42:33 -0400334 // If mips weren't requested (or this was too small to have any), then take the fast path
335 if (GrMipMapped::kNo == mipMapped ||
336 0 == SkMipMap::ComputeLevelCount(baseLevel->width(), baseLevel->height())) {
Brian Osman7dcc6162019-03-25 10:12:57 -0400337 return this->createTextureProxy(std::move(baseLevel), kNone_GrSurfaceFlags, 1,
338 SkBudgeted::kYes, SkBackingFit::kExact);
Greg Daniela4ead652018-02-07 10:21:48 -0500339 }
340
Greg Daniel3fc5df42019-06-14 09:42:17 -0400341 SkColorType colorType = bitmap.info().colorType();
Brian Osmand29dcd12018-09-13 15:04:29 -0400342 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info());
Greg Daniel3fc5df42019-06-14 09:42:17 -0400343
Brian Osmand29dcd12018-09-13 15:04:29 -0400344 if (!this->caps()->isConfigTexturable(desc.fConfig)) {
345 SkBitmap copy8888;
346 if (!copy8888.tryAllocPixels(bitmap.info().makeColorType(kRGBA_8888_SkColorType)) ||
347 !bitmap.readPixels(copy8888.pixmap())) {
348 return nullptr;
349 }
350 copy8888.setImmutable();
351 baseLevel = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode);
352 desc.fConfig = kRGBA_8888_GrPixelConfig;
Greg Daniel3fc5df42019-06-14 09:42:17 -0400353 colorType = kRGBA_8888_SkColorType;
354 }
355
356 const GrBackendFormat format = this->caps()->getBackendFormatFromColorType(colorType);
357 if (!format.isValid()) {
358 return nullptr;
Brian Osmand29dcd12018-09-13 15:04:29 -0400359 }
360
361 SkPixmap pixmap;
362 SkAssertResult(baseLevel->peekPixels(&pixmap));
363 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(pixmap, nullptr));
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400364 if (!mipmaps) {
365 return nullptr;
366 }
367
Greg Daniela4ead652018-02-07 10:21:48 -0500368 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Osman1b97f132018-09-13 17:33:48 +0000369 [desc, baseLevel, mipmaps](GrResourceProvider* resourceProvider) {
Brian Osman1b97f132018-09-13 17:33:48 +0000370 const int mipLevelCount = mipmaps->countLevels() + 1;
371 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
372
Greg Daniela4ead652018-02-07 10:21:48 -0500373 SkPixmap pixmap;
374 SkAssertResult(baseLevel->peekPixels(&pixmap));
375
376 // DDL TODO: Instead of copying all this info into GrMipLevels we should just plumb
377 // the use of SkMipMap down through Ganesh.
378 texels[0].fPixels = pixmap.addr();
379 texels[0].fRowBytes = pixmap.rowBytes();
380
381 for (int i = 1; i < mipLevelCount; ++i) {
382 SkMipMap::Level generatedMipLevel;
383 mipmaps->getLevel(i - 1, &generatedMipLevel);
384 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
385 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
386 SkASSERT(texels[i].fPixels);
387 }
388
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400389 return LazyInstantiationResult(resourceProvider->createTexture(
390 desc, SkBudgeted::kYes, texels.get(), mipLevelCount));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500391 },
Greg Daniel4065d452018-11-16 15:43:41 -0500392 format, desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kYes, SkBackingFit::kExact,
393 SkBudgeted::kYes);
Greg Daniela4ead652018-02-07 10:21:48 -0500394
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400395 if (!proxy) {
396 return nullptr;
397 }
398
Robert Phillipsa41c6852019-02-07 10:44:10 -0500399 GrContext* direct = fImageContext->priv().asDirectContext();
400 if (direct) {
401 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Daniela4ead652018-02-07 10:21:48 -0500402 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
403 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500404 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Greg Daniela4ead652018-02-07 10:21:48 -0500405 return nullptr;
406 }
407 }
408 return proxy;
409}
410
Greg Danielfa55f2e2019-06-13 15:21:38 -0400411#ifdef SK_DEBUG
412static bool validate_backend_format_and_config(const GrCaps* caps,
413 const GrBackendFormat& format,
414 GrPixelConfig config) {
415 if (kUnknown_GrPixelConfig == config) {
416 return false;
417 }
418 SkColorType colorType = GrColorTypeToSkColorType(GrPixelConfigToColorType(config));
419 if (colorType == kUnknown_SkColorType) {
420 // We should add support for validating GrColorType with a GrBackendFormat. Currently we
421 // only support SkColorType. For now we just assume things that don't have a corresponding
422 // SkColorType are correct.
423 return true;
424 }
425 GrPixelConfig testConfig = caps->getConfigFromBackendFormat(format, colorType);
426 return testConfig != kUnknown_GrPixelConfig;
427}
428#endif
429
Greg Daniel4065d452018-11-16 15:43:41 -0500430sk_sp<GrTextureProxy> GrProxyProvider::createProxy(const GrBackendFormat& format,
431 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500432 GrSurfaceOrigin origin,
Greg Danielf6f7b672018-02-15 13:06:26 -0500433 GrMipMapped mipMapped,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500434 SkBackingFit fit,
435 SkBudgeted budgeted,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400436 GrInternalSurfaceFlags surfaceFlags) {
Greg Danielfa55f2e2019-06-13 15:21:38 -0400437 SkASSERT(validate_backend_format_and_config(this->caps(), format, desc.fConfig));
Greg Danielf6f7b672018-02-15 13:06:26 -0500438 if (GrMipMapped::kYes == mipMapped) {
439 // SkMipMap doesn't include the base level in the level count so we have to add 1
440 int mipCount = SkMipMap::ComputeLevelCount(desc.fWidth, desc.fHeight) + 1;
441 if (1 == mipCount) {
442 mipMapped = GrMipMapped::kNo;
443 }
444 }
445
446 if (!this->caps()->validateSurfaceDesc(desc, mipMapped)) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500447 return nullptr;
448 }
Brian Salomon3a2cc2c2018-02-03 00:25:12 +0000449 GrSurfaceDesc copyDesc = desc;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500450 if (desc.fFlags & kRenderTarget_GrSurfaceFlag) {
451 copyDesc.fSampleCnt =
452 this->caps()->getRenderTargetSampleCount(desc.fSampleCnt, desc.fConfig);
453 }
Brian Salomon3a2cc2c2018-02-03 00:25:12 +0000454
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400455 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
456 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(format, colorType);
457
Brian Salomonbdecacf2018-02-02 20:32:49 -0500458 if (copyDesc.fFlags & kRenderTarget_GrSurfaceFlag) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500459 // We know anything we instantiate later from this deferred path will be
460 // both texturable and renderable
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400461 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(format, colorType);
Greg Daniel4065d452018-11-16 15:43:41 -0500462 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(*this->caps(), format, copyDesc,
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400463 origin, mipMapped, texSwizzle,
464 outSwizzle, fit, budgeted,
465 surfaceFlags));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500466 }
467
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400468 return sk_sp<GrTextureProxy>(new GrTextureProxy(format, copyDesc, origin, mipMapped, texSwizzle,
Brian Salomon7226c232018-07-30 13:13:17 -0400469 fit, budgeted, surfaceFlags));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500470}
471
Jim Van Verthee06b332019-01-18 10:36:32 -0500472sk_sp<GrTextureProxy> GrProxyProvider::createProxy(sk_sp<SkData> data, const GrSurfaceDesc& desc) {
473 if (!this->caps()->isConfigTexturable(desc.fConfig)) {
474 return nullptr;
475 }
476
477 const GrColorType ct = GrPixelConfigToColorType(desc.fConfig);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500478 const GrBackendFormat format =
479 this->caps()->getBackendFormatFromGrColorType(ct, GrSRGBEncoded::kNo);
Jim Van Verthee06b332019-01-18 10:36:32 -0500480
481 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
482 [desc, data](GrResourceProvider* resourceProvider) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500483 GrMipLevel texels;
484 texels.fPixels = data->data();
485 texels.fRowBytes = GrBytesPerPixel(desc.fConfig)*desc.fWidth;
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400486 return LazyInstantiationResult(
487 resourceProvider->createTexture(desc, SkBudgeted::kYes, &texels, 1));
Jim Van Verthee06b332019-01-18 10:36:32 -0500488 },
489 format, desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, SkBackingFit::kExact,
490 SkBudgeted::kYes);
491
492 if (!proxy) {
493 return nullptr;
494 }
495
Robert Phillipsa41c6852019-02-07 10:44:10 -0500496 GrContext* direct = fImageContext->priv().asDirectContext();
497 if (direct) {
498 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Jim Van Verthee06b332019-01-18 10:36:32 -0500499 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
500 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500501 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500502 return nullptr;
503 }
504 }
505 return proxy;
506}
507
Brian Salomon7578f3e2018-03-07 14:39:54 -0500508sk_sp<GrTextureProxy> GrProxyProvider::wrapBackendTexture(const GrBackendTexture& backendTex,
509 GrSurfaceOrigin origin,
510 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500511 GrWrapCacheable cacheable,
Brian Salomonc67c31c2018-12-06 10:00:03 -0500512 GrIOType ioType,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500513 ReleaseProc releaseProc,
514 ReleaseContext releaseCtx) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500515 SkASSERT(ioType != kWrite_GrIOType);
Robert Phillipsf9bec202018-01-16 09:21:01 -0500516 if (this->isAbandoned()) {
517 return nullptr;
518 }
519
Brian Salomonf7778972018-03-08 10:13:17 -0500520 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500521 GrContext* direct = fImageContext->priv().asDirectContext();
522 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500523 return nullptr;
524 }
525
Greg Danielfa55f2e2019-06-13 15:21:38 -0400526#ifdef SK_DEBUG
527 SkASSERT(validate_backend_format_and_config(this->caps(), backendTex.getBackendFormat(),
528 backendTex.config()));
529#endif
530
Robert Phillipsa41c6852019-02-07 10:44:10 -0500531 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
532
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500533 sk_sp<GrTexture> tex =
Robert Phillipsa41c6852019-02-07 10:44:10 -0500534 resourceProvider->wrapBackendTexture(backendTex, ownership, cacheable, ioType);
Brian Salomonf7778972018-03-08 10:13:17 -0500535 if (!tex) {
536 return nullptr;
537 }
Robert Phillipsadbe1322018-01-17 13:35:46 -0500538
Greg Daniel6a0176b2018-01-30 09:28:44 -0500539 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500540 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel6a0176b2018-01-30 09:28:44 -0500541 }
542
Brian Salomonf7778972018-03-08 10:13:17 -0500543 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
544 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500545 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500546
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400547 GrColorType colorType = GrPixelConfigToColorType(tex->config());
548 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(tex->backendFormat(), colorType);
549
550 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin, texSwizzle));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500551}
552
Brian Salomon7578f3e2018-03-07 14:39:54 -0500553sk_sp<GrTextureProxy> GrProxyProvider::wrapRenderableBackendTexture(
Brian Salomon02bd2952018-03-07 15:20:21 -0500554 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt,
Greg Daniel8ce79912019-02-05 10:08:43 -0500555 GrWrapOwnership ownership, GrWrapCacheable cacheable, ReleaseProc releaseProc,
556 ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500557 if (this->isAbandoned()) {
558 return nullptr;
559 }
560
Brian Salomonf7778972018-03-08 10:13:17 -0500561 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500562 GrContext* direct = fImageContext->priv().asDirectContext();
563 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500564 return nullptr;
565 }
566
Greg Danielfa55f2e2019-06-13 15:21:38 -0400567 SkASSERT(validate_backend_format_and_config(this->caps(), backendTex.getBackendFormat(),
568 backendTex.config()));
569
Robert Phillipsa41c6852019-02-07 10:44:10 -0500570 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
571
Greg Daniel6abda432018-02-15 14:55:00 -0500572 sampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config());
Greg Danielf87651e2018-02-21 11:36:53 -0500573 if (!sampleCnt) {
574 return nullptr;
575 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500576
Robert Phillipsa41c6852019-02-07 10:44:10 -0500577 sk_sp<GrTexture> tex = resourceProvider->wrapRenderableBackendTexture(backendTex, sampleCnt,
578 ownership, cacheable);
Brian Salomonf7778972018-03-08 10:13:17 -0500579 if (!tex) {
580 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500581 }
582
Greg Daniel8ce79912019-02-05 10:08:43 -0500583 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500584 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500585 }
586
Brian Salomonf7778972018-03-08 10:13:17 -0500587 SkASSERT(tex->asRenderTarget()); // A GrTextureRenderTarget
588 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500589 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Greg Daniel6abda432018-02-15 14:55:00 -0500590
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400591 GrColorType colorType = GrPixelConfigToColorType(tex->config());
592 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(tex->backendFormat(), colorType);
593 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(tex->backendFormat(), colorType);
594
595 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin, texSwizzle,
596 outSwizzle));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500597}
598
Brian Salomon7578f3e2018-03-07 14:39:54 -0500599sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget(
Greg Daniel8ce79912019-02-05 10:08:43 -0500600 const GrBackendRenderTarget& backendRT, GrSurfaceOrigin origin, ReleaseProc releaseProc,
601 ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500602 if (this->isAbandoned()) {
603 return nullptr;
604 }
605
Brian Salomonf7778972018-03-08 10:13:17 -0500606 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500607 GrContext* direct = fImageContext->priv().asDirectContext();
608 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500609 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500610 }
611
Brian Salomon3ec1f542019-06-17 17:54:57 +0000612 GrColorType colorType = GrPixelConfigToColorType(backendRT.config());
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400613#ifdef SK_DEBUG
Greg Danielfa55f2e2019-06-13 15:21:38 -0400614 GrPixelConfig testConfig =
615 this->caps()->validateBackendRenderTarget(backendRT,
616 GrColorTypeToSkColorType(colorType));
617 SkASSERT(testConfig != kUnknown_GrPixelConfig);
618#endif
619
Robert Phillipsa41c6852019-02-07 10:44:10 -0500620 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
621
622 sk_sp<GrRenderTarget> rt = resourceProvider->wrapBackendRenderTarget(backendRT);
Brian Salomonf7778972018-03-08 10:13:17 -0500623 if (!rt) {
624 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500625 }
Greg Daniel8ce79912019-02-05 10:08:43 -0500626
Greg Daniel8ce79912019-02-05 10:08:43 -0500627 if (releaseProc) {
Brian Salomon2ca31f82019-03-05 13:28:58 -0500628 rt->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500629 }
630
Brian Salomonf7778972018-03-08 10:13:17 -0500631 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
632 SkASSERT(!rt->getUniqueKey().isValid());
633 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500634 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Brian Salomonf7778972018-03-08 10:13:17 -0500635
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400636 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(rt->backendFormat(), colorType);
637 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(rt->backendFormat(), colorType);
638
639 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(std::move(rt), origin, texSwizzle,
640 outSwizzle));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500641}
642
Brian Salomon7578f3e2018-03-07 14:39:54 -0500643sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendTextureAsRenderTarget(
644 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500645 if (this->isAbandoned()) {
646 return nullptr;
647 }
648
Brian Salomonf7778972018-03-08 10:13:17 -0500649 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500650 GrContext* direct = fImageContext->priv().asDirectContext();
651 if (!direct) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500652 return nullptr;
653 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500654
Greg Danielfa55f2e2019-06-13 15:21:38 -0400655 SkASSERT(validate_backend_format_and_config(this->caps(), backendTex.getBackendFormat(),
656 backendTex.config()));
657
Robert Phillipsa41c6852019-02-07 10:44:10 -0500658 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
659
Brian Salomonf7778972018-03-08 10:13:17 -0500660 sk_sp<GrRenderTarget> rt =
Robert Phillipsa41c6852019-02-07 10:44:10 -0500661 resourceProvider->wrapBackendTextureAsRenderTarget(backendTex, sampleCnt);
Brian Salomonf7778972018-03-08 10:13:17 -0500662 if (!rt) {
663 return nullptr;
Greg Danielf87651e2018-02-21 11:36:53 -0500664 }
Brian Salomonf7778972018-03-08 10:13:17 -0500665 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
666 SkASSERT(!rt->getUniqueKey().isValid());
Greg Danielb46add82019-01-02 14:51:29 -0500667 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500668 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielf87651e2018-02-21 11:36:53 -0500669
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400670 GrColorType colorType = GrPixelConfigToColorType(rt->config());
671 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(rt->backendFormat(), colorType);
672 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(rt->backendFormat(), colorType);
673
674 return sk_sp<GrSurfaceProxy>(new GrRenderTargetProxy(std::move(rt), origin, texSwizzle,
675 outSwizzle));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500676}
677
Greg Danielb46add82019-01-02 14:51:29 -0500678sk_sp<GrRenderTargetProxy> GrProxyProvider::wrapVulkanSecondaryCBAsRenderTarget(
679 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
680 if (this->isAbandoned()) {
681 return nullptr;
682 }
683
684 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500685 GrContext* direct = fImageContext->priv().asDirectContext();
686 if (!direct) {
Greg Danielb46add82019-01-02 14:51:29 -0500687 return nullptr;
688 }
689
Robert Phillipsa41c6852019-02-07 10:44:10 -0500690 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Danielb46add82019-01-02 14:51:29 -0500691
Robert Phillipsa41c6852019-02-07 10:44:10 -0500692 sk_sp<GrRenderTarget> rt = resourceProvider->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
693 vkInfo);
Greg Danielb46add82019-01-02 14:51:29 -0500694 if (!rt) {
695 return nullptr;
696 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500697
Greg Danielb46add82019-01-02 14:51:29 -0500698 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
699 SkASSERT(!rt->getUniqueKey().isValid());
700 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500701 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielb46add82019-01-02 14:51:29 -0500702
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400703 GrColorType colorType = GrPixelConfigToColorType(rt->config());
704 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(rt->backendFormat(), colorType);
705 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(rt->backendFormat(), colorType);
706
Greg Danielb46add82019-01-02 14:51:29 -0500707 // All Vulkan surfaces uses top left origins.
708 return sk_sp<GrRenderTargetProxy>(
709 new GrRenderTargetProxy(std::move(rt),
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400710 kTopLeft_GrSurfaceOrigin, texSwizzle, outSwizzle,
Greg Danielb46add82019-01-02 14:51:29 -0500711 GrRenderTargetProxy::WrapsVkSecondaryCB::kYes));
712}
713
Robert Phillips777707b2018-01-17 11:40:14 -0500714sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500715 const GrBackendFormat& format,
Robert Phillips777707b2018-01-17 11:40:14 -0500716 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500717 GrSurfaceOrigin origin,
Brian Salomon7226c232018-07-30 13:13:17 -0400718 GrMipMapped mipMapped,
Brian Salomon7226c232018-07-30 13:13:17 -0400719 SkBackingFit fit,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500720 SkBudgeted budgeted) {
Greg Daniel4065d452018-11-16 15:43:41 -0500721 return this->createLazyProxy(std::move(callback), format, desc, origin, mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400722 GrInternalSurfaceFlags::kNone, fit, budgeted);
Greg Daniel2a303902018-02-20 10:25:54 -0500723}
724
725sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500726 const GrBackendFormat& format,
Greg Daniel2a303902018-02-20 10:25:54 -0500727 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500728 GrSurfaceOrigin origin,
Greg Daniel2a303902018-02-20 10:25:54 -0500729 GrMipMapped mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400730 GrInternalSurfaceFlags surfaceFlags,
Brian Salomon7226c232018-07-30 13:13:17 -0400731 SkBackingFit fit,
732 SkBudgeted budgeted) {
Greg Daniela8d92112018-03-09 12:05:04 -0500733 // For non-ddl draws always make lazy proxy's single use.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500734 LazyInstantiationType lazyType = this->renderingDirectly() ? LazyInstantiationType::kSingleUse
735 : LazyInstantiationType::kMultipleUse;
Greg Daniel4065d452018-11-16 15:43:41 -0500736 return this->createLazyProxy(std::move(callback), format, desc, origin, mipMapped, surfaceFlags,
737 fit, budgeted, lazyType);
Greg Daniela8d92112018-03-09 12:05:04 -0500738}
739
740sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500741 const GrBackendFormat& format,
Greg Daniela8d92112018-03-09 12:05:04 -0500742 const GrSurfaceDesc& desc,
743 GrSurfaceOrigin origin,
744 GrMipMapped mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400745 GrInternalSurfaceFlags surfaceFlags,
Brian Salomon7226c232018-07-30 13:13:17 -0400746 SkBackingFit fit,
747 SkBudgeted budgeted,
Greg Daniela8d92112018-03-09 12:05:04 -0500748 LazyInstantiationType lazyType) {
Robert Phillips777707b2018-01-17 11:40:14 -0500749 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
750 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400751
Robert Phillipsa41c6852019-02-07 10:44:10 -0500752 if (desc.fWidth > this->caps()->maxTextureSize() ||
753 desc.fHeight > this->caps()->maxTextureSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400754 return nullptr;
755 }
756
Greg Danielfa55f2e2019-06-13 15:21:38 -0400757 SkASSERT(validate_backend_format_and_config(this->caps(), format, desc.fConfig));
Greg Daniel457469c2018-02-08 15:05:44 -0500758
Greg Daniel2a303902018-02-20 10:25:54 -0500759#ifdef SK_DEBUG
760 if (SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags)) {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400761 if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kMixedSampled)) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500762 SkASSERT(this->caps()->usesMixedSamples() && desc.fSampleCnt > 1);
Greg Daniel2a303902018-02-20 10:25:54 -0500763 }
Greg Daniel2a303902018-02-20 10:25:54 -0500764 }
765#endif
766
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400767 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
768 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(format, colorType);
769 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(format, colorType);
770
Brian Salomon2a4f9832018-03-03 22:43:43 -0500771 return sk_sp<GrTextureProxy>(
772 SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags)
Greg Daniel4065d452018-11-16 15:43:41 -0500773 ? new GrTextureRenderTargetProxy(std::move(callback), lazyType, format, desc,
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400774 origin, mipMapped, texSwizzle, outSwizzle, fit,
775 budgeted, surfaceFlags)
Greg Daniel4065d452018-11-16 15:43:41 -0500776 : new GrTextureProxy(std::move(callback), lazyType, format, desc, origin,
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400777 mipMapped, texSwizzle, fit, budgeted, surfaceFlags));
Robert Phillips777707b2018-01-17 11:40:14 -0500778}
779
Robert Phillipse8fabb22018-02-04 14:33:21 -0500780sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500781 LazyInstantiateCallback&& callback, const GrBackendFormat& format,
782 const GrSurfaceDesc& desc, GrSurfaceOrigin origin, GrInternalSurfaceFlags surfaceFlags,
Greg Danielb085fa92019-03-05 16:55:12 -0500783 const TextureInfo* textureInfo, SkBackingFit fit, SkBudgeted budgeted,
784 bool wrapsVkSecondaryCB) {
Robert Phillipse8fabb22018-02-04 14:33:21 -0500785 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
786 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400787
Robert Phillipsa41c6852019-02-07 10:44:10 -0500788 if (desc.fWidth > this->caps()->maxRenderTargetSize() ||
789 desc.fHeight > this->caps()->maxRenderTargetSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400790 return nullptr;
791 }
792
Greg Daniel2a303902018-02-20 10:25:54 -0500793 SkASSERT(SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags));
Greg Danielfa55f2e2019-06-13 15:21:38 -0400794 SkASSERT(validate_backend_format_and_config(this->caps(), format, desc.fConfig));
Greg Daniel457469c2018-02-08 15:05:44 -0500795
Greg Daniel2a303902018-02-20 10:25:54 -0500796#ifdef SK_DEBUG
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400797 if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kMixedSampled)) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500798 SkASSERT(this->caps()->usesMixedSamples() && desc.fSampleCnt > 1);
Greg Daniel2a303902018-02-20 10:25:54 -0500799 }
Greg Daniel2a303902018-02-20 10:25:54 -0500800#endif
801
Greg Daniel457469c2018-02-08 15:05:44 -0500802 using LazyInstantiationType = GrSurfaceProxy::LazyInstantiationType;
803 // For non-ddl draws always make lazy proxy's single use.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500804 LazyInstantiationType lazyType = this->renderingDirectly() ? LazyInstantiationType::kSingleUse
805 : LazyInstantiationType::kMultipleUse;
Greg Daniel457469c2018-02-08 15:05:44 -0500806
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400807 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
808 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(format, colorType);
809 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(format, colorType);
810
Brian Salomon7226c232018-07-30 13:13:17 -0400811 if (textureInfo) {
Greg Danielb085fa92019-03-05 16:55:12 -0500812 // Wrapped vulkan secondary command buffers don't support texturing since we won't have an
813 // actual VkImage to texture from.
814 SkASSERT(!wrapsVkSecondaryCB);
Brian Salomon7226c232018-07-30 13:13:17 -0400815 return sk_sp<GrRenderTargetProxy>(new GrTextureRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500816 std::move(callback), lazyType, format, desc, origin, textureInfo->fMipMapped,
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400817 texSwizzle, outSwizzle, fit, budgeted, surfaceFlags));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500818 }
819
Greg Danielb085fa92019-03-05 16:55:12 -0500820 GrRenderTargetProxy::WrapsVkSecondaryCB vkSCB =
821 wrapsVkSecondaryCB ? GrRenderTargetProxy::WrapsVkSecondaryCB::kYes
822 : GrRenderTargetProxy::WrapsVkSecondaryCB::kNo;
823
Brian Salomon2a4f9832018-03-03 22:43:43 -0500824 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400825 std::move(callback), lazyType, format, desc, origin, texSwizzle, outSwizzle, fit,
826 budgeted, surfaceFlags, vkSCB));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500827}
828
Chris Dalton19cc00f2019-04-15 09:06:28 -0600829sk_sp<GrTextureProxy> GrProxyProvider::MakeFullyLazyProxy(
830 LazyInstantiateCallback&& callback, const GrBackendFormat& format, Renderable renderable,
831 GrSurfaceOrigin origin, GrPixelConfig config, const GrCaps& caps, int sampleCnt) {
Greg Danielfa55f2e2019-06-13 15:21:38 -0400832 SkASSERT(validate_backend_format_and_config(&caps, format, config));
Robert Phillips777707b2018-01-17 11:40:14 -0500833 GrSurfaceDesc desc;
Robert Phillips10d17212019-04-24 14:09:10 -0400834 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNone;
Robert Phillips777707b2018-01-17 11:40:14 -0500835 if (Renderable::kYes == renderable) {
836 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Chris Dalton19cc00f2019-04-15 09:06:28 -0600837 if (sampleCnt > 1 && caps.usesMixedSamples()) {
838 surfaceFlags |= GrInternalSurfaceFlags::kMixedSampled;
839 }
Robert Phillips777707b2018-01-17 11:40:14 -0500840 }
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}