| Jim Van Verth | f49262d | 2018-10-02 12:07:20 -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 | |
| 8 | #include <cstddef> |
| 9 | #include <cstring> |
| 10 | #include <type_traits> |
| 11 | |
| 12 | #include "GrClip.h" |
| 13 | #include "GrContext.h" |
| 14 | #include "GrContextPriv.h" |
| Jim Van Verth | 30e0d7f | 2018-11-02 13:36:42 -0400 | [diff] [blame] | 15 | #include "GrGpu.h" |
| Robert Phillips | 6603a17 | 2019-03-05 12:35:44 -0500 | [diff] [blame] | 16 | #include "GrRecordingContext.h" |
| 17 | #include "GrRecordingContextPriv.h" |
| Jim Van Verth | f49262d | 2018-10-02 12:07:20 -0400 | [diff] [blame] | 18 | #include "GrRenderTargetContext.h" |
| 19 | #include "GrTexture.h" |
| Jim Van Verth | 30e0d7f | 2018-11-02 13:36:42 -0400 | [diff] [blame] | 20 | #include "GrTextureProducer.h" |
| Jim Van Verth | c8429ad | 2018-11-20 11:12:37 -0500 | [diff] [blame] | 21 | #include "SkAutoPixmapStorage.h" |
| Jim Van Verth | 803a502 | 2018-11-05 15:55:53 -0500 | [diff] [blame] | 22 | #include "SkGr.h" |
| Jim Van Verth | f49262d | 2018-10-02 12:07:20 -0400 | [diff] [blame] | 23 | #include "SkImage_Gpu.h" |
| 24 | #include "SkImage_GpuYUVA.h" |
| Jim Van Verth | 30e0d7f | 2018-11-02 13:36:42 -0400 | [diff] [blame] | 25 | #include "SkMipMap.h" |
| Brian Salomon | be5a093 | 2018-12-10 10:03:26 -0500 | [diff] [blame] | 26 | #include "SkScopeExit.h" |
| Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 27 | #include "SkYUVASizeInfo.h" |
| Jim Van Verth | f49262d | 2018-10-02 12:07:20 -0400 | [diff] [blame] | 28 | #include "effects/GrYUVtoRGBEffect.h" |
| 29 | |
| Jim Van Verth | cea3902 | 2018-10-12 16:15:34 -0400 | [diff] [blame] | 30 | SkImage_GpuYUVA::SkImage_GpuYUVA(sk_sp<GrContext> context, int width, int height, uint32_t uniqueID, |
| Jim Van Verth | f49262d | 2018-10-02 12:07:20 -0400 | [diff] [blame] | 31 | SkYUVColorSpace colorSpace, sk_sp<GrTextureProxy> proxies[], |
| Jim Van Verth | 0e67194 | 2018-11-09 12:03:57 -0500 | [diff] [blame] | 32 | int numProxies, const SkYUVAIndex yuvaIndices[4], |
| Brian Salomon | f05e6d3 | 2018-12-20 08:41:41 -0500 | [diff] [blame] | 33 | GrSurfaceOrigin origin, sk_sp<SkColorSpace> imageColorSpace) |
| Jim Van Verth | cea3902 | 2018-10-12 16:15:34 -0400 | [diff] [blame] | 34 | : INHERITED(std::move(context), width, height, uniqueID, |
| Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 35 | // If an alpha channel is present we always switch to kPremul. This is because, |
| 36 | // although the planar data is always un-premul, the final interleaved RGB image |
| 37 | // is/would-be premul. |
| Brian Salomon | f05e6d3 | 2018-12-20 08:41:41 -0500 | [diff] [blame] | 38 | GetAlphaTypeFromYUVAIndices(yuvaIndices), imageColorSpace) |
| Jim Van Verth | 0e67194 | 2018-11-09 12:03:57 -0500 | [diff] [blame] | 39 | , fNumProxies(numProxies) |
| Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 40 | , fYUVColorSpace(colorSpace) |
| 41 | , fOrigin(origin) { |
| Jim Van Verth | 0e67194 | 2018-11-09 12:03:57 -0500 | [diff] [blame] | 42 | // The caller should have done this work, just verifying |
| 43 | SkDEBUGCODE(int textureCount;) |
| 44 | SkASSERT(SkYUVAIndex::AreValidIndices(yuvaIndices, &textureCount)); |
| 45 | SkASSERT(textureCount == fNumProxies); |
| 46 | |
| 47 | for (int i = 0; i < numProxies; ++i) { |
| Jim Van Verth | f49262d | 2018-10-02 12:07:20 -0400 | [diff] [blame] | 48 | fProxies[i] = std::move(proxies[i]); |
| 49 | } |
| 50 | memcpy(fYUVAIndices, yuvaIndices, 4*sizeof(SkYUVAIndex)); |
| Jim Van Verth | f49262d | 2018-10-02 12:07:20 -0400 | [diff] [blame] | 51 | } |
| 52 | |
| Jim Van Verth | 3e4c2f3 | 2019-01-11 13:32:45 -0500 | [diff] [blame] | 53 | // For onMakeColorSpace() |
| 54 | SkImage_GpuYUVA::SkImage_GpuYUVA(const SkImage_GpuYUVA* image, sk_sp<SkColorSpace> targetCS) |
| 55 | : INHERITED(image->fContext, image->width(), image->height(), kNeedNewImageUniqueID, |
| 56 | // If an alpha channel is present we always switch to kPremul. This is because, |
| 57 | // although the planar data is always un-premul, the final interleaved RGB image |
| 58 | // is/would-be premul. |
| 59 | GetAlphaTypeFromYUVAIndices(image->fYUVAIndices), image->fColorSpace) |
| 60 | , fNumProxies(image->fNumProxies) |
| 61 | , fYUVColorSpace(image->fYUVColorSpace) |
| 62 | , fOrigin(image->fOrigin) |
| 63 | , fTargetColorSpace(targetCS) { |
| 64 | // The caller should have done this work, just verifying |
| 65 | SkDEBUGCODE(int textureCount;) |
| 66 | SkASSERT(SkYUVAIndex::AreValidIndices(image->fYUVAIndices, &textureCount)); |
| 67 | SkASSERT(textureCount == fNumProxies); |
| 68 | |
| 69 | for (int i = 0; i < fNumProxies; ++i) { |
| 70 | fProxies[i] = image->fProxies[i]; // we ref in this case, not move |
| 71 | } |
| 72 | memcpy(fYUVAIndices, image->fYUVAIndices, 4 * sizeof(SkYUVAIndex)); |
| 73 | } |
| 74 | |
| Jim Van Verth | f49262d | 2018-10-02 12:07:20 -0400 | [diff] [blame] | 75 | SkImage_GpuYUVA::~SkImage_GpuYUVA() {} |
| 76 | |
| 77 | SkImageInfo SkImage_GpuYUVA::onImageInfo() const { |
| 78 | // Note: this is the imageInfo for the flattened image, not the YUV planes |
| 79 | return SkImageInfo::Make(this->width(), this->height(), kRGBA_8888_SkColorType, |
| Brian Osman | e956049 | 2019-02-05 17:00:03 -0500 | [diff] [blame] | 80 | fAlphaType, fTargetColorSpace ? fTargetColorSpace : fColorSpace); |
| Jim Van Verth | f49262d | 2018-10-02 12:07:20 -0400 | [diff] [blame] | 81 | } |
| 82 | |
| Robert Phillips | 8defcc1 | 2019-03-05 15:58:59 -0500 | [diff] [blame] | 83 | bool SkImage_GpuYUVA::setupMipmapsForPlanes(GrRecordingContext* context) const { |
| 84 | if (!context || !fContext->priv().matches(context)) { |
| 85 | return false; |
| 86 | } |
| 87 | |
| Jim Van Verth | 0e67194 | 2018-11-09 12:03:57 -0500 | [diff] [blame] | 88 | for (int i = 0; i < fNumProxies; ++i) { |
| Jim Van Verth | 30e0d7f | 2018-11-02 13:36:42 -0400 | [diff] [blame] | 89 | GrTextureProducer::CopyParams copyParams; |
| 90 | int mipCount = SkMipMap::ComputeLevelCount(fProxies[i]->width(), fProxies[i]->height()); |
| Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 91 | if (mipCount && GrGpu::IsACopyNeededForMips(fContext->priv().caps(), |
| Jim Van Verth | 30e0d7f | 2018-11-02 13:36:42 -0400 | [diff] [blame] | 92 | fProxies[i].get(), |
| 93 | GrSamplerState::Filter::kMipMap, |
| 94 | ©Params)) { |
| Robert Phillips | 8defcc1 | 2019-03-05 15:58:59 -0500 | [diff] [blame] | 95 | auto mippedProxy = GrCopyBaseMipMapToTextureProxy(context, fProxies[i].get()); |
| Jim Van Verth | f542cab | 2018-11-07 12:08:21 -0500 | [diff] [blame] | 96 | if (!mippedProxy) { |
| 97 | return false; |
| 98 | } |
| 99 | fProxies[i] = mippedProxy; |
| Jim Van Verth | 30e0d7f | 2018-11-02 13:36:42 -0400 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | return true; |
| 103 | } |
| 104 | |
| Jim Van Verth | f49262d | 2018-10-02 12:07:20 -0400 | [diff] [blame] | 105 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
| Jim Van Verth | f49262d | 2018-10-02 12:07:20 -0400 | [diff] [blame] | 106 | |
| Robert Phillips | 193c421 | 2019-03-04 12:18:53 -0500 | [diff] [blame] | 107 | GrTextureProxy* SkImage_GpuYUVA::peekProxy() const { |
| 108 | return fRGBProxy.get(); |
| 109 | } |
| 110 | |
| Robert Phillips | 6603a17 | 2019-03-05 12:35:44 -0500 | [diff] [blame] | 111 | sk_sp<GrTextureProxy> SkImage_GpuYUVA::asTextureProxyRef(GrRecordingContext* context) const { |
| 112 | if (fRGBProxy) { |
| 113 | return fRGBProxy; |
| Jim Van Verth | f49262d | 2018-10-02 12:07:20 -0400 | [diff] [blame] | 114 | } |
| 115 | |
| Robert Phillips | 6603a17 | 2019-03-05 12:35:44 -0500 | [diff] [blame] | 116 | if (!context || !fContext->priv().matches(context)) { |
| 117 | return nullptr; |
| 118 | } |
| 119 | |
| 120 | const GrBackendFormat format = |
| 121 | fContext->priv().caps()->getBackendFormatFromColorType(kRGBA_8888_SkColorType); |
| 122 | |
| 123 | // Needs to create a render target in order to draw to it for the yuv->rgb conversion. |
| 124 | sk_sp<GrRenderTargetContext> renderTargetContext( |
| 125 | context->priv().makeDeferredRenderTargetContext( |
| 126 | format, SkBackingFit::kExact, this->width(), this->height(), |
| 127 | kRGBA_8888_GrPixelConfig, fColorSpace, 1, GrMipMapped::kNo, fOrigin)); |
| 128 | if (!renderTargetContext) { |
| 129 | return nullptr; |
| 130 | } |
| 131 | |
| 132 | auto colorSpaceXform = GrColorSpaceXform::Make(fColorSpace.get(), fAlphaType, |
| 133 | fTargetColorSpace.get(), fAlphaType); |
| 134 | const SkRect rect = SkRect::MakeIWH(this->width(), this->height()); |
| 135 | if (!RenderYUVAToRGBA(fContext.get(), renderTargetContext.get(), rect, fYUVColorSpace, |
| 136 | std::move(colorSpaceXform), fProxies, fYUVAIndices)) { |
| 137 | return nullptr; |
| 138 | } |
| 139 | |
| 140 | fRGBProxy = renderTargetContext->asTextureProxyRef(); |
| Jim Van Verth | f49262d | 2018-10-02 12:07:20 -0400 | [diff] [blame] | 141 | return fRGBProxy; |
| 142 | } |
| 143 | |
| Robert Phillips | 6603a17 | 2019-03-05 12:35:44 -0500 | [diff] [blame] | 144 | sk_sp<GrTextureProxy> SkImage_GpuYUVA::asMippedTextureProxyRef(GrRecordingContext* context) const { |
| 145 | if (!context || !fContext->priv().matches(context)) { |
| 146 | return nullptr; |
| 147 | } |
| 148 | |
| Jim Van Verth | 803a502 | 2018-11-05 15:55:53 -0500 | [diff] [blame] | 149 | // if invalid or already has miplevels |
| Robert Phillips | 6603a17 | 2019-03-05 12:35:44 -0500 | [diff] [blame] | 150 | auto proxy = this->asTextureProxyRef(context); |
| Jim Van Verth | 803a502 | 2018-11-05 15:55:53 -0500 | [diff] [blame] | 151 | if (!proxy || GrMipMapped::kYes == fRGBProxy->mipMapped()) { |
| 152 | return proxy; |
| 153 | } |
| 154 | |
| 155 | // need to generate mips for the proxy |
| Robert Phillips | 8defcc1 | 2019-03-05 15:58:59 -0500 | [diff] [blame] | 156 | if (auto mippedProxy = GrCopyBaseMipMapToTextureProxy(context, proxy.get())) { |
| Jim Van Verth | 803a502 | 2018-11-05 15:55:53 -0500 | [diff] [blame] | 157 | fRGBProxy = mippedProxy; |
| 158 | return mippedProxy; |
| 159 | } |
| 160 | |
| 161 | // failed to generate mips |
| 162 | return nullptr; |
| 163 | } |
| 164 | |
| Jim Van Verth | 9bf8120 | 2018-10-30 15:53:36 -0400 | [diff] [blame] | 165 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
| 166 | |
| Robert Phillips | 6603a17 | 2019-03-05 12:35:44 -0500 | [diff] [blame] | 167 | sk_sp<SkImage> SkImage_GpuYUVA::onMakeColorTypeAndColorSpace(GrRecordingContext*, |
| 168 | SkColorType, |
| Brian Osman | f48c996 | 2019-01-14 11:15:50 -0500 | [diff] [blame] | 169 | sk_sp<SkColorSpace> targetCS) const { |
| 170 | // We explicitly ignore color type changes, for now. |
| 171 | |
| Jim Van Verth | 3e4c2f3 | 2019-01-11 13:32:45 -0500 | [diff] [blame] | 172 | // we may need a mutex here but for now we expect usage to be in a single thread |
| 173 | if (fOnMakeColorSpaceTarget && |
| Brian Osman | f48c996 | 2019-01-14 11:15:50 -0500 | [diff] [blame] | 174 | SkColorSpace::Equals(targetCS.get(), fOnMakeColorSpaceTarget.get())) { |
| Jim Van Verth | 3e4c2f3 | 2019-01-11 13:32:45 -0500 | [diff] [blame] | 175 | return fOnMakeColorSpaceResult; |
| 176 | } |
| Brian Osman | f48c996 | 2019-01-14 11:15:50 -0500 | [diff] [blame] | 177 | sk_sp<SkImage> result = sk_sp<SkImage>(new SkImage_GpuYUVA(this, targetCS)); |
| Jim Van Verth | 3e4c2f3 | 2019-01-11 13:32:45 -0500 | [diff] [blame] | 178 | if (result) { |
| Brian Osman | f48c996 | 2019-01-14 11:15:50 -0500 | [diff] [blame] | 179 | fOnMakeColorSpaceTarget = targetCS; |
| Jim Van Verth | 3e4c2f3 | 2019-01-11 13:32:45 -0500 | [diff] [blame] | 180 | fOnMakeColorSpaceResult = result; |
| 181 | } |
| 182 | return result; |
| 183 | } |
| 184 | |
| 185 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
| 186 | |
| Jim Van Verth | 9bf8120 | 2018-10-30 15:53:36 -0400 | [diff] [blame] | 187 | sk_sp<SkImage> SkImage::MakeFromYUVATextures(GrContext* ctx, |
| 188 | SkYUVColorSpace colorSpace, |
| 189 | const GrBackendTexture yuvaTextures[], |
| 190 | const SkYUVAIndex yuvaIndices[4], |
| 191 | SkISize imageSize, |
| 192 | GrSurfaceOrigin imageOrigin, |
| 193 | sk_sp<SkColorSpace> imageColorSpace) { |
| Jim Van Verth | 0e67194 | 2018-11-09 12:03:57 -0500 | [diff] [blame] | 194 | int numTextures; |
| 195 | if (!SkYUVAIndex::AreValidIndices(yuvaIndices, &numTextures)) { |
| 196 | return nullptr; |
| Jim Van Verth | 9bf8120 | 2018-10-30 15:53:36 -0400 | [diff] [blame] | 197 | } |
| 198 | |
| Jim Van Verth | 0e67194 | 2018-11-09 12:03:57 -0500 | [diff] [blame] | 199 | sk_sp<GrTextureProxy> tempTextureProxies[4]; |
| Jim Van Verth | 5327536 | 2018-11-09 15:42:35 -0500 | [diff] [blame] | 200 | if (!SkImage_GpuBase::MakeTempTextureProxies(ctx, yuvaTextures, numTextures, yuvaIndices, |
| 201 | imageOrigin, tempTextureProxies)) { |
| Jim Van Verth | 0e67194 | 2018-11-09 12:03:57 -0500 | [diff] [blame] | 202 | return nullptr; |
| Jim Van Verth | 9bf8120 | 2018-10-30 15:53:36 -0400 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | return sk_make_sp<SkImage_GpuYUVA>(sk_ref_sp(ctx), imageSize.width(), imageSize.height(), |
| 206 | kNeedNewImageUniqueID, colorSpace, tempTextureProxies, |
| Brian Salomon | f05e6d3 | 2018-12-20 08:41:41 -0500 | [diff] [blame] | 207 | numTextures, yuvaIndices, imageOrigin, imageColorSpace); |
| Jim Van Verth | 9bf8120 | 2018-10-30 15:53:36 -0400 | [diff] [blame] | 208 | } |
| Jim Van Verth | c8429ad | 2018-11-20 11:12:37 -0500 | [diff] [blame] | 209 | |
| 210 | sk_sp<SkImage> SkImage::MakeFromYUVAPixmaps( |
| 211 | GrContext* context, SkYUVColorSpace yuvColorSpace, const SkPixmap yuvaPixmaps[], |
| 212 | const SkYUVAIndex yuvaIndices[4], SkISize imageSize, GrSurfaceOrigin imageOrigin, |
| 213 | bool buildMips, bool limitToMaxTextureSize, sk_sp<SkColorSpace> imageColorSpace) { |
| 214 | int numPixmaps; |
| 215 | if (!SkYUVAIndex::AreValidIndices(yuvaIndices, &numPixmaps)) { |
| 216 | return nullptr; |
| 217 | } |
| 218 | |
| 219 | // Make proxies |
| Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 220 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
| Jim Van Verth | c8429ad | 2018-11-20 11:12:37 -0500 | [diff] [blame] | 221 | sk_sp<GrTextureProxy> tempTextureProxies[4]; |
| 222 | for (int i = 0; i < numPixmaps; ++i) { |
| 223 | const SkPixmap* pixmap = &yuvaPixmaps[i]; |
| 224 | SkAutoPixmapStorage resized; |
| Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 225 | int maxTextureSize = context->priv().caps()->maxTextureSize(); |
| Jim Van Verth | c8429ad | 2018-11-20 11:12:37 -0500 | [diff] [blame] | 226 | int maxDim = SkTMax(yuvaPixmaps[i].width(), yuvaPixmaps[i].height()); |
| 227 | if (limitToMaxTextureSize && maxDim > maxTextureSize) { |
| 228 | float scale = static_cast<float>(maxTextureSize) / maxDim; |
| 229 | int newWidth = SkTMin(static_cast<int>(yuvaPixmaps[i].width() * scale), |
| 230 | maxTextureSize); |
| 231 | int newHeight = SkTMin(static_cast<int>(yuvaPixmaps[i].height() * scale), |
| 232 | maxTextureSize); |
| 233 | SkImageInfo info = yuvaPixmaps[i].info().makeWH(newWidth, newHeight); |
| 234 | if (!resized.tryAlloc(info) || |
| 235 | !yuvaPixmaps[i].scalePixels(resized, kLow_SkFilterQuality)) { |
| 236 | return nullptr; |
| 237 | } |
| 238 | pixmap = &resized; |
| 239 | } |
| 240 | // Turn the pixmap into a GrTextureProxy |
| 241 | if (buildMips) { |
| 242 | SkBitmap bmp; |
| 243 | bmp.installPixels(*pixmap); |
| 244 | tempTextureProxies[i] = proxyProvider->createMipMapProxyFromBitmap(bmp); |
| Jim Van Verth | bd975d4 | 2018-12-06 15:16:14 +0000 | [diff] [blame] | 245 | } |
| 246 | if (!tempTextureProxies[i]) { |
| Jim Van Verth | c8429ad | 2018-11-20 11:12:37 -0500 | [diff] [blame] | 247 | if (SkImageInfoIsValid(pixmap->info())) { |
| 248 | ATRACE_ANDROID_FRAMEWORK("Upload Texture [%ux%u]", |
| 249 | pixmap->width(), pixmap->height()); |
| 250 | // We don't need a release proc on the data in pixmap since we know we are in a |
| 251 | // GrContext that has a resource provider. Thus the createTextureProxy call will |
| 252 | // immediately upload the data. |
| 253 | sk_sp<SkImage> image = SkImage::MakeFromRaster(*pixmap, nullptr, nullptr); |
| 254 | tempTextureProxies[i] = |
| 255 | proxyProvider->createTextureProxy(std::move(image), kNone_GrSurfaceFlags, 1, |
| 256 | SkBudgeted::kYes, SkBackingFit::kExact); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | if (!tempTextureProxies[i]) { |
| 261 | return nullptr; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | return sk_make_sp<SkImage_GpuYUVA>(sk_ref_sp(context), imageSize.width(), imageSize.height(), |
| 266 | kNeedNewImageUniqueID, yuvColorSpace, tempTextureProxies, |
| Brian Salomon | f05e6d3 | 2018-12-20 08:41:41 -0500 | [diff] [blame] | 267 | numPixmaps, yuvaIndices, imageOrigin, imageColorSpace); |
| Jim Van Verth | c8429ad | 2018-11-20 11:12:37 -0500 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | |
| Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 271 | ///////////////////////////////////////////////////////////////////////////////////////////////// |
| Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 272 | sk_sp<SkImage> SkImage_GpuYUVA::MakePromiseYUVATexture( |
| 273 | GrContext* context, |
| 274 | SkYUVColorSpace yuvColorSpace, |
| 275 | const GrBackendFormat yuvaFormats[], |
| 276 | const SkISize yuvaSizes[], |
| 277 | const SkYUVAIndex yuvaIndices[4], |
| 278 | int imageWidth, |
| 279 | int imageHeight, |
| 280 | GrSurfaceOrigin imageOrigin, |
| 281 | sk_sp<SkColorSpace> imageColorSpace, |
| 282 | PromiseImageTextureFulfillProc textureFulfillProc, |
| 283 | PromiseImageTextureReleaseProc textureReleaseProc, |
| 284 | PromiseImageTextureDoneProc promiseDoneProc, |
| Brian Salomon | d716d44 | 2019-03-07 15:23:30 +0000 | [diff] [blame] | 285 | PromiseImageTextureContext textureContexts[]) { |
| Jim Van Verth | f00b162 | 2018-10-10 13:03:23 -0400 | [diff] [blame] | 286 | int numTextures; |
| 287 | bool valid = SkYUVAIndex::AreValidIndices(yuvaIndices, &numTextures); |
| 288 | |
| Brian Salomon | be5a093 | 2018-12-10 10:03:26 -0500 | [diff] [blame] | 289 | // The contract here is that if 'promiseDoneProc' is passed in it should always be called, |
| 290 | // even if creation of the SkImage fails. Once we call MakePromiseImageLazyProxy it takes |
| 291 | // responsibility for calling the done proc. |
| 292 | if (!promiseDoneProc) { |
| 293 | return nullptr; |
| Jim Van Verth | f00b162 | 2018-10-10 13:03:23 -0400 | [diff] [blame] | 294 | } |
| Brian Salomon | be5a093 | 2018-12-10 10:03:26 -0500 | [diff] [blame] | 295 | int proxiesCreated = 0; |
| 296 | SkScopeExit callDone([promiseDoneProc, textureContexts, numTextures, &proxiesCreated]() { |
| 297 | for (int i = proxiesCreated; i < numTextures; ++i) { |
| 298 | promiseDoneProc(textureContexts[i]); |
| 299 | } |
| 300 | }); |
| Jim Van Verth | f00b162 | 2018-10-10 13:03:23 -0400 | [diff] [blame] | 301 | |
| 302 | if (!valid) { |
| 303 | return nullptr; |
| 304 | } |
| Robert Phillips | ef85d19 | 2018-10-09 11:24:09 -0400 | [diff] [blame] | 305 | |
| Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 306 | if (!context) { |
| 307 | return nullptr; |
| 308 | } |
| 309 | |
| Greg Kaiser | 9a2169e | 2019-02-10 17:29:46 -0800 | [diff] [blame] | 310 | if (imageWidth <= 0 || imageHeight <= 0) { |
| Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 311 | return nullptr; |
| 312 | } |
| 313 | |
| Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 314 | SkAlphaType at = (-1 != yuvaIndices[SkYUVAIndex::kA_Index].fIndex) ? kPremul_SkAlphaType |
| 315 | : kOpaque_SkAlphaType; |
| Jim Van Verth | cea3902 | 2018-10-12 16:15:34 -0400 | [diff] [blame] | 316 | SkImageInfo info = SkImageInfo::Make(imageWidth, imageHeight, kRGBA_8888_SkColorType, |
| Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 317 | at, imageColorSpace); |
| 318 | if (!SkImageInfoIsValid(info)) { |
| 319 | return nullptr; |
| 320 | } |
| 321 | |
| Jim Van Verth | f9f0735 | 2018-10-24 10:32:20 -0400 | [diff] [blame] | 322 | // verify sizes with expected texture count |
| Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 323 | for (int i = 0; i < numTextures; ++i) { |
| Jim Van Verth | f9f0735 | 2018-10-24 10:32:20 -0400 | [diff] [blame] | 324 | if (yuvaSizes[i].isEmpty()) { |
| Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 325 | return nullptr; |
| Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 326 | } |
| 327 | } |
| Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 328 | for (int i = numTextures; i < SkYUVASizeInfo::kMaxCount; ++i) { |
| Jim Van Verth | f9f0735 | 2018-10-24 10:32:20 -0400 | [diff] [blame] | 329 | if (!yuvaSizes[i].isEmpty()) { |
| Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 330 | return nullptr; |
| 331 | } |
| Jim Van Verth | f99a674 | 2018-10-18 16:13:18 +0000 | [diff] [blame] | 332 | } |
| Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 333 | |
| 334 | // Get lazy proxies |
| Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 335 | sk_sp<GrTextureProxy> proxies[4]; |
| Jim Van Verth | f00b162 | 2018-10-10 13:03:23 -0400 | [diff] [blame] | 336 | for (int texIdx = 0; texIdx < numTextures; ++texIdx) { |
| Brian Salomon | f391d0f | 2018-12-14 09:18:50 -0500 | [diff] [blame] | 337 | GrPixelConfig config = |
| Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 338 | context->priv().caps()->getYUVAConfigFromBackendFormat(yuvaFormats[texIdx]); |
| Brian Salomon | f391d0f | 2018-12-14 09:18:50 -0500 | [diff] [blame] | 339 | if (config == kUnknown_GrPixelConfig) { |
| Jim Van Verth | f00b162 | 2018-10-10 13:03:23 -0400 | [diff] [blame] | 340 | return nullptr; |
| 341 | } |
| Brian Salomon | be5a093 | 2018-12-10 10:03:26 -0500 | [diff] [blame] | 342 | proxies[texIdx] = MakePromiseImageLazyProxy( |
| 343 | context, yuvaSizes[texIdx].width(), yuvaSizes[texIdx].height(), imageOrigin, config, |
| 344 | yuvaFormats[texIdx], GrMipMapped::kNo, textureFulfillProc, textureReleaseProc, |
| Brian Salomon | d716d44 | 2019-03-07 15:23:30 +0000 | [diff] [blame] | 345 | promiseDoneProc, textureContexts[texIdx]); |
| Brian Salomon | be5a093 | 2018-12-10 10:03:26 -0500 | [diff] [blame] | 346 | ++proxiesCreated; |
| Jim Van Verth | f00b162 | 2018-10-10 13:03:23 -0400 | [diff] [blame] | 347 | if (!proxies[texIdx]) { |
| 348 | return nullptr; |
| Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 349 | } |
| 350 | } |
| 351 | |
| Jim Van Verth | cea3902 | 2018-10-12 16:15:34 -0400 | [diff] [blame] | 352 | return sk_make_sp<SkImage_GpuYUVA>(sk_ref_sp(context), imageWidth, imageHeight, |
| Jim Van Verth | 0e67194 | 2018-11-09 12:03:57 -0500 | [diff] [blame] | 353 | kNeedNewImageUniqueID, yuvColorSpace, proxies, numTextures, |
| Brian Salomon | f05e6d3 | 2018-12-20 08:41:41 -0500 | [diff] [blame] | 354 | yuvaIndices, imageOrigin, std::move(imageColorSpace)); |
| Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 355 | } |