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