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 | 920d488 | 2019-03-04 15:16:44 -0500 | [diff] [blame] | 64 | auto direct = fContext->priv().asDirectContext(); |
| 65 | if (!direct) { |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 66 | // DDL TODO: buffer up the readback so it occurs when the DDL is drawn? |
| 67 | return false; |
| 68 | } |
| 69 | |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 70 | const auto desc = SkBitmapCacheDesc::Make(this); |
| 71 | if (SkBitmapCache::Find(desc, dst)) { |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 72 | SkASSERT(dst->isImmutable()); |
| 73 | SkASSERT(dst->getPixels()); |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | SkBitmapCache::RecPtr rec = nullptr; |
| 78 | SkPixmap pmap; |
| 79 | if (kAllow_CachingHint == chint) { |
| 80 | rec = SkBitmapCache::Alloc(desc, this->onImageInfo(), &pmap); |
| 81 | if (!rec) { |
| 82 | return false; |
| 83 | } |
| 84 | } else { |
| 85 | if (!dst->tryAllocPixels(this->onImageInfo()) || !dst->peekPixels(&pmap)) { |
| 86 | return false; |
| 87 | } |
| 88 | } |
| 89 | |
Robert Phillips | 920d488 | 2019-03-04 15:16:44 -0500 | [diff] [blame] | 90 | sk_sp<GrSurfaceContext> sContext = direct->priv().makeWrappedSurfaceContext( |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 91 | this->asTextureProxyRef(), |
| 92 | fColorSpace); |
| 93 | if (!sContext) { |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | if (!sContext->readPixels(pmap.info(), pmap.writable_addr(), pmap.rowBytes(), 0, 0)) { |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | if (rec) { |
| 102 | SkBitmapCache::Add(std::move(rec), dst); |
| 103 | this->notifyAddedToRasterCache(); |
| 104 | } |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | sk_sp<SkImage> SkImage_GpuBase::onMakeSubset(const SkIRect& subset) const { |
| 109 | sk_sp<GrSurfaceProxy> proxy = this->asTextureProxyRef(); |
| 110 | |
| 111 | GrSurfaceDesc desc; |
| 112 | desc.fWidth = subset.width(); |
| 113 | desc.fHeight = subset.height(); |
| 114 | desc.fConfig = proxy->config(); |
| 115 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 116 | GrBackendFormat format = proxy->backendFormat().makeTexture2D(); |
| 117 | if (!format.isValid()) { |
| 118 | return nullptr; |
| 119 | } |
| 120 | |
Brian Salomon | f05e6d3 | 2018-12-20 08:41:41 -0500 | [diff] [blame] | 121 | // TODO: Should this inherit our proxy's budgeted status? |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 122 | sk_sp<GrSurfaceContext> sContext(fContext->priv().makeDeferredSurfaceContext( |
Brian Salomon | f05e6d3 | 2018-12-20 08:41:41 -0500 | [diff] [blame] | 123 | format, desc, proxy->origin(), GrMipMapped::kNo, SkBackingFit::kExact, |
| 124 | proxy->isBudgeted())); |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 125 | if (!sContext) { |
| 126 | return nullptr; |
| 127 | } |
| 128 | |
| 129 | if (!sContext->copy(proxy.get(), subset, SkIPoint::Make(0, 0))) { |
| 130 | return nullptr; |
| 131 | } |
| 132 | |
| 133 | // MDB: this call is okay bc we know 'sContext' was kExact |
Brian Salomon | f05e6d3 | 2018-12-20 08:41:41 -0500 | [diff] [blame] | 134 | return sk_make_sp<SkImage_Gpu>(fContext, kNeedNewImageUniqueID, fAlphaType, |
Brian Osman | e956049 | 2019-02-05 17:00:03 -0500 | [diff] [blame] | 135 | sContext->asTextureProxyRef(), this->refColorSpace()); |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 136 | } |
| 137 | |
Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 138 | static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes) { |
| 139 | switch (info.colorType()) { |
| 140 | case kRGBA_8888_SkColorType: |
| 141 | case kBGRA_8888_SkColorType: |
| 142 | break; |
| 143 | default: |
| 144 | return; // nothing to do |
| 145 | } |
| 146 | |
Brian Salomon | 3f4cd77 | 2019-01-11 16:03:19 -0500 | [diff] [blame] | 147 | // 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] | 148 | // and in either case, the alpha-byte is always in the same place, so we can safely call |
| 149 | // SkPreMultiplyColor() |
| 150 | // |
| 151 | SkColor* row = (SkColor*)pixels; |
| 152 | for (int y = 0; y < info.height(); ++y) { |
| 153 | for (int x = 0; x < info.width(); ++x) { |
| 154 | row[x] = SkPreMultiplyColor(row[x]); |
| 155 | } |
| 156 | row = (SkColor*)((char*)(row)+rowBytes); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | bool SkImage_GpuBase::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB, |
| 161 | int srcX, int srcY, CachingHint) const { |
Robert Phillips | 920d488 | 2019-03-04 15:16:44 -0500 | [diff] [blame] | 162 | auto direct = fContext->priv().asDirectContext(); |
| 163 | if (!direct) { |
Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 164 | // DDL TODO: buffer up the readback so it occurs when the DDL is drawn? |
| 165 | return false; |
| 166 | } |
| 167 | |
| 168 | if (!SkImageInfoValidConversion(dstInfo, this->onImageInfo())) { |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | SkReadPixelsRec rec(dstInfo, dstPixels, dstRB, srcX, srcY); |
| 173 | if (!rec.trim(this->width(), this->height())) { |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | // TODO: this seems to duplicate code in GrTextureContext::onReadPixels and |
| 178 | // GrRenderTargetContext::onReadPixels |
| 179 | uint32_t flags = 0; |
| 180 | if (kUnpremul_SkAlphaType == rec.fInfo.alphaType() && kPremul_SkAlphaType == fAlphaType) { |
| 181 | // let the GPU perform this transformation for us |
| 182 | flags = GrContextPriv::kUnpremul_PixelOpsFlag; |
| 183 | } |
| 184 | |
Robert Phillips | 920d488 | 2019-03-04 15:16:44 -0500 | [diff] [blame] | 185 | sk_sp<GrSurfaceContext> sContext = direct->priv().makeWrappedSurfaceContext( |
Brian Osman | e956049 | 2019-02-05 17:00:03 -0500 | [diff] [blame] | 186 | this->asTextureProxyRef(), this->refColorSpace()); |
Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 187 | if (!sContext) { |
| 188 | return false; |
| 189 | } |
| 190 | |
| 191 | if (!sContext->readPixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY, flags)) { |
| 192 | return false; |
| 193 | } |
| 194 | |
| 195 | // do we have to manually fix-up the alpha channel? |
| 196 | // src dst |
| 197 | // unpremul premul fix manually |
| 198 | // premul unpremul done by kUnpremul_PixelOpsFlag |
| 199 | // all other combos need to change. |
| 200 | // |
| 201 | // Should this be handled by Ganesh? todo:? |
| 202 | // |
| 203 | if (kPremul_SkAlphaType == rec.fInfo.alphaType() && kUnpremul_SkAlphaType == fAlphaType) { |
| 204 | apply_premul(rec.fInfo, rec.fPixels, rec.fRowBytes); |
| 205 | } |
| 206 | return true; |
| 207 | } |
| 208 | |
Robert Phillips | 9338c60 | 2019-02-19 12:52:29 -0500 | [diff] [blame] | 209 | sk_sp<GrTextureProxy> SkImage_GpuBase::asTextureProxyRef(GrRecordingContext* context, |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 210 | const GrSamplerState& params, |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 211 | SkScalar scaleAdjust[2]) const { |
Robert Phillips | fe0963c | 2019-02-07 13:25:07 -0500 | [diff] [blame] | 212 | if (!fContext->priv().matches(context)) { |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 213 | SkASSERT(0); |
| 214 | return nullptr; |
| 215 | } |
| 216 | |
| 217 | GrTextureAdjuster adjuster(fContext.get(), this->asTextureProxyRef(), fAlphaType, |
| 218 | this->uniqueID(), fColorSpace.get()); |
Brian Osman | 6064e1c | 2018-10-19 14:27:54 -0400 | [diff] [blame] | 219 | return adjuster.refTextureProxyForParams(params, scaleAdjust); |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | GrBackendTexture SkImage_GpuBase::onGetBackendTexture(bool flushPendingGrContextIO, |
| 223 | GrSurfaceOrigin* origin) const { |
Robert Phillips | 920d488 | 2019-03-04 15:16:44 -0500 | [diff] [blame] | 224 | auto direct = fContext->priv().asDirectContext(); |
| 225 | if (!direct) { |
| 226 | // This image was created with a DDL context and cannot be instantiated. |
| 227 | return GrBackendTexture(); |
| 228 | } |
| 229 | |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 230 | sk_sp<GrTextureProxy> proxy = this->asTextureProxyRef(); |
| 231 | SkASSERT(proxy); |
| 232 | |
Robert Phillips | 920d488 | 2019-03-04 15:16:44 -0500 | [diff] [blame] | 233 | if (!proxy->isInstantiated()) { |
| 234 | auto resourceProvider = direct->priv().resourceProvider(); |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 235 | |
Robert Phillips | 920d488 | 2019-03-04 15:16:44 -0500 | [diff] [blame] | 236 | if (!proxy->instantiate(resourceProvider)) { |
| 237 | return GrBackendTexture(); // invalid |
| 238 | } |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | GrTexture* texture = proxy->peekTexture(); |
| 242 | |
| 243 | if (texture) { |
| 244 | if (flushPendingGrContextIO) { |
Robert Phillips | 920d488 | 2019-03-04 15:16:44 -0500 | [diff] [blame] | 245 | direct->priv().prepareSurfaceForExternalIO(proxy.get()); |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 246 | } |
| 247 | if (origin) { |
| 248 | *origin = proxy->origin(); |
| 249 | } |
| 250 | return texture->getBackendTexture(); |
| 251 | } |
| 252 | return GrBackendTexture(); // invalid |
| 253 | } |
| 254 | |
| 255 | GrTexture* SkImage_GpuBase::onGetTexture() const { |
| 256 | GrTextureProxy* proxy = this->peekProxy(); |
Robert Phillips | 193c421 | 2019-03-04 12:18:53 -0500 | [diff] [blame] | 257 | if (proxy && proxy->isInstantiated()) { |
| 258 | return proxy->peekTexture(); |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 259 | } |
| 260 | |
Robert Phillips | 193c421 | 2019-03-04 12:18:53 -0500 | [diff] [blame] | 261 | auto direct = fContext->priv().asDirectContext(); |
| 262 | if (!direct) { |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 263 | // This image was created with a DDL context and cannot be instantiated. |
| 264 | return nullptr; |
| 265 | } |
| 266 | |
Robert Phillips | 193c421 | 2019-03-04 12:18:53 -0500 | [diff] [blame] | 267 | sk_sp<GrTextureProxy> proxyRef = this->asTextureProxyRef(); |
| 268 | SkASSERT(proxyRef && !proxyRef->isInstantiated()); |
| 269 | |
| 270 | if (!proxyRef->instantiate(direct->priv().resourceProvider())) { |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 271 | return nullptr; |
| 272 | } |
| 273 | |
Robert Phillips | 193c421 | 2019-03-04 12:18:53 -0500 | [diff] [blame] | 274 | return proxyRef->peekTexture(); |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 275 | } |
| 276 | |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 277 | bool SkImage_GpuBase::onIsValid(GrContext* context) const { |
| 278 | // The base class has already checked that context isn't abandoned (if it's not nullptr) |
Robert Phillips | 920d488 | 2019-03-04 15:16:44 -0500 | [diff] [blame] | 279 | if (fContext->priv().abandoned()) { |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 280 | return false; |
| 281 | } |
| 282 | |
Robert Phillips | 920d488 | 2019-03-04 15:16:44 -0500 | [diff] [blame] | 283 | if (context && !fContext->priv().matches(context)) { |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 284 | return false; |
| 285 | } |
| 286 | |
| 287 | return true; |
| 288 | } |
Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 289 | |
Jim Van Verth | 0e67194 | 2018-11-09 12:03:57 -0500 | [diff] [blame] | 290 | bool SkImage_GpuBase::MakeTempTextureProxies(GrContext* ctx, const GrBackendTexture yuvaTextures[], |
Jim Van Verth | 5327536 | 2018-11-09 15:42:35 -0500 | [diff] [blame] | 291 | int numTextures, const SkYUVAIndex yuvaIndices[4], |
| 292 | GrSurfaceOrigin imageOrigin, |
Jim Van Verth | 0e67194 | 2018-11-09 12:03:57 -0500 | [diff] [blame] | 293 | sk_sp<GrTextureProxy> tempTextureProxies[4]) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 294 | GrProxyProvider* proxyProvider = ctx->priv().proxyProvider(); |
Jim Van Verth | 0e67194 | 2018-11-09 12:03:57 -0500 | [diff] [blame] | 295 | |
| 296 | // We need to make a copy of the input backend textures because we need to preserve the result |
| 297 | // of validate_backend_texture. |
| 298 | GrBackendTexture yuvaTexturesCopy[4]; |
| 299 | for (int textureIndex = 0; textureIndex < numTextures; ++textureIndex) { |
| 300 | yuvaTexturesCopy[textureIndex] = yuvaTextures[textureIndex]; |
Brian Salomon | f391d0f | 2018-12-14 09:18:50 -0500 | [diff] [blame] | 301 | GrBackendFormat backendFormat = yuvaTexturesCopy[textureIndex].getBackendFormat(); |
| 302 | if (!backendFormat.isValid()) { |
| 303 | return false; |
| 304 | } |
| 305 | yuvaTexturesCopy[textureIndex].fConfig = |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 306 | ctx->priv().caps()->getYUVAConfigFromBackendFormat(backendFormat); |
Brian Salomon | f391d0f | 2018-12-14 09:18:50 -0500 | [diff] [blame] | 307 | if (yuvaTexturesCopy[textureIndex].fConfig == kUnknown_GrPixelConfig) { |
Jim Van Verth | 0e67194 | 2018-11-09 12:03:57 -0500 | [diff] [blame] | 308 | return false; |
| 309 | } |
| 310 | SkASSERT(yuvaTexturesCopy[textureIndex].isValid()); |
| 311 | |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 312 | tempTextureProxies[textureIndex] = proxyProvider->wrapBackendTexture( |
| 313 | yuvaTexturesCopy[textureIndex], imageOrigin, kBorrow_GrWrapOwnership, |
| 314 | GrWrapCacheable::kNo, kRead_GrIOType); |
Jim Van Verth | 0e67194 | 2018-11-09 12:03:57 -0500 | [diff] [blame] | 315 | if (!tempTextureProxies[textureIndex]) { |
| 316 | return false; |
| 317 | } |
Jim Van Verth | 5327536 | 2018-11-09 15:42:35 -0500 | [diff] [blame] | 318 | |
| 319 | // Check that each texture contains the channel data for the corresponding YUVA index |
| 320 | GrPixelConfig config = yuvaTexturesCopy[textureIndex].fConfig; |
| 321 | for (int yuvaIndex = 0; yuvaIndex < SkYUVAIndex::kIndexCount; ++yuvaIndex) { |
| 322 | if (yuvaIndices[yuvaIndex].fIndex == textureIndex) { |
| 323 | switch (yuvaIndices[yuvaIndex].fChannel) { |
| 324 | case SkColorChannel::kR: |
| 325 | if (kAlpha_8_as_Alpha_GrPixelConfig == config) { |
| 326 | return false; |
| 327 | } |
| 328 | break; |
| 329 | case SkColorChannel::kG: |
| 330 | case SkColorChannel::kB: |
| 331 | if (kAlpha_8_as_Alpha_GrPixelConfig == config || |
| 332 | kAlpha_8_as_Red_GrPixelConfig == config) { |
| 333 | return false; |
| 334 | } |
| 335 | break; |
| 336 | case SkColorChannel::kA: |
| 337 | default: |
| 338 | if (kRGB_888_GrPixelConfig == config) { |
| 339 | return false; |
| 340 | } |
| 341 | break; |
| 342 | } |
| 343 | } |
| 344 | } |
Jim Van Verth | 0e67194 | 2018-11-09 12:03:57 -0500 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | return true; |
| 348 | } |
| 349 | |
| 350 | bool SkImage_GpuBase::RenderYUVAToRGBA(GrContext* ctx, GrRenderTargetContext* renderTargetContext, |
| 351 | const SkRect& rect, SkYUVColorSpace yuvColorSpace, |
Brian Osman | e956049 | 2019-02-05 17:00:03 -0500 | [diff] [blame] | 352 | sk_sp<GrColorSpaceXform> colorSpaceXform, |
Jim Van Verth | 0e67194 | 2018-11-09 12:03:57 -0500 | [diff] [blame] | 353 | const sk_sp<GrTextureProxy> proxies[4], |
| 354 | const SkYUVAIndex yuvaIndices[4]) { |
| 355 | SkASSERT(renderTargetContext); |
| 356 | if (!renderTargetContext->asSurfaceProxy()) { |
| 357 | return false; |
| 358 | } |
| 359 | |
| 360 | GrPaint paint; |
| 361 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
| 362 | |
Brian Osman | e956049 | 2019-02-05 17:00:03 -0500 | [diff] [blame] | 363 | auto fp = GrYUVtoRGBEffect::Make(proxies, yuvaIndices, yuvColorSpace, |
| 364 | GrSamplerState::Filter::kNearest); |
| 365 | if (colorSpaceXform) { |
| 366 | fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(colorSpaceXform)); |
| 367 | } |
| 368 | paint.addColorFragmentProcessor(std::move(fp)); |
Jim Van Verth | 0e67194 | 2018-11-09 12:03:57 -0500 | [diff] [blame] | 369 | |
| 370 | renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect); |
Mike Reed | fbc887d | 2019-03-05 01:32:07 +0000 | [diff] [blame] | 371 | |
| 372 | // DDL TODO: in the promise image version we must not flush here |
| 373 | ctx->priv().flushSurfaceWrites(renderTargetContext->asSurfaceProxy()); |
| 374 | |
Jim Van Verth | 0e67194 | 2018-11-09 12:03:57 -0500 | [diff] [blame] | 375 | return true; |
| 376 | } |
| 377 | |
Brian Salomon | be5a093 | 2018-12-10 10:03:26 -0500 | [diff] [blame] | 378 | sk_sp<GrTextureProxy> SkImage_GpuBase::MakePromiseImageLazyProxy( |
| 379 | GrContext* context, int width, int height, GrSurfaceOrigin origin, GrPixelConfig config, |
| 380 | GrBackendFormat backendFormat, GrMipMapped mipMapped, |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 381 | PromiseImageTextureFulfillProc fulfillProc, |
| 382 | PromiseImageTextureReleaseProc releaseProc, |
| 383 | PromiseImageTextureDoneProc doneProc, |
Brian Salomon | 7d88f31 | 2019-02-28 10:03:03 -0500 | [diff] [blame] | 384 | PromiseImageTextureContext textureContext) { |
Brian Salomon | be5a093 | 2018-12-10 10:03:26 -0500 | [diff] [blame] | 385 | SkASSERT(context); |
| 386 | SkASSERT(width > 0 && height > 0); |
| 387 | SkASSERT(doneProc); |
Brian Salomon | f391d0f | 2018-12-14 09:18:50 -0500 | [diff] [blame] | 388 | SkASSERT(config != kUnknown_GrPixelConfig); |
Brian Salomon | be5a093 | 2018-12-10 10:03:26 -0500 | [diff] [blame] | 389 | |
| 390 | if (!fulfillProc || !releaseProc) { |
| 391 | doneProc(textureContext); |
| 392 | return nullptr; |
Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 393 | } |
| 394 | |
Brian Salomon | be5a093 | 2018-12-10 10:03:26 -0500 | [diff] [blame] | 395 | if (mipMapped == GrMipMapped::kYes && |
| 396 | GrTextureTypeHasRestrictedSampling(backendFormat.textureType())) { |
| 397 | // It is invalid to have a GL_TEXTURE_EXTERNAL or GL_TEXTURE_RECTANGLE and have mips as |
| 398 | // well. |
| 399 | doneProc(textureContext); |
| 400 | return nullptr; |
Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 401 | } |
Brian Salomon | be5a093 | 2018-12-10 10:03:26 -0500 | [diff] [blame] | 402 | |
| 403 | /** |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 404 | * This class is the lazy instantiation callback for promise images. It manages calling the |
| 405 | * client's Fulfill, Release, and Done procs. It attempts to reuse a GrTexture instance in |
Brian Salomon | b2c5dae | 2019-03-04 10:25:17 -0500 | [diff] [blame^] | 406 | * cases where the client provides the same SkPromiseImageTexture as Fulfill results for |
| 407 | * multiple SkImages. The created GrTexture is given a key based on a unique ID associated with |
| 408 | * the SkPromiseImageTexture. |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 409 | * |
Brian Salomon | b2c5dae | 2019-03-04 10:25:17 -0500 | [diff] [blame^] | 410 | * The GrTexutre idle proc mechanism is used to call the Release and Done procs. We use this |
| 411 | * instead of the GrSurface release proc because the GrTexture is cached and therefore may |
| 412 | * outlive the proxy into which this callback is installed. |
| 413 | * |
| 414 | * A key invalidation message is installed on the SkPromiseImageTexture so that the GrTexture |
| 415 | * is deleted once it can no longer be used to instantiate a proxy. |
Brian Salomon | be5a093 | 2018-12-10 10:03:26 -0500 | [diff] [blame] | 416 | */ |
| 417 | class PromiseLazyInstantiateCallback { |
| 418 | public: |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 419 | PromiseLazyInstantiateCallback(PromiseImageTextureFulfillProc fulfillProc, |
| 420 | PromiseImageTextureReleaseProc releaseProc, |
| 421 | PromiseImageTextureDoneProc doneProc, |
| 422 | PromiseImageTextureContext context, |
Brian Salomon | be5a093 | 2018-12-10 10:03:26 -0500 | [diff] [blame] | 423 | GrPixelConfig config) |
Brian Salomon | 7d88f31 | 2019-02-28 10:03:03 -0500 | [diff] [blame] | 424 | : fFulfillProc(fulfillProc), fConfig(config) { |
Brian Salomon | b2c5dae | 2019-03-04 10:25:17 -0500 | [diff] [blame^] | 425 | auto doneHelper = sk_make_sp<GrRefCntedCallback>(doneProc, context); |
| 426 | fIdleCallback = sk_make_sp<GrRefCntedCallback>(releaseProc, context); |
| 427 | fIdleCallback->addChild(std::move(doneHelper)); |
| 428 | } |
| 429 | PromiseLazyInstantiateCallback(PromiseLazyInstantiateCallback&&) = default; |
| 430 | PromiseLazyInstantiateCallback(const PromiseLazyInstantiateCallback&) { |
| 431 | // Because we get wrapped in std::function we must be copyable. But we should never |
| 432 | // be copied. |
| 433 | SkASSERT(false); |
| 434 | } |
| 435 | PromiseLazyInstantiateCallback& operator=(PromiseLazyInstantiateCallback&&) = default; |
| 436 | PromiseLazyInstantiateCallback& operator=(const PromiseLazyInstantiateCallback&) { |
| 437 | SkASSERT(false); |
| 438 | return *this; |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 439 | } |
Brian Salomon | 553610d | 2019-01-14 17:34:12 -0500 | [diff] [blame] | 440 | |
Brian Salomon | b2c5dae | 2019-03-04 10:25:17 -0500 | [diff] [blame^] | 441 | ~PromiseLazyInstantiateCallback() { |
| 442 | if (fIdleCallback) { |
| 443 | // We were never fulfilled. Pass false so done proc is still called. |
| 444 | fIdleCallback->abandon(); |
| 445 | } |
| 446 | } |
Brian Salomon | be5a093 | 2018-12-10 10:03:26 -0500 | [diff] [blame] | 447 | |
| 448 | sk_sp<GrSurface> operator()(GrResourceProvider* resourceProvider) { |
Brian Salomon | b2c5dae | 2019-03-04 10:25:17 -0500 | [diff] [blame^] | 449 | SkASSERT(fIdleCallback); |
| 450 | PromiseImageTextureContext textureContext = fIdleCallback->context(); |
| 451 | sk_sp<SkPromiseImageTexture> promiseTexture = fFulfillProc(textureContext); |
| 452 | // From here on out our contract is that the release proc must be called, even if |
| 453 | // the return from fulfill was invalid or we fail for some other reason. |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 454 | if (!promiseTexture) { |
Brian Salomon | b2c5dae | 2019-03-04 10:25:17 -0500 | [diff] [blame^] | 455 | // Make sure we explicitly reset this because our destructor assumes a non-null |
| 456 | // fIdleCallback means fulfill was never called. |
| 457 | fIdleCallback.reset(); |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 458 | return sk_sp<GrTexture>(); |
| 459 | } |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 460 | |
Brian Salomon | b2c5dae | 2019-03-04 10:25:17 -0500 | [diff] [blame^] | 461 | auto backendTexture = promiseTexture->backendTexture(); |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 462 | backendTexture.fConfig = fConfig; |
| 463 | if (!backendTexture.isValid()) { |
Brian Salomon | b2c5dae | 2019-03-04 10:25:17 -0500 | [diff] [blame^] | 464 | fIdleCallback.reset(); |
Brian Salomon | 426ba46 | 2019-01-10 16:33:06 +0000 | [diff] [blame] | 465 | return sk_sp<GrTexture>(); |
Brian Salomon | 559c617 | 2019-01-10 10:23:44 -0500 | [diff] [blame] | 466 | } |
| 467 | |
Brian Salomon | f55e8d5 | 2019-01-30 17:28:20 -0500 | [diff] [blame] | 468 | sk_sp<GrTexture> tex; |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 469 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
Brian Salomon | 7d88f31 | 2019-02-28 10:03:03 -0500 | [diff] [blame] | 470 | GrUniqueKey key; |
| 471 | GrUniqueKey::Builder builder(&key, kDomain, 2, "promise"); |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 472 | builder[0] = promiseTexture->uniqueID(); |
Brian Salomon | 1bf0ed8 | 2019-01-16 13:51:35 -0500 | [diff] [blame] | 473 | builder[1] = fConfig; |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 474 | builder.finish(); |
Brian Salomon | 0d60676 | 2019-01-25 09:58:38 -0500 | [diff] [blame] | 475 | // A texture with this key may already exist from a different instance of this lazy |
| 476 | // callback. This could happen if the client fulfills a promise image with a texture |
| 477 | // that was previously used to fulfill a different promise image. |
Brian Salomon | 7d88f31 | 2019-02-28 10:03:03 -0500 | [diff] [blame] | 478 | if (auto surf = resourceProvider->findByUniqueKey<GrSurface>(key)) { |
Brian Salomon | 0d60676 | 2019-01-25 09:58:38 -0500 | [diff] [blame] | 479 | tex = sk_ref_sp(surf->asTexture()); |
| 480 | SkASSERT(tex); |
| 481 | } else { |
| 482 | if ((tex = resourceProvider->wrapBackendTexture( |
| 483 | backendTexture, kBorrow_GrWrapOwnership, GrWrapCacheable::kYes, |
| 484 | kRead_GrIOType))) { |
Brian Salomon | 7d88f31 | 2019-02-28 10:03:03 -0500 | [diff] [blame] | 485 | tex->resourcePriv().setUniqueKey(key); |
Brian Salomon | 0d60676 | 2019-01-25 09:58:38 -0500 | [diff] [blame] | 486 | } else { |
Brian Salomon | b2c5dae | 2019-03-04 10:25:17 -0500 | [diff] [blame^] | 487 | fIdleCallback.reset(); |
Brian Salomon | 0d60676 | 2019-01-25 09:58:38 -0500 | [diff] [blame] | 488 | return sk_sp<GrTexture>(); |
| 489 | } |
| 490 | } |
Brian Salomon | b2c5dae | 2019-03-04 10:25:17 -0500 | [diff] [blame^] | 491 | tex->addIdleProc(std::move(fIdleCallback)); |
| 492 | promiseTexture->addKeyToInvalidate(tex->getContext()->priv().contextID(), key); |
Brian Salomon | be5a093 | 2018-12-10 10:03:26 -0500 | [diff] [blame] | 493 | return std::move(tex); |
| 494 | } |
| 495 | |
| 496 | private: |
Brian Salomon | b2c5dae | 2019-03-04 10:25:17 -0500 | [diff] [blame^] | 497 | sk_sp<GrRefCntedCallback> fIdleCallback; |
Brian Salomon | 553610d | 2019-01-14 17:34:12 -0500 | [diff] [blame] | 498 | PromiseImageTextureFulfillProc fFulfillProc; |
| 499 | GrPixelConfig fConfig; |
Brian Salomon | 7d88f31 | 2019-02-28 10:03:03 -0500 | [diff] [blame] | 500 | } callback(fulfillProc, releaseProc, doneProc, textureContext, config); |
Brian Salomon | be5a093 | 2018-12-10 10:03:26 -0500 | [diff] [blame] | 501 | |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 502 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
Brian Salomon | be5a093 | 2018-12-10 10:03:26 -0500 | [diff] [blame] | 503 | |
| 504 | GrSurfaceDesc desc; |
| 505 | desc.fWidth = width; |
| 506 | desc.fHeight = height; |
| 507 | desc.fConfig = config; |
| 508 | |
| 509 | // We pass kReadOnly here since we should treat content of the client's texture as immutable. |
| 510 | return proxyProvider->createLazyProxy(std::move(callback), backendFormat, desc, origin, |
| 511 | mipMapped, GrInternalSurfaceFlags::kReadOnly, |
| 512 | SkBackingFit::kExact, SkBudgeted::kNo, |
Brian Salomon | b2c5dae | 2019-03-04 10:25:17 -0500 | [diff] [blame^] | 513 | GrSurfaceProxy::LazyInstantiationType::kSingleUse); |
Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 514 | } |