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