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