blob: cf20dd1d715b1ff44997222376983ec5c0758aea [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/private/GrImageContext.h"
14#include "include/private/GrResourceKey.h"
15#include "include/private/GrSingleOwner.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/private/SkImageInfoPriv.h"
17#include "src/core/SkAutoPixmapStorage.h"
Robert Phillips99dead92020-01-27 16:11:57 -050018#include "src/core/SkCompressedDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#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"
Brian Salomond005b692020-04-01 15:47:05 -040026#include "src/gpu/GrRenderTargetContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/GrResourceProvider.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040028#include "src/gpu/GrSurfaceProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/gpu/GrSurfaceProxyPriv.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000030#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "src/gpu/GrTextureProxyCacheAccess.h"
32#include "src/gpu/GrTextureRenderTargetProxy.h"
33#include "src/gpu/SkGr.h"
34#include "src/image/SkImage_Base.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050035
36#define ASSERT_SINGLE_OWNER \
Robert Phillipsa41c6852019-02-07 10:44:10 -050037 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fImageContext->priv().singleOwner());)
Robert Phillips1afd4cd2018-01-08 13:40:32 -050038
Robert Phillipsa9162df2019-02-11 14:12:03 -050039GrProxyProvider::GrProxyProvider(GrImageContext* imageContext) : fImageContext(imageContext) {}
Robert Phillips1afd4cd2018-01-08 13:40:32 -050040
41GrProxyProvider::~GrProxyProvider() {
Robert Phillipsa41c6852019-02-07 10:44:10 -050042 if (this->renderingDirectly()) {
Robert Phillips0790f8a2018-09-18 13:11:03 -040043 // In DDL-mode a proxy provider can still have extant uniquely keyed proxies (since
44 // they need their unique keys to, potentially, find a cached resource when the
45 // DDL is played) but, in non-DDL-mode they should all have been cleaned up by this point.
46 SkASSERT(!fUniquelyKeyedProxies.count());
47 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050048}
49
Robert Phillipsadbe1322018-01-17 13:35:46 -050050bool GrProxyProvider::assignUniqueKeyToProxy(const GrUniqueKey& key, GrTextureProxy* proxy) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050051 ASSERT_SINGLE_OWNER
52 SkASSERT(key.isValid());
53 if (this->isAbandoned() || !proxy) {
Robert Phillipsadbe1322018-01-17 13:35:46 -050054 return false;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050055 }
56
Robert Phillipsa41c6852019-02-07 10:44:10 -050057#ifdef SK_DEBUG
58 {
59 GrContext* direct = fImageContext->priv().asDirectContext();
60 if (direct) {
61 GrResourceCache* resourceCache = direct->priv().getResourceCache();
62 // If there is already a GrResource with this key then the caller has violated the
63 // normal usage pattern of uniquely keyed resources (e.g., they have created one w/o
64 // first seeing if it already existed in the cache).
65 SkASSERT(!resourceCache->findAndRefUniqueResource(key));
66 }
67 }
68#endif
Robert Phillips1afd4cd2018-01-08 13:40:32 -050069
Robert Phillips1afd4cd2018-01-08 13:40:32 -050070 SkASSERT(!fUniquelyKeyedProxies.find(key)); // multiple proxies can't get the same key
71
72 proxy->cacheAccess().setUniqueKey(this, key);
73 SkASSERT(proxy->getUniqueKey() == key);
74 fUniquelyKeyedProxies.add(proxy);
Robert Phillipsadbe1322018-01-17 13:35:46 -050075 return true;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050076}
77
78void GrProxyProvider::adoptUniqueKeyFromSurface(GrTextureProxy* proxy, const GrSurface* surf) {
79 SkASSERT(surf->getUniqueKey().isValid());
80 proxy->cacheAccess().setUniqueKey(this, surf->getUniqueKey());
81 SkASSERT(proxy->getUniqueKey() == surf->getUniqueKey());
82 // multiple proxies can't get the same key
83 SkASSERT(!fUniquelyKeyedProxies.find(surf->getUniqueKey()));
84 fUniquelyKeyedProxies.add(proxy);
85}
86
Chris Dalton2de13dd2019-01-03 15:11:59 -070087void GrProxyProvider::removeUniqueKeyFromProxy(GrTextureProxy* proxy) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050088 ASSERT_SINGLE_OWNER
Chris Dalton2de13dd2019-01-03 15:11:59 -070089 SkASSERT(proxy);
90 SkASSERT(proxy->getUniqueKey().isValid());
91
92 if (this->isAbandoned()) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050093 return;
94 }
Robert Phillips0790f8a2018-09-18 13:11:03 -040095
Chris Dalton2de13dd2019-01-03 15:11:59 -070096 this->processInvalidUniqueKey(proxy->getUniqueKey(), proxy, InvalidateGPUResource::kYes);
Robert Phillips1afd4cd2018-01-08 13:40:32 -050097}
98
Greg Daniel3a365112020-02-14 10:47:18 -050099sk_sp<GrTextureProxy> GrProxyProvider::findProxyByUniqueKey(const GrUniqueKey& key) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500100 ASSERT_SINGLE_OWNER
101
102 if (this->isAbandoned()) {
103 return nullptr;
104 }
105
Brian Salomon01ceae92019-04-02 11:49:54 -0400106 GrTextureProxy* proxy = fUniquelyKeyedProxies.find(key);
Brian Salomon01ceae92019-04-02 11:49:54 -0400107 if (proxy) {
Robert Phillipse5f73282019-06-18 17:15:04 -0400108 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 Salomona56a7462020-02-07 14:17:25 -0500117 SkISize dimensions,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400118 const GrBackendFormat& format,
119 GrRenderable renderable,
120 int renderTargetSampleCnt,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400121 SkBackingFit fit,
122 SkBudgeted budgeted,
123 GrProtected isProtected) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500124 ASSERT_SINGLE_OWNER
125 if (this->isAbandoned()) {
126 return nullptr;
127 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500128 GrContext* direct = fImageContext->priv().asDirectContext();
129 if (!direct) {
130 return nullptr;
131 }
132
Brian Salomon4eb38b72019-08-05 12:58:39 -0400133 if (this->caps()->isFormatCompressed(format)) {
134 // TODO: Allow this to go to GrResourceProvider::createCompressedTexture() once we no longer
Greg Daniel4cb29332020-01-23 10:07:02 -0500135 // rely on GrColorType to get a swizzle for the proxy.
Brian Salomon4eb38b72019-08-05 12:58:39 -0400136 return nullptr;
137 }
Brian Salomon4eb38b72019-08-05 12:58:39 -0400138
Robert Phillipsa41c6852019-02-07 10:44:10 -0500139 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
140 sk_sp<GrTexture> tex;
141
142 if (SkBackingFit::kApprox == fit) {
Brian Salomona56a7462020-02-07 14:17:25 -0500143 tex = resourceProvider->createApproxTexture(dimensions, format, renderable,
144 renderTargetSampleCnt, isProtected);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500145 } else {
Brian Salomona56a7462020-02-07 14:17:25 -0500146 tex = resourceProvider->createTexture(dimensions, format, renderable, renderTargetSampleCnt,
Brian Salomona90382f2019-09-17 09:01:56 -0400147 GrMipMapped::kNo, budgeted, isProtected);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500148 }
149 if (!tex) {
150 return nullptr;
151 }
152
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400153 return this->createWrapped(std::move(tex), UseAllocator::kYes);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500154}
155
Brian Salomon4eb38b72019-08-05 12:58:39 -0400156sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createInstantiatedProxy(
Brian Salomona56a7462020-02-07 14:17:25 -0500157 SkISize dimensions,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400158 GrColorType colorType,
159 GrRenderable renderable,
160 int renderTargetSampleCnt,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400161 SkBackingFit fit,
162 SkBudgeted budgeted,
163 GrProtected isProtected) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500164 ASSERT_SINGLE_OWNER
165 if (this->isAbandoned()) {
166 return nullptr;
167 }
Brian Salomon4eb38b72019-08-05 12:58:39 -0400168 auto format = this->caps()->getDefaultBackendFormat(colorType, renderable);
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400169 return this->testingOnly_createInstantiatedProxy(dimensions,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400170 format,
171 renderable,
172 renderTargetSampleCnt,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400173 fit,
174 budgeted,
175 isProtected);
176}
177
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400178sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createWrapped(sk_sp<GrTexture> tex) {
179 return this->createWrapped(std::move(tex), UseAllocator::kYes);
Robert Phillipsa41c6852019-02-07 10:44:10 -0500180}
181#endif
182
Brian Salomonbeb7f522019-08-30 16:19:42 -0400183sk_sp<GrTextureProxy> GrProxyProvider::createWrapped(sk_sp<GrTexture> tex,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400184 UseAllocator useAllocator) {
Robert Phillipsadbe1322018-01-17 13:35:46 -0500185#ifdef SK_DEBUG
186 if (tex->getUniqueKey().isValid()) {
Greg Daniel3a365112020-02-14 10:47:18 -0500187 SkASSERT(!this->findProxyByUniqueKey(tex->getUniqueKey()));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500188 }
189#endif
190
191 if (tex->asRenderTarget()) {
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400192 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), useAllocator));
Greg Daniel3a365112020-02-14 10:47:18 -0500193 } else {
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400194 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), useAllocator));
Robert Phillipsadbe1322018-01-17 13:35:46 -0500195 }
196}
197
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500198sk_sp<GrTextureProxy> GrProxyProvider::findOrCreateProxyByUniqueKey(const GrUniqueKey& key,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400199 UseAllocator useAllocator) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500200 ASSERT_SINGLE_OWNER
201
202 if (this->isAbandoned()) {
203 return nullptr;
204 }
205
Greg Daniel3a365112020-02-14 10:47:18 -0500206 sk_sp<GrTextureProxy> result = this->findProxyByUniqueKey(key);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500207 if (result) {
208 return result;
209 }
210
Robert Phillipsa41c6852019-02-07 10:44:10 -0500211 GrContext* direct = fImageContext->priv().asDirectContext();
212 if (!direct) {
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500213 return nullptr;
214 }
215
Robert Phillipsa41c6852019-02-07 10:44:10 -0500216 GrResourceCache* resourceCache = direct->priv().getResourceCache();
217
218 GrGpuResource* resource = resourceCache->findAndRefUniqueResource(key);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500219 if (!resource) {
220 return nullptr;
221 }
222
223 sk_sp<GrTexture> texture(static_cast<GrSurface*>(resource)->asTexture());
224 SkASSERT(texture);
225
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400226 result = this->createWrapped(std::move(texture), useAllocator);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500227 SkASSERT(result->getUniqueKey() == key);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500228 // createWrapped should've added this for us
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500229 SkASSERT(fUniquelyKeyedProxies.find(key));
230 return result;
231}
232
Brian Salomond005b692020-04-01 15:47:05 -0400233GrSurfaceProxyView GrProxyProvider::findCachedProxyWithColorTypeFallback(const GrUniqueKey& key,
234 GrSurfaceOrigin origin,
Brian Salomon0029db02020-04-03 10:41:24 -0400235 GrColorType ct,
236 int sampleCnt) {
Brian Salomond005b692020-04-01 15:47:05 -0400237 auto proxy = this->findOrCreateProxyByUniqueKey(key);
238 if (!proxy) {
239 return {};
240 }
241 // Assume that we used a fallback color type if and only if the proxy is renderable.
242 if (proxy->asRenderTargetProxy()) {
243 GrBackendFormat expectedFormat;
244 std::tie(ct, expectedFormat) =
Brian Salomon0029db02020-04-03 10:41:24 -0400245 GrRenderTargetContext::GetFallbackColorTypeAndFormat(fImageContext, ct, sampleCnt);
Brian Salomond005b692020-04-01 15:47:05 -0400246 SkASSERT(expectedFormat == proxy->backendFormat());
247 }
248 GrSwizzle swizzle = fImageContext->priv().caps()->getReadSwizzle(proxy->backendFormat(), ct);
249 return {std::move(proxy), origin, swizzle};
250}
251
Brian Osmande496652019-03-22 13:42:33 -0400252sk_sp<GrTextureProxy> GrProxyProvider::createProxyFromBitmap(const SkBitmap& bitmap,
Greg Daniel6f5441a2020-01-28 17:02:49 -0500253 GrMipMapped mipMapped,
Brian Salomonbc074a62020-03-18 10:06:13 -0400254 SkBackingFit fit,
255 SkBudgeted budgeted) {
Brian Osman412674f2019-02-07 15:34:58 -0500256 ASSERT_SINGLE_OWNER
Greg Daniel6f5441a2020-01-28 17:02:49 -0500257 SkASSERT(fit == SkBackingFit::kExact || mipMapped == GrMipMapped::kNo);
Brian Osman412674f2019-02-07 15:34:58 -0500258
259 if (this->isAbandoned()) {
260 return nullptr;
261 }
262
Brian Osman2b23c4b2018-06-01 12:25:08 -0400263 if (!SkImageInfoIsValid(bitmap.info())) {
Greg Daniela4ead652018-02-07 10:21:48 -0500264 return nullptr;
265 }
266
Brian Osmande496652019-03-22 13:42:33 -0400267 ATRACE_ANDROID_FRAMEWORK("Upload %sTexture [%ux%u]",
268 GrMipMapped::kYes == mipMapped ? "MipMap " : "",
269 bitmap.width(), bitmap.height());
Greg Daniela4ead652018-02-07 10:21:48 -0500270
271 // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
272 // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
273 // upload of the data to the gpu can happen at anytime and the bitmap may change by then.
Greg Daniel6f5441a2020-01-28 17:02:49 -0500274 SkBitmap copyBitmap = bitmap;
275 if (!this->renderingDirectly() && !bitmap.isImmutable()) {
276 copyBitmap.allocPixels();
277 if (!bitmap.readPixels(copyBitmap.pixmap())) {
278 return nullptr;
279 }
280 copyBitmap.setImmutable();
Greg Daniela4ead652018-02-07 10:21:48 -0500281 }
282
Greg Daniel6f5441a2020-01-28 17:02:49 -0500283 sk_sp<GrTextureProxy> proxy;
284 if (mipMapped == GrMipMapped::kNo ||
285 0 == SkMipMap::ComputeLevelCount(copyBitmap.width(), copyBitmap.height())) {
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400286 proxy = this->createNonMippedProxyFromBitmap(copyBitmap, fit, budgeted);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500287 } else {
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400288 proxy = this->createMippedProxyFromBitmap(copyBitmap, budgeted);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500289 }
290
291 if (!proxy) {
292 return nullptr;
293 }
294
295 GrContext* direct = fImageContext->priv().asDirectContext();
296 if (direct) {
297 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
298
299 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
300 // we're better off instantiating the proxy immediately here.
301 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
302 return nullptr;
303 }
304 }
305 return proxy;
306}
307
308sk_sp<GrTextureProxy> GrProxyProvider::createNonMippedProxyFromBitmap(const SkBitmap& bitmap,
309 SkBackingFit fit,
Brian Salomonbc074a62020-03-18 10:06:13 -0400310 SkBudgeted budgeted) {
Brian Salomona56a7462020-02-07 14:17:25 -0500311 auto dims = bitmap.dimensions();
Greg Daniel6f5441a2020-01-28 17:02:49 -0500312
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400313 auto colorType = SkColorTypeToGrColorType(bitmap.colorType());
314 GrBackendFormat format = this->caps()->getDefaultBackendFormat(colorType, GrRenderable::kNo);
315 if (!format.isValid()) {
316 return nullptr;
317 }
318
Greg Daniel6f5441a2020-01-28 17:02:49 -0500319 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomon63410e92020-03-23 18:32:50 -0400320 [bitmap](GrResourceProvider* resourceProvider, const LazySurfaceDesc& desc) {
321 SkASSERT(desc.fMipMapped == GrMipMapped::kNo);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500322 GrMipLevel mipLevel = { bitmap.getPixels(), bitmap.rowBytes() };
Brian Salomon63410e92020-03-23 18:32:50 -0400323 auto colorType = SkColorTypeToGrColorType(bitmap.colorType());
Greg Daniel6f5441a2020-01-28 17:02:49 -0500324 return LazyCallbackResult(resourceProvider->createTexture(
Brian Salomon63410e92020-03-23 18:32:50 -0400325 desc.fDimensions, desc.fFormat, colorType, desc.fRenderable,
326 desc.fSampleCnt, desc.fBudgeted, desc.fFit, desc.fProtected, mipLevel));
Greg Daniel6f5441a2020-01-28 17:02:49 -0500327 },
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400328 format, dims, GrRenderable::kNo, 1, GrMipMapped::kNo, GrMipMapsStatus::kNotAllocated,
329 GrInternalSurfaceFlags::kNone, fit, budgeted, GrProtected::kNo, UseAllocator::kYes);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500330
331 if (!proxy) {
332 return nullptr;
333 }
Brian Salomona56a7462020-02-07 14:17:25 -0500334 SkASSERT(proxy->dimensions() == bitmap.dimensions());
Greg Daniel6f5441a2020-01-28 17:02:49 -0500335 return proxy;
336}
337
338sk_sp<GrTextureProxy> GrProxyProvider::createMippedProxyFromBitmap(const SkBitmap& bitmap,
Brian Salomonbc074a62020-03-18 10:06:13 -0400339 SkBudgeted budgeted) {
Greg Daniel6f5441a2020-01-28 17:02:49 -0500340 SkASSERT(this->caps()->mipMapSupport());
341
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400342 auto colorType = SkColorTypeToGrColorType(bitmap.colorType());
343 GrBackendFormat format = this->caps()->getDefaultBackendFormat(colorType, GrRenderable::kNo);
344 if (!format.isValid()) {
345 return nullptr;
346 }
Greg Daniel6f5441a2020-01-28 17:02:49 -0500347
348 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(bitmap.pixmap(), nullptr));
Brian Osmanbc6b9cb2018-09-13 13:43:25 -0400349 if (!mipmaps) {
350 return nullptr;
351 }
352
Brian Salomona56a7462020-02-07 14:17:25 -0500353 auto dims = bitmap.dimensions();
Greg Danielce3ddaa2020-01-22 16:58:15 -0500354
Greg Daniela4ead652018-02-07 10:21:48 -0500355 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomon63410e92020-03-23 18:32:50 -0400356 [bitmap, mipmaps](GrResourceProvider* resourceProvider, const LazySurfaceDesc& desc) {
Brian Osman1b97f132018-09-13 17:33:48 +0000357 const int mipLevelCount = mipmaps->countLevels() + 1;
358 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
Brian Salomon63410e92020-03-23 18:32:50 -0400359 auto colorType = SkColorTypeToGrColorType(bitmap.colorType());
Brian Osman1b97f132018-09-13 17:33:48 +0000360
Greg Daniel6f5441a2020-01-28 17:02:49 -0500361 texels[0].fPixels = bitmap.getPixels();
362 texels[0].fRowBytes = bitmap.rowBytes();
Greg Daniela4ead652018-02-07 10:21:48 -0500363
Greg Daniela4ead652018-02-07 10:21:48 -0500364 for (int i = 1; i < mipLevelCount; ++i) {
365 SkMipMap::Level generatedMipLevel;
366 mipmaps->getLevel(i - 1, &generatedMipLevel);
367 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
368 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
369 SkASSERT(texels[i].fPixels);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500370 SkASSERT(generatedMipLevel.fPixmap.colorType() == bitmap.colorType());
Greg Daniela4ead652018-02-07 10:21:48 -0500371 }
Brian Salomonbeb7f522019-08-30 16:19:42 -0400372 return LazyCallbackResult(resourceProvider->createTexture(
Brian Salomon63410e92020-03-23 18:32:50 -0400373 desc.fDimensions, desc.fFormat, colorType, GrRenderable::kNo, 1,
374 desc.fBudgeted, GrProtected::kNo, texels.get(), mipLevelCount));
Brian Salomon2a4f9832018-03-03 22:43:43 -0500375 },
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400376 format, dims, GrRenderable::kNo, 1, GrMipMapped::kYes, GrMipMapsStatus::kValid,
377 GrInternalSurfaceFlags::kNone, SkBackingFit::kExact, budgeted, GrProtected::kNo,
378 UseAllocator::kYes);
Greg Daniela4ead652018-02-07 10:21:48 -0500379
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400380 if (!proxy) {
381 return nullptr;
382 }
383
Brian Salomona56a7462020-02-07 14:17:25 -0500384 SkASSERT(proxy->dimensions() == bitmap.dimensions());
Greg Daniel6f5441a2020-01-28 17:02:49 -0500385
Greg Daniela4ead652018-02-07 10:21:48 -0500386 return proxy;
387}
388
Greg Daniel4065d452018-11-16 15:43:41 -0500389sk_sp<GrTextureProxy> GrProxyProvider::createProxy(const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -0500390 SkISize dimensions,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400391 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400392 int renderTargetSampleCnt,
Greg Danielf6f7b672018-02-15 13:06:26 -0500393 GrMipMapped mipMapped,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500394 SkBackingFit fit,
395 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -0400396 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400397 GrInternalSurfaceFlags surfaceFlags,
398 GrSurfaceProxy::UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500399 ASSERT_SINGLE_OWNER
400 if (this->isAbandoned()) {
401 return nullptr;
402 }
403
Robert Phillips8ff8bcc2019-07-29 17:03:35 -0400404 const GrCaps* caps = this->caps();
405
406 if (caps->isFormatCompressed(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400407 // Deferred proxies for compressed textures are not supported.
408 return nullptr;
409 }
Robert Phillips0902c982019-07-16 07:47:56 -0400410
Greg Danielf6f7b672018-02-15 13:06:26 -0500411 if (GrMipMapped::kYes == mipMapped) {
412 // SkMipMap doesn't include the base level in the level count so we have to add 1
Brian Salomona56a7462020-02-07 14:17:25 -0500413 int mipCount = SkMipMap::ComputeLevelCount(dimensions.fWidth, dimensions.fHeight) + 1;
Greg Danielf6f7b672018-02-15 13:06:26 -0500414 if (1 == mipCount) {
415 mipMapped = GrMipMapped::kNo;
416 }
417 }
418
Brian Salomona56a7462020-02-07 14:17:25 -0500419 if (!caps->validateSurfaceParams(dimensions, format, renderable, renderTargetSampleCnt,
420 mipMapped)) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500421 return nullptr;
422 }
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600423 GrMipMapsStatus mipMapsStatus = (GrMipMapped::kYes == mipMapped)
424 ? GrMipMapsStatus::kDirty
425 : GrMipMapsStatus::kNotAllocated;
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400426 if (renderable == GrRenderable::kYes) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400427 renderTargetSampleCnt =
Greg Daniel6fa62e22019-08-07 15:52:37 -0400428 caps->getRenderTargetSampleCount(renderTargetSampleCnt, format);
429 SkASSERT(renderTargetSampleCnt);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500430 // We know anything we instantiate later from this deferred path will be
431 // both texturable and renderable
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400432 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400433 *caps, format, dimensions, renderTargetSampleCnt, mipMapped, mipMapsStatus, fit,
434 budgeted, isProtected, surfaceFlags, useAllocator));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500435 }
436
Greg Daniel3a365112020-02-14 10:47:18 -0500437 return sk_sp<GrTextureProxy>(new GrTextureProxy(format, dimensions, mipMapped, mipMapsStatus,
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400438 fit, budgeted, isProtected, surfaceFlags,
439 useAllocator));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500440}
441
Brian Salomonbb8dde82019-06-27 10:52:13 -0400442sk_sp<GrTextureProxy> GrProxyProvider::createCompressedTextureProxy(
Robert Phillips3a833922020-01-21 15:25:58 -0500443 SkISize dimensions, SkBudgeted budgeted, GrMipMapped mipMapped, GrProtected isProtected,
Robert Phillipse4720c62020-01-14 14:33:24 -0500444 SkImage::CompressionType compressionType, sk_sp<SkData> data) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500445 ASSERT_SINGLE_OWNER
446 if (this->isAbandoned()) {
447 return nullptr;
448 }
Brian Salomonbb8dde82019-06-27 10:52:13 -0400449
Robert Phillips8ff8bcc2019-07-29 17:03:35 -0400450 GrBackendFormat format = this->caps()->getBackendFormatFromCompressionType(compressionType);
451
Greg Daniel7bfc9132019-08-14 14:23:53 -0400452 if (!this->caps()->isFormatTexturable(format)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500453 return nullptr;
454 }
455
Robert Phillipse4720c62020-01-14 14:33:24 -0500456 GrMipMapsStatus mipMapsStatus = (GrMipMapped::kYes == mipMapped)
457 ? GrMipMapsStatus::kValid
458 : GrMipMapsStatus::kNotAllocated;
459
Jim Van Verthee06b332019-01-18 10:36:32 -0500460 sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
Brian Salomon63410e92020-03-23 18:32:50 -0400461 [data](GrResourceProvider* resourceProvider, const LazySurfaceDesc& desc) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400462 return LazyCallbackResult(resourceProvider->createCompressedTexture(
Brian Salomon63410e92020-03-23 18:32:50 -0400463 desc.fDimensions, desc.fFormat, desc.fBudgeted, desc.fMipMapped,
464 desc.fProtected, data.get()));
Brian Salomonbb8dde82019-06-27 10:52:13 -0400465 },
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400466 format, dimensions, GrRenderable::kNo, 1, mipMapped, mipMapsStatus,
Greg Daniel3a365112020-02-14 10:47:18 -0500467 GrInternalSurfaceFlags::kReadOnly, SkBackingFit::kExact, SkBudgeted::kYes,
468 GrProtected::kNo, UseAllocator::kYes);
Jim Van Verthee06b332019-01-18 10:36:32 -0500469
470 if (!proxy) {
471 return nullptr;
472 }
473
Robert Phillipsa41c6852019-02-07 10:44:10 -0500474 GrContext* direct = fImageContext->priv().asDirectContext();
475 if (direct) {
476 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Jim Van Verthee06b332019-01-18 10:36:32 -0500477 // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
478 // we're better off instantiating the proxy immediately here.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500479 if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
Jim Van Verthee06b332019-01-18 10:36:32 -0500480 return nullptr;
481 }
482 }
483 return proxy;
484}
485
Brian Salomon7578f3e2018-03-07 14:39:54 -0500486sk_sp<GrTextureProxy> GrProxyProvider::wrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500487 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500488 GrWrapCacheable cacheable,
Brian Salomonc67c31c2018-12-06 10:00:03 -0500489 GrIOType ioType,
Brian Salomon7578f3e2018-03-07 14:39:54 -0500490 ReleaseProc releaseProc,
491 ReleaseContext releaseCtx) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500492 SkASSERT(ioType != kWrite_GrIOType);
Robert Phillipsf9bec202018-01-16 09:21:01 -0500493 if (this->isAbandoned()) {
494 return nullptr;
495 }
496
Brian Salomonf7778972018-03-08 10:13:17 -0500497 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500498 GrContext* direct = fImageContext->priv().asDirectContext();
499 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500500 return nullptr;
501 }
502
Robert Phillipsa41c6852019-02-07 10:44:10 -0500503 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
504
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500505 sk_sp<GrTexture> tex =
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400506 resourceProvider->wrapBackendTexture(backendTex, ownership, cacheable, ioType);
Brian Salomonf7778972018-03-08 10:13:17 -0500507 if (!tex) {
508 return nullptr;
509 }
Robert Phillipsadbe1322018-01-17 13:35:46 -0500510
Greg Daniel6a0176b2018-01-30 09:28:44 -0500511 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500512 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel6a0176b2018-01-30 09:28:44 -0500513 }
514
Brian Salomonf7778972018-03-08 10:13:17 -0500515 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
516 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500517 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500518
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400519 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500520}
521
Robert Phillipsead321b2019-12-19 10:16:32 -0500522sk_sp<GrTextureProxy> GrProxyProvider::wrapCompressedBackendTexture(const GrBackendTexture& beTex,
Robert Phillipsead321b2019-12-19 10:16:32 -0500523 GrWrapOwnership ownership,
524 GrWrapCacheable cacheable,
525 ReleaseProc releaseProc,
526 ReleaseContext releaseCtx) {
527 if (this->isAbandoned()) {
528 return nullptr;
529 }
530
531 // This is only supported on a direct GrContext.
532 GrContext* direct = fImageContext->priv().asDirectContext();
533 if (!direct) {
534 return nullptr;
535 }
536
537 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
538
539 sk_sp<GrTexture> tex = resourceProvider->wrapCompressedBackendTexture(beTex, ownership,
540 cacheable);
541 if (!tex) {
542 return nullptr;
543 }
544
545 if (releaseProc) {
546 tex->setRelease(releaseProc, releaseCtx);
547 }
548
549 SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
550 // Make sure we match how we created the proxy with SkBudgeted::kNo
551 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
552
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400553 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), UseAllocator::kNo));
Robert Phillipsead321b2019-12-19 10:16:32 -0500554}
555
Brian Salomon7578f3e2018-03-07 14:39:54 -0500556sk_sp<GrTextureProxy> GrProxyProvider::wrapRenderableBackendTexture(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400557 const GrBackendTexture& backendTex,
558 int sampleCnt,
559 GrWrapOwnership ownership,
560 GrWrapCacheable cacheable,
561 ReleaseProc releaseProc,
Greg Daniel3a365112020-02-14 10:47:18 -0500562 ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500563 if (this->isAbandoned()) {
564 return nullptr;
565 }
566
Brian Salomonf7778972018-03-08 10:13:17 -0500567 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500568 GrContext* direct = fImageContext->priv().asDirectContext();
569 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500570 return nullptr;
571 }
572
Robert Phillips0902c982019-07-16 07:47:56 -0400573 const GrCaps* caps = this->caps();
574
Robert Phillipsa41c6852019-02-07 10:44:10 -0500575 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
576
Greg Daniel6fa62e22019-08-07 15:52:37 -0400577 sampleCnt = caps->getRenderTargetSampleCount(sampleCnt, backendTex.getBackendFormat());
578 SkASSERT(sampleCnt);
579
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400580 sk_sp<GrTexture> tex = resourceProvider->wrapRenderableBackendTexture(
581 backendTex, sampleCnt, ownership, cacheable);
Brian Salomonf7778972018-03-08 10:13:17 -0500582 if (!tex) {
583 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500584 }
585
Greg Daniel8ce79912019-02-05 10:08:43 -0500586 if (releaseProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500587 tex->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500588 }
589
Brian Salomonf7778972018-03-08 10:13:17 -0500590 SkASSERT(tex->asRenderTarget()); // A GrTextureRenderTarget
591 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500592 SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
Greg Daniel6abda432018-02-15 14:55:00 -0500593
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400594 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500595}
596
Brian Salomon7578f3e2018-03-07 14:39:54 -0500597sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400598 const GrBackendRenderTarget& backendRT,
599 ReleaseProc releaseProc,
Greg Daniel3a365112020-02-14 10:47:18 -0500600 ReleaseContext releaseCtx) {
Robert Phillipsf9bec202018-01-16 09:21:01 -0500601 if (this->isAbandoned()) {
602 return nullptr;
603 }
604
Brian Salomonf7778972018-03-08 10:13:17 -0500605 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500606 GrContext* direct = fImageContext->priv().asDirectContext();
607 if (!direct) {
Brian Salomonf7778972018-03-08 10:13:17 -0500608 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500609 }
610
Robert Phillipsa41c6852019-02-07 10:44:10 -0500611 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
612
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400613 sk_sp<GrRenderTarget> rt = resourceProvider->wrapBackendRenderTarget(backendRT);
Brian Salomonf7778972018-03-08 10:13:17 -0500614 if (!rt) {
615 return nullptr;
Greg Daniel2a303902018-02-20 10:25:54 -0500616 }
Greg Daniel8ce79912019-02-05 10:08:43 -0500617
Greg Daniel8ce79912019-02-05 10:08:43 -0500618 if (releaseProc) {
Brian Salomon2ca31f82019-03-05 13:28:58 -0500619 rt->setRelease(releaseProc, releaseCtx);
Greg Daniel8ce79912019-02-05 10:08:43 -0500620 }
621
Brian Salomonf7778972018-03-08 10:13:17 -0500622 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
623 SkASSERT(!rt->getUniqueKey().isValid());
624 // Make sure we match how we created the proxy with SkBudgeted::kNo
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500625 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Brian Salomonf7778972018-03-08 10:13:17 -0500626
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400627 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(std::move(rt), UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500628}
629
Brian Salomon7578f3e2018-03-07 14:39:54 -0500630sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendTextureAsRenderTarget(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400631 const GrBackendTexture& backendTex, int sampleCnt) {
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) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500639 return nullptr;
640 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500641
Robert Phillipsa41c6852019-02-07 10:44:10 -0500642 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
643
Brian Salomonf7778972018-03-08 10:13:17 -0500644 sk_sp<GrRenderTarget> rt =
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400645 resourceProvider->wrapBackendTextureAsRenderTarget(backendTex, sampleCnt);
Brian Salomonf7778972018-03-08 10:13:17 -0500646 if (!rt) {
647 return nullptr;
Greg Danielf87651e2018-02-21 11:36:53 -0500648 }
Brian Salomonf7778972018-03-08 10:13:17 -0500649 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
650 SkASSERT(!rt->getUniqueKey().isValid());
Greg Danielb46add82019-01-02 14:51:29 -0500651 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500652 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielf87651e2018-02-21 11:36:53 -0500653
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400654 return sk_sp<GrSurfaceProxy>(new GrRenderTargetProxy(std::move(rt), UseAllocator::kNo));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500655}
656
Greg Danielb46add82019-01-02 14:51:29 -0500657sk_sp<GrRenderTargetProxy> GrProxyProvider::wrapVulkanSecondaryCBAsRenderTarget(
658 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
659 if (this->isAbandoned()) {
660 return nullptr;
661 }
662
663 // This is only supported on a direct GrContext.
Robert Phillipsa41c6852019-02-07 10:44:10 -0500664 GrContext* direct = fImageContext->priv().asDirectContext();
665 if (!direct) {
Greg Danielb46add82019-01-02 14:51:29 -0500666 return nullptr;
667 }
668
Robert Phillipsa41c6852019-02-07 10:44:10 -0500669 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
Greg Danielb46add82019-01-02 14:51:29 -0500670
Robert Phillipsa41c6852019-02-07 10:44:10 -0500671 sk_sp<GrRenderTarget> rt = resourceProvider->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
672 vkInfo);
Greg Danielb46add82019-01-02 14:51:29 -0500673 if (!rt) {
674 return nullptr;
675 }
Robert Phillipsa41c6852019-02-07 10:44:10 -0500676
Greg Danielb46add82019-01-02 14:51:29 -0500677 SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
678 SkASSERT(!rt->getUniqueKey().isValid());
679 // This proxy should be unbudgeted because we're just wrapping an external resource
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500680 SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
Greg Danielb46add82019-01-02 14:51:29 -0500681
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400682 GrColorType colorType = SkColorTypeToGrColorType(imageInfo.colorType());
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400683
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400684 if (!this->caps()->isFormatAsColorTypeRenderable(colorType, rt->backendFormat(),
685 rt->numSamples())) {
686 return nullptr;
687 }
688
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400689 return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
690 std::move(rt), UseAllocator::kNo, GrRenderTargetProxy::WrapsVkSecondaryCB::kYes));
Brian Salomone8a766b2019-07-19 14:24:36 -0400691}
692
693sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
694 const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -0500695 SkISize dimensions,
Brian Salomone8a766b2019-07-19 14:24:36 -0400696 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400697 int renderTargetSampleCnt,
Brian Salomone8a766b2019-07-19 14:24:36 -0400698 GrMipMapped mipMapped,
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600699 GrMipMapsStatus mipMapsStatus,
Brian Salomone8a766b2019-07-19 14:24:36 -0400700 GrInternalSurfaceFlags surfaceFlags,
701 SkBackingFit fit,
702 SkBudgeted budgeted,
703 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400704 GrSurfaceProxy::UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500705 ASSERT_SINGLE_OWNER
706 if (this->isAbandoned()) {
707 return nullptr;
708 }
Brian Salomona56a7462020-02-07 14:17:25 -0500709 SkASSERT((dimensions.fWidth <= 0 && dimensions.fHeight <= 0) ||
710 (dimensions.fWidth > 0 && dimensions.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400711
Robert Phillips0a15cc62019-07-30 12:49:10 -0400712 if (!format.isValid()) {
713 return nullptr;
714 }
715
Brian Salomona56a7462020-02-07 14:17:25 -0500716 if (dimensions.fWidth > this->caps()->maxTextureSize() ||
717 dimensions.fHeight > this->caps()->maxTextureSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400718 return nullptr;
719 }
720
Brian Salomonbeb7f522019-08-30 16:19:42 -0400721 if (renderable == GrRenderable::kYes) {
722 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(*this->caps(),
723 std::move(callback),
724 format,
Brian Salomona56a7462020-02-07 14:17:25 -0500725 dimensions,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400726 renderTargetSampleCnt,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400727 mipMapped,
728 mipMapsStatus,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400729 fit,
730 budgeted,
731 isProtected,
732 surfaceFlags,
733 useAllocator));
734 } else {
735 return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(callback),
736 format,
Brian Salomona56a7462020-02-07 14:17:25 -0500737 dimensions,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400738 mipMapped,
739 mipMapsStatus,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400740 fit,
741 budgeted,
742 isProtected,
743 surfaceFlags,
744 useAllocator));
745 }
Robert Phillips777707b2018-01-17 11:40:14 -0500746}
747
Robert Phillipse8fabb22018-02-04 14:33:21 -0500748sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy(
Brian Salomonbeb7f522019-08-30 16:19:42 -0400749 LazyInstantiateCallback&& callback,
750 const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -0500751 SkISize dimensions,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400752 int sampleCnt,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400753 GrInternalSurfaceFlags surfaceFlags,
754 const TextureInfo* textureInfo,
755 GrMipMapsStatus mipMapsStatus,
756 SkBackingFit fit,
757 SkBudgeted budgeted,
758 GrProtected isProtected,
759 bool wrapsVkSecondaryCB,
760 UseAllocator useAllocator) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500761 ASSERT_SINGLE_OWNER
762 if (this->isAbandoned()) {
763 return nullptr;
764 }
Brian Salomona56a7462020-02-07 14:17:25 -0500765 SkASSERT((dimensions.fWidth <= 0 && dimensions.fHeight <= 0) ||
766 (dimensions.fWidth > 0 && dimensions.fHeight > 0));
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400767
Brian Salomona56a7462020-02-07 14:17:25 -0500768 if (dimensions.fWidth > this->caps()->maxRenderTargetSize() ||
769 dimensions.fHeight > this->caps()->maxRenderTargetSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -0400770 return nullptr;
771 }
772
Brian Salomon7226c232018-07-30 13:13:17 -0400773 if (textureInfo) {
Greg Danielb085fa92019-03-05 16:55:12 -0500774 // Wrapped vulkan secondary command buffers don't support texturing since we won't have an
775 // actual VkImage to texture from.
776 SkASSERT(!wrapsVkSecondaryCB);
Brian Salomon7226c232018-07-30 13:13:17 -0400777 return sk_sp<GrRenderTargetProxy>(new GrTextureRenderTargetProxy(
Greg Daniel3a365112020-02-14 10:47:18 -0500778 *this->caps(), std::move(callback), format, dimensions, sampleCnt,
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400779 textureInfo->fMipMapped, mipMapsStatus, fit, budgeted, isProtected, surfaceFlags,
780 useAllocator));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500781 }
782
Greg Danielb085fa92019-03-05 16:55:12 -0500783 GrRenderTargetProxy::WrapsVkSecondaryCB vkSCB =
784 wrapsVkSecondaryCB ? GrRenderTargetProxy::WrapsVkSecondaryCB::kYes
785 : GrRenderTargetProxy::WrapsVkSecondaryCB::kNo;
786
Greg Daniel3a365112020-02-14 10:47:18 -0500787 return sk_sp<GrRenderTargetProxy>(
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400788 new GrRenderTargetProxy(std::move(callback), format, dimensions, sampleCnt, fit,
789 budgeted, isProtected, surfaceFlags, useAllocator, vkSCB));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500790}
791
Brian Salomonbeb7f522019-08-30 16:19:42 -0400792sk_sp<GrTextureProxy> GrProxyProvider::MakeFullyLazyProxy(LazyInstantiateCallback&& callback,
793 const GrBackendFormat& format,
794 GrRenderable renderable,
795 int renderTargetSampleCnt,
796 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400797 const GrCaps& caps,
798 UseAllocator useAllocator) {
Robert Phillips0a15cc62019-07-30 12:49:10 -0400799 if (!format.isValid()) {
800 return nullptr;
801 }
802
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400803 SkASSERT(renderTargetSampleCnt == 1 || renderable == GrRenderable::kYes);
Robert Phillips10d17212019-04-24 14:09:10 -0400804 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNone;
Robert Phillips777707b2018-01-17 11:40:14 -0500805
Brian Salomona56a7462020-02-07 14:17:25 -0500806 static constexpr SkISize kLazyDims = {-1, -1};
Brian Salomonbeb7f522019-08-30 16:19:42 -0400807 if (GrRenderable::kYes == renderable) {
808 return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
Greg Daniel3a365112020-02-14 10:47:18 -0500809 caps, std::move(callback), format, kLazyDims, renderTargetSampleCnt,
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400810 GrMipMapped::kNo, GrMipMapsStatus::kNotAllocated, SkBackingFit::kApprox,
Greg Daniel14b57212019-12-17 16:18:06 -0500811 SkBudgeted::kYes, isProtected, surfaceFlags, useAllocator));
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400812 } else {
813 return sk_sp<GrTextureProxy>(
814 new GrTextureProxy(std::move(callback), format, kLazyDims, GrMipMapped::kNo,
815 GrMipMapsStatus::kNotAllocated, SkBackingFit::kApprox,
816 SkBudgeted::kYes, isProtected, surfaceFlags, useAllocator));
Brian Salomonbeb7f522019-08-30 16:19:42 -0400817 }
Robert Phillips777707b2018-01-17 11:40:14 -0500818}
819
Robert Phillips427966a2018-12-20 17:20:43 -0500820void GrProxyProvider::processInvalidUniqueKey(const GrUniqueKey& key, GrTextureProxy* proxy,
821 InvalidateGPUResource invalidateGPUResource) {
Mike Klein04ef8102020-03-16 12:44:23 -0500822 this->processInvalidUniqueKeyImpl(key, proxy, invalidateGPUResource, RemoveTableEntry::kYes);
823}
824
825void GrProxyProvider::processInvalidUniqueKeyImpl(const GrUniqueKey& key, GrTextureProxy* proxy,
826 InvalidateGPUResource invalidateGPUResource,
827 RemoveTableEntry removeTableEntry) {
Chris Dalton2de13dd2019-01-03 15:11:59 -0700828 SkASSERT(key.isValid());
829
Robert Phillips427966a2018-12-20 17:20:43 -0500830 if (!proxy) {
831 proxy = fUniquelyKeyedProxies.find(key);
832 }
Chris Dalton2de13dd2019-01-03 15:11:59 -0700833 SkASSERT(!proxy || proxy->getUniqueKey() == key);
834
835 // Locate the corresponding GrGpuResource (if it needs to be invalidated) before clearing the
836 // proxy's unique key. We must do it in this order because 'key' may alias the proxy's key.
837 sk_sp<GrGpuResource> invalidGpuResource;
838 if (InvalidateGPUResource::kYes == invalidateGPUResource) {
Brian Salomon01ceae92019-04-02 11:49:54 -0400839 GrContext* direct = fImageContext->priv().asDirectContext();
840 if (direct) {
841 GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
842 invalidGpuResource = resourceProvider->findByUniqueKey<GrGpuResource>(key);
Chris Dalton2de13dd2019-01-03 15:11:59 -0700843 }
844 SkASSERT(!invalidGpuResource || invalidGpuResource->getUniqueKey() == key);
845 }
Robert Phillips427966a2018-12-20 17:20:43 -0500846
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500847 // Note: this method is called for the whole variety of GrGpuResources so often 'key'
848 // will not be in 'fUniquelyKeyedProxies'.
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500849 if (proxy) {
Mike Klein04ef8102020-03-16 12:44:23 -0500850 if (removeTableEntry == RemoveTableEntry::kYes) {
851 fUniquelyKeyedProxies.remove(key);
852 }
Robert Phillips427966a2018-12-20 17:20:43 -0500853 proxy->cacheAccess().clearUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500854 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500855
Chris Dalton2de13dd2019-01-03 15:11:59 -0700856 if (invalidGpuResource) {
857 invalidGpuResource->resourcePriv().removeUniqueKey();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500858 }
859}
860
Robert Phillipsa41c6852019-02-07 10:44:10 -0500861uint32_t GrProxyProvider::contextID() const {
862 return fImageContext->priv().contextID();
863}
864
865const GrCaps* GrProxyProvider::caps() const {
866 return fImageContext->priv().caps();
867}
868
869sk_sp<const GrCaps> GrProxyProvider::refCaps() const {
870 return fImageContext->priv().refCaps();
871}
872
Robert Phillipsa9162df2019-02-11 14:12:03 -0500873bool GrProxyProvider::isAbandoned() const {
874 return fImageContext->priv().abandoned();
875}
876
Robert Phillips0790f8a2018-09-18 13:11:03 -0400877void GrProxyProvider::orphanAllUniqueKeys() {
Mike Kleincff63962020-03-14 16:22:45 -0500878 fUniquelyKeyedProxies.foreach([&](GrTextureProxy* proxy){
879 proxy->fProxyProvider = nullptr;
880 });
Robert Phillips0790f8a2018-09-18 13:11:03 -0400881}
882
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500883void GrProxyProvider::removeAllUniqueKeys() {
Mike Klein04ef8102020-03-16 12:44:23 -0500884 fUniquelyKeyedProxies.foreach([&](GrTextureProxy* proxy){
885 // It's not safe to remove table entries while iterating with foreach(),
886 // but since we're going to remove them all anyway, simply save that for the end.
887 this->processInvalidUniqueKeyImpl(proxy->getUniqueKey(), proxy,
888 InvalidateGPUResource::kNo,
889 RemoveTableEntry::kNo);
890 });
891 // Removing all those table entries is safe now.
892 fUniquelyKeyedProxies.reset();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500893}
Robert Phillipsa41c6852019-02-07 10:44:10 -0500894
895bool GrProxyProvider::renderingDirectly() const {
896 return fImageContext->priv().asDirectContext();
897}