blob: c831471493eab1ebe48b616283fad1e537857cf5 [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
8#include "GrProxyProvider.h"
9
10#include "GrCaps.h"
Robert Phillipsa41c6852019-02-07 10:44:10 -050011#include "GrContext.h"
12#include "GrContextPriv.h"
13#include "GrImageContext.h"
14#include "GrImageContextPriv.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050015#include "GrRenderTarget.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050016#include "GrResourceKey.h"
17#include "GrResourceProvider.h"
18#include "GrSurfaceProxy.h"
19#include "GrSurfaceProxyPriv.h"
20#include "GrTexture.h"
21#include "GrTextureProxyCacheAccess.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050022#include "GrTextureRenderTargetProxy.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050023#include "../private/GrSingleOwner.h"
Brian Osmand29dcd12018-09-13 15:04:29 -040024#include "SkAutoPixmapStorage.h"
Greg Daniela4ead652018-02-07 10:21:48 -050025#include "SkBitmap.h"
Greg Daniel9d86f1d2018-01-29 09:33:59 -050026#include "SkGr.h"
27#include "SkImage.h"
28#include "SkImage_Base.h"
Greg Daniela4ead652018-02-07 10:21:48 -050029#include "SkImageInfoPriv.h"
30#include "SkImagePriv.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050031#include "SkMipMap.h"
Greg Daniela4ead652018-02-07 10:21:48 -050032#include "SkTraceEvent.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) {
108 proxy->firstRefAccess().ref();
109 result.reset(proxy);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500110 SkASSERT(result->origin() == origin);
111 }
112 return result;
113}
114
Robert Phillipsa41c6852019-02-07 10:44:10 -0500115///////////////////////////////////////////////////////////////////////////////
116
117#if GR_TEST_UTILS
118sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createInstantiatedProxy(
119 const GrSurfaceDesc& desc, GrSurfaceOrigin origin, SkBackingFit fit, SkBudgeted budgeted) {
120 GrContext* direct = fImageContext->priv().asDirectContext();
121 if (!direct) {
122 return nullptr;
123 }
124
125 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
126 sk_sp<GrTexture> tex;
127
128 if (SkBackingFit::kApprox == fit) {
129 tex = resourceProvider->createApproxTexture(desc, GrResourceProvider::Flags::kNone);
130 } else {
131 tex = resourceProvider->createTexture(desc, budgeted, GrResourceProvider::Flags::kNone);
132 }
133 if (!tex) {
134 return nullptr;
135 }
136
137 return this->createWrapped(std::move(tex), origin);
138}
139
140sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createWrapped(sk_sp<GrTexture> tex,
141 GrSurfaceOrigin origin) {
142 return this->createWrapped(std::move(tex), origin);
143}
144#endif
145
Robert Phillipsadbe1322018-01-17 13:35:46 -0500146sk_sp<GrTextureProxy> GrProxyProvider::createWrapped(sk_sp<GrTexture> tex, GrSurfaceOrigin origin) {
147#ifdef SK_DEBUG
148 if (tex->getUniqueKey().isValid()) {
149 SkASSERT(!this->findProxyByUniqueKey(tex->getUniqueKey(), origin));
150 }
151#endif
152
153 if (tex->asRenderTarget()) {
154 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin));
155 } else {
156 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin));
157 }
158}
159
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500160sk_sp<GrTextureProxy> GrProxyProvider::findOrCreateProxyByUniqueKey(const GrUniqueKey& key,
161 GrSurfaceOrigin origin) {
162 ASSERT_SINGLE_OWNER
163
164 if (this->isAbandoned()) {
165 return nullptr;
166 }
167
168 sk_sp<GrTextureProxy> result = this->findProxyByUniqueKey(key, origin);
169 if (result) {
170 return result;
171 }
172
Robert Phillipsa41c6852019-02-07 10:44:10 -0500173 GrContext* direct = fImageContext->priv().asDirectContext();
174 if (!direct) {
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500175 return nullptr;
176 }
177
Robert Phillipsa41c6852019-02-07 10:44:10 -0500178 GrResourceCache* resourceCache = direct->priv().getResourceCache();
179
180 GrGpuResource* resource = resourceCache->findAndRefUniqueResource(key);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500181 if (!resource) {
182 return nullptr;
183 }
184
185 sk_sp<GrTexture> texture(static_cast<GrSurface*>(resource)->asTexture());
186 SkASSERT(texture);
187
Robert Phillipsadbe1322018-01-17 13:35:46 -0500188 result = this->createWrapped(std::move(texture), origin);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500189 SkASSERT(result->getUniqueKey() == key);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500190 // createWrapped should've added this for us
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500191 SkASSERT(fUniquelyKeyedProxies.find(key));
192 return result;
193}
194
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500195sk_sp<GrTextureProxy> GrProxyProvider::createTextureProxy(sk_sp<SkImage> srcImage,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400196 GrSurfaceDescFlags descFlags,
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500197 int sampleCnt,
Greg Danielfb3abcd2018-02-02 15:48:33 -0500198 SkBudgeted budgeted,
Chris Daltond004e0b2018-09-27 09:28:03 -0600199 SkBackingFit fit,
200 GrInternalSurfaceFlags surfaceFlags) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500201 ASSERT_SINGLE_OWNER
202 SkASSERT(srcImage);
203
204 if (this->isAbandoned()) {
205 return nullptr;
206 }
207
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400208 const SkImageInfo& info = srcImage->imageInfo();
Brian Osmand29dcd12018-09-13 15:04:29 -0400209 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
Greg Danielf87651e2018-02-21 11:36:53 -0500210
Greg Daniel0a7aa142018-02-21 13:02:32 -0500211 if (kUnknown_GrPixelConfig == config) {
212 return nullptr;
213 }
214
Robert Phillipsa41c6852019-02-07 10:44:10 -0500215 GrBackendFormat format = this->caps()->getBackendFormatFromColorType(info.colorType());
Greg Daniel4065d452018-11-16 15:43:41 -0500216 if (!format.isValid()) {
217 return nullptr;
218 }
219
Brian Osmand29dcd12018-09-13 15:04:29 -0400220 if (!this->caps()->isConfigTexturable(config)) {
221 SkBitmap copy8888;
222 if (!copy8888.tryAllocPixels(info.makeColorType(kRGBA_8888_SkColorType)) ||
223 !srcImage->readPixels(copy8888.pixmap(), 0, 0)) {
224 return nullptr;
225 }
226 copy8888.setImmutable();
227 srcImage = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode);
228 config = kRGBA_8888_GrPixelConfig;
229 }
230
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400231 if (SkToBool(descFlags & kRenderTarget_GrSurfaceFlag)) {
Greg Danielf87651e2018-02-21 11:36:53 -0500232 sampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, config);
233 if (!sampleCnt) {
234 return nullptr;
235 }
236 }
237
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400238 if (SkToBool(descFlags & kRenderTarget_GrSurfaceFlag)) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500239 if (this->caps()->usesMixedSamples() && sampleCnt > 1) {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400240 surfaceFlags |= GrInternalSurfaceFlags::kMixedSampled;
Greg Daniel2a303902018-02-20 10:25:54 -0500241 }
Greg Daniel2a303902018-02-20 10:25:54 -0500242 }
243
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500244 GrSurfaceDesc desc;
245 desc.fWidth = srcImage->width();
246 desc.fHeight = srcImage->height();
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400247 desc.fFlags = descFlags;
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500248 desc.fSampleCnt = sampleCnt;
Greg Danielf87651e2018-02-21 11:36:53 -0500249 desc.fConfig = config;
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500250
251 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Chris Daltond004e0b2018-09-27 09:28:03 -0600252 [desc, budgeted, srcImage, fit, surfaceFlags](GrResourceProvider* resourceProvider) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500253 SkPixmap pixMap;
254 SkAssertResult(srcImage->peekPixels(&pixMap));
255 GrMipLevel mipLevel = { pixMap.addr(), pixMap.rowBytes() };
256
Chris Daltond004e0b2018-09-27 09:28:03 -0600257 auto resourceProviderFlags = GrResourceProvider::Flags::kNone;
258 if (surfaceFlags & GrInternalSurfaceFlags::kNoPendingIO) {
259 resourceProviderFlags |= GrResourceProvider::Flags::kNoPendingIO;
260 }
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400261 return LazyInstantiationResult(resourceProvider->createTexture(
262 desc, budgeted, fit, mipLevel, resourceProviderFlags));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500263 },
Greg Daniel4065d452018-11-16 15:43:41 -0500264 format, desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, surfaceFlags, fit, budgeted);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500265
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400266 if (!proxy) {
267 return nullptr;
268 }
269
Robert Phillipsa41c6852019-02-07 10:44:10 -0500270 GrContext* direct = fImageContext->priv().asDirectContext();
271 if (direct) {
272 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
273
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500274 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
275 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500276 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500277 return nullptr;
278 }
279 }
Robert Phillipsc1b60662018-06-26 10:20:08 -0400280
281 SkASSERT(proxy->width() == desc.fWidth);
282 SkASSERT(proxy->height() == desc.fHeight);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500283 return proxy;
284}
285
Greg Daniel4065d452018-11-16 15:43:41 -0500286sk_sp<GrTextureProxy> GrProxyProvider::createMipMapProxy(const GrBackendFormat& format,
287 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500288 GrSurfaceOrigin origin,
Greg Daniel30815082018-02-09 16:08:30 -0500289 SkBudgeted budgeted) {
Robert Phillips579f0942018-01-08 14:53:35 -0500290 ASSERT_SINGLE_OWNER
291
292 if (this->isAbandoned()) {
293 return nullptr;
294 }
295
Greg Daniel4065d452018-11-16 15:43:41 -0500296 return this->createProxy(format, desc, origin, GrMipMapped::kYes, SkBackingFit::kExact,
297 budgeted, GrInternalSurfaceFlags::kNone);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500298}
299
Brian Osmande496652019-03-22 13:42:33 -0400300sk_sp<GrTextureProxy> GrProxyProvider::createProxyFromBitmap(const SkBitmap& bitmap,
301 GrMipMapped mipMapped) {
Brian Osman412674f2019-02-07 15:34:58 -0500302 ASSERT_SINGLE_OWNER
Brian Osman7dcc6162019-03-25 10:12:57 -0400303 SkASSERT(GrMipMapped::kNo == mipMapped || this->caps()->mipMapSupport());
Brian Osman412674f2019-02-07 15:34:58 -0500304
305 if (this->isAbandoned()) {
306 return nullptr;
307 }
308
Brian Osman2b23c4b2018-06-01 12:25:08 -0400309 if (!SkImageInfoIsValid(bitmap.info())) {
Greg Daniela4ead652018-02-07 10:21:48 -0500310 return nullptr;
311 }
312
Brian Osmande496652019-03-22 13:42:33 -0400313 ATRACE_ANDROID_FRAMEWORK("Upload %sTexture [%ux%u]",
314 GrMipMapped::kYes == mipMapped ? "MipMap " : "",
315 bitmap.width(), bitmap.height());
Greg Daniela4ead652018-02-07 10:21:48 -0500316
317 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
318 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
319 // 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 -0500320 SkCopyPixelsMode copyMode = this->renderingDirectly() ? kNever_SkCopyPixelsMode
321 : kIfMutable_SkCopyPixelsMode;
Greg Daniela4ead652018-02-07 10:21:48 -0500322 sk_sp<SkImage> baseLevel = SkMakeImageFromRasterBitmap(bitmap, copyMode);
Greg Daniela4ead652018-02-07 10:21:48 -0500323 if (!baseLevel) {
324 return nullptr;
325 }
326
Brian Osmande496652019-03-22 13:42:33 -0400327 // If mips weren't requested (or this was too small to have any), then take the fast path
328 if (GrMipMapped::kNo == mipMapped ||
329 0 == SkMipMap::ComputeLevelCount(baseLevel->width(), baseLevel->height())) {
Brian Osman7dcc6162019-03-25 10:12:57 -0400330 return this->createTextureProxy(std::move(baseLevel), kNone_GrSurfaceFlags, 1,
331 SkBudgeted::kYes, SkBackingFit::kExact);
Greg Daniela4ead652018-02-07 10:21:48 -0500332 }
333
Robert Phillipsa41c6852019-02-07 10:44:10 -0500334 const GrBackendFormat format =
335 this->caps()->getBackendFormatFromColorType(bitmap.info().colorType());
Greg Daniel4065d452018-11-16 15:43:41 -0500336 if (!format.isValid()) {
337 return nullptr;
338 }
339
Brian Osmand29dcd12018-09-13 15:04:29 -0400340 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info());
341 if (!this->caps()->isConfigTexturable(desc.fConfig)) {
342 SkBitmap copy8888;
343 if (!copy8888.tryAllocPixels(bitmap.info().makeColorType(kRGBA_8888_SkColorType)) ||
344 !bitmap.readPixels(copy8888.pixmap())) {
345 return nullptr;
346 }
347 copy8888.setImmutable();
348 baseLevel = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode);
349 desc.fConfig = kRGBA_8888_GrPixelConfig;
350 }
351
352 SkPixmap pixmap;
353 SkAssertResult(baseLevel->peekPixels(&pixmap));
354 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(pixmap, nullptr));
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400355 if (!mipmaps) {
356 return nullptr;
357 }
358
Greg Daniela4ead652018-02-07 10:21:48 -0500359 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Osman1b97f132018-09-13 17:33:48 +0000360 [desc, baseLevel, mipmaps](GrResourceProvider* resourceProvider) {
Brian Osman1b97f132018-09-13 17:33:48 +0000361 const int mipLevelCount = mipmaps->countLevels() + 1;
362 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
363
Greg Daniela4ead652018-02-07 10:21:48 -0500364 SkPixmap pixmap;
365 SkAssertResult(baseLevel->peekPixels(&pixmap));
366
367 // DDL TODO: Instead of copying all this info into GrMipLevels we should just plumb
368 // the use of SkMipMap down through Ganesh.
369 texels[0].fPixels = pixmap.addr();
370 texels[0].fRowBytes = pixmap.rowBytes();
371
372 for (int i = 1; i < mipLevelCount; ++i) {
373 SkMipMap::Level generatedMipLevel;
374 mipmaps->getLevel(i - 1, &generatedMipLevel);
375 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
376 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
377 SkASSERT(texels[i].fPixels);
378 }
379
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400380 return LazyInstantiationResult(resourceProvider->createTexture(
381 desc, SkBudgeted::kYes, texels.get(), mipLevelCount));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500382 },
Greg Daniel4065d452018-11-16 15:43:41 -0500383 format, desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kYes, SkBackingFit::kExact,
384 SkBudgeted::kYes);
Greg Daniela4ead652018-02-07 10:21:48 -0500385
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400386 if (!proxy) {
387 return nullptr;
388 }
389
Robert Phillipsa41c6852019-02-07 10:44:10 -0500390 GrContext* direct = fImageContext->priv().asDirectContext();
391 if (direct) {
392 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Daniela4ead652018-02-07 10:21:48 -0500393 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
394 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500395 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Greg Daniela4ead652018-02-07 10:21:48 -0500396 return nullptr;
397 }
398 }
399 return proxy;
400}
401
Greg Daniel4065d452018-11-16 15:43:41 -0500402sk_sp<GrTextureProxy> GrProxyProvider::createProxy(const GrBackendFormat& format,
403 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500404 GrSurfaceOrigin origin,
Greg Danielf6f7b672018-02-15 13:06:26 -0500405 GrMipMapped mipMapped,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500406 SkBackingFit fit,
407 SkBudgeted budgeted,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400408 GrInternalSurfaceFlags surfaceFlags) {
Greg Danielf6f7b672018-02-15 13:06:26 -0500409 if (GrMipMapped::kYes == mipMapped) {
410 // SkMipMap doesn't include the base level in the level count so we have to add 1
411 int mipCount = SkMipMap::ComputeLevelCount(desc.fWidth, desc.fHeight) + 1;
412 if (1 == mipCount) {
413 mipMapped = GrMipMapped::kNo;
414 }
415 }
416
417 if (!this->caps()->validateSurfaceDesc(desc, mipMapped)) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500418 return nullptr;
419 }
Brian Salomon3a2cc2c2018-02-03 00:25:12 +0000420 GrSurfaceDesc copyDesc = desc;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500421 if (desc.fFlags & kRenderTarget_GrSurfaceFlag) {
422 copyDesc.fSampleCnt =
423 this->caps()->getRenderTargetSampleCount(desc.fSampleCnt, desc.fConfig);
424 }
Brian Salomon3a2cc2c2018-02-03 00:25:12 +0000425
Brian Salomonbdecacf2018-02-02 20:32:49 -0500426 if (copyDesc.fFlags & kRenderTarget_GrSurfaceFlag) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500427 // We know anything we instantiate later from this deferred path will be
428 // both texturable and renderable
Greg Daniel4065d452018-11-16 15:43:41 -0500429 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(*this->caps(), format, copyDesc,
430 origin, mipMapped,
Brian Salomon7226c232018-07-30 13:13:17 -0400431 fit, budgeted, surfaceFlags));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500432 }
433
Greg Daniel4065d452018-11-16 15:43:41 -0500434 return sk_sp<GrTextureProxy>(new GrTextureProxy(format, copyDesc, origin, mipMapped,
Brian Salomon7226c232018-07-30 13:13:17 -0400435 fit, budgeted, surfaceFlags));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500436}
437
Jim Van Verthee06b332019-01-18 10:36:32 -0500438sk_sp<GrTextureProxy> GrProxyProvider::createProxy(sk_sp<SkData> data, const GrSurfaceDesc& desc) {
439 if (!this->caps()->isConfigTexturable(desc.fConfig)) {
440 return nullptr;
441 }
442
443 const GrColorType ct = GrPixelConfigToColorType(desc.fConfig);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500444 const GrBackendFormat format =
445 this->caps()->getBackendFormatFromGrColorType(ct, GrSRGBEncoded::kNo);
Jim Van Verthee06b332019-01-18 10:36:32 -0500446
447 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
448 [desc, data](GrResourceProvider* resourceProvider) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500449 GrMipLevel texels;
450 texels.fPixels = data->data();
451 texels.fRowBytes = GrBytesPerPixel(desc.fConfig)*desc.fWidth;
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400452 return LazyInstantiationResult(
453 resourceProvider->createTexture(desc, SkBudgeted::kYes, &texels, 1));
Jim Van Verthee06b332019-01-18 10:36:32 -0500454 },
455 format, desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, SkBackingFit::kExact,
456 SkBudgeted::kYes);
457
458 if (!proxy) {
459 return nullptr;
460 }
461
Robert Phillipsa41c6852019-02-07 10:44:10 -0500462 GrContext* direct = fImageContext->priv().asDirectContext();
463 if (direct) {
464 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Jim Van Verthee06b332019-01-18 10:36:32 -0500465 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
466 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500467 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500468 return nullptr;
469 }
470 }
471 return proxy;
472}
473
Brian Salomon7578f3e2018-03-07 14:39:54 -0500474sk_sp<GrTextureProxy> GrProxyProvider::wrapBackendTexture(const GrBackendTexture& backendTex,
475 GrSurfaceOrigin origin,
476 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500477 GrWrapCacheable cacheable,
Brian Salomonc67c31c2018-12-06 10:00:03 -0500478 GrIOType ioType,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500479 ReleaseProc releaseProc,
480 ReleaseContext releaseCtx) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500481 SkASSERT(ioType != kWrite_GrIOType);
Robert Phillipsf9bec202018-01-16 09:21:01 -0500482 if (this->isAbandoned()) {
483 return nullptr;
484 }
485
Brian Salomonf7778972018-03-08 10:13:17 -0500486 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500487 GrContext* direct = fImageContext->priv().asDirectContext();
488 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500489 return nullptr;
490 }
491
Robert Phillipsa41c6852019-02-07 10:44:10 -0500492 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
493
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500494 sk_sp<GrTexture> tex =
Robert Phillipsa41c6852019-02-07 10:44:10 -0500495 resourceProvider->wrapBackendTexture(backendTex, ownership, cacheable, ioType);
Brian Salomonf7778972018-03-08 10:13:17 -0500496 if (!tex) {
497 return nullptr;
498 }
Robert Phillipsadbe1322018-01-17 13:35:46 -0500499
Greg Daniel6a0176b2018-01-30 09:28:44 -0500500 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500501 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel6a0176b2018-01-30 09:28:44 -0500502 }
503
Brian Salomonf7778972018-03-08 10:13:17 -0500504 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
505 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500506 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500507
Brian Salomonf7778972018-03-08 10:13:17 -0500508 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500509}
510
Brian Salomon7578f3e2018-03-07 14:39:54 -0500511sk_sp<GrTextureProxy> GrProxyProvider::wrapRenderableBackendTexture(
Brian Salomon02bd2952018-03-07 15:20:21 -0500512 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt,
Greg Daniel8ce79912019-02-05 10:08:43 -0500513 GrWrapOwnership ownership, GrWrapCacheable cacheable, ReleaseProc releaseProc,
514 ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500515 if (this->isAbandoned()) {
516 return nullptr;
517 }
518
Brian Salomonf7778972018-03-08 10:13:17 -0500519 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500520 GrContext* direct = fImageContext->priv().asDirectContext();
521 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500522 return nullptr;
523 }
524
Robert Phillipsa41c6852019-02-07 10:44:10 -0500525 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
526
Greg Daniel6abda432018-02-15 14:55:00 -0500527 sampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config());
Greg Danielf87651e2018-02-21 11:36:53 -0500528 if (!sampleCnt) {
529 return nullptr;
530 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500531
Robert Phillipsa41c6852019-02-07 10:44:10 -0500532 sk_sp<GrTexture> tex = resourceProvider->wrapRenderableBackendTexture(backendTex, sampleCnt,
533 ownership, cacheable);
Brian Salomonf7778972018-03-08 10:13:17 -0500534 if (!tex) {
535 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500536 }
537
Greg Daniel8ce79912019-02-05 10:08:43 -0500538 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500539 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500540 }
541
Brian Salomonf7778972018-03-08 10:13:17 -0500542 SkASSERT(tex->asRenderTarget()); // A GrTextureRenderTarget
543 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500544 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Greg Daniel6abda432018-02-15 14:55:00 -0500545
Brian Salomonf7778972018-03-08 10:13:17 -0500546 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500547}
548
Brian Salomon7578f3e2018-03-07 14:39:54 -0500549sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget(
Greg Daniel8ce79912019-02-05 10:08:43 -0500550 const GrBackendRenderTarget& backendRT, GrSurfaceOrigin origin, ReleaseProc releaseProc,
551 ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500552 if (this->isAbandoned()) {
553 return nullptr;
554 }
555
Brian Salomonf7778972018-03-08 10:13:17 -0500556 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500557 GrContext* direct = fImageContext->priv().asDirectContext();
558 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500559 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500560 }
561
Robert Phillipsa41c6852019-02-07 10:44:10 -0500562 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
563
564 sk_sp<GrRenderTarget> rt = resourceProvider->wrapBackendRenderTarget(backendRT);
Brian Salomonf7778972018-03-08 10:13:17 -0500565 if (!rt) {
566 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500567 }
Greg Daniel8ce79912019-02-05 10:08:43 -0500568
Greg Daniel8ce79912019-02-05 10:08:43 -0500569 if (releaseProc) {
Brian Salomon2ca31f82019-03-05 13:28:58 -0500570 rt->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500571 }
572
Brian Salomonf7778972018-03-08 10:13:17 -0500573 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
574 SkASSERT(!rt->getUniqueKey().isValid());
575 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500576 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Brian Salomonf7778972018-03-08 10:13:17 -0500577
578 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(std::move(rt), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500579}
580
Brian Salomon7578f3e2018-03-07 14:39:54 -0500581sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendTextureAsRenderTarget(
582 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500583 if (this->isAbandoned()) {
584 return nullptr;
585 }
586
Brian Salomonf7778972018-03-08 10:13:17 -0500587 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500588 GrContext* direct = fImageContext->priv().asDirectContext();
589 if (!direct) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500590 return nullptr;
591 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500592
Robert Phillipsa41c6852019-02-07 10:44:10 -0500593 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
594
Brian Salomonf7778972018-03-08 10:13:17 -0500595 sk_sp<GrRenderTarget> rt =
Robert Phillipsa41c6852019-02-07 10:44:10 -0500596 resourceProvider->wrapBackendTextureAsRenderTarget(backendTex, sampleCnt);
Brian Salomonf7778972018-03-08 10:13:17 -0500597 if (!rt) {
598 return nullptr;
Greg Danielf87651e2018-02-21 11:36:53 -0500599 }
Brian Salomonf7778972018-03-08 10:13:17 -0500600 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
601 SkASSERT(!rt->getUniqueKey().isValid());
Greg Danielb46add82019-01-02 14:51:29 -0500602 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500603 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielf87651e2018-02-21 11:36:53 -0500604
Brian Salomonf7778972018-03-08 10:13:17 -0500605 return sk_sp<GrSurfaceProxy>(new GrRenderTargetProxy(std::move(rt), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500606}
607
Greg Danielb46add82019-01-02 14:51:29 -0500608sk_sp<GrRenderTargetProxy> GrProxyProvider::wrapVulkanSecondaryCBAsRenderTarget(
609 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
610 if (this->isAbandoned()) {
611 return nullptr;
612 }
613
614 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500615 GrContext* direct = fImageContext->priv().asDirectContext();
616 if (!direct) {
Greg Danielb46add82019-01-02 14:51:29 -0500617 return nullptr;
618 }
619
Robert Phillipsa41c6852019-02-07 10:44:10 -0500620 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Danielb46add82019-01-02 14:51:29 -0500621
Robert Phillipsa41c6852019-02-07 10:44:10 -0500622 sk_sp<GrRenderTarget> rt = resourceProvider->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
623 vkInfo);
Greg Danielb46add82019-01-02 14:51:29 -0500624 if (!rt) {
625 return nullptr;
626 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500627
Greg Danielb46add82019-01-02 14:51:29 -0500628 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
629 SkASSERT(!rt->getUniqueKey().isValid());
630 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500631 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielb46add82019-01-02 14:51:29 -0500632
633 // All Vulkan surfaces uses top left origins.
634 return sk_sp<GrRenderTargetProxy>(
635 new GrRenderTargetProxy(std::move(rt),
636 kTopLeft_GrSurfaceOrigin,
637 GrRenderTargetProxy::WrapsVkSecondaryCB::kYes));
638}
639
Robert Phillips777707b2018-01-17 11:40:14 -0500640sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500641 const GrBackendFormat& format,
Robert Phillips777707b2018-01-17 11:40:14 -0500642 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500643 GrSurfaceOrigin origin,
Brian Salomon7226c232018-07-30 13:13:17 -0400644 GrMipMapped mipMapped,
Brian Salomon7226c232018-07-30 13:13:17 -0400645 SkBackingFit fit,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500646 SkBudgeted budgeted) {
Greg Daniel4065d452018-11-16 15:43:41 -0500647 return this->createLazyProxy(std::move(callback), format, desc, origin, mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400648 GrInternalSurfaceFlags::kNone, fit, budgeted);
Greg Daniel2a303902018-02-20 10:25:54 -0500649}
650
651sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500652 const GrBackendFormat& format,
Greg Daniel2a303902018-02-20 10:25:54 -0500653 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500654 GrSurfaceOrigin origin,
Greg Daniel2a303902018-02-20 10:25:54 -0500655 GrMipMapped mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400656 GrInternalSurfaceFlags surfaceFlags,
Brian Salomon7226c232018-07-30 13:13:17 -0400657 SkBackingFit fit,
658 SkBudgeted budgeted) {
Greg Daniela8d92112018-03-09 12:05:04 -0500659 // For non-ddl draws always make lazy proxy's single use.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500660 LazyInstantiationType lazyType = this->renderingDirectly() ? LazyInstantiationType::kSingleUse
661 : LazyInstantiationType::kMultipleUse;
Greg Daniel4065d452018-11-16 15:43:41 -0500662 return this->createLazyProxy(std::move(callback), format, desc, origin, mipMapped, surfaceFlags,
663 fit, budgeted, lazyType);
Greg Daniela8d92112018-03-09 12:05:04 -0500664}
665
666sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500667 const GrBackendFormat& format,
Greg Daniela8d92112018-03-09 12:05:04 -0500668 const GrSurfaceDesc& desc,
669 GrSurfaceOrigin origin,
670 GrMipMapped mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400671 GrInternalSurfaceFlags surfaceFlags,
Brian Salomon7226c232018-07-30 13:13:17 -0400672 SkBackingFit fit,
673 SkBudgeted budgeted,
Greg Daniela8d92112018-03-09 12:05:04 -0500674 LazyInstantiationType lazyType) {
Robert Phillips777707b2018-01-17 11:40:14 -0500675 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
676 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400677
Robert Phillipsa41c6852019-02-07 10:44:10 -0500678 if (desc.fWidth > this->caps()->maxTextureSize() ||
679 desc.fHeight > this->caps()->maxTextureSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400680 return nullptr;
681 }
682
Greg Daniel457469c2018-02-08 15:05:44 -0500683
Greg Daniel2a303902018-02-20 10:25:54 -0500684#ifdef SK_DEBUG
685 if (SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags)) {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400686 if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kMixedSampled)) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500687 SkASSERT(this->caps()->usesMixedSamples() && desc.fSampleCnt > 1);
Greg Daniel2a303902018-02-20 10:25:54 -0500688 }
Greg Daniel2a303902018-02-20 10:25:54 -0500689 }
690#endif
691
Brian Salomon2a4f9832018-03-03 22:43:43 -0500692 return sk_sp<GrTextureProxy>(
693 SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags)
Greg Daniel4065d452018-11-16 15:43:41 -0500694 ? new GrTextureRenderTargetProxy(std::move(callback), lazyType, format, desc,
695 origin, mipMapped, fit, budgeted, surfaceFlags)
696 : new GrTextureProxy(std::move(callback), lazyType, format, desc, origin,
697 mipMapped, fit, budgeted, surfaceFlags));
Robert Phillips777707b2018-01-17 11:40:14 -0500698}
699
Robert Phillipse8fabb22018-02-04 14:33:21 -0500700sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500701 LazyInstantiateCallback&& callback, const GrBackendFormat& format,
702 const GrSurfaceDesc& desc, GrSurfaceOrigin origin, GrInternalSurfaceFlags surfaceFlags,
Greg Danielb085fa92019-03-05 16:55:12 -0500703 const TextureInfo* textureInfo, SkBackingFit fit, SkBudgeted budgeted,
704 bool wrapsVkSecondaryCB) {
Robert Phillipse8fabb22018-02-04 14:33:21 -0500705 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
706 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400707
Robert Phillipsa41c6852019-02-07 10:44:10 -0500708 if (desc.fWidth > this->caps()->maxRenderTargetSize() ||
709 desc.fHeight > this->caps()->maxRenderTargetSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400710 return nullptr;
711 }
712
Greg Daniel2a303902018-02-20 10:25:54 -0500713 SkASSERT(SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags));
Greg Daniel457469c2018-02-08 15:05:44 -0500714
Greg Daniel2a303902018-02-20 10:25:54 -0500715#ifdef SK_DEBUG
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400716 if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kMixedSampled)) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500717 SkASSERT(this->caps()->usesMixedSamples() && desc.fSampleCnt > 1);
Greg Daniel2a303902018-02-20 10:25:54 -0500718 }
Greg Daniel2a303902018-02-20 10:25:54 -0500719#endif
720
Greg Daniel457469c2018-02-08 15:05:44 -0500721 using LazyInstantiationType = GrSurfaceProxy::LazyInstantiationType;
722 // For non-ddl draws always make lazy proxy's single use.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500723 LazyInstantiationType lazyType = this->renderingDirectly() ? LazyInstantiationType::kSingleUse
724 : LazyInstantiationType::kMultipleUse;
Greg Daniel457469c2018-02-08 15:05:44 -0500725
Brian Salomon7226c232018-07-30 13:13:17 -0400726 if (textureInfo) {
Greg Danielb085fa92019-03-05 16:55:12 -0500727 // Wrapped vulkan secondary command buffers don't support texturing since we won't have an
728 // actual VkImage to texture from.
729 SkASSERT(!wrapsVkSecondaryCB);
Brian Salomon7226c232018-07-30 13:13:17 -0400730 return sk_sp<GrRenderTargetProxy>(new GrTextureRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500731 std::move(callback), lazyType, format, desc, origin, textureInfo->fMipMapped,
732 fit, budgeted, surfaceFlags));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500733 }
734
Greg Danielb085fa92019-03-05 16:55:12 -0500735 GrRenderTargetProxy::WrapsVkSecondaryCB vkSCB =
736 wrapsVkSecondaryCB ? GrRenderTargetProxy::WrapsVkSecondaryCB::kYes
737 : GrRenderTargetProxy::WrapsVkSecondaryCB::kNo;
738
Brian Salomon2a4f9832018-03-03 22:43:43 -0500739 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
Greg Danielb085fa92019-03-05 16:55:12 -0500740 std::move(callback), lazyType, format, desc, origin, fit, budgeted, surfaceFlags,
741 vkSCB));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500742}
743
Chris Dalton4c458b12018-06-16 17:22:59 -0600744sk_sp<GrTextureProxy> GrProxyProvider::MakeFullyLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500745 const GrBackendFormat& format,
Chris Dalton4c458b12018-06-16 17:22:59 -0600746 Renderable renderable,
747 GrSurfaceOrigin origin,
748 GrPixelConfig config,
749 const GrCaps& caps) {
Robert Phillips777707b2018-01-17 11:40:14 -0500750 GrSurfaceDesc desc;
Greg Daniele3204862018-04-16 11:24:10 -0400751 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNoPendingIO;
Robert Phillips777707b2018-01-17 11:40:14 -0500752 if (Renderable::kYes == renderable) {
753 desc.fFlags = kRenderTarget_GrSurfaceFlag;
754 }
Robert Phillips777707b2018-01-17 11:40:14 -0500755 desc.fWidth = -1;
756 desc.fHeight = -1;
757 desc.fConfig = config;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500758 desc.fSampleCnt = 1;
Robert Phillips777707b2018-01-17 11:40:14 -0500759
Chris Dalton4c458b12018-06-16 17:22:59 -0600760 return sk_sp<GrTextureProxy>(
761 (Renderable::kYes == renderable)
Brian Salomon7226c232018-07-30 13:13:17 -0400762 ? new GrTextureRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500763 std::move(callback), LazyInstantiationType::kSingleUse, format, desc,
764 origin, GrMipMapped::kNo, SkBackingFit::kApprox, SkBudgeted::kYes,
765 surfaceFlags)
Chris Dalton4c458b12018-06-16 17:22:59 -0600766 : new GrTextureProxy(std::move(callback), LazyInstantiationType::kSingleUse,
Greg Daniel4065d452018-11-16 15:43:41 -0500767 format, desc, origin, GrMipMapped::kNo,
Brian Salomon7226c232018-07-30 13:13:17 -0400768 SkBackingFit::kApprox, SkBudgeted::kYes, surfaceFlags));
Robert Phillips777707b2018-01-17 11:40:14 -0500769}
770
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500771bool GrProxyProvider::IsFunctionallyExact(GrSurfaceProxy* proxy) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400772 const bool isInstantiated = proxy->isInstantiated();
Robert Phillipsdb3b9792018-06-12 15:18:00 -0400773 // A proxy is functionally exact if:
774 // it is exact (obvs)
775 // when it is instantiated it will be exact (i.e., power of two dimensions)
776 // it is already instantiated and the proxy covers the entire backing surface
777 return proxy->priv().isExact() ||
778 (!isInstantiated && SkIsPow2(proxy->width()) && SkIsPow2(proxy->height())) ||
779 (isInstantiated && proxy->worstCaseWidth() == proxy->width() &&
780 proxy->worstCaseHeight() == proxy->height());
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500781}
782
Robert Phillips427966a2018-12-20 17:20:43 -0500783void GrProxyProvider::processInvalidUniqueKey(const GrUniqueKey& key, GrTextureProxy* proxy,
784 InvalidateGPUResource invalidateGPUResource) {
Chris Dalton2de13dd2019-01-03 15:11:59 -0700785 SkASSERT(key.isValid());
786
Robert Phillips427966a2018-12-20 17:20:43 -0500787 if (!proxy) {
788 proxy = fUniquelyKeyedProxies.find(key);
789 }
Chris Dalton2de13dd2019-01-03 15:11:59 -0700790 SkASSERT(!proxy || proxy->getUniqueKey() == key);
791
792 // Locate the corresponding GrGpuResource (if it needs to be invalidated) before clearing the
793 // proxy's unique key. We must do it in this order because 'key' may alias the proxy's key.
794 sk_sp<GrGpuResource> invalidGpuResource;
795 if (InvalidateGPUResource::kYes == invalidateGPUResource) {
Brian Salomon01ceae92019-04-02 11:49:54 -0400796 GrContext* direct = fImageContext->priv().asDirectContext();
797 if (direct) {
798 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
799 invalidGpuResource = resourceProvider->findByUniqueKey<GrGpuResource>(key);
Chris Dalton2de13dd2019-01-03 15:11:59 -0700800 }
801 SkASSERT(!invalidGpuResource || invalidGpuResource->getUniqueKey() == key);
802 }
Robert Phillips427966a2018-12-20 17:20:43 -0500803
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500804 // Note: this method is called for the whole variety of GrGpuResources so often 'key'
805 // will not be in 'fUniquelyKeyedProxies'.
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500806 if (proxy) {
Robert Phillips427966a2018-12-20 17:20:43 -0500807 fUniquelyKeyedProxies.remove(key);
808 proxy->cacheAccess().clearUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500809 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500810
Chris Dalton2de13dd2019-01-03 15:11:59 -0700811 if (invalidGpuResource) {
812 invalidGpuResource->resourcePriv().removeUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500813 }
814}
815
Robert Phillipsa41c6852019-02-07 10:44:10 -0500816uint32_t GrProxyProvider::contextID() const {
817 return fImageContext->priv().contextID();
818}
819
820const GrCaps* GrProxyProvider::caps() const {
821 return fImageContext->priv().caps();
822}
823
824sk_sp<const GrCaps> GrProxyProvider::refCaps() const {
825 return fImageContext->priv().refCaps();
826}
827
Robert Phillipsa9162df2019-02-11 14:12:03 -0500828bool GrProxyProvider::isAbandoned() const {
829 return fImageContext->priv().abandoned();
830}
831
Robert Phillips0790f8a2018-09-18 13:11:03 -0400832void GrProxyProvider::orphanAllUniqueKeys() {
833 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
834 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
835 GrTextureProxy& tmp = *iter;
836
837 tmp.fProxyProvider = nullptr;
838 }
839}
840
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500841void GrProxyProvider::removeAllUniqueKeys() {
842 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
843 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
844 GrTextureProxy& tmp = *iter;
845
Chris Dalton2de13dd2019-01-03 15:11:59 -0700846 this->processInvalidUniqueKey(tmp.getUniqueKey(), &tmp, InvalidateGPUResource::kNo);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500847 }
848 SkASSERT(!fUniquelyKeyedProxies.count());
849}
Robert Phillipsa41c6852019-02-07 10:44:10 -0500850
851bool GrProxyProvider::renderingDirectly() const {
852 return fImageContext->priv().asDirectContext();
853}