| 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 | |
| 143 | const GrBackendFormat format = |
| Brian Salomon | 5ad6fd3 | 2019-03-21 15:30:08 -0400 | [diff] [blame] | 144 | fContext->priv().caps()->getBackendFormatFromColorType(kAssumedColorType); |
| Robert Phillips | 6603a17 | 2019-03-05 12:35:44 -0500 | [diff] [blame] | 145 | |
| 146 | // Needs to create a render target in order to draw to it for the yuv->rgb conversion. |
| 147 | sk_sp<GrRenderTargetContext> renderTargetContext( |
| Brian Salomon | 5ad6fd3 | 2019-03-21 15:30:08 -0400 | [diff] [blame] | 148 | context->priv().makeDeferredRenderTargetContext( |
| 149 | format, SkBackingFit::kExact, this->width(), this->height(), |
| 150 | kRGBA_8888_GrPixelConfig, this->refColorSpace(), 1, GrMipMapped::kNo, fOrigin)); |
| Robert Phillips | 6603a17 | 2019-03-05 12:35:44 -0500 | [diff] [blame] | 151 | if (!renderTargetContext) { |
| 152 | return nullptr; |
| 153 | } |
| 154 | |
| Brian Salomon | 5ad6fd3 | 2019-03-21 15:30:08 -0400 | [diff] [blame] | 155 | sk_sp<GrColorSpaceXform> colorSpaceXform; |
| 156 | if (fFromColorSpace) { |
| 157 | colorSpaceXform = GrColorSpaceXform::Make(fFromColorSpace.get(), this->alphaType(), |
| 158 | this->colorSpace(), this->alphaType()); |
| 159 | } |
| Robert Phillips | 6603a17 | 2019-03-05 12:35:44 -0500 | [diff] [blame] | 160 | const SkRect rect = SkRect::MakeIWH(this->width(), this->height()); |
| 161 | if (!RenderYUVAToRGBA(fContext.get(), renderTargetContext.get(), rect, fYUVColorSpace, |
| 162 | std::move(colorSpaceXform), fProxies, fYUVAIndices)) { |
| 163 | return nullptr; |
| 164 | } |
| 165 | |
| 166 | fRGBProxy = renderTargetContext->asTextureProxyRef(); |
| Brian Salomon | ad8efda | 2019-05-10 09:22:49 -0400 | [diff] [blame] | 167 | for (auto& p : fProxies) { |
| 168 | p.reset(); |
| 169 | } |
| Jim Van Verth | f49262d | 2018-10-02 12:07:20 -0400 | [diff] [blame] | 170 | return fRGBProxy; |
| 171 | } |
| 172 | |
| Robert Phillips | 6603a17 | 2019-03-05 12:35:44 -0500 | [diff] [blame] | 173 | sk_sp<GrTextureProxy> SkImage_GpuYUVA::asMippedTextureProxyRef(GrRecordingContext* context) const { |
| 174 | if (!context || !fContext->priv().matches(context)) { |
| 175 | return nullptr; |
| 176 | } |
| 177 | |
| Jim Van Verth | 803a502 | 2018-11-05 15:55:53 -0500 | [diff] [blame] | 178 | // if invalid or already has miplevels |
| Robert Phillips | 6603a17 | 2019-03-05 12:35:44 -0500 | [diff] [blame] | 179 | auto proxy = this->asTextureProxyRef(context); |
| Jim Van Verth | 803a502 | 2018-11-05 15:55:53 -0500 | [diff] [blame] | 180 | if (!proxy || GrMipMapped::kYes == fRGBProxy->mipMapped()) { |
| 181 | return proxy; |
| 182 | } |
| 183 | |
| 184 | // need to generate mips for the proxy |
| Robert Phillips | 8defcc1 | 2019-03-05 15:58:59 -0500 | [diff] [blame] | 185 | if (auto mippedProxy = GrCopyBaseMipMapToTextureProxy(context, proxy.get())) { |
| Jim Van Verth | 803a502 | 2018-11-05 15:55:53 -0500 | [diff] [blame] | 186 | fRGBProxy = mippedProxy; |
| 187 | return mippedProxy; |
| 188 | } |
| 189 | |
| 190 | // failed to generate mips |
| 191 | return nullptr; |
| 192 | } |
| 193 | |
| Jim Van Verth | 9bf8120 | 2018-10-30 15:53:36 -0400 | [diff] [blame] | 194 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
| 195 | |
| Robert Phillips | 6603a17 | 2019-03-05 12:35:44 -0500 | [diff] [blame] | 196 | sk_sp<SkImage> SkImage_GpuYUVA::onMakeColorTypeAndColorSpace(GrRecordingContext*, |
| 197 | SkColorType, |
| Brian Osman | f48c996 | 2019-01-14 11:15:50 -0500 | [diff] [blame] | 198 | sk_sp<SkColorSpace> targetCS) const { |
| 199 | // We explicitly ignore color type changes, for now. |
| 200 | |
| Jim Van Verth | 3e4c2f3 | 2019-01-11 13:32:45 -0500 | [diff] [blame] | 201 | // we may need a mutex here but for now we expect usage to be in a single thread |
| 202 | if (fOnMakeColorSpaceTarget && |
| Brian Osman | f48c996 | 2019-01-14 11:15:50 -0500 | [diff] [blame] | 203 | SkColorSpace::Equals(targetCS.get(), fOnMakeColorSpaceTarget.get())) { |
| Jim Van Verth | 3e4c2f3 | 2019-01-11 13:32:45 -0500 | [diff] [blame] | 204 | return fOnMakeColorSpaceResult; |
| 205 | } |
| Brian Osman | f48c996 | 2019-01-14 11:15:50 -0500 | [diff] [blame] | 206 | 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] | 207 | if (result) { |
| Brian Osman | f48c996 | 2019-01-14 11:15:50 -0500 | [diff] [blame] | 208 | fOnMakeColorSpaceTarget = targetCS; |
| Jim Van Verth | 3e4c2f3 | 2019-01-11 13:32:45 -0500 | [diff] [blame] | 209 | fOnMakeColorSpaceResult = result; |
| 210 | } |
| 211 | return result; |
| 212 | } |
| 213 | |
| 214 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
| 215 | |
| Jim Van Verth | 9bf8120 | 2018-10-30 15:53:36 -0400 | [diff] [blame] | 216 | sk_sp<SkImage> SkImage::MakeFromYUVATextures(GrContext* ctx, |
| 217 | SkYUVColorSpace colorSpace, |
| 218 | const GrBackendTexture yuvaTextures[], |
| 219 | const SkYUVAIndex yuvaIndices[4], |
| 220 | SkISize imageSize, |
| 221 | GrSurfaceOrigin imageOrigin, |
| 222 | sk_sp<SkColorSpace> imageColorSpace) { |
| Jim Van Verth | 0e67194 | 2018-11-09 12:03:57 -0500 | [diff] [blame] | 223 | int numTextures; |
| 224 | if (!SkYUVAIndex::AreValidIndices(yuvaIndices, &numTextures)) { |
| 225 | return nullptr; |
| Jim Van Verth | 9bf8120 | 2018-10-30 15:53:36 -0400 | [diff] [blame] | 226 | } |
| 227 | |
| Jim Van Verth | 0e67194 | 2018-11-09 12:03:57 -0500 | [diff] [blame] | 228 | sk_sp<GrTextureProxy> tempTextureProxies[4]; |
| Jim Van Verth | 5327536 | 2018-11-09 15:42:35 -0500 | [diff] [blame] | 229 | if (!SkImage_GpuBase::MakeTempTextureProxies(ctx, yuvaTextures, numTextures, yuvaIndices, |
| 230 | imageOrigin, tempTextureProxies)) { |
| Jim Van Verth | 0e67194 | 2018-11-09 12:03:57 -0500 | [diff] [blame] | 231 | return nullptr; |
| Jim Van Verth | 9bf8120 | 2018-10-30 15:53:36 -0400 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | return sk_make_sp<SkImage_GpuYUVA>(sk_ref_sp(ctx), imageSize.width(), imageSize.height(), |
| 235 | kNeedNewImageUniqueID, colorSpace, tempTextureProxies, |
| Brian Salomon | f05e6d3 | 2018-12-20 08:41:41 -0500 | [diff] [blame] | 236 | numTextures, yuvaIndices, imageOrigin, imageColorSpace); |
| Jim Van Verth | 9bf8120 | 2018-10-30 15:53:36 -0400 | [diff] [blame] | 237 | } |
| Jim Van Verth | c8429ad | 2018-11-20 11:12:37 -0500 | [diff] [blame] | 238 | |
| 239 | sk_sp<SkImage> SkImage::MakeFromYUVAPixmaps( |
| 240 | GrContext* context, SkYUVColorSpace yuvColorSpace, const SkPixmap yuvaPixmaps[], |
| 241 | const SkYUVAIndex yuvaIndices[4], SkISize imageSize, GrSurfaceOrigin imageOrigin, |
| 242 | bool buildMips, bool limitToMaxTextureSize, sk_sp<SkColorSpace> imageColorSpace) { |
| Mike Reed | 6a5f7e2 | 2019-05-23 15:30:07 -0400 | [diff] [blame^] | 243 | if (!context) { |
| 244 | return nullptr; // until we impl this for raster backend |
| 245 | } |
| 246 | |
| Jim Van Verth | c8429ad | 2018-11-20 11:12:37 -0500 | [diff] [blame] | 247 | int numPixmaps; |
| 248 | if (!SkYUVAIndex::AreValidIndices(yuvaIndices, &numPixmaps)) { |
| 249 | return nullptr; |
| 250 | } |
| 251 | |
| Brian Osman | 7dcc616 | 2019-03-25 10:12:57 -0400 | [diff] [blame] | 252 | if (!context->priv().caps()->mipMapSupport()) { |
| 253 | buildMips = false; |
| 254 | } |
| 255 | |
| Jim Van Verth | c8429ad | 2018-11-20 11:12:37 -0500 | [diff] [blame] | 256 | // Make proxies |
| Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 257 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
| Jim Van Verth | c8429ad | 2018-11-20 11:12:37 -0500 | [diff] [blame] | 258 | sk_sp<GrTextureProxy> tempTextureProxies[4]; |
| 259 | for (int i = 0; i < numPixmaps; ++i) { |
| 260 | const SkPixmap* pixmap = &yuvaPixmaps[i]; |
| 261 | SkAutoPixmapStorage resized; |
| Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 262 | int maxTextureSize = context->priv().caps()->maxTextureSize(); |
| Jim Van Verth | c8429ad | 2018-11-20 11:12:37 -0500 | [diff] [blame] | 263 | int maxDim = SkTMax(yuvaPixmaps[i].width(), yuvaPixmaps[i].height()); |
| 264 | if (limitToMaxTextureSize && maxDim > maxTextureSize) { |
| 265 | float scale = static_cast<float>(maxTextureSize) / maxDim; |
| 266 | int newWidth = SkTMin(static_cast<int>(yuvaPixmaps[i].width() * scale), |
| 267 | maxTextureSize); |
| 268 | int newHeight = SkTMin(static_cast<int>(yuvaPixmaps[i].height() * scale), |
| 269 | maxTextureSize); |
| 270 | SkImageInfo info = yuvaPixmaps[i].info().makeWH(newWidth, newHeight); |
| 271 | if (!resized.tryAlloc(info) || |
| 272 | !yuvaPixmaps[i].scalePixels(resized, kLow_SkFilterQuality)) { |
| 273 | return nullptr; |
| 274 | } |
| 275 | pixmap = &resized; |
| 276 | } |
| 277 | // Turn the pixmap into a GrTextureProxy |
| Brian Osman | 7dcc616 | 2019-03-25 10:12:57 -0400 | [diff] [blame] | 278 | SkBitmap bmp; |
| 279 | bmp.installPixels(*pixmap); |
| 280 | GrMipMapped mipMapped = buildMips ? GrMipMapped::kYes : GrMipMapped::kNo; |
| 281 | tempTextureProxies[i] = proxyProvider->createProxyFromBitmap(bmp, mipMapped); |
| Jim Van Verth | c8429ad | 2018-11-20 11:12:37 -0500 | [diff] [blame] | 282 | if (!tempTextureProxies[i]) { |
| 283 | return nullptr; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | return sk_make_sp<SkImage_GpuYUVA>(sk_ref_sp(context), imageSize.width(), imageSize.height(), |
| 288 | kNeedNewImageUniqueID, yuvColorSpace, tempTextureProxies, |
| Brian Salomon | f05e6d3 | 2018-12-20 08:41:41 -0500 | [diff] [blame] | 289 | numPixmaps, yuvaIndices, imageOrigin, imageColorSpace); |
| Jim Van Verth | c8429ad | 2018-11-20 11:12:37 -0500 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | |
| Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 293 | ///////////////////////////////////////////////////////////////////////////////////////////////// |
| Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 294 | sk_sp<SkImage> SkImage_GpuYUVA::MakePromiseYUVATexture( |
| 295 | GrContext* context, |
| 296 | SkYUVColorSpace yuvColorSpace, |
| 297 | const GrBackendFormat yuvaFormats[], |
| 298 | const SkISize yuvaSizes[], |
| 299 | const SkYUVAIndex yuvaIndices[4], |
| 300 | int imageWidth, |
| 301 | int imageHeight, |
| 302 | GrSurfaceOrigin imageOrigin, |
| 303 | sk_sp<SkColorSpace> imageColorSpace, |
| 304 | PromiseImageTextureFulfillProc textureFulfillProc, |
| 305 | PromiseImageTextureReleaseProc textureReleaseProc, |
| 306 | PromiseImageTextureDoneProc promiseDoneProc, |
| Brian Salomon | 0cc5754 | 2019-03-08 13:28:46 -0500 | [diff] [blame] | 307 | PromiseImageTextureContext textureContexts[], |
| 308 | PromiseImageApiVersion version) { |
| Jim Van Verth | f00b162 | 2018-10-10 13:03:23 -0400 | [diff] [blame] | 309 | int numTextures; |
| 310 | bool valid = SkYUVAIndex::AreValidIndices(yuvaIndices, &numTextures); |
| 311 | |
| Brian Salomon | be5a093 | 2018-12-10 10:03:26 -0500 | [diff] [blame] | 312 | // The contract here is that if 'promiseDoneProc' is passed in it should always be called, |
| 313 | // even if creation of the SkImage fails. Once we call MakePromiseImageLazyProxy it takes |
| 314 | // responsibility for calling the done proc. |
| 315 | if (!promiseDoneProc) { |
| 316 | return nullptr; |
| Jim Van Verth | f00b162 | 2018-10-10 13:03:23 -0400 | [diff] [blame] | 317 | } |
| Brian Salomon | be5a093 | 2018-12-10 10:03:26 -0500 | [diff] [blame] | 318 | int proxiesCreated = 0; |
| 319 | SkScopeExit callDone([promiseDoneProc, textureContexts, numTextures, &proxiesCreated]() { |
| 320 | for (int i = proxiesCreated; i < numTextures; ++i) { |
| 321 | promiseDoneProc(textureContexts[i]); |
| 322 | } |
| 323 | }); |
| Jim Van Verth | f00b162 | 2018-10-10 13:03:23 -0400 | [diff] [blame] | 324 | |
| 325 | if (!valid) { |
| 326 | return nullptr; |
| 327 | } |
| Robert Phillips | ef85d19 | 2018-10-09 11:24:09 -0400 | [diff] [blame] | 328 | |
| Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 329 | if (!context) { |
| 330 | return nullptr; |
| 331 | } |
| 332 | |
| Greg Kaiser | 9a2169e | 2019-02-10 17:29:46 -0800 | [diff] [blame] | 333 | if (imageWidth <= 0 || imageHeight <= 0) { |
| Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 334 | return nullptr; |
| 335 | } |
| 336 | |
| Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 337 | SkAlphaType at = (-1 != yuvaIndices[SkYUVAIndex::kA_Index].fIndex) ? kPremul_SkAlphaType |
| 338 | : kOpaque_SkAlphaType; |
| Brian Salomon | 5ad6fd3 | 2019-03-21 15:30:08 -0400 | [diff] [blame] | 339 | SkImageInfo info = SkImageInfo::Make(imageWidth, imageHeight, kAssumedColorType, |
| Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 340 | at, imageColorSpace); |
| 341 | if (!SkImageInfoIsValid(info)) { |
| 342 | return nullptr; |
| 343 | } |
| 344 | |
| Jim Van Verth | f9f0735 | 2018-10-24 10:32:20 -0400 | [diff] [blame] | 345 | // verify sizes with expected texture count |
| Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 346 | for (int i = 0; i < numTextures; ++i) { |
| Jim Van Verth | f9f0735 | 2018-10-24 10:32:20 -0400 | [diff] [blame] | 347 | if (yuvaSizes[i].isEmpty()) { |
| Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 348 | return nullptr; |
| Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 349 | } |
| 350 | } |
| Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 351 | for (int i = numTextures; i < SkYUVASizeInfo::kMaxCount; ++i) { |
| Jim Van Verth | f9f0735 | 2018-10-24 10:32:20 -0400 | [diff] [blame] | 352 | if (!yuvaSizes[i].isEmpty()) { |
| Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 353 | return nullptr; |
| 354 | } |
| Jim Van Verth | f99a674 | 2018-10-18 16:13:18 +0000 | [diff] [blame] | 355 | } |
| Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 356 | |
| 357 | // Get lazy proxies |
| Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 358 | sk_sp<GrTextureProxy> proxies[4]; |
| Jim Van Verth | f00b162 | 2018-10-10 13:03:23 -0400 | [diff] [blame] | 359 | for (int texIdx = 0; texIdx < numTextures; ++texIdx) { |
| Brian Salomon | f391d0f | 2018-12-14 09:18:50 -0500 | [diff] [blame] | 360 | GrPixelConfig config = |
| Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 361 | context->priv().caps()->getYUVAConfigFromBackendFormat(yuvaFormats[texIdx]); |
| Brian Salomon | f391d0f | 2018-12-14 09:18:50 -0500 | [diff] [blame] | 362 | if (config == kUnknown_GrPixelConfig) { |
| Jim Van Verth | f00b162 | 2018-10-10 13:03:23 -0400 | [diff] [blame] | 363 | return nullptr; |
| 364 | } |
| Brian Salomon | be5a093 | 2018-12-10 10:03:26 -0500 | [diff] [blame] | 365 | proxies[texIdx] = MakePromiseImageLazyProxy( |
| 366 | context, yuvaSizes[texIdx].width(), yuvaSizes[texIdx].height(), imageOrigin, config, |
| 367 | yuvaFormats[texIdx], GrMipMapped::kNo, textureFulfillProc, textureReleaseProc, |
| Brian Salomon | 0cc5754 | 2019-03-08 13:28:46 -0500 | [diff] [blame] | 368 | promiseDoneProc, textureContexts[texIdx], version); |
| Brian Salomon | be5a093 | 2018-12-10 10:03:26 -0500 | [diff] [blame] | 369 | ++proxiesCreated; |
| Jim Van Verth | f00b162 | 2018-10-10 13:03:23 -0400 | [diff] [blame] | 370 | if (!proxies[texIdx]) { |
| 371 | return nullptr; |
| Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | |
| Jim Van Verth | cea3902 | 2018-10-12 16:15:34 -0400 | [diff] [blame] | 375 | 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] | 376 | kNeedNewImageUniqueID, yuvColorSpace, proxies, numTextures, |
| Brian Salomon | f05e6d3 | 2018-12-20 08:41:41 -0500 | [diff] [blame] | 377 | yuvaIndices, imageOrigin, std::move(imageColorSpace)); |
| Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 378 | } |