blob: efa7bf0dddd5166632a41c1621046bf0bdf8b2d9 [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 }
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 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 Salomon58389b92018-03-07 13:01:25 -0500377 return resourceProvider->createTexture(desc, SkBudgeted::kYes, texels.get(),
Brian Osman2b23c4b2018-06-01 12:25:08 -0400378 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;
449 return resourceProvider->createTexture(desc, SkBudgeted::kYes, &texels, 1);
450 },
451 format, desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, SkBackingFit::kExact,
452 SkBudgeted::kYes);
453
454 if (!proxy) {
455 return nullptr;
456 }
457
Robert Phillipsa41c6852019-02-07 10:44:10 -0500458 GrContext* direct = fImageContext->priv().asDirectContext();
459 if (direct) {
460 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Jim Van Verthee06b332019-01-18 10:36:32 -0500461 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
462 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500463 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500464 return nullptr;
465 }
466 }
467 return proxy;
468}
469
Brian Salomon7578f3e2018-03-07 14:39:54 -0500470sk_sp<GrTextureProxy> GrProxyProvider::wrapBackendTexture(const GrBackendTexture& backendTex,
471 GrSurfaceOrigin origin,
472 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500473 GrWrapCacheable cacheable,
Brian Salomonc67c31c2018-12-06 10:00:03 -0500474 GrIOType ioType,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500475 ReleaseProc releaseProc,
476 ReleaseContext releaseCtx) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500477 SkASSERT(ioType != kWrite_GrIOType);
Robert Phillipsf9bec202018-01-16 09:21:01 -0500478 if (this->isAbandoned()) {
479 return nullptr;
480 }
481
Brian Salomonf7778972018-03-08 10:13:17 -0500482 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500483 GrContext* direct = fImageContext->priv().asDirectContext();
484 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500485 return nullptr;
486 }
487
Robert Phillipsa41c6852019-02-07 10:44:10 -0500488 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
489
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500490 sk_sp<GrTexture> tex =
Robert Phillipsa41c6852019-02-07 10:44:10 -0500491 resourceProvider->wrapBackendTexture(backendTex, ownership, cacheable, ioType);
Brian Salomonf7778972018-03-08 10:13:17 -0500492 if (!tex) {
493 return nullptr;
494 }
Robert Phillipsadbe1322018-01-17 13:35:46 -0500495
Greg Daniel6a0176b2018-01-30 09:28:44 -0500496 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500497 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel6a0176b2018-01-30 09:28:44 -0500498 }
499
Brian Salomonf7778972018-03-08 10:13:17 -0500500 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
501 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500502 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500503
Brian Salomonf7778972018-03-08 10:13:17 -0500504 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500505}
506
Brian Salomon7578f3e2018-03-07 14:39:54 -0500507sk_sp<GrTextureProxy> GrProxyProvider::wrapRenderableBackendTexture(
Brian Salomon02bd2952018-03-07 15:20:21 -0500508 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt,
Greg Daniel8ce79912019-02-05 10:08:43 -0500509 GrWrapOwnership ownership, GrWrapCacheable cacheable, ReleaseProc releaseProc,
510 ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500511 if (this->isAbandoned()) {
512 return nullptr;
513 }
514
Brian Salomonf7778972018-03-08 10:13:17 -0500515 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500516 GrContext* direct = fImageContext->priv().asDirectContext();
517 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500518 return nullptr;
519 }
520
Robert Phillipsa41c6852019-02-07 10:44:10 -0500521 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
522
Greg Daniel6abda432018-02-15 14:55:00 -0500523 sampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config());
Greg Danielf87651e2018-02-21 11:36:53 -0500524 if (!sampleCnt) {
525 return nullptr;
526 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500527
Robert Phillipsa41c6852019-02-07 10:44:10 -0500528 sk_sp<GrTexture> tex = resourceProvider->wrapRenderableBackendTexture(backendTex, sampleCnt,
529 ownership, cacheable);
Brian Salomonf7778972018-03-08 10:13:17 -0500530 if (!tex) {
531 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500532 }
533
Greg Daniel8ce79912019-02-05 10:08:43 -0500534 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500535 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500536 }
537
Brian Salomonf7778972018-03-08 10:13:17 -0500538 SkASSERT(tex->asRenderTarget()); // A GrTextureRenderTarget
539 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500540 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Greg Daniel6abda432018-02-15 14:55:00 -0500541
Brian Salomonf7778972018-03-08 10:13:17 -0500542 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500543}
544
Brian Salomon7578f3e2018-03-07 14:39:54 -0500545sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget(
Greg Daniel8ce79912019-02-05 10:08:43 -0500546 const GrBackendRenderTarget& backendRT, GrSurfaceOrigin origin, ReleaseProc releaseProc,
547 ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500548 if (this->isAbandoned()) {
549 return nullptr;
550 }
551
Brian Salomonf7778972018-03-08 10:13:17 -0500552 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500553 GrContext* direct = fImageContext->priv().asDirectContext();
554 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500555 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500556 }
557
Robert Phillipsa41c6852019-02-07 10:44:10 -0500558 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
559
560 sk_sp<GrRenderTarget> rt = resourceProvider->wrapBackendRenderTarget(backendRT);
Brian Salomonf7778972018-03-08 10:13:17 -0500561 if (!rt) {
562 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500563 }
Greg Daniel8ce79912019-02-05 10:08:43 -0500564
Greg Daniel8ce79912019-02-05 10:08:43 -0500565 if (releaseProc) {
Brian Salomon2ca31f82019-03-05 13:28:58 -0500566 rt->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500567 }
568
Brian Salomonf7778972018-03-08 10:13:17 -0500569 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
570 SkASSERT(!rt->getUniqueKey().isValid());
571 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500572 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Brian Salomonf7778972018-03-08 10:13:17 -0500573
574 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(std::move(rt), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500575}
576
Brian Salomon7578f3e2018-03-07 14:39:54 -0500577sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendTextureAsRenderTarget(
578 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500579 if (this->isAbandoned()) {
580 return nullptr;
581 }
582
Brian Salomonf7778972018-03-08 10:13:17 -0500583 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500584 GrContext* direct = fImageContext->priv().asDirectContext();
585 if (!direct) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500586 return nullptr;
587 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500588
Robert Phillipsa41c6852019-02-07 10:44:10 -0500589 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
590
Brian Salomonf7778972018-03-08 10:13:17 -0500591 sk_sp<GrRenderTarget> rt =
Robert Phillipsa41c6852019-02-07 10:44:10 -0500592 resourceProvider->wrapBackendTextureAsRenderTarget(backendTex, sampleCnt);
Brian Salomonf7778972018-03-08 10:13:17 -0500593 if (!rt) {
594 return nullptr;
Greg Danielf87651e2018-02-21 11:36:53 -0500595 }
Brian Salomonf7778972018-03-08 10:13:17 -0500596 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
597 SkASSERT(!rt->getUniqueKey().isValid());
Greg Danielb46add82019-01-02 14:51:29 -0500598 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500599 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielf87651e2018-02-21 11:36:53 -0500600
Brian Salomonf7778972018-03-08 10:13:17 -0500601 return sk_sp<GrSurfaceProxy>(new GrRenderTargetProxy(std::move(rt), origin));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500602}
603
Greg Danielb46add82019-01-02 14:51:29 -0500604sk_sp<GrRenderTargetProxy> GrProxyProvider::wrapVulkanSecondaryCBAsRenderTarget(
605 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
606 if (this->isAbandoned()) {
607 return nullptr;
608 }
609
610 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500611 GrContext* direct = fImageContext->priv().asDirectContext();
612 if (!direct) {
Greg Danielb46add82019-01-02 14:51:29 -0500613 return nullptr;
614 }
615
Robert Phillipsa41c6852019-02-07 10:44:10 -0500616 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Danielb46add82019-01-02 14:51:29 -0500617
Robert Phillipsa41c6852019-02-07 10:44:10 -0500618 sk_sp<GrRenderTarget> rt = resourceProvider->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
619 vkInfo);
Greg Danielb46add82019-01-02 14:51:29 -0500620 if (!rt) {
621 return nullptr;
622 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500623
Greg Danielb46add82019-01-02 14:51:29 -0500624 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
625 SkASSERT(!rt->getUniqueKey().isValid());
626 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500627 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielb46add82019-01-02 14:51:29 -0500628
629 // All Vulkan surfaces uses top left origins.
630 return sk_sp<GrRenderTargetProxy>(
631 new GrRenderTargetProxy(std::move(rt),
632 kTopLeft_GrSurfaceOrigin,
633 GrRenderTargetProxy::WrapsVkSecondaryCB::kYes));
634}
635
Robert Phillips777707b2018-01-17 11:40:14 -0500636sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500637 const GrBackendFormat& format,
Robert Phillips777707b2018-01-17 11:40:14 -0500638 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500639 GrSurfaceOrigin origin,
Brian Salomon7226c232018-07-30 13:13:17 -0400640 GrMipMapped mipMapped,
Brian Salomon7226c232018-07-30 13:13:17 -0400641 SkBackingFit fit,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500642 SkBudgeted budgeted) {
Greg Daniel4065d452018-11-16 15:43:41 -0500643 return this->createLazyProxy(std::move(callback), format, desc, origin, mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400644 GrInternalSurfaceFlags::kNone, fit, budgeted);
Greg Daniel2a303902018-02-20 10:25:54 -0500645}
646
647sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500648 const GrBackendFormat& format,
Greg Daniel2a303902018-02-20 10:25:54 -0500649 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500650 GrSurfaceOrigin origin,
Greg Daniel2a303902018-02-20 10:25:54 -0500651 GrMipMapped mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400652 GrInternalSurfaceFlags surfaceFlags,
Brian Salomon7226c232018-07-30 13:13:17 -0400653 SkBackingFit fit,
654 SkBudgeted budgeted) {
Greg Daniela8d92112018-03-09 12:05:04 -0500655 // For non-ddl draws always make lazy proxy's single use.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500656 LazyInstantiationType lazyType = this->renderingDirectly() ? LazyInstantiationType::kSingleUse
657 : LazyInstantiationType::kMultipleUse;
Greg Daniel4065d452018-11-16 15:43:41 -0500658 return this->createLazyProxy(std::move(callback), format, desc, origin, mipMapped, surfaceFlags,
659 fit, budgeted, lazyType);
Greg Daniela8d92112018-03-09 12:05:04 -0500660}
661
662sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500663 const GrBackendFormat& format,
Greg Daniela8d92112018-03-09 12:05:04 -0500664 const GrSurfaceDesc& desc,
665 GrSurfaceOrigin origin,
666 GrMipMapped mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400667 GrInternalSurfaceFlags surfaceFlags,
Brian Salomon7226c232018-07-30 13:13:17 -0400668 SkBackingFit fit,
669 SkBudgeted budgeted,
Greg Daniela8d92112018-03-09 12:05:04 -0500670 LazyInstantiationType lazyType) {
Robert Phillips777707b2018-01-17 11:40:14 -0500671 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
672 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400673
Robert Phillipsa41c6852019-02-07 10:44:10 -0500674 if (desc.fWidth > this->caps()->maxTextureSize() ||
675 desc.fHeight > this->caps()->maxTextureSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400676 return nullptr;
677 }
678
Greg Daniel457469c2018-02-08 15:05:44 -0500679
Greg Daniel2a303902018-02-20 10:25:54 -0500680#ifdef SK_DEBUG
681 if (SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags)) {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400682 if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kMixedSampled)) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500683 SkASSERT(this->caps()->usesMixedSamples() && desc.fSampleCnt > 1);
Greg Daniel2a303902018-02-20 10:25:54 -0500684 }
Greg Daniel2a303902018-02-20 10:25:54 -0500685 }
686#endif
687
Brian Salomon2a4f9832018-03-03 22:43:43 -0500688 return sk_sp<GrTextureProxy>(
689 SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags)
Greg Daniel4065d452018-11-16 15:43:41 -0500690 ? new GrTextureRenderTargetProxy(std::move(callback), lazyType, format, desc,
691 origin, mipMapped, fit, budgeted, surfaceFlags)
692 : new GrTextureProxy(std::move(callback), lazyType, format, desc, origin,
693 mipMapped, fit, budgeted, surfaceFlags));
Robert Phillips777707b2018-01-17 11:40:14 -0500694}
695
Robert Phillipse8fabb22018-02-04 14:33:21 -0500696sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500697 LazyInstantiateCallback&& callback, const GrBackendFormat& format,
698 const GrSurfaceDesc& desc, GrSurfaceOrigin origin, GrInternalSurfaceFlags surfaceFlags,
Greg Danielb085fa92019-03-05 16:55:12 -0500699 const TextureInfo* textureInfo, SkBackingFit fit, SkBudgeted budgeted,
700 bool wrapsVkSecondaryCB) {
Robert Phillipse8fabb22018-02-04 14:33:21 -0500701 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
702 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400703
Robert Phillipsa41c6852019-02-07 10:44:10 -0500704 if (desc.fWidth > this->caps()->maxRenderTargetSize() ||
705 desc.fHeight > this->caps()->maxRenderTargetSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400706 return nullptr;
707 }
708
Greg Daniel2a303902018-02-20 10:25:54 -0500709 SkASSERT(SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags));
Greg Daniel457469c2018-02-08 15:05:44 -0500710
Greg Daniel2a303902018-02-20 10:25:54 -0500711#ifdef SK_DEBUG
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400712 if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kMixedSampled)) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500713 SkASSERT(this->caps()->usesMixedSamples() && desc.fSampleCnt > 1);
Greg Daniel2a303902018-02-20 10:25:54 -0500714 }
Greg Daniel2a303902018-02-20 10:25:54 -0500715#endif
716
Greg Daniel457469c2018-02-08 15:05:44 -0500717 using LazyInstantiationType = GrSurfaceProxy::LazyInstantiationType;
718 // For non-ddl draws always make lazy proxy's single use.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500719 LazyInstantiationType lazyType = this->renderingDirectly() ? LazyInstantiationType::kSingleUse
720 : LazyInstantiationType::kMultipleUse;
Greg Daniel457469c2018-02-08 15:05:44 -0500721
Brian Salomon7226c232018-07-30 13:13:17 -0400722 if (textureInfo) {
Greg Danielb085fa92019-03-05 16:55:12 -0500723 // Wrapped vulkan secondary command buffers don't support texturing since we won't have an
724 // actual VkImage to texture from.
725 SkASSERT(!wrapsVkSecondaryCB);
Brian Salomon7226c232018-07-30 13:13:17 -0400726 return sk_sp<GrRenderTargetProxy>(new GrTextureRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500727 std::move(callback), lazyType, format, desc, origin, textureInfo->fMipMapped,
728 fit, budgeted, surfaceFlags));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500729 }
730
Greg Danielb085fa92019-03-05 16:55:12 -0500731 GrRenderTargetProxy::WrapsVkSecondaryCB vkSCB =
732 wrapsVkSecondaryCB ? GrRenderTargetProxy::WrapsVkSecondaryCB::kYes
733 : GrRenderTargetProxy::WrapsVkSecondaryCB::kNo;
734
Brian Salomon2a4f9832018-03-03 22:43:43 -0500735 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
Greg Danielb085fa92019-03-05 16:55:12 -0500736 std::move(callback), lazyType, format, desc, origin, fit, budgeted, surfaceFlags,
737 vkSCB));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500738}
739
Chris Dalton4c458b12018-06-16 17:22:59 -0600740sk_sp<GrTextureProxy> GrProxyProvider::MakeFullyLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500741 const GrBackendFormat& format,
Chris Dalton4c458b12018-06-16 17:22:59 -0600742 Renderable renderable,
743 GrSurfaceOrigin origin,
744 GrPixelConfig config,
745 const GrCaps& caps) {
Robert Phillips777707b2018-01-17 11:40:14 -0500746 GrSurfaceDesc desc;
Greg Daniele3204862018-04-16 11:24:10 -0400747 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNoPendingIO;
Robert Phillips777707b2018-01-17 11:40:14 -0500748 if (Renderable::kYes == renderable) {
749 desc.fFlags = kRenderTarget_GrSurfaceFlag;
750 }
Robert Phillips777707b2018-01-17 11:40:14 -0500751 desc.fWidth = -1;
752 desc.fHeight = -1;
753 desc.fConfig = config;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500754 desc.fSampleCnt = 1;
Robert Phillips777707b2018-01-17 11:40:14 -0500755
Chris Dalton4c458b12018-06-16 17:22:59 -0600756 return sk_sp<GrTextureProxy>(
757 (Renderable::kYes == renderable)
Brian Salomon7226c232018-07-30 13:13:17 -0400758 ? new GrTextureRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500759 std::move(callback), LazyInstantiationType::kSingleUse, format, desc,
760 origin, GrMipMapped::kNo, SkBackingFit::kApprox, SkBudgeted::kYes,
761 surfaceFlags)
Chris Dalton4c458b12018-06-16 17:22:59 -0600762 : new GrTextureProxy(std::move(callback), LazyInstantiationType::kSingleUse,
Greg Daniel4065d452018-11-16 15:43:41 -0500763 format, desc, origin, GrMipMapped::kNo,
Brian Salomon7226c232018-07-30 13:13:17 -0400764 SkBackingFit::kApprox, SkBudgeted::kYes, surfaceFlags));
Robert Phillips777707b2018-01-17 11:40:14 -0500765}
766
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500767bool GrProxyProvider::IsFunctionallyExact(GrSurfaceProxy* proxy) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400768 const bool isInstantiated = proxy->isInstantiated();
Robert Phillipsdb3b9792018-06-12 15:18:00 -0400769 // A proxy is functionally exact if:
770 // it is exact (obvs)
771 // when it is instantiated it will be exact (i.e., power of two dimensions)
772 // it is already instantiated and the proxy covers the entire backing surface
773 return proxy->priv().isExact() ||
774 (!isInstantiated && SkIsPow2(proxy->width()) && SkIsPow2(proxy->height())) ||
775 (isInstantiated && proxy->worstCaseWidth() == proxy->width() &&
776 proxy->worstCaseHeight() == proxy->height());
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500777}
778
Robert Phillips427966a2018-12-20 17:20:43 -0500779void GrProxyProvider::processInvalidUniqueKey(const GrUniqueKey& key, GrTextureProxy* proxy,
780 InvalidateGPUResource invalidateGPUResource) {
Chris Dalton2de13dd2019-01-03 15:11:59 -0700781 SkASSERT(key.isValid());
782
Robert Phillips427966a2018-12-20 17:20:43 -0500783 if (!proxy) {
784 proxy = fUniquelyKeyedProxies.find(key);
785 }
Chris Dalton2de13dd2019-01-03 15:11:59 -0700786 SkASSERT(!proxy || proxy->getUniqueKey() == key);
787
788 // Locate the corresponding GrGpuResource (if it needs to be invalidated) before clearing the
789 // proxy's unique key. We must do it in this order because 'key' may alias the proxy's key.
790 sk_sp<GrGpuResource> invalidGpuResource;
791 if (InvalidateGPUResource::kYes == invalidateGPUResource) {
792 if (proxy && proxy->isInstantiated()) {
793 invalidGpuResource = sk_ref_sp(proxy->peekSurface());
794 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500795 if (!invalidGpuResource) {
796 GrContext* direct = fImageContext->priv().asDirectContext();
797 if (direct) {
798 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
799 invalidGpuResource = resourceProvider->findByUniqueKey<GrGpuResource>(key);
800 }
Chris Dalton2de13dd2019-01-03 15:11:59 -0700801 }
802 SkASSERT(!invalidGpuResource || invalidGpuResource->getUniqueKey() == key);
803 }
Robert Phillips427966a2018-12-20 17:20:43 -0500804
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500805 // Note: this method is called for the whole variety of GrGpuResources so often 'key'
806 // will not be in 'fUniquelyKeyedProxies'.
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500807 if (proxy) {
Robert Phillips427966a2018-12-20 17:20:43 -0500808 fUniquelyKeyedProxies.remove(key);
809 proxy->cacheAccess().clearUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500810 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500811
Chris Dalton2de13dd2019-01-03 15:11:59 -0700812 if (invalidGpuResource) {
813 invalidGpuResource->resourcePriv().removeUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500814 }
815}
816
Robert Phillipsa41c6852019-02-07 10:44:10 -0500817uint32_t GrProxyProvider::contextID() const {
818 return fImageContext->priv().contextID();
819}
820
821const GrCaps* GrProxyProvider::caps() const {
822 return fImageContext->priv().caps();
823}
824
825sk_sp<const GrCaps> GrProxyProvider::refCaps() const {
826 return fImageContext->priv().refCaps();
827}
828
Robert Phillipsa9162df2019-02-11 14:12:03 -0500829bool GrProxyProvider::isAbandoned() const {
830 return fImageContext->priv().abandoned();
831}
832
Robert Phillips0790f8a2018-09-18 13:11:03 -0400833void GrProxyProvider::orphanAllUniqueKeys() {
834 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
835 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
836 GrTextureProxy& tmp = *iter;
837
838 tmp.fProxyProvider = nullptr;
839 }
840}
841
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500842void GrProxyProvider::removeAllUniqueKeys() {
843 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
844 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
845 GrTextureProxy& tmp = *iter;
846
Chris Dalton2de13dd2019-01-03 15:11:59 -0700847 this->processInvalidUniqueKey(tmp.getUniqueKey(), &tmp, InvalidateGPUResource::kNo);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500848 }
849 SkASSERT(!fUniquelyKeyedProxies.count());
850}
Robert Phillipsa41c6852019-02-07 10:44:10 -0500851
852bool GrProxyProvider::renderingDirectly() const {
853 return fImageContext->priv().asDirectContext();
854}