blob: a4b8d61cf3231a3a1e408ec576b65406a3fa4f79 [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"
Greg Daniela4ead652018-02-07 10:21:48 -050020#include "SkBitmap.h"
Greg Daniel9d86f1d2018-01-29 09:33:59 -050021#include "SkGr.h"
22#include "SkImage.h"
23#include "SkImage_Base.h"
Greg Daniela4ead652018-02-07 10:21:48 -050024#include "SkImageInfoPriv.h"
25#include "SkImagePriv.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050026#include "SkMipMap.h"
Greg Daniela4ead652018-02-07 10:21:48 -050027#include "SkTraceEvent.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050028
29#define ASSERT_SINGLE_OWNER \
30 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fSingleOwner);)
31
32GrProxyProvider::GrProxyProvider(GrResourceProvider* resourceProvider,
33 GrResourceCache* resourceCache,
34 sk_sp<const GrCaps> caps,
35 GrSingleOwner* owner)
36 : fResourceProvider(resourceProvider)
37 , fResourceCache(resourceCache)
Robert Phillips4d120512018-01-19 13:22:07 -050038 , fAbandoned(false)
Robert Phillips1afd4cd2018-01-08 13:40:32 -050039 , fCaps(caps)
40#ifdef SK_DEBUG
41 , fSingleOwner(owner)
42#endif
43{
44
45}
46
47GrProxyProvider::~GrProxyProvider() {
48 SkASSERT(!fUniquelyKeyedProxies.count());
49}
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
58 // If there is already a GrResource with this key then the caller has violated the normal
59 // usage pattern of uniquely keyed resources (e.g., they have created one w/o first seeing
60 // if it already existed in the cache).
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -050061 SkASSERT(!fResourceCache || !fResourceCache->findAndRefUniqueResource(key));
Robert Phillips1afd4cd2018-01-08 13:40:32 -050062
63 // Uncached resources can never have a unique key, unless they're wrapped resources. Wrapped
64 // resources are a special case: the unique keys give us a weak ref so that we can reuse the
65 // same resource (rather than re-wrapping). When a wrapped resource is no longer referenced,
66 // it will always be released - it is never converted to a scratch resource.
67 if (SkBudgeted::kNo == proxy->isBudgeted() &&
68 (!proxy->priv().isInstantiated() ||
69 !proxy->priv().peekSurface()->resourcePriv().refsWrappedObjects())) {
Robert Phillipsadbe1322018-01-17 13:35:46 -050070 return false;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050071 }
72
73 SkASSERT(!fUniquelyKeyedProxies.find(key)); // multiple proxies can't get the same key
74
75 proxy->cacheAccess().setUniqueKey(this, key);
76 SkASSERT(proxy->getUniqueKey() == key);
77 fUniquelyKeyedProxies.add(proxy);
Robert Phillipsadbe1322018-01-17 13:35:46 -050078 return true;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050079}
80
81void GrProxyProvider::adoptUniqueKeyFromSurface(GrTextureProxy* proxy, const GrSurface* surf) {
82 SkASSERT(surf->getUniqueKey().isValid());
83 proxy->cacheAccess().setUniqueKey(this, surf->getUniqueKey());
84 SkASSERT(proxy->getUniqueKey() == surf->getUniqueKey());
85 // multiple proxies can't get the same key
86 SkASSERT(!fUniquelyKeyedProxies.find(surf->getUniqueKey()));
87 fUniquelyKeyedProxies.add(proxy);
88}
89
90void GrProxyProvider::removeUniqueKeyFromProxy(const GrUniqueKey& key, GrTextureProxy* proxy) {
91 ASSERT_SINGLE_OWNER
92 if (this->isAbandoned() || !proxy) {
93 return;
94 }
95 this->processInvalidProxyUniqueKey(key, proxy, true);
96}
97
98sk_sp<GrTextureProxy> GrProxyProvider::findProxyByUniqueKey(const GrUniqueKey& key,
99 GrSurfaceOrigin origin) {
100 ASSERT_SINGLE_OWNER
101
102 if (this->isAbandoned()) {
103 return nullptr;
104 }
105
106 sk_sp<GrTextureProxy> result = sk_ref_sp(fUniquelyKeyedProxies.find(key));
107 if (result) {
108 SkASSERT(result->origin() == origin);
109 }
110 return result;
111}
112
Robert Phillipsadbe1322018-01-17 13:35:46 -0500113sk_sp<GrTextureProxy> GrProxyProvider::createWrapped(sk_sp<GrTexture> tex, GrSurfaceOrigin origin) {
114#ifdef SK_DEBUG
115 if (tex->getUniqueKey().isValid()) {
116 SkASSERT(!this->findProxyByUniqueKey(tex->getUniqueKey(), origin));
117 }
118#endif
119
120 if (tex->asRenderTarget()) {
121 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin));
122 } else {
123 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin));
124 }
125}
126
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500127sk_sp<GrTextureProxy> GrProxyProvider::findOrCreateProxyByUniqueKey(const GrUniqueKey& key,
128 GrSurfaceOrigin origin) {
129 ASSERT_SINGLE_OWNER
130
131 if (this->isAbandoned()) {
132 return nullptr;
133 }
134
135 sk_sp<GrTextureProxy> result = this->findProxyByUniqueKey(key, origin);
136 if (result) {
137 return result;
138 }
139
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500140 if (!fResourceCache) {
141 return nullptr;
142 }
143
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500144 GrGpuResource* resource = fResourceCache->findAndRefUniqueResource(key);
145 if (!resource) {
146 return nullptr;
147 }
148
149 sk_sp<GrTexture> texture(static_cast<GrSurface*>(resource)->asTexture());
150 SkASSERT(texture);
151
Robert Phillipsadbe1322018-01-17 13:35:46 -0500152 result = this->createWrapped(std::move(texture), origin);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500153 SkASSERT(result->getUniqueKey() == key);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500154 // createWrapped should've added this for us
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500155 SkASSERT(fUniquelyKeyedProxies.find(key));
156 return result;
157}
158
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500159sk_sp<GrTextureProxy> GrProxyProvider::createInstantiatedProxy(const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500160 GrSurfaceOrigin origin,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500161 SkBackingFit fit,
162 SkBudgeted budgeted,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400163 GrSurfaceDescFlags descFlags) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500164 sk_sp<GrTexture> tex;
165
166 if (SkBackingFit::kApprox == fit) {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400167 tex = fResourceProvider->createApproxTexture(desc, descFlags);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500168 } else {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400169 tex = fResourceProvider->createTexture(desc, budgeted, descFlags);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500170 }
171 if (!tex) {
172 return nullptr;
173 }
174
Brian Salomon2a4f9832018-03-03 22:43:43 -0500175 return this->createWrapped(std::move(tex), origin);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500176}
177
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500178sk_sp<GrTextureProxy> GrProxyProvider::createTextureProxy(const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500179 SkBudgeted budgeted, const void* srcData,
180 size_t rowBytes) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500181 ASSERT_SINGLE_OWNER
182
Robert Phillips579f0942018-01-08 14:53:35 -0500183 if (this->isAbandoned()) {
184 return nullptr;
185 }
186
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500187 if (srcData) {
188 GrMipLevel mipLevel = { srcData, rowBytes };
189
Brian Salomon58389b92018-03-07 13:01:25 -0500190 sk_sp<GrTexture> tex =
191 fResourceProvider->createTexture(desc, budgeted, SkBackingFit::kExact, mipLevel);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500192 if (!tex) {
193 return nullptr;
194 }
195
Brian Salomon58389b92018-03-07 13:01:25 -0500196 return this->createWrapped(std::move(tex), kTopLeft_GrSurfaceOrigin);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500197 }
198
Brian Salomon58389b92018-03-07 13:01:25 -0500199 return this->createProxy(desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kExact, budgeted);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500200}
201
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500202sk_sp<GrTextureProxy> GrProxyProvider::createTextureProxy(sk_sp<SkImage> srcImage,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400203 GrSurfaceDescFlags descFlags,
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500204 int sampleCnt,
Greg Danielfb3abcd2018-02-02 15:48:33 -0500205 SkBudgeted budgeted,
206 SkBackingFit fit) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500207 ASSERT_SINGLE_OWNER
208 SkASSERT(srcImage);
209
210 if (this->isAbandoned()) {
211 return nullptr;
212 }
213
Greg Danielf87651e2018-02-21 11:36:53 -0500214 GrPixelConfig config = SkImageInfo2GrPixelConfig(as_IB(srcImage)->onImageInfo(),
215 *this->caps());
216
Greg Daniel0a7aa142018-02-21 13:02:32 -0500217 if (kUnknown_GrPixelConfig == config) {
218 return nullptr;
219 }
220
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400221 if (SkToBool(descFlags & kRenderTarget_GrSurfaceFlag)) {
Greg Danielf87651e2018-02-21 11:36:53 -0500222 sampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, config);
223 if (!sampleCnt) {
224 return nullptr;
225 }
226 }
227
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400228 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNone;
229 if (SkToBool(descFlags & kRenderTarget_GrSurfaceFlag)) {
Greg Daniel2a303902018-02-20 10:25:54 -0500230 if (fCaps->usesMixedSamples() && sampleCnt > 1) {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400231 surfaceFlags |= GrInternalSurfaceFlags::kMixedSampled;
Greg Daniel2a303902018-02-20 10:25:54 -0500232 }
233 if (fCaps->maxWindowRectangles() > 0) {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400234 surfaceFlags |= GrInternalSurfaceFlags::kWindowRectsSupport;
Greg Daniel2a303902018-02-20 10:25:54 -0500235 }
236 }
237
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500238 GrSurfaceDesc desc;
239 desc.fWidth = srcImage->width();
240 desc.fHeight = srcImage->height();
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400241 desc.fFlags = descFlags;
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500242 desc.fSampleCnt = sampleCnt;
Greg Danielf87651e2018-02-21 11:36:53 -0500243 desc.fConfig = config;
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500244
245 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomon58389b92018-03-07 13:01:25 -0500246 [desc, budgeted, srcImage, fit](GrResourceProvider* resourceProvider) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500247 if (!resourceProvider) {
Greg Daniel0a375db2018-02-01 12:21:39 -0500248 // Nothing to clean up here. Once the proxy (and thus lambda) is deleted the ref
249 // on srcImage will be released.
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500250 return sk_sp<GrTexture>();
251 }
252 SkPixmap pixMap;
253 SkAssertResult(srcImage->peekPixels(&pixMap));
254 GrMipLevel mipLevel = { pixMap.addr(), pixMap.rowBytes() };
255
Brian Salomon58389b92018-03-07 13:01:25 -0500256 return resourceProvider->createTexture(desc, budgeted, fit, mipLevel);
Brian Salomon2a4f9832018-03-03 22:43:43 -0500257 },
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400258 desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, surfaceFlags, fit, budgeted);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500259
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400260 if (!proxy) {
261 return nullptr;
262 }
263
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500264 if (fResourceProvider) {
265 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
266 // we're better off instantiating the proxy immediately here.
267 if (!proxy->priv().doLazyInstantiation(fResourceProvider)) {
268 return nullptr;
269 }
270 }
271 return proxy;
272}
273
Greg Daniel30815082018-02-09 16:08:30 -0500274sk_sp<GrTextureProxy> GrProxyProvider::createMipMapProxy(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
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400283 return this->createProxy(desc, origin, GrMipMapped::kYes, SkBackingFit::kExact, budgeted,
284 GrInternalSurfaceFlags::kNone);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500285}
286
Greg Daniela4ead652018-02-07 10:21:48 -0500287sk_sp<GrTextureProxy> GrProxyProvider::createMipMapProxyFromBitmap(const SkBitmap& bitmap,
288 SkColorSpace* dstColorSpace) {
289 SkDestinationSurfaceColorMode mipColorMode = dstColorSpace
290 ? SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware
291 : SkDestinationSurfaceColorMode::kLegacy;
292
293 if (!SkImageInfoIsValid(bitmap.info(), mipColorMode)) {
294 return nullptr;
295 }
296
297 SkPixmap pixmap;
298 if (!bitmap.peekPixels(&pixmap)) {
299 return nullptr;
300 }
301
302 ATRACE_ANDROID_FRAMEWORK("Upload MipMap Texture [%ux%u]", pixmap.width(), pixmap.height());
303 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(pixmap, mipColorMode, nullptr));
304 if (!mipmaps) {
305 return nullptr;
306 }
307
308 if (mipmaps->countLevels() < 0) {
309 return nullptr;
310 }
311
312 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
313 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
314 // 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 -0400315 SkCopyPixelsMode copyMode = this->recordingDDL() ? kIfMutable_SkCopyPixelsMode
316 : kNever_SkCopyPixelsMode;
Greg Daniela4ead652018-02-07 10:21:48 -0500317 sk_sp<SkImage> baseLevel = SkMakeImageFromRasterBitmap(bitmap, copyMode);
318
319 if (!baseLevel) {
320 return nullptr;
321 }
322
323 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(pixmap.info(), *this->caps());
324
325 if (0 == mipmaps->countLevels()) {
Brian Salomon58389b92018-03-07 13:01:25 -0500326 return this->createTextureProxy(baseLevel, kNone_GrSurfaceFlags, 1, SkBudgeted::kYes,
327 SkBackingFit::kExact);
Greg Daniela4ead652018-02-07 10:21:48 -0500328 }
329
330 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomon2a4f9832018-03-03 22:43:43 -0500331 [desc, baseLevel, mipmaps, mipColorMode](GrResourceProvider* resourceProvider) {
Greg Daniela4ead652018-02-07 10:21:48 -0500332 if (!resourceProvider) {
333 return sk_sp<GrTexture>();
334 }
335
336 const int mipLevelCount = mipmaps->countLevels() + 1;
337 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
338
339 SkPixmap pixmap;
340 SkAssertResult(baseLevel->peekPixels(&pixmap));
341
342 // DDL TODO: Instead of copying all this info into GrMipLevels we should just plumb
343 // the use of SkMipMap down through Ganesh.
344 texels[0].fPixels = pixmap.addr();
345 texels[0].fRowBytes = pixmap.rowBytes();
346
347 for (int i = 1; i < mipLevelCount; ++i) {
348 SkMipMap::Level generatedMipLevel;
349 mipmaps->getLevel(i - 1, &generatedMipLevel);
350 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
351 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
352 SkASSERT(texels[i].fPixels);
353 }
354
Brian Salomon58389b92018-03-07 13:01:25 -0500355 return resourceProvider->createTexture(desc, SkBudgeted::kYes, texels.get(),
Greg Daniela4ead652018-02-07 10:21:48 -0500356 mipLevelCount, mipColorMode);
Brian Salomon2a4f9832018-03-03 22:43:43 -0500357 },
358 desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kYes, SkBackingFit::kExact,
359 SkBudgeted::kYes);
Greg Daniela4ead652018-02-07 10:21:48 -0500360
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400361 if (!proxy) {
362 return nullptr;
363 }
364
Greg Daniela4ead652018-02-07 10:21:48 -0500365 if (fResourceProvider) {
366 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
367 // we're better off instantiating the proxy immediately here.
368 if (!proxy->priv().doLazyInstantiation(fResourceProvider)) {
369 return nullptr;
370 }
371 }
372 return proxy;
373}
374
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500375sk_sp<GrTextureProxy> GrProxyProvider::createProxy(const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500376 GrSurfaceOrigin origin,
Greg Danielf6f7b672018-02-15 13:06:26 -0500377 GrMipMapped mipMapped,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500378 SkBackingFit fit,
379 SkBudgeted budgeted,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400380 GrInternalSurfaceFlags surfaceFlags) {
Greg Danielf6f7b672018-02-15 13:06:26 -0500381 if (GrMipMapped::kYes == mipMapped) {
382 // SkMipMap doesn't include the base level in the level count so we have to add 1
383 int mipCount = SkMipMap::ComputeLevelCount(desc.fWidth, desc.fHeight) + 1;
384 if (1 == mipCount) {
385 mipMapped = GrMipMapped::kNo;
386 }
387 }
388
389 if (!this->caps()->validateSurfaceDesc(desc, mipMapped)) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500390 return nullptr;
391 }
Brian Salomon3a2cc2c2018-02-03 00:25:12 +0000392 GrSurfaceDesc copyDesc = desc;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500393 if (desc.fFlags & kRenderTarget_GrSurfaceFlag) {
394 copyDesc.fSampleCnt =
395 this->caps()->getRenderTargetSampleCount(desc.fSampleCnt, desc.fConfig);
396 }
Brian Salomon3a2cc2c2018-02-03 00:25:12 +0000397
Brian Salomonbdecacf2018-02-02 20:32:49 -0500398 if (copyDesc.fFlags & kRenderTarget_GrSurfaceFlag) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500399 // We know anything we instantiate later from this deferred path will be
400 // both texturable and renderable
Brian Salomon2a4f9832018-03-03 22:43:43 -0500401 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400402 *this->caps(), copyDesc, origin, mipMapped, fit, budgeted, surfaceFlags));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500403 }
404
Brian Salomon2a4f9832018-03-03 22:43:43 -0500405 return sk_sp<GrTextureProxy>(
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400406 new GrTextureProxy(copyDesc, origin, mipMapped, fit, budgeted, surfaceFlags));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500407}
408
Brian Salomon7578f3e2018-03-07 14:39:54 -0500409sk_sp<GrTextureProxy> GrProxyProvider::wrapBackendTexture(const GrBackendTexture& backendTex,
410 GrSurfaceOrigin origin,
411 GrWrapOwnership ownership,
412 ReleaseProc releaseProc,
413 ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500414 if (this->isAbandoned()) {
415 return nullptr;
416 }
417
Brian Salomonf7778972018-03-08 10:13:17 -0500418 // This is only supported on a direct GrContext.
419 if (!fResourceProvider) {
420 return nullptr;
421 }
422
423 sk_sp<GrTexture> tex = fResourceProvider->wrapBackendTexture(backendTex, ownership);
424 if (!tex) {
425 return nullptr;
426 }
Robert Phillipsadbe1322018-01-17 13:35:46 -0500427
Greg Daniel6a0176b2018-01-30 09:28:44 -0500428 sk_sp<GrReleaseProcHelper> releaseHelper;
429 if (releaseProc) {
430 releaseHelper.reset(new GrReleaseProcHelper(releaseProc, releaseCtx));
Brian Salomonf7778972018-03-08 10:13:17 -0500431 // This gives the texture a ref on the releaseHelper
432 tex->setRelease(releaseHelper);
Greg Daniel6a0176b2018-01-30 09:28:44 -0500433 }
434
Brian Salomonf7778972018-03-08 10:13:17 -0500435 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
436 // Make sure we match how we created the proxy with SkBudgeted::kNo
437 SkASSERT(SkBudgeted::kNo == tex->resourcePriv().isBudgeted());
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500438
Brian Salomonf7778972018-03-08 10:13:17 -0500439 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500440}
441
Brian Salomon7578f3e2018-03-07 14:39:54 -0500442sk_sp<GrTextureProxy> GrProxyProvider::wrapRenderableBackendTexture(
Brian Salomon02bd2952018-03-07 15:20:21 -0500443 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt,
444 GrWrapOwnership ownership) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500445 if (this->isAbandoned()) {
446 return nullptr;
447 }
448
Brian Salomonf7778972018-03-08 10:13:17 -0500449 // This is only supported on a direct GrContext.
450 if (!fResourceProvider) {
451 return nullptr;
452 }
453
Greg Daniel6abda432018-02-15 14:55:00 -0500454 sampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config());
Greg Danielf87651e2018-02-21 11:36:53 -0500455 if (!sampleCnt) {
456 return nullptr;
457 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500458
Brian Salomonf7778972018-03-08 10:13:17 -0500459 sk_sp<GrTexture> tex =
460 fResourceProvider->wrapRenderableBackendTexture(backendTex, sampleCnt, ownership);
461 if (!tex) {
462 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500463 }
464
Brian Salomonf7778972018-03-08 10:13:17 -0500465 SkASSERT(tex->asRenderTarget()); // A GrTextureRenderTarget
466 // Make sure we match how we created the proxy with SkBudgeted::kNo
467 SkASSERT(SkBudgeted::kNo == tex->resourcePriv().isBudgeted());
Greg Daniel6abda432018-02-15 14:55:00 -0500468
Brian Salomonf7778972018-03-08 10:13:17 -0500469 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500470}
471
Brian Salomon7578f3e2018-03-07 14:39:54 -0500472sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget(
473 const GrBackendRenderTarget& backendRT, GrSurfaceOrigin origin) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500474 if (this->isAbandoned()) {
475 return nullptr;
476 }
477
Brian Salomonf7778972018-03-08 10:13:17 -0500478 // This is only supported on a direct GrContext.
479 if (!fResourceProvider) {
480 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500481 }
482
Brian Salomonf7778972018-03-08 10:13:17 -0500483 sk_sp<GrRenderTarget> rt = fResourceProvider->wrapBackendRenderTarget(backendRT);
484 if (!rt) {
485 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500486 }
Brian Salomonf7778972018-03-08 10:13:17 -0500487 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
488 SkASSERT(!rt->getUniqueKey().isValid());
489 // Make sure we match how we created the proxy with SkBudgeted::kNo
490 SkASSERT(SkBudgeted::kNo == rt->resourcePriv().isBudgeted());
491
492 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(std::move(rt), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500493}
494
Brian Salomon7578f3e2018-03-07 14:39:54 -0500495sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendTextureAsRenderTarget(
496 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500497 if (this->isAbandoned()) {
498 return nullptr;
499 }
500
Brian Salomonf7778972018-03-08 10:13:17 -0500501 // This is only supported on a direct GrContext.
502 if (!fResourceProvider) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500503 return nullptr;
504 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500505
Brian Salomonf7778972018-03-08 10:13:17 -0500506 sk_sp<GrRenderTarget> rt =
507 fResourceProvider->wrapBackendTextureAsRenderTarget(backendTex, sampleCnt);
508 if (!rt) {
509 return nullptr;
Greg Danielf87651e2018-02-21 11:36:53 -0500510 }
Brian Salomonf7778972018-03-08 10:13:17 -0500511 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
512 SkASSERT(!rt->getUniqueKey().isValid());
513 // Make sure we match how we created the proxy with SkBudgeted::kNo
514 SkASSERT(SkBudgeted::kNo == rt->resourcePriv().isBudgeted());
Greg Danielf87651e2018-02-21 11:36:53 -0500515
Brian Salomonf7778972018-03-08 10:13:17 -0500516 return sk_sp<GrSurfaceProxy>(new GrRenderTargetProxy(std::move(rt), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500517}
518
Robert Phillips777707b2018-01-17 11:40:14 -0500519sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
520 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500521 GrSurfaceOrigin origin,
522 GrMipMapped mipMapped, SkBackingFit fit,
523 SkBudgeted budgeted) {
524 return this->createLazyProxy(std::move(callback), desc, origin, mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400525 GrInternalSurfaceFlags::kNone, fit, budgeted);
Greg Daniel2a303902018-02-20 10:25:54 -0500526}
527
528sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
529 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500530 GrSurfaceOrigin origin,
Greg Daniel2a303902018-02-20 10:25:54 -0500531 GrMipMapped mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400532 GrInternalSurfaceFlags surfaceFlags,
Greg Daniel2a303902018-02-20 10:25:54 -0500533 SkBackingFit fit, SkBudgeted budgeted) {
Greg Daniela8d92112018-03-09 12:05:04 -0500534 // For non-ddl draws always make lazy proxy's single use.
535 LazyInstantiationType lazyType = fResourceProvider ? LazyInstantiationType::kSingleUse
536 : LazyInstantiationType::kMultipleUse;
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400537 return this->createLazyProxy(std::move(callback), desc, origin, mipMapped, surfaceFlags,
Greg Daniela8d92112018-03-09 12:05:04 -0500538 fit, budgeted, lazyType);
539}
540
541sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
542 const GrSurfaceDesc& desc,
543 GrSurfaceOrigin origin,
544 GrMipMapped mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400545 GrInternalSurfaceFlags surfaceFlags,
Greg Daniela8d92112018-03-09 12:05:04 -0500546 SkBackingFit fit, SkBudgeted budgeted,
547 LazyInstantiationType lazyType) {
Robert Phillips777707b2018-01-17 11:40:14 -0500548 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
549 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400550
551 if (desc.fWidth > fCaps->maxTextureSize() || desc.fHeight > fCaps->maxTextureSize()) {
552 return nullptr;
553 }
554
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400555 surfaceFlags |= GrInternalSurfaceFlags::kNoPendingIO;
Greg Daniel457469c2018-02-08 15:05:44 -0500556
Greg Daniel2a303902018-02-20 10:25:54 -0500557#ifdef SK_DEBUG
558 if (SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags)) {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400559 if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kMixedSampled)) {
Greg Daniel2a303902018-02-20 10:25:54 -0500560 SkASSERT(fCaps->usesMixedSamples() && desc.fSampleCnt > 1);
561 }
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400562 if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kWindowRectsSupport)) {
Greg Daniel2a303902018-02-20 10:25:54 -0500563 SkASSERT(fCaps->maxWindowRectangles() > 0);
564 }
565 }
566#endif
567
Brian Salomon2a4f9832018-03-03 22:43:43 -0500568 return sk_sp<GrTextureProxy>(
569 SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags)
570 ? new GrTextureRenderTargetProxy(std::move(callback), lazyType, desc, origin,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400571 mipMapped, fit, budgeted, surfaceFlags)
Brian Salomon2a4f9832018-03-03 22:43:43 -0500572 : new GrTextureProxy(std::move(callback), lazyType, desc, origin, mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400573 fit, budgeted, surfaceFlags));
Robert Phillips777707b2018-01-17 11:40:14 -0500574}
575
Robert Phillipse8fabb22018-02-04 14:33:21 -0500576sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy(
Brian Salomon2a4f9832018-03-03 22:43:43 -0500577 LazyInstantiateCallback&& callback, const GrSurfaceDesc& desc, GrSurfaceOrigin origin,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400578 GrInternalSurfaceFlags surfaceFlags, Textureable textureable, GrMipMapped mipMapped,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500579 SkBackingFit fit, SkBudgeted budgeted) {
Robert Phillipse8fabb22018-02-04 14:33:21 -0500580 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
581 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400582
583 if (desc.fWidth > fCaps->maxRenderTargetSize() || desc.fHeight > fCaps->maxRenderTargetSize()) {
584 return nullptr;
585 }
586
Greg Daniel2a303902018-02-20 10:25:54 -0500587 SkASSERT(SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags));
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400588 surfaceFlags |= GrInternalSurfaceFlags::kNoPendingIO;
Greg Daniel457469c2018-02-08 15:05:44 -0500589
Greg Daniel2a303902018-02-20 10:25:54 -0500590#ifdef SK_DEBUG
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400591 if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kMixedSampled)) {
Greg Daniel2a303902018-02-20 10:25:54 -0500592 SkASSERT(fCaps->usesMixedSamples() && desc.fSampleCnt > 1);
593 }
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400594 if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kWindowRectsSupport)) {
Greg Daniel2a303902018-02-20 10:25:54 -0500595 SkASSERT(fCaps->maxWindowRectangles() > 0);
596 }
597#endif
598
Greg Daniel457469c2018-02-08 15:05:44 -0500599 using LazyInstantiationType = GrSurfaceProxy::LazyInstantiationType;
600 // For non-ddl draws always make lazy proxy's single use.
601 LazyInstantiationType lazyType = fResourceProvider ? LazyInstantiationType::kSingleUse
602 : LazyInstantiationType::kMultipleUse;
603
Robert Phillipse8fabb22018-02-04 14:33:21 -0500604 if (Textureable::kYes == textureable) {
Brian Salomon2a4f9832018-03-03 22:43:43 -0500605 return sk_sp<GrRenderTargetProxy>(
606 new GrTextureRenderTargetProxy(std::move(callback), lazyType, desc, origin,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400607 mipMapped, fit, budgeted, surfaceFlags));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500608 }
609
Brian Salomon2a4f9832018-03-03 22:43:43 -0500610 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400611 std::move(callback), lazyType, desc, origin, fit, budgeted, surfaceFlags));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500612}
613
Robert Phillips777707b2018-01-17 11:40:14 -0500614sk_sp<GrTextureProxy> GrProxyProvider::createFullyLazyProxy(LazyInstantiateCallback&& callback,
615 Renderable renderable,
Robert Phillipsce5209a2018-02-13 11:13:51 -0500616 GrSurfaceOrigin origin,
Robert Phillips777707b2018-01-17 11:40:14 -0500617 GrPixelConfig config) {
618 GrSurfaceDesc desc;
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400619 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNone;
Robert Phillips777707b2018-01-17 11:40:14 -0500620 if (Renderable::kYes == renderable) {
621 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Greg Daniel2a303902018-02-20 10:25:54 -0500622 if (fCaps->maxWindowRectangles() > 0) {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400623 surfaceFlags |= GrInternalSurfaceFlags::kWindowRectsSupport;
Greg Daniel2a303902018-02-20 10:25:54 -0500624 }
Robert Phillips777707b2018-01-17 11:40:14 -0500625 }
Robert Phillips777707b2018-01-17 11:40:14 -0500626 desc.fWidth = -1;
627 desc.fHeight = -1;
628 desc.fConfig = config;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500629 desc.fSampleCnt = 1;
Robert Phillips777707b2018-01-17 11:40:14 -0500630
Brian Salomon2a4f9832018-03-03 22:43:43 -0500631 return this->createLazyProxy(std::move(callback), desc, origin, GrMipMapped::kNo,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400632 surfaceFlags, SkBackingFit::kApprox, SkBudgeted::kYes);
Robert Phillips777707b2018-01-17 11:40:14 -0500633}
634
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500635bool GrProxyProvider::IsFunctionallyExact(GrSurfaceProxy* proxy) {
636 return proxy->priv().isExact() || (SkIsPow2(proxy->width()) && SkIsPow2(proxy->height()));
637}
638
639void GrProxyProvider::processInvalidProxyUniqueKey(const GrUniqueKey& key) {
640 // Note: this method is called for the whole variety of GrGpuResources so often 'key'
641 // will not be in 'fUniquelyKeyedProxies'.
642 GrTextureProxy* proxy = fUniquelyKeyedProxies.find(key);
643 if (proxy) {
644 this->processInvalidProxyUniqueKey(key, proxy, false);
645 }
646}
647
648void GrProxyProvider::processInvalidProxyUniqueKey(const GrUniqueKey& key, GrTextureProxy* proxy,
649 bool invalidateSurface) {
650 SkASSERT(proxy);
651 SkASSERT(proxy->getUniqueKey().isValid());
652 SkASSERT(proxy->getUniqueKey() == key);
653
654 fUniquelyKeyedProxies.remove(key);
655 proxy->cacheAccess().clearUniqueKey();
656
657 if (invalidateSurface && proxy->priv().isInstantiated()) {
658 GrSurface* surface = proxy->priv().peekSurface();
659 if (surface) {
660 surface->resourcePriv().removeUniqueKey();
661 }
662 }
663}
664
665void GrProxyProvider::removeAllUniqueKeys() {
666 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
667 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
668 GrTextureProxy& tmp = *iter;
669
670 this->processInvalidProxyUniqueKey(tmp.getUniqueKey(), &tmp, false);
671 }
672 SkASSERT(!fUniquelyKeyedProxies.count());
673}