blob: d31e3d8a4d7849c0f44191a6e410161a8c12b051 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrProxyProvider.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkBitmap.h"
11#include "include/core/SkImage.h"
12#include "include/gpu/GrContext.h"
13#include "include/gpu/GrRenderTarget.h"
14#include "include/gpu/GrTexture.h"
15#include "include/private/GrImageContext.h"
16#include "include/private/GrResourceKey.h"
17#include "include/private/GrSingleOwner.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "include/private/SkImageInfoPriv.h"
19#include "src/core/SkAutoPixmapStorage.h"
20#include "src/core/SkImagePriv.h"
21#include "src/core/SkMipMap.h"
22#include "src/core/SkTraceEvent.h"
23#include "src/gpu/GrCaps.h"
24#include "src/gpu/GrContextPriv.h"
25#include "src/gpu/GrImageContextPriv.h"
26#include "src/gpu/GrResourceProvider.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040027#include "src/gpu/GrSurfaceProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/gpu/GrSurfaceProxyPriv.h"
29#include "src/gpu/GrTextureProxyCacheAccess.h"
30#include "src/gpu/GrTextureRenderTargetProxy.h"
31#include "src/gpu/SkGr.h"
32#include "src/image/SkImage_Base.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);
Brian Salomon01ceae92019-04-02 11:49:54 -0400106 if (proxy) {
Robert Phillipse5f73282019-06-18 17:15:04 -0400107 SkASSERT(proxy->getProxyRefCnt() >= 1);
108 SkASSERT(proxy->origin() == origin);
109 return sk_ref_sp(proxy);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500110 }
Robert Phillipse5f73282019-06-18 17:15:04 -0400111 return nullptr;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500112}
113
Robert Phillipsa41c6852019-02-07 10:44:10 -0500114///////////////////////////////////////////////////////////////////////////////
115
116#if GR_TEST_UTILS
117sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createInstantiatedProxy(
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400118 const GrSurfaceDesc& desc, GrRenderable renderable, GrSurfaceOrigin origin,
Brian Salomone8a766b2019-07-19 14:24:36 -0400119 SkBackingFit fit, SkBudgeted budgeted, GrProtected isProtected) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500120 GrContext* direct = fImageContext->priv().asDirectContext();
121 if (!direct) {
122 return nullptr;
123 }
124
125 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
126 sk_sp<GrTexture> tex;
127
128 if (SkBackingFit::kApprox == fit) {
Brian Salomone8a766b2019-07-19 14:24:36 -0400129 tex = resourceProvider->createApproxTexture(desc, renderable, isProtected,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400130 GrResourceProvider::Flags::kNoPendingIO);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500131 } else {
Brian Salomone8a766b2019-07-19 14:24:36 -0400132 tex = resourceProvider->createTexture(desc, renderable, budgeted, isProtected,
Robert Phillips9313aa72019-04-09 18:41:27 -0400133 GrResourceProvider::Flags::kNoPendingIO);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500134 }
135 if (!tex) {
136 return nullptr;
137 }
138
139 return this->createWrapped(std::move(tex), origin);
140}
141
142sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createWrapped(sk_sp<GrTexture> tex,
143 GrSurfaceOrigin origin) {
144 return this->createWrapped(std::move(tex), origin);
145}
146#endif
147
Robert Phillipsadbe1322018-01-17 13:35:46 -0500148sk_sp<GrTextureProxy> GrProxyProvider::createWrapped(sk_sp<GrTexture> tex, GrSurfaceOrigin origin) {
149#ifdef SK_DEBUG
150 if (tex->getUniqueKey().isValid()) {
151 SkASSERT(!this->findProxyByUniqueKey(tex->getUniqueKey(), origin));
152 }
153#endif
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400154 GrColorType colorType = GrPixelConfigToColorType(tex->config());
155 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(tex->backendFormat(), colorType);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500156
157 if (tex->asRenderTarget()) {
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400158 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(tex->backendFormat(), colorType);
159 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin,
160 texSwizzle, outSwizzle));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500161 } else {
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400162 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin, texSwizzle));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500163 }
164}
165
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500166sk_sp<GrTextureProxy> GrProxyProvider::findOrCreateProxyByUniqueKey(const GrUniqueKey& key,
167 GrSurfaceOrigin origin) {
168 ASSERT_SINGLE_OWNER
169
170 if (this->isAbandoned()) {
171 return nullptr;
172 }
173
174 sk_sp<GrTextureProxy> result = this->findProxyByUniqueKey(key, origin);
175 if (result) {
176 return result;
177 }
178
Robert Phillipsa41c6852019-02-07 10:44:10 -0500179 GrContext* direct = fImageContext->priv().asDirectContext();
180 if (!direct) {
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500181 return nullptr;
182 }
183
Robert Phillipsa41c6852019-02-07 10:44:10 -0500184 GrResourceCache* resourceCache = direct->priv().getResourceCache();
185
186 GrGpuResource* resource = resourceCache->findAndRefUniqueResource(key);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500187 if (!resource) {
188 return nullptr;
189 }
190
191 sk_sp<GrTexture> texture(static_cast<GrSurface*>(resource)->asTexture());
192 SkASSERT(texture);
193
Robert Phillipsadbe1322018-01-17 13:35:46 -0500194 result = this->createWrapped(std::move(texture), origin);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500195 SkASSERT(result->getUniqueKey() == key);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500196 // createWrapped should've added this for us
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500197 SkASSERT(fUniquelyKeyedProxies.find(key));
198 return result;
199}
200
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500201sk_sp<GrTextureProxy> GrProxyProvider::createTextureProxy(sk_sp<SkImage> srcImage,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400202 GrRenderable renderable,
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500203 int sampleCnt,
Greg Danielfb3abcd2018-02-02 15:48:33 -0500204 SkBudgeted budgeted,
Chris Daltond004e0b2018-09-27 09:28:03 -0600205 SkBackingFit fit,
206 GrInternalSurfaceFlags surfaceFlags) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500207 ASSERT_SINGLE_OWNER
208 SkASSERT(srcImage);
209
210 if (this->isAbandoned()) {
211 return nullptr;
212 }
213
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400214 const SkImageInfo& info = srcImage->imageInfo();
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400215 SkColorType ct = info.colorType();
Greg Daniel2f2caea2019-07-08 14:24:47 -0400216 GrColorType grCT = SkColorTypeToGrColorType(ct);
Greg Danielf87651e2018-02-21 11:36:53 -0500217
Greg Daniel627d0532019-07-08 16:48:14 -0400218 GrBackendFormat format = this->caps()->getBackendFormatFromColorType(grCT);
Greg Daniel0a7aa142018-02-21 13:02:32 -0500219
Greg Daniel7b1bebf2019-07-16 17:09:59 -0400220 if (!format.isValid() || !this->caps()->isFormatTexturable(grCT, format)) {
Brian Osmand29dcd12018-09-13 15:04:29 -0400221 SkBitmap copy8888;
222 if (!copy8888.tryAllocPixels(info.makeColorType(kRGBA_8888_SkColorType)) ||
223 !srcImage->readPixels(copy8888.pixmap(), 0, 0)) {
224 return nullptr;
225 }
226 copy8888.setImmutable();
227 srcImage = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode);
Jim Van Verthc8098322019-04-16 10:50:01 -0400228 ct = kRGBA_8888_SkColorType;
Greg Daniel627d0532019-07-08 16:48:14 -0400229 grCT = GrColorType::kRGBA_8888;
230 format = this->caps()->getBackendFormatFromColorType(grCT);
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400231 if (!format.isValid()) {
232 return nullptr;
233 }
Brian Osmand29dcd12018-09-13 15:04:29 -0400234 }
235
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400236 if (renderable == GrRenderable::kYes) {
Greg Daniel5c96db82019-07-09 14:06:58 -0400237 sampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, grCT, format);
Greg Danielf87651e2018-02-21 11:36:53 -0500238 if (!sampleCnt) {
239 return nullptr;
240 }
241 }
242
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400243 GrPixelConfig config = SkColorType2GrPixelConfig(ct);
244 if (kUnknown_GrPixelConfig == config) {
245 return nullptr;
246 }
247
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500248 GrSurfaceDesc desc;
249 desc.fWidth = srcImage->width();
250 desc.fHeight = srcImage->height();
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500251 desc.fSampleCnt = sampleCnt;
Greg Danielf87651e2018-02-21 11:36:53 -0500252 desc.fConfig = config;
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500253
254 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400255 [desc, renderable, budgeted, srcImage, fit](GrResourceProvider* resourceProvider) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500256 SkPixmap pixMap;
257 SkAssertResult(srcImage->peekPixels(&pixMap));
258 GrMipLevel mipLevel = { pixMap.addr(), pixMap.rowBytes() };
259
Brian Salomone8a766b2019-07-19 14:24:36 -0400260 return LazyInstantiationResult(resourceProvider->createTexture(
261 desc, renderable, budgeted, fit, GrProtected::kNo, mipLevel,
262 GrResourceProvider::Flags::kNoPendingIO));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500263 },
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400264 format, desc, renderable, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, surfaceFlags, fit,
Brian Salomone8a766b2019-07-19 14:24:36 -0400265 budgeted, GrProtected::kNo);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500266
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400267 if (!proxy) {
268 return nullptr;
269 }
270
Robert Phillipsa41c6852019-02-07 10:44:10 -0500271 GrContext* direct = fImageContext->priv().asDirectContext();
272 if (direct) {
273 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
274
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500275 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
276 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500277 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500278 return nullptr;
279 }
280 }
Robert Phillipsc1b60662018-06-26 10:20:08 -0400281
282 SkASSERT(proxy->width() == desc.fWidth);
283 SkASSERT(proxy->height() == desc.fHeight);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500284 return proxy;
285}
286
Greg Daniel4065d452018-11-16 15:43:41 -0500287sk_sp<GrTextureProxy> GrProxyProvider::createMipMapProxy(const GrBackendFormat& format,
288 const GrSurfaceDesc& desc,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400289 GrRenderable renderable,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500290 GrSurfaceOrigin origin,
Brian Salomone8a766b2019-07-19 14:24:36 -0400291 SkBudgeted budgeted,
292 GrProtected isProtected) {
Robert Phillips579f0942018-01-08 14:53:35 -0500293 ASSERT_SINGLE_OWNER
294
295 if (this->isAbandoned()) {
296 return nullptr;
297 }
298
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400299 return this->createProxy(format, desc, renderable, origin, GrMipMapped::kYes,
Brian Salomone8a766b2019-07-19 14:24:36 -0400300 SkBackingFit::kExact, budgeted, isProtected,
301 GrInternalSurfaceFlags::kNone);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500302}
303
Brian Osmande496652019-03-22 13:42:33 -0400304sk_sp<GrTextureProxy> GrProxyProvider::createProxyFromBitmap(const SkBitmap& bitmap,
305 GrMipMapped mipMapped) {
Brian Osman412674f2019-02-07 15:34:58 -0500306 ASSERT_SINGLE_OWNER
Brian Osman7dcc6162019-03-25 10:12:57 -0400307 SkASSERT(GrMipMapped::kNo == mipMapped || this->caps()->mipMapSupport());
Brian Osman412674f2019-02-07 15:34:58 -0500308
309 if (this->isAbandoned()) {
310 return nullptr;
311 }
312
Brian Osman2b23c4b2018-06-01 12:25:08 -0400313 if (!SkImageInfoIsValid(bitmap.info())) {
Greg Daniela4ead652018-02-07 10:21:48 -0500314 return nullptr;
315 }
316
Brian Osmande496652019-03-22 13:42:33 -0400317 ATRACE_ANDROID_FRAMEWORK("Upload %sTexture [%ux%u]",
318 GrMipMapped::kYes == mipMapped ? "MipMap " : "",
319 bitmap.width(), bitmap.height());
Greg Daniela4ead652018-02-07 10:21:48 -0500320
321 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
322 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
323 // 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 -0500324 SkCopyPixelsMode copyMode = this->renderingDirectly() ? kNever_SkCopyPixelsMode
325 : kIfMutable_SkCopyPixelsMode;
Greg Daniela4ead652018-02-07 10:21:48 -0500326 sk_sp<SkImage> baseLevel = SkMakeImageFromRasterBitmap(bitmap, copyMode);
Greg Daniela4ead652018-02-07 10:21:48 -0500327 if (!baseLevel) {
328 return nullptr;
329 }
330
Brian Osmande496652019-03-22 13:42:33 -0400331 // If mips weren't requested (or this was too small to have any), then take the fast path
332 if (GrMipMapped::kNo == mipMapped ||
333 0 == SkMipMap::ComputeLevelCount(baseLevel->width(), baseLevel->height())) {
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400334 return this->createTextureProxy(std::move(baseLevel), GrRenderable::kNo, 1,
Brian Osman7dcc6162019-03-25 10:12:57 -0400335 SkBudgeted::kYes, SkBackingFit::kExact);
Greg Daniela4ead652018-02-07 10:21:48 -0500336 }
337
Greg Daniel627d0532019-07-08 16:48:14 -0400338 GrColorType grColorType = SkColorTypeToGrColorType(bitmap.info().colorType());
339 GrBackendFormat format = this->caps()->getBackendFormatFromColorType(grColorType);
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400340 if (!format.isValid()) {
341 return nullptr;
342 }
Brian Osmand29dcd12018-09-13 15:04:29 -0400343 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info());
Greg Daniel3fc5df42019-06-14 09:42:17 -0400344
Greg Daniel2f2caea2019-07-08 14:24:47 -0400345 if (!this->caps()->isFormatTexturable(grColorType, format)) {
Brian Osmand29dcd12018-09-13 15:04:29 -0400346 SkBitmap copy8888;
347 if (!copy8888.tryAllocPixels(bitmap.info().makeColorType(kRGBA_8888_SkColorType)) ||
348 !bitmap.readPixels(copy8888.pixmap())) {
349 return nullptr;
350 }
351 copy8888.setImmutable();
352 baseLevel = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode);
353 desc.fConfig = kRGBA_8888_GrPixelConfig;
Greg Daniel627d0532019-07-08 16:48:14 -0400354 grColorType = GrColorType::kRGBA_8888;
355 format = this->caps()->getBackendFormatFromColorType(grColorType);
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400356 if (!format.isValid()) {
357 return nullptr;
358 }
Greg Daniel3fc5df42019-06-14 09:42:17 -0400359 }
360
Brian Osmand29dcd12018-09-13 15:04:29 -0400361
362 SkPixmap pixmap;
363 SkAssertResult(baseLevel->peekPixels(&pixmap));
364 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(pixmap, nullptr));
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400365 if (!mipmaps) {
366 return nullptr;
367 }
368
Greg Daniela4ead652018-02-07 10:21:48 -0500369 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Osman1b97f132018-09-13 17:33:48 +0000370 [desc, baseLevel, mipmaps](GrResourceProvider* resourceProvider) {
Brian Osman1b97f132018-09-13 17:33:48 +0000371 const int mipLevelCount = mipmaps->countLevels() + 1;
372 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
373
Greg Daniela4ead652018-02-07 10:21:48 -0500374 SkPixmap pixmap;
375 SkAssertResult(baseLevel->peekPixels(&pixmap));
376
377 // DDL TODO: Instead of copying all this info into GrMipLevels we should just plumb
378 // the use of SkMipMap down through Ganesh.
379 texels[0].fPixels = pixmap.addr();
380 texels[0].fRowBytes = pixmap.rowBytes();
381
382 for (int i = 1; i < mipLevelCount; ++i) {
383 SkMipMap::Level generatedMipLevel;
384 mipmaps->getLevel(i - 1, &generatedMipLevel);
385 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
386 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
387 SkASSERT(texels[i].fPixels);
388 }
389
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400390 return LazyInstantiationResult(resourceProvider->createTexture(
Brian Salomone8a766b2019-07-19 14:24:36 -0400391 desc, GrRenderable::kNo, SkBudgeted::kYes, GrProtected::kNo, texels.get(),
392 mipLevelCount));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500393 },
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400394 format, desc, GrRenderable::kNo, kTopLeft_GrSurfaceOrigin, GrMipMapped::kYes,
Brian Salomone8a766b2019-07-19 14:24:36 -0400395 SkBackingFit::kExact, SkBudgeted::kYes, GrProtected::kNo);
Greg Daniela4ead652018-02-07 10:21:48 -0500396
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400397 if (!proxy) {
398 return nullptr;
399 }
400
Robert Phillipsa41c6852019-02-07 10:44:10 -0500401 GrContext* direct = fImageContext->priv().asDirectContext();
402 if (direct) {
403 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Daniela4ead652018-02-07 10:21:48 -0500404 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
405 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500406 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Greg Daniela4ead652018-02-07 10:21:48 -0500407 return nullptr;
408 }
409 }
410 return proxy;
411}
412
Greg Danielfa55f2e2019-06-13 15:21:38 -0400413#ifdef SK_DEBUG
414static bool validate_backend_format_and_config(const GrCaps* caps,
415 const GrBackendFormat& format,
416 GrPixelConfig config) {
417 if (kUnknown_GrPixelConfig == config) {
418 return false;
419 }
Brian Salomonbb8dde82019-06-27 10:52:13 -0400420 if (GrPixelConfigIsCompressed(config)) {
421 // We have no way to verify these at the moment.
422 return true;
423 }
Robert Phillipsc046ff02019-07-01 10:34:03 -0400424
Robert Phillips1e2cb442019-07-02 15:51:28 -0400425 GrColorType grCT = GrPixelConfigToColorType(config);
426
427 return caps->areColorTypeAndFormatCompatible(grCT, format);
Greg Danielfa55f2e2019-06-13 15:21:38 -0400428}
Robert Phillips0902c982019-07-16 07:47:56 -0400429
430static bool validate_backend_format_and_colortype(const GrCaps* caps,
431 GrColorType colorType,
432 const GrBackendFormat& format) {
433 return caps->areColorTypeAndFormatCompatible(colorType, format);
434}
Greg Danielfa55f2e2019-06-13 15:21:38 -0400435#endif
436
Greg Daniel4065d452018-11-16 15:43:41 -0500437sk_sp<GrTextureProxy> GrProxyProvider::createProxy(const GrBackendFormat& format,
438 const GrSurfaceDesc& desc,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400439 GrRenderable renderable,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500440 GrSurfaceOrigin origin,
Greg Danielf6f7b672018-02-15 13:06:26 -0500441 GrMipMapped mipMapped,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500442 SkBackingFit fit,
443 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -0400444 GrProtected isProtected,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400445 GrInternalSurfaceFlags surfaceFlags) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400446 if (GrPixelConfigIsCompressed(desc.fConfig)) {
447 // Deferred proxies for compressed textures are not supported.
448 return nullptr;
449 }
Robert Phillips0902c982019-07-16 07:47:56 -0400450
451 const GrCaps* caps = this->caps();
452 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
453
454 SkASSERT(GrCaps::AreConfigsCompatible(desc.fConfig,
455 caps->getConfigFromBackendFormat(format, colorType)));
456 SkASSERT(validate_backend_format_and_colortype(caps, colorType, format));
Greg Danielf6f7b672018-02-15 13:06:26 -0500457 if (GrMipMapped::kYes == mipMapped) {
458 // SkMipMap doesn't include the base level in the level count so we have to add 1
459 int mipCount = SkMipMap::ComputeLevelCount(desc.fWidth, desc.fHeight) + 1;
460 if (1 == mipCount) {
461 mipMapped = GrMipMapped::kNo;
462 }
463 }
464
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400465 if (!caps->validateSurfaceDesc(desc, renderable, mipMapped)) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500466 return nullptr;
467 }
Brian Salomon3a2cc2c2018-02-03 00:25:12 +0000468 GrSurfaceDesc copyDesc = desc;
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400469 if (renderable == GrRenderable::kYes) {
Robert Phillips0902c982019-07-16 07:47:56 -0400470 copyDesc.fSampleCnt = caps->getRenderTargetSampleCount(desc.fSampleCnt, colorType, format);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500471 }
Brian Salomon3a2cc2c2018-02-03 00:25:12 +0000472
Robert Phillips0902c982019-07-16 07:47:56 -0400473 GrSwizzle texSwizzle = caps->getTextureSwizzle(format, colorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400474
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400475 if (renderable == GrRenderable::kYes) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500476 // We know anything we instantiate later from this deferred path will be
477 // both texturable and renderable
Robert Phillips0902c982019-07-16 07:47:56 -0400478 GrSwizzle outSwizzle = caps->getOutputSwizzle(format, colorType);
479 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(*caps, format, copyDesc,
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400480 origin, mipMapped, texSwizzle,
481 outSwizzle, fit, budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -0400482 isProtected, surfaceFlags));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500483 }
484
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400485 return sk_sp<GrTextureProxy>(new GrTextureProxy(format, copyDesc, origin, mipMapped, texSwizzle,
Brian Salomone8a766b2019-07-19 14:24:36 -0400486 fit, budgeted, isProtected, surfaceFlags));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500487}
488
Brian Salomonbb8dde82019-06-27 10:52:13 -0400489sk_sp<GrTextureProxy> GrProxyProvider::createCompressedTextureProxy(
490 int width, int height, SkBudgeted budgeted, SkImage::CompressionType compressionType,
491 sk_sp<SkData> data) {
492 GrBackendFormat format = this->caps()->getBackendFormatFromCompressionType(compressionType);
493
494 GrSurfaceDesc desc;
495 desc.fConfig = GrCompressionTypePixelConfig(compressionType);
496 desc.fWidth = width;
497 desc.fHeight = height;
498
Jim Van Verthee06b332019-01-18 10:36:32 -0500499 if (!this->caps()->isConfigTexturable(desc.fConfig)) {
500 return nullptr;
501 }
502
Jim Van Verthee06b332019-01-18 10:36:32 -0500503 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomonbb8dde82019-06-27 10:52:13 -0400504 [width, height, compressionType, budgeted, data](GrResourceProvider* resourceProvider) {
505 return LazyInstantiationResult(resourceProvider->createCompressedTexture(
506 width, height, compressionType, budgeted, data.get()));
507 },
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400508 format, desc, GrRenderable::kNo, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo,
Brian Salomone8a766b2019-07-19 14:24:36 -0400509 SkBackingFit::kExact, SkBudgeted::kYes, GrProtected::kNo);
Jim Van Verthee06b332019-01-18 10:36:32 -0500510
511 if (!proxy) {
512 return nullptr;
513 }
514
Robert Phillipsa41c6852019-02-07 10:44:10 -0500515 GrContext* direct = fImageContext->priv().asDirectContext();
516 if (direct) {
517 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Jim Van Verthee06b332019-01-18 10:36:32 -0500518 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
519 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500520 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500521 return nullptr;
522 }
523 }
524 return proxy;
525}
526
Brian Salomon7578f3e2018-03-07 14:39:54 -0500527sk_sp<GrTextureProxy> GrProxyProvider::wrapBackendTexture(const GrBackendTexture& backendTex,
528 GrSurfaceOrigin origin,
529 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500530 GrWrapCacheable cacheable,
Brian Salomonc67c31c2018-12-06 10:00:03 -0500531 GrIOType ioType,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500532 ReleaseProc releaseProc,
533 ReleaseContext releaseCtx) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500534 SkASSERT(ioType != kWrite_GrIOType);
Robert Phillipsf9bec202018-01-16 09:21:01 -0500535 if (this->isAbandoned()) {
536 return nullptr;
537 }
538
Brian Salomonf7778972018-03-08 10:13:17 -0500539 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500540 GrContext* direct = fImageContext->priv().asDirectContext();
541 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500542 return nullptr;
543 }
544
Robert Phillipsdd399802019-07-18 12:28:00 +0000545 SkASSERT(validate_backend_format_and_config(this->caps(), backendTex.getBackendFormat(),
546 backendTex.config()));
Greg Danielfa55f2e2019-06-13 15:21:38 -0400547
Robert Phillipsa41c6852019-02-07 10:44:10 -0500548 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
549
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500550 sk_sp<GrTexture> tex =
Robert Phillipsdd399802019-07-18 12:28:00 +0000551 resourceProvider->wrapBackendTexture(backendTex, ownership, cacheable, ioType);
Brian Salomonf7778972018-03-08 10:13:17 -0500552 if (!tex) {
553 return nullptr;
554 }
Robert Phillipsadbe1322018-01-17 13:35:46 -0500555
Greg Daniel6a0176b2018-01-30 09:28:44 -0500556 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500557 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel6a0176b2018-01-30 09:28:44 -0500558 }
559
Brian Salomonf7778972018-03-08 10:13:17 -0500560 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
561 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500562 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500563
Robert Phillipsdd399802019-07-18 12:28:00 +0000564 GrColorType colorType = GrPixelConfigToColorType(tex->config());
565 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(tex->backendFormat(), colorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400566
567 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin, texSwizzle));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500568}
569
Brian Salomon7578f3e2018-03-07 14:39:54 -0500570sk_sp<GrTextureProxy> GrProxyProvider::wrapRenderableBackendTexture(
Brian Salomon02bd2952018-03-07 15:20:21 -0500571 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400572 GrColorType colorType, GrWrapOwnership ownership, GrWrapCacheable cacheable,
573 ReleaseProc releaseProc, ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500574 if (this->isAbandoned()) {
575 return nullptr;
576 }
577
Brian Salomonf7778972018-03-08 10:13:17 -0500578 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500579 GrContext* direct = fImageContext->priv().asDirectContext();
580 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500581 return nullptr;
582 }
583
Robert Phillips0902c982019-07-16 07:47:56 -0400584 const GrCaps* caps = this->caps();
585
586 SkASSERT(GrCaps::AreConfigsCompatible(backendTex.config(),
587 caps->getConfigFromBackendFormat(
588 backendTex.getBackendFormat(),
589 colorType)));
590 SkASSERT(validate_backend_format_and_colortype(caps, colorType, backendTex.getBackendFormat()));
Greg Danielfa55f2e2019-06-13 15:21:38 -0400591
Robert Phillipsa41c6852019-02-07 10:44:10 -0500592 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
593
Robert Phillips0902c982019-07-16 07:47:56 -0400594 sampleCnt = caps->getRenderTargetSampleCount(sampleCnt, colorType,
595 backendTex.getBackendFormat());
Greg Danielf87651e2018-02-21 11:36:53 -0500596 if (!sampleCnt) {
597 return nullptr;
598 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500599
Robert Phillipsa41c6852019-02-07 10:44:10 -0500600 sk_sp<GrTexture> tex = resourceProvider->wrapRenderableBackendTexture(backendTex, sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400601 colorType, ownership,
602 cacheable);
Brian Salomonf7778972018-03-08 10:13:17 -0500603 if (!tex) {
604 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500605 }
606
Greg Daniel8ce79912019-02-05 10:08:43 -0500607 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500608 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500609 }
610
Brian Salomonf7778972018-03-08 10:13:17 -0500611 SkASSERT(tex->asRenderTarget()); // A GrTextureRenderTarget
612 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500613 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Greg Daniel6abda432018-02-15 14:55:00 -0500614
Robert Phillips0902c982019-07-16 07:47:56 -0400615 GrSwizzle texSwizzle = caps->getTextureSwizzle(tex->backendFormat(), colorType);
616 GrSwizzle outSwizzle = caps->getOutputSwizzle(tex->backendFormat(), colorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400617
618 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin, texSwizzle,
619 outSwizzle));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500620}
621
Brian Salomon7578f3e2018-03-07 14:39:54 -0500622sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget(
Robert Phillipsdd399802019-07-18 12:28:00 +0000623 const GrBackendRenderTarget& backendRT, GrSurfaceOrigin origin, ReleaseProc releaseProc,
624 ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500625 if (this->isAbandoned()) {
626 return nullptr;
627 }
628
Brian Salomonf7778972018-03-08 10:13:17 -0500629 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500630 GrContext* direct = fImageContext->priv().asDirectContext();
631 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500632 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500633 }
634
Robert Phillipsdd399802019-07-18 12:28:00 +0000635 GrColorType colorType = GrPixelConfigToColorType(backendRT.config());
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400636#ifdef SK_DEBUG
Greg Danielfa55f2e2019-06-13 15:21:38 -0400637 GrPixelConfig testConfig =
Robert Phillipsdd399802019-07-18 12:28:00 +0000638 this->caps()->validateBackendRenderTarget(backendRT, colorType);
Greg Danielfa55f2e2019-06-13 15:21:38 -0400639 SkASSERT(testConfig != kUnknown_GrPixelConfig);
640#endif
641
Robert Phillipsa41c6852019-02-07 10:44:10 -0500642 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
643
Robert Phillipsdd399802019-07-18 12:28:00 +0000644 sk_sp<GrRenderTarget> rt = resourceProvider->wrapBackendRenderTarget(backendRT);
Brian Salomonf7778972018-03-08 10:13:17 -0500645 if (!rt) {
646 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500647 }
Greg Daniel8ce79912019-02-05 10:08:43 -0500648
Greg Daniel8ce79912019-02-05 10:08:43 -0500649 if (releaseProc) {
Brian Salomon2ca31f82019-03-05 13:28:58 -0500650 rt->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500651 }
652
Brian Salomonf7778972018-03-08 10:13:17 -0500653 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
654 SkASSERT(!rt->getUniqueKey().isValid());
655 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500656 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Brian Salomonf7778972018-03-08 10:13:17 -0500657
Robert Phillipsdd399802019-07-18 12:28:00 +0000658 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(rt->backendFormat(), colorType);
659 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(rt->backendFormat(), colorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400660
661 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(std::move(rt), origin, texSwizzle,
662 outSwizzle));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500663}
664
Brian Salomon7578f3e2018-03-07 14:39:54 -0500665sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendTextureAsRenderTarget(
Robert Phillipsdd399802019-07-18 12:28:00 +0000666 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500667 if (this->isAbandoned()) {
668 return nullptr;
669 }
670
Brian Salomonf7778972018-03-08 10:13:17 -0500671 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500672 GrContext* direct = fImageContext->priv().asDirectContext();
673 if (!direct) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500674 return nullptr;
675 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500676
Robert Phillipsdd399802019-07-18 12:28:00 +0000677 SkASSERT(validate_backend_format_and_config(this->caps(), backendTex.getBackendFormat(),
678 backendTex.config()));
Greg Danielfa55f2e2019-06-13 15:21:38 -0400679
Robert Phillipsa41c6852019-02-07 10:44:10 -0500680 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
681
Brian Salomonf7778972018-03-08 10:13:17 -0500682 sk_sp<GrRenderTarget> rt =
Robert Phillipsdd399802019-07-18 12:28:00 +0000683 resourceProvider->wrapBackendTextureAsRenderTarget(backendTex, sampleCnt);
Brian Salomonf7778972018-03-08 10:13:17 -0500684 if (!rt) {
685 return nullptr;
Greg Danielf87651e2018-02-21 11:36:53 -0500686 }
Brian Salomonf7778972018-03-08 10:13:17 -0500687 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
688 SkASSERT(!rt->getUniqueKey().isValid());
Greg Danielb46add82019-01-02 14:51:29 -0500689 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500690 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielf87651e2018-02-21 11:36:53 -0500691
Robert Phillipsdd399802019-07-18 12:28:00 +0000692 GrColorType colorType = GrPixelConfigToColorType(rt->config());
693 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(rt->backendFormat(), colorType);
694 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(rt->backendFormat(), colorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400695
696 return sk_sp<GrSurfaceProxy>(new GrRenderTargetProxy(std::move(rt), origin, texSwizzle,
697 outSwizzle));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500698}
699
Greg Danielb46add82019-01-02 14:51:29 -0500700sk_sp<GrRenderTargetProxy> GrProxyProvider::wrapVulkanSecondaryCBAsRenderTarget(
701 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
702 if (this->isAbandoned()) {
703 return nullptr;
704 }
705
706 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500707 GrContext* direct = fImageContext->priv().asDirectContext();
708 if (!direct) {
Greg Danielb46add82019-01-02 14:51:29 -0500709 return nullptr;
710 }
711
Robert Phillipsa41c6852019-02-07 10:44:10 -0500712 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Danielb46add82019-01-02 14:51:29 -0500713
Robert Phillipsa41c6852019-02-07 10:44:10 -0500714 sk_sp<GrRenderTarget> rt = resourceProvider->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
715 vkInfo);
Greg Danielb46add82019-01-02 14:51:29 -0500716 if (!rt) {
717 return nullptr;
718 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500719
Greg Danielb46add82019-01-02 14:51:29 -0500720 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
721 SkASSERT(!rt->getUniqueKey().isValid());
722 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500723 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielb46add82019-01-02 14:51:29 -0500724
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400725 GrColorType colorType = GrPixelConfigToColorType(rt->config());
726 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(rt->backendFormat(), colorType);
727 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(rt->backendFormat(), colorType);
728
Greg Danielb46add82019-01-02 14:51:29 -0500729 // All Vulkan surfaces uses top left origins.
730 return sk_sp<GrRenderTargetProxy>(
731 new GrRenderTargetProxy(std::move(rt),
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400732 kTopLeft_GrSurfaceOrigin, texSwizzle, outSwizzle,
Greg Danielb46add82019-01-02 14:51:29 -0500733 GrRenderTargetProxy::WrapsVkSecondaryCB::kYes));
734}
735
Robert Phillips777707b2018-01-17 11:40:14 -0500736sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500737 const GrBackendFormat& format,
Robert Phillips777707b2018-01-17 11:40:14 -0500738 const GrSurfaceDesc& desc,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400739 GrRenderable renderable,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500740 GrSurfaceOrigin origin,
Brian Salomon7226c232018-07-30 13:13:17 -0400741 GrMipMapped mipMapped,
Brian Salomon7226c232018-07-30 13:13:17 -0400742 SkBackingFit fit,
Brian Salomone8a766b2019-07-19 14:24:36 -0400743 SkBudgeted budgeted,
744 GrProtected isProtected) {
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400745 return this->createLazyProxy(std::move(callback), format, desc, renderable, origin, mipMapped,
Brian Salomone8a766b2019-07-19 14:24:36 -0400746 GrInternalSurfaceFlags::kNone, fit, budgeted, isProtected);
Greg Daniela8d92112018-03-09 12:05:04 -0500747}
748
749sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500750 const GrBackendFormat& format,
Greg Daniela8d92112018-03-09 12:05:04 -0500751 const GrSurfaceDesc& desc,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400752 GrRenderable renderable,
Greg Daniela8d92112018-03-09 12:05:04 -0500753 GrSurfaceOrigin origin,
754 GrMipMapped mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400755 GrInternalSurfaceFlags surfaceFlags,
Brian Salomon7226c232018-07-30 13:13:17 -0400756 SkBackingFit fit,
757 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -0400758 GrProtected isProtected) {
759 // For non-ddl draws always make lazy proxy's single use.
760 LazyInstantiationType lazyType = this->renderingDirectly() ? LazyInstantiationType::kSingleUse
761 : LazyInstantiationType::kMultipleUse;
762 return this->createLazyProxy(std::move(callback), format, desc, renderable, origin, mipMapped,
763 surfaceFlags, fit, budgeted, isProtected, lazyType);
764}
765
766sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
767 const GrBackendFormat& format,
768 const GrSurfaceDesc& desc,
769 GrRenderable renderable,
770 GrSurfaceOrigin origin,
771 GrMipMapped mipMapped,
772 GrInternalSurfaceFlags surfaceFlags,
773 SkBackingFit fit,
774 SkBudgeted budgeted,
775 GrProtected isProtected,
Greg Daniela8d92112018-03-09 12:05:04 -0500776 LazyInstantiationType lazyType) {
Robert Phillips777707b2018-01-17 11:40:14 -0500777 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
778 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400779
Robert Phillipsa41c6852019-02-07 10:44:10 -0500780 if (desc.fWidth > this->caps()->maxTextureSize() ||
781 desc.fHeight > this->caps()->maxTextureSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400782 return nullptr;
783 }
784
Greg Danielfa55f2e2019-06-13 15:21:38 -0400785 SkASSERT(validate_backend_format_and_config(this->caps(), format, desc.fConfig));
Greg Daniel457469c2018-02-08 15:05:44 -0500786
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400787 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
788 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(format, colorType);
789 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(format, colorType);
790
Brian Salomon2a4f9832018-03-03 22:43:43 -0500791 return sk_sp<GrTextureProxy>(
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400792 renderable == GrRenderable::kYes
Greg Daniel4065d452018-11-16 15:43:41 -0500793 ? new GrTextureRenderTargetProxy(std::move(callback), lazyType, format, desc,
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400794 origin, mipMapped, texSwizzle, outSwizzle, fit,
Brian Salomone8a766b2019-07-19 14:24:36 -0400795 budgeted, isProtected, surfaceFlags)
Greg Daniel4065d452018-11-16 15:43:41 -0500796 : new GrTextureProxy(std::move(callback), lazyType, format, desc, origin,
Brian Salomone8a766b2019-07-19 14:24:36 -0400797 mipMapped, texSwizzle, fit, budgeted, isProtected,
798 surfaceFlags));
Robert Phillips777707b2018-01-17 11:40:14 -0500799}
800
Robert Phillipse8fabb22018-02-04 14:33:21 -0500801sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500802 LazyInstantiateCallback&& callback, const GrBackendFormat& format,
803 const GrSurfaceDesc& desc, GrSurfaceOrigin origin, GrInternalSurfaceFlags surfaceFlags,
Greg Danielb085fa92019-03-05 16:55:12 -0500804 const TextureInfo* textureInfo, SkBackingFit fit, SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -0400805 GrProtected isProtected, bool wrapsVkSecondaryCB) {
Robert Phillipse8fabb22018-02-04 14:33:21 -0500806 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
807 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400808
Robert Phillipsa41c6852019-02-07 10:44:10 -0500809 if (desc.fWidth > this->caps()->maxRenderTargetSize() ||
810 desc.fHeight > this->caps()->maxRenderTargetSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400811 return nullptr;
812 }
813
Greg Danielfa55f2e2019-06-13 15:21:38 -0400814 SkASSERT(validate_backend_format_and_config(this->caps(), format, desc.fConfig));
Greg Daniel457469c2018-02-08 15:05:44 -0500815
816 using LazyInstantiationType = GrSurfaceProxy::LazyInstantiationType;
817 // For non-ddl draws always make lazy proxy's single use.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500818 LazyInstantiationType lazyType = this->renderingDirectly() ? LazyInstantiationType::kSingleUse
819 : LazyInstantiationType::kMultipleUse;
Greg Daniel457469c2018-02-08 15:05:44 -0500820
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400821 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
822 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(format, colorType);
823 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(format, colorType);
824
Brian Salomon7226c232018-07-30 13:13:17 -0400825 if (textureInfo) {
Greg Danielb085fa92019-03-05 16:55:12 -0500826 // Wrapped vulkan secondary command buffers don't support texturing since we won't have an
827 // actual VkImage to texture from.
828 SkASSERT(!wrapsVkSecondaryCB);
Brian Salomon7226c232018-07-30 13:13:17 -0400829 return sk_sp<GrRenderTargetProxy>(new GrTextureRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500830 std::move(callback), lazyType, format, desc, origin, textureInfo->fMipMapped,
Brian Salomone8a766b2019-07-19 14:24:36 -0400831 texSwizzle, outSwizzle, fit, budgeted, isProtected, surfaceFlags));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500832 }
833
Greg Danielb085fa92019-03-05 16:55:12 -0500834 GrRenderTargetProxy::WrapsVkSecondaryCB vkSCB =
835 wrapsVkSecondaryCB ? GrRenderTargetProxy::WrapsVkSecondaryCB::kYes
836 : GrRenderTargetProxy::WrapsVkSecondaryCB::kNo;
837
Brian Salomone8a766b2019-07-19 14:24:36 -0400838 return sk_sp<GrRenderTargetProxy>(
839 new GrRenderTargetProxy(std::move(callback), lazyType, format, desc, origin, texSwizzle,
840 outSwizzle, fit, budgeted, isProtected, surfaceFlags, vkSCB));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500841}
842
Chris Dalton19cc00f2019-04-15 09:06:28 -0600843sk_sp<GrTextureProxy> GrProxyProvider::MakeFullyLazyProxy(
844 LazyInstantiateCallback&& callback, const GrBackendFormat& format, Renderable renderable,
Brian Salomone8a766b2019-07-19 14:24:36 -0400845 GrProtected isProtected, GrSurfaceOrigin origin, GrPixelConfig config, const GrCaps& caps,
846 int sampleCnt) {
Greg Danielfa55f2e2019-06-13 15:21:38 -0400847 SkASSERT(validate_backend_format_and_config(&caps, format, config));
Robert Phillips777707b2018-01-17 11:40:14 -0500848 GrSurfaceDesc desc;
Robert Phillips10d17212019-04-24 14:09:10 -0400849 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNone;
Robert Phillips777707b2018-01-17 11:40:14 -0500850 desc.fWidth = -1;
851 desc.fHeight = -1;
852 desc.fConfig = config;
Chris Dalton19cc00f2019-04-15 09:06:28 -0600853 desc.fSampleCnt = sampleCnt;
Robert Phillips777707b2018-01-17 11:40:14 -0500854
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400855 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
856 GrSwizzle texSwizzle = caps.getTextureSwizzle(format, colorType);
857 GrSwizzle outSwizzle = caps.getOutputSwizzle(format, colorType);
858
Chris Dalton4c458b12018-06-16 17:22:59 -0600859 return sk_sp<GrTextureProxy>(
860 (Renderable::kYes == renderable)
Brian Salomon7226c232018-07-30 13:13:17 -0400861 ? new GrTextureRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500862 std::move(callback), LazyInstantiationType::kSingleUse, format, desc,
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400863 origin, GrMipMapped::kNo, texSwizzle, outSwizzle,
Brian Salomone8a766b2019-07-19 14:24:36 -0400864 SkBackingFit::kApprox, SkBudgeted::kYes, isProtected, surfaceFlags)
Chris Dalton4c458b12018-06-16 17:22:59 -0600865 : new GrTextureProxy(std::move(callback), LazyInstantiationType::kSingleUse,
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400866 format, desc, origin, GrMipMapped::kNo, texSwizzle,
Brian Salomone8a766b2019-07-19 14:24:36 -0400867 SkBackingFit::kApprox, SkBudgeted::kYes, isProtected,
868 surfaceFlags));
Robert Phillips777707b2018-01-17 11:40:14 -0500869}
870
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500871bool GrProxyProvider::IsFunctionallyExact(GrSurfaceProxy* proxy) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400872 const bool isInstantiated = proxy->isInstantiated();
Robert Phillipsdb3b9792018-06-12 15:18:00 -0400873 // A proxy is functionally exact if:
874 // it is exact (obvs)
875 // when it is instantiated it will be exact (i.e., power of two dimensions)
876 // it is already instantiated and the proxy covers the entire backing surface
877 return proxy->priv().isExact() ||
878 (!isInstantiated && SkIsPow2(proxy->width()) && SkIsPow2(proxy->height())) ||
879 (isInstantiated && proxy->worstCaseWidth() == proxy->width() &&
880 proxy->worstCaseHeight() == proxy->height());
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500881}
882
Robert Phillips427966a2018-12-20 17:20:43 -0500883void GrProxyProvider::processInvalidUniqueKey(const GrUniqueKey& key, GrTextureProxy* proxy,
884 InvalidateGPUResource invalidateGPUResource) {
Chris Dalton2de13dd2019-01-03 15:11:59 -0700885 SkASSERT(key.isValid());
886
Robert Phillips427966a2018-12-20 17:20:43 -0500887 if (!proxy) {
888 proxy = fUniquelyKeyedProxies.find(key);
889 }
Chris Dalton2de13dd2019-01-03 15:11:59 -0700890 SkASSERT(!proxy || proxy->getUniqueKey() == key);
891
892 // Locate the corresponding GrGpuResource (if it needs to be invalidated) before clearing the
893 // proxy's unique key. We must do it in this order because 'key' may alias the proxy's key.
894 sk_sp<GrGpuResource> invalidGpuResource;
895 if (InvalidateGPUResource::kYes == invalidateGPUResource) {
Brian Salomon01ceae92019-04-02 11:49:54 -0400896 GrContext* direct = fImageContext->priv().asDirectContext();
897 if (direct) {
898 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
899 invalidGpuResource = resourceProvider->findByUniqueKey<GrGpuResource>(key);
Chris Dalton2de13dd2019-01-03 15:11:59 -0700900 }
901 SkASSERT(!invalidGpuResource || invalidGpuResource->getUniqueKey() == key);
902 }
Robert Phillips427966a2018-12-20 17:20:43 -0500903
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500904 // Note: this method is called for the whole variety of GrGpuResources so often 'key'
905 // will not be in 'fUniquelyKeyedProxies'.
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500906 if (proxy) {
Robert Phillips427966a2018-12-20 17:20:43 -0500907 fUniquelyKeyedProxies.remove(key);
908 proxy->cacheAccess().clearUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500909 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500910
Chris Dalton2de13dd2019-01-03 15:11:59 -0700911 if (invalidGpuResource) {
912 invalidGpuResource->resourcePriv().removeUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500913 }
914}
915
Robert Phillipsa41c6852019-02-07 10:44:10 -0500916uint32_t GrProxyProvider::contextID() const {
917 return fImageContext->priv().contextID();
918}
919
920const GrCaps* GrProxyProvider::caps() const {
921 return fImageContext->priv().caps();
922}
923
924sk_sp<const GrCaps> GrProxyProvider::refCaps() const {
925 return fImageContext->priv().refCaps();
926}
927
Robert Phillipsa9162df2019-02-11 14:12:03 -0500928bool GrProxyProvider::isAbandoned() const {
929 return fImageContext->priv().abandoned();
930}
931
Robert Phillips0790f8a2018-09-18 13:11:03 -0400932void GrProxyProvider::orphanAllUniqueKeys() {
933 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
934 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
935 GrTextureProxy& tmp = *iter;
936
937 tmp.fProxyProvider = nullptr;
938 }
939}
940
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500941void GrProxyProvider::removeAllUniqueKeys() {
942 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
943 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
944 GrTextureProxy& tmp = *iter;
945
Chris Dalton2de13dd2019-01-03 15:11:59 -0700946 this->processInvalidUniqueKey(tmp.getUniqueKey(), &tmp, InvalidateGPUResource::kNo);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500947 }
948 SkASSERT(!fUniquelyKeyedProxies.count());
949}
Robert Phillipsa41c6852019-02-07 10:44:10 -0500950
951bool GrProxyProvider::renderingDirectly() const {
952 return fImageContext->priv().asDirectContext();
953}