blob: 5afd87ddc977763fac95f65c10dccfaa68782e28 [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 Phillips0bd24dc2018-01-16 08:06:32 -050011#include "GrRenderTarget.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050012#include "GrResourceKey.h"
13#include "GrResourceProvider.h"
14#include "GrSurfaceProxy.h"
15#include "GrSurfaceProxyPriv.h"
16#include "GrTexture.h"
17#include "GrTextureProxyCacheAccess.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050018#include "GrTextureRenderTargetProxy.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050019#include "../private/GrSingleOwner.h"
Brian Osmand29dcd12018-09-13 15:04:29 -040020#include "SkAutoPixmapStorage.h"
Greg Daniela4ead652018-02-07 10:21:48 -050021#include "SkBitmap.h"
Greg Daniel9d86f1d2018-01-29 09:33:59 -050022#include "SkGr.h"
23#include "SkImage.h"
24#include "SkImage_Base.h"
Greg Daniela4ead652018-02-07 10:21:48 -050025#include "SkImageInfoPriv.h"
26#include "SkImagePriv.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050027#include "SkMipMap.h"
Greg Daniela4ead652018-02-07 10:21:48 -050028#include "SkTraceEvent.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050029
30#define ASSERT_SINGLE_OWNER \
31 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fSingleOwner);)
32
33GrProxyProvider::GrProxyProvider(GrResourceProvider* resourceProvider,
34 GrResourceCache* resourceCache,
35 sk_sp<const GrCaps> caps,
36 GrSingleOwner* owner)
37 : fResourceProvider(resourceProvider)
38 , fResourceCache(resourceCache)
Robert Phillips4d120512018-01-19 13:22:07 -050039 , fAbandoned(false)
Robert Phillips1afd4cd2018-01-08 13:40:32 -050040 , fCaps(caps)
Brian Salomon238069b2018-07-11 15:58:57 -040041 , fContextUniqueID(resourceCache->contextUniqueID())
Robert Phillips1afd4cd2018-01-08 13:40:32 -050042#ifdef SK_DEBUG
43 , fSingleOwner(owner)
44#endif
45{
Brian Salomon238069b2018-07-11 15:58:57 -040046 SkASSERT(fResourceProvider);
47 SkASSERT(fResourceCache);
48 SkASSERT(fCaps);
49 SkASSERT(fSingleOwner);
50}
Robert Phillips1afd4cd2018-01-08 13:40:32 -050051
Brian Salomon238069b2018-07-11 15:58:57 -040052GrProxyProvider::GrProxyProvider(uint32_t contextUniqueID,
53 sk_sp<const GrCaps> caps,
54 GrSingleOwner* owner)
55 : fResourceProvider(nullptr)
56 , fResourceCache(nullptr)
57 , fAbandoned(false)
58 , fCaps(caps)
59 , fContextUniqueID(contextUniqueID)
60#ifdef SK_DEBUG
61 , fSingleOwner(owner)
62#endif
63{
64 SkASSERT(fContextUniqueID != SK_InvalidUniqueID);
65 SkASSERT(fCaps);
66 SkASSERT(fSingleOwner);
Robert Phillips1afd4cd2018-01-08 13:40:32 -050067}
68
69GrProxyProvider::~GrProxyProvider() {
Robert Phillips0790f8a2018-09-18 13:11:03 -040070 if (fResourceCache) {
71 // In DDL-mode a proxy provider can still have extant uniquely keyed proxies (since
72 // they need their unique keys to, potentially, find a cached resource when the
73 // DDL is played) but, in non-DDL-mode they should all have been cleaned up by this point.
74 SkASSERT(!fUniquelyKeyedProxies.count());
75 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050076}
77
Robert Phillipsadbe1322018-01-17 13:35:46 -050078bool GrProxyProvider::assignUniqueKeyToProxy(const GrUniqueKey& key, GrTextureProxy* proxy) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050079 ASSERT_SINGLE_OWNER
80 SkASSERT(key.isValid());
81 if (this->isAbandoned() || !proxy) {
Robert Phillipsadbe1322018-01-17 13:35:46 -050082 return false;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050083 }
84
85 // If there is already a GrResource with this key then the caller has violated the normal
86 // usage pattern of uniquely keyed resources (e.g., they have created one w/o first seeing
87 // if it already existed in the cache).
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -050088 SkASSERT(!fResourceCache || !fResourceCache->findAndRefUniqueResource(key));
Robert Phillips1afd4cd2018-01-08 13:40:32 -050089
Robert Phillips1afd4cd2018-01-08 13:40:32 -050090 SkASSERT(!fUniquelyKeyedProxies.find(key)); // multiple proxies can't get the same key
91
92 proxy->cacheAccess().setUniqueKey(this, key);
93 SkASSERT(proxy->getUniqueKey() == key);
94 fUniquelyKeyedProxies.add(proxy);
Robert Phillipsadbe1322018-01-17 13:35:46 -050095 return true;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050096}
97
98void GrProxyProvider::adoptUniqueKeyFromSurface(GrTextureProxy* proxy, const GrSurface* surf) {
99 SkASSERT(surf->getUniqueKey().isValid());
100 proxy->cacheAccess().setUniqueKey(this, surf->getUniqueKey());
101 SkASSERT(proxy->getUniqueKey() == surf->getUniqueKey());
102 // multiple proxies can't get the same key
103 SkASSERT(!fUniquelyKeyedProxies.find(surf->getUniqueKey()));
104 fUniquelyKeyedProxies.add(proxy);
105}
106
Chris Dalton2de13dd2019-01-03 15:11:59 -0700107void GrProxyProvider::removeUniqueKeyFromProxy(GrTextureProxy* proxy) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500108 ASSERT_SINGLE_OWNER
Chris Dalton2de13dd2019-01-03 15:11:59 -0700109 SkASSERT(proxy);
110 SkASSERT(proxy->getUniqueKey().isValid());
111
112 if (this->isAbandoned()) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500113 return;
114 }
Robert Phillips0790f8a2018-09-18 13:11:03 -0400115
Chris Dalton2de13dd2019-01-03 15:11:59 -0700116 this->processInvalidUniqueKey(proxy->getUniqueKey(), proxy, InvalidateGPUResource::kYes);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500117}
118
119sk_sp<GrTextureProxy> GrProxyProvider::findProxyByUniqueKey(const GrUniqueKey& key,
120 GrSurfaceOrigin origin) {
121 ASSERT_SINGLE_OWNER
122
123 if (this->isAbandoned()) {
124 return nullptr;
125 }
126
127 sk_sp<GrTextureProxy> result = sk_ref_sp(fUniquelyKeyedProxies.find(key));
128 if (result) {
129 SkASSERT(result->origin() == origin);
130 }
131 return result;
132}
133
Robert Phillipsadbe1322018-01-17 13:35:46 -0500134sk_sp<GrTextureProxy> GrProxyProvider::createWrapped(sk_sp<GrTexture> tex, GrSurfaceOrigin origin) {
135#ifdef SK_DEBUG
136 if (tex->getUniqueKey().isValid()) {
137 SkASSERT(!this->findProxyByUniqueKey(tex->getUniqueKey(), origin));
138 }
139#endif
140
141 if (tex->asRenderTarget()) {
142 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin));
143 } else {
144 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin));
145 }
146}
147
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500148sk_sp<GrTextureProxy> GrProxyProvider::findOrCreateProxyByUniqueKey(const GrUniqueKey& key,
149 GrSurfaceOrigin origin) {
150 ASSERT_SINGLE_OWNER
151
152 if (this->isAbandoned()) {
153 return nullptr;
154 }
155
156 sk_sp<GrTextureProxy> result = this->findProxyByUniqueKey(key, origin);
157 if (result) {
158 return result;
159 }
160
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500161 if (!fResourceCache) {
162 return nullptr;
163 }
164
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500165 GrGpuResource* resource = fResourceCache->findAndRefUniqueResource(key);
166 if (!resource) {
167 return nullptr;
168 }
169
170 sk_sp<GrTexture> texture(static_cast<GrSurface*>(resource)->asTexture());
171 SkASSERT(texture);
172
Robert Phillipsadbe1322018-01-17 13:35:46 -0500173 result = this->createWrapped(std::move(texture), origin);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500174 SkASSERT(result->getUniqueKey() == key);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500175 // createWrapped should've added this for us
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500176 SkASSERT(fUniquelyKeyedProxies.find(key));
177 return result;
178}
179
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500180sk_sp<GrTextureProxy> GrProxyProvider::createTextureProxy(sk_sp<SkImage> srcImage,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400181 GrSurfaceDescFlags descFlags,
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500182 int sampleCnt,
Greg Danielfb3abcd2018-02-02 15:48:33 -0500183 SkBudgeted budgeted,
Chris Daltond004e0b2018-09-27 09:28:03 -0600184 SkBackingFit fit,
185 GrInternalSurfaceFlags surfaceFlags) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500186 ASSERT_SINGLE_OWNER
187 SkASSERT(srcImage);
188
189 if (this->isAbandoned()) {
190 return nullptr;
191 }
192
Brian Osmand29dcd12018-09-13 15:04:29 -0400193 SkImageInfo info = as_IB(srcImage)->onImageInfo();
194 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
Greg Danielf87651e2018-02-21 11:36:53 -0500195
Greg Daniel0a7aa142018-02-21 13:02:32 -0500196 if (kUnknown_GrPixelConfig == config) {
197 return nullptr;
198 }
199
Greg Daniel4065d452018-11-16 15:43:41 -0500200 GrBackendFormat format = fCaps->getBackendFormatFromColorType(info.colorType());
201 if (!format.isValid()) {
202 return nullptr;
203 }
204
Brian Osmand29dcd12018-09-13 15:04:29 -0400205 if (!this->caps()->isConfigTexturable(config)) {
206 SkBitmap copy8888;
207 if (!copy8888.tryAllocPixels(info.makeColorType(kRGBA_8888_SkColorType)) ||
208 !srcImage->readPixels(copy8888.pixmap(), 0, 0)) {
209 return nullptr;
210 }
211 copy8888.setImmutable();
212 srcImage = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode);
213 config = kRGBA_8888_GrPixelConfig;
214 }
215
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400216 if (SkToBool(descFlags & kRenderTarget_GrSurfaceFlag)) {
Greg Danielf87651e2018-02-21 11:36:53 -0500217 sampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, config);
218 if (!sampleCnt) {
219 return nullptr;
220 }
221 }
222
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400223 if (SkToBool(descFlags & kRenderTarget_GrSurfaceFlag)) {
Greg Daniel2a303902018-02-20 10:25:54 -0500224 if (fCaps->usesMixedSamples() && sampleCnt > 1) {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400225 surfaceFlags |= GrInternalSurfaceFlags::kMixedSampled;
Greg Daniel2a303902018-02-20 10:25:54 -0500226 }
Greg Daniel2a303902018-02-20 10:25:54 -0500227 }
228
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500229 GrSurfaceDesc desc;
230 desc.fWidth = srcImage->width();
231 desc.fHeight = srcImage->height();
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400232 desc.fFlags = descFlags;
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500233 desc.fSampleCnt = sampleCnt;
Greg Danielf87651e2018-02-21 11:36:53 -0500234 desc.fConfig = config;
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500235
236 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Chris Daltond004e0b2018-09-27 09:28:03 -0600237 [desc, budgeted, srcImage, fit, surfaceFlags](GrResourceProvider* resourceProvider) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500238 if (!resourceProvider) {
Greg Daniel0a375db2018-02-01 12:21:39 -0500239 // Nothing to clean up here. Once the proxy (and thus lambda) is deleted the ref
240 // on srcImage will be released.
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500241 return sk_sp<GrTexture>();
242 }
243 SkPixmap pixMap;
244 SkAssertResult(srcImage->peekPixels(&pixMap));
245 GrMipLevel mipLevel = { pixMap.addr(), pixMap.rowBytes() };
246
Chris Daltond004e0b2018-09-27 09:28:03 -0600247 auto resourceProviderFlags = GrResourceProvider::Flags::kNone;
248 if (surfaceFlags & GrInternalSurfaceFlags::kNoPendingIO) {
249 resourceProviderFlags |= GrResourceProvider::Flags::kNoPendingIO;
250 }
251 return resourceProvider->createTexture(desc, budgeted, fit, mipLevel,
252 resourceProviderFlags);
Brian Salomon2a4f9832018-03-03 22:43:43 -0500253 },
Greg Daniel4065d452018-11-16 15:43:41 -0500254 format, desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, surfaceFlags, fit, budgeted);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500255
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400256 if (!proxy) {
257 return nullptr;
258 }
259
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500260 if (fResourceProvider) {
261 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
262 // we're better off instantiating the proxy immediately here.
263 if (!proxy->priv().doLazyInstantiation(fResourceProvider)) {
264 return nullptr;
265 }
266 }
Robert Phillipsc1b60662018-06-26 10:20:08 -0400267
268 SkASSERT(proxy->width() == desc.fWidth);
269 SkASSERT(proxy->height() == desc.fHeight);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500270 return proxy;
271}
272
Greg Daniel4065d452018-11-16 15:43:41 -0500273sk_sp<GrTextureProxy> GrProxyProvider::createMipMapProxy(const GrBackendFormat& format,
274 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500275 GrSurfaceOrigin origin,
Greg Daniel30815082018-02-09 16:08:30 -0500276 SkBudgeted budgeted) {
Robert Phillips579f0942018-01-08 14:53:35 -0500277 ASSERT_SINGLE_OWNER
278
279 if (this->isAbandoned()) {
280 return nullptr;
281 }
282
Greg Daniel4065d452018-11-16 15:43:41 -0500283 return this->createProxy(format, desc, origin, GrMipMapped::kYes, SkBackingFit::kExact,
284 budgeted, GrInternalSurfaceFlags::kNone);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500285}
286
Brian Osman2b23c4b2018-06-01 12:25:08 -0400287sk_sp<GrTextureProxy> GrProxyProvider::createMipMapProxyFromBitmap(const SkBitmap& bitmap) {
Brian Osman412674f2019-02-07 15:34:58 -0500288 ASSERT_SINGLE_OWNER
289
290 if (this->isAbandoned()) {
291 return nullptr;
292 }
293
Brian Osman2b23c4b2018-06-01 12:25:08 -0400294 if (!SkImageInfoIsValid(bitmap.info())) {
Greg Daniela4ead652018-02-07 10:21:48 -0500295 return nullptr;
296 }
297
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400298 ATRACE_ANDROID_FRAMEWORK("Upload MipMap Texture [%ux%u]", bitmap.width(), bitmap.height());
Greg Daniela4ead652018-02-07 10:21:48 -0500299
300 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
301 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
302 // upload of the data to the gpu can happen at anytime and the bitmap may change by then.
Robert Phillips5c4b33b2018-03-20 16:23:08 -0400303 SkCopyPixelsMode copyMode = this->recordingDDL() ? kIfMutable_SkCopyPixelsMode
304 : kNever_SkCopyPixelsMode;
Greg Daniela4ead652018-02-07 10:21:48 -0500305 sk_sp<SkImage> baseLevel = SkMakeImageFromRasterBitmap(bitmap, copyMode);
Greg Daniela4ead652018-02-07 10:21:48 -0500306 if (!baseLevel) {
307 return nullptr;
308 }
309
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400310 // This was never going to have mips anyway
311 if (0 == SkMipMap::ComputeLevelCount(baseLevel->width(), baseLevel->height())) {
Brian Salomon58389b92018-03-07 13:01:25 -0500312 return this->createTextureProxy(baseLevel, kNone_GrSurfaceFlags, 1, SkBudgeted::kYes,
313 SkBackingFit::kExact);
Greg Daniela4ead652018-02-07 10:21:48 -0500314 }
315
Greg Daniel4065d452018-11-16 15:43:41 -0500316 const GrBackendFormat format = fCaps->getBackendFormatFromColorType(bitmap.info().colorType());
317 if (!format.isValid()) {
318 return nullptr;
319 }
320
Brian Osmand29dcd12018-09-13 15:04:29 -0400321 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info());
322 if (!this->caps()->isConfigTexturable(desc.fConfig)) {
323 SkBitmap copy8888;
324 if (!copy8888.tryAllocPixels(bitmap.info().makeColorType(kRGBA_8888_SkColorType)) ||
325 !bitmap.readPixels(copy8888.pixmap())) {
326 return nullptr;
327 }
328 copy8888.setImmutable();
329 baseLevel = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode);
330 desc.fConfig = kRGBA_8888_GrPixelConfig;
331 }
332
333 SkPixmap pixmap;
334 SkAssertResult(baseLevel->peekPixels(&pixmap));
335 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(pixmap, nullptr));
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400336 if (!mipmaps) {
337 return nullptr;
338 }
339
Greg Daniela4ead652018-02-07 10:21:48 -0500340 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Osman1b97f132018-09-13 17:33:48 +0000341 [desc, baseLevel, mipmaps](GrResourceProvider* resourceProvider) {
Greg Daniela4ead652018-02-07 10:21:48 -0500342 if (!resourceProvider) {
343 return sk_sp<GrTexture>();
344 }
345
Brian Osman1b97f132018-09-13 17:33:48 +0000346 const int mipLevelCount = mipmaps->countLevels() + 1;
347 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
348
Greg Daniela4ead652018-02-07 10:21:48 -0500349 SkPixmap pixmap;
350 SkAssertResult(baseLevel->peekPixels(&pixmap));
351
352 // DDL TODO: Instead of copying all this info into GrMipLevels we should just plumb
353 // the use of SkMipMap down through Ganesh.
354 texels[0].fPixels = pixmap.addr();
355 texels[0].fRowBytes = pixmap.rowBytes();
356
357 for (int i = 1; i < mipLevelCount; ++i) {
358 SkMipMap::Level generatedMipLevel;
359 mipmaps->getLevel(i - 1, &generatedMipLevel);
360 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
361 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
362 SkASSERT(texels[i].fPixels);
363 }
364
Brian Salomon58389b92018-03-07 13:01:25 -0500365 return resourceProvider->createTexture(desc, SkBudgeted::kYes, texels.get(),
Brian Osman2b23c4b2018-06-01 12:25:08 -0400366 mipLevelCount);
Brian Salomon2a4f9832018-03-03 22:43:43 -0500367 },
Greg Daniel4065d452018-11-16 15:43:41 -0500368 format, desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kYes, SkBackingFit::kExact,
369 SkBudgeted::kYes);
Greg Daniela4ead652018-02-07 10:21:48 -0500370
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400371 if (!proxy) {
372 return nullptr;
373 }
374
Greg Daniela4ead652018-02-07 10:21:48 -0500375 if (fResourceProvider) {
376 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
377 // we're better off instantiating the proxy immediately here.
378 if (!proxy->priv().doLazyInstantiation(fResourceProvider)) {
379 return nullptr;
380 }
381 }
382 return proxy;
383}
384
Greg Daniel4065d452018-11-16 15:43:41 -0500385sk_sp<GrTextureProxy> GrProxyProvider::createProxy(const GrBackendFormat& format,
386 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500387 GrSurfaceOrigin origin,
Greg Danielf6f7b672018-02-15 13:06:26 -0500388 GrMipMapped mipMapped,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500389 SkBackingFit fit,
390 SkBudgeted budgeted,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400391 GrInternalSurfaceFlags surfaceFlags) {
Greg Danielf6f7b672018-02-15 13:06:26 -0500392 if (GrMipMapped::kYes == mipMapped) {
393 // SkMipMap doesn't include the base level in the level count so we have to add 1
394 int mipCount = SkMipMap::ComputeLevelCount(desc.fWidth, desc.fHeight) + 1;
395 if (1 == mipCount) {
396 mipMapped = GrMipMapped::kNo;
397 }
398 }
399
400 if (!this->caps()->validateSurfaceDesc(desc, mipMapped)) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500401 return nullptr;
402 }
Brian Salomon3a2cc2c2018-02-03 00:25:12 +0000403 GrSurfaceDesc copyDesc = desc;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500404 if (desc.fFlags & kRenderTarget_GrSurfaceFlag) {
405 copyDesc.fSampleCnt =
406 this->caps()->getRenderTargetSampleCount(desc.fSampleCnt, desc.fConfig);
407 }
Brian Salomon3a2cc2c2018-02-03 00:25:12 +0000408
Brian Salomonbdecacf2018-02-02 20:32:49 -0500409 if (copyDesc.fFlags & kRenderTarget_GrSurfaceFlag) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500410 // We know anything we instantiate later from this deferred path will be
411 // both texturable and renderable
Greg Daniel4065d452018-11-16 15:43:41 -0500412 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(*this->caps(), format, copyDesc,
413 origin, mipMapped,
Brian Salomon7226c232018-07-30 13:13:17 -0400414 fit, budgeted, surfaceFlags));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500415 }
416
Greg Daniel4065d452018-11-16 15:43:41 -0500417 return sk_sp<GrTextureProxy>(new GrTextureProxy(format, copyDesc, origin, mipMapped,
Brian Salomon7226c232018-07-30 13:13:17 -0400418 fit, budgeted, surfaceFlags));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500419}
420
Jim Van Verthee06b332019-01-18 10:36:32 -0500421sk_sp<GrTextureProxy> GrProxyProvider::createProxy(sk_sp<SkData> data, const GrSurfaceDesc& desc) {
422 if (!this->caps()->isConfigTexturable(desc.fConfig)) {
423 return nullptr;
424 }
425
426 const GrColorType ct = GrPixelConfigToColorType(desc.fConfig);
427 const GrBackendFormat format = fCaps->getBackendFormatFromGrColorType(ct, GrSRGBEncoded::kNo);
428
429 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
430 [desc, data](GrResourceProvider* resourceProvider) {
431 if (!resourceProvider) {
432 return sk_sp<GrTexture>();
433 }
434
435 GrMipLevel texels;
436 texels.fPixels = data->data();
437 texels.fRowBytes = GrBytesPerPixel(desc.fConfig)*desc.fWidth;
438 return resourceProvider->createTexture(desc, SkBudgeted::kYes, &texels, 1);
439 },
440 format, desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, SkBackingFit::kExact,
441 SkBudgeted::kYes);
442
443 if (!proxy) {
444 return nullptr;
445 }
446
447 if (fResourceProvider) {
448 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
449 // we're better off instantiating the proxy immediately here.
450 if (!proxy->priv().doLazyInstantiation(fResourceProvider)) {
451 return nullptr;
452 }
453 }
454 return proxy;
455}
456
Brian Salomon7578f3e2018-03-07 14:39:54 -0500457sk_sp<GrTextureProxy> GrProxyProvider::wrapBackendTexture(const GrBackendTexture& backendTex,
458 GrSurfaceOrigin origin,
459 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500460 GrWrapCacheable cacheable,
Brian Salomonc67c31c2018-12-06 10:00:03 -0500461 GrIOType ioType,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500462 ReleaseProc releaseProc,
463 ReleaseContext releaseCtx) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500464 SkASSERT(ioType != kWrite_GrIOType);
Robert Phillipsf9bec202018-01-16 09:21:01 -0500465 if (this->isAbandoned()) {
466 return nullptr;
467 }
468
Brian Salomonf7778972018-03-08 10:13:17 -0500469 // This is only supported on a direct GrContext.
470 if (!fResourceProvider) {
471 return nullptr;
472 }
473
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500474 sk_sp<GrTexture> tex =
475 fResourceProvider->wrapBackendTexture(backendTex, ownership, cacheable, ioType);
Brian Salomonf7778972018-03-08 10:13:17 -0500476 if (!tex) {
477 return nullptr;
478 }
Robert Phillipsadbe1322018-01-17 13:35:46 -0500479
Greg Daniel6a0176b2018-01-30 09:28:44 -0500480 sk_sp<GrReleaseProcHelper> releaseHelper;
481 if (releaseProc) {
482 releaseHelper.reset(new GrReleaseProcHelper(releaseProc, releaseCtx));
Brian Salomonf7778972018-03-08 10:13:17 -0500483 // This gives the texture a ref on the releaseHelper
Greg Daniel8ce79912019-02-05 10:08:43 -0500484 tex->setRelease(std::move(releaseHelper));
Greg Daniel6a0176b2018-01-30 09:28:44 -0500485 }
486
Brian Salomonf7778972018-03-08 10:13:17 -0500487 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
488 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500489 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500490
Brian Salomonf7778972018-03-08 10:13:17 -0500491 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500492}
493
Brian Salomon7578f3e2018-03-07 14:39:54 -0500494sk_sp<GrTextureProxy> GrProxyProvider::wrapRenderableBackendTexture(
Brian Salomon02bd2952018-03-07 15:20:21 -0500495 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt,
Greg Daniel8ce79912019-02-05 10:08:43 -0500496 GrWrapOwnership ownership, GrWrapCacheable cacheable, ReleaseProc releaseProc,
497 ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500498 if (this->isAbandoned()) {
499 return nullptr;
500 }
501
Brian Salomonf7778972018-03-08 10:13:17 -0500502 // This is only supported on a direct GrContext.
503 if (!fResourceProvider) {
504 return nullptr;
505 }
506
Greg Daniel6abda432018-02-15 14:55:00 -0500507 sampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config());
Greg Danielf87651e2018-02-21 11:36:53 -0500508 if (!sampleCnt) {
509 return nullptr;
510 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500511
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500512 sk_sp<GrTexture> tex = fResourceProvider->wrapRenderableBackendTexture(backendTex, sampleCnt,
513 ownership, cacheable);
Brian Salomonf7778972018-03-08 10:13:17 -0500514 if (!tex) {
515 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500516 }
517
Greg Daniel8ce79912019-02-05 10:08:43 -0500518 sk_sp<GrReleaseProcHelper> releaseHelper;
519 if (releaseProc) {
520 releaseHelper.reset(new GrReleaseProcHelper(releaseProc, releaseCtx));
521 // This gives the texture a ref on the releaseHelper
522 tex->setRelease(std::move(releaseHelper));
523 }
524
Brian Salomonf7778972018-03-08 10:13:17 -0500525 SkASSERT(tex->asRenderTarget()); // A GrTextureRenderTarget
526 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500527 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Greg Daniel6abda432018-02-15 14:55:00 -0500528
Brian Salomonf7778972018-03-08 10:13:17 -0500529 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500530}
531
Brian Salomon7578f3e2018-03-07 14:39:54 -0500532sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget(
Greg Daniel8ce79912019-02-05 10:08:43 -0500533 const GrBackendRenderTarget& backendRT, GrSurfaceOrigin origin, ReleaseProc releaseProc,
534 ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500535 if (this->isAbandoned()) {
536 return nullptr;
537 }
538
Brian Salomonf7778972018-03-08 10:13:17 -0500539 // This is only supported on a direct GrContext.
540 if (!fResourceProvider) {
541 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500542 }
543
Brian Salomonf7778972018-03-08 10:13:17 -0500544 sk_sp<GrRenderTarget> rt = fResourceProvider->wrapBackendRenderTarget(backendRT);
545 if (!rt) {
546 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500547 }
Greg Daniel8ce79912019-02-05 10:08:43 -0500548
549 sk_sp<GrReleaseProcHelper> releaseHelper;
550 if (releaseProc) {
551 releaseHelper.reset(new GrReleaseProcHelper(releaseProc, releaseCtx));
552 // This gives the render target a ref on the releaseHelper
553 rt->setRelease(std::move(releaseHelper));
554 }
555
Brian Salomonf7778972018-03-08 10:13:17 -0500556 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
557 SkASSERT(!rt->getUniqueKey().isValid());
558 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500559 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Brian Salomonf7778972018-03-08 10:13:17 -0500560
561 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(std::move(rt), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500562}
563
Brian Salomon7578f3e2018-03-07 14:39:54 -0500564sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendTextureAsRenderTarget(
565 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500566 if (this->isAbandoned()) {
567 return nullptr;
568 }
569
Brian Salomonf7778972018-03-08 10:13:17 -0500570 // This is only supported on a direct GrContext.
571 if (!fResourceProvider) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500572 return nullptr;
573 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500574
Brian Salomonf7778972018-03-08 10:13:17 -0500575 sk_sp<GrRenderTarget> rt =
576 fResourceProvider->wrapBackendTextureAsRenderTarget(backendTex, sampleCnt);
577 if (!rt) {
578 return nullptr;
Greg Danielf87651e2018-02-21 11:36:53 -0500579 }
Brian Salomonf7778972018-03-08 10:13:17 -0500580 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
581 SkASSERT(!rt->getUniqueKey().isValid());
Greg Danielb46add82019-01-02 14:51:29 -0500582 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500583 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielf87651e2018-02-21 11:36:53 -0500584
Brian Salomonf7778972018-03-08 10:13:17 -0500585 return sk_sp<GrSurfaceProxy>(new GrRenderTargetProxy(std::move(rt), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500586}
587
Greg Danielb46add82019-01-02 14:51:29 -0500588sk_sp<GrRenderTargetProxy> GrProxyProvider::wrapVulkanSecondaryCBAsRenderTarget(
589 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
590 if (this->isAbandoned()) {
591 return nullptr;
592 }
593
594 // This is only supported on a direct GrContext.
595 if (!fResourceProvider) {
596 return nullptr;
597 }
598
599 sk_sp<GrRenderTarget> rt = fResourceProvider->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
600 vkInfo);
601
602 if (!rt) {
603 return nullptr;
604 }
605 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
606 SkASSERT(!rt->getUniqueKey().isValid());
607 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500608 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielb46add82019-01-02 14:51:29 -0500609
610 // All Vulkan surfaces uses top left origins.
611 return sk_sp<GrRenderTargetProxy>(
612 new GrRenderTargetProxy(std::move(rt),
613 kTopLeft_GrSurfaceOrigin,
614 GrRenderTargetProxy::WrapsVkSecondaryCB::kYes));
615}
616
Robert Phillips777707b2018-01-17 11:40:14 -0500617sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500618 const GrBackendFormat& format,
Robert Phillips777707b2018-01-17 11:40:14 -0500619 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500620 GrSurfaceOrigin origin,
Brian Salomon7226c232018-07-30 13:13:17 -0400621 GrMipMapped mipMapped,
Brian Salomon7226c232018-07-30 13:13:17 -0400622 SkBackingFit fit,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500623 SkBudgeted budgeted) {
Greg Daniel4065d452018-11-16 15:43:41 -0500624 return this->createLazyProxy(std::move(callback), format, desc, origin, mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400625 GrInternalSurfaceFlags::kNone, fit, budgeted);
Greg Daniel2a303902018-02-20 10:25:54 -0500626}
627
628sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500629 const GrBackendFormat& format,
Greg Daniel2a303902018-02-20 10:25:54 -0500630 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500631 GrSurfaceOrigin origin,
Greg Daniel2a303902018-02-20 10:25:54 -0500632 GrMipMapped mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400633 GrInternalSurfaceFlags surfaceFlags,
Brian Salomon7226c232018-07-30 13:13:17 -0400634 SkBackingFit fit,
635 SkBudgeted budgeted) {
Greg Daniela8d92112018-03-09 12:05:04 -0500636 // For non-ddl draws always make lazy proxy's single use.
637 LazyInstantiationType lazyType = fResourceProvider ? LazyInstantiationType::kSingleUse
638 : LazyInstantiationType::kMultipleUse;
Greg Daniel4065d452018-11-16 15:43:41 -0500639 return this->createLazyProxy(std::move(callback), format, desc, origin, mipMapped, surfaceFlags,
640 fit, budgeted, lazyType);
Greg Daniela8d92112018-03-09 12:05:04 -0500641}
642
643sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500644 const GrBackendFormat& format,
Greg Daniela8d92112018-03-09 12:05:04 -0500645 const GrSurfaceDesc& desc,
646 GrSurfaceOrigin origin,
647 GrMipMapped mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400648 GrInternalSurfaceFlags surfaceFlags,
Brian Salomon7226c232018-07-30 13:13:17 -0400649 SkBackingFit fit,
650 SkBudgeted budgeted,
Greg Daniela8d92112018-03-09 12:05:04 -0500651 LazyInstantiationType lazyType) {
Robert Phillips777707b2018-01-17 11:40:14 -0500652 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
653 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400654
655 if (desc.fWidth > fCaps->maxTextureSize() || desc.fHeight > fCaps->maxTextureSize()) {
656 return nullptr;
657 }
658
Greg Daniel457469c2018-02-08 15:05:44 -0500659
Greg Daniel2a303902018-02-20 10:25:54 -0500660#ifdef SK_DEBUG
661 if (SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags)) {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400662 if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kMixedSampled)) {
Greg Daniel2a303902018-02-20 10:25:54 -0500663 SkASSERT(fCaps->usesMixedSamples() && desc.fSampleCnt > 1);
664 }
Greg Daniel2a303902018-02-20 10:25:54 -0500665 }
666#endif
667
Brian Salomon2a4f9832018-03-03 22:43:43 -0500668 return sk_sp<GrTextureProxy>(
669 SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags)
Greg Daniel4065d452018-11-16 15:43:41 -0500670 ? new GrTextureRenderTargetProxy(std::move(callback), lazyType, format, desc,
671 origin, mipMapped, fit, budgeted, surfaceFlags)
672 : new GrTextureProxy(std::move(callback), lazyType, format, desc, origin,
673 mipMapped, fit, budgeted, surfaceFlags));
Robert Phillips777707b2018-01-17 11:40:14 -0500674}
675
Robert Phillipse8fabb22018-02-04 14:33:21 -0500676sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500677 LazyInstantiateCallback&& callback, const GrBackendFormat& format,
678 const GrSurfaceDesc& desc, GrSurfaceOrigin origin, GrInternalSurfaceFlags surfaceFlags,
679 const TextureInfo* textureInfo, SkBackingFit fit, SkBudgeted budgeted) {
Robert Phillipse8fabb22018-02-04 14:33:21 -0500680 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
681 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400682
683 if (desc.fWidth > fCaps->maxRenderTargetSize() || desc.fHeight > fCaps->maxRenderTargetSize()) {
684 return nullptr;
685 }
686
Greg Daniel2a303902018-02-20 10:25:54 -0500687 SkASSERT(SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags));
Greg Daniel457469c2018-02-08 15:05:44 -0500688
Greg Daniel2a303902018-02-20 10:25:54 -0500689#ifdef SK_DEBUG
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400690 if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kMixedSampled)) {
Greg Daniel2a303902018-02-20 10:25:54 -0500691 SkASSERT(fCaps->usesMixedSamples() && desc.fSampleCnt > 1);
692 }
Greg Daniel2a303902018-02-20 10:25:54 -0500693#endif
694
Greg Daniel457469c2018-02-08 15:05:44 -0500695 using LazyInstantiationType = GrSurfaceProxy::LazyInstantiationType;
696 // For non-ddl draws always make lazy proxy's single use.
697 LazyInstantiationType lazyType = fResourceProvider ? LazyInstantiationType::kSingleUse
698 : LazyInstantiationType::kMultipleUse;
699
Brian Salomon7226c232018-07-30 13:13:17 -0400700 if (textureInfo) {
701 return sk_sp<GrRenderTargetProxy>(new GrTextureRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500702 std::move(callback), lazyType, format, desc, origin, textureInfo->fMipMapped,
703 fit, budgeted, surfaceFlags));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500704 }
705
Brian Salomon2a4f9832018-03-03 22:43:43 -0500706 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500707 std::move(callback), lazyType, format, desc, origin, fit, budgeted, surfaceFlags));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500708}
709
Chris Dalton4c458b12018-06-16 17:22:59 -0600710sk_sp<GrTextureProxy> GrProxyProvider::MakeFullyLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500711 const GrBackendFormat& format,
Chris Dalton4c458b12018-06-16 17:22:59 -0600712 Renderable renderable,
713 GrSurfaceOrigin origin,
714 GrPixelConfig config,
715 const GrCaps& caps) {
Robert Phillips777707b2018-01-17 11:40:14 -0500716 GrSurfaceDesc desc;
Greg Daniele3204862018-04-16 11:24:10 -0400717 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNoPendingIO;
Robert Phillips777707b2018-01-17 11:40:14 -0500718 if (Renderable::kYes == renderable) {
719 desc.fFlags = kRenderTarget_GrSurfaceFlag;
720 }
Robert Phillips777707b2018-01-17 11:40:14 -0500721 desc.fWidth = -1;
722 desc.fHeight = -1;
723 desc.fConfig = config;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500724 desc.fSampleCnt = 1;
Robert Phillips777707b2018-01-17 11:40:14 -0500725
Chris Dalton4c458b12018-06-16 17:22:59 -0600726 return sk_sp<GrTextureProxy>(
727 (Renderable::kYes == renderable)
Brian Salomon7226c232018-07-30 13:13:17 -0400728 ? new GrTextureRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500729 std::move(callback), LazyInstantiationType::kSingleUse, format, desc,
730 origin, GrMipMapped::kNo, SkBackingFit::kApprox, SkBudgeted::kYes,
731 surfaceFlags)
Chris Dalton4c458b12018-06-16 17:22:59 -0600732 : new GrTextureProxy(std::move(callback), LazyInstantiationType::kSingleUse,
Greg Daniel4065d452018-11-16 15:43:41 -0500733 format, desc, origin, GrMipMapped::kNo,
Brian Salomon7226c232018-07-30 13:13:17 -0400734 SkBackingFit::kApprox, SkBudgeted::kYes, surfaceFlags));
Robert Phillips777707b2018-01-17 11:40:14 -0500735}
736
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500737bool GrProxyProvider::IsFunctionallyExact(GrSurfaceProxy* proxy) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400738 const bool isInstantiated = proxy->isInstantiated();
Robert Phillipsdb3b9792018-06-12 15:18:00 -0400739 // A proxy is functionally exact if:
740 // it is exact (obvs)
741 // when it is instantiated it will be exact (i.e., power of two dimensions)
742 // it is already instantiated and the proxy covers the entire backing surface
743 return proxy->priv().isExact() ||
744 (!isInstantiated && SkIsPow2(proxy->width()) && SkIsPow2(proxy->height())) ||
745 (isInstantiated && proxy->worstCaseWidth() == proxy->width() &&
746 proxy->worstCaseHeight() == proxy->height());
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500747}
748
Robert Phillips427966a2018-12-20 17:20:43 -0500749void GrProxyProvider::processInvalidUniqueKey(const GrUniqueKey& key, GrTextureProxy* proxy,
750 InvalidateGPUResource invalidateGPUResource) {
Chris Dalton2de13dd2019-01-03 15:11:59 -0700751 SkASSERT(key.isValid());
752
Robert Phillips427966a2018-12-20 17:20:43 -0500753 if (!proxy) {
754 proxy = fUniquelyKeyedProxies.find(key);
755 }
Chris Dalton2de13dd2019-01-03 15:11:59 -0700756 SkASSERT(!proxy || proxy->getUniqueKey() == key);
757
758 // Locate the corresponding GrGpuResource (if it needs to be invalidated) before clearing the
759 // proxy's unique key. We must do it in this order because 'key' may alias the proxy's key.
760 sk_sp<GrGpuResource> invalidGpuResource;
761 if (InvalidateGPUResource::kYes == invalidateGPUResource) {
762 if (proxy && proxy->isInstantiated()) {
763 invalidGpuResource = sk_ref_sp(proxy->peekSurface());
764 }
765 if (!invalidGpuResource && fResourceProvider) {
766 invalidGpuResource = fResourceProvider->findByUniqueKey<GrGpuResource>(key);
767 }
768 SkASSERT(!invalidGpuResource || invalidGpuResource->getUniqueKey() == key);
769 }
Robert Phillips427966a2018-12-20 17:20:43 -0500770
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500771 // Note: this method is called for the whole variety of GrGpuResources so often 'key'
772 // will not be in 'fUniquelyKeyedProxies'.
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500773 if (proxy) {
Robert Phillips427966a2018-12-20 17:20:43 -0500774 fUniquelyKeyedProxies.remove(key);
775 proxy->cacheAccess().clearUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500776 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500777
Chris Dalton2de13dd2019-01-03 15:11:59 -0700778 if (invalidGpuResource) {
779 invalidGpuResource->resourcePriv().removeUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500780 }
781}
782
Robert Phillips0790f8a2018-09-18 13:11:03 -0400783void GrProxyProvider::orphanAllUniqueKeys() {
784 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
785 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
786 GrTextureProxy& tmp = *iter;
787
788 tmp.fProxyProvider = nullptr;
789 }
790}
791
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500792void GrProxyProvider::removeAllUniqueKeys() {
793 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
794 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
795 GrTextureProxy& tmp = *iter;
796
Chris Dalton2de13dd2019-01-03 15:11:59 -0700797 this->processInvalidUniqueKey(tmp.getUniqueKey(), &tmp, InvalidateGPUResource::kNo);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500798 }
799 SkASSERT(!fUniquelyKeyedProxies.count());
800}