blob: a108569f70ab4cb186ae41ce6f08243ff05a7a04 [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 Phillipsa41c6852019-02-07 10:44:10 -050037GrProxyProvider::GrProxyProvider(GrImageContext* imageContext)
38 : fImageContext(imageContext)
39 , fAbandoned(false) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050040}
41
42GrProxyProvider::~GrProxyProvider() {
Robert Phillipsa41c6852019-02-07 10:44:10 -050043 if (this->renderingDirectly()) {
Robert Phillips0790f8a2018-09-18 13:11:03 -040044 // In DDL-mode a proxy provider can still have extant uniquely keyed proxies (since
45 // they need their unique keys to, potentially, find a cached resource when the
46 // DDL is played) but, in non-DDL-mode they should all have been cleaned up by this point.
47 SkASSERT(!fUniquelyKeyedProxies.count());
48 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050049}
50
Robert Phillipsadbe1322018-01-17 13:35:46 -050051bool GrProxyProvider::assignUniqueKeyToProxy(const GrUniqueKey& key, GrTextureProxy* proxy) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050052 ASSERT_SINGLE_OWNER
53 SkASSERT(key.isValid());
54 if (this->isAbandoned() || !proxy) {
Robert Phillipsadbe1322018-01-17 13:35:46 -050055 return false;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050056 }
57
Robert Phillipsa41c6852019-02-07 10:44:10 -050058#ifdef SK_DEBUG
59 {
60 GrContext* direct = fImageContext->priv().asDirectContext();
61 if (direct) {
62 GrResourceCache* resourceCache = direct->priv().getResourceCache();
63 // If there is already a GrResource with this key then the caller has violated the
64 // normal usage pattern of uniquely keyed resources (e.g., they have created one w/o
65 // first seeing if it already existed in the cache).
66 SkASSERT(!resourceCache->findAndRefUniqueResource(key));
67 }
68 }
69#endif
Robert Phillips1afd4cd2018-01-08 13:40:32 -050070
Robert Phillips1afd4cd2018-01-08 13:40:32 -050071 SkASSERT(!fUniquelyKeyedProxies.find(key)); // multiple proxies can't get the same key
72
73 proxy->cacheAccess().setUniqueKey(this, key);
74 SkASSERT(proxy->getUniqueKey() == key);
75 fUniquelyKeyedProxies.add(proxy);
Robert Phillipsadbe1322018-01-17 13:35:46 -050076 return true;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050077}
78
79void GrProxyProvider::adoptUniqueKeyFromSurface(GrTextureProxy* proxy, const GrSurface* surf) {
80 SkASSERT(surf->getUniqueKey().isValid());
81 proxy->cacheAccess().setUniqueKey(this, surf->getUniqueKey());
82 SkASSERT(proxy->getUniqueKey() == surf->getUniqueKey());
83 // multiple proxies can't get the same key
84 SkASSERT(!fUniquelyKeyedProxies.find(surf->getUniqueKey()));
85 fUniquelyKeyedProxies.add(proxy);
86}
87
Chris Dalton2de13dd2019-01-03 15:11:59 -070088void GrProxyProvider::removeUniqueKeyFromProxy(GrTextureProxy* proxy) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050089 ASSERT_SINGLE_OWNER
Chris Dalton2de13dd2019-01-03 15:11:59 -070090 SkASSERT(proxy);
91 SkASSERT(proxy->getUniqueKey().isValid());
92
93 if (this->isAbandoned()) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050094 return;
95 }
Robert Phillips0790f8a2018-09-18 13:11:03 -040096
Chris Dalton2de13dd2019-01-03 15:11:59 -070097 this->processInvalidUniqueKey(proxy->getUniqueKey(), proxy, InvalidateGPUResource::kYes);
Robert Phillips1afd4cd2018-01-08 13:40:32 -050098}
99
100sk_sp<GrTextureProxy> GrProxyProvider::findProxyByUniqueKey(const GrUniqueKey& key,
101 GrSurfaceOrigin origin) {
102 ASSERT_SINGLE_OWNER
103
104 if (this->isAbandoned()) {
105 return nullptr;
106 }
107
108 sk_sp<GrTextureProxy> result = sk_ref_sp(fUniquelyKeyedProxies.find(key));
109 if (result) {
110 SkASSERT(result->origin() == origin);
111 }
112 return result;
113}
114
Robert Phillipsa41c6852019-02-07 10:44:10 -0500115///////////////////////////////////////////////////////////////////////////////
116
117#if GR_TEST_UTILS
118sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createInstantiatedProxy(
119 const GrSurfaceDesc& desc, GrSurfaceOrigin origin, SkBackingFit fit, SkBudgeted budgeted) {
120 GrContext* direct = fImageContext->priv().asDirectContext();
121 if (!direct) {
122 return nullptr;
123 }
124
125 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
126 sk_sp<GrTexture> tex;
127
128 if (SkBackingFit::kApprox == fit) {
129 tex = resourceProvider->createApproxTexture(desc, GrResourceProvider::Flags::kNone);
130 } else {
131 tex = resourceProvider->createTexture(desc, budgeted, GrResourceProvider::Flags::kNone);
132 }
133 if (!tex) {
134 return nullptr;
135 }
136
137 return this->createWrapped(std::move(tex), origin);
138}
139
140sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createWrapped(sk_sp<GrTexture> tex,
141 GrSurfaceOrigin origin) {
142 return this->createWrapped(std::move(tex), origin);
143}
144#endif
145
Robert Phillipsadbe1322018-01-17 13:35:46 -0500146sk_sp<GrTextureProxy> GrProxyProvider::createWrapped(sk_sp<GrTexture> tex, GrSurfaceOrigin origin) {
147#ifdef SK_DEBUG
148 if (tex->getUniqueKey().isValid()) {
149 SkASSERT(!this->findProxyByUniqueKey(tex->getUniqueKey(), origin));
150 }
151#endif
152
153 if (tex->asRenderTarget()) {
154 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin));
155 } else {
156 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin));
157 }
158}
159
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500160sk_sp<GrTextureProxy> GrProxyProvider::findOrCreateProxyByUniqueKey(const GrUniqueKey& key,
161 GrSurfaceOrigin origin) {
162 ASSERT_SINGLE_OWNER
163
164 if (this->isAbandoned()) {
165 return nullptr;
166 }
167
168 sk_sp<GrTextureProxy> result = this->findProxyByUniqueKey(key, origin);
169 if (result) {
170 return result;
171 }
172
Robert Phillipsa41c6852019-02-07 10:44:10 -0500173 GrContext* direct = fImageContext->priv().asDirectContext();
174 if (!direct) {
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500175 return nullptr;
176 }
177
Robert Phillipsa41c6852019-02-07 10:44:10 -0500178 GrResourceCache* resourceCache = direct->priv().getResourceCache();
179
180 GrGpuResource* resource = resourceCache->findAndRefUniqueResource(key);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500181 if (!resource) {
182 return nullptr;
183 }
184
185 sk_sp<GrTexture> texture(static_cast<GrSurface*>(resource)->asTexture());
186 SkASSERT(texture);
187
Robert Phillipsadbe1322018-01-17 13:35:46 -0500188 result = this->createWrapped(std::move(texture), origin);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500189 SkASSERT(result->getUniqueKey() == key);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500190 // createWrapped should've added this for us
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500191 SkASSERT(fUniquelyKeyedProxies.find(key));
192 return result;
193}
194
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500195sk_sp<GrTextureProxy> GrProxyProvider::createTextureProxy(sk_sp<SkImage> srcImage,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400196 GrSurfaceDescFlags descFlags,
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500197 int sampleCnt,
Greg Danielfb3abcd2018-02-02 15:48:33 -0500198 SkBudgeted budgeted,
Chris Daltond004e0b2018-09-27 09:28:03 -0600199 SkBackingFit fit,
200 GrInternalSurfaceFlags surfaceFlags) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500201 ASSERT_SINGLE_OWNER
202 SkASSERT(srcImage);
203
204 if (this->isAbandoned()) {
205 return nullptr;
206 }
207
Brian Osmand29dcd12018-09-13 15:04:29 -0400208 SkImageInfo info = as_IB(srcImage)->onImageInfo();
209 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
Greg Danielf87651e2018-02-21 11:36:53 -0500210
Greg Daniel0a7aa142018-02-21 13:02:32 -0500211 if (kUnknown_GrPixelConfig == config) {
212 return nullptr;
213 }
214
Robert Phillipsa41c6852019-02-07 10:44:10 -0500215 GrBackendFormat format = this->caps()->getBackendFormatFromColorType(info.colorType());
Greg Daniel4065d452018-11-16 15:43:41 -0500216 if (!format.isValid()) {
217 return nullptr;
218 }
219
Brian Osmand29dcd12018-09-13 15:04:29 -0400220 if (!this->caps()->isConfigTexturable(config)) {
221 SkBitmap copy8888;
222 if (!copy8888.tryAllocPixels(info.makeColorType(kRGBA_8888_SkColorType)) ||
223 !srcImage->readPixels(copy8888.pixmap(), 0, 0)) {
224 return nullptr;
225 }
226 copy8888.setImmutable();
227 srcImage = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode);
228 config = kRGBA_8888_GrPixelConfig;
229 }
230
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400231 if (SkToBool(descFlags & kRenderTarget_GrSurfaceFlag)) {
Greg Danielf87651e2018-02-21 11:36:53 -0500232 sampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, config);
233 if (!sampleCnt) {
234 return nullptr;
235 }
236 }
237
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400238 if (SkToBool(descFlags & kRenderTarget_GrSurfaceFlag)) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500239 if (this->caps()->usesMixedSamples() && sampleCnt > 1) {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400240 surfaceFlags |= GrInternalSurfaceFlags::kMixedSampled;
Greg Daniel2a303902018-02-20 10:25:54 -0500241 }
Greg Daniel2a303902018-02-20 10:25:54 -0500242 }
243
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500244 GrSurfaceDesc desc;
245 desc.fWidth = srcImage->width();
246 desc.fHeight = srcImage->height();
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400247 desc.fFlags = descFlags;
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500248 desc.fSampleCnt = sampleCnt;
Greg Danielf87651e2018-02-21 11:36:53 -0500249 desc.fConfig = config;
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500250
251 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Chris Daltond004e0b2018-09-27 09:28:03 -0600252 [desc, budgeted, srcImage, fit, surfaceFlags](GrResourceProvider* resourceProvider) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500253 if (!resourceProvider) {
Greg Daniel0a375db2018-02-01 12:21:39 -0500254 // Nothing to clean up here. Once the proxy (and thus lambda) is deleted the ref
255 // on srcImage will be released.
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500256 return sk_sp<GrTexture>();
257 }
258 SkPixmap pixMap;
259 SkAssertResult(srcImage->peekPixels(&pixMap));
260 GrMipLevel mipLevel = { pixMap.addr(), pixMap.rowBytes() };
261
Chris Daltond004e0b2018-09-27 09:28:03 -0600262 auto resourceProviderFlags = GrResourceProvider::Flags::kNone;
263 if (surfaceFlags & GrInternalSurfaceFlags::kNoPendingIO) {
264 resourceProviderFlags |= GrResourceProvider::Flags::kNoPendingIO;
265 }
266 return resourceProvider->createTexture(desc, budgeted, fit, mipLevel,
267 resourceProviderFlags);
Brian Salomon2a4f9832018-03-03 22:43:43 -0500268 },
Greg Daniel4065d452018-11-16 15:43:41 -0500269 format, desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, surfaceFlags, fit, budgeted);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500270
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400271 if (!proxy) {
272 return nullptr;
273 }
274
Robert Phillipsa41c6852019-02-07 10:44:10 -0500275 GrContext* direct = fImageContext->priv().asDirectContext();
276 if (direct) {
277 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
278
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500279 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
280 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500281 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500282 return nullptr;
283 }
284 }
Robert Phillipsc1b60662018-06-26 10:20:08 -0400285
286 SkASSERT(proxy->width() == desc.fWidth);
287 SkASSERT(proxy->height() == desc.fHeight);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500288 return proxy;
289}
290
Greg Daniel4065d452018-11-16 15:43:41 -0500291sk_sp<GrTextureProxy> GrProxyProvider::createMipMapProxy(const GrBackendFormat& format,
292 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500293 GrSurfaceOrigin origin,
Greg Daniel30815082018-02-09 16:08:30 -0500294 SkBudgeted budgeted) {
Robert Phillips579f0942018-01-08 14:53:35 -0500295 ASSERT_SINGLE_OWNER
296
297 if (this->isAbandoned()) {
298 return nullptr;
299 }
300
Greg Daniel4065d452018-11-16 15:43:41 -0500301 return this->createProxy(format, desc, origin, GrMipMapped::kYes, SkBackingFit::kExact,
302 budgeted, GrInternalSurfaceFlags::kNone);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500303}
304
Brian Osman2b23c4b2018-06-01 12:25:08 -0400305sk_sp<GrTextureProxy> GrProxyProvider::createMipMapProxyFromBitmap(const SkBitmap& bitmap) {
Brian Osman412674f2019-02-07 15:34:58 -0500306 ASSERT_SINGLE_OWNER
307
308 if (this->isAbandoned()) {
309 return nullptr;
310 }
311
Brian Osman2b23c4b2018-06-01 12:25:08 -0400312 if (!SkImageInfoIsValid(bitmap.info())) {
Greg Daniela4ead652018-02-07 10:21:48 -0500313 return nullptr;
314 }
315
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400316 ATRACE_ANDROID_FRAMEWORK("Upload MipMap Texture [%ux%u]", bitmap.width(), bitmap.height());
Greg Daniela4ead652018-02-07 10:21:48 -0500317
318 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
319 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
320 // 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 -0500321 SkCopyPixelsMode copyMode = this->renderingDirectly() ? kNever_SkCopyPixelsMode
322 : kIfMutable_SkCopyPixelsMode;
Greg Daniela4ead652018-02-07 10:21:48 -0500323 sk_sp<SkImage> baseLevel = SkMakeImageFromRasterBitmap(bitmap, copyMode);
Greg Daniela4ead652018-02-07 10:21:48 -0500324 if (!baseLevel) {
325 return nullptr;
326 }
327
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400328 // This was never going to have mips anyway
329 if (0 == SkMipMap::ComputeLevelCount(baseLevel->width(), baseLevel->height())) {
Brian Salomon58389b92018-03-07 13:01:25 -0500330 return this->createTextureProxy(baseLevel, kNone_GrSurfaceFlags, 1, SkBudgeted::kYes,
331 SkBackingFit::kExact);
Greg Daniela4ead652018-02-07 10:21:48 -0500332 }
333
Robert Phillipsa41c6852019-02-07 10:44:10 -0500334 const GrBackendFormat format =
335 this->caps()->getBackendFormatFromColorType(bitmap.info().colorType());
Greg Daniel4065d452018-11-16 15:43:41 -0500336 if (!format.isValid()) {
337 return nullptr;
338 }
339
Brian Osmand29dcd12018-09-13 15:04:29 -0400340 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info());
341 if (!this->caps()->isConfigTexturable(desc.fConfig)) {
342 SkBitmap copy8888;
343 if (!copy8888.tryAllocPixels(bitmap.info().makeColorType(kRGBA_8888_SkColorType)) ||
344 !bitmap.readPixels(copy8888.pixmap())) {
345 return nullptr;
346 }
347 copy8888.setImmutable();
348 baseLevel = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode);
349 desc.fConfig = kRGBA_8888_GrPixelConfig;
350 }
351
352 SkPixmap pixmap;
353 SkAssertResult(baseLevel->peekPixels(&pixmap));
354 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(pixmap, nullptr));
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400355 if (!mipmaps) {
356 return nullptr;
357 }
358
Greg Daniela4ead652018-02-07 10:21:48 -0500359 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Osman1b97f132018-09-13 17:33:48 +0000360 [desc, baseLevel, mipmaps](GrResourceProvider* resourceProvider) {
Greg Daniela4ead652018-02-07 10:21:48 -0500361 if (!resourceProvider) {
362 return sk_sp<GrTexture>();
363 }
364
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 Salomon58389b92018-03-07 13:01:25 -0500384 return resourceProvider->createTexture(desc, SkBudgeted::kYes, texels.get(),
Brian Osman2b23c4b2018-06-01 12:25:08 -0400385 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) {
453 if (!resourceProvider) {
454 return sk_sp<GrTexture>();
455 }
456
457 GrMipLevel texels;
458 texels.fPixels = data->data();
459 texels.fRowBytes = GrBytesPerPixel(desc.fConfig)*desc.fWidth;
460 return resourceProvider->createTexture(desc, SkBudgeted::kYes, &texels, 1);
461 },
462 format, desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, SkBackingFit::kExact,
463 SkBudgeted::kYes);
464
465 if (!proxy) {
466 return nullptr;
467 }
468
Robert Phillipsa41c6852019-02-07 10:44:10 -0500469 GrContext* direct = fImageContext->priv().asDirectContext();
470 if (direct) {
471 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Jim Van Verthee06b332019-01-18 10:36:32 -0500472 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
473 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500474 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500475 return nullptr;
476 }
477 }
478 return proxy;
479}
480
Brian Salomon7578f3e2018-03-07 14:39:54 -0500481sk_sp<GrTextureProxy> GrProxyProvider::wrapBackendTexture(const GrBackendTexture& backendTex,
482 GrSurfaceOrigin origin,
483 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500484 GrWrapCacheable cacheable,
Brian Salomonc67c31c2018-12-06 10:00:03 -0500485 GrIOType ioType,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500486 ReleaseProc releaseProc,
487 ReleaseContext releaseCtx) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500488 SkASSERT(ioType != kWrite_GrIOType);
Robert Phillipsf9bec202018-01-16 09:21:01 -0500489 if (this->isAbandoned()) {
490 return nullptr;
491 }
492
Brian Salomonf7778972018-03-08 10:13:17 -0500493 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500494 GrContext* direct = fImageContext->priv().asDirectContext();
495 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500496 return nullptr;
497 }
498
Robert Phillipsa41c6852019-02-07 10:44:10 -0500499 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
500
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500501 sk_sp<GrTexture> tex =
Robert Phillipsa41c6852019-02-07 10:44:10 -0500502 resourceProvider->wrapBackendTexture(backendTex, ownership, cacheable, ioType);
Brian Salomonf7778972018-03-08 10:13:17 -0500503 if (!tex) {
504 return nullptr;
505 }
Robert Phillipsadbe1322018-01-17 13:35:46 -0500506
Greg Daniel6a0176b2018-01-30 09:28:44 -0500507 sk_sp<GrReleaseProcHelper> releaseHelper;
508 if (releaseProc) {
509 releaseHelper.reset(new GrReleaseProcHelper(releaseProc, releaseCtx));
Brian Salomonf7778972018-03-08 10:13:17 -0500510 // This gives the texture a ref on the releaseHelper
Greg Daniel8ce79912019-02-05 10:08:43 -0500511 tex->setRelease(std::move(releaseHelper));
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 sk_sp<GrReleaseProcHelper> releaseHelper;
549 if (releaseProc) {
550 releaseHelper.reset(new GrReleaseProcHelper(releaseProc, releaseCtx));
551 // This gives the texture a ref on the releaseHelper
552 tex->setRelease(std::move(releaseHelper));
553 }
554
Brian Salomonf7778972018-03-08 10:13:17 -0500555 SkASSERT(tex->asRenderTarget()); // A GrTextureRenderTarget
556 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500557 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Greg Daniel6abda432018-02-15 14:55:00 -0500558
Brian Salomonf7778972018-03-08 10:13:17 -0500559 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500560}
561
Brian Salomon7578f3e2018-03-07 14:39:54 -0500562sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget(
Greg Daniel8ce79912019-02-05 10:08:43 -0500563 const GrBackendRenderTarget& backendRT, GrSurfaceOrigin origin, ReleaseProc releaseProc,
564 ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500565 if (this->isAbandoned()) {
566 return nullptr;
567 }
568
Brian Salomonf7778972018-03-08 10:13:17 -0500569 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500570 GrContext* direct = fImageContext->priv().asDirectContext();
571 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500572 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500573 }
574
Robert Phillipsa41c6852019-02-07 10:44:10 -0500575 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
576
577 sk_sp<GrRenderTarget> rt = resourceProvider->wrapBackendRenderTarget(backendRT);
Brian Salomonf7778972018-03-08 10:13:17 -0500578 if (!rt) {
579 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500580 }
Greg Daniel8ce79912019-02-05 10:08:43 -0500581
582 sk_sp<GrReleaseProcHelper> releaseHelper;
583 if (releaseProc) {
584 releaseHelper.reset(new GrReleaseProcHelper(releaseProc, releaseCtx));
585 // This gives the render target a ref on the releaseHelper
586 rt->setRelease(std::move(releaseHelper));
587 }
588
Brian Salomonf7778972018-03-08 10:13:17 -0500589 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
590 SkASSERT(!rt->getUniqueKey().isValid());
591 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500592 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Brian Salomonf7778972018-03-08 10:13:17 -0500593
594 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(std::move(rt), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500595}
596
Brian Salomon7578f3e2018-03-07 14:39:54 -0500597sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendTextureAsRenderTarget(
598 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500599 if (this->isAbandoned()) {
600 return nullptr;
601 }
602
Brian Salomonf7778972018-03-08 10:13:17 -0500603 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500604 GrContext* direct = fImageContext->priv().asDirectContext();
605 if (!direct) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500606 return nullptr;
607 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500608
Robert Phillipsa41c6852019-02-07 10:44:10 -0500609 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
610
Brian Salomonf7778972018-03-08 10:13:17 -0500611 sk_sp<GrRenderTarget> rt =
Robert Phillipsa41c6852019-02-07 10:44:10 -0500612 resourceProvider->wrapBackendTextureAsRenderTarget(backendTex, sampleCnt);
Brian Salomonf7778972018-03-08 10:13:17 -0500613 if (!rt) {
614 return nullptr;
Greg Danielf87651e2018-02-21 11:36:53 -0500615 }
Brian Salomonf7778972018-03-08 10:13:17 -0500616 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
617 SkASSERT(!rt->getUniqueKey().isValid());
Greg Danielb46add82019-01-02 14:51:29 -0500618 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500619 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielf87651e2018-02-21 11:36:53 -0500620
Brian Salomonf7778972018-03-08 10:13:17 -0500621 return sk_sp<GrSurfaceProxy>(new GrRenderTargetProxy(std::move(rt), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500622}
623
Greg Danielb46add82019-01-02 14:51:29 -0500624sk_sp<GrRenderTargetProxy> GrProxyProvider::wrapVulkanSecondaryCBAsRenderTarget(
625 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
626 if (this->isAbandoned()) {
627 return nullptr;
628 }
629
630 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500631 GrContext* direct = fImageContext->priv().asDirectContext();
632 if (!direct) {
Greg Danielb46add82019-01-02 14:51:29 -0500633 return nullptr;
634 }
635
Robert Phillipsa41c6852019-02-07 10:44:10 -0500636 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Danielb46add82019-01-02 14:51:29 -0500637
Robert Phillipsa41c6852019-02-07 10:44:10 -0500638 sk_sp<GrRenderTarget> rt = resourceProvider->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
639 vkInfo);
Greg Danielb46add82019-01-02 14:51:29 -0500640 if (!rt) {
641 return nullptr;
642 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500643
Greg Danielb46add82019-01-02 14:51:29 -0500644 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
645 SkASSERT(!rt->getUniqueKey().isValid());
646 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500647 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielb46add82019-01-02 14:51:29 -0500648
649 // All Vulkan surfaces uses top left origins.
650 return sk_sp<GrRenderTargetProxy>(
651 new GrRenderTargetProxy(std::move(rt),
652 kTopLeft_GrSurfaceOrigin,
653 GrRenderTargetProxy::WrapsVkSecondaryCB::kYes));
654}
655
Robert Phillips777707b2018-01-17 11:40:14 -0500656sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500657 const GrBackendFormat& format,
Robert Phillips777707b2018-01-17 11:40:14 -0500658 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500659 GrSurfaceOrigin origin,
Brian Salomon7226c232018-07-30 13:13:17 -0400660 GrMipMapped mipMapped,
Brian Salomon7226c232018-07-30 13:13:17 -0400661 SkBackingFit fit,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500662 SkBudgeted budgeted) {
Greg Daniel4065d452018-11-16 15:43:41 -0500663 return this->createLazyProxy(std::move(callback), format, desc, origin, mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400664 GrInternalSurfaceFlags::kNone, fit, budgeted);
Greg Daniel2a303902018-02-20 10:25:54 -0500665}
666
667sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500668 const GrBackendFormat& format,
Greg Daniel2a303902018-02-20 10:25:54 -0500669 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500670 GrSurfaceOrigin origin,
Greg Daniel2a303902018-02-20 10:25:54 -0500671 GrMipMapped mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400672 GrInternalSurfaceFlags surfaceFlags,
Brian Salomon7226c232018-07-30 13:13:17 -0400673 SkBackingFit fit,
674 SkBudgeted budgeted) {
Greg Daniela8d92112018-03-09 12:05:04 -0500675 // For non-ddl draws always make lazy proxy's single use.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500676 LazyInstantiationType lazyType = this->renderingDirectly() ? LazyInstantiationType::kSingleUse
677 : LazyInstantiationType::kMultipleUse;
Greg Daniel4065d452018-11-16 15:43:41 -0500678 return this->createLazyProxy(std::move(callback), format, desc, origin, mipMapped, surfaceFlags,
679 fit, budgeted, lazyType);
Greg Daniela8d92112018-03-09 12:05:04 -0500680}
681
682sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500683 const GrBackendFormat& format,
Greg Daniela8d92112018-03-09 12:05:04 -0500684 const GrSurfaceDesc& desc,
685 GrSurfaceOrigin origin,
686 GrMipMapped mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400687 GrInternalSurfaceFlags surfaceFlags,
Brian Salomon7226c232018-07-30 13:13:17 -0400688 SkBackingFit fit,
689 SkBudgeted budgeted,
Greg Daniela8d92112018-03-09 12:05:04 -0500690 LazyInstantiationType lazyType) {
Robert Phillips777707b2018-01-17 11:40:14 -0500691 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
692 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400693
Robert Phillipsa41c6852019-02-07 10:44:10 -0500694 if (desc.fWidth > this->caps()->maxTextureSize() ||
695 desc.fHeight > this->caps()->maxTextureSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400696 return nullptr;
697 }
698
Greg Daniel457469c2018-02-08 15:05:44 -0500699
Greg Daniel2a303902018-02-20 10:25:54 -0500700#ifdef SK_DEBUG
701 if (SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags)) {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400702 if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kMixedSampled)) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500703 SkASSERT(this->caps()->usesMixedSamples() && desc.fSampleCnt > 1);
Greg Daniel2a303902018-02-20 10:25:54 -0500704 }
Greg Daniel2a303902018-02-20 10:25:54 -0500705 }
706#endif
707
Brian Salomon2a4f9832018-03-03 22:43:43 -0500708 return sk_sp<GrTextureProxy>(
709 SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags)
Greg Daniel4065d452018-11-16 15:43:41 -0500710 ? new GrTextureRenderTargetProxy(std::move(callback), lazyType, format, desc,
711 origin, mipMapped, fit, budgeted, surfaceFlags)
712 : new GrTextureProxy(std::move(callback), lazyType, format, desc, origin,
713 mipMapped, fit, budgeted, surfaceFlags));
Robert Phillips777707b2018-01-17 11:40:14 -0500714}
715
Robert Phillipse8fabb22018-02-04 14:33:21 -0500716sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500717 LazyInstantiateCallback&& callback, const GrBackendFormat& format,
718 const GrSurfaceDesc& desc, GrSurfaceOrigin origin, GrInternalSurfaceFlags surfaceFlags,
719 const TextureInfo* textureInfo, SkBackingFit fit, SkBudgeted budgeted) {
Robert Phillipse8fabb22018-02-04 14:33:21 -0500720 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
721 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400722
Robert Phillipsa41c6852019-02-07 10:44:10 -0500723 if (desc.fWidth > this->caps()->maxRenderTargetSize() ||
724 desc.fHeight > this->caps()->maxRenderTargetSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400725 return nullptr;
726 }
727
Greg Daniel2a303902018-02-20 10:25:54 -0500728 SkASSERT(SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags));
Greg Daniel457469c2018-02-08 15:05:44 -0500729
Greg Daniel2a303902018-02-20 10:25:54 -0500730#ifdef SK_DEBUG
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400731 if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kMixedSampled)) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500732 SkASSERT(this->caps()->usesMixedSamples() && desc.fSampleCnt > 1);
Greg Daniel2a303902018-02-20 10:25:54 -0500733 }
Greg Daniel2a303902018-02-20 10:25:54 -0500734#endif
735
Greg Daniel457469c2018-02-08 15:05:44 -0500736 using LazyInstantiationType = GrSurfaceProxy::LazyInstantiationType;
737 // For non-ddl draws always make lazy proxy's single use.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500738 LazyInstantiationType lazyType = this->renderingDirectly() ? LazyInstantiationType::kSingleUse
739 : LazyInstantiationType::kMultipleUse;
Greg Daniel457469c2018-02-08 15:05:44 -0500740
Brian Salomon7226c232018-07-30 13:13:17 -0400741 if (textureInfo) {
742 return sk_sp<GrRenderTargetProxy>(new GrTextureRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500743 std::move(callback), lazyType, format, desc, origin, textureInfo->fMipMapped,
744 fit, budgeted, surfaceFlags));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500745 }
746
Brian Salomon2a4f9832018-03-03 22:43:43 -0500747 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500748 std::move(callback), lazyType, format, desc, origin, fit, budgeted, surfaceFlags));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500749}
750
Chris Dalton4c458b12018-06-16 17:22:59 -0600751sk_sp<GrTextureProxy> GrProxyProvider::MakeFullyLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500752 const GrBackendFormat& format,
Chris Dalton4c458b12018-06-16 17:22:59 -0600753 Renderable renderable,
754 GrSurfaceOrigin origin,
755 GrPixelConfig config,
756 const GrCaps& caps) {
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;
761 }
Robert Phillips777707b2018-01-17 11:40:14 -0500762 desc.fWidth = -1;
763 desc.fHeight = -1;
764 desc.fConfig = config;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500765 desc.fSampleCnt = 1;
Robert Phillips777707b2018-01-17 11:40:14 -0500766
Chris Dalton4c458b12018-06-16 17:22:59 -0600767 return sk_sp<GrTextureProxy>(
768 (Renderable::kYes == renderable)
Brian Salomon7226c232018-07-30 13:13:17 -0400769 ? new GrTextureRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500770 std::move(callback), LazyInstantiationType::kSingleUse, format, desc,
771 origin, GrMipMapped::kNo, SkBackingFit::kApprox, SkBudgeted::kYes,
772 surfaceFlags)
Chris Dalton4c458b12018-06-16 17:22:59 -0600773 : new GrTextureProxy(std::move(callback), LazyInstantiationType::kSingleUse,
Greg Daniel4065d452018-11-16 15:43:41 -0500774 format, desc, origin, GrMipMapped::kNo,
Brian Salomon7226c232018-07-30 13:13:17 -0400775 SkBackingFit::kApprox, SkBudgeted::kYes, surfaceFlags));
Robert Phillips777707b2018-01-17 11:40:14 -0500776}
777
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500778bool GrProxyProvider::IsFunctionallyExact(GrSurfaceProxy* proxy) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400779 const bool isInstantiated = proxy->isInstantiated();
Robert Phillipsdb3b9792018-06-12 15:18:00 -0400780 // A proxy is functionally exact if:
781 // it is exact (obvs)
782 // when it is instantiated it will be exact (i.e., power of two dimensions)
783 // it is already instantiated and the proxy covers the entire backing surface
784 return proxy->priv().isExact() ||
785 (!isInstantiated && SkIsPow2(proxy->width()) && SkIsPow2(proxy->height())) ||
786 (isInstantiated && proxy->worstCaseWidth() == proxy->width() &&
787 proxy->worstCaseHeight() == proxy->height());
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500788}
789
Robert Phillips427966a2018-12-20 17:20:43 -0500790void GrProxyProvider::processInvalidUniqueKey(const GrUniqueKey& key, GrTextureProxy* proxy,
791 InvalidateGPUResource invalidateGPUResource) {
Chris Dalton2de13dd2019-01-03 15:11:59 -0700792 SkASSERT(key.isValid());
793
Robert Phillips427966a2018-12-20 17:20:43 -0500794 if (!proxy) {
795 proxy = fUniquelyKeyedProxies.find(key);
796 }
Chris Dalton2de13dd2019-01-03 15:11:59 -0700797 SkASSERT(!proxy || proxy->getUniqueKey() == key);
798
799 // Locate the corresponding GrGpuResource (if it needs to be invalidated) before clearing the
800 // proxy's unique key. We must do it in this order because 'key' may alias the proxy's key.
801 sk_sp<GrGpuResource> invalidGpuResource;
802 if (InvalidateGPUResource::kYes == invalidateGPUResource) {
803 if (proxy && proxy->isInstantiated()) {
804 invalidGpuResource = sk_ref_sp(proxy->peekSurface());
805 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500806 if (!invalidGpuResource) {
807 GrContext* direct = fImageContext->priv().asDirectContext();
808 if (direct) {
809 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
810 invalidGpuResource = resourceProvider->findByUniqueKey<GrGpuResource>(key);
811 }
Chris Dalton2de13dd2019-01-03 15:11:59 -0700812 }
813 SkASSERT(!invalidGpuResource || invalidGpuResource->getUniqueKey() == key);
814 }
Robert Phillips427966a2018-12-20 17:20:43 -0500815
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500816 // Note: this method is called for the whole variety of GrGpuResources so often 'key'
817 // will not be in 'fUniquelyKeyedProxies'.
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500818 if (proxy) {
Robert Phillips427966a2018-12-20 17:20:43 -0500819 fUniquelyKeyedProxies.remove(key);
820 proxy->cacheAccess().clearUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500821 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500822
Chris Dalton2de13dd2019-01-03 15:11:59 -0700823 if (invalidGpuResource) {
824 invalidGpuResource->resourcePriv().removeUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500825 }
826}
827
Robert Phillipsa41c6852019-02-07 10:44:10 -0500828uint32_t GrProxyProvider::contextID() const {
829 return fImageContext->priv().contextID();
830}
831
832const GrCaps* GrProxyProvider::caps() const {
833 return fImageContext->priv().caps();
834}
835
836sk_sp<const GrCaps> GrProxyProvider::refCaps() const {
837 return fImageContext->priv().refCaps();
838}
839
Robert Phillips0790f8a2018-09-18 13:11:03 -0400840void GrProxyProvider::orphanAllUniqueKeys() {
841 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
842 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
843 GrTextureProxy& tmp = *iter;
844
845 tmp.fProxyProvider = nullptr;
846 }
847}
848
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500849void GrProxyProvider::removeAllUniqueKeys() {
850 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
851 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
852 GrTextureProxy& tmp = *iter;
853
Chris Dalton2de13dd2019-01-03 15:11:59 -0700854 this->processInvalidUniqueKey(tmp.getUniqueKey(), &tmp, InvalidateGPUResource::kNo);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500855 }
856 SkASSERT(!fUniquelyKeyedProxies.count());
857}
Robert Phillipsa41c6852019-02-07 10:44:10 -0500858
859bool GrProxyProvider::renderingDirectly() const {
860 return fImageContext->priv().asDirectContext();
861}