blob: f33755d23747f7c090a8372f8f2d3425d85c13f9 [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) {
133 tex = resourceProvider->createApproxTexture(desc, GrResourceProvider::Flags::kNone);
134 } else {
135 tex = resourceProvider->createTexture(desc, budgeted, GrResourceProvider::Flags::kNone);
136 }
137 if (!tex) {
138 return nullptr;
139 }
140
141 return this->createWrapped(std::move(tex), origin);
142}
143
144sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createWrapped(sk_sp<GrTexture> tex,
145 GrSurfaceOrigin origin) {
146 return this->createWrapped(std::move(tex), origin);
147}
148#endif
149
Robert Phillipsadbe1322018-01-17 13:35:46 -0500150sk_sp<GrTextureProxy> GrProxyProvider::createWrapped(sk_sp<GrTexture> tex, GrSurfaceOrigin origin) {
151#ifdef SK_DEBUG
152 if (tex->getUniqueKey().isValid()) {
153 SkASSERT(!this->findProxyByUniqueKey(tex->getUniqueKey(), origin));
154 }
155#endif
156
157 if (tex->asRenderTarget()) {
158 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin));
159 } else {
160 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin));
161 }
162}
163
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500164sk_sp<GrTextureProxy> GrProxyProvider::findOrCreateProxyByUniqueKey(const GrUniqueKey& key,
165 GrSurfaceOrigin origin) {
166 ASSERT_SINGLE_OWNER
167
168 if (this->isAbandoned()) {
169 return nullptr;
170 }
171
172 sk_sp<GrTextureProxy> result = this->findProxyByUniqueKey(key, origin);
173 if (result) {
174 return result;
175 }
176
Robert Phillipsa41c6852019-02-07 10:44:10 -0500177 GrContext* direct = fImageContext->priv().asDirectContext();
178 if (!direct) {
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500179 return nullptr;
180 }
181
Robert Phillipsa41c6852019-02-07 10:44:10 -0500182 GrResourceCache* resourceCache = direct->priv().getResourceCache();
183
184 GrGpuResource* resource = resourceCache->findAndRefUniqueResource(key);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500185 if (!resource) {
186 return nullptr;
187 }
188
189 sk_sp<GrTexture> texture(static_cast<GrSurface*>(resource)->asTexture());
190 SkASSERT(texture);
191
Robert Phillipsadbe1322018-01-17 13:35:46 -0500192 result = this->createWrapped(std::move(texture), origin);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500193 SkASSERT(result->getUniqueKey() == key);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500194 // createWrapped should've added this for us
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500195 SkASSERT(fUniquelyKeyedProxies.find(key));
196 return result;
197}
198
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500199sk_sp<GrTextureProxy> GrProxyProvider::createTextureProxy(sk_sp<SkImage> srcImage,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400200 GrSurfaceDescFlags descFlags,
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500201 int sampleCnt,
Greg Danielfb3abcd2018-02-02 15:48:33 -0500202 SkBudgeted budgeted,
Chris Daltond004e0b2018-09-27 09:28:03 -0600203 SkBackingFit fit,
204 GrInternalSurfaceFlags surfaceFlags) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500205 ASSERT_SINGLE_OWNER
206 SkASSERT(srcImage);
207
208 if (this->isAbandoned()) {
209 return nullptr;
210 }
211
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400212 const SkImageInfo& info = srcImage->imageInfo();
Brian Osmand29dcd12018-09-13 15:04:29 -0400213 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
Greg Danielf87651e2018-02-21 11:36:53 -0500214
Greg Daniel0a7aa142018-02-21 13:02:32 -0500215 if (kUnknown_GrPixelConfig == config) {
216 return nullptr;
217 }
218
Robert Phillipsa41c6852019-02-07 10:44:10 -0500219 GrBackendFormat format = this->caps()->getBackendFormatFromColorType(info.colorType());
Greg Daniel4065d452018-11-16 15:43:41 -0500220 if (!format.isValid()) {
221 return nullptr;
222 }
223
Brian Osmand29dcd12018-09-13 15:04:29 -0400224 if (!this->caps()->isConfigTexturable(config)) {
225 SkBitmap copy8888;
226 if (!copy8888.tryAllocPixels(info.makeColorType(kRGBA_8888_SkColorType)) ||
227 !srcImage->readPixels(copy8888.pixmap(), 0, 0)) {
228 return nullptr;
229 }
230 copy8888.setImmutable();
231 srcImage = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode);
232 config = kRGBA_8888_GrPixelConfig;
233 }
234
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400235 if (SkToBool(descFlags & kRenderTarget_GrSurfaceFlag)) {
Greg Danielf87651e2018-02-21 11:36:53 -0500236 sampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, config);
237 if (!sampleCnt) {
238 return nullptr;
239 }
240 }
241
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400242 if (SkToBool(descFlags & kRenderTarget_GrSurfaceFlag)) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500243 if (this->caps()->usesMixedSamples() && sampleCnt > 1) {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400244 surfaceFlags |= GrInternalSurfaceFlags::kMixedSampled;
Greg Daniel2a303902018-02-20 10:25:54 -0500245 }
Greg Daniel2a303902018-02-20 10:25:54 -0500246 }
247
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500248 GrSurfaceDesc desc;
249 desc.fWidth = srcImage->width();
250 desc.fHeight = srcImage->height();
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400251 desc.fFlags = descFlags;
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500252 desc.fSampleCnt = sampleCnt;
Greg Danielf87651e2018-02-21 11:36:53 -0500253 desc.fConfig = config;
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500254
255 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Chris Daltond004e0b2018-09-27 09:28:03 -0600256 [desc, budgeted, srcImage, fit, surfaceFlags](GrResourceProvider* resourceProvider) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500257 SkPixmap pixMap;
258 SkAssertResult(srcImage->peekPixels(&pixMap));
259 GrMipLevel mipLevel = { pixMap.addr(), pixMap.rowBytes() };
260
Chris Daltond004e0b2018-09-27 09:28:03 -0600261 auto resourceProviderFlags = GrResourceProvider::Flags::kNone;
262 if (surfaceFlags & GrInternalSurfaceFlags::kNoPendingIO) {
263 resourceProviderFlags |= GrResourceProvider::Flags::kNoPendingIO;
264 }
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400265 return LazyInstantiationResult(resourceProvider->createTexture(
266 desc, budgeted, fit, mipLevel, resourceProviderFlags));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500267 },
Greg Daniel4065d452018-11-16 15:43:41 -0500268 format, desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, surfaceFlags, fit, budgeted);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500269
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400270 if (!proxy) {
271 return nullptr;
272 }
273
Robert Phillipsa41c6852019-02-07 10:44:10 -0500274 GrContext* direct = fImageContext->priv().asDirectContext();
275 if (direct) {
276 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
277
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500278 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
279 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500280 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500281 return nullptr;
282 }
283 }
Robert Phillipsc1b60662018-06-26 10:20:08 -0400284
285 SkASSERT(proxy->width() == desc.fWidth);
286 SkASSERT(proxy->height() == desc.fHeight);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500287 return proxy;
288}
289
Greg Daniel4065d452018-11-16 15:43:41 -0500290sk_sp<GrTextureProxy> GrProxyProvider::createMipMapProxy(const GrBackendFormat& format,
291 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500292 GrSurfaceOrigin origin,
Greg Daniel30815082018-02-09 16:08:30 -0500293 SkBudgeted budgeted) {
Robert Phillips579f0942018-01-08 14:53:35 -0500294 ASSERT_SINGLE_OWNER
295
296 if (this->isAbandoned()) {
297 return nullptr;
298 }
299
Greg Daniel4065d452018-11-16 15:43:41 -0500300 return this->createProxy(format, desc, origin, GrMipMapped::kYes, SkBackingFit::kExact,
301 budgeted, GrInternalSurfaceFlags::kNone);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500302}
303
Brian Osmande496652019-03-22 13:42:33 -0400304sk_sp<GrTextureProxy> GrProxyProvider::createProxyFromBitmap(const SkBitmap& bitmap,
305 GrMipMapped mipMapped) {
Brian Osman412674f2019-02-07 15:34:58 -0500306 ASSERT_SINGLE_OWNER
Brian Osman7dcc6162019-03-25 10:12:57 -0400307 SkASSERT(GrMipMapped::kNo == mipMapped || this->caps()->mipMapSupport());
Brian Osman412674f2019-02-07 15:34:58 -0500308
309 if (this->isAbandoned()) {
310 return nullptr;
311 }
312
Brian Osman2b23c4b2018-06-01 12:25:08 -0400313 if (!SkImageInfoIsValid(bitmap.info())) {
Greg Daniela4ead652018-02-07 10:21:48 -0500314 return nullptr;
315 }
316
Brian Osmande496652019-03-22 13:42:33 -0400317 ATRACE_ANDROID_FRAMEWORK("Upload %sTexture [%ux%u]",
318 GrMipMapped::kYes == mipMapped ? "MipMap " : "",
319 bitmap.width(), bitmap.height());
Greg Daniela4ead652018-02-07 10:21:48 -0500320
321 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
322 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
323 // 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 -0500324 SkCopyPixelsMode copyMode = this->renderingDirectly() ? kNever_SkCopyPixelsMode
325 : kIfMutable_SkCopyPixelsMode;
Greg Daniela4ead652018-02-07 10:21:48 -0500326 sk_sp<SkImage> baseLevel = SkMakeImageFromRasterBitmap(bitmap, copyMode);
Greg Daniela4ead652018-02-07 10:21:48 -0500327 if (!baseLevel) {
328 return nullptr;
329 }
330
Brian Osmande496652019-03-22 13:42:33 -0400331 // If mips weren't requested (or this was too small to have any), then take the fast path
332 if (GrMipMapped::kNo == mipMapped ||
333 0 == SkMipMap::ComputeLevelCount(baseLevel->width(), baseLevel->height())) {
Brian Osman7dcc6162019-03-25 10:12:57 -0400334 return this->createTextureProxy(std::move(baseLevel), kNone_GrSurfaceFlags, 1,
335 SkBudgeted::kYes, SkBackingFit::kExact);
Greg Daniela4ead652018-02-07 10:21:48 -0500336 }
337
Robert Phillipsa41c6852019-02-07 10:44:10 -0500338 const GrBackendFormat format =
339 this->caps()->getBackendFormatFromColorType(bitmap.info().colorType());
Greg Daniel4065d452018-11-16 15:43:41 -0500340 if (!format.isValid()) {
341 return nullptr;
342 }
343
Brian Osmand29dcd12018-09-13 15:04:29 -0400344 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info());
345 if (!this->caps()->isConfigTexturable(desc.fConfig)) {
346 SkBitmap copy8888;
347 if (!copy8888.tryAllocPixels(bitmap.info().makeColorType(kRGBA_8888_SkColorType)) ||
348 !bitmap.readPixels(copy8888.pixmap())) {
349 return nullptr;
350 }
351 copy8888.setImmutable();
352 baseLevel = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode);
353 desc.fConfig = kRGBA_8888_GrPixelConfig;
354 }
355
356 SkPixmap pixmap;
357 SkAssertResult(baseLevel->peekPixels(&pixmap));
358 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(pixmap, nullptr));
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400359 if (!mipmaps) {
360 return nullptr;
361 }
362
Greg Daniela4ead652018-02-07 10:21:48 -0500363 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Osman1b97f132018-09-13 17:33:48 +0000364 [desc, baseLevel, mipmaps](GrResourceProvider* resourceProvider) {
Brian Osman1b97f132018-09-13 17:33:48 +0000365 const int mipLevelCount = mipmaps->countLevels() + 1;
366 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
367
Greg Daniela4ead652018-02-07 10:21:48 -0500368 SkPixmap pixmap;
369 SkAssertResult(baseLevel->peekPixels(&pixmap));
370
371 // DDL TODO: Instead of copying all this info into GrMipLevels we should just plumb
372 // the use of SkMipMap down through Ganesh.
373 texels[0].fPixels = pixmap.addr();
374 texels[0].fRowBytes = pixmap.rowBytes();
375
376 for (int i = 1; i < mipLevelCount; ++i) {
377 SkMipMap::Level generatedMipLevel;
378 mipmaps->getLevel(i - 1, &generatedMipLevel);
379 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
380 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
381 SkASSERT(texels[i].fPixels);
382 }
383
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400384 return LazyInstantiationResult(resourceProvider->createTexture(
385 desc, SkBudgeted::kYes, texels.get(), mipLevelCount));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500386 },
Greg Daniel4065d452018-11-16 15:43:41 -0500387 format, desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kYes, SkBackingFit::kExact,
388 SkBudgeted::kYes);
Greg Daniela4ead652018-02-07 10:21:48 -0500389
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400390 if (!proxy) {
391 return nullptr;
392 }
393
Robert Phillipsa41c6852019-02-07 10:44:10 -0500394 GrContext* direct = fImageContext->priv().asDirectContext();
395 if (direct) {
396 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Daniela4ead652018-02-07 10:21:48 -0500397 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
398 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500399 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Greg Daniela4ead652018-02-07 10:21:48 -0500400 return nullptr;
401 }
402 }
403 return proxy;
404}
405
Greg Daniel4065d452018-11-16 15:43:41 -0500406sk_sp<GrTextureProxy> GrProxyProvider::createProxy(const GrBackendFormat& format,
407 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500408 GrSurfaceOrigin origin,
Greg Danielf6f7b672018-02-15 13:06:26 -0500409 GrMipMapped mipMapped,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500410 SkBackingFit fit,
411 SkBudgeted budgeted,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400412 GrInternalSurfaceFlags surfaceFlags) {
Greg Danielf6f7b672018-02-15 13:06:26 -0500413 if (GrMipMapped::kYes == mipMapped) {
414 // SkMipMap doesn't include the base level in the level count so we have to add 1
415 int mipCount = SkMipMap::ComputeLevelCount(desc.fWidth, desc.fHeight) + 1;
416 if (1 == mipCount) {
417 mipMapped = GrMipMapped::kNo;
418 }
419 }
420
421 if (!this->caps()->validateSurfaceDesc(desc, mipMapped)) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500422 return nullptr;
423 }
Brian Salomon3a2cc2c2018-02-03 00:25:12 +0000424 GrSurfaceDesc copyDesc = desc;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500425 if (desc.fFlags & kRenderTarget_GrSurfaceFlag) {
426 copyDesc.fSampleCnt =
427 this->caps()->getRenderTargetSampleCount(desc.fSampleCnt, desc.fConfig);
428 }
Brian Salomon3a2cc2c2018-02-03 00:25:12 +0000429
Brian Salomonbdecacf2018-02-02 20:32:49 -0500430 if (copyDesc.fFlags & kRenderTarget_GrSurfaceFlag) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500431 // We know anything we instantiate later from this deferred path will be
432 // both texturable and renderable
Greg Daniel4065d452018-11-16 15:43:41 -0500433 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(*this->caps(), format, copyDesc,
434 origin, mipMapped,
Brian Salomon7226c232018-07-30 13:13:17 -0400435 fit, budgeted, surfaceFlags));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500436 }
437
Greg Daniel4065d452018-11-16 15:43:41 -0500438 return sk_sp<GrTextureProxy>(new GrTextureProxy(format, copyDesc, origin, mipMapped,
Brian Salomon7226c232018-07-30 13:13:17 -0400439 fit, budgeted, surfaceFlags));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500440}
441
Jim Van Verthee06b332019-01-18 10:36:32 -0500442sk_sp<GrTextureProxy> GrProxyProvider::createProxy(sk_sp<SkData> data, const GrSurfaceDesc& desc) {
443 if (!this->caps()->isConfigTexturable(desc.fConfig)) {
444 return nullptr;
445 }
446
447 const GrColorType ct = GrPixelConfigToColorType(desc.fConfig);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500448 const GrBackendFormat format =
449 this->caps()->getBackendFormatFromGrColorType(ct, GrSRGBEncoded::kNo);
Jim Van Verthee06b332019-01-18 10:36:32 -0500450
451 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
452 [desc, data](GrResourceProvider* resourceProvider) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500453 GrMipLevel texels;
454 texels.fPixels = data->data();
455 texels.fRowBytes = GrBytesPerPixel(desc.fConfig)*desc.fWidth;
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400456 return LazyInstantiationResult(
457 resourceProvider->createTexture(desc, SkBudgeted::kYes, &texels, 1));
Jim Van Verthee06b332019-01-18 10:36:32 -0500458 },
459 format, desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, SkBackingFit::kExact,
460 SkBudgeted::kYes);
461
462 if (!proxy) {
463 return nullptr;
464 }
465
Robert Phillipsa41c6852019-02-07 10:44:10 -0500466 GrContext* direct = fImageContext->priv().asDirectContext();
467 if (direct) {
468 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Jim Van Verthee06b332019-01-18 10:36:32 -0500469 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
470 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500471 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500472 return nullptr;
473 }
474 }
475 return proxy;
476}
477
Brian Salomon7578f3e2018-03-07 14:39:54 -0500478sk_sp<GrTextureProxy> GrProxyProvider::wrapBackendTexture(const GrBackendTexture& backendTex,
479 GrSurfaceOrigin origin,
480 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500481 GrWrapCacheable cacheable,
Brian Salomonc67c31c2018-12-06 10:00:03 -0500482 GrIOType ioType,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500483 ReleaseProc releaseProc,
484 ReleaseContext releaseCtx) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500485 SkASSERT(ioType != kWrite_GrIOType);
Robert Phillipsf9bec202018-01-16 09:21:01 -0500486 if (this->isAbandoned()) {
487 return nullptr;
488 }
489
Brian Salomonf7778972018-03-08 10:13:17 -0500490 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500491 GrContext* direct = fImageContext->priv().asDirectContext();
492 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500493 return nullptr;
494 }
495
Robert Phillipsa41c6852019-02-07 10:44:10 -0500496 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
497
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500498 sk_sp<GrTexture> tex =
Robert Phillipsa41c6852019-02-07 10:44:10 -0500499 resourceProvider->wrapBackendTexture(backendTex, ownership, cacheable, ioType);
Brian Salomonf7778972018-03-08 10:13:17 -0500500 if (!tex) {
501 return nullptr;
502 }
Robert Phillipsadbe1322018-01-17 13:35:46 -0500503
Greg Daniel6a0176b2018-01-30 09:28:44 -0500504 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500505 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel6a0176b2018-01-30 09:28:44 -0500506 }
507
Brian Salomonf7778972018-03-08 10:13:17 -0500508 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
509 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500510 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500511
Brian Salomonf7778972018-03-08 10:13:17 -0500512 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500513}
514
Brian Salomon7578f3e2018-03-07 14:39:54 -0500515sk_sp<GrTextureProxy> GrProxyProvider::wrapRenderableBackendTexture(
Brian Salomon02bd2952018-03-07 15:20:21 -0500516 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt,
Greg Daniel8ce79912019-02-05 10:08:43 -0500517 GrWrapOwnership ownership, GrWrapCacheable cacheable, ReleaseProc releaseProc,
518 ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500519 if (this->isAbandoned()) {
520 return nullptr;
521 }
522
Brian Salomonf7778972018-03-08 10:13:17 -0500523 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500524 GrContext* direct = fImageContext->priv().asDirectContext();
525 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500526 return nullptr;
527 }
528
Robert Phillipsa41c6852019-02-07 10:44:10 -0500529 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
530
Greg Daniel6abda432018-02-15 14:55:00 -0500531 sampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config());
Greg Danielf87651e2018-02-21 11:36:53 -0500532 if (!sampleCnt) {
533 return nullptr;
534 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500535
Robert Phillipsa41c6852019-02-07 10:44:10 -0500536 sk_sp<GrTexture> tex = resourceProvider->wrapRenderableBackendTexture(backendTex, sampleCnt,
537 ownership, cacheable);
Brian Salomonf7778972018-03-08 10:13:17 -0500538 if (!tex) {
539 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500540 }
541
Greg Daniel8ce79912019-02-05 10:08:43 -0500542 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500543 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500544 }
545
Brian Salomonf7778972018-03-08 10:13:17 -0500546 SkASSERT(tex->asRenderTarget()); // A GrTextureRenderTarget
547 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500548 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Greg Daniel6abda432018-02-15 14:55:00 -0500549
Brian Salomonf7778972018-03-08 10:13:17 -0500550 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500551}
552
Brian Salomon7578f3e2018-03-07 14:39:54 -0500553sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget(
Greg Daniel8ce79912019-02-05 10:08:43 -0500554 const GrBackendRenderTarget& backendRT, GrSurfaceOrigin origin, ReleaseProc releaseProc,
555 ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500556 if (this->isAbandoned()) {
557 return nullptr;
558 }
559
Brian Salomonf7778972018-03-08 10:13:17 -0500560 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500561 GrContext* direct = fImageContext->priv().asDirectContext();
562 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500563 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500564 }
565
Robert Phillipsa41c6852019-02-07 10:44:10 -0500566 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
567
568 sk_sp<GrRenderTarget> rt = resourceProvider->wrapBackendRenderTarget(backendRT);
Brian Salomonf7778972018-03-08 10:13:17 -0500569 if (!rt) {
570 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500571 }
Greg Daniel8ce79912019-02-05 10:08:43 -0500572
Greg Daniel8ce79912019-02-05 10:08:43 -0500573 if (releaseProc) {
Brian Salomon2ca31f82019-03-05 13:28:58 -0500574 rt->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500575 }
576
Brian Salomonf7778972018-03-08 10:13:17 -0500577 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
578 SkASSERT(!rt->getUniqueKey().isValid());
579 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500580 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Brian Salomonf7778972018-03-08 10:13:17 -0500581
582 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(std::move(rt), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500583}
584
Brian Salomon7578f3e2018-03-07 14:39:54 -0500585sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendTextureAsRenderTarget(
586 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500587 if (this->isAbandoned()) {
588 return nullptr;
589 }
590
Brian Salomonf7778972018-03-08 10:13:17 -0500591 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500592 GrContext* direct = fImageContext->priv().asDirectContext();
593 if (!direct) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500594 return nullptr;
595 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500596
Robert Phillipsa41c6852019-02-07 10:44:10 -0500597 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
598
Brian Salomonf7778972018-03-08 10:13:17 -0500599 sk_sp<GrRenderTarget> rt =
Robert Phillipsa41c6852019-02-07 10:44:10 -0500600 resourceProvider->wrapBackendTextureAsRenderTarget(backendTex, sampleCnt);
Brian Salomonf7778972018-03-08 10:13:17 -0500601 if (!rt) {
602 return nullptr;
Greg Danielf87651e2018-02-21 11:36:53 -0500603 }
Brian Salomonf7778972018-03-08 10:13:17 -0500604 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
605 SkASSERT(!rt->getUniqueKey().isValid());
Greg Danielb46add82019-01-02 14:51:29 -0500606 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500607 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielf87651e2018-02-21 11:36:53 -0500608
Brian Salomonf7778972018-03-08 10:13:17 -0500609 return sk_sp<GrSurfaceProxy>(new GrRenderTargetProxy(std::move(rt), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500610}
611
Greg Danielb46add82019-01-02 14:51:29 -0500612sk_sp<GrRenderTargetProxy> GrProxyProvider::wrapVulkanSecondaryCBAsRenderTarget(
613 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
614 if (this->isAbandoned()) {
615 return nullptr;
616 }
617
618 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500619 GrContext* direct = fImageContext->priv().asDirectContext();
620 if (!direct) {
Greg Danielb46add82019-01-02 14:51:29 -0500621 return nullptr;
622 }
623
Robert Phillipsa41c6852019-02-07 10:44:10 -0500624 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Danielb46add82019-01-02 14:51:29 -0500625
Robert Phillipsa41c6852019-02-07 10:44:10 -0500626 sk_sp<GrRenderTarget> rt = resourceProvider->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
627 vkInfo);
Greg Danielb46add82019-01-02 14:51:29 -0500628 if (!rt) {
629 return nullptr;
630 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500631
Greg Danielb46add82019-01-02 14:51:29 -0500632 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
633 SkASSERT(!rt->getUniqueKey().isValid());
634 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500635 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielb46add82019-01-02 14:51:29 -0500636
637 // All Vulkan surfaces uses top left origins.
638 return sk_sp<GrRenderTargetProxy>(
639 new GrRenderTargetProxy(std::move(rt),
640 kTopLeft_GrSurfaceOrigin,
641 GrRenderTargetProxy::WrapsVkSecondaryCB::kYes));
642}
643
Robert Phillips777707b2018-01-17 11:40:14 -0500644sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500645 const GrBackendFormat& format,
Robert Phillips777707b2018-01-17 11:40:14 -0500646 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500647 GrSurfaceOrigin origin,
Brian Salomon7226c232018-07-30 13:13:17 -0400648 GrMipMapped mipMapped,
Brian Salomon7226c232018-07-30 13:13:17 -0400649 SkBackingFit fit,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500650 SkBudgeted budgeted) {
Greg Daniel4065d452018-11-16 15:43:41 -0500651 return this->createLazyProxy(std::move(callback), format, desc, origin, mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400652 GrInternalSurfaceFlags::kNone, fit, budgeted);
Greg Daniel2a303902018-02-20 10:25:54 -0500653}
654
655sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500656 const GrBackendFormat& format,
Greg Daniel2a303902018-02-20 10:25:54 -0500657 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500658 GrSurfaceOrigin origin,
Greg Daniel2a303902018-02-20 10:25:54 -0500659 GrMipMapped mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400660 GrInternalSurfaceFlags surfaceFlags,
Brian Salomon7226c232018-07-30 13:13:17 -0400661 SkBackingFit fit,
662 SkBudgeted budgeted) {
Greg Daniela8d92112018-03-09 12:05:04 -0500663 // For non-ddl draws always make lazy proxy's single use.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500664 LazyInstantiationType lazyType = this->renderingDirectly() ? LazyInstantiationType::kSingleUse
665 : LazyInstantiationType::kMultipleUse;
Greg Daniel4065d452018-11-16 15:43:41 -0500666 return this->createLazyProxy(std::move(callback), format, desc, origin, mipMapped, surfaceFlags,
667 fit, budgeted, lazyType);
Greg Daniela8d92112018-03-09 12:05:04 -0500668}
669
670sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500671 const GrBackendFormat& format,
Greg Daniela8d92112018-03-09 12:05:04 -0500672 const GrSurfaceDesc& desc,
673 GrSurfaceOrigin origin,
674 GrMipMapped mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400675 GrInternalSurfaceFlags surfaceFlags,
Brian Salomon7226c232018-07-30 13:13:17 -0400676 SkBackingFit fit,
677 SkBudgeted budgeted,
Greg Daniela8d92112018-03-09 12:05:04 -0500678 LazyInstantiationType lazyType) {
Robert Phillips777707b2018-01-17 11:40:14 -0500679 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
680 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400681
Robert Phillipsa41c6852019-02-07 10:44:10 -0500682 if (desc.fWidth > this->caps()->maxTextureSize() ||
683 desc.fHeight > this->caps()->maxTextureSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400684 return nullptr;
685 }
686
Greg Daniel457469c2018-02-08 15:05:44 -0500687
Greg Daniel2a303902018-02-20 10:25:54 -0500688#ifdef SK_DEBUG
689 if (SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags)) {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400690 if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kMixedSampled)) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500691 SkASSERT(this->caps()->usesMixedSamples() && desc.fSampleCnt > 1);
Greg Daniel2a303902018-02-20 10:25:54 -0500692 }
Greg Daniel2a303902018-02-20 10:25:54 -0500693 }
694#endif
695
Brian Salomon2a4f9832018-03-03 22:43:43 -0500696 return sk_sp<GrTextureProxy>(
697 SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags)
Greg Daniel4065d452018-11-16 15:43:41 -0500698 ? new GrTextureRenderTargetProxy(std::move(callback), lazyType, format, desc,
699 origin, mipMapped, fit, budgeted, surfaceFlags)
700 : new GrTextureProxy(std::move(callback), lazyType, format, desc, origin,
701 mipMapped, fit, budgeted, surfaceFlags));
Robert Phillips777707b2018-01-17 11:40:14 -0500702}
703
Robert Phillipse8fabb22018-02-04 14:33:21 -0500704sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500705 LazyInstantiateCallback&& callback, const GrBackendFormat& format,
706 const GrSurfaceDesc& desc, GrSurfaceOrigin origin, GrInternalSurfaceFlags surfaceFlags,
Greg Danielb085fa92019-03-05 16:55:12 -0500707 const TextureInfo* textureInfo, SkBackingFit fit, SkBudgeted budgeted,
708 bool wrapsVkSecondaryCB) {
Robert Phillipse8fabb22018-02-04 14:33:21 -0500709 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
710 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400711
Robert Phillipsa41c6852019-02-07 10:44:10 -0500712 if (desc.fWidth > this->caps()->maxRenderTargetSize() ||
713 desc.fHeight > this->caps()->maxRenderTargetSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400714 return nullptr;
715 }
716
Greg Daniel2a303902018-02-20 10:25:54 -0500717 SkASSERT(SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags));
Greg Daniel457469c2018-02-08 15:05:44 -0500718
Greg Daniel2a303902018-02-20 10:25:54 -0500719#ifdef SK_DEBUG
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400720 if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kMixedSampled)) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500721 SkASSERT(this->caps()->usesMixedSamples() && desc.fSampleCnt > 1);
Greg Daniel2a303902018-02-20 10:25:54 -0500722 }
Greg Daniel2a303902018-02-20 10:25:54 -0500723#endif
724
Greg Daniel457469c2018-02-08 15:05:44 -0500725 using LazyInstantiationType = GrSurfaceProxy::LazyInstantiationType;
726 // For non-ddl draws always make lazy proxy's single use.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500727 LazyInstantiationType lazyType = this->renderingDirectly() ? LazyInstantiationType::kSingleUse
728 : LazyInstantiationType::kMultipleUse;
Greg Daniel457469c2018-02-08 15:05:44 -0500729
Brian Salomon7226c232018-07-30 13:13:17 -0400730 if (textureInfo) {
Greg Danielb085fa92019-03-05 16:55:12 -0500731 // Wrapped vulkan secondary command buffers don't support texturing since we won't have an
732 // actual VkImage to texture from.
733 SkASSERT(!wrapsVkSecondaryCB);
Brian Salomon7226c232018-07-30 13:13:17 -0400734 return sk_sp<GrRenderTargetProxy>(new GrTextureRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500735 std::move(callback), lazyType, format, desc, origin, textureInfo->fMipMapped,
736 fit, budgeted, surfaceFlags));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500737 }
738
Greg Danielb085fa92019-03-05 16:55:12 -0500739 GrRenderTargetProxy::WrapsVkSecondaryCB vkSCB =
740 wrapsVkSecondaryCB ? GrRenderTargetProxy::WrapsVkSecondaryCB::kYes
741 : GrRenderTargetProxy::WrapsVkSecondaryCB::kNo;
742
Brian Salomon2a4f9832018-03-03 22:43:43 -0500743 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
Greg Danielb085fa92019-03-05 16:55:12 -0500744 std::move(callback), lazyType, format, desc, origin, fit, budgeted, surfaceFlags,
745 vkSCB));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500746}
747
Chris Dalton4c458b12018-06-16 17:22:59 -0600748sk_sp<GrTextureProxy> GrProxyProvider::MakeFullyLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500749 const GrBackendFormat& format,
Chris Dalton4c458b12018-06-16 17:22:59 -0600750 Renderable renderable,
751 GrSurfaceOrigin origin,
752 GrPixelConfig config,
753 const GrCaps& caps) {
Robert Phillips777707b2018-01-17 11:40:14 -0500754 GrSurfaceDesc desc;
Greg Daniele3204862018-04-16 11:24:10 -0400755 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNoPendingIO;
Robert Phillips777707b2018-01-17 11:40:14 -0500756 if (Renderable::kYes == renderable) {
757 desc.fFlags = kRenderTarget_GrSurfaceFlag;
758 }
Robert Phillips777707b2018-01-17 11:40:14 -0500759 desc.fWidth = -1;
760 desc.fHeight = -1;
761 desc.fConfig = config;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500762 desc.fSampleCnt = 1;
Robert Phillips777707b2018-01-17 11:40:14 -0500763
Chris Dalton4c458b12018-06-16 17:22:59 -0600764 return sk_sp<GrTextureProxy>(
765 (Renderable::kYes == renderable)
Brian Salomon7226c232018-07-30 13:13:17 -0400766 ? new GrTextureRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500767 std::move(callback), LazyInstantiationType::kSingleUse, format, desc,
768 origin, GrMipMapped::kNo, SkBackingFit::kApprox, SkBudgeted::kYes,
769 surfaceFlags)
Chris Dalton4c458b12018-06-16 17:22:59 -0600770 : new GrTextureProxy(std::move(callback), LazyInstantiationType::kSingleUse,
Greg Daniel4065d452018-11-16 15:43:41 -0500771 format, desc, origin, GrMipMapped::kNo,
Brian Salomon7226c232018-07-30 13:13:17 -0400772 SkBackingFit::kApprox, SkBudgeted::kYes, surfaceFlags));
Robert Phillips777707b2018-01-17 11:40:14 -0500773}
774
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500775bool GrProxyProvider::IsFunctionallyExact(GrSurfaceProxy* proxy) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400776 const bool isInstantiated = proxy->isInstantiated();
Robert Phillipsdb3b9792018-06-12 15:18:00 -0400777 // A proxy is functionally exact if:
778 // it is exact (obvs)
779 // when it is instantiated it will be exact (i.e., power of two dimensions)
780 // it is already instantiated and the proxy covers the entire backing surface
781 return proxy->priv().isExact() ||
782 (!isInstantiated && SkIsPow2(proxy->width()) && SkIsPow2(proxy->height())) ||
783 (isInstantiated && proxy->worstCaseWidth() == proxy->width() &&
784 proxy->worstCaseHeight() == proxy->height());
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500785}
786
Robert Phillips427966a2018-12-20 17:20:43 -0500787void GrProxyProvider::processInvalidUniqueKey(const GrUniqueKey& key, GrTextureProxy* proxy,
788 InvalidateGPUResource invalidateGPUResource) {
Chris Dalton2de13dd2019-01-03 15:11:59 -0700789 SkASSERT(key.isValid());
790
Robert Phillips427966a2018-12-20 17:20:43 -0500791 if (!proxy) {
792 proxy = fUniquelyKeyedProxies.find(key);
793 }
Chris Dalton2de13dd2019-01-03 15:11:59 -0700794 SkASSERT(!proxy || proxy->getUniqueKey() == key);
795
796 // Locate the corresponding GrGpuResource (if it needs to be invalidated) before clearing the
797 // proxy's unique key. We must do it in this order because 'key' may alias the proxy's key.
798 sk_sp<GrGpuResource> invalidGpuResource;
799 if (InvalidateGPUResource::kYes == invalidateGPUResource) {
Brian Salomon01ceae92019-04-02 11:49:54 -0400800 GrContext* direct = fImageContext->priv().asDirectContext();
801 if (direct) {
802 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
803 invalidGpuResource = resourceProvider->findByUniqueKey<GrGpuResource>(key);
Chris Dalton2de13dd2019-01-03 15:11:59 -0700804 }
805 SkASSERT(!invalidGpuResource || invalidGpuResource->getUniqueKey() == key);
806 }
Robert Phillips427966a2018-12-20 17:20:43 -0500807
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500808 // Note: this method is called for the whole variety of GrGpuResources so often 'key'
809 // will not be in 'fUniquelyKeyedProxies'.
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500810 if (proxy) {
Robert Phillips427966a2018-12-20 17:20:43 -0500811 fUniquelyKeyedProxies.remove(key);
812 proxy->cacheAccess().clearUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500813 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500814
Chris Dalton2de13dd2019-01-03 15:11:59 -0700815 if (invalidGpuResource) {
816 invalidGpuResource->resourcePriv().removeUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500817 }
818}
819
Robert Phillipsa41c6852019-02-07 10:44:10 -0500820uint32_t GrProxyProvider::contextID() const {
821 return fImageContext->priv().contextID();
822}
823
824const GrCaps* GrProxyProvider::caps() const {
825 return fImageContext->priv().caps();
826}
827
828sk_sp<const GrCaps> GrProxyProvider::refCaps() const {
829 return fImageContext->priv().refCaps();
830}
831
Robert Phillipsa9162df2019-02-11 14:12:03 -0500832bool GrProxyProvider::isAbandoned() const {
833 return fImageContext->priv().abandoned();
834}
835
Robert Phillips0790f8a2018-09-18 13:11:03 -0400836void GrProxyProvider::orphanAllUniqueKeys() {
837 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
838 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
839 GrTextureProxy& tmp = *iter;
840
841 tmp.fProxyProvider = nullptr;
842 }
843}
844
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500845void GrProxyProvider::removeAllUniqueKeys() {
846 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
847 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
848 GrTextureProxy& tmp = *iter;
849
Chris Dalton2de13dd2019-01-03 15:11:59 -0700850 this->processInvalidUniqueKey(tmp.getUniqueKey(), &tmp, InvalidateGPUResource::kNo);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500851 }
852 SkASSERT(!fUniquelyKeyedProxies.count());
853}
Robert Phillipsa41c6852019-02-07 10:44:10 -0500854
855bool GrProxyProvider::renderingDirectly() const {
856 return fImageContext->priv().asDirectContext();
857}