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