blob: df7b61e7005f8e0b87a5a50d4f9277194f95083a [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)
Brian Salomon238069b2018-07-11 15:58:57 -040040 , fContextUniqueID(resourceCache->contextUniqueID())
Robert Phillips1afd4cd2018-01-08 13:40:32 -050041#ifdef SK_DEBUG
42 , fSingleOwner(owner)
43#endif
44{
Brian Salomon238069b2018-07-11 15:58:57 -040045 SkASSERT(fResourceProvider);
46 SkASSERT(fResourceCache);
47 SkASSERT(fCaps);
48 SkASSERT(fSingleOwner);
49}
Robert Phillips1afd4cd2018-01-08 13:40:32 -050050
Brian Salomon238069b2018-07-11 15:58:57 -040051GrProxyProvider::GrProxyProvider(uint32_t contextUniqueID,
52 sk_sp<const GrCaps> caps,
53 GrSingleOwner* owner)
54 : fResourceProvider(nullptr)
55 , fResourceCache(nullptr)
56 , fAbandoned(false)
57 , fCaps(caps)
58 , fContextUniqueID(contextUniqueID)
59#ifdef SK_DEBUG
60 , fSingleOwner(owner)
61#endif
62{
63 SkASSERT(fContextUniqueID != SK_InvalidUniqueID);
64 SkASSERT(fCaps);
65 SkASSERT(fSingleOwner);
Robert Phillips1afd4cd2018-01-08 13:40:32 -050066}
67
68GrProxyProvider::~GrProxyProvider() {
69 SkASSERT(!fUniquelyKeyedProxies.count());
70}
71
Robert Phillipsadbe1322018-01-17 13:35:46 -050072bool GrProxyProvider::assignUniqueKeyToProxy(const GrUniqueKey& key, GrTextureProxy* proxy) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050073 ASSERT_SINGLE_OWNER
74 SkASSERT(key.isValid());
75 if (this->isAbandoned() || !proxy) {
Robert Phillipsadbe1322018-01-17 13:35:46 -050076 return false;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050077 }
78
79 // If there is already a GrResource with this key then the caller has violated the normal
80 // usage pattern of uniquely keyed resources (e.g., they have created one w/o first seeing
81 // if it already existed in the cache).
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -050082 SkASSERT(!fResourceCache || !fResourceCache->findAndRefUniqueResource(key));
Robert Phillips1afd4cd2018-01-08 13:40:32 -050083
84 // Uncached resources can never have a unique key, unless they're wrapped resources. Wrapped
85 // resources are a special case: the unique keys give us a weak ref so that we can reuse the
86 // same resource (rather than re-wrapping). When a wrapped resource is no longer referenced,
87 // it will always be released - it is never converted to a scratch resource.
88 if (SkBudgeted::kNo == proxy->isBudgeted() &&
89 (!proxy->priv().isInstantiated() ||
90 !proxy->priv().peekSurface()->resourcePriv().refsWrappedObjects())) {
Robert Phillipsadbe1322018-01-17 13:35:46 -050091 return false;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050092 }
93
94 SkASSERT(!fUniquelyKeyedProxies.find(key)); // multiple proxies can't get the same key
95
96 proxy->cacheAccess().setUniqueKey(this, key);
97 SkASSERT(proxy->getUniqueKey() == key);
98 fUniquelyKeyedProxies.add(proxy);
Robert Phillipsadbe1322018-01-17 13:35:46 -050099 return true;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500100}
101
102void GrProxyProvider::adoptUniqueKeyFromSurface(GrTextureProxy* proxy, const GrSurface* surf) {
103 SkASSERT(surf->getUniqueKey().isValid());
104 proxy->cacheAccess().setUniqueKey(this, surf->getUniqueKey());
105 SkASSERT(proxy->getUniqueKey() == surf->getUniqueKey());
106 // multiple proxies can't get the same key
107 SkASSERT(!fUniquelyKeyedProxies.find(surf->getUniqueKey()));
108 fUniquelyKeyedProxies.add(proxy);
109}
110
111void GrProxyProvider::removeUniqueKeyFromProxy(const GrUniqueKey& key, GrTextureProxy* proxy) {
112 ASSERT_SINGLE_OWNER
113 if (this->isAbandoned() || !proxy) {
114 return;
115 }
116 this->processInvalidProxyUniqueKey(key, proxy, true);
117}
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
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500180sk_sp<GrTextureProxy> GrProxyProvider::createInstantiatedProxy(const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500181 GrSurfaceOrigin origin,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500182 SkBackingFit fit,
183 SkBudgeted budgeted,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400184 GrSurfaceDescFlags descFlags) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500185 sk_sp<GrTexture> tex;
186
187 if (SkBackingFit::kApprox == fit) {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400188 tex = fResourceProvider->createApproxTexture(desc, descFlags);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500189 } else {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400190 tex = fResourceProvider->createTexture(desc, budgeted, descFlags);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500191 }
192 if (!tex) {
193 return nullptr;
194 }
195
Brian Salomon2a4f9832018-03-03 22:43:43 -0500196 return this->createWrapped(std::move(tex), origin);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500197}
198
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500199sk_sp<GrTextureProxy> GrProxyProvider::createTextureProxy(const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500200 SkBudgeted budgeted, const void* srcData,
201 size_t rowBytes) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500202 ASSERT_SINGLE_OWNER
203
Robert Phillips579f0942018-01-08 14:53:35 -0500204 if (this->isAbandoned()) {
205 return nullptr;
206 }
207
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500208 if (srcData) {
209 GrMipLevel mipLevel = { srcData, rowBytes };
210
Brian Salomon58389b92018-03-07 13:01:25 -0500211 sk_sp<GrTexture> tex =
212 fResourceProvider->createTexture(desc, budgeted, SkBackingFit::kExact, mipLevel);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500213 if (!tex) {
214 return nullptr;
215 }
216
Brian Salomon58389b92018-03-07 13:01:25 -0500217 return this->createWrapped(std::move(tex), kTopLeft_GrSurfaceOrigin);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500218 }
219
Brian Salomon58389b92018-03-07 13:01:25 -0500220 return this->createProxy(desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kExact, budgeted);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500221}
222
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500223sk_sp<GrTextureProxy> GrProxyProvider::createTextureProxy(sk_sp<SkImage> srcImage,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400224 GrSurfaceDescFlags descFlags,
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500225 int sampleCnt,
Greg Danielfb3abcd2018-02-02 15:48:33 -0500226 SkBudgeted budgeted,
227 SkBackingFit fit) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500228 ASSERT_SINGLE_OWNER
229 SkASSERT(srcImage);
230
231 if (this->isAbandoned()) {
232 return nullptr;
233 }
234
Brian Osman2b23c4b2018-06-01 12:25:08 -0400235 GrPixelConfig config = SkImageInfo2GrPixelConfig(as_IB(srcImage)->onImageInfo());
Greg Danielf87651e2018-02-21 11:36:53 -0500236
Greg Daniel0a7aa142018-02-21 13:02:32 -0500237 if (kUnknown_GrPixelConfig == config) {
238 return nullptr;
239 }
240
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400241 if (SkToBool(descFlags & kRenderTarget_GrSurfaceFlag)) {
Greg Danielf87651e2018-02-21 11:36:53 -0500242 sampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, config);
243 if (!sampleCnt) {
244 return nullptr;
245 }
246 }
247
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400248 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNone;
249 if (SkToBool(descFlags & kRenderTarget_GrSurfaceFlag)) {
Greg Daniel2a303902018-02-20 10:25:54 -0500250 if (fCaps->usesMixedSamples() && sampleCnt > 1) {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400251 surfaceFlags |= GrInternalSurfaceFlags::kMixedSampled;
Greg Daniel2a303902018-02-20 10:25:54 -0500252 }
253 if (fCaps->maxWindowRectangles() > 0) {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400254 surfaceFlags |= GrInternalSurfaceFlags::kWindowRectsSupport;
Greg Daniel2a303902018-02-20 10:25:54 -0500255 }
256 }
257
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500258 GrSurfaceDesc desc;
259 desc.fWidth = srcImage->width();
260 desc.fHeight = srcImage->height();
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400261 desc.fFlags = descFlags;
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500262 desc.fSampleCnt = sampleCnt;
Greg Danielf87651e2018-02-21 11:36:53 -0500263 desc.fConfig = config;
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500264
265 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomon58389b92018-03-07 13:01:25 -0500266 [desc, budgeted, srcImage, fit](GrResourceProvider* resourceProvider) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500267 if (!resourceProvider) {
Greg Daniel0a375db2018-02-01 12:21:39 -0500268 // Nothing to clean up here. Once the proxy (and thus lambda) is deleted the ref
269 // on srcImage will be released.
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500270 return sk_sp<GrTexture>();
271 }
272 SkPixmap pixMap;
273 SkAssertResult(srcImage->peekPixels(&pixMap));
274 GrMipLevel mipLevel = { pixMap.addr(), pixMap.rowBytes() };
275
Brian Salomon58389b92018-03-07 13:01:25 -0500276 return resourceProvider->createTexture(desc, budgeted, fit, mipLevel);
Brian Salomon2a4f9832018-03-03 22:43:43 -0500277 },
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400278 desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, surfaceFlags, fit, budgeted);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500279
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400280 if (!proxy) {
281 return nullptr;
282 }
283
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500284 if (fResourceProvider) {
285 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
286 // we're better off instantiating the proxy immediately here.
287 if (!proxy->priv().doLazyInstantiation(fResourceProvider)) {
288 return nullptr;
289 }
290 }
Robert Phillipsc1b60662018-06-26 10:20:08 -0400291
292 SkASSERT(proxy->width() == desc.fWidth);
293 SkASSERT(proxy->height() == desc.fHeight);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500294 return proxy;
295}
296
Greg Daniel30815082018-02-09 16:08:30 -0500297sk_sp<GrTextureProxy> GrProxyProvider::createMipMapProxy(const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500298 GrSurfaceOrigin origin,
Greg Daniel30815082018-02-09 16:08:30 -0500299 SkBudgeted budgeted) {
Robert Phillips579f0942018-01-08 14:53:35 -0500300 ASSERT_SINGLE_OWNER
301
302 if (this->isAbandoned()) {
303 return nullptr;
304 }
305
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400306 return this->createProxy(desc, origin, GrMipMapped::kYes, SkBackingFit::kExact, budgeted,
307 GrInternalSurfaceFlags::kNone);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500308}
309
Brian Osman2b23c4b2018-06-01 12:25:08 -0400310sk_sp<GrTextureProxy> GrProxyProvider::createMipMapProxyFromBitmap(const SkBitmap& bitmap) {
311 if (!SkImageInfoIsValid(bitmap.info())) {
Greg Daniela4ead652018-02-07 10:21:48 -0500312 return nullptr;
313 }
314
315 SkPixmap pixmap;
316 if (!bitmap.peekPixels(&pixmap)) {
317 return nullptr;
318 }
319
320 ATRACE_ANDROID_FRAMEWORK("Upload MipMap Texture [%ux%u]", pixmap.width(), pixmap.height());
Brian Osman2b23c4b2018-06-01 12:25:08 -0400321 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(pixmap, nullptr));
Greg Daniela4ead652018-02-07 10:21:48 -0500322 if (!mipmaps) {
323 return nullptr;
324 }
325
326 if (mipmaps->countLevels() < 0) {
327 return nullptr;
328 }
329
330 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
331 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
332 // 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 -0400333 SkCopyPixelsMode copyMode = this->recordingDDL() ? kIfMutable_SkCopyPixelsMode
334 : kNever_SkCopyPixelsMode;
Greg Daniela4ead652018-02-07 10:21:48 -0500335 sk_sp<SkImage> baseLevel = SkMakeImageFromRasterBitmap(bitmap, copyMode);
336
337 if (!baseLevel) {
338 return nullptr;
339 }
340
Brian Osman2b23c4b2018-06-01 12:25:08 -0400341 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(pixmap.info());
Greg Daniela4ead652018-02-07 10:21:48 -0500342
343 if (0 == mipmaps->countLevels()) {
Brian Salomon58389b92018-03-07 13:01:25 -0500344 return this->createTextureProxy(baseLevel, kNone_GrSurfaceFlags, 1, SkBudgeted::kYes,
345 SkBackingFit::kExact);
Greg Daniela4ead652018-02-07 10:21:48 -0500346 }
347
348 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Osman2b23c4b2018-06-01 12:25:08 -0400349 [desc, baseLevel, mipmaps](GrResourceProvider* resourceProvider) {
Greg Daniela4ead652018-02-07 10:21:48 -0500350 if (!resourceProvider) {
351 return sk_sp<GrTexture>();
352 }
353
354 const int mipLevelCount = mipmaps->countLevels() + 1;
355 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
356
357 SkPixmap pixmap;
358 SkAssertResult(baseLevel->peekPixels(&pixmap));
359
360 // DDL TODO: Instead of copying all this info into GrMipLevels we should just plumb
361 // the use of SkMipMap down through Ganesh.
362 texels[0].fPixels = pixmap.addr();
363 texels[0].fRowBytes = pixmap.rowBytes();
364
365 for (int i = 1; i < mipLevelCount; ++i) {
366 SkMipMap::Level generatedMipLevel;
367 mipmaps->getLevel(i - 1, &generatedMipLevel);
368 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
369 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
370 SkASSERT(texels[i].fPixels);
371 }
372
Brian Salomon58389b92018-03-07 13:01:25 -0500373 return resourceProvider->createTexture(desc, SkBudgeted::kYes, texels.get(),
Brian Osman2b23c4b2018-06-01 12:25:08 -0400374 mipLevelCount);
Brian Salomon2a4f9832018-03-03 22:43:43 -0500375 },
376 desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kYes, SkBackingFit::kExact,
377 SkBudgeted::kYes);
Greg Daniela4ead652018-02-07 10:21:48 -0500378
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400379 if (!proxy) {
380 return nullptr;
381 }
382
Greg Daniela4ead652018-02-07 10:21:48 -0500383 if (fResourceProvider) {
384 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
385 // we're better off instantiating the proxy immediately here.
386 if (!proxy->priv().doLazyInstantiation(fResourceProvider)) {
387 return nullptr;
388 }
389 }
390 return proxy;
391}
392
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500393sk_sp<GrTextureProxy> GrProxyProvider::createProxy(const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500394 GrSurfaceOrigin origin,
Greg Danielf6f7b672018-02-15 13:06:26 -0500395 GrMipMapped mipMapped,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500396 SkBackingFit fit,
397 SkBudgeted budgeted,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400398 GrInternalSurfaceFlags surfaceFlags) {
Greg Danielf6f7b672018-02-15 13:06:26 -0500399 if (GrMipMapped::kYes == mipMapped) {
400 // SkMipMap doesn't include the base level in the level count so we have to add 1
401 int mipCount = SkMipMap::ComputeLevelCount(desc.fWidth, desc.fHeight) + 1;
402 if (1 == mipCount) {
403 mipMapped = GrMipMapped::kNo;
404 }
405 }
406
407 if (!this->caps()->validateSurfaceDesc(desc, mipMapped)) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500408 return nullptr;
409 }
Brian Salomon3a2cc2c2018-02-03 00:25:12 +0000410 GrSurfaceDesc copyDesc = desc;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500411 if (desc.fFlags & kRenderTarget_GrSurfaceFlag) {
412 copyDesc.fSampleCnt =
413 this->caps()->getRenderTargetSampleCount(desc.fSampleCnt, desc.fConfig);
414 }
Brian Salomon3a2cc2c2018-02-03 00:25:12 +0000415
Brian Salomonbdecacf2018-02-02 20:32:49 -0500416 if (copyDesc.fFlags & kRenderTarget_GrSurfaceFlag) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500417 // We know anything we instantiate later from this deferred path will be
418 // both texturable and renderable
Brian Salomon2a4f9832018-03-03 22:43:43 -0500419 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400420 *this->caps(), copyDesc, origin, mipMapped, fit, budgeted, surfaceFlags));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500421 }
422
Brian Salomon2a4f9832018-03-03 22:43:43 -0500423 return sk_sp<GrTextureProxy>(
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400424 new GrTextureProxy(copyDesc, origin, mipMapped, fit, budgeted, surfaceFlags));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500425}
426
Brian Salomon7578f3e2018-03-07 14:39:54 -0500427sk_sp<GrTextureProxy> GrProxyProvider::wrapBackendTexture(const GrBackendTexture& backendTex,
428 GrSurfaceOrigin origin,
429 GrWrapOwnership ownership,
430 ReleaseProc releaseProc,
431 ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500432 if (this->isAbandoned()) {
433 return nullptr;
434 }
435
Brian Salomonf7778972018-03-08 10:13:17 -0500436 // This is only supported on a direct GrContext.
437 if (!fResourceProvider) {
438 return nullptr;
439 }
440
441 sk_sp<GrTexture> tex = fResourceProvider->wrapBackendTexture(backendTex, ownership);
442 if (!tex) {
443 return nullptr;
444 }
Robert Phillipsadbe1322018-01-17 13:35:46 -0500445
Greg Daniel6a0176b2018-01-30 09:28:44 -0500446 sk_sp<GrReleaseProcHelper> releaseHelper;
447 if (releaseProc) {
448 releaseHelper.reset(new GrReleaseProcHelper(releaseProc, releaseCtx));
Brian Salomonf7778972018-03-08 10:13:17 -0500449 // This gives the texture a ref on the releaseHelper
450 tex->setRelease(releaseHelper);
Greg Daniel6a0176b2018-01-30 09:28:44 -0500451 }
452
Brian Salomonf7778972018-03-08 10:13:17 -0500453 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
454 // Make sure we match how we created the proxy with SkBudgeted::kNo
455 SkASSERT(SkBudgeted::kNo == tex->resourcePriv().isBudgeted());
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500456
Brian Salomonf7778972018-03-08 10:13:17 -0500457 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500458}
459
Brian Salomon7578f3e2018-03-07 14:39:54 -0500460sk_sp<GrTextureProxy> GrProxyProvider::wrapRenderableBackendTexture(
Brian Salomon02bd2952018-03-07 15:20:21 -0500461 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt,
462 GrWrapOwnership ownership) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500463 if (this->isAbandoned()) {
464 return nullptr;
465 }
466
Brian Salomonf7778972018-03-08 10:13:17 -0500467 // This is only supported on a direct GrContext.
468 if (!fResourceProvider) {
469 return nullptr;
470 }
471
Greg Daniel6abda432018-02-15 14:55:00 -0500472 sampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config());
Greg Danielf87651e2018-02-21 11:36:53 -0500473 if (!sampleCnt) {
474 return nullptr;
475 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500476
Brian Salomonf7778972018-03-08 10:13:17 -0500477 sk_sp<GrTexture> tex =
478 fResourceProvider->wrapRenderableBackendTexture(backendTex, sampleCnt, ownership);
479 if (!tex) {
480 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500481 }
482
Brian Salomonf7778972018-03-08 10:13:17 -0500483 SkASSERT(tex->asRenderTarget()); // A GrTextureRenderTarget
484 // Make sure we match how we created the proxy with SkBudgeted::kNo
485 SkASSERT(SkBudgeted::kNo == tex->resourcePriv().isBudgeted());
Greg Daniel6abda432018-02-15 14:55:00 -0500486
Brian Salomonf7778972018-03-08 10:13:17 -0500487 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500488}
489
Brian Salomon7578f3e2018-03-07 14:39:54 -0500490sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget(
491 const GrBackendRenderTarget& backendRT, GrSurfaceOrigin origin) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500492 if (this->isAbandoned()) {
493 return nullptr;
494 }
495
Brian Salomonf7778972018-03-08 10:13:17 -0500496 // This is only supported on a direct GrContext.
497 if (!fResourceProvider) {
498 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500499 }
500
Brian Salomonf7778972018-03-08 10:13:17 -0500501 sk_sp<GrRenderTarget> rt = fResourceProvider->wrapBackendRenderTarget(backendRT);
502 if (!rt) {
503 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500504 }
Brian Salomonf7778972018-03-08 10:13:17 -0500505 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
506 SkASSERT(!rt->getUniqueKey().isValid());
507 // Make sure we match how we created the proxy with SkBudgeted::kNo
508 SkASSERT(SkBudgeted::kNo == rt->resourcePriv().isBudgeted());
509
510 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(std::move(rt), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500511}
512
Brian Salomon7578f3e2018-03-07 14:39:54 -0500513sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendTextureAsRenderTarget(
514 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500515 if (this->isAbandoned()) {
516 return nullptr;
517 }
518
Brian Salomonf7778972018-03-08 10:13:17 -0500519 // This is only supported on a direct GrContext.
520 if (!fResourceProvider) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500521 return nullptr;
522 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500523
Brian Salomonf7778972018-03-08 10:13:17 -0500524 sk_sp<GrRenderTarget> rt =
525 fResourceProvider->wrapBackendTextureAsRenderTarget(backendTex, sampleCnt);
526 if (!rt) {
527 return nullptr;
Greg Danielf87651e2018-02-21 11:36:53 -0500528 }
Brian Salomonf7778972018-03-08 10:13:17 -0500529 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
530 SkASSERT(!rt->getUniqueKey().isValid());
531 // Make sure we match how we created the proxy with SkBudgeted::kNo
532 SkASSERT(SkBudgeted::kNo == rt->resourcePriv().isBudgeted());
Greg Danielf87651e2018-02-21 11:36:53 -0500533
Brian Salomonf7778972018-03-08 10:13:17 -0500534 return sk_sp<GrSurfaceProxy>(new GrRenderTargetProxy(std::move(rt), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500535}
536
Robert Phillips777707b2018-01-17 11:40:14 -0500537sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
538 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500539 GrSurfaceOrigin origin,
540 GrMipMapped mipMapped, SkBackingFit fit,
541 SkBudgeted budgeted) {
542 return this->createLazyProxy(std::move(callback), desc, origin, mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400543 GrInternalSurfaceFlags::kNone, fit, budgeted);
Greg Daniel2a303902018-02-20 10:25:54 -0500544}
545
546sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
547 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500548 GrSurfaceOrigin origin,
Greg Daniel2a303902018-02-20 10:25:54 -0500549 GrMipMapped mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400550 GrInternalSurfaceFlags surfaceFlags,
Greg Daniel2a303902018-02-20 10:25:54 -0500551 SkBackingFit fit, SkBudgeted budgeted) {
Greg Daniela8d92112018-03-09 12:05:04 -0500552 // For non-ddl draws always make lazy proxy's single use.
553 LazyInstantiationType lazyType = fResourceProvider ? LazyInstantiationType::kSingleUse
554 : LazyInstantiationType::kMultipleUse;
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400555 return this->createLazyProxy(std::move(callback), desc, origin, mipMapped, surfaceFlags,
Greg Daniela8d92112018-03-09 12:05:04 -0500556 fit, budgeted, lazyType);
557}
558
559sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
560 const GrSurfaceDesc& desc,
561 GrSurfaceOrigin origin,
562 GrMipMapped mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400563 GrInternalSurfaceFlags surfaceFlags,
Greg Daniela8d92112018-03-09 12:05:04 -0500564 SkBackingFit fit, SkBudgeted budgeted,
565 LazyInstantiationType lazyType) {
Robert Phillips777707b2018-01-17 11:40:14 -0500566 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
567 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400568
569 if (desc.fWidth > fCaps->maxTextureSize() || desc.fHeight > fCaps->maxTextureSize()) {
570 return nullptr;
571 }
572
Greg Daniel457469c2018-02-08 15:05:44 -0500573
Greg Daniel2a303902018-02-20 10:25:54 -0500574#ifdef SK_DEBUG
575 if (SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags)) {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400576 if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kMixedSampled)) {
Greg Daniel2a303902018-02-20 10:25:54 -0500577 SkASSERT(fCaps->usesMixedSamples() && desc.fSampleCnt > 1);
578 }
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400579 if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kWindowRectsSupport)) {
Greg Daniel2a303902018-02-20 10:25:54 -0500580 SkASSERT(fCaps->maxWindowRectangles() > 0);
581 }
582 }
583#endif
584
Brian Salomon2a4f9832018-03-03 22:43:43 -0500585 return sk_sp<GrTextureProxy>(
586 SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags)
587 ? new GrTextureRenderTargetProxy(std::move(callback), lazyType, desc, origin,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400588 mipMapped, fit, budgeted, surfaceFlags)
Brian Salomon2a4f9832018-03-03 22:43:43 -0500589 : new GrTextureProxy(std::move(callback), lazyType, desc, origin, mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400590 fit, budgeted, surfaceFlags));
Robert Phillips777707b2018-01-17 11:40:14 -0500591}
592
Robert Phillipse8fabb22018-02-04 14:33:21 -0500593sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy(
Brian Salomon2a4f9832018-03-03 22:43:43 -0500594 LazyInstantiateCallback&& callback, const GrSurfaceDesc& desc, GrSurfaceOrigin origin,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400595 GrInternalSurfaceFlags surfaceFlags, Textureable textureable, GrMipMapped mipMapped,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500596 SkBackingFit fit, SkBudgeted budgeted) {
Robert Phillipse8fabb22018-02-04 14:33:21 -0500597 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
598 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400599
600 if (desc.fWidth > fCaps->maxRenderTargetSize() || desc.fHeight > fCaps->maxRenderTargetSize()) {
601 return nullptr;
602 }
603
Greg Daniel2a303902018-02-20 10:25:54 -0500604 SkASSERT(SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags));
Greg Daniel457469c2018-02-08 15:05:44 -0500605
Greg Daniel2a303902018-02-20 10:25:54 -0500606#ifdef SK_DEBUG
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400607 if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kMixedSampled)) {
Greg Daniel2a303902018-02-20 10:25:54 -0500608 SkASSERT(fCaps->usesMixedSamples() && desc.fSampleCnt > 1);
609 }
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400610 if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kWindowRectsSupport)) {
Greg Daniel2a303902018-02-20 10:25:54 -0500611 SkASSERT(fCaps->maxWindowRectangles() > 0);
612 }
613#endif
614
Greg Daniel457469c2018-02-08 15:05:44 -0500615 using LazyInstantiationType = GrSurfaceProxy::LazyInstantiationType;
616 // For non-ddl draws always make lazy proxy's single use.
617 LazyInstantiationType lazyType = fResourceProvider ? LazyInstantiationType::kSingleUse
618 : LazyInstantiationType::kMultipleUse;
619
Robert Phillipse8fabb22018-02-04 14:33:21 -0500620 if (Textureable::kYes == textureable) {
Brian Salomon2a4f9832018-03-03 22:43:43 -0500621 return sk_sp<GrRenderTargetProxy>(
622 new GrTextureRenderTargetProxy(std::move(callback), lazyType, desc, origin,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400623 mipMapped, fit, budgeted, surfaceFlags));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500624 }
625
Brian Salomon2a4f9832018-03-03 22:43:43 -0500626 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400627 std::move(callback), lazyType, desc, origin, fit, budgeted, surfaceFlags));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500628}
629
Chris Dalton4c458b12018-06-16 17:22:59 -0600630sk_sp<GrTextureProxy> GrProxyProvider::MakeFullyLazyProxy(LazyInstantiateCallback&& callback,
631 Renderable renderable,
632 GrSurfaceOrigin origin,
633 GrPixelConfig config,
634 const GrCaps& caps) {
Robert Phillips777707b2018-01-17 11:40:14 -0500635 GrSurfaceDesc desc;
Greg Daniele3204862018-04-16 11:24:10 -0400636 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNoPendingIO;
Robert Phillips777707b2018-01-17 11:40:14 -0500637 if (Renderable::kYes == renderable) {
638 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Chris Dalton4c458b12018-06-16 17:22:59 -0600639 if (caps.maxWindowRectangles() > 0) {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400640 surfaceFlags |= GrInternalSurfaceFlags::kWindowRectsSupport;
Greg Daniel2a303902018-02-20 10:25:54 -0500641 }
Robert Phillips777707b2018-01-17 11:40:14 -0500642 }
Robert Phillips777707b2018-01-17 11:40:14 -0500643 desc.fWidth = -1;
644 desc.fHeight = -1;
645 desc.fConfig = config;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500646 desc.fSampleCnt = 1;
Robert Phillips777707b2018-01-17 11:40:14 -0500647
Chris Dalton4c458b12018-06-16 17:22:59 -0600648 return sk_sp<GrTextureProxy>(
649 (Renderable::kYes == renderable)
650 ? new GrTextureRenderTargetProxy(std::move(callback),
651 LazyInstantiationType::kSingleUse, desc,
652 origin, GrMipMapped::kNo,
653 SkBackingFit::kApprox, SkBudgeted::kYes,
654 surfaceFlags)
655 : new GrTextureProxy(std::move(callback), LazyInstantiationType::kSingleUse,
656 desc, origin, GrMipMapped::kNo, SkBackingFit::kApprox,
657 SkBudgeted::kYes, surfaceFlags));
Robert Phillips777707b2018-01-17 11:40:14 -0500658}
659
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500660bool GrProxyProvider::IsFunctionallyExact(GrSurfaceProxy* proxy) {
Robert Phillipsdb3b9792018-06-12 15:18:00 -0400661 const bool isInstantiated = proxy->priv().isInstantiated();
662 // A proxy is functionally exact if:
663 // it is exact (obvs)
664 // when it is instantiated it will be exact (i.e., power of two dimensions)
665 // it is already instantiated and the proxy covers the entire backing surface
666 return proxy->priv().isExact() ||
667 (!isInstantiated && SkIsPow2(proxy->width()) && SkIsPow2(proxy->height())) ||
668 (isInstantiated && proxy->worstCaseWidth() == proxy->width() &&
669 proxy->worstCaseHeight() == proxy->height());
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500670}
671
672void GrProxyProvider::processInvalidProxyUniqueKey(const GrUniqueKey& key) {
673 // Note: this method is called for the whole variety of GrGpuResources so often 'key'
674 // will not be in 'fUniquelyKeyedProxies'.
675 GrTextureProxy* proxy = fUniquelyKeyedProxies.find(key);
676 if (proxy) {
677 this->processInvalidProxyUniqueKey(key, proxy, false);
678 }
679}
680
681void GrProxyProvider::processInvalidProxyUniqueKey(const GrUniqueKey& key, GrTextureProxy* proxy,
682 bool invalidateSurface) {
683 SkASSERT(proxy);
684 SkASSERT(proxy->getUniqueKey().isValid());
685 SkASSERT(proxy->getUniqueKey() == key);
686
687 fUniquelyKeyedProxies.remove(key);
688 proxy->cacheAccess().clearUniqueKey();
689
690 if (invalidateSurface && proxy->priv().isInstantiated()) {
691 GrSurface* surface = proxy->priv().peekSurface();
692 if (surface) {
693 surface->resourcePriv().removeUniqueKey();
694 }
695 }
696}
697
698void GrProxyProvider::removeAllUniqueKeys() {
699 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
700 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
701 GrTextureProxy& tmp = *iter;
702
703 this->processInvalidProxyUniqueKey(tmp.getUniqueKey(), &tmp, false);
704 }
705 SkASSERT(!fUniquelyKeyedProxies.count());
706}