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