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