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