Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/GrProxyProvider.h" |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkBitmap.h" |
| 11 | #include "include/core/SkImage.h" |
| 12 | #include "include/gpu/GrContext.h" |
| 13 | #include "include/gpu/GrRenderTarget.h" |
| 14 | #include "include/gpu/GrTexture.h" |
| 15 | #include "include/private/GrImageContext.h" |
| 16 | #include "include/private/GrResourceKey.h" |
| 17 | #include "include/private/GrSingleOwner.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "include/private/SkImageInfoPriv.h" |
| 19 | #include "src/core/SkAutoPixmapStorage.h" |
| 20 | #include "src/core/SkImagePriv.h" |
| 21 | #include "src/core/SkMipMap.h" |
| 22 | #include "src/core/SkTraceEvent.h" |
| 23 | #include "src/gpu/GrCaps.h" |
| 24 | #include "src/gpu/GrContextPriv.h" |
| 25 | #include "src/gpu/GrImageContextPriv.h" |
| 26 | #include "src/gpu/GrResourceProvider.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 27 | #include "src/gpu/GrSurfaceProxy.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 28 | #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 Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 33 | |
| 34 | #define ASSERT_SINGLE_OWNER \ |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 35 | SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fImageContext->priv().singleOwner());) |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 36 | |
Robert Phillips | a9162df | 2019-02-11 14:12:03 -0500 | [diff] [blame] | 37 | GrProxyProvider::GrProxyProvider(GrImageContext* imageContext) : fImageContext(imageContext) {} |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 38 | |
| 39 | GrProxyProvider::~GrProxyProvider() { |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 40 | if (this->renderingDirectly()) { |
Robert Phillips | 0790f8a | 2018-09-18 13:11:03 -0400 | [diff] [blame] | 41 | // 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 Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 46 | } |
| 47 | |
Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 48 | bool GrProxyProvider::assignUniqueKeyToProxy(const GrUniqueKey& key, GrTextureProxy* proxy) { |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 49 | ASSERT_SINGLE_OWNER |
| 50 | SkASSERT(key.isValid()); |
| 51 | if (this->isAbandoned() || !proxy) { |
Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 52 | return false; |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 53 | } |
| 54 | |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 55 | #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 Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 67 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 68 | 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 Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 73 | return true; |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void 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 Dalton | 2de13dd | 2019-01-03 15:11:59 -0700 | [diff] [blame] | 85 | void GrProxyProvider::removeUniqueKeyFromProxy(GrTextureProxy* proxy) { |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 86 | ASSERT_SINGLE_OWNER |
Chris Dalton | 2de13dd | 2019-01-03 15:11:59 -0700 | [diff] [blame] | 87 | SkASSERT(proxy); |
| 88 | SkASSERT(proxy->getUniqueKey().isValid()); |
| 89 | |
| 90 | if (this->isAbandoned()) { |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 91 | return; |
| 92 | } |
Robert Phillips | 0790f8a | 2018-09-18 13:11:03 -0400 | [diff] [blame] | 93 | |
Chris Dalton | 2de13dd | 2019-01-03 15:11:59 -0700 | [diff] [blame] | 94 | this->processInvalidUniqueKey(proxy->getUniqueKey(), proxy, InvalidateGPUResource::kYes); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | sk_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 Salomon | 01ceae9 | 2019-04-02 11:49:54 -0400 | [diff] [blame] | 105 | GrTextureProxy* proxy = fUniquelyKeyedProxies.find(key); |
Brian Salomon | 01ceae9 | 2019-04-02 11:49:54 -0400 | [diff] [blame] | 106 | if (proxy) { |
Robert Phillips | e5f7328 | 2019-06-18 17:15:04 -0400 | [diff] [blame] | 107 | SkASSERT(proxy->getProxyRefCnt() >= 1); |
| 108 | SkASSERT(proxy->origin() == origin); |
| 109 | return sk_ref_sp(proxy); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 110 | } |
Robert Phillips | e5f7328 | 2019-06-18 17:15:04 -0400 | [diff] [blame] | 111 | return nullptr; |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 112 | } |
| 113 | |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 114 | /////////////////////////////////////////////////////////////////////////////// |
| 115 | |
| 116 | #if GR_TEST_UTILS |
| 117 | sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createInstantiatedProxy( |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 118 | const GrSurfaceDesc& desc, GrRenderable renderable, GrSurfaceOrigin origin, |
Brian Salomon | e8a766b | 2019-07-19 14:24:36 -0400 | [diff] [blame^] | 119 | SkBackingFit fit, SkBudgeted budgeted, GrProtected isProtected) { |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 120 | GrContext* direct = fImageContext->priv().asDirectContext(); |
| 121 | if (!direct) { |
| 122 | return nullptr; |
| 123 | } |
| 124 | |
| 125 | GrResourceProvider* resourceProvider = direct->priv().resourceProvider(); |
| 126 | sk_sp<GrTexture> tex; |
| 127 | |
| 128 | if (SkBackingFit::kApprox == fit) { |
Brian Salomon | e8a766b | 2019-07-19 14:24:36 -0400 | [diff] [blame^] | 129 | tex = resourceProvider->createApproxTexture(desc, renderable, isProtected, |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 130 | GrResourceProvider::Flags::kNoPendingIO); |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 131 | } else { |
Brian Salomon | e8a766b | 2019-07-19 14:24:36 -0400 | [diff] [blame^] | 132 | tex = resourceProvider->createTexture(desc, renderable, budgeted, isProtected, |
Robert Phillips | 9313aa7 | 2019-04-09 18:41:27 -0400 | [diff] [blame] | 133 | GrResourceProvider::Flags::kNoPendingIO); |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 134 | } |
| 135 | if (!tex) { |
| 136 | return nullptr; |
| 137 | } |
| 138 | |
| 139 | return this->createWrapped(std::move(tex), origin); |
| 140 | } |
| 141 | |
| 142 | sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createWrapped(sk_sp<GrTexture> tex, |
| 143 | GrSurfaceOrigin origin) { |
| 144 | return this->createWrapped(std::move(tex), origin); |
| 145 | } |
| 146 | #endif |
| 147 | |
Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 148 | sk_sp<GrTextureProxy> GrProxyProvider::createWrapped(sk_sp<GrTexture> tex, GrSurfaceOrigin origin) { |
| 149 | #ifdef SK_DEBUG |
| 150 | if (tex->getUniqueKey().isValid()) { |
| 151 | SkASSERT(!this->findProxyByUniqueKey(tex->getUniqueKey(), origin)); |
| 152 | } |
| 153 | #endif |
Greg Daniel | 2c19e7f | 2019-06-18 13:29:21 -0400 | [diff] [blame] | 154 | GrColorType colorType = GrPixelConfigToColorType(tex->config()); |
| 155 | GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(tex->backendFormat(), colorType); |
Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 156 | |
| 157 | if (tex->asRenderTarget()) { |
Greg Daniel | 2c19e7f | 2019-06-18 13:29:21 -0400 | [diff] [blame] | 158 | GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(tex->backendFormat(), colorType); |
| 159 | return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin, |
| 160 | texSwizzle, outSwizzle)); |
Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 161 | } else { |
Greg Daniel | 2c19e7f | 2019-06-18 13:29:21 -0400 | [diff] [blame] | 162 | return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin, texSwizzle)); |
Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 166 | sk_sp<GrTextureProxy> GrProxyProvider::findOrCreateProxyByUniqueKey(const GrUniqueKey& key, |
| 167 | GrSurfaceOrigin origin) { |
| 168 | ASSERT_SINGLE_OWNER |
| 169 | |
| 170 | if (this->isAbandoned()) { |
| 171 | return nullptr; |
| 172 | } |
| 173 | |
| 174 | sk_sp<GrTextureProxy> result = this->findProxyByUniqueKey(key, origin); |
| 175 | if (result) { |
| 176 | return result; |
| 177 | } |
| 178 | |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 179 | GrContext* direct = fImageContext->priv().asDirectContext(); |
| 180 | if (!direct) { |
Robert Phillips | d5f9cdd | 2018-01-31 09:29:48 -0500 | [diff] [blame] | 181 | return nullptr; |
| 182 | } |
| 183 | |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 184 | GrResourceCache* resourceCache = direct->priv().getResourceCache(); |
| 185 | |
| 186 | GrGpuResource* resource = resourceCache->findAndRefUniqueResource(key); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 187 | if (!resource) { |
| 188 | return nullptr; |
| 189 | } |
| 190 | |
| 191 | sk_sp<GrTexture> texture(static_cast<GrSurface*>(resource)->asTexture()); |
| 192 | SkASSERT(texture); |
| 193 | |
Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 194 | result = this->createWrapped(std::move(texture), origin); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 195 | SkASSERT(result->getUniqueKey() == key); |
Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 196 | // createWrapped should've added this for us |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 197 | SkASSERT(fUniquelyKeyedProxies.find(key)); |
| 198 | return result; |
| 199 | } |
| 200 | |
Greg Daniel | 9d86f1d | 2018-01-29 09:33:59 -0500 | [diff] [blame] | 201 | sk_sp<GrTextureProxy> GrProxyProvider::createTextureProxy(sk_sp<SkImage> srcImage, |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 202 | GrRenderable renderable, |
Greg Daniel | 9d86f1d | 2018-01-29 09:33:59 -0500 | [diff] [blame] | 203 | int sampleCnt, |
Greg Daniel | fb3abcd | 2018-02-02 15:48:33 -0500 | [diff] [blame] | 204 | SkBudgeted budgeted, |
Chris Dalton | d004e0b | 2018-09-27 09:28:03 -0600 | [diff] [blame] | 205 | SkBackingFit fit, |
| 206 | GrInternalSurfaceFlags surfaceFlags) { |
Greg Daniel | 9d86f1d | 2018-01-29 09:33:59 -0500 | [diff] [blame] | 207 | ASSERT_SINGLE_OWNER |
| 208 | SkASSERT(srcImage); |
| 209 | |
| 210 | if (this->isAbandoned()) { |
| 211 | return nullptr; |
| 212 | } |
| 213 | |
Brian Salomon | 5ad6fd3 | 2019-03-21 15:30:08 -0400 | [diff] [blame] | 214 | const SkImageInfo& info = srcImage->imageInfo(); |
Robert Phillips | d8f79a2 | 2019-06-24 13:25:42 -0400 | [diff] [blame] | 215 | SkColorType ct = info.colorType(); |
Greg Daniel | 2f2caea | 2019-07-08 14:24:47 -0400 | [diff] [blame] | 216 | GrColorType grCT = SkColorTypeToGrColorType(ct); |
Greg Daniel | f87651e | 2018-02-21 11:36:53 -0500 | [diff] [blame] | 217 | |
Greg Daniel | 627d053 | 2019-07-08 16:48:14 -0400 | [diff] [blame] | 218 | GrBackendFormat format = this->caps()->getBackendFormatFromColorType(grCT); |
Greg Daniel | 0a7aa14 | 2018-02-21 13:02:32 -0500 | [diff] [blame] | 219 | |
Greg Daniel | 7b1bebf | 2019-07-16 17:09:59 -0400 | [diff] [blame] | 220 | if (!format.isValid() || !this->caps()->isFormatTexturable(grCT, format)) { |
Brian Osman | d29dcd1 | 2018-09-13 15:04:29 -0400 | [diff] [blame] | 221 | SkBitmap copy8888; |
| 222 | if (!copy8888.tryAllocPixels(info.makeColorType(kRGBA_8888_SkColorType)) || |
| 223 | !srcImage->readPixels(copy8888.pixmap(), 0, 0)) { |
| 224 | return nullptr; |
| 225 | } |
| 226 | copy8888.setImmutable(); |
| 227 | srcImage = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode); |
Jim Van Verth | c809832 | 2019-04-16 10:50:01 -0400 | [diff] [blame] | 228 | ct = kRGBA_8888_SkColorType; |
Greg Daniel | 627d053 | 2019-07-08 16:48:14 -0400 | [diff] [blame] | 229 | grCT = GrColorType::kRGBA_8888; |
| 230 | format = this->caps()->getBackendFormatFromColorType(grCT); |
Robert Phillips | d8f79a2 | 2019-06-24 13:25:42 -0400 | [diff] [blame] | 231 | if (!format.isValid()) { |
| 232 | return nullptr; |
| 233 | } |
Brian Osman | d29dcd1 | 2018-09-13 15:04:29 -0400 | [diff] [blame] | 234 | } |
| 235 | |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 236 | if (renderable == GrRenderable::kYes) { |
Greg Daniel | 5c96db8 | 2019-07-09 14:06:58 -0400 | [diff] [blame] | 237 | sampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, grCT, format); |
Greg Daniel | f87651e | 2018-02-21 11:36:53 -0500 | [diff] [blame] | 238 | if (!sampleCnt) { |
| 239 | return nullptr; |
| 240 | } |
| 241 | } |
| 242 | |
Robert Phillips | d8f79a2 | 2019-06-24 13:25:42 -0400 | [diff] [blame] | 243 | GrPixelConfig config = SkColorType2GrPixelConfig(ct); |
| 244 | if (kUnknown_GrPixelConfig == config) { |
| 245 | return nullptr; |
| 246 | } |
| 247 | |
Greg Daniel | 9d86f1d | 2018-01-29 09:33:59 -0500 | [diff] [blame] | 248 | GrSurfaceDesc desc; |
| 249 | desc.fWidth = srcImage->width(); |
| 250 | desc.fHeight = srcImage->height(); |
Greg Daniel | 9d86f1d | 2018-01-29 09:33:59 -0500 | [diff] [blame] | 251 | desc.fSampleCnt = sampleCnt; |
Greg Daniel | f87651e | 2018-02-21 11:36:53 -0500 | [diff] [blame] | 252 | desc.fConfig = config; |
Greg Daniel | 9d86f1d | 2018-01-29 09:33:59 -0500 | [diff] [blame] | 253 | |
| 254 | sk_sp<GrTextureProxy> proxy = this->createLazyProxy( |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 255 | [desc, renderable, budgeted, srcImage, fit](GrResourceProvider* resourceProvider) { |
Greg Daniel | 9d86f1d | 2018-01-29 09:33:59 -0500 | [diff] [blame] | 256 | SkPixmap pixMap; |
| 257 | SkAssertResult(srcImage->peekPixels(&pixMap)); |
| 258 | GrMipLevel mipLevel = { pixMap.addr(), pixMap.rowBytes() }; |
| 259 | |
Brian Salomon | e8a766b | 2019-07-19 14:24:36 -0400 | [diff] [blame^] | 260 | return LazyInstantiationResult(resourceProvider->createTexture( |
| 261 | desc, renderable, budgeted, fit, GrProtected::kNo, mipLevel, |
| 262 | GrResourceProvider::Flags::kNoPendingIO)); |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 263 | }, |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 264 | format, desc, renderable, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, surfaceFlags, fit, |
Brian Salomon | e8a766b | 2019-07-19 14:24:36 -0400 | [diff] [blame^] | 265 | budgeted, GrProtected::kNo); |
Greg Daniel | 9d86f1d | 2018-01-29 09:33:59 -0500 | [diff] [blame] | 266 | |
Greg Daniel | 92cbf3f | 2018-04-12 16:50:17 -0400 | [diff] [blame] | 267 | if (!proxy) { |
| 268 | return nullptr; |
| 269 | } |
| 270 | |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 271 | GrContext* direct = fImageContext->priv().asDirectContext(); |
| 272 | if (direct) { |
| 273 | GrResourceProvider* resourceProvider = direct->priv().resourceProvider(); |
| 274 | |
Greg Daniel | 9d86f1d | 2018-01-29 09:33:59 -0500 | [diff] [blame] | 275 | // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however |
| 276 | // we're better off instantiating the proxy immediately here. |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 277 | if (!proxy->priv().doLazyInstantiation(resourceProvider)) { |
Greg Daniel | 9d86f1d | 2018-01-29 09:33:59 -0500 | [diff] [blame] | 278 | return nullptr; |
| 279 | } |
| 280 | } |
Robert Phillips | c1b6066 | 2018-06-26 10:20:08 -0400 | [diff] [blame] | 281 | |
| 282 | SkASSERT(proxy->width() == desc.fWidth); |
| 283 | SkASSERT(proxy->height() == desc.fHeight); |
Greg Daniel | 9d86f1d | 2018-01-29 09:33:59 -0500 | [diff] [blame] | 284 | return proxy; |
| 285 | } |
| 286 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 287 | sk_sp<GrTextureProxy> GrProxyProvider::createMipMapProxy(const GrBackendFormat& format, |
| 288 | const GrSurfaceDesc& desc, |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 289 | GrRenderable renderable, |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 290 | GrSurfaceOrigin origin, |
Brian Salomon | e8a766b | 2019-07-19 14:24:36 -0400 | [diff] [blame^] | 291 | SkBudgeted budgeted, |
| 292 | GrProtected isProtected) { |
Robert Phillips | 579f094 | 2018-01-08 14:53:35 -0500 | [diff] [blame] | 293 | ASSERT_SINGLE_OWNER |
| 294 | |
| 295 | if (this->isAbandoned()) { |
| 296 | return nullptr; |
| 297 | } |
| 298 | |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 299 | return this->createProxy(format, desc, renderable, origin, GrMipMapped::kYes, |
Brian Salomon | e8a766b | 2019-07-19 14:24:36 -0400 | [diff] [blame^] | 300 | SkBackingFit::kExact, budgeted, isProtected, |
| 301 | GrInternalSurfaceFlags::kNone); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 302 | } |
| 303 | |
Brian Osman | de49665 | 2019-03-22 13:42:33 -0400 | [diff] [blame] | 304 | sk_sp<GrTextureProxy> GrProxyProvider::createProxyFromBitmap(const SkBitmap& bitmap, |
| 305 | GrMipMapped mipMapped) { |
Brian Osman | 412674f | 2019-02-07 15:34:58 -0500 | [diff] [blame] | 306 | ASSERT_SINGLE_OWNER |
Brian Osman | 7dcc616 | 2019-03-25 10:12:57 -0400 | [diff] [blame] | 307 | SkASSERT(GrMipMapped::kNo == mipMapped || this->caps()->mipMapSupport()); |
Brian Osman | 412674f | 2019-02-07 15:34:58 -0500 | [diff] [blame] | 308 | |
| 309 | if (this->isAbandoned()) { |
| 310 | return nullptr; |
| 311 | } |
| 312 | |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 313 | if (!SkImageInfoIsValid(bitmap.info())) { |
Greg Daniel | a4ead65 | 2018-02-07 10:21:48 -0500 | [diff] [blame] | 314 | return nullptr; |
| 315 | } |
| 316 | |
Brian Osman | de49665 | 2019-03-22 13:42:33 -0400 | [diff] [blame] | 317 | ATRACE_ANDROID_FRAMEWORK("Upload %sTexture [%ux%u]", |
| 318 | GrMipMapped::kYes == mipMapped ? "MipMap " : "", |
| 319 | bitmap.width(), bitmap.height()); |
Greg Daniel | a4ead65 | 2018-02-07 10:21:48 -0500 | [diff] [blame] | 320 | |
| 321 | // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap |
| 322 | // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the |
| 323 | // upload of the data to the gpu can happen at anytime and the bitmap may change by then. |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 324 | SkCopyPixelsMode copyMode = this->renderingDirectly() ? kNever_SkCopyPixelsMode |
| 325 | : kIfMutable_SkCopyPixelsMode; |
Greg Daniel | a4ead65 | 2018-02-07 10:21:48 -0500 | [diff] [blame] | 326 | sk_sp<SkImage> baseLevel = SkMakeImageFromRasterBitmap(bitmap, copyMode); |
Greg Daniel | a4ead65 | 2018-02-07 10:21:48 -0500 | [diff] [blame] | 327 | if (!baseLevel) { |
| 328 | return nullptr; |
| 329 | } |
| 330 | |
Brian Osman | de49665 | 2019-03-22 13:42:33 -0400 | [diff] [blame] | 331 | // If mips weren't requested (or this was too small to have any), then take the fast path |
| 332 | if (GrMipMapped::kNo == mipMapped || |
| 333 | 0 == SkMipMap::ComputeLevelCount(baseLevel->width(), baseLevel->height())) { |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 334 | return this->createTextureProxy(std::move(baseLevel), GrRenderable::kNo, 1, |
Brian Osman | 7dcc616 | 2019-03-25 10:12:57 -0400 | [diff] [blame] | 335 | SkBudgeted::kYes, SkBackingFit::kExact); |
Greg Daniel | a4ead65 | 2018-02-07 10:21:48 -0500 | [diff] [blame] | 336 | } |
| 337 | |
Greg Daniel | 627d053 | 2019-07-08 16:48:14 -0400 | [diff] [blame] | 338 | GrColorType grColorType = SkColorTypeToGrColorType(bitmap.info().colorType()); |
| 339 | GrBackendFormat format = this->caps()->getBackendFormatFromColorType(grColorType); |
Robert Phillips | d8f79a2 | 2019-06-24 13:25:42 -0400 | [diff] [blame] | 340 | if (!format.isValid()) { |
| 341 | return nullptr; |
| 342 | } |
Brian Osman | d29dcd1 | 2018-09-13 15:04:29 -0400 | [diff] [blame] | 343 | GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info()); |
Greg Daniel | 3fc5df4 | 2019-06-14 09:42:17 -0400 | [diff] [blame] | 344 | |
Greg Daniel | 2f2caea | 2019-07-08 14:24:47 -0400 | [diff] [blame] | 345 | if (!this->caps()->isFormatTexturable(grColorType, format)) { |
Brian Osman | d29dcd1 | 2018-09-13 15:04:29 -0400 | [diff] [blame] | 346 | SkBitmap copy8888; |
| 347 | if (!copy8888.tryAllocPixels(bitmap.info().makeColorType(kRGBA_8888_SkColorType)) || |
| 348 | !bitmap.readPixels(copy8888.pixmap())) { |
| 349 | return nullptr; |
| 350 | } |
| 351 | copy8888.setImmutable(); |
| 352 | baseLevel = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode); |
| 353 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
Greg Daniel | 627d053 | 2019-07-08 16:48:14 -0400 | [diff] [blame] | 354 | grColorType = GrColorType::kRGBA_8888; |
| 355 | format = this->caps()->getBackendFormatFromColorType(grColorType); |
Robert Phillips | d8f79a2 | 2019-06-24 13:25:42 -0400 | [diff] [blame] | 356 | if (!format.isValid()) { |
| 357 | return nullptr; |
| 358 | } |
Greg Daniel | 3fc5df4 | 2019-06-14 09:42:17 -0400 | [diff] [blame] | 359 | } |
| 360 | |
Brian Osman | d29dcd1 | 2018-09-13 15:04:29 -0400 | [diff] [blame] | 361 | |
| 362 | SkPixmap pixmap; |
| 363 | SkAssertResult(baseLevel->peekPixels(&pixmap)); |
| 364 | sk_sp<SkMipMap> mipmaps(SkMipMap::Build(pixmap, nullptr)); |
Brian Osman | bc6b9cb | 2018-09-13 13:43:25 -0400 | [diff] [blame] | 365 | if (!mipmaps) { |
| 366 | return nullptr; |
| 367 | } |
| 368 | |
Greg Daniel | a4ead65 | 2018-02-07 10:21:48 -0500 | [diff] [blame] | 369 | sk_sp<GrTextureProxy> proxy = this->createLazyProxy( |
Brian Osman | 1b97f13 | 2018-09-13 17:33:48 +0000 | [diff] [blame] | 370 | [desc, baseLevel, mipmaps](GrResourceProvider* resourceProvider) { |
Brian Osman | 1b97f13 | 2018-09-13 17:33:48 +0000 | [diff] [blame] | 371 | const int mipLevelCount = mipmaps->countLevels() + 1; |
| 372 | std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]); |
| 373 | |
Greg Daniel | a4ead65 | 2018-02-07 10:21:48 -0500 | [diff] [blame] | 374 | SkPixmap pixmap; |
| 375 | SkAssertResult(baseLevel->peekPixels(&pixmap)); |
| 376 | |
| 377 | // DDL TODO: Instead of copying all this info into GrMipLevels we should just plumb |
| 378 | // the use of SkMipMap down through Ganesh. |
| 379 | texels[0].fPixels = pixmap.addr(); |
| 380 | texels[0].fRowBytes = pixmap.rowBytes(); |
| 381 | |
| 382 | for (int i = 1; i < mipLevelCount; ++i) { |
| 383 | SkMipMap::Level generatedMipLevel; |
| 384 | mipmaps->getLevel(i - 1, &generatedMipLevel); |
| 385 | texels[i].fPixels = generatedMipLevel.fPixmap.addr(); |
| 386 | texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes(); |
| 387 | SkASSERT(texels[i].fPixels); |
| 388 | } |
| 389 | |
Brian Salomon | b6a3a3b | 2019-04-01 12:29:34 -0400 | [diff] [blame] | 390 | return LazyInstantiationResult(resourceProvider->createTexture( |
Brian Salomon | e8a766b | 2019-07-19 14:24:36 -0400 | [diff] [blame^] | 391 | desc, GrRenderable::kNo, SkBudgeted::kYes, GrProtected::kNo, texels.get(), |
| 392 | mipLevelCount)); |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 393 | }, |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 394 | format, desc, GrRenderable::kNo, kTopLeft_GrSurfaceOrigin, GrMipMapped::kYes, |
Brian Salomon | e8a766b | 2019-07-19 14:24:36 -0400 | [diff] [blame^] | 395 | SkBackingFit::kExact, SkBudgeted::kYes, GrProtected::kNo); |
Greg Daniel | a4ead65 | 2018-02-07 10:21:48 -0500 | [diff] [blame] | 396 | |
Greg Daniel | 92cbf3f | 2018-04-12 16:50:17 -0400 | [diff] [blame] | 397 | if (!proxy) { |
| 398 | return nullptr; |
| 399 | } |
| 400 | |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 401 | GrContext* direct = fImageContext->priv().asDirectContext(); |
| 402 | if (direct) { |
| 403 | GrResourceProvider* resourceProvider = direct->priv().resourceProvider(); |
Greg Daniel | a4ead65 | 2018-02-07 10:21:48 -0500 | [diff] [blame] | 404 | // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however |
| 405 | // we're better off instantiating the proxy immediately here. |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 406 | if (!proxy->priv().doLazyInstantiation(resourceProvider)) { |
Greg Daniel | a4ead65 | 2018-02-07 10:21:48 -0500 | [diff] [blame] | 407 | return nullptr; |
| 408 | } |
| 409 | } |
| 410 | return proxy; |
| 411 | } |
| 412 | |
Greg Daniel | fa55f2e | 2019-06-13 15:21:38 -0400 | [diff] [blame] | 413 | #ifdef SK_DEBUG |
| 414 | static bool validate_backend_format_and_config(const GrCaps* caps, |
| 415 | const GrBackendFormat& format, |
| 416 | GrPixelConfig config) { |
| 417 | if (kUnknown_GrPixelConfig == config) { |
| 418 | return false; |
| 419 | } |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 420 | if (GrPixelConfigIsCompressed(config)) { |
| 421 | // We have no way to verify these at the moment. |
| 422 | return true; |
| 423 | } |
Robert Phillips | c046ff0 | 2019-07-01 10:34:03 -0400 | [diff] [blame] | 424 | |
Robert Phillips | 1e2cb44 | 2019-07-02 15:51:28 -0400 | [diff] [blame] | 425 | GrColorType grCT = GrPixelConfigToColorType(config); |
| 426 | |
| 427 | return caps->areColorTypeAndFormatCompatible(grCT, format); |
Greg Daniel | fa55f2e | 2019-06-13 15:21:38 -0400 | [diff] [blame] | 428 | } |
Robert Phillips | 0902c98 | 2019-07-16 07:47:56 -0400 | [diff] [blame] | 429 | |
| 430 | static bool validate_backend_format_and_colortype(const GrCaps* caps, |
| 431 | GrColorType colorType, |
| 432 | const GrBackendFormat& format) { |
| 433 | return caps->areColorTypeAndFormatCompatible(colorType, format); |
| 434 | } |
Greg Daniel | fa55f2e | 2019-06-13 15:21:38 -0400 | [diff] [blame] | 435 | #endif |
| 436 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 437 | sk_sp<GrTextureProxy> GrProxyProvider::createProxy(const GrBackendFormat& format, |
| 438 | const GrSurfaceDesc& desc, |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 439 | GrRenderable renderable, |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 440 | GrSurfaceOrigin origin, |
Greg Daniel | f6f7b67 | 2018-02-15 13:06:26 -0500 | [diff] [blame] | 441 | GrMipMapped mipMapped, |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 442 | SkBackingFit fit, |
| 443 | SkBudgeted budgeted, |
Brian Salomon | e8a766b | 2019-07-19 14:24:36 -0400 | [diff] [blame^] | 444 | GrProtected isProtected, |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 445 | GrInternalSurfaceFlags surfaceFlags) { |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 446 | if (GrPixelConfigIsCompressed(desc.fConfig)) { |
| 447 | // Deferred proxies for compressed textures are not supported. |
| 448 | return nullptr; |
| 449 | } |
Robert Phillips | 0902c98 | 2019-07-16 07:47:56 -0400 | [diff] [blame] | 450 | |
| 451 | const GrCaps* caps = this->caps(); |
| 452 | GrColorType colorType = GrPixelConfigToColorType(desc.fConfig); |
| 453 | |
| 454 | SkASSERT(GrCaps::AreConfigsCompatible(desc.fConfig, |
| 455 | caps->getConfigFromBackendFormat(format, colorType))); |
| 456 | SkASSERT(validate_backend_format_and_colortype(caps, colorType, format)); |
Greg Daniel | f6f7b67 | 2018-02-15 13:06:26 -0500 | [diff] [blame] | 457 | if (GrMipMapped::kYes == mipMapped) { |
| 458 | // SkMipMap doesn't include the base level in the level count so we have to add 1 |
| 459 | int mipCount = SkMipMap::ComputeLevelCount(desc.fWidth, desc.fHeight) + 1; |
| 460 | if (1 == mipCount) { |
| 461 | mipMapped = GrMipMapped::kNo; |
| 462 | } |
| 463 | } |
| 464 | |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 465 | if (!caps->validateSurfaceDesc(desc, renderable, mipMapped)) { |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 466 | return nullptr; |
| 467 | } |
Brian Salomon | 3a2cc2c | 2018-02-03 00:25:12 +0000 | [diff] [blame] | 468 | GrSurfaceDesc copyDesc = desc; |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 469 | if (renderable == GrRenderable::kYes) { |
Robert Phillips | 0902c98 | 2019-07-16 07:47:56 -0400 | [diff] [blame] | 470 | copyDesc.fSampleCnt = caps->getRenderTargetSampleCount(desc.fSampleCnt, colorType, format); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 471 | } |
Brian Salomon | 3a2cc2c | 2018-02-03 00:25:12 +0000 | [diff] [blame] | 472 | |
Robert Phillips | 0902c98 | 2019-07-16 07:47:56 -0400 | [diff] [blame] | 473 | GrSwizzle texSwizzle = caps->getTextureSwizzle(format, colorType); |
Greg Daniel | 2c19e7f | 2019-06-18 13:29:21 -0400 | [diff] [blame] | 474 | |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 475 | if (renderable == GrRenderable::kYes) { |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 476 | // We know anything we instantiate later from this deferred path will be |
| 477 | // both texturable and renderable |
Robert Phillips | 0902c98 | 2019-07-16 07:47:56 -0400 | [diff] [blame] | 478 | GrSwizzle outSwizzle = caps->getOutputSwizzle(format, colorType); |
| 479 | return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(*caps, format, copyDesc, |
Greg Daniel | 2c19e7f | 2019-06-18 13:29:21 -0400 | [diff] [blame] | 480 | origin, mipMapped, texSwizzle, |
| 481 | outSwizzle, fit, budgeted, |
Brian Salomon | e8a766b | 2019-07-19 14:24:36 -0400 | [diff] [blame^] | 482 | isProtected, surfaceFlags)); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 483 | } |
| 484 | |
Greg Daniel | 2c19e7f | 2019-06-18 13:29:21 -0400 | [diff] [blame] | 485 | return sk_sp<GrTextureProxy>(new GrTextureProxy(format, copyDesc, origin, mipMapped, texSwizzle, |
Brian Salomon | e8a766b | 2019-07-19 14:24:36 -0400 | [diff] [blame^] | 486 | fit, budgeted, isProtected, surfaceFlags)); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 487 | } |
| 488 | |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 489 | sk_sp<GrTextureProxy> GrProxyProvider::createCompressedTextureProxy( |
| 490 | int width, int height, SkBudgeted budgeted, SkImage::CompressionType compressionType, |
| 491 | sk_sp<SkData> data) { |
| 492 | GrBackendFormat format = this->caps()->getBackendFormatFromCompressionType(compressionType); |
| 493 | |
| 494 | GrSurfaceDesc desc; |
| 495 | desc.fConfig = GrCompressionTypePixelConfig(compressionType); |
| 496 | desc.fWidth = width; |
| 497 | desc.fHeight = height; |
| 498 | |
Jim Van Verth | ee06b33 | 2019-01-18 10:36:32 -0500 | [diff] [blame] | 499 | if (!this->caps()->isConfigTexturable(desc.fConfig)) { |
| 500 | return nullptr; |
| 501 | } |
| 502 | |
Jim Van Verth | ee06b33 | 2019-01-18 10:36:32 -0500 | [diff] [blame] | 503 | sk_sp<GrTextureProxy> proxy = this->createLazyProxy( |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 504 | [width, height, compressionType, budgeted, data](GrResourceProvider* resourceProvider) { |
| 505 | return LazyInstantiationResult(resourceProvider->createCompressedTexture( |
| 506 | width, height, compressionType, budgeted, data.get())); |
| 507 | }, |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 508 | format, desc, GrRenderable::kNo, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, |
Brian Salomon | e8a766b | 2019-07-19 14:24:36 -0400 | [diff] [blame^] | 509 | SkBackingFit::kExact, SkBudgeted::kYes, GrProtected::kNo); |
Jim Van Verth | ee06b33 | 2019-01-18 10:36:32 -0500 | [diff] [blame] | 510 | |
| 511 | if (!proxy) { |
| 512 | return nullptr; |
| 513 | } |
| 514 | |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 515 | GrContext* direct = fImageContext->priv().asDirectContext(); |
| 516 | if (direct) { |
| 517 | GrResourceProvider* resourceProvider = direct->priv().resourceProvider(); |
Jim Van Verth | ee06b33 | 2019-01-18 10:36:32 -0500 | [diff] [blame] | 518 | // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however |
| 519 | // we're better off instantiating the proxy immediately here. |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 520 | if (!proxy->priv().doLazyInstantiation(resourceProvider)) { |
Jim Van Verth | ee06b33 | 2019-01-18 10:36:32 -0500 | [diff] [blame] | 521 | return nullptr; |
| 522 | } |
| 523 | } |
| 524 | return proxy; |
| 525 | } |
| 526 | |
Brian Salomon | 7578f3e | 2018-03-07 14:39:54 -0500 | [diff] [blame] | 527 | sk_sp<GrTextureProxy> GrProxyProvider::wrapBackendTexture(const GrBackendTexture& backendTex, |
| 528 | GrSurfaceOrigin origin, |
| 529 | GrWrapOwnership ownership, |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 530 | GrWrapCacheable cacheable, |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 531 | GrIOType ioType, |
Brian Salomon | 7578f3e | 2018-03-07 14:39:54 -0500 | [diff] [blame] | 532 | ReleaseProc releaseProc, |
| 533 | ReleaseContext releaseCtx) { |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 534 | SkASSERT(ioType != kWrite_GrIOType); |
Robert Phillips | f9bec20 | 2018-01-16 09:21:01 -0500 | [diff] [blame] | 535 | if (this->isAbandoned()) { |
| 536 | return nullptr; |
| 537 | } |
| 538 | |
Brian Salomon | f777897 | 2018-03-08 10:13:17 -0500 | [diff] [blame] | 539 | // This is only supported on a direct GrContext. |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 540 | GrContext* direct = fImageContext->priv().asDirectContext(); |
| 541 | if (!direct) { |
Brian Salomon | f777897 | 2018-03-08 10:13:17 -0500 | [diff] [blame] | 542 | return nullptr; |
| 543 | } |
| 544 | |
Robert Phillips | dd39980 | 2019-07-18 12:28:00 +0000 | [diff] [blame] | 545 | SkASSERT(validate_backend_format_and_config(this->caps(), backendTex.getBackendFormat(), |
| 546 | backendTex.config())); |
Greg Daniel | fa55f2e | 2019-06-13 15:21:38 -0400 | [diff] [blame] | 547 | |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 548 | GrResourceProvider* resourceProvider = direct->priv().resourceProvider(); |
| 549 | |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 550 | sk_sp<GrTexture> tex = |
Robert Phillips | dd39980 | 2019-07-18 12:28:00 +0000 | [diff] [blame] | 551 | resourceProvider->wrapBackendTexture(backendTex, ownership, cacheable, ioType); |
Brian Salomon | f777897 | 2018-03-08 10:13:17 -0500 | [diff] [blame] | 552 | if (!tex) { |
| 553 | return nullptr; |
| 554 | } |
Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 555 | |
Greg Daniel | 6a0176b | 2018-01-30 09:28:44 -0500 | [diff] [blame] | 556 | if (releaseProc) { |
Brian Salomon | b2c5dae | 2019-03-04 10:25:17 -0500 | [diff] [blame] | 557 | tex->setRelease(releaseProc, releaseCtx); |
Greg Daniel | 6a0176b | 2018-01-30 09:28:44 -0500 | [diff] [blame] | 558 | } |
| 559 | |
Brian Salomon | f777897 | 2018-03-08 10:13:17 -0500 | [diff] [blame] | 560 | SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture |
| 561 | // Make sure we match how we created the proxy with SkBudgeted::kNo |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 562 | SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType()); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 563 | |
Robert Phillips | dd39980 | 2019-07-18 12:28:00 +0000 | [diff] [blame] | 564 | GrColorType colorType = GrPixelConfigToColorType(tex->config()); |
| 565 | GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(tex->backendFormat(), colorType); |
Greg Daniel | 2c19e7f | 2019-06-18 13:29:21 -0400 | [diff] [blame] | 566 | |
| 567 | return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin, texSwizzle)); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 568 | } |
| 569 | |
Brian Salomon | 7578f3e | 2018-03-07 14:39:54 -0500 | [diff] [blame] | 570 | sk_sp<GrTextureProxy> GrProxyProvider::wrapRenderableBackendTexture( |
Brian Salomon | 02bd295 | 2018-03-07 15:20:21 -0500 | [diff] [blame] | 571 | const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt, |
Robert Phillips | 0902c98 | 2019-07-16 07:47:56 -0400 | [diff] [blame] | 572 | GrColorType colorType, GrWrapOwnership ownership, GrWrapCacheable cacheable, |
| 573 | ReleaseProc releaseProc, ReleaseContext releaseCtx) { |
Robert Phillips | f9bec20 | 2018-01-16 09:21:01 -0500 | [diff] [blame] | 574 | if (this->isAbandoned()) { |
| 575 | return nullptr; |
| 576 | } |
| 577 | |
Brian Salomon | f777897 | 2018-03-08 10:13:17 -0500 | [diff] [blame] | 578 | // This is only supported on a direct GrContext. |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 579 | GrContext* direct = fImageContext->priv().asDirectContext(); |
| 580 | if (!direct) { |
Brian Salomon | f777897 | 2018-03-08 10:13:17 -0500 | [diff] [blame] | 581 | return nullptr; |
| 582 | } |
| 583 | |
Robert Phillips | 0902c98 | 2019-07-16 07:47:56 -0400 | [diff] [blame] | 584 | const GrCaps* caps = this->caps(); |
| 585 | |
| 586 | SkASSERT(GrCaps::AreConfigsCompatible(backendTex.config(), |
| 587 | caps->getConfigFromBackendFormat( |
| 588 | backendTex.getBackendFormat(), |
| 589 | colorType))); |
| 590 | SkASSERT(validate_backend_format_and_colortype(caps, colorType, backendTex.getBackendFormat())); |
Greg Daniel | fa55f2e | 2019-06-13 15:21:38 -0400 | [diff] [blame] | 591 | |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 592 | GrResourceProvider* resourceProvider = direct->priv().resourceProvider(); |
| 593 | |
Robert Phillips | 0902c98 | 2019-07-16 07:47:56 -0400 | [diff] [blame] | 594 | sampleCnt = caps->getRenderTargetSampleCount(sampleCnt, colorType, |
| 595 | backendTex.getBackendFormat()); |
Greg Daniel | f87651e | 2018-02-21 11:36:53 -0500 | [diff] [blame] | 596 | if (!sampleCnt) { |
| 597 | return nullptr; |
| 598 | } |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 599 | |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 600 | sk_sp<GrTexture> tex = resourceProvider->wrapRenderableBackendTexture(backendTex, sampleCnt, |
Robert Phillips | 0902c98 | 2019-07-16 07:47:56 -0400 | [diff] [blame] | 601 | colorType, ownership, |
| 602 | cacheable); |
Brian Salomon | f777897 | 2018-03-08 10:13:17 -0500 | [diff] [blame] | 603 | if (!tex) { |
| 604 | return nullptr; |
Greg Daniel | 2a30390 | 2018-02-20 10:25:54 -0500 | [diff] [blame] | 605 | } |
| 606 | |
Greg Daniel | 8ce7991 | 2019-02-05 10:08:43 -0500 | [diff] [blame] | 607 | if (releaseProc) { |
Brian Salomon | b2c5dae | 2019-03-04 10:25:17 -0500 | [diff] [blame] | 608 | tex->setRelease(releaseProc, releaseCtx); |
Greg Daniel | 8ce7991 | 2019-02-05 10:08:43 -0500 | [diff] [blame] | 609 | } |
| 610 | |
Brian Salomon | f777897 | 2018-03-08 10:13:17 -0500 | [diff] [blame] | 611 | SkASSERT(tex->asRenderTarget()); // A GrTextureRenderTarget |
| 612 | // Make sure we match how we created the proxy with SkBudgeted::kNo |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 613 | SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType()); |
Greg Daniel | 6abda43 | 2018-02-15 14:55:00 -0500 | [diff] [blame] | 614 | |
Robert Phillips | 0902c98 | 2019-07-16 07:47:56 -0400 | [diff] [blame] | 615 | GrSwizzle texSwizzle = caps->getTextureSwizzle(tex->backendFormat(), colorType); |
| 616 | GrSwizzle outSwizzle = caps->getOutputSwizzle(tex->backendFormat(), colorType); |
Greg Daniel | 2c19e7f | 2019-06-18 13:29:21 -0400 | [diff] [blame] | 617 | |
| 618 | return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin, texSwizzle, |
| 619 | outSwizzle)); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 620 | } |
| 621 | |
Brian Salomon | 7578f3e | 2018-03-07 14:39:54 -0500 | [diff] [blame] | 622 | sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget( |
Robert Phillips | dd39980 | 2019-07-18 12:28:00 +0000 | [diff] [blame] | 623 | const GrBackendRenderTarget& backendRT, GrSurfaceOrigin origin, ReleaseProc releaseProc, |
| 624 | ReleaseContext releaseCtx) { |
Robert Phillips | f9bec20 | 2018-01-16 09:21:01 -0500 | [diff] [blame] | 625 | if (this->isAbandoned()) { |
| 626 | return nullptr; |
| 627 | } |
| 628 | |
Brian Salomon | f777897 | 2018-03-08 10:13:17 -0500 | [diff] [blame] | 629 | // This is only supported on a direct GrContext. |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 630 | GrContext* direct = fImageContext->priv().asDirectContext(); |
| 631 | if (!direct) { |
Brian Salomon | f777897 | 2018-03-08 10:13:17 -0500 | [diff] [blame] | 632 | return nullptr; |
Greg Daniel | 2a30390 | 2018-02-20 10:25:54 -0500 | [diff] [blame] | 633 | } |
| 634 | |
Robert Phillips | dd39980 | 2019-07-18 12:28:00 +0000 | [diff] [blame] | 635 | GrColorType colorType = GrPixelConfigToColorType(backendRT.config()); |
Greg Daniel | 2c19e7f | 2019-06-18 13:29:21 -0400 | [diff] [blame] | 636 | #ifdef SK_DEBUG |
Greg Daniel | fa55f2e | 2019-06-13 15:21:38 -0400 | [diff] [blame] | 637 | GrPixelConfig testConfig = |
Robert Phillips | dd39980 | 2019-07-18 12:28:00 +0000 | [diff] [blame] | 638 | this->caps()->validateBackendRenderTarget(backendRT, colorType); |
Greg Daniel | fa55f2e | 2019-06-13 15:21:38 -0400 | [diff] [blame] | 639 | SkASSERT(testConfig != kUnknown_GrPixelConfig); |
| 640 | #endif |
| 641 | |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 642 | GrResourceProvider* resourceProvider = direct->priv().resourceProvider(); |
| 643 | |
Robert Phillips | dd39980 | 2019-07-18 12:28:00 +0000 | [diff] [blame] | 644 | sk_sp<GrRenderTarget> rt = resourceProvider->wrapBackendRenderTarget(backendRT); |
Brian Salomon | f777897 | 2018-03-08 10:13:17 -0500 | [diff] [blame] | 645 | if (!rt) { |
| 646 | return nullptr; |
Greg Daniel | 2a30390 | 2018-02-20 10:25:54 -0500 | [diff] [blame] | 647 | } |
Greg Daniel | 8ce7991 | 2019-02-05 10:08:43 -0500 | [diff] [blame] | 648 | |
Greg Daniel | 8ce7991 | 2019-02-05 10:08:43 -0500 | [diff] [blame] | 649 | if (releaseProc) { |
Brian Salomon | 2ca31f8 | 2019-03-05 13:28:58 -0500 | [diff] [blame] | 650 | rt->setRelease(releaseProc, releaseCtx); |
Greg Daniel | 8ce7991 | 2019-02-05 10:08:43 -0500 | [diff] [blame] | 651 | } |
| 652 | |
Brian Salomon | f777897 | 2018-03-08 10:13:17 -0500 | [diff] [blame] | 653 | SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable |
| 654 | SkASSERT(!rt->getUniqueKey().isValid()); |
| 655 | // Make sure we match how we created the proxy with SkBudgeted::kNo |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 656 | SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType()); |
Brian Salomon | f777897 | 2018-03-08 10:13:17 -0500 | [diff] [blame] | 657 | |
Robert Phillips | dd39980 | 2019-07-18 12:28:00 +0000 | [diff] [blame] | 658 | GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(rt->backendFormat(), colorType); |
| 659 | GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(rt->backendFormat(), colorType); |
Greg Daniel | 2c19e7f | 2019-06-18 13:29:21 -0400 | [diff] [blame] | 660 | |
| 661 | return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(std::move(rt), origin, texSwizzle, |
| 662 | outSwizzle)); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 663 | } |
| 664 | |
Brian Salomon | 7578f3e | 2018-03-07 14:39:54 -0500 | [diff] [blame] | 665 | sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendTextureAsRenderTarget( |
Robert Phillips | dd39980 | 2019-07-18 12:28:00 +0000 | [diff] [blame] | 666 | const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt) { |
Robert Phillips | f9bec20 | 2018-01-16 09:21:01 -0500 | [diff] [blame] | 667 | if (this->isAbandoned()) { |
| 668 | return nullptr; |
| 669 | } |
| 670 | |
Brian Salomon | f777897 | 2018-03-08 10:13:17 -0500 | [diff] [blame] | 671 | // This is only supported on a direct GrContext. |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 672 | GrContext* direct = fImageContext->priv().asDirectContext(); |
| 673 | if (!direct) { |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 674 | return nullptr; |
| 675 | } |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 676 | |
Robert Phillips | dd39980 | 2019-07-18 12:28:00 +0000 | [diff] [blame] | 677 | SkASSERT(validate_backend_format_and_config(this->caps(), backendTex.getBackendFormat(), |
| 678 | backendTex.config())); |
Greg Daniel | fa55f2e | 2019-06-13 15:21:38 -0400 | [diff] [blame] | 679 | |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 680 | GrResourceProvider* resourceProvider = direct->priv().resourceProvider(); |
| 681 | |
Brian Salomon | f777897 | 2018-03-08 10:13:17 -0500 | [diff] [blame] | 682 | sk_sp<GrRenderTarget> rt = |
Robert Phillips | dd39980 | 2019-07-18 12:28:00 +0000 | [diff] [blame] | 683 | resourceProvider->wrapBackendTextureAsRenderTarget(backendTex, sampleCnt); |
Brian Salomon | f777897 | 2018-03-08 10:13:17 -0500 | [diff] [blame] | 684 | if (!rt) { |
| 685 | return nullptr; |
Greg Daniel | f87651e | 2018-02-21 11:36:53 -0500 | [diff] [blame] | 686 | } |
Brian Salomon | f777897 | 2018-03-08 10:13:17 -0500 | [diff] [blame] | 687 | SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable |
| 688 | SkASSERT(!rt->getUniqueKey().isValid()); |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 689 | // This proxy should be unbudgeted because we're just wrapping an external resource |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 690 | SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType()); |
Greg Daniel | f87651e | 2018-02-21 11:36:53 -0500 | [diff] [blame] | 691 | |
Robert Phillips | dd39980 | 2019-07-18 12:28:00 +0000 | [diff] [blame] | 692 | GrColorType colorType = GrPixelConfigToColorType(rt->config()); |
| 693 | GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(rt->backendFormat(), colorType); |
| 694 | GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(rt->backendFormat(), colorType); |
Greg Daniel | 2c19e7f | 2019-06-18 13:29:21 -0400 | [diff] [blame] | 695 | |
| 696 | return sk_sp<GrSurfaceProxy>(new GrRenderTargetProxy(std::move(rt), origin, texSwizzle, |
| 697 | outSwizzle)); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 698 | } |
| 699 | |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 700 | sk_sp<GrRenderTargetProxy> GrProxyProvider::wrapVulkanSecondaryCBAsRenderTarget( |
| 701 | const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) { |
| 702 | if (this->isAbandoned()) { |
| 703 | return nullptr; |
| 704 | } |
| 705 | |
| 706 | // This is only supported on a direct GrContext. |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 707 | GrContext* direct = fImageContext->priv().asDirectContext(); |
| 708 | if (!direct) { |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 709 | return nullptr; |
| 710 | } |
| 711 | |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 712 | GrResourceProvider* resourceProvider = direct->priv().resourceProvider(); |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 713 | |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 714 | sk_sp<GrRenderTarget> rt = resourceProvider->wrapVulkanSecondaryCBAsRenderTarget(imageInfo, |
| 715 | vkInfo); |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 716 | if (!rt) { |
| 717 | return nullptr; |
| 718 | } |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 719 | |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 720 | SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable |
| 721 | SkASSERT(!rt->getUniqueKey().isValid()); |
| 722 | // This proxy should be unbudgeted because we're just wrapping an external resource |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 723 | SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType()); |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 724 | |
Greg Daniel | 2c19e7f | 2019-06-18 13:29:21 -0400 | [diff] [blame] | 725 | GrColorType colorType = GrPixelConfigToColorType(rt->config()); |
| 726 | GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(rt->backendFormat(), colorType); |
| 727 | GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(rt->backendFormat(), colorType); |
| 728 | |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 729 | // All Vulkan surfaces uses top left origins. |
| 730 | return sk_sp<GrRenderTargetProxy>( |
| 731 | new GrRenderTargetProxy(std::move(rt), |
Greg Daniel | 2c19e7f | 2019-06-18 13:29:21 -0400 | [diff] [blame] | 732 | kTopLeft_GrSurfaceOrigin, texSwizzle, outSwizzle, |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 733 | GrRenderTargetProxy::WrapsVkSecondaryCB::kYes)); |
| 734 | } |
| 735 | |
Robert Phillips | 777707b | 2018-01-17 11:40:14 -0500 | [diff] [blame] | 736 | sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback, |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 737 | const GrBackendFormat& format, |
Robert Phillips | 777707b | 2018-01-17 11:40:14 -0500 | [diff] [blame] | 738 | const GrSurfaceDesc& desc, |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 739 | GrRenderable renderable, |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 740 | GrSurfaceOrigin origin, |
Brian Salomon | 7226c23 | 2018-07-30 13:13:17 -0400 | [diff] [blame] | 741 | GrMipMapped mipMapped, |
Brian Salomon | 7226c23 | 2018-07-30 13:13:17 -0400 | [diff] [blame] | 742 | SkBackingFit fit, |
Brian Salomon | e8a766b | 2019-07-19 14:24:36 -0400 | [diff] [blame^] | 743 | SkBudgeted budgeted, |
| 744 | GrProtected isProtected) { |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 745 | return this->createLazyProxy(std::move(callback), format, desc, renderable, origin, mipMapped, |
Brian Salomon | e8a766b | 2019-07-19 14:24:36 -0400 | [diff] [blame^] | 746 | GrInternalSurfaceFlags::kNone, fit, budgeted, isProtected); |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback, |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 750 | const GrBackendFormat& format, |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 751 | const GrSurfaceDesc& desc, |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 752 | GrRenderable renderable, |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 753 | GrSurfaceOrigin origin, |
| 754 | GrMipMapped mipMapped, |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 755 | GrInternalSurfaceFlags surfaceFlags, |
Brian Salomon | 7226c23 | 2018-07-30 13:13:17 -0400 | [diff] [blame] | 756 | SkBackingFit fit, |
| 757 | SkBudgeted budgeted, |
Brian Salomon | e8a766b | 2019-07-19 14:24:36 -0400 | [diff] [blame^] | 758 | GrProtected isProtected) { |
| 759 | // For non-ddl draws always make lazy proxy's single use. |
| 760 | LazyInstantiationType lazyType = this->renderingDirectly() ? LazyInstantiationType::kSingleUse |
| 761 | : LazyInstantiationType::kMultipleUse; |
| 762 | return this->createLazyProxy(std::move(callback), format, desc, renderable, origin, mipMapped, |
| 763 | surfaceFlags, fit, budgeted, isProtected, lazyType); |
| 764 | } |
| 765 | |
| 766 | sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback, |
| 767 | const GrBackendFormat& format, |
| 768 | const GrSurfaceDesc& desc, |
| 769 | GrRenderable renderable, |
| 770 | GrSurfaceOrigin origin, |
| 771 | GrMipMapped mipMapped, |
| 772 | GrInternalSurfaceFlags surfaceFlags, |
| 773 | SkBackingFit fit, |
| 774 | SkBudgeted budgeted, |
| 775 | GrProtected isProtected, |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 776 | LazyInstantiationType lazyType) { |
Robert Phillips | 777707b | 2018-01-17 11:40:14 -0500 | [diff] [blame] | 777 | SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) || |
| 778 | (desc.fWidth > 0 && desc.fHeight > 0)); |
Greg Daniel | 92cbf3f | 2018-04-12 16:50:17 -0400 | [diff] [blame] | 779 | |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 780 | if (desc.fWidth > this->caps()->maxTextureSize() || |
| 781 | desc.fHeight > this->caps()->maxTextureSize()) { |
Greg Daniel | 92cbf3f | 2018-04-12 16:50:17 -0400 | [diff] [blame] | 782 | return nullptr; |
| 783 | } |
| 784 | |
Greg Daniel | fa55f2e | 2019-06-13 15:21:38 -0400 | [diff] [blame] | 785 | SkASSERT(validate_backend_format_and_config(this->caps(), format, desc.fConfig)); |
Greg Daniel | 457469c | 2018-02-08 15:05:44 -0500 | [diff] [blame] | 786 | |
Greg Daniel | 2c19e7f | 2019-06-18 13:29:21 -0400 | [diff] [blame] | 787 | GrColorType colorType = GrPixelConfigToColorType(desc.fConfig); |
| 788 | GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(format, colorType); |
| 789 | GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(format, colorType); |
| 790 | |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 791 | return sk_sp<GrTextureProxy>( |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 792 | renderable == GrRenderable::kYes |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 793 | ? new GrTextureRenderTargetProxy(std::move(callback), lazyType, format, desc, |
Greg Daniel | 2c19e7f | 2019-06-18 13:29:21 -0400 | [diff] [blame] | 794 | origin, mipMapped, texSwizzle, outSwizzle, fit, |
Brian Salomon | e8a766b | 2019-07-19 14:24:36 -0400 | [diff] [blame^] | 795 | budgeted, isProtected, surfaceFlags) |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 796 | : new GrTextureProxy(std::move(callback), lazyType, format, desc, origin, |
Brian Salomon | e8a766b | 2019-07-19 14:24:36 -0400 | [diff] [blame^] | 797 | mipMapped, texSwizzle, fit, budgeted, isProtected, |
| 798 | surfaceFlags)); |
Robert Phillips | 777707b | 2018-01-17 11:40:14 -0500 | [diff] [blame] | 799 | } |
| 800 | |
Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 801 | sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy( |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 802 | LazyInstantiateCallback&& callback, const GrBackendFormat& format, |
| 803 | const GrSurfaceDesc& desc, GrSurfaceOrigin origin, GrInternalSurfaceFlags surfaceFlags, |
Greg Daniel | b085fa9 | 2019-03-05 16:55:12 -0500 | [diff] [blame] | 804 | const TextureInfo* textureInfo, SkBackingFit fit, SkBudgeted budgeted, |
Brian Salomon | e8a766b | 2019-07-19 14:24:36 -0400 | [diff] [blame^] | 805 | GrProtected isProtected, bool wrapsVkSecondaryCB) { |
Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 806 | SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) || |
| 807 | (desc.fWidth > 0 && desc.fHeight > 0)); |
Greg Daniel | 92cbf3f | 2018-04-12 16:50:17 -0400 | [diff] [blame] | 808 | |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 809 | if (desc.fWidth > this->caps()->maxRenderTargetSize() || |
| 810 | desc.fHeight > this->caps()->maxRenderTargetSize()) { |
Greg Daniel | 92cbf3f | 2018-04-12 16:50:17 -0400 | [diff] [blame] | 811 | return nullptr; |
| 812 | } |
| 813 | |
Greg Daniel | fa55f2e | 2019-06-13 15:21:38 -0400 | [diff] [blame] | 814 | SkASSERT(validate_backend_format_and_config(this->caps(), format, desc.fConfig)); |
Greg Daniel | 457469c | 2018-02-08 15:05:44 -0500 | [diff] [blame] | 815 | |
| 816 | using LazyInstantiationType = GrSurfaceProxy::LazyInstantiationType; |
| 817 | // For non-ddl draws always make lazy proxy's single use. |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 818 | LazyInstantiationType lazyType = this->renderingDirectly() ? LazyInstantiationType::kSingleUse |
| 819 | : LazyInstantiationType::kMultipleUse; |
Greg Daniel | 457469c | 2018-02-08 15:05:44 -0500 | [diff] [blame] | 820 | |
Greg Daniel | 2c19e7f | 2019-06-18 13:29:21 -0400 | [diff] [blame] | 821 | GrColorType colorType = GrPixelConfigToColorType(desc.fConfig); |
| 822 | GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(format, colorType); |
| 823 | GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(format, colorType); |
| 824 | |
Brian Salomon | 7226c23 | 2018-07-30 13:13:17 -0400 | [diff] [blame] | 825 | if (textureInfo) { |
Greg Daniel | b085fa9 | 2019-03-05 16:55:12 -0500 | [diff] [blame] | 826 | // Wrapped vulkan secondary command buffers don't support texturing since we won't have an |
| 827 | // actual VkImage to texture from. |
| 828 | SkASSERT(!wrapsVkSecondaryCB); |
Brian Salomon | 7226c23 | 2018-07-30 13:13:17 -0400 | [diff] [blame] | 829 | return sk_sp<GrRenderTargetProxy>(new GrTextureRenderTargetProxy( |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 830 | std::move(callback), lazyType, format, desc, origin, textureInfo->fMipMapped, |
Brian Salomon | e8a766b | 2019-07-19 14:24:36 -0400 | [diff] [blame^] | 831 | texSwizzle, outSwizzle, fit, budgeted, isProtected, surfaceFlags)); |
Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 832 | } |
| 833 | |
Greg Daniel | b085fa9 | 2019-03-05 16:55:12 -0500 | [diff] [blame] | 834 | GrRenderTargetProxy::WrapsVkSecondaryCB vkSCB = |
| 835 | wrapsVkSecondaryCB ? GrRenderTargetProxy::WrapsVkSecondaryCB::kYes |
| 836 | : GrRenderTargetProxy::WrapsVkSecondaryCB::kNo; |
| 837 | |
Brian Salomon | e8a766b | 2019-07-19 14:24:36 -0400 | [diff] [blame^] | 838 | return sk_sp<GrRenderTargetProxy>( |
| 839 | new GrRenderTargetProxy(std::move(callback), lazyType, format, desc, origin, texSwizzle, |
| 840 | outSwizzle, fit, budgeted, isProtected, surfaceFlags, vkSCB)); |
Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 841 | } |
| 842 | |
Chris Dalton | 19cc00f | 2019-04-15 09:06:28 -0600 | [diff] [blame] | 843 | sk_sp<GrTextureProxy> GrProxyProvider::MakeFullyLazyProxy( |
| 844 | LazyInstantiateCallback&& callback, const GrBackendFormat& format, Renderable renderable, |
Brian Salomon | e8a766b | 2019-07-19 14:24:36 -0400 | [diff] [blame^] | 845 | GrProtected isProtected, GrSurfaceOrigin origin, GrPixelConfig config, const GrCaps& caps, |
| 846 | int sampleCnt) { |
Greg Daniel | fa55f2e | 2019-06-13 15:21:38 -0400 | [diff] [blame] | 847 | SkASSERT(validate_backend_format_and_config(&caps, format, config)); |
Robert Phillips | 777707b | 2018-01-17 11:40:14 -0500 | [diff] [blame] | 848 | GrSurfaceDesc desc; |
Robert Phillips | 10d1721 | 2019-04-24 14:09:10 -0400 | [diff] [blame] | 849 | GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNone; |
Robert Phillips | 777707b | 2018-01-17 11:40:14 -0500 | [diff] [blame] | 850 | desc.fWidth = -1; |
| 851 | desc.fHeight = -1; |
| 852 | desc.fConfig = config; |
Chris Dalton | 19cc00f | 2019-04-15 09:06:28 -0600 | [diff] [blame] | 853 | desc.fSampleCnt = sampleCnt; |
Robert Phillips | 777707b | 2018-01-17 11:40:14 -0500 | [diff] [blame] | 854 | |
Greg Daniel | 2c19e7f | 2019-06-18 13:29:21 -0400 | [diff] [blame] | 855 | GrColorType colorType = GrPixelConfigToColorType(desc.fConfig); |
| 856 | GrSwizzle texSwizzle = caps.getTextureSwizzle(format, colorType); |
| 857 | GrSwizzle outSwizzle = caps.getOutputSwizzle(format, colorType); |
| 858 | |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 859 | return sk_sp<GrTextureProxy>( |
| 860 | (Renderable::kYes == renderable) |
Brian Salomon | 7226c23 | 2018-07-30 13:13:17 -0400 | [diff] [blame] | 861 | ? new GrTextureRenderTargetProxy( |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 862 | std::move(callback), LazyInstantiationType::kSingleUse, format, desc, |
Greg Daniel | 2c19e7f | 2019-06-18 13:29:21 -0400 | [diff] [blame] | 863 | origin, GrMipMapped::kNo, texSwizzle, outSwizzle, |
Brian Salomon | e8a766b | 2019-07-19 14:24:36 -0400 | [diff] [blame^] | 864 | SkBackingFit::kApprox, SkBudgeted::kYes, isProtected, surfaceFlags) |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 865 | : new GrTextureProxy(std::move(callback), LazyInstantiationType::kSingleUse, |
Greg Daniel | 2c19e7f | 2019-06-18 13:29:21 -0400 | [diff] [blame] | 866 | format, desc, origin, GrMipMapped::kNo, texSwizzle, |
Brian Salomon | e8a766b | 2019-07-19 14:24:36 -0400 | [diff] [blame^] | 867 | SkBackingFit::kApprox, SkBudgeted::kYes, isProtected, |
| 868 | surfaceFlags)); |
Robert Phillips | 777707b | 2018-01-17 11:40:14 -0500 | [diff] [blame] | 869 | } |
| 870 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 871 | bool GrProxyProvider::IsFunctionallyExact(GrSurfaceProxy* proxy) { |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 872 | const bool isInstantiated = proxy->isInstantiated(); |
Robert Phillips | db3b979 | 2018-06-12 15:18:00 -0400 | [diff] [blame] | 873 | // A proxy is functionally exact if: |
| 874 | // it is exact (obvs) |
| 875 | // when it is instantiated it will be exact (i.e., power of two dimensions) |
| 876 | // it is already instantiated and the proxy covers the entire backing surface |
| 877 | return proxy->priv().isExact() || |
| 878 | (!isInstantiated && SkIsPow2(proxy->width()) && SkIsPow2(proxy->height())) || |
| 879 | (isInstantiated && proxy->worstCaseWidth() == proxy->width() && |
| 880 | proxy->worstCaseHeight() == proxy->height()); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 881 | } |
| 882 | |
Robert Phillips | 427966a | 2018-12-20 17:20:43 -0500 | [diff] [blame] | 883 | void GrProxyProvider::processInvalidUniqueKey(const GrUniqueKey& key, GrTextureProxy* proxy, |
| 884 | InvalidateGPUResource invalidateGPUResource) { |
Chris Dalton | 2de13dd | 2019-01-03 15:11:59 -0700 | [diff] [blame] | 885 | SkASSERT(key.isValid()); |
| 886 | |
Robert Phillips | 427966a | 2018-12-20 17:20:43 -0500 | [diff] [blame] | 887 | if (!proxy) { |
| 888 | proxy = fUniquelyKeyedProxies.find(key); |
| 889 | } |
Chris Dalton | 2de13dd | 2019-01-03 15:11:59 -0700 | [diff] [blame] | 890 | SkASSERT(!proxy || proxy->getUniqueKey() == key); |
| 891 | |
| 892 | // Locate the corresponding GrGpuResource (if it needs to be invalidated) before clearing the |
| 893 | // proxy's unique key. We must do it in this order because 'key' may alias the proxy's key. |
| 894 | sk_sp<GrGpuResource> invalidGpuResource; |
| 895 | if (InvalidateGPUResource::kYes == invalidateGPUResource) { |
Brian Salomon | 01ceae9 | 2019-04-02 11:49:54 -0400 | [diff] [blame] | 896 | GrContext* direct = fImageContext->priv().asDirectContext(); |
| 897 | if (direct) { |
| 898 | GrResourceProvider* resourceProvider = direct->priv().resourceProvider(); |
| 899 | invalidGpuResource = resourceProvider->findByUniqueKey<GrGpuResource>(key); |
Chris Dalton | 2de13dd | 2019-01-03 15:11:59 -0700 | [diff] [blame] | 900 | } |
| 901 | SkASSERT(!invalidGpuResource || invalidGpuResource->getUniqueKey() == key); |
| 902 | } |
Robert Phillips | 427966a | 2018-12-20 17:20:43 -0500 | [diff] [blame] | 903 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 904 | // Note: this method is called for the whole variety of GrGpuResources so often 'key' |
| 905 | // will not be in 'fUniquelyKeyedProxies'. |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 906 | if (proxy) { |
Robert Phillips | 427966a | 2018-12-20 17:20:43 -0500 | [diff] [blame] | 907 | fUniquelyKeyedProxies.remove(key); |
| 908 | proxy->cacheAccess().clearUniqueKey(); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 909 | } |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 910 | |
Chris Dalton | 2de13dd | 2019-01-03 15:11:59 -0700 | [diff] [blame] | 911 | if (invalidGpuResource) { |
| 912 | invalidGpuResource->resourcePriv().removeUniqueKey(); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 913 | } |
| 914 | } |
| 915 | |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 916 | uint32_t GrProxyProvider::contextID() const { |
| 917 | return fImageContext->priv().contextID(); |
| 918 | } |
| 919 | |
| 920 | const GrCaps* GrProxyProvider::caps() const { |
| 921 | return fImageContext->priv().caps(); |
| 922 | } |
| 923 | |
| 924 | sk_sp<const GrCaps> GrProxyProvider::refCaps() const { |
| 925 | return fImageContext->priv().refCaps(); |
| 926 | } |
| 927 | |
Robert Phillips | a9162df | 2019-02-11 14:12:03 -0500 | [diff] [blame] | 928 | bool GrProxyProvider::isAbandoned() const { |
| 929 | return fImageContext->priv().abandoned(); |
| 930 | } |
| 931 | |
Robert Phillips | 0790f8a | 2018-09-18 13:11:03 -0400 | [diff] [blame] | 932 | void GrProxyProvider::orphanAllUniqueKeys() { |
| 933 | UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); |
| 934 | for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) { |
| 935 | GrTextureProxy& tmp = *iter; |
| 936 | |
| 937 | tmp.fProxyProvider = nullptr; |
| 938 | } |
| 939 | } |
| 940 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 941 | void GrProxyProvider::removeAllUniqueKeys() { |
| 942 | UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); |
| 943 | for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) { |
| 944 | GrTextureProxy& tmp = *iter; |
| 945 | |
Chris Dalton | 2de13dd | 2019-01-03 15:11:59 -0700 | [diff] [blame] | 946 | this->processInvalidUniqueKey(tmp.getUniqueKey(), &tmp, InvalidateGPUResource::kNo); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 947 | } |
| 948 | SkASSERT(!fUniquelyKeyedProxies.count()); |
| 949 | } |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 950 | |
| 951 | bool GrProxyProvider::renderingDirectly() const { |
| 952 | return fImageContext->priv().asDirectContext(); |
| 953 | } |