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