blob: e259e34e28525147003d2c4e12f39a60c3ac99b5 [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
Brian Osman2b23c4b2018-06-01 12:25:08 -0400214 GrPixelConfig config = SkImageInfo2GrPixelConfig(as_IB(srcImage)->onImageInfo());
Greg Danielf87651e2018-02-21 11:36:53 -0500215
Greg Daniel0a7aa142018-02-21 13:02:32 -0500216 if (kUnknown_GrPixelConfig == config) {
217 return nullptr;
218 }
219
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400220 if (SkToBool(descFlags & kRenderTarget_GrSurfaceFlag)) {
Greg Danielf87651e2018-02-21 11:36:53 -0500221 sampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, config);
222 if (!sampleCnt) {
223 return nullptr;
224 }
225 }
226
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400227 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNone;
228 if (SkToBool(descFlags & kRenderTarget_GrSurfaceFlag)) {
Greg Daniel2a303902018-02-20 10:25:54 -0500229 if (fCaps->usesMixedSamples() && sampleCnt > 1) {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400230 surfaceFlags |= GrInternalSurfaceFlags::kMixedSampled;
Greg Daniel2a303902018-02-20 10:25:54 -0500231 }
232 if (fCaps->maxWindowRectangles() > 0) {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400233 surfaceFlags |= GrInternalSurfaceFlags::kWindowRectsSupport;
Greg Daniel2a303902018-02-20 10:25:54 -0500234 }
235 }
236
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500237 GrSurfaceDesc desc;
238 desc.fWidth = srcImage->width();
239 desc.fHeight = srcImage->height();
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400240 desc.fFlags = descFlags;
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500241 desc.fSampleCnt = sampleCnt;
Greg Danielf87651e2018-02-21 11:36:53 -0500242 desc.fConfig = config;
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500243
244 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomon58389b92018-03-07 13:01:25 -0500245 [desc, budgeted, srcImage, fit](GrResourceProvider* resourceProvider) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500246 if (!resourceProvider) {
Greg Daniel0a375db2018-02-01 12:21:39 -0500247 // Nothing to clean up here. Once the proxy (and thus lambda) is deleted the ref
248 // on srcImage will be released.
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500249 return sk_sp<GrTexture>();
250 }
251 SkPixmap pixMap;
252 SkAssertResult(srcImage->peekPixels(&pixMap));
253 GrMipLevel mipLevel = { pixMap.addr(), pixMap.rowBytes() };
254
Brian Salomon58389b92018-03-07 13:01:25 -0500255 return resourceProvider->createTexture(desc, budgeted, fit, mipLevel);
Brian Salomon2a4f9832018-03-03 22:43:43 -0500256 },
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400257 desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, surfaceFlags, fit, budgeted);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500258
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400259 if (!proxy) {
260 return nullptr;
261 }
262
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500263 if (fResourceProvider) {
264 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
265 // we're better off instantiating the proxy immediately here.
266 if (!proxy->priv().doLazyInstantiation(fResourceProvider)) {
267 return nullptr;
268 }
269 }
Robert Phillipsc1b60662018-06-26 10:20:08 -0400270
271 SkASSERT(proxy->width() == desc.fWidth);
272 SkASSERT(proxy->height() == desc.fHeight);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500273 return proxy;
274}
275
Greg Daniel30815082018-02-09 16:08:30 -0500276sk_sp<GrTextureProxy> GrProxyProvider::createMipMapProxy(const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500277 GrSurfaceOrigin origin,
Greg Daniel30815082018-02-09 16:08:30 -0500278 SkBudgeted budgeted) {
Robert Phillips579f0942018-01-08 14:53:35 -0500279 ASSERT_SINGLE_OWNER
280
281 if (this->isAbandoned()) {
282 return nullptr;
283 }
284
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400285 return this->createProxy(desc, origin, GrMipMapped::kYes, SkBackingFit::kExact, budgeted,
286 GrInternalSurfaceFlags::kNone);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500287}
288
Brian Osman2b23c4b2018-06-01 12:25:08 -0400289sk_sp<GrTextureProxy> GrProxyProvider::createMipMapProxyFromBitmap(const SkBitmap& bitmap) {
290 if (!SkImageInfoIsValid(bitmap.info())) {
Greg Daniela4ead652018-02-07 10:21:48 -0500291 return nullptr;
292 }
293
294 SkPixmap pixmap;
295 if (!bitmap.peekPixels(&pixmap)) {
296 return nullptr;
297 }
298
299 ATRACE_ANDROID_FRAMEWORK("Upload MipMap Texture [%ux%u]", pixmap.width(), pixmap.height());
Brian Osman2b23c4b2018-06-01 12:25:08 -0400300 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(pixmap, nullptr));
Greg Daniela4ead652018-02-07 10:21:48 -0500301 if (!mipmaps) {
302 return nullptr;
303 }
304
305 if (mipmaps->countLevels() < 0) {
306 return nullptr;
307 }
308
309 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
310 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
311 // 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 -0400312 SkCopyPixelsMode copyMode = this->recordingDDL() ? kIfMutable_SkCopyPixelsMode
313 : kNever_SkCopyPixelsMode;
Greg Daniela4ead652018-02-07 10:21:48 -0500314 sk_sp<SkImage> baseLevel = SkMakeImageFromRasterBitmap(bitmap, copyMode);
315
316 if (!baseLevel) {
317 return nullptr;
318 }
319
Brian Osman2b23c4b2018-06-01 12:25:08 -0400320 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(pixmap.info());
Greg Daniela4ead652018-02-07 10:21:48 -0500321
322 if (0 == mipmaps->countLevels()) {
Brian Salomon58389b92018-03-07 13:01:25 -0500323 return this->createTextureProxy(baseLevel, kNone_GrSurfaceFlags, 1, SkBudgeted::kYes,
324 SkBackingFit::kExact);
Greg Daniela4ead652018-02-07 10:21:48 -0500325 }
326
327 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Osman2b23c4b2018-06-01 12:25:08 -0400328 [desc, baseLevel, mipmaps](GrResourceProvider* resourceProvider) {
Greg Daniela4ead652018-02-07 10:21:48 -0500329 if (!resourceProvider) {
330 return sk_sp<GrTexture>();
331 }
332
333 const int mipLevelCount = mipmaps->countLevels() + 1;
334 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
335
336 SkPixmap pixmap;
337 SkAssertResult(baseLevel->peekPixels(&pixmap));
338
339 // DDL TODO: Instead of copying all this info into GrMipLevels we should just plumb
340 // the use of SkMipMap down through Ganesh.
341 texels[0].fPixels = pixmap.addr();
342 texels[0].fRowBytes = pixmap.rowBytes();
343
344 for (int i = 1; i < mipLevelCount; ++i) {
345 SkMipMap::Level generatedMipLevel;
346 mipmaps->getLevel(i - 1, &generatedMipLevel);
347 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
348 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
349 SkASSERT(texels[i].fPixels);
350 }
351
Brian Salomon58389b92018-03-07 13:01:25 -0500352 return resourceProvider->createTexture(desc, SkBudgeted::kYes, texels.get(),
Brian Osman2b23c4b2018-06-01 12:25:08 -0400353 mipLevelCount);
Brian Salomon2a4f9832018-03-03 22:43:43 -0500354 },
355 desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kYes, SkBackingFit::kExact,
356 SkBudgeted::kYes);
Greg Daniela4ead652018-02-07 10:21:48 -0500357
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400358 if (!proxy) {
359 return nullptr;
360 }
361
Greg Daniela4ead652018-02-07 10:21:48 -0500362 if (fResourceProvider) {
363 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
364 // we're better off instantiating the proxy immediately here.
365 if (!proxy->priv().doLazyInstantiation(fResourceProvider)) {
366 return nullptr;
367 }
368 }
369 return proxy;
370}
371
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500372sk_sp<GrTextureProxy> GrProxyProvider::createProxy(const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500373 GrSurfaceOrigin origin,
Greg Danielf6f7b672018-02-15 13:06:26 -0500374 GrMipMapped mipMapped,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500375 SkBackingFit fit,
376 SkBudgeted budgeted,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400377 GrInternalSurfaceFlags surfaceFlags) {
Greg Danielf6f7b672018-02-15 13:06:26 -0500378 if (GrMipMapped::kYes == mipMapped) {
379 // SkMipMap doesn't include the base level in the level count so we have to add 1
380 int mipCount = SkMipMap::ComputeLevelCount(desc.fWidth, desc.fHeight) + 1;
381 if (1 == mipCount) {
382 mipMapped = GrMipMapped::kNo;
383 }
384 }
385
386 if (!this->caps()->validateSurfaceDesc(desc, mipMapped)) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500387 return nullptr;
388 }
Brian Salomon3a2cc2c2018-02-03 00:25:12 +0000389 GrSurfaceDesc copyDesc = desc;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500390 if (desc.fFlags & kRenderTarget_GrSurfaceFlag) {
391 copyDesc.fSampleCnt =
392 this->caps()->getRenderTargetSampleCount(desc.fSampleCnt, desc.fConfig);
393 }
Brian Salomon3a2cc2c2018-02-03 00:25:12 +0000394
Brian Salomonbdecacf2018-02-02 20:32:49 -0500395 if (copyDesc.fFlags & kRenderTarget_GrSurfaceFlag) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500396 // We know anything we instantiate later from this deferred path will be
397 // both texturable and renderable
Brian Salomon2a4f9832018-03-03 22:43:43 -0500398 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400399 *this->caps(), copyDesc, origin, mipMapped, fit, budgeted, surfaceFlags));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500400 }
401
Brian Salomon2a4f9832018-03-03 22:43:43 -0500402 return sk_sp<GrTextureProxy>(
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400403 new GrTextureProxy(copyDesc, origin, mipMapped, fit, budgeted, surfaceFlags));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500404}
405
Brian Salomon7578f3e2018-03-07 14:39:54 -0500406sk_sp<GrTextureProxy> GrProxyProvider::wrapBackendTexture(const GrBackendTexture& backendTex,
407 GrSurfaceOrigin origin,
408 GrWrapOwnership ownership,
409 ReleaseProc releaseProc,
410 ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500411 if (this->isAbandoned()) {
412 return nullptr;
413 }
414
Brian Salomonf7778972018-03-08 10:13:17 -0500415 // This is only supported on a direct GrContext.
416 if (!fResourceProvider) {
417 return nullptr;
418 }
419
420 sk_sp<GrTexture> tex = fResourceProvider->wrapBackendTexture(backendTex, ownership);
421 if (!tex) {
422 return nullptr;
423 }
Robert Phillipsadbe1322018-01-17 13:35:46 -0500424
Greg Daniel6a0176b2018-01-30 09:28:44 -0500425 sk_sp<GrReleaseProcHelper> releaseHelper;
426 if (releaseProc) {
427 releaseHelper.reset(new GrReleaseProcHelper(releaseProc, releaseCtx));
Brian Salomonf7778972018-03-08 10:13:17 -0500428 // This gives the texture a ref on the releaseHelper
429 tex->setRelease(releaseHelper);
Greg Daniel6a0176b2018-01-30 09:28:44 -0500430 }
431
Brian Salomonf7778972018-03-08 10:13:17 -0500432 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
433 // Make sure we match how we created the proxy with SkBudgeted::kNo
434 SkASSERT(SkBudgeted::kNo == tex->resourcePriv().isBudgeted());
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500435
Brian Salomonf7778972018-03-08 10:13:17 -0500436 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500437}
438
Brian Salomon7578f3e2018-03-07 14:39:54 -0500439sk_sp<GrTextureProxy> GrProxyProvider::wrapRenderableBackendTexture(
Brian Salomon02bd2952018-03-07 15:20:21 -0500440 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt,
441 GrWrapOwnership ownership) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500442 if (this->isAbandoned()) {
443 return nullptr;
444 }
445
Brian Salomonf7778972018-03-08 10:13:17 -0500446 // This is only supported on a direct GrContext.
447 if (!fResourceProvider) {
448 return nullptr;
449 }
450
Greg Daniel6abda432018-02-15 14:55:00 -0500451 sampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config());
Greg Danielf87651e2018-02-21 11:36:53 -0500452 if (!sampleCnt) {
453 return nullptr;
454 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500455
Brian Salomonf7778972018-03-08 10:13:17 -0500456 sk_sp<GrTexture> tex =
457 fResourceProvider->wrapRenderableBackendTexture(backendTex, sampleCnt, ownership);
458 if (!tex) {
459 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500460 }
461
Brian Salomonf7778972018-03-08 10:13:17 -0500462 SkASSERT(tex->asRenderTarget()); // A GrTextureRenderTarget
463 // Make sure we match how we created the proxy with SkBudgeted::kNo
464 SkASSERT(SkBudgeted::kNo == tex->resourcePriv().isBudgeted());
Greg Daniel6abda432018-02-15 14:55:00 -0500465
Brian Salomonf7778972018-03-08 10:13:17 -0500466 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500467}
468
Brian Salomon7578f3e2018-03-07 14:39:54 -0500469sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget(
470 const GrBackendRenderTarget& backendRT, GrSurfaceOrigin origin) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500471 if (this->isAbandoned()) {
472 return nullptr;
473 }
474
Brian Salomonf7778972018-03-08 10:13:17 -0500475 // This is only supported on a direct GrContext.
476 if (!fResourceProvider) {
477 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500478 }
479
Brian Salomonf7778972018-03-08 10:13:17 -0500480 sk_sp<GrRenderTarget> rt = fResourceProvider->wrapBackendRenderTarget(backendRT);
481 if (!rt) {
482 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500483 }
Brian Salomonf7778972018-03-08 10:13:17 -0500484 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
485 SkASSERT(!rt->getUniqueKey().isValid());
486 // Make sure we match how we created the proxy with SkBudgeted::kNo
487 SkASSERT(SkBudgeted::kNo == rt->resourcePriv().isBudgeted());
488
489 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(std::move(rt), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500490}
491
Brian Salomon7578f3e2018-03-07 14:39:54 -0500492sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendTextureAsRenderTarget(
493 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500494 if (this->isAbandoned()) {
495 return nullptr;
496 }
497
Brian Salomonf7778972018-03-08 10:13:17 -0500498 // This is only supported on a direct GrContext.
499 if (!fResourceProvider) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500500 return nullptr;
501 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500502
Brian Salomonf7778972018-03-08 10:13:17 -0500503 sk_sp<GrRenderTarget> rt =
504 fResourceProvider->wrapBackendTextureAsRenderTarget(backendTex, sampleCnt);
505 if (!rt) {
506 return nullptr;
Greg Danielf87651e2018-02-21 11:36:53 -0500507 }
Brian Salomonf7778972018-03-08 10:13:17 -0500508 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
509 SkASSERT(!rt->getUniqueKey().isValid());
510 // Make sure we match how we created the proxy with SkBudgeted::kNo
511 SkASSERT(SkBudgeted::kNo == rt->resourcePriv().isBudgeted());
Greg Danielf87651e2018-02-21 11:36:53 -0500512
Brian Salomonf7778972018-03-08 10:13:17 -0500513 return sk_sp<GrSurfaceProxy>(new GrRenderTargetProxy(std::move(rt), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500514}
515
Robert Phillips777707b2018-01-17 11:40:14 -0500516sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
517 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500518 GrSurfaceOrigin origin,
519 GrMipMapped mipMapped, SkBackingFit fit,
520 SkBudgeted budgeted) {
521 return this->createLazyProxy(std::move(callback), desc, origin, mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400522 GrInternalSurfaceFlags::kNone, fit, budgeted);
Greg Daniel2a303902018-02-20 10:25:54 -0500523}
524
525sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
526 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500527 GrSurfaceOrigin origin,
Greg Daniel2a303902018-02-20 10:25:54 -0500528 GrMipMapped mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400529 GrInternalSurfaceFlags surfaceFlags,
Greg Daniel2a303902018-02-20 10:25:54 -0500530 SkBackingFit fit, SkBudgeted budgeted) {
Greg Daniela8d92112018-03-09 12:05:04 -0500531 // For non-ddl draws always make lazy proxy's single use.
532 LazyInstantiationType lazyType = fResourceProvider ? LazyInstantiationType::kSingleUse
533 : LazyInstantiationType::kMultipleUse;
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400534 return this->createLazyProxy(std::move(callback), desc, origin, mipMapped, surfaceFlags,
Greg Daniela8d92112018-03-09 12:05:04 -0500535 fit, budgeted, lazyType);
536}
537
538sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
539 const GrSurfaceDesc& desc,
540 GrSurfaceOrigin origin,
541 GrMipMapped mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400542 GrInternalSurfaceFlags surfaceFlags,
Greg Daniela8d92112018-03-09 12:05:04 -0500543 SkBackingFit fit, SkBudgeted budgeted,
544 LazyInstantiationType lazyType) {
Robert Phillips777707b2018-01-17 11:40:14 -0500545 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
546 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400547
548 if (desc.fWidth > fCaps->maxTextureSize() || desc.fHeight > fCaps->maxTextureSize()) {
549 return nullptr;
550 }
551
Greg Daniel457469c2018-02-08 15:05:44 -0500552
Greg Daniel2a303902018-02-20 10:25:54 -0500553#ifdef SK_DEBUG
554 if (SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags)) {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400555 if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kMixedSampled)) {
Greg Daniel2a303902018-02-20 10:25:54 -0500556 SkASSERT(fCaps->usesMixedSamples() && desc.fSampleCnt > 1);
557 }
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400558 if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kWindowRectsSupport)) {
Greg Daniel2a303902018-02-20 10:25:54 -0500559 SkASSERT(fCaps->maxWindowRectangles() > 0);
560 }
561 }
562#endif
563
Brian Salomon2a4f9832018-03-03 22:43:43 -0500564 return sk_sp<GrTextureProxy>(
565 SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags)
566 ? new GrTextureRenderTargetProxy(std::move(callback), lazyType, desc, origin,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400567 mipMapped, fit, budgeted, surfaceFlags)
Brian Salomon2a4f9832018-03-03 22:43:43 -0500568 : new GrTextureProxy(std::move(callback), lazyType, desc, origin, mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400569 fit, budgeted, surfaceFlags));
Robert Phillips777707b2018-01-17 11:40:14 -0500570}
571
Robert Phillipse8fabb22018-02-04 14:33:21 -0500572sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy(
Brian Salomon2a4f9832018-03-03 22:43:43 -0500573 LazyInstantiateCallback&& callback, const GrSurfaceDesc& desc, GrSurfaceOrigin origin,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400574 GrInternalSurfaceFlags surfaceFlags, Textureable textureable, GrMipMapped mipMapped,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500575 SkBackingFit fit, SkBudgeted budgeted) {
Robert Phillipse8fabb22018-02-04 14:33:21 -0500576 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
577 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400578
579 if (desc.fWidth > fCaps->maxRenderTargetSize() || desc.fHeight > fCaps->maxRenderTargetSize()) {
580 return nullptr;
581 }
582
Greg Daniel2a303902018-02-20 10:25:54 -0500583 SkASSERT(SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags));
Greg Daniel457469c2018-02-08 15:05:44 -0500584
Greg Daniel2a303902018-02-20 10:25:54 -0500585#ifdef SK_DEBUG
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400586 if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kMixedSampled)) {
Greg Daniel2a303902018-02-20 10:25:54 -0500587 SkASSERT(fCaps->usesMixedSamples() && desc.fSampleCnt > 1);
588 }
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400589 if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kWindowRectsSupport)) {
Greg Daniel2a303902018-02-20 10:25:54 -0500590 SkASSERT(fCaps->maxWindowRectangles() > 0);
591 }
592#endif
593
Greg Daniel457469c2018-02-08 15:05:44 -0500594 using LazyInstantiationType = GrSurfaceProxy::LazyInstantiationType;
595 // For non-ddl draws always make lazy proxy's single use.
596 LazyInstantiationType lazyType = fResourceProvider ? LazyInstantiationType::kSingleUse
597 : LazyInstantiationType::kMultipleUse;
598
Robert Phillipse8fabb22018-02-04 14:33:21 -0500599 if (Textureable::kYes == textureable) {
Brian Salomon2a4f9832018-03-03 22:43:43 -0500600 return sk_sp<GrRenderTargetProxy>(
601 new GrTextureRenderTargetProxy(std::move(callback), lazyType, desc, origin,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400602 mipMapped, fit, budgeted, surfaceFlags));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500603 }
604
Brian Salomon2a4f9832018-03-03 22:43:43 -0500605 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400606 std::move(callback), lazyType, desc, origin, fit, budgeted, surfaceFlags));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500607}
608
Chris Dalton4c458b12018-06-16 17:22:59 -0600609sk_sp<GrTextureProxy> GrProxyProvider::MakeFullyLazyProxy(LazyInstantiateCallback&& callback,
610 Renderable renderable,
611 GrSurfaceOrigin origin,
612 GrPixelConfig config,
613 const GrCaps& caps) {
Robert Phillips777707b2018-01-17 11:40:14 -0500614 GrSurfaceDesc desc;
Greg Daniele3204862018-04-16 11:24:10 -0400615 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNoPendingIO;
Robert Phillips777707b2018-01-17 11:40:14 -0500616 if (Renderable::kYes == renderable) {
617 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Chris Dalton4c458b12018-06-16 17:22:59 -0600618 if (caps.maxWindowRectangles() > 0) {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400619 surfaceFlags |= GrInternalSurfaceFlags::kWindowRectsSupport;
Greg Daniel2a303902018-02-20 10:25:54 -0500620 }
Robert Phillips777707b2018-01-17 11:40:14 -0500621 }
Robert Phillips777707b2018-01-17 11:40:14 -0500622 desc.fWidth = -1;
623 desc.fHeight = -1;
624 desc.fConfig = config;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500625 desc.fSampleCnt = 1;
Robert Phillips777707b2018-01-17 11:40:14 -0500626
Chris Dalton4c458b12018-06-16 17:22:59 -0600627 return sk_sp<GrTextureProxy>(
628 (Renderable::kYes == renderable)
629 ? new GrTextureRenderTargetProxy(std::move(callback),
630 LazyInstantiationType::kSingleUse, desc,
631 origin, GrMipMapped::kNo,
632 SkBackingFit::kApprox, SkBudgeted::kYes,
633 surfaceFlags)
634 : new GrTextureProxy(std::move(callback), LazyInstantiationType::kSingleUse,
635 desc, origin, GrMipMapped::kNo, SkBackingFit::kApprox,
636 SkBudgeted::kYes, surfaceFlags));
Robert Phillips777707b2018-01-17 11:40:14 -0500637}
638
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500639bool GrProxyProvider::IsFunctionallyExact(GrSurfaceProxy* proxy) {
Robert Phillipsdb3b9792018-06-12 15:18:00 -0400640 const bool isInstantiated = proxy->priv().isInstantiated();
641 // A proxy is functionally exact if:
642 // it is exact (obvs)
643 // when it is instantiated it will be exact (i.e., power of two dimensions)
644 // it is already instantiated and the proxy covers the entire backing surface
645 return proxy->priv().isExact() ||
646 (!isInstantiated && SkIsPow2(proxy->width()) && SkIsPow2(proxy->height())) ||
647 (isInstantiated && proxy->worstCaseWidth() == proxy->width() &&
648 proxy->worstCaseHeight() == proxy->height());
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500649}
650
651void GrProxyProvider::processInvalidProxyUniqueKey(const GrUniqueKey& key) {
652 // Note: this method is called for the whole variety of GrGpuResources so often 'key'
653 // will not be in 'fUniquelyKeyedProxies'.
654 GrTextureProxy* proxy = fUniquelyKeyedProxies.find(key);
655 if (proxy) {
656 this->processInvalidProxyUniqueKey(key, proxy, false);
657 }
658}
659
660void GrProxyProvider::processInvalidProxyUniqueKey(const GrUniqueKey& key, GrTextureProxy* proxy,
661 bool invalidateSurface) {
662 SkASSERT(proxy);
663 SkASSERT(proxy->getUniqueKey().isValid());
664 SkASSERT(proxy->getUniqueKey() == key);
665
666 fUniquelyKeyedProxies.remove(key);
667 proxy->cacheAccess().clearUniqueKey();
668
669 if (invalidateSurface && proxy->priv().isInstantiated()) {
670 GrSurface* surface = proxy->priv().peekSurface();
671 if (surface) {
672 surface->resourcePriv().removeUniqueKey();
673 }
674 }
675}
676
677void GrProxyProvider::removeAllUniqueKeys() {
678 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
679 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
680 GrTextureProxy& tmp = *iter;
681
682 this->processInvalidProxyUniqueKey(tmp.getUniqueKey(), &tmp, false);
683 }
684 SkASSERT(!fUniquelyKeyedProxies.count());
685}