blob: e4e0e8407d30cf3b60dbacb1a67c9bb39a2b533f [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/gpu/GrTexture.h"
14#include "include/private/GrImageContext.h"
15#include "include/private/GrResourceKey.h"
16#include "include/private/GrSingleOwner.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "include/private/SkImageInfoPriv.h"
18#include "src/core/SkAutoPixmapStorage.h"
19#include "src/core/SkImagePriv.h"
20#include "src/core/SkMipMap.h"
21#include "src/core/SkTraceEvent.h"
22#include "src/gpu/GrCaps.h"
23#include "src/gpu/GrContextPriv.h"
24#include "src/gpu/GrImageContextPriv.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040025#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#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) {
Brian Salomona036f0d2019-08-29 11:16:04 -0400107 SkASSERT(proxy->refCnt() >= 1);
Robert Phillipse5f73282019-06-18 17:15:04 -0400108 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 Salomon4eb38b72019-08-05 12:58:39 -0400118 const SkISize& size,
119 GrColorType colorType,
120 const GrBackendFormat& format,
121 GrRenderable renderable,
122 int renderTargetSampleCnt,
123 GrSurfaceOrigin origin,
124 SkBackingFit fit,
125 SkBudgeted budgeted,
126 GrProtected isProtected) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500127 GrContext* direct = fImageContext->priv().asDirectContext();
128 if (!direct) {
129 return nullptr;
130 }
131
Brian Salomon4eb38b72019-08-05 12:58:39 -0400132 if (this->caps()->isFormatCompressed(format)) {
133 // TODO: Allow this to go to GrResourceProvider::createCompressedTexture() once we no longer
134 // rely on GrColorType to get to GrPixelConfig. Currently this will cause
135 // makeConfigSpecific() to assert because GrColorTypeToPixelConfig() never returns a
136 // compressed GrPixelConfig.
137 return nullptr;
138 }
139 GrSurfaceDesc desc;
140 desc.fConfig = GrColorTypeToPixelConfig(colorType);
141 desc.fConfig = this->caps()->makeConfigSpecific(desc.fConfig, format);
142 desc.fWidth = size.width();
143 desc.fHeight = size.height();
144
Robert Phillipsa41c6852019-02-07 10:44:10 -0500145 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
146 sk_sp<GrTexture> tex;
147
148 if (SkBackingFit::kApprox == fit) {
Brian Salomon4eb38b72019-08-05 12:58:39 -0400149 tex = resourceProvider->createApproxTexture(desc, format, renderable, renderTargetSampleCnt,
Robert Phillipsaee18c92019-09-06 11:48:27 -0400150 isProtected);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500151 } else {
Brian Salomon4eb38b72019-08-05 12:58:39 -0400152 tex = resourceProvider->createTexture(desc, format, renderable, renderTargetSampleCnt,
Brian Salomona90382f2019-09-17 09:01:56 -0400153 GrMipMapped::kNo, budgeted, isProtected);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500154 }
155 if (!tex) {
156 return nullptr;
157 }
158
Brian Salomonbeb7f522019-08-30 16:19:42 -0400159 return this->createWrapped(std::move(tex), colorType, origin, UseAllocator::kYes);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500160}
161
Brian Salomon4eb38b72019-08-05 12:58:39 -0400162sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createInstantiatedProxy(
163 const SkISize& size,
164 GrColorType colorType,
165 GrRenderable renderable,
166 int renderTargetSampleCnt,
167 GrSurfaceOrigin origin,
168 SkBackingFit fit,
169 SkBudgeted budgeted,
170 GrProtected isProtected) {
171 auto format = this->caps()->getDefaultBackendFormat(colorType, renderable);
172 return this->testingOnly_createInstantiatedProxy(size,
173 colorType,
174 format,
175 renderable,
176 renderTargetSampleCnt,
177 origin,
178 fit,
179 budgeted,
180 isProtected);
181}
182
Robert Phillipsa41c6852019-02-07 10:44:10 -0500183sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createWrapped(sk_sp<GrTexture> tex,
Brian Salomon2af3e702019-08-11 19:10:31 -0400184 GrColorType colorType,
Robert Phillipsa41c6852019-02-07 10:44:10 -0500185 GrSurfaceOrigin origin) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400186 return this->createWrapped(std::move(tex), colorType, origin, UseAllocator::kYes);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500187}
188#endif
189
Brian Salomonbeb7f522019-08-30 16:19:42 -0400190sk_sp<GrTextureProxy> GrProxyProvider::createWrapped(sk_sp<GrTexture> tex,
191 GrColorType colorType,
192 GrSurfaceOrigin origin,
193 UseAllocator useAllocator) {
Robert Phillipsadbe1322018-01-17 13:35:46 -0500194#ifdef SK_DEBUG
195 if (tex->getUniqueKey().isValid()) {
196 SkASSERT(!this->findProxyByUniqueKey(tex->getUniqueKey(), origin));
197 }
198#endif
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400199 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(tex->backendFormat(), colorType);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500200
201 if (tex->asRenderTarget()) {
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400202 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(tex->backendFormat(), colorType);
Brian Salomonbeb7f522019-08-30 16:19:42 -0400203 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
204 std::move(tex), origin, texSwizzle, outSwizzle, useAllocator));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500205 } else {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400206 return sk_sp<GrTextureProxy>(
207 new GrTextureProxy(std::move(tex), origin, texSwizzle, useAllocator));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500208 }
209}
210
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500211sk_sp<GrTextureProxy> GrProxyProvider::findOrCreateProxyByUniqueKey(const GrUniqueKey& key,
Brian Salomon2af3e702019-08-11 19:10:31 -0400212 GrColorType colorType,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400213 GrSurfaceOrigin origin,
214 UseAllocator useAllocator) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500215 ASSERT_SINGLE_OWNER
216
217 if (this->isAbandoned()) {
218 return nullptr;
219 }
220
221 sk_sp<GrTextureProxy> result = this->findProxyByUniqueKey(key, origin);
222 if (result) {
223 return result;
224 }
225
Robert Phillipsa41c6852019-02-07 10:44:10 -0500226 GrContext* direct = fImageContext->priv().asDirectContext();
227 if (!direct) {
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500228 return nullptr;
229 }
230
Robert Phillipsa41c6852019-02-07 10:44:10 -0500231 GrResourceCache* resourceCache = direct->priv().getResourceCache();
232
233 GrGpuResource* resource = resourceCache->findAndRefUniqueResource(key);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500234 if (!resource) {
235 return nullptr;
236 }
237
238 sk_sp<GrTexture> texture(static_cast<GrSurface*>(resource)->asTexture());
239 SkASSERT(texture);
240
Brian Salomonbeb7f522019-08-30 16:19:42 -0400241 result = this->createWrapped(std::move(texture), colorType, origin, useAllocator);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500242 SkASSERT(result->getUniqueKey() == key);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500243 // createWrapped should've added this for us
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500244 SkASSERT(fUniquelyKeyedProxies.find(key));
Brian Salomon2af3e702019-08-11 19:10:31 -0400245 SkASSERT(result->textureSwizzle() ==
246 this->caps()->getTextureSwizzle(result->backendFormat(), colorType));
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500247 return result;
248}
249
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500250sk_sp<GrTextureProxy> GrProxyProvider::createTextureProxy(sk_sp<SkImage> srcImage,
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500251 int sampleCnt,
Greg Danielfb3abcd2018-02-02 15:48:33 -0500252 SkBudgeted budgeted,
Chris Daltond004e0b2018-09-27 09:28:03 -0600253 SkBackingFit fit,
254 GrInternalSurfaceFlags surfaceFlags) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500255 ASSERT_SINGLE_OWNER
256 SkASSERT(srcImage);
257
258 if (this->isAbandoned()) {
259 return nullptr;
260 }
261
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400262 const SkImageInfo& info = srcImage->imageInfo();
Brian Salomon2af3e702019-08-11 19:10:31 -0400263 GrColorType ct = SkColorTypeToGrColorType(info.colorType());
Greg Danielf87651e2018-02-21 11:36:53 -0500264
Brian Salomon96b383a2019-08-13 16:55:41 -0400265 GrBackendFormat format = this->caps()->getDefaultBackendFormat(ct, GrRenderable::kNo);
Greg Daniel0a7aa142018-02-21 13:02:32 -0500266
Robert Phillips0a15cc62019-07-30 12:49:10 -0400267 if (!format.isValid()) {
Brian Osmand29dcd12018-09-13 15:04:29 -0400268 SkBitmap copy8888;
269 if (!copy8888.tryAllocPixels(info.makeColorType(kRGBA_8888_SkColorType)) ||
270 !srcImage->readPixels(copy8888.pixmap(), 0, 0)) {
271 return nullptr;
272 }
273 copy8888.setImmutable();
274 srcImage = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode);
Brian Salomon2af3e702019-08-11 19:10:31 -0400275 ct = GrColorType::kRGBA_8888;
Brian Salomon96b383a2019-08-13 16:55:41 -0400276 format = this->caps()->getDefaultBackendFormat(ct, GrRenderable::kNo);
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400277 if (!format.isValid()) {
278 return nullptr;
279 }
Brian Osmand29dcd12018-09-13 15:04:29 -0400280 }
281
Brian Salomon2af3e702019-08-11 19:10:31 -0400282 GrPixelConfig config = GrColorTypeToPixelConfig(ct);
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400283 if (kUnknown_GrPixelConfig == config) {
284 return nullptr;
285 }
286
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500287 GrSurfaceDesc desc;
288 desc.fWidth = srcImage->width();
289 desc.fHeight = srcImage->height();
Greg Danielf87651e2018-02-21 11:36:53 -0500290 desc.fConfig = config;
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500291
292 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomon96b383a2019-08-13 16:55:41 -0400293 [desc, format, sampleCnt, budgeted, srcImage, fit,
Brian Salomon2af3e702019-08-11 19:10:31 -0400294 ct](GrResourceProvider* resourceProvider) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500295 SkPixmap pixMap;
296 SkAssertResult(srcImage->peekPixels(&pixMap));
297 GrMipLevel mipLevel = { pixMap.addr(), pixMap.rowBytes() };
298
Brian Salomonbeb7f522019-08-30 16:19:42 -0400299 return LazyCallbackResult(resourceProvider->createTexture(
Brian Salomona90382f2019-09-17 09:01:56 -0400300 desc, format, ct, GrRenderable::kNo, sampleCnt, budgeted, fit,
301 GrProtected::kNo, mipLevel));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500302 },
Brian Salomon96b383a2019-08-13 16:55:41 -0400303 format, desc, GrRenderable::kNo, sampleCnt, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400304 GrMipMapsStatus::kNotAllocated, surfaceFlags, fit, budgeted, GrProtected::kNo,
305 UseAllocator::kYes);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500306
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400307 if (!proxy) {
308 return nullptr;
309 }
310
Robert Phillipsa41c6852019-02-07 10:44:10 -0500311 GrContext* direct = fImageContext->priv().asDirectContext();
312 if (direct) {
313 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
314
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500315 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
316 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500317 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500318 return nullptr;
319 }
320 }
Robert Phillipsc1b60662018-06-26 10:20:08 -0400321
322 SkASSERT(proxy->width() == desc.fWidth);
323 SkASSERT(proxy->height() == desc.fHeight);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500324 return proxy;
325}
326
Brian Osmande496652019-03-22 13:42:33 -0400327sk_sp<GrTextureProxy> GrProxyProvider::createProxyFromBitmap(const SkBitmap& bitmap,
328 GrMipMapped mipMapped) {
Brian Osman412674f2019-02-07 15:34:58 -0500329 ASSERT_SINGLE_OWNER
Brian Osman7dcc6162019-03-25 10:12:57 -0400330 SkASSERT(GrMipMapped::kNo == mipMapped || this->caps()->mipMapSupport());
Brian Osman412674f2019-02-07 15:34:58 -0500331
332 if (this->isAbandoned()) {
333 return nullptr;
334 }
335
Brian Osman2b23c4b2018-06-01 12:25:08 -0400336 if (!SkImageInfoIsValid(bitmap.info())) {
Greg Daniela4ead652018-02-07 10:21:48 -0500337 return nullptr;
338 }
339
Brian Osmande496652019-03-22 13:42:33 -0400340 ATRACE_ANDROID_FRAMEWORK("Upload %sTexture [%ux%u]",
341 GrMipMapped::kYes == mipMapped ? "MipMap " : "",
342 bitmap.width(), bitmap.height());
Greg Daniela4ead652018-02-07 10:21:48 -0500343
344 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
345 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
346 // 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 -0500347 SkCopyPixelsMode copyMode = this->renderingDirectly() ? kNever_SkCopyPixelsMode
348 : kIfMutable_SkCopyPixelsMode;
Greg Daniela4ead652018-02-07 10:21:48 -0500349 sk_sp<SkImage> baseLevel = SkMakeImageFromRasterBitmap(bitmap, copyMode);
Greg Daniela4ead652018-02-07 10:21:48 -0500350 if (!baseLevel) {
351 return nullptr;
352 }
353
Brian Osmande496652019-03-22 13:42:33 -0400354 // If mips weren't requested (or this was too small to have any), then take the fast path
355 if (GrMipMapped::kNo == mipMapped ||
356 0 == SkMipMap::ComputeLevelCount(baseLevel->width(), baseLevel->height())) {
Brian Salomon96b383a2019-08-13 16:55:41 -0400357 return this->createTextureProxy(std::move(baseLevel), 1, SkBudgeted::kYes,
358 SkBackingFit::kExact);
Greg Daniela4ead652018-02-07 10:21:48 -0500359 }
360
Brian Osmand29dcd12018-09-13 15:04:29 -0400361 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info());
Greg Daniel3fc5df42019-06-14 09:42:17 -0400362
Brian Salomona90382f2019-09-17 09:01:56 -0400363 GrBackendFormat format = this->caps()->getDefaultBackendFormat(
364 SkColorTypeToGrColorType(bitmap.info().colorType()), GrRenderable::kNo);
Robert Phillips0a15cc62019-07-30 12:49:10 -0400365 if (!format.isValid()) {
Brian Osmand29dcd12018-09-13 15:04:29 -0400366 SkBitmap copy8888;
367 if (!copy8888.tryAllocPixels(bitmap.info().makeColorType(kRGBA_8888_SkColorType)) ||
368 !bitmap.readPixels(copy8888.pixmap())) {
369 return nullptr;
370 }
371 copy8888.setImmutable();
372 baseLevel = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode);
373 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Salomona90382f2019-09-17 09:01:56 -0400374 format = this->caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888, GrRenderable::kNo);
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400375 if (!format.isValid()) {
376 return nullptr;
377 }
Greg Daniel3fc5df42019-06-14 09:42:17 -0400378 }
379
Brian Osmand29dcd12018-09-13 15:04:29 -0400380 SkPixmap pixmap;
381 SkAssertResult(baseLevel->peekPixels(&pixmap));
382 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(pixmap, nullptr));
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400383 if (!mipmaps) {
384 return nullptr;
385 }
386
Greg Daniela4ead652018-02-07 10:21:48 -0500387 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomon4eb38b72019-08-05 12:58:39 -0400388 [desc, format, baseLevel, mipmaps](GrResourceProvider* resourceProvider) {
Brian Osman1b97f132018-09-13 17:33:48 +0000389 const int mipLevelCount = mipmaps->countLevels() + 1;
390 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
391
Greg Daniela4ead652018-02-07 10:21:48 -0500392 SkPixmap pixmap;
393 SkAssertResult(baseLevel->peekPixels(&pixmap));
394
Greg Daniela4ead652018-02-07 10:21:48 -0500395 texels[0].fPixels = pixmap.addr();
396 texels[0].fRowBytes = pixmap.rowBytes();
397
Brian Salomona90382f2019-09-17 09:01:56 -0400398 auto colorType = SkColorTypeToGrColorType(pixmap.colorType());
Greg Daniela4ead652018-02-07 10:21:48 -0500399 for (int i = 1; i < mipLevelCount; ++i) {
400 SkMipMap::Level generatedMipLevel;
401 mipmaps->getLevel(i - 1, &generatedMipLevel);
402 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
403 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
404 SkASSERT(texels[i].fPixels);
Brian Salomona90382f2019-09-17 09:01:56 -0400405 SkASSERT(generatedMipLevel.fPixmap.colorType() == pixmap.colorType());
Greg Daniela4ead652018-02-07 10:21:48 -0500406 }
Brian Salomonbeb7f522019-08-30 16:19:42 -0400407 return LazyCallbackResult(resourceProvider->createTexture(
Brian Salomona90382f2019-09-17 09:01:56 -0400408 desc, format, colorType, GrRenderable::kNo, 1, SkBudgeted::kYes,
409 GrProtected::kNo, texels.get(), mipLevelCount));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500410 },
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400411 format, desc, GrRenderable::kNo, 1, kTopLeft_GrSurfaceOrigin, GrMipMapped::kYes,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400412 GrMipMapsStatus::kValid, GrInternalSurfaceFlags::kNone, SkBackingFit::kExact,
413 SkBudgeted::kYes, GrProtected::kNo, UseAllocator::kYes);
Greg Daniela4ead652018-02-07 10:21:48 -0500414
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400415 if (!proxy) {
416 return nullptr;
417 }
418
Robert Phillipsa41c6852019-02-07 10:44:10 -0500419 GrContext* direct = fImageContext->priv().asDirectContext();
420 if (direct) {
421 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Daniela4ead652018-02-07 10:21:48 -0500422 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
423 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500424 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Greg Daniela4ead652018-02-07 10:21:48 -0500425 return nullptr;
426 }
427 }
428 return proxy;
429}
430
Greg Danielfa55f2e2019-06-13 15:21:38 -0400431#ifdef SK_DEBUG
432static bool validate_backend_format_and_config(const GrCaps* caps,
433 const GrBackendFormat& format,
434 GrPixelConfig config) {
435 if (kUnknown_GrPixelConfig == config) {
436 return false;
437 }
Brian Salomonbb8dde82019-06-27 10:52:13 -0400438 if (GrPixelConfigIsCompressed(config)) {
439 // We have no way to verify these at the moment.
440 return true;
441 }
Robert Phillipsc046ff02019-07-01 10:34:03 -0400442
Robert Phillips1e2cb442019-07-02 15:51:28 -0400443 GrColorType grCT = GrPixelConfigToColorType(config);
444
445 return caps->areColorTypeAndFormatCompatible(grCT, format);
Greg Danielfa55f2e2019-06-13 15:21:38 -0400446}
447#endif
448
Greg Daniel4065d452018-11-16 15:43:41 -0500449sk_sp<GrTextureProxy> GrProxyProvider::createProxy(const GrBackendFormat& format,
450 const GrSurfaceDesc& desc,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400451 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400452 int renderTargetSampleCnt,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500453 GrSurfaceOrigin origin,
Greg Danielf6f7b672018-02-15 13:06:26 -0500454 GrMipMapped mipMapped,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500455 SkBackingFit fit,
456 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -0400457 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400458 GrInternalSurfaceFlags surfaceFlags,
459 GrSurfaceProxy::UseAllocator useAllocator) {
Robert Phillips8ff8bcc2019-07-29 17:03:35 -0400460 const GrCaps* caps = this->caps();
461
462 if (caps->isFormatCompressed(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400463 // Deferred proxies for compressed textures are not supported.
464 return nullptr;
465 }
Robert Phillips0902c982019-07-16 07:47:56 -0400466
Robert Phillips0902c982019-07-16 07:47:56 -0400467 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
468
469 SkASSERT(GrCaps::AreConfigsCompatible(desc.fConfig,
470 caps->getConfigFromBackendFormat(format, colorType)));
Greg Daniel0fac8692019-08-16 13:13:17 -0400471 // TODO: This check should be removed once we get the swizzle outside of GrProxyProvider and
472 // either pass them to the proxy or store the on some view object.
473 if (!caps->areColorTypeAndFormatCompatible(colorType, format)) {
474 return nullptr;
475 }
Robert Phillips62221e72019-07-24 15:07:38 -0400476
Greg Danielf6f7b672018-02-15 13:06:26 -0500477 if (GrMipMapped::kYes == mipMapped) {
478 // SkMipMap doesn't include the base level in the level count so we have to add 1
479 int mipCount = SkMipMap::ComputeLevelCount(desc.fWidth, desc.fHeight) + 1;
480 if (1 == mipCount) {
481 mipMapped = GrMipMapped::kNo;
482 }
483 }
484
Greg Daniel6fa62e22019-08-07 15:52:37 -0400485 if (!caps->validateSurfaceParams({desc.fWidth, desc.fHeight}, format, desc.fConfig, renderable,
486 renderTargetSampleCnt, mipMapped)) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500487 return nullptr;
488 }
Brian Salomon3a2cc2c2018-02-03 00:25:12 +0000489 GrSurfaceDesc copyDesc = desc;
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600490 GrMipMapsStatus mipMapsStatus = (GrMipMapped::kYes == mipMapped)
491 ? GrMipMapsStatus::kDirty
492 : GrMipMapsStatus::kNotAllocated;
Robert Phillips0902c982019-07-16 07:47:56 -0400493 GrSwizzle texSwizzle = caps->getTextureSwizzle(format, colorType);
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400494 if (renderable == GrRenderable::kYes) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400495 renderTargetSampleCnt =
Greg Daniel6fa62e22019-08-07 15:52:37 -0400496 caps->getRenderTargetSampleCount(renderTargetSampleCnt, format);
497 SkASSERT(renderTargetSampleCnt);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500498 // We know anything we instantiate later from this deferred path will be
499 // both texturable and renderable
Robert Phillips0902c982019-07-16 07:47:56 -0400500 GrSwizzle outSwizzle = caps->getOutputSwizzle(format, colorType);
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400501 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600502 *caps, format, copyDesc, renderTargetSampleCnt, origin, mipMapped, mipMapsStatus,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400503 texSwizzle, outSwizzle, fit, budgeted, isProtected, surfaceFlags, useAllocator));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500504 }
505
Brian Salomonbeb7f522019-08-30 16:19:42 -0400506 return sk_sp<GrTextureProxy>(new GrTextureProxy(format, copyDesc, origin, mipMapped,
507 mipMapsStatus, texSwizzle, fit, budgeted,
508 isProtected, surfaceFlags, useAllocator));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500509}
510
Brian Salomonbb8dde82019-06-27 10:52:13 -0400511sk_sp<GrTextureProxy> GrProxyProvider::createCompressedTextureProxy(
512 int width, int height, SkBudgeted budgeted, SkImage::CompressionType compressionType,
513 sk_sp<SkData> data) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400514
515 GrSurfaceDesc desc;
516 desc.fConfig = GrCompressionTypePixelConfig(compressionType);
517 desc.fWidth = width;
518 desc.fHeight = height;
519
Robert Phillips8ff8bcc2019-07-29 17:03:35 -0400520 GrBackendFormat format = this->caps()->getBackendFormatFromCompressionType(compressionType);
521
Greg Daniel7bfc9132019-08-14 14:23:53 -0400522 if (!this->caps()->isFormatTexturable(format)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500523 return nullptr;
524 }
525
Jim Van Verthee06b332019-01-18 10:36:32 -0500526 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomonbeb7f522019-08-30 16:19:42 -0400527 [width, height, format, compressionType, budgeted,
528 data](GrResourceProvider* resourceProvider) {
529 return LazyCallbackResult(resourceProvider->createCompressedTexture(
Greg Daniel7bfc9132019-08-14 14:23:53 -0400530 width, height, format, compressionType, budgeted, data.get()));
Brian Salomonbb8dde82019-06-27 10:52:13 -0400531 },
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400532 format, desc, GrRenderable::kNo, 1, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400533 GrMipMapsStatus::kNotAllocated, GrInternalSurfaceFlags::kNone, SkBackingFit::kExact,
534 SkBudgeted::kYes, GrProtected::kNo, UseAllocator::kYes);
Jim Van Verthee06b332019-01-18 10:36:32 -0500535
536 if (!proxy) {
537 return nullptr;
538 }
539
Robert Phillipsa41c6852019-02-07 10:44:10 -0500540 GrContext* direct = fImageContext->priv().asDirectContext();
541 if (direct) {
542 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Jim Van Verthee06b332019-01-18 10:36:32 -0500543 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
544 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500545 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500546 return nullptr;
547 }
548 }
549 return proxy;
550}
551
Brian Salomon7578f3e2018-03-07 14:39:54 -0500552sk_sp<GrTextureProxy> GrProxyProvider::wrapBackendTexture(const GrBackendTexture& backendTex,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400553 GrColorType grColorType,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500554 GrSurfaceOrigin origin,
555 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500556 GrWrapCacheable cacheable,
Brian Salomonc67c31c2018-12-06 10:00:03 -0500557 GrIOType ioType,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500558 ReleaseProc releaseProc,
559 ReleaseContext releaseCtx) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500560 SkASSERT(ioType != kWrite_GrIOType);
Robert Phillipsf9bec202018-01-16 09:21:01 -0500561 if (this->isAbandoned()) {
562 return nullptr;
563 }
564
Brian Salomonf7778972018-03-08 10:13:17 -0500565 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500566 GrContext* direct = fImageContext->priv().asDirectContext();
567 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500568 return nullptr;
569 }
570
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400571 const GrCaps* caps = this->caps();
572
Robert Phillips62221e72019-07-24 15:07:38 -0400573 SkASSERT(caps->areColorTypeAndFormatCompatible(grColorType, backendTex.getBackendFormat()));
Greg Danielfa55f2e2019-06-13 15:21:38 -0400574
Robert Phillipsa41c6852019-02-07 10:44:10 -0500575 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
576
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500577 sk_sp<GrTexture> tex =
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400578 resourceProvider->wrapBackendTexture(backendTex, grColorType,
579 ownership, cacheable, ioType);
Brian Salomonf7778972018-03-08 10:13:17 -0500580 if (!tex) {
581 return nullptr;
582 }
Robert Phillipsadbe1322018-01-17 13:35:46 -0500583
Greg Daniel6a0176b2018-01-30 09:28:44 -0500584 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500585 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel6a0176b2018-01-30 09:28:44 -0500586 }
587
Brian Salomonf7778972018-03-08 10:13:17 -0500588 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
589 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500590 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500591
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400592 GrSwizzle texSwizzle = caps->getTextureSwizzle(tex->backendFormat(), grColorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400593
Brian Salomonbeb7f522019-08-30 16:19:42 -0400594 return sk_sp<GrTextureProxy>(
595 new GrTextureProxy(std::move(tex), origin, texSwizzle, UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500596}
597
Brian Salomon7578f3e2018-03-07 14:39:54 -0500598sk_sp<GrTextureProxy> GrProxyProvider::wrapRenderableBackendTexture(
Brian Salomon02bd2952018-03-07 15:20:21 -0500599 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400600 GrColorType colorType, GrWrapOwnership ownership, GrWrapCacheable cacheable,
601 ReleaseProc releaseProc, ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500602 if (this->isAbandoned()) {
603 return nullptr;
604 }
605
Brian Salomonf7778972018-03-08 10:13:17 -0500606 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500607 GrContext* direct = fImageContext->priv().asDirectContext();
608 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500609 return nullptr;
610 }
611
Robert Phillips0902c982019-07-16 07:47:56 -0400612 const GrCaps* caps = this->caps();
613
Robert Phillips62221e72019-07-24 15:07:38 -0400614 SkASSERT(caps->areColorTypeAndFormatCompatible(colorType, backendTex.getBackendFormat()));
Greg Danielfa55f2e2019-06-13 15:21:38 -0400615
Robert Phillipsa41c6852019-02-07 10:44:10 -0500616 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
617
Greg Daniel6fa62e22019-08-07 15:52:37 -0400618 // TODO: This should have been checked and validated before getting into GrProxyProvider.
619 if (!caps->isFormatAsColorTypeRenderable(colorType, backendTex.getBackendFormat(), sampleCnt)) {
Greg Danielf87651e2018-02-21 11:36:53 -0500620 return nullptr;
621 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500622
Greg Daniel6fa62e22019-08-07 15:52:37 -0400623 sampleCnt = caps->getRenderTargetSampleCount(sampleCnt, backendTex.getBackendFormat());
624 SkASSERT(sampleCnt);
625
Robert Phillipsa41c6852019-02-07 10:44:10 -0500626 sk_sp<GrTexture> tex = resourceProvider->wrapRenderableBackendTexture(backendTex, sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400627 colorType, ownership,
628 cacheable);
Brian Salomonf7778972018-03-08 10:13:17 -0500629 if (!tex) {
630 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500631 }
632
Greg Daniel8ce79912019-02-05 10:08:43 -0500633 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500634 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500635 }
636
Brian Salomonf7778972018-03-08 10:13:17 -0500637 SkASSERT(tex->asRenderTarget()); // A GrTextureRenderTarget
638 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500639 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Greg Daniel6abda432018-02-15 14:55:00 -0500640
Robert Phillips0902c982019-07-16 07:47:56 -0400641 GrSwizzle texSwizzle = caps->getTextureSwizzle(tex->backendFormat(), colorType);
642 GrSwizzle outSwizzle = caps->getOutputSwizzle(tex->backendFormat(), colorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400643
644 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin, texSwizzle,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400645 outSwizzle, UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500646}
647
Brian Salomon7578f3e2018-03-07 14:39:54 -0500648sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400649 const GrBackendRenderTarget& backendRT, GrColorType grColorType,
650 GrSurfaceOrigin origin, ReleaseProc releaseProc, ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500651 if (this->isAbandoned()) {
652 return nullptr;
653 }
654
Brian Salomonf7778972018-03-08 10:13:17 -0500655 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500656 GrContext* direct = fImageContext->priv().asDirectContext();
657 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500658 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500659 }
660
Robert Phillips62221e72019-07-24 15:07:38 -0400661 const GrCaps* caps = this->caps();
662
663 SkASSERT(caps->areColorTypeAndFormatCompatible(grColorType, backendRT.getBackendFormat()));
Greg Danielfa55f2e2019-06-13 15:21:38 -0400664
Robert Phillipsa41c6852019-02-07 10:44:10 -0500665 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
666
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400667 sk_sp<GrRenderTarget> rt = resourceProvider->wrapBackendRenderTarget(backendRT, grColorType);
Brian Salomonf7778972018-03-08 10:13:17 -0500668 if (!rt) {
669 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500670 }
Greg Daniel8ce79912019-02-05 10:08:43 -0500671
Greg Daniel8ce79912019-02-05 10:08:43 -0500672 if (releaseProc) {
Brian Salomon2ca31f82019-03-05 13:28:58 -0500673 rt->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500674 }
675
Brian Salomonf7778972018-03-08 10:13:17 -0500676 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
677 SkASSERT(!rt->getUniqueKey().isValid());
678 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500679 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Brian Salomonf7778972018-03-08 10:13:17 -0500680
Robert Phillips62221e72019-07-24 15:07:38 -0400681 GrSwizzle texSwizzle = caps->getTextureSwizzle(rt->backendFormat(), grColorType);
682 GrSwizzle outSwizzle = caps->getOutputSwizzle(rt->backendFormat(), grColorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400683
684 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(std::move(rt), origin, texSwizzle,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400685 outSwizzle, UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500686}
687
Brian Salomon7578f3e2018-03-07 14:39:54 -0500688sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendTextureAsRenderTarget(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400689 const GrBackendTexture& backendTex, GrColorType grColorType,
690 GrSurfaceOrigin origin, int sampleCnt) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500691 if (this->isAbandoned()) {
692 return nullptr;
693 }
694
Brian Salomonf7778972018-03-08 10:13:17 -0500695 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500696 GrContext* direct = fImageContext->priv().asDirectContext();
697 if (!direct) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500698 return nullptr;
699 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500700
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400701 const GrCaps* caps = this->caps();
702
Robert Phillips62221e72019-07-24 15:07:38 -0400703 SkASSERT(caps->areColorTypeAndFormatCompatible(grColorType, backendTex.getBackendFormat()));
Greg Danielfa55f2e2019-06-13 15:21:38 -0400704
Robert Phillipsa41c6852019-02-07 10:44:10 -0500705 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
706
Brian Salomonf7778972018-03-08 10:13:17 -0500707 sk_sp<GrRenderTarget> rt =
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400708 resourceProvider->wrapBackendTextureAsRenderTarget(backendTex, sampleCnt, grColorType);
Brian Salomonf7778972018-03-08 10:13:17 -0500709 if (!rt) {
710 return nullptr;
Greg Danielf87651e2018-02-21 11:36:53 -0500711 }
Brian Salomonf7778972018-03-08 10:13:17 -0500712 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
713 SkASSERT(!rt->getUniqueKey().isValid());
Greg Danielb46add82019-01-02 14:51:29 -0500714 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500715 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielf87651e2018-02-21 11:36:53 -0500716
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400717 GrSwizzle texSwizzle = caps->getTextureSwizzle(rt->backendFormat(), grColorType);
718 GrSwizzle outSwizzle = caps->getOutputSwizzle(rt->backendFormat(), grColorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400719
720 return sk_sp<GrSurfaceProxy>(new GrRenderTargetProxy(std::move(rt), origin, texSwizzle,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400721 outSwizzle, UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500722}
723
Greg Danielb46add82019-01-02 14:51:29 -0500724sk_sp<GrRenderTargetProxy> GrProxyProvider::wrapVulkanSecondaryCBAsRenderTarget(
725 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
726 if (this->isAbandoned()) {
727 return nullptr;
728 }
729
730 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500731 GrContext* direct = fImageContext->priv().asDirectContext();
732 if (!direct) {
Greg Danielb46add82019-01-02 14:51:29 -0500733 return nullptr;
734 }
735
Robert Phillipsa41c6852019-02-07 10:44:10 -0500736 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Danielb46add82019-01-02 14:51:29 -0500737
Robert Phillipsa41c6852019-02-07 10:44:10 -0500738 sk_sp<GrRenderTarget> rt = resourceProvider->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
739 vkInfo);
Greg Danielb46add82019-01-02 14:51:29 -0500740 if (!rt) {
741 return nullptr;
742 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500743
Greg Danielb46add82019-01-02 14:51:29 -0500744 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
745 SkASSERT(!rt->getUniqueKey().isValid());
746 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500747 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielb46add82019-01-02 14:51:29 -0500748
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400749 GrColorType colorType = SkColorTypeToGrColorType(imageInfo.colorType());
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400750 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(rt->backendFormat(), colorType);
751 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(rt->backendFormat(), colorType);
752
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400753 if (!this->caps()->isFormatAsColorTypeRenderable(colorType, rt->backendFormat(),
754 rt->numSamples())) {
755 return nullptr;
756 }
757
Greg Danielb46add82019-01-02 14:51:29 -0500758 // All Vulkan surfaces uses top left origins.
Brian Salomonbeb7f522019-08-30 16:19:42 -0400759 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
760 std::move(rt), kTopLeft_GrSurfaceOrigin, texSwizzle, outSwizzle, UseAllocator::kNo,
761 GrRenderTargetProxy::WrapsVkSecondaryCB::kYes));
Brian Salomone8a766b2019-07-19 14:24:36 -0400762}
763
764sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
765 const GrBackendFormat& format,
766 const GrSurfaceDesc& desc,
767 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400768 int renderTargetSampleCnt,
Brian Salomone8a766b2019-07-19 14:24:36 -0400769 GrSurfaceOrigin origin,
770 GrMipMapped mipMapped,
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600771 GrMipMapsStatus mipMapsStatus,
Brian Salomone8a766b2019-07-19 14:24:36 -0400772 GrInternalSurfaceFlags surfaceFlags,
773 SkBackingFit fit,
774 SkBudgeted budgeted,
775 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400776 GrSurfaceProxy::UseAllocator useAllocator) {
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 Phillips0a15cc62019-07-30 12:49:10 -0400780 if (!format.isValid()) {
781 return nullptr;
782 }
783
Robert Phillipsa41c6852019-02-07 10:44:10 -0500784 if (desc.fWidth > this->caps()->maxTextureSize() ||
785 desc.fHeight > this->caps()->maxTextureSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400786 return nullptr;
787 }
788
Greg Danielfa55f2e2019-06-13 15:21:38 -0400789 SkASSERT(validate_backend_format_and_config(this->caps(), format, desc.fConfig));
Greg Daniel457469c2018-02-08 15:05:44 -0500790
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400791 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
792 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(format, colorType);
793 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(format, colorType);
794
Brian Salomonbeb7f522019-08-30 16:19:42 -0400795 if (renderable == GrRenderable::kYes) {
796 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(*this->caps(),
797 std::move(callback),
798 format,
799 desc,
800 renderTargetSampleCnt,
801 origin,
802 mipMapped,
803 mipMapsStatus,
804 texSwizzle,
805 outSwizzle,
806 fit,
807 budgeted,
808 isProtected,
809 surfaceFlags,
810 useAllocator));
811 } else {
812 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(callback),
813 format,
814 desc,
815 origin,
816 mipMapped,
817 mipMapsStatus,
818 texSwizzle,
819 fit,
820 budgeted,
821 isProtected,
822 surfaceFlags,
823 useAllocator));
824 }
Robert Phillips777707b2018-01-17 11:40:14 -0500825}
826
Robert Phillipse8fabb22018-02-04 14:33:21 -0500827sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy(
Brian Salomonbeb7f522019-08-30 16:19:42 -0400828 LazyInstantiateCallback&& callback,
829 const GrBackendFormat& format,
830 const GrSurfaceDesc& desc,
831 int sampleCnt,
832 GrSurfaceOrigin origin,
833 GrInternalSurfaceFlags surfaceFlags,
834 const TextureInfo* textureInfo,
835 GrMipMapsStatus mipMapsStatus,
836 SkBackingFit fit,
837 SkBudgeted budgeted,
838 GrProtected isProtected,
839 bool wrapsVkSecondaryCB,
840 UseAllocator useAllocator) {
Robert Phillipse8fabb22018-02-04 14:33:21 -0500841 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
842 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400843
Robert Phillipsa41c6852019-02-07 10:44:10 -0500844 if (desc.fWidth > this->caps()->maxRenderTargetSize() ||
845 desc.fHeight > this->caps()->maxRenderTargetSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400846 return nullptr;
847 }
848
Greg Danielfa55f2e2019-06-13 15:21:38 -0400849 SkASSERT(validate_backend_format_and_config(this->caps(), format, desc.fConfig));
Greg Daniel457469c2018-02-08 15:05:44 -0500850
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400851 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
852 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(format, colorType);
853 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(format, colorType);
854
Brian Salomon7226c232018-07-30 13:13:17 -0400855 if (textureInfo) {
Greg Danielb085fa92019-03-05 16:55:12 -0500856 // Wrapped vulkan secondary command buffers don't support texturing since we won't have an
857 // actual VkImage to texture from.
858 SkASSERT(!wrapsVkSecondaryCB);
Brian Salomon7226c232018-07-30 13:13:17 -0400859 return sk_sp<GrRenderTargetProxy>(new GrTextureRenderTargetProxy(
Brian Salomonbeb7f522019-08-30 16:19:42 -0400860 *this->caps(), std::move(callback), format, desc, sampleCnt, origin,
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600861 textureInfo->fMipMapped, mipMapsStatus, texSwizzle, outSwizzle, fit, budgeted,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400862 isProtected, surfaceFlags, useAllocator));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500863 }
864
Greg Danielb085fa92019-03-05 16:55:12 -0500865 GrRenderTargetProxy::WrapsVkSecondaryCB vkSCB =
866 wrapsVkSecondaryCB ? GrRenderTargetProxy::WrapsVkSecondaryCB::kYes
867 : GrRenderTargetProxy::WrapsVkSecondaryCB::kNo;
868
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400869 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
Brian Salomonbeb7f522019-08-30 16:19:42 -0400870 std::move(callback), format, desc, sampleCnt, origin, texSwizzle, outSwizzle, fit,
871 budgeted, isProtected, surfaceFlags, useAllocator, vkSCB));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500872}
873
Brian Salomonbeb7f522019-08-30 16:19:42 -0400874sk_sp<GrTextureProxy> GrProxyProvider::MakeFullyLazyProxy(LazyInstantiateCallback&& callback,
875 const GrBackendFormat& format,
876 GrRenderable renderable,
877 int renderTargetSampleCnt,
878 GrProtected isProtected,
879 GrSurfaceOrigin origin,
880 GrPixelConfig config,
881 const GrCaps& caps,
882 UseAllocator useAllocator) {
Robert Phillips0a15cc62019-07-30 12:49:10 -0400883 if (!format.isValid()) {
884 return nullptr;
885 }
886
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400887 SkASSERT(renderTargetSampleCnt == 1 || renderable == GrRenderable::kYes);
Greg Danielfa55f2e2019-06-13 15:21:38 -0400888 SkASSERT(validate_backend_format_and_config(&caps, format, config));
Robert Phillips777707b2018-01-17 11:40:14 -0500889 GrSurfaceDesc desc;
Robert Phillips10d17212019-04-24 14:09:10 -0400890 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNone;
Robert Phillips777707b2018-01-17 11:40:14 -0500891 desc.fWidth = -1;
892 desc.fHeight = -1;
893 desc.fConfig = config;
Robert Phillips777707b2018-01-17 11:40:14 -0500894
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400895 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
896 GrSwizzle texSwizzle = caps.getTextureSwizzle(format, colorType);
897 GrSwizzle outSwizzle = caps.getOutputSwizzle(format, colorType);
898
Brian Salomonbeb7f522019-08-30 16:19:42 -0400899 if (GrRenderable::kYes == renderable) {
900 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
901 caps, std::move(callback), format, desc, renderTargetSampleCnt, origin,
902 GrMipMapped::kNo, GrMipMapsStatus::kNotAllocated, texSwizzle, outSwizzle,
903 SkBackingFit::kApprox, SkBudgeted::kYes, isProtected, surfaceFlags, useAllocator));
904 } else {
905 return sk_sp<GrTextureProxy>(new GrTextureProxy(
906 std::move(callback), format, desc, origin, GrMipMapped::kNo,
907 GrMipMapsStatus::kNotAllocated, texSwizzle, SkBackingFit::kApprox, SkBudgeted::kYes,
908 isProtected, surfaceFlags, useAllocator));
909 }
Robert Phillips777707b2018-01-17 11:40:14 -0500910}
911
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500912bool GrProxyProvider::IsFunctionallyExact(GrSurfaceProxy* proxy) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400913 const bool isInstantiated = proxy->isInstantiated();
Robert Phillipsdb3b9792018-06-12 15:18:00 -0400914 // A proxy is functionally exact if:
915 // it is exact (obvs)
916 // when it is instantiated it will be exact (i.e., power of two dimensions)
917 // it is already instantiated and the proxy covers the entire backing surface
918 return proxy->priv().isExact() ||
919 (!isInstantiated && SkIsPow2(proxy->width()) && SkIsPow2(proxy->height())) ||
920 (isInstantiated && proxy->worstCaseWidth() == proxy->width() &&
921 proxy->worstCaseHeight() == proxy->height());
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500922}
923
Robert Phillips427966a2018-12-20 17:20:43 -0500924void GrProxyProvider::processInvalidUniqueKey(const GrUniqueKey& key, GrTextureProxy* proxy,
925 InvalidateGPUResource invalidateGPUResource) {
Chris Dalton2de13dd2019-01-03 15:11:59 -0700926 SkASSERT(key.isValid());
927
Robert Phillips427966a2018-12-20 17:20:43 -0500928 if (!proxy) {
929 proxy = fUniquelyKeyedProxies.find(key);
930 }
Chris Dalton2de13dd2019-01-03 15:11:59 -0700931 SkASSERT(!proxy || proxy->getUniqueKey() == key);
932
933 // Locate the corresponding GrGpuResource (if it needs to be invalidated) before clearing the
934 // proxy's unique key. We must do it in this order because 'key' may alias the proxy's key.
935 sk_sp<GrGpuResource> invalidGpuResource;
936 if (InvalidateGPUResource::kYes == invalidateGPUResource) {
Brian Salomon01ceae92019-04-02 11:49:54 -0400937 GrContext* direct = fImageContext->priv().asDirectContext();
938 if (direct) {
939 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
940 invalidGpuResource = resourceProvider->findByUniqueKey<GrGpuResource>(key);
Chris Dalton2de13dd2019-01-03 15:11:59 -0700941 }
942 SkASSERT(!invalidGpuResource || invalidGpuResource->getUniqueKey() == key);
943 }
Robert Phillips427966a2018-12-20 17:20:43 -0500944
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500945 // Note: this method is called for the whole variety of GrGpuResources so often 'key'
946 // will not be in 'fUniquelyKeyedProxies'.
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500947 if (proxy) {
Robert Phillips427966a2018-12-20 17:20:43 -0500948 fUniquelyKeyedProxies.remove(key);
949 proxy->cacheAccess().clearUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500950 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500951
Chris Dalton2de13dd2019-01-03 15:11:59 -0700952 if (invalidGpuResource) {
953 invalidGpuResource->resourcePriv().removeUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500954 }
955}
956
Robert Phillipsa41c6852019-02-07 10:44:10 -0500957uint32_t GrProxyProvider::contextID() const {
958 return fImageContext->priv().contextID();
959}
960
961const GrCaps* GrProxyProvider::caps() const {
962 return fImageContext->priv().caps();
963}
964
965sk_sp<const GrCaps> GrProxyProvider::refCaps() const {
966 return fImageContext->priv().refCaps();
967}
968
Robert Phillipsa9162df2019-02-11 14:12:03 -0500969bool GrProxyProvider::isAbandoned() const {
970 return fImageContext->priv().abandoned();
971}
972
Robert Phillips0790f8a2018-09-18 13:11:03 -0400973void GrProxyProvider::orphanAllUniqueKeys() {
974 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
975 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
976 GrTextureProxy& tmp = *iter;
977
978 tmp.fProxyProvider = nullptr;
979 }
980}
981
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500982void GrProxyProvider::removeAllUniqueKeys() {
983 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
984 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
985 GrTextureProxy& tmp = *iter;
986
Chris Dalton2de13dd2019-01-03 15:11:59 -0700987 this->processInvalidUniqueKey(tmp.getUniqueKey(), &tmp, InvalidateGPUResource::kNo);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500988 }
989 SkASSERT(!fUniquelyKeyedProxies.count());
990}
Robert Phillipsa41c6852019-02-07 10:44:10 -0500991
992bool GrProxyProvider::renderingDirectly() const {
993 return fImageContext->priv().asDirectContext();
994}