blob: 0f192a55c3c6a2107e16e0a7a3654fcfa9a03c48 [file] [log] [blame]
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001/*
2 * Copyright 2018 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrProxyProvider.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkBitmap.h"
11#include "include/core/SkImage.h"
12#include "include/gpu/GrContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/gpu/GrTexture.h"
14#include "include/private/GrImageContext.h"
15#include "include/private/GrResourceKey.h"
16#include "include/private/GrSingleOwner.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "include/private/SkImageInfoPriv.h"
18#include "src/core/SkAutoPixmapStorage.h"
19#include "src/core/SkImagePriv.h"
20#include "src/core/SkMipMap.h"
21#include "src/core/SkTraceEvent.h"
22#include "src/gpu/GrCaps.h"
23#include "src/gpu/GrContextPriv.h"
24#include "src/gpu/GrImageContextPriv.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040025#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/gpu/GrResourceProvider.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040027#include "src/gpu/GrSurfaceProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/gpu/GrSurfaceProxyPriv.h"
29#include "src/gpu/GrTextureProxyCacheAccess.h"
30#include "src/gpu/GrTextureRenderTargetProxy.h"
31#include "src/gpu/SkGr.h"
32#include "src/image/SkImage_Base.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050033
34#define ASSERT_SINGLE_OWNER \
Robert Phillipsa41c6852019-02-07 10:44:10 -050035 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fImageContext->priv().singleOwner());)
Robert Phillips1afd4cd2018-01-08 13:40:32 -050036
Robert Phillipsa9162df2019-02-11 14:12:03 -050037GrProxyProvider::GrProxyProvider(GrImageContext* imageContext) : fImageContext(imageContext) {}
Robert Phillips1afd4cd2018-01-08 13:40:32 -050038
39GrProxyProvider::~GrProxyProvider() {
Robert Phillipsa41c6852019-02-07 10:44:10 -050040 if (this->renderingDirectly()) {
Robert Phillips0790f8a2018-09-18 13:11:03 -040041 // In DDL-mode a proxy provider can still have extant uniquely keyed proxies (since
42 // they need their unique keys to, potentially, find a cached resource when the
43 // DDL is played) but, in non-DDL-mode they should all have been cleaned up by this point.
44 SkASSERT(!fUniquelyKeyedProxies.count());
45 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050046}
47
Robert Phillipsadbe1322018-01-17 13:35:46 -050048bool GrProxyProvider::assignUniqueKeyToProxy(const GrUniqueKey& key, GrTextureProxy* proxy) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050049 ASSERT_SINGLE_OWNER
50 SkASSERT(key.isValid());
51 if (this->isAbandoned() || !proxy) {
Robert Phillipsadbe1322018-01-17 13:35:46 -050052 return false;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050053 }
54
Robert Phillipsa41c6852019-02-07 10:44:10 -050055#ifdef SK_DEBUG
56 {
57 GrContext* direct = fImageContext->priv().asDirectContext();
58 if (direct) {
59 GrResourceCache* resourceCache = direct->priv().getResourceCache();
60 // If there is already a GrResource with this key then the caller has violated the
61 // normal usage pattern of uniquely keyed resources (e.g., they have created one w/o
62 // first seeing if it already existed in the cache).
63 SkASSERT(!resourceCache->findAndRefUniqueResource(key));
64 }
65 }
66#endif
Robert Phillips1afd4cd2018-01-08 13:40:32 -050067
Robert Phillips1afd4cd2018-01-08 13:40:32 -050068 SkASSERT(!fUniquelyKeyedProxies.find(key)); // multiple proxies can't get the same key
69
70 proxy->cacheAccess().setUniqueKey(this, key);
71 SkASSERT(proxy->getUniqueKey() == key);
72 fUniquelyKeyedProxies.add(proxy);
Robert Phillipsadbe1322018-01-17 13:35:46 -050073 return true;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050074}
75
76void GrProxyProvider::adoptUniqueKeyFromSurface(GrTextureProxy* proxy, const GrSurface* surf) {
77 SkASSERT(surf->getUniqueKey().isValid());
78 proxy->cacheAccess().setUniqueKey(this, surf->getUniqueKey());
79 SkASSERT(proxy->getUniqueKey() == surf->getUniqueKey());
80 // multiple proxies can't get the same key
81 SkASSERT(!fUniquelyKeyedProxies.find(surf->getUniqueKey()));
82 fUniquelyKeyedProxies.add(proxy);
83}
84
Chris Dalton2de13dd2019-01-03 15:11:59 -070085void GrProxyProvider::removeUniqueKeyFromProxy(GrTextureProxy* proxy) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050086 ASSERT_SINGLE_OWNER
Chris Dalton2de13dd2019-01-03 15:11:59 -070087 SkASSERT(proxy);
88 SkASSERT(proxy->getUniqueKey().isValid());
89
90 if (this->isAbandoned()) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050091 return;
92 }
Robert Phillips0790f8a2018-09-18 13:11:03 -040093
Chris Dalton2de13dd2019-01-03 15:11:59 -070094 this->processInvalidUniqueKey(proxy->getUniqueKey(), proxy, InvalidateGPUResource::kYes);
Robert Phillips1afd4cd2018-01-08 13:40:32 -050095}
96
97sk_sp<GrTextureProxy> GrProxyProvider::findProxyByUniqueKey(const GrUniqueKey& key,
98 GrSurfaceOrigin origin) {
99 ASSERT_SINGLE_OWNER
100
101 if (this->isAbandoned()) {
102 return nullptr;
103 }
104
Brian Salomon01ceae92019-04-02 11:49:54 -0400105 GrTextureProxy* proxy = fUniquelyKeyedProxies.find(key);
Brian Salomon01ceae92019-04-02 11:49:54 -0400106 if (proxy) {
Robert Phillipse5f73282019-06-18 17:15:04 -0400107 SkASSERT(proxy->origin() == origin);
108 return sk_ref_sp(proxy);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500109 }
Robert Phillipse5f73282019-06-18 17:15:04 -0400110 return nullptr;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500111}
112
Robert Phillipsa41c6852019-02-07 10:44:10 -0500113///////////////////////////////////////////////////////////////////////////////
114
115#if GR_TEST_UTILS
116sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createInstantiatedProxy(
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400117 const SkISize& dimensions,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400118 GrColorType colorType,
119 const GrBackendFormat& format,
120 GrRenderable renderable,
121 int renderTargetSampleCnt,
122 GrSurfaceOrigin origin,
123 SkBackingFit fit,
124 SkBudgeted budgeted,
125 GrProtected isProtected) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500126 GrContext* direct = fImageContext->priv().asDirectContext();
127 if (!direct) {
128 return nullptr;
129 }
130
Brian Salomon4eb38b72019-08-05 12:58:39 -0400131 if (this->caps()->isFormatCompressed(format)) {
132 // TODO: Allow this to go to GrResourceProvider::createCompressedTexture() once we no longer
133 // rely on GrColorType to get to GrPixelConfig. Currently this will cause
134 // makeConfigSpecific() to assert because GrColorTypeToPixelConfig() never returns a
135 // compressed GrPixelConfig.
136 return nullptr;
137 }
138 GrSurfaceDesc desc;
139 desc.fConfig = GrColorTypeToPixelConfig(colorType);
140 desc.fConfig = this->caps()->makeConfigSpecific(desc.fConfig, format);
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400141 desc.fWidth = dimensions.width();
142 desc.fHeight = dimensions.height();
Brian Salomon4eb38b72019-08-05 12:58:39 -0400143
Robert Phillipsa41c6852019-02-07 10:44:10 -0500144 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
145 sk_sp<GrTexture> tex;
146
147 if (SkBackingFit::kApprox == fit) {
Brian Salomon4eb38b72019-08-05 12:58:39 -0400148 tex = resourceProvider->createApproxTexture(desc, format, renderable, renderTargetSampleCnt,
Robert Phillipsaee18c92019-09-06 11:48:27 -0400149 isProtected);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500150 } else {
Brian Salomon4eb38b72019-08-05 12:58:39 -0400151 tex = resourceProvider->createTexture(desc, format, renderable, renderTargetSampleCnt,
Brian Salomona90382f2019-09-17 09:01:56 -0400152 GrMipMapped::kNo, budgeted, isProtected);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500153 }
154 if (!tex) {
155 return nullptr;
156 }
157
Brian Salomonbeb7f522019-08-30 16:19:42 -0400158 return this->createWrapped(std::move(tex), colorType, origin, UseAllocator::kYes);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500159}
160
Brian Salomon4eb38b72019-08-05 12:58:39 -0400161sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createInstantiatedProxy(
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400162 const SkISize& dimensions,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400163 GrColorType colorType,
164 GrRenderable renderable,
165 int renderTargetSampleCnt,
166 GrSurfaceOrigin origin,
167 SkBackingFit fit,
168 SkBudgeted budgeted,
169 GrProtected isProtected) {
170 auto format = this->caps()->getDefaultBackendFormat(colorType, renderable);
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400171 return this->testingOnly_createInstantiatedProxy(dimensions,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400172 colorType,
173 format,
174 renderable,
175 renderTargetSampleCnt,
176 origin,
177 fit,
178 budgeted,
179 isProtected);
180}
181
Robert Phillipsa41c6852019-02-07 10:44:10 -0500182sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createWrapped(sk_sp<GrTexture> tex,
Brian Salomon2af3e702019-08-11 19:10:31 -0400183 GrColorType colorType,
Robert Phillipsa41c6852019-02-07 10:44:10 -0500184 GrSurfaceOrigin origin) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400185 return this->createWrapped(std::move(tex), colorType, origin, UseAllocator::kYes);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500186}
187#endif
188
Brian Salomonbeb7f522019-08-30 16:19:42 -0400189sk_sp<GrTextureProxy> GrProxyProvider::createWrapped(sk_sp<GrTexture> tex,
190 GrColorType colorType,
191 GrSurfaceOrigin origin,
192 UseAllocator useAllocator) {
Robert Phillipsadbe1322018-01-17 13:35:46 -0500193#ifdef SK_DEBUG
194 if (tex->getUniqueKey().isValid()) {
195 SkASSERT(!this->findProxyByUniqueKey(tex->getUniqueKey(), origin));
196 }
197#endif
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400198 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(tex->backendFormat(), colorType);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500199
200 if (tex->asRenderTarget()) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400201 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
Greg Danielbaf8d992019-10-29 14:14:32 -0400202 std::move(tex), origin, texSwizzle, useAllocator));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500203 } else {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400204 return sk_sp<GrTextureProxy>(
205 new GrTextureProxy(std::move(tex), origin, texSwizzle, useAllocator));
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,
Brian Salomon2af3e702019-08-11 19:10:31 -0400210 GrColorType colorType,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400211 GrSurfaceOrigin origin,
212 UseAllocator useAllocator) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500213 ASSERT_SINGLE_OWNER
214
215 if (this->isAbandoned()) {
216 return nullptr;
217 }
218
219 sk_sp<GrTextureProxy> result = this->findProxyByUniqueKey(key, origin);
220 if (result) {
221 return result;
222 }
223
Robert Phillipsa41c6852019-02-07 10:44:10 -0500224 GrContext* direct = fImageContext->priv().asDirectContext();
225 if (!direct) {
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500226 return nullptr;
227 }
228
Robert Phillipsa41c6852019-02-07 10:44:10 -0500229 GrResourceCache* resourceCache = direct->priv().getResourceCache();
230
231 GrGpuResource* resource = resourceCache->findAndRefUniqueResource(key);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500232 if (!resource) {
233 return nullptr;
234 }
235
236 sk_sp<GrTexture> texture(static_cast<GrSurface*>(resource)->asTexture());
237 SkASSERT(texture);
238
Brian Salomonbeb7f522019-08-30 16:19:42 -0400239 result = this->createWrapped(std::move(texture), colorType, origin, useAllocator);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500240 SkASSERT(result->getUniqueKey() == key);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500241 // createWrapped should've added this for us
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500242 SkASSERT(fUniquelyKeyedProxies.find(key));
Brian Salomon2af3e702019-08-11 19:10:31 -0400243 SkASSERT(result->textureSwizzle() ==
244 this->caps()->getTextureSwizzle(result->backendFormat(), colorType));
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500245 return result;
246}
247
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500248sk_sp<GrTextureProxy> GrProxyProvider::createTextureProxy(sk_sp<SkImage> srcImage,
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500249 int sampleCnt,
Greg Danielfb3abcd2018-02-02 15:48:33 -0500250 SkBudgeted budgeted,
Chris Daltond004e0b2018-09-27 09:28:03 -0600251 SkBackingFit fit,
252 GrInternalSurfaceFlags surfaceFlags) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500253 ASSERT_SINGLE_OWNER
254 SkASSERT(srcImage);
255
256 if (this->isAbandoned()) {
257 return nullptr;
258 }
259
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400260 const SkImageInfo& info = srcImage->imageInfo();
Brian Salomon2af3e702019-08-11 19:10:31 -0400261 GrColorType ct = SkColorTypeToGrColorType(info.colorType());
Greg Danielf87651e2018-02-21 11:36:53 -0500262
Brian Salomon96b383a2019-08-13 16:55:41 -0400263 GrBackendFormat format = this->caps()->getDefaultBackendFormat(ct, GrRenderable::kNo);
Greg Daniel0a7aa142018-02-21 13:02:32 -0500264
Robert Phillips0a15cc62019-07-30 12:49:10 -0400265 if (!format.isValid()) {
Brian Osmand29dcd12018-09-13 15:04:29 -0400266 SkBitmap copy8888;
267 if (!copy8888.tryAllocPixels(info.makeColorType(kRGBA_8888_SkColorType)) ||
268 !srcImage->readPixels(copy8888.pixmap(), 0, 0)) {
269 return nullptr;
270 }
271 copy8888.setImmutable();
272 srcImage = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode);
Brian Salomon2af3e702019-08-11 19:10:31 -0400273 ct = GrColorType::kRGBA_8888;
Brian Salomon96b383a2019-08-13 16:55:41 -0400274 format = this->caps()->getDefaultBackendFormat(ct, GrRenderable::kNo);
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400275 if (!format.isValid()) {
276 return nullptr;
277 }
Brian Osmand29dcd12018-09-13 15:04:29 -0400278 }
279
Brian Salomon2af3e702019-08-11 19:10:31 -0400280 GrPixelConfig config = GrColorTypeToPixelConfig(ct);
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400281 if (kUnknown_GrPixelConfig == config) {
282 return nullptr;
283 }
284
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500285 GrSurfaceDesc desc;
286 desc.fWidth = srcImage->width();
287 desc.fHeight = srcImage->height();
Greg Danielf87651e2018-02-21 11:36:53 -0500288 desc.fConfig = config;
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500289
290 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomon96b383a2019-08-13 16:55:41 -0400291 [desc, format, sampleCnt, budgeted, srcImage, fit,
Brian Salomon2af3e702019-08-11 19:10:31 -0400292 ct](GrResourceProvider* resourceProvider) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500293 SkPixmap pixMap;
294 SkAssertResult(srcImage->peekPixels(&pixMap));
295 GrMipLevel mipLevel = { pixMap.addr(), pixMap.rowBytes() };
296
Brian Salomonbeb7f522019-08-30 16:19:42 -0400297 return LazyCallbackResult(resourceProvider->createTexture(
Brian Salomona90382f2019-09-17 09:01:56 -0400298 desc, format, ct, GrRenderable::kNo, sampleCnt, budgeted, fit,
299 GrProtected::kNo, mipLevel));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500300 },
Brian Salomon96b383a2019-08-13 16:55:41 -0400301 format, desc, GrRenderable::kNo, sampleCnt, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400302 GrMipMapsStatus::kNotAllocated, surfaceFlags, fit, budgeted, GrProtected::kNo,
303 UseAllocator::kYes);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500304
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400305 if (!proxy) {
306 return nullptr;
307 }
308
Robert Phillipsa41c6852019-02-07 10:44:10 -0500309 GrContext* direct = fImageContext->priv().asDirectContext();
310 if (direct) {
311 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
312
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500313 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
314 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500315 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500316 return nullptr;
317 }
318 }
Robert Phillipsc1b60662018-06-26 10:20:08 -0400319
320 SkASSERT(proxy->width() == desc.fWidth);
321 SkASSERT(proxy->height() == desc.fHeight);
Greg Daniel9d86f1d2018-01-29 09:33:59 -0500322 return proxy;
323}
324
Brian Osmande496652019-03-22 13:42:33 -0400325sk_sp<GrTextureProxy> GrProxyProvider::createProxyFromBitmap(const SkBitmap& bitmap,
326 GrMipMapped mipMapped) {
Brian Osman412674f2019-02-07 15:34:58 -0500327 ASSERT_SINGLE_OWNER
Brian Osman7dcc6162019-03-25 10:12:57 -0400328 SkASSERT(GrMipMapped::kNo == mipMapped || this->caps()->mipMapSupport());
Brian Osman412674f2019-02-07 15:34:58 -0500329
330 if (this->isAbandoned()) {
331 return nullptr;
332 }
333
Brian Osman2b23c4b2018-06-01 12:25:08 -0400334 if (!SkImageInfoIsValid(bitmap.info())) {
Greg Daniela4ead652018-02-07 10:21:48 -0500335 return nullptr;
336 }
337
Brian Osmande496652019-03-22 13:42:33 -0400338 ATRACE_ANDROID_FRAMEWORK("Upload %sTexture [%ux%u]",
339 GrMipMapped::kYes == mipMapped ? "MipMap " : "",
340 bitmap.width(), bitmap.height());
Greg Daniela4ead652018-02-07 10:21:48 -0500341
342 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
343 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
344 // 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 -0500345 SkCopyPixelsMode copyMode = this->renderingDirectly() ? kNever_SkCopyPixelsMode
346 : kIfMutable_SkCopyPixelsMode;
Greg Daniela4ead652018-02-07 10:21:48 -0500347 sk_sp<SkImage> baseLevel = SkMakeImageFromRasterBitmap(bitmap, copyMode);
Greg Daniela4ead652018-02-07 10:21:48 -0500348 if (!baseLevel) {
349 return nullptr;
350 }
351
Brian Osmande496652019-03-22 13:42:33 -0400352 // If mips weren't requested (or this was too small to have any), then take the fast path
353 if (GrMipMapped::kNo == mipMapped ||
354 0 == SkMipMap::ComputeLevelCount(baseLevel->width(), baseLevel->height())) {
Brian Salomon96b383a2019-08-13 16:55:41 -0400355 return this->createTextureProxy(std::move(baseLevel), 1, SkBudgeted::kYes,
356 SkBackingFit::kExact);
Greg Daniela4ead652018-02-07 10:21:48 -0500357 }
358
Brian Osmand29dcd12018-09-13 15:04:29 -0400359 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info());
Greg Daniel3fc5df42019-06-14 09:42:17 -0400360
Brian Salomona90382f2019-09-17 09:01:56 -0400361 GrBackendFormat format = this->caps()->getDefaultBackendFormat(
362 SkColorTypeToGrColorType(bitmap.info().colorType()), GrRenderable::kNo);
Robert Phillips0a15cc62019-07-30 12:49:10 -0400363 if (!format.isValid()) {
Brian Osmand29dcd12018-09-13 15:04:29 -0400364 SkBitmap copy8888;
365 if (!copy8888.tryAllocPixels(bitmap.info().makeColorType(kRGBA_8888_SkColorType)) ||
366 !bitmap.readPixels(copy8888.pixmap())) {
367 return nullptr;
368 }
369 copy8888.setImmutable();
370 baseLevel = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode);
371 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Salomona90382f2019-09-17 09:01:56 -0400372 format = this->caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888, GrRenderable::kNo);
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400373 if (!format.isValid()) {
374 return nullptr;
375 }
Greg Daniel3fc5df42019-06-14 09:42:17 -0400376 }
377
Brian Osmand29dcd12018-09-13 15:04:29 -0400378 SkPixmap pixmap;
379 SkAssertResult(baseLevel->peekPixels(&pixmap));
380 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(pixmap, nullptr));
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400381 if (!mipmaps) {
382 return nullptr;
383 }
384
Greg Daniela4ead652018-02-07 10:21:48 -0500385 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomon4eb38b72019-08-05 12:58:39 -0400386 [desc, format, baseLevel, mipmaps](GrResourceProvider* resourceProvider) {
Brian Osman1b97f132018-09-13 17:33:48 +0000387 const int mipLevelCount = mipmaps->countLevels() + 1;
388 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
389
Greg Daniela4ead652018-02-07 10:21:48 -0500390 SkPixmap pixmap;
391 SkAssertResult(baseLevel->peekPixels(&pixmap));
392
Greg Daniela4ead652018-02-07 10:21:48 -0500393 texels[0].fPixels = pixmap.addr();
394 texels[0].fRowBytes = pixmap.rowBytes();
395
Brian Salomona90382f2019-09-17 09:01:56 -0400396 auto colorType = SkColorTypeToGrColorType(pixmap.colorType());
Greg Daniela4ead652018-02-07 10:21:48 -0500397 for (int i = 1; i < mipLevelCount; ++i) {
398 SkMipMap::Level generatedMipLevel;
399 mipmaps->getLevel(i - 1, &generatedMipLevel);
400 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
401 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
402 SkASSERT(texels[i].fPixels);
Brian Salomona90382f2019-09-17 09:01:56 -0400403 SkASSERT(generatedMipLevel.fPixmap.colorType() == pixmap.colorType());
Greg Daniela4ead652018-02-07 10:21:48 -0500404 }
Brian Salomonbeb7f522019-08-30 16:19:42 -0400405 return LazyCallbackResult(resourceProvider->createTexture(
Brian Salomona90382f2019-09-17 09:01:56 -0400406 desc, format, colorType, GrRenderable::kNo, 1, SkBudgeted::kYes,
407 GrProtected::kNo, texels.get(), mipLevelCount));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500408 },
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400409 format, desc, GrRenderable::kNo, 1, kTopLeft_GrSurfaceOrigin, GrMipMapped::kYes,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400410 GrMipMapsStatus::kValid, GrInternalSurfaceFlags::kNone, SkBackingFit::kExact,
411 SkBudgeted::kYes, GrProtected::kNo, UseAllocator::kYes);
Greg Daniela4ead652018-02-07 10:21:48 -0500412
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400413 if (!proxy) {
414 return nullptr;
415 }
416
Robert Phillipsa41c6852019-02-07 10:44:10 -0500417 GrContext* direct = fImageContext->priv().asDirectContext();
418 if (direct) {
419 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Daniela4ead652018-02-07 10:21:48 -0500420 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
421 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500422 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Greg Daniela4ead652018-02-07 10:21:48 -0500423 return nullptr;
424 }
425 }
426 return proxy;
427}
428
Greg Daniel4065d452018-11-16 15:43:41 -0500429sk_sp<GrTextureProxy> GrProxyProvider::createProxy(const GrBackendFormat& format,
430 const GrSurfaceDesc& desc,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400431 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400432 int renderTargetSampleCnt,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500433 GrSurfaceOrigin origin,
Greg Danielf6f7b672018-02-15 13:06:26 -0500434 GrMipMapped mipMapped,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500435 SkBackingFit fit,
436 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -0400437 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400438 GrInternalSurfaceFlags surfaceFlags,
439 GrSurfaceProxy::UseAllocator useAllocator) {
Robert Phillips8ff8bcc2019-07-29 17:03:35 -0400440 const GrCaps* caps = this->caps();
441
442 if (caps->isFormatCompressed(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400443 // Deferred proxies for compressed textures are not supported.
444 return nullptr;
445 }
Robert Phillips0902c982019-07-16 07:47:56 -0400446
Robert Phillips0902c982019-07-16 07:47:56 -0400447 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
448
449 SkASSERT(GrCaps::AreConfigsCompatible(desc.fConfig,
450 caps->getConfigFromBackendFormat(format, colorType)));
Robert Phillips62221e72019-07-24 15:07:38 -0400451
Greg Danielf6f7b672018-02-15 13:06:26 -0500452 if (GrMipMapped::kYes == mipMapped) {
453 // SkMipMap doesn't include the base level in the level count so we have to add 1
454 int mipCount = SkMipMap::ComputeLevelCount(desc.fWidth, desc.fHeight) + 1;
455 if (1 == mipCount) {
456 mipMapped = GrMipMapped::kNo;
457 }
458 }
459
Greg Daniel6fa62e22019-08-07 15:52:37 -0400460 if (!caps->validateSurfaceParams({desc.fWidth, desc.fHeight}, format, desc.fConfig, renderable,
461 renderTargetSampleCnt, mipMapped)) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500462 return nullptr;
463 }
Brian Salomon3a2cc2c2018-02-03 00:25:12 +0000464 GrSurfaceDesc copyDesc = desc;
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600465 GrMipMapsStatus mipMapsStatus = (GrMipMapped::kYes == mipMapped)
466 ? GrMipMapsStatus::kDirty
467 : GrMipMapsStatus::kNotAllocated;
Robert Phillips0902c982019-07-16 07:47:56 -0400468 GrSwizzle texSwizzle = caps->getTextureSwizzle(format, colorType);
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400469 if (renderable == GrRenderable::kYes) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400470 renderTargetSampleCnt =
Greg Daniel6fa62e22019-08-07 15:52:37 -0400471 caps->getRenderTargetSampleCount(renderTargetSampleCnt, format);
472 SkASSERT(renderTargetSampleCnt);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500473 // We know anything we instantiate later from this deferred path will be
474 // both texturable and renderable
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400475 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600476 *caps, format, copyDesc, renderTargetSampleCnt, origin, mipMapped, mipMapsStatus,
Greg Danielbaf8d992019-10-29 14:14:32 -0400477 texSwizzle, fit, budgeted, isProtected, surfaceFlags, useAllocator));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500478 }
479
Brian Salomonbeb7f522019-08-30 16:19:42 -0400480 return sk_sp<GrTextureProxy>(new GrTextureProxy(format, copyDesc, origin, mipMapped,
481 mipMapsStatus, texSwizzle, fit, budgeted,
482 isProtected, surfaceFlags, useAllocator));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500483}
484
Brian Salomonbb8dde82019-06-27 10:52:13 -0400485sk_sp<GrTextureProxy> GrProxyProvider::createCompressedTextureProxy(
486 int width, int height, SkBudgeted budgeted, SkImage::CompressionType compressionType,
487 sk_sp<SkData> data) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400488
489 GrSurfaceDesc desc;
Robert Phillipsa27d6252019-12-10 14:48:36 -0500490 desc.fConfig = GrCompressionTypeToPixelConfig(compressionType);
Brian Salomonbb8dde82019-06-27 10:52:13 -0400491 desc.fWidth = width;
492 desc.fHeight = height;
493
Robert Phillips8ff8bcc2019-07-29 17:03:35 -0400494 GrBackendFormat format = this->caps()->getBackendFormatFromCompressionType(compressionType);
495
Greg Daniel7bfc9132019-08-14 14:23:53 -0400496 if (!this->caps()->isFormatTexturable(format)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500497 return nullptr;
498 }
499
Jim Van Verthee06b332019-01-18 10:36:32 -0500500 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomonbeb7f522019-08-30 16:19:42 -0400501 [width, height, format, compressionType, budgeted,
502 data](GrResourceProvider* resourceProvider) {
503 return LazyCallbackResult(resourceProvider->createCompressedTexture(
Greg Daniel7bfc9132019-08-14 14:23:53 -0400504 width, height, format, compressionType, budgeted, data.get()));
Brian Salomonbb8dde82019-06-27 10:52:13 -0400505 },
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400506 format, desc, GrRenderable::kNo, 1, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo,
Greg Danielc3a927f2019-10-16 12:03:50 -0400507 GrMipMapsStatus::kNotAllocated, GrInternalSurfaceFlags::kReadOnly, SkBackingFit::kExact,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400508 SkBudgeted::kYes, GrProtected::kNo, UseAllocator::kYes);
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
Robert Phillipsa41c6852019-02-07 10:44:10 -0500547 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
548
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500549 sk_sp<GrTexture> tex =
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400550 resourceProvider->wrapBackendTexture(backendTex, grColorType,
551 ownership, cacheable, ioType);
Brian Salomonf7778972018-03-08 10:13:17 -0500552 if (!tex) {
553 return nullptr;
554 }
Robert Phillipsadbe1322018-01-17 13:35:46 -0500555
Greg Daniel6a0176b2018-01-30 09:28:44 -0500556 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500557 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel6a0176b2018-01-30 09:28:44 -0500558 }
559
Brian Salomonf7778972018-03-08 10:13:17 -0500560 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
561 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500562 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500563
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400564 GrSwizzle texSwizzle = caps->getTextureSwizzle(tex->backendFormat(), grColorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400565
Brian Salomonbeb7f522019-08-30 16:19:42 -0400566 return sk_sp<GrTextureProxy>(
567 new GrTextureProxy(std::move(tex), origin, texSwizzle, UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500568}
569
Brian Salomon7578f3e2018-03-07 14:39:54 -0500570sk_sp<GrTextureProxy> GrProxyProvider::wrapRenderableBackendTexture(
Brian Salomon02bd2952018-03-07 15:20:21 -0500571 const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400572 GrColorType colorType, GrWrapOwnership ownership, GrWrapCacheable cacheable,
573 ReleaseProc releaseProc, ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500574 if (this->isAbandoned()) {
575 return nullptr;
576 }
577
Brian Salomonf7778972018-03-08 10:13:17 -0500578 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500579 GrContext* direct = fImageContext->priv().asDirectContext();
580 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500581 return nullptr;
582 }
583
Robert Phillips0902c982019-07-16 07:47:56 -0400584 const GrCaps* caps = this->caps();
585
Robert Phillipsa41c6852019-02-07 10:44:10 -0500586 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
587
Greg Daniel6fa62e22019-08-07 15:52:37 -0400588 // TODO: This should have been checked and validated before getting into GrProxyProvider.
589 if (!caps->isFormatAsColorTypeRenderable(colorType, backendTex.getBackendFormat(), sampleCnt)) {
Greg Danielf87651e2018-02-21 11:36:53 -0500590 return nullptr;
591 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500592
Greg Daniel6fa62e22019-08-07 15:52:37 -0400593 sampleCnt = caps->getRenderTargetSampleCount(sampleCnt, backendTex.getBackendFormat());
594 SkASSERT(sampleCnt);
595
Robert Phillipsa41c6852019-02-07 10:44:10 -0500596 sk_sp<GrTexture> tex = resourceProvider->wrapRenderableBackendTexture(backendTex, sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400597 colorType, ownership,
598 cacheable);
Brian Salomonf7778972018-03-08 10:13:17 -0500599 if (!tex) {
600 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500601 }
602
Greg Daniel8ce79912019-02-05 10:08:43 -0500603 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500604 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500605 }
606
Brian Salomonf7778972018-03-08 10:13:17 -0500607 SkASSERT(tex->asRenderTarget()); // A GrTextureRenderTarget
608 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500609 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Greg Daniel6abda432018-02-15 14:55:00 -0500610
Robert Phillips0902c982019-07-16 07:47:56 -0400611 GrSwizzle texSwizzle = caps->getTextureSwizzle(tex->backendFormat(), colorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400612
613 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin, texSwizzle,
Greg Danielbaf8d992019-10-29 14:14:32 -0400614 UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500615}
616
Brian Salomon7578f3e2018-03-07 14:39:54 -0500617sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400618 const GrBackendRenderTarget& backendRT, GrColorType grColorType,
619 GrSurfaceOrigin origin, ReleaseProc releaseProc, ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500620 if (this->isAbandoned()) {
621 return nullptr;
622 }
623
Brian Salomonf7778972018-03-08 10:13:17 -0500624 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500625 GrContext* direct = fImageContext->priv().asDirectContext();
626 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500627 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500628 }
629
Robert Phillips62221e72019-07-24 15:07:38 -0400630 const GrCaps* caps = this->caps();
631
Robert Phillipsa41c6852019-02-07 10:44:10 -0500632 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
633
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400634 sk_sp<GrRenderTarget> rt = resourceProvider->wrapBackendRenderTarget(backendRT, grColorType);
Brian Salomonf7778972018-03-08 10:13:17 -0500635 if (!rt) {
636 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500637 }
Greg Daniel8ce79912019-02-05 10:08:43 -0500638
Greg Daniel8ce79912019-02-05 10:08:43 -0500639 if (releaseProc) {
Brian Salomon2ca31f82019-03-05 13:28:58 -0500640 rt->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500641 }
642
Brian Salomonf7778972018-03-08 10:13:17 -0500643 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
644 SkASSERT(!rt->getUniqueKey().isValid());
645 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500646 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Brian Salomonf7778972018-03-08 10:13:17 -0500647
Robert Phillips62221e72019-07-24 15:07:38 -0400648 GrSwizzle texSwizzle = caps->getTextureSwizzle(rt->backendFormat(), grColorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400649
650 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(std::move(rt), origin, texSwizzle,
Greg Danielbaf8d992019-10-29 14:14:32 -0400651 UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500652}
653
Brian Salomon7578f3e2018-03-07 14:39:54 -0500654sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendTextureAsRenderTarget(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400655 const GrBackendTexture& backendTex, GrColorType grColorType,
656 GrSurfaceOrigin origin, int sampleCnt) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500657 if (this->isAbandoned()) {
658 return nullptr;
659 }
660
Brian Salomonf7778972018-03-08 10:13:17 -0500661 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500662 GrContext* direct = fImageContext->priv().asDirectContext();
663 if (!direct) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500664 return nullptr;
665 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500666
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400667 const GrCaps* caps = this->caps();
668
Robert Phillipsa41c6852019-02-07 10:44:10 -0500669 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
670
Brian Salomonf7778972018-03-08 10:13:17 -0500671 sk_sp<GrRenderTarget> rt =
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400672 resourceProvider->wrapBackendTextureAsRenderTarget(backendTex, sampleCnt, grColorType);
Brian Salomonf7778972018-03-08 10:13:17 -0500673 if (!rt) {
674 return nullptr;
Greg Danielf87651e2018-02-21 11:36:53 -0500675 }
Brian Salomonf7778972018-03-08 10:13:17 -0500676 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
677 SkASSERT(!rt->getUniqueKey().isValid());
Greg Danielb46add82019-01-02 14:51:29 -0500678 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500679 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielf87651e2018-02-21 11:36:53 -0500680
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400681 GrSwizzle texSwizzle = caps->getTextureSwizzle(rt->backendFormat(), grColorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400682
683 return sk_sp<GrSurfaceProxy>(new GrRenderTargetProxy(std::move(rt), origin, texSwizzle,
Greg Danielbaf8d992019-10-29 14:14:32 -0400684 UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500685}
686
Greg Danielb46add82019-01-02 14:51:29 -0500687sk_sp<GrRenderTargetProxy> GrProxyProvider::wrapVulkanSecondaryCBAsRenderTarget(
688 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
689 if (this->isAbandoned()) {
690 return nullptr;
691 }
692
693 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500694 GrContext* direct = fImageContext->priv().asDirectContext();
695 if (!direct) {
Greg Danielb46add82019-01-02 14:51:29 -0500696 return nullptr;
697 }
698
Robert Phillipsa41c6852019-02-07 10:44:10 -0500699 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Danielb46add82019-01-02 14:51:29 -0500700
Robert Phillipsa41c6852019-02-07 10:44:10 -0500701 sk_sp<GrRenderTarget> rt = resourceProvider->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
702 vkInfo);
Greg Danielb46add82019-01-02 14:51:29 -0500703 if (!rt) {
704 return nullptr;
705 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500706
Greg Danielb46add82019-01-02 14:51:29 -0500707 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
708 SkASSERT(!rt->getUniqueKey().isValid());
709 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500710 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielb46add82019-01-02 14:51:29 -0500711
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400712 GrColorType colorType = SkColorTypeToGrColorType(imageInfo.colorType());
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400713 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(rt->backendFormat(), colorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400714
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400715 if (!this->caps()->isFormatAsColorTypeRenderable(colorType, rt->backendFormat(),
716 rt->numSamples())) {
717 return nullptr;
718 }
719
Greg Danielb46add82019-01-02 14:51:29 -0500720 // All Vulkan surfaces uses top left origins.
Brian Salomonbeb7f522019-08-30 16:19:42 -0400721 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
Greg Danielbaf8d992019-10-29 14:14:32 -0400722 std::move(rt), kTopLeft_GrSurfaceOrigin, texSwizzle, UseAllocator::kNo,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400723 GrRenderTargetProxy::WrapsVkSecondaryCB::kYes));
Brian Salomone8a766b2019-07-19 14:24:36 -0400724}
725
726sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
727 const GrBackendFormat& format,
728 const GrSurfaceDesc& desc,
729 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400730 int renderTargetSampleCnt,
Brian Salomone8a766b2019-07-19 14:24:36 -0400731 GrSurfaceOrigin origin,
732 GrMipMapped mipMapped,
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600733 GrMipMapsStatus mipMapsStatus,
Brian Salomone8a766b2019-07-19 14:24:36 -0400734 GrInternalSurfaceFlags surfaceFlags,
735 SkBackingFit fit,
736 SkBudgeted budgeted,
737 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400738 GrSurfaceProxy::UseAllocator useAllocator) {
Robert Phillips777707b2018-01-17 11:40:14 -0500739 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
740 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400741
Robert Phillips0a15cc62019-07-30 12:49:10 -0400742 if (!format.isValid()) {
743 return nullptr;
744 }
745
Robert Phillipsa41c6852019-02-07 10:44:10 -0500746 if (desc.fWidth > this->caps()->maxTextureSize() ||
747 desc.fHeight > this->caps()->maxTextureSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400748 return nullptr;
749 }
750
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400751 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
752 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(format, colorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400753
Brian Salomonbeb7f522019-08-30 16:19:42 -0400754 if (renderable == GrRenderable::kYes) {
755 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(*this->caps(),
756 std::move(callback),
757 format,
758 desc,
759 renderTargetSampleCnt,
760 origin,
761 mipMapped,
762 mipMapsStatus,
763 texSwizzle,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400764 fit,
765 budgeted,
766 isProtected,
767 surfaceFlags,
768 useAllocator));
769 } else {
770 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(callback),
771 format,
772 desc,
773 origin,
774 mipMapped,
775 mipMapsStatus,
776 texSwizzle,
777 fit,
778 budgeted,
779 isProtected,
780 surfaceFlags,
781 useAllocator));
782 }
Robert Phillips777707b2018-01-17 11:40:14 -0500783}
784
Robert Phillipse8fabb22018-02-04 14:33:21 -0500785sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy(
Brian Salomonbeb7f522019-08-30 16:19:42 -0400786 LazyInstantiateCallback&& callback,
787 const GrBackendFormat& format,
788 const GrSurfaceDesc& desc,
789 int sampleCnt,
790 GrSurfaceOrigin origin,
791 GrInternalSurfaceFlags surfaceFlags,
792 const TextureInfo* textureInfo,
793 GrMipMapsStatus mipMapsStatus,
794 SkBackingFit fit,
795 SkBudgeted budgeted,
796 GrProtected isProtected,
797 bool wrapsVkSecondaryCB,
798 UseAllocator useAllocator) {
Robert Phillipse8fabb22018-02-04 14:33:21 -0500799 SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
800 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400801
Robert Phillipsa41c6852019-02-07 10:44:10 -0500802 if (desc.fWidth > this->caps()->maxRenderTargetSize() ||
803 desc.fHeight > this->caps()->maxRenderTargetSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400804 return nullptr;
805 }
806
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400807 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
808 GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(format, colorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400809
Brian Salomon7226c232018-07-30 13:13:17 -0400810 if (textureInfo) {
Greg Danielb085fa92019-03-05 16:55:12 -0500811 // Wrapped vulkan secondary command buffers don't support texturing since we won't have an
812 // actual VkImage to texture from.
813 SkASSERT(!wrapsVkSecondaryCB);
Brian Salomon7226c232018-07-30 13:13:17 -0400814 return sk_sp<GrRenderTargetProxy>(new GrTextureRenderTargetProxy(
Brian Salomonbeb7f522019-08-30 16:19:42 -0400815 *this->caps(), std::move(callback), format, desc, sampleCnt, origin,
Greg Danielbaf8d992019-10-29 14:14:32 -0400816 textureInfo->fMipMapped, mipMapsStatus, texSwizzle, fit, budgeted, isProtected,
817 surfaceFlags, useAllocator));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500818 }
819
Greg Danielb085fa92019-03-05 16:55:12 -0500820 GrRenderTargetProxy::WrapsVkSecondaryCB vkSCB =
821 wrapsVkSecondaryCB ? GrRenderTargetProxy::WrapsVkSecondaryCB::kYes
822 : GrRenderTargetProxy::WrapsVkSecondaryCB::kNo;
823
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400824 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
Greg Danielbaf8d992019-10-29 14:14:32 -0400825 std::move(callback), format, desc, sampleCnt, origin, texSwizzle, fit, budgeted,
826 isProtected, surfaceFlags, useAllocator, vkSCB));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500827}
828
Brian Salomonbeb7f522019-08-30 16:19:42 -0400829sk_sp<GrTextureProxy> GrProxyProvider::MakeFullyLazyProxy(LazyInstantiateCallback&& callback,
830 const GrBackendFormat& format,
831 GrRenderable renderable,
832 int renderTargetSampleCnt,
833 GrProtected isProtected,
834 GrSurfaceOrigin origin,
835 GrPixelConfig config,
836 const GrCaps& caps,
837 UseAllocator useAllocator) {
Robert Phillips0a15cc62019-07-30 12:49:10 -0400838 if (!format.isValid()) {
839 return nullptr;
840 }
841
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400842 SkASSERT(renderTargetSampleCnt == 1 || renderable == GrRenderable::kYes);
Robert Phillips777707b2018-01-17 11:40:14 -0500843 GrSurfaceDesc desc;
Robert Phillips10d17212019-04-24 14:09:10 -0400844 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNone;
Robert Phillips777707b2018-01-17 11:40:14 -0500845 desc.fWidth = -1;
846 desc.fHeight = -1;
847 desc.fConfig = config;
Robert Phillips777707b2018-01-17 11:40:14 -0500848
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400849 GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
850 GrSwizzle texSwizzle = caps.getTextureSwizzle(format, colorType);
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400851
Brian Salomonbeb7f522019-08-30 16:19:42 -0400852 if (GrRenderable::kYes == renderable) {
853 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
854 caps, std::move(callback), format, desc, renderTargetSampleCnt, origin,
Greg Danielbaf8d992019-10-29 14:14:32 -0400855 GrMipMapped::kNo, GrMipMapsStatus::kNotAllocated, texSwizzle, SkBackingFit::kApprox,
856 SkBudgeted::kYes, isProtected, surfaceFlags, useAllocator));
Brian Salomonbeb7f522019-08-30 16:19:42 -0400857 } else {
858 return sk_sp<GrTextureProxy>(new GrTextureProxy(
859 std::move(callback), format, desc, origin, GrMipMapped::kNo,
860 GrMipMapsStatus::kNotAllocated, texSwizzle, SkBackingFit::kApprox, SkBudgeted::kYes,
861 isProtected, surfaceFlags, useAllocator));
862 }
Robert Phillips777707b2018-01-17 11:40:14 -0500863}
864
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500865bool GrProxyProvider::IsFunctionallyExact(GrSurfaceProxy* proxy) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400866 const bool isInstantiated = proxy->isInstantiated();
Robert Phillipsdb3b9792018-06-12 15:18:00 -0400867 // A proxy is functionally exact if:
868 // it is exact (obvs)
869 // when it is instantiated it will be exact (i.e., power of two dimensions)
Robert Phillipse9462dd2019-10-23 12:41:29 -0400870 // when it is instantiated the content rect will cover the entire backing surface
Robert Phillipsdb3b9792018-06-12 15:18:00 -0400871 return proxy->priv().isExact() ||
872 (!isInstantiated && SkIsPow2(proxy->width()) && SkIsPow2(proxy->height())) ||
Robert Phillipse9462dd2019-10-23 12:41:29 -0400873 (proxy->backingStoreDimensions() == proxy->dimensions());
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500874}
875
Robert Phillips427966a2018-12-20 17:20:43 -0500876void GrProxyProvider::processInvalidUniqueKey(const GrUniqueKey& key, GrTextureProxy* proxy,
877 InvalidateGPUResource invalidateGPUResource) {
Chris Dalton2de13dd2019-01-03 15:11:59 -0700878 SkASSERT(key.isValid());
879
Robert Phillips427966a2018-12-20 17:20:43 -0500880 if (!proxy) {
881 proxy = fUniquelyKeyedProxies.find(key);
882 }
Chris Dalton2de13dd2019-01-03 15:11:59 -0700883 SkASSERT(!proxy || proxy->getUniqueKey() == key);
884
885 // Locate the corresponding GrGpuResource (if it needs to be invalidated) before clearing the
886 // proxy's unique key. We must do it in this order because 'key' may alias the proxy's key.
887 sk_sp<GrGpuResource> invalidGpuResource;
888 if (InvalidateGPUResource::kYes == invalidateGPUResource) {
Brian Salomon01ceae92019-04-02 11:49:54 -0400889 GrContext* direct = fImageContext->priv().asDirectContext();
890 if (direct) {
891 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
892 invalidGpuResource = resourceProvider->findByUniqueKey<GrGpuResource>(key);
Chris Dalton2de13dd2019-01-03 15:11:59 -0700893 }
894 SkASSERT(!invalidGpuResource || invalidGpuResource->getUniqueKey() == key);
895 }
Robert Phillips427966a2018-12-20 17:20:43 -0500896
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500897 // Note: this method is called for the whole variety of GrGpuResources so often 'key'
898 // will not be in 'fUniquelyKeyedProxies'.
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500899 if (proxy) {
Robert Phillips427966a2018-12-20 17:20:43 -0500900 fUniquelyKeyedProxies.remove(key);
901 proxy->cacheAccess().clearUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500902 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500903
Chris Dalton2de13dd2019-01-03 15:11:59 -0700904 if (invalidGpuResource) {
905 invalidGpuResource->resourcePriv().removeUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500906 }
907}
908
Robert Phillipsa41c6852019-02-07 10:44:10 -0500909uint32_t GrProxyProvider::contextID() const {
910 return fImageContext->priv().contextID();
911}
912
913const GrCaps* GrProxyProvider::caps() const {
914 return fImageContext->priv().caps();
915}
916
917sk_sp<const GrCaps> GrProxyProvider::refCaps() const {
918 return fImageContext->priv().refCaps();
919}
920
Robert Phillipsa9162df2019-02-11 14:12:03 -0500921bool GrProxyProvider::isAbandoned() const {
922 return fImageContext->priv().abandoned();
923}
924
Robert Phillips0790f8a2018-09-18 13:11:03 -0400925void GrProxyProvider::orphanAllUniqueKeys() {
926 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
927 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
928 GrTextureProxy& tmp = *iter;
929
930 tmp.fProxyProvider = nullptr;
931 }
932}
933
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500934void GrProxyProvider::removeAllUniqueKeys() {
935 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
936 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
937 GrTextureProxy& tmp = *iter;
938
Chris Dalton2de13dd2019-01-03 15:11:59 -0700939 this->processInvalidUniqueKey(tmp.getUniqueKey(), &tmp, InvalidateGPUResource::kNo);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500940 }
941 SkASSERT(!fUniquelyKeyedProxies.count());
942}
Robert Phillipsa41c6852019-02-07 10:44:10 -0500943
944bool GrProxyProvider::renderingDirectly() const {
945 return fImageContext->priv().asDirectContext();
946}