blob: f9e63cda86afd0111725adc786b2f57c936cb7c8 [file] [log] [blame]
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001/*
2 * Copyright 2018 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "GrProxyProvider.h"
9
10#include "GrCaps.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050011#include "GrRenderTarget.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050012#include "GrResourceKey.h"
13#include "GrResourceProvider.h"
14#include "GrSurfaceProxy.h"
15#include "GrSurfaceProxyPriv.h"
16#include "GrTexture.h"
17#include "GrTextureProxyCacheAccess.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050018#include "GrTextureRenderTargetProxy.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050019#include "../private/GrSingleOwner.h"
Brian Osmand29dcd12018-09-13 15:04:29 -040020#include "SkAutoPixmapStorage.h"
Greg Daniela4ead652018-02-07 10:21:48 -050021#include "SkBitmap.h"
Greg Daniel9d86f1d2018-01-29 09:33:59 -050022#include "SkGr.h"
23#include "SkImage.h"
24#include "SkImage_Base.h"
Greg Daniela4ead652018-02-07 10:21:48 -050025#include "SkImageInfoPriv.h"
26#include "SkImagePriv.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050027#include "SkMipMap.h"
Greg Daniela4ead652018-02-07 10:21:48 -050028#include "SkTraceEvent.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050029
30#define ASSERT_SINGLE_OWNER \
31 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fSingleOwner);)
32
33GrProxyProvider::GrProxyProvider(GrResourceProvider* resourceProvider,
34 GrResourceCache* resourceCache,
35 sk_sp<const GrCaps> caps,
36 GrSingleOwner* owner)
37 : fResourceProvider(resourceProvider)
38 , fResourceCache(resourceCache)
Robert Phillips4d120512018-01-19 13:22:07 -050039 , fAbandoned(false)
Robert Phillips1afd4cd2018-01-08 13:40:32 -050040 , fCaps(caps)
Brian Salomon238069b2018-07-11 15:58:57 -040041 , fContextUniqueID(resourceCache->contextUniqueID())
Robert Phillips1afd4cd2018-01-08 13:40:32 -050042#ifdef SK_DEBUG
43 , fSingleOwner(owner)
44#endif
45{
Brian Salomon238069b2018-07-11 15:58:57 -040046 SkASSERT(fResourceProvider);
47 SkASSERT(fResourceCache);
48 SkASSERT(fCaps);
49 SkASSERT(fSingleOwner);
50}
Robert Phillips1afd4cd2018-01-08 13:40:32 -050051
Brian Salomon238069b2018-07-11 15:58:57 -040052GrProxyProvider::GrProxyProvider(uint32_t contextUniqueID,
53 sk_sp<const GrCaps> caps,
54 GrSingleOwner* owner)
55 : fResourceProvider(nullptr)
56 , fResourceCache(nullptr)
57 , fAbandoned(false)
58 , fCaps(caps)
59 , fContextUniqueID(contextUniqueID)
60#ifdef SK_DEBUG
61 , fSingleOwner(owner)
62#endif
63{
64 SkASSERT(fContextUniqueID != SK_InvalidUniqueID);
65 SkASSERT(fCaps);
66 SkASSERT(fSingleOwner);
Robert Phillips1afd4cd2018-01-08 13:40:32 -050067}
68
69GrProxyProvider::~GrProxyProvider() {
70 SkASSERT(!fUniquelyKeyedProxies.count());
71}
72
Robert Phillipsadbe1322018-01-17 13:35:46 -050073bool GrProxyProvider::assignUniqueKeyToProxy(const GrUniqueKey& key, GrTextureProxy* proxy) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050074 ASSERT_SINGLE_OWNER
75 SkASSERT(key.isValid());
76 if (this->isAbandoned() || !proxy) {
Robert Phillipsadbe1322018-01-17 13:35:46 -050077 return false;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050078 }
79
80 // If there is already a GrResource with this key then the caller has violated the normal
81 // usage pattern of uniquely keyed resources (e.g., they have created one w/o first seeing
82 // if it already existed in the cache).
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -050083 SkASSERT(!fResourceCache || !fResourceCache->findAndRefUniqueResource(key));
Robert Phillips1afd4cd2018-01-08 13:40:32 -050084
Robert Phillips1afd4cd2018-01-08 13:40:32 -050085 SkASSERT(!fUniquelyKeyedProxies.find(key)); // multiple proxies can't get the same key
86
87 proxy->cacheAccess().setUniqueKey(this, key);
88 SkASSERT(proxy->getUniqueKey() == key);
89 fUniquelyKeyedProxies.add(proxy);
Robert Phillipsadbe1322018-01-17 13:35:46 -050090 return true;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050091}
92
93void GrProxyProvider::adoptUniqueKeyFromSurface(GrTextureProxy* proxy, const GrSurface* surf) {
94 SkASSERT(surf->getUniqueKey().isValid());
95 proxy->cacheAccess().setUniqueKey(this, surf->getUniqueKey());
96 SkASSERT(proxy->getUniqueKey() == surf->getUniqueKey());
97 // multiple proxies can't get the same key
98 SkASSERT(!fUniquelyKeyedProxies.find(surf->getUniqueKey()));
99 fUniquelyKeyedProxies.add(proxy);
100}
101
102void GrProxyProvider::removeUniqueKeyFromProxy(const GrUniqueKey& key, GrTextureProxy* proxy) {
103 ASSERT_SINGLE_OWNER
104 if (this->isAbandoned() || !proxy) {
105 return;
106 }
107 this->processInvalidProxyUniqueKey(key, proxy, true);
108}
109
110sk_sp<GrTextureProxy> GrProxyProvider::findProxyByUniqueKey(const GrUniqueKey& key,
111 GrSurfaceOrigin origin) {
112 ASSERT_SINGLE_OWNER
113
114 if (this->isAbandoned()) {
115 return nullptr;
116 }
117
118 sk_sp<GrTextureProxy> result = sk_ref_sp(fUniquelyKeyedProxies.find(key));
119 if (result) {
120 SkASSERT(result->origin() == origin);
121 }
122 return result;
123}
124
Robert Phillipsadbe1322018-01-17 13:35:46 -0500125sk_sp<GrTextureProxy> GrProxyProvider::createWrapped(sk_sp<GrTexture> tex, GrSurfaceOrigin origin) {
126#ifdef SK_DEBUG
127 if (tex->getUniqueKey().isValid()) {
128 SkASSERT(!this->findProxyByUniqueKey(tex->getUniqueKey(), origin));
129 }
130#endif
131
132 if (tex->asRenderTarget()) {
133 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin));
134 } else {
135 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin));
136 }
137}
138
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500139sk_sp<GrTextureProxy> GrProxyProvider::findOrCreateProxyByUniqueKey(const GrUniqueKey& key,
140 GrSurfaceOrigin origin) {
141 ASSERT_SINGLE_OWNER
142
143 if (this->isAbandoned()) {
144 return nullptr;
145 }
146
147 sk_sp<GrTextureProxy> result = this->findProxyByUniqueKey(key, origin);
148 if (result) {
149 return result;
150 }
151
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500152 if (!fResourceCache) {
153 return nullptr;
154 }
155
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500156 GrGpuResource* resource = fResourceCache->findAndRefUniqueResource(key);
157 if (!resource) {
158 return nullptr;
159 }
160
161 sk_sp<GrTexture> texture(static_cast<GrSurface*>(resource)->asTexture());
162 SkASSERT(texture);
163
Robert Phillipsadbe1322018-01-17 13:35:46 -0500164 result = this->createWrapped(std::move(texture), origin);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500165 SkASSERT(result->getUniqueKey() == key);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500166 // createWrapped should've added this for us
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500167 SkASSERT(fUniquelyKeyedProxies.find(key));
168 return result;
169}
170
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500171sk_sp<GrTextureProxy> GrProxyProvider::createInstantiatedProxy(const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500172 GrSurfaceOrigin origin,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500173 SkBackingFit fit,
174 SkBudgeted budgeted,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400175 GrSurfaceDescFlags descFlags) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500176 sk_sp<GrTexture> tex;
177
178 if (SkBackingFit::kApprox == fit) {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400179 tex = fResourceProvider->createApproxTexture(desc, descFlags);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500180 } else {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400181 tex = fResourceProvider->createTexture(desc, budgeted, descFlags);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500182 }
183 if (!tex) {
184 return nullptr;
185 }
186
Brian Salomon2a4f9832018-03-03 22:43:43 -0500187 return this->createWrapped(std::move(tex), origin);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500188}
189
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500190sk_sp<GrTextureProxy> GrProxyProvider::createTextureProxy(sk_sp<SkImage> srcImage,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400191 GrSurfaceDescFlags descFlags,
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500192 int sampleCnt,
Greg Danielfb3abcd2018-02-02 15:48:33 -0500193 SkBudgeted budgeted,
194 SkBackingFit fit) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500195 ASSERT_SINGLE_OWNER
196 SkASSERT(srcImage);
197
198 if (this->isAbandoned()) {
199 return nullptr;
200 }
201
Brian Osmand29dcd12018-09-13 15:04:29 -0400202 SkImageInfo info = as_IB(srcImage)->onImageInfo();
203 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
Greg Danielf87651e2018-02-21 11:36:53 -0500204
Greg Daniel0a7aa142018-02-21 13:02:32 -0500205 if (kUnknown_GrPixelConfig == config) {
206 return nullptr;
207 }
208
Brian Osmand29dcd12018-09-13 15:04:29 -0400209 if (!this->caps()->isConfigTexturable(config)) {
210 SkBitmap copy8888;
211 if (!copy8888.tryAllocPixels(info.makeColorType(kRGBA_8888_SkColorType)) ||
212 !srcImage->readPixels(copy8888.pixmap(), 0, 0)) {
213 return nullptr;
214 }
215 copy8888.setImmutable();
216 srcImage = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode);
217 config = kRGBA_8888_GrPixelConfig;
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 },
Brian Salomon7226c232018-07-30 13:13:17 -0400257 desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, GrTextureType::k2D, surfaceFlags, fit,
258 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 }
Robert Phillipsc1b60662018-06-26 10:20:08 -0400271
272 SkASSERT(proxy->width() == desc.fWidth);
273 SkASSERT(proxy->height() == desc.fHeight);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500274 return proxy;
275}
276
Greg Daniel30815082018-02-09 16:08:30 -0500277sk_sp<GrTextureProxy> GrProxyProvider::createMipMapProxy(const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500278 GrSurfaceOrigin origin,
Greg Daniel30815082018-02-09 16:08:30 -0500279 SkBudgeted budgeted) {
Robert Phillips579f0942018-01-08 14:53:35 -0500280 ASSERT_SINGLE_OWNER
281
282 if (this->isAbandoned()) {
283 return nullptr;
284 }
285
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400286 return this->createProxy(desc, origin, GrMipMapped::kYes, SkBackingFit::kExact, budgeted,
287 GrInternalSurfaceFlags::kNone);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500288}
289
Brian Osman2b23c4b2018-06-01 12:25:08 -0400290sk_sp<GrTextureProxy> GrProxyProvider::createMipMapProxyFromBitmap(const SkBitmap& bitmap) {
291 if (!SkImageInfoIsValid(bitmap.info())) {
Greg Daniela4ead652018-02-07 10:21:48 -0500292 return nullptr;
293 }
294
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400295 ATRACE_ANDROID_FRAMEWORK("Upload MipMap Texture [%ux%u]", bitmap.width(), bitmap.height());
Greg Daniela4ead652018-02-07 10:21:48 -0500296
297 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
298 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
299 // 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 -0400300 SkCopyPixelsMode copyMode = this->recordingDDL() ? kIfMutable_SkCopyPixelsMode
301 : kNever_SkCopyPixelsMode;
Greg Daniela4ead652018-02-07 10:21:48 -0500302 sk_sp<SkImage> baseLevel = SkMakeImageFromRasterBitmap(bitmap, copyMode);
Greg Daniela4ead652018-02-07 10:21:48 -0500303 if (!baseLevel) {
304 return nullptr;
305 }
306
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400307 // This was never going to have mips anyway
308 if (0 == SkMipMap::ComputeLevelCount(baseLevel->width(), baseLevel->height())) {
Brian Salomon58389b92018-03-07 13:01:25 -0500309 return this->createTextureProxy(baseLevel, kNone_GrSurfaceFlags, 1, SkBudgeted::kYes,
310 SkBackingFit::kExact);
Greg Daniela4ead652018-02-07 10:21:48 -0500311 }
312
Brian Osmand29dcd12018-09-13 15:04:29 -0400313 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info());
314 if (!this->caps()->isConfigTexturable(desc.fConfig)) {
315 SkBitmap copy8888;
316 if (!copy8888.tryAllocPixels(bitmap.info().makeColorType(kRGBA_8888_SkColorType)) ||
317 !bitmap.readPixels(copy8888.pixmap())) {
318 return nullptr;
319 }
320 copy8888.setImmutable();
321 baseLevel = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode);
322 desc.fConfig = kRGBA_8888_GrPixelConfig;
323 }
324
325 SkPixmap pixmap;
326 SkAssertResult(baseLevel->peekPixels(&pixmap));
327 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(pixmap, nullptr));
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400328 if (!mipmaps) {
329 return nullptr;
330 }
331
Greg Daniela4ead652018-02-07 10:21:48 -0500332 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Osman1b97f132018-09-13 17:33:48 +0000333 [desc, baseLevel, mipmaps](GrResourceProvider* resourceProvider) {
Greg Daniela4ead652018-02-07 10:21:48 -0500334 if (!resourceProvider) {
335 return sk_sp<GrTexture>();
336 }
337
Brian Osman1b97f132018-09-13 17:33:48 +0000338 const int mipLevelCount = mipmaps->countLevels() + 1;
339 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
340
Greg Daniela4ead652018-02-07 10:21:48 -0500341 SkPixmap pixmap;
342 SkAssertResult(baseLevel->peekPixels(&pixmap));
343
344 // DDL TODO: Instead of copying all this info into GrMipLevels we should just plumb
345 // the use of SkMipMap down through Ganesh.
346 texels[0].fPixels = pixmap.addr();
347 texels[0].fRowBytes = pixmap.rowBytes();
348
349 for (int i = 1; i < mipLevelCount; ++i) {
350 SkMipMap::Level generatedMipLevel;
351 mipmaps->getLevel(i - 1, &generatedMipLevel);
352 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
353 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
354 SkASSERT(texels[i].fPixels);
355 }
356
Brian Salomon58389b92018-03-07 13:01:25 -0500357 return resourceProvider->createTexture(desc, SkBudgeted::kYes, texels.get(),
Brian Osman2b23c4b2018-06-01 12:25:08 -0400358 mipLevelCount);
Brian Salomon2a4f9832018-03-03 22:43:43 -0500359 },
Brian Salomon7226c232018-07-30 13:13:17 -0400360 desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kYes, GrTextureType::k2D,
361 SkBackingFit::kExact, SkBudgeted::kYes);
Greg Daniela4ead652018-02-07 10:21:48 -0500362
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400363 if (!proxy) {
364 return nullptr;
365 }
366
Greg Daniela4ead652018-02-07 10:21:48 -0500367 if (fResourceProvider) {
368 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
369 // we're better off instantiating the proxy immediately here.
370 if (!proxy->priv().doLazyInstantiation(fResourceProvider)) {
371 return nullptr;
372 }
373 }
374 return proxy;
375}
376
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500377sk_sp<GrTextureProxy> GrProxyProvider::createProxy(const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500378 GrSurfaceOrigin origin,
Greg Danielf6f7b672018-02-15 13:06:26 -0500379 GrMipMapped mipMapped,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500380 SkBackingFit fit,
381 SkBudgeted budgeted,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400382 GrInternalSurfaceFlags surfaceFlags) {
Greg Danielf6f7b672018-02-15 13:06:26 -0500383 if (GrMipMapped::kYes == mipMapped) {
384 // SkMipMap doesn't include the base level in the level count so we have to add 1
385 int mipCount = SkMipMap::ComputeLevelCount(desc.fWidth, desc.fHeight) + 1;
386 if (1 == mipCount) {
387 mipMapped = GrMipMapped::kNo;
388 }
389 }
390
391 if (!this->caps()->validateSurfaceDesc(desc, mipMapped)) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500392 return nullptr;
393 }
Brian Salomon3a2cc2c2018-02-03 00:25:12 +0000394 GrSurfaceDesc copyDesc = desc;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500395 if (desc.fFlags & kRenderTarget_GrSurfaceFlag) {
396 copyDesc.fSampleCnt =
397 this->caps()->getRenderTargetSampleCount(desc.fSampleCnt, desc.fConfig);
398 }
Brian Salomon3a2cc2c2018-02-03 00:25:12 +0000399
Brian Salomonbdecacf2018-02-02 20:32:49 -0500400 if (copyDesc.fFlags & kRenderTarget_GrSurfaceFlag) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500401 // We know anything we instantiate later from this deferred path will be
402 // both texturable and renderable
Brian Salomon7226c232018-07-30 13:13:17 -0400403 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(*this->caps(), copyDesc, origin,
404 mipMapped, GrTextureType::k2D,
405 fit, budgeted, surfaceFlags));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500406 }
407
Brian Salomon7226c232018-07-30 13:13:17 -0400408 return sk_sp<GrTextureProxy>(new GrTextureProxy(copyDesc, origin, mipMapped, GrTextureType::k2D,
409 fit, budgeted, surfaceFlags));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500410}
411
Brian Salomon7578f3e2018-03-07 14:39:54 -0500412sk_sp<GrTextureProxy> GrProxyProvider::wrapBackendTexture(const GrBackendTexture& backendTex,
413 GrSurfaceOrigin origin,
414 GrWrapOwnership ownership,
415 ReleaseProc releaseProc,
416 ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500417 if (this->isAbandoned()) {
418 return nullptr;
419 }
420
Brian Salomonf7778972018-03-08 10:13:17 -0500421 // This is only supported on a direct GrContext.
422 if (!fResourceProvider) {
423 return nullptr;
424 }
425
426 sk_sp<GrTexture> tex = fResourceProvider->wrapBackendTexture(backendTex, ownership);
427 if (!tex) {
428 return nullptr;
429 }
Robert Phillipsadbe1322018-01-17 13:35:46 -0500430
Greg Daniel6a0176b2018-01-30 09:28:44 -0500431 sk_sp<GrReleaseProcHelper> releaseHelper;
432 if (releaseProc) {
433 releaseHelper.reset(new GrReleaseProcHelper(releaseProc, releaseCtx));
Brian Salomonf7778972018-03-08 10:13:17 -0500434 // This gives the texture a ref on the releaseHelper
435 tex->setRelease(releaseHelper);
Greg Daniel6a0176b2018-01-30 09:28:44 -0500436 }
437
Brian Salomonf7778972018-03-08 10:13:17 -0500438 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
439 // Make sure we match how we created the proxy with SkBudgeted::kNo
440 SkASSERT(SkBudgeted::kNo == tex->resourcePriv().isBudgeted());
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500441
Brian Salomonf7778972018-03-08 10:13:17 -0500442 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500443}
444
Brian Salomon7578f3e2018-03-07 14:39:54 -0500445sk_sp<GrTextureProxy> GrProxyProvider::wrapRenderableBackendTexture(
Brian Salomon02bd2952018-03-07 15:20:21 -0500446 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt,
447 GrWrapOwnership ownership) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500448 if (this->isAbandoned()) {
449 return nullptr;
450 }
451
Brian Salomonf7778972018-03-08 10:13:17 -0500452 // This is only supported on a direct GrContext.
453 if (!fResourceProvider) {
454 return nullptr;
455 }
456
Greg Daniel6abda432018-02-15 14:55:00 -0500457 sampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config());
Greg Danielf87651e2018-02-21 11:36:53 -0500458 if (!sampleCnt) {
459 return nullptr;
460 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500461
Brian Salomonf7778972018-03-08 10:13:17 -0500462 sk_sp<GrTexture> tex =
463 fResourceProvider->wrapRenderableBackendTexture(backendTex, sampleCnt, ownership);
464 if (!tex) {
465 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500466 }
467
Brian Salomonf7778972018-03-08 10:13:17 -0500468 SkASSERT(tex->asRenderTarget()); // A GrTextureRenderTarget
469 // Make sure we match how we created the proxy with SkBudgeted::kNo
470 SkASSERT(SkBudgeted::kNo == tex->resourcePriv().isBudgeted());
Greg Daniel6abda432018-02-15 14:55:00 -0500471
Brian Salomonf7778972018-03-08 10:13:17 -0500472 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500473}
474
Brian Salomon7578f3e2018-03-07 14:39:54 -0500475sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget(
476 const GrBackendRenderTarget& backendRT, GrSurfaceOrigin origin) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500477 if (this->isAbandoned()) {
478 return nullptr;
479 }
480
Brian Salomonf7778972018-03-08 10:13:17 -0500481 // This is only supported on a direct GrContext.
482 if (!fResourceProvider) {
483 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500484 }
485
Brian Salomonf7778972018-03-08 10:13:17 -0500486 sk_sp<GrRenderTarget> rt = fResourceProvider->wrapBackendRenderTarget(backendRT);
487 if (!rt) {
488 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500489 }
Brian Salomonf7778972018-03-08 10:13:17 -0500490 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
491 SkASSERT(!rt->getUniqueKey().isValid());
492 // Make sure we match how we created the proxy with SkBudgeted::kNo
493 SkASSERT(SkBudgeted::kNo == rt->resourcePriv().isBudgeted());
494
495 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(std::move(rt), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500496}
497
Brian Salomon7578f3e2018-03-07 14:39:54 -0500498sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendTextureAsRenderTarget(
499 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500500 if (this->isAbandoned()) {
501 return nullptr;
502 }
503
Brian Salomonf7778972018-03-08 10:13:17 -0500504 // This is only supported on a direct GrContext.
505 if (!fResourceProvider) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500506 return nullptr;
507 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500508
Brian Salomonf7778972018-03-08 10:13:17 -0500509 sk_sp<GrRenderTarget> rt =
510 fResourceProvider->wrapBackendTextureAsRenderTarget(backendTex, sampleCnt);
511 if (!rt) {
512 return nullptr;
Greg Danielf87651e2018-02-21 11:36:53 -0500513 }
Brian Salomonf7778972018-03-08 10:13:17 -0500514 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
515 SkASSERT(!rt->getUniqueKey().isValid());
516 // Make sure we match how we created the proxy with SkBudgeted::kNo
517 SkASSERT(SkBudgeted::kNo == rt->resourcePriv().isBudgeted());
Greg Danielf87651e2018-02-21 11:36:53 -0500518
Brian Salomonf7778972018-03-08 10:13:17 -0500519 return sk_sp<GrSurfaceProxy>(new GrRenderTargetProxy(std::move(rt), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500520}
521
Robert Phillips777707b2018-01-17 11:40:14 -0500522sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
523 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500524 GrSurfaceOrigin origin,
Brian Salomon7226c232018-07-30 13:13:17 -0400525 GrMipMapped mipMapped,
526 GrTextureType textureType,
527 SkBackingFit fit,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500528 SkBudgeted budgeted) {
Brian Salomon7226c232018-07-30 13:13:17 -0400529 return this->createLazyProxy(std::move(callback), desc, origin, mipMapped, textureType,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400530 GrInternalSurfaceFlags::kNone, fit, budgeted);
Greg Daniel2a303902018-02-20 10:25:54 -0500531}
532
533sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
534 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500535 GrSurfaceOrigin origin,
Greg Daniel2a303902018-02-20 10:25:54 -0500536 GrMipMapped mipMapped,
Brian Salomon7226c232018-07-30 13:13:17 -0400537 GrTextureType textureType,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400538 GrInternalSurfaceFlags surfaceFlags,
Brian Salomon7226c232018-07-30 13:13:17 -0400539 SkBackingFit fit,
540 SkBudgeted budgeted) {
Greg Daniela8d92112018-03-09 12:05:04 -0500541 // For non-ddl draws always make lazy proxy's single use.
542 LazyInstantiationType lazyType = fResourceProvider ? LazyInstantiationType::kSingleUse
543 : LazyInstantiationType::kMultipleUse;
Brian Salomon7226c232018-07-30 13:13:17 -0400544 return this->createLazyProxy(std::move(callback), desc, origin, mipMapped, textureType,
545 surfaceFlags, fit, budgeted, lazyType);
Greg Daniela8d92112018-03-09 12:05:04 -0500546}
547
548sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
549 const GrSurfaceDesc& desc,
550 GrSurfaceOrigin origin,
551 GrMipMapped mipMapped,
Brian Salomon7226c232018-07-30 13:13:17 -0400552 GrTextureType textureType,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400553 GrInternalSurfaceFlags surfaceFlags,
Brian Salomon7226c232018-07-30 13:13:17 -0400554 SkBackingFit fit,
555 SkBudgeted budgeted,
Greg Daniela8d92112018-03-09 12:05:04 -0500556 LazyInstantiationType lazyType) {
Robert Phillips777707b2018-01-17 11:40:14 -0500557 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
558 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400559
560 if (desc.fWidth > fCaps->maxTextureSize() || desc.fHeight > fCaps->maxTextureSize()) {
561 return nullptr;
562 }
563
Greg Daniel457469c2018-02-08 15:05:44 -0500564
Greg Daniel2a303902018-02-20 10:25:54 -0500565#ifdef SK_DEBUG
566 if (SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags)) {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400567 if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kMixedSampled)) {
Greg Daniel2a303902018-02-20 10:25:54 -0500568 SkASSERT(fCaps->usesMixedSamples() && desc.fSampleCnt > 1);
569 }
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400570 if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kWindowRectsSupport)) {
Greg Daniel2a303902018-02-20 10:25:54 -0500571 SkASSERT(fCaps->maxWindowRectangles() > 0);
572 }
573 }
574#endif
575
Brian Salomon2a4f9832018-03-03 22:43:43 -0500576 return sk_sp<GrTextureProxy>(
577 SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags)
578 ? new GrTextureRenderTargetProxy(std::move(callback), lazyType, desc, origin,
Brian Salomon7226c232018-07-30 13:13:17 -0400579 mipMapped, textureType, fit, budgeted,
580 surfaceFlags)
Brian Salomon2a4f9832018-03-03 22:43:43 -0500581 : new GrTextureProxy(std::move(callback), lazyType, desc, origin, mipMapped,
Brian Salomon7226c232018-07-30 13:13:17 -0400582 textureType, fit, budgeted, surfaceFlags));
Robert Phillips777707b2018-01-17 11:40:14 -0500583}
584
Robert Phillipse8fabb22018-02-04 14:33:21 -0500585sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy(
Brian Salomon2a4f9832018-03-03 22:43:43 -0500586 LazyInstantiateCallback&& callback, const GrSurfaceDesc& desc, GrSurfaceOrigin origin,
Brian Salomon7226c232018-07-30 13:13:17 -0400587 GrInternalSurfaceFlags surfaceFlags, const TextureInfo* textureInfo, SkBackingFit fit,
588 SkBudgeted budgeted) {
Robert Phillipse8fabb22018-02-04 14:33:21 -0500589 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
590 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400591
592 if (desc.fWidth > fCaps->maxRenderTargetSize() || desc.fHeight > fCaps->maxRenderTargetSize()) {
593 return nullptr;
594 }
595
Greg Daniel2a303902018-02-20 10:25:54 -0500596 SkASSERT(SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags));
Greg Daniel457469c2018-02-08 15:05:44 -0500597
Greg Daniel2a303902018-02-20 10:25:54 -0500598#ifdef SK_DEBUG
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400599 if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kMixedSampled)) {
Greg Daniel2a303902018-02-20 10:25:54 -0500600 SkASSERT(fCaps->usesMixedSamples() && desc.fSampleCnt > 1);
601 }
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400602 if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kWindowRectsSupport)) {
Greg Daniel2a303902018-02-20 10:25:54 -0500603 SkASSERT(fCaps->maxWindowRectangles() > 0);
604 }
605#endif
606
Greg Daniel457469c2018-02-08 15:05:44 -0500607 using LazyInstantiationType = GrSurfaceProxy::LazyInstantiationType;
608 // For non-ddl draws always make lazy proxy's single use.
609 LazyInstantiationType lazyType = fResourceProvider ? LazyInstantiationType::kSingleUse
610 : LazyInstantiationType::kMultipleUse;
611
Brian Salomon7226c232018-07-30 13:13:17 -0400612 if (textureInfo) {
613 return sk_sp<GrRenderTargetProxy>(new GrTextureRenderTargetProxy(
614 std::move(callback), lazyType, desc, origin, textureInfo->fMipMapped,
615 textureInfo->fTextureType, fit, budgeted, surfaceFlags));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500616 }
617
Brian Salomon2a4f9832018-03-03 22:43:43 -0500618 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400619 std::move(callback), lazyType, desc, origin, fit, budgeted, surfaceFlags));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500620}
621
Chris Dalton4c458b12018-06-16 17:22:59 -0600622sk_sp<GrTextureProxy> GrProxyProvider::MakeFullyLazyProxy(LazyInstantiateCallback&& callback,
623 Renderable renderable,
624 GrSurfaceOrigin origin,
625 GrPixelConfig config,
626 const GrCaps& caps) {
Robert Phillips777707b2018-01-17 11:40:14 -0500627 GrSurfaceDesc desc;
Greg Daniele3204862018-04-16 11:24:10 -0400628 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNoPendingIO;
Robert Phillips777707b2018-01-17 11:40:14 -0500629 if (Renderable::kYes == renderable) {
630 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Chris Dalton4c458b12018-06-16 17:22:59 -0600631 if (caps.maxWindowRectangles() > 0) {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400632 surfaceFlags |= GrInternalSurfaceFlags::kWindowRectsSupport;
Greg Daniel2a303902018-02-20 10:25:54 -0500633 }
Robert Phillips777707b2018-01-17 11:40:14 -0500634 }
Robert Phillips777707b2018-01-17 11:40:14 -0500635 desc.fWidth = -1;
636 desc.fHeight = -1;
637 desc.fConfig = config;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500638 desc.fSampleCnt = 1;
Robert Phillips777707b2018-01-17 11:40:14 -0500639
Brian Salomon7226c232018-07-30 13:13:17 -0400640 static constexpr auto kTextureType = GrTextureType::k2D;
Chris Dalton4c458b12018-06-16 17:22:59 -0600641 return sk_sp<GrTextureProxy>(
642 (Renderable::kYes == renderable)
Brian Salomon7226c232018-07-30 13:13:17 -0400643 ? new GrTextureRenderTargetProxy(
644 std::move(callback), LazyInstantiationType::kSingleUse, desc, origin,
645 GrMipMapped::kNo, kTextureType, SkBackingFit::kApprox,
646 SkBudgeted::kYes, surfaceFlags)
Chris Dalton4c458b12018-06-16 17:22:59 -0600647 : new GrTextureProxy(std::move(callback), LazyInstantiationType::kSingleUse,
Brian Salomon7226c232018-07-30 13:13:17 -0400648 desc, origin, GrMipMapped::kNo, kTextureType,
649 SkBackingFit::kApprox, SkBudgeted::kYes, surfaceFlags));
Robert Phillips777707b2018-01-17 11:40:14 -0500650}
651
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500652bool GrProxyProvider::IsFunctionallyExact(GrSurfaceProxy* proxy) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400653 const bool isInstantiated = proxy->isInstantiated();
Robert Phillipsdb3b9792018-06-12 15:18:00 -0400654 // A proxy is functionally exact if:
655 // it is exact (obvs)
656 // when it is instantiated it will be exact (i.e., power of two dimensions)
657 // it is already instantiated and the proxy covers the entire backing surface
658 return proxy->priv().isExact() ||
659 (!isInstantiated && SkIsPow2(proxy->width()) && SkIsPow2(proxy->height())) ||
660 (isInstantiated && proxy->worstCaseWidth() == proxy->width() &&
661 proxy->worstCaseHeight() == proxy->height());
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500662}
663
664void GrProxyProvider::processInvalidProxyUniqueKey(const GrUniqueKey& key) {
665 // Note: this method is called for the whole variety of GrGpuResources so often 'key'
666 // will not be in 'fUniquelyKeyedProxies'.
667 GrTextureProxy* proxy = fUniquelyKeyedProxies.find(key);
668 if (proxy) {
669 this->processInvalidProxyUniqueKey(key, proxy, false);
670 }
671}
672
673void GrProxyProvider::processInvalidProxyUniqueKey(const GrUniqueKey& key, GrTextureProxy* proxy,
674 bool invalidateSurface) {
675 SkASSERT(proxy);
676 SkASSERT(proxy->getUniqueKey().isValid());
677 SkASSERT(proxy->getUniqueKey() == key);
678
679 fUniquelyKeyedProxies.remove(key);
680 proxy->cacheAccess().clearUniqueKey();
681
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400682 if (invalidateSurface && proxy->isInstantiated()) {
683 GrSurface* surface = proxy->peekSurface();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500684 if (surface) {
685 surface->resourcePriv().removeUniqueKey();
686 }
687 }
688}
689
690void GrProxyProvider::removeAllUniqueKeys() {
691 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
692 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
693 GrTextureProxy& tmp = *iter;
694
695 this->processInvalidProxyUniqueKey(tmp.getUniqueKey(), &tmp, false);
696 }
697 SkASSERT(!fUniquelyKeyedProxies.count());
698}