Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 1 | /* |
Ethan Nicholas | 130fb3f | 2018-02-01 12:14:34 -0500 | [diff] [blame] | 2 | * Copyright 2018 Google Inc. |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/effects/GrYUVtoRGBEffect.h" |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 9 | |
Brian Osman | 904cac8 | 2019-10-30 16:57:14 -0400 | [diff] [blame] | 10 | #include "src/core/SkYUVMath.h" |
Greg Daniel | 456f9b5 | 2020-03-05 19:14:18 +0000 | [diff] [blame] | 11 | #include "src/gpu/GrTexture.h" |
Brian Salomon | b9b1373 | 2020-06-25 11:13:49 -0400 | [diff] [blame] | 12 | #include "src/gpu/effects/GrMatrixEffect.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
| 14 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 15 | #include "src/gpu/glsl/GrGLSLProgramBuilder.h" |
| 16 | #include "src/sksl/SkSLCPP.h" |
| 17 | #include "src/sksl/SkSLUtil.h" |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 18 | |
Brian Salomon | d71548a | 2020-02-29 19:43:30 -0500 | [diff] [blame] | 19 | static void border_colors(SkYUVColorSpace cs, |
| 20 | const SkYUVAIndex yuvaIndices[4], |
| 21 | float planeBorders[4][4]) { |
| 22 | float m[20]; |
| 23 | SkColorMatrix_RGB2YUV(cs, m); |
| 24 | for (int i = 0; i < 4; ++i) { |
| 25 | if (yuvaIndices[i].fIndex == -1) { |
| 26 | return; |
| 27 | } |
| 28 | auto c = static_cast<int>(yuvaIndices[i].fChannel); |
| 29 | planeBorders[yuvaIndices[i].fIndex][c] = m[i*5 + 4]; |
| 30 | } |
| 31 | } |
| 32 | |
Greg Daniel | c767209 | 2020-02-06 14:32:54 -0500 | [diff] [blame] | 33 | std::unique_ptr<GrFragmentProcessor> GrYUVtoRGBEffect::Make(GrSurfaceProxyView views[], |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 34 | const SkYUVAIndex yuvaIndices[4], |
Jim Van Verth | 30e0d7f | 2018-11-02 13:36:42 -0400 | [diff] [blame] | 35 | SkYUVColorSpace yuvColorSpace, |
Brian Salomon | d71548a | 2020-02-29 19:43:30 -0500 | [diff] [blame] | 36 | GrSamplerState samplerState, |
Brian Salomon | ca6b2f4 | 2020-01-24 11:31:21 -0500 | [diff] [blame] | 37 | const GrCaps& caps, |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 38 | const SkMatrix& localMatrix, |
Brian Salomon | 0ea3307 | 2020-07-14 10:43:42 -0400 | [diff] [blame] | 39 | const SkRect* subset, |
| 40 | const SkRect* domain) { |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 41 | int numPlanes; |
Jim Van Verth | f00b162 | 2018-10-10 13:03:23 -0400 | [diff] [blame] | 42 | SkAssertResult(SkYUVAIndex::AreValidIndices(yuvaIndices, &numPlanes)); |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 43 | |
Brian Salomon | d71548a | 2020-02-29 19:43:30 -0500 | [diff] [blame] | 44 | const SkISize yDimensions = |
| 45 | views[yuvaIndices[SkYUVAIndex::kY_Index].fIndex].proxy()->dimensions(); |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 46 | |
Brian Salomon | d71548a | 2020-02-29 19:43:30 -0500 | [diff] [blame] | 47 | bool usesBorder = samplerState.wrapModeX() == GrSamplerState::WrapMode::kClampToBorder || |
| 48 | samplerState.wrapModeY() == GrSamplerState::WrapMode::kClampToBorder; |
| 49 | float planeBorders[4][4] = {}; |
| 50 | if (usesBorder) { |
| 51 | border_colors(yuvColorSpace, yuvaIndices, planeBorders); |
| 52 | } |
| 53 | |
Brian Salomon | b9b1373 | 2020-06-25 11:13:49 -0400 | [diff] [blame] | 54 | bool snap[2] = {false, false}; |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 55 | std::unique_ptr<GrFragmentProcessor> planeFPs[4]; |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 56 | for (int i = 0; i < numPlanes; ++i) { |
Greg Daniel | c767209 | 2020-02-06 14:32:54 -0500 | [diff] [blame] | 57 | SkISize dimensions = views[i].proxy()->dimensions(); |
Brian Salomon | b9b1373 | 2020-06-25 11:13:49 -0400 | [diff] [blame] | 58 | SkTCopyOnFirstWrite<SkMatrix> planeMatrix(&SkMatrix::I()); |
| 59 | SkRect planeSubset; |
Brian Salomon | 0ea3307 | 2020-07-14 10:43:42 -0400 | [diff] [blame] | 60 | SkRect planeDomain; |
Brian Salomon | a3b02f5 | 2020-07-15 16:02:01 -0400 | [diff] [blame] | 61 | bool makeLinearWithSnap = false; |
Brian Salomon | b9b1373 | 2020-06-25 11:13:49 -0400 | [diff] [blame] | 62 | float sx = 1.f, |
| 63 | sy = 1.f; |
Brian Salomon | d71548a | 2020-02-29 19:43:30 -0500 | [diff] [blame] | 64 | if (dimensions != yDimensions) { |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 65 | // JPEG chroma subsampling of odd dimensions produces U and V planes with the ceiling of |
| 66 | // the image size divided by the subsampling factor (2). Our API for creating YUVA |
| 67 | // doesn't capture the intended subsampling (and we should fix that). This fixes up 2x |
| 68 | // subsampling for images with odd widths/heights (e.g. JPEG 420 or 422). |
Brian Salomon | b9b1373 | 2020-06-25 11:13:49 -0400 | [diff] [blame] | 69 | sx = (float)dimensions.width() / yDimensions.width(); |
| 70 | sy = (float)dimensions.height() / yDimensions.height(); |
Brian Salomon | d71548a | 2020-02-29 19:43:30 -0500 | [diff] [blame] | 71 | if ((yDimensions.width() & 0b1) && dimensions.width() == yDimensions.width() / 2 + 1) { |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 72 | sx = 0.5f; |
| 73 | } |
Brian Salomon | d71548a | 2020-02-29 19:43:30 -0500 | [diff] [blame] | 74 | if ((yDimensions.height() & 0b1) && |
| 75 | dimensions.height() == yDimensions.height() / 2 + 1) { |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 76 | sy = 0.5f; |
| 77 | } |
Brian Salomon | 0ea3307 | 2020-07-14 10:43:42 -0400 | [diff] [blame] | 78 | *planeMatrix.writable() = SkMatrix::Scale(sx, sy); |
| 79 | if (subset) { |
| 80 | planeSubset = {subset->fLeft * sx, |
| 81 | subset->fTop * sy, |
| 82 | subset->fRight * sx, |
| 83 | subset->fBottom * sy}; |
| 84 | } |
| 85 | if (domain) { |
| 86 | planeDomain = {domain->fLeft * sx, |
| 87 | domain->fTop * sy, |
| 88 | domain->fRight * sx, |
| 89 | domain->fBottom * sy}; |
| 90 | } |
Brian Salomon | a3b02f5 | 2020-07-15 16:02:01 -0400 | [diff] [blame] | 91 | // This promotion of nearest to linear filtering for UV planes exists to mimic |
| 92 | // libjpeg[-turbo]'s do_fancy_upsampling option. We will filter the subsampled plane, |
| 93 | // however we want to filter at a fixed point for each logical image pixel to simulate |
| 94 | // nearest neighbor. |
Brian Salomon | b9b1373 | 2020-06-25 11:13:49 -0400 | [diff] [blame] | 95 | if (samplerState.filter() == GrSamplerState::Filter::kNearest) { |
| 96 | bool snapX = (sx != 1.f), |
| 97 | snapY = (sy != 1.f); |
Brian Salomon | a3b02f5 | 2020-07-15 16:02:01 -0400 | [diff] [blame] | 98 | makeLinearWithSnap = snapX || snapY; |
Brian Salomon | b9b1373 | 2020-06-25 11:13:49 -0400 | [diff] [blame] | 99 | snap[0] |= snapX; |
| 100 | snap[1] |= snapY; |
Brian Salomon | 0ea3307 | 2020-07-14 10:43:42 -0400 | [diff] [blame] | 101 | if (domain) { |
| 102 | // The outer YUVToRGB effect will ensure sampling happens at pixel centers |
| 103 | // within this plane. |
| 104 | planeDomain = {std::floor(planeDomain.fLeft) + 0.5f, |
| 105 | std::floor(planeDomain.fTop) + 0.5f, |
| 106 | std::floor(planeDomain.fRight) + 0.5f, |
| 107 | std::floor(planeDomain.fBottom) + 0.5f}; |
| 108 | } |
Brian Salomon | b9b1373 | 2020-06-25 11:13:49 -0400 | [diff] [blame] | 109 | } |
Brian Salomon | 0ea3307 | 2020-07-14 10:43:42 -0400 | [diff] [blame] | 110 | } else { |
Brian Salomon | d71548a | 2020-02-29 19:43:30 -0500 | [diff] [blame] | 111 | if (subset) { |
Brian Salomon | 0ea3307 | 2020-07-14 10:43:42 -0400 | [diff] [blame] | 112 | planeSubset = *subset; |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 113 | } |
Brian Salomon | 0ea3307 | 2020-07-14 10:43:42 -0400 | [diff] [blame] | 114 | if (domain) { |
| 115 | planeDomain = *domain; |
| 116 | } |
Brian Salomon | 8a99a41 | 2019-12-04 11:05:35 -0500 | [diff] [blame] | 117 | } |
Brian Salomon | d71548a | 2020-02-29 19:43:30 -0500 | [diff] [blame] | 118 | if (subset) { |
Brian Salomon | e69b9ef | 2020-07-22 11:18:06 -0400 | [diff] [blame] | 119 | SkASSERT(samplerState.mipmapped() == GrMipmapped::kNo); |
Brian Salomon | a3b02f5 | 2020-07-15 16:02:01 -0400 | [diff] [blame] | 120 | if (makeLinearWithSnap) { |
Brian Salomon | b9b1373 | 2020-06-25 11:13:49 -0400 | [diff] [blame] | 121 | // The plane is subsampled and we have an overall subset on the image. We're |
Brian Salomon | a3b02f5 | 2020-07-15 16:02:01 -0400 | [diff] [blame] | 122 | // emulating do_fancy_upsampling using linear filtering but snapping look ups to the |
| 123 | // y-plane pixel centers. Consider a logical image pixel at the edge of the subset. |
| 124 | // When computing the logical pixel color value we should use a 50/50 blend of two |
| 125 | // values from the subsampled plane. Depending on where the subset edge falls in |
| 126 | // actual subsampled plane, one of those values may come from outside the subset. |
| 127 | // Hence, we use this custom inset factory which applies the wrap mode to |
| 128 | // planeSubset but allows linear filtering to read pixels from the plane that are |
| 129 | // just outside planeSubset. |
Brian Salomon | 0ea3307 | 2020-07-14 10:43:42 -0400 | [diff] [blame] | 130 | SkRect* domainRect = domain ? &planeDomain : nullptr; |
Brian Salomon | a3b02f5 | 2020-07-15 16:02:01 -0400 | [diff] [blame] | 131 | planeFPs[i] = GrTextureEffect::MakeCustomLinearFilterInset( |
Brian Salomon | b9b1373 | 2020-06-25 11:13:49 -0400 | [diff] [blame] | 132 | views[i], kUnknown_SkAlphaType, *planeMatrix, samplerState.wrapModeX(), |
Brian Salomon | a3b02f5 | 2020-07-15 16:02:01 -0400 | [diff] [blame] | 133 | samplerState.wrapModeY(), planeSubset, domainRect, {sx / 2.f, sy / 2.f}, |
| 134 | caps, planeBorders[i]); |
Brian Salomon | 0ea3307 | 2020-07-14 10:43:42 -0400 | [diff] [blame] | 135 | } else if (domain) { |
| 136 | planeFPs[i] = GrTextureEffect::MakeSubset(views[i], kUnknown_SkAlphaType, |
| 137 | *planeMatrix, samplerState, planeSubset, |
| 138 | planeDomain, caps, planeBorders[i]); |
Brian Salomon | b9b1373 | 2020-06-25 11:13:49 -0400 | [diff] [blame] | 139 | } else { |
Brian Salomon | b9b1373 | 2020-06-25 11:13:49 -0400 | [diff] [blame] | 140 | planeFPs[i] = GrTextureEffect::MakeSubset(views[i], kUnknown_SkAlphaType, |
| 141 | *planeMatrix, samplerState, planeSubset, |
| 142 | caps, planeBorders[i]); |
| 143 | } |
Brian Salomon | ca6b2f4 | 2020-01-24 11:31:21 -0500 | [diff] [blame] | 144 | } else { |
Brian Salomon | b9b1373 | 2020-06-25 11:13:49 -0400 | [diff] [blame] | 145 | GrSamplerState planeSampler = samplerState; |
Brian Salomon | a3b02f5 | 2020-07-15 16:02:01 -0400 | [diff] [blame] | 146 | if (makeLinearWithSnap) { |
| 147 | planeSampler.setFilterMode(GrSamplerState::Filter::kLinear); |
Brian Salomon | b9b1373 | 2020-06-25 11:13:49 -0400 | [diff] [blame] | 148 | } |
Greg Daniel | c767209 | 2020-02-06 14:32:54 -0500 | [diff] [blame] | 149 | planeFPs[i] = GrTextureEffect::Make(views[i], kUnknown_SkAlphaType, *planeMatrix, |
Brian Salomon | b9b1373 | 2020-06-25 11:13:49 -0400 | [diff] [blame] | 150 | planeSampler, caps, planeBorders[i]); |
Brian Salomon | 8a99a41 | 2019-12-04 11:05:35 -0500 | [diff] [blame] | 151 | } |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 152 | } |
Brian Salomon | b9b1373 | 2020-06-25 11:13:49 -0400 | [diff] [blame] | 153 | auto fp = std::unique_ptr<GrFragmentProcessor>( |
| 154 | new GrYUVtoRGBEffect(planeFPs, numPlanes, yuvaIndices, snap, yuvColorSpace)); |
| 155 | return GrMatrixEffect::Make(localMatrix, std::move(fp)); |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | static SkAlphaType alpha_type(const SkYUVAIndex yuvaIndices[4]) { |
| 159 | return yuvaIndices[3].fIndex >= 0 ? kPremul_SkAlphaType : kOpaque_SkAlphaType; |
| 160 | } |
| 161 | |
Brian Salomon | b9b1373 | 2020-06-25 11:13:49 -0400 | [diff] [blame] | 162 | GrYUVtoRGBEffect::GrYUVtoRGBEffect(std::unique_ptr<GrFragmentProcessor> planeFPs[4], |
| 163 | int numPlanes, |
| 164 | const SkYUVAIndex yuvaIndices[4], |
| 165 | const bool snap[2], |
| 166 | SkYUVColorSpace yuvColorSpace) |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 167 | : GrFragmentProcessor(kGrYUVtoRGBEffect_ClassID, |
| 168 | ModulateForClampedSamplerOptFlags(alpha_type(yuvaIndices))) |
| 169 | , fYUVColorSpace(yuvColorSpace) { |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 170 | std::copy_n(yuvaIndices, 4, fYUVAIndices); |
Brian Salomon | b9b1373 | 2020-06-25 11:13:49 -0400 | [diff] [blame] | 171 | std::copy_n(snap, 2, fSnap); |
| 172 | |
| 173 | if (fSnap[0] || fSnap[1]) { |
| 174 | // Need this so that we can access coords in SKSL to perform snapping. |
Michael Ludwig | fbe2859 | 2020-06-26 16:02:15 -0400 | [diff] [blame] | 175 | this->setUsesSampleCoordsDirectly(); |
Brian Salomon | b9b1373 | 2020-06-25 11:13:49 -0400 | [diff] [blame] | 176 | for (int i = 0; i < numPlanes; ++i) { |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 177 | this->registerChild(std::move(planeFPs[i]), SkSL::SampleUsage::Explicit()); |
Brian Salomon | b9b1373 | 2020-06-25 11:13:49 -0400 | [diff] [blame] | 178 | } |
| 179 | } else { |
| 180 | for (int i = 0; i < numPlanes; ++i) { |
| 181 | this->registerChild(std::move(planeFPs[i])); |
| 182 | } |
| 183 | } |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 184 | } |
Robert Phillips | ba5c439 | 2018-07-25 12:37:14 -0400 | [diff] [blame] | 185 | |
Brian Osman | 9a390ac | 2018-11-12 09:47:48 -0500 | [diff] [blame] | 186 | #ifdef SK_DEBUG |
Robert Phillips | ba5c439 | 2018-07-25 12:37:14 -0400 | [diff] [blame] | 187 | SkString GrYUVtoRGBEffect::dumpInfo() const { |
John Stiles | ba1879d | 2020-08-11 13:58:32 -0400 | [diff] [blame^] | 188 | SkString str("YUVtoRGBEffect("); |
Brian Salomon | d90b3d3 | 2020-07-09 12:04:31 -0400 | [diff] [blame] | 189 | for (int i = 0; i < 4; ++i) { |
John Stiles | ba1879d | 2020-08-11 13:58:32 -0400 | [diff] [blame^] | 190 | str.appendf("YUVAIndices[%d]=%d %d, ", |
| 191 | i, fYUVAIndices[i].fIndex, static_cast<int>(fYUVAIndices[i].fChannel)); |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 192 | } |
John Stiles | ba1879d | 2020-08-11 13:58:32 -0400 | [diff] [blame^] | 193 | str.appendf("YUVColorSpace=%d, snap=(%d, %d))", |
| 194 | static_cast<int>(fYUVColorSpace), fSnap[0], fSnap[1]); |
Robert Phillips | ba5c439 | 2018-07-25 12:37:14 -0400 | [diff] [blame] | 195 | return str; |
| 196 | } |
Brian Osman | 9a390ac | 2018-11-12 09:47:48 -0500 | [diff] [blame] | 197 | #endif |
| 198 | |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 199 | GrGLSLFragmentProcessor* GrYUVtoRGBEffect::onCreateGLSLInstance() const { |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 200 | class GrGLSLYUVtoRGBEffect : public GrGLSLFragmentProcessor { |
| 201 | public: |
| 202 | GrGLSLYUVtoRGBEffect() {} |
| 203 | |
| 204 | void emitCode(EmitArgs& args) override { |
| 205 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 206 | const GrYUVtoRGBEffect& yuvEffect = args.fFp.cast<GrYUVtoRGBEffect>(); |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 207 | |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 208 | int numPlanes = yuvEffect.numChildProcessors(); |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 209 | |
Brian Salomon | b9b1373 | 2020-06-25 11:13:49 -0400 | [diff] [blame] | 210 | const char* sampleCoords = ""; |
| 211 | if (yuvEffect.fSnap[0] || yuvEffect.fSnap[1]) { |
| 212 | fragBuilder->codeAppendf("float2 snappedCoords = %s;", args.fSampleCoord); |
| 213 | if (yuvEffect.fSnap[0]) { |
| 214 | fragBuilder->codeAppend("snappedCoords.x = floor(snappedCoords.x) + 0.5;"); |
| 215 | } |
| 216 | if (yuvEffect.fSnap[1]) { |
| 217 | fragBuilder->codeAppend("snappedCoords.y = floor(snappedCoords.y) + 0.5;"); |
| 218 | } |
| 219 | sampleCoords = "snappedCoords"; |
| 220 | } |
| 221 | |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 222 | fragBuilder->codeAppendf("half4 planes[%d];", numPlanes); |
| 223 | for (int i = 0; i < numPlanes; ++i) { |
Brian Salomon | b9b1373 | 2020-06-25 11:13:49 -0400 | [diff] [blame] | 224 | SkString tempVar = this->invokeChild(i, args, sampleCoords); |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 225 | fragBuilder->codeAppendf("planes[%d] = %s;", i, tempVar.c_str()); |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 226 | } |
| 227 | |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 228 | bool hasAlpha = yuvEffect.fYUVAIndices[3].fIndex >= 0; |
| 229 | SkString rgba[4]; |
| 230 | rgba[3] = "1"; |
| 231 | for (int i = 0; i < (hasAlpha ? 4 : 3); ++i) { |
| 232 | auto info = yuvEffect.fYUVAIndices[i]; |
| 233 | auto letter = "rgba"[static_cast<int>(info.fChannel)]; |
| 234 | rgba[i].printf("planes[%d].%c", info.fIndex, letter); |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 235 | } |
| 236 | |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 237 | fragBuilder->codeAppendf("half4 color = half4(%s, %s, %s, %s);", |
| 238 | rgba[0].c_str(), rgba[1].c_str(), rgba[2].c_str(), rgba[3].c_str()); |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 239 | |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 240 | if (kIdentity_SkYUVColorSpace != yuvEffect.fYUVColorSpace) { |
Ethan Nicholas | 16464c3 | 2020-04-06 13:53:05 -0400 | [diff] [blame] | 241 | fColorSpaceMatrixVar = args.fUniformHandler->addUniform(&yuvEffect, |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 242 | kFragment_GrShaderFlag, kHalf3x3_GrSLType, "colorSpaceMatrix"); |
Ethan Nicholas | 16464c3 | 2020-04-06 13:53:05 -0400 | [diff] [blame] | 243 | fColorSpaceTranslateVar = args.fUniformHandler->addUniform(&yuvEffect, |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 244 | kFragment_GrShaderFlag, kHalf3_GrSLType, "colorSpaceTranslate"); |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 245 | fragBuilder->codeAppendf( |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 246 | "color.rgb = saturate(color.rgb * %s + %s);", |
| 247 | args.fUniformHandler->getUniformCStr(fColorSpaceMatrixVar), |
| 248 | args.fUniformHandler->getUniformCStr(fColorSpaceTranslateVar)); |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 249 | } |
| 250 | |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 251 | if (hasAlpha) { |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 252 | // premultiply alpha |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 253 | fragBuilder->codeAppendf("color.rgb *= color.a;"); |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 254 | } |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 255 | fragBuilder->codeAppendf("%s = color;", args.fOutputColor); |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | private: |
| 259 | void onSetData(const GrGLSLProgramDataManager& pdman, |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 260 | const GrFragmentProcessor& proc) override { |
| 261 | const GrYUVtoRGBEffect& yuvEffect = proc.cast<GrYUVtoRGBEffect>(); |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 262 | |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 263 | if (yuvEffect.fYUVColorSpace != kIdentity_SkYUVColorSpace) { |
Brian Osman | 904cac8 | 2019-10-30 16:57:14 -0400 | [diff] [blame] | 264 | SkASSERT(fColorSpaceMatrixVar.isValid()); |
| 265 | float yuvM[20]; |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 266 | SkColorMatrix_YUV2RGB(yuvEffect.fYUVColorSpace, yuvM); |
| 267 | // We drop the fourth column entirely since the transformation |
| 268 | // should not depend on alpha. The fifth column is sent as a separate |
| 269 | // vector. The fourth row is also dropped entirely because alpha should |
| 270 | // never be modified. |
| 271 | SkASSERT(yuvM[3] == 0 && yuvM[8] == 0 && yuvM[13] == 0 && yuvM[18] == 1); |
| 272 | SkASSERT(yuvM[15] == 0 && yuvM[16] == 0 && yuvM[17] == 0 && yuvM[19] == 0); |
| 273 | float mtx[9] = { |
| 274 | yuvM[ 0], yuvM[ 1], yuvM[ 2], |
| 275 | yuvM[ 5], yuvM[ 6], yuvM[ 7], |
| 276 | yuvM[10], yuvM[11], yuvM[12], |
Brian Osman | 904cac8 | 2019-10-30 16:57:14 -0400 | [diff] [blame] | 277 | }; |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 278 | float v[3] = {yuvM[4], yuvM[9], yuvM[14]}; |
| 279 | pdman.setMatrix3f(fColorSpaceMatrixVar, mtx); |
| 280 | pdman.set3fv(fColorSpaceTranslateVar, 1, v); |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | |
| 284 | UniformHandle fColorSpaceMatrixVar; |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 285 | UniformHandle fColorSpaceTranslateVar; |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 286 | }; |
| 287 | |
| 288 | return new GrGLSLYUVtoRGBEffect; |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 289 | } |
| 290 | void GrYUVtoRGBEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
| 291 | GrProcessorKeyBuilder* b) const { |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 292 | uint32_t packed = 0; |
| 293 | for (int i = 0; i < 4; ++i) { |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 294 | if (fYUVAIndices[i].fIndex < 0) { |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 295 | continue; |
| 296 | } |
| 297 | |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 298 | uint8_t index = fYUVAIndices[i].fIndex; |
| 299 | uint8_t chann = static_cast<int>(fYUVAIndices[i].fChannel); |
Robert Phillips | 6ba8c83 | 2018-10-10 11:43:01 -0400 | [diff] [blame] | 300 | |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 301 | SkASSERT(index < 4 && chann < 4); |
| 302 | |
| 303 | packed |= (index | (chann << 2)) << (i * 4); |
| 304 | } |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 305 | if (fYUVColorSpace == kIdentity_SkYUVColorSpace) { |
Brian Salomon | b9b1373 | 2020-06-25 11:13:49 -0400 | [diff] [blame] | 306 | packed |= 1 << 16; |
| 307 | } |
| 308 | if (fSnap[0]) { |
| 309 | packed |= 1 << 17; |
| 310 | } |
| 311 | if (fSnap[1]) { |
| 312 | packed |= 1 << 18; |
Robert Phillips | b651aac | 2019-03-15 12:18:49 -0400 | [diff] [blame] | 313 | } |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 314 | b->add32(packed); |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 315 | } |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 316 | |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 317 | bool GrYUVtoRGBEffect::onIsEqual(const GrFragmentProcessor& other) const { |
| 318 | const GrYUVtoRGBEffect& that = other.cast<GrYUVtoRGBEffect>(); |
Robert Phillips | 6ba8c83 | 2018-10-10 11:43:01 -0400 | [diff] [blame] | 319 | |
Brian Salomon | b9b1373 | 2020-06-25 11:13:49 -0400 | [diff] [blame] | 320 | return std::equal(fYUVAIndices, fYUVAIndices + 4, that.fYUVAIndices) && |
| 321 | std::equal(fSnap, fSnap + 2, that.fSnap) && |
| 322 | fYUVColorSpace == that.fYUVColorSpace; |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 323 | } |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 324 | |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 325 | GrYUVtoRGBEffect::GrYUVtoRGBEffect(const GrYUVtoRGBEffect& src) |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 326 | : GrFragmentProcessor(kGrYUVtoRGBEffect_ClassID, src.optimizationFlags()) |
Robert Phillips | b651aac | 2019-03-15 12:18:49 -0400 | [diff] [blame] | 327 | , fYUVColorSpace(src.fYUVColorSpace) { |
Michael Ludwig | 9aba625 | 2020-06-22 14:46:36 -0400 | [diff] [blame] | 328 | this->cloneAndRegisterAllChildProcessors(src); |
Michael Ludwig | fbe2859 | 2020-06-26 16:02:15 -0400 | [diff] [blame] | 329 | if (src.fSnap[0] || src.fSnap[1]) { |
| 330 | this->setUsesSampleCoordsDirectly(); |
| 331 | } |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 332 | std::copy_n(src.fYUVAIndices, this->numChildProcessors(), fYUVAIndices); |
Brian Salomon | b9b1373 | 2020-06-25 11:13:49 -0400 | [diff] [blame] | 333 | std::copy_n(src.fSnap, 2, fSnap); |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 334 | } |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 335 | |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 336 | std::unique_ptr<GrFragmentProcessor> GrYUVtoRGBEffect::clone() const { |
| 337 | return std::unique_ptr<GrFragmentProcessor>(new GrYUVtoRGBEffect(*this)); |
| 338 | } |