blob: d2c47accf81e900b24e9e8c83567a1978920aaed [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
Brian Salomon01ceae92019-04-02 11:49:54 -0400105 GrTextureProxy* proxy = fUniquelyKeyedProxies.find(key);
106 sk_sp<GrTextureProxy> result;
107 if (proxy) {
Brian Salomon2c791fc2019-04-02 11:52:03 -0400108 GrResourceCache* cache = nullptr;
109 if (auto directContext = fImageContext->priv().asDirectContext()) {
110 cache = directContext->priv().getResourceCache();
111 }
112 proxy->firstRefAccess().ref(cache);
Brian Salomon01ceae92019-04-02 11:49:54 -0400113 result.reset(proxy);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500114 SkASSERT(result->origin() == origin);
115 }
116 return result;
117}
118
Robert Phillipsa41c6852019-02-07 10:44:10 -0500119///////////////////////////////////////////////////////////////////////////////
120
121#if GR_TEST_UTILS
122sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createInstantiatedProxy(
123 const GrSurfaceDesc& desc, GrSurfaceOrigin origin, SkBackingFit fit, SkBudgeted budgeted) {
124 GrContext* direct = fImageContext->priv().asDirectContext();
125 if (!direct) {
126 return nullptr;
127 }
128
129 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
130 sk_sp<GrTexture> tex;
131
132 if (SkBackingFit::kApprox == fit) {
Robert Phillips9313aa72019-04-09 18:41:27 -0400133 tex = resourceProvider->createApproxTexture(desc, GrResourceProvider::Flags::kNoPendingIO);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500134 } else {
Robert Phillips9313aa72019-04-09 18:41:27 -0400135 tex = resourceProvider->createTexture(desc, budgeted,
136 GrResourceProvider::Flags::kNoPendingIO);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500137 }
138 if (!tex) {
139 return nullptr;
140 }
141
142 return this->createWrapped(std::move(tex), origin);
143}
144
145sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createWrapped(sk_sp<GrTexture> tex,
146 GrSurfaceOrigin origin) {
147 return this->createWrapped(std::move(tex), origin);
148}
149#endif
150
Robert Phillipsadbe1322018-01-17 13:35:46 -0500151sk_sp<GrTextureProxy> GrProxyProvider::createWrapped(sk_sp<GrTexture> tex, GrSurfaceOrigin origin) {
152#ifdef SK_DEBUG
153 if (tex->getUniqueKey().isValid()) {
154 SkASSERT(!this->findProxyByUniqueKey(tex->getUniqueKey(), origin));
155 }
156#endif
157
158 if (tex->asRenderTarget()) {
159 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin));
160 } else {
161 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin));
162 }
163}
164
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500165sk_sp<GrTextureProxy> GrProxyProvider::findOrCreateProxyByUniqueKey(const GrUniqueKey& key,
166 GrSurfaceOrigin origin) {
167 ASSERT_SINGLE_OWNER
168
169 if (this->isAbandoned()) {
170 return nullptr;
171 }
172
173 sk_sp<GrTextureProxy> result = this->findProxyByUniqueKey(key, origin);
174 if (result) {
175 return result;
176 }
177
Robert Phillipsa41c6852019-02-07 10:44:10 -0500178 GrContext* direct = fImageContext->priv().asDirectContext();
179 if (!direct) {
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500180 return nullptr;
181 }
182
Robert Phillipsa41c6852019-02-07 10:44:10 -0500183 GrResourceCache* resourceCache = direct->priv().getResourceCache();
184
185 GrGpuResource* resource = resourceCache->findAndRefUniqueResource(key);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500186 if (!resource) {
187 return nullptr;
188 }
189
190 sk_sp<GrTexture> texture(static_cast<GrSurface*>(resource)->asTexture());
191 SkASSERT(texture);
192
Robert Phillipsadbe1322018-01-17 13:35:46 -0500193 result = this->createWrapped(std::move(texture), origin);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500194 SkASSERT(result->getUniqueKey() == key);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500195 // createWrapped should've added this for us
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500196 SkASSERT(fUniquelyKeyedProxies.find(key));
197 return result;
198}
199
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500200sk_sp<GrTextureProxy> GrProxyProvider::createTextureProxy(sk_sp<SkImage> srcImage,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400201 GrSurfaceDescFlags descFlags,
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500202 int sampleCnt,
Greg Danielfb3abcd2018-02-02 15:48:33 -0500203 SkBudgeted budgeted,
Chris Daltond004e0b2018-09-27 09:28:03 -0600204 SkBackingFit fit,
205 GrInternalSurfaceFlags surfaceFlags) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500206 ASSERT_SINGLE_OWNER
207 SkASSERT(srcImage);
208
209 if (this->isAbandoned()) {
210 return nullptr;
211 }
212
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400213 const SkImageInfo& info = srcImage->imageInfo();
Brian Osmand29dcd12018-09-13 15:04:29 -0400214 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
Greg Danielf87651e2018-02-21 11:36:53 -0500215
Greg Daniel0a7aa142018-02-21 13:02:32 -0500216 if (kUnknown_GrPixelConfig == config) {
217 return nullptr;
218 }
219
Robert Phillipsa41c6852019-02-07 10:44:10 -0500220 GrBackendFormat format = this->caps()->getBackendFormatFromColorType(info.colorType());
Greg Daniel4065d452018-11-16 15:43:41 -0500221 if (!format.isValid()) {
222 return nullptr;
223 }
224
Brian Osmand29dcd12018-09-13 15:04:29 -0400225 if (!this->caps()->isConfigTexturable(config)) {
226 SkBitmap copy8888;
227 if (!copy8888.tryAllocPixels(info.makeColorType(kRGBA_8888_SkColorType)) ||
228 !srcImage->readPixels(copy8888.pixmap(), 0, 0)) {
229 return nullptr;
230 }
231 copy8888.setImmutable();
232 srcImage = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode);
233 config = kRGBA_8888_GrPixelConfig;
234 }
235
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400236 if (SkToBool(descFlags & kRenderTarget_GrSurfaceFlag)) {
Greg Danielf87651e2018-02-21 11:36:53 -0500237 sampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, config);
238 if (!sampleCnt) {
239 return nullptr;
240 }
241 }
242
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400243 if (SkToBool(descFlags & kRenderTarget_GrSurfaceFlag)) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500244 if (this->caps()->usesMixedSamples() && sampleCnt > 1) {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400245 surfaceFlags |= GrInternalSurfaceFlags::kMixedSampled;
Greg Daniel2a303902018-02-20 10:25:54 -0500246 }
Greg Daniel2a303902018-02-20 10:25:54 -0500247 }
Robert Phillips9313aa72019-04-09 18:41:27 -0400248 if (fImageContext->priv().explicitlyAllocateGPUResources()) {
249 surfaceFlags |= GrInternalSurfaceFlags::kNoPendingIO;
250 }
Greg Daniel2a303902018-02-20 10:25:54 -0500251
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500252 GrSurfaceDesc desc;
253 desc.fWidth = srcImage->width();
254 desc.fHeight = srcImage->height();
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400255 desc.fFlags = descFlags;
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500256 desc.fSampleCnt = sampleCnt;
Greg Danielf87651e2018-02-21 11:36:53 -0500257 desc.fConfig = config;
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500258
259 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Chris Daltond004e0b2018-09-27 09:28:03 -0600260 [desc, budgeted, srcImage, fit, surfaceFlags](GrResourceProvider* resourceProvider) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500261 SkPixmap pixMap;
262 SkAssertResult(srcImage->peekPixels(&pixMap));
263 GrMipLevel mipLevel = { pixMap.addr(), pixMap.rowBytes() };
264
Chris Daltond004e0b2018-09-27 09:28:03 -0600265 auto resourceProviderFlags = GrResourceProvider::Flags::kNone;
266 if (surfaceFlags & GrInternalSurfaceFlags::kNoPendingIO) {
267 resourceProviderFlags |= GrResourceProvider::Flags::kNoPendingIO;
268 }
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400269 return LazyInstantiationResult(resourceProvider->createTexture(
270 desc, budgeted, fit, mipLevel, resourceProviderFlags));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500271 },
Greg Daniel4065d452018-11-16 15:43:41 -0500272 format, desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, surfaceFlags, fit, budgeted);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500273
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400274 if (!proxy) {
275 return nullptr;
276 }
277
Robert Phillipsa41c6852019-02-07 10:44:10 -0500278 GrContext* direct = fImageContext->priv().asDirectContext();
279 if (direct) {
280 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
281
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500282 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
283 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500284 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500285 return nullptr;
286 }
287 }
Robert Phillipsc1b60662018-06-26 10:20:08 -0400288
289 SkASSERT(proxy->width() == desc.fWidth);
290 SkASSERT(proxy->height() == desc.fHeight);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500291 return proxy;
292}
293
Greg Daniel4065d452018-11-16 15:43:41 -0500294sk_sp<GrTextureProxy> GrProxyProvider::createMipMapProxy(const GrBackendFormat& format,
295 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500296 GrSurfaceOrigin origin,
Greg Daniel30815082018-02-09 16:08:30 -0500297 SkBudgeted budgeted) {
Robert Phillips579f0942018-01-08 14:53:35 -0500298 ASSERT_SINGLE_OWNER
299
300 if (this->isAbandoned()) {
301 return nullptr;
302 }
303
Greg Daniel4065d452018-11-16 15:43:41 -0500304 return this->createProxy(format, desc, origin, GrMipMapped::kYes, SkBackingFit::kExact,
305 budgeted, GrInternalSurfaceFlags::kNone);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500306}
307
Brian Osmande496652019-03-22 13:42:33 -0400308sk_sp<GrTextureProxy> GrProxyProvider::createProxyFromBitmap(const SkBitmap& bitmap,
309 GrMipMapped mipMapped) {
Brian Osman412674f2019-02-07 15:34:58 -0500310 ASSERT_SINGLE_OWNER
Brian Osman7dcc6162019-03-25 10:12:57 -0400311 SkASSERT(GrMipMapped::kNo == mipMapped || this->caps()->mipMapSupport());
Brian Osman412674f2019-02-07 15:34:58 -0500312
313 if (this->isAbandoned()) {
314 return nullptr;
315 }
316
Brian Osman2b23c4b2018-06-01 12:25:08 -0400317 if (!SkImageInfoIsValid(bitmap.info())) {
Greg Daniela4ead652018-02-07 10:21:48 -0500318 return nullptr;
319 }
320
Brian Osmande496652019-03-22 13:42:33 -0400321 ATRACE_ANDROID_FRAMEWORK("Upload %sTexture [%ux%u]",
322 GrMipMapped::kYes == mipMapped ? "MipMap " : "",
323 bitmap.width(), bitmap.height());
Greg Daniela4ead652018-02-07 10:21:48 -0500324
325 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
326 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
327 // 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 -0500328 SkCopyPixelsMode copyMode = this->renderingDirectly() ? kNever_SkCopyPixelsMode
329 : kIfMutable_SkCopyPixelsMode;
Greg Daniela4ead652018-02-07 10:21:48 -0500330 sk_sp<SkImage> baseLevel = SkMakeImageFromRasterBitmap(bitmap, copyMode);
Greg Daniela4ead652018-02-07 10:21:48 -0500331 if (!baseLevel) {
332 return nullptr;
333 }
334
Brian Osmande496652019-03-22 13:42:33 -0400335 // If mips weren't requested (or this was too small to have any), then take the fast path
336 if (GrMipMapped::kNo == mipMapped ||
337 0 == SkMipMap::ComputeLevelCount(baseLevel->width(), baseLevel->height())) {
Brian Osman7dcc6162019-03-25 10:12:57 -0400338 return this->createTextureProxy(std::move(baseLevel), kNone_GrSurfaceFlags, 1,
339 SkBudgeted::kYes, SkBackingFit::kExact);
Greg Daniela4ead652018-02-07 10:21:48 -0500340 }
341
Robert Phillipsa41c6852019-02-07 10:44:10 -0500342 const GrBackendFormat format =
343 this->caps()->getBackendFormatFromColorType(bitmap.info().colorType());
Greg Daniel4065d452018-11-16 15:43:41 -0500344 if (!format.isValid()) {
345 return nullptr;
346 }
347
Brian Osmand29dcd12018-09-13 15:04:29 -0400348 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info());
349 if (!this->caps()->isConfigTexturable(desc.fConfig)) {
350 SkBitmap copy8888;
351 if (!copy8888.tryAllocPixels(bitmap.info().makeColorType(kRGBA_8888_SkColorType)) ||
352 !bitmap.readPixels(copy8888.pixmap())) {
353 return nullptr;
354 }
355 copy8888.setImmutable();
356 baseLevel = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode);
357 desc.fConfig = kRGBA_8888_GrPixelConfig;
358 }
359
360 SkPixmap pixmap;
361 SkAssertResult(baseLevel->peekPixels(&pixmap));
362 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(pixmap, nullptr));
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400363 if (!mipmaps) {
364 return nullptr;
365 }
366
Greg Daniela4ead652018-02-07 10:21:48 -0500367 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Osman1b97f132018-09-13 17:33:48 +0000368 [desc, baseLevel, mipmaps](GrResourceProvider* resourceProvider) {
Brian Osman1b97f132018-09-13 17:33:48 +0000369 const int mipLevelCount = mipmaps->countLevels() + 1;
370 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
371
Greg Daniela4ead652018-02-07 10:21:48 -0500372 SkPixmap pixmap;
373 SkAssertResult(baseLevel->peekPixels(&pixmap));
374
375 // DDL TODO: Instead of copying all this info into GrMipLevels we should just plumb
376 // the use of SkMipMap down through Ganesh.
377 texels[0].fPixels = pixmap.addr();
378 texels[0].fRowBytes = pixmap.rowBytes();
379
380 for (int i = 1; i < mipLevelCount; ++i) {
381 SkMipMap::Level generatedMipLevel;
382 mipmaps->getLevel(i - 1, &generatedMipLevel);
383 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
384 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
385 SkASSERT(texels[i].fPixels);
386 }
387
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400388 return LazyInstantiationResult(resourceProvider->createTexture(
389 desc, SkBudgeted::kYes, texels.get(), mipLevelCount));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500390 },
Greg Daniel4065d452018-11-16 15:43:41 -0500391 format, desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kYes, SkBackingFit::kExact,
392 SkBudgeted::kYes);
Greg Daniela4ead652018-02-07 10:21:48 -0500393
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400394 if (!proxy) {
395 return nullptr;
396 }
397
Robert Phillipsa41c6852019-02-07 10:44:10 -0500398 GrContext* direct = fImageContext->priv().asDirectContext();
399 if (direct) {
400 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Daniela4ead652018-02-07 10:21:48 -0500401 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
402 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500403 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Greg Daniela4ead652018-02-07 10:21:48 -0500404 return nullptr;
405 }
406 }
407 return proxy;
408}
409
Greg Daniel4065d452018-11-16 15:43:41 -0500410sk_sp<GrTextureProxy> GrProxyProvider::createProxy(const GrBackendFormat& format,
411 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500412 GrSurfaceOrigin origin,
Greg Danielf6f7b672018-02-15 13:06:26 -0500413 GrMipMapped mipMapped,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500414 SkBackingFit fit,
415 SkBudgeted budgeted,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400416 GrInternalSurfaceFlags surfaceFlags) {
Greg Danielf6f7b672018-02-15 13:06:26 -0500417 if (GrMipMapped::kYes == mipMapped) {
418 // SkMipMap doesn't include the base level in the level count so we have to add 1
419 int mipCount = SkMipMap::ComputeLevelCount(desc.fWidth, desc.fHeight) + 1;
420 if (1 == mipCount) {
421 mipMapped = GrMipMapped::kNo;
422 }
423 }
424
425 if (!this->caps()->validateSurfaceDesc(desc, mipMapped)) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500426 return nullptr;
427 }
Brian Salomon3a2cc2c2018-02-03 00:25:12 +0000428 GrSurfaceDesc copyDesc = desc;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500429 if (desc.fFlags & kRenderTarget_GrSurfaceFlag) {
430 copyDesc.fSampleCnt =
431 this->caps()->getRenderTargetSampleCount(desc.fSampleCnt, desc.fConfig);
432 }
Brian Salomon3a2cc2c2018-02-03 00:25:12 +0000433
Brian Salomonbdecacf2018-02-02 20:32:49 -0500434 if (copyDesc.fFlags & kRenderTarget_GrSurfaceFlag) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500435 // We know anything we instantiate later from this deferred path will be
436 // both texturable and renderable
Greg Daniel4065d452018-11-16 15:43:41 -0500437 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(*this->caps(), format, copyDesc,
438 origin, mipMapped,
Brian Salomon7226c232018-07-30 13:13:17 -0400439 fit, budgeted, surfaceFlags));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500440 }
441
Greg Daniel4065d452018-11-16 15:43:41 -0500442 return sk_sp<GrTextureProxy>(new GrTextureProxy(format, copyDesc, origin, mipMapped,
Brian Salomon7226c232018-07-30 13:13:17 -0400443 fit, budgeted, surfaceFlags));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500444}
445
Jim Van Verthee06b332019-01-18 10:36:32 -0500446sk_sp<GrTextureProxy> GrProxyProvider::createProxy(sk_sp<SkData> data, const GrSurfaceDesc& desc) {
447 if (!this->caps()->isConfigTexturable(desc.fConfig)) {
448 return nullptr;
449 }
450
451 const GrColorType ct = GrPixelConfigToColorType(desc.fConfig);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500452 const GrBackendFormat format =
453 this->caps()->getBackendFormatFromGrColorType(ct, GrSRGBEncoded::kNo);
Jim Van Verthee06b332019-01-18 10:36:32 -0500454
455 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
456 [desc, data](GrResourceProvider* resourceProvider) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500457 GrMipLevel texels;
458 texels.fPixels = data->data();
459 texels.fRowBytes = GrBytesPerPixel(desc.fConfig)*desc.fWidth;
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400460 return LazyInstantiationResult(
461 resourceProvider->createTexture(desc, SkBudgeted::kYes, &texels, 1));
Jim Van Verthee06b332019-01-18 10:36:32 -0500462 },
463 format, desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, SkBackingFit::kExact,
464 SkBudgeted::kYes);
465
466 if (!proxy) {
467 return nullptr;
468 }
469
Robert Phillipsa41c6852019-02-07 10:44:10 -0500470 GrContext* direct = fImageContext->priv().asDirectContext();
471 if (direct) {
472 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Jim Van Verthee06b332019-01-18 10:36:32 -0500473 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
474 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500475 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500476 return nullptr;
477 }
478 }
479 return proxy;
480}
481
Brian Salomon7578f3e2018-03-07 14:39:54 -0500482sk_sp<GrTextureProxy> GrProxyProvider::wrapBackendTexture(const GrBackendTexture& backendTex,
483 GrSurfaceOrigin origin,
484 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500485 GrWrapCacheable cacheable,
Brian Salomonc67c31c2018-12-06 10:00:03 -0500486 GrIOType ioType,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500487 ReleaseProc releaseProc,
488 ReleaseContext releaseCtx) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500489 SkASSERT(ioType != kWrite_GrIOType);
Robert Phillipsf9bec202018-01-16 09:21:01 -0500490 if (this->isAbandoned()) {
491 return nullptr;
492 }
493
Brian Salomonf7778972018-03-08 10:13:17 -0500494 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500495 GrContext* direct = fImageContext->priv().asDirectContext();
496 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500497 return nullptr;
498 }
499
Robert Phillipsa41c6852019-02-07 10:44:10 -0500500 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
501
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500502 sk_sp<GrTexture> tex =
Robert Phillipsa41c6852019-02-07 10:44:10 -0500503 resourceProvider->wrapBackendTexture(backendTex, ownership, cacheable, ioType);
Brian Salomonf7778972018-03-08 10:13:17 -0500504 if (!tex) {
505 return nullptr;
506 }
Robert Phillipsadbe1322018-01-17 13:35:46 -0500507
Greg Daniel6a0176b2018-01-30 09:28:44 -0500508 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500509 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel6a0176b2018-01-30 09:28:44 -0500510 }
511
Brian Salomonf7778972018-03-08 10:13:17 -0500512 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
513 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500514 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500515
Brian Salomonf7778972018-03-08 10:13:17 -0500516 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500517}
518
Brian Salomon7578f3e2018-03-07 14:39:54 -0500519sk_sp<GrTextureProxy> GrProxyProvider::wrapRenderableBackendTexture(
Brian Salomon02bd2952018-03-07 15:20:21 -0500520 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt,
Greg Daniel8ce79912019-02-05 10:08:43 -0500521 GrWrapOwnership ownership, GrWrapCacheable cacheable, ReleaseProc releaseProc,
522 ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500523 if (this->isAbandoned()) {
524 return nullptr;
525 }
526
Brian Salomonf7778972018-03-08 10:13:17 -0500527 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500528 GrContext* direct = fImageContext->priv().asDirectContext();
529 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500530 return nullptr;
531 }
532
Robert Phillipsa41c6852019-02-07 10:44:10 -0500533 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
534
Greg Daniel6abda432018-02-15 14:55:00 -0500535 sampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config());
Greg Danielf87651e2018-02-21 11:36:53 -0500536 if (!sampleCnt) {
537 return nullptr;
538 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500539
Robert Phillipsa41c6852019-02-07 10:44:10 -0500540 sk_sp<GrTexture> tex = resourceProvider->wrapRenderableBackendTexture(backendTex, sampleCnt,
541 ownership, cacheable);
Brian Salomonf7778972018-03-08 10:13:17 -0500542 if (!tex) {
543 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500544 }
545
Greg Daniel8ce79912019-02-05 10:08:43 -0500546 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500547 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500548 }
549
Brian Salomonf7778972018-03-08 10:13:17 -0500550 SkASSERT(tex->asRenderTarget()); // A GrTextureRenderTarget
551 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500552 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Greg Daniel6abda432018-02-15 14:55:00 -0500553
Brian Salomonf7778972018-03-08 10:13:17 -0500554 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500555}
556
Brian Salomon7578f3e2018-03-07 14:39:54 -0500557sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget(
Greg Daniel8ce79912019-02-05 10:08:43 -0500558 const GrBackendRenderTarget& backendRT, GrSurfaceOrigin origin, ReleaseProc releaseProc,
559 ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500560 if (this->isAbandoned()) {
561 return nullptr;
562 }
563
Brian Salomonf7778972018-03-08 10:13:17 -0500564 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500565 GrContext* direct = fImageContext->priv().asDirectContext();
566 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500567 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500568 }
569
Robert Phillipsa41c6852019-02-07 10:44:10 -0500570 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
571
572 sk_sp<GrRenderTarget> rt = resourceProvider->wrapBackendRenderTarget(backendRT);
Brian Salomonf7778972018-03-08 10:13:17 -0500573 if (!rt) {
574 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500575 }
Greg Daniel8ce79912019-02-05 10:08:43 -0500576
Greg Daniel8ce79912019-02-05 10:08:43 -0500577 if (releaseProc) {
Brian Salomon2ca31f82019-03-05 13:28:58 -0500578 rt->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500579 }
580
Brian Salomonf7778972018-03-08 10:13:17 -0500581 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
582 SkASSERT(!rt->getUniqueKey().isValid());
583 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500584 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Brian Salomonf7778972018-03-08 10:13:17 -0500585
586 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(std::move(rt), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500587}
588
Brian Salomon7578f3e2018-03-07 14:39:54 -0500589sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendTextureAsRenderTarget(
590 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500591 if (this->isAbandoned()) {
592 return nullptr;
593 }
594
Brian Salomonf7778972018-03-08 10:13:17 -0500595 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500596 GrContext* direct = fImageContext->priv().asDirectContext();
597 if (!direct) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500598 return nullptr;
599 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500600
Robert Phillipsa41c6852019-02-07 10:44:10 -0500601 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
602
Brian Salomonf7778972018-03-08 10:13:17 -0500603 sk_sp<GrRenderTarget> rt =
Robert Phillipsa41c6852019-02-07 10:44:10 -0500604 resourceProvider->wrapBackendTextureAsRenderTarget(backendTex, sampleCnt);
Brian Salomonf7778972018-03-08 10:13:17 -0500605 if (!rt) {
606 return nullptr;
Greg Danielf87651e2018-02-21 11:36:53 -0500607 }
Brian Salomonf7778972018-03-08 10:13:17 -0500608 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
609 SkASSERT(!rt->getUniqueKey().isValid());
Greg Danielb46add82019-01-02 14:51:29 -0500610 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500611 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielf87651e2018-02-21 11:36:53 -0500612
Brian Salomonf7778972018-03-08 10:13:17 -0500613 return sk_sp<GrSurfaceProxy>(new GrRenderTargetProxy(std::move(rt), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500614}
615
Greg Danielb46add82019-01-02 14:51:29 -0500616sk_sp<GrRenderTargetProxy> GrProxyProvider::wrapVulkanSecondaryCBAsRenderTarget(
617 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
618 if (this->isAbandoned()) {
619 return nullptr;
620 }
621
622 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500623 GrContext* direct = fImageContext->priv().asDirectContext();
624 if (!direct) {
Greg Danielb46add82019-01-02 14:51:29 -0500625 return nullptr;
626 }
627
Robert Phillipsa41c6852019-02-07 10:44:10 -0500628 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Danielb46add82019-01-02 14:51:29 -0500629
Robert Phillipsa41c6852019-02-07 10:44:10 -0500630 sk_sp<GrRenderTarget> rt = resourceProvider->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
631 vkInfo);
Greg Danielb46add82019-01-02 14:51:29 -0500632 if (!rt) {
633 return nullptr;
634 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500635
Greg Danielb46add82019-01-02 14:51:29 -0500636 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
637 SkASSERT(!rt->getUniqueKey().isValid());
638 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500639 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielb46add82019-01-02 14:51:29 -0500640
641 // All Vulkan surfaces uses top left origins.
642 return sk_sp<GrRenderTargetProxy>(
643 new GrRenderTargetProxy(std::move(rt),
644 kTopLeft_GrSurfaceOrigin,
645 GrRenderTargetProxy::WrapsVkSecondaryCB::kYes));
646}
647
Robert Phillips777707b2018-01-17 11:40:14 -0500648sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500649 const GrBackendFormat& format,
Robert Phillips777707b2018-01-17 11:40:14 -0500650 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500651 GrSurfaceOrigin origin,
Brian Salomon7226c232018-07-30 13:13:17 -0400652 GrMipMapped mipMapped,
Brian Salomon7226c232018-07-30 13:13:17 -0400653 SkBackingFit fit,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500654 SkBudgeted budgeted) {
Greg Daniel4065d452018-11-16 15:43:41 -0500655 return this->createLazyProxy(std::move(callback), format, desc, origin, mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400656 GrInternalSurfaceFlags::kNone, fit, budgeted);
Greg Daniel2a303902018-02-20 10:25:54 -0500657}
658
659sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500660 const GrBackendFormat& format,
Greg Daniel2a303902018-02-20 10:25:54 -0500661 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500662 GrSurfaceOrigin origin,
Greg Daniel2a303902018-02-20 10:25:54 -0500663 GrMipMapped mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400664 GrInternalSurfaceFlags surfaceFlags,
Brian Salomon7226c232018-07-30 13:13:17 -0400665 SkBackingFit fit,
666 SkBudgeted budgeted) {
Greg Daniela8d92112018-03-09 12:05:04 -0500667 // For non-ddl draws always make lazy proxy's single use.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500668 LazyInstantiationType lazyType = this->renderingDirectly() ? LazyInstantiationType::kSingleUse
669 : LazyInstantiationType::kMultipleUse;
Greg Daniel4065d452018-11-16 15:43:41 -0500670 return this->createLazyProxy(std::move(callback), format, desc, origin, mipMapped, surfaceFlags,
671 fit, budgeted, lazyType);
Greg Daniela8d92112018-03-09 12:05:04 -0500672}
673
674sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500675 const GrBackendFormat& format,
Greg Daniela8d92112018-03-09 12:05:04 -0500676 const GrSurfaceDesc& desc,
677 GrSurfaceOrigin origin,
678 GrMipMapped mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400679 GrInternalSurfaceFlags surfaceFlags,
Brian Salomon7226c232018-07-30 13:13:17 -0400680 SkBackingFit fit,
681 SkBudgeted budgeted,
Greg Daniela8d92112018-03-09 12:05:04 -0500682 LazyInstantiationType lazyType) {
Robert Phillips777707b2018-01-17 11:40:14 -0500683 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
684 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400685
Robert Phillipsa41c6852019-02-07 10:44:10 -0500686 if (desc.fWidth > this->caps()->maxTextureSize() ||
687 desc.fHeight > this->caps()->maxTextureSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400688 return nullptr;
689 }
690
Greg Daniel457469c2018-02-08 15:05:44 -0500691
Greg Daniel2a303902018-02-20 10:25:54 -0500692#ifdef SK_DEBUG
693 if (SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags)) {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400694 if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kMixedSampled)) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500695 SkASSERT(this->caps()->usesMixedSamples() && desc.fSampleCnt > 1);
Greg Daniel2a303902018-02-20 10:25:54 -0500696 }
Greg Daniel2a303902018-02-20 10:25:54 -0500697 }
698#endif
699
Brian Salomon2a4f9832018-03-03 22:43:43 -0500700 return sk_sp<GrTextureProxy>(
701 SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags)
Greg Daniel4065d452018-11-16 15:43:41 -0500702 ? new GrTextureRenderTargetProxy(std::move(callback), lazyType, format, desc,
703 origin, mipMapped, fit, budgeted, surfaceFlags)
704 : new GrTextureProxy(std::move(callback), lazyType, format, desc, origin,
705 mipMapped, fit, budgeted, surfaceFlags));
Robert Phillips777707b2018-01-17 11:40:14 -0500706}
707
Robert Phillipse8fabb22018-02-04 14:33:21 -0500708sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500709 LazyInstantiateCallback&& callback, const GrBackendFormat& format,
710 const GrSurfaceDesc& desc, GrSurfaceOrigin origin, GrInternalSurfaceFlags surfaceFlags,
Greg Danielb085fa92019-03-05 16:55:12 -0500711 const TextureInfo* textureInfo, SkBackingFit fit, SkBudgeted budgeted,
712 bool wrapsVkSecondaryCB) {
Robert Phillipse8fabb22018-02-04 14:33:21 -0500713 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
714 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400715
Robert Phillipsa41c6852019-02-07 10:44:10 -0500716 if (desc.fWidth > this->caps()->maxRenderTargetSize() ||
717 desc.fHeight > this->caps()->maxRenderTargetSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400718 return nullptr;
719 }
720
Greg Daniel2a303902018-02-20 10:25:54 -0500721 SkASSERT(SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags));
Greg Daniel457469c2018-02-08 15:05:44 -0500722
Greg Daniel2a303902018-02-20 10:25:54 -0500723#ifdef SK_DEBUG
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400724 if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kMixedSampled)) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500725 SkASSERT(this->caps()->usesMixedSamples() && desc.fSampleCnt > 1);
Greg Daniel2a303902018-02-20 10:25:54 -0500726 }
Greg Daniel2a303902018-02-20 10:25:54 -0500727#endif
728
Greg Daniel457469c2018-02-08 15:05:44 -0500729 using LazyInstantiationType = GrSurfaceProxy::LazyInstantiationType;
730 // For non-ddl draws always make lazy proxy's single use.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500731 LazyInstantiationType lazyType = this->renderingDirectly() ? LazyInstantiationType::kSingleUse
732 : LazyInstantiationType::kMultipleUse;
Greg Daniel457469c2018-02-08 15:05:44 -0500733
Brian Salomon7226c232018-07-30 13:13:17 -0400734 if (textureInfo) {
Greg Danielb085fa92019-03-05 16:55:12 -0500735 // Wrapped vulkan secondary command buffers don't support texturing since we won't have an
736 // actual VkImage to texture from.
737 SkASSERT(!wrapsVkSecondaryCB);
Brian Salomon7226c232018-07-30 13:13:17 -0400738 return sk_sp<GrRenderTargetProxy>(new GrTextureRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500739 std::move(callback), lazyType, format, desc, origin, textureInfo->fMipMapped,
740 fit, budgeted, surfaceFlags));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500741 }
742
Greg Danielb085fa92019-03-05 16:55:12 -0500743 GrRenderTargetProxy::WrapsVkSecondaryCB vkSCB =
744 wrapsVkSecondaryCB ? GrRenderTargetProxy::WrapsVkSecondaryCB::kYes
745 : GrRenderTargetProxy::WrapsVkSecondaryCB::kNo;
746
Brian Salomon2a4f9832018-03-03 22:43:43 -0500747 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
Greg Danielb085fa92019-03-05 16:55:12 -0500748 std::move(callback), lazyType, format, desc, origin, fit, budgeted, surfaceFlags,
749 vkSCB));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500750}
751
Chris Dalton4c458b12018-06-16 17:22:59 -0600752sk_sp<GrTextureProxy> GrProxyProvider::MakeFullyLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500753 const GrBackendFormat& format,
Chris Dalton4c458b12018-06-16 17:22:59 -0600754 Renderable renderable,
755 GrSurfaceOrigin origin,
756 GrPixelConfig config,
757 const GrCaps& caps) {
Robert Phillips777707b2018-01-17 11:40:14 -0500758 GrSurfaceDesc desc;
Greg Daniele3204862018-04-16 11:24:10 -0400759 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNoPendingIO;
Robert Phillips777707b2018-01-17 11:40:14 -0500760 if (Renderable::kYes == renderable) {
761 desc.fFlags = kRenderTarget_GrSurfaceFlag;
762 }
Robert Phillips777707b2018-01-17 11:40:14 -0500763 desc.fWidth = -1;
764 desc.fHeight = -1;
765 desc.fConfig = config;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500766 desc.fSampleCnt = 1;
Robert Phillips777707b2018-01-17 11:40:14 -0500767
Chris Dalton4c458b12018-06-16 17:22:59 -0600768 return sk_sp<GrTextureProxy>(
769 (Renderable::kYes == renderable)
Brian Salomon7226c232018-07-30 13:13:17 -0400770 ? new GrTextureRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500771 std::move(callback), LazyInstantiationType::kSingleUse, format, desc,
772 origin, GrMipMapped::kNo, SkBackingFit::kApprox, SkBudgeted::kYes,
773 surfaceFlags)
Chris Dalton4c458b12018-06-16 17:22:59 -0600774 : new GrTextureProxy(std::move(callback), LazyInstantiationType::kSingleUse,
Greg Daniel4065d452018-11-16 15:43:41 -0500775 format, desc, origin, GrMipMapped::kNo,
Brian Salomon7226c232018-07-30 13:13:17 -0400776 SkBackingFit::kApprox, SkBudgeted::kYes, surfaceFlags));
Robert Phillips777707b2018-01-17 11:40:14 -0500777}
778
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500779bool GrProxyProvider::IsFunctionallyExact(GrSurfaceProxy* proxy) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400780 const bool isInstantiated = proxy->isInstantiated();
Robert Phillipsdb3b9792018-06-12 15:18:00 -0400781 // A proxy is functionally exact if:
782 // it is exact (obvs)
783 // when it is instantiated it will be exact (i.e., power of two dimensions)
784 // it is already instantiated and the proxy covers the entire backing surface
785 return proxy->priv().isExact() ||
786 (!isInstantiated && SkIsPow2(proxy->width()) && SkIsPow2(proxy->height())) ||
787 (isInstantiated && proxy->worstCaseWidth() == proxy->width() &&
788 proxy->worstCaseHeight() == proxy->height());
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500789}
790
Robert Phillips427966a2018-12-20 17:20:43 -0500791void GrProxyProvider::processInvalidUniqueKey(const GrUniqueKey& key, GrTextureProxy* proxy,
792 InvalidateGPUResource invalidateGPUResource) {
Chris Dalton2de13dd2019-01-03 15:11:59 -0700793 SkASSERT(key.isValid());
794
Robert Phillips427966a2018-12-20 17:20:43 -0500795 if (!proxy) {
796 proxy = fUniquelyKeyedProxies.find(key);
797 }
Chris Dalton2de13dd2019-01-03 15:11:59 -0700798 SkASSERT(!proxy || proxy->getUniqueKey() == key);
799
800 // Locate the corresponding GrGpuResource (if it needs to be invalidated) before clearing the
801 // proxy's unique key. We must do it in this order because 'key' may alias the proxy's key.
802 sk_sp<GrGpuResource> invalidGpuResource;
803 if (InvalidateGPUResource::kYes == invalidateGPUResource) {
Brian Salomon01ceae92019-04-02 11:49:54 -0400804 GrContext* direct = fImageContext->priv().asDirectContext();
805 if (direct) {
806 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
807 invalidGpuResource = resourceProvider->findByUniqueKey<GrGpuResource>(key);
Chris Dalton2de13dd2019-01-03 15:11:59 -0700808 }
809 SkASSERT(!invalidGpuResource || invalidGpuResource->getUniqueKey() == key);
810 }
Robert Phillips427966a2018-12-20 17:20:43 -0500811
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500812 // Note: this method is called for the whole variety of GrGpuResources so often 'key'
813 // will not be in 'fUniquelyKeyedProxies'.
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500814 if (proxy) {
Robert Phillips427966a2018-12-20 17:20:43 -0500815 fUniquelyKeyedProxies.remove(key);
816 proxy->cacheAccess().clearUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500817 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500818
Chris Dalton2de13dd2019-01-03 15:11:59 -0700819 if (invalidGpuResource) {
820 invalidGpuResource->resourcePriv().removeUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500821 }
822}
823
Robert Phillipsa41c6852019-02-07 10:44:10 -0500824uint32_t GrProxyProvider::contextID() const {
825 return fImageContext->priv().contextID();
826}
827
828const GrCaps* GrProxyProvider::caps() const {
829 return fImageContext->priv().caps();
830}
831
832sk_sp<const GrCaps> GrProxyProvider::refCaps() const {
833 return fImageContext->priv().refCaps();
834}
835
Robert Phillipsa9162df2019-02-11 14:12:03 -0500836bool GrProxyProvider::isAbandoned() const {
837 return fImageContext->priv().abandoned();
838}
839
Robert Phillips0790f8a2018-09-18 13:11:03 -0400840void GrProxyProvider::orphanAllUniqueKeys() {
841 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
842 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
843 GrTextureProxy& tmp = *iter;
844
845 tmp.fProxyProvider = nullptr;
846 }
847}
848
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500849void GrProxyProvider::removeAllUniqueKeys() {
850 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
851 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
852 GrTextureProxy& tmp = *iter;
853
Chris Dalton2de13dd2019-01-03 15:11:59 -0700854 this->processInvalidUniqueKey(tmp.getUniqueKey(), &tmp, InvalidateGPUResource::kNo);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500855 }
856 SkASSERT(!fUniquelyKeyedProxies.count());
857}
Robert Phillipsa41c6852019-02-07 10:44:10 -0500858
859bool GrProxyProvider::renderingDirectly() const {
860 return fImageContext->priv().asDirectContext();
861}