blob: cc76aa2abc5f420b9f06b3b993bfcf950a6c0f9e [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 Salomon27b4d8d2019-07-22 14:23:45 -0400118 const GrSurfaceDesc& desc, GrRenderable renderable, int renderTargetSampleCnt,
119 GrSurfaceOrigin origin, SkBackingFit fit, SkBudgeted budgeted, GrProtected isProtected) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500120 GrContext* direct = fImageContext->priv().asDirectContext();
121 if (!direct) {
122 return nullptr;
123 }
124
125 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
126 sk_sp<GrTexture> tex;
127
128 if (SkBackingFit::kApprox == fit) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400129 tex = resourceProvider->createApproxTexture(desc, renderable, renderTargetSampleCnt,
130 isProtected,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400131 GrResourceProvider::Flags::kNoPendingIO);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500132 } else {
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400133 tex = resourceProvider->createTexture(desc, renderable, renderTargetSampleCnt, budgeted,
134 isProtected, GrResourceProvider::Flags::kNoPendingIO);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500135 }
136 if (!tex) {
137 return nullptr;
138 }
139
140 return this->createWrapped(std::move(tex), origin);
141}
142
143sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createWrapped(sk_sp<GrTexture> tex,
144 GrSurfaceOrigin origin) {
145 return this->createWrapped(std::move(tex), origin);
146}
147#endif
148
Robert Phillipsadbe1322018-01-17 13:35:46 -0500149sk_sp<GrTextureProxy> GrProxyProvider::createWrapped(sk_sp<GrTexture> tex, GrSurfaceOrigin origin) {
150#ifdef SK_DEBUG
151 if (tex->getUniqueKey().isValid()) {
152 SkASSERT(!this->findProxyByUniqueKey(tex->getUniqueKey(), origin));
153 }
154#endif
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400155 GrColorType colorType = GrPixelConfigToColorType(tex->config());
156 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(tex->backendFormat(), colorType);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500157
158 if (tex->asRenderTarget()) {
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400159 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(tex->backendFormat(), colorType);
160 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin,
161 texSwizzle, outSwizzle));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500162 } else {
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400163 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin, texSwizzle));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500164 }
165}
166
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500167sk_sp<GrTextureProxy> GrProxyProvider::findOrCreateProxyByUniqueKey(const GrUniqueKey& key,
168 GrSurfaceOrigin origin) {
169 ASSERT_SINGLE_OWNER
170
171 if (this->isAbandoned()) {
172 return nullptr;
173 }
174
175 sk_sp<GrTextureProxy> result = this->findProxyByUniqueKey(key, origin);
176 if (result) {
177 return result;
178 }
179
Robert Phillipsa41c6852019-02-07 10:44:10 -0500180 GrContext* direct = fImageContext->priv().asDirectContext();
181 if (!direct) {
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500182 return nullptr;
183 }
184
Robert Phillipsa41c6852019-02-07 10:44:10 -0500185 GrResourceCache* resourceCache = direct->priv().getResourceCache();
186
187 GrGpuResource* resource = resourceCache->findAndRefUniqueResource(key);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500188 if (!resource) {
189 return nullptr;
190 }
191
192 sk_sp<GrTexture> texture(static_cast<GrSurface*>(resource)->asTexture());
193 SkASSERT(texture);
194
Robert Phillipsadbe1322018-01-17 13:35:46 -0500195 result = this->createWrapped(std::move(texture), origin);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500196 SkASSERT(result->getUniqueKey() == key);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500197 // createWrapped should've added this for us
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500198 SkASSERT(fUniquelyKeyedProxies.find(key));
199 return result;
200}
201
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500202sk_sp<GrTextureProxy> GrProxyProvider::createTextureProxy(sk_sp<SkImage> srcImage,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400203 GrRenderable renderable,
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500204 int sampleCnt,
Greg Danielfb3abcd2018-02-02 15:48:33 -0500205 SkBudgeted budgeted,
Chris Daltond004e0b2018-09-27 09:28:03 -0600206 SkBackingFit fit,
207 GrInternalSurfaceFlags surfaceFlags) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500208 ASSERT_SINGLE_OWNER
209 SkASSERT(srcImage);
210
211 if (this->isAbandoned()) {
212 return nullptr;
213 }
214
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400215 const SkImageInfo& info = srcImage->imageInfo();
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400216 SkColorType ct = info.colorType();
Greg Daniel2f2caea2019-07-08 14:24:47 -0400217 GrColorType grCT = SkColorTypeToGrColorType(ct);
Greg Danielf87651e2018-02-21 11:36:53 -0500218
Greg Daniel627d0532019-07-08 16:48:14 -0400219 GrBackendFormat format = this->caps()->getBackendFormatFromColorType(grCT);
Greg Daniel0a7aa142018-02-21 13:02:32 -0500220
Greg Daniel7b1bebf2019-07-16 17:09:59 -0400221 if (!format.isValid() || !this->caps()->isFormatTexturable(grCT, format)) {
Brian Osmand29dcd12018-09-13 15:04:29 -0400222 SkBitmap copy8888;
223 if (!copy8888.tryAllocPixels(info.makeColorType(kRGBA_8888_SkColorType)) ||
224 !srcImage->readPixels(copy8888.pixmap(), 0, 0)) {
225 return nullptr;
226 }
227 copy8888.setImmutable();
228 srcImage = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode);
Jim Van Verthc8098322019-04-16 10:50:01 -0400229 ct = kRGBA_8888_SkColorType;
Greg Daniel627d0532019-07-08 16:48:14 -0400230 grCT = GrColorType::kRGBA_8888;
231 format = this->caps()->getBackendFormatFromColorType(grCT);
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400232 if (!format.isValid()) {
233 return nullptr;
234 }
Brian Osmand29dcd12018-09-13 15:04:29 -0400235 }
236
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400237 if (renderable == GrRenderable::kYes) {
Greg Daniel5c96db82019-07-09 14:06:58 -0400238 sampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, grCT, format);
Greg Danielf87651e2018-02-21 11:36:53 -0500239 if (!sampleCnt) {
240 return nullptr;
241 }
242 }
243
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400244 GrPixelConfig config = SkColorType2GrPixelConfig(ct);
245 if (kUnknown_GrPixelConfig == config) {
246 return nullptr;
247 }
248
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500249 GrSurfaceDesc desc;
250 desc.fWidth = srcImage->width();
251 desc.fHeight = srcImage->height();
Greg Danielf87651e2018-02-21 11:36:53 -0500252 desc.fConfig = config;
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500253
254 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400255 [desc, renderable, sampleCnt, budgeted, srcImage,
256 fit](GrResourceProvider* resourceProvider) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500257 SkPixmap pixMap;
258 SkAssertResult(srcImage->peekPixels(&pixMap));
259 GrMipLevel mipLevel = { pixMap.addr(), pixMap.rowBytes() };
260
Brian Salomone8a766b2019-07-19 14:24:36 -0400261 return LazyInstantiationResult(resourceProvider->createTexture(
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400262 desc, renderable, sampleCnt, budgeted, fit, GrProtected::kNo, mipLevel,
Brian Salomone8a766b2019-07-19 14:24:36 -0400263 GrResourceProvider::Flags::kNoPendingIO));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500264 },
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400265 format, desc, renderable, sampleCnt, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo,
266 surfaceFlags, fit, budgeted, GrProtected::kNo);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500267
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400268 if (!proxy) {
269 return nullptr;
270 }
271
Robert Phillipsa41c6852019-02-07 10:44:10 -0500272 GrContext* direct = fImageContext->priv().asDirectContext();
273 if (direct) {
274 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
275
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500276 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
277 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500278 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500279 return nullptr;
280 }
281 }
Robert Phillipsc1b60662018-06-26 10:20:08 -0400282
283 SkASSERT(proxy->width() == desc.fWidth);
284 SkASSERT(proxy->height() == desc.fHeight);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500285 return proxy;
286}
287
Greg Daniel4065d452018-11-16 15:43:41 -0500288sk_sp<GrTextureProxy> GrProxyProvider::createMipMapProxy(const GrBackendFormat& format,
289 const GrSurfaceDesc& desc,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400290 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400291 int renderTargetSampleCnt,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500292 GrSurfaceOrigin origin,
Brian Salomone8a766b2019-07-19 14:24:36 -0400293 SkBudgeted budgeted,
294 GrProtected isProtected) {
Robert Phillips579f0942018-01-08 14:53:35 -0500295 ASSERT_SINGLE_OWNER
296
297 if (this->isAbandoned()) {
298 return nullptr;
299 }
300
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400301 return this->createProxy(format, desc, renderable, renderTargetSampleCnt, origin,
302 GrMipMapped::kYes, SkBackingFit::kExact, budgeted, isProtected,
Brian Salomone8a766b2019-07-19 14:24:36 -0400303 GrInternalSurfaceFlags::kNone);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500304}
305
Brian Osmande496652019-03-22 13:42:33 -0400306sk_sp<GrTextureProxy> GrProxyProvider::createProxyFromBitmap(const SkBitmap& bitmap,
307 GrMipMapped mipMapped) {
Brian Osman412674f2019-02-07 15:34:58 -0500308 ASSERT_SINGLE_OWNER
Brian Osman7dcc6162019-03-25 10:12:57 -0400309 SkASSERT(GrMipMapped::kNo == mipMapped || this->caps()->mipMapSupport());
Brian Osman412674f2019-02-07 15:34:58 -0500310
311 if (this->isAbandoned()) {
312 return nullptr;
313 }
314
Brian Osman2b23c4b2018-06-01 12:25:08 -0400315 if (!SkImageInfoIsValid(bitmap.info())) {
Greg Daniela4ead652018-02-07 10:21:48 -0500316 return nullptr;
317 }
318
Brian Osmande496652019-03-22 13:42:33 -0400319 ATRACE_ANDROID_FRAMEWORK("Upload %sTexture [%ux%u]",
320 GrMipMapped::kYes == mipMapped ? "MipMap " : "",
321 bitmap.width(), bitmap.height());
Greg Daniela4ead652018-02-07 10:21:48 -0500322
323 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
324 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
325 // 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 -0500326 SkCopyPixelsMode copyMode = this->renderingDirectly() ? kNever_SkCopyPixelsMode
327 : kIfMutable_SkCopyPixelsMode;
Greg Daniela4ead652018-02-07 10:21:48 -0500328 sk_sp<SkImage> baseLevel = SkMakeImageFromRasterBitmap(bitmap, copyMode);
Greg Daniela4ead652018-02-07 10:21:48 -0500329 if (!baseLevel) {
330 return nullptr;
331 }
332
Brian Osmande496652019-03-22 13:42:33 -0400333 // If mips weren't requested (or this was too small to have any), then take the fast path
334 if (GrMipMapped::kNo == mipMapped ||
335 0 == SkMipMap::ComputeLevelCount(baseLevel->width(), baseLevel->height())) {
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400336 return this->createTextureProxy(std::move(baseLevel), GrRenderable::kNo, 1,
Brian Osman7dcc6162019-03-25 10:12:57 -0400337 SkBudgeted::kYes, SkBackingFit::kExact);
Greg Daniela4ead652018-02-07 10:21:48 -0500338 }
339
Greg Daniel627d0532019-07-08 16:48:14 -0400340 GrColorType grColorType = SkColorTypeToGrColorType(bitmap.info().colorType());
341 GrBackendFormat format = this->caps()->getBackendFormatFromColorType(grColorType);
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400342 if (!format.isValid()) {
343 return nullptr;
344 }
Brian Osmand29dcd12018-09-13 15:04:29 -0400345 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info());
Greg Daniel3fc5df42019-06-14 09:42:17 -0400346
Greg Daniel2f2caea2019-07-08 14:24:47 -0400347 if (!this->caps()->isFormatTexturable(grColorType, format)) {
Brian Osmand29dcd12018-09-13 15:04:29 -0400348 SkBitmap copy8888;
349 if (!copy8888.tryAllocPixels(bitmap.info().makeColorType(kRGBA_8888_SkColorType)) ||
350 !bitmap.readPixels(copy8888.pixmap())) {
351 return nullptr;
352 }
353 copy8888.setImmutable();
354 baseLevel = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode);
355 desc.fConfig = kRGBA_8888_GrPixelConfig;
Greg Daniel627d0532019-07-08 16:48:14 -0400356 grColorType = GrColorType::kRGBA_8888;
357 format = this->caps()->getBackendFormatFromColorType(grColorType);
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400358 if (!format.isValid()) {
359 return nullptr;
360 }
Greg Daniel3fc5df42019-06-14 09:42:17 -0400361 }
362
Brian Osmand29dcd12018-09-13 15:04:29 -0400363
364 SkPixmap pixmap;
365 SkAssertResult(baseLevel->peekPixels(&pixmap));
366 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(pixmap, nullptr));
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400367 if (!mipmaps) {
368 return nullptr;
369 }
370
Greg Daniela4ead652018-02-07 10:21:48 -0500371 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Osman1b97f132018-09-13 17:33:48 +0000372 [desc, baseLevel, mipmaps](GrResourceProvider* resourceProvider) {
Brian Osman1b97f132018-09-13 17:33:48 +0000373 const int mipLevelCount = mipmaps->countLevels() + 1;
374 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
375
Greg Daniela4ead652018-02-07 10:21:48 -0500376 SkPixmap pixmap;
377 SkAssertResult(baseLevel->peekPixels(&pixmap));
378
379 // DDL TODO: Instead of copying all this info into GrMipLevels we should just plumb
380 // the use of SkMipMap down through Ganesh.
381 texels[0].fPixels = pixmap.addr();
382 texels[0].fRowBytes = pixmap.rowBytes();
383
384 for (int i = 1; i < mipLevelCount; ++i) {
385 SkMipMap::Level generatedMipLevel;
386 mipmaps->getLevel(i - 1, &generatedMipLevel);
387 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
388 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
389 SkASSERT(texels[i].fPixels);
390 }
391
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400392 return LazyInstantiationResult(resourceProvider->createTexture(
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400393 desc, GrRenderable::kNo, 1, SkBudgeted::kYes, GrProtected::kNo,
394 texels.get(), mipLevelCount));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500395 },
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400396 format, desc, GrRenderable::kNo, 1, kTopLeft_GrSurfaceOrigin, GrMipMapped::kYes,
Brian Salomone8a766b2019-07-19 14:24:36 -0400397 SkBackingFit::kExact, SkBudgeted::kYes, GrProtected::kNo);
Greg Daniela4ead652018-02-07 10:21:48 -0500398
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400399 if (!proxy) {
400 return nullptr;
401 }
402
Robert Phillipsa41c6852019-02-07 10:44:10 -0500403 GrContext* direct = fImageContext->priv().asDirectContext();
404 if (direct) {
405 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Daniela4ead652018-02-07 10:21:48 -0500406 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
407 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500408 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Greg Daniela4ead652018-02-07 10:21:48 -0500409 return nullptr;
410 }
411 }
412 return proxy;
413}
414
Greg Danielfa55f2e2019-06-13 15:21:38 -0400415#ifdef SK_DEBUG
416static bool validate_backend_format_and_config(const GrCaps* caps,
417 const GrBackendFormat& format,
418 GrPixelConfig config) {
419 if (kUnknown_GrPixelConfig == config) {
420 return false;
421 }
Brian Salomonbb8dde82019-06-27 10:52:13 -0400422 if (GrPixelConfigIsCompressed(config)) {
423 // We have no way to verify these at the moment.
424 return true;
425 }
Robert Phillipsc046ff02019-07-01 10:34:03 -0400426
Robert Phillips1e2cb442019-07-02 15:51:28 -0400427 GrColorType grCT = GrPixelConfigToColorType(config);
428
429 return caps->areColorTypeAndFormatCompatible(grCT, format);
Greg Danielfa55f2e2019-06-13 15:21:38 -0400430}
Robert Phillips0902c982019-07-16 07:47:56 -0400431
432static bool validate_backend_format_and_colortype(const GrCaps* caps,
433 GrColorType colorType,
434 const GrBackendFormat& format) {
435 return caps->areColorTypeAndFormatCompatible(colorType, format);
436}
Greg Danielfa55f2e2019-06-13 15:21:38 -0400437#endif
438
Greg Daniel4065d452018-11-16 15:43:41 -0500439sk_sp<GrTextureProxy> GrProxyProvider::createProxy(const GrBackendFormat& format,
440 const GrSurfaceDesc& desc,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400441 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400442 int renderTargetSampleCnt,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500443 GrSurfaceOrigin origin,
Greg Danielf6f7b672018-02-15 13:06:26 -0500444 GrMipMapped mipMapped,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500445 SkBackingFit fit,
446 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -0400447 GrProtected isProtected,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400448 GrInternalSurfaceFlags surfaceFlags) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400449 if (GrPixelConfigIsCompressed(desc.fConfig)) {
450 // Deferred proxies for compressed textures are not supported.
451 return nullptr;
452 }
Robert Phillips0902c982019-07-16 07:47:56 -0400453
454 const GrCaps* caps = this->caps();
455 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
456
457 SkASSERT(GrCaps::AreConfigsCompatible(desc.fConfig,
458 caps->getConfigFromBackendFormat(format, colorType)));
459 SkASSERT(validate_backend_format_and_colortype(caps, colorType, format));
Greg Danielf6f7b672018-02-15 13:06:26 -0500460 if (GrMipMapped::kYes == mipMapped) {
461 // SkMipMap doesn't include the base level in the level count so we have to add 1
462 int mipCount = SkMipMap::ComputeLevelCount(desc.fWidth, desc.fHeight) + 1;
463 if (1 == mipCount) {
464 mipMapped = GrMipMapped::kNo;
465 }
466 }
467
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400468 if (!caps->validateSurfaceDesc(desc, renderable, renderTargetSampleCnt, mipMapped)) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500469 return nullptr;
470 }
Brian Salomon3a2cc2c2018-02-03 00:25:12 +0000471 GrSurfaceDesc copyDesc = desc;
Robert Phillips0902c982019-07-16 07:47:56 -0400472 GrSwizzle texSwizzle = caps->getTextureSwizzle(format, colorType);
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400473 if (renderable == GrRenderable::kYes) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400474 renderTargetSampleCnt =
475 caps->getRenderTargetSampleCount(renderTargetSampleCnt, colorType, format);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500476 // We know anything we instantiate later from this deferred path will be
477 // both texturable and renderable
Robert Phillips0902c982019-07-16 07:47:56 -0400478 GrSwizzle outSwizzle = caps->getOutputSwizzle(format, colorType);
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400479 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
480 *caps, format, copyDesc, renderTargetSampleCnt, origin, mipMapped, texSwizzle,
481 outSwizzle, fit, budgeted, isProtected, surfaceFlags));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500482 }
483
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400484 return sk_sp<GrTextureProxy>(new GrTextureProxy(format, copyDesc, origin, mipMapped, texSwizzle,
Brian Salomone8a766b2019-07-19 14:24:36 -0400485 fit, budgeted, isProtected, surfaceFlags));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500486}
487
Brian Salomonbb8dde82019-06-27 10:52:13 -0400488sk_sp<GrTextureProxy> GrProxyProvider::createCompressedTextureProxy(
489 int width, int height, SkBudgeted budgeted, SkImage::CompressionType compressionType,
490 sk_sp<SkData> data) {
491 GrBackendFormat format = this->caps()->getBackendFormatFromCompressionType(compressionType);
492
493 GrSurfaceDesc desc;
494 desc.fConfig = GrCompressionTypePixelConfig(compressionType);
495 desc.fWidth = width;
496 desc.fHeight = height;
497
Jim Van Verthee06b332019-01-18 10:36:32 -0500498 if (!this->caps()->isConfigTexturable(desc.fConfig)) {
499 return nullptr;
500 }
501
Jim Van Verthee06b332019-01-18 10:36:32 -0500502 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomonbb8dde82019-06-27 10:52:13 -0400503 [width, height, compressionType, budgeted, data](GrResourceProvider* resourceProvider) {
504 return LazyInstantiationResult(resourceProvider->createCompressedTexture(
505 width, height, compressionType, budgeted, data.get()));
506 },
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400507 format, desc, GrRenderable::kNo, 1, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo,
Brian Salomone8a766b2019-07-19 14:24:36 -0400508 SkBackingFit::kExact, SkBudgeted::kYes, GrProtected::kNo);
Jim Van Verthee06b332019-01-18 10:36:32 -0500509
510 if (!proxy) {
511 return nullptr;
512 }
513
Robert Phillipsa41c6852019-02-07 10:44:10 -0500514 GrContext* direct = fImageContext->priv().asDirectContext();
515 if (direct) {
516 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Jim Van Verthee06b332019-01-18 10:36:32 -0500517 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
518 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500519 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500520 return nullptr;
521 }
522 }
523 return proxy;
524}
525
Brian Salomon7578f3e2018-03-07 14:39:54 -0500526sk_sp<GrTextureProxy> GrProxyProvider::wrapBackendTexture(const GrBackendTexture& backendTex,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400527 GrColorType grColorType,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500528 GrSurfaceOrigin origin,
529 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500530 GrWrapCacheable cacheable,
Brian Salomonc67c31c2018-12-06 10:00:03 -0500531 GrIOType ioType,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500532 ReleaseProc releaseProc,
533 ReleaseContext releaseCtx) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500534 SkASSERT(ioType != kWrite_GrIOType);
Robert Phillipsf9bec202018-01-16 09:21:01 -0500535 if (this->isAbandoned()) {
536 return nullptr;
537 }
538
Brian Salomonf7778972018-03-08 10:13:17 -0500539 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500540 GrContext* direct = fImageContext->priv().asDirectContext();
541 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500542 return nullptr;
543 }
544
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400545 const GrCaps* caps = this->caps();
546
547 SkASSERT(GrCaps::AreConfigsCompatible(backendTex.config(),
548 caps->getConfigFromBackendFormat(
549 backendTex.getBackendFormat(),
550 grColorType)));
551
552 SkASSERT(validate_backend_format_and_colortype(caps, grColorType,
553 backendTex.getBackendFormat()));
Greg Danielfa55f2e2019-06-13 15:21:38 -0400554
Robert Phillipsa41c6852019-02-07 10:44:10 -0500555 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
556
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500557 sk_sp<GrTexture> tex =
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400558 resourceProvider->wrapBackendTexture(backendTex, grColorType,
559 ownership, cacheable, ioType);
Brian Salomonf7778972018-03-08 10:13:17 -0500560 if (!tex) {
561 return nullptr;
562 }
Robert Phillipsadbe1322018-01-17 13:35:46 -0500563
Greg Daniel6a0176b2018-01-30 09:28:44 -0500564 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500565 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel6a0176b2018-01-30 09:28:44 -0500566 }
567
Brian Salomonf7778972018-03-08 10:13:17 -0500568 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
569 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500570 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500571
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400572 GrSwizzle texSwizzle = caps->getTextureSwizzle(tex->backendFormat(), grColorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400573
574 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin, texSwizzle));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500575}
576
Brian Salomon7578f3e2018-03-07 14:39:54 -0500577sk_sp<GrTextureProxy> GrProxyProvider::wrapRenderableBackendTexture(
Brian Salomon02bd2952018-03-07 15:20:21 -0500578 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400579 GrColorType colorType, GrWrapOwnership ownership, GrWrapCacheable cacheable,
580 ReleaseProc releaseProc, ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500581 if (this->isAbandoned()) {
582 return nullptr;
583 }
584
Brian Salomonf7778972018-03-08 10:13:17 -0500585 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500586 GrContext* direct = fImageContext->priv().asDirectContext();
587 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500588 return nullptr;
589 }
590
Robert Phillips0902c982019-07-16 07:47:56 -0400591 const GrCaps* caps = this->caps();
592
593 SkASSERT(GrCaps::AreConfigsCompatible(backendTex.config(),
594 caps->getConfigFromBackendFormat(
595 backendTex.getBackendFormat(),
596 colorType)));
597 SkASSERT(validate_backend_format_and_colortype(caps, colorType, backendTex.getBackendFormat()));
Greg Danielfa55f2e2019-06-13 15:21:38 -0400598
Robert Phillipsa41c6852019-02-07 10:44:10 -0500599 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
600
Robert Phillips0902c982019-07-16 07:47:56 -0400601 sampleCnt = caps->getRenderTargetSampleCount(sampleCnt, colorType,
602 backendTex.getBackendFormat());
Greg Danielf87651e2018-02-21 11:36:53 -0500603 if (!sampleCnt) {
604 return nullptr;
605 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500606
Robert Phillipsa41c6852019-02-07 10:44:10 -0500607 sk_sp<GrTexture> tex = resourceProvider->wrapRenderableBackendTexture(backendTex, sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400608 colorType, ownership,
609 cacheable);
Brian Salomonf7778972018-03-08 10:13:17 -0500610 if (!tex) {
611 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500612 }
613
Greg Daniel8ce79912019-02-05 10:08:43 -0500614 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500615 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500616 }
617
Brian Salomonf7778972018-03-08 10:13:17 -0500618 SkASSERT(tex->asRenderTarget()); // A GrTextureRenderTarget
619 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500620 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Greg Daniel6abda432018-02-15 14:55:00 -0500621
Robert Phillips0902c982019-07-16 07:47:56 -0400622 GrSwizzle texSwizzle = caps->getTextureSwizzle(tex->backendFormat(), colorType);
623 GrSwizzle outSwizzle = caps->getOutputSwizzle(tex->backendFormat(), colorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400624
625 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin, texSwizzle,
626 outSwizzle));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500627}
628
Brian Salomon7578f3e2018-03-07 14:39:54 -0500629sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400630 const GrBackendRenderTarget& backendRT, GrColorType grColorType,
631 GrSurfaceOrigin origin, ReleaseProc releaseProc, ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500632 if (this->isAbandoned()) {
633 return nullptr;
634 }
635
Brian Salomonf7778972018-03-08 10:13:17 -0500636 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500637 GrContext* direct = fImageContext->priv().asDirectContext();
638 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500639 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500640 }
641
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400642#ifdef SK_DEBUG
Greg Danielfa55f2e2019-06-13 15:21:38 -0400643 GrPixelConfig testConfig =
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400644 this->caps()->validateBackendRenderTarget(backendRT, grColorType);
Greg Danielfa55f2e2019-06-13 15:21:38 -0400645 SkASSERT(testConfig != kUnknown_GrPixelConfig);
646#endif
647
Robert Phillipsa41c6852019-02-07 10:44:10 -0500648 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
649
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400650 sk_sp<GrRenderTarget> rt = resourceProvider->wrapBackendRenderTarget(backendRT, grColorType);
Brian Salomonf7778972018-03-08 10:13:17 -0500651 if (!rt) {
652 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500653 }
Greg Daniel8ce79912019-02-05 10:08:43 -0500654
Greg Daniel8ce79912019-02-05 10:08:43 -0500655 if (releaseProc) {
Brian Salomon2ca31f82019-03-05 13:28:58 -0500656 rt->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500657 }
658
Brian Salomonf7778972018-03-08 10:13:17 -0500659 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
660 SkASSERT(!rt->getUniqueKey().isValid());
661 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500662 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Brian Salomonf7778972018-03-08 10:13:17 -0500663
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400664 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(rt->backendFormat(), grColorType);
665 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(rt->backendFormat(), grColorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400666
667 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(std::move(rt), origin, texSwizzle,
668 outSwizzle));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500669}
670
Brian Salomon7578f3e2018-03-07 14:39:54 -0500671sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendTextureAsRenderTarget(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400672 const GrBackendTexture& backendTex, GrColorType grColorType,
673 GrSurfaceOrigin origin, int sampleCnt) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500674 if (this->isAbandoned()) {
675 return nullptr;
676 }
677
Brian Salomonf7778972018-03-08 10:13:17 -0500678 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500679 GrContext* direct = fImageContext->priv().asDirectContext();
680 if (!direct) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500681 return nullptr;
682 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500683
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400684 const GrCaps* caps = this->caps();
685
686 SkASSERT(GrCaps::AreConfigsCompatible(backendTex.config(),
687 caps->getConfigFromBackendFormat(
688 backendTex.getBackendFormat(),
689 grColorType)));
690
691 SkASSERT(validate_backend_format_and_colortype(caps, grColorType,
692 backendTex.getBackendFormat()));
Greg Danielfa55f2e2019-06-13 15:21:38 -0400693
Robert Phillipsa41c6852019-02-07 10:44:10 -0500694 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
695
Brian Salomonf7778972018-03-08 10:13:17 -0500696 sk_sp<GrRenderTarget> rt =
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400697 resourceProvider->wrapBackendTextureAsRenderTarget(backendTex, sampleCnt, grColorType);
Brian Salomonf7778972018-03-08 10:13:17 -0500698 if (!rt) {
699 return nullptr;
Greg Danielf87651e2018-02-21 11:36:53 -0500700 }
Brian Salomonf7778972018-03-08 10:13:17 -0500701 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
702 SkASSERT(!rt->getUniqueKey().isValid());
Greg Danielb46add82019-01-02 14:51:29 -0500703 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500704 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielf87651e2018-02-21 11:36:53 -0500705
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400706 GrSwizzle texSwizzle = caps->getTextureSwizzle(rt->backendFormat(), grColorType);
707 GrSwizzle outSwizzle = caps->getOutputSwizzle(rt->backendFormat(), grColorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400708
709 return sk_sp<GrSurfaceProxy>(new GrRenderTargetProxy(std::move(rt), origin, texSwizzle,
710 outSwizzle));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500711}
712
Greg Danielb46add82019-01-02 14:51:29 -0500713sk_sp<GrRenderTargetProxy> GrProxyProvider::wrapVulkanSecondaryCBAsRenderTarget(
714 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
715 if (this->isAbandoned()) {
716 return nullptr;
717 }
718
719 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500720 GrContext* direct = fImageContext->priv().asDirectContext();
721 if (!direct) {
Greg Danielb46add82019-01-02 14:51:29 -0500722 return nullptr;
723 }
724
Robert Phillipsa41c6852019-02-07 10:44:10 -0500725 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Danielb46add82019-01-02 14:51:29 -0500726
Robert Phillipsa41c6852019-02-07 10:44:10 -0500727 sk_sp<GrRenderTarget> rt = resourceProvider->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
728 vkInfo);
Greg Danielb46add82019-01-02 14:51:29 -0500729 if (!rt) {
730 return nullptr;
731 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500732
Greg Danielb46add82019-01-02 14:51:29 -0500733 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
734 SkASSERT(!rt->getUniqueKey().isValid());
735 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500736 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielb46add82019-01-02 14:51:29 -0500737
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400738 GrColorType colorType = GrPixelConfigToColorType(rt->config());
739 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(rt->backendFormat(), colorType);
740 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(rt->backendFormat(), colorType);
741
Greg Danielb46add82019-01-02 14:51:29 -0500742 // All Vulkan surfaces uses top left origins.
743 return sk_sp<GrRenderTargetProxy>(
744 new GrRenderTargetProxy(std::move(rt),
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400745 kTopLeft_GrSurfaceOrigin, texSwizzle, outSwizzle,
Greg Danielb46add82019-01-02 14:51:29 -0500746 GrRenderTargetProxy::WrapsVkSecondaryCB::kYes));
747}
748
Robert Phillips777707b2018-01-17 11:40:14 -0500749sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500750 const GrBackendFormat& format,
Robert Phillips777707b2018-01-17 11:40:14 -0500751 const GrSurfaceDesc& desc,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400752 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400753 int renderTargetSampleCnt,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500754 GrSurfaceOrigin origin,
Brian Salomon7226c232018-07-30 13:13:17 -0400755 GrMipMapped mipMapped,
Brian Salomon7226c232018-07-30 13:13:17 -0400756 SkBackingFit fit,
Brian Salomone8a766b2019-07-19 14:24:36 -0400757 SkBudgeted budgeted,
758 GrProtected isProtected) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400759 return this->createLazyProxy(std::move(callback), format, desc, renderable,
760 renderTargetSampleCnt, origin, mipMapped,
Brian Salomone8a766b2019-07-19 14:24:36 -0400761 GrInternalSurfaceFlags::kNone, fit, budgeted, isProtected);
Greg Daniela8d92112018-03-09 12:05:04 -0500762}
763
764sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -0500765 const GrBackendFormat& format,
Greg Daniela8d92112018-03-09 12:05:04 -0500766 const GrSurfaceDesc& desc,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400767 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400768 int renderTargetSampleCnt,
Greg Daniela8d92112018-03-09 12:05:04 -0500769 GrSurfaceOrigin origin,
770 GrMipMapped mipMapped,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400771 GrInternalSurfaceFlags surfaceFlags,
Brian Salomon7226c232018-07-30 13:13:17 -0400772 SkBackingFit fit,
773 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -0400774 GrProtected isProtected) {
775 // For non-ddl draws always make lazy proxy's single use.
776 LazyInstantiationType lazyType = this->renderingDirectly() ? LazyInstantiationType::kSingleUse
777 : LazyInstantiationType::kMultipleUse;
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400778 return this->createLazyProxy(std::move(callback), format, desc, renderable,
779 renderTargetSampleCnt, origin, mipMapped, surfaceFlags, fit,
780 budgeted, isProtected, lazyType);
Brian Salomone8a766b2019-07-19 14:24:36 -0400781}
782
783sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
784 const GrBackendFormat& format,
785 const GrSurfaceDesc& desc,
786 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400787 int renderTargetSampleCnt,
Brian Salomone8a766b2019-07-19 14:24:36 -0400788 GrSurfaceOrigin origin,
789 GrMipMapped mipMapped,
790 GrInternalSurfaceFlags surfaceFlags,
791 SkBackingFit fit,
792 SkBudgeted budgeted,
793 GrProtected isProtected,
Greg Daniela8d92112018-03-09 12:05:04 -0500794 LazyInstantiationType lazyType) {
Robert Phillips777707b2018-01-17 11:40:14 -0500795 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
796 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400797
Robert Phillipsa41c6852019-02-07 10:44:10 -0500798 if (desc.fWidth > this->caps()->maxTextureSize() ||
799 desc.fHeight > this->caps()->maxTextureSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400800 return nullptr;
801 }
802
Greg Danielfa55f2e2019-06-13 15:21:38 -0400803 SkASSERT(validate_backend_format_and_config(this->caps(), format, desc.fConfig));
Greg Daniel457469c2018-02-08 15:05:44 -0500804
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400805 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
806 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(format, colorType);
807 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(format, colorType);
808
Brian Salomon2a4f9832018-03-03 22:43:43 -0500809 return sk_sp<GrTextureProxy>(
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400810 renderable == GrRenderable::kYes
Greg Daniel4065d452018-11-16 15:43:41 -0500811 ? new GrTextureRenderTargetProxy(std::move(callback), lazyType, format, desc,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400812 renderTargetSampleCnt, origin, mipMapped,
813 texSwizzle, outSwizzle, fit, budgeted,
814 isProtected, surfaceFlags)
Greg Daniel4065d452018-11-16 15:43:41 -0500815 : new GrTextureProxy(std::move(callback), lazyType, format, desc, origin,
Brian Salomone8a766b2019-07-19 14:24:36 -0400816 mipMapped, texSwizzle, fit, budgeted, isProtected,
817 surfaceFlags));
Robert Phillips777707b2018-01-17 11:40:14 -0500818}
819
Robert Phillipse8fabb22018-02-04 14:33:21 -0500820sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500821 LazyInstantiateCallback&& callback, const GrBackendFormat& format,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400822 const GrSurfaceDesc& desc, int sampleCnt, GrSurfaceOrigin origin,
823 GrInternalSurfaceFlags surfaceFlags, const TextureInfo* textureInfo, SkBackingFit fit,
824 SkBudgeted budgeted, GrProtected isProtected, bool wrapsVkSecondaryCB) {
Robert Phillipse8fabb22018-02-04 14:33:21 -0500825 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
826 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400827
Robert Phillipsa41c6852019-02-07 10:44:10 -0500828 if (desc.fWidth > this->caps()->maxRenderTargetSize() ||
829 desc.fHeight > this->caps()->maxRenderTargetSize()) {
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
835 using LazyInstantiationType = GrSurfaceProxy::LazyInstantiationType;
836 // For non-ddl draws always make lazy proxy's single use.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500837 LazyInstantiationType lazyType = this->renderingDirectly() ? LazyInstantiationType::kSingleUse
838 : LazyInstantiationType::kMultipleUse;
Greg Daniel457469c2018-02-08 15:05:44 -0500839
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400840 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
841 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(format, colorType);
842 GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(format, colorType);
843
Brian Salomon7226c232018-07-30 13:13:17 -0400844 if (textureInfo) {
Greg Danielb085fa92019-03-05 16:55:12 -0500845 // Wrapped vulkan secondary command buffers don't support texturing since we won't have an
846 // actual VkImage to texture from.
847 SkASSERT(!wrapsVkSecondaryCB);
Brian Salomon7226c232018-07-30 13:13:17 -0400848 return sk_sp<GrRenderTargetProxy>(new GrTextureRenderTargetProxy(
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400849 std::move(callback), lazyType, format, desc, sampleCnt, origin,
850 textureInfo->fMipMapped, texSwizzle, outSwizzle, fit, budgeted, isProtected,
851 surfaceFlags));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500852 }
853
Greg Danielb085fa92019-03-05 16:55:12 -0500854 GrRenderTargetProxy::WrapsVkSecondaryCB vkSCB =
855 wrapsVkSecondaryCB ? GrRenderTargetProxy::WrapsVkSecondaryCB::kYes
856 : GrRenderTargetProxy::WrapsVkSecondaryCB::kNo;
857
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400858 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
859 std::move(callback), lazyType, format, desc, sampleCnt, origin, texSwizzle, outSwizzle,
860 fit, budgeted, isProtected, surfaceFlags, vkSCB));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500861}
862
Chris Dalton19cc00f2019-04-15 09:06:28 -0600863sk_sp<GrTextureProxy> GrProxyProvider::MakeFullyLazyProxy(
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400864 LazyInstantiateCallback&& callback, const GrBackendFormat& format, GrRenderable renderable,
865 int renderTargetSampleCnt, GrProtected isProtected, GrSurfaceOrigin origin,
866 GrPixelConfig config, const GrCaps& caps) {
867 SkASSERT(renderTargetSampleCnt == 1 || renderable == GrRenderable::kYes);
Greg Danielfa55f2e2019-06-13 15:21:38 -0400868 SkASSERT(validate_backend_format_and_config(&caps, format, config));
Robert Phillips777707b2018-01-17 11:40:14 -0500869 GrSurfaceDesc desc;
Robert Phillips10d17212019-04-24 14:09:10 -0400870 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNone;
Robert Phillips777707b2018-01-17 11:40:14 -0500871 desc.fWidth = -1;
872 desc.fHeight = -1;
873 desc.fConfig = config;
Robert Phillips777707b2018-01-17 11:40:14 -0500874
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400875 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
876 GrSwizzle texSwizzle = caps.getTextureSwizzle(format, colorType);
877 GrSwizzle outSwizzle = caps.getOutputSwizzle(format, colorType);
878
Chris Dalton4c458b12018-06-16 17:22:59 -0600879 return sk_sp<GrTextureProxy>(
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400880 (GrRenderable::kYes == renderable)
Brian Salomon7226c232018-07-30 13:13:17 -0400881 ? new GrTextureRenderTargetProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500882 std::move(callback), LazyInstantiationType::kSingleUse, format, desc,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400883 renderTargetSampleCnt, origin, GrMipMapped::kNo, texSwizzle,
884 outSwizzle, SkBackingFit::kApprox, SkBudgeted::kYes, isProtected,
885 surfaceFlags)
Chris Dalton4c458b12018-06-16 17:22:59 -0600886 : new GrTextureProxy(std::move(callback), LazyInstantiationType::kSingleUse,
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400887 format, desc, origin, GrMipMapped::kNo, texSwizzle,
Brian Salomone8a766b2019-07-19 14:24:36 -0400888 SkBackingFit::kApprox, SkBudgeted::kYes, isProtected,
889 surfaceFlags));
Robert Phillips777707b2018-01-17 11:40:14 -0500890}
891
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500892bool GrProxyProvider::IsFunctionallyExact(GrSurfaceProxy* proxy) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400893 const bool isInstantiated = proxy->isInstantiated();
Robert Phillipsdb3b9792018-06-12 15:18:00 -0400894 // A proxy is functionally exact if:
895 // it is exact (obvs)
896 // when it is instantiated it will be exact (i.e., power of two dimensions)
897 // it is already instantiated and the proxy covers the entire backing surface
898 return proxy->priv().isExact() ||
899 (!isInstantiated && SkIsPow2(proxy->width()) && SkIsPow2(proxy->height())) ||
900 (isInstantiated && proxy->worstCaseWidth() == proxy->width() &&
901 proxy->worstCaseHeight() == proxy->height());
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500902}
903
Robert Phillips427966a2018-12-20 17:20:43 -0500904void GrProxyProvider::processInvalidUniqueKey(const GrUniqueKey& key, GrTextureProxy* proxy,
905 InvalidateGPUResource invalidateGPUResource) {
Chris Dalton2de13dd2019-01-03 15:11:59 -0700906 SkASSERT(key.isValid());
907
Robert Phillips427966a2018-12-20 17:20:43 -0500908 if (!proxy) {
909 proxy = fUniquelyKeyedProxies.find(key);
910 }
Chris Dalton2de13dd2019-01-03 15:11:59 -0700911 SkASSERT(!proxy || proxy->getUniqueKey() == key);
912
913 // Locate the corresponding GrGpuResource (if it needs to be invalidated) before clearing the
914 // proxy's unique key. We must do it in this order because 'key' may alias the proxy's key.
915 sk_sp<GrGpuResource> invalidGpuResource;
916 if (InvalidateGPUResource::kYes == invalidateGPUResource) {
Brian Salomon01ceae92019-04-02 11:49:54 -0400917 GrContext* direct = fImageContext->priv().asDirectContext();
918 if (direct) {
919 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
920 invalidGpuResource = resourceProvider->findByUniqueKey<GrGpuResource>(key);
Chris Dalton2de13dd2019-01-03 15:11:59 -0700921 }
922 SkASSERT(!invalidGpuResource || invalidGpuResource->getUniqueKey() == key);
923 }
Robert Phillips427966a2018-12-20 17:20:43 -0500924
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500925 // Note: this method is called for the whole variety of GrGpuResources so often 'key'
926 // will not be in 'fUniquelyKeyedProxies'.
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500927 if (proxy) {
Robert Phillips427966a2018-12-20 17:20:43 -0500928 fUniquelyKeyedProxies.remove(key);
929 proxy->cacheAccess().clearUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500930 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500931
Chris Dalton2de13dd2019-01-03 15:11:59 -0700932 if (invalidGpuResource) {
933 invalidGpuResource->resourcePriv().removeUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500934 }
935}
936
Robert Phillipsa41c6852019-02-07 10:44:10 -0500937uint32_t GrProxyProvider::contextID() const {
938 return fImageContext->priv().contextID();
939}
940
941const GrCaps* GrProxyProvider::caps() const {
942 return fImageContext->priv().caps();
943}
944
945sk_sp<const GrCaps> GrProxyProvider::refCaps() const {
946 return fImageContext->priv().refCaps();
947}
948
Robert Phillipsa9162df2019-02-11 14:12:03 -0500949bool GrProxyProvider::isAbandoned() const {
950 return fImageContext->priv().abandoned();
951}
952
Robert Phillips0790f8a2018-09-18 13:11:03 -0400953void GrProxyProvider::orphanAllUniqueKeys() {
954 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
955 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
956 GrTextureProxy& tmp = *iter;
957
958 tmp.fProxyProvider = nullptr;
959 }
960}
961
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500962void GrProxyProvider::removeAllUniqueKeys() {
963 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
964 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
965 GrTextureProxy& tmp = *iter;
966
Chris Dalton2de13dd2019-01-03 15:11:59 -0700967 this->processInvalidUniqueKey(tmp.getUniqueKey(), &tmp, InvalidateGPUResource::kNo);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500968 }
969 SkASSERT(!fUniquelyKeyedProxies.count());
970}
Robert Phillipsa41c6852019-02-07 10:44:10 -0500971
972bool GrProxyProvider::renderingDirectly() const {
973 return fImageContext->priv().asDirectContext();
974}