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