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