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