blob: 1331fe4ec343abf7ee6a484b2f7191bde447081c [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
157
158 if (tex->asRenderTarget()) {
Brian Salomon3ec1f542019-06-17 17:54:57 +0000159 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500160 } else {
Brian Salomon3ec1f542019-06-17 17:54:57 +0000161 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500162 }
163}
164
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500165sk_sp<GrTextureProxy> GrProxyProvider::findOrCreateProxyByUniqueKey(const GrUniqueKey& key,
166 GrSurfaceOrigin origin) {
167 ASSERT_SINGLE_OWNER
168
169 if (this->isAbandoned()) {
170 return nullptr;
171 }
172
173 sk_sp<GrTextureProxy> result = this->findProxyByUniqueKey(key, origin);
174 if (result) {
175 return result;
176 }
177
Robert Phillipsa41c6852019-02-07 10:44:10 -0500178 GrContext* direct = fImageContext->priv().asDirectContext();
179 if (!direct) {
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500180 return nullptr;
181 }
182
Robert Phillipsa41c6852019-02-07 10:44:10 -0500183 GrResourceCache* resourceCache = direct->priv().getResourceCache();
184
185 GrGpuResource* resource = resourceCache->findAndRefUniqueResource(key);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500186 if (!resource) {
187 return nullptr;
188 }
189
190 sk_sp<GrTexture> texture(static_cast<GrSurface*>(resource)->asTexture());
191 SkASSERT(texture);
192
Robert Phillipsadbe1322018-01-17 13:35:46 -0500193 result = this->createWrapped(std::move(texture), origin);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500194 SkASSERT(result->getUniqueKey() == key);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500195 // createWrapped should've added this for us
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500196 SkASSERT(fUniquelyKeyedProxies.find(key));
197 return result;
198}
199
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500200sk_sp<GrTextureProxy> GrProxyProvider::createTextureProxy(sk_sp<SkImage> srcImage,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400201 GrSurfaceDescFlags descFlags,
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500202 int sampleCnt,
Greg Danielfb3abcd2018-02-02 15:48:33 -0500203 SkBudgeted budgeted,
Chris Daltond004e0b2018-09-27 09:28:03 -0600204 SkBackingFit fit,
205 GrInternalSurfaceFlags surfaceFlags) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500206 ASSERT_SINGLE_OWNER
207 SkASSERT(srcImage);
208
209 if (this->isAbandoned()) {
210 return nullptr;
211 }
212
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400213 const SkImageInfo& info = srcImage->imageInfo();
Brian Osmand29dcd12018-09-13 15:04:29 -0400214 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
Greg Danielf87651e2018-02-21 11:36:53 -0500215
Greg Daniel0a7aa142018-02-21 13:02:32 -0500216 if (kUnknown_GrPixelConfig == config) {
217 return nullptr;
218 }
219
Jim Van Verthc8098322019-04-16 10:50:01 -0400220 SkColorType ct = info.colorType();
Brian Osmand29dcd12018-09-13 15:04:29 -0400221 if (!this->caps()->isConfigTexturable(config)) {
222 SkBitmap copy8888;
223 if (!copy8888.tryAllocPixels(info.makeColorType(kRGBA_8888_SkColorType)) ||
224 !srcImage->readPixels(copy8888.pixmap(), 0, 0)) {
225 return nullptr;
226 }
227 copy8888.setImmutable();
228 srcImage = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode);
229 config = kRGBA_8888_GrPixelConfig;
Jim Van Verthc8098322019-04-16 10:50:01 -0400230 ct = kRGBA_8888_SkColorType;
231 }
232
233 GrBackendFormat format = this->caps()->getBackendFormatFromColorType(ct);
234 if (!format.isValid()) {
235 return nullptr;
Brian Osmand29dcd12018-09-13 15:04:29 -0400236 }
237
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400238 if (SkToBool(descFlags & kRenderTarget_GrSurfaceFlag)) {
Greg Danielf87651e2018-02-21 11:36:53 -0500239 sampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, config);
240 if (!sampleCnt) {
241 return nullptr;
242 }
243 }
244
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400245 if (SkToBool(descFlags & kRenderTarget_GrSurfaceFlag)) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500246 if (this->caps()->usesMixedSamples() && sampleCnt > 1) {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400247 surfaceFlags |= GrInternalSurfaceFlags::kMixedSampled;
Greg Daniel2a303902018-02-20 10:25:54 -0500248 }
Greg Daniel2a303902018-02-20 10:25:54 -0500249 }
250
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500251 GrSurfaceDesc desc;
252 desc.fWidth = srcImage->width();
253 desc.fHeight = srcImage->height();
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400254 desc.fFlags = descFlags;
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500255 desc.fSampleCnt = sampleCnt;
Greg Danielf87651e2018-02-21 11:36:53 -0500256 desc.fConfig = config;
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500257
258 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Robert Phillips10d17212019-04-24 14:09:10 -0400259 [desc, budgeted, srcImage, fit](GrResourceProvider* resourceProvider) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500260 SkPixmap pixMap;
261 SkAssertResult(srcImage->peekPixels(&pixMap));
262 GrMipLevel mipLevel = { pixMap.addr(), pixMap.rowBytes() };
263
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400264 return LazyInstantiationResult(resourceProvider->createTexture(
Robert Phillips10d17212019-04-24 14:09:10 -0400265 desc, budgeted, fit, mipLevel, GrResourceProvider::Flags::kNoPendingIO));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500266 },
Greg Daniel4065d452018-11-16 15:43:41 -0500267 format, desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, surfaceFlags, fit, budgeted);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500268
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400269 if (!proxy) {
270 return nullptr;
271 }
272
Robert Phillipsa41c6852019-02-07 10:44:10 -0500273 GrContext* direct = fImageContext->priv().asDirectContext();
274 if (direct) {
275 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
276
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500277 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
278 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500279 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500280 return nullptr;
281 }
282 }
Robert Phillipsc1b60662018-06-26 10:20:08 -0400283
284 SkASSERT(proxy->width() == desc.fWidth);
285 SkASSERT(proxy->height() == desc.fHeight);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500286 return proxy;
287}
288
Greg Daniel4065d452018-11-16 15:43:41 -0500289sk_sp<GrTextureProxy> GrProxyProvider::createMipMapProxy(const GrBackendFormat& format,
290 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500291 GrSurfaceOrigin origin,
Greg Daniel30815082018-02-09 16:08:30 -0500292 SkBudgeted budgeted) {
Robert Phillips579f0942018-01-08 14:53:35 -0500293 ASSERT_SINGLE_OWNER
294
295 if (this->isAbandoned()) {
296 return nullptr;
297 }
298
Greg Daniel4065d452018-11-16 15:43:41 -0500299 return this->createProxy(format, desc, origin, GrMipMapped::kYes, SkBackingFit::kExact,
300 budgeted, GrInternalSurfaceFlags::kNone);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500301}
302
Brian Osmande496652019-03-22 13:42:33 -0400303sk_sp<GrTextureProxy> GrProxyProvider::createProxyFromBitmap(const SkBitmap& bitmap,
304 GrMipMapped mipMapped) {
Brian Osman412674f2019-02-07 15:34:58 -0500305 ASSERT_SINGLE_OWNER
Brian Osman7dcc6162019-03-25 10:12:57 -0400306 SkASSERT(GrMipMapped::kNo == mipMapped || this->caps()->mipMapSupport());
Brian Osman412674f2019-02-07 15:34:58 -0500307
308 if (this->isAbandoned()) {
309 return nullptr;
310 }
311
Brian Osman2b23c4b2018-06-01 12:25:08 -0400312 if (!SkImageInfoIsValid(bitmap.info())) {
Greg Daniela4ead652018-02-07 10:21:48 -0500313 return nullptr;
314 }
315
Brian Osmande496652019-03-22 13:42:33 -0400316 ATRACE_ANDROID_FRAMEWORK("Upload %sTexture [%ux%u]",
317 GrMipMapped::kYes == mipMapped ? "MipMap " : "",
318 bitmap.width(), bitmap.height());
Greg Daniela4ead652018-02-07 10:21:48 -0500319
320 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
321 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
322 // 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 -0500323 SkCopyPixelsMode copyMode = this->renderingDirectly() ? kNever_SkCopyPixelsMode
324 : kIfMutable_SkCopyPixelsMode;
Greg Daniela4ead652018-02-07 10:21:48 -0500325 sk_sp<SkImage> baseLevel = SkMakeImageFromRasterBitmap(bitmap, copyMode);
Greg Daniela4ead652018-02-07 10:21:48 -0500326 if (!baseLevel) {
327 return nullptr;
328 }
329
Brian Osmande496652019-03-22 13:42:33 -0400330 // If mips weren't requested (or this was too small to have any), then take the fast path
331 if (GrMipMapped::kNo == mipMapped ||
332 0 == SkMipMap::ComputeLevelCount(baseLevel->width(), baseLevel->height())) {
Brian Osman7dcc6162019-03-25 10:12:57 -0400333 return this->createTextureProxy(std::move(baseLevel), kNone_GrSurfaceFlags, 1,
334 SkBudgeted::kYes, SkBackingFit::kExact);
Greg Daniela4ead652018-02-07 10:21:48 -0500335 }
336
Greg Daniel3fc5df42019-06-14 09:42:17 -0400337 SkColorType colorType = bitmap.info().colorType();
Brian Osmand29dcd12018-09-13 15:04:29 -0400338 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info());
Greg Daniel3fc5df42019-06-14 09:42:17 -0400339
Brian Osmand29dcd12018-09-13 15:04:29 -0400340 if (!this->caps()->isConfigTexturable(desc.fConfig)) {
341 SkBitmap copy8888;
342 if (!copy8888.tryAllocPixels(bitmap.info().makeColorType(kRGBA_8888_SkColorType)) ||
343 !bitmap.readPixels(copy8888.pixmap())) {
344 return nullptr;
345 }
346 copy8888.setImmutable();
347 baseLevel = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode);
348 desc.fConfig = kRGBA_8888_GrPixelConfig;
Greg Daniel3fc5df42019-06-14 09:42:17 -0400349 colorType = kRGBA_8888_SkColorType;
350 }
351
352 const GrBackendFormat format = this->caps()->getBackendFormatFromColorType(colorType);
353 if (!format.isValid()) {
354 return nullptr;
Brian Osmand29dcd12018-09-13 15:04:29 -0400355 }
356
357 SkPixmap pixmap;
358 SkAssertResult(baseLevel->peekPixels(&pixmap));
359 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(pixmap, nullptr));
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400360 if (!mipmaps) {
361 return nullptr;
362 }
363
Greg Daniela4ead652018-02-07 10:21:48 -0500364 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Osman1b97f132018-09-13 17:33:48 +0000365 [desc, baseLevel, mipmaps](GrResourceProvider* resourceProvider) {
Brian Osman1b97f132018-09-13 17:33:48 +0000366 const int mipLevelCount = mipmaps->countLevels() + 1;
367 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
368
Greg Daniela4ead652018-02-07 10:21:48 -0500369 SkPixmap pixmap;
370 SkAssertResult(baseLevel->peekPixels(&pixmap));
371
372 // DDL TODO: Instead of copying all this info into GrMipLevels we should just plumb
373 // the use of SkMipMap down through Ganesh.
374 texels[0].fPixels = pixmap.addr();
375 texels[0].fRowBytes = pixmap.rowBytes();
376
377 for (int i = 1; i < mipLevelCount; ++i) {
378 SkMipMap::Level generatedMipLevel;
379 mipmaps->getLevel(i - 1, &generatedMipLevel);
380 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
381 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
382 SkASSERT(texels[i].fPixels);
383 }
384
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400385 return LazyInstantiationResult(resourceProvider->createTexture(
386 desc, SkBudgeted::kYes, texels.get(), mipLevelCount));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500387 },
Greg Daniel4065d452018-11-16 15:43:41 -0500388 format, desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kYes, SkBackingFit::kExact,
389 SkBudgeted::kYes);
Greg Daniela4ead652018-02-07 10:21:48 -0500390
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400391 if (!proxy) {
392 return nullptr;
393 }
394
Robert Phillipsa41c6852019-02-07 10:44:10 -0500395 GrContext* direct = fImageContext->priv().asDirectContext();
396 if (direct) {
397 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Daniela4ead652018-02-07 10:21:48 -0500398 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
399 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500400 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Greg Daniela4ead652018-02-07 10:21:48 -0500401 return nullptr;
402 }
403 }
404 return proxy;
405}
406
Greg Danielfa55f2e2019-06-13 15:21:38 -0400407#ifdef SK_DEBUG
408static bool validate_backend_format_and_config(const GrCaps* caps,
409 const GrBackendFormat& format,
410 GrPixelConfig config) {
411 if (kUnknown_GrPixelConfig == config) {
412 return false;
413 }
414 SkColorType colorType = GrColorTypeToSkColorType(GrPixelConfigToColorType(config));
415 if (colorType == kUnknown_SkColorType) {
416 // We should add support for validating GrColorType with a GrBackendFormat. Currently we
417 // only support SkColorType. For now we just assume things that don't have a corresponding
418 // SkColorType are correct.
419 return true;
420 }
421 GrPixelConfig testConfig = caps->getConfigFromBackendFormat(format, colorType);
422 return testConfig != kUnknown_GrPixelConfig;
423}
424#endif
425
Greg Daniel4065d452018-11-16 15:43:41 -0500426sk_sp<GrTextureProxy> GrProxyProvider::createProxy(const GrBackendFormat& format,
427 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500428 GrSurfaceOrigin origin,
Greg Danielf6f7b672018-02-15 13:06:26 -0500429 GrMipMapped mipMapped,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500430 SkBackingFit fit,
431 SkBudgeted budgeted,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400432 GrInternalSurfaceFlags surfaceFlags) {
Greg Danielfa55f2e2019-06-13 15:21:38 -0400433 SkASSERT(validate_backend_format_and_config(this->caps(), format, desc.fConfig));
Greg Danielf6f7b672018-02-15 13:06:26 -0500434 if (GrMipMapped::kYes == mipMapped) {
435 // SkMipMap doesn't include the base level in the level count so we have to add 1
436 int mipCount = SkMipMap::ComputeLevelCount(desc.fWidth, desc.fHeight) + 1;
437 if (1 == mipCount) {
438 mipMapped = GrMipMapped::kNo;
439 }
440 }
441
442 if (!this->caps()->validateSurfaceDesc(desc, mipMapped)) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500443 return nullptr;
444 }
Brian Salomon3a2cc2c2018-02-03 00:25:12 +0000445 GrSurfaceDesc copyDesc = desc;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500446 if (desc.fFlags & kRenderTarget_GrSurfaceFlag) {
447 copyDesc.fSampleCnt =
448 this->caps()->getRenderTargetSampleCount(desc.fSampleCnt, desc.fConfig);
449 }
Brian Salomon3a2cc2c2018-02-03 00:25:12 +0000450
Brian Salomonbdecacf2018-02-02 20:32:49 -0500451 if (copyDesc.fFlags & kRenderTarget_GrSurfaceFlag) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500452 // We know anything we instantiate later from this deferred path will be
453 // both texturable and renderable
Greg Daniel4065d452018-11-16 15:43:41 -0500454 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(*this->caps(), format, copyDesc,
Brian Salomon3ec1f542019-06-17 17:54:57 +0000455 origin, mipMapped,
456 fit, budgeted, surfaceFlags));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500457 }
458
Brian Salomon3ec1f542019-06-17 17:54:57 +0000459 return sk_sp<GrTextureProxy>(new GrTextureProxy(format, copyDesc, origin, mipMapped,
Brian Salomon7226c232018-07-30 13:13:17 -0400460 fit, budgeted, surfaceFlags));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500461}
462
Jim Van Verthee06b332019-01-18 10:36:32 -0500463sk_sp<GrTextureProxy> GrProxyProvider::createProxy(sk_sp<SkData> data, const GrSurfaceDesc& desc) {
464 if (!this->caps()->isConfigTexturable(desc.fConfig)) {
465 return nullptr;
466 }
467
468 const GrColorType ct = GrPixelConfigToColorType(desc.fConfig);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500469 const GrBackendFormat format =
470 this->caps()->getBackendFormatFromGrColorType(ct, GrSRGBEncoded::kNo);
Jim Van Verthee06b332019-01-18 10:36:32 -0500471
472 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
473 [desc, data](GrResourceProvider* resourceProvider) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500474 GrMipLevel texels;
475 texels.fPixels = data->data();
476 texels.fRowBytes = GrBytesPerPixel(desc.fConfig)*desc.fWidth;
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400477 return LazyInstantiationResult(
478 resourceProvider->createTexture(desc, SkBudgeted::kYes, &texels, 1));
Jim Van Verthee06b332019-01-18 10:36:32 -0500479 },
480 format, desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, SkBackingFit::kExact,
481 SkBudgeted::kYes);
482
483 if (!proxy) {
484 return nullptr;
485 }
486
Robert Phillipsa41c6852019-02-07 10:44:10 -0500487 GrContext* direct = fImageContext->priv().asDirectContext();
488 if (direct) {
489 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Jim Van Verthee06b332019-01-18 10:36:32 -0500490 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
491 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500492 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500493 return nullptr;
494 }
495 }
496 return proxy;
497}
498
Brian Salomon7578f3e2018-03-07 14:39:54 -0500499sk_sp<GrTextureProxy> GrProxyProvider::wrapBackendTexture(const GrBackendTexture& backendTex,
500 GrSurfaceOrigin origin,
501 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500502 GrWrapCacheable cacheable,
Brian Salomonc67c31c2018-12-06 10:00:03 -0500503 GrIOType ioType,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500504 ReleaseProc releaseProc,
505 ReleaseContext releaseCtx) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500506 SkASSERT(ioType != kWrite_GrIOType);
Robert Phillipsf9bec202018-01-16 09:21:01 -0500507 if (this->isAbandoned()) {
508 return nullptr;
509 }
510
Brian Salomonf7778972018-03-08 10:13:17 -0500511 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500512 GrContext* direct = fImageContext->priv().asDirectContext();
513 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500514 return nullptr;
515 }
516
Greg Danielfa55f2e2019-06-13 15:21:38 -0400517#ifdef SK_DEBUG
518 SkASSERT(validate_backend_format_and_config(this->caps(), backendTex.getBackendFormat(),
519 backendTex.config()));
520#endif
521
Robert Phillipsa41c6852019-02-07 10:44:10 -0500522 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
523
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500524 sk_sp<GrTexture> tex =
Robert Phillipsa41c6852019-02-07 10:44:10 -0500525 resourceProvider->wrapBackendTexture(backendTex, ownership, cacheable, ioType);
Brian Salomonf7778972018-03-08 10:13:17 -0500526 if (!tex) {
527 return nullptr;
528 }
Robert Phillipsadbe1322018-01-17 13:35:46 -0500529
Greg Daniel6a0176b2018-01-30 09:28:44 -0500530 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500531 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel6a0176b2018-01-30 09:28:44 -0500532 }
533
Brian Salomonf7778972018-03-08 10:13:17 -0500534 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
535 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500536 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500537
Brian Salomon3ec1f542019-06-17 17:54:57 +0000538 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500539}
540
Brian Salomon7578f3e2018-03-07 14:39:54 -0500541sk_sp<GrTextureProxy> GrProxyProvider::wrapRenderableBackendTexture(
Brian Salomon02bd2952018-03-07 15:20:21 -0500542 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt,
Greg Daniel8ce79912019-02-05 10:08:43 -0500543 GrWrapOwnership ownership, GrWrapCacheable cacheable, ReleaseProc releaseProc,
544 ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500545 if (this->isAbandoned()) {
546 return nullptr;
547 }
548
Brian Salomonf7778972018-03-08 10:13:17 -0500549 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500550 GrContext* direct = fImageContext->priv().asDirectContext();
551 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500552 return nullptr;
553 }
554
Greg Danielfa55f2e2019-06-13 15:21:38 -0400555 SkASSERT(validate_backend_format_and_config(this->caps(), backendTex.getBackendFormat(),
556 backendTex.config()));
557
Robert Phillipsa41c6852019-02-07 10:44:10 -0500558 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
559
Greg Daniel6abda432018-02-15 14:55:00 -0500560 sampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config());
Greg Danielf87651e2018-02-21 11:36:53 -0500561 if (!sampleCnt) {
562 return nullptr;
563 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500564
Robert Phillipsa41c6852019-02-07 10:44:10 -0500565 sk_sp<GrTexture> tex = resourceProvider->wrapRenderableBackendTexture(backendTex, sampleCnt,
566 ownership, cacheable);
Brian Salomonf7778972018-03-08 10:13:17 -0500567 if (!tex) {
568 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500569 }
570
Greg Daniel8ce79912019-02-05 10:08:43 -0500571 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500572 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500573 }
574
Brian Salomonf7778972018-03-08 10:13:17 -0500575 SkASSERT(tex->asRenderTarget()); // A GrTextureRenderTarget
576 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500577 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Greg Daniel6abda432018-02-15 14:55:00 -0500578
Brian Salomon3ec1f542019-06-17 17:54:57 +0000579 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500580}
581
Brian Salomon7578f3e2018-03-07 14:39:54 -0500582sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget(
Greg Daniel8ce79912019-02-05 10:08:43 -0500583 const GrBackendRenderTarget& backendRT, GrSurfaceOrigin origin, ReleaseProc releaseProc,
584 ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500585 if (this->isAbandoned()) {
586 return nullptr;
587 }
588
Brian Salomonf7778972018-03-08 10:13:17 -0500589 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500590 GrContext* direct = fImageContext->priv().asDirectContext();
591 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500592 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500593 }
594
Greg Daniel23093132019-06-17 11:10:48 -0400595#ifdef SK_DEBUG
Brian Salomon3ec1f542019-06-17 17:54:57 +0000596 GrColorType colorType = GrPixelConfigToColorType(backendRT.config());
Greg Danielfa55f2e2019-06-13 15:21:38 -0400597 GrPixelConfig testConfig =
598 this->caps()->validateBackendRenderTarget(backendRT,
599 GrColorTypeToSkColorType(colorType));
600 SkASSERT(testConfig != kUnknown_GrPixelConfig);
601#endif
602
Robert Phillipsa41c6852019-02-07 10:44:10 -0500603 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
604
605 sk_sp<GrRenderTarget> rt = resourceProvider->wrapBackendRenderTarget(backendRT);
Brian Salomonf7778972018-03-08 10:13:17 -0500606 if (!rt) {
607 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500608 }
Greg Daniel8ce79912019-02-05 10:08:43 -0500609
Greg Daniel8ce79912019-02-05 10:08:43 -0500610 if (releaseProc) {
Brian Salomon2ca31f82019-03-05 13:28:58 -0500611 rt->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500612 }
613
Brian Salomonf7778972018-03-08 10:13:17 -0500614 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
615 SkASSERT(!rt->getUniqueKey().isValid());
616 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500617 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Brian Salomonf7778972018-03-08 10:13:17 -0500618
Brian Salomon3ec1f542019-06-17 17:54:57 +0000619 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(std::move(rt), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500620}
621
Brian Salomon7578f3e2018-03-07 14:39:54 -0500622sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendTextureAsRenderTarget(
623 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500624 if (this->isAbandoned()) {
625 return nullptr;
626 }
627
Brian Salomonf7778972018-03-08 10:13:17 -0500628 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500629 GrContext* direct = fImageContext->priv().asDirectContext();
630 if (!direct) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500631 return nullptr;
632 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500633
Greg Danielfa55f2e2019-06-13 15:21:38 -0400634 SkASSERT(validate_backend_format_and_config(this->caps(), backendTex.getBackendFormat(),
635 backendTex.config()));
636
Robert Phillipsa41c6852019-02-07 10:44:10 -0500637 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
638
Brian Salomonf7778972018-03-08 10:13:17 -0500639 sk_sp<GrRenderTarget> rt =
Robert Phillipsa41c6852019-02-07 10:44:10 -0500640 resourceProvider->wrapBackendTextureAsRenderTarget(backendTex, sampleCnt);
Brian Salomonf7778972018-03-08 10:13:17 -0500641 if (!rt) {
642 return nullptr;
Greg Danielf87651e2018-02-21 11:36:53 -0500643 }
Brian Salomonf7778972018-03-08 10:13:17 -0500644 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
645 SkASSERT(!rt->getUniqueKey().isValid());
Greg Danielb46add82019-01-02 14:51:29 -0500646 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500647 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielf87651e2018-02-21 11:36:53 -0500648
Brian Salomon3ec1f542019-06-17 17:54:57 +0000649 return sk_sp<GrSurfaceProxy>(new GrRenderTargetProxy(std::move(rt), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500650}
651
Greg Danielb46add82019-01-02 14:51:29 -0500652sk_sp<GrRenderTargetProxy> GrProxyProvider::wrapVulkanSecondaryCBAsRenderTarget(
653 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
654 if (this->isAbandoned()) {
655 return nullptr;
656 }
657
658 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500659 GrContext* direct = fImageContext->priv().asDirectContext();
660 if (!direct) {
Greg Danielb46add82019-01-02 14:51:29 -0500661 return nullptr;
662 }
663
Robert Phillipsa41c6852019-02-07 10:44:10 -0500664 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Danielb46add82019-01-02 14:51:29 -0500665
Robert Phillipsa41c6852019-02-07 10:44:10 -0500666 sk_sp<GrRenderTarget> rt = resourceProvider->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
667 vkInfo);
Greg Danielb46add82019-01-02 14:51:29 -0500668 if (!rt) {
669 return nullptr;
670 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500671
Greg Danielb46add82019-01-02 14:51:29 -0500672 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
673 SkASSERT(!rt->getUniqueKey().isValid());
674 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500675 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielb46add82019-01-02 14:51:29 -0500676
677 // All Vulkan surfaces uses top left origins.
678 return sk_sp<GrRenderTargetProxy>(
679 new GrRenderTargetProxy(std::move(rt),
Brian Salomon3ec1f542019-06-17 17:54:57 +0000680 kTopLeft_GrSurfaceOrigin,
Greg Danielb46add82019-01-02 14:51:29 -0500681 GrRenderTargetProxy::WrapsVkSecondaryCB::kYes));
682}
683
Robert Phillips777707b2018-01-17 11:40:14 -0500684sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500685 const GrBackendFormat& format,
Robert Phillips777707b2018-01-17 11:40:14 -0500686 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500687 GrSurfaceOrigin origin,
Brian Salomon7226c232018-07-30 13:13:17 -0400688 GrMipMapped mipMapped,
Brian Salomon7226c232018-07-30 13:13:17 -0400689 SkBackingFit fit,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500690 SkBudgeted budgeted) {
Greg Daniel4065d452018-11-16 15:43:41 -0500691 return this->createLazyProxy(std::move(callback), format, desc, origin, mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400692 GrInternalSurfaceFlags::kNone, fit, budgeted);
Greg Daniel2a303902018-02-20 10:25:54 -0500693}
694
695sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500696 const GrBackendFormat& format,
Greg Daniel2a303902018-02-20 10:25:54 -0500697 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500698 GrSurfaceOrigin origin,
Greg Daniel2a303902018-02-20 10:25:54 -0500699 GrMipMapped mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400700 GrInternalSurfaceFlags surfaceFlags,
Brian Salomon7226c232018-07-30 13:13:17 -0400701 SkBackingFit fit,
702 SkBudgeted budgeted) {
Greg Daniela8d92112018-03-09 12:05:04 -0500703 // For non-ddl draws always make lazy proxy's single use.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500704 LazyInstantiationType lazyType = this->renderingDirectly() ? LazyInstantiationType::kSingleUse
705 : LazyInstantiationType::kMultipleUse;
Greg Daniel4065d452018-11-16 15:43:41 -0500706 return this->createLazyProxy(std::move(callback), format, desc, origin, mipMapped, surfaceFlags,
707 fit, budgeted, lazyType);
Greg Daniela8d92112018-03-09 12:05:04 -0500708}
709
710sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500711 const GrBackendFormat& format,
Greg Daniela8d92112018-03-09 12:05:04 -0500712 const GrSurfaceDesc& desc,
713 GrSurfaceOrigin origin,
714 GrMipMapped mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400715 GrInternalSurfaceFlags surfaceFlags,
Brian Salomon7226c232018-07-30 13:13:17 -0400716 SkBackingFit fit,
717 SkBudgeted budgeted,
Greg Daniela8d92112018-03-09 12:05:04 -0500718 LazyInstantiationType lazyType) {
Robert Phillips777707b2018-01-17 11:40:14 -0500719 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
720 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400721
Robert Phillipsa41c6852019-02-07 10:44:10 -0500722 if (desc.fWidth > this->caps()->maxTextureSize() ||
723 desc.fHeight > this->caps()->maxTextureSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400724 return nullptr;
725 }
726
Greg Danielfa55f2e2019-06-13 15:21:38 -0400727 SkASSERT(validate_backend_format_and_config(this->caps(), format, desc.fConfig));
Greg Daniel457469c2018-02-08 15:05:44 -0500728
Greg Daniel2a303902018-02-20 10:25:54 -0500729#ifdef SK_DEBUG
730 if (SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags)) {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400731 if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kMixedSampled)) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500732 SkASSERT(this->caps()->usesMixedSamples() && desc.fSampleCnt > 1);
Greg Daniel2a303902018-02-20 10:25:54 -0500733 }
Greg Daniel2a303902018-02-20 10:25:54 -0500734 }
735#endif
736
Brian Salomon2a4f9832018-03-03 22:43:43 -0500737 return sk_sp<GrTextureProxy>(
738 SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags)
Greg Daniel4065d452018-11-16 15:43:41 -0500739 ? new GrTextureRenderTargetProxy(std::move(callback), lazyType, format, desc,
Brian Salomon3ec1f542019-06-17 17:54:57 +0000740 origin, mipMapped, fit, budgeted, surfaceFlags)
Greg Daniel4065d452018-11-16 15:43:41 -0500741 : new GrTextureProxy(std::move(callback), lazyType, format, desc, origin,
Brian Salomon3ec1f542019-06-17 17:54:57 +0000742 mipMapped, fit, budgeted, surfaceFlags));
Robert Phillips777707b2018-01-17 11:40:14 -0500743}
744
Robert Phillipse8fabb22018-02-04 14:33:21 -0500745sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500746 LazyInstantiateCallback&& callback, const GrBackendFormat& format,
747 const GrSurfaceDesc& desc, GrSurfaceOrigin origin, GrInternalSurfaceFlags surfaceFlags,
Greg Danielb085fa92019-03-05 16:55:12 -0500748 const TextureInfo* textureInfo, SkBackingFit fit, SkBudgeted budgeted,
749 bool wrapsVkSecondaryCB) {
Robert Phillipse8fabb22018-02-04 14:33:21 -0500750 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
751 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400752
Robert Phillipsa41c6852019-02-07 10:44:10 -0500753 if (desc.fWidth > this->caps()->maxRenderTargetSize() ||
754 desc.fHeight > this->caps()->maxRenderTargetSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400755 return nullptr;
756 }
757
Greg Daniel2a303902018-02-20 10:25:54 -0500758 SkASSERT(SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags));
Greg Danielfa55f2e2019-06-13 15:21:38 -0400759 SkASSERT(validate_backend_format_and_config(this->caps(), format, desc.fConfig));
Greg Daniel457469c2018-02-08 15:05:44 -0500760
Greg Daniel2a303902018-02-20 10:25:54 -0500761#ifdef SK_DEBUG
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400762 if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kMixedSampled)) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500763 SkASSERT(this->caps()->usesMixedSamples() && desc.fSampleCnt > 1);
Greg Daniel2a303902018-02-20 10:25:54 -0500764 }
Greg Daniel2a303902018-02-20 10:25:54 -0500765#endif
766
Greg Daniel457469c2018-02-08 15:05:44 -0500767 using LazyInstantiationType = GrSurfaceProxy::LazyInstantiationType;
768 // For non-ddl draws always make lazy proxy's single use.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500769 LazyInstantiationType lazyType = this->renderingDirectly() ? LazyInstantiationType::kSingleUse
770 : LazyInstantiationType::kMultipleUse;
Greg Daniel457469c2018-02-08 15:05:44 -0500771
Brian Salomon7226c232018-07-30 13:13:17 -0400772 if (textureInfo) {
Greg Danielb085fa92019-03-05 16:55:12 -0500773 // Wrapped vulkan secondary command buffers don't support texturing since we won't have an
774 // actual VkImage to texture from.
775 SkASSERT(!wrapsVkSecondaryCB);
Brian Salomon7226c232018-07-30 13:13:17 -0400776 return sk_sp<GrRenderTargetProxy>(new GrTextureRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500777 std::move(callback), lazyType, format, desc, origin, textureInfo->fMipMapped,
Brian Salomon3ec1f542019-06-17 17:54:57 +0000778 fit, budgeted, surfaceFlags));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500779 }
780
Greg Danielb085fa92019-03-05 16:55:12 -0500781 GrRenderTargetProxy::WrapsVkSecondaryCB vkSCB =
782 wrapsVkSecondaryCB ? GrRenderTargetProxy::WrapsVkSecondaryCB::kYes
783 : GrRenderTargetProxy::WrapsVkSecondaryCB::kNo;
784
Brian Salomon2a4f9832018-03-03 22:43:43 -0500785 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
Brian Salomon3ec1f542019-06-17 17:54:57 +0000786 std::move(callback), lazyType, format, desc, origin, fit, budgeted, surfaceFlags,
787 vkSCB));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500788}
789
Chris Dalton19cc00f2019-04-15 09:06:28 -0600790sk_sp<GrTextureProxy> GrProxyProvider::MakeFullyLazyProxy(
791 LazyInstantiateCallback&& callback, const GrBackendFormat& format, Renderable renderable,
792 GrSurfaceOrigin origin, GrPixelConfig config, const GrCaps& caps, int sampleCnt) {
Greg Danielfa55f2e2019-06-13 15:21:38 -0400793 SkASSERT(validate_backend_format_and_config(&caps, format, config));
Robert Phillips777707b2018-01-17 11:40:14 -0500794 GrSurfaceDesc desc;
Robert Phillips10d17212019-04-24 14:09:10 -0400795 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNone;
Robert Phillips777707b2018-01-17 11:40:14 -0500796 if (Renderable::kYes == renderable) {
797 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Chris Dalton19cc00f2019-04-15 09:06:28 -0600798 if (sampleCnt > 1 && caps.usesMixedSamples()) {
799 surfaceFlags |= GrInternalSurfaceFlags::kMixedSampled;
800 }
Robert Phillips777707b2018-01-17 11:40:14 -0500801 }
Robert Phillips777707b2018-01-17 11:40:14 -0500802 desc.fWidth = -1;
803 desc.fHeight = -1;
804 desc.fConfig = config;
Chris Dalton19cc00f2019-04-15 09:06:28 -0600805 desc.fSampleCnt = sampleCnt;
Robert Phillips777707b2018-01-17 11:40:14 -0500806
Chris Dalton4c458b12018-06-16 17:22:59 -0600807 return sk_sp<GrTextureProxy>(
808 (Renderable::kYes == renderable)
Brian Salomon7226c232018-07-30 13:13:17 -0400809 ? new GrTextureRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500810 std::move(callback), LazyInstantiationType::kSingleUse, format, desc,
Brian Salomon3ec1f542019-06-17 17:54:57 +0000811 origin, GrMipMapped::kNo, SkBackingFit::kApprox, SkBudgeted::kYes,
812 surfaceFlags)
Chris Dalton4c458b12018-06-16 17:22:59 -0600813 : new GrTextureProxy(std::move(callback), LazyInstantiationType::kSingleUse,
Brian Salomon3ec1f542019-06-17 17:54:57 +0000814 format, desc, origin, GrMipMapped::kNo,
Brian Salomon7226c232018-07-30 13:13:17 -0400815 SkBackingFit::kApprox, SkBudgeted::kYes, surfaceFlags));
Robert Phillips777707b2018-01-17 11:40:14 -0500816}
817
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500818bool GrProxyProvider::IsFunctionallyExact(GrSurfaceProxy* proxy) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400819 const bool isInstantiated = proxy->isInstantiated();
Robert Phillipsdb3b9792018-06-12 15:18:00 -0400820 // A proxy is functionally exact if:
821 // it is exact (obvs)
822 // when it is instantiated it will be exact (i.e., power of two dimensions)
823 // it is already instantiated and the proxy covers the entire backing surface
824 return proxy->priv().isExact() ||
825 (!isInstantiated && SkIsPow2(proxy->width()) && SkIsPow2(proxy->height())) ||
826 (isInstantiated && proxy->worstCaseWidth() == proxy->width() &&
827 proxy->worstCaseHeight() == proxy->height());
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500828}
829
Robert Phillips427966a2018-12-20 17:20:43 -0500830void GrProxyProvider::processInvalidUniqueKey(const GrUniqueKey& key, GrTextureProxy* proxy,
831 InvalidateGPUResource invalidateGPUResource) {
Chris Dalton2de13dd2019-01-03 15:11:59 -0700832 SkASSERT(key.isValid());
833
Robert Phillips427966a2018-12-20 17:20:43 -0500834 if (!proxy) {
835 proxy = fUniquelyKeyedProxies.find(key);
836 }
Chris Dalton2de13dd2019-01-03 15:11:59 -0700837 SkASSERT(!proxy || proxy->getUniqueKey() == key);
838
839 // Locate the corresponding GrGpuResource (if it needs to be invalidated) before clearing the
840 // proxy's unique key. We must do it in this order because 'key' may alias the proxy's key.
841 sk_sp<GrGpuResource> invalidGpuResource;
842 if (InvalidateGPUResource::kYes == invalidateGPUResource) {
Brian Salomon01ceae92019-04-02 11:49:54 -0400843 GrContext* direct = fImageContext->priv().asDirectContext();
844 if (direct) {
845 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
846 invalidGpuResource = resourceProvider->findByUniqueKey<GrGpuResource>(key);
Chris Dalton2de13dd2019-01-03 15:11:59 -0700847 }
848 SkASSERT(!invalidGpuResource || invalidGpuResource->getUniqueKey() == key);
849 }
Robert Phillips427966a2018-12-20 17:20:43 -0500850
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500851 // Note: this method is called for the whole variety of GrGpuResources so often 'key'
852 // will not be in 'fUniquelyKeyedProxies'.
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500853 if (proxy) {
Robert Phillips427966a2018-12-20 17:20:43 -0500854 fUniquelyKeyedProxies.remove(key);
855 proxy->cacheAccess().clearUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500856 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500857
Chris Dalton2de13dd2019-01-03 15:11:59 -0700858 if (invalidGpuResource) {
859 invalidGpuResource->resourcePriv().removeUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500860 }
861}
862
Robert Phillipsa41c6852019-02-07 10:44:10 -0500863uint32_t GrProxyProvider::contextID() const {
864 return fImageContext->priv().contextID();
865}
866
867const GrCaps* GrProxyProvider::caps() const {
868 return fImageContext->priv().caps();
869}
870
871sk_sp<const GrCaps> GrProxyProvider::refCaps() const {
872 return fImageContext->priv().refCaps();
873}
874
Robert Phillipsa9162df2019-02-11 14:12:03 -0500875bool GrProxyProvider::isAbandoned() const {
876 return fImageContext->priv().abandoned();
877}
878
Robert Phillips0790f8a2018-09-18 13:11:03 -0400879void GrProxyProvider::orphanAllUniqueKeys() {
880 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
881 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
882 GrTextureProxy& tmp = *iter;
883
884 tmp.fProxyProvider = nullptr;
885 }
886}
887
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500888void GrProxyProvider::removeAllUniqueKeys() {
889 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
890 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
891 GrTextureProxy& tmp = *iter;
892
Chris Dalton2de13dd2019-01-03 15:11:59 -0700893 this->processInvalidUniqueKey(tmp.getUniqueKey(), &tmp, InvalidateGPUResource::kNo);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500894 }
895 SkASSERT(!fUniquelyKeyedProxies.count());
896}
Robert Phillipsa41c6852019-02-07 10:44:10 -0500897
898bool GrProxyProvider::renderingDirectly() const {
899 return fImageContext->priv().asDirectContext();
900}