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