blob: ec05243d189a33c62d91fc2f64b782f835f496bb [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 Daniel9d86f1d2018-01-29 09:33:59 -050020#include "SkGr.h"
21#include "SkImage.h"
22#include "SkImage_Base.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050023#include "SkMipMap.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050024
25#define ASSERT_SINGLE_OWNER \
26 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fSingleOwner);)
27
28GrProxyProvider::GrProxyProvider(GrResourceProvider* resourceProvider,
29 GrResourceCache* resourceCache,
30 sk_sp<const GrCaps> caps,
31 GrSingleOwner* owner)
32 : fResourceProvider(resourceProvider)
33 , fResourceCache(resourceCache)
Robert Phillips4d120512018-01-19 13:22:07 -050034 , fAbandoned(false)
Robert Phillips1afd4cd2018-01-08 13:40:32 -050035 , fCaps(caps)
36#ifdef SK_DEBUG
37 , fSingleOwner(owner)
38#endif
39{
40
41}
42
43GrProxyProvider::~GrProxyProvider() {
44 SkASSERT(!fUniquelyKeyedProxies.count());
45}
46
Robert Phillipsadbe1322018-01-17 13:35:46 -050047bool GrProxyProvider::assignUniqueKeyToProxy(const GrUniqueKey& key, GrTextureProxy* proxy) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050048 ASSERT_SINGLE_OWNER
49 SkASSERT(key.isValid());
50 if (this->isAbandoned() || !proxy) {
Robert Phillipsadbe1322018-01-17 13:35:46 -050051 return false;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050052 }
53
54 // If there is already a GrResource with this key then the caller has violated the normal
55 // usage pattern of uniquely keyed resources (e.g., they have created one w/o first seeing
56 // if it already existed in the cache).
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -050057 SkASSERT(!fResourceCache || !fResourceCache->findAndRefUniqueResource(key));
Robert Phillips1afd4cd2018-01-08 13:40:32 -050058
59 // Uncached resources can never have a unique key, unless they're wrapped resources. Wrapped
60 // resources are a special case: the unique keys give us a weak ref so that we can reuse the
61 // same resource (rather than re-wrapping). When a wrapped resource is no longer referenced,
62 // it will always be released - it is never converted to a scratch resource.
63 if (SkBudgeted::kNo == proxy->isBudgeted() &&
64 (!proxy->priv().isInstantiated() ||
65 !proxy->priv().peekSurface()->resourcePriv().refsWrappedObjects())) {
Robert Phillipsadbe1322018-01-17 13:35:46 -050066 return false;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050067 }
68
69 SkASSERT(!fUniquelyKeyedProxies.find(key)); // multiple proxies can't get the same key
70
71 proxy->cacheAccess().setUniqueKey(this, key);
72 SkASSERT(proxy->getUniqueKey() == key);
73 fUniquelyKeyedProxies.add(proxy);
Robert Phillipsadbe1322018-01-17 13:35:46 -050074 return true;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050075}
76
77void GrProxyProvider::adoptUniqueKeyFromSurface(GrTextureProxy* proxy, const GrSurface* surf) {
78 SkASSERT(surf->getUniqueKey().isValid());
79 proxy->cacheAccess().setUniqueKey(this, surf->getUniqueKey());
80 SkASSERT(proxy->getUniqueKey() == surf->getUniqueKey());
81 // multiple proxies can't get the same key
82 SkASSERT(!fUniquelyKeyedProxies.find(surf->getUniqueKey()));
83 fUniquelyKeyedProxies.add(proxy);
84}
85
86void GrProxyProvider::removeUniqueKeyFromProxy(const GrUniqueKey& key, GrTextureProxy* proxy) {
87 ASSERT_SINGLE_OWNER
88 if (this->isAbandoned() || !proxy) {
89 return;
90 }
91 this->processInvalidProxyUniqueKey(key, proxy, true);
92}
93
94sk_sp<GrTextureProxy> GrProxyProvider::findProxyByUniqueKey(const GrUniqueKey& key,
95 GrSurfaceOrigin origin) {
96 ASSERT_SINGLE_OWNER
97
98 if (this->isAbandoned()) {
99 return nullptr;
100 }
101
102 sk_sp<GrTextureProxy> result = sk_ref_sp(fUniquelyKeyedProxies.find(key));
103 if (result) {
104 SkASSERT(result->origin() == origin);
105 }
106 return result;
107}
108
Robert Phillipsadbe1322018-01-17 13:35:46 -0500109sk_sp<GrTextureProxy> GrProxyProvider::createWrapped(sk_sp<GrTexture> tex, GrSurfaceOrigin origin) {
110#ifdef SK_DEBUG
111 if (tex->getUniqueKey().isValid()) {
112 SkASSERT(!this->findProxyByUniqueKey(tex->getUniqueKey(), origin));
113 }
114#endif
115
116 if (tex->asRenderTarget()) {
117 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin));
118 } else {
119 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin));
120 }
121}
122
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500123sk_sp<GrTextureProxy> GrProxyProvider::findOrCreateProxyByUniqueKey(const GrUniqueKey& key,
124 GrSurfaceOrigin origin) {
125 ASSERT_SINGLE_OWNER
126
127 if (this->isAbandoned()) {
128 return nullptr;
129 }
130
131 sk_sp<GrTextureProxy> result = this->findProxyByUniqueKey(key, origin);
132 if (result) {
133 return result;
134 }
135
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500136 if (!fResourceCache) {
137 return nullptr;
138 }
139
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500140 GrGpuResource* resource = fResourceCache->findAndRefUniqueResource(key);
141 if (!resource) {
142 return nullptr;
143 }
144
145 sk_sp<GrTexture> texture(static_cast<GrSurface*>(resource)->asTexture());
146 SkASSERT(texture);
147
Robert Phillipsadbe1322018-01-17 13:35:46 -0500148 result = this->createWrapped(std::move(texture), origin);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500149 SkASSERT(result->getUniqueKey() == key);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500150 // createWrapped should've added this for us
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500151 SkASSERT(fUniquelyKeyedProxies.find(key));
152 return result;
153}
154
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500155sk_sp<GrTextureProxy> GrProxyProvider::createInstantiatedProxy(const GrSurfaceDesc& desc,
156 SkBackingFit fit,
157 SkBudgeted budgeted,
158 uint32_t flags) {
159 sk_sp<GrTexture> tex;
160
161 if (SkBackingFit::kApprox == fit) {
162 tex = fResourceProvider->createApproxTexture(desc, flags);
163 } else {
164 tex = fResourceProvider->createTexture(desc, budgeted, flags);
165 }
166 if (!tex) {
167 return nullptr;
168 }
169
Robert Phillipsadbe1322018-01-17 13:35:46 -0500170 return this->createWrapped(std::move(tex), desc.fOrigin);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500171}
172
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500173sk_sp<GrTextureProxy> GrProxyProvider::createTextureProxy(const GrSurfaceDesc& desc,
174 SkBudgeted budgeted,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500175 const void* srcData, size_t rowBytes) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500176 ASSERT_SINGLE_OWNER
177
Robert Phillips579f0942018-01-08 14:53:35 -0500178 if (this->isAbandoned()) {
179 return nullptr;
180 }
181
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500182 if (srcData) {
183 GrMipLevel mipLevel = { srcData, rowBytes };
184
185 sk_sp<GrTexture> tex = fResourceProvider->createTexture(desc, budgeted, mipLevel);
186 if (!tex) {
187 return nullptr;
188 }
189
Robert Phillipsadbe1322018-01-17 13:35:46 -0500190 return this->createWrapped(std::move(tex), desc.fOrigin);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500191 }
192
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500193 return this->createProxy(desc, SkBackingFit::kExact, budgeted);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500194}
195
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500196sk_sp<GrTextureProxy> GrProxyProvider::createTextureProxy(sk_sp<SkImage> srcImage,
197 GrSurfaceFlags flags,
198 GrSurfaceOrigin origin,
199 int sampleCnt,
200 SkBudgeted budgeted) {
201 ASSERT_SINGLE_OWNER
202 SkASSERT(srcImage);
203
204 if (this->isAbandoned()) {
205 return nullptr;
206 }
207
208 GrSurfaceDesc desc;
209 desc.fWidth = srcImage->width();
210 desc.fHeight = srcImage->height();
211 desc.fFlags = flags;
212 desc.fOrigin = origin;
213 desc.fSampleCnt = sampleCnt;
214 desc.fConfig = SkImageInfo2GrPixelConfig(as_IB(srcImage)->onImageInfo(), *this->caps());
215
216 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
217 [desc, budgeted, srcImage]
218 (GrResourceProvider* resourceProvider, GrSurfaceOrigin* /*outOrigin*/) {
219 if (!resourceProvider) {
Greg Daniel0a375db2018-02-01 12:21:39 -0500220 // Nothing to clean up here. Once the proxy (and thus lambda) is deleted the ref
221 // on srcImage will be released.
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500222 return sk_sp<GrTexture>();
223 }
224 SkPixmap pixMap;
225 SkAssertResult(srcImage->peekPixels(&pixMap));
226 GrMipLevel mipLevel = { pixMap.addr(), pixMap.rowBytes() };
227
228 return resourceProvider->createTexture(desc, budgeted, mipLevel);
229 }, desc, GrMipMapped::kNo, SkBackingFit::kExact, budgeted);
230
231 if (fResourceProvider) {
232 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
233 // we're better off instantiating the proxy immediately here.
234 if (!proxy->priv().doLazyInstantiation(fResourceProvider)) {
235 return nullptr;
236 }
237 }
238 return proxy;
239}
240
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500241sk_sp<GrTextureProxy> GrProxyProvider::createMipMapProxy(
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500242 const GrSurfaceDesc& desc, SkBudgeted budgeted,
243 const GrMipLevel texels[], int mipLevelCount,
244 SkDestinationSurfaceColorMode mipColorMode) {
Robert Phillips579f0942018-01-08 14:53:35 -0500245 ASSERT_SINGLE_OWNER
246
247 if (this->isAbandoned()) {
248 return nullptr;
249 }
250
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500251 if (!mipLevelCount) {
252 if (texels) {
253 return nullptr;
254 }
255 return this->createProxy(desc, SkBackingFit::kExact, budgeted);
256 }
257 if (!texels) {
258 return nullptr;
259 }
260
261 if (1 == mipLevelCount) {
262 return this->createTextureProxy(desc, budgeted, texels[0].fPixels, texels[0].fRowBytes);
263 }
264
265#ifdef SK_DEBUG
266 // There are only three states we want to be in when uploading data to a mipped surface.
267 // 1) We have data to upload to all layers
268 // 2) We are not uploading data to any layers
269 // 3) We are only uploading data to the base layer
270 // We check here to make sure we do not have any other state.
271 bool firstLevelHasData = SkToBool(texels[0].fPixels);
272 bool allOtherLevelsHaveData = true, allOtherLevelsLackData = true;
273 for (int i = 1; i < mipLevelCount; ++i) {
274 if (texels[i].fPixels) {
275 allOtherLevelsLackData = false;
276 } else {
277 allOtherLevelsHaveData = false;
278 }
279 }
280 SkASSERT((firstLevelHasData && allOtherLevelsHaveData) || allOtherLevelsLackData);
281#endif
282
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500283 sk_sp<GrTexture> tex(fResourceProvider->createTexture(desc, budgeted,
284 texels, mipLevelCount,
285 mipColorMode));
286 if (!tex) {
287 return nullptr;
288 }
289
Robert Phillipsadbe1322018-01-17 13:35:46 -0500290 return this->createWrapped(std::move(tex), desc.fOrigin);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500291}
292
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500293sk_sp<GrTextureProxy> GrProxyProvider::createMipMapProxy(const GrSurfaceDesc& desc,
294 SkBudgeted budgeted) {
295 // SkMipMap doesn't include the base level in the level count so we have to add 1
296 int mipCount = SkMipMap::ComputeLevelCount(desc.fWidth, desc.fHeight) + 1;
297
298 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipCount]);
299
300 // We don't want to upload any texel data
301 for (int i = 0; i < mipCount; i++) {
302 texels[i].fPixels = nullptr;
303 texels[i].fRowBytes = 0;
304 }
305
306 return this->createMipMapProxy(desc, budgeted, texels.get(), mipCount,
307 SkDestinationSurfaceColorMode::kLegacy);
308}
309
310sk_sp<GrTextureProxy> GrProxyProvider::createProxy(const GrSurfaceDesc& desc,
311 SkBackingFit fit,
312 SkBudgeted budgeted,
313 uint32_t flags) {
314 SkASSERT(0 == flags || GrResourceProvider::kNoPendingIO_Flag == flags);
315
316 const GrCaps* caps = this->caps();
317
318 // TODO: move this logic into GrResourceProvider!
319 // TODO: share this testing code with check_texture_creation_params
320 if (!caps->isConfigTexturable(desc.fConfig)) {
321 return nullptr;
322 }
323
324 bool willBeRT = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
Brian Salomonc1ce2f72018-02-01 18:34:32 +0000325 if (willBeRT && !caps->isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500326 return nullptr;
327 }
328
329 // We currently do not support multisampled textures
Brian Salomonc1ce2f72018-02-01 18:34:32 +0000330 if (!willBeRT && desc.fSampleCnt > 0) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500331 return nullptr;
332 }
333
334 int maxSize;
335 if (willBeRT) {
336 maxSize = caps->maxRenderTargetSize();
337 } else {
338 maxSize = caps->maxTextureSize();
339 }
340
341 if (desc.fWidth > maxSize || desc.fHeight > maxSize || desc.fWidth <= 0 || desc.fHeight <= 0) {
342 return nullptr;
343 }
344
345 GrSurfaceDesc copyDesc = desc;
346 copyDesc.fSampleCnt = caps->getSampleCount(desc.fSampleCnt, desc.fConfig);
347
348#ifdef SK_DISABLE_DEFERRED_PROXIES
349 // Temporarily force instantiation for crbug.com/769760 and crbug.com/769898
350 sk_sp<GrTexture> tex;
351
352 if (SkBackingFit::kApprox == fit) {
353 tex = resourceProvider->createApproxTexture(copyDesc, flags);
354 } else {
355 tex = resourceProvider->createTexture(copyDesc, budgeted, flags);
356 }
357
358 if (!tex) {
359 return nullptr;
360 }
361
362 return GrSurfaceProxy::MakeWrapped(std::move(tex), copyDesc.fOrigin);
363#else
364 if (willBeRT) {
365 // We know anything we instantiate later from this deferred path will be
366 // both texturable and renderable
367 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(*caps, copyDesc, fit,
368 budgeted, flags));
369 }
370
371 return sk_sp<GrTextureProxy>(new GrTextureProxy(copyDesc, fit, budgeted, nullptr, 0, flags));
372#endif
373}
374
Robert Phillipsadbe1322018-01-17 13:35:46 -0500375sk_sp<GrTextureProxy> GrProxyProvider::createWrappedTextureProxy(
376 const GrBackendTexture& backendTex,
377 GrSurfaceOrigin origin,
378 GrWrapOwnership ownership,
379 ReleaseProc releaseProc,
380 ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500381 if (this->isAbandoned()) {
382 return nullptr;
383 }
384
Greg Danielf2336e42018-01-23 16:38:14 -0500385 GrSurfaceDesc desc;
386 desc.fOrigin = origin;
387 desc.fWidth = backendTex.width();
388 desc.fHeight = backendTex.height();
389 desc.fConfig = backendTex.config();
390 GrMipMapped mipMapped = backendTex.hasMipMaps() ? GrMipMapped::kYes : GrMipMapped::kNo;
Robert Phillipsadbe1322018-01-17 13:35:46 -0500391
Greg Daniel6a0176b2018-01-30 09:28:44 -0500392 sk_sp<GrReleaseProcHelper> releaseHelper;
393 if (releaseProc) {
394 releaseHelper.reset(new GrReleaseProcHelper(releaseProc, releaseCtx));
395 }
396
Greg Danielf2336e42018-01-23 16:38:14 -0500397 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Greg Daniel6a0176b2018-01-30 09:28:44 -0500398 [backendTex, ownership, releaseHelper]
Greg Danielf2336e42018-01-23 16:38:14 -0500399 (GrResourceProvider* resourceProvider, GrSurfaceOrigin* /*outOrigin*/) {
400 if (!resourceProvider) {
Greg Daniel0a375db2018-02-01 12:21:39 -0500401 // If this had a releaseHelper it will get unrefed when we delete this lambda
402 // and will call the release proc so that the client knows they can free the
403 // underlying backend object.
Greg Danielf2336e42018-01-23 16:38:14 -0500404 return sk_sp<GrTexture>();
405 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500406
Greg Danielf2336e42018-01-23 16:38:14 -0500407 sk_sp<GrTexture> tex = resourceProvider->wrapBackendTexture(backendTex,
408 ownership);
409 if (!tex) {
410 return sk_sp<GrTexture>();
411 }
Greg Daniel6a0176b2018-01-30 09:28:44 -0500412 if (releaseHelper) {
Greg Daniel0a375db2018-02-01 12:21:39 -0500413 // This gives the texture a ref on the releaseHelper
414 tex->setRelease(releaseHelper);
Greg Danielf2336e42018-01-23 16:38:14 -0500415 }
416 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
417 // Make sure we match how we created the proxy with SkBudgeted::kNo
418 SkASSERT(SkBudgeted::kNo == tex->resourcePriv().isBudgeted());
419
420 return tex;
421 }, desc, mipMapped, SkBackingFit::kExact, SkBudgeted::kNo);
422
423 if (fResourceProvider) {
424 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however,
425 // we're better off instantiating the proxy immediately here.
Greg Danielbddcc952018-01-24 13:22:24 -0500426 if (!proxy->priv().doLazyInstantiation(fResourceProvider)) {
427 return nullptr;
428 }
Greg Danielf2336e42018-01-23 16:38:14 -0500429 }
430 return proxy;
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500431}
432
433sk_sp<GrTextureProxy> GrProxyProvider::createWrappedTextureProxy(const GrBackendTexture& tex,
434 GrSurfaceOrigin origin,
435 int sampleCnt) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500436 if (this->isAbandoned()) {
437 return nullptr;
438 }
439
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500440 sk_sp<GrTexture> texture(fResourceProvider->wrapRenderableBackendTexture(tex, sampleCnt));
441 if (!texture) {
442 return nullptr;
443 }
444 SkASSERT(texture->asRenderTarget()); // A GrTextureRenderTarget
445
Robert Phillipsadbe1322018-01-17 13:35:46 -0500446 return this->createWrapped(std::move(texture), origin);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500447}
448
449sk_sp<GrSurfaceProxy> GrProxyProvider::createWrappedRenderTargetProxy(
450 const GrBackendRenderTarget& backendRT,
451 GrSurfaceOrigin origin) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500452 if (this->isAbandoned()) {
453 return nullptr;
454 }
455
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500456 sk_sp<GrRenderTarget> rt(fResourceProvider->wrapBackendRenderTarget(backendRT));
457 if (!rt) {
458 return nullptr;
459 }
460 SkASSERT(!rt->asTexture()); // Strictly a GrRenderTarget
461 SkASSERT(!rt->getUniqueKey().isValid());
462
463 return sk_sp<GrSurfaceProxy>(new GrRenderTargetProxy(std::move(rt), origin));
464}
465
466sk_sp<GrSurfaceProxy> GrProxyProvider::createWrappedRenderTargetProxy(const GrBackendTexture& tex,
467 GrSurfaceOrigin origin,
468 int sampleCnt) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500469 if (this->isAbandoned()) {
470 return nullptr;
471 }
472
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500473 sk_sp<GrRenderTarget> rt(fResourceProvider->wrapBackendTextureAsRenderTarget(tex, sampleCnt));
474 if (!rt) {
475 return nullptr;
476 }
477 SkASSERT(!rt->asTexture()); // Strictly a GrRenderTarget
478 SkASSERT(!rt->getUniqueKey().isValid());
479
480 return sk_sp<GrSurfaceProxy>(new GrRenderTargetProxy(std::move(rt), origin));
481}
482
Robert Phillips777707b2018-01-17 11:40:14 -0500483sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
484 const GrSurfaceDesc& desc,
485 GrMipMapped mipMapped,
486 SkBackingFit fit, SkBudgeted budgeted) {
487 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
488 (desc.fWidth > 0 && desc.fHeight > 0));
489 uint32_t flags = GrResourceProvider::kNoPendingIO_Flag;
490 return sk_sp<GrTextureProxy>(SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags) ?
491 new GrTextureRenderTargetProxy(std::move(callback), desc,
492 mipMapped, fit, budgeted, flags) :
493 new GrTextureProxy(std::move(callback), desc, mipMapped, fit,
494 budgeted, flags));
Robert Phillips777707b2018-01-17 11:40:14 -0500495}
496
497sk_sp<GrTextureProxy> GrProxyProvider::createFullyLazyProxy(LazyInstantiateCallback&& callback,
498 Renderable renderable,
499 GrPixelConfig config) {
500 GrSurfaceDesc desc;
501 if (Renderable::kYes == renderable) {
502 desc.fFlags = kRenderTarget_GrSurfaceFlag;
503 }
504 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
505 desc.fWidth = -1;
506 desc.fHeight = -1;
507 desc.fConfig = config;
Brian Salomonc1ce2f72018-02-01 18:34:32 +0000508 desc.fSampleCnt = 0;
Robert Phillips777707b2018-01-17 11:40:14 -0500509
510 return this->createLazyProxy(std::move(callback), desc, GrMipMapped::kNo,
511 SkBackingFit::kApprox, SkBudgeted::kYes);
512
513}
514
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500515bool GrProxyProvider::IsFunctionallyExact(GrSurfaceProxy* proxy) {
516 return proxy->priv().isExact() || (SkIsPow2(proxy->width()) && SkIsPow2(proxy->height()));
517}
518
519void GrProxyProvider::processInvalidProxyUniqueKey(const GrUniqueKey& key) {
520 // Note: this method is called for the whole variety of GrGpuResources so often 'key'
521 // will not be in 'fUniquelyKeyedProxies'.
522 GrTextureProxy* proxy = fUniquelyKeyedProxies.find(key);
523 if (proxy) {
524 this->processInvalidProxyUniqueKey(key, proxy, false);
525 }
526}
527
528void GrProxyProvider::processInvalidProxyUniqueKey(const GrUniqueKey& key, GrTextureProxy* proxy,
529 bool invalidateSurface) {
530 SkASSERT(proxy);
531 SkASSERT(proxy->getUniqueKey().isValid());
532 SkASSERT(proxy->getUniqueKey() == key);
533
534 fUniquelyKeyedProxies.remove(key);
535 proxy->cacheAccess().clearUniqueKey();
536
537 if (invalidateSurface && proxy->priv().isInstantiated()) {
538 GrSurface* surface = proxy->priv().peekSurface();
539 if (surface) {
540 surface->resourcePriv().removeUniqueKey();
541 }
542 }
543}
544
545void GrProxyProvider::removeAllUniqueKeys() {
546 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
547 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
548 GrTextureProxy& tmp = *iter;
549
550 this->processInvalidProxyUniqueKey(tmp.getUniqueKey(), &tmp, false);
551 }
552 SkASSERT(!fUniquelyKeyedProxies.count());
553}