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