blob: f65ad6f454e0b46d6920c7fc6724993e6f4bb0d9 [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,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400150 isProtected,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400151 GrResourceProvider::Flags::kNoPendingIO);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500152 } else {
Brian Salomon4eb38b72019-08-05 12:58:39 -0400153 tex = resourceProvider->createTexture(desc, format, renderable, renderTargetSampleCnt,
154 budgeted, isProtected,
155 GrResourceProvider::Flags::kNoPendingIO);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500156 }
157 if (!tex) {
158 return nullptr;
159 }
160
Brian Salomonbeb7f522019-08-30 16:19:42 -0400161 return this->createWrapped(std::move(tex), colorType, origin, UseAllocator::kYes);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500162}
163
Brian Salomon4eb38b72019-08-05 12:58:39 -0400164sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createInstantiatedProxy(
165 const SkISize& size,
166 GrColorType colorType,
167 GrRenderable renderable,
168 int renderTargetSampleCnt,
169 GrSurfaceOrigin origin,
170 SkBackingFit fit,
171 SkBudgeted budgeted,
172 GrProtected isProtected) {
173 auto format = this->caps()->getDefaultBackendFormat(colorType, renderable);
174 return this->testingOnly_createInstantiatedProxy(size,
175 colorType,
176 format,
177 renderable,
178 renderTargetSampleCnt,
179 origin,
180 fit,
181 budgeted,
182 isProtected);
183}
184
Robert Phillipsa41c6852019-02-07 10:44:10 -0500185sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createWrapped(sk_sp<GrTexture> tex,
Brian Salomon2af3e702019-08-11 19:10:31 -0400186 GrColorType colorType,
Robert Phillipsa41c6852019-02-07 10:44:10 -0500187 GrSurfaceOrigin origin) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400188 return this->createWrapped(std::move(tex), colorType, origin, UseAllocator::kYes);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500189}
190#endif
191
Brian Salomonbeb7f522019-08-30 16:19:42 -0400192sk_sp<GrTextureProxy> GrProxyProvider::createWrapped(sk_sp<GrTexture> tex,
193 GrColorType colorType,
194 GrSurfaceOrigin origin,
195 UseAllocator useAllocator) {
Robert Phillipsadbe1322018-01-17 13:35:46 -0500196#ifdef SK_DEBUG
197 if (tex->getUniqueKey().isValid()) {
198 SkASSERT(!this->findProxyByUniqueKey(tex->getUniqueKey(), origin));
199 }
200#endif
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400201 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(tex->backendFormat(), colorType);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500202
203 if (tex->asRenderTarget()) {
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400204 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(tex->backendFormat(), colorType);
Brian Salomonbeb7f522019-08-30 16:19:42 -0400205 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
206 std::move(tex), origin, texSwizzle, outSwizzle, useAllocator));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500207 } else {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400208 return sk_sp<GrTextureProxy>(
209 new GrTextureProxy(std::move(tex), origin, texSwizzle, useAllocator));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500210 }
211}
212
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500213sk_sp<GrTextureProxy> GrProxyProvider::findOrCreateProxyByUniqueKey(const GrUniqueKey& key,
Brian Salomon2af3e702019-08-11 19:10:31 -0400214 GrColorType colorType,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400215 GrSurfaceOrigin origin,
216 UseAllocator useAllocator) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500217 ASSERT_SINGLE_OWNER
218
219 if (this->isAbandoned()) {
220 return nullptr;
221 }
222
223 sk_sp<GrTextureProxy> result = this->findProxyByUniqueKey(key, origin);
224 if (result) {
225 return result;
226 }
227
Robert Phillipsa41c6852019-02-07 10:44:10 -0500228 GrContext* direct = fImageContext->priv().asDirectContext();
229 if (!direct) {
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500230 return nullptr;
231 }
232
Robert Phillipsa41c6852019-02-07 10:44:10 -0500233 GrResourceCache* resourceCache = direct->priv().getResourceCache();
234
235 GrGpuResource* resource = resourceCache->findAndRefUniqueResource(key);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500236 if (!resource) {
237 return nullptr;
238 }
239
240 sk_sp<GrTexture> texture(static_cast<GrSurface*>(resource)->asTexture());
241 SkASSERT(texture);
242
Brian Salomonbeb7f522019-08-30 16:19:42 -0400243 result = this->createWrapped(std::move(texture), colorType, origin, useAllocator);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500244 SkASSERT(result->getUniqueKey() == key);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500245 // createWrapped should've added this for us
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500246 SkASSERT(fUniquelyKeyedProxies.find(key));
Brian Salomon2af3e702019-08-11 19:10:31 -0400247 SkASSERT(result->textureSwizzle() ==
248 this->caps()->getTextureSwizzle(result->backendFormat(), colorType));
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500249 return result;
250}
251
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500252sk_sp<GrTextureProxy> GrProxyProvider::createTextureProxy(sk_sp<SkImage> srcImage,
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500253 int sampleCnt,
Greg Danielfb3abcd2018-02-02 15:48:33 -0500254 SkBudgeted budgeted,
Chris Daltond004e0b2018-09-27 09:28:03 -0600255 SkBackingFit fit,
256 GrInternalSurfaceFlags surfaceFlags) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500257 ASSERT_SINGLE_OWNER
258 SkASSERT(srcImage);
259
260 if (this->isAbandoned()) {
261 return nullptr;
262 }
263
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400264 const SkImageInfo& info = srcImage->imageInfo();
Brian Salomon2af3e702019-08-11 19:10:31 -0400265 GrColorType ct = SkColorTypeToGrColorType(info.colorType());
Greg Danielf87651e2018-02-21 11:36:53 -0500266
Brian Salomon96b383a2019-08-13 16:55:41 -0400267 GrBackendFormat format = this->caps()->getDefaultBackendFormat(ct, GrRenderable::kNo);
Greg Daniel0a7aa142018-02-21 13:02:32 -0500268
Robert Phillips0a15cc62019-07-30 12:49:10 -0400269 if (!format.isValid()) {
Brian Osmand29dcd12018-09-13 15:04:29 -0400270 SkBitmap copy8888;
271 if (!copy8888.tryAllocPixels(info.makeColorType(kRGBA_8888_SkColorType)) ||
272 !srcImage->readPixels(copy8888.pixmap(), 0, 0)) {
273 return nullptr;
274 }
275 copy8888.setImmutable();
276 srcImage = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode);
Brian Salomon2af3e702019-08-11 19:10:31 -0400277 ct = GrColorType::kRGBA_8888;
Brian Salomon96b383a2019-08-13 16:55:41 -0400278 format = this->caps()->getDefaultBackendFormat(ct, GrRenderable::kNo);
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400279 if (!format.isValid()) {
280 return nullptr;
281 }
Brian Osmand29dcd12018-09-13 15:04:29 -0400282 }
283
Brian Salomon2af3e702019-08-11 19:10:31 -0400284 GrPixelConfig config = GrColorTypeToPixelConfig(ct);
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400285 if (kUnknown_GrPixelConfig == config) {
286 return nullptr;
287 }
288
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500289 GrSurfaceDesc desc;
290 desc.fWidth = srcImage->width();
291 desc.fHeight = srcImage->height();
Greg Danielf87651e2018-02-21 11:36:53 -0500292 desc.fConfig = config;
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500293
294 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomon96b383a2019-08-13 16:55:41 -0400295 [desc, format, sampleCnt, budgeted, srcImage, fit,
Brian Salomon2af3e702019-08-11 19:10:31 -0400296 ct](GrResourceProvider* resourceProvider) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500297 SkPixmap pixMap;
298 SkAssertResult(srcImage->peekPixels(&pixMap));
299 GrMipLevel mipLevel = { pixMap.addr(), pixMap.rowBytes() };
300
Brian Salomonbeb7f522019-08-30 16:19:42 -0400301 return LazyCallbackResult(resourceProvider->createTexture(
Brian Salomon96b383a2019-08-13 16:55:41 -0400302 desc, format, GrRenderable::kNo, sampleCnt, budgeted, fit, GrProtected::kNo,
303 ct, mipLevel, GrResourceProvider::Flags::kNoPendingIO));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500304 },
Brian Salomon96b383a2019-08-13 16:55:41 -0400305 format, desc, GrRenderable::kNo, sampleCnt, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400306 GrMipMapsStatus::kNotAllocated, surfaceFlags, fit, budgeted, GrProtected::kNo,
307 UseAllocator::kYes);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500308
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400309 if (!proxy) {
310 return nullptr;
311 }
312
Robert Phillipsa41c6852019-02-07 10:44:10 -0500313 GrContext* direct = fImageContext->priv().asDirectContext();
314 if (direct) {
315 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
316
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500317 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
318 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500319 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500320 return nullptr;
321 }
322 }
Robert Phillipsc1b60662018-06-26 10:20:08 -0400323
324 SkASSERT(proxy->width() == desc.fWidth);
325 SkASSERT(proxy->height() == desc.fHeight);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500326 return proxy;
327}
328
Brian Osmande496652019-03-22 13:42:33 -0400329sk_sp<GrTextureProxy> GrProxyProvider::createProxyFromBitmap(const SkBitmap& bitmap,
330 GrMipMapped mipMapped) {
Brian Osman412674f2019-02-07 15:34:58 -0500331 ASSERT_SINGLE_OWNER
Brian Osman7dcc6162019-03-25 10:12:57 -0400332 SkASSERT(GrMipMapped::kNo == mipMapped || this->caps()->mipMapSupport());
Brian Osman412674f2019-02-07 15:34:58 -0500333
334 if (this->isAbandoned()) {
335 return nullptr;
336 }
337
Brian Osman2b23c4b2018-06-01 12:25:08 -0400338 if (!SkImageInfoIsValid(bitmap.info())) {
Greg Daniela4ead652018-02-07 10:21:48 -0500339 return nullptr;
340 }
341
Brian Osmande496652019-03-22 13:42:33 -0400342 ATRACE_ANDROID_FRAMEWORK("Upload %sTexture [%ux%u]",
343 GrMipMapped::kYes == mipMapped ? "MipMap " : "",
344 bitmap.width(), bitmap.height());
Greg Daniela4ead652018-02-07 10:21:48 -0500345
346 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
347 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
348 // 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 -0500349 SkCopyPixelsMode copyMode = this->renderingDirectly() ? kNever_SkCopyPixelsMode
350 : kIfMutable_SkCopyPixelsMode;
Greg Daniela4ead652018-02-07 10:21:48 -0500351 sk_sp<SkImage> baseLevel = SkMakeImageFromRasterBitmap(bitmap, copyMode);
Greg Daniela4ead652018-02-07 10:21:48 -0500352 if (!baseLevel) {
353 return nullptr;
354 }
355
Brian Osmande496652019-03-22 13:42:33 -0400356 // If mips weren't requested (or this was too small to have any), then take the fast path
357 if (GrMipMapped::kNo == mipMapped ||
358 0 == SkMipMap::ComputeLevelCount(baseLevel->width(), baseLevel->height())) {
Brian Salomon96b383a2019-08-13 16:55:41 -0400359 return this->createTextureProxy(std::move(baseLevel), 1, SkBudgeted::kYes,
360 SkBackingFit::kExact);
Greg Daniela4ead652018-02-07 10:21:48 -0500361 }
362
Brian Osmand29dcd12018-09-13 15:04:29 -0400363 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info());
Greg Daniel3fc5df42019-06-14 09:42:17 -0400364
Robert Phillips0a15cc62019-07-30 12:49:10 -0400365 GrColorType grColorType = SkColorTypeToGrColorType(bitmap.info().colorType());
366 GrBackendFormat format = this->caps()->getDefaultBackendFormat(grColorType, GrRenderable::kNo);
367 if (!format.isValid()) {
Brian Osmand29dcd12018-09-13 15:04:29 -0400368 SkBitmap copy8888;
369 if (!copy8888.tryAllocPixels(bitmap.info().makeColorType(kRGBA_8888_SkColorType)) ||
370 !bitmap.readPixels(copy8888.pixmap())) {
371 return nullptr;
372 }
373 copy8888.setImmutable();
374 baseLevel = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode);
375 desc.fConfig = kRGBA_8888_GrPixelConfig;
Greg Daniel627d0532019-07-08 16:48:14 -0400376 grColorType = GrColorType::kRGBA_8888;
Robert Phillips0a15cc62019-07-30 12:49:10 -0400377 format = this->caps()->getDefaultBackendFormat(grColorType, GrRenderable::kNo);
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400378 if (!format.isValid()) {
379 return nullptr;
380 }
Greg Daniel3fc5df42019-06-14 09:42:17 -0400381 }
382
Brian Osmand29dcd12018-09-13 15:04:29 -0400383 SkPixmap pixmap;
384 SkAssertResult(baseLevel->peekPixels(&pixmap));
385 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(pixmap, nullptr));
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400386 if (!mipmaps) {
387 return nullptr;
388 }
389
Greg Daniela4ead652018-02-07 10:21:48 -0500390 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomon4eb38b72019-08-05 12:58:39 -0400391 [desc, format, baseLevel, mipmaps](GrResourceProvider* resourceProvider) {
Brian Osman1b97f132018-09-13 17:33:48 +0000392 const int mipLevelCount = mipmaps->countLevels() + 1;
393 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
394
Greg Daniela4ead652018-02-07 10:21:48 -0500395 SkPixmap pixmap;
396 SkAssertResult(baseLevel->peekPixels(&pixmap));
397
398 // DDL TODO: Instead of copying all this info into GrMipLevels we should just plumb
399 // the use of SkMipMap down through Ganesh.
400 texels[0].fPixels = pixmap.addr();
401 texels[0].fRowBytes = pixmap.rowBytes();
402
403 for (int i = 1; i < mipLevelCount; ++i) {
404 SkMipMap::Level generatedMipLevel;
405 mipmaps->getLevel(i - 1, &generatedMipLevel);
406 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
407 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
408 SkASSERT(texels[i].fPixels);
409 }
410
Brian Salomonbeb7f522019-08-30 16:19:42 -0400411 return LazyCallbackResult(resourceProvider->createTexture(
Brian Salomon4eb38b72019-08-05 12:58:39 -0400412 desc, format, GrRenderable::kNo, 1, SkBudgeted::kYes, GrProtected::kNo,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400413 texels.get(), mipLevelCount));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500414 },
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400415 format, desc, GrRenderable::kNo, 1, kTopLeft_GrSurfaceOrigin, GrMipMapped::kYes,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400416 GrMipMapsStatus::kValid, GrInternalSurfaceFlags::kNone, SkBackingFit::kExact,
417 SkBudgeted::kYes, GrProtected::kNo, UseAllocator::kYes);
Greg Daniela4ead652018-02-07 10:21:48 -0500418
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400419 if (!proxy) {
420 return nullptr;
421 }
422
Robert Phillipsa41c6852019-02-07 10:44:10 -0500423 GrContext* direct = fImageContext->priv().asDirectContext();
424 if (direct) {
425 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Daniela4ead652018-02-07 10:21:48 -0500426 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
427 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500428 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Greg Daniela4ead652018-02-07 10:21:48 -0500429 return nullptr;
430 }
431 }
432 return proxy;
433}
434
Greg Danielfa55f2e2019-06-13 15:21:38 -0400435#ifdef SK_DEBUG
436static bool validate_backend_format_and_config(const GrCaps* caps,
437 const GrBackendFormat& format,
438 GrPixelConfig config) {
439 if (kUnknown_GrPixelConfig == config) {
440 return false;
441 }
Brian Salomonbb8dde82019-06-27 10:52:13 -0400442 if (GrPixelConfigIsCompressed(config)) {
443 // We have no way to verify these at the moment.
444 return true;
445 }
Robert Phillipsc046ff02019-07-01 10:34:03 -0400446
Robert Phillips1e2cb442019-07-02 15:51:28 -0400447 GrColorType grCT = GrPixelConfigToColorType(config);
448
449 return caps->areColorTypeAndFormatCompatible(grCT, format);
Greg Danielfa55f2e2019-06-13 15:21:38 -0400450}
451#endif
452
Greg Daniel4065d452018-11-16 15:43:41 -0500453sk_sp<GrTextureProxy> GrProxyProvider::createProxy(const GrBackendFormat& format,
454 const GrSurfaceDesc& desc,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400455 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400456 int renderTargetSampleCnt,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500457 GrSurfaceOrigin origin,
Greg Danielf6f7b672018-02-15 13:06:26 -0500458 GrMipMapped mipMapped,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500459 SkBackingFit fit,
460 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -0400461 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400462 GrInternalSurfaceFlags surfaceFlags,
463 GrSurfaceProxy::UseAllocator useAllocator) {
Robert Phillips8ff8bcc2019-07-29 17:03:35 -0400464 const GrCaps* caps = this->caps();
465
466 if (caps->isFormatCompressed(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400467 // Deferred proxies for compressed textures are not supported.
468 return nullptr;
469 }
Robert Phillips0902c982019-07-16 07:47:56 -0400470
Robert Phillips0902c982019-07-16 07:47:56 -0400471 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
472
473 SkASSERT(GrCaps::AreConfigsCompatible(desc.fConfig,
474 caps->getConfigFromBackendFormat(format, colorType)));
Greg Daniel0fac8692019-08-16 13:13:17 -0400475 // TODO: This check should be removed once we get the swizzle outside of GrProxyProvider and
476 // either pass them to the proxy or store the on some view object.
477 if (!caps->areColorTypeAndFormatCompatible(colorType, format)) {
478 return nullptr;
479 }
Robert Phillips62221e72019-07-24 15:07:38 -0400480
Greg Danielf6f7b672018-02-15 13:06:26 -0500481 if (GrMipMapped::kYes == mipMapped) {
482 // SkMipMap doesn't include the base level in the level count so we have to add 1
483 int mipCount = SkMipMap::ComputeLevelCount(desc.fWidth, desc.fHeight) + 1;
484 if (1 == mipCount) {
485 mipMapped = GrMipMapped::kNo;
486 }
487 }
488
Greg Daniel6fa62e22019-08-07 15:52:37 -0400489 if (!caps->validateSurfaceParams({desc.fWidth, desc.fHeight}, format, desc.fConfig, renderable,
490 renderTargetSampleCnt, mipMapped)) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500491 return nullptr;
492 }
Brian Salomon3a2cc2c2018-02-03 00:25:12 +0000493 GrSurfaceDesc copyDesc = desc;
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600494 GrMipMapsStatus mipMapsStatus = (GrMipMapped::kYes == mipMapped)
495 ? GrMipMapsStatus::kDirty
496 : GrMipMapsStatus::kNotAllocated;
Robert Phillips0902c982019-07-16 07:47:56 -0400497 GrSwizzle texSwizzle = caps->getTextureSwizzle(format, colorType);
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400498 if (renderable == GrRenderable::kYes) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400499 renderTargetSampleCnt =
Greg Daniel6fa62e22019-08-07 15:52:37 -0400500 caps->getRenderTargetSampleCount(renderTargetSampleCnt, format);
501 SkASSERT(renderTargetSampleCnt);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500502 // We know anything we instantiate later from this deferred path will be
503 // both texturable and renderable
Robert Phillips0902c982019-07-16 07:47:56 -0400504 GrSwizzle outSwizzle = caps->getOutputSwizzle(format, colorType);
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400505 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600506 *caps, format, copyDesc, renderTargetSampleCnt, origin, mipMapped, mipMapsStatus,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400507 texSwizzle, outSwizzle, fit, budgeted, isProtected, surfaceFlags, useAllocator));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500508 }
509
Brian Salomonbeb7f522019-08-30 16:19:42 -0400510 return sk_sp<GrTextureProxy>(new GrTextureProxy(format, copyDesc, origin, mipMapped,
511 mipMapsStatus, texSwizzle, fit, budgeted,
512 isProtected, surfaceFlags, useAllocator));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500513}
514
Brian Salomonbb8dde82019-06-27 10:52:13 -0400515sk_sp<GrTextureProxy> GrProxyProvider::createCompressedTextureProxy(
516 int width, int height, SkBudgeted budgeted, SkImage::CompressionType compressionType,
517 sk_sp<SkData> data) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400518
519 GrSurfaceDesc desc;
520 desc.fConfig = GrCompressionTypePixelConfig(compressionType);
521 desc.fWidth = width;
522 desc.fHeight = height;
523
Robert Phillips8ff8bcc2019-07-29 17:03:35 -0400524 GrBackendFormat format = this->caps()->getBackendFormatFromCompressionType(compressionType);
525
Greg Daniel7bfc9132019-08-14 14:23:53 -0400526 if (!this->caps()->isFormatTexturable(format)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500527 return nullptr;
528 }
529
Jim Van Verthee06b332019-01-18 10:36:32 -0500530 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomonbeb7f522019-08-30 16:19:42 -0400531 [width, height, format, compressionType, budgeted,
532 data](GrResourceProvider* resourceProvider) {
533 return LazyCallbackResult(resourceProvider->createCompressedTexture(
Greg Daniel7bfc9132019-08-14 14:23:53 -0400534 width, height, format, compressionType, budgeted, data.get()));
Brian Salomonbb8dde82019-06-27 10:52:13 -0400535 },
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400536 format, desc, GrRenderable::kNo, 1, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400537 GrMipMapsStatus::kNotAllocated, GrInternalSurfaceFlags::kNone, SkBackingFit::kExact,
538 SkBudgeted::kYes, GrProtected::kNo, UseAllocator::kYes);
Jim Van Verthee06b332019-01-18 10:36:32 -0500539
540 if (!proxy) {
541 return nullptr;
542 }
543
Robert Phillipsa41c6852019-02-07 10:44:10 -0500544 GrContext* direct = fImageContext->priv().asDirectContext();
545 if (direct) {
546 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Jim Van Verthee06b332019-01-18 10:36:32 -0500547 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
548 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500549 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500550 return nullptr;
551 }
552 }
553 return proxy;
554}
555
Brian Salomon7578f3e2018-03-07 14:39:54 -0500556sk_sp<GrTextureProxy> GrProxyProvider::wrapBackendTexture(const GrBackendTexture& backendTex,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400557 GrColorType grColorType,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500558 GrSurfaceOrigin origin,
559 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500560 GrWrapCacheable cacheable,
Brian Salomonc67c31c2018-12-06 10:00:03 -0500561 GrIOType ioType,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500562 ReleaseProc releaseProc,
563 ReleaseContext releaseCtx) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500564 SkASSERT(ioType != kWrite_GrIOType);
Robert Phillipsf9bec202018-01-16 09:21:01 -0500565 if (this->isAbandoned()) {
566 return nullptr;
567 }
568
Brian Salomonf7778972018-03-08 10:13:17 -0500569 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500570 GrContext* direct = fImageContext->priv().asDirectContext();
571 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500572 return nullptr;
573 }
574
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400575 const GrCaps* caps = this->caps();
576
Robert Phillips62221e72019-07-24 15:07:38 -0400577 SkASSERT(caps->areColorTypeAndFormatCompatible(grColorType, backendTex.getBackendFormat()));
Greg Danielfa55f2e2019-06-13 15:21:38 -0400578
Robert Phillipsa41c6852019-02-07 10:44:10 -0500579 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
580
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500581 sk_sp<GrTexture> tex =
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400582 resourceProvider->wrapBackendTexture(backendTex, grColorType,
583 ownership, cacheable, ioType);
Brian Salomonf7778972018-03-08 10:13:17 -0500584 if (!tex) {
585 return nullptr;
586 }
Robert Phillipsadbe1322018-01-17 13:35:46 -0500587
Greg Daniel6a0176b2018-01-30 09:28:44 -0500588 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500589 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel6a0176b2018-01-30 09:28:44 -0500590 }
591
Brian Salomonf7778972018-03-08 10:13:17 -0500592 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
593 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500594 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500595
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400596 GrSwizzle texSwizzle = caps->getTextureSwizzle(tex->backendFormat(), grColorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400597
Brian Salomonbeb7f522019-08-30 16:19:42 -0400598 return sk_sp<GrTextureProxy>(
599 new GrTextureProxy(std::move(tex), origin, texSwizzle, UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500600}
601
Brian Salomon7578f3e2018-03-07 14:39:54 -0500602sk_sp<GrTextureProxy> GrProxyProvider::wrapRenderableBackendTexture(
Brian Salomon02bd2952018-03-07 15:20:21 -0500603 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400604 GrColorType colorType, GrWrapOwnership ownership, GrWrapCacheable cacheable,
605 ReleaseProc releaseProc, ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500606 if (this->isAbandoned()) {
607 return nullptr;
608 }
609
Brian Salomonf7778972018-03-08 10:13:17 -0500610 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500611 GrContext* direct = fImageContext->priv().asDirectContext();
612 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500613 return nullptr;
614 }
615
Robert Phillips0902c982019-07-16 07:47:56 -0400616 const GrCaps* caps = this->caps();
617
Robert Phillips62221e72019-07-24 15:07:38 -0400618 SkASSERT(caps->areColorTypeAndFormatCompatible(colorType, backendTex.getBackendFormat()));
Greg Danielfa55f2e2019-06-13 15:21:38 -0400619
Robert Phillipsa41c6852019-02-07 10:44:10 -0500620 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
621
Greg Daniel6fa62e22019-08-07 15:52:37 -0400622 // TODO: This should have been checked and validated before getting into GrProxyProvider.
623 if (!caps->isFormatAsColorTypeRenderable(colorType, backendTex.getBackendFormat(), sampleCnt)) {
Greg Danielf87651e2018-02-21 11:36:53 -0500624 return nullptr;
625 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500626
Greg Daniel6fa62e22019-08-07 15:52:37 -0400627 sampleCnt = caps->getRenderTargetSampleCount(sampleCnt, backendTex.getBackendFormat());
628 SkASSERT(sampleCnt);
629
Robert Phillipsa41c6852019-02-07 10:44:10 -0500630 sk_sp<GrTexture> tex = resourceProvider->wrapRenderableBackendTexture(backendTex, sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400631 colorType, ownership,
632 cacheable);
Brian Salomonf7778972018-03-08 10:13:17 -0500633 if (!tex) {
634 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500635 }
636
Greg Daniel8ce79912019-02-05 10:08:43 -0500637 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500638 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500639 }
640
Brian Salomonf7778972018-03-08 10:13:17 -0500641 SkASSERT(tex->asRenderTarget()); // A GrTextureRenderTarget
642 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500643 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Greg Daniel6abda432018-02-15 14:55:00 -0500644
Robert Phillips0902c982019-07-16 07:47:56 -0400645 GrSwizzle texSwizzle = caps->getTextureSwizzle(tex->backendFormat(), colorType);
646 GrSwizzle outSwizzle = caps->getOutputSwizzle(tex->backendFormat(), colorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400647
648 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin, texSwizzle,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400649 outSwizzle, UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500650}
651
Brian Salomon7578f3e2018-03-07 14:39:54 -0500652sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400653 const GrBackendRenderTarget& backendRT, GrColorType grColorType,
654 GrSurfaceOrigin origin, ReleaseProc releaseProc, ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500655 if (this->isAbandoned()) {
656 return nullptr;
657 }
658
Brian Salomonf7778972018-03-08 10:13:17 -0500659 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500660 GrContext* direct = fImageContext->priv().asDirectContext();
661 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500662 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500663 }
664
Robert Phillips62221e72019-07-24 15:07:38 -0400665 const GrCaps* caps = this->caps();
666
667 SkASSERT(caps->areColorTypeAndFormatCompatible(grColorType, backendRT.getBackendFormat()));
Greg Danielfa55f2e2019-06-13 15:21:38 -0400668
Robert Phillipsa41c6852019-02-07 10:44:10 -0500669 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
670
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400671 sk_sp<GrRenderTarget> rt = resourceProvider->wrapBackendRenderTarget(backendRT, grColorType);
Brian Salomonf7778972018-03-08 10:13:17 -0500672 if (!rt) {
673 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500674 }
Greg Daniel8ce79912019-02-05 10:08:43 -0500675
Greg Daniel8ce79912019-02-05 10:08:43 -0500676 if (releaseProc) {
Brian Salomon2ca31f82019-03-05 13:28:58 -0500677 rt->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500678 }
679
Brian Salomonf7778972018-03-08 10:13:17 -0500680 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
681 SkASSERT(!rt->getUniqueKey().isValid());
682 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500683 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Brian Salomonf7778972018-03-08 10:13:17 -0500684
Robert Phillips62221e72019-07-24 15:07:38 -0400685 GrSwizzle texSwizzle = caps->getTextureSwizzle(rt->backendFormat(), grColorType);
686 GrSwizzle outSwizzle = caps->getOutputSwizzle(rt->backendFormat(), grColorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400687
688 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(std::move(rt), origin, texSwizzle,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400689 outSwizzle, UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500690}
691
Brian Salomon7578f3e2018-03-07 14:39:54 -0500692sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendTextureAsRenderTarget(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400693 const GrBackendTexture& backendTex, GrColorType grColorType,
694 GrSurfaceOrigin origin, int sampleCnt) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500695 if (this->isAbandoned()) {
696 return nullptr;
697 }
698
Brian Salomonf7778972018-03-08 10:13:17 -0500699 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500700 GrContext* direct = fImageContext->priv().asDirectContext();
701 if (!direct) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500702 return nullptr;
703 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500704
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400705 const GrCaps* caps = this->caps();
706
Robert Phillips62221e72019-07-24 15:07:38 -0400707 SkASSERT(caps->areColorTypeAndFormatCompatible(grColorType, backendTex.getBackendFormat()));
Greg Danielfa55f2e2019-06-13 15:21:38 -0400708
Robert Phillipsa41c6852019-02-07 10:44:10 -0500709 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
710
Brian Salomonf7778972018-03-08 10:13:17 -0500711 sk_sp<GrRenderTarget> rt =
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400712 resourceProvider->wrapBackendTextureAsRenderTarget(backendTex, sampleCnt, grColorType);
Brian Salomonf7778972018-03-08 10:13:17 -0500713 if (!rt) {
714 return nullptr;
Greg Danielf87651e2018-02-21 11:36:53 -0500715 }
Brian Salomonf7778972018-03-08 10:13:17 -0500716 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
717 SkASSERT(!rt->getUniqueKey().isValid());
Greg Danielb46add82019-01-02 14:51:29 -0500718 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500719 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielf87651e2018-02-21 11:36:53 -0500720
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400721 GrSwizzle texSwizzle = caps->getTextureSwizzle(rt->backendFormat(), grColorType);
722 GrSwizzle outSwizzle = caps->getOutputSwizzle(rt->backendFormat(), grColorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400723
724 return sk_sp<GrSurfaceProxy>(new GrRenderTargetProxy(std::move(rt), origin, texSwizzle,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400725 outSwizzle, UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500726}
727
Greg Danielb46add82019-01-02 14:51:29 -0500728sk_sp<GrRenderTargetProxy> GrProxyProvider::wrapVulkanSecondaryCBAsRenderTarget(
729 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
730 if (this->isAbandoned()) {
731 return nullptr;
732 }
733
734 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500735 GrContext* direct = fImageContext->priv().asDirectContext();
736 if (!direct) {
Greg Danielb46add82019-01-02 14:51:29 -0500737 return nullptr;
738 }
739
Robert Phillipsa41c6852019-02-07 10:44:10 -0500740 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Danielb46add82019-01-02 14:51:29 -0500741
Robert Phillipsa41c6852019-02-07 10:44:10 -0500742 sk_sp<GrRenderTarget> rt = resourceProvider->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
743 vkInfo);
Greg Danielb46add82019-01-02 14:51:29 -0500744 if (!rt) {
745 return nullptr;
746 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500747
Greg Danielb46add82019-01-02 14:51:29 -0500748 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
749 SkASSERT(!rt->getUniqueKey().isValid());
750 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500751 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielb46add82019-01-02 14:51:29 -0500752
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400753 GrColorType colorType = SkColorTypeToGrColorType(imageInfo.colorType());
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400754 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(rt->backendFormat(), colorType);
755 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(rt->backendFormat(), colorType);
756
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400757 if (!this->caps()->isFormatAsColorTypeRenderable(colorType, rt->backendFormat(),
758 rt->numSamples())) {
759 return nullptr;
760 }
761
Greg Danielb46add82019-01-02 14:51:29 -0500762 // All Vulkan surfaces uses top left origins.
Brian Salomonbeb7f522019-08-30 16:19:42 -0400763 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
764 std::move(rt), kTopLeft_GrSurfaceOrigin, texSwizzle, outSwizzle, UseAllocator::kNo,
765 GrRenderTargetProxy::WrapsVkSecondaryCB::kYes));
Brian Salomone8a766b2019-07-19 14:24:36 -0400766}
767
768sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
769 const GrBackendFormat& format,
770 const GrSurfaceDesc& desc,
771 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400772 int renderTargetSampleCnt,
Brian Salomone8a766b2019-07-19 14:24:36 -0400773 GrSurfaceOrigin origin,
774 GrMipMapped mipMapped,
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600775 GrMipMapsStatus mipMapsStatus,
Brian Salomone8a766b2019-07-19 14:24:36 -0400776 GrInternalSurfaceFlags surfaceFlags,
777 SkBackingFit fit,
778 SkBudgeted budgeted,
779 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400780 GrSurfaceProxy::UseAllocator useAllocator) {
Robert Phillips777707b2018-01-17 11:40:14 -0500781 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
782 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400783
Robert Phillips0a15cc62019-07-30 12:49:10 -0400784 if (!format.isValid()) {
785 return nullptr;
786 }
787
Robert Phillipsa41c6852019-02-07 10:44:10 -0500788 if (desc.fWidth > this->caps()->maxTextureSize() ||
789 desc.fHeight > this->caps()->maxTextureSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400790 return nullptr;
791 }
792
Greg Danielfa55f2e2019-06-13 15:21:38 -0400793 SkASSERT(validate_backend_format_and_config(this->caps(), format, desc.fConfig));
Greg Daniel457469c2018-02-08 15:05:44 -0500794
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400795 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
796 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(format, colorType);
797 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(format, colorType);
798
Brian Salomonbeb7f522019-08-30 16:19:42 -0400799 if (renderable == GrRenderable::kYes) {
800 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(*this->caps(),
801 std::move(callback),
802 format,
803 desc,
804 renderTargetSampleCnt,
805 origin,
806 mipMapped,
807 mipMapsStatus,
808 texSwizzle,
809 outSwizzle,
810 fit,
811 budgeted,
812 isProtected,
813 surfaceFlags,
814 useAllocator));
815 } else {
816 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(callback),
817 format,
818 desc,
819 origin,
820 mipMapped,
821 mipMapsStatus,
822 texSwizzle,
823 fit,
824 budgeted,
825 isProtected,
826 surfaceFlags,
827 useAllocator));
828 }
Robert Phillips777707b2018-01-17 11:40:14 -0500829}
830
Robert Phillipse8fabb22018-02-04 14:33:21 -0500831sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy(
Brian Salomonbeb7f522019-08-30 16:19:42 -0400832 LazyInstantiateCallback&& callback,
833 const GrBackendFormat& format,
834 const GrSurfaceDesc& desc,
835 int sampleCnt,
836 GrSurfaceOrigin origin,
837 GrInternalSurfaceFlags surfaceFlags,
838 const TextureInfo* textureInfo,
839 GrMipMapsStatus mipMapsStatus,
840 SkBackingFit fit,
841 SkBudgeted budgeted,
842 GrProtected isProtected,
843 bool wrapsVkSecondaryCB,
844 UseAllocator useAllocator) {
Robert Phillipse8fabb22018-02-04 14:33:21 -0500845 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
846 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400847
Robert Phillipsa41c6852019-02-07 10:44:10 -0500848 if (desc.fWidth > this->caps()->maxRenderTargetSize() ||
849 desc.fHeight > this->caps()->maxRenderTargetSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400850 return nullptr;
851 }
852
Greg Danielfa55f2e2019-06-13 15:21:38 -0400853 SkASSERT(validate_backend_format_and_config(this->caps(), format, desc.fConfig));
Greg Daniel457469c2018-02-08 15:05:44 -0500854
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400855 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
856 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(format, colorType);
857 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(format, colorType);
858
Brian Salomon7226c232018-07-30 13:13:17 -0400859 if (textureInfo) {
Greg Danielb085fa92019-03-05 16:55:12 -0500860 // Wrapped vulkan secondary command buffers don't support texturing since we won't have an
861 // actual VkImage to texture from.
862 SkASSERT(!wrapsVkSecondaryCB);
Brian Salomon7226c232018-07-30 13:13:17 -0400863 return sk_sp<GrRenderTargetProxy>(new GrTextureRenderTargetProxy(
Brian Salomonbeb7f522019-08-30 16:19:42 -0400864 *this->caps(), std::move(callback), format, desc, sampleCnt, origin,
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600865 textureInfo->fMipMapped, mipMapsStatus, texSwizzle, outSwizzle, fit, budgeted,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400866 isProtected, surfaceFlags, useAllocator));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500867 }
868
Greg Danielb085fa92019-03-05 16:55:12 -0500869 GrRenderTargetProxy::WrapsVkSecondaryCB vkSCB =
870 wrapsVkSecondaryCB ? GrRenderTargetProxy::WrapsVkSecondaryCB::kYes
871 : GrRenderTargetProxy::WrapsVkSecondaryCB::kNo;
872
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400873 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
Brian Salomonbeb7f522019-08-30 16:19:42 -0400874 std::move(callback), format, desc, sampleCnt, origin, texSwizzle, outSwizzle, fit,
875 budgeted, isProtected, surfaceFlags, useAllocator, vkSCB));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500876}
877
Brian Salomonbeb7f522019-08-30 16:19:42 -0400878sk_sp<GrTextureProxy> GrProxyProvider::MakeFullyLazyProxy(LazyInstantiateCallback&& callback,
879 const GrBackendFormat& format,
880 GrRenderable renderable,
881 int renderTargetSampleCnt,
882 GrProtected isProtected,
883 GrSurfaceOrigin origin,
884 GrPixelConfig config,
885 const GrCaps& caps,
886 UseAllocator useAllocator) {
Robert Phillips0a15cc62019-07-30 12:49:10 -0400887 if (!format.isValid()) {
888 return nullptr;
889 }
890
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400891 SkASSERT(renderTargetSampleCnt == 1 || renderable == GrRenderable::kYes);
Greg Danielfa55f2e2019-06-13 15:21:38 -0400892 SkASSERT(validate_backend_format_and_config(&caps, format, config));
Robert Phillips777707b2018-01-17 11:40:14 -0500893 GrSurfaceDesc desc;
Robert Phillips10d17212019-04-24 14:09:10 -0400894 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNone;
Robert Phillips777707b2018-01-17 11:40:14 -0500895 desc.fWidth = -1;
896 desc.fHeight = -1;
897 desc.fConfig = config;
Robert Phillips777707b2018-01-17 11:40:14 -0500898
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400899 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
900 GrSwizzle texSwizzle = caps.getTextureSwizzle(format, colorType);
901 GrSwizzle outSwizzle = caps.getOutputSwizzle(format, colorType);
902
Brian Salomonbeb7f522019-08-30 16:19:42 -0400903 if (GrRenderable::kYes == renderable) {
904 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
905 caps, std::move(callback), format, desc, renderTargetSampleCnt, origin,
906 GrMipMapped::kNo, GrMipMapsStatus::kNotAllocated, texSwizzle, outSwizzle,
907 SkBackingFit::kApprox, SkBudgeted::kYes, isProtected, surfaceFlags, useAllocator));
908 } else {
909 return sk_sp<GrTextureProxy>(new GrTextureProxy(
910 std::move(callback), format, desc, origin, GrMipMapped::kNo,
911 GrMipMapsStatus::kNotAllocated, texSwizzle, SkBackingFit::kApprox, SkBudgeted::kYes,
912 isProtected, surfaceFlags, useAllocator));
913 }
Robert Phillips777707b2018-01-17 11:40:14 -0500914}
915
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500916bool GrProxyProvider::IsFunctionallyExact(GrSurfaceProxy* proxy) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400917 const bool isInstantiated = proxy->isInstantiated();
Robert Phillipsdb3b9792018-06-12 15:18:00 -0400918 // A proxy is functionally exact if:
919 // it is exact (obvs)
920 // when it is instantiated it will be exact (i.e., power of two dimensions)
921 // it is already instantiated and the proxy covers the entire backing surface
922 return proxy->priv().isExact() ||
923 (!isInstantiated && SkIsPow2(proxy->width()) && SkIsPow2(proxy->height())) ||
924 (isInstantiated && proxy->worstCaseWidth() == proxy->width() &&
925 proxy->worstCaseHeight() == proxy->height());
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500926}
927
Robert Phillips427966a2018-12-20 17:20:43 -0500928void GrProxyProvider::processInvalidUniqueKey(const GrUniqueKey& key, GrTextureProxy* proxy,
929 InvalidateGPUResource invalidateGPUResource) {
Chris Dalton2de13dd2019-01-03 15:11:59 -0700930 SkASSERT(key.isValid());
931
Robert Phillips427966a2018-12-20 17:20:43 -0500932 if (!proxy) {
933 proxy = fUniquelyKeyedProxies.find(key);
934 }
Chris Dalton2de13dd2019-01-03 15:11:59 -0700935 SkASSERT(!proxy || proxy->getUniqueKey() == key);
936
937 // Locate the corresponding GrGpuResource (if it needs to be invalidated) before clearing the
938 // proxy's unique key. We must do it in this order because 'key' may alias the proxy's key.
939 sk_sp<GrGpuResource> invalidGpuResource;
940 if (InvalidateGPUResource::kYes == invalidateGPUResource) {
Brian Salomon01ceae92019-04-02 11:49:54 -0400941 GrContext* direct = fImageContext->priv().asDirectContext();
942 if (direct) {
943 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
944 invalidGpuResource = resourceProvider->findByUniqueKey<GrGpuResource>(key);
Chris Dalton2de13dd2019-01-03 15:11:59 -0700945 }
946 SkASSERT(!invalidGpuResource || invalidGpuResource->getUniqueKey() == key);
947 }
Robert Phillips427966a2018-12-20 17:20:43 -0500948
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500949 // Note: this method is called for the whole variety of GrGpuResources so often 'key'
950 // will not be in 'fUniquelyKeyedProxies'.
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500951 if (proxy) {
Robert Phillips427966a2018-12-20 17:20:43 -0500952 fUniquelyKeyedProxies.remove(key);
953 proxy->cacheAccess().clearUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500954 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500955
Chris Dalton2de13dd2019-01-03 15:11:59 -0700956 if (invalidGpuResource) {
957 invalidGpuResource->resourcePriv().removeUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500958 }
959}
960
Robert Phillipsa41c6852019-02-07 10:44:10 -0500961uint32_t GrProxyProvider::contextID() const {
962 return fImageContext->priv().contextID();
963}
964
965const GrCaps* GrProxyProvider::caps() const {
966 return fImageContext->priv().caps();
967}
968
969sk_sp<const GrCaps> GrProxyProvider::refCaps() const {
970 return fImageContext->priv().refCaps();
971}
972
Robert Phillipsa9162df2019-02-11 14:12:03 -0500973bool GrProxyProvider::isAbandoned() const {
974 return fImageContext->priv().abandoned();
975}
976
Robert Phillips0790f8a2018-09-18 13:11:03 -0400977void GrProxyProvider::orphanAllUniqueKeys() {
978 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
979 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
980 GrTextureProxy& tmp = *iter;
981
982 tmp.fProxyProvider = nullptr;
983 }
984}
985
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500986void GrProxyProvider::removeAllUniqueKeys() {
987 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
988 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
989 GrTextureProxy& tmp = *iter;
990
Chris Dalton2de13dd2019-01-03 15:11:59 -0700991 this->processInvalidUniqueKey(tmp.getUniqueKey(), &tmp, InvalidateGPUResource::kNo);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500992 }
993 SkASSERT(!fUniquelyKeyedProxies.count());
994}
Robert Phillipsa41c6852019-02-07 10:44:10 -0500995
996bool GrProxyProvider::renderingDirectly() const {
997 return fImageContext->priv().asDirectContext();
998}