blob: f64b345ac6cbe766a7747a15a5ec4c7d78d910bc [file] [log] [blame]
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001/*
2 * Copyright 2018 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrProxyProvider.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkBitmap.h"
11#include "include/core/SkImage.h"
12#include "include/gpu/GrContext.h"
13#include "include/gpu/GrRenderTarget.h"
14#include "include/gpu/GrTexture.h"
15#include "include/private/GrImageContext.h"
16#include "include/private/GrResourceKey.h"
17#include "include/private/GrSingleOwner.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "include/private/SkImageInfoPriv.h"
19#include "src/core/SkAutoPixmapStorage.h"
20#include "src/core/SkImagePriv.h"
21#include "src/core/SkMipMap.h"
22#include "src/core/SkTraceEvent.h"
23#include "src/gpu/GrCaps.h"
24#include "src/gpu/GrContextPriv.h"
25#include "src/gpu/GrImageContextPriv.h"
26#include "src/gpu/GrResourceProvider.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040027#include "src/gpu/GrSurfaceProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/gpu/GrSurfaceProxyPriv.h"
29#include "src/gpu/GrTextureProxyCacheAccess.h"
30#include "src/gpu/GrTextureRenderTargetProxy.h"
31#include "src/gpu/SkGr.h"
32#include "src/image/SkImage_Base.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050033
34#define ASSERT_SINGLE_OWNER \
Robert Phillipsa41c6852019-02-07 10:44:10 -050035 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fImageContext->priv().singleOwner());)
Robert Phillips1afd4cd2018-01-08 13:40:32 -050036
Robert Phillipsa9162df2019-02-11 14:12:03 -050037GrProxyProvider::GrProxyProvider(GrImageContext* imageContext) : fImageContext(imageContext) {}
Robert Phillips1afd4cd2018-01-08 13:40:32 -050038
39GrProxyProvider::~GrProxyProvider() {
Robert Phillipsa41c6852019-02-07 10:44:10 -050040 if (this->renderingDirectly()) {
Robert Phillips0790f8a2018-09-18 13:11:03 -040041 // In DDL-mode a proxy provider can still have extant uniquely keyed proxies (since
42 // they need their unique keys to, potentially, find a cached resource when the
43 // DDL is played) but, in non-DDL-mode they should all have been cleaned up by this point.
44 SkASSERT(!fUniquelyKeyedProxies.count());
45 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050046}
47
Robert Phillipsadbe1322018-01-17 13:35:46 -050048bool GrProxyProvider::assignUniqueKeyToProxy(const GrUniqueKey& key, GrTextureProxy* proxy) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050049 ASSERT_SINGLE_OWNER
50 SkASSERT(key.isValid());
51 if (this->isAbandoned() || !proxy) {
Robert Phillipsadbe1322018-01-17 13:35:46 -050052 return false;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050053 }
54
Robert Phillipsa41c6852019-02-07 10:44:10 -050055#ifdef SK_DEBUG
56 {
57 GrContext* direct = fImageContext->priv().asDirectContext();
58 if (direct) {
59 GrResourceCache* resourceCache = direct->priv().getResourceCache();
60 // If there is already a GrResource with this key then the caller has violated the
61 // normal usage pattern of uniquely keyed resources (e.g., they have created one w/o
62 // first seeing if it already existed in the cache).
63 SkASSERT(!resourceCache->findAndRefUniqueResource(key));
64 }
65 }
66#endif
Robert Phillips1afd4cd2018-01-08 13:40:32 -050067
Robert Phillips1afd4cd2018-01-08 13:40:32 -050068 SkASSERT(!fUniquelyKeyedProxies.find(key)); // multiple proxies can't get the same key
69
70 proxy->cacheAccess().setUniqueKey(this, key);
71 SkASSERT(proxy->getUniqueKey() == key);
72 fUniquelyKeyedProxies.add(proxy);
Robert Phillipsadbe1322018-01-17 13:35:46 -050073 return true;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050074}
75
76void GrProxyProvider::adoptUniqueKeyFromSurface(GrTextureProxy* proxy, const GrSurface* surf) {
77 SkASSERT(surf->getUniqueKey().isValid());
78 proxy->cacheAccess().setUniqueKey(this, surf->getUniqueKey());
79 SkASSERT(proxy->getUniqueKey() == surf->getUniqueKey());
80 // multiple proxies can't get the same key
81 SkASSERT(!fUniquelyKeyedProxies.find(surf->getUniqueKey()));
82 fUniquelyKeyedProxies.add(proxy);
83}
84
Chris Dalton2de13dd2019-01-03 15:11:59 -070085void GrProxyProvider::removeUniqueKeyFromProxy(GrTextureProxy* proxy) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050086 ASSERT_SINGLE_OWNER
Chris Dalton2de13dd2019-01-03 15:11:59 -070087 SkASSERT(proxy);
88 SkASSERT(proxy->getUniqueKey().isValid());
89
90 if (this->isAbandoned()) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050091 return;
92 }
Robert Phillips0790f8a2018-09-18 13:11:03 -040093
Chris Dalton2de13dd2019-01-03 15:11:59 -070094 this->processInvalidUniqueKey(proxy->getUniqueKey(), proxy, InvalidateGPUResource::kYes);
Robert Phillips1afd4cd2018-01-08 13:40:32 -050095}
96
97sk_sp<GrTextureProxy> GrProxyProvider::findProxyByUniqueKey(const GrUniqueKey& key,
98 GrSurfaceOrigin origin) {
99 ASSERT_SINGLE_OWNER
100
101 if (this->isAbandoned()) {
102 return nullptr;
103 }
104
Brian Salomon01ceae92019-04-02 11:49:54 -0400105 GrTextureProxy* proxy = fUniquelyKeyedProxies.find(key);
Brian Salomon01ceae92019-04-02 11:49:54 -0400106 if (proxy) {
Robert Phillipse5f73282019-06-18 17:15:04 -0400107 SkASSERT(proxy->getProxyRefCnt() >= 1);
108 SkASSERT(proxy->origin() == origin);
109 return sk_ref_sp(proxy);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500110 }
Robert Phillipse5f73282019-06-18 17:15:04 -0400111 return nullptr;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500112}
113
Robert Phillipsa41c6852019-02-07 10:44:10 -0500114///////////////////////////////////////////////////////////////////////////////
115
116#if GR_TEST_UTILS
117sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createInstantiatedProxy(
Brian 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
161 return this->createWrapped(std::move(tex), origin);
162}
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,
186 GrSurfaceOrigin origin) {
187 return this->createWrapped(std::move(tex), origin);
188}
189#endif
190
Robert Phillipsadbe1322018-01-17 13:35:46 -0500191sk_sp<GrTextureProxy> GrProxyProvider::createWrapped(sk_sp<GrTexture> tex, GrSurfaceOrigin origin) {
192#ifdef SK_DEBUG
193 if (tex->getUniqueKey().isValid()) {
194 SkASSERT(!this->findProxyByUniqueKey(tex->getUniqueKey(), origin));
195 }
196#endif
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400197 GrColorType colorType = GrPixelConfigToColorType(tex->config());
198 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);
202 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin,
203 texSwizzle, outSwizzle));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500204 } else {
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400205 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin, texSwizzle));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500206 }
207}
208
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500209sk_sp<GrTextureProxy> GrProxyProvider::findOrCreateProxyByUniqueKey(const GrUniqueKey& key,
210 GrSurfaceOrigin origin) {
211 ASSERT_SINGLE_OWNER
212
213 if (this->isAbandoned()) {
214 return nullptr;
215 }
216
217 sk_sp<GrTextureProxy> result = this->findProxyByUniqueKey(key, origin);
218 if (result) {
219 return result;
220 }
221
Robert Phillipsa41c6852019-02-07 10:44:10 -0500222 GrContext* direct = fImageContext->priv().asDirectContext();
223 if (!direct) {
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500224 return nullptr;
225 }
226
Robert Phillipsa41c6852019-02-07 10:44:10 -0500227 GrResourceCache* resourceCache = direct->priv().getResourceCache();
228
229 GrGpuResource* resource = resourceCache->findAndRefUniqueResource(key);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500230 if (!resource) {
231 return nullptr;
232 }
233
234 sk_sp<GrTexture> texture(static_cast<GrSurface*>(resource)->asTexture());
235 SkASSERT(texture);
236
Robert Phillipsadbe1322018-01-17 13:35:46 -0500237 result = this->createWrapped(std::move(texture), origin);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500238 SkASSERT(result->getUniqueKey() == key);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500239 // createWrapped should've added this for us
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500240 SkASSERT(fUniquelyKeyedProxies.find(key));
241 return result;
242}
243
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500244sk_sp<GrTextureProxy> GrProxyProvider::createTextureProxy(sk_sp<SkImage> srcImage,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400245 GrRenderable renderable,
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500246 int sampleCnt,
Greg Danielfb3abcd2018-02-02 15:48:33 -0500247 SkBudgeted budgeted,
Chris Daltond004e0b2018-09-27 09:28:03 -0600248 SkBackingFit fit,
249 GrInternalSurfaceFlags surfaceFlags) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500250 ASSERT_SINGLE_OWNER
251 SkASSERT(srcImage);
252
253 if (this->isAbandoned()) {
254 return nullptr;
255 }
256
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400257 const SkImageInfo& info = srcImage->imageInfo();
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400258 SkColorType ct = info.colorType();
Greg Daniel2f2caea2019-07-08 14:24:47 -0400259 GrColorType grCT = SkColorTypeToGrColorType(ct);
Greg Danielf87651e2018-02-21 11:36:53 -0500260
Robert Phillips0a15cc62019-07-30 12:49:10 -0400261 GrBackendFormat format = this->caps()->getDefaultBackendFormat(grCT, renderable);
Greg Daniel0a7aa142018-02-21 13:02:32 -0500262
Robert Phillips0a15cc62019-07-30 12:49:10 -0400263 if (!format.isValid()) {
Brian Osmand29dcd12018-09-13 15:04:29 -0400264 SkBitmap copy8888;
265 if (!copy8888.tryAllocPixels(info.makeColorType(kRGBA_8888_SkColorType)) ||
266 !srcImage->readPixels(copy8888.pixmap(), 0, 0)) {
267 return nullptr;
268 }
269 copy8888.setImmutable();
270 srcImage = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode);
Jim Van Verthc8098322019-04-16 10:50:01 -0400271 ct = kRGBA_8888_SkColorType;
Greg Daniel627d0532019-07-08 16:48:14 -0400272 grCT = GrColorType::kRGBA_8888;
Robert Phillips0a15cc62019-07-30 12:49:10 -0400273 format = this->caps()->getDefaultBackendFormat(grCT, renderable);
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400274 if (!format.isValid()) {
275 return nullptr;
276 }
Brian Osmand29dcd12018-09-13 15:04:29 -0400277 }
278
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400279 if (renderable == GrRenderable::kYes) {
Greg Daniel5c96db82019-07-09 14:06:58 -0400280 sampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, grCT, format);
Greg Danielf87651e2018-02-21 11:36:53 -0500281 if (!sampleCnt) {
282 return nullptr;
283 }
284 }
285
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400286 GrPixelConfig config = SkColorType2GrPixelConfig(ct);
287 if (kUnknown_GrPixelConfig == config) {
288 return nullptr;
289 }
290
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500291 GrSurfaceDesc desc;
292 desc.fWidth = srcImage->width();
293 desc.fHeight = srcImage->height();
Greg Danielf87651e2018-02-21 11:36:53 -0500294 desc.fConfig = config;
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500295
296 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomon4eb38b72019-08-05 12:58:39 -0400297 [desc, format, renderable, sampleCnt, budgeted, srcImage,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400298 fit](GrResourceProvider* resourceProvider) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500299 SkPixmap pixMap;
300 SkAssertResult(srcImage->peekPixels(&pixMap));
301 GrMipLevel mipLevel = { pixMap.addr(), pixMap.rowBytes() };
302
Brian Salomone8a766b2019-07-19 14:24:36 -0400303 return LazyInstantiationResult(resourceProvider->createTexture(
Brian Salomon4eb38b72019-08-05 12:58:39 -0400304 desc, format, renderable, sampleCnt, budgeted, fit, GrProtected::kNo,
305 mipLevel, GrResourceProvider::Flags::kNoPendingIO));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500306 },
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400307 format, desc, renderable, sampleCnt, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo,
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600308 GrMipMapsStatus::kNotAllocated, surfaceFlags, fit, budgeted, GrProtected::kNo);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500309
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400310 if (!proxy) {
311 return nullptr;
312 }
313
Robert Phillipsa41c6852019-02-07 10:44:10 -0500314 GrContext* direct = fImageContext->priv().asDirectContext();
315 if (direct) {
316 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
317
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500318 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
319 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500320 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500321 return nullptr;
322 }
323 }
Robert Phillipsc1b60662018-06-26 10:20:08 -0400324
325 SkASSERT(proxy->width() == desc.fWidth);
326 SkASSERT(proxy->height() == desc.fHeight);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500327 return proxy;
328}
329
Greg Daniel4065d452018-11-16 15:43:41 -0500330sk_sp<GrTextureProxy> GrProxyProvider::createMipMapProxy(const GrBackendFormat& format,
331 const GrSurfaceDesc& desc,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400332 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400333 int renderTargetSampleCnt,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500334 GrSurfaceOrigin origin,
Brian Salomone8a766b2019-07-19 14:24:36 -0400335 SkBudgeted budgeted,
336 GrProtected isProtected) {
Robert Phillips579f0942018-01-08 14:53:35 -0500337 ASSERT_SINGLE_OWNER
338
339 if (this->isAbandoned()) {
340 return nullptr;
341 }
342
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400343 return this->createProxy(format, desc, renderable, renderTargetSampleCnt, origin,
344 GrMipMapped::kYes, SkBackingFit::kExact, budgeted, isProtected,
Brian Salomone8a766b2019-07-19 14:24:36 -0400345 GrInternalSurfaceFlags::kNone);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500346}
347
Brian Osmande496652019-03-22 13:42:33 -0400348sk_sp<GrTextureProxy> GrProxyProvider::createProxyFromBitmap(const SkBitmap& bitmap,
349 GrMipMapped mipMapped) {
Brian Osman412674f2019-02-07 15:34:58 -0500350 ASSERT_SINGLE_OWNER
Brian Osman7dcc6162019-03-25 10:12:57 -0400351 SkASSERT(GrMipMapped::kNo == mipMapped || this->caps()->mipMapSupport());
Brian Osman412674f2019-02-07 15:34:58 -0500352
353 if (this->isAbandoned()) {
354 return nullptr;
355 }
356
Brian Osman2b23c4b2018-06-01 12:25:08 -0400357 if (!SkImageInfoIsValid(bitmap.info())) {
Greg Daniela4ead652018-02-07 10:21:48 -0500358 return nullptr;
359 }
360
Brian Osmande496652019-03-22 13:42:33 -0400361 ATRACE_ANDROID_FRAMEWORK("Upload %sTexture [%ux%u]",
362 GrMipMapped::kYes == mipMapped ? "MipMap " : "",
363 bitmap.width(), bitmap.height());
Greg Daniela4ead652018-02-07 10:21:48 -0500364
365 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
366 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
367 // 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 -0500368 SkCopyPixelsMode copyMode = this->renderingDirectly() ? kNever_SkCopyPixelsMode
369 : kIfMutable_SkCopyPixelsMode;
Greg Daniela4ead652018-02-07 10:21:48 -0500370 sk_sp<SkImage> baseLevel = SkMakeImageFromRasterBitmap(bitmap, copyMode);
Greg Daniela4ead652018-02-07 10:21:48 -0500371 if (!baseLevel) {
372 return nullptr;
373 }
374
Brian Osmande496652019-03-22 13:42:33 -0400375 // If mips weren't requested (or this was too small to have any), then take the fast path
376 if (GrMipMapped::kNo == mipMapped ||
377 0 == SkMipMap::ComputeLevelCount(baseLevel->width(), baseLevel->height())) {
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400378 return this->createTextureProxy(std::move(baseLevel), GrRenderable::kNo, 1,
Brian Osman7dcc6162019-03-25 10:12:57 -0400379 SkBudgeted::kYes, SkBackingFit::kExact);
Greg Daniela4ead652018-02-07 10:21:48 -0500380 }
381
Brian Osmand29dcd12018-09-13 15:04:29 -0400382 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info());
Greg Daniel3fc5df42019-06-14 09:42:17 -0400383
Robert Phillips0a15cc62019-07-30 12:49:10 -0400384 GrColorType grColorType = SkColorTypeToGrColorType(bitmap.info().colorType());
385 GrBackendFormat format = this->caps()->getDefaultBackendFormat(grColorType, GrRenderable::kNo);
386 if (!format.isValid()) {
Brian Osmand29dcd12018-09-13 15:04:29 -0400387 SkBitmap copy8888;
388 if (!copy8888.tryAllocPixels(bitmap.info().makeColorType(kRGBA_8888_SkColorType)) ||
389 !bitmap.readPixels(copy8888.pixmap())) {
390 return nullptr;
391 }
392 copy8888.setImmutable();
393 baseLevel = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode);
394 desc.fConfig = kRGBA_8888_GrPixelConfig;
Greg Daniel627d0532019-07-08 16:48:14 -0400395 grColorType = GrColorType::kRGBA_8888;
Robert Phillips0a15cc62019-07-30 12:49:10 -0400396 format = this->caps()->getDefaultBackendFormat(grColorType, GrRenderable::kNo);
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400397 if (!format.isValid()) {
398 return nullptr;
399 }
Greg Daniel3fc5df42019-06-14 09:42:17 -0400400 }
401
Brian Osmand29dcd12018-09-13 15:04:29 -0400402 SkPixmap pixmap;
403 SkAssertResult(baseLevel->peekPixels(&pixmap));
404 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(pixmap, nullptr));
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400405 if (!mipmaps) {
406 return nullptr;
407 }
408
Greg Daniela4ead652018-02-07 10:21:48 -0500409 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomon4eb38b72019-08-05 12:58:39 -0400410 [desc, format, baseLevel, mipmaps](GrResourceProvider* resourceProvider) {
Brian Osman1b97f132018-09-13 17:33:48 +0000411 const int mipLevelCount = mipmaps->countLevels() + 1;
412 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
413
Greg Daniela4ead652018-02-07 10:21:48 -0500414 SkPixmap pixmap;
415 SkAssertResult(baseLevel->peekPixels(&pixmap));
416
417 // DDL TODO: Instead of copying all this info into GrMipLevels we should just plumb
418 // the use of SkMipMap down through Ganesh.
419 texels[0].fPixels = pixmap.addr();
420 texels[0].fRowBytes = pixmap.rowBytes();
421
422 for (int i = 1; i < mipLevelCount; ++i) {
423 SkMipMap::Level generatedMipLevel;
424 mipmaps->getLevel(i - 1, &generatedMipLevel);
425 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
426 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
427 SkASSERT(texels[i].fPixels);
428 }
429
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400430 return LazyInstantiationResult(resourceProvider->createTexture(
Brian Salomon4eb38b72019-08-05 12:58:39 -0400431 desc, format, GrRenderable::kNo, 1, SkBudgeted::kYes, GrProtected::kNo,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400432 texels.get(), mipLevelCount));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500433 },
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400434 format, desc, GrRenderable::kNo, 1, kTopLeft_GrSurfaceOrigin, GrMipMapped::kYes,
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600435 GrMipMapsStatus::kValid, SkBackingFit::kExact, SkBudgeted::kYes, GrProtected::kNo);
Greg Daniela4ead652018-02-07 10:21:48 -0500436
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400437 if (!proxy) {
438 return nullptr;
439 }
440
Robert Phillipsa41c6852019-02-07 10:44:10 -0500441 GrContext* direct = fImageContext->priv().asDirectContext();
442 if (direct) {
443 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Daniela4ead652018-02-07 10:21:48 -0500444 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
445 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500446 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Greg Daniela4ead652018-02-07 10:21:48 -0500447 return nullptr;
448 }
449 }
450 return proxy;
451}
452
Greg Danielfa55f2e2019-06-13 15:21:38 -0400453#ifdef SK_DEBUG
454static bool validate_backend_format_and_config(const GrCaps* caps,
455 const GrBackendFormat& format,
456 GrPixelConfig config) {
457 if (kUnknown_GrPixelConfig == config) {
458 return false;
459 }
Brian Salomonbb8dde82019-06-27 10:52:13 -0400460 if (GrPixelConfigIsCompressed(config)) {
461 // We have no way to verify these at the moment.
462 return true;
463 }
Robert Phillipsc046ff02019-07-01 10:34:03 -0400464
Robert Phillips1e2cb442019-07-02 15:51:28 -0400465 GrColorType grCT = GrPixelConfigToColorType(config);
466
467 return caps->areColorTypeAndFormatCompatible(grCT, format);
Greg Danielfa55f2e2019-06-13 15:21:38 -0400468}
469#endif
470
Greg Daniel4065d452018-11-16 15:43:41 -0500471sk_sp<GrTextureProxy> GrProxyProvider::createProxy(const GrBackendFormat& format,
472 const GrSurfaceDesc& desc,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400473 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400474 int renderTargetSampleCnt,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500475 GrSurfaceOrigin origin,
Greg Danielf6f7b672018-02-15 13:06:26 -0500476 GrMipMapped mipMapped,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500477 SkBackingFit fit,
478 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -0400479 GrProtected isProtected,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400480 GrInternalSurfaceFlags surfaceFlags) {
Robert Phillips8ff8bcc2019-07-29 17:03:35 -0400481 const GrCaps* caps = this->caps();
482
483 if (caps->isFormatCompressed(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400484 // Deferred proxies for compressed textures are not supported.
485 return nullptr;
486 }
Robert Phillips0902c982019-07-16 07:47:56 -0400487
Robert Phillips0902c982019-07-16 07:47:56 -0400488 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
489
490 SkASSERT(GrCaps::AreConfigsCompatible(desc.fConfig,
491 caps->getConfigFromBackendFormat(format, colorType)));
Robert Phillips62221e72019-07-24 15:07:38 -0400492 SkASSERT(caps->areColorTypeAndFormatCompatible(colorType, format));
493
Greg Danielf6f7b672018-02-15 13:06:26 -0500494 if (GrMipMapped::kYes == mipMapped) {
495 // SkMipMap doesn't include the base level in the level count so we have to add 1
496 int mipCount = SkMipMap::ComputeLevelCount(desc.fWidth, desc.fHeight) + 1;
497 if (1 == mipCount) {
498 mipMapped = GrMipMapped::kNo;
499 }
500 }
501
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400502 if (!caps->validateSurfaceDesc(desc, renderable, renderTargetSampleCnt, mipMapped)) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500503 return nullptr;
504 }
Brian Salomon3a2cc2c2018-02-03 00:25:12 +0000505 GrSurfaceDesc copyDesc = desc;
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600506 GrMipMapsStatus mipMapsStatus = (GrMipMapped::kYes == mipMapped)
507 ? GrMipMapsStatus::kDirty
508 : GrMipMapsStatus::kNotAllocated;
Robert Phillips0902c982019-07-16 07:47:56 -0400509 GrSwizzle texSwizzle = caps->getTextureSwizzle(format, colorType);
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400510 if (renderable == GrRenderable::kYes) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400511 renderTargetSampleCnt =
512 caps->getRenderTargetSampleCount(renderTargetSampleCnt, colorType, format);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500513 // We know anything we instantiate later from this deferred path will be
514 // both texturable and renderable
Robert Phillips0902c982019-07-16 07:47:56 -0400515 GrSwizzle outSwizzle = caps->getOutputSwizzle(format, colorType);
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400516 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600517 *caps, format, copyDesc, renderTargetSampleCnt, origin, mipMapped, mipMapsStatus,
518 texSwizzle, outSwizzle, fit, budgeted, isProtected, surfaceFlags));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500519 }
520
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600521 return sk_sp<GrTextureProxy>(new GrTextureProxy(
522 format, copyDesc, origin, mipMapped, mipMapsStatus, texSwizzle, fit, budgeted,
523 isProtected, surfaceFlags));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500524}
525
Brian Salomonbb8dde82019-06-27 10:52:13 -0400526sk_sp<GrTextureProxy> GrProxyProvider::createCompressedTextureProxy(
527 int width, int height, SkBudgeted budgeted, SkImage::CompressionType compressionType,
528 sk_sp<SkData> data) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400529
530 GrSurfaceDesc desc;
531 desc.fConfig = GrCompressionTypePixelConfig(compressionType);
532 desc.fWidth = width;
533 desc.fHeight = height;
534
Robert Phillips8ff8bcc2019-07-29 17:03:35 -0400535 GrColorType grColorType = GrCompressionTypeClosestColorType(compressionType);
536 GrBackendFormat format = this->caps()->getBackendFormatFromCompressionType(compressionType);
537
538 if (!this->caps()->isFormatTexturable(grColorType, format)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500539 return nullptr;
540 }
541
Jim Van Verthee06b332019-01-18 10:36:32 -0500542 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomonbb8dde82019-06-27 10:52:13 -0400543 [width, height, compressionType, budgeted, data](GrResourceProvider* resourceProvider) {
544 return LazyInstantiationResult(resourceProvider->createCompressedTexture(
545 width, height, compressionType, budgeted, data.get()));
546 },
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400547 format, desc, GrRenderable::kNo, 1, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo,
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600548 GrMipMapsStatus::kNotAllocated, SkBackingFit::kExact, SkBudgeted::kYes,
549 GrProtected::kNo);
Jim Van Verthee06b332019-01-18 10:36:32 -0500550
551 if (!proxy) {
552 return nullptr;
553 }
554
Robert Phillipsa41c6852019-02-07 10:44:10 -0500555 GrContext* direct = fImageContext->priv().asDirectContext();
556 if (direct) {
557 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Jim Van Verthee06b332019-01-18 10:36:32 -0500558 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
559 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500560 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500561 return nullptr;
562 }
563 }
564 return proxy;
565}
566
Brian Salomon7578f3e2018-03-07 14:39:54 -0500567sk_sp<GrTextureProxy> GrProxyProvider::wrapBackendTexture(const GrBackendTexture& backendTex,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400568 GrColorType grColorType,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500569 GrSurfaceOrigin origin,
570 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500571 GrWrapCacheable cacheable,
Brian Salomonc67c31c2018-12-06 10:00:03 -0500572 GrIOType ioType,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500573 ReleaseProc releaseProc,
574 ReleaseContext releaseCtx) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500575 SkASSERT(ioType != kWrite_GrIOType);
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 Phillipsc80b0e92019-07-23 10:27:09 -0400586 const GrCaps* caps = this->caps();
587
Robert Phillips62221e72019-07-24 15:07:38 -0400588 SkASSERT(caps->areColorTypeAndFormatCompatible(grColorType, backendTex.getBackendFormat()));
Greg Danielfa55f2e2019-06-13 15:21:38 -0400589
Robert Phillipsa41c6852019-02-07 10:44:10 -0500590 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
591
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500592 sk_sp<GrTexture> tex =
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400593 resourceProvider->wrapBackendTexture(backendTex, grColorType,
594 ownership, cacheable, ioType);
Brian Salomonf7778972018-03-08 10:13:17 -0500595 if (!tex) {
596 return nullptr;
597 }
Robert Phillipsadbe1322018-01-17 13:35:46 -0500598
Greg Daniel6a0176b2018-01-30 09:28:44 -0500599 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500600 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel6a0176b2018-01-30 09:28:44 -0500601 }
602
Brian Salomonf7778972018-03-08 10:13:17 -0500603 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
604 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500605 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500606
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400607 GrSwizzle texSwizzle = caps->getTextureSwizzle(tex->backendFormat(), grColorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400608
609 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin, texSwizzle));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500610}
611
Brian Salomon7578f3e2018-03-07 14:39:54 -0500612sk_sp<GrTextureProxy> GrProxyProvider::wrapRenderableBackendTexture(
Brian Salomon02bd2952018-03-07 15:20:21 -0500613 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400614 GrColorType colorType, GrWrapOwnership ownership, GrWrapCacheable cacheable,
615 ReleaseProc releaseProc, ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500616 if (this->isAbandoned()) {
617 return nullptr;
618 }
619
Brian Salomonf7778972018-03-08 10:13:17 -0500620 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500621 GrContext* direct = fImageContext->priv().asDirectContext();
622 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500623 return nullptr;
624 }
625
Robert Phillips0902c982019-07-16 07:47:56 -0400626 const GrCaps* caps = this->caps();
627
Robert Phillips62221e72019-07-24 15:07:38 -0400628 SkASSERT(caps->areColorTypeAndFormatCompatible(colorType, backendTex.getBackendFormat()));
Greg Danielfa55f2e2019-06-13 15:21:38 -0400629
Robert Phillipsa41c6852019-02-07 10:44:10 -0500630 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
631
Robert Phillips0902c982019-07-16 07:47:56 -0400632 sampleCnt = caps->getRenderTargetSampleCount(sampleCnt, colorType,
633 backendTex.getBackendFormat());
Greg Danielf87651e2018-02-21 11:36:53 -0500634 if (!sampleCnt) {
635 return nullptr;
636 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500637
Robert Phillipsa41c6852019-02-07 10:44:10 -0500638 sk_sp<GrTexture> tex = resourceProvider->wrapRenderableBackendTexture(backendTex, sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400639 colorType, ownership,
640 cacheable);
Brian Salomonf7778972018-03-08 10:13:17 -0500641 if (!tex) {
642 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500643 }
644
Greg Daniel8ce79912019-02-05 10:08:43 -0500645 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500646 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500647 }
648
Brian Salomonf7778972018-03-08 10:13:17 -0500649 SkASSERT(tex->asRenderTarget()); // A GrTextureRenderTarget
650 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500651 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Greg Daniel6abda432018-02-15 14:55:00 -0500652
Robert Phillips0902c982019-07-16 07:47:56 -0400653 GrSwizzle texSwizzle = caps->getTextureSwizzle(tex->backendFormat(), colorType);
654 GrSwizzle outSwizzle = caps->getOutputSwizzle(tex->backendFormat(), colorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400655
656 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin, texSwizzle,
657 outSwizzle));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500658}
659
Brian Salomon7578f3e2018-03-07 14:39:54 -0500660sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400661 const GrBackendRenderTarget& backendRT, GrColorType grColorType,
662 GrSurfaceOrigin origin, ReleaseProc releaseProc, ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500663 if (this->isAbandoned()) {
664 return nullptr;
665 }
666
Brian Salomonf7778972018-03-08 10:13:17 -0500667 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500668 GrContext* direct = fImageContext->priv().asDirectContext();
669 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500670 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500671 }
672
Robert Phillips62221e72019-07-24 15:07:38 -0400673 const GrCaps* caps = this->caps();
674
675 SkASSERT(caps->areColorTypeAndFormatCompatible(grColorType, backendRT.getBackendFormat()));
Greg Danielfa55f2e2019-06-13 15:21:38 -0400676
Robert Phillipsa41c6852019-02-07 10:44:10 -0500677 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
678
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400679 sk_sp<GrRenderTarget> rt = resourceProvider->wrapBackendRenderTarget(backendRT, grColorType);
Brian Salomonf7778972018-03-08 10:13:17 -0500680 if (!rt) {
681 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500682 }
Greg Daniel8ce79912019-02-05 10:08:43 -0500683
Greg Daniel8ce79912019-02-05 10:08:43 -0500684 if (releaseProc) {
Brian Salomon2ca31f82019-03-05 13:28:58 -0500685 rt->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500686 }
687
Brian Salomonf7778972018-03-08 10:13:17 -0500688 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
689 SkASSERT(!rt->getUniqueKey().isValid());
690 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500691 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Brian Salomonf7778972018-03-08 10:13:17 -0500692
Robert Phillips62221e72019-07-24 15:07:38 -0400693 GrSwizzle texSwizzle = caps->getTextureSwizzle(rt->backendFormat(), grColorType);
694 GrSwizzle outSwizzle = caps->getOutputSwizzle(rt->backendFormat(), grColorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400695
696 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(std::move(rt), origin, texSwizzle,
697 outSwizzle));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500698}
699
Brian Salomon7578f3e2018-03-07 14:39:54 -0500700sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendTextureAsRenderTarget(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400701 const GrBackendTexture& backendTex, GrColorType grColorType,
702 GrSurfaceOrigin origin, int sampleCnt) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500703 if (this->isAbandoned()) {
704 return nullptr;
705 }
706
Brian Salomonf7778972018-03-08 10:13:17 -0500707 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500708 GrContext* direct = fImageContext->priv().asDirectContext();
709 if (!direct) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500710 return nullptr;
711 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500712
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400713 const GrCaps* caps = this->caps();
714
Robert Phillips62221e72019-07-24 15:07:38 -0400715 SkASSERT(caps->areColorTypeAndFormatCompatible(grColorType, backendTex.getBackendFormat()));
Greg Danielfa55f2e2019-06-13 15:21:38 -0400716
Robert Phillipsa41c6852019-02-07 10:44:10 -0500717 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
718
Brian Salomonf7778972018-03-08 10:13:17 -0500719 sk_sp<GrRenderTarget> rt =
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400720 resourceProvider->wrapBackendTextureAsRenderTarget(backendTex, sampleCnt, grColorType);
Brian Salomonf7778972018-03-08 10:13:17 -0500721 if (!rt) {
722 return nullptr;
Greg Danielf87651e2018-02-21 11:36:53 -0500723 }
Brian Salomonf7778972018-03-08 10:13:17 -0500724 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
725 SkASSERT(!rt->getUniqueKey().isValid());
Greg Danielb46add82019-01-02 14:51:29 -0500726 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500727 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielf87651e2018-02-21 11:36:53 -0500728
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400729 GrSwizzle texSwizzle = caps->getTextureSwizzle(rt->backendFormat(), grColorType);
730 GrSwizzle outSwizzle = caps->getOutputSwizzle(rt->backendFormat(), grColorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400731
732 return sk_sp<GrSurfaceProxy>(new GrRenderTargetProxy(std::move(rt), origin, texSwizzle,
733 outSwizzle));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500734}
735
Greg Danielb46add82019-01-02 14:51:29 -0500736sk_sp<GrRenderTargetProxy> GrProxyProvider::wrapVulkanSecondaryCBAsRenderTarget(
737 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
738 if (this->isAbandoned()) {
739 return nullptr;
740 }
741
742 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500743 GrContext* direct = fImageContext->priv().asDirectContext();
744 if (!direct) {
Greg Danielb46add82019-01-02 14:51:29 -0500745 return nullptr;
746 }
747
Robert Phillipsa41c6852019-02-07 10:44:10 -0500748 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Danielb46add82019-01-02 14:51:29 -0500749
Robert Phillipsa41c6852019-02-07 10:44:10 -0500750 sk_sp<GrRenderTarget> rt = resourceProvider->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
751 vkInfo);
Greg Danielb46add82019-01-02 14:51:29 -0500752 if (!rt) {
753 return nullptr;
754 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500755
Greg Danielb46add82019-01-02 14:51:29 -0500756 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
757 SkASSERT(!rt->getUniqueKey().isValid());
758 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500759 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielb46add82019-01-02 14:51:29 -0500760
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400761 GrColorType colorType = GrPixelConfigToColorType(rt->config());
762 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(rt->backendFormat(), colorType);
763 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(rt->backendFormat(), colorType);
764
Greg Danielb46add82019-01-02 14:51:29 -0500765 // All Vulkan surfaces uses top left origins.
766 return sk_sp<GrRenderTargetProxy>(
767 new GrRenderTargetProxy(std::move(rt),
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400768 kTopLeft_GrSurfaceOrigin, texSwizzle, outSwizzle,
Greg Danielb46add82019-01-02 14:51:29 -0500769 GrRenderTargetProxy::WrapsVkSecondaryCB::kYes));
770}
771
Robert Phillips777707b2018-01-17 11:40:14 -0500772sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500773 const GrBackendFormat& format,
Robert Phillips777707b2018-01-17 11:40:14 -0500774 const GrSurfaceDesc& desc,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400775 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400776 int renderTargetSampleCnt,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500777 GrSurfaceOrigin origin,
Brian Salomon7226c232018-07-30 13:13:17 -0400778 GrMipMapped mipMapped,
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600779 GrMipMapsStatus mipMapsStatus,
Brian Salomon7226c232018-07-30 13:13:17 -0400780 SkBackingFit fit,
Brian Salomone8a766b2019-07-19 14:24:36 -0400781 SkBudgeted budgeted,
782 GrProtected isProtected) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400783 return this->createLazyProxy(std::move(callback), format, desc, renderable,
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600784 renderTargetSampleCnt, origin, mipMapped, mipMapsStatus,
Brian Salomone8a766b2019-07-19 14:24:36 -0400785 GrInternalSurfaceFlags::kNone, fit, budgeted, isProtected);
Greg Daniela8d92112018-03-09 12:05:04 -0500786}
787
788sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500789 const GrBackendFormat& format,
Greg Daniela8d92112018-03-09 12:05:04 -0500790 const GrSurfaceDesc& desc,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400791 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400792 int renderTargetSampleCnt,
Greg Daniela8d92112018-03-09 12:05:04 -0500793 GrSurfaceOrigin origin,
794 GrMipMapped mipMapped,
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600795 GrMipMapsStatus mipMapsStatus,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400796 GrInternalSurfaceFlags surfaceFlags,
Brian Salomon7226c232018-07-30 13:13:17 -0400797 SkBackingFit fit,
798 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -0400799 GrProtected isProtected) {
800 // For non-ddl draws always make lazy proxy's single use.
801 LazyInstantiationType lazyType = this->renderingDirectly() ? LazyInstantiationType::kSingleUse
802 : LazyInstantiationType::kMultipleUse;
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400803 return this->createLazyProxy(std::move(callback), format, desc, renderable,
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600804 renderTargetSampleCnt, origin, mipMapped, mipMapsStatus,
805 surfaceFlags, fit, budgeted, isProtected, lazyType);
Brian Salomone8a766b2019-07-19 14:24:36 -0400806}
807
808sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
809 const GrBackendFormat& format,
810 const GrSurfaceDesc& desc,
811 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400812 int renderTargetSampleCnt,
Brian Salomone8a766b2019-07-19 14:24:36 -0400813 GrSurfaceOrigin origin,
814 GrMipMapped mipMapped,
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600815 GrMipMapsStatus mipMapsStatus,
Brian Salomone8a766b2019-07-19 14:24:36 -0400816 GrInternalSurfaceFlags surfaceFlags,
817 SkBackingFit fit,
818 SkBudgeted budgeted,
819 GrProtected isProtected,
Greg Daniela8d92112018-03-09 12:05:04 -0500820 LazyInstantiationType lazyType) {
Robert Phillips777707b2018-01-17 11:40:14 -0500821 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
822 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400823
Robert Phillips0a15cc62019-07-30 12:49:10 -0400824 if (!format.isValid()) {
825 return nullptr;
826 }
827
Robert Phillipsa41c6852019-02-07 10:44:10 -0500828 if (desc.fWidth > this->caps()->maxTextureSize() ||
829 desc.fHeight > this->caps()->maxTextureSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400830 return nullptr;
831 }
832
Greg Danielfa55f2e2019-06-13 15:21:38 -0400833 SkASSERT(validate_backend_format_and_config(this->caps(), format, desc.fConfig));
Greg Daniel457469c2018-02-08 15:05:44 -0500834
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400835 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
836 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(format, colorType);
837 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(format, colorType);
838
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600839 return sk_sp<GrTextureProxy>((renderable == GrRenderable::kYes)
840 ? new GrTextureRenderTargetProxy(
841 std::move(callback), lazyType, format, desc, renderTargetSampleCnt, origin,
842 mipMapped, mipMapsStatus, texSwizzle, outSwizzle, fit, budgeted, isProtected,
843 surfaceFlags)
844 : new GrTextureProxy(
845 std::move(callback), lazyType, format, desc, origin, mipMapped, mipMapsStatus,
846 texSwizzle, fit, budgeted, isProtected, surfaceFlags));
Robert Phillips777707b2018-01-17 11:40:14 -0500847}
848
Robert Phillipse8fabb22018-02-04 14:33:21 -0500849sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500850 LazyInstantiateCallback&& callback, const GrBackendFormat& format,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400851 const GrSurfaceDesc& desc, int sampleCnt, GrSurfaceOrigin origin,
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600852 GrInternalSurfaceFlags surfaceFlags, const TextureInfo* textureInfo,
853 GrMipMapsStatus mipMapsStatus, SkBackingFit fit, SkBudgeted budgeted,
854 GrProtected isProtected, bool wrapsVkSecondaryCB) {
Robert Phillipse8fabb22018-02-04 14:33:21 -0500855 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
856 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400857
Robert Phillipsa41c6852019-02-07 10:44:10 -0500858 if (desc.fWidth > this->caps()->maxRenderTargetSize() ||
859 desc.fHeight > this->caps()->maxRenderTargetSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400860 return nullptr;
861 }
862
Greg Danielfa55f2e2019-06-13 15:21:38 -0400863 SkASSERT(validate_backend_format_and_config(this->caps(), format, desc.fConfig));
Greg Daniel457469c2018-02-08 15:05:44 -0500864
865 using LazyInstantiationType = GrSurfaceProxy::LazyInstantiationType;
866 // For non-ddl draws always make lazy proxy's single use.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500867 LazyInstantiationType lazyType = this->renderingDirectly() ? LazyInstantiationType::kSingleUse
868 : LazyInstantiationType::kMultipleUse;
Greg Daniel457469c2018-02-08 15:05:44 -0500869
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400870 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
871 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(format, colorType);
872 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(format, colorType);
873
Brian Salomon7226c232018-07-30 13:13:17 -0400874 if (textureInfo) {
Greg Danielb085fa92019-03-05 16:55:12 -0500875 // Wrapped vulkan secondary command buffers don't support texturing since we won't have an
876 // actual VkImage to texture from.
877 SkASSERT(!wrapsVkSecondaryCB);
Brian Salomon7226c232018-07-30 13:13:17 -0400878 return sk_sp<GrRenderTargetProxy>(new GrTextureRenderTargetProxy(
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400879 std::move(callback), lazyType, format, desc, sampleCnt, origin,
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600880 textureInfo->fMipMapped, mipMapsStatus, texSwizzle, outSwizzle, fit, budgeted,
881 isProtected, surfaceFlags));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500882 }
883
Greg Danielb085fa92019-03-05 16:55:12 -0500884 GrRenderTargetProxy::WrapsVkSecondaryCB vkSCB =
885 wrapsVkSecondaryCB ? GrRenderTargetProxy::WrapsVkSecondaryCB::kYes
886 : GrRenderTargetProxy::WrapsVkSecondaryCB::kNo;
887
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400888 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
889 std::move(callback), lazyType, format, desc, sampleCnt, origin, texSwizzle, outSwizzle,
890 fit, budgeted, isProtected, surfaceFlags, vkSCB));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500891}
892
Chris Dalton19cc00f2019-04-15 09:06:28 -0600893sk_sp<GrTextureProxy> GrProxyProvider::MakeFullyLazyProxy(
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400894 LazyInstantiateCallback&& callback, const GrBackendFormat& format, GrRenderable renderable,
895 int renderTargetSampleCnt, GrProtected isProtected, GrSurfaceOrigin origin,
896 GrPixelConfig config, const GrCaps& caps) {
Robert Phillips0a15cc62019-07-30 12:49:10 -0400897 if (!format.isValid()) {
898 return nullptr;
899 }
900
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400901 SkASSERT(renderTargetSampleCnt == 1 || renderable == GrRenderable::kYes);
Greg Danielfa55f2e2019-06-13 15:21:38 -0400902 SkASSERT(validate_backend_format_and_config(&caps, format, config));
Robert Phillips777707b2018-01-17 11:40:14 -0500903 GrSurfaceDesc desc;
Robert Phillips10d17212019-04-24 14:09:10 -0400904 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNone;
Robert Phillips777707b2018-01-17 11:40:14 -0500905 desc.fWidth = -1;
906 desc.fHeight = -1;
907 desc.fConfig = config;
Robert Phillips777707b2018-01-17 11:40:14 -0500908
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400909 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
910 GrSwizzle texSwizzle = caps.getTextureSwizzle(format, colorType);
911 GrSwizzle outSwizzle = caps.getOutputSwizzle(format, colorType);
912
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600913 return sk_sp<GrTextureProxy>((GrRenderable::kYes == renderable)
914 ? new GrTextureRenderTargetProxy(
915 std::move(callback), LazyInstantiationType::kSingleUse, format, desc,
916 renderTargetSampleCnt, origin, GrMipMapped::kNo, GrMipMapsStatus::kNotAllocated,
917 texSwizzle, outSwizzle, SkBackingFit::kApprox, SkBudgeted::kYes, isProtected,
918 surfaceFlags)
919 : new GrTextureProxy(
920 std::move(callback), LazyInstantiationType::kSingleUse, format, desc, origin,
921 GrMipMapped::kNo, GrMipMapsStatus::kNotAllocated, texSwizzle,
922 SkBackingFit::kApprox, SkBudgeted::kYes, isProtected, surfaceFlags));
Robert Phillips777707b2018-01-17 11:40:14 -0500923}
924
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500925bool GrProxyProvider::IsFunctionallyExact(GrSurfaceProxy* proxy) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400926 const bool isInstantiated = proxy->isInstantiated();
Robert Phillipsdb3b9792018-06-12 15:18:00 -0400927 // A proxy is functionally exact if:
928 // it is exact (obvs)
929 // when it is instantiated it will be exact (i.e., power of two dimensions)
930 // it is already instantiated and the proxy covers the entire backing surface
931 return proxy->priv().isExact() ||
932 (!isInstantiated && SkIsPow2(proxy->width()) && SkIsPow2(proxy->height())) ||
933 (isInstantiated && proxy->worstCaseWidth() == proxy->width() &&
934 proxy->worstCaseHeight() == proxy->height());
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500935}
936
Robert Phillips427966a2018-12-20 17:20:43 -0500937void GrProxyProvider::processInvalidUniqueKey(const GrUniqueKey& key, GrTextureProxy* proxy,
938 InvalidateGPUResource invalidateGPUResource) {
Chris Dalton2de13dd2019-01-03 15:11:59 -0700939 SkASSERT(key.isValid());
940
Robert Phillips427966a2018-12-20 17:20:43 -0500941 if (!proxy) {
942 proxy = fUniquelyKeyedProxies.find(key);
943 }
Chris Dalton2de13dd2019-01-03 15:11:59 -0700944 SkASSERT(!proxy || proxy->getUniqueKey() == key);
945
946 // Locate the corresponding GrGpuResource (if it needs to be invalidated) before clearing the
947 // proxy's unique key. We must do it in this order because 'key' may alias the proxy's key.
948 sk_sp<GrGpuResource> invalidGpuResource;
949 if (InvalidateGPUResource::kYes == invalidateGPUResource) {
Brian Salomon01ceae92019-04-02 11:49:54 -0400950 GrContext* direct = fImageContext->priv().asDirectContext();
951 if (direct) {
952 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
953 invalidGpuResource = resourceProvider->findByUniqueKey<GrGpuResource>(key);
Chris Dalton2de13dd2019-01-03 15:11:59 -0700954 }
955 SkASSERT(!invalidGpuResource || invalidGpuResource->getUniqueKey() == key);
956 }
Robert Phillips427966a2018-12-20 17:20:43 -0500957
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500958 // Note: this method is called for the whole variety of GrGpuResources so often 'key'
959 // will not be in 'fUniquelyKeyedProxies'.
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500960 if (proxy) {
Robert Phillips427966a2018-12-20 17:20:43 -0500961 fUniquelyKeyedProxies.remove(key);
962 proxy->cacheAccess().clearUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500963 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500964
Chris Dalton2de13dd2019-01-03 15:11:59 -0700965 if (invalidGpuResource) {
966 invalidGpuResource->resourcePriv().removeUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500967 }
968}
969
Robert Phillipsa41c6852019-02-07 10:44:10 -0500970uint32_t GrProxyProvider::contextID() const {
971 return fImageContext->priv().contextID();
972}
973
974const GrCaps* GrProxyProvider::caps() const {
975 return fImageContext->priv().caps();
976}
977
978sk_sp<const GrCaps> GrProxyProvider::refCaps() const {
979 return fImageContext->priv().refCaps();
980}
981
Robert Phillipsa9162df2019-02-11 14:12:03 -0500982bool GrProxyProvider::isAbandoned() const {
983 return fImageContext->priv().abandoned();
984}
985
Robert Phillips0790f8a2018-09-18 13:11:03 -0400986void GrProxyProvider::orphanAllUniqueKeys() {
987 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
988 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
989 GrTextureProxy& tmp = *iter;
990
991 tmp.fProxyProvider = nullptr;
992 }
993}
994
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500995void GrProxyProvider::removeAllUniqueKeys() {
996 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
997 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
998 GrTextureProxy& tmp = *iter;
999
Chris Dalton2de13dd2019-01-03 15:11:59 -07001000 this->processInvalidUniqueKey(tmp.getUniqueKey(), &tmp, InvalidateGPUResource::kNo);
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001001 }
1002 SkASSERT(!fUniquelyKeyedProxies.count());
1003}
Robert Phillipsa41c6852019-02-07 10:44:10 -05001004
1005bool GrProxyProvider::renderingDirectly() const {
1006 return fImageContext->priv().asDirectContext();
1007}