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 Salomon | 0857bef | 2021-01-13 15:54:04 -0500 | [diff] [blame] | 10 | #include "include/core/SkYUVAInfo.h" |
Brian Osman | 904cac8 | 2019-10-30 16:57:14 -0400 | [diff] [blame] | 11 | #include "src/core/SkYUVMath.h" |
Greg Daniel | 456f9b5 | 2020-03-05 19:14:18 +0000 | [diff] [blame] | 12 | #include "src/gpu/GrTexture.h" |
Brian Salomon | 0857bef | 2021-01-13 15:54:04 -0500 | [diff] [blame] | 13 | #include "src/gpu/GrYUVATextureProxies.h" |
Brian Salomon | b9b1373 | 2020-06-25 11:13:49 -0400 | [diff] [blame] | 14 | #include "src/gpu/effects/GrMatrixEffect.h" |
Robert Phillips | 550de7f | 2021-07-06 16:28:52 -0400 | [diff] [blame] | 15 | #include "src/gpu/effects/GrTextureEffect.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
| 17 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 18 | #include "src/gpu/glsl/GrGLSLProgramBuilder.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "src/sksl/SkSLUtil.h" |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 20 | |
Brian Salomon | 0857bef | 2021-01-13 15:54:04 -0500 | [diff] [blame] | 21 | static void border_colors(const GrYUVATextureProxies& yuvaProxies, float planeBorders[4][4]) { |
Brian Salomon | d71548a | 2020-02-29 19:43:30 -0500 | [diff] [blame] | 22 | float m[20]; |
Brian Salomon | 0857bef | 2021-01-13 15:54:04 -0500 | [diff] [blame] | 23 | SkColorMatrix_RGB2YUV(yuvaProxies.yuvaInfo().yuvColorSpace(), m); |
| 24 | for (int i = 0; i < SkYUVAInfo::kYUVAChannelCount; ++i) { |
| 25 | auto [plane, channel] = yuvaProxies.yuvaLocations()[i]; |
Brian Salomon | 0c0b5a6 | 2021-01-11 14:40:44 -0500 | [diff] [blame] | 26 | if (plane == -1) { |
Brian Salomon | 0857bef | 2021-01-13 15:54:04 -0500 | [diff] [blame] | 27 | return; |
Brian Salomon | d71548a | 2020-02-29 19:43:30 -0500 | [diff] [blame] | 28 | } |
Brian Salomon | 0c0b5a6 | 2021-01-11 14:40:44 -0500 | [diff] [blame] | 29 | auto c = static_cast<int>(channel); |
| 30 | planeBorders[plane][c] = m[i*5 + 4]; |
Brian Salomon | d71548a | 2020-02-29 19:43:30 -0500 | [diff] [blame] | 31 | } |
| 32 | } |
| 33 | |
Brian Salomon | 0857bef | 2021-01-13 15:54:04 -0500 | [diff] [blame] | 34 | std::unique_ptr<GrFragmentProcessor> GrYUVtoRGBEffect::Make(const GrYUVATextureProxies& yuvaProxies, |
| 35 | GrSamplerState samplerState, |
| 36 | const GrCaps& caps, |
| 37 | const SkMatrix& localMatrix, |
| 38 | const SkRect* subset, |
| 39 | const SkRect* domain) { |
Brian Salomon | 7b0c8df | 2021-05-24 10:51:47 -0400 | [diff] [blame] | 40 | SkASSERT(!subset || SkRect::Make(yuvaProxies.yuvaInfo().dimensions()).contains(*subset)); |
| 41 | |
Brian Salomon | 0857bef | 2021-01-13 15:54:04 -0500 | [diff] [blame] | 42 | int numPlanes = yuvaProxies.yuvaInfo().numPlanes(); |
| 43 | if (!yuvaProxies.isValid()) { |
| 44 | return nullptr; |
| 45 | } |
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) { |
Brian Salomon | 0857bef | 2021-01-13 15:54:04 -0500 | [diff] [blame] | 51 | border_colors(yuvaProxies, planeBorders); |
Brian Salomon | d71548a | 2020-02-29 19:43:30 -0500 | [diff] [blame] | 52 | } |
| 53 | |
Brian Salomon | b9b1373 | 2020-06-25 11:13:49 -0400 | [diff] [blame] | 54 | bool snap[2] = {false, false}; |
Brian Salomon | 0857bef | 2021-01-13 15:54:04 -0500 | [diff] [blame] | 55 | std::unique_ptr<GrFragmentProcessor> planeFPs[SkYUVAInfo::kMaxPlanes]; |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 56 | for (int i = 0; i < numPlanes; ++i) { |
Brian Salomon | 7b0c8df | 2021-05-24 10:51:47 -0400 | [diff] [blame] | 57 | bool useSubset = SkToBool(subset); |
Brian Salomon | 0857bef | 2021-01-13 15:54:04 -0500 | [diff] [blame] | 58 | GrSurfaceProxyView view = yuvaProxies.makeView(i); |
| 59 | SkMatrix planeMatrix = yuvaProxies.yuvaInfo().originMatrix(); |
| 60 | // The returned matrix is a view matrix but we need a local matrix. |
| 61 | SkAssertResult(planeMatrix.invert(&planeMatrix)); |
Brian Salomon | b9b1373 | 2020-06-25 11:13:49 -0400 | [diff] [blame] | 62 | SkRect planeSubset; |
Brian Salomon | 0ea3307 | 2020-07-14 10:43:42 -0400 | [diff] [blame] | 63 | SkRect planeDomain; |
Brian Salomon | a3b02f5 | 2020-07-15 16:02:01 -0400 | [diff] [blame] | 64 | bool makeLinearWithSnap = false; |
Brian Salomon | 0857bef | 2021-01-13 15:54:04 -0500 | [diff] [blame] | 65 | auto [ssx, ssy] = yuvaProxies.yuvaInfo().planeSubsamplingFactors(i); |
| 66 | SkASSERT(ssx > 0 && ssx <= 4); |
| 67 | SkASSERT(ssy > 0 && ssy <= 2); |
| 68 | float scaleX = 1.f; |
| 69 | float scaleY = 1.f; |
| 70 | if (ssx > 1 || ssy > 1) { |
Brian Salomon | 0857bef | 2021-01-13 15:54:04 -0500 | [diff] [blame] | 71 | scaleX = 1.f/ssx; |
| 72 | scaleY = 1.f/ssy; |
| 73 | // We would want to add a translation to this matrix to handle other sitings. |
| 74 | SkASSERT(yuvaProxies.yuvaInfo().sitingX() == SkYUVAInfo::Siting::kCentered); |
| 75 | SkASSERT(yuvaProxies.yuvaInfo().sitingY() == SkYUVAInfo::Siting::kCentered); |
| 76 | planeMatrix.postConcat(SkMatrix::Scale(scaleX, scaleY)); |
Brian Salomon | 0ea3307 | 2020-07-14 10:43:42 -0400 | [diff] [blame] | 77 | if (subset) { |
Brian Salomon | 0857bef | 2021-01-13 15:54:04 -0500 | [diff] [blame] | 78 | planeSubset = {subset->fLeft *scaleX, |
| 79 | subset->fTop *scaleY, |
| 80 | subset->fRight *scaleX, |
| 81 | subset->fBottom*scaleY}; |
Brian Salomon | 7b0c8df | 2021-05-24 10:51:47 -0400 | [diff] [blame] | 82 | } else { |
| 83 | planeSubset = SkRect::Make(view.dimensions()); |
Brian Salomon | 0ea3307 | 2020-07-14 10:43:42 -0400 | [diff] [blame] | 84 | } |
| 85 | if (domain) { |
Brian Salomon | 0857bef | 2021-01-13 15:54:04 -0500 | [diff] [blame] | 86 | planeDomain = {domain->fLeft *scaleX, |
| 87 | domain->fTop *scaleY, |
| 88 | domain->fRight *scaleX, |
| 89 | domain->fBottom*scaleY}; |
Brian Salomon | 0ea3307 | 2020-07-14 10:43:42 -0400 | [diff] [blame] | 90 | } |
Brian Salomon | 7b0c8df | 2021-05-24 10:51:47 -0400 | [diff] [blame] | 91 | // If the image is not a multiple of the subsampling then the subsampled plane needs to |
| 92 | // be tiled at less than its full width/height. This only matters when the mode is not |
| 93 | // clamp. |
| 94 | if (samplerState.wrapModeX() != GrSamplerState::WrapMode::kClamp) { |
| 95 | int dx = (ssx*view.width() - yuvaProxies.yuvaInfo().width()); |
| 96 | float maxRight = view.width() - dx*scaleX; |
| 97 | if (planeSubset.fRight > maxRight) { |
| 98 | planeSubset.fRight = maxRight; |
| 99 | useSubset = true; |
| 100 | } |
| 101 | } |
| 102 | if (samplerState.wrapModeY() != GrSamplerState::WrapMode::kClamp) { |
| 103 | int dy = (ssy*view.height() - yuvaProxies.yuvaInfo().height()); |
| 104 | float maxBottom = view.height() - dy*scaleY; |
| 105 | if (planeSubset.fBottom > maxBottom) { |
| 106 | planeSubset.fBottom = maxBottom; |
| 107 | useSubset = true; |
| 108 | } |
| 109 | } |
Brian Salomon | a3b02f5 | 2020-07-15 16:02:01 -0400 | [diff] [blame] | 110 | // This promotion of nearest to linear filtering for UV planes exists to mimic |
| 111 | // libjpeg[-turbo]'s do_fancy_upsampling option. We will filter the subsampled plane, |
| 112 | // however we want to filter at a fixed point for each logical image pixel to simulate |
| 113 | // nearest neighbor. |
Brian Salomon | b9b1373 | 2020-06-25 11:13:49 -0400 | [diff] [blame] | 114 | if (samplerState.filter() == GrSamplerState::Filter::kNearest) { |
Brian Salomon | 0857bef | 2021-01-13 15:54:04 -0500 | [diff] [blame] | 115 | bool snapX = (ssx != 1), |
| 116 | snapY = (ssy != 1); |
Brian Salomon | a3b02f5 | 2020-07-15 16:02:01 -0400 | [diff] [blame] | 117 | makeLinearWithSnap = snapX || snapY; |
Brian Salomon | b9b1373 | 2020-06-25 11:13:49 -0400 | [diff] [blame] | 118 | snap[0] |= snapX; |
| 119 | snap[1] |= snapY; |
Brian Salomon | 0ea3307 | 2020-07-14 10:43:42 -0400 | [diff] [blame] | 120 | if (domain) { |
| 121 | // The outer YUVToRGB effect will ensure sampling happens at pixel centers |
| 122 | // within this plane. |
| 123 | planeDomain = {std::floor(planeDomain.fLeft) + 0.5f, |
| 124 | std::floor(planeDomain.fTop) + 0.5f, |
| 125 | std::floor(planeDomain.fRight) + 0.5f, |
| 126 | std::floor(planeDomain.fBottom) + 0.5f}; |
| 127 | } |
Brian Salomon | b9b1373 | 2020-06-25 11:13:49 -0400 | [diff] [blame] | 128 | } |
Brian Salomon | 0ea3307 | 2020-07-14 10:43:42 -0400 | [diff] [blame] | 129 | } else { |
Brian Salomon | d71548a | 2020-02-29 19:43:30 -0500 | [diff] [blame] | 130 | if (subset) { |
Brian Salomon | 0ea3307 | 2020-07-14 10:43:42 -0400 | [diff] [blame] | 131 | planeSubset = *subset; |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 132 | } |
Brian Salomon | 0ea3307 | 2020-07-14 10:43:42 -0400 | [diff] [blame] | 133 | if (domain) { |
| 134 | planeDomain = *domain; |
| 135 | } |
Brian Salomon | 8a99a41 | 2019-12-04 11:05:35 -0500 | [diff] [blame] | 136 | } |
Brian Salomon | 7b0c8df | 2021-05-24 10:51:47 -0400 | [diff] [blame] | 137 | if (useSubset) { |
Brian Salomon | a3b02f5 | 2020-07-15 16:02:01 -0400 | [diff] [blame] | 138 | if (makeLinearWithSnap) { |
Brian Salomon | b9b1373 | 2020-06-25 11:13:49 -0400 | [diff] [blame] | 139 | // 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] | 140 | // emulating do_fancy_upsampling using linear filtering but snapping look ups to the |
| 141 | // y-plane pixel centers. Consider a logical image pixel at the edge of the subset. |
| 142 | // When computing the logical pixel color value we should use a 50/50 blend of two |
| 143 | // values from the subsampled plane. Depending on where the subset edge falls in |
| 144 | // actual subsampled plane, one of those values may come from outside the subset. |
| 145 | // Hence, we use this custom inset factory which applies the wrap mode to |
| 146 | // planeSubset but allows linear filtering to read pixels from the plane that are |
| 147 | // just outside planeSubset. |
Brian Salomon | 0ea3307 | 2020-07-14 10:43:42 -0400 | [diff] [blame] | 148 | SkRect* domainRect = domain ? &planeDomain : nullptr; |
Brian Salomon | 0857bef | 2021-01-13 15:54:04 -0500 | [diff] [blame] | 149 | planeFPs[i] = GrTextureEffect::MakeCustomLinearFilterInset(std::move(view), |
| 150 | kUnknown_SkAlphaType, |
| 151 | planeMatrix, |
| 152 | samplerState.wrapModeX(), |
| 153 | samplerState.wrapModeY(), |
| 154 | planeSubset, |
| 155 | domainRect, |
| 156 | {scaleX/2.f, scaleY/2.f}, |
| 157 | caps, |
| 158 | planeBorders[i]); |
Brian Salomon | 0ea3307 | 2020-07-14 10:43:42 -0400 | [diff] [blame] | 159 | } else if (domain) { |
Brian Salomon | 0857bef | 2021-01-13 15:54:04 -0500 | [diff] [blame] | 160 | planeFPs[i] = GrTextureEffect::MakeSubset(std::move(view), |
| 161 | kUnknown_SkAlphaType, |
| 162 | planeMatrix, |
| 163 | samplerState, |
| 164 | planeSubset, |
| 165 | planeDomain, |
| 166 | caps, |
| 167 | planeBorders[i]); |
Brian Salomon | b9b1373 | 2020-06-25 11:13:49 -0400 | [diff] [blame] | 168 | } else { |
Brian Salomon | 0857bef | 2021-01-13 15:54:04 -0500 | [diff] [blame] | 169 | planeFPs[i] = GrTextureEffect::MakeSubset(std::move(view), |
| 170 | kUnknown_SkAlphaType, |
| 171 | planeMatrix, |
| 172 | samplerState, |
| 173 | planeSubset, |
| 174 | caps, |
| 175 | planeBorders[i]); |
Brian Salomon | b9b1373 | 2020-06-25 11:13:49 -0400 | [diff] [blame] | 176 | } |
Brian Salomon | ca6b2f4 | 2020-01-24 11:31:21 -0500 | [diff] [blame] | 177 | } else { |
Brian Salomon | b9b1373 | 2020-06-25 11:13:49 -0400 | [diff] [blame] | 178 | GrSamplerState planeSampler = samplerState; |
Brian Salomon | a3b02f5 | 2020-07-15 16:02:01 -0400 | [diff] [blame] | 179 | if (makeLinearWithSnap) { |
| 180 | planeSampler.setFilterMode(GrSamplerState::Filter::kLinear); |
Brian Salomon | b9b1373 | 2020-06-25 11:13:49 -0400 | [diff] [blame] | 181 | } |
Brian Salomon | 0857bef | 2021-01-13 15:54:04 -0500 | [diff] [blame] | 182 | planeFPs[i] = GrTextureEffect::Make(std::move(view), |
| 183 | kUnknown_SkAlphaType, |
| 184 | planeMatrix, |
| 185 | planeSampler, |
| 186 | caps, |
| 187 | planeBorders[i]); |
Brian Salomon | 8a99a41 | 2019-12-04 11:05:35 -0500 | [diff] [blame] | 188 | } |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 189 | } |
Brian Salomon | 0857bef | 2021-01-13 15:54:04 -0500 | [diff] [blame] | 190 | std::unique_ptr<GrFragmentProcessor> fp( |
| 191 | new GrYUVtoRGBEffect(planeFPs, |
| 192 | numPlanes, |
| 193 | yuvaProxies.yuvaLocations(), |
| 194 | snap, |
| 195 | yuvaProxies.yuvaInfo().yuvColorSpace())); |
Brian Salomon | b9b1373 | 2020-06-25 11:13:49 -0400 | [diff] [blame] | 196 | return GrMatrixEffect::Make(localMatrix, std::move(fp)); |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 197 | } |
| 198 | |
Brian Salomon | 0c0b5a6 | 2021-01-11 14:40:44 -0500 | [diff] [blame] | 199 | static SkAlphaType alpha_type(const SkYUVAInfo::YUVALocations locations) { |
| 200 | return locations[SkYUVAInfo::YUVAChannels::kA].fPlane >= 0 ? kPremul_SkAlphaType |
| 201 | : kOpaque_SkAlphaType; |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 202 | } |
| 203 | |
Brian Salomon | b9b1373 | 2020-06-25 11:13:49 -0400 | [diff] [blame] | 204 | GrYUVtoRGBEffect::GrYUVtoRGBEffect(std::unique_ptr<GrFragmentProcessor> planeFPs[4], |
| 205 | int numPlanes, |
Brian Salomon | 0c0b5a6 | 2021-01-11 14:40:44 -0500 | [diff] [blame] | 206 | const SkYUVAInfo::YUVALocations& locations, |
Brian Salomon | b9b1373 | 2020-06-25 11:13:49 -0400 | [diff] [blame] | 207 | const bool snap[2], |
| 208 | SkYUVColorSpace yuvColorSpace) |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 209 | : GrFragmentProcessor(kGrYUVtoRGBEffect_ClassID, |
Brian Salomon | 0c0b5a6 | 2021-01-11 14:40:44 -0500 | [diff] [blame] | 210 | ModulateForClampedSamplerOptFlags(alpha_type(locations))) |
| 211 | , fLocations(locations) |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 212 | , fYUVColorSpace(yuvColorSpace) { |
Brian Salomon | b9b1373 | 2020-06-25 11:13:49 -0400 | [diff] [blame] | 213 | std::copy_n(snap, 2, fSnap); |
| 214 | |
| 215 | if (fSnap[0] || fSnap[1]) { |
| 216 | // 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] | 217 | this->setUsesSampleCoordsDirectly(); |
Brian Salomon | b9b1373 | 2020-06-25 11:13:49 -0400 | [diff] [blame] | 218 | for (int i = 0; i < numPlanes; ++i) { |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 219 | this->registerChild(std::move(planeFPs[i]), SkSL::SampleUsage::Explicit()); |
Brian Salomon | b9b1373 | 2020-06-25 11:13:49 -0400 | [diff] [blame] | 220 | } |
| 221 | } else { |
| 222 | for (int i = 0; i < numPlanes; ++i) { |
| 223 | this->registerChild(std::move(planeFPs[i])); |
| 224 | } |
| 225 | } |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 226 | } |
Robert Phillips | ba5c439 | 2018-07-25 12:37:14 -0400 | [diff] [blame] | 227 | |
John Stiles | 8d9bf64 | 2020-08-12 15:07:45 -0400 | [diff] [blame] | 228 | #if GR_TEST_UTILS |
John Stiles | cab5886 | 2020-08-12 15:47:06 -0400 | [diff] [blame] | 229 | SkString GrYUVtoRGBEffect::onDumpInfo() const { |
| 230 | SkString str("("); |
Brian Salomon | 0c0b5a6 | 2021-01-11 14:40:44 -0500 | [diff] [blame] | 231 | for (int i = 0; i < SkYUVAInfo::kYUVAChannelCount; ++i) { |
| 232 | str.appendf("Locations[%d]=%d %d, ", |
| 233 | i, fLocations[i].fPlane, static_cast<int>(fLocations[i].fChannel)); |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 234 | } |
John Stiles | ba1879d | 2020-08-11 13:58:32 -0400 | [diff] [blame] | 235 | str.appendf("YUVColorSpace=%d, snap=(%d, %d))", |
| 236 | static_cast<int>(fYUVColorSpace), fSnap[0], fSnap[1]); |
Robert Phillips | ba5c439 | 2018-07-25 12:37:14 -0400 | [diff] [blame] | 237 | return str; |
| 238 | } |
Brian Osman | 9a390ac | 2018-11-12 09:47:48 -0500 | [diff] [blame] | 239 | #endif |
| 240 | |
Brian Salomon | 3176e86 | 2021-08-09 11:23:04 -0400 | [diff] [blame] | 241 | std::unique_ptr<GrFragmentProcessor::ProgramImpl> GrYUVtoRGBEffect::onMakeProgramImpl() const { |
| 242 | class GrGLSLYUVtoRGBEffect : public ProgramImpl { |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 243 | public: |
| 244 | GrGLSLYUVtoRGBEffect() {} |
| 245 | |
| 246 | void emitCode(EmitArgs& args) override { |
| 247 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 248 | const GrYUVtoRGBEffect& yuvEffect = args.fFp.cast<GrYUVtoRGBEffect>(); |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 249 | |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 250 | int numPlanes = yuvEffect.numChildProcessors(); |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 251 | |
Brian Salomon | b9b1373 | 2020-06-25 11:13:49 -0400 | [diff] [blame] | 252 | const char* sampleCoords = ""; |
| 253 | if (yuvEffect.fSnap[0] || yuvEffect.fSnap[1]) { |
| 254 | fragBuilder->codeAppendf("float2 snappedCoords = %s;", args.fSampleCoord); |
| 255 | if (yuvEffect.fSnap[0]) { |
| 256 | fragBuilder->codeAppend("snappedCoords.x = floor(snappedCoords.x) + 0.5;"); |
| 257 | } |
| 258 | if (yuvEffect.fSnap[1]) { |
| 259 | fragBuilder->codeAppend("snappedCoords.y = floor(snappedCoords.y) + 0.5;"); |
| 260 | } |
| 261 | sampleCoords = "snappedCoords"; |
| 262 | } |
| 263 | |
John Stiles | 95eca3e | 2021-03-18 16:00:22 -0400 | [diff] [blame] | 264 | fragBuilder->codeAppendf("half4 color;"); |
| 265 | const bool hasAlpha = yuvEffect.fLocations[SkYUVAInfo::YUVAChannels::kA].fPlane >= 0; |
| 266 | |
| 267 | for (int planeIdx = 0; planeIdx < numPlanes; ++planeIdx) { |
| 268 | std::string colorChannel; |
| 269 | std::string planeChannel; |
| 270 | for (int locIdx = 0; locIdx < (hasAlpha ? 4 : 3); ++locIdx) { |
| 271 | auto [yuvPlane, yuvChannel] = yuvEffect.fLocations[locIdx]; |
| 272 | if (yuvPlane == planeIdx) { |
| 273 | colorChannel.push_back("rgba"[locIdx]); |
| 274 | planeChannel.push_back("rgba"[static_cast<int>(yuvChannel)]); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | SkASSERT(colorChannel.size() == planeChannel.size()); |
| 279 | if (!colorChannel.empty()) { |
| 280 | fragBuilder->codeAppendf( |
| 281 | "color.%s = (%s).%s;", |
| 282 | colorChannel.c_str(), |
| 283 | this->invokeChild(planeIdx, args, sampleCoords).c_str(), |
| 284 | planeChannel.c_str()); |
| 285 | } |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 286 | } |
| 287 | |
John Stiles | 95eca3e | 2021-03-18 16:00:22 -0400 | [diff] [blame] | 288 | if (!hasAlpha) { |
| 289 | fragBuilder->codeAppendf("color.a = 1;"); |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 290 | } |
| 291 | |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 292 | if (kIdentity_SkYUVColorSpace != yuvEffect.fYUVColorSpace) { |
Ethan Nicholas | 16464c3 | 2020-04-06 13:53:05 -0400 | [diff] [blame] | 293 | fColorSpaceMatrixVar = args.fUniformHandler->addUniform(&yuvEffect, |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 294 | kFragment_GrShaderFlag, kHalf3x3_GrSLType, "colorSpaceMatrix"); |
Ethan Nicholas | 16464c3 | 2020-04-06 13:53:05 -0400 | [diff] [blame] | 295 | fColorSpaceTranslateVar = args.fUniformHandler->addUniform(&yuvEffect, |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 296 | kFragment_GrShaderFlag, kHalf3_GrSLType, "colorSpaceTranslate"); |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 297 | fragBuilder->codeAppendf( |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 298 | "color.rgb = saturate(color.rgb * %s + %s);", |
| 299 | args.fUniformHandler->getUniformCStr(fColorSpaceMatrixVar), |
| 300 | args.fUniformHandler->getUniformCStr(fColorSpaceTranslateVar)); |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 301 | } |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 302 | if (hasAlpha) { |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 303 | // premultiply alpha |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 304 | fragBuilder->codeAppendf("color.rgb *= color.a;"); |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 305 | } |
John Stiles | 5e7c040 | 2020-10-20 19:43:18 -0400 | [diff] [blame] | 306 | fragBuilder->codeAppendf("return color;"); |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | private: |
| 310 | void onSetData(const GrGLSLProgramDataManager& pdman, |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 311 | const GrFragmentProcessor& proc) override { |
| 312 | const GrYUVtoRGBEffect& yuvEffect = proc.cast<GrYUVtoRGBEffect>(); |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 313 | |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 314 | if (yuvEffect.fYUVColorSpace != kIdentity_SkYUVColorSpace) { |
Brian Osman | 904cac8 | 2019-10-30 16:57:14 -0400 | [diff] [blame] | 315 | SkASSERT(fColorSpaceMatrixVar.isValid()); |
| 316 | float yuvM[20]; |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 317 | SkColorMatrix_YUV2RGB(yuvEffect.fYUVColorSpace, yuvM); |
| 318 | // We drop the fourth column entirely since the transformation |
| 319 | // should not depend on alpha. The fifth column is sent as a separate |
| 320 | // vector. The fourth row is also dropped entirely because alpha should |
| 321 | // never be modified. |
| 322 | SkASSERT(yuvM[3] == 0 && yuvM[8] == 0 && yuvM[13] == 0 && yuvM[18] == 1); |
| 323 | SkASSERT(yuvM[15] == 0 && yuvM[16] == 0 && yuvM[17] == 0 && yuvM[19] == 0); |
| 324 | float mtx[9] = { |
| 325 | yuvM[ 0], yuvM[ 1], yuvM[ 2], |
| 326 | yuvM[ 5], yuvM[ 6], yuvM[ 7], |
| 327 | yuvM[10], yuvM[11], yuvM[12], |
Brian Osman | 904cac8 | 2019-10-30 16:57:14 -0400 | [diff] [blame] | 328 | }; |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 329 | float v[3] = {yuvM[4], yuvM[9], yuvM[14]}; |
| 330 | pdman.setMatrix3f(fColorSpaceMatrixVar, mtx); |
| 331 | pdman.set3fv(fColorSpaceTranslateVar, 1, v); |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | |
| 335 | UniformHandle fColorSpaceMatrixVar; |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 336 | UniformHandle fColorSpaceTranslateVar; |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 337 | }; |
| 338 | |
Brian Salomon | 18ab203 | 2021-02-23 10:07:05 -0500 | [diff] [blame] | 339 | return std::make_unique<GrGLSLYUVtoRGBEffect>(); |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 340 | } |
Brian Salomon | 13b2873 | 2021-08-06 15:33:58 -0400 | [diff] [blame] | 341 | void GrYUVtoRGBEffect::onAddToKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const { |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 342 | uint32_t packed = 0; |
Brian Salomon | 0c0b5a6 | 2021-01-11 14:40:44 -0500 | [diff] [blame] | 343 | int i = 0; |
| 344 | for (auto [plane, channel] : fLocations) { |
| 345 | if (plane < 0) { |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 346 | continue; |
| 347 | } |
| 348 | |
Brian Salomon | 0c0b5a6 | 2021-01-11 14:40:44 -0500 | [diff] [blame] | 349 | uint8_t chann = static_cast<int>(channel); |
Robert Phillips | 6ba8c83 | 2018-10-10 11:43:01 -0400 | [diff] [blame] | 350 | |
Brian Salomon | 0c0b5a6 | 2021-01-11 14:40:44 -0500 | [diff] [blame] | 351 | SkASSERT(plane < 4 && chann < 4); |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 352 | |
Brian Salomon | 0c0b5a6 | 2021-01-11 14:40:44 -0500 | [diff] [blame] | 353 | packed |= (plane | (chann << 2)) << (i++ * 4); |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 354 | } |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 355 | if (fYUVColorSpace == kIdentity_SkYUVColorSpace) { |
Brian Salomon | b9b1373 | 2020-06-25 11:13:49 -0400 | [diff] [blame] | 356 | packed |= 1 << 16; |
| 357 | } |
| 358 | if (fSnap[0]) { |
| 359 | packed |= 1 << 17; |
| 360 | } |
| 361 | if (fSnap[1]) { |
| 362 | packed |= 1 << 18; |
Robert Phillips | b651aac | 2019-03-15 12:18:49 -0400 | [diff] [blame] | 363 | } |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 364 | b->add32(packed); |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 365 | } |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 366 | |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 367 | bool GrYUVtoRGBEffect::onIsEqual(const GrFragmentProcessor& other) const { |
| 368 | const GrYUVtoRGBEffect& that = other.cast<GrYUVtoRGBEffect>(); |
Robert Phillips | 6ba8c83 | 2018-10-10 11:43:01 -0400 | [diff] [blame] | 369 | |
Brian Salomon | 0c0b5a6 | 2021-01-11 14:40:44 -0500 | [diff] [blame] | 370 | return fLocations == that.fLocations && |
Brian Salomon | b9b1373 | 2020-06-25 11:13:49 -0400 | [diff] [blame] | 371 | std::equal(fSnap, fSnap + 2, that.fSnap) && |
| 372 | fYUVColorSpace == that.fYUVColorSpace; |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 373 | } |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 374 | |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 375 | GrYUVtoRGBEffect::GrYUVtoRGBEffect(const GrYUVtoRGBEffect& src) |
John Stiles | 307f8f5 | 2021-08-09 15:36:59 -0400 | [diff] [blame^] | 376 | : GrFragmentProcessor(src) |
Brian Salomon | 0c0b5a6 | 2021-01-11 14:40:44 -0500 | [diff] [blame] | 377 | , fLocations((src.fLocations)) |
Robert Phillips | b651aac | 2019-03-15 12:18:49 -0400 | [diff] [blame] | 378 | , fYUVColorSpace(src.fYUVColorSpace) { |
Brian Salomon | b9b1373 | 2020-06-25 11:13:49 -0400 | [diff] [blame] | 379 | std::copy_n(src.fSnap, 2, fSnap); |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 380 | } |
Brian Salomon | fa9232c | 2019-12-04 16:09:04 -0500 | [diff] [blame] | 381 | |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 382 | std::unique_ptr<GrFragmentProcessor> GrYUVtoRGBEffect::clone() const { |
| 383 | return std::unique_ptr<GrFragmentProcessor>(new GrYUVtoRGBEffect(*this)); |
| 384 | } |