blob: 3259f28ff07fefc179b1c480386c4222cb88f549 [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
107void GrProxyProvider::removeUniqueKeyFromProxy(const GrUniqueKey& key, GrTextureProxy* proxy) {
108 ASSERT_SINGLE_OWNER
109 if (this->isAbandoned() || !proxy) {
110 return;
111 }
Robert Phillips0790f8a2018-09-18 13:11:03 -0400112
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500113 this->processInvalidProxyUniqueKey(key, proxy, true);
114}
115
116sk_sp<GrTextureProxy> GrProxyProvider::findProxyByUniqueKey(const GrUniqueKey& key,
117 GrSurfaceOrigin origin) {
118 ASSERT_SINGLE_OWNER
119
120 if (this->isAbandoned()) {
121 return nullptr;
122 }
123
124 sk_sp<GrTextureProxy> result = sk_ref_sp(fUniquelyKeyedProxies.find(key));
125 if (result) {
126 SkASSERT(result->origin() == origin);
127 }
128 return result;
129}
130
Robert Phillipsadbe1322018-01-17 13:35:46 -0500131sk_sp<GrTextureProxy> GrProxyProvider::createWrapped(sk_sp<GrTexture> tex, GrSurfaceOrigin origin) {
132#ifdef SK_DEBUG
133 if (tex->getUniqueKey().isValid()) {
134 SkASSERT(!this->findProxyByUniqueKey(tex->getUniqueKey(), origin));
135 }
136#endif
137
138 if (tex->asRenderTarget()) {
139 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin));
140 } else {
141 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin));
142 }
143}
144
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500145sk_sp<GrTextureProxy> GrProxyProvider::findOrCreateProxyByUniqueKey(const GrUniqueKey& key,
146 GrSurfaceOrigin origin) {
147 ASSERT_SINGLE_OWNER
148
149 if (this->isAbandoned()) {
150 return nullptr;
151 }
152
153 sk_sp<GrTextureProxy> result = this->findProxyByUniqueKey(key, origin);
154 if (result) {
155 return result;
156 }
157
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500158 if (!fResourceCache) {
159 return nullptr;
160 }
161
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500162 GrGpuResource* resource = fResourceCache->findAndRefUniqueResource(key);
163 if (!resource) {
164 return nullptr;
165 }
166
167 sk_sp<GrTexture> texture(static_cast<GrSurface*>(resource)->asTexture());
168 SkASSERT(texture);
169
Robert Phillipsadbe1322018-01-17 13:35:46 -0500170 result = this->createWrapped(std::move(texture), origin);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500171 SkASSERT(result->getUniqueKey() == key);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500172 // createWrapped should've added this for us
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500173 SkASSERT(fUniquelyKeyedProxies.find(key));
174 return result;
175}
176
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500177sk_sp<GrTextureProxy> GrProxyProvider::createTextureProxy(sk_sp<SkImage> srcImage,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400178 GrSurfaceDescFlags descFlags,
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500179 int sampleCnt,
Greg Danielfb3abcd2018-02-02 15:48:33 -0500180 SkBudgeted budgeted,
Chris Daltond004e0b2018-09-27 09:28:03 -0600181 SkBackingFit fit,
182 GrInternalSurfaceFlags surfaceFlags) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500183 ASSERT_SINGLE_OWNER
184 SkASSERT(srcImage);
185
186 if (this->isAbandoned()) {
187 return nullptr;
188 }
189
Brian Osmand29dcd12018-09-13 15:04:29 -0400190 SkImageInfo info = as_IB(srcImage)->onImageInfo();
191 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
Greg Danielf87651e2018-02-21 11:36:53 -0500192
Greg Daniel0a7aa142018-02-21 13:02:32 -0500193 if (kUnknown_GrPixelConfig == config) {
194 return nullptr;
195 }
196
Greg Daniel4065d452018-11-16 15:43:41 -0500197 GrBackendFormat format = fCaps->getBackendFormatFromColorType(info.colorType());
198 if (!format.isValid()) {
199 return nullptr;
200 }
201
Brian Osmand29dcd12018-09-13 15:04:29 -0400202 if (!this->caps()->isConfigTexturable(config)) {
203 SkBitmap copy8888;
204 if (!copy8888.tryAllocPixels(info.makeColorType(kRGBA_8888_SkColorType)) ||
205 !srcImage->readPixels(copy8888.pixmap(), 0, 0)) {
206 return nullptr;
207 }
208 copy8888.setImmutable();
209 srcImage = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode);
210 config = kRGBA_8888_GrPixelConfig;
211 }
212
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400213 if (SkToBool(descFlags & kRenderTarget_GrSurfaceFlag)) {
Greg Danielf87651e2018-02-21 11:36:53 -0500214 sampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, config);
215 if (!sampleCnt) {
216 return nullptr;
217 }
218 }
219
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400220 if (SkToBool(descFlags & kRenderTarget_GrSurfaceFlag)) {
Greg Daniel2a303902018-02-20 10:25:54 -0500221 if (fCaps->usesMixedSamples() && sampleCnt > 1) {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400222 surfaceFlags |= GrInternalSurfaceFlags::kMixedSampled;
Greg Daniel2a303902018-02-20 10:25:54 -0500223 }
224 if (fCaps->maxWindowRectangles() > 0) {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400225 surfaceFlags |= GrInternalSurfaceFlags::kWindowRectsSupport;
Greg Daniel2a303902018-02-20 10:25:54 -0500226 }
227 }
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) {
288 if (!SkImageInfoIsValid(bitmap.info())) {
Greg Daniela4ead652018-02-07 10:21:48 -0500289 return nullptr;
290 }
291
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400292 ATRACE_ANDROID_FRAMEWORK("Upload MipMap Texture [%ux%u]", bitmap.width(), bitmap.height());
Greg Daniela4ead652018-02-07 10:21:48 -0500293
294 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
295 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
296 // 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 -0400297 SkCopyPixelsMode copyMode = this->recordingDDL() ? kIfMutable_SkCopyPixelsMode
298 : kNever_SkCopyPixelsMode;
Greg Daniela4ead652018-02-07 10:21:48 -0500299 sk_sp<SkImage> baseLevel = SkMakeImageFromRasterBitmap(bitmap, copyMode);
Greg Daniela4ead652018-02-07 10:21:48 -0500300 if (!baseLevel) {
301 return nullptr;
302 }
303
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400304 // This was never going to have mips anyway
305 if (0 == SkMipMap::ComputeLevelCount(baseLevel->width(), baseLevel->height())) {
Brian Salomon58389b92018-03-07 13:01:25 -0500306 return this->createTextureProxy(baseLevel, kNone_GrSurfaceFlags, 1, SkBudgeted::kYes,
307 SkBackingFit::kExact);
Greg Daniela4ead652018-02-07 10:21:48 -0500308 }
309
Greg Daniel4065d452018-11-16 15:43:41 -0500310 const GrBackendFormat format = fCaps->getBackendFormatFromColorType(bitmap.info().colorType());
311 if (!format.isValid()) {
312 return nullptr;
313 }
314
Brian Osmand29dcd12018-09-13 15:04:29 -0400315 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info());
316 if (!this->caps()->isConfigTexturable(desc.fConfig)) {
317 SkBitmap copy8888;
318 if (!copy8888.tryAllocPixels(bitmap.info().makeColorType(kRGBA_8888_SkColorType)) ||
319 !bitmap.readPixels(copy8888.pixmap())) {
320 return nullptr;
321 }
322 copy8888.setImmutable();
323 baseLevel = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode);
324 desc.fConfig = kRGBA_8888_GrPixelConfig;
325 }
326
327 SkPixmap pixmap;
328 SkAssertResult(baseLevel->peekPixels(&pixmap));
329 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(pixmap, nullptr));
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400330 if (!mipmaps) {
331 return nullptr;
332 }
333
Greg Daniela4ead652018-02-07 10:21:48 -0500334 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Osman1b97f132018-09-13 17:33:48 +0000335 [desc, baseLevel, mipmaps](GrResourceProvider* resourceProvider) {
Greg Daniela4ead652018-02-07 10:21:48 -0500336 if (!resourceProvider) {
337 return sk_sp<GrTexture>();
338 }
339
Brian Osman1b97f132018-09-13 17:33:48 +0000340 const int mipLevelCount = mipmaps->countLevels() + 1;
341 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
342
Greg Daniela4ead652018-02-07 10:21:48 -0500343 SkPixmap pixmap;
344 SkAssertResult(baseLevel->peekPixels(&pixmap));
345
346 // DDL TODO: Instead of copying all this info into GrMipLevels we should just plumb
347 // the use of SkMipMap down through Ganesh.
348 texels[0].fPixels = pixmap.addr();
349 texels[0].fRowBytes = pixmap.rowBytes();
350
351 for (int i = 1; i < mipLevelCount; ++i) {
352 SkMipMap::Level generatedMipLevel;
353 mipmaps->getLevel(i - 1, &generatedMipLevel);
354 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
355 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
356 SkASSERT(texels[i].fPixels);
357 }
358
Brian Salomon58389b92018-03-07 13:01:25 -0500359 return resourceProvider->createTexture(desc, SkBudgeted::kYes, texels.get(),
Brian Osman2b23c4b2018-06-01 12:25:08 -0400360 mipLevelCount);
Brian Salomon2a4f9832018-03-03 22:43:43 -0500361 },
Greg Daniel4065d452018-11-16 15:43:41 -0500362 format, desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kYes, SkBackingFit::kExact,
363 SkBudgeted::kYes);
Greg Daniela4ead652018-02-07 10:21:48 -0500364
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400365 if (!proxy) {
366 return nullptr;
367 }
368
Greg Daniela4ead652018-02-07 10:21:48 -0500369 if (fResourceProvider) {
370 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
371 // we're better off instantiating the proxy immediately here.
372 if (!proxy->priv().doLazyInstantiation(fResourceProvider)) {
373 return nullptr;
374 }
375 }
376 return proxy;
377}
378
Greg Daniel4065d452018-11-16 15:43:41 -0500379sk_sp<GrTextureProxy> GrProxyProvider::createProxy(const GrBackendFormat& format,
380 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500381 GrSurfaceOrigin origin,
Greg Danielf6f7b672018-02-15 13:06:26 -0500382 GrMipMapped mipMapped,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500383 SkBackingFit fit,
384 SkBudgeted budgeted,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400385 GrInternalSurfaceFlags surfaceFlags) {
Greg Danielf6f7b672018-02-15 13:06:26 -0500386 if (GrMipMapped::kYes == mipMapped) {
387 // SkMipMap doesn't include the base level in the level count so we have to add 1
388 int mipCount = SkMipMap::ComputeLevelCount(desc.fWidth, desc.fHeight) + 1;
389 if (1 == mipCount) {
390 mipMapped = GrMipMapped::kNo;
391 }
392 }
393
394 if (!this->caps()->validateSurfaceDesc(desc, mipMapped)) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500395 return nullptr;
396 }
Brian Salomon3a2cc2c2018-02-03 00:25:12 +0000397 GrSurfaceDesc copyDesc = desc;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500398 if (desc.fFlags & kRenderTarget_GrSurfaceFlag) {
399 copyDesc.fSampleCnt =
400 this->caps()->getRenderTargetSampleCount(desc.fSampleCnt, desc.fConfig);
401 }
Brian Salomon3a2cc2c2018-02-03 00:25:12 +0000402
Brian Salomonbdecacf2018-02-02 20:32:49 -0500403 if (copyDesc.fFlags & kRenderTarget_GrSurfaceFlag) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500404 // We know anything we instantiate later from this deferred path will be
405 // both texturable and renderable
Greg Daniel4065d452018-11-16 15:43:41 -0500406 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(*this->caps(), format, copyDesc,
407 origin, mipMapped,
Brian Salomon7226c232018-07-30 13:13:17 -0400408 fit, budgeted, surfaceFlags));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500409 }
410
Greg Daniel4065d452018-11-16 15:43:41 -0500411 return sk_sp<GrTextureProxy>(new GrTextureProxy(format, copyDesc, origin, mipMapped,
Brian Salomon7226c232018-07-30 13:13:17 -0400412 fit, budgeted, surfaceFlags));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500413}
414
Brian Salomon7578f3e2018-03-07 14:39:54 -0500415sk_sp<GrTextureProxy> GrProxyProvider::wrapBackendTexture(const GrBackendTexture& backendTex,
416 GrSurfaceOrigin origin,
417 GrWrapOwnership ownership,
418 ReleaseProc releaseProc,
419 ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500420 if (this->isAbandoned()) {
421 return nullptr;
422 }
423
Brian Salomonf7778972018-03-08 10:13:17 -0500424 // This is only supported on a direct GrContext.
425 if (!fResourceProvider) {
426 return nullptr;
427 }
428
Brian Salomonff4ccaa2018-12-05 21:35:33 +0000429 sk_sp<GrTexture> tex = fResourceProvider->wrapBackendTexture(backendTex, ownership);
Brian Salomonf7778972018-03-08 10:13:17 -0500430 if (!tex) {
431 return nullptr;
432 }
Robert Phillipsadbe1322018-01-17 13:35:46 -0500433
Greg Daniel6a0176b2018-01-30 09:28:44 -0500434 sk_sp<GrReleaseProcHelper> releaseHelper;
435 if (releaseProc) {
436 releaseHelper.reset(new GrReleaseProcHelper(releaseProc, releaseCtx));
Brian Salomonf7778972018-03-08 10:13:17 -0500437 // This gives the texture a ref on the releaseHelper
438 tex->setRelease(releaseHelper);
Greg Daniel6a0176b2018-01-30 09:28:44 -0500439 }
440
Brian Salomonf7778972018-03-08 10:13:17 -0500441 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
442 // Make sure we match how we created the proxy with SkBudgeted::kNo
443 SkASSERT(SkBudgeted::kNo == tex->resourcePriv().isBudgeted());
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500444
Brian Salomonf7778972018-03-08 10:13:17 -0500445 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500446}
447
Brian Salomon7578f3e2018-03-07 14:39:54 -0500448sk_sp<GrTextureProxy> GrProxyProvider::wrapRenderableBackendTexture(
Brian Salomon02bd2952018-03-07 15:20:21 -0500449 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt,
450 GrWrapOwnership ownership) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500451 if (this->isAbandoned()) {
452 return nullptr;
453 }
454
Brian Salomonf7778972018-03-08 10:13:17 -0500455 // This is only supported on a direct GrContext.
456 if (!fResourceProvider) {
457 return nullptr;
458 }
459
Greg Daniel6abda432018-02-15 14:55:00 -0500460 sampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config());
Greg Danielf87651e2018-02-21 11:36:53 -0500461 if (!sampleCnt) {
462 return nullptr;
463 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500464
Brian Salomonf7778972018-03-08 10:13:17 -0500465 sk_sp<GrTexture> tex =
466 fResourceProvider->wrapRenderableBackendTexture(backendTex, sampleCnt, ownership);
467 if (!tex) {
468 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500469 }
470
Brian Salomonf7778972018-03-08 10:13:17 -0500471 SkASSERT(tex->asRenderTarget()); // A GrTextureRenderTarget
472 // Make sure we match how we created the proxy with SkBudgeted::kNo
473 SkASSERT(SkBudgeted::kNo == tex->resourcePriv().isBudgeted());
Greg Daniel6abda432018-02-15 14:55:00 -0500474
Brian Salomonf7778972018-03-08 10:13:17 -0500475 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500476}
477
Brian Salomon7578f3e2018-03-07 14:39:54 -0500478sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget(
479 const GrBackendRenderTarget& backendRT, GrSurfaceOrigin origin) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500480 if (this->isAbandoned()) {
481 return nullptr;
482 }
483
Brian Salomonf7778972018-03-08 10:13:17 -0500484 // This is only supported on a direct GrContext.
485 if (!fResourceProvider) {
486 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500487 }
488
Brian Salomonf7778972018-03-08 10:13:17 -0500489 sk_sp<GrRenderTarget> rt = fResourceProvider->wrapBackendRenderTarget(backendRT);
490 if (!rt) {
491 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500492 }
Brian Salomonf7778972018-03-08 10:13:17 -0500493 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
494 SkASSERT(!rt->getUniqueKey().isValid());
495 // Make sure we match how we created the proxy with SkBudgeted::kNo
496 SkASSERT(SkBudgeted::kNo == rt->resourcePriv().isBudgeted());
497
498 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(std::move(rt), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500499}
500
Brian Salomon7578f3e2018-03-07 14:39:54 -0500501sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendTextureAsRenderTarget(
502 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500503 if (this->isAbandoned()) {
504 return nullptr;
505 }
506
Brian Salomonf7778972018-03-08 10:13:17 -0500507 // This is only supported on a direct GrContext.
508 if (!fResourceProvider) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500509 return nullptr;
510 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500511
Brian Salomonf7778972018-03-08 10:13:17 -0500512 sk_sp<GrRenderTarget> rt =
513 fResourceProvider->wrapBackendTextureAsRenderTarget(backendTex, sampleCnt);
514 if (!rt) {
515 return nullptr;
Greg Danielf87651e2018-02-21 11:36:53 -0500516 }
Brian Salomonf7778972018-03-08 10:13:17 -0500517 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
518 SkASSERT(!rt->getUniqueKey().isValid());
519 // Make sure we match how we created the proxy with SkBudgeted::kNo
520 SkASSERT(SkBudgeted::kNo == rt->resourcePriv().isBudgeted());
Greg Danielf87651e2018-02-21 11:36:53 -0500521
Brian Salomonf7778972018-03-08 10:13:17 -0500522 return sk_sp<GrSurfaceProxy>(new GrRenderTargetProxy(std::move(rt), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500523}
524
Robert Phillips777707b2018-01-17 11:40:14 -0500525sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500526 const GrBackendFormat& format,
Robert Phillips777707b2018-01-17 11:40:14 -0500527 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500528 GrSurfaceOrigin origin,
Brian Salomon7226c232018-07-30 13:13:17 -0400529 GrMipMapped mipMapped,
Brian Salomon7226c232018-07-30 13:13:17 -0400530 SkBackingFit fit,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500531 SkBudgeted budgeted) {
Greg Daniel4065d452018-11-16 15:43:41 -0500532 return this->createLazyProxy(std::move(callback), format, desc, origin, mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400533 GrInternalSurfaceFlags::kNone, fit, budgeted);
Greg Daniel2a303902018-02-20 10:25:54 -0500534}
535
536sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500537 const GrBackendFormat& format,
Greg Daniel2a303902018-02-20 10:25:54 -0500538 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500539 GrSurfaceOrigin origin,
Greg Daniel2a303902018-02-20 10:25:54 -0500540 GrMipMapped mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400541 GrInternalSurfaceFlags surfaceFlags,
Brian Salomon7226c232018-07-30 13:13:17 -0400542 SkBackingFit fit,
543 SkBudgeted budgeted) {
Greg Daniela8d92112018-03-09 12:05:04 -0500544 // For non-ddl draws always make lazy proxy's single use.
545 LazyInstantiationType lazyType = fResourceProvider ? LazyInstantiationType::kSingleUse
546 : LazyInstantiationType::kMultipleUse;
Greg Daniel4065d452018-11-16 15:43:41 -0500547 return this->createLazyProxy(std::move(callback), format, desc, origin, mipMapped, surfaceFlags,
548 fit, budgeted, lazyType);
Greg Daniela8d92112018-03-09 12:05:04 -0500549}
550
551sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500552 const GrBackendFormat& format,
Greg Daniela8d92112018-03-09 12:05:04 -0500553 const GrSurfaceDesc& desc,
554 GrSurfaceOrigin origin,
555 GrMipMapped mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400556 GrInternalSurfaceFlags surfaceFlags,
Brian Salomon7226c232018-07-30 13:13:17 -0400557 SkBackingFit fit,
558 SkBudgeted budgeted,
Greg Daniela8d92112018-03-09 12:05:04 -0500559 LazyInstantiationType lazyType) {
Robert Phillips777707b2018-01-17 11:40:14 -0500560 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
561 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400562
563 if (desc.fWidth > fCaps->maxTextureSize() || desc.fHeight > fCaps->maxTextureSize()) {
564 return nullptr;
565 }
566
Greg Daniel457469c2018-02-08 15:05:44 -0500567
Greg Daniel2a303902018-02-20 10:25:54 -0500568#ifdef SK_DEBUG
569 if (SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags)) {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400570 if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kMixedSampled)) {
Greg Daniel2a303902018-02-20 10:25:54 -0500571 SkASSERT(fCaps->usesMixedSamples() && desc.fSampleCnt > 1);
572 }
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400573 if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kWindowRectsSupport)) {
Greg Daniel2a303902018-02-20 10:25:54 -0500574 SkASSERT(fCaps->maxWindowRectangles() > 0);
575 }
576 }
577#endif
578
Brian Salomon2a4f9832018-03-03 22:43:43 -0500579 return sk_sp<GrTextureProxy>(
580 SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags)
Greg Daniel4065d452018-11-16 15:43:41 -0500581 ? new GrTextureRenderTargetProxy(std::move(callback), lazyType, format, desc,
582 origin, mipMapped, fit, budgeted, surfaceFlags)
583 : new GrTextureProxy(std::move(callback), lazyType, format, desc, origin,
584 mipMapped, fit, budgeted, surfaceFlags));
Robert Phillips777707b2018-01-17 11:40:14 -0500585}
586
Robert Phillipse8fabb22018-02-04 14:33:21 -0500587sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500588 LazyInstantiateCallback&& callback, const GrBackendFormat& format,
589 const GrSurfaceDesc& desc, GrSurfaceOrigin origin, GrInternalSurfaceFlags surfaceFlags,
590 const TextureInfo* textureInfo, SkBackingFit fit, SkBudgeted budgeted) {
Robert Phillipse8fabb22018-02-04 14:33:21 -0500591 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
592 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400593
594 if (desc.fWidth > fCaps->maxRenderTargetSize() || desc.fHeight > fCaps->maxRenderTargetSize()) {
595 return nullptr;
596 }
597
Greg Daniel2a303902018-02-20 10:25:54 -0500598 SkASSERT(SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags));
Greg Daniel457469c2018-02-08 15:05:44 -0500599
Greg Daniel2a303902018-02-20 10:25:54 -0500600#ifdef SK_DEBUG
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400601 if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kMixedSampled)) {
Greg Daniel2a303902018-02-20 10:25:54 -0500602 SkASSERT(fCaps->usesMixedSamples() && desc.fSampleCnt > 1);
603 }
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400604 if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kWindowRectsSupport)) {
Greg Daniel2a303902018-02-20 10:25:54 -0500605 SkASSERT(fCaps->maxWindowRectangles() > 0);
606 }
607#endif
608
Greg Daniel457469c2018-02-08 15:05:44 -0500609 using LazyInstantiationType = GrSurfaceProxy::LazyInstantiationType;
610 // For non-ddl draws always make lazy proxy's single use.
611 LazyInstantiationType lazyType = fResourceProvider ? LazyInstantiationType::kSingleUse
612 : LazyInstantiationType::kMultipleUse;
613
Brian Salomon7226c232018-07-30 13:13:17 -0400614 if (textureInfo) {
615 return sk_sp<GrRenderTargetProxy>(new GrTextureRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500616 std::move(callback), lazyType, format, desc, origin, textureInfo->fMipMapped,
617 fit, budgeted, surfaceFlags));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500618 }
619
Brian Salomon2a4f9832018-03-03 22:43:43 -0500620 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500621 std::move(callback), lazyType, format, desc, origin, fit, budgeted, surfaceFlags));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500622}
623
Chris Dalton4c458b12018-06-16 17:22:59 -0600624sk_sp<GrTextureProxy> GrProxyProvider::MakeFullyLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500625 const GrBackendFormat& format,
Chris Dalton4c458b12018-06-16 17:22:59 -0600626 Renderable renderable,
627 GrSurfaceOrigin origin,
628 GrPixelConfig config,
629 const GrCaps& caps) {
Robert Phillips777707b2018-01-17 11:40:14 -0500630 GrSurfaceDesc desc;
Greg Daniele3204862018-04-16 11:24:10 -0400631 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNoPendingIO;
Robert Phillips777707b2018-01-17 11:40:14 -0500632 if (Renderable::kYes == renderable) {
633 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Chris Dalton4c458b12018-06-16 17:22:59 -0600634 if (caps.maxWindowRectangles() > 0) {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400635 surfaceFlags |= GrInternalSurfaceFlags::kWindowRectsSupport;
Greg Daniel2a303902018-02-20 10:25:54 -0500636 }
Robert Phillips777707b2018-01-17 11:40:14 -0500637 }
Robert Phillips777707b2018-01-17 11:40:14 -0500638 desc.fWidth = -1;
639 desc.fHeight = -1;
640 desc.fConfig = config;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500641 desc.fSampleCnt = 1;
Robert Phillips777707b2018-01-17 11:40:14 -0500642
Chris Dalton4c458b12018-06-16 17:22:59 -0600643 return sk_sp<GrTextureProxy>(
644 (Renderable::kYes == renderable)
Brian Salomon7226c232018-07-30 13:13:17 -0400645 ? new GrTextureRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500646 std::move(callback), LazyInstantiationType::kSingleUse, format, desc,
647 origin, GrMipMapped::kNo, SkBackingFit::kApprox, SkBudgeted::kYes,
648 surfaceFlags)
Chris Dalton4c458b12018-06-16 17:22:59 -0600649 : new GrTextureProxy(std::move(callback), LazyInstantiationType::kSingleUse,
Greg Daniel4065d452018-11-16 15:43:41 -0500650 format, desc, origin, GrMipMapped::kNo,
Brian Salomon7226c232018-07-30 13:13:17 -0400651 SkBackingFit::kApprox, SkBudgeted::kYes, surfaceFlags));
Robert Phillips777707b2018-01-17 11:40:14 -0500652}
653
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500654bool GrProxyProvider::IsFunctionallyExact(GrSurfaceProxy* proxy) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400655 const bool isInstantiated = proxy->isInstantiated();
Robert Phillipsdb3b9792018-06-12 15:18:00 -0400656 // A proxy is functionally exact if:
657 // it is exact (obvs)
658 // when it is instantiated it will be exact (i.e., power of two dimensions)
659 // it is already instantiated and the proxy covers the entire backing surface
660 return proxy->priv().isExact() ||
661 (!isInstantiated && SkIsPow2(proxy->width()) && SkIsPow2(proxy->height())) ||
662 (isInstantiated && proxy->worstCaseWidth() == proxy->width() &&
663 proxy->worstCaseHeight() == proxy->height());
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500664}
665
666void GrProxyProvider::processInvalidProxyUniqueKey(const GrUniqueKey& key) {
667 // Note: this method is called for the whole variety of GrGpuResources so often 'key'
668 // will not be in 'fUniquelyKeyedProxies'.
669 GrTextureProxy* proxy = fUniquelyKeyedProxies.find(key);
670 if (proxy) {
671 this->processInvalidProxyUniqueKey(key, proxy, false);
672 }
673}
674
675void GrProxyProvider::processInvalidProxyUniqueKey(const GrUniqueKey& key, GrTextureProxy* proxy,
676 bool invalidateSurface) {
677 SkASSERT(proxy);
678 SkASSERT(proxy->getUniqueKey().isValid());
679 SkASSERT(proxy->getUniqueKey() == key);
680
681 fUniquelyKeyedProxies.remove(key);
682 proxy->cacheAccess().clearUniqueKey();
683
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400684 if (invalidateSurface && proxy->isInstantiated()) {
685 GrSurface* surface = proxy->peekSurface();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500686 if (surface) {
687 surface->resourcePriv().removeUniqueKey();
688 }
689 }
690}
691
Robert Phillips0790f8a2018-09-18 13:11:03 -0400692void GrProxyProvider::orphanAllUniqueKeys() {
693 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
694 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
695 GrTextureProxy& tmp = *iter;
696
697 tmp.fProxyProvider = nullptr;
698 }
699}
700
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500701void GrProxyProvider::removeAllUniqueKeys() {
702 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
703 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
704 GrTextureProxy& tmp = *iter;
705
706 this->processInvalidProxyUniqueKey(tmp.getUniqueKey(), &tmp, false);
707 }
708 SkASSERT(!fUniquelyKeyedProxies.count());
709}