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