blob: d4f55559804bbe37fd40a95e0fe31e3e10b8b388 [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 Phillipsa41c6852019-02-07 10:44:10 -050011#include "GrContext.h"
12#include "GrContextPriv.h"
13#include "GrImageContext.h"
14#include "GrImageContextPriv.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050015#include "GrRenderTarget.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050016#include "GrResourceKey.h"
17#include "GrResourceProvider.h"
18#include "GrSurfaceProxy.h"
19#include "GrSurfaceProxyPriv.h"
20#include "GrTexture.h"
21#include "GrTextureProxyCacheAccess.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050022#include "GrTextureRenderTargetProxy.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050023#include "../private/GrSingleOwner.h"
Brian Osmand29dcd12018-09-13 15:04:29 -040024#include "SkAutoPixmapStorage.h"
Greg Daniela4ead652018-02-07 10:21:48 -050025#include "SkBitmap.h"
Greg Daniel9d86f1d2018-01-29 09:33:59 -050026#include "SkGr.h"
27#include "SkImage.h"
28#include "SkImage_Base.h"
Greg Daniela4ead652018-02-07 10:21:48 -050029#include "SkImageInfoPriv.h"
30#include "SkImagePriv.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050031#include "SkMipMap.h"
Greg Daniela4ead652018-02-07 10:21:48 -050032#include "SkTraceEvent.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050033
34#define ASSERT_SINGLE_OWNER \
Robert Phillipsa41c6852019-02-07 10:44:10 -050035 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fImageContext->priv().singleOwner());)
Robert Phillips1afd4cd2018-01-08 13:40:32 -050036
Robert Phillipsa9162df2019-02-11 14:12:03 -050037GrProxyProvider::GrProxyProvider(GrImageContext* imageContext) : fImageContext(imageContext) {}
Robert Phillips1afd4cd2018-01-08 13:40:32 -050038
39GrProxyProvider::~GrProxyProvider() {
Robert Phillipsa41c6852019-02-07 10:44:10 -050040 if (this->renderingDirectly()) {
Robert Phillips0790f8a2018-09-18 13:11:03 -040041 // In DDL-mode a proxy provider can still have extant uniquely keyed proxies (since
42 // they need their unique keys to, potentially, find a cached resource when the
43 // DDL is played) but, in non-DDL-mode they should all have been cleaned up by this point.
44 SkASSERT(!fUniquelyKeyedProxies.count());
45 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050046}
47
Robert Phillipsadbe1322018-01-17 13:35:46 -050048bool GrProxyProvider::assignUniqueKeyToProxy(const GrUniqueKey& key, GrTextureProxy* proxy) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050049 ASSERT_SINGLE_OWNER
50 SkASSERT(key.isValid());
51 if (this->isAbandoned() || !proxy) {
Robert Phillipsadbe1322018-01-17 13:35:46 -050052 return false;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050053 }
54
Robert Phillipsa41c6852019-02-07 10:44:10 -050055#ifdef SK_DEBUG
56 {
57 GrContext* direct = fImageContext->priv().asDirectContext();
58 if (direct) {
59 GrResourceCache* resourceCache = direct->priv().getResourceCache();
60 // If there is already a GrResource with this key then the caller has violated the
61 // normal usage pattern of uniquely keyed resources (e.g., they have created one w/o
62 // first seeing if it already existed in the cache).
63 SkASSERT(!resourceCache->findAndRefUniqueResource(key));
64 }
65 }
66#endif
Robert Phillips1afd4cd2018-01-08 13:40:32 -050067
Robert Phillips1afd4cd2018-01-08 13:40:32 -050068 SkASSERT(!fUniquelyKeyedProxies.find(key)); // multiple proxies can't get the same key
69
70 proxy->cacheAccess().setUniqueKey(this, key);
71 SkASSERT(proxy->getUniqueKey() == key);
72 fUniquelyKeyedProxies.add(proxy);
Robert Phillipsadbe1322018-01-17 13:35:46 -050073 return true;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050074}
75
76void GrProxyProvider::adoptUniqueKeyFromSurface(GrTextureProxy* proxy, const GrSurface* surf) {
77 SkASSERT(surf->getUniqueKey().isValid());
78 proxy->cacheAccess().setUniqueKey(this, surf->getUniqueKey());
79 SkASSERT(proxy->getUniqueKey() == surf->getUniqueKey());
80 // multiple proxies can't get the same key
81 SkASSERT(!fUniquelyKeyedProxies.find(surf->getUniqueKey()));
82 fUniquelyKeyedProxies.add(proxy);
83}
84
Chris Dalton2de13dd2019-01-03 15:11:59 -070085void GrProxyProvider::removeUniqueKeyFromProxy(GrTextureProxy* proxy) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050086 ASSERT_SINGLE_OWNER
Chris Dalton2de13dd2019-01-03 15:11:59 -070087 SkASSERT(proxy);
88 SkASSERT(proxy->getUniqueKey().isValid());
89
90 if (this->isAbandoned()) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050091 return;
92 }
Robert Phillips0790f8a2018-09-18 13:11:03 -040093
Chris Dalton2de13dd2019-01-03 15:11:59 -070094 this->processInvalidUniqueKey(proxy->getUniqueKey(), proxy, InvalidateGPUResource::kYes);
Robert Phillips1afd4cd2018-01-08 13:40:32 -050095}
96
97sk_sp<GrTextureProxy> GrProxyProvider::findProxyByUniqueKey(const GrUniqueKey& key,
98 GrSurfaceOrigin origin) {
99 ASSERT_SINGLE_OWNER
100
101 if (this->isAbandoned()) {
102 return nullptr;
103 }
104
105 sk_sp<GrTextureProxy> result = sk_ref_sp(fUniquelyKeyedProxies.find(key));
106 if (result) {
107 SkASSERT(result->origin() == origin);
108 }
109 return result;
110}
111
Robert Phillipsa41c6852019-02-07 10:44:10 -0500112///////////////////////////////////////////////////////////////////////////////
113
114#if GR_TEST_UTILS
115sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createInstantiatedProxy(
116 const GrSurfaceDesc& desc, GrSurfaceOrigin origin, SkBackingFit fit, SkBudgeted budgeted) {
117 GrContext* direct = fImageContext->priv().asDirectContext();
118 if (!direct) {
119 return nullptr;
120 }
121
122 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
123 sk_sp<GrTexture> tex;
124
125 if (SkBackingFit::kApprox == fit) {
126 tex = resourceProvider->createApproxTexture(desc, GrResourceProvider::Flags::kNone);
127 } else {
128 tex = resourceProvider->createTexture(desc, budgeted, GrResourceProvider::Flags::kNone);
129 }
130 if (!tex) {
131 return nullptr;
132 }
133
134 return this->createWrapped(std::move(tex), origin);
135}
136
137sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createWrapped(sk_sp<GrTexture> tex,
138 GrSurfaceOrigin origin) {
139 return this->createWrapped(std::move(tex), origin);
140}
141#endif
142
Robert Phillipsadbe1322018-01-17 13:35:46 -0500143sk_sp<GrTextureProxy> GrProxyProvider::createWrapped(sk_sp<GrTexture> tex, GrSurfaceOrigin origin) {
144#ifdef SK_DEBUG
145 if (tex->getUniqueKey().isValid()) {
146 SkASSERT(!this->findProxyByUniqueKey(tex->getUniqueKey(), origin));
147 }
148#endif
149
150 if (tex->asRenderTarget()) {
151 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin));
152 } else {
153 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin));
154 }
155}
156
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500157sk_sp<GrTextureProxy> GrProxyProvider::findOrCreateProxyByUniqueKey(const GrUniqueKey& key,
158 GrSurfaceOrigin origin) {
159 ASSERT_SINGLE_OWNER
160
161 if (this->isAbandoned()) {
162 return nullptr;
163 }
164
165 sk_sp<GrTextureProxy> result = this->findProxyByUniqueKey(key, origin);
166 if (result) {
167 return result;
168 }
169
Robert Phillipsa41c6852019-02-07 10:44:10 -0500170 GrContext* direct = fImageContext->priv().asDirectContext();
171 if (!direct) {
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500172 return nullptr;
173 }
174
Robert Phillipsa41c6852019-02-07 10:44:10 -0500175 GrResourceCache* resourceCache = direct->priv().getResourceCache();
176
177 GrGpuResource* resource = resourceCache->findAndRefUniqueResource(key);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500178 if (!resource) {
179 return nullptr;
180 }
181
182 sk_sp<GrTexture> texture(static_cast<GrSurface*>(resource)->asTexture());
183 SkASSERT(texture);
184
Robert Phillipsadbe1322018-01-17 13:35:46 -0500185 result = this->createWrapped(std::move(texture), origin);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500186 SkASSERT(result->getUniqueKey() == key);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500187 // createWrapped should've added this for us
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500188 SkASSERT(fUniquelyKeyedProxies.find(key));
189 return result;
190}
191
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500192sk_sp<GrTextureProxy> GrProxyProvider::createTextureProxy(sk_sp<SkImage> srcImage,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400193 GrSurfaceDescFlags descFlags,
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500194 int sampleCnt,
Greg Danielfb3abcd2018-02-02 15:48:33 -0500195 SkBudgeted budgeted,
Chris Daltond004e0b2018-09-27 09:28:03 -0600196 SkBackingFit fit,
197 GrInternalSurfaceFlags surfaceFlags) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500198 ASSERT_SINGLE_OWNER
199 SkASSERT(srcImage);
200
201 if (this->isAbandoned()) {
202 return nullptr;
203 }
204
Brian Osmand29dcd12018-09-13 15:04:29 -0400205 SkImageInfo info = as_IB(srcImage)->onImageInfo();
206 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
Greg Danielf87651e2018-02-21 11:36:53 -0500207
Greg Daniel0a7aa142018-02-21 13:02:32 -0500208 if (kUnknown_GrPixelConfig == config) {
209 return nullptr;
210 }
211
Robert Phillipsa41c6852019-02-07 10:44:10 -0500212 GrBackendFormat format = this->caps()->getBackendFormatFromColorType(info.colorType());
Greg Daniel4065d452018-11-16 15:43:41 -0500213 if (!format.isValid()) {
214 return nullptr;
215 }
216
Brian Osmand29dcd12018-09-13 15:04:29 -0400217 if (!this->caps()->isConfigTexturable(config)) {
218 SkBitmap copy8888;
219 if (!copy8888.tryAllocPixels(info.makeColorType(kRGBA_8888_SkColorType)) ||
220 !srcImage->readPixels(copy8888.pixmap(), 0, 0)) {
221 return nullptr;
222 }
223 copy8888.setImmutable();
224 srcImage = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode);
225 config = kRGBA_8888_GrPixelConfig;
226 }
227
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400228 if (SkToBool(descFlags & kRenderTarget_GrSurfaceFlag)) {
Greg Danielf87651e2018-02-21 11:36:53 -0500229 sampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, config);
230 if (!sampleCnt) {
231 return nullptr;
232 }
233 }
234
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400235 if (SkToBool(descFlags & kRenderTarget_GrSurfaceFlag)) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500236 if (this->caps()->usesMixedSamples() && sampleCnt > 1) {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400237 surfaceFlags |= GrInternalSurfaceFlags::kMixedSampled;
Greg Daniel2a303902018-02-20 10:25:54 -0500238 }
Greg Daniel2a303902018-02-20 10:25:54 -0500239 }
240
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500241 GrSurfaceDesc desc;
242 desc.fWidth = srcImage->width();
243 desc.fHeight = srcImage->height();
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400244 desc.fFlags = descFlags;
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500245 desc.fSampleCnt = sampleCnt;
Greg Danielf87651e2018-02-21 11:36:53 -0500246 desc.fConfig = config;
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500247
248 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Chris Daltond004e0b2018-09-27 09:28:03 -0600249 [desc, budgeted, srcImage, fit, surfaceFlags](GrResourceProvider* resourceProvider) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500250 SkPixmap pixMap;
251 SkAssertResult(srcImage->peekPixels(&pixMap));
252 GrMipLevel mipLevel = { pixMap.addr(), pixMap.rowBytes() };
253
Chris Daltond004e0b2018-09-27 09:28:03 -0600254 auto resourceProviderFlags = GrResourceProvider::Flags::kNone;
255 if (surfaceFlags & GrInternalSurfaceFlags::kNoPendingIO) {
256 resourceProviderFlags |= GrResourceProvider::Flags::kNoPendingIO;
257 }
258 return resourceProvider->createTexture(desc, budgeted, fit, mipLevel,
259 resourceProviderFlags);
Brian Salomon2a4f9832018-03-03 22:43:43 -0500260 },
Greg Daniel4065d452018-11-16 15:43:41 -0500261 format, desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, surfaceFlags, fit, budgeted);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500262
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400263 if (!proxy) {
264 return nullptr;
265 }
266
Robert Phillipsa41c6852019-02-07 10:44:10 -0500267 GrContext* direct = fImageContext->priv().asDirectContext();
268 if (direct) {
269 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
270
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500271 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
272 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500273 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500274 return nullptr;
275 }
276 }
Robert Phillipsc1b60662018-06-26 10:20:08 -0400277
278 SkASSERT(proxy->width() == desc.fWidth);
279 SkASSERT(proxy->height() == desc.fHeight);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500280 return proxy;
281}
282
Greg Daniel4065d452018-11-16 15:43:41 -0500283sk_sp<GrTextureProxy> GrProxyProvider::createMipMapProxy(const GrBackendFormat& format,
284 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500285 GrSurfaceOrigin origin,
Greg Daniel30815082018-02-09 16:08:30 -0500286 SkBudgeted budgeted) {
Robert Phillips579f0942018-01-08 14:53:35 -0500287 ASSERT_SINGLE_OWNER
288
289 if (this->isAbandoned()) {
290 return nullptr;
291 }
292
Greg Daniel4065d452018-11-16 15:43:41 -0500293 return this->createProxy(format, desc, origin, GrMipMapped::kYes, SkBackingFit::kExact,
294 budgeted, GrInternalSurfaceFlags::kNone);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500295}
296
Brian Osman2b23c4b2018-06-01 12:25:08 -0400297sk_sp<GrTextureProxy> GrProxyProvider::createMipMapProxyFromBitmap(const SkBitmap& bitmap) {
Brian Osman412674f2019-02-07 15:34:58 -0500298 ASSERT_SINGLE_OWNER
299
300 if (this->isAbandoned()) {
301 return nullptr;
302 }
303
Brian Osman2b23c4b2018-06-01 12:25:08 -0400304 if (!SkImageInfoIsValid(bitmap.info())) {
Greg Daniela4ead652018-02-07 10:21:48 -0500305 return nullptr;
306 }
307
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400308 ATRACE_ANDROID_FRAMEWORK("Upload MipMap Texture [%ux%u]", bitmap.width(), bitmap.height());
Greg Daniela4ead652018-02-07 10:21:48 -0500309
310 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
311 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
312 // upload of the data to the gpu can happen at anytime and the bitmap may change by then.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500313 SkCopyPixelsMode copyMode = this->renderingDirectly() ? kNever_SkCopyPixelsMode
314 : kIfMutable_SkCopyPixelsMode;
Greg Daniela4ead652018-02-07 10:21:48 -0500315 sk_sp<SkImage> baseLevel = SkMakeImageFromRasterBitmap(bitmap, copyMode);
Greg Daniela4ead652018-02-07 10:21:48 -0500316 if (!baseLevel) {
317 return nullptr;
318 }
319
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400320 // This was never going to have mips anyway
321 if (0 == SkMipMap::ComputeLevelCount(baseLevel->width(), baseLevel->height())) {
Brian Salomon58389b92018-03-07 13:01:25 -0500322 return this->createTextureProxy(baseLevel, kNone_GrSurfaceFlags, 1, SkBudgeted::kYes,
323 SkBackingFit::kExact);
Greg Daniela4ead652018-02-07 10:21:48 -0500324 }
325
Robert Phillipsa41c6852019-02-07 10:44:10 -0500326 const GrBackendFormat format =
327 this->caps()->getBackendFormatFromColorType(bitmap.info().colorType());
Greg Daniel4065d452018-11-16 15:43:41 -0500328 if (!format.isValid()) {
329 return nullptr;
330 }
331
Brian Osmand29dcd12018-09-13 15:04:29 -0400332 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info());
333 if (!this->caps()->isConfigTexturable(desc.fConfig)) {
334 SkBitmap copy8888;
335 if (!copy8888.tryAllocPixels(bitmap.info().makeColorType(kRGBA_8888_SkColorType)) ||
336 !bitmap.readPixels(copy8888.pixmap())) {
337 return nullptr;
338 }
339 copy8888.setImmutable();
340 baseLevel = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode);
341 desc.fConfig = kRGBA_8888_GrPixelConfig;
342 }
343
344 SkPixmap pixmap;
345 SkAssertResult(baseLevel->peekPixels(&pixmap));
346 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(pixmap, nullptr));
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400347 if (!mipmaps) {
348 return nullptr;
349 }
350
Greg Daniela4ead652018-02-07 10:21:48 -0500351 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Osman1b97f132018-09-13 17:33:48 +0000352 [desc, baseLevel, mipmaps](GrResourceProvider* resourceProvider) {
Brian Osman1b97f132018-09-13 17:33:48 +0000353 const int mipLevelCount = mipmaps->countLevels() + 1;
354 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
355
Greg Daniela4ead652018-02-07 10:21:48 -0500356 SkPixmap pixmap;
357 SkAssertResult(baseLevel->peekPixels(&pixmap));
358
359 // DDL TODO: Instead of copying all this info into GrMipLevels we should just plumb
360 // the use of SkMipMap down through Ganesh.
361 texels[0].fPixels = pixmap.addr();
362 texels[0].fRowBytes = pixmap.rowBytes();
363
364 for (int i = 1; i < mipLevelCount; ++i) {
365 SkMipMap::Level generatedMipLevel;
366 mipmaps->getLevel(i - 1, &generatedMipLevel);
367 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
368 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
369 SkASSERT(texels[i].fPixels);
370 }
371
Brian Salomon58389b92018-03-07 13:01:25 -0500372 return resourceProvider->createTexture(desc, SkBudgeted::kYes, texels.get(),
Brian Osman2b23c4b2018-06-01 12:25:08 -0400373 mipLevelCount);
Brian Salomon2a4f9832018-03-03 22:43:43 -0500374 },
Greg Daniel4065d452018-11-16 15:43:41 -0500375 format, desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kYes, SkBackingFit::kExact,
376 SkBudgeted::kYes);
Greg Daniela4ead652018-02-07 10:21:48 -0500377
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400378 if (!proxy) {
379 return nullptr;
380 }
381
Robert Phillipsa41c6852019-02-07 10:44:10 -0500382 GrContext* direct = fImageContext->priv().asDirectContext();
383 if (direct) {
384 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Daniela4ead652018-02-07 10:21:48 -0500385 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
386 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500387 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Greg Daniela4ead652018-02-07 10:21:48 -0500388 return nullptr;
389 }
390 }
391 return proxy;
392}
393
Greg Daniel4065d452018-11-16 15:43:41 -0500394sk_sp<GrTextureProxy> GrProxyProvider::createProxy(const GrBackendFormat& format,
395 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500396 GrSurfaceOrigin origin,
Greg Danielf6f7b672018-02-15 13:06:26 -0500397 GrMipMapped mipMapped,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500398 SkBackingFit fit,
399 SkBudgeted budgeted,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400400 GrInternalSurfaceFlags surfaceFlags) {
Greg Danielf6f7b672018-02-15 13:06:26 -0500401 if (GrMipMapped::kYes == mipMapped) {
402 // SkMipMap doesn't include the base level in the level count so we have to add 1
403 int mipCount = SkMipMap::ComputeLevelCount(desc.fWidth, desc.fHeight) + 1;
404 if (1 == mipCount) {
405 mipMapped = GrMipMapped::kNo;
406 }
407 }
408
409 if (!this->caps()->validateSurfaceDesc(desc, mipMapped)) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500410 return nullptr;
411 }
Brian Salomon3a2cc2c2018-02-03 00:25:12 +0000412 GrSurfaceDesc copyDesc = desc;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500413 if (desc.fFlags & kRenderTarget_GrSurfaceFlag) {
414 copyDesc.fSampleCnt =
415 this->caps()->getRenderTargetSampleCount(desc.fSampleCnt, desc.fConfig);
416 }
Brian Salomon3a2cc2c2018-02-03 00:25:12 +0000417
Brian Salomonbdecacf2018-02-02 20:32:49 -0500418 if (copyDesc.fFlags & kRenderTarget_GrSurfaceFlag) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500419 // We know anything we instantiate later from this deferred path will be
420 // both texturable and renderable
Greg Daniel4065d452018-11-16 15:43:41 -0500421 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(*this->caps(), format, copyDesc,
422 origin, mipMapped,
Brian Salomon7226c232018-07-30 13:13:17 -0400423 fit, budgeted, surfaceFlags));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500424 }
425
Greg Daniel4065d452018-11-16 15:43:41 -0500426 return sk_sp<GrTextureProxy>(new GrTextureProxy(format, copyDesc, origin, mipMapped,
Brian Salomon7226c232018-07-30 13:13:17 -0400427 fit, budgeted, surfaceFlags));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500428}
429
Jim Van Verthee06b332019-01-18 10:36:32 -0500430sk_sp<GrTextureProxy> GrProxyProvider::createProxy(sk_sp<SkData> data, const GrSurfaceDesc& desc) {
431 if (!this->caps()->isConfigTexturable(desc.fConfig)) {
432 return nullptr;
433 }
434
435 const GrColorType ct = GrPixelConfigToColorType(desc.fConfig);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500436 const GrBackendFormat format =
437 this->caps()->getBackendFormatFromGrColorType(ct, GrSRGBEncoded::kNo);
Jim Van Verthee06b332019-01-18 10:36:32 -0500438
439 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
440 [desc, data](GrResourceProvider* resourceProvider) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500441 GrMipLevel texels;
442 texels.fPixels = data->data();
443 texels.fRowBytes = GrBytesPerPixel(desc.fConfig)*desc.fWidth;
444 return resourceProvider->createTexture(desc, SkBudgeted::kYes, &texels, 1);
445 },
446 format, desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, SkBackingFit::kExact,
447 SkBudgeted::kYes);
448
449 if (!proxy) {
450 return nullptr;
451 }
452
Robert Phillipsa41c6852019-02-07 10:44:10 -0500453 GrContext* direct = fImageContext->priv().asDirectContext();
454 if (direct) {
455 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Jim Van Verthee06b332019-01-18 10:36:32 -0500456 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
457 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500458 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500459 return nullptr;
460 }
461 }
462 return proxy;
463}
464
Brian Salomon7578f3e2018-03-07 14:39:54 -0500465sk_sp<GrTextureProxy> GrProxyProvider::wrapBackendTexture(const GrBackendTexture& backendTex,
466 GrSurfaceOrigin origin,
467 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500468 GrWrapCacheable cacheable,
Brian Salomonc67c31c2018-12-06 10:00:03 -0500469 GrIOType ioType,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500470 ReleaseProc releaseProc,
471 ReleaseContext releaseCtx) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500472 SkASSERT(ioType != kWrite_GrIOType);
Robert Phillipsf9bec202018-01-16 09:21:01 -0500473 if (this->isAbandoned()) {
474 return nullptr;
475 }
476
Brian Salomonf7778972018-03-08 10:13:17 -0500477 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500478 GrContext* direct = fImageContext->priv().asDirectContext();
479 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500480 return nullptr;
481 }
482
Robert Phillipsa41c6852019-02-07 10:44:10 -0500483 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
484
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500485 sk_sp<GrTexture> tex =
Robert Phillipsa41c6852019-02-07 10:44:10 -0500486 resourceProvider->wrapBackendTexture(backendTex, ownership, cacheable, ioType);
Brian Salomonf7778972018-03-08 10:13:17 -0500487 if (!tex) {
488 return nullptr;
489 }
Robert Phillipsadbe1322018-01-17 13:35:46 -0500490
Greg Daniel6a0176b2018-01-30 09:28:44 -0500491 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500492 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel6a0176b2018-01-30 09:28:44 -0500493 }
494
Brian Salomonf7778972018-03-08 10:13:17 -0500495 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
496 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500497 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500498
Brian Salomonf7778972018-03-08 10:13:17 -0500499 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500500}
501
Brian Salomon7578f3e2018-03-07 14:39:54 -0500502sk_sp<GrTextureProxy> GrProxyProvider::wrapRenderableBackendTexture(
Brian Salomon02bd2952018-03-07 15:20:21 -0500503 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt,
Greg Daniel8ce79912019-02-05 10:08:43 -0500504 GrWrapOwnership ownership, GrWrapCacheable cacheable, ReleaseProc releaseProc,
505 ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500506 if (this->isAbandoned()) {
507 return nullptr;
508 }
509
Brian Salomonf7778972018-03-08 10:13:17 -0500510 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500511 GrContext* direct = fImageContext->priv().asDirectContext();
512 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500513 return nullptr;
514 }
515
Robert Phillipsa41c6852019-02-07 10:44:10 -0500516 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
517
Greg Daniel6abda432018-02-15 14:55:00 -0500518 sampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config());
Greg Danielf87651e2018-02-21 11:36:53 -0500519 if (!sampleCnt) {
520 return nullptr;
521 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500522
Robert Phillipsa41c6852019-02-07 10:44:10 -0500523 sk_sp<GrTexture> tex = resourceProvider->wrapRenderableBackendTexture(backendTex, sampleCnt,
524 ownership, cacheable);
Brian Salomonf7778972018-03-08 10:13:17 -0500525 if (!tex) {
526 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500527 }
528
Greg Daniel8ce79912019-02-05 10:08:43 -0500529 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500530 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500531 }
532
Brian Salomonf7778972018-03-08 10:13:17 -0500533 SkASSERT(tex->asRenderTarget()); // A GrTextureRenderTarget
534 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500535 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Greg Daniel6abda432018-02-15 14:55:00 -0500536
Brian Salomonf7778972018-03-08 10:13:17 -0500537 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500538}
539
Brian Salomon7578f3e2018-03-07 14:39:54 -0500540sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget(
Greg Daniel8ce79912019-02-05 10:08:43 -0500541 const GrBackendRenderTarget& backendRT, GrSurfaceOrigin origin, ReleaseProc releaseProc,
542 ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500543 if (this->isAbandoned()) {
544 return nullptr;
545 }
546
Brian Salomonf7778972018-03-08 10:13:17 -0500547 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500548 GrContext* direct = fImageContext->priv().asDirectContext();
549 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500550 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500551 }
552
Robert Phillipsa41c6852019-02-07 10:44:10 -0500553 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
554
555 sk_sp<GrRenderTarget> rt = resourceProvider->wrapBackendRenderTarget(backendRT);
Brian Salomonf7778972018-03-08 10:13:17 -0500556 if (!rt) {
557 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500558 }
Greg Daniel8ce79912019-02-05 10:08:43 -0500559
Greg Daniel8ce79912019-02-05 10:08:43 -0500560 if (releaseProc) {
Brian Salomon2ca31f82019-03-05 13:28:58 -0500561 rt->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500562 }
563
Brian Salomonf7778972018-03-08 10:13:17 -0500564 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
565 SkASSERT(!rt->getUniqueKey().isValid());
566 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500567 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Brian Salomonf7778972018-03-08 10:13:17 -0500568
569 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(std::move(rt), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500570}
571
Brian Salomon7578f3e2018-03-07 14:39:54 -0500572sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendTextureAsRenderTarget(
573 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500574 if (this->isAbandoned()) {
575 return nullptr;
576 }
577
Brian Salomonf7778972018-03-08 10:13:17 -0500578 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500579 GrContext* direct = fImageContext->priv().asDirectContext();
580 if (!direct) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500581 return nullptr;
582 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500583
Robert Phillipsa41c6852019-02-07 10:44:10 -0500584 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
585
Brian Salomonf7778972018-03-08 10:13:17 -0500586 sk_sp<GrRenderTarget> rt =
Robert Phillipsa41c6852019-02-07 10:44:10 -0500587 resourceProvider->wrapBackendTextureAsRenderTarget(backendTex, sampleCnt);
Brian Salomonf7778972018-03-08 10:13:17 -0500588 if (!rt) {
589 return nullptr;
Greg Danielf87651e2018-02-21 11:36:53 -0500590 }
Brian Salomonf7778972018-03-08 10:13:17 -0500591 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
592 SkASSERT(!rt->getUniqueKey().isValid());
Greg Danielb46add82019-01-02 14:51:29 -0500593 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500594 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielf87651e2018-02-21 11:36:53 -0500595
Brian Salomonf7778972018-03-08 10:13:17 -0500596 return sk_sp<GrSurfaceProxy>(new GrRenderTargetProxy(std::move(rt), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500597}
598
Greg Danielb46add82019-01-02 14:51:29 -0500599sk_sp<GrRenderTargetProxy> GrProxyProvider::wrapVulkanSecondaryCBAsRenderTarget(
600 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
601 if (this->isAbandoned()) {
602 return nullptr;
603 }
604
605 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500606 GrContext* direct = fImageContext->priv().asDirectContext();
607 if (!direct) {
Greg Danielb46add82019-01-02 14:51:29 -0500608 return nullptr;
609 }
610
Robert Phillipsa41c6852019-02-07 10:44:10 -0500611 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Danielb46add82019-01-02 14:51:29 -0500612
Robert Phillipsa41c6852019-02-07 10:44:10 -0500613 sk_sp<GrRenderTarget> rt = resourceProvider->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
614 vkInfo);
Greg Danielb46add82019-01-02 14:51:29 -0500615 if (!rt) {
616 return nullptr;
617 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500618
Greg Danielb46add82019-01-02 14:51:29 -0500619 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
620 SkASSERT(!rt->getUniqueKey().isValid());
621 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500622 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielb46add82019-01-02 14:51:29 -0500623
624 // All Vulkan surfaces uses top left origins.
625 return sk_sp<GrRenderTargetProxy>(
626 new GrRenderTargetProxy(std::move(rt),
627 kTopLeft_GrSurfaceOrigin,
628 GrRenderTargetProxy::WrapsVkSecondaryCB::kYes));
629}
630
Robert Phillips777707b2018-01-17 11:40:14 -0500631sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500632 const GrBackendFormat& format,
Robert Phillips777707b2018-01-17 11:40:14 -0500633 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500634 GrSurfaceOrigin origin,
Brian Salomon7226c232018-07-30 13:13:17 -0400635 GrMipMapped mipMapped,
Brian Salomon7226c232018-07-30 13:13:17 -0400636 SkBackingFit fit,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500637 SkBudgeted budgeted) {
Greg Daniel4065d452018-11-16 15:43:41 -0500638 return this->createLazyProxy(std::move(callback), format, desc, origin, mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400639 GrInternalSurfaceFlags::kNone, fit, budgeted);
Greg Daniel2a303902018-02-20 10:25:54 -0500640}
641
642sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500643 const GrBackendFormat& format,
Greg Daniel2a303902018-02-20 10:25:54 -0500644 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500645 GrSurfaceOrigin origin,
Greg Daniel2a303902018-02-20 10:25:54 -0500646 GrMipMapped mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400647 GrInternalSurfaceFlags surfaceFlags,
Brian Salomon7226c232018-07-30 13:13:17 -0400648 SkBackingFit fit,
649 SkBudgeted budgeted) {
Greg Daniela8d92112018-03-09 12:05:04 -0500650 // For non-ddl draws always make lazy proxy's single use.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500651 LazyInstantiationType lazyType = this->renderingDirectly() ? LazyInstantiationType::kSingleUse
652 : LazyInstantiationType::kMultipleUse;
Greg Daniel4065d452018-11-16 15:43:41 -0500653 return this->createLazyProxy(std::move(callback), format, desc, origin, mipMapped, surfaceFlags,
654 fit, budgeted, lazyType);
Greg Daniela8d92112018-03-09 12:05:04 -0500655}
656
657sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500658 const GrBackendFormat& format,
Greg Daniela8d92112018-03-09 12:05:04 -0500659 const GrSurfaceDesc& desc,
660 GrSurfaceOrigin origin,
661 GrMipMapped mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400662 GrInternalSurfaceFlags surfaceFlags,
Brian Salomon7226c232018-07-30 13:13:17 -0400663 SkBackingFit fit,
664 SkBudgeted budgeted,
Greg Daniela8d92112018-03-09 12:05:04 -0500665 LazyInstantiationType lazyType) {
Robert Phillips777707b2018-01-17 11:40:14 -0500666 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
667 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400668
Robert Phillipsa41c6852019-02-07 10:44:10 -0500669 if (desc.fWidth > this->caps()->maxTextureSize() ||
670 desc.fHeight > this->caps()->maxTextureSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400671 return nullptr;
672 }
673
Greg Daniel457469c2018-02-08 15:05:44 -0500674
Greg Daniel2a303902018-02-20 10:25:54 -0500675#ifdef SK_DEBUG
676 if (SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags)) {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400677 if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kMixedSampled)) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500678 SkASSERT(this->caps()->usesMixedSamples() && desc.fSampleCnt > 1);
Greg Daniel2a303902018-02-20 10:25:54 -0500679 }
Greg Daniel2a303902018-02-20 10:25:54 -0500680 }
681#endif
682
Brian Salomon2a4f9832018-03-03 22:43:43 -0500683 return sk_sp<GrTextureProxy>(
684 SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags)
Greg Daniel4065d452018-11-16 15:43:41 -0500685 ? new GrTextureRenderTargetProxy(std::move(callback), lazyType, format, desc,
686 origin, mipMapped, fit, budgeted, surfaceFlags)
687 : new GrTextureProxy(std::move(callback), lazyType, format, desc, origin,
688 mipMapped, fit, budgeted, surfaceFlags));
Robert Phillips777707b2018-01-17 11:40:14 -0500689}
690
Robert Phillipse8fabb22018-02-04 14:33:21 -0500691sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500692 LazyInstantiateCallback&& callback, const GrBackendFormat& format,
693 const GrSurfaceDesc& desc, GrSurfaceOrigin origin, GrInternalSurfaceFlags surfaceFlags,
Greg Danielb085fa92019-03-05 16:55:12 -0500694 const TextureInfo* textureInfo, SkBackingFit fit, SkBudgeted budgeted,
695 bool wrapsVkSecondaryCB) {
Robert Phillipse8fabb22018-02-04 14:33:21 -0500696 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
697 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400698
Robert Phillipsa41c6852019-02-07 10:44:10 -0500699 if (desc.fWidth > this->caps()->maxRenderTargetSize() ||
700 desc.fHeight > this->caps()->maxRenderTargetSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400701 return nullptr;
702 }
703
Greg Daniel2a303902018-02-20 10:25:54 -0500704 SkASSERT(SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags));
Greg Daniel457469c2018-02-08 15:05:44 -0500705
Greg Daniel2a303902018-02-20 10:25:54 -0500706#ifdef SK_DEBUG
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400707 if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kMixedSampled)) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500708 SkASSERT(this->caps()->usesMixedSamples() && desc.fSampleCnt > 1);
Greg Daniel2a303902018-02-20 10:25:54 -0500709 }
Greg Daniel2a303902018-02-20 10:25:54 -0500710#endif
711
Greg Daniel457469c2018-02-08 15:05:44 -0500712 using LazyInstantiationType = GrSurfaceProxy::LazyInstantiationType;
713 // For non-ddl draws always make lazy proxy's single use.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500714 LazyInstantiationType lazyType = this->renderingDirectly() ? LazyInstantiationType::kSingleUse
715 : LazyInstantiationType::kMultipleUse;
Greg Daniel457469c2018-02-08 15:05:44 -0500716
Brian Salomon7226c232018-07-30 13:13:17 -0400717 if (textureInfo) {
Greg Danielb085fa92019-03-05 16:55:12 -0500718 // Wrapped vulkan secondary command buffers don't support texturing since we won't have an
719 // actual VkImage to texture from.
720 SkASSERT(!wrapsVkSecondaryCB);
Brian Salomon7226c232018-07-30 13:13:17 -0400721 return sk_sp<GrRenderTargetProxy>(new GrTextureRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500722 std::move(callback), lazyType, format, desc, origin, textureInfo->fMipMapped,
723 fit, budgeted, surfaceFlags));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500724 }
725
Greg Danielb085fa92019-03-05 16:55:12 -0500726 GrRenderTargetProxy::WrapsVkSecondaryCB vkSCB =
727 wrapsVkSecondaryCB ? GrRenderTargetProxy::WrapsVkSecondaryCB::kYes
728 : GrRenderTargetProxy::WrapsVkSecondaryCB::kNo;
729
Brian Salomon2a4f9832018-03-03 22:43:43 -0500730 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
Greg Danielb085fa92019-03-05 16:55:12 -0500731 std::move(callback), lazyType, format, desc, origin, fit, budgeted, surfaceFlags,
732 vkSCB));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500733}
734
Chris Dalton4c458b12018-06-16 17:22:59 -0600735sk_sp<GrTextureProxy> GrProxyProvider::MakeFullyLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500736 const GrBackendFormat& format,
Chris Dalton4c458b12018-06-16 17:22:59 -0600737 Renderable renderable,
738 GrSurfaceOrigin origin,
739 GrPixelConfig config,
740 const GrCaps& caps) {
Robert Phillips777707b2018-01-17 11:40:14 -0500741 GrSurfaceDesc desc;
Greg Daniele3204862018-04-16 11:24:10 -0400742 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNoPendingIO;
Robert Phillips777707b2018-01-17 11:40:14 -0500743 if (Renderable::kYes == renderable) {
744 desc.fFlags = kRenderTarget_GrSurfaceFlag;
745 }
Robert Phillips777707b2018-01-17 11:40:14 -0500746 desc.fWidth = -1;
747 desc.fHeight = -1;
748 desc.fConfig = config;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500749 desc.fSampleCnt = 1;
Robert Phillips777707b2018-01-17 11:40:14 -0500750
Chris Dalton4c458b12018-06-16 17:22:59 -0600751 return sk_sp<GrTextureProxy>(
752 (Renderable::kYes == renderable)
Brian Salomon7226c232018-07-30 13:13:17 -0400753 ? new GrTextureRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500754 std::move(callback), LazyInstantiationType::kSingleUse, format, desc,
755 origin, GrMipMapped::kNo, SkBackingFit::kApprox, SkBudgeted::kYes,
756 surfaceFlags)
Chris Dalton4c458b12018-06-16 17:22:59 -0600757 : new GrTextureProxy(std::move(callback), LazyInstantiationType::kSingleUse,
Greg Daniel4065d452018-11-16 15:43:41 -0500758 format, desc, origin, GrMipMapped::kNo,
Brian Salomon7226c232018-07-30 13:13:17 -0400759 SkBackingFit::kApprox, SkBudgeted::kYes, surfaceFlags));
Robert Phillips777707b2018-01-17 11:40:14 -0500760}
761
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500762bool GrProxyProvider::IsFunctionallyExact(GrSurfaceProxy* proxy) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400763 const bool isInstantiated = proxy->isInstantiated();
Robert Phillipsdb3b9792018-06-12 15:18:00 -0400764 // A proxy is functionally exact if:
765 // it is exact (obvs)
766 // when it is instantiated it will be exact (i.e., power of two dimensions)
767 // it is already instantiated and the proxy covers the entire backing surface
768 return proxy->priv().isExact() ||
769 (!isInstantiated && SkIsPow2(proxy->width()) && SkIsPow2(proxy->height())) ||
770 (isInstantiated && proxy->worstCaseWidth() == proxy->width() &&
771 proxy->worstCaseHeight() == proxy->height());
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500772}
773
Robert Phillips427966a2018-12-20 17:20:43 -0500774void GrProxyProvider::processInvalidUniqueKey(const GrUniqueKey& key, GrTextureProxy* proxy,
775 InvalidateGPUResource invalidateGPUResource) {
Chris Dalton2de13dd2019-01-03 15:11:59 -0700776 SkASSERT(key.isValid());
777
Robert Phillips427966a2018-12-20 17:20:43 -0500778 if (!proxy) {
779 proxy = fUniquelyKeyedProxies.find(key);
780 }
Chris Dalton2de13dd2019-01-03 15:11:59 -0700781 SkASSERT(!proxy || proxy->getUniqueKey() == key);
782
783 // Locate the corresponding GrGpuResource (if it needs to be invalidated) before clearing the
784 // proxy's unique key. We must do it in this order because 'key' may alias the proxy's key.
785 sk_sp<GrGpuResource> invalidGpuResource;
786 if (InvalidateGPUResource::kYes == invalidateGPUResource) {
787 if (proxy && proxy->isInstantiated()) {
788 invalidGpuResource = sk_ref_sp(proxy->peekSurface());
789 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500790 if (!invalidGpuResource) {
791 GrContext* direct = fImageContext->priv().asDirectContext();
792 if (direct) {
793 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
794 invalidGpuResource = resourceProvider->findByUniqueKey<GrGpuResource>(key);
795 }
Chris Dalton2de13dd2019-01-03 15:11:59 -0700796 }
797 SkASSERT(!invalidGpuResource || invalidGpuResource->getUniqueKey() == key);
798 }
Robert Phillips427966a2018-12-20 17:20:43 -0500799
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500800 // Note: this method is called for the whole variety of GrGpuResources so often 'key'
801 // will not be in 'fUniquelyKeyedProxies'.
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500802 if (proxy) {
Robert Phillips427966a2018-12-20 17:20:43 -0500803 fUniquelyKeyedProxies.remove(key);
804 proxy->cacheAccess().clearUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500805 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500806
Chris Dalton2de13dd2019-01-03 15:11:59 -0700807 if (invalidGpuResource) {
808 invalidGpuResource->resourcePriv().removeUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500809 }
810}
811
Robert Phillipsa41c6852019-02-07 10:44:10 -0500812uint32_t GrProxyProvider::contextID() const {
813 return fImageContext->priv().contextID();
814}
815
816const GrCaps* GrProxyProvider::caps() const {
817 return fImageContext->priv().caps();
818}
819
820sk_sp<const GrCaps> GrProxyProvider::refCaps() const {
821 return fImageContext->priv().refCaps();
822}
823
Robert Phillipsa9162df2019-02-11 14:12:03 -0500824bool GrProxyProvider::isAbandoned() const {
825 return fImageContext->priv().abandoned();
826}
827
Robert Phillips0790f8a2018-09-18 13:11:03 -0400828void GrProxyProvider::orphanAllUniqueKeys() {
829 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
830 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
831 GrTextureProxy& tmp = *iter;
832
833 tmp.fProxyProvider = nullptr;
834 }
835}
836
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500837void GrProxyProvider::removeAllUniqueKeys() {
838 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
839 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
840 GrTextureProxy& tmp = *iter;
841
Chris Dalton2de13dd2019-01-03 15:11:59 -0700842 this->processInvalidUniqueKey(tmp.getUniqueKey(), &tmp, InvalidateGPUResource::kNo);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500843 }
844 SkASSERT(!fUniquelyKeyedProxies.count());
845}
Robert Phillipsa41c6852019-02-07 10:44:10 -0500846
847bool GrProxyProvider::renderingDirectly() const {
848 return fImageContext->priv().asDirectContext();
849}