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 | |
| 8 | #include "GrBackendSurface.h" |
| 9 | #include "GrClip.h" |
| 10 | #include "GrContext.h" |
| 11 | #include "GrContextPriv.h" |
| 12 | #include "GrRenderTargetContext.h" |
| 13 | #include "GrTexture.h" |
| 14 | #include "GrTextureAdjuster.h" |
Jim Van Verth | 0e67194 | 2018-11-09 12:03:57 -0500 | [diff] [blame] | 15 | #include "effects/GrYUVtoRGBEffect.h" |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 16 | #include "SkBitmapCache.h" |
| 17 | #include "SkImage_Gpu.h" |
| 18 | #include "SkImage_GpuBase.h" |
Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 19 | #include "SkReadPixelsRec.h" |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 20 | |
| 21 | SkImage_GpuBase::SkImage_GpuBase(sk_sp<GrContext> context, int width, int height, uint32_t uniqueID, |
| 22 | SkAlphaType at, SkBudgeted budgeted, sk_sp<SkColorSpace> cs) |
| 23 | : INHERITED(width, height, uniqueID) |
| 24 | , fContext(std::move(context)) |
| 25 | , fAlphaType(at) |
| 26 | , fBudgeted(budgeted) |
| 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 | } |
| 45 | |
| 46 | return ctx->contextPriv().caps()->validateBackendTexture(tex, ct, config); |
| 47 | } |
| 48 | |
| 49 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
| 50 | |
Brian Osman | e50cdf0 | 2018-10-19 13:02:14 -0400 | [diff] [blame] | 51 | bool SkImage_GpuBase::getROPixels(SkBitmap* dst, CachingHint chint) const { |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 52 | if (!fContext->contextPriv().resourceProvider()) { |
| 53 | // DDL TODO: buffer up the readback so it occurs when the DDL is drawn? |
| 54 | return false; |
| 55 | } |
| 56 | |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 57 | const auto desc = SkBitmapCacheDesc::Make(this); |
| 58 | if (SkBitmapCache::Find(desc, dst)) { |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 59 | SkASSERT(dst->isImmutable()); |
| 60 | SkASSERT(dst->getPixels()); |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | SkBitmapCache::RecPtr rec = nullptr; |
| 65 | SkPixmap pmap; |
| 66 | if (kAllow_CachingHint == chint) { |
| 67 | rec = SkBitmapCache::Alloc(desc, this->onImageInfo(), &pmap); |
| 68 | if (!rec) { |
| 69 | return false; |
| 70 | } |
| 71 | } else { |
| 72 | if (!dst->tryAllocPixels(this->onImageInfo()) || !dst->peekPixels(&pmap)) { |
| 73 | return false; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | sk_sp<GrSurfaceContext> sContext = fContext->contextPriv().makeWrappedSurfaceContext( |
| 78 | this->asTextureProxyRef(), |
| 79 | fColorSpace); |
| 80 | if (!sContext) { |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | if (!sContext->readPixels(pmap.info(), pmap.writable_addr(), pmap.rowBytes(), 0, 0)) { |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | if (rec) { |
| 89 | SkBitmapCache::Add(std::move(rec), dst); |
| 90 | this->notifyAddedToRasterCache(); |
| 91 | } |
| 92 | return true; |
| 93 | } |
| 94 | |
| 95 | sk_sp<SkImage> SkImage_GpuBase::onMakeSubset(const SkIRect& subset) const { |
| 96 | sk_sp<GrSurfaceProxy> proxy = this->asTextureProxyRef(); |
| 97 | |
| 98 | GrSurfaceDesc desc; |
| 99 | desc.fWidth = subset.width(); |
| 100 | desc.fHeight = subset.height(); |
| 101 | desc.fConfig = proxy->config(); |
| 102 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 103 | GrBackendFormat format = proxy->backendFormat().makeTexture2D(); |
| 104 | if (!format.isValid()) { |
| 105 | return nullptr; |
| 106 | } |
| 107 | |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 108 | sk_sp<GrSurfaceContext> sContext(fContext->contextPriv().makeDeferredSurfaceContext( |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 109 | format, desc, proxy->origin(), GrMipMapped::kNo, SkBackingFit::kExact, fBudgeted)); |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 110 | if (!sContext) { |
| 111 | return nullptr; |
| 112 | } |
| 113 | |
| 114 | if (!sContext->copy(proxy.get(), subset, SkIPoint::Make(0, 0))) { |
| 115 | return nullptr; |
| 116 | } |
| 117 | |
| 118 | // MDB: this call is okay bc we know 'sContext' was kExact |
| 119 | return sk_make_sp<SkImage_Gpu>(fContext, kNeedNewImageUniqueID, |
| 120 | fAlphaType, sContext->asTextureProxyRef(), |
| 121 | fColorSpace, fBudgeted); |
| 122 | } |
| 123 | |
Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 124 | static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes) { |
| 125 | switch (info.colorType()) { |
| 126 | case kRGBA_8888_SkColorType: |
| 127 | case kBGRA_8888_SkColorType: |
| 128 | break; |
| 129 | default: |
| 130 | return; // nothing to do |
| 131 | } |
| 132 | |
| 133 | // SkColor is not necesarily RGBA or BGRA, but it is one of them on little-endian, |
| 134 | // and in either case, the alpha-byte is always in the same place, so we can safely call |
| 135 | // SkPreMultiplyColor() |
| 136 | // |
| 137 | SkColor* row = (SkColor*)pixels; |
| 138 | for (int y = 0; y < info.height(); ++y) { |
| 139 | for (int x = 0; x < info.width(); ++x) { |
| 140 | row[x] = SkPreMultiplyColor(row[x]); |
| 141 | } |
| 142 | row = (SkColor*)((char*)(row)+rowBytes); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | bool SkImage_GpuBase::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB, |
| 147 | int srcX, int srcY, CachingHint) const { |
| 148 | if (!fContext->contextPriv().resourceProvider()) { |
| 149 | // DDL TODO: buffer up the readback so it occurs when the DDL is drawn? |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | if (!SkImageInfoValidConversion(dstInfo, this->onImageInfo())) { |
| 154 | return false; |
| 155 | } |
| 156 | |
| 157 | SkReadPixelsRec rec(dstInfo, dstPixels, dstRB, srcX, srcY); |
| 158 | if (!rec.trim(this->width(), this->height())) { |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | // TODO: this seems to duplicate code in GrTextureContext::onReadPixels and |
| 163 | // GrRenderTargetContext::onReadPixels |
| 164 | uint32_t flags = 0; |
| 165 | if (kUnpremul_SkAlphaType == rec.fInfo.alphaType() && kPremul_SkAlphaType == fAlphaType) { |
| 166 | // let the GPU perform this transformation for us |
| 167 | flags = GrContextPriv::kUnpremul_PixelOpsFlag; |
| 168 | } |
| 169 | |
| 170 | sk_sp<GrSurfaceContext> sContext = fContext->contextPriv().makeWrappedSurfaceContext( |
| 171 | this->asTextureProxyRef(), fColorSpace); |
| 172 | if (!sContext) { |
| 173 | return false; |
| 174 | } |
| 175 | |
| 176 | if (!sContext->readPixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY, flags)) { |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | // do we have to manually fix-up the alpha channel? |
| 181 | // src dst |
| 182 | // unpremul premul fix manually |
| 183 | // premul unpremul done by kUnpremul_PixelOpsFlag |
| 184 | // all other combos need to change. |
| 185 | // |
| 186 | // Should this be handled by Ganesh? todo:? |
| 187 | // |
| 188 | if (kPremul_SkAlphaType == rec.fInfo.alphaType() && kUnpremul_SkAlphaType == fAlphaType) { |
| 189 | apply_premul(rec.fInfo, rec.fPixels, rec.fRowBytes); |
| 190 | } |
| 191 | return true; |
| 192 | } |
| 193 | |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 194 | sk_sp<GrTextureProxy> SkImage_GpuBase::asTextureProxyRef(GrContext* context, |
| 195 | const GrSamplerState& params, |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 196 | SkScalar scaleAdjust[2]) const { |
| 197 | if (context->uniqueID() != fContext->uniqueID()) { |
| 198 | SkASSERT(0); |
| 199 | return nullptr; |
| 200 | } |
| 201 | |
| 202 | GrTextureAdjuster adjuster(fContext.get(), this->asTextureProxyRef(), fAlphaType, |
| 203 | this->uniqueID(), fColorSpace.get()); |
Brian Osman | 6064e1c | 2018-10-19 14:27:54 -0400 | [diff] [blame] | 204 | return adjuster.refTextureProxyForParams(params, scaleAdjust); |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | GrBackendTexture SkImage_GpuBase::onGetBackendTexture(bool flushPendingGrContextIO, |
| 208 | GrSurfaceOrigin* origin) const { |
| 209 | sk_sp<GrTextureProxy> proxy = this->asTextureProxyRef(); |
| 210 | SkASSERT(proxy); |
| 211 | |
| 212 | if (!fContext->contextPriv().resourceProvider() && !proxy->isInstantiated()) { |
| 213 | // This image was created with a DDL context and cannot be instantiated. |
| 214 | return GrBackendTexture(); |
| 215 | } |
| 216 | |
| 217 | if (!proxy->instantiate(fContext->contextPriv().resourceProvider())) { |
| 218 | return GrBackendTexture(); // invalid |
| 219 | } |
| 220 | |
| 221 | GrTexture* texture = proxy->peekTexture(); |
| 222 | |
| 223 | if (texture) { |
| 224 | if (flushPendingGrContextIO) { |
| 225 | fContext->contextPriv().prepareSurfaceForExternalIO(proxy.get()); |
| 226 | } |
| 227 | if (origin) { |
| 228 | *origin = proxy->origin(); |
| 229 | } |
| 230 | return texture->getBackendTexture(); |
| 231 | } |
| 232 | return GrBackendTexture(); // invalid |
| 233 | } |
| 234 | |
| 235 | GrTexture* SkImage_GpuBase::onGetTexture() const { |
| 236 | GrTextureProxy* proxy = this->peekProxy(); |
| 237 | if (!proxy) { |
| 238 | return nullptr; |
| 239 | } |
| 240 | |
| 241 | sk_sp<GrTextureProxy> proxyRef = this->asTextureProxyRef(); |
| 242 | if (!fContext->contextPriv().resourceProvider() && !proxyRef->isInstantiated()) { |
| 243 | // This image was created with a DDL context and cannot be instantiated. |
| 244 | return nullptr; |
| 245 | } |
| 246 | |
| 247 | if (!proxy->instantiate(fContext->contextPriv().resourceProvider())) { |
| 248 | return nullptr; |
| 249 | } |
| 250 | |
| 251 | return proxy->peekTexture(); |
| 252 | } |
| 253 | |
| 254 | sk_sp<SkImage> SkImage_GpuBase::onMakeColorSpace(sk_sp<SkColorSpace> target) const { |
Brian Osman | be686f0 | 2018-10-12 11:18:02 -0400 | [diff] [blame] | 255 | auto xform = GrColorSpaceXformEffect::Make(fColorSpace.get(), fAlphaType, |
| 256 | target.get(), fAlphaType); |
Brian Osman | b4ae499 | 2018-10-18 11:16:14 -0400 | [diff] [blame] | 257 | SkASSERT(xform); |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 258 | |
Brian Osman | 9c11135 | 2018-10-16 10:18:26 -0400 | [diff] [blame] | 259 | sk_sp<GrTextureProxy> proxy = this->asTextureProxyRef(); |
| 260 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 261 | GrBackendFormat format = proxy->backendFormat().makeTexture2D(); |
| 262 | if (!format.isValid()) { |
| 263 | return nullptr; |
| 264 | } |
| 265 | |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 266 | sk_sp<GrRenderTargetContext> renderTargetContext( |
Brian Osman | 9c11135 | 2018-10-16 10:18:26 -0400 | [diff] [blame] | 267 | fContext->contextPriv().makeDeferredRenderTargetContextWithFallback( |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 268 | format, SkBackingFit::kExact, this->width(), this->height(), |
| 269 | proxy->config(), nullptr)); |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 270 | if (!renderTargetContext) { |
| 271 | return nullptr; |
| 272 | } |
| 273 | |
| 274 | GrPaint paint; |
| 275 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
Brian Osman | 9c11135 | 2018-10-16 10:18:26 -0400 | [diff] [blame] | 276 | paint.addColorTextureProcessor(std::move(proxy), SkMatrix::I()); |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 277 | paint.addColorFragmentProcessor(std::move(xform)); |
| 278 | |
Brian Osman | b4ae499 | 2018-10-18 11:16:14 -0400 | [diff] [blame] | 279 | renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), |
| 280 | SkRect::MakeIWH(this->width(), this->height())); |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 281 | if (!renderTargetContext->asTextureProxy()) { |
| 282 | return nullptr; |
| 283 | } |
| 284 | |
| 285 | // MDB: this call is okay bc we know 'renderTargetContext' was exact |
| 286 | return sk_make_sp<SkImage_Gpu>(fContext, kNeedNewImageUniqueID, |
Brian Osman | be686f0 | 2018-10-12 11:18:02 -0400 | [diff] [blame] | 287 | fAlphaType, renderTargetContext->asTextureProxyRef(), |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 288 | std::move(target), fBudgeted); |
| 289 | } |
| 290 | |
| 291 | bool SkImage_GpuBase::onIsValid(GrContext* context) const { |
| 292 | // The base class has already checked that context isn't abandoned (if it's not nullptr) |
| 293 | if (fContext->abandoned()) { |
| 294 | return false; |
| 295 | } |
| 296 | |
| 297 | if (context && context != fContext.get()) { |
| 298 | return false; |
| 299 | } |
| 300 | |
| 301 | return true; |
| 302 | } |
Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 303 | |
Jim Van Verth | 0e67194 | 2018-11-09 12:03:57 -0500 | [diff] [blame] | 304 | bool SkImage_GpuBase::MakeTempTextureProxies(GrContext* ctx, const GrBackendTexture yuvaTextures[], |
Jim Van Verth | 5327536 | 2018-11-09 15:42:35 -0500 | [diff] [blame] | 305 | int numTextures, const SkYUVAIndex yuvaIndices[4], |
| 306 | GrSurfaceOrigin imageOrigin, |
Jim Van Verth | 0e67194 | 2018-11-09 12:03:57 -0500 | [diff] [blame] | 307 | sk_sp<GrTextureProxy> tempTextureProxies[4]) { |
| 308 | GrProxyProvider* proxyProvider = ctx->contextPriv().proxyProvider(); |
| 309 | |
| 310 | // We need to make a copy of the input backend textures because we need to preserve the result |
| 311 | // of validate_backend_texture. |
| 312 | GrBackendTexture yuvaTexturesCopy[4]; |
| 313 | for (int textureIndex = 0; textureIndex < numTextures; ++textureIndex) { |
| 314 | yuvaTexturesCopy[textureIndex] = yuvaTextures[textureIndex]; |
| 315 | if (!ctx->contextPriv().caps()->getYUVAConfigFromBackendTexture( |
| 316 | yuvaTexturesCopy[textureIndex], |
| 317 | &yuvaTexturesCopy[textureIndex].fConfig)) { |
| 318 | return false; |
| 319 | } |
| 320 | SkASSERT(yuvaTexturesCopy[textureIndex].isValid()); |
| 321 | |
| 322 | tempTextureProxies[textureIndex] = |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 323 | proxyProvider->wrapBackendTexture(yuvaTexturesCopy[textureIndex], imageOrigin, |
| 324 | kBorrow_GrWrapOwnership, kRead_GrIOType); |
Jim Van Verth | 0e67194 | 2018-11-09 12:03:57 -0500 | [diff] [blame] | 325 | if (!tempTextureProxies[textureIndex]) { |
| 326 | return false; |
| 327 | } |
Jim Van Verth | 5327536 | 2018-11-09 15:42:35 -0500 | [diff] [blame] | 328 | |
| 329 | // Check that each texture contains the channel data for the corresponding YUVA index |
| 330 | GrPixelConfig config = yuvaTexturesCopy[textureIndex].fConfig; |
| 331 | for (int yuvaIndex = 0; yuvaIndex < SkYUVAIndex::kIndexCount; ++yuvaIndex) { |
| 332 | if (yuvaIndices[yuvaIndex].fIndex == textureIndex) { |
| 333 | switch (yuvaIndices[yuvaIndex].fChannel) { |
| 334 | case SkColorChannel::kR: |
| 335 | if (kAlpha_8_as_Alpha_GrPixelConfig == config) { |
| 336 | return false; |
| 337 | } |
| 338 | break; |
| 339 | case SkColorChannel::kG: |
| 340 | case SkColorChannel::kB: |
| 341 | if (kAlpha_8_as_Alpha_GrPixelConfig == config || |
| 342 | kAlpha_8_as_Red_GrPixelConfig == config) { |
| 343 | return false; |
| 344 | } |
| 345 | break; |
| 346 | case SkColorChannel::kA: |
| 347 | default: |
| 348 | if (kRGB_888_GrPixelConfig == config) { |
| 349 | return false; |
| 350 | } |
| 351 | break; |
| 352 | } |
| 353 | } |
| 354 | } |
Jim Van Verth | 0e67194 | 2018-11-09 12:03:57 -0500 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | return true; |
| 358 | } |
| 359 | |
| 360 | bool SkImage_GpuBase::RenderYUVAToRGBA(GrContext* ctx, GrRenderTargetContext* renderTargetContext, |
| 361 | const SkRect& rect, SkYUVColorSpace yuvColorSpace, |
| 362 | const sk_sp<GrTextureProxy> proxies[4], |
| 363 | const SkYUVAIndex yuvaIndices[4]) { |
| 364 | SkASSERT(renderTargetContext); |
| 365 | if (!renderTargetContext->asSurfaceProxy()) { |
| 366 | return false; |
| 367 | } |
| 368 | |
| 369 | GrPaint paint; |
| 370 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
| 371 | |
Jim Van Verth | 0e67194 | 2018-11-09 12:03:57 -0500 | [diff] [blame] | 372 | paint.addColorFragmentProcessor(GrYUVtoRGBEffect::Make(proxies, yuvaIndices, |
| 373 | yuvColorSpace, |
| 374 | GrSamplerState::Filter::kNearest)); |
| 375 | |
| 376 | renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect); |
| 377 | |
| 378 | // DDL TODO: in the promise image version we must not flush here |
| 379 | ctx->contextPriv().flushSurfaceWrites(renderTargetContext->asSurfaceProxy()); |
| 380 | |
| 381 | return true; |
| 382 | } |
| 383 | |
Brian Salomon | be5a093 | 2018-12-10 10:03:26 -0500 | [diff] [blame^] | 384 | sk_sp<GrTextureProxy> SkImage_GpuBase::MakePromiseImageLazyProxy( |
| 385 | GrContext* context, int width, int height, GrSurfaceOrigin origin, GrPixelConfig config, |
| 386 | GrBackendFormat backendFormat, GrMipMapped mipMapped, |
| 387 | SkImage_GpuBase::TextureFulfillProc fulfillProc, |
| 388 | SkImage_GpuBase::TextureReleaseProc releaseProc, SkImage_GpuBase::PromiseDoneProc doneProc, |
| 389 | SkImage_GpuBase::TextureContext textureContext) { |
| 390 | SkASSERT(context); |
| 391 | SkASSERT(width > 0 && height > 0); |
| 392 | SkASSERT(doneProc); |
| 393 | |
| 394 | if (!fulfillProc || !releaseProc) { |
| 395 | doneProc(textureContext); |
| 396 | return nullptr; |
Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 397 | } |
| 398 | |
Brian Salomon | be5a093 | 2018-12-10 10:03:26 -0500 | [diff] [blame^] | 399 | if (mipMapped == GrMipMapped::kYes && |
| 400 | GrTextureTypeHasRestrictedSampling(backendFormat.textureType())) { |
| 401 | // It is invalid to have a GL_TEXTURE_EXTERNAL or GL_TEXTURE_RECTANGLE and have mips as |
| 402 | // well. |
| 403 | doneProc(textureContext); |
| 404 | return nullptr; |
Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 405 | } |
Brian Salomon | be5a093 | 2018-12-10 10:03:26 -0500 | [diff] [blame^] | 406 | |
| 407 | /** |
| 408 | * This helper class manages the ref counting for the the ReleaseProc and DoneProc for promise |
| 409 | * images. It holds a weak ref on the ReleaseProc (hard refs are owned by GrTextures). The weak |
| 410 | * ref allows us to reuse an outstanding ReleaseProc (because we dropped our GrTexture but the |
| 411 | * GrTexture isn't done on the GPU) without needing to call FulfillProc again. It also holds a |
| 412 | * hard ref on the DoneProc. The idea is that after every flush we may call the ReleaseProc so |
| 413 | * that the client can free up their GPU memory if they want to. The life time of the DoneProc |
| 414 | * matches that of any outstanding ReleaseProc as well as the PromiseLazyInstantiateCallback. |
| 415 | * Thus we won't call the DoneProc until all ReleaseProcs are finished and we are finished with |
| 416 | * the PromiseImageHelper (i.e. won't call FulfillProc again). |
| 417 | */ |
| 418 | class PromiseLazyInstantiateCallback { |
| 419 | public: |
| 420 | PromiseLazyInstantiateCallback(SkImage_GpuBase::TextureFulfillProc fulfillProc, |
| 421 | SkImage_GpuBase::TextureReleaseProc releaseProc, |
| 422 | SkImage_GpuBase::PromiseDoneProc doneProc, |
| 423 | SkImage_GpuBase::TextureContext context, |
| 424 | GrPixelConfig config) |
| 425 | : fFulfillProc(fulfillProc) |
| 426 | , fReleaseProc(releaseProc) |
| 427 | , fContext(context) |
| 428 | , fConfig(config) { |
| 429 | fDoneHelper.reset(new GrReleaseProcHelper(doneProc, context)); |
| 430 | } |
| 431 | |
| 432 | sk_sp<GrSurface> operator()(GrResourceProvider* resourceProvider) { |
| 433 | if (!resourceProvider) { |
| 434 | this->reset(); |
| 435 | return sk_sp<GrTexture>(); |
| 436 | } |
| 437 | |
| 438 | // Releases the promise helper if there are no outstanding hard refs. This means that we |
| 439 | // don't have any ReleaseProcs waiting to be called so we will need to do a fulfill. |
| 440 | if (fReleaseHelper && fReleaseHelper->weak_expired()) { |
| 441 | this->resetReleaseHelper(); |
| 442 | } |
| 443 | |
| 444 | sk_sp<GrTexture> tex; |
| 445 | if (!fReleaseHelper) { |
| 446 | fFulfillProc(fContext, &fBackendTex); |
| 447 | fBackendTex.fConfig = fConfig; |
| 448 | if (!fBackendTex.isValid()) { |
| 449 | // Even though the GrBackendTexture is not valid, we must call the release |
| 450 | // proc to keep our contract of always calling Fulfill and Release in pairs. |
| 451 | fReleaseProc(fContext); |
| 452 | return sk_sp<GrTexture>(); |
| 453 | } |
| 454 | |
| 455 | tex = resourceProvider->wrapBackendTexture(fBackendTex, kBorrow_GrWrapOwnership, |
| 456 | kRead_GrIOType); |
| 457 | if (!tex) { |
| 458 | // Even though the GrBackendTexture is not valid, we must call the release |
| 459 | // proc to keep our contract of always calling Fulfill and Release in pairs. |
| 460 | fReleaseProc(fContext); |
| 461 | return sk_sp<GrTexture>(); |
| 462 | } |
| 463 | fReleaseHelper = |
| 464 | new SkPromiseReleaseProcHelper(fReleaseProc, fContext, fDoneHelper); |
| 465 | // Take a weak ref |
| 466 | fReleaseHelper->weak_ref(); |
| 467 | } else { |
| 468 | SkASSERT(fBackendTex.isValid()); |
| 469 | tex = resourceProvider->wrapBackendTexture(fBackendTex, kBorrow_GrWrapOwnership, |
| 470 | kRead_GrIOType); |
| 471 | if (!tex) { |
| 472 | // We weren't able to make a texture here, but since we are in this branch |
| 473 | // of the calls (promiseHelper.fReleaseHelper is valid) there is already a |
| 474 | // texture out there which will call the release proc so we don't need to |
| 475 | // call it here. |
| 476 | return sk_sp<GrTexture>(); |
| 477 | } |
| 478 | |
| 479 | SkAssertResult(fReleaseHelper->try_ref()); |
| 480 | } |
| 481 | SkASSERT(tex); |
| 482 | // Pass the hard ref off to the texture |
| 483 | tex->setRelease(sk_sp<GrReleaseProcHelper>(fReleaseHelper)); |
| 484 | return std::move(tex); |
| 485 | } |
| 486 | |
| 487 | private: |
| 488 | void reset() { |
| 489 | this->resetReleaseHelper(); |
| 490 | fDoneHelper.reset(); |
| 491 | } |
| 492 | |
| 493 | // Weak unrefs fReleaseHelper and sets it to null |
| 494 | void resetReleaseHelper() { |
| 495 | if (fReleaseHelper) { |
| 496 | fReleaseHelper->weak_unref(); |
| 497 | fReleaseHelper = nullptr; |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | SkImage_GpuBase::TextureFulfillProc fFulfillProc; |
| 502 | SkImage_GpuBase::TextureReleaseProc fReleaseProc; |
| 503 | SkImage_GpuBase::TextureContext fContext; |
| 504 | |
| 505 | GrPixelConfig fConfig; |
| 506 | // We cache the GrBackendTexture so that if we deleted the GrTexture but the the release |
| 507 | // proc has yet not been called (this can happen on Vulkan), then we can create a new |
| 508 | // texture without needing to call the fulfill proc again. |
| 509 | GrBackendTexture fBackendTex; |
| 510 | // The fReleaseHelper is used to track a weak ref on the release proc. This helps us make |
| 511 | // sure we are always pairing fulfill and release proc calls correctly. |
| 512 | SkPromiseReleaseProcHelper* fReleaseHelper = nullptr; |
| 513 | // We don't want to call the fDoneHelper until we are done with the PromiseImageHelper and |
| 514 | // all ReleaseHelpers are finished. Thus we hold a hard ref here and we will pass a hard ref |
| 515 | // to each fReleaseHelper we make. |
| 516 | sk_sp<GrReleaseProcHelper> fDoneHelper; |
| 517 | } callback(fulfillProc, releaseProc, doneProc, textureContext, config); |
| 518 | |
| 519 | GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider(); |
| 520 | |
| 521 | GrSurfaceDesc desc; |
| 522 | desc.fWidth = width; |
| 523 | desc.fHeight = height; |
| 524 | desc.fConfig = config; |
| 525 | |
| 526 | // We pass kReadOnly here since we should treat content of the client's texture as immutable. |
| 527 | return proxyProvider->createLazyProxy(std::move(callback), backendFormat, desc, origin, |
| 528 | mipMapped, GrInternalSurfaceFlags::kReadOnly, |
| 529 | SkBackingFit::kExact, SkBudgeted::kNo, |
| 530 | GrSurfaceProxy::LazyInstantiationType::kDeinstantiate); |
Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 531 | } |