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