blob: 33dd09dddc4e7962faf67a9add564511c32c82ba [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) {
Robert Phillipse5f73282019-06-18 17:15:04 -0400107 SkASSERT(proxy->origin() == origin);
108 return sk_ref_sp(proxy);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500109 }
Robert Phillipse5f73282019-06-18 17:15:04 -0400110 return nullptr;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500111}
112
Robert Phillipsa41c6852019-02-07 10:44:10 -0500113///////////////////////////////////////////////////////////////////////////////
114
115#if GR_TEST_UTILS
116sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createInstantiatedProxy(
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400117 const SkISize& dimensions,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400118 GrColorType colorType,
119 const GrBackendFormat& format,
120 GrRenderable renderable,
121 int renderTargetSampleCnt,
122 GrSurfaceOrigin origin,
123 SkBackingFit fit,
124 SkBudgeted budgeted,
125 GrProtected isProtected) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500126 GrContext* direct = fImageContext->priv().asDirectContext();
127 if (!direct) {
128 return nullptr;
129 }
130
Brian Salomon4eb38b72019-08-05 12:58:39 -0400131 if (this->caps()->isFormatCompressed(format)) {
132 // TODO: Allow this to go to GrResourceProvider::createCompressedTexture() once we no longer
133 // rely on GrColorType to get to GrPixelConfig. Currently this will cause
134 // makeConfigSpecific() to assert because GrColorTypeToPixelConfig() never returns a
135 // compressed GrPixelConfig.
136 return nullptr;
137 }
138 GrSurfaceDesc desc;
139 desc.fConfig = GrColorTypeToPixelConfig(colorType);
140 desc.fConfig = this->caps()->makeConfigSpecific(desc.fConfig, format);
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400141 desc.fWidth = dimensions.width();
142 desc.fHeight = dimensions.height();
Brian Salomon4eb38b72019-08-05 12:58:39 -0400143
Robert Phillipsa41c6852019-02-07 10:44:10 -0500144 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
145 sk_sp<GrTexture> tex;
146
147 if (SkBackingFit::kApprox == fit) {
Brian Salomon4eb38b72019-08-05 12:58:39 -0400148 tex = resourceProvider->createApproxTexture(desc, format, renderable, renderTargetSampleCnt,
Robert Phillipsaee18c92019-09-06 11:48:27 -0400149 isProtected);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500150 } else {
Brian Salomon4eb38b72019-08-05 12:58:39 -0400151 tex = resourceProvider->createTexture(desc, format, renderable, renderTargetSampleCnt,
Brian Salomona90382f2019-09-17 09:01:56 -0400152 GrMipMapped::kNo, budgeted, isProtected);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500153 }
154 if (!tex) {
155 return nullptr;
156 }
157
Brian Salomonbeb7f522019-08-30 16:19:42 -0400158 return this->createWrapped(std::move(tex), colorType, origin, UseAllocator::kYes);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500159}
160
Brian Salomon4eb38b72019-08-05 12:58:39 -0400161sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createInstantiatedProxy(
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400162 const SkISize& dimensions,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400163 GrColorType colorType,
164 GrRenderable renderable,
165 int renderTargetSampleCnt,
166 GrSurfaceOrigin origin,
167 SkBackingFit fit,
168 SkBudgeted budgeted,
169 GrProtected isProtected) {
170 auto format = this->caps()->getDefaultBackendFormat(colorType, renderable);
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400171 return this->testingOnly_createInstantiatedProxy(dimensions,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400172 colorType,
173 format,
174 renderable,
175 renderTargetSampleCnt,
176 origin,
177 fit,
178 budgeted,
179 isProtected);
180}
181
Robert Phillipsa41c6852019-02-07 10:44:10 -0500182sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createWrapped(sk_sp<GrTexture> tex,
Brian Salomon2af3e702019-08-11 19:10:31 -0400183 GrColorType colorType,
Robert Phillipsa41c6852019-02-07 10:44:10 -0500184 GrSurfaceOrigin origin) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400185 return this->createWrapped(std::move(tex), colorType, origin, UseAllocator::kYes);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500186}
187#endif
188
Brian Salomonbeb7f522019-08-30 16:19:42 -0400189sk_sp<GrTextureProxy> GrProxyProvider::createWrapped(sk_sp<GrTexture> tex,
190 GrColorType colorType,
191 GrSurfaceOrigin origin,
192 UseAllocator useAllocator) {
Robert Phillipsadbe1322018-01-17 13:35:46 -0500193#ifdef SK_DEBUG
194 if (tex->getUniqueKey().isValid()) {
195 SkASSERT(!this->findProxyByUniqueKey(tex->getUniqueKey(), origin));
196 }
197#endif
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400198 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(tex->backendFormat(), colorType);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500199
200 if (tex->asRenderTarget()) {
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400201 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(tex->backendFormat(), colorType);
Brian Salomonbeb7f522019-08-30 16:19:42 -0400202 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
203 std::move(tex), origin, texSwizzle, outSwizzle, useAllocator));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500204 } else {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400205 return sk_sp<GrTextureProxy>(
206 new GrTextureProxy(std::move(tex), origin, texSwizzle, useAllocator));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500207 }
208}
209
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500210sk_sp<GrTextureProxy> GrProxyProvider::findOrCreateProxyByUniqueKey(const GrUniqueKey& key,
Brian Salomon2af3e702019-08-11 19:10:31 -0400211 GrColorType colorType,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400212 GrSurfaceOrigin origin,
213 UseAllocator useAllocator) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500214 ASSERT_SINGLE_OWNER
215
216 if (this->isAbandoned()) {
217 return nullptr;
218 }
219
220 sk_sp<GrTextureProxy> result = this->findProxyByUniqueKey(key, origin);
221 if (result) {
222 return result;
223 }
224
Robert Phillipsa41c6852019-02-07 10:44:10 -0500225 GrContext* direct = fImageContext->priv().asDirectContext();
226 if (!direct) {
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500227 return nullptr;
228 }
229
Robert Phillipsa41c6852019-02-07 10:44:10 -0500230 GrResourceCache* resourceCache = direct->priv().getResourceCache();
231
232 GrGpuResource* resource = resourceCache->findAndRefUniqueResource(key);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500233 if (!resource) {
234 return nullptr;
235 }
236
237 sk_sp<GrTexture> texture(static_cast<GrSurface*>(resource)->asTexture());
238 SkASSERT(texture);
239
Brian Salomonbeb7f522019-08-30 16:19:42 -0400240 result = this->createWrapped(std::move(texture), colorType, origin, useAllocator);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500241 SkASSERT(result->getUniqueKey() == key);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500242 // createWrapped should've added this for us
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500243 SkASSERT(fUniquelyKeyedProxies.find(key));
Brian Salomon2af3e702019-08-11 19:10:31 -0400244 SkASSERT(result->textureSwizzle() ==
245 this->caps()->getTextureSwizzle(result->backendFormat(), colorType));
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500246 return result;
247}
248
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500249sk_sp<GrTextureProxy> GrProxyProvider::createTextureProxy(sk_sp<SkImage> srcImage,
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500250 int sampleCnt,
Greg Danielfb3abcd2018-02-02 15:48:33 -0500251 SkBudgeted budgeted,
Chris Daltond004e0b2018-09-27 09:28:03 -0600252 SkBackingFit fit,
253 GrInternalSurfaceFlags surfaceFlags) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500254 ASSERT_SINGLE_OWNER
255 SkASSERT(srcImage);
256
257 if (this->isAbandoned()) {
258 return nullptr;
259 }
260
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400261 const SkImageInfo& info = srcImage->imageInfo();
Brian Salomon2af3e702019-08-11 19:10:31 -0400262 GrColorType ct = SkColorTypeToGrColorType(info.colorType());
Greg Danielf87651e2018-02-21 11:36:53 -0500263
Brian Salomon96b383a2019-08-13 16:55:41 -0400264 GrBackendFormat format = this->caps()->getDefaultBackendFormat(ct, GrRenderable::kNo);
Greg Daniel0a7aa142018-02-21 13:02:32 -0500265
Robert Phillips0a15cc62019-07-30 12:49:10 -0400266 if (!format.isValid()) {
Brian Osmand29dcd12018-09-13 15:04:29 -0400267 SkBitmap copy8888;
268 if (!copy8888.tryAllocPixels(info.makeColorType(kRGBA_8888_SkColorType)) ||
269 !srcImage->readPixels(copy8888.pixmap(), 0, 0)) {
270 return nullptr;
271 }
272 copy8888.setImmutable();
273 srcImage = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode);
Brian Salomon2af3e702019-08-11 19:10:31 -0400274 ct = GrColorType::kRGBA_8888;
Brian Salomon96b383a2019-08-13 16:55:41 -0400275 format = this->caps()->getDefaultBackendFormat(ct, GrRenderable::kNo);
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400276 if (!format.isValid()) {
277 return nullptr;
278 }
Brian Osmand29dcd12018-09-13 15:04:29 -0400279 }
280
Brian Salomon2af3e702019-08-11 19:10:31 -0400281 GrPixelConfig config = GrColorTypeToPixelConfig(ct);
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400282 if (kUnknown_GrPixelConfig == config) {
283 return nullptr;
284 }
285
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500286 GrSurfaceDesc desc;
287 desc.fWidth = srcImage->width();
288 desc.fHeight = srcImage->height();
Greg Danielf87651e2018-02-21 11:36:53 -0500289 desc.fConfig = config;
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500290
291 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomon96b383a2019-08-13 16:55:41 -0400292 [desc, format, sampleCnt, budgeted, srcImage, fit,
Brian Salomon2af3e702019-08-11 19:10:31 -0400293 ct](GrResourceProvider* resourceProvider) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500294 SkPixmap pixMap;
295 SkAssertResult(srcImage->peekPixels(&pixMap));
296 GrMipLevel mipLevel = { pixMap.addr(), pixMap.rowBytes() };
297
Brian Salomonbeb7f522019-08-30 16:19:42 -0400298 return LazyCallbackResult(resourceProvider->createTexture(
Brian Salomona90382f2019-09-17 09:01:56 -0400299 desc, format, ct, GrRenderable::kNo, sampleCnt, budgeted, fit,
300 GrProtected::kNo, mipLevel));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500301 },
Brian Salomon96b383a2019-08-13 16:55:41 -0400302 format, desc, GrRenderable::kNo, sampleCnt, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400303 GrMipMapsStatus::kNotAllocated, surfaceFlags, fit, budgeted, GrProtected::kNo,
304 UseAllocator::kYes);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500305
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400306 if (!proxy) {
307 return nullptr;
308 }
309
Robert Phillipsa41c6852019-02-07 10:44:10 -0500310 GrContext* direct = fImageContext->priv().asDirectContext();
311 if (direct) {
312 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
313
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500314 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
315 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500316 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500317 return nullptr;
318 }
319 }
Robert Phillipsc1b60662018-06-26 10:20:08 -0400320
321 SkASSERT(proxy->width() == desc.fWidth);
322 SkASSERT(proxy->height() == desc.fHeight);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500323 return proxy;
324}
325
Brian Osmande496652019-03-22 13:42:33 -0400326sk_sp<GrTextureProxy> GrProxyProvider::createProxyFromBitmap(const SkBitmap& bitmap,
327 GrMipMapped mipMapped) {
Brian Osman412674f2019-02-07 15:34:58 -0500328 ASSERT_SINGLE_OWNER
Brian Osman7dcc6162019-03-25 10:12:57 -0400329 SkASSERT(GrMipMapped::kNo == mipMapped || this->caps()->mipMapSupport());
Brian Osman412674f2019-02-07 15:34:58 -0500330
331 if (this->isAbandoned()) {
332 return nullptr;
333 }
334
Brian Osman2b23c4b2018-06-01 12:25:08 -0400335 if (!SkImageInfoIsValid(bitmap.info())) {
Greg Daniela4ead652018-02-07 10:21:48 -0500336 return nullptr;
337 }
338
Brian Osmande496652019-03-22 13:42:33 -0400339 ATRACE_ANDROID_FRAMEWORK("Upload %sTexture [%ux%u]",
340 GrMipMapped::kYes == mipMapped ? "MipMap " : "",
341 bitmap.width(), bitmap.height());
Greg Daniela4ead652018-02-07 10:21:48 -0500342
343 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
344 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
345 // 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 -0500346 SkCopyPixelsMode copyMode = this->renderingDirectly() ? kNever_SkCopyPixelsMode
347 : kIfMutable_SkCopyPixelsMode;
Greg Daniela4ead652018-02-07 10:21:48 -0500348 sk_sp<SkImage> baseLevel = SkMakeImageFromRasterBitmap(bitmap, copyMode);
Greg Daniela4ead652018-02-07 10:21:48 -0500349 if (!baseLevel) {
350 return nullptr;
351 }
352
Brian Osmande496652019-03-22 13:42:33 -0400353 // If mips weren't requested (or this was too small to have any), then take the fast path
354 if (GrMipMapped::kNo == mipMapped ||
355 0 == SkMipMap::ComputeLevelCount(baseLevel->width(), baseLevel->height())) {
Brian Salomon96b383a2019-08-13 16:55:41 -0400356 return this->createTextureProxy(std::move(baseLevel), 1, SkBudgeted::kYes,
357 SkBackingFit::kExact);
Greg Daniela4ead652018-02-07 10:21:48 -0500358 }
359
Brian Osmand29dcd12018-09-13 15:04:29 -0400360 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info());
Greg Daniel3fc5df42019-06-14 09:42:17 -0400361
Brian Salomona90382f2019-09-17 09:01:56 -0400362 GrBackendFormat format = this->caps()->getDefaultBackendFormat(
363 SkColorTypeToGrColorType(bitmap.info().colorType()), GrRenderable::kNo);
Robert Phillips0a15cc62019-07-30 12:49:10 -0400364 if (!format.isValid()) {
Brian Osmand29dcd12018-09-13 15:04:29 -0400365 SkBitmap copy8888;
366 if (!copy8888.tryAllocPixels(bitmap.info().makeColorType(kRGBA_8888_SkColorType)) ||
367 !bitmap.readPixels(copy8888.pixmap())) {
368 return nullptr;
369 }
370 copy8888.setImmutable();
371 baseLevel = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode);
372 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Salomona90382f2019-09-17 09:01:56 -0400373 format = this->caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888, GrRenderable::kNo);
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400374 if (!format.isValid()) {
375 return nullptr;
376 }
Greg Daniel3fc5df42019-06-14 09:42:17 -0400377 }
378
Brian Osmand29dcd12018-09-13 15:04:29 -0400379 SkPixmap pixmap;
380 SkAssertResult(baseLevel->peekPixels(&pixmap));
381 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(pixmap, nullptr));
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400382 if (!mipmaps) {
383 return nullptr;
384 }
385
Greg Daniela4ead652018-02-07 10:21:48 -0500386 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomon4eb38b72019-08-05 12:58:39 -0400387 [desc, format, baseLevel, mipmaps](GrResourceProvider* resourceProvider) {
Brian Osman1b97f132018-09-13 17:33:48 +0000388 const int mipLevelCount = mipmaps->countLevels() + 1;
389 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
390
Greg Daniela4ead652018-02-07 10:21:48 -0500391 SkPixmap pixmap;
392 SkAssertResult(baseLevel->peekPixels(&pixmap));
393
Greg Daniela4ead652018-02-07 10:21:48 -0500394 texels[0].fPixels = pixmap.addr();
395 texels[0].fRowBytes = pixmap.rowBytes();
396
Brian Salomona90382f2019-09-17 09:01:56 -0400397 auto colorType = SkColorTypeToGrColorType(pixmap.colorType());
Greg Daniela4ead652018-02-07 10:21:48 -0500398 for (int i = 1; i < mipLevelCount; ++i) {
399 SkMipMap::Level generatedMipLevel;
400 mipmaps->getLevel(i - 1, &generatedMipLevel);
401 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
402 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
403 SkASSERT(texels[i].fPixels);
Brian Salomona90382f2019-09-17 09:01:56 -0400404 SkASSERT(generatedMipLevel.fPixmap.colorType() == pixmap.colorType());
Greg Daniela4ead652018-02-07 10:21:48 -0500405 }
Brian Salomonbeb7f522019-08-30 16:19:42 -0400406 return LazyCallbackResult(resourceProvider->createTexture(
Brian Salomona90382f2019-09-17 09:01:56 -0400407 desc, format, colorType, GrRenderable::kNo, 1, SkBudgeted::kYes,
408 GrProtected::kNo, texels.get(), mipLevelCount));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500409 },
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400410 format, desc, GrRenderable::kNo, 1, kTopLeft_GrSurfaceOrigin, GrMipMapped::kYes,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400411 GrMipMapsStatus::kValid, GrInternalSurfaceFlags::kNone, SkBackingFit::kExact,
412 SkBudgeted::kYes, GrProtected::kNo, UseAllocator::kYes);
Greg Daniela4ead652018-02-07 10:21:48 -0500413
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400414 if (!proxy) {
415 return nullptr;
416 }
417
Robert Phillipsa41c6852019-02-07 10:44:10 -0500418 GrContext* direct = fImageContext->priv().asDirectContext();
419 if (direct) {
420 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Daniela4ead652018-02-07 10:21:48 -0500421 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
422 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500423 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Greg Daniela4ead652018-02-07 10:21:48 -0500424 return nullptr;
425 }
426 }
427 return proxy;
428}
429
Greg Daniel4065d452018-11-16 15:43:41 -0500430sk_sp<GrTextureProxy> GrProxyProvider::createProxy(const GrBackendFormat& format,
431 const GrSurfaceDesc& desc,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400432 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400433 int renderTargetSampleCnt,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500434 GrSurfaceOrigin origin,
Greg Danielf6f7b672018-02-15 13:06:26 -0500435 GrMipMapped mipMapped,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500436 SkBackingFit fit,
437 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -0400438 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400439 GrInternalSurfaceFlags surfaceFlags,
440 GrSurfaceProxy::UseAllocator useAllocator) {
Robert Phillips8ff8bcc2019-07-29 17:03:35 -0400441 const GrCaps* caps = this->caps();
442
443 if (caps->isFormatCompressed(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400444 // Deferred proxies for compressed textures are not supported.
445 return nullptr;
446 }
Robert Phillips0902c982019-07-16 07:47:56 -0400447
Robert Phillips0902c982019-07-16 07:47:56 -0400448 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
449
450 SkASSERT(GrCaps::AreConfigsCompatible(desc.fConfig,
451 caps->getConfigFromBackendFormat(format, colorType)));
Robert Phillips62221e72019-07-24 15:07:38 -0400452
Greg Danielf6f7b672018-02-15 13:06:26 -0500453 if (GrMipMapped::kYes == mipMapped) {
454 // SkMipMap doesn't include the base level in the level count so we have to add 1
455 int mipCount = SkMipMap::ComputeLevelCount(desc.fWidth, desc.fHeight) + 1;
456 if (1 == mipCount) {
457 mipMapped = GrMipMapped::kNo;
458 }
459 }
460
Greg Daniel6fa62e22019-08-07 15:52:37 -0400461 if (!caps->validateSurfaceParams({desc.fWidth, desc.fHeight}, format, desc.fConfig, renderable,
462 renderTargetSampleCnt, mipMapped)) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500463 return nullptr;
464 }
Brian Salomon3a2cc2c2018-02-03 00:25:12 +0000465 GrSurfaceDesc copyDesc = desc;
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600466 GrMipMapsStatus mipMapsStatus = (GrMipMapped::kYes == mipMapped)
467 ? GrMipMapsStatus::kDirty
468 : GrMipMapsStatus::kNotAllocated;
Robert Phillips0902c982019-07-16 07:47:56 -0400469 GrSwizzle texSwizzle = caps->getTextureSwizzle(format, colorType);
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400470 if (renderable == GrRenderable::kYes) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400471 renderTargetSampleCnt =
Greg Daniel6fa62e22019-08-07 15:52:37 -0400472 caps->getRenderTargetSampleCount(renderTargetSampleCnt, format);
473 SkASSERT(renderTargetSampleCnt);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500474 // We know anything we instantiate later from this deferred path will be
475 // both texturable and renderable
Robert Phillips0902c982019-07-16 07:47:56 -0400476 GrSwizzle outSwizzle = caps->getOutputSwizzle(format, colorType);
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400477 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600478 *caps, format, copyDesc, renderTargetSampleCnt, origin, mipMapped, mipMapsStatus,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400479 texSwizzle, outSwizzle, fit, budgeted, isProtected, surfaceFlags, useAllocator));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500480 }
481
Brian Salomonbeb7f522019-08-30 16:19:42 -0400482 return sk_sp<GrTextureProxy>(new GrTextureProxy(format, copyDesc, origin, mipMapped,
483 mipMapsStatus, texSwizzle, fit, budgeted,
484 isProtected, surfaceFlags, useAllocator));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500485}
486
Brian Salomonbb8dde82019-06-27 10:52:13 -0400487sk_sp<GrTextureProxy> GrProxyProvider::createCompressedTextureProxy(
488 int width, int height, SkBudgeted budgeted, SkImage::CompressionType compressionType,
489 sk_sp<SkData> data) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400490
491 GrSurfaceDesc desc;
492 desc.fConfig = GrCompressionTypePixelConfig(compressionType);
493 desc.fWidth = width;
494 desc.fHeight = height;
495
Robert Phillips8ff8bcc2019-07-29 17:03:35 -0400496 GrBackendFormat format = this->caps()->getBackendFormatFromCompressionType(compressionType);
497
Greg Daniel7bfc9132019-08-14 14:23:53 -0400498 if (!this->caps()->isFormatTexturable(format)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500499 return nullptr;
500 }
501
Jim Van Verthee06b332019-01-18 10:36:32 -0500502 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomonbeb7f522019-08-30 16:19:42 -0400503 [width, height, format, compressionType, budgeted,
504 data](GrResourceProvider* resourceProvider) {
505 return LazyCallbackResult(resourceProvider->createCompressedTexture(
Greg Daniel7bfc9132019-08-14 14:23:53 -0400506 width, height, format, compressionType, budgeted, data.get()));
Brian Salomonbb8dde82019-06-27 10:52:13 -0400507 },
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400508 format, desc, GrRenderable::kNo, 1, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo,
Greg Danielc3a927f2019-10-16 12:03:50 -0400509 GrMipMapsStatus::kNotAllocated, GrInternalSurfaceFlags::kReadOnly, SkBackingFit::kExact,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400510 SkBudgeted::kYes, GrProtected::kNo, UseAllocator::kYes);
Jim Van Verthee06b332019-01-18 10:36:32 -0500511
512 if (!proxy) {
513 return nullptr;
514 }
515
Robert Phillipsa41c6852019-02-07 10:44:10 -0500516 GrContext* direct = fImageContext->priv().asDirectContext();
517 if (direct) {
518 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Jim Van Verthee06b332019-01-18 10:36:32 -0500519 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
520 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500521 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500522 return nullptr;
523 }
524 }
525 return proxy;
526}
527
Brian Salomon7578f3e2018-03-07 14:39:54 -0500528sk_sp<GrTextureProxy> GrProxyProvider::wrapBackendTexture(const GrBackendTexture& backendTex,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400529 GrColorType grColorType,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500530 GrSurfaceOrigin origin,
531 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500532 GrWrapCacheable cacheable,
Brian Salomonc67c31c2018-12-06 10:00:03 -0500533 GrIOType ioType,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500534 ReleaseProc releaseProc,
535 ReleaseContext releaseCtx) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500536 SkASSERT(ioType != kWrite_GrIOType);
Robert Phillipsf9bec202018-01-16 09:21:01 -0500537 if (this->isAbandoned()) {
538 return nullptr;
539 }
540
Brian Salomonf7778972018-03-08 10:13:17 -0500541 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500542 GrContext* direct = fImageContext->priv().asDirectContext();
543 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500544 return nullptr;
545 }
546
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400547 const GrCaps* caps = this->caps();
548
Robert Phillipsa41c6852019-02-07 10:44:10 -0500549 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
550
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500551 sk_sp<GrTexture> tex =
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400552 resourceProvider->wrapBackendTexture(backendTex, grColorType,
553 ownership, cacheable, ioType);
Brian Salomonf7778972018-03-08 10:13:17 -0500554 if (!tex) {
555 return nullptr;
556 }
Robert Phillipsadbe1322018-01-17 13:35:46 -0500557
Greg Daniel6a0176b2018-01-30 09:28:44 -0500558 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500559 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel6a0176b2018-01-30 09:28:44 -0500560 }
561
Brian Salomonf7778972018-03-08 10:13:17 -0500562 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
563 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500564 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500565
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400566 GrSwizzle texSwizzle = caps->getTextureSwizzle(tex->backendFormat(), grColorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400567
Brian Salomonbeb7f522019-08-30 16:19:42 -0400568 return sk_sp<GrTextureProxy>(
569 new GrTextureProxy(std::move(tex), origin, texSwizzle, UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500570}
571
Brian Salomon7578f3e2018-03-07 14:39:54 -0500572sk_sp<GrTextureProxy> GrProxyProvider::wrapRenderableBackendTexture(
Brian Salomon02bd2952018-03-07 15:20:21 -0500573 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400574 GrColorType colorType, GrWrapOwnership ownership, GrWrapCacheable cacheable,
575 ReleaseProc releaseProc, ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500576 if (this->isAbandoned()) {
577 return nullptr;
578 }
579
Brian Salomonf7778972018-03-08 10:13:17 -0500580 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500581 GrContext* direct = fImageContext->priv().asDirectContext();
582 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500583 return nullptr;
584 }
585
Robert Phillips0902c982019-07-16 07:47:56 -0400586 const GrCaps* caps = this->caps();
587
Robert Phillipsa41c6852019-02-07 10:44:10 -0500588 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
589
Greg Daniel6fa62e22019-08-07 15:52:37 -0400590 // TODO: This should have been checked and validated before getting into GrProxyProvider.
591 if (!caps->isFormatAsColorTypeRenderable(colorType, backendTex.getBackendFormat(), sampleCnt)) {
Greg Danielf87651e2018-02-21 11:36:53 -0500592 return nullptr;
593 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500594
Greg Daniel6fa62e22019-08-07 15:52:37 -0400595 sampleCnt = caps->getRenderTargetSampleCount(sampleCnt, backendTex.getBackendFormat());
596 SkASSERT(sampleCnt);
597
Robert Phillipsa41c6852019-02-07 10:44:10 -0500598 sk_sp<GrTexture> tex = resourceProvider->wrapRenderableBackendTexture(backendTex, sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400599 colorType, ownership,
600 cacheable);
Brian Salomonf7778972018-03-08 10:13:17 -0500601 if (!tex) {
602 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500603 }
604
Greg Daniel8ce79912019-02-05 10:08:43 -0500605 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500606 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500607 }
608
Brian Salomonf7778972018-03-08 10:13:17 -0500609 SkASSERT(tex->asRenderTarget()); // A GrTextureRenderTarget
610 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500611 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Greg Daniel6abda432018-02-15 14:55:00 -0500612
Robert Phillips0902c982019-07-16 07:47:56 -0400613 GrSwizzle texSwizzle = caps->getTextureSwizzle(tex->backendFormat(), colorType);
614 GrSwizzle outSwizzle = caps->getOutputSwizzle(tex->backendFormat(), colorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400615
616 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin, texSwizzle,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400617 outSwizzle, UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500618}
619
Brian Salomon7578f3e2018-03-07 14:39:54 -0500620sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400621 const GrBackendRenderTarget& backendRT, GrColorType grColorType,
622 GrSurfaceOrigin origin, ReleaseProc releaseProc, ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500623 if (this->isAbandoned()) {
624 return nullptr;
625 }
626
Brian Salomonf7778972018-03-08 10:13:17 -0500627 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500628 GrContext* direct = fImageContext->priv().asDirectContext();
629 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500630 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500631 }
632
Robert Phillips62221e72019-07-24 15:07:38 -0400633 const GrCaps* caps = this->caps();
634
Robert Phillipsa41c6852019-02-07 10:44:10 -0500635 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
636
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400637 sk_sp<GrRenderTarget> rt = resourceProvider->wrapBackendRenderTarget(backendRT, grColorType);
Brian Salomonf7778972018-03-08 10:13:17 -0500638 if (!rt) {
639 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500640 }
Greg Daniel8ce79912019-02-05 10:08:43 -0500641
Greg Daniel8ce79912019-02-05 10:08:43 -0500642 if (releaseProc) {
Brian Salomon2ca31f82019-03-05 13:28:58 -0500643 rt->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500644 }
645
Brian Salomonf7778972018-03-08 10:13:17 -0500646 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
647 SkASSERT(!rt->getUniqueKey().isValid());
648 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500649 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Brian Salomonf7778972018-03-08 10:13:17 -0500650
Robert Phillips62221e72019-07-24 15:07:38 -0400651 GrSwizzle texSwizzle = caps->getTextureSwizzle(rt->backendFormat(), grColorType);
652 GrSwizzle outSwizzle = caps->getOutputSwizzle(rt->backendFormat(), grColorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400653
654 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(std::move(rt), origin, texSwizzle,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400655 outSwizzle, UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500656}
657
Brian Salomon7578f3e2018-03-07 14:39:54 -0500658sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendTextureAsRenderTarget(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400659 const GrBackendTexture& backendTex, GrColorType grColorType,
660 GrSurfaceOrigin origin, int sampleCnt) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500661 if (this->isAbandoned()) {
662 return nullptr;
663 }
664
Brian Salomonf7778972018-03-08 10:13:17 -0500665 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500666 GrContext* direct = fImageContext->priv().asDirectContext();
667 if (!direct) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500668 return nullptr;
669 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500670
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400671 const GrCaps* caps = this->caps();
672
Robert Phillipsa41c6852019-02-07 10:44:10 -0500673 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
674
Brian Salomonf7778972018-03-08 10:13:17 -0500675 sk_sp<GrRenderTarget> rt =
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400676 resourceProvider->wrapBackendTextureAsRenderTarget(backendTex, sampleCnt, grColorType);
Brian Salomonf7778972018-03-08 10:13:17 -0500677 if (!rt) {
678 return nullptr;
Greg Danielf87651e2018-02-21 11:36:53 -0500679 }
Brian Salomonf7778972018-03-08 10:13:17 -0500680 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
681 SkASSERT(!rt->getUniqueKey().isValid());
Greg Danielb46add82019-01-02 14:51:29 -0500682 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500683 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielf87651e2018-02-21 11:36:53 -0500684
Robert Phillipsc80b0e92019-07-23 10:27:09 -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<GrSurfaceProxy>(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
Greg Danielb46add82019-01-02 14:51:29 -0500692sk_sp<GrRenderTargetProxy> GrProxyProvider::wrapVulkanSecondaryCBAsRenderTarget(
693 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
694 if (this->isAbandoned()) {
695 return nullptr;
696 }
697
698 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500699 GrContext* direct = fImageContext->priv().asDirectContext();
700 if (!direct) {
Greg Danielb46add82019-01-02 14:51:29 -0500701 return nullptr;
702 }
703
Robert Phillipsa41c6852019-02-07 10:44:10 -0500704 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Danielb46add82019-01-02 14:51:29 -0500705
Robert Phillipsa41c6852019-02-07 10:44:10 -0500706 sk_sp<GrRenderTarget> rt = resourceProvider->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
707 vkInfo);
Greg Danielb46add82019-01-02 14:51:29 -0500708 if (!rt) {
709 return nullptr;
710 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500711
Greg Danielb46add82019-01-02 14:51:29 -0500712 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
713 SkASSERT(!rt->getUniqueKey().isValid());
714 // 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 Danielb46add82019-01-02 14:51:29 -0500716
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400717 GrColorType colorType = SkColorTypeToGrColorType(imageInfo.colorType());
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400718 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(rt->backendFormat(), colorType);
719 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(rt->backendFormat(), colorType);
720
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400721 if (!this->caps()->isFormatAsColorTypeRenderable(colorType, rt->backendFormat(),
722 rt->numSamples())) {
723 return nullptr;
724 }
725
Greg Danielb46add82019-01-02 14:51:29 -0500726 // All Vulkan surfaces uses top left origins.
Brian Salomonbeb7f522019-08-30 16:19:42 -0400727 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
728 std::move(rt), kTopLeft_GrSurfaceOrigin, texSwizzle, outSwizzle, UseAllocator::kNo,
729 GrRenderTargetProxy::WrapsVkSecondaryCB::kYes));
Brian Salomone8a766b2019-07-19 14:24:36 -0400730}
731
732sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
733 const GrBackendFormat& format,
734 const GrSurfaceDesc& desc,
735 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400736 int renderTargetSampleCnt,
Brian Salomone8a766b2019-07-19 14:24:36 -0400737 GrSurfaceOrigin origin,
738 GrMipMapped mipMapped,
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600739 GrMipMapsStatus mipMapsStatus,
Brian Salomone8a766b2019-07-19 14:24:36 -0400740 GrInternalSurfaceFlags surfaceFlags,
741 SkBackingFit fit,
742 SkBudgeted budgeted,
743 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400744 GrSurfaceProxy::UseAllocator useAllocator) {
Robert Phillips777707b2018-01-17 11:40:14 -0500745 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
746 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400747
Robert Phillips0a15cc62019-07-30 12:49:10 -0400748 if (!format.isValid()) {
749 return nullptr;
750 }
751
Robert Phillipsa41c6852019-02-07 10:44:10 -0500752 if (desc.fWidth > this->caps()->maxTextureSize() ||
753 desc.fHeight > this->caps()->maxTextureSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400754 return nullptr;
755 }
756
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400757 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
758 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(format, colorType);
759 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(format, colorType);
760
Brian Salomonbeb7f522019-08-30 16:19:42 -0400761 if (renderable == GrRenderable::kYes) {
762 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(*this->caps(),
763 std::move(callback),
764 format,
765 desc,
766 renderTargetSampleCnt,
767 origin,
768 mipMapped,
769 mipMapsStatus,
770 texSwizzle,
771 outSwizzle,
772 fit,
773 budgeted,
774 isProtected,
775 surfaceFlags,
776 useAllocator));
777 } else {
778 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(callback),
779 format,
780 desc,
781 origin,
782 mipMapped,
783 mipMapsStatus,
784 texSwizzle,
785 fit,
786 budgeted,
787 isProtected,
788 surfaceFlags,
789 useAllocator));
790 }
Robert Phillips777707b2018-01-17 11:40:14 -0500791}
792
Robert Phillipse8fabb22018-02-04 14:33:21 -0500793sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy(
Brian Salomonbeb7f522019-08-30 16:19:42 -0400794 LazyInstantiateCallback&& callback,
795 const GrBackendFormat& format,
796 const GrSurfaceDesc& desc,
797 int sampleCnt,
798 GrSurfaceOrigin origin,
799 GrInternalSurfaceFlags surfaceFlags,
800 const TextureInfo* textureInfo,
801 GrMipMapsStatus mipMapsStatus,
802 SkBackingFit fit,
803 SkBudgeted budgeted,
804 GrProtected isProtected,
805 bool wrapsVkSecondaryCB,
806 UseAllocator useAllocator) {
Robert Phillipse8fabb22018-02-04 14:33:21 -0500807 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
808 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400809
Robert Phillipsa41c6852019-02-07 10:44:10 -0500810 if (desc.fWidth > this->caps()->maxRenderTargetSize() ||
811 desc.fHeight > this->caps()->maxRenderTargetSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400812 return nullptr;
813 }
814
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400815 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
816 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(format, colorType);
817 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(format, colorType);
818
Brian Salomon7226c232018-07-30 13:13:17 -0400819 if (textureInfo) {
Greg Danielb085fa92019-03-05 16:55:12 -0500820 // Wrapped vulkan secondary command buffers don't support texturing since we won't have an
821 // actual VkImage to texture from.
822 SkASSERT(!wrapsVkSecondaryCB);
Brian Salomon7226c232018-07-30 13:13:17 -0400823 return sk_sp<GrRenderTargetProxy>(new GrTextureRenderTargetProxy(
Brian Salomonbeb7f522019-08-30 16:19:42 -0400824 *this->caps(), std::move(callback), format, desc, sampleCnt, origin,
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600825 textureInfo->fMipMapped, mipMapsStatus, texSwizzle, outSwizzle, fit, budgeted,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400826 isProtected, surfaceFlags, useAllocator));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500827 }
828
Greg Danielb085fa92019-03-05 16:55:12 -0500829 GrRenderTargetProxy::WrapsVkSecondaryCB vkSCB =
830 wrapsVkSecondaryCB ? GrRenderTargetProxy::WrapsVkSecondaryCB::kYes
831 : GrRenderTargetProxy::WrapsVkSecondaryCB::kNo;
832
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400833 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
Brian Salomonbeb7f522019-08-30 16:19:42 -0400834 std::move(callback), format, desc, sampleCnt, origin, texSwizzle, outSwizzle, fit,
835 budgeted, isProtected, surfaceFlags, useAllocator, vkSCB));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500836}
837
Brian Salomonbeb7f522019-08-30 16:19:42 -0400838sk_sp<GrTextureProxy> GrProxyProvider::MakeFullyLazyProxy(LazyInstantiateCallback&& callback,
839 const GrBackendFormat& format,
840 GrRenderable renderable,
841 int renderTargetSampleCnt,
842 GrProtected isProtected,
843 GrSurfaceOrigin origin,
844 GrPixelConfig config,
845 const GrCaps& caps,
846 UseAllocator useAllocator) {
Robert Phillips0a15cc62019-07-30 12:49:10 -0400847 if (!format.isValid()) {
848 return nullptr;
849 }
850
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400851 SkASSERT(renderTargetSampleCnt == 1 || renderable == GrRenderable::kYes);
Robert Phillips777707b2018-01-17 11:40:14 -0500852 GrSurfaceDesc desc;
Robert Phillips10d17212019-04-24 14:09:10 -0400853 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNone;
Robert Phillips777707b2018-01-17 11:40:14 -0500854 desc.fWidth = -1;
855 desc.fHeight = -1;
856 desc.fConfig = config;
Robert Phillips777707b2018-01-17 11:40:14 -0500857
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400858 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
859 GrSwizzle texSwizzle = caps.getTextureSwizzle(format, colorType);
860 GrSwizzle outSwizzle = caps.getOutputSwizzle(format, colorType);
861
Brian Salomonbeb7f522019-08-30 16:19:42 -0400862 if (GrRenderable::kYes == renderable) {
863 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
864 caps, std::move(callback), format, desc, renderTargetSampleCnt, origin,
865 GrMipMapped::kNo, GrMipMapsStatus::kNotAllocated, texSwizzle, outSwizzle,
866 SkBackingFit::kApprox, SkBudgeted::kYes, isProtected, surfaceFlags, useAllocator));
867 } else {
868 return sk_sp<GrTextureProxy>(new GrTextureProxy(
869 std::move(callback), format, desc, origin, GrMipMapped::kNo,
870 GrMipMapsStatus::kNotAllocated, texSwizzle, SkBackingFit::kApprox, SkBudgeted::kYes,
871 isProtected, surfaceFlags, useAllocator));
872 }
Robert Phillips777707b2018-01-17 11:40:14 -0500873}
874
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500875bool GrProxyProvider::IsFunctionallyExact(GrSurfaceProxy* proxy) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400876 const bool isInstantiated = proxy->isInstantiated();
Robert Phillipsdb3b9792018-06-12 15:18:00 -0400877 // A proxy is functionally exact if:
878 // it is exact (obvs)
879 // when it is instantiated it will be exact (i.e., power of two dimensions)
Robert Phillipse9462dd2019-10-23 12:41:29 -0400880 // when it is instantiated the content rect will cover the entire backing surface
Robert Phillipsdb3b9792018-06-12 15:18:00 -0400881 return proxy->priv().isExact() ||
882 (!isInstantiated && SkIsPow2(proxy->width()) && SkIsPow2(proxy->height())) ||
Robert Phillipse9462dd2019-10-23 12:41:29 -0400883 (proxy->backingStoreDimensions() == proxy->dimensions());
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500884}
885
Robert Phillips427966a2018-12-20 17:20:43 -0500886void GrProxyProvider::processInvalidUniqueKey(const GrUniqueKey& key, GrTextureProxy* proxy,
887 InvalidateGPUResource invalidateGPUResource) {
Chris Dalton2de13dd2019-01-03 15:11:59 -0700888 SkASSERT(key.isValid());
889
Robert Phillips427966a2018-12-20 17:20:43 -0500890 if (!proxy) {
891 proxy = fUniquelyKeyedProxies.find(key);
892 }
Chris Dalton2de13dd2019-01-03 15:11:59 -0700893 SkASSERT(!proxy || proxy->getUniqueKey() == key);
894
895 // Locate the corresponding GrGpuResource (if it needs to be invalidated) before clearing the
896 // proxy's unique key. We must do it in this order because 'key' may alias the proxy's key.
897 sk_sp<GrGpuResource> invalidGpuResource;
898 if (InvalidateGPUResource::kYes == invalidateGPUResource) {
Brian Salomon01ceae92019-04-02 11:49:54 -0400899 GrContext* direct = fImageContext->priv().asDirectContext();
900 if (direct) {
901 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
902 invalidGpuResource = resourceProvider->findByUniqueKey<GrGpuResource>(key);
Chris Dalton2de13dd2019-01-03 15:11:59 -0700903 }
904 SkASSERT(!invalidGpuResource || invalidGpuResource->getUniqueKey() == key);
905 }
Robert Phillips427966a2018-12-20 17:20:43 -0500906
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500907 // Note: this method is called for the whole variety of GrGpuResources so often 'key'
908 // will not be in 'fUniquelyKeyedProxies'.
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500909 if (proxy) {
Robert Phillips427966a2018-12-20 17:20:43 -0500910 fUniquelyKeyedProxies.remove(key);
911 proxy->cacheAccess().clearUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500912 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500913
Chris Dalton2de13dd2019-01-03 15:11:59 -0700914 if (invalidGpuResource) {
915 invalidGpuResource->resourcePriv().removeUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500916 }
917}
918
Robert Phillipsa41c6852019-02-07 10:44:10 -0500919uint32_t GrProxyProvider::contextID() const {
920 return fImageContext->priv().contextID();
921}
922
923const GrCaps* GrProxyProvider::caps() const {
924 return fImageContext->priv().caps();
925}
926
927sk_sp<const GrCaps> GrProxyProvider::refCaps() const {
928 return fImageContext->priv().refCaps();
929}
930
Robert Phillipsa9162df2019-02-11 14:12:03 -0500931bool GrProxyProvider::isAbandoned() const {
932 return fImageContext->priv().abandoned();
933}
934
Robert Phillips0790f8a2018-09-18 13:11:03 -0400935void GrProxyProvider::orphanAllUniqueKeys() {
936 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
937 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
938 GrTextureProxy& tmp = *iter;
939
940 tmp.fProxyProvider = nullptr;
941 }
942}
943
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500944void GrProxyProvider::removeAllUniqueKeys() {
945 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
946 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
947 GrTextureProxy& tmp = *iter;
948
Chris Dalton2de13dd2019-01-03 15:11:59 -0700949 this->processInvalidUniqueKey(tmp.getUniqueKey(), &tmp, InvalidateGPUResource::kNo);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500950 }
951 SkASSERT(!fUniquelyKeyedProxies.count());
952}
Robert Phillipsa41c6852019-02-07 10:44:10 -0500953
954bool GrProxyProvider::renderingDirectly() const {
955 return fImageContext->priv().asDirectContext();
956}