Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [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 | |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 8 | #include "SkImage_GpuBase.h" |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 9 | #include "GrBackendSurface.h" |
| 10 | #include "GrClip.h" |
| 11 | #include "GrContext.h" |
| 12 | #include "GrContextPriv.h" |
| 13 | #include "GrRenderTargetContext.h" |
| 14 | #include "GrTexture.h" |
| 15 | #include "GrTextureAdjuster.h" |
| 16 | #include "SkBitmapCache.h" |
| 17 | #include "SkImage_Gpu.h" |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 18 | #include "SkPromiseImageTexture.h" |
Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 19 | #include "SkReadPixelsRec.h" |
Brian Salomon | 1bf0ed8 | 2019-01-16 13:51:35 -0500 | [diff] [blame] | 20 | #include "SkTLList.h" |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 21 | #include "effects/GrYUVtoRGBEffect.h" |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 22 | |
| 23 | SkImage_GpuBase::SkImage_GpuBase(sk_sp<GrContext> context, int width, int height, uint32_t uniqueID, |
Brian Salomon | f05e6d3 | 2018-12-20 08:41:41 -0500 | [diff] [blame] | 24 | SkAlphaType at, sk_sp<SkColorSpace> cs) |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 25 | : INHERITED(width, height, uniqueID) |
| 26 | , fContext(std::move(context)) |
| 27 | , fAlphaType(at) |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 28 | , fColorSpace(std::move(cs)) {} |
| 29 | |
| 30 | SkImage_GpuBase::~SkImage_GpuBase() {} |
| 31 | |
| 32 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
| 33 | |
| 34 | bool SkImage_GpuBase::ValidateBackendTexture(GrContext* ctx, const GrBackendTexture& tex, |
| 35 | GrPixelConfig* config, SkColorType ct, SkAlphaType at, |
| 36 | sk_sp<SkColorSpace> cs) { |
| 37 | if (!tex.isValid()) { |
| 38 | return false; |
| 39 | } |
| 40 | // TODO: Create a SkImageColorInfo struct for color, alpha, and color space so we don't need to |
| 41 | // create a fake image info here. |
| 42 | SkImageInfo info = SkImageInfo::Make(1, 1, ct, at, cs); |
| 43 | if (!SkImageInfoIsValid(info)) { |
| 44 | return false; |
| 45 | } |
Brian Salomon | f391d0f | 2018-12-14 09:18:50 -0500 | [diff] [blame] | 46 | GrBackendFormat backendFormat = tex.getBackendFormat(); |
| 47 | if (!backendFormat.isValid()) { |
| 48 | return false; |
| 49 | } |
| 50 | *config = ctx->contextPriv().caps()->getConfigFromBackendFormat(backendFormat, ct); |
| 51 | return *config != kUnknown_GrPixelConfig; |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
| 55 | |
Brian Osman | e50cdf0 | 2018-10-19 13:02:14 -0400 | [diff] [blame] | 56 | bool SkImage_GpuBase::getROPixels(SkBitmap* dst, CachingHint chint) const { |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 57 | if (!fContext->contextPriv().resourceProvider()) { |
| 58 | // DDL TODO: buffer up the readback so it occurs when the DDL is drawn? |
| 59 | return false; |
| 60 | } |
| 61 | |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 62 | const auto desc = SkBitmapCacheDesc::Make(this); |
| 63 | if (SkBitmapCache::Find(desc, dst)) { |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 64 | SkASSERT(dst->isImmutable()); |
| 65 | SkASSERT(dst->getPixels()); |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | SkBitmapCache::RecPtr rec = nullptr; |
| 70 | SkPixmap pmap; |
| 71 | if (kAllow_CachingHint == chint) { |
| 72 | rec = SkBitmapCache::Alloc(desc, this->onImageInfo(), &pmap); |
| 73 | if (!rec) { |
| 74 | return false; |
| 75 | } |
| 76 | } else { |
| 77 | if (!dst->tryAllocPixels(this->onImageInfo()) || !dst->peekPixels(&pmap)) { |
| 78 | return false; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | sk_sp<GrSurfaceContext> sContext = fContext->contextPriv().makeWrappedSurfaceContext( |
| 83 | this->asTextureProxyRef(), |
| 84 | fColorSpace); |
| 85 | if (!sContext) { |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | if (!sContext->readPixels(pmap.info(), pmap.writable_addr(), pmap.rowBytes(), 0, 0)) { |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | if (rec) { |
| 94 | SkBitmapCache::Add(std::move(rec), dst); |
| 95 | this->notifyAddedToRasterCache(); |
| 96 | } |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | sk_sp<SkImage> SkImage_GpuBase::onMakeSubset(const SkIRect& subset) const { |
| 101 | sk_sp<GrSurfaceProxy> proxy = this->asTextureProxyRef(); |
| 102 | |
| 103 | GrSurfaceDesc desc; |
| 104 | desc.fWidth = subset.width(); |
| 105 | desc.fHeight = subset.height(); |
| 106 | desc.fConfig = proxy->config(); |
| 107 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 108 | GrBackendFormat format = proxy->backendFormat().makeTexture2D(); |
| 109 | if (!format.isValid()) { |
| 110 | return nullptr; |
| 111 | } |
| 112 | |
Brian Salomon | f05e6d3 | 2018-12-20 08:41:41 -0500 | [diff] [blame] | 113 | // TODO: Should this inherit our proxy's budgeted status? |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 114 | sk_sp<GrSurfaceContext> sContext(fContext->contextPriv().makeDeferredSurfaceContext( |
Brian Salomon | f05e6d3 | 2018-12-20 08:41:41 -0500 | [diff] [blame] | 115 | format, desc, proxy->origin(), GrMipMapped::kNo, SkBackingFit::kExact, |
| 116 | proxy->isBudgeted())); |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 117 | if (!sContext) { |
| 118 | return nullptr; |
| 119 | } |
| 120 | |
| 121 | if (!sContext->copy(proxy.get(), subset, SkIPoint::Make(0, 0))) { |
| 122 | return nullptr; |
| 123 | } |
| 124 | |
| 125 | // MDB: this call is okay bc we know 'sContext' was kExact |
Brian Salomon | f05e6d3 | 2018-12-20 08:41:41 -0500 | [diff] [blame] | 126 | return sk_make_sp<SkImage_Gpu>(fContext, kNeedNewImageUniqueID, fAlphaType, |
| 127 | sContext->asTextureProxyRef(), fColorSpace); |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 128 | } |
| 129 | |
Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 130 | static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes) { |
| 131 | switch (info.colorType()) { |
| 132 | case kRGBA_8888_SkColorType: |
| 133 | case kBGRA_8888_SkColorType: |
| 134 | break; |
| 135 | default: |
| 136 | return; // nothing to do |
| 137 | } |
| 138 | |
Brian Salomon | 3f4cd77 | 2019-01-11 16:03:19 -0500 | [diff] [blame] | 139 | // SkColor is not necessarily RGBA or BGRA, but it is one of them on little-endian, |
Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 140 | // and in either case, the alpha-byte is always in the same place, so we can safely call |
| 141 | // SkPreMultiplyColor() |
| 142 | // |
| 143 | SkColor* row = (SkColor*)pixels; |
| 144 | for (int y = 0; y < info.height(); ++y) { |
| 145 | for (int x = 0; x < info.width(); ++x) { |
| 146 | row[x] = SkPreMultiplyColor(row[x]); |
| 147 | } |
| 148 | row = (SkColor*)((char*)(row)+rowBytes); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | bool SkImage_GpuBase::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB, |
| 153 | int srcX, int srcY, CachingHint) const { |
| 154 | if (!fContext->contextPriv().resourceProvider()) { |
| 155 | // DDL TODO: buffer up the readback so it occurs when the DDL is drawn? |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | if (!SkImageInfoValidConversion(dstInfo, this->onImageInfo())) { |
| 160 | return false; |
| 161 | } |
| 162 | |
| 163 | SkReadPixelsRec rec(dstInfo, dstPixels, dstRB, srcX, srcY); |
| 164 | if (!rec.trim(this->width(), this->height())) { |
| 165 | return false; |
| 166 | } |
| 167 | |
| 168 | // TODO: this seems to duplicate code in GrTextureContext::onReadPixels and |
| 169 | // GrRenderTargetContext::onReadPixels |
| 170 | uint32_t flags = 0; |
| 171 | if (kUnpremul_SkAlphaType == rec.fInfo.alphaType() && kPremul_SkAlphaType == fAlphaType) { |
| 172 | // let the GPU perform this transformation for us |
| 173 | flags = GrContextPriv::kUnpremul_PixelOpsFlag; |
| 174 | } |
| 175 | |
| 176 | sk_sp<GrSurfaceContext> sContext = fContext->contextPriv().makeWrappedSurfaceContext( |
| 177 | this->asTextureProxyRef(), fColorSpace); |
| 178 | if (!sContext) { |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | if (!sContext->readPixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY, flags)) { |
| 183 | return false; |
| 184 | } |
| 185 | |
| 186 | // do we have to manually fix-up the alpha channel? |
| 187 | // src dst |
| 188 | // unpremul premul fix manually |
| 189 | // premul unpremul done by kUnpremul_PixelOpsFlag |
| 190 | // all other combos need to change. |
| 191 | // |
| 192 | // Should this be handled by Ganesh? todo:? |
| 193 | // |
| 194 | if (kPremul_SkAlphaType == rec.fInfo.alphaType() && kUnpremul_SkAlphaType == fAlphaType) { |
| 195 | apply_premul(rec.fInfo, rec.fPixels, rec.fRowBytes); |
| 196 | } |
| 197 | return true; |
| 198 | } |
| 199 | |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 200 | sk_sp<GrTextureProxy> SkImage_GpuBase::asTextureProxyRef(GrContext* context, |
| 201 | const GrSamplerState& params, |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 202 | SkScalar scaleAdjust[2]) const { |
| 203 | if (context->uniqueID() != fContext->uniqueID()) { |
| 204 | SkASSERT(0); |
| 205 | return nullptr; |
| 206 | } |
| 207 | |
| 208 | GrTextureAdjuster adjuster(fContext.get(), this->asTextureProxyRef(), fAlphaType, |
| 209 | this->uniqueID(), fColorSpace.get()); |
Brian Osman | 6064e1c | 2018-10-19 14:27:54 -0400 | [diff] [blame] | 210 | return adjuster.refTextureProxyForParams(params, scaleAdjust); |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | GrBackendTexture SkImage_GpuBase::onGetBackendTexture(bool flushPendingGrContextIO, |
| 214 | GrSurfaceOrigin* origin) const { |
| 215 | sk_sp<GrTextureProxy> proxy = this->asTextureProxyRef(); |
| 216 | SkASSERT(proxy); |
| 217 | |
| 218 | if (!fContext->contextPriv().resourceProvider() && !proxy->isInstantiated()) { |
| 219 | // This image was created with a DDL context and cannot be instantiated. |
| 220 | return GrBackendTexture(); |
| 221 | } |
| 222 | |
| 223 | if (!proxy->instantiate(fContext->contextPriv().resourceProvider())) { |
| 224 | return GrBackendTexture(); // invalid |
| 225 | } |
| 226 | |
| 227 | GrTexture* texture = proxy->peekTexture(); |
| 228 | |
| 229 | if (texture) { |
| 230 | if (flushPendingGrContextIO) { |
| 231 | fContext->contextPriv().prepareSurfaceForExternalIO(proxy.get()); |
| 232 | } |
| 233 | if (origin) { |
| 234 | *origin = proxy->origin(); |
| 235 | } |
| 236 | return texture->getBackendTexture(); |
| 237 | } |
| 238 | return GrBackendTexture(); // invalid |
| 239 | } |
| 240 | |
| 241 | GrTexture* SkImage_GpuBase::onGetTexture() const { |
| 242 | GrTextureProxy* proxy = this->peekProxy(); |
| 243 | if (!proxy) { |
| 244 | return nullptr; |
| 245 | } |
| 246 | |
| 247 | sk_sp<GrTextureProxy> proxyRef = this->asTextureProxyRef(); |
| 248 | if (!fContext->contextPriv().resourceProvider() && !proxyRef->isInstantiated()) { |
| 249 | // This image was created with a DDL context and cannot be instantiated. |
| 250 | return nullptr; |
| 251 | } |
| 252 | |
| 253 | if (!proxy->instantiate(fContext->contextPriv().resourceProvider())) { |
| 254 | return nullptr; |
| 255 | } |
| 256 | |
| 257 | return proxy->peekTexture(); |
| 258 | } |
| 259 | |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 260 | bool SkImage_GpuBase::onIsValid(GrContext* context) const { |
| 261 | // The base class has already checked that context isn't abandoned (if it's not nullptr) |
| 262 | if (fContext->abandoned()) { |
| 263 | return false; |
| 264 | } |
| 265 | |
| 266 | if (context && context != fContext.get()) { |
| 267 | return false; |
| 268 | } |
| 269 | |
| 270 | return true; |
| 271 | } |
Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 272 | |
Jim Van Verth | 0e67194 | 2018-11-09 12:03:57 -0500 | [diff] [blame] | 273 | bool SkImage_GpuBase::MakeTempTextureProxies(GrContext* ctx, const GrBackendTexture yuvaTextures[], |
Jim Van Verth | 5327536 | 2018-11-09 15:42:35 -0500 | [diff] [blame] | 274 | int numTextures, const SkYUVAIndex yuvaIndices[4], |
| 275 | GrSurfaceOrigin imageOrigin, |
Jim Van Verth | 0e67194 | 2018-11-09 12:03:57 -0500 | [diff] [blame] | 276 | sk_sp<GrTextureProxy> tempTextureProxies[4]) { |
| 277 | GrProxyProvider* proxyProvider = ctx->contextPriv().proxyProvider(); |
| 278 | |
| 279 | // We need to make a copy of the input backend textures because we need to preserve the result |
| 280 | // of validate_backend_texture. |
| 281 | GrBackendTexture yuvaTexturesCopy[4]; |
| 282 | for (int textureIndex = 0; textureIndex < numTextures; ++textureIndex) { |
| 283 | yuvaTexturesCopy[textureIndex] = yuvaTextures[textureIndex]; |
Brian Salomon | f391d0f | 2018-12-14 09:18:50 -0500 | [diff] [blame] | 284 | GrBackendFormat backendFormat = yuvaTexturesCopy[textureIndex].getBackendFormat(); |
| 285 | if (!backendFormat.isValid()) { |
| 286 | return false; |
| 287 | } |
| 288 | yuvaTexturesCopy[textureIndex].fConfig = |
| 289 | ctx->contextPriv().caps()->getYUVAConfigFromBackendFormat(backendFormat); |
| 290 | if (yuvaTexturesCopy[textureIndex].fConfig == kUnknown_GrPixelConfig) { |
Jim Van Verth | 0e67194 | 2018-11-09 12:03:57 -0500 | [diff] [blame] | 291 | return false; |
| 292 | } |
| 293 | SkASSERT(yuvaTexturesCopy[textureIndex].isValid()); |
| 294 | |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 295 | tempTextureProxies[textureIndex] = proxyProvider->wrapBackendTexture( |
| 296 | yuvaTexturesCopy[textureIndex], imageOrigin, kBorrow_GrWrapOwnership, |
| 297 | GrWrapCacheable::kNo, kRead_GrIOType); |
Jim Van Verth | 0e67194 | 2018-11-09 12:03:57 -0500 | [diff] [blame] | 298 | if (!tempTextureProxies[textureIndex]) { |
| 299 | return false; |
| 300 | } |
Jim Van Verth | 5327536 | 2018-11-09 15:42:35 -0500 | [diff] [blame] | 301 | |
| 302 | // Check that each texture contains the channel data for the corresponding YUVA index |
| 303 | GrPixelConfig config = yuvaTexturesCopy[textureIndex].fConfig; |
| 304 | for (int yuvaIndex = 0; yuvaIndex < SkYUVAIndex::kIndexCount; ++yuvaIndex) { |
| 305 | if (yuvaIndices[yuvaIndex].fIndex == textureIndex) { |
| 306 | switch (yuvaIndices[yuvaIndex].fChannel) { |
| 307 | case SkColorChannel::kR: |
| 308 | if (kAlpha_8_as_Alpha_GrPixelConfig == config) { |
| 309 | return false; |
| 310 | } |
| 311 | break; |
| 312 | case SkColorChannel::kG: |
| 313 | case SkColorChannel::kB: |
| 314 | if (kAlpha_8_as_Alpha_GrPixelConfig == config || |
| 315 | kAlpha_8_as_Red_GrPixelConfig == config) { |
| 316 | return false; |
| 317 | } |
| 318 | break; |
| 319 | case SkColorChannel::kA: |
| 320 | default: |
| 321 | if (kRGB_888_GrPixelConfig == config) { |
| 322 | return false; |
| 323 | } |
| 324 | break; |
| 325 | } |
| 326 | } |
| 327 | } |
Jim Van Verth | 0e67194 | 2018-11-09 12:03:57 -0500 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | return true; |
| 331 | } |
| 332 | |
| 333 | bool SkImage_GpuBase::RenderYUVAToRGBA(GrContext* ctx, GrRenderTargetContext* renderTargetContext, |
| 334 | const SkRect& rect, SkYUVColorSpace yuvColorSpace, |
| 335 | const sk_sp<GrTextureProxy> proxies[4], |
| 336 | const SkYUVAIndex yuvaIndices[4]) { |
| 337 | SkASSERT(renderTargetContext); |
| 338 | if (!renderTargetContext->asSurfaceProxy()) { |
| 339 | return false; |
| 340 | } |
| 341 | |
| 342 | GrPaint paint; |
| 343 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
| 344 | |
Jim Van Verth | 0e67194 | 2018-11-09 12:03:57 -0500 | [diff] [blame] | 345 | paint.addColorFragmentProcessor(GrYUVtoRGBEffect::Make(proxies, yuvaIndices, |
| 346 | yuvColorSpace, |
| 347 | GrSamplerState::Filter::kNearest)); |
| 348 | |
| 349 | renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect); |
| 350 | |
| 351 | // DDL TODO: in the promise image version we must not flush here |
| 352 | ctx->contextPriv().flushSurfaceWrites(renderTargetContext->asSurfaceProxy()); |
| 353 | |
| 354 | return true; |
| 355 | } |
| 356 | |
Brian Salomon | be5a093 | 2018-12-10 10:03:26 -0500 | [diff] [blame] | 357 | sk_sp<GrTextureProxy> SkImage_GpuBase::MakePromiseImageLazyProxy( |
| 358 | GrContext* context, int width, int height, GrSurfaceOrigin origin, GrPixelConfig config, |
| 359 | GrBackendFormat backendFormat, GrMipMapped mipMapped, |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 360 | PromiseImageTextureFulfillProc fulfillProc, |
| 361 | PromiseImageTextureReleaseProc releaseProc, |
| 362 | PromiseImageTextureDoneProc doneProc, |
| 363 | PromiseImageTextureContext textureContext) { |
Brian Salomon | be5a093 | 2018-12-10 10:03:26 -0500 | [diff] [blame] | 364 | SkASSERT(context); |
| 365 | SkASSERT(width > 0 && height > 0); |
| 366 | SkASSERT(doneProc); |
Brian Salomon | f391d0f | 2018-12-14 09:18:50 -0500 | [diff] [blame] | 367 | SkASSERT(config != kUnknown_GrPixelConfig); |
Brian Salomon | be5a093 | 2018-12-10 10:03:26 -0500 | [diff] [blame] | 368 | |
| 369 | if (!fulfillProc || !releaseProc) { |
| 370 | doneProc(textureContext); |
| 371 | return nullptr; |
Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 372 | } |
| 373 | |
Brian Salomon | be5a093 | 2018-12-10 10:03:26 -0500 | [diff] [blame] | 374 | if (mipMapped == GrMipMapped::kYes && |
| 375 | GrTextureTypeHasRestrictedSampling(backendFormat.textureType())) { |
| 376 | // It is invalid to have a GL_TEXTURE_EXTERNAL or GL_TEXTURE_RECTANGLE and have mips as |
| 377 | // well. |
| 378 | doneProc(textureContext); |
| 379 | return nullptr; |
Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 380 | } |
Brian Salomon | be5a093 | 2018-12-10 10:03:26 -0500 | [diff] [blame] | 381 | |
| 382 | /** |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 383 | * This class is the lazy instantiation callback for promise images. It manages calling the |
| 384 | * client's Fulfill, Release, and Done procs. It attempts to reuse a GrTexture instance in |
| 385 | * cases where the client provides the same SkPromiseImageTexture for successive Fulfill calls. |
| 386 | * The created GrTexture is given a key based on a unique ID associated with the |
| 387 | * SkPromiseImageTexture. When the texture enters "idle" state (meaning it is not being used by |
| 388 | * the GPU and is at rest in the resource cache) the client's Release proc is called |
| 389 | * using GrTexture's idle proc mechanism. If the same SkPromiseImageTexture is provided for |
| 390 | * another fulfill we find the cached GrTexture. If the proxy, and therefore this object, |
| 391 | * is destroyed, we invalidate the GrTexture's key. Also if the client overwrites or |
| 392 | * destroys their SkPromiseImageTexture we invalidate the key. |
| 393 | * |
| 394 | * Currently a GrTexture is only reused for a given SkPromiseImageTexture if the |
| 395 | * SkPromiseImageTexture is reused in Fulfill for the same promise SkImage. However, we'd |
| 396 | * like to relax that so that a SkPromiseImageTexture can be reused with different promise |
| 397 | * SkImages that will reuse a single GrTexture. |
Brian Salomon | be5a093 | 2018-12-10 10:03:26 -0500 | [diff] [blame] | 398 | */ |
| 399 | class PromiseLazyInstantiateCallback { |
| 400 | public: |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 401 | PromiseLazyInstantiateCallback(PromiseImageTextureFulfillProc fulfillProc, |
| 402 | PromiseImageTextureReleaseProc releaseProc, |
| 403 | PromiseImageTextureDoneProc doneProc, |
| 404 | PromiseImageTextureContext context, |
Brian Salomon | be5a093 | 2018-12-10 10:03:26 -0500 | [diff] [blame] | 405 | GrPixelConfig config) |
| 406 | : fFulfillProc(fulfillProc) |
Brian Salomon | be5a093 | 2018-12-10 10:03:26 -0500 | [diff] [blame] | 407 | , fConfig(config) { |
Brian Salomon | 553610d | 2019-01-14 17:34:12 -0500 | [diff] [blame] | 408 | auto doneHelper = sk_make_sp<GrReleaseProcHelper>(doneProc, context); |
Brian Salomon | 1bf0ed8 | 2019-01-16 13:51:35 -0500 | [diff] [blame] | 409 | fReleaseContext = sk_make_sp<IdleContext::PromiseImageReleaseContext>( |
| 410 | releaseProc, context, std::move(doneHelper)); |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 411 | } |
Brian Salomon | 553610d | 2019-01-14 17:34:12 -0500 | [diff] [blame] | 412 | |
Brian Salomon | 1bf0ed8 | 2019-01-16 13:51:35 -0500 | [diff] [blame] | 413 | ~PromiseLazyInstantiateCallback() = default; |
Brian Salomon | be5a093 | 2018-12-10 10:03:26 -0500 | [diff] [blame] | 414 | |
| 415 | sk_sp<GrSurface> operator()(GrResourceProvider* resourceProvider) { |
| 416 | if (!resourceProvider) { |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 417 | return nullptr; |
| 418 | } |
| 419 | |
| 420 | sk_sp<GrTexture> cachedTexture; |
| 421 | SkASSERT(fLastFulfilledKey.isValid() == (fLastFulfillID > 0)); |
| 422 | if (fLastFulfilledKey.isValid()) { |
| 423 | auto surf = resourceProvider->findByUniqueKey<GrSurface>(fLastFulfilledKey); |
| 424 | if (surf) { |
| 425 | cachedTexture = sk_ref_sp(surf->asTexture()); |
| 426 | SkASSERT(cachedTexture); |
| 427 | } |
| 428 | } |
| 429 | // If the release callback hasn't been called already by releasing the GrTexture |
| 430 | // then we can be sure that won't happen so long as we have a ref to the texture. |
Brian Salomon | 1bf0ed8 | 2019-01-16 13:51:35 -0500 | [diff] [blame] | 431 | if (cachedTexture && !fReleaseContext->isReleased()) { |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 432 | return std::move(cachedTexture); |
| 433 | } |
| 434 | GrBackendTexture backendTexture; |
Brian Salomon | 553610d | 2019-01-14 17:34:12 -0500 | [diff] [blame] | 435 | sk_sp<SkPromiseImageTexture> promiseTexture = |
Brian Salomon | 1bf0ed8 | 2019-01-16 13:51:35 -0500 | [diff] [blame] | 436 | fFulfillProc(fReleaseContext->textureContext()); |
| 437 | fReleaseContext->notifyWasFulfilled(); |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 438 | if (!promiseTexture) { |
Brian Salomon | 1bf0ed8 | 2019-01-16 13:51:35 -0500 | [diff] [blame] | 439 | fReleaseContext->release(); |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 440 | return sk_sp<GrTexture>(); |
| 441 | } |
| 442 | bool same = promiseTexture->uniqueID() == fLastFulfillID; |
| 443 | SkASSERT(!same || fLastFulfilledKey.isValid()); |
| 444 | if (same && cachedTexture) { |
Brian Salomon | 1bf0ed8 | 2019-01-16 13:51:35 -0500 | [diff] [blame] | 445 | SkASSERT(fReleaseContext->unique()); |
| 446 | this->addToIdleContext(cachedTexture.get()); |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 447 | return std::move(cachedTexture); |
| 448 | } else if (cachedTexture) { |
| 449 | cachedTexture->resourcePriv().removeUniqueKey(); |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 450 | } |
| 451 | fLastFulfillID = promiseTexture->uniqueID(); |
| 452 | |
| 453 | backendTexture = promiseTexture->backendTexture(); |
| 454 | backendTexture.fConfig = fConfig; |
| 455 | if (!backendTexture.isValid()) { |
| 456 | // Even though the GrBackendTexture is not valid, we must call the release |
| 457 | // proc to keep our contract of always calling Fulfill and Release in pairs. |
Brian Salomon | 1bf0ed8 | 2019-01-16 13:51:35 -0500 | [diff] [blame] | 458 | fReleaseContext->release(); |
Brian Salomon | 426ba46 | 2019-01-10 16:33:06 +0000 | [diff] [blame] | 459 | return sk_sp<GrTexture>(); |
Brian Salomon | 559c617 | 2019-01-10 10:23:44 -0500 | [diff] [blame] | 460 | } |
| 461 | |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 462 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
| 463 | GrUniqueKey::Builder builder(&fLastFulfilledKey, kDomain, 2, "promise"); |
| 464 | builder[0] = promiseTexture->uniqueID(); |
Brian Salomon | 1bf0ed8 | 2019-01-16 13:51:35 -0500 | [diff] [blame] | 465 | builder[1] = fConfig; |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 466 | builder.finish(); |
Brian Salomon | 0d60676 | 2019-01-25 09:58:38 -0500 | [diff] [blame^] | 467 | // A texture with this key may already exist from a different instance of this lazy |
| 468 | // callback. This could happen if the client fulfills a promise image with a texture |
| 469 | // that was previously used to fulfill a different promise image. |
| 470 | sk_sp<GrTexture> tex; |
| 471 | if (auto surf = resourceProvider->findByUniqueKey<GrSurface>(fLastFulfilledKey)) { |
| 472 | tex = sk_ref_sp(surf->asTexture()); |
| 473 | SkASSERT(tex); |
| 474 | } else { |
| 475 | if ((tex = resourceProvider->wrapBackendTexture( |
| 476 | backendTexture, kBorrow_GrWrapOwnership, GrWrapCacheable::kYes, |
| 477 | kRead_GrIOType))) { |
| 478 | tex->resourcePriv().setUniqueKey(fLastFulfilledKey); |
| 479 | } else { |
| 480 | // Even though we failed to wrap the backend texture, we must call the release |
| 481 | // proc to keep our contract of always calling Fulfill and Release in pairs. |
| 482 | fReleaseContext->release(); |
| 483 | return sk_sp<GrTexture>(); |
| 484 | } |
| 485 | } |
| 486 | this->addToIdleContext(tex.get()); |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 487 | tex->resourcePriv().setUniqueKey(fLastFulfilledKey); |
| 488 | SkASSERT(fContextID == SK_InvalidUniqueID || |
| 489 | fContextID == tex->getContext()->uniqueID()); |
| 490 | fContextID = tex->getContext()->uniqueID(); |
| 491 | promiseTexture->addKeyToInvalidate(fContextID, fLastFulfilledKey); |
Brian Salomon | be5a093 | 2018-12-10 10:03:26 -0500 | [diff] [blame] | 492 | return std::move(tex); |
| 493 | } |
| 494 | |
| 495 | private: |
Brian Salomon | 553610d | 2019-01-14 17:34:12 -0500 | [diff] [blame] | 496 | // The GrTexture's idle callback mechanism is used to call the client's Release proc via |
| 497 | // this class. This also owns a ref counted helper that calls the client's ReleaseProc when |
| 498 | // the ref count reaches zero. The callback and any Fulfilled but un-Released texture share |
| 499 | // ownership of the IdleContext. Thus, the IdleContext is destroyed and calls the Done proc |
| 500 | // after the last fulfilled texture goes idle and calls the Release proc or the proxy's |
| 501 | // destructor destroys the lazy callback, whichever comes last. |
Brian Salomon | 1bf0ed8 | 2019-01-16 13:51:35 -0500 | [diff] [blame] | 502 | class IdleContext { |
Brian Salomon | 553610d | 2019-01-14 17:34:12 -0500 | [diff] [blame] | 503 | public: |
Brian Salomon | 1bf0ed8 | 2019-01-16 13:51:35 -0500 | [diff] [blame] | 504 | class PromiseImageReleaseContext; |
Brian Salomon | 553610d | 2019-01-14 17:34:12 -0500 | [diff] [blame] | 505 | |
Brian Salomon | 1bf0ed8 | 2019-01-16 13:51:35 -0500 | [diff] [blame] | 506 | IdleContext() = default; |
Brian Salomon | 553610d | 2019-01-14 17:34:12 -0500 | [diff] [blame] | 507 | |
Brian Salomon | 1bf0ed8 | 2019-01-16 13:51:35 -0500 | [diff] [blame] | 508 | ~IdleContext() = default; |
Brian Salomon | 553610d | 2019-01-14 17:34:12 -0500 | [diff] [blame] | 509 | |
Brian Salomon | 1bf0ed8 | 2019-01-16 13:51:35 -0500 | [diff] [blame] | 510 | void addImageReleaseContext(sk_sp<PromiseImageReleaseContext> context) { |
| 511 | fReleaseContexts.addToHead(std::move(context)); |
Brian Salomon | 553610d | 2019-01-14 17:34:12 -0500 | [diff] [blame] | 512 | } |
| 513 | |
Brian Salomon | 1bf0ed8 | 2019-01-16 13:51:35 -0500 | [diff] [blame] | 514 | static void IdleProc(void* context) { |
| 515 | IdleContext* idleContext = static_cast<IdleContext*>(context); |
| 516 | for (ReleaseContextList::Iter iter = idleContext->fReleaseContexts.headIter(); |
| 517 | iter.get(); |
| 518 | iter.next()) { |
| 519 | (*iter.get())->release(); |
| 520 | } |
| 521 | idleContext->fReleaseContexts.reset(); |
| 522 | delete idleContext; |
| 523 | } |
| 524 | |
| 525 | class PromiseImageReleaseContext : public SkNVRefCnt<PromiseImageReleaseContext> { |
| 526 | public: |
| 527 | PromiseImageReleaseContext(PromiseImageTextureReleaseProc releaseProc, |
| 528 | PromiseImageTextureContext textureContext, |
| 529 | sk_sp<GrReleaseProcHelper> doneHelper) |
| 530 | : fReleaseProc(releaseProc) |
| 531 | , fTextureContext(textureContext) |
| 532 | , fDoneHelper(std::move(doneHelper)) {} |
| 533 | |
| 534 | ~PromiseImageReleaseContext() { SkASSERT(fIsReleased); } |
| 535 | |
| 536 | void release() { |
| 537 | SkASSERT(!fIsReleased); |
| 538 | fReleaseProc(fTextureContext); |
| 539 | fIsReleased = true; |
| 540 | } |
| 541 | |
| 542 | void notifyWasFulfilled() { fIsReleased = false; } |
| 543 | bool isReleased() const { return fIsReleased; } |
| 544 | |
| 545 | PromiseImageTextureContext textureContext() const { return fTextureContext; } |
| 546 | |
| 547 | private: |
| 548 | PromiseImageTextureReleaseProc fReleaseProc; |
| 549 | PromiseImageTextureContext fTextureContext; |
| 550 | sk_sp<GrReleaseProcHelper> fDoneHelper; |
| 551 | bool fIsReleased = true; |
| 552 | }; |
| 553 | |
Brian Salomon | 553610d | 2019-01-14 17:34:12 -0500 | [diff] [blame] | 554 | private: |
Brian Salomon | 1bf0ed8 | 2019-01-16 13:51:35 -0500 | [diff] [blame] | 555 | using ReleaseContextList = SkTLList<sk_sp<PromiseImageReleaseContext>, 4>; |
| 556 | ReleaseContextList fReleaseContexts; |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 557 | }; |
Brian Salomon | be5a093 | 2018-12-10 10:03:26 -0500 | [diff] [blame] | 558 | |
Brian Salomon | 1bf0ed8 | 2019-01-16 13:51:35 -0500 | [diff] [blame] | 559 | void addToIdleContext(GrTexture* texture) { |
| 560 | SkASSERT(!fReleaseContext->isReleased()); |
| 561 | IdleContext* idleContext = static_cast<IdleContext*>(texture->idleContext()); |
| 562 | if (!idleContext) { |
| 563 | idleContext = new IdleContext(); |
| 564 | texture->setIdleProc(IdleContext::IdleProc, idleContext); |
| 565 | } |
| 566 | idleContext->addImageReleaseContext(fReleaseContext); |
| 567 | } |
| 568 | |
| 569 | sk_sp<IdleContext::PromiseImageReleaseContext> fReleaseContext; |
Brian Salomon | 553610d | 2019-01-14 17:34:12 -0500 | [diff] [blame] | 570 | PromiseImageTextureFulfillProc fFulfillProc; |
| 571 | GrPixelConfig fConfig; |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 572 | |
| 573 | // ID of the last SkPromiseImageTexture given to us by the client. |
| 574 | uint32_t fLastFulfillID = 0; |
| 575 | // ID of the GrContext that we are interacting with. |
| 576 | uint32_t fContextID = SK_InvalidUniqueID; |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 577 | GrUniqueKey fLastFulfilledKey; |
Brian Salomon | be5a093 | 2018-12-10 10:03:26 -0500 | [diff] [blame] | 578 | } callback(fulfillProc, releaseProc, doneProc, textureContext, config); |
| 579 | |
| 580 | GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider(); |
| 581 | |
| 582 | GrSurfaceDesc desc; |
| 583 | desc.fWidth = width; |
| 584 | desc.fHeight = height; |
| 585 | desc.fConfig = config; |
| 586 | |
| 587 | // We pass kReadOnly here since we should treat content of the client's texture as immutable. |
| 588 | return proxyProvider->createLazyProxy(std::move(callback), backendFormat, desc, origin, |
| 589 | mipMapped, GrInternalSurfaceFlags::kReadOnly, |
| 590 | SkBackingFit::kExact, SkBudgeted::kNo, |
| 591 | GrSurfaceProxy::LazyInstantiationType::kDeinstantiate); |
Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 592 | } |