blob: 526e99c92b1267e1ca42fe8bd5d9c19b9567510f [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 Salomon5ad6fd32019-03-21 15:30:08 -0400205 const SkImageInfo& info = srcImage->imageInfo();
Brian Osmand29dcd12018-09-13 15:04:29 -0400206 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 }
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400258 return LazyInstantiationResult(resourceProvider->createTexture(
259 desc, budgeted, fit, mipLevel, 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 Osmande496652019-03-22 13:42:33 -0400297sk_sp<GrTextureProxy> GrProxyProvider::createProxyFromBitmap(const SkBitmap& bitmap,
298 GrMipMapped mipMapped) {
Brian Osman412674f2019-02-07 15:34:58 -0500299 ASSERT_SINGLE_OWNER
Brian Osman7dcc6162019-03-25 10:12:57 -0400300 SkASSERT(GrMipMapped::kNo == mipMapped || this->caps()->mipMapSupport());
Brian Osman412674f2019-02-07 15:34:58 -0500301
302 if (this->isAbandoned()) {
303 return nullptr;
304 }
305
Brian Osman2b23c4b2018-06-01 12:25:08 -0400306 if (!SkImageInfoIsValid(bitmap.info())) {
Greg Daniela4ead652018-02-07 10:21:48 -0500307 return nullptr;
308 }
309
Brian Osmande496652019-03-22 13:42:33 -0400310 ATRACE_ANDROID_FRAMEWORK("Upload %sTexture [%ux%u]",
311 GrMipMapped::kYes == mipMapped ? "MipMap " : "",
312 bitmap.width(), bitmap.height());
Greg Daniela4ead652018-02-07 10:21:48 -0500313
314 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
315 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
316 // 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 -0500317 SkCopyPixelsMode copyMode = this->renderingDirectly() ? kNever_SkCopyPixelsMode
318 : kIfMutable_SkCopyPixelsMode;
Greg Daniela4ead652018-02-07 10:21:48 -0500319 sk_sp<SkImage> baseLevel = SkMakeImageFromRasterBitmap(bitmap, copyMode);
Greg Daniela4ead652018-02-07 10:21:48 -0500320 if (!baseLevel) {
321 return nullptr;
322 }
323
Brian Osmande496652019-03-22 13:42:33 -0400324 // If mips weren't requested (or this was too small to have any), then take the fast path
325 if (GrMipMapped::kNo == mipMapped ||
326 0 == SkMipMap::ComputeLevelCount(baseLevel->width(), baseLevel->height())) {
Brian Osman7dcc6162019-03-25 10:12:57 -0400327 return this->createTextureProxy(std::move(baseLevel), kNone_GrSurfaceFlags, 1,
328 SkBudgeted::kYes, SkBackingFit::kExact);
Greg Daniela4ead652018-02-07 10:21:48 -0500329 }
330
Robert Phillipsa41c6852019-02-07 10:44:10 -0500331 const GrBackendFormat format =
332 this->caps()->getBackendFormatFromColorType(bitmap.info().colorType());
Greg Daniel4065d452018-11-16 15:43:41 -0500333 if (!format.isValid()) {
334 return nullptr;
335 }
336
Brian Osmand29dcd12018-09-13 15:04:29 -0400337 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info());
338 if (!this->caps()->isConfigTexturable(desc.fConfig)) {
339 SkBitmap copy8888;
340 if (!copy8888.tryAllocPixels(bitmap.info().makeColorType(kRGBA_8888_SkColorType)) ||
341 !bitmap.readPixels(copy8888.pixmap())) {
342 return nullptr;
343 }
344 copy8888.setImmutable();
345 baseLevel = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode);
346 desc.fConfig = kRGBA_8888_GrPixelConfig;
347 }
348
349 SkPixmap pixmap;
350 SkAssertResult(baseLevel->peekPixels(&pixmap));
351 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(pixmap, nullptr));
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400352 if (!mipmaps) {
353 return nullptr;
354 }
355
Greg Daniela4ead652018-02-07 10:21:48 -0500356 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Osman1b97f132018-09-13 17:33:48 +0000357 [desc, baseLevel, mipmaps](GrResourceProvider* resourceProvider) {
Brian Osman1b97f132018-09-13 17:33:48 +0000358 const int mipLevelCount = mipmaps->countLevels() + 1;
359 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
360
Greg Daniela4ead652018-02-07 10:21:48 -0500361 SkPixmap pixmap;
362 SkAssertResult(baseLevel->peekPixels(&pixmap));
363
364 // DDL TODO: Instead of copying all this info into GrMipLevels we should just plumb
365 // the use of SkMipMap down through Ganesh.
366 texels[0].fPixels = pixmap.addr();
367 texels[0].fRowBytes = pixmap.rowBytes();
368
369 for (int i = 1; i < mipLevelCount; ++i) {
370 SkMipMap::Level generatedMipLevel;
371 mipmaps->getLevel(i - 1, &generatedMipLevel);
372 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
373 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
374 SkASSERT(texels[i].fPixels);
375 }
376
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400377 return LazyInstantiationResult(resourceProvider->createTexture(
378 desc, SkBudgeted::kYes, texels.get(), mipLevelCount));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500379 },
Greg Daniel4065d452018-11-16 15:43:41 -0500380 format, desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kYes, SkBackingFit::kExact,
381 SkBudgeted::kYes);
Greg Daniela4ead652018-02-07 10:21:48 -0500382
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400383 if (!proxy) {
384 return nullptr;
385 }
386
Robert Phillipsa41c6852019-02-07 10:44:10 -0500387 GrContext* direct = fImageContext->priv().asDirectContext();
388 if (direct) {
389 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Daniela4ead652018-02-07 10:21:48 -0500390 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
391 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500392 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Greg Daniela4ead652018-02-07 10:21:48 -0500393 return nullptr;
394 }
395 }
396 return proxy;
397}
398
Greg Daniel4065d452018-11-16 15:43:41 -0500399sk_sp<GrTextureProxy> GrProxyProvider::createProxy(const GrBackendFormat& format,
400 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500401 GrSurfaceOrigin origin,
Greg Danielf6f7b672018-02-15 13:06:26 -0500402 GrMipMapped mipMapped,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500403 SkBackingFit fit,
404 SkBudgeted budgeted,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400405 GrInternalSurfaceFlags surfaceFlags) {
Greg Danielf6f7b672018-02-15 13:06:26 -0500406 if (GrMipMapped::kYes == mipMapped) {
407 // SkMipMap doesn't include the base level in the level count so we have to add 1
408 int mipCount = SkMipMap::ComputeLevelCount(desc.fWidth, desc.fHeight) + 1;
409 if (1 == mipCount) {
410 mipMapped = GrMipMapped::kNo;
411 }
412 }
413
414 if (!this->caps()->validateSurfaceDesc(desc, mipMapped)) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500415 return nullptr;
416 }
Brian Salomon3a2cc2c2018-02-03 00:25:12 +0000417 GrSurfaceDesc copyDesc = desc;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500418 if (desc.fFlags & kRenderTarget_GrSurfaceFlag) {
419 copyDesc.fSampleCnt =
420 this->caps()->getRenderTargetSampleCount(desc.fSampleCnt, desc.fConfig);
421 }
Brian Salomon3a2cc2c2018-02-03 00:25:12 +0000422
Brian Salomonbdecacf2018-02-02 20:32:49 -0500423 if (copyDesc.fFlags & kRenderTarget_GrSurfaceFlag) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500424 // We know anything we instantiate later from this deferred path will be
425 // both texturable and renderable
Greg Daniel4065d452018-11-16 15:43:41 -0500426 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(*this->caps(), format, copyDesc,
427 origin, mipMapped,
Brian Salomon7226c232018-07-30 13:13:17 -0400428 fit, budgeted, surfaceFlags));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500429 }
430
Greg Daniel4065d452018-11-16 15:43:41 -0500431 return sk_sp<GrTextureProxy>(new GrTextureProxy(format, copyDesc, origin, mipMapped,
Brian Salomon7226c232018-07-30 13:13:17 -0400432 fit, budgeted, surfaceFlags));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500433}
434
Jim Van Verthee06b332019-01-18 10:36:32 -0500435sk_sp<GrTextureProxy> GrProxyProvider::createProxy(sk_sp<SkData> data, const GrSurfaceDesc& desc) {
436 if (!this->caps()->isConfigTexturable(desc.fConfig)) {
437 return nullptr;
438 }
439
440 const GrColorType ct = GrPixelConfigToColorType(desc.fConfig);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500441 const GrBackendFormat format =
442 this->caps()->getBackendFormatFromGrColorType(ct, GrSRGBEncoded::kNo);
Jim Van Verthee06b332019-01-18 10:36:32 -0500443
444 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
445 [desc, data](GrResourceProvider* resourceProvider) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500446 GrMipLevel texels;
447 texels.fPixels = data->data();
448 texels.fRowBytes = GrBytesPerPixel(desc.fConfig)*desc.fWidth;
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400449 return LazyInstantiationResult(
450 resourceProvider->createTexture(desc, SkBudgeted::kYes, &texels, 1));
Jim Van Verthee06b332019-01-18 10:36:32 -0500451 },
452 format, desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, SkBackingFit::kExact,
453 SkBudgeted::kYes);
454
455 if (!proxy) {
456 return nullptr;
457 }
458
Robert Phillipsa41c6852019-02-07 10:44:10 -0500459 GrContext* direct = fImageContext->priv().asDirectContext();
460 if (direct) {
461 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Jim Van Verthee06b332019-01-18 10:36:32 -0500462 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
463 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500464 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500465 return nullptr;
466 }
467 }
468 return proxy;
469}
470
Brian Salomon7578f3e2018-03-07 14:39:54 -0500471sk_sp<GrTextureProxy> GrProxyProvider::wrapBackendTexture(const GrBackendTexture& backendTex,
472 GrSurfaceOrigin origin,
473 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500474 GrWrapCacheable cacheable,
Brian Salomonc67c31c2018-12-06 10:00:03 -0500475 GrIOType ioType,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500476 ReleaseProc releaseProc,
477 ReleaseContext releaseCtx) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500478 SkASSERT(ioType != kWrite_GrIOType);
Robert Phillipsf9bec202018-01-16 09:21:01 -0500479 if (this->isAbandoned()) {
480 return nullptr;
481 }
482
Brian Salomonf7778972018-03-08 10:13:17 -0500483 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500484 GrContext* direct = fImageContext->priv().asDirectContext();
485 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500486 return nullptr;
487 }
488
Robert Phillipsa41c6852019-02-07 10:44:10 -0500489 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
490
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500491 sk_sp<GrTexture> tex =
Robert Phillipsa41c6852019-02-07 10:44:10 -0500492 resourceProvider->wrapBackendTexture(backendTex, ownership, cacheable, ioType);
Brian Salomonf7778972018-03-08 10:13:17 -0500493 if (!tex) {
494 return nullptr;
495 }
Robert Phillipsadbe1322018-01-17 13:35:46 -0500496
Greg Daniel6a0176b2018-01-30 09:28:44 -0500497 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500498 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel6a0176b2018-01-30 09:28:44 -0500499 }
500
Brian Salomonf7778972018-03-08 10:13:17 -0500501 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
502 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500503 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500504
Brian Salomonf7778972018-03-08 10:13:17 -0500505 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500506}
507
Brian Salomon7578f3e2018-03-07 14:39:54 -0500508sk_sp<GrTextureProxy> GrProxyProvider::wrapRenderableBackendTexture(
Brian Salomon02bd2952018-03-07 15:20:21 -0500509 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt,
Greg Daniel8ce79912019-02-05 10:08:43 -0500510 GrWrapOwnership ownership, GrWrapCacheable cacheable, ReleaseProc releaseProc,
511 ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500512 if (this->isAbandoned()) {
513 return nullptr;
514 }
515
Brian Salomonf7778972018-03-08 10:13:17 -0500516 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500517 GrContext* direct = fImageContext->priv().asDirectContext();
518 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500519 return nullptr;
520 }
521
Robert Phillipsa41c6852019-02-07 10:44:10 -0500522 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
523
Greg Daniel6abda432018-02-15 14:55:00 -0500524 sampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config());
Greg Danielf87651e2018-02-21 11:36:53 -0500525 if (!sampleCnt) {
526 return nullptr;
527 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500528
Robert Phillipsa41c6852019-02-07 10:44:10 -0500529 sk_sp<GrTexture> tex = resourceProvider->wrapRenderableBackendTexture(backendTex, sampleCnt,
530 ownership, cacheable);
Brian Salomonf7778972018-03-08 10:13:17 -0500531 if (!tex) {
532 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500533 }
534
Greg Daniel8ce79912019-02-05 10:08:43 -0500535 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500536 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500537 }
538
Brian Salomonf7778972018-03-08 10:13:17 -0500539 SkASSERT(tex->asRenderTarget()); // A GrTextureRenderTarget
540 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500541 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Greg Daniel6abda432018-02-15 14:55:00 -0500542
Brian Salomonf7778972018-03-08 10:13:17 -0500543 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500544}
545
Brian Salomon7578f3e2018-03-07 14:39:54 -0500546sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget(
Greg Daniel8ce79912019-02-05 10:08:43 -0500547 const GrBackendRenderTarget& backendRT, GrSurfaceOrigin origin, ReleaseProc releaseProc,
548 ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500549 if (this->isAbandoned()) {
550 return nullptr;
551 }
552
Brian Salomonf7778972018-03-08 10:13:17 -0500553 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500554 GrContext* direct = fImageContext->priv().asDirectContext();
555 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500556 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500557 }
558
Robert Phillipsa41c6852019-02-07 10:44:10 -0500559 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
560
561 sk_sp<GrRenderTarget> rt = resourceProvider->wrapBackendRenderTarget(backendRT);
Brian Salomonf7778972018-03-08 10:13:17 -0500562 if (!rt) {
563 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500564 }
Greg Daniel8ce79912019-02-05 10:08:43 -0500565
Greg Daniel8ce79912019-02-05 10:08:43 -0500566 if (releaseProc) {
Brian Salomon2ca31f82019-03-05 13:28:58 -0500567 rt->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500568 }
569
Brian Salomonf7778972018-03-08 10:13:17 -0500570 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
571 SkASSERT(!rt->getUniqueKey().isValid());
572 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500573 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Brian Salomonf7778972018-03-08 10:13:17 -0500574
575 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(std::move(rt), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500576}
577
Brian Salomon7578f3e2018-03-07 14:39:54 -0500578sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendTextureAsRenderTarget(
579 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500580 if (this->isAbandoned()) {
581 return nullptr;
582 }
583
Brian Salomonf7778972018-03-08 10:13:17 -0500584 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500585 GrContext* direct = fImageContext->priv().asDirectContext();
586 if (!direct) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500587 return nullptr;
588 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500589
Robert Phillipsa41c6852019-02-07 10:44:10 -0500590 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
591
Brian Salomonf7778972018-03-08 10:13:17 -0500592 sk_sp<GrRenderTarget> rt =
Robert Phillipsa41c6852019-02-07 10:44:10 -0500593 resourceProvider->wrapBackendTextureAsRenderTarget(backendTex, sampleCnt);
Brian Salomonf7778972018-03-08 10:13:17 -0500594 if (!rt) {
595 return nullptr;
Greg Danielf87651e2018-02-21 11:36:53 -0500596 }
Brian Salomonf7778972018-03-08 10:13:17 -0500597 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
598 SkASSERT(!rt->getUniqueKey().isValid());
Greg Danielb46add82019-01-02 14:51:29 -0500599 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500600 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielf87651e2018-02-21 11:36:53 -0500601
Brian Salomonf7778972018-03-08 10:13:17 -0500602 return sk_sp<GrSurfaceProxy>(new GrRenderTargetProxy(std::move(rt), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500603}
604
Greg Danielb46add82019-01-02 14:51:29 -0500605sk_sp<GrRenderTargetProxy> GrProxyProvider::wrapVulkanSecondaryCBAsRenderTarget(
606 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
607 if (this->isAbandoned()) {
608 return nullptr;
609 }
610
611 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500612 GrContext* direct = fImageContext->priv().asDirectContext();
613 if (!direct) {
Greg Danielb46add82019-01-02 14:51:29 -0500614 return nullptr;
615 }
616
Robert Phillipsa41c6852019-02-07 10:44:10 -0500617 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Danielb46add82019-01-02 14:51:29 -0500618
Robert Phillipsa41c6852019-02-07 10:44:10 -0500619 sk_sp<GrRenderTarget> rt = resourceProvider->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
620 vkInfo);
Greg Danielb46add82019-01-02 14:51:29 -0500621 if (!rt) {
622 return nullptr;
623 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500624
Greg Danielb46add82019-01-02 14:51:29 -0500625 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
626 SkASSERT(!rt->getUniqueKey().isValid());
627 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500628 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielb46add82019-01-02 14:51:29 -0500629
630 // All Vulkan surfaces uses top left origins.
631 return sk_sp<GrRenderTargetProxy>(
632 new GrRenderTargetProxy(std::move(rt),
633 kTopLeft_GrSurfaceOrigin,
634 GrRenderTargetProxy::WrapsVkSecondaryCB::kYes));
635}
636
Robert Phillips777707b2018-01-17 11:40:14 -0500637sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500638 const GrBackendFormat& format,
Robert Phillips777707b2018-01-17 11:40:14 -0500639 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500640 GrSurfaceOrigin origin,
Brian Salomon7226c232018-07-30 13:13:17 -0400641 GrMipMapped mipMapped,
Brian Salomon7226c232018-07-30 13:13:17 -0400642 SkBackingFit fit,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500643 SkBudgeted budgeted) {
Greg Daniel4065d452018-11-16 15:43:41 -0500644 return this->createLazyProxy(std::move(callback), format, desc, origin, mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400645 GrInternalSurfaceFlags::kNone, fit, budgeted);
Greg Daniel2a303902018-02-20 10:25:54 -0500646}
647
648sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500649 const GrBackendFormat& format,
Greg Daniel2a303902018-02-20 10:25:54 -0500650 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500651 GrSurfaceOrigin origin,
Greg Daniel2a303902018-02-20 10:25:54 -0500652 GrMipMapped mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400653 GrInternalSurfaceFlags surfaceFlags,
Brian Salomon7226c232018-07-30 13:13:17 -0400654 SkBackingFit fit,
655 SkBudgeted budgeted) {
Greg Daniela8d92112018-03-09 12:05:04 -0500656 // For non-ddl draws always make lazy proxy's single use.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500657 LazyInstantiationType lazyType = this->renderingDirectly() ? LazyInstantiationType::kSingleUse
658 : LazyInstantiationType::kMultipleUse;
Greg Daniel4065d452018-11-16 15:43:41 -0500659 return this->createLazyProxy(std::move(callback), format, desc, origin, mipMapped, surfaceFlags,
660 fit, budgeted, lazyType);
Greg Daniela8d92112018-03-09 12:05:04 -0500661}
662
663sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500664 const GrBackendFormat& format,
Greg Daniela8d92112018-03-09 12:05:04 -0500665 const GrSurfaceDesc& desc,
666 GrSurfaceOrigin origin,
667 GrMipMapped mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400668 GrInternalSurfaceFlags surfaceFlags,
Brian Salomon7226c232018-07-30 13:13:17 -0400669 SkBackingFit fit,
670 SkBudgeted budgeted,
Greg Daniela8d92112018-03-09 12:05:04 -0500671 LazyInstantiationType lazyType) {
Robert Phillips777707b2018-01-17 11:40:14 -0500672 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
673 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400674
Robert Phillipsa41c6852019-02-07 10:44:10 -0500675 if (desc.fWidth > this->caps()->maxTextureSize() ||
676 desc.fHeight > this->caps()->maxTextureSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400677 return nullptr;
678 }
679
Greg Daniel457469c2018-02-08 15:05:44 -0500680
Greg Daniel2a303902018-02-20 10:25:54 -0500681#ifdef SK_DEBUG
682 if (SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags)) {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400683 if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kMixedSampled)) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500684 SkASSERT(this->caps()->usesMixedSamples() && desc.fSampleCnt > 1);
Greg Daniel2a303902018-02-20 10:25:54 -0500685 }
Greg Daniel2a303902018-02-20 10:25:54 -0500686 }
687#endif
688
Brian Salomon2a4f9832018-03-03 22:43:43 -0500689 return sk_sp<GrTextureProxy>(
690 SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags)
Greg Daniel4065d452018-11-16 15:43:41 -0500691 ? new GrTextureRenderTargetProxy(std::move(callback), lazyType, format, desc,
692 origin, mipMapped, fit, budgeted, surfaceFlags)
693 : new GrTextureProxy(std::move(callback), lazyType, format, desc, origin,
694 mipMapped, fit, budgeted, surfaceFlags));
Robert Phillips777707b2018-01-17 11:40:14 -0500695}
696
Robert Phillipse8fabb22018-02-04 14:33:21 -0500697sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500698 LazyInstantiateCallback&& callback, const GrBackendFormat& format,
699 const GrSurfaceDesc& desc, GrSurfaceOrigin origin, GrInternalSurfaceFlags surfaceFlags,
Greg Danielb085fa92019-03-05 16:55:12 -0500700 const TextureInfo* textureInfo, SkBackingFit fit, SkBudgeted budgeted,
701 bool wrapsVkSecondaryCB) {
Robert Phillipse8fabb22018-02-04 14:33:21 -0500702 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
703 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400704
Robert Phillipsa41c6852019-02-07 10:44:10 -0500705 if (desc.fWidth > this->caps()->maxRenderTargetSize() ||
706 desc.fHeight > this->caps()->maxRenderTargetSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400707 return nullptr;
708 }
709
Greg Daniel2a303902018-02-20 10:25:54 -0500710 SkASSERT(SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags));
Greg Daniel457469c2018-02-08 15:05:44 -0500711
Greg Daniel2a303902018-02-20 10:25:54 -0500712#ifdef SK_DEBUG
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400713 if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kMixedSampled)) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500714 SkASSERT(this->caps()->usesMixedSamples() && desc.fSampleCnt > 1);
Greg Daniel2a303902018-02-20 10:25:54 -0500715 }
Greg Daniel2a303902018-02-20 10:25:54 -0500716#endif
717
Greg Daniel457469c2018-02-08 15:05:44 -0500718 using LazyInstantiationType = GrSurfaceProxy::LazyInstantiationType;
719 // For non-ddl draws always make lazy proxy's single use.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500720 LazyInstantiationType lazyType = this->renderingDirectly() ? LazyInstantiationType::kSingleUse
721 : LazyInstantiationType::kMultipleUse;
Greg Daniel457469c2018-02-08 15:05:44 -0500722
Brian Salomon7226c232018-07-30 13:13:17 -0400723 if (textureInfo) {
Greg Danielb085fa92019-03-05 16:55:12 -0500724 // Wrapped vulkan secondary command buffers don't support texturing since we won't have an
725 // actual VkImage to texture from.
726 SkASSERT(!wrapsVkSecondaryCB);
Brian Salomon7226c232018-07-30 13:13:17 -0400727 return sk_sp<GrRenderTargetProxy>(new GrTextureRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500728 std::move(callback), lazyType, format, desc, origin, textureInfo->fMipMapped,
729 fit, budgeted, surfaceFlags));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500730 }
731
Greg Danielb085fa92019-03-05 16:55:12 -0500732 GrRenderTargetProxy::WrapsVkSecondaryCB vkSCB =
733 wrapsVkSecondaryCB ? GrRenderTargetProxy::WrapsVkSecondaryCB::kYes
734 : GrRenderTargetProxy::WrapsVkSecondaryCB::kNo;
735
Brian Salomon2a4f9832018-03-03 22:43:43 -0500736 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
Greg Danielb085fa92019-03-05 16:55:12 -0500737 std::move(callback), lazyType, format, desc, origin, fit, budgeted, surfaceFlags,
738 vkSCB));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500739}
740
Chris Dalton4c458b12018-06-16 17:22:59 -0600741sk_sp<GrTextureProxy> GrProxyProvider::MakeFullyLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500742 const GrBackendFormat& format,
Chris Dalton4c458b12018-06-16 17:22:59 -0600743 Renderable renderable,
744 GrSurfaceOrigin origin,
745 GrPixelConfig config,
746 const GrCaps& caps) {
Robert Phillips777707b2018-01-17 11:40:14 -0500747 GrSurfaceDesc desc;
Greg Daniele3204862018-04-16 11:24:10 -0400748 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNoPendingIO;
Robert Phillips777707b2018-01-17 11:40:14 -0500749 if (Renderable::kYes == renderable) {
750 desc.fFlags = kRenderTarget_GrSurfaceFlag;
751 }
Robert Phillips777707b2018-01-17 11:40:14 -0500752 desc.fWidth = -1;
753 desc.fHeight = -1;
754 desc.fConfig = config;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500755 desc.fSampleCnt = 1;
Robert Phillips777707b2018-01-17 11:40:14 -0500756
Chris Dalton4c458b12018-06-16 17:22:59 -0600757 return sk_sp<GrTextureProxy>(
758 (Renderable::kYes == renderable)
Brian Salomon7226c232018-07-30 13:13:17 -0400759 ? new GrTextureRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500760 std::move(callback), LazyInstantiationType::kSingleUse, format, desc,
761 origin, GrMipMapped::kNo, SkBackingFit::kApprox, SkBudgeted::kYes,
762 surfaceFlags)
Chris Dalton4c458b12018-06-16 17:22:59 -0600763 : new GrTextureProxy(std::move(callback), LazyInstantiationType::kSingleUse,
Greg Daniel4065d452018-11-16 15:43:41 -0500764 format, desc, origin, GrMipMapped::kNo,
Brian Salomon7226c232018-07-30 13:13:17 -0400765 SkBackingFit::kApprox, SkBudgeted::kYes, surfaceFlags));
Robert Phillips777707b2018-01-17 11:40:14 -0500766}
767
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500768bool GrProxyProvider::IsFunctionallyExact(GrSurfaceProxy* proxy) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400769 const bool isInstantiated = proxy->isInstantiated();
Robert Phillipsdb3b9792018-06-12 15:18:00 -0400770 // A proxy is functionally exact if:
771 // it is exact (obvs)
772 // when it is instantiated it will be exact (i.e., power of two dimensions)
773 // it is already instantiated and the proxy covers the entire backing surface
774 return proxy->priv().isExact() ||
775 (!isInstantiated && SkIsPow2(proxy->width()) && SkIsPow2(proxy->height())) ||
776 (isInstantiated && proxy->worstCaseWidth() == proxy->width() &&
777 proxy->worstCaseHeight() == proxy->height());
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500778}
779
Robert Phillips427966a2018-12-20 17:20:43 -0500780void GrProxyProvider::processInvalidUniqueKey(const GrUniqueKey& key, GrTextureProxy* proxy,
781 InvalidateGPUResource invalidateGPUResource) {
Chris Dalton2de13dd2019-01-03 15:11:59 -0700782 SkASSERT(key.isValid());
783
Robert Phillips427966a2018-12-20 17:20:43 -0500784 if (!proxy) {
785 proxy = fUniquelyKeyedProxies.find(key);
786 }
Chris Dalton2de13dd2019-01-03 15:11:59 -0700787 SkASSERT(!proxy || proxy->getUniqueKey() == key);
788
789 // Locate the corresponding GrGpuResource (if it needs to be invalidated) before clearing the
790 // proxy's unique key. We must do it in this order because 'key' may alias the proxy's key.
791 sk_sp<GrGpuResource> invalidGpuResource;
792 if (InvalidateGPUResource::kYes == invalidateGPUResource) {
793 if (proxy && proxy->isInstantiated()) {
794 invalidGpuResource = sk_ref_sp(proxy->peekSurface());
795 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500796 if (!invalidGpuResource) {
797 GrContext* direct = fImageContext->priv().asDirectContext();
798 if (direct) {
799 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
800 invalidGpuResource = resourceProvider->findByUniqueKey<GrGpuResource>(key);
801 }
Chris Dalton2de13dd2019-01-03 15:11:59 -0700802 }
803 SkASSERT(!invalidGpuResource || invalidGpuResource->getUniqueKey() == key);
804 }
Robert Phillips427966a2018-12-20 17:20:43 -0500805
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500806 // Note: this method is called for the whole variety of GrGpuResources so often 'key'
807 // will not be in 'fUniquelyKeyedProxies'.
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500808 if (proxy) {
Robert Phillips427966a2018-12-20 17:20:43 -0500809 fUniquelyKeyedProxies.remove(key);
810 proxy->cacheAccess().clearUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500811 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500812
Chris Dalton2de13dd2019-01-03 15:11:59 -0700813 if (invalidGpuResource) {
814 invalidGpuResource->resourcePriv().removeUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500815 }
816}
817
Robert Phillipsa41c6852019-02-07 10:44:10 -0500818uint32_t GrProxyProvider::contextID() const {
819 return fImageContext->priv().contextID();
820}
821
822const GrCaps* GrProxyProvider::caps() const {
823 return fImageContext->priv().caps();
824}
825
826sk_sp<const GrCaps> GrProxyProvider::refCaps() const {
827 return fImageContext->priv().refCaps();
828}
829
Robert Phillipsa9162df2019-02-11 14:12:03 -0500830bool GrProxyProvider::isAbandoned() const {
831 return fImageContext->priv().abandoned();
832}
833
Robert Phillips0790f8a2018-09-18 13:11:03 -0400834void GrProxyProvider::orphanAllUniqueKeys() {
835 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
836 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
837 GrTextureProxy& tmp = *iter;
838
839 tmp.fProxyProvider = nullptr;
840 }
841}
842
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500843void GrProxyProvider::removeAllUniqueKeys() {
844 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
845 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
846 GrTextureProxy& tmp = *iter;
847
Chris Dalton2de13dd2019-01-03 15:11:59 -0700848 this->processInvalidUniqueKey(tmp.getUniqueKey(), &tmp, InvalidateGPUResource::kNo);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500849 }
850 SkASSERT(!fUniquelyKeyedProxies.count());
851}
Robert Phillipsa41c6852019-02-07 10:44:10 -0500852
853bool GrProxyProvider::renderingDirectly() const {
854 return fImageContext->priv().asDirectContext();
855}