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 | |
Brian Salomon | c2a9a97 | 2020-09-15 11:24:28 -0400 | [diff] [blame] | 12 | #include "include/core/SkYUVAPixmaps.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/core/SkYUVASizeInfo.h" |
Adlai Holler | 3a22017 | 2020-07-15 10:37:50 -0400 | [diff] [blame] | 14 | #include "include/gpu/GrDirectContext.h" |
Robert Phillips | b7bfbc2 | 2020-07-01 12:55:01 -0400 | [diff] [blame] | 15 | #include "include/gpu/GrRecordingContext.h" |
Brian Salomon | c1a249d | 2020-10-19 10:55:45 -0400 | [diff] [blame] | 16 | #include "include/gpu/GrYUVABackendTextures.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "src/core/SkAutoPixmapStorage.h" |
Mike Reed | 13711eb | 2020-07-14 17:16:32 -0400 | [diff] [blame] | 18 | #include "src/core/SkMipmap.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "src/core/SkScopeExit.h" |
Greg Daniel | 82c6b10 | 2020-01-21 10:33:22 -0500 | [diff] [blame] | 20 | #include "src/gpu/GrBitmapTextureMaker.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 21 | #include "src/gpu/GrClip.h" |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 22 | #include "src/gpu/GrDirectContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "src/gpu/GrGpu.h" |
Adlai Holler | 302e8fb | 2020-09-14 11:58:06 -0400 | [diff] [blame] | 24 | #include "src/gpu/GrImageContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #include "src/gpu/GrRecordingContextPriv.h" |
| 26 | #include "src/gpu/GrRenderTargetContext.h" |
Greg Daniel | 456f9b5 | 2020-03-05 19:14:18 +0000 | [diff] [blame] | 27 | #include "src/gpu/GrTexture.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 28 | #include "src/gpu/GrTextureProducer.h" |
| 29 | #include "src/gpu/SkGr.h" |
| 30 | #include "src/gpu/effects/GrYUVtoRGBEffect.h" |
| 31 | #include "src/image/SkImage_Gpu.h" |
| 32 | #include "src/image/SkImage_GpuYUVA.h" |
Jim Van Verth | f49262d | 2018-10-02 12:07:20 -0400 | [diff] [blame] | 33 | |
Brian Salomon | 5ad6fd3 | 2019-03-21 15:30:08 -0400 | [diff] [blame] | 34 | static constexpr auto kAssumedColorType = kRGBA_8888_SkColorType; |
| 35 | |
Adlai Holler | 302e8fb | 2020-09-14 11:58:06 -0400 | [diff] [blame] | 36 | SkImage_GpuYUVA::SkImage_GpuYUVA(sk_sp<GrImageContext> context, |
Brian Salomon | c505a45 | 2020-04-06 10:29:02 -0400 | [diff] [blame] | 37 | SkISize size, |
| 38 | uint32_t uniqueID, |
| 39 | SkYUVColorSpace colorSpace, |
| 40 | GrSurfaceProxyView views[], |
| 41 | int numViews, |
| 42 | const SkYUVAIndex yuvaIndices[4], |
Greg Daniel | c594e62 | 2019-10-15 14:01:49 -0400 | [diff] [blame] | 43 | sk_sp<SkColorSpace> imageColorSpace) |
Adlai Holler | 302e8fb | 2020-09-14 11:58:06 -0400 | [diff] [blame] | 44 | : INHERITED(std::move(context), |
Brian Salomon | c505a45 | 2020-04-06 10:29:02 -0400 | [diff] [blame] | 45 | size, |
| 46 | uniqueID, |
| 47 | kAssumedColorType, |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 48 | // If an alpha channel is present we always switch to kPremul. This is because, |
| 49 | // although the planar data is always un-premul, the final interleaved RGB image |
| 50 | // is/would-be premul. |
Brian Salomon | c505a45 | 2020-04-06 10:29:02 -0400 | [diff] [blame] | 51 | GetAlphaTypeFromYUVAIndices(yuvaIndices), |
| 52 | std::move(imageColorSpace)) |
Greg Daniel | c767209 | 2020-02-06 14:32:54 -0500 | [diff] [blame] | 53 | , fNumViews(numViews) |
Brian Salomon | 8e5cf44 | 2020-10-05 16:05:23 -0400 | [diff] [blame] | 54 | , fYUVColorSpace(colorSpace) { |
Jim Van Verth | 0e67194 | 2018-11-09 12:03:57 -0500 | [diff] [blame] | 55 | // The caller should have done this work, just verifying |
| 56 | SkDEBUGCODE(int textureCount;) |
| 57 | SkASSERT(SkYUVAIndex::AreValidIndices(yuvaIndices, &textureCount)); |
Greg Daniel | c767209 | 2020-02-06 14:32:54 -0500 | [diff] [blame] | 58 | SkASSERT(textureCount == fNumViews); |
Jim Van Verth | 0e67194 | 2018-11-09 12:03:57 -0500 | [diff] [blame] | 59 | |
Greg Daniel | c767209 | 2020-02-06 14:32:54 -0500 | [diff] [blame] | 60 | for (int i = 0; i < numViews; ++i) { |
| 61 | fViews[i] = std::move(views[i]); |
Jim Van Verth | f49262d | 2018-10-02 12:07:20 -0400 | [diff] [blame] | 62 | } |
Greg Daniel | c767209 | 2020-02-06 14:32:54 -0500 | [diff] [blame] | 63 | memcpy(fYUVAIndices, yuvaIndices, 4 * sizeof(SkYUVAIndex)); |
Jim Van Verth | f49262d | 2018-10-02 12:07:20 -0400 | [diff] [blame] | 64 | } |
| 65 | |
Jim Van Verth | 3e4c2f3 | 2019-01-11 13:32:45 -0500 | [diff] [blame] | 66 | // For onMakeColorSpace() |
Adlai Holler | 302e8fb | 2020-09-14 11:58:06 -0400 | [diff] [blame] | 67 | SkImage_GpuYUVA::SkImage_GpuYUVA(sk_sp<GrImageContext> context, const SkImage_GpuYUVA* image, |
Adlai Holler | 3a22017 | 2020-07-15 10:37:50 -0400 | [diff] [blame] | 68 | sk_sp<SkColorSpace> targetCS) |
| 69 | : INHERITED(std::move(context), image->dimensions(), kNeedNewImageUniqueID, |
| 70 | kAssumedColorType, |
Brian Salomon | 5ad6fd3 | 2019-03-21 15:30:08 -0400 | [diff] [blame] | 71 | // If an alpha channel is present we always switch to kPremul. This is because, |
| 72 | // although the planar data is always un-premul, the final interleaved RGB image |
| 73 | // is/would-be premul. |
| 74 | GetAlphaTypeFromYUVAIndices(image->fYUVAIndices), std::move(targetCS)) |
Greg Daniel | c767209 | 2020-02-06 14:32:54 -0500 | [diff] [blame] | 75 | , fNumViews(image->fNumViews) |
Brian Salomon | 5ad6fd3 | 2019-03-21 15:30:08 -0400 | [diff] [blame] | 76 | , fYUVColorSpace(image->fYUVColorSpace) |
Brian Salomon | 5ad6fd3 | 2019-03-21 15:30:08 -0400 | [diff] [blame] | 77 | // Since null fFromColorSpace means no GrColorSpaceXform, we turn a null |
| 78 | // image->refColorSpace() into an explicit SRGB. |
| 79 | , fFromColorSpace(image->colorSpace() ? image->refColorSpace() : SkColorSpace::MakeSRGB()) { |
| 80 | // The caller should have done this work, just verifying |
Jim Van Verth | 3e4c2f3 | 2019-01-11 13:32:45 -0500 | [diff] [blame] | 81 | SkDEBUGCODE(int textureCount;) |
Greg Daniel | c767209 | 2020-02-06 14:32:54 -0500 | [diff] [blame] | 82 | SkASSERT(SkYUVAIndex::AreValidIndices(image->fYUVAIndices, &textureCount)); |
| 83 | SkASSERT(textureCount == fNumViews); |
Jim Van Verth | 3e4c2f3 | 2019-01-11 13:32:45 -0500 | [diff] [blame] | 84 | |
Greg Daniel | 1a372c3 | 2019-12-17 13:53:38 -0500 | [diff] [blame] | 85 | if (image->fRGBView.proxy()) { |
| 86 | fRGBView = image->fRGBView; // we ref in this case, not move |
Brian Salomon | ad8efda | 2019-05-10 09:22:49 -0400 | [diff] [blame] | 87 | } else { |
Greg Daniel | c767209 | 2020-02-06 14:32:54 -0500 | [diff] [blame] | 88 | for (int i = 0; i < fNumViews; ++i) { |
| 89 | fViews[i] = image->fViews[i]; // we ref in this case, not move |
Brian Salomon | ad8efda | 2019-05-10 09:22:49 -0400 | [diff] [blame] | 90 | } |
Jim Van Verth | 3e4c2f3 | 2019-01-11 13:32:45 -0500 | [diff] [blame] | 91 | } |
| 92 | memcpy(fYUVAIndices, image->fYUVAIndices, 4 * sizeof(SkYUVAIndex)); |
| 93 | } |
| 94 | |
Robert Phillips | 8defcc1 | 2019-03-05 15:58:59 -0500 | [diff] [blame] | 95 | bool SkImage_GpuYUVA::setupMipmapsForPlanes(GrRecordingContext* context) const { |
Brian Salomon | ad8efda | 2019-05-10 09:22:49 -0400 | [diff] [blame] | 96 | // 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] | 97 | SkASSERT(fViews[0].proxy() && !fRGBView.proxy()); |
Robert Phillips | 8defcc1 | 2019-03-05 15:58:59 -0500 | [diff] [blame] | 98 | if (!context || !fContext->priv().matches(context)) { |
| 99 | return false; |
| 100 | } |
Brian Salomon | 4759dca | 2020-07-14 15:44:15 -0400 | [diff] [blame] | 101 | GrSurfaceProxyView newViews[4]; |
Brian Salomon | 69100f0 | 2020-07-21 10:49:25 -0400 | [diff] [blame] | 102 | if (!context->priv().caps()->mipmapSupport()) { |
Brian Salomon | 4759dca | 2020-07-14 15:44:15 -0400 | [diff] [blame] | 103 | // We succeed in this case by doing nothing. |
| 104 | return true; |
| 105 | } |
Greg Daniel | c767209 | 2020-02-06 14:32:54 -0500 | [diff] [blame] | 106 | for (int i = 0; i < fNumViews; ++i) { |
Brian Salomon | 4759dca | 2020-07-14 15:44:15 -0400 | [diff] [blame] | 107 | auto* t = fViews[i].asTextureProxy(); |
Brian Salomon | 8c82a87 | 2020-07-21 12:09:58 -0400 | [diff] [blame] | 108 | if (t->mipmapped() == GrMipmapped::kNo && (t->width() > 1 || t->height() > 1)) { |
Brian Salomon | 4759dca | 2020-07-14 15:44:15 -0400 | [diff] [blame] | 109 | if (!(newViews[i] = GrCopyBaseMipMapToView(context, fViews[i]))) { |
Jim Van Verth | f542cab | 2018-11-07 12:08:21 -0500 | [diff] [blame] | 110 | return false; |
| 111 | } |
Brian Salomon | 4759dca | 2020-07-14 15:44:15 -0400 | [diff] [blame] | 112 | } else { |
| 113 | newViews[i] = fViews[i]; |
Jim Van Verth | 30e0d7f | 2018-11-02 13:36:42 -0400 | [diff] [blame] | 114 | } |
| 115 | } |
Brian Salomon | 4759dca | 2020-07-14 15:44:15 -0400 | [diff] [blame] | 116 | for (int i = 0; i < fNumViews; ++i) { |
| 117 | fViews[i] = std::move(newViews[i]); |
| 118 | } |
Jim Van Verth | 30e0d7f | 2018-11-02 13:36:42 -0400 | [diff] [blame] | 119 | return true; |
| 120 | } |
| 121 | |
Jim Van Verth | f49262d | 2018-10-02 12:07:20 -0400 | [diff] [blame] | 122 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
Jim Van Verth | f49262d | 2018-10-02 12:07:20 -0400 | [diff] [blame] | 123 | |
Adlai Holler | 40ad5fd | 2020-07-22 10:22:12 -0400 | [diff] [blame] | 124 | GrSemaphoresSubmitted SkImage_GpuYUVA::onFlush(GrDirectContext* dContext, const GrFlushInfo& info) { |
| 125 | if (!fContext->priv().matches(dContext) || dContext->abandoned()) { |
Greg Daniel | 55822f1 | 2020-05-26 11:26:45 -0400 | [diff] [blame] | 126 | if (info.fSubmittedProc) { |
| 127 | info.fSubmittedProc(info.fSubmittedContext, false); |
| 128 | } |
| 129 | if (info.fFinishedProc) { |
| 130 | info.fFinishedProc(info.fFinishedContext); |
| 131 | } |
Brian Salomon | f9a1fdf | 2019-05-09 10:30:12 -0400 | [diff] [blame] | 132 | return GrSemaphoresSubmitted::kNo; |
| 133 | } |
| 134 | |
Greg Daniel | c767209 | 2020-02-06 14:32:54 -0500 | [diff] [blame] | 135 | GrSurfaceProxy* proxies[4] = {fViews[0].proxy(), fViews[1].proxy(), fViews[2].proxy(), |
| 136 | fViews[3].proxy()}; |
Adlai Holler | c2bfcff | 2020-11-06 15:39:36 -0500 | [diff] [blame] | 137 | size_t numProxies = fNumViews; |
Greg Daniel | 1a372c3 | 2019-12-17 13:53:38 -0500 | [diff] [blame] | 138 | if (fRGBView.proxy()) { |
Brian Salomon | ad8efda | 2019-05-10 09:22:49 -0400 | [diff] [blame] | 139 | // 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] | 140 | // latter case it should still be ok to just pass fRGBView proxy because it in turn depends |
| 141 | // on the planar proxies and will cause all of their work to flush as well. |
| 142 | proxies[0] = fRGBView.proxy(); |
Brian Salomon | ad8efda | 2019-05-10 09:22:49 -0400 | [diff] [blame] | 143 | numProxies = 1; |
Brian Salomon | f9a1fdf | 2019-05-09 10:30:12 -0400 | [diff] [blame] | 144 | } |
Robert Phillips | 80bfda8 | 2020-11-12 09:23:36 -0500 | [diff] [blame^] | 145 | return dContext->priv().flushSurfaces({proxies, numProxies}, |
| 146 | SkSurface::BackendSurfaceAccess::kNoAccess, |
| 147 | info); |
Brian Salomon | f9a1fdf | 2019-05-09 10:30:12 -0400 | [diff] [blame] | 148 | } |
| 149 | |
Greg Daniel | c767209 | 2020-02-06 14:32:54 -0500 | [diff] [blame] | 150 | GrTextureProxy* SkImage_GpuYUVA::peekProxy() const { return fRGBView.asTextureProxy(); } |
Robert Phillips | 193c421 | 2019-03-04 12:18:53 -0500 | [diff] [blame] | 151 | |
Greg Daniel | 1a372c3 | 2019-12-17 13:53:38 -0500 | [diff] [blame] | 152 | void SkImage_GpuYUVA::flattenToRGB(GrRecordingContext* context) const { |
| 153 | if (fRGBView.proxy()) { |
| 154 | return; |
Jim Van Verth | f49262d | 2018-10-02 12:07:20 -0400 | [diff] [blame] | 155 | } |
| 156 | |
Robert Phillips | 6603a17 | 2019-03-05 12:35:44 -0500 | [diff] [blame] | 157 | if (!context || !fContext->priv().matches(context)) { |
Greg Daniel | 1a372c3 | 2019-12-17 13:53:38 -0500 | [diff] [blame] | 158 | return; |
Robert Phillips | 6603a17 | 2019-03-05 12:35:44 -0500 | [diff] [blame] | 159 | } |
| 160 | |
Robert Phillips | 6603a17 | 2019-03-05 12:35:44 -0500 | [diff] [blame] | 161 | // 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] | 162 | auto renderTargetContext = GrRenderTargetContext::Make( |
| 163 | context, GrColorType::kRGBA_8888, this->refColorSpace(), SkBackingFit::kExact, |
Brian Salomon | 8e5cf44 | 2020-10-05 16:05:23 -0400 | [diff] [blame] | 164 | this->dimensions(), 1, GrMipmapped::kNo, GrProtected::kNo); |
Robert Phillips | 6603a17 | 2019-03-05 12:35:44 -0500 | [diff] [blame] | 165 | if (!renderTargetContext) { |
Greg Daniel | 1a372c3 | 2019-12-17 13:53:38 -0500 | [diff] [blame] | 166 | return; |
Robert Phillips | 6603a17 | 2019-03-05 12:35:44 -0500 | [diff] [blame] | 167 | } |
| 168 | |
Brian Salomon | 5ad6fd3 | 2019-03-21 15:30:08 -0400 | [diff] [blame] | 169 | sk_sp<GrColorSpaceXform> colorSpaceXform; |
| 170 | if (fFromColorSpace) { |
| 171 | colorSpaceXform = GrColorSpaceXform::Make(fFromColorSpace.get(), this->alphaType(), |
| 172 | this->colorSpace(), this->alphaType()); |
| 173 | } |
Robert Phillips | 6603a17 | 2019-03-05 12:35:44 -0500 | [diff] [blame] | 174 | const SkRect rect = SkRect::MakeIWH(this->width(), this->height()); |
Adlai Holler | 52ea577 | 2020-07-28 15:23:33 -0400 | [diff] [blame] | 175 | const GrCaps& caps = *context->priv().caps(); |
| 176 | if (!RenderYUVAToRGBA(caps, renderTargetContext.get(), rect, fYUVColorSpace, |
Greg Daniel | c767209 | 2020-02-06 14:32:54 -0500 | [diff] [blame] | 177 | std::move(colorSpaceXform), fViews, fYUVAIndices)) { |
Greg Daniel | 1a372c3 | 2019-12-17 13:53:38 -0500 | [diff] [blame] | 178 | return; |
Robert Phillips | 6603a17 | 2019-03-05 12:35:44 -0500 | [diff] [blame] | 179 | } |
| 180 | |
Greg Daniel | 1a372c3 | 2019-12-17 13:53:38 -0500 | [diff] [blame] | 181 | fRGBView = renderTargetContext->readSurfaceView(); |
Greg Daniel | 1a372c3 | 2019-12-17 13:53:38 -0500 | [diff] [blame] | 182 | SkASSERT(fRGBView.swizzle() == GrSwizzle()); |
Greg Daniel | c767209 | 2020-02-06 14:32:54 -0500 | [diff] [blame] | 183 | for (auto& v : fViews) { |
| 184 | v.reset(); |
Brian Salomon | ad8efda | 2019-05-10 09:22:49 -0400 | [diff] [blame] | 185 | } |
Greg Daniel | 1a372c3 | 2019-12-17 13:53:38 -0500 | [diff] [blame] | 186 | } |
| 187 | |
Greg Daniel | 37c127f | 2020-02-05 10:37:27 -0500 | [diff] [blame] | 188 | GrSurfaceProxyView SkImage_GpuYUVA::refMippedView(GrRecordingContext* context) const { |
Jim Van Verth | 803a502 | 2018-11-05 15:55:53 -0500 | [diff] [blame] | 189 | // if invalid or already has miplevels |
Greg Daniel | 1a372c3 | 2019-12-17 13:53:38 -0500 | [diff] [blame] | 190 | this->flattenToRGB(context); |
Brian Salomon | 8c82a87 | 2020-07-21 12:09:58 -0400 | [diff] [blame] | 191 | if (!fRGBView || fRGBView.asTextureProxy()->mipmapped() == GrMipmapped::kYes) { |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 192 | return fRGBView; |
Jim Van Verth | 803a502 | 2018-11-05 15:55:53 -0500 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | // need to generate mips for the proxy |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 196 | auto mippedView = GrCopyBaseMipMapToView(context, fRGBView); |
| 197 | if (!mippedView) { |
| 198 | return {}; |
Jim Van Verth | 803a502 | 2018-11-05 15:55:53 -0500 | [diff] [blame] | 199 | } |
| 200 | |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 201 | fRGBView = std::move(mippedView); |
| 202 | return fRGBView; |
Jim Van Verth | 803a502 | 2018-11-05 15:55:53 -0500 | [diff] [blame] | 203 | } |
| 204 | |
Greg Daniel | 37c127f | 2020-02-05 10:37:27 -0500 | [diff] [blame] | 205 | const GrSurfaceProxyView* SkImage_GpuYUVA::view(GrRecordingContext* context) const { |
Greg Daniel | 1a372c3 | 2019-12-17 13:53:38 -0500 | [diff] [blame] | 206 | this->flattenToRGB(context); |
Greg Daniel | 37c127f | 2020-02-05 10:37:27 -0500 | [diff] [blame] | 207 | if (!fRGBView.proxy()) { |
| 208 | return nullptr; |
| 209 | } |
| 210 | return &fRGBView; |
Greg Daniel | 81b9897 | 2019-12-13 11:09:43 -0500 | [diff] [blame] | 211 | } |
| 212 | |
Jim Van Verth | 9bf8120 | 2018-10-30 15:53:36 -0400 | [diff] [blame] | 213 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
| 214 | |
Greg Daniel | c767209 | 2020-02-06 14:32:54 -0500 | [diff] [blame] | 215 | sk_sp<SkImage> SkImage_GpuYUVA::onMakeColorTypeAndColorSpace( |
Adlai Holler | 3a22017 | 2020-07-15 10:37:50 -0400 | [diff] [blame] | 216 | SkColorType, sk_sp<SkColorSpace> targetCS, GrDirectContext* direct) const { |
Brian Osman | f48c996 | 2019-01-14 11:15:50 -0500 | [diff] [blame] | 217 | // We explicitly ignore color type changes, for now. |
| 218 | |
Jim Van Verth | 3e4c2f3 | 2019-01-11 13:32:45 -0500 | [diff] [blame] | 219 | // we may need a mutex here but for now we expect usage to be in a single thread |
| 220 | if (fOnMakeColorSpaceTarget && |
Brian Osman | f48c996 | 2019-01-14 11:15:50 -0500 | [diff] [blame] | 221 | SkColorSpace::Equals(targetCS.get(), fOnMakeColorSpaceTarget.get())) { |
Jim Van Verth | 3e4c2f3 | 2019-01-11 13:32:45 -0500 | [diff] [blame] | 222 | return fOnMakeColorSpaceResult; |
| 223 | } |
Adlai Holler | 3a22017 | 2020-07-15 10:37:50 -0400 | [diff] [blame] | 224 | sk_sp<SkImage> result = sk_sp<SkImage>(new SkImage_GpuYUVA(sk_ref_sp(direct), this, targetCS)); |
Jim Van Verth | 3e4c2f3 | 2019-01-11 13:32:45 -0500 | [diff] [blame] | 225 | if (result) { |
Brian Osman | f48c996 | 2019-01-14 11:15:50 -0500 | [diff] [blame] | 226 | fOnMakeColorSpaceTarget = targetCS; |
Jim Van Verth | 3e4c2f3 | 2019-01-11 13:32:45 -0500 | [diff] [blame] | 227 | fOnMakeColorSpaceResult = result; |
| 228 | } |
| 229 | return result; |
| 230 | } |
| 231 | |
Brian Osman | d514837 | 2019-08-14 16:14:51 -0400 | [diff] [blame] | 232 | sk_sp<SkImage> SkImage_GpuYUVA::onReinterpretColorSpace(sk_sp<SkColorSpace> newCS) const { |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 233 | return sk_make_sp<SkImage_GpuYUVA>(fContext, this->dimensions(), kNeedNewImageUniqueID, |
Brian Salomon | 8e5cf44 | 2020-10-05 16:05:23 -0400 | [diff] [blame] | 234 | fYUVColorSpace, fViews, fNumViews, fYUVAIndices, |
Brian Salomon | c505a45 | 2020-04-06 10:29:02 -0400 | [diff] [blame] | 235 | std::move(newCS)); |
Brian Osman | d514837 | 2019-08-14 16:14:51 -0400 | [diff] [blame] | 236 | } |
| 237 | |
Jim Van Verth | 3e4c2f3 | 2019-01-11 13:32:45 -0500 | [diff] [blame] | 238 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
| 239 | |
Brian Salomon | c1a249d | 2020-10-19 10:55:45 -0400 | [diff] [blame] | 240 | sk_sp<SkImage> SkImage::MakeFromYUVATextures(GrRecordingContext* context, |
| 241 | const GrYUVABackendTextures& yuvaTextures, |
| 242 | sk_sp<SkColorSpace> imageColorSpace, |
| 243 | TextureReleaseProc textureReleaseProc, |
| 244 | ReleaseContext releaseContext) { |
Brian Salomon | 694ff17 | 2020-11-04 16:54:28 -0500 | [diff] [blame] | 245 | auto releaseHelper = GrRefCntedCallback::Make(textureReleaseProc, releaseContext); |
Brian Salomon | c1a249d | 2020-10-19 10:55:45 -0400 | [diff] [blame] | 246 | |
| 247 | SkYUVAIndex yuvaIndices[4]; |
| 248 | int numTextures; |
| 249 | if (!yuvaTextures.toYUVAIndices(yuvaIndices) || |
| 250 | !SkYUVAIndex::AreValidIndices(yuvaIndices, &numTextures)) { |
| 251 | return nullptr; |
| 252 | } |
| 253 | SkASSERT(numTextures == yuvaTextures.numPlanes()); |
| 254 | |
| 255 | GrSurfaceProxyView tempViews[4]; |
| 256 | if (!SkImage_GpuBase::MakeTempTextureProxies(context, |
| 257 | yuvaTextures.textures().data(), |
| 258 | numTextures, |
| 259 | yuvaIndices, |
| 260 | yuvaTextures.textureOrigin(), |
| 261 | tempViews, |
| 262 | std::move(releaseHelper))) { |
| 263 | return nullptr; |
| 264 | } |
| 265 | |
| 266 | return sk_make_sp<SkImage_GpuYUVA>(sk_ref_sp(context), |
| 267 | yuvaTextures.yuvaInfo().dimensions(), |
| 268 | kNeedNewImageUniqueID, |
| 269 | yuvaTextures.yuvaInfo().yuvColorSpace(), |
| 270 | tempViews, |
| 271 | numTextures, |
| 272 | yuvaIndices, |
| 273 | imageColorSpace); |
| 274 | } |
| 275 | |
Adlai Holler | b270568 | 2020-10-20 10:11:53 -0400 | [diff] [blame] | 276 | sk_sp<SkImage> SkImage::MakeFromYUVATextures(GrRecordingContext* ctx, |
Jim Van Verth | 9bf8120 | 2018-10-30 15:53:36 -0400 | [diff] [blame] | 277 | SkYUVColorSpace colorSpace, |
| 278 | const GrBackendTexture yuvaTextures[], |
| 279 | const SkYUVAIndex yuvaIndices[4], |
| 280 | SkISize imageSize, |
Brian Salomon | 8e5cf44 | 2020-10-05 16:05:23 -0400 | [diff] [blame] | 281 | GrSurfaceOrigin textureOrigin, |
Robert Phillips | e22c5ca | 2020-06-19 12:29:57 -0400 | [diff] [blame] | 282 | sk_sp<SkColorSpace> imageColorSpace, |
| 283 | TextureReleaseProc textureReleaseProc, |
| 284 | ReleaseContext releaseContext) { |
Brian Salomon | 694ff17 | 2020-11-04 16:54:28 -0500 | [diff] [blame] | 285 | auto releaseHelper = GrRefCntedCallback::Make(textureReleaseProc, releaseContext); |
Robert Phillips | e22c5ca | 2020-06-19 12:29:57 -0400 | [diff] [blame] | 286 | |
Jim Van Verth | 0e67194 | 2018-11-09 12:03:57 -0500 | [diff] [blame] | 287 | int numTextures; |
| 288 | if (!SkYUVAIndex::AreValidIndices(yuvaIndices, &numTextures)) { |
| 289 | return nullptr; |
Jim Van Verth | 9bf8120 | 2018-10-30 15:53:36 -0400 | [diff] [blame] | 290 | } |
| 291 | |
Greg Daniel | c767209 | 2020-02-06 14:32:54 -0500 | [diff] [blame] | 292 | GrSurfaceProxyView tempViews[4]; |
Jim Van Verth | 5327536 | 2018-11-09 15:42:35 -0500 | [diff] [blame] | 293 | if (!SkImage_GpuBase::MakeTempTextureProxies(ctx, yuvaTextures, numTextures, yuvaIndices, |
Brian Salomon | 8e5cf44 | 2020-10-05 16:05:23 -0400 | [diff] [blame] | 294 | textureOrigin, tempViews, |
Robert Phillips | e22c5ca | 2020-06-19 12:29:57 -0400 | [diff] [blame] | 295 | std::move(releaseHelper))) { |
Jim Van Verth | 0e67194 | 2018-11-09 12:03:57 -0500 | [diff] [blame] | 296 | return nullptr; |
Jim Van Verth | 9bf8120 | 2018-10-30 15:53:36 -0400 | [diff] [blame] | 297 | } |
| 298 | |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 299 | return sk_make_sp<SkImage_GpuYUVA>(sk_ref_sp(ctx), imageSize, kNeedNewImageUniqueID, colorSpace, |
Brian Salomon | 8e5cf44 | 2020-10-05 16:05:23 -0400 | [diff] [blame] | 300 | tempViews, numTextures, yuvaIndices, imageColorSpace); |
Jim Van Verth | 9bf8120 | 2018-10-30 15:53:36 -0400 | [diff] [blame] | 301 | } |
Jim Van Verth | c8429ad | 2018-11-20 11:12:37 -0500 | [diff] [blame] | 302 | |
Adlai Holler | 302e8fb | 2020-09-14 11:58:06 -0400 | [diff] [blame] | 303 | sk_sp<SkImage> SkImage::MakeFromYUVAPixmaps(GrRecordingContext* context, |
Brian Salomon | c2a9a97 | 2020-09-15 11:24:28 -0400 | [diff] [blame] | 304 | const SkYUVAPixmaps& pixmaps, |
| 305 | GrMipMapped buildMips, |
| 306 | bool limitToMaxTextureSize, |
| 307 | sk_sp<SkColorSpace> imageColorSpace) { |
| 308 | if (!context) { |
| 309 | return nullptr; // until we impl this for raster backend |
| 310 | } |
| 311 | |
| 312 | if (!pixmaps.isValid()) { |
| 313 | return nullptr; |
| 314 | } |
| 315 | |
| 316 | SkYUVAIndex yuvaIndices[4]; |
| 317 | if (!pixmaps.toLegacy(nullptr, yuvaIndices)) { |
| 318 | return nullptr; |
| 319 | } |
| 320 | |
| 321 | // SkImage_GpuYUVA doesn't yet support different encoded origins. |
| 322 | if (pixmaps.yuvaInfo().origin() != kTopLeft_SkEncodedOrigin) { |
| 323 | return nullptr; |
| 324 | } |
| 325 | |
| 326 | if (!context->priv().caps()->mipmapSupport()) { |
| 327 | buildMips = GrMipMapped::kNo; |
| 328 | } |
| 329 | |
| 330 | // Make proxies |
| 331 | GrSurfaceProxyView tempViews[4]; |
| 332 | int numPlanes = pixmaps.numPlanes(); |
| 333 | int maxTextureSize = context->priv().caps()->maxTextureSize(); |
| 334 | for (int i = 0; i < numPlanes; ++i) { |
| 335 | const SkPixmap* pixmap = &pixmaps.plane(i); |
| 336 | SkAutoPixmapStorage resized; |
| 337 | int maxDim = std::max(pixmap->width(), pixmap->height()); |
| 338 | if (maxDim > maxTextureSize) { |
| 339 | if (!limitToMaxTextureSize) { |
| 340 | return nullptr; |
| 341 | } |
| 342 | float scale = static_cast<float>(maxTextureSize)/maxDim; |
| 343 | int newWidth = std::min(static_cast<int>(pixmap->width() *scale), maxTextureSize); |
| 344 | int newHeight = std::min(static_cast<int>(pixmap->height()*scale), maxTextureSize); |
| 345 | SkImageInfo info = pixmap->info().makeWH(newWidth, newHeight); |
| 346 | if (!resized.tryAlloc(info) || !pixmap->scalePixels(resized, kLow_SkFilterQuality)) { |
| 347 | return nullptr; |
| 348 | } |
| 349 | pixmap = &resized; |
| 350 | } |
| 351 | // Turn the pixmap into a GrTextureProxy |
| 352 | SkBitmap bmp; |
| 353 | bmp.installPixels(*pixmap); |
| 354 | GrBitmapTextureMaker bitmapMaker(context, bmp, GrImageTexGenPolicy::kNew_Uncached_Budgeted); |
| 355 | tempViews[i] = bitmapMaker.view(buildMips); |
| 356 | if (!tempViews[i]) { |
| 357 | return nullptr; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | return sk_make_sp<SkImage_GpuYUVA>(sk_ref_sp(context), |
| 362 | pixmaps.yuvaInfo().dimensions(), |
| 363 | kNeedNewImageUniqueID, |
| 364 | pixmaps.yuvaInfo().yuvColorSpace(), |
| 365 | tempViews, |
| 366 | numPlanes, |
| 367 | yuvaIndices, |
Brian Salomon | c2a9a97 | 2020-09-15 11:24:28 -0400 | [diff] [blame] | 368 | std::move(imageColorSpace)); |
Jim Van Verth | c8429ad | 2018-11-20 11:12:37 -0500 | [diff] [blame] | 369 | } |
| 370 | |
Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 371 | ///////////////////////////////////////////////////////////////////////////////////////////////// |
Brian Salomon | f143274 | 2020-11-09 15:40:27 -0500 | [diff] [blame] | 372 | |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 373 | sk_sp<SkImage> SkImage_GpuYUVA::MakePromiseYUVATexture( |
Robert Phillips | 07531a0 | 2020-07-15 15:11:09 -0400 | [diff] [blame] | 374 | GrRecordingContext* context, |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 375 | SkYUVColorSpace yuvColorSpace, |
| 376 | const GrBackendFormat yuvaFormats[], |
| 377 | const SkISize yuvaSizes[], |
| 378 | const SkYUVAIndex yuvaIndices[4], |
| 379 | int imageWidth, |
| 380 | int imageHeight, |
Brian Salomon | 8e5cf44 | 2020-10-05 16:05:23 -0400 | [diff] [blame] | 381 | GrSurfaceOrigin textureOrigin, |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 382 | sk_sp<SkColorSpace> imageColorSpace, |
| 383 | PromiseImageTextureFulfillProc textureFulfillProc, |
| 384 | PromiseImageTextureReleaseProc textureReleaseProc, |
Brian Salomon | f143274 | 2020-11-09 15:40:27 -0500 | [diff] [blame] | 385 | PromiseImageTextureContext textureContexts[]) { |
Jim Van Verth | f00b162 | 2018-10-10 13:03:23 -0400 | [diff] [blame] | 386 | int numTextures; |
| 387 | bool valid = SkYUVAIndex::AreValidIndices(yuvaIndices, &numTextures); |
| 388 | |
Brian Salomon | f143274 | 2020-11-09 15:40:27 -0500 | [diff] [blame] | 389 | // Our contract is that we will always call the release proc even on failure. |
| 390 | // We use the helper to convey the context, so we need to ensure make doesn't fail. |
| 391 | textureReleaseProc = textureReleaseProc ? textureReleaseProc : [](void*) {}; |
| 392 | sk_sp<GrRefCntedCallback> releaseHelpers[4]; |
| 393 | for (int i = 0; i < numTextures; ++i) { |
| 394 | releaseHelpers[i] = GrRefCntedCallback::Make(textureReleaseProc, textureContexts[i]); |
| 395 | } |
| 396 | |
| 397 | if (!context) { |
Brian Salomon | be5a093 | 2018-12-10 10:03:26 -0500 | [diff] [blame] | 398 | return nullptr; |
Jim Van Verth | f00b162 | 2018-10-10 13:03:23 -0400 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | if (!valid) { |
| 402 | return nullptr; |
| 403 | } |
Robert Phillips | ef85d19 | 2018-10-09 11:24:09 -0400 | [diff] [blame] | 404 | |
Greg Kaiser | 9a2169e | 2019-02-10 17:29:46 -0800 | [diff] [blame] | 405 | if (imageWidth <= 0 || imageHeight <= 0) { |
Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 406 | return nullptr; |
| 407 | } |
| 408 | |
Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 409 | SkAlphaType at = (-1 != yuvaIndices[SkYUVAIndex::kA_Index].fIndex) ? kPremul_SkAlphaType |
| 410 | : kOpaque_SkAlphaType; |
Greg Daniel | c767209 | 2020-02-06 14:32:54 -0500 | [diff] [blame] | 411 | SkImageInfo info = |
| 412 | SkImageInfo::Make(imageWidth, imageHeight, kAssumedColorType, at, imageColorSpace); |
Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 413 | if (!SkImageInfoIsValid(info)) { |
| 414 | return nullptr; |
| 415 | } |
| 416 | |
Jim Van Verth | f9f0735 | 2018-10-24 10:32:20 -0400 | [diff] [blame] | 417 | // verify sizes with expected texture count |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 418 | for (int i = 0; i < numTextures; ++i) { |
Jim Van Verth | f9f0735 | 2018-10-24 10:32:20 -0400 | [diff] [blame] | 419 | if (yuvaSizes[i].isEmpty()) { |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 420 | return nullptr; |
Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 421 | } |
| 422 | } |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 423 | for (int i = numTextures; i < SkYUVASizeInfo::kMaxCount; ++i) { |
Jim Van Verth | f9f0735 | 2018-10-24 10:32:20 -0400 | [diff] [blame] | 424 | if (!yuvaSizes[i].isEmpty()) { |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 425 | return nullptr; |
| 426 | } |
Jim Van Verth | f99a674 | 2018-10-18 16:13:18 +0000 | [diff] [blame] | 427 | } |
Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 428 | |
| 429 | // Get lazy proxies |
Greg Daniel | c767209 | 2020-02-06 14:32:54 -0500 | [diff] [blame] | 430 | GrSurfaceProxyView views[4]; |
Jim Van Verth | f00b162 | 2018-10-10 13:03:23 -0400 | [diff] [blame] | 431 | for (int texIdx = 0; texIdx < numTextures; ++texIdx) { |
Brian Salomon | f143274 | 2020-11-09 15:40:27 -0500 | [diff] [blame] | 432 | auto proxy = MakePromiseImageLazyProxy(context, |
| 433 | yuvaSizes[texIdx], |
| 434 | yuvaFormats[texIdx], |
| 435 | GrMipmapped::kNo, |
| 436 | textureFulfillProc, |
| 437 | std::move(releaseHelpers[texIdx])); |
Greg Daniel | c767209 | 2020-02-06 14:32:54 -0500 | [diff] [blame] | 438 | if (!proxy) { |
Jim Van Verth | f00b162 | 2018-10-10 13:03:23 -0400 | [diff] [blame] | 439 | return nullptr; |
Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 440 | } |
Brian Salomon | 8e5cf44 | 2020-10-05 16:05:23 -0400 | [diff] [blame] | 441 | views[texIdx] = GrSurfaceProxyView(std::move(proxy), textureOrigin, GrSwizzle("rgba")); |
Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 442 | } |
| 443 | |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 444 | 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] | 445 | kNeedNewImageUniqueID, yuvColorSpace, views, numTextures, |
Brian Salomon | 8e5cf44 | 2020-10-05 16:05:23 -0400 | [diff] [blame] | 446 | yuvaIndices, std::move(imageColorSpace)); |
Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 447 | } |