blob: a7b72133a79acee5e66238255e7dd130e8a23988 [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
105 sk_sp<GrTextureProxy> result = sk_ref_sp(fUniquelyKeyedProxies.find(key));
106 if (result) {
107 SkASSERT(result->origin() == origin);
108 }
109 return result;
110}
111
Robert Phillipsa41c6852019-02-07 10:44:10 -0500112///////////////////////////////////////////////////////////////////////////////
113
114#if GR_TEST_UTILS
115sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createInstantiatedProxy(
116 const GrSurfaceDesc& desc, GrSurfaceOrigin origin, SkBackingFit fit, SkBudgeted budgeted) {
117 GrContext* direct = fImageContext->priv().asDirectContext();
118 if (!direct) {
119 return nullptr;
120 }
121
122 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
123 sk_sp<GrTexture> tex;
124
125 if (SkBackingFit::kApprox == fit) {
126 tex = resourceProvider->createApproxTexture(desc, GrResourceProvider::Flags::kNone);
127 } else {
128 tex = resourceProvider->createTexture(desc, budgeted, GrResourceProvider::Flags::kNone);
129 }
130 if (!tex) {
131 return nullptr;
132 }
133
134 return this->createWrapped(std::move(tex), origin);
135}
136
137sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createWrapped(sk_sp<GrTexture> tex,
138 GrSurfaceOrigin origin) {
139 return this->createWrapped(std::move(tex), origin);
140}
141#endif
142
Robert Phillipsadbe1322018-01-17 13:35:46 -0500143sk_sp<GrTextureProxy> GrProxyProvider::createWrapped(sk_sp<GrTexture> tex, GrSurfaceOrigin origin) {
144#ifdef SK_DEBUG
145 if (tex->getUniqueKey().isValid()) {
146 SkASSERT(!this->findProxyByUniqueKey(tex->getUniqueKey(), origin));
147 }
148#endif
149
150 if (tex->asRenderTarget()) {
151 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin));
152 } else {
153 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin));
154 }
155}
156
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500157sk_sp<GrTextureProxy> GrProxyProvider::findOrCreateProxyByUniqueKey(const GrUniqueKey& key,
158 GrSurfaceOrigin origin) {
159 ASSERT_SINGLE_OWNER
160
161 if (this->isAbandoned()) {
162 return nullptr;
163 }
164
165 sk_sp<GrTextureProxy> result = this->findProxyByUniqueKey(key, origin);
166 if (result) {
167 return result;
168 }
169
Robert Phillipsa41c6852019-02-07 10:44:10 -0500170 GrContext* direct = fImageContext->priv().asDirectContext();
171 if (!direct) {
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500172 return nullptr;
173 }
174
Robert Phillipsa41c6852019-02-07 10:44:10 -0500175 GrResourceCache* resourceCache = direct->priv().getResourceCache();
176
177 GrGpuResource* resource = resourceCache->findAndRefUniqueResource(key);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500178 if (!resource) {
179 return nullptr;
180 }
181
182 sk_sp<GrTexture> texture(static_cast<GrSurface*>(resource)->asTexture());
183 SkASSERT(texture);
184
Robert Phillipsadbe1322018-01-17 13:35:46 -0500185 result = this->createWrapped(std::move(texture), origin);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500186 SkASSERT(result->getUniqueKey() == key);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500187 // createWrapped should've added this for us
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500188 SkASSERT(fUniquelyKeyedProxies.find(key));
189 return result;
190}
191
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500192sk_sp<GrTextureProxy> GrProxyProvider::createTextureProxy(sk_sp<SkImage> srcImage,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400193 GrSurfaceDescFlags descFlags,
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500194 int sampleCnt,
Greg Danielfb3abcd2018-02-02 15:48:33 -0500195 SkBudgeted budgeted,
Chris Daltond004e0b2018-09-27 09:28:03 -0600196 SkBackingFit fit,
197 GrInternalSurfaceFlags surfaceFlags) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500198 ASSERT_SINGLE_OWNER
199 SkASSERT(srcImage);
200
201 if (this->isAbandoned()) {
202 return nullptr;
203 }
204
Brian Osmand29dcd12018-09-13 15:04:29 -0400205 SkImageInfo info = as_IB(srcImage)->onImageInfo();
206 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
Greg Danielf87651e2018-02-21 11:36:53 -0500207
Greg Daniel0a7aa142018-02-21 13:02:32 -0500208 if (kUnknown_GrPixelConfig == config) {
209 return nullptr;
210 }
211
Robert Phillipsa41c6852019-02-07 10:44:10 -0500212 GrBackendFormat format = this->caps()->getBackendFormatFromColorType(info.colorType());
Greg Daniel4065d452018-11-16 15:43:41 -0500213 if (!format.isValid()) {
214 return nullptr;
215 }
216
Brian Osmand29dcd12018-09-13 15:04:29 -0400217 if (!this->caps()->isConfigTexturable(config)) {
218 SkBitmap copy8888;
219 if (!copy8888.tryAllocPixels(info.makeColorType(kRGBA_8888_SkColorType)) ||
220 !srcImage->readPixels(copy8888.pixmap(), 0, 0)) {
221 return nullptr;
222 }
223 copy8888.setImmutable();
224 srcImage = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode);
225 config = kRGBA_8888_GrPixelConfig;
226 }
227
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400228 if (SkToBool(descFlags & kRenderTarget_GrSurfaceFlag)) {
Greg Danielf87651e2018-02-21 11:36:53 -0500229 sampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, config);
230 if (!sampleCnt) {
231 return nullptr;
232 }
233 }
234
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400235 if (SkToBool(descFlags & kRenderTarget_GrSurfaceFlag)) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500236 if (this->caps()->usesMixedSamples() && sampleCnt > 1) {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400237 surfaceFlags |= GrInternalSurfaceFlags::kMixedSampled;
Greg Daniel2a303902018-02-20 10:25:54 -0500238 }
Greg Daniel2a303902018-02-20 10:25:54 -0500239 }
240
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500241 GrSurfaceDesc desc;
242 desc.fWidth = srcImage->width();
243 desc.fHeight = srcImage->height();
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400244 desc.fFlags = descFlags;
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500245 desc.fSampleCnt = sampleCnt;
Greg Danielf87651e2018-02-21 11:36:53 -0500246 desc.fConfig = config;
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500247
248 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Chris Daltond004e0b2018-09-27 09:28:03 -0600249 [desc, budgeted, srcImage, fit, surfaceFlags](GrResourceProvider* resourceProvider) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500250 if (!resourceProvider) {
Greg Daniel0a375db2018-02-01 12:21:39 -0500251 // Nothing to clean up here. Once the proxy (and thus lambda) is deleted the ref
252 // on srcImage will be released.
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500253 return sk_sp<GrTexture>();
254 }
255 SkPixmap pixMap;
256 SkAssertResult(srcImage->peekPixels(&pixMap));
257 GrMipLevel mipLevel = { pixMap.addr(), pixMap.rowBytes() };
258
Chris Daltond004e0b2018-09-27 09:28:03 -0600259 auto resourceProviderFlags = GrResourceProvider::Flags::kNone;
260 if (surfaceFlags & GrInternalSurfaceFlags::kNoPendingIO) {
261 resourceProviderFlags |= GrResourceProvider::Flags::kNoPendingIO;
262 }
263 return resourceProvider->createTexture(desc, budgeted, fit, mipLevel,
264 resourceProviderFlags);
Brian Salomon2a4f9832018-03-03 22:43:43 -0500265 },
Greg Daniel4065d452018-11-16 15:43:41 -0500266 format, desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, surfaceFlags, fit, budgeted);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500267
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400268 if (!proxy) {
269 return nullptr;
270 }
271
Robert Phillipsa41c6852019-02-07 10:44:10 -0500272 GrContext* direct = fImageContext->priv().asDirectContext();
273 if (direct) {
274 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
275
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500276 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
277 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500278 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500279 return nullptr;
280 }
281 }
Robert Phillipsc1b60662018-06-26 10:20:08 -0400282
283 SkASSERT(proxy->width() == desc.fWidth);
284 SkASSERT(proxy->height() == desc.fHeight);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500285 return proxy;
286}
287
Greg Daniel4065d452018-11-16 15:43:41 -0500288sk_sp<GrTextureProxy> GrProxyProvider::createMipMapProxy(const GrBackendFormat& format,
289 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500290 GrSurfaceOrigin origin,
Greg Daniel30815082018-02-09 16:08:30 -0500291 SkBudgeted budgeted) {
Robert Phillips579f0942018-01-08 14:53:35 -0500292 ASSERT_SINGLE_OWNER
293
294 if (this->isAbandoned()) {
295 return nullptr;
296 }
297
Greg Daniel4065d452018-11-16 15:43:41 -0500298 return this->createProxy(format, desc, origin, GrMipMapped::kYes, SkBackingFit::kExact,
299 budgeted, GrInternalSurfaceFlags::kNone);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500300}
301
Brian Osman2b23c4b2018-06-01 12:25:08 -0400302sk_sp<GrTextureProxy> GrProxyProvider::createMipMapProxyFromBitmap(const SkBitmap& bitmap) {
Brian Osman412674f2019-02-07 15:34:58 -0500303 ASSERT_SINGLE_OWNER
304
305 if (this->isAbandoned()) {
306 return nullptr;
307 }
308
Brian Osman2b23c4b2018-06-01 12:25:08 -0400309 if (!SkImageInfoIsValid(bitmap.info())) {
Greg Daniela4ead652018-02-07 10:21:48 -0500310 return nullptr;
311 }
312
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400313 ATRACE_ANDROID_FRAMEWORK("Upload MipMap Texture [%ux%u]", bitmap.width(), bitmap.height());
Greg Daniela4ead652018-02-07 10:21:48 -0500314
315 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
316 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
317 // 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 -0500318 SkCopyPixelsMode copyMode = this->renderingDirectly() ? kNever_SkCopyPixelsMode
319 : kIfMutable_SkCopyPixelsMode;
Greg Daniela4ead652018-02-07 10:21:48 -0500320 sk_sp<SkImage> baseLevel = SkMakeImageFromRasterBitmap(bitmap, copyMode);
Greg Daniela4ead652018-02-07 10:21:48 -0500321 if (!baseLevel) {
322 return nullptr;
323 }
324
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400325 // This was never going to have mips anyway
326 if (0 == SkMipMap::ComputeLevelCount(baseLevel->width(), baseLevel->height())) {
Brian Salomon58389b92018-03-07 13:01:25 -0500327 return this->createTextureProxy(baseLevel, kNone_GrSurfaceFlags, 1, SkBudgeted::kYes,
328 SkBackingFit::kExact);
Greg Daniela4ead652018-02-07 10:21:48 -0500329 }
330
Robert Phillipsa41c6852019-02-07 10:44:10 -0500331 const GrBackendFormat format =
332 this->caps()->getBackendFormatFromColorType(bitmap.info().colorType());
Greg Daniel4065d452018-11-16 15:43:41 -0500333 if (!format.isValid()) {
334 return nullptr;
335 }
336
Brian Osmand29dcd12018-09-13 15:04:29 -0400337 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info());
338 if (!this->caps()->isConfigTexturable(desc.fConfig)) {
339 SkBitmap copy8888;
340 if (!copy8888.tryAllocPixels(bitmap.info().makeColorType(kRGBA_8888_SkColorType)) ||
341 !bitmap.readPixels(copy8888.pixmap())) {
342 return nullptr;
343 }
344 copy8888.setImmutable();
345 baseLevel = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode);
346 desc.fConfig = kRGBA_8888_GrPixelConfig;
347 }
348
349 SkPixmap pixmap;
350 SkAssertResult(baseLevel->peekPixels(&pixmap));
351 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(pixmap, nullptr));
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400352 if (!mipmaps) {
353 return nullptr;
354 }
355
Greg Daniela4ead652018-02-07 10:21:48 -0500356 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Osman1b97f132018-09-13 17:33:48 +0000357 [desc, baseLevel, mipmaps](GrResourceProvider* resourceProvider) {
Greg Daniela4ead652018-02-07 10:21:48 -0500358 if (!resourceProvider) {
359 return sk_sp<GrTexture>();
360 }
361
Brian Osman1b97f132018-09-13 17:33:48 +0000362 const int mipLevelCount = mipmaps->countLevels() + 1;
363 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
364
Greg Daniela4ead652018-02-07 10:21:48 -0500365 SkPixmap pixmap;
366 SkAssertResult(baseLevel->peekPixels(&pixmap));
367
368 // DDL TODO: Instead of copying all this info into GrMipLevels we should just plumb
369 // the use of SkMipMap down through Ganesh.
370 texels[0].fPixels = pixmap.addr();
371 texels[0].fRowBytes = pixmap.rowBytes();
372
373 for (int i = 1; i < mipLevelCount; ++i) {
374 SkMipMap::Level generatedMipLevel;
375 mipmaps->getLevel(i - 1, &generatedMipLevel);
376 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
377 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
378 SkASSERT(texels[i].fPixels);
379 }
380
Brian Salomon58389b92018-03-07 13:01:25 -0500381 return resourceProvider->createTexture(desc, SkBudgeted::kYes, texels.get(),
Brian Osman2b23c4b2018-06-01 12:25:08 -0400382 mipLevelCount);
Brian Salomon2a4f9832018-03-03 22:43:43 -0500383 },
Greg Daniel4065d452018-11-16 15:43:41 -0500384 format, desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kYes, SkBackingFit::kExact,
385 SkBudgeted::kYes);
Greg Daniela4ead652018-02-07 10:21:48 -0500386
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400387 if (!proxy) {
388 return nullptr;
389 }
390
Robert Phillipsa41c6852019-02-07 10:44:10 -0500391 GrContext* direct = fImageContext->priv().asDirectContext();
392 if (direct) {
393 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Daniela4ead652018-02-07 10:21:48 -0500394 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
395 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500396 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Greg Daniela4ead652018-02-07 10:21:48 -0500397 return nullptr;
398 }
399 }
400 return proxy;
401}
402
Greg Daniel4065d452018-11-16 15:43:41 -0500403sk_sp<GrTextureProxy> GrProxyProvider::createProxy(const GrBackendFormat& format,
404 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500405 GrSurfaceOrigin origin,
Greg Danielf6f7b672018-02-15 13:06:26 -0500406 GrMipMapped mipMapped,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500407 SkBackingFit fit,
408 SkBudgeted budgeted,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400409 GrInternalSurfaceFlags surfaceFlags) {
Greg Danielf6f7b672018-02-15 13:06:26 -0500410 if (GrMipMapped::kYes == mipMapped) {
411 // SkMipMap doesn't include the base level in the level count so we have to add 1
412 int mipCount = SkMipMap::ComputeLevelCount(desc.fWidth, desc.fHeight) + 1;
413 if (1 == mipCount) {
414 mipMapped = GrMipMapped::kNo;
415 }
416 }
417
418 if (!this->caps()->validateSurfaceDesc(desc, mipMapped)) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500419 return nullptr;
420 }
Brian Salomon3a2cc2c2018-02-03 00:25:12 +0000421 GrSurfaceDesc copyDesc = desc;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500422 if (desc.fFlags & kRenderTarget_GrSurfaceFlag) {
423 copyDesc.fSampleCnt =
424 this->caps()->getRenderTargetSampleCount(desc.fSampleCnt, desc.fConfig);
425 }
Brian Salomon3a2cc2c2018-02-03 00:25:12 +0000426
Brian Salomonbdecacf2018-02-02 20:32:49 -0500427 if (copyDesc.fFlags & kRenderTarget_GrSurfaceFlag) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500428 // We know anything we instantiate later from this deferred path will be
429 // both texturable and renderable
Greg Daniel4065d452018-11-16 15:43:41 -0500430 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(*this->caps(), format, copyDesc,
431 origin, mipMapped,
Brian Salomon7226c232018-07-30 13:13:17 -0400432 fit, budgeted, surfaceFlags));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500433 }
434
Greg Daniel4065d452018-11-16 15:43:41 -0500435 return sk_sp<GrTextureProxy>(new GrTextureProxy(format, copyDesc, origin, mipMapped,
Brian Salomon7226c232018-07-30 13:13:17 -0400436 fit, budgeted, surfaceFlags));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500437}
438
Jim Van Verthee06b332019-01-18 10:36:32 -0500439sk_sp<GrTextureProxy> GrProxyProvider::createProxy(sk_sp<SkData> data, const GrSurfaceDesc& desc) {
440 if (!this->caps()->isConfigTexturable(desc.fConfig)) {
441 return nullptr;
442 }
443
444 const GrColorType ct = GrPixelConfigToColorType(desc.fConfig);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500445 const GrBackendFormat format =
446 this->caps()->getBackendFormatFromGrColorType(ct, GrSRGBEncoded::kNo);
Jim Van Verthee06b332019-01-18 10:36:32 -0500447
448 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
449 [desc, data](GrResourceProvider* resourceProvider) {
450 if (!resourceProvider) {
451 return sk_sp<GrTexture>();
452 }
453
454 GrMipLevel texels;
455 texels.fPixels = data->data();
456 texels.fRowBytes = GrBytesPerPixel(desc.fConfig)*desc.fWidth;
457 return resourceProvider->createTexture(desc, SkBudgeted::kYes, &texels, 1);
458 },
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 sk_sp<GrReleaseProcHelper> releaseHelper;
505 if (releaseProc) {
506 releaseHelper.reset(new GrReleaseProcHelper(releaseProc, releaseCtx));
Brian Salomonf7778972018-03-08 10:13:17 -0500507 // This gives the texture a ref on the releaseHelper
Greg Daniel8ce79912019-02-05 10:08:43 -0500508 tex->setRelease(std::move(releaseHelper));
Greg Daniel6a0176b2018-01-30 09:28:44 -0500509 }
510
Brian Salomonf7778972018-03-08 10:13:17 -0500511 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
512 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500513 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500514
Brian Salomonf7778972018-03-08 10:13:17 -0500515 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500516}
517
Brian Salomon7578f3e2018-03-07 14:39:54 -0500518sk_sp<GrTextureProxy> GrProxyProvider::wrapRenderableBackendTexture(
Brian Salomon02bd2952018-03-07 15:20:21 -0500519 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt,
Greg Daniel8ce79912019-02-05 10:08:43 -0500520 GrWrapOwnership ownership, GrWrapCacheable cacheable, ReleaseProc releaseProc,
521 ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500522 if (this->isAbandoned()) {
523 return nullptr;
524 }
525
Brian Salomonf7778972018-03-08 10:13:17 -0500526 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500527 GrContext* direct = fImageContext->priv().asDirectContext();
528 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500529 return nullptr;
530 }
531
Robert Phillipsa41c6852019-02-07 10:44:10 -0500532 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
533
Greg Daniel6abda432018-02-15 14:55:00 -0500534 sampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config());
Greg Danielf87651e2018-02-21 11:36:53 -0500535 if (!sampleCnt) {
536 return nullptr;
537 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500538
Robert Phillipsa41c6852019-02-07 10:44:10 -0500539 sk_sp<GrTexture> tex = resourceProvider->wrapRenderableBackendTexture(backendTex, sampleCnt,
540 ownership, cacheable);
Brian Salomonf7778972018-03-08 10:13:17 -0500541 if (!tex) {
542 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500543 }
544
Greg Daniel8ce79912019-02-05 10:08:43 -0500545 sk_sp<GrReleaseProcHelper> releaseHelper;
546 if (releaseProc) {
547 releaseHelper.reset(new GrReleaseProcHelper(releaseProc, releaseCtx));
548 // This gives the texture a ref on the releaseHelper
549 tex->setRelease(std::move(releaseHelper));
550 }
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
579 sk_sp<GrReleaseProcHelper> releaseHelper;
580 if (releaseProc) {
581 releaseHelper.reset(new GrReleaseProcHelper(releaseProc, releaseCtx));
582 // This gives the render target a ref on the releaseHelper
583 rt->setRelease(std::move(releaseHelper));
584 }
585
Brian Salomonf7778972018-03-08 10:13:17 -0500586 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
587 SkASSERT(!rt->getUniqueKey().isValid());
588 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500589 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Brian Salomonf7778972018-03-08 10:13:17 -0500590
591 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(std::move(rt), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500592}
593
Brian Salomon7578f3e2018-03-07 14:39:54 -0500594sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendTextureAsRenderTarget(
595 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500596 if (this->isAbandoned()) {
597 return nullptr;
598 }
599
Brian Salomonf7778972018-03-08 10:13:17 -0500600 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500601 GrContext* direct = fImageContext->priv().asDirectContext();
602 if (!direct) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500603 return nullptr;
604 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500605
Robert Phillipsa41c6852019-02-07 10:44:10 -0500606 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
607
Brian Salomonf7778972018-03-08 10:13:17 -0500608 sk_sp<GrRenderTarget> rt =
Robert Phillipsa41c6852019-02-07 10:44:10 -0500609 resourceProvider->wrapBackendTextureAsRenderTarget(backendTex, sampleCnt);
Brian Salomonf7778972018-03-08 10:13:17 -0500610 if (!rt) {
611 return nullptr;
Greg Danielf87651e2018-02-21 11:36:53 -0500612 }
Brian Salomonf7778972018-03-08 10:13:17 -0500613 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
614 SkASSERT(!rt->getUniqueKey().isValid());
Greg Danielb46add82019-01-02 14:51:29 -0500615 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500616 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielf87651e2018-02-21 11:36:53 -0500617
Brian Salomonf7778972018-03-08 10:13:17 -0500618 return sk_sp<GrSurfaceProxy>(new GrRenderTargetProxy(std::move(rt), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500619}
620
Greg Danielb46add82019-01-02 14:51:29 -0500621sk_sp<GrRenderTargetProxy> GrProxyProvider::wrapVulkanSecondaryCBAsRenderTarget(
622 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
623 if (this->isAbandoned()) {
624 return nullptr;
625 }
626
627 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500628 GrContext* direct = fImageContext->priv().asDirectContext();
629 if (!direct) {
Greg Danielb46add82019-01-02 14:51:29 -0500630 return nullptr;
631 }
632
Robert Phillipsa41c6852019-02-07 10:44:10 -0500633 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Danielb46add82019-01-02 14:51:29 -0500634
Robert Phillipsa41c6852019-02-07 10:44:10 -0500635 sk_sp<GrRenderTarget> rt = resourceProvider->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
636 vkInfo);
Greg Danielb46add82019-01-02 14:51:29 -0500637 if (!rt) {
638 return nullptr;
639 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500640
Greg Danielb46add82019-01-02 14:51:29 -0500641 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
642 SkASSERT(!rt->getUniqueKey().isValid());
643 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500644 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielb46add82019-01-02 14:51:29 -0500645
646 // All Vulkan surfaces uses top left origins.
647 return sk_sp<GrRenderTargetProxy>(
648 new GrRenderTargetProxy(std::move(rt),
649 kTopLeft_GrSurfaceOrigin,
650 GrRenderTargetProxy::WrapsVkSecondaryCB::kYes));
651}
652
Robert Phillips777707b2018-01-17 11:40:14 -0500653sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500654 const GrBackendFormat& format,
Robert Phillips777707b2018-01-17 11:40:14 -0500655 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500656 GrSurfaceOrigin origin,
Brian Salomon7226c232018-07-30 13:13:17 -0400657 GrMipMapped mipMapped,
Brian Salomon7226c232018-07-30 13:13:17 -0400658 SkBackingFit fit,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500659 SkBudgeted budgeted) {
Greg Daniel4065d452018-11-16 15:43:41 -0500660 return this->createLazyProxy(std::move(callback), format, desc, origin, mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400661 GrInternalSurfaceFlags::kNone, fit, budgeted);
Greg Daniel2a303902018-02-20 10:25:54 -0500662}
663
664sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500665 const GrBackendFormat& format,
Greg Daniel2a303902018-02-20 10:25:54 -0500666 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500667 GrSurfaceOrigin origin,
Greg Daniel2a303902018-02-20 10:25:54 -0500668 GrMipMapped mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400669 GrInternalSurfaceFlags surfaceFlags,
Brian Salomon7226c232018-07-30 13:13:17 -0400670 SkBackingFit fit,
671 SkBudgeted budgeted) {
Greg Daniela8d92112018-03-09 12:05:04 -0500672 // For non-ddl draws always make lazy proxy's single use.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500673 LazyInstantiationType lazyType = this->renderingDirectly() ? LazyInstantiationType::kSingleUse
674 : LazyInstantiationType::kMultipleUse;
Greg Daniel4065d452018-11-16 15:43:41 -0500675 return this->createLazyProxy(std::move(callback), format, desc, origin, mipMapped, surfaceFlags,
676 fit, budgeted, lazyType);
Greg Daniela8d92112018-03-09 12:05:04 -0500677}
678
679sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500680 const GrBackendFormat& format,
Greg Daniela8d92112018-03-09 12:05:04 -0500681 const GrSurfaceDesc& desc,
682 GrSurfaceOrigin origin,
683 GrMipMapped mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400684 GrInternalSurfaceFlags surfaceFlags,
Brian Salomon7226c232018-07-30 13:13:17 -0400685 SkBackingFit fit,
686 SkBudgeted budgeted,
Greg Daniela8d92112018-03-09 12:05:04 -0500687 LazyInstantiationType lazyType) {
Robert Phillips777707b2018-01-17 11:40:14 -0500688 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
689 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400690
Robert Phillipsa41c6852019-02-07 10:44:10 -0500691 if (desc.fWidth > this->caps()->maxTextureSize() ||
692 desc.fHeight > this->caps()->maxTextureSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400693 return nullptr;
694 }
695
Greg Daniel457469c2018-02-08 15:05:44 -0500696
Greg Daniel2a303902018-02-20 10:25:54 -0500697#ifdef SK_DEBUG
698 if (SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags)) {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400699 if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kMixedSampled)) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500700 SkASSERT(this->caps()->usesMixedSamples() && desc.fSampleCnt > 1);
Greg Daniel2a303902018-02-20 10:25:54 -0500701 }
Greg Daniel2a303902018-02-20 10:25:54 -0500702 }
703#endif
704
Brian Salomon2a4f9832018-03-03 22:43:43 -0500705 return sk_sp<GrTextureProxy>(
706 SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags)
Greg Daniel4065d452018-11-16 15:43:41 -0500707 ? new GrTextureRenderTargetProxy(std::move(callback), lazyType, format, desc,
708 origin, mipMapped, fit, budgeted, surfaceFlags)
709 : new GrTextureProxy(std::move(callback), lazyType, format, desc, origin,
710 mipMapped, fit, budgeted, surfaceFlags));
Robert Phillips777707b2018-01-17 11:40:14 -0500711}
712
Robert Phillipse8fabb22018-02-04 14:33:21 -0500713sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500714 LazyInstantiateCallback&& callback, const GrBackendFormat& format,
715 const GrSurfaceDesc& desc, GrSurfaceOrigin origin, GrInternalSurfaceFlags surfaceFlags,
716 const TextureInfo* textureInfo, SkBackingFit fit, SkBudgeted budgeted) {
Robert Phillipse8fabb22018-02-04 14:33:21 -0500717 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
718 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400719
Robert Phillipsa41c6852019-02-07 10:44:10 -0500720 if (desc.fWidth > this->caps()->maxRenderTargetSize() ||
721 desc.fHeight > this->caps()->maxRenderTargetSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400722 return nullptr;
723 }
724
Greg Daniel2a303902018-02-20 10:25:54 -0500725 SkASSERT(SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags));
Greg Daniel457469c2018-02-08 15:05:44 -0500726
Greg Daniel2a303902018-02-20 10:25:54 -0500727#ifdef SK_DEBUG
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400728 if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kMixedSampled)) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500729 SkASSERT(this->caps()->usesMixedSamples() && desc.fSampleCnt > 1);
Greg Daniel2a303902018-02-20 10:25:54 -0500730 }
Greg Daniel2a303902018-02-20 10:25:54 -0500731#endif
732
Greg Daniel457469c2018-02-08 15:05:44 -0500733 using LazyInstantiationType = GrSurfaceProxy::LazyInstantiationType;
734 // For non-ddl draws always make lazy proxy's single use.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500735 LazyInstantiationType lazyType = this->renderingDirectly() ? LazyInstantiationType::kSingleUse
736 : LazyInstantiationType::kMultipleUse;
Greg Daniel457469c2018-02-08 15:05:44 -0500737
Brian Salomon7226c232018-07-30 13:13:17 -0400738 if (textureInfo) {
739 return sk_sp<GrRenderTargetProxy>(new GrTextureRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500740 std::move(callback), lazyType, format, desc, origin, textureInfo->fMipMapped,
741 fit, budgeted, surfaceFlags));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500742 }
743
Brian Salomon2a4f9832018-03-03 22:43:43 -0500744 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500745 std::move(callback), lazyType, format, desc, origin, fit, budgeted, surfaceFlags));
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) {
800 if (proxy && proxy->isInstantiated()) {
801 invalidGpuResource = sk_ref_sp(proxy->peekSurface());
802 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500803 if (!invalidGpuResource) {
804 GrContext* direct = fImageContext->priv().asDirectContext();
805 if (direct) {
806 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
807 invalidGpuResource = resourceProvider->findByUniqueKey<GrGpuResource>(key);
808 }
Chris Dalton2de13dd2019-01-03 15:11:59 -0700809 }
810 SkASSERT(!invalidGpuResource || invalidGpuResource->getUniqueKey() == key);
811 }
Robert Phillips427966a2018-12-20 17:20:43 -0500812
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500813 // Note: this method is called for the whole variety of GrGpuResources so often 'key'
814 // will not be in 'fUniquelyKeyedProxies'.
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500815 if (proxy) {
Robert Phillips427966a2018-12-20 17:20:43 -0500816 fUniquelyKeyedProxies.remove(key);
817 proxy->cacheAccess().clearUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500818 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500819
Chris Dalton2de13dd2019-01-03 15:11:59 -0700820 if (invalidGpuResource) {
821 invalidGpuResource->resourcePriv().removeUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500822 }
823}
824
Robert Phillipsa41c6852019-02-07 10:44:10 -0500825uint32_t GrProxyProvider::contextID() const {
826 return fImageContext->priv().contextID();
827}
828
829const GrCaps* GrProxyProvider::caps() const {
830 return fImageContext->priv().caps();
831}
832
833sk_sp<const GrCaps> GrProxyProvider::refCaps() const {
834 return fImageContext->priv().refCaps();
835}
836
Robert Phillipsa9162df2019-02-11 14:12:03 -0500837bool GrProxyProvider::isAbandoned() const {
838 return fImageContext->priv().abandoned();
839}
840
Robert Phillips0790f8a2018-09-18 13:11:03 -0400841void GrProxyProvider::orphanAllUniqueKeys() {
842 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
843 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
844 GrTextureProxy& tmp = *iter;
845
846 tmp.fProxyProvider = nullptr;
847 }
848}
849
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500850void GrProxyProvider::removeAllUniqueKeys() {
851 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
852 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
853 GrTextureProxy& tmp = *iter;
854
Chris Dalton2de13dd2019-01-03 15:11:59 -0700855 this->processInvalidUniqueKey(tmp.getUniqueKey(), &tmp, InvalidateGPUResource::kNo);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500856 }
857 SkASSERT(!fUniquelyKeyedProxies.count());
858}
Robert Phillipsa41c6852019-02-07 10:44:10 -0500859
860bool GrProxyProvider::renderingDirectly() const {
861 return fImageContext->priv().asDirectContext();
862}