blob: dad4d2fdad2ed843964745b4e90782cbf85516ee [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) {
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()) {
159 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin));
160 } else {
161 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin));
162 }
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 }
Robert Phillips9313aa72019-04-09 18:41:27 -0400250 if (fImageContext->priv().explicitlyAllocateGPUResources()) {
251 surfaceFlags |= GrInternalSurfaceFlags::kNoPendingIO;
252 }
Greg Daniel2a303902018-02-20 10:25:54 -0500253
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500254 GrSurfaceDesc desc;
255 desc.fWidth = srcImage->width();
256 desc.fHeight = srcImage->height();
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400257 desc.fFlags = descFlags;
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500258 desc.fSampleCnt = sampleCnt;
Greg Danielf87651e2018-02-21 11:36:53 -0500259 desc.fConfig = config;
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500260
261 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Chris Daltond004e0b2018-09-27 09:28:03 -0600262 [desc, budgeted, srcImage, fit, surfaceFlags](GrResourceProvider* resourceProvider) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500263 SkPixmap pixMap;
264 SkAssertResult(srcImage->peekPixels(&pixMap));
265 GrMipLevel mipLevel = { pixMap.addr(), pixMap.rowBytes() };
266
Chris Daltond004e0b2018-09-27 09:28:03 -0600267 auto resourceProviderFlags = GrResourceProvider::Flags::kNone;
268 if (surfaceFlags & GrInternalSurfaceFlags::kNoPendingIO) {
269 resourceProviderFlags |= GrResourceProvider::Flags::kNoPendingIO;
270 }
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400271 return LazyInstantiationResult(resourceProvider->createTexture(
272 desc, budgeted, fit, mipLevel, resourceProviderFlags));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500273 },
Greg Daniel4065d452018-11-16 15:43:41 -0500274 format, desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, surfaceFlags, fit, budgeted);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500275
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400276 if (!proxy) {
277 return nullptr;
278 }
279
Robert Phillipsa41c6852019-02-07 10:44:10 -0500280 GrContext* direct = fImageContext->priv().asDirectContext();
281 if (direct) {
282 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
283
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500284 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
285 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500286 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500287 return nullptr;
288 }
289 }
Robert Phillipsc1b60662018-06-26 10:20:08 -0400290
291 SkASSERT(proxy->width() == desc.fWidth);
292 SkASSERT(proxy->height() == desc.fHeight);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500293 return proxy;
294}
295
Greg Daniel4065d452018-11-16 15:43:41 -0500296sk_sp<GrTextureProxy> GrProxyProvider::createMipMapProxy(const GrBackendFormat& format,
297 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500298 GrSurfaceOrigin origin,
Greg Daniel30815082018-02-09 16:08:30 -0500299 SkBudgeted budgeted) {
Robert Phillips579f0942018-01-08 14:53:35 -0500300 ASSERT_SINGLE_OWNER
301
302 if (this->isAbandoned()) {
303 return nullptr;
304 }
305
Greg Daniel4065d452018-11-16 15:43:41 -0500306 return this->createProxy(format, desc, origin, GrMipMapped::kYes, SkBackingFit::kExact,
307 budgeted, GrInternalSurfaceFlags::kNone);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500308}
309
Brian Osmande496652019-03-22 13:42:33 -0400310sk_sp<GrTextureProxy> GrProxyProvider::createProxyFromBitmap(const SkBitmap& bitmap,
311 GrMipMapped mipMapped) {
Brian Osman412674f2019-02-07 15:34:58 -0500312 ASSERT_SINGLE_OWNER
Brian Osman7dcc6162019-03-25 10:12:57 -0400313 SkASSERT(GrMipMapped::kNo == mipMapped || this->caps()->mipMapSupport());
Brian Osman412674f2019-02-07 15:34:58 -0500314
315 if (this->isAbandoned()) {
316 return nullptr;
317 }
318
Brian Osman2b23c4b2018-06-01 12:25:08 -0400319 if (!SkImageInfoIsValid(bitmap.info())) {
Greg Daniela4ead652018-02-07 10:21:48 -0500320 return nullptr;
321 }
322
Brian Osmande496652019-03-22 13:42:33 -0400323 ATRACE_ANDROID_FRAMEWORK("Upload %sTexture [%ux%u]",
324 GrMipMapped::kYes == mipMapped ? "MipMap " : "",
325 bitmap.width(), bitmap.height());
Greg Daniela4ead652018-02-07 10:21:48 -0500326
327 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
328 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
329 // 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 -0500330 SkCopyPixelsMode copyMode = this->renderingDirectly() ? kNever_SkCopyPixelsMode
331 : kIfMutable_SkCopyPixelsMode;
Greg Daniela4ead652018-02-07 10:21:48 -0500332 sk_sp<SkImage> baseLevel = SkMakeImageFromRasterBitmap(bitmap, copyMode);
Greg Daniela4ead652018-02-07 10:21:48 -0500333 if (!baseLevel) {
334 return nullptr;
335 }
336
Brian Osmande496652019-03-22 13:42:33 -0400337 // If mips weren't requested (or this was too small to have any), then take the fast path
338 if (GrMipMapped::kNo == mipMapped ||
339 0 == SkMipMap::ComputeLevelCount(baseLevel->width(), baseLevel->height())) {
Brian Osman7dcc6162019-03-25 10:12:57 -0400340 return this->createTextureProxy(std::move(baseLevel), kNone_GrSurfaceFlags, 1,
341 SkBudgeted::kYes, SkBackingFit::kExact);
Greg Daniela4ead652018-02-07 10:21:48 -0500342 }
343
Robert Phillipsa41c6852019-02-07 10:44:10 -0500344 const GrBackendFormat format =
345 this->caps()->getBackendFormatFromColorType(bitmap.info().colorType());
Greg Daniel4065d452018-11-16 15:43:41 -0500346 if (!format.isValid()) {
347 return nullptr;
348 }
349
Brian Osmand29dcd12018-09-13 15:04:29 -0400350 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info());
351 if (!this->caps()->isConfigTexturable(desc.fConfig)) {
352 SkBitmap copy8888;
353 if (!copy8888.tryAllocPixels(bitmap.info().makeColorType(kRGBA_8888_SkColorType)) ||
354 !bitmap.readPixels(copy8888.pixmap())) {
355 return nullptr;
356 }
357 copy8888.setImmutable();
358 baseLevel = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode);
359 desc.fConfig = kRGBA_8888_GrPixelConfig;
360 }
361
362 SkPixmap pixmap;
363 SkAssertResult(baseLevel->peekPixels(&pixmap));
364 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(pixmap, nullptr));
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400365 if (!mipmaps) {
366 return nullptr;
367 }
368
Greg Daniela4ead652018-02-07 10:21:48 -0500369 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Osman1b97f132018-09-13 17:33:48 +0000370 [desc, baseLevel, mipmaps](GrResourceProvider* resourceProvider) {
Brian Osman1b97f132018-09-13 17:33:48 +0000371 const int mipLevelCount = mipmaps->countLevels() + 1;
372 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
373
Greg Daniela4ead652018-02-07 10:21:48 -0500374 SkPixmap pixmap;
375 SkAssertResult(baseLevel->peekPixels(&pixmap));
376
377 // DDL TODO: Instead of copying all this info into GrMipLevels we should just plumb
378 // the use of SkMipMap down through Ganesh.
379 texels[0].fPixels = pixmap.addr();
380 texels[0].fRowBytes = pixmap.rowBytes();
381
382 for (int i = 1; i < mipLevelCount; ++i) {
383 SkMipMap::Level generatedMipLevel;
384 mipmaps->getLevel(i - 1, &generatedMipLevel);
385 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
386 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
387 SkASSERT(texels[i].fPixels);
388 }
389
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400390 return LazyInstantiationResult(resourceProvider->createTexture(
391 desc, SkBudgeted::kYes, texels.get(), mipLevelCount));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500392 },
Greg Daniel4065d452018-11-16 15:43:41 -0500393 format, desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kYes, SkBackingFit::kExact,
394 SkBudgeted::kYes);
Greg Daniela4ead652018-02-07 10:21:48 -0500395
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400396 if (!proxy) {
397 return nullptr;
398 }
399
Robert Phillipsa41c6852019-02-07 10:44:10 -0500400 GrContext* direct = fImageContext->priv().asDirectContext();
401 if (direct) {
402 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Daniela4ead652018-02-07 10:21:48 -0500403 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
404 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500405 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Greg Daniela4ead652018-02-07 10:21:48 -0500406 return nullptr;
407 }
408 }
409 return proxy;
410}
411
Greg Daniel4065d452018-11-16 15:43:41 -0500412sk_sp<GrTextureProxy> GrProxyProvider::createProxy(const GrBackendFormat& format,
413 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500414 GrSurfaceOrigin origin,
Greg Danielf6f7b672018-02-15 13:06:26 -0500415 GrMipMapped mipMapped,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500416 SkBackingFit fit,
417 SkBudgeted budgeted,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400418 GrInternalSurfaceFlags surfaceFlags) {
Greg Danielf6f7b672018-02-15 13:06:26 -0500419 if (GrMipMapped::kYes == mipMapped) {
420 // SkMipMap doesn't include the base level in the level count so we have to add 1
421 int mipCount = SkMipMap::ComputeLevelCount(desc.fWidth, desc.fHeight) + 1;
422 if (1 == mipCount) {
423 mipMapped = GrMipMapped::kNo;
424 }
425 }
426
427 if (!this->caps()->validateSurfaceDesc(desc, mipMapped)) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500428 return nullptr;
429 }
Brian Salomon3a2cc2c2018-02-03 00:25:12 +0000430 GrSurfaceDesc copyDesc = desc;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500431 if (desc.fFlags & kRenderTarget_GrSurfaceFlag) {
432 copyDesc.fSampleCnt =
433 this->caps()->getRenderTargetSampleCount(desc.fSampleCnt, desc.fConfig);
434 }
Brian Salomon3a2cc2c2018-02-03 00:25:12 +0000435
Brian Salomonbdecacf2018-02-02 20:32:49 -0500436 if (copyDesc.fFlags & kRenderTarget_GrSurfaceFlag) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500437 // We know anything we instantiate later from this deferred path will be
438 // both texturable and renderable
Greg Daniel4065d452018-11-16 15:43:41 -0500439 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(*this->caps(), format, copyDesc,
440 origin, mipMapped,
Brian Salomon7226c232018-07-30 13:13:17 -0400441 fit, budgeted, surfaceFlags));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500442 }
443
Greg Daniel4065d452018-11-16 15:43:41 -0500444 return sk_sp<GrTextureProxy>(new GrTextureProxy(format, copyDesc, origin, mipMapped,
Brian Salomon7226c232018-07-30 13:13:17 -0400445 fit, budgeted, surfaceFlags));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500446}
447
Jim Van Verthee06b332019-01-18 10:36:32 -0500448sk_sp<GrTextureProxy> GrProxyProvider::createProxy(sk_sp<SkData> data, const GrSurfaceDesc& desc) {
449 if (!this->caps()->isConfigTexturable(desc.fConfig)) {
450 return nullptr;
451 }
452
453 const GrColorType ct = GrPixelConfigToColorType(desc.fConfig);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500454 const GrBackendFormat format =
455 this->caps()->getBackendFormatFromGrColorType(ct, GrSRGBEncoded::kNo);
Jim Van Verthee06b332019-01-18 10:36:32 -0500456
457 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
458 [desc, data](GrResourceProvider* resourceProvider) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500459 GrMipLevel texels;
460 texels.fPixels = data->data();
461 texels.fRowBytes = GrBytesPerPixel(desc.fConfig)*desc.fWidth;
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400462 return LazyInstantiationResult(
463 resourceProvider->createTexture(desc, SkBudgeted::kYes, &texels, 1));
Jim Van Verthee06b332019-01-18 10:36:32 -0500464 },
465 format, desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, SkBackingFit::kExact,
466 SkBudgeted::kYes);
467
468 if (!proxy) {
469 return nullptr;
470 }
471
Robert Phillipsa41c6852019-02-07 10:44:10 -0500472 GrContext* direct = fImageContext->priv().asDirectContext();
473 if (direct) {
474 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Jim Van Verthee06b332019-01-18 10:36:32 -0500475 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
476 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500477 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500478 return nullptr;
479 }
480 }
481 return proxy;
482}
483
Brian Salomon7578f3e2018-03-07 14:39:54 -0500484sk_sp<GrTextureProxy> GrProxyProvider::wrapBackendTexture(const GrBackendTexture& backendTex,
485 GrSurfaceOrigin origin,
486 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500487 GrWrapCacheable cacheable,
Brian Salomonc67c31c2018-12-06 10:00:03 -0500488 GrIOType ioType,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500489 ReleaseProc releaseProc,
490 ReleaseContext releaseCtx) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500491 SkASSERT(ioType != kWrite_GrIOType);
Robert Phillipsf9bec202018-01-16 09:21:01 -0500492 if (this->isAbandoned()) {
493 return nullptr;
494 }
495
Brian Salomonf7778972018-03-08 10:13:17 -0500496 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500497 GrContext* direct = fImageContext->priv().asDirectContext();
498 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500499 return nullptr;
500 }
501
Robert Phillipsa41c6852019-02-07 10:44:10 -0500502 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
503
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500504 sk_sp<GrTexture> tex =
Robert Phillipsa41c6852019-02-07 10:44:10 -0500505 resourceProvider->wrapBackendTexture(backendTex, ownership, cacheable, ioType);
Brian Salomonf7778972018-03-08 10:13:17 -0500506 if (!tex) {
507 return nullptr;
508 }
Robert Phillipsadbe1322018-01-17 13:35:46 -0500509
Greg Daniel6a0176b2018-01-30 09:28:44 -0500510 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500511 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel6a0176b2018-01-30 09:28:44 -0500512 }
513
Brian Salomonf7778972018-03-08 10:13:17 -0500514 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
515 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500516 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500517
Brian Salomonf7778972018-03-08 10:13:17 -0500518 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500519}
520
Brian Salomon7578f3e2018-03-07 14:39:54 -0500521sk_sp<GrTextureProxy> GrProxyProvider::wrapRenderableBackendTexture(
Brian Salomon02bd2952018-03-07 15:20:21 -0500522 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt,
Greg Daniel8ce79912019-02-05 10:08:43 -0500523 GrWrapOwnership ownership, GrWrapCacheable cacheable, ReleaseProc releaseProc,
524 ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500525 if (this->isAbandoned()) {
526 return nullptr;
527 }
528
Brian Salomonf7778972018-03-08 10:13:17 -0500529 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500530 GrContext* direct = fImageContext->priv().asDirectContext();
531 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500532 return nullptr;
533 }
534
Robert Phillipsa41c6852019-02-07 10:44:10 -0500535 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
536
Greg Daniel6abda432018-02-15 14:55:00 -0500537 sampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config());
Greg Danielf87651e2018-02-21 11:36:53 -0500538 if (!sampleCnt) {
539 return nullptr;
540 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500541
Robert Phillipsa41c6852019-02-07 10:44:10 -0500542 sk_sp<GrTexture> tex = resourceProvider->wrapRenderableBackendTexture(backendTex, sampleCnt,
543 ownership, cacheable);
Brian Salomonf7778972018-03-08 10:13:17 -0500544 if (!tex) {
545 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500546 }
547
Greg Daniel8ce79912019-02-05 10:08:43 -0500548 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500549 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500550 }
551
Brian Salomonf7778972018-03-08 10:13:17 -0500552 SkASSERT(tex->asRenderTarget()); // A GrTextureRenderTarget
553 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500554 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Greg Daniel6abda432018-02-15 14:55:00 -0500555
Brian Salomonf7778972018-03-08 10:13:17 -0500556 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500557}
558
Brian Salomon7578f3e2018-03-07 14:39:54 -0500559sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget(
Greg Daniel8ce79912019-02-05 10:08:43 -0500560 const GrBackendRenderTarget& backendRT, GrSurfaceOrigin origin, ReleaseProc releaseProc,
561 ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500562 if (this->isAbandoned()) {
563 return nullptr;
564 }
565
Brian Salomonf7778972018-03-08 10:13:17 -0500566 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500567 GrContext* direct = fImageContext->priv().asDirectContext();
568 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500569 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500570 }
571
Robert Phillipsa41c6852019-02-07 10:44:10 -0500572 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
573
574 sk_sp<GrRenderTarget> rt = resourceProvider->wrapBackendRenderTarget(backendRT);
Brian Salomonf7778972018-03-08 10:13:17 -0500575 if (!rt) {
576 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500577 }
Greg Daniel8ce79912019-02-05 10:08:43 -0500578
Greg Daniel8ce79912019-02-05 10:08:43 -0500579 if (releaseProc) {
Brian Salomon2ca31f82019-03-05 13:28:58 -0500580 rt->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500581 }
582
Brian Salomonf7778972018-03-08 10:13:17 -0500583 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
584 SkASSERT(!rt->getUniqueKey().isValid());
585 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500586 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Brian Salomonf7778972018-03-08 10:13:17 -0500587
588 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(std::move(rt), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500589}
590
Brian Salomon7578f3e2018-03-07 14:39:54 -0500591sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendTextureAsRenderTarget(
592 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500593 if (this->isAbandoned()) {
594 return nullptr;
595 }
596
Brian Salomonf7778972018-03-08 10:13:17 -0500597 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500598 GrContext* direct = fImageContext->priv().asDirectContext();
599 if (!direct) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500600 return nullptr;
601 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500602
Robert Phillipsa41c6852019-02-07 10:44:10 -0500603 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
604
Brian Salomonf7778972018-03-08 10:13:17 -0500605 sk_sp<GrRenderTarget> rt =
Robert Phillipsa41c6852019-02-07 10:44:10 -0500606 resourceProvider->wrapBackendTextureAsRenderTarget(backendTex, sampleCnt);
Brian Salomonf7778972018-03-08 10:13:17 -0500607 if (!rt) {
608 return nullptr;
Greg Danielf87651e2018-02-21 11:36:53 -0500609 }
Brian Salomonf7778972018-03-08 10:13:17 -0500610 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
611 SkASSERT(!rt->getUniqueKey().isValid());
Greg Danielb46add82019-01-02 14:51:29 -0500612 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500613 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielf87651e2018-02-21 11:36:53 -0500614
Brian Salomonf7778972018-03-08 10:13:17 -0500615 return sk_sp<GrSurfaceProxy>(new GrRenderTargetProxy(std::move(rt), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500616}
617
Greg Danielb46add82019-01-02 14:51:29 -0500618sk_sp<GrRenderTargetProxy> GrProxyProvider::wrapVulkanSecondaryCBAsRenderTarget(
619 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
620 if (this->isAbandoned()) {
621 return nullptr;
622 }
623
624 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500625 GrContext* direct = fImageContext->priv().asDirectContext();
626 if (!direct) {
Greg Danielb46add82019-01-02 14:51:29 -0500627 return nullptr;
628 }
629
Robert Phillipsa41c6852019-02-07 10:44:10 -0500630 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Danielb46add82019-01-02 14:51:29 -0500631
Robert Phillipsa41c6852019-02-07 10:44:10 -0500632 sk_sp<GrRenderTarget> rt = resourceProvider->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
633 vkInfo);
Greg Danielb46add82019-01-02 14:51:29 -0500634 if (!rt) {
635 return nullptr;
636 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500637
Greg Danielb46add82019-01-02 14:51:29 -0500638 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
639 SkASSERT(!rt->getUniqueKey().isValid());
640 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500641 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielb46add82019-01-02 14:51:29 -0500642
643 // All Vulkan surfaces uses top left origins.
644 return sk_sp<GrRenderTargetProxy>(
645 new GrRenderTargetProxy(std::move(rt),
646 kTopLeft_GrSurfaceOrigin,
647 GrRenderTargetProxy::WrapsVkSecondaryCB::kYes));
648}
649
Robert Phillips777707b2018-01-17 11:40:14 -0500650sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500651 const GrBackendFormat& format,
Robert Phillips777707b2018-01-17 11:40:14 -0500652 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500653 GrSurfaceOrigin origin,
Brian Salomon7226c232018-07-30 13:13:17 -0400654 GrMipMapped mipMapped,
Brian Salomon7226c232018-07-30 13:13:17 -0400655 SkBackingFit fit,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500656 SkBudgeted budgeted) {
Greg Daniel4065d452018-11-16 15:43:41 -0500657 return this->createLazyProxy(std::move(callback), format, desc, origin, mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400658 GrInternalSurfaceFlags::kNone, fit, budgeted);
Greg Daniel2a303902018-02-20 10:25:54 -0500659}
660
661sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500662 const GrBackendFormat& format,
Greg Daniel2a303902018-02-20 10:25:54 -0500663 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500664 GrSurfaceOrigin origin,
Greg Daniel2a303902018-02-20 10:25:54 -0500665 GrMipMapped mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400666 GrInternalSurfaceFlags surfaceFlags,
Brian Salomon7226c232018-07-30 13:13:17 -0400667 SkBackingFit fit,
668 SkBudgeted budgeted) {
Greg Daniela8d92112018-03-09 12:05:04 -0500669 // For non-ddl draws always make lazy proxy's single use.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500670 LazyInstantiationType lazyType = this->renderingDirectly() ? LazyInstantiationType::kSingleUse
671 : LazyInstantiationType::kMultipleUse;
Greg Daniel4065d452018-11-16 15:43:41 -0500672 return this->createLazyProxy(std::move(callback), format, desc, origin, mipMapped, surfaceFlags,
673 fit, budgeted, lazyType);
Greg Daniela8d92112018-03-09 12:05:04 -0500674}
675
676sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500677 const GrBackendFormat& format,
Greg Daniela8d92112018-03-09 12:05:04 -0500678 const GrSurfaceDesc& desc,
679 GrSurfaceOrigin origin,
680 GrMipMapped mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400681 GrInternalSurfaceFlags surfaceFlags,
Brian Salomon7226c232018-07-30 13:13:17 -0400682 SkBackingFit fit,
683 SkBudgeted budgeted,
Greg Daniela8d92112018-03-09 12:05:04 -0500684 LazyInstantiationType lazyType) {
Robert Phillips777707b2018-01-17 11:40:14 -0500685 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
686 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400687
Robert Phillipsa41c6852019-02-07 10:44:10 -0500688 if (desc.fWidth > this->caps()->maxTextureSize() ||
689 desc.fHeight > this->caps()->maxTextureSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400690 return nullptr;
691 }
692
Greg Daniel457469c2018-02-08 15:05:44 -0500693
Greg Daniel2a303902018-02-20 10:25:54 -0500694#ifdef SK_DEBUG
695 if (SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags)) {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400696 if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kMixedSampled)) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500697 SkASSERT(this->caps()->usesMixedSamples() && desc.fSampleCnt > 1);
Greg Daniel2a303902018-02-20 10:25:54 -0500698 }
Greg Daniel2a303902018-02-20 10:25:54 -0500699 }
700#endif
701
Brian Salomon2a4f9832018-03-03 22:43:43 -0500702 return sk_sp<GrTextureProxy>(
703 SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags)
Greg Daniel4065d452018-11-16 15:43:41 -0500704 ? new GrTextureRenderTargetProxy(std::move(callback), lazyType, format, desc,
705 origin, mipMapped, fit, budgeted, surfaceFlags)
706 : new GrTextureProxy(std::move(callback), lazyType, format, desc, origin,
707 mipMapped, fit, budgeted, surfaceFlags));
Robert Phillips777707b2018-01-17 11:40:14 -0500708}
709
Robert Phillipse8fabb22018-02-04 14:33:21 -0500710sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500711 LazyInstantiateCallback&& callback, const GrBackendFormat& format,
712 const GrSurfaceDesc& desc, GrSurfaceOrigin origin, GrInternalSurfaceFlags surfaceFlags,
Greg Danielb085fa92019-03-05 16:55:12 -0500713 const TextureInfo* textureInfo, SkBackingFit fit, SkBudgeted budgeted,
714 bool wrapsVkSecondaryCB) {
Robert Phillipse8fabb22018-02-04 14:33:21 -0500715 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
716 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400717
Robert Phillipsa41c6852019-02-07 10:44:10 -0500718 if (desc.fWidth > this->caps()->maxRenderTargetSize() ||
719 desc.fHeight > this->caps()->maxRenderTargetSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400720 return nullptr;
721 }
722
Greg Daniel2a303902018-02-20 10:25:54 -0500723 SkASSERT(SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags));
Greg Daniel457469c2018-02-08 15:05:44 -0500724
Greg Daniel2a303902018-02-20 10:25:54 -0500725#ifdef SK_DEBUG
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400726 if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kMixedSampled)) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500727 SkASSERT(this->caps()->usesMixedSamples() && desc.fSampleCnt > 1);
Greg Daniel2a303902018-02-20 10:25:54 -0500728 }
Greg Daniel2a303902018-02-20 10:25:54 -0500729#endif
730
Greg Daniel457469c2018-02-08 15:05:44 -0500731 using LazyInstantiationType = GrSurfaceProxy::LazyInstantiationType;
732 // For non-ddl draws always make lazy proxy's single use.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500733 LazyInstantiationType lazyType = this->renderingDirectly() ? LazyInstantiationType::kSingleUse
734 : LazyInstantiationType::kMultipleUse;
Greg Daniel457469c2018-02-08 15:05:44 -0500735
Brian Salomon7226c232018-07-30 13:13:17 -0400736 if (textureInfo) {
Greg Danielb085fa92019-03-05 16:55:12 -0500737 // Wrapped vulkan secondary command buffers don't support texturing since we won't have an
738 // actual VkImage to texture from.
739 SkASSERT(!wrapsVkSecondaryCB);
Brian Salomon7226c232018-07-30 13:13:17 -0400740 return sk_sp<GrRenderTargetProxy>(new GrTextureRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500741 std::move(callback), lazyType, format, desc, origin, textureInfo->fMipMapped,
742 fit, budgeted, surfaceFlags));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500743 }
744
Greg Danielb085fa92019-03-05 16:55:12 -0500745 GrRenderTargetProxy::WrapsVkSecondaryCB vkSCB =
746 wrapsVkSecondaryCB ? GrRenderTargetProxy::WrapsVkSecondaryCB::kYes
747 : GrRenderTargetProxy::WrapsVkSecondaryCB::kNo;
748
Brian Salomon2a4f9832018-03-03 22:43:43 -0500749 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
Greg Danielb085fa92019-03-05 16:55:12 -0500750 std::move(callback), lazyType, format, desc, origin, fit, budgeted, surfaceFlags,
751 vkSCB));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500752}
753
Chris Dalton19cc00f2019-04-15 09:06:28 -0600754sk_sp<GrTextureProxy> GrProxyProvider::MakeFullyLazyProxy(
755 LazyInstantiateCallback&& callback, const GrBackendFormat& format, Renderable renderable,
756 GrSurfaceOrigin origin, GrPixelConfig config, const GrCaps& caps, int sampleCnt) {
Robert Phillips777707b2018-01-17 11:40:14 -0500757 GrSurfaceDesc desc;
Greg Daniele3204862018-04-16 11:24:10 -0400758 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNoPendingIO;
Robert Phillips777707b2018-01-17 11:40:14 -0500759 if (Renderable::kYes == renderable) {
760 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Chris Dalton19cc00f2019-04-15 09:06:28 -0600761 if (sampleCnt > 1 && caps.usesMixedSamples()) {
762 surfaceFlags |= GrInternalSurfaceFlags::kMixedSampled;
763 }
Robert Phillips777707b2018-01-17 11:40:14 -0500764 }
Robert Phillips777707b2018-01-17 11:40:14 -0500765 desc.fWidth = -1;
766 desc.fHeight = -1;
767 desc.fConfig = config;
Chris Dalton19cc00f2019-04-15 09:06:28 -0600768 desc.fSampleCnt = sampleCnt;
Robert Phillips777707b2018-01-17 11:40:14 -0500769
Chris Dalton4c458b12018-06-16 17:22:59 -0600770 return sk_sp<GrTextureProxy>(
771 (Renderable::kYes == renderable)
Brian Salomon7226c232018-07-30 13:13:17 -0400772 ? new GrTextureRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500773 std::move(callback), LazyInstantiationType::kSingleUse, format, desc,
774 origin, GrMipMapped::kNo, SkBackingFit::kApprox, SkBudgeted::kYes,
775 surfaceFlags)
Chris Dalton4c458b12018-06-16 17:22:59 -0600776 : new GrTextureProxy(std::move(callback), LazyInstantiationType::kSingleUse,
Greg Daniel4065d452018-11-16 15:43:41 -0500777 format, desc, origin, GrMipMapped::kNo,
Brian Salomon7226c232018-07-30 13:13:17 -0400778 SkBackingFit::kApprox, SkBudgeted::kYes, surfaceFlags));
Robert Phillips777707b2018-01-17 11:40:14 -0500779}
780
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500781bool GrProxyProvider::IsFunctionallyExact(GrSurfaceProxy* proxy) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400782 const bool isInstantiated = proxy->isInstantiated();
Robert Phillipsdb3b9792018-06-12 15:18:00 -0400783 // A proxy is functionally exact if:
784 // it is exact (obvs)
785 // when it is instantiated it will be exact (i.e., power of two dimensions)
786 // it is already instantiated and the proxy covers the entire backing surface
787 return proxy->priv().isExact() ||
788 (!isInstantiated && SkIsPow2(proxy->width()) && SkIsPow2(proxy->height())) ||
789 (isInstantiated && proxy->worstCaseWidth() == proxy->width() &&
790 proxy->worstCaseHeight() == proxy->height());
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500791}
792
Robert Phillips427966a2018-12-20 17:20:43 -0500793void GrProxyProvider::processInvalidUniqueKey(const GrUniqueKey& key, GrTextureProxy* proxy,
794 InvalidateGPUResource invalidateGPUResource) {
Chris Dalton2de13dd2019-01-03 15:11:59 -0700795 SkASSERT(key.isValid());
796
Robert Phillips427966a2018-12-20 17:20:43 -0500797 if (!proxy) {
798 proxy = fUniquelyKeyedProxies.find(key);
799 }
Chris Dalton2de13dd2019-01-03 15:11:59 -0700800 SkASSERT(!proxy || proxy->getUniqueKey() == key);
801
802 // Locate the corresponding GrGpuResource (if it needs to be invalidated) before clearing the
803 // proxy's unique key. We must do it in this order because 'key' may alias the proxy's key.
804 sk_sp<GrGpuResource> invalidGpuResource;
805 if (InvalidateGPUResource::kYes == invalidateGPUResource) {
Brian Salomon01ceae92019-04-02 11:49:54 -0400806 GrContext* direct = fImageContext->priv().asDirectContext();
807 if (direct) {
808 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
809 invalidGpuResource = resourceProvider->findByUniqueKey<GrGpuResource>(key);
Chris Dalton2de13dd2019-01-03 15:11:59 -0700810 }
811 SkASSERT(!invalidGpuResource || invalidGpuResource->getUniqueKey() == key);
812 }
Robert Phillips427966a2018-12-20 17:20:43 -0500813
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500814 // Note: this method is called for the whole variety of GrGpuResources so often 'key'
815 // will not be in 'fUniquelyKeyedProxies'.
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500816 if (proxy) {
Robert Phillips427966a2018-12-20 17:20:43 -0500817 fUniquelyKeyedProxies.remove(key);
818 proxy->cacheAccess().clearUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500819 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500820
Chris Dalton2de13dd2019-01-03 15:11:59 -0700821 if (invalidGpuResource) {
822 invalidGpuResource->resourcePriv().removeUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500823 }
824}
825
Robert Phillipsa41c6852019-02-07 10:44:10 -0500826uint32_t GrProxyProvider::contextID() const {
827 return fImageContext->priv().contextID();
828}
829
830const GrCaps* GrProxyProvider::caps() const {
831 return fImageContext->priv().caps();
832}
833
834sk_sp<const GrCaps> GrProxyProvider::refCaps() const {
835 return fImageContext->priv().refCaps();
836}
837
Robert Phillipsa9162df2019-02-11 14:12:03 -0500838bool GrProxyProvider::isAbandoned() const {
839 return fImageContext->priv().abandoned();
840}
841
Robert Phillips0790f8a2018-09-18 13:11:03 -0400842void GrProxyProvider::orphanAllUniqueKeys() {
843 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
844 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
845 GrTextureProxy& tmp = *iter;
846
847 tmp.fProxyProvider = nullptr;
848 }
849}
850
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500851void GrProxyProvider::removeAllUniqueKeys() {
852 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
853 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
854 GrTextureProxy& tmp = *iter;
855
Chris Dalton2de13dd2019-01-03 15:11:59 -0700856 this->processInvalidUniqueKey(tmp.getUniqueKey(), &tmp, InvalidateGPUResource::kNo);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500857 }
858 SkASSERT(!fUniquelyKeyedProxies.count());
859}
Robert Phillipsa41c6852019-02-07 10:44:10 -0500860
861bool GrProxyProvider::renderingDirectly() const {
862 return fImageContext->priv().asDirectContext();
863}