blob: 16aff67a9db957230a99e68283d708bbd4c3e84f [file] [log] [blame]
Ethan Nicholas7461a4a2017-12-21 14:18:01 -05001/*
Ethan Nicholas130fb3f2018-02-01 12:14:34 -05002 * Copyright 2018 Google Inc.
Ethan Nicholas7461a4a2017-12-21 14:18:01 -05003 *
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/effects/GrYUVtoRGBEffect.h"
Ethan Nicholas7461a4a2017-12-21 14:18:01 -05009
Brian Salomon0857bef2021-01-13 15:54:04 -050010#include "include/core/SkYUVAInfo.h"
Brian Osman904cac82019-10-30 16:57:14 -040011#include "src/core/SkYUVMath.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000012#include "src/gpu/GrTexture.h"
Brian Salomon0857bef2021-01-13 15:54:04 -050013#include "src/gpu/GrYUVATextureProxies.h"
Brian Salomonb9b13732020-06-25 11:13:49 -040014#include "src/gpu/effects/GrMatrixEffect.h"
Robert Phillips550de7f2021-07-06 16:28:52 -040015#include "src/gpu/effects/GrTextureEffect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
17#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
18#include "src/gpu/glsl/GrGLSLProgramBuilder.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/sksl/SkSLUtil.h"
Michael Ludwiga6a84002019-04-12 15:03:02 -040020
Brian Salomon0857bef2021-01-13 15:54:04 -050021static void border_colors(const GrYUVATextureProxies& yuvaProxies, float planeBorders[4][4]) {
Brian Salomond71548a2020-02-29 19:43:30 -050022 float m[20];
Brian Salomon0857bef2021-01-13 15:54:04 -050023 SkColorMatrix_RGB2YUV(yuvaProxies.yuvaInfo().yuvColorSpace(), m);
24 for (int i = 0; i < SkYUVAInfo::kYUVAChannelCount; ++i) {
25 auto [plane, channel] = yuvaProxies.yuvaLocations()[i];
Brian Salomon0c0b5a62021-01-11 14:40:44 -050026 if (plane == -1) {
Brian Salomon0857bef2021-01-13 15:54:04 -050027 return;
Brian Salomond71548a2020-02-29 19:43:30 -050028 }
Brian Salomon0c0b5a62021-01-11 14:40:44 -050029 auto c = static_cast<int>(channel);
30 planeBorders[plane][c] = m[i*5 + 4];
Brian Salomond71548a2020-02-29 19:43:30 -050031 }
32}
33
Brian Salomon0857bef2021-01-13 15:54:04 -050034std::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 Salomon7b0c8df2021-05-24 10:51:47 -040040 SkASSERT(!subset || SkRect::Make(yuvaProxies.yuvaInfo().dimensions()).contains(*subset));
41
Brian Salomon0857bef2021-01-13 15:54:04 -050042 int numPlanes = yuvaProxies.yuvaInfo().numPlanes();
43 if (!yuvaProxies.isValid()) {
44 return nullptr;
45 }
Robert Phillips94ade752018-10-09 12:32:31 -040046
Brian Salomond71548a2020-02-29 19:43:30 -050047 bool usesBorder = samplerState.wrapModeX() == GrSamplerState::WrapMode::kClampToBorder ||
48 samplerState.wrapModeY() == GrSamplerState::WrapMode::kClampToBorder;
49 float planeBorders[4][4] = {};
50 if (usesBorder) {
Brian Salomon0857bef2021-01-13 15:54:04 -050051 border_colors(yuvaProxies, planeBorders);
Brian Salomond71548a2020-02-29 19:43:30 -050052 }
53
Brian Salomonb9b13732020-06-25 11:13:49 -040054 bool snap[2] = {false, false};
Brian Salomon0857bef2021-01-13 15:54:04 -050055 std::unique_ptr<GrFragmentProcessor> planeFPs[SkYUVAInfo::kMaxPlanes];
Robert Phillips94ade752018-10-09 12:32:31 -040056 for (int i = 0; i < numPlanes; ++i) {
Brian Salomon7b0c8df2021-05-24 10:51:47 -040057 bool useSubset = SkToBool(subset);
Brian Salomon0857bef2021-01-13 15:54:04 -050058 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 Salomonb9b13732020-06-25 11:13:49 -040062 SkRect planeSubset;
Brian Salomon0ea33072020-07-14 10:43:42 -040063 SkRect planeDomain;
Brian Salomona3b02f52020-07-15 16:02:01 -040064 bool makeLinearWithSnap = false;
Brian Salomon0857bef2021-01-13 15:54:04 -050065 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 Salomon0857bef2021-01-13 15:54:04 -050071 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 Salomon0ea33072020-07-14 10:43:42 -040077 if (subset) {
Brian Salomon0857bef2021-01-13 15:54:04 -050078 planeSubset = {subset->fLeft *scaleX,
79 subset->fTop *scaleY,
80 subset->fRight *scaleX,
81 subset->fBottom*scaleY};
Brian Salomon7b0c8df2021-05-24 10:51:47 -040082 } else {
83 planeSubset = SkRect::Make(view.dimensions());
Brian Salomon0ea33072020-07-14 10:43:42 -040084 }
85 if (domain) {
Brian Salomon0857bef2021-01-13 15:54:04 -050086 planeDomain = {domain->fLeft *scaleX,
87 domain->fTop *scaleY,
88 domain->fRight *scaleX,
89 domain->fBottom*scaleY};
Brian Salomon0ea33072020-07-14 10:43:42 -040090 }
Brian Salomon7b0c8df2021-05-24 10:51:47 -040091 // 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 Salomona3b02f52020-07-15 16:02:01 -0400110 // 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 Salomonb9b13732020-06-25 11:13:49 -0400114 if (samplerState.filter() == GrSamplerState::Filter::kNearest) {
Brian Salomon0857bef2021-01-13 15:54:04 -0500115 bool snapX = (ssx != 1),
116 snapY = (ssy != 1);
Brian Salomona3b02f52020-07-15 16:02:01 -0400117 makeLinearWithSnap = snapX || snapY;
Brian Salomonb9b13732020-06-25 11:13:49 -0400118 snap[0] |= snapX;
119 snap[1] |= snapY;
Brian Salomon0ea33072020-07-14 10:43:42 -0400120 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 Salomonb9b13732020-06-25 11:13:49 -0400128 }
Brian Salomon0ea33072020-07-14 10:43:42 -0400129 } else {
Brian Salomond71548a2020-02-29 19:43:30 -0500130 if (subset) {
Brian Salomon0ea33072020-07-14 10:43:42 -0400131 planeSubset = *subset;
Brian Salomonfa9232c2019-12-04 16:09:04 -0500132 }
Brian Salomon0ea33072020-07-14 10:43:42 -0400133 if (domain) {
134 planeDomain = *domain;
135 }
Brian Salomon8a99a412019-12-04 11:05:35 -0500136 }
Brian Salomon7b0c8df2021-05-24 10:51:47 -0400137 if (useSubset) {
Brian Salomona3b02f52020-07-15 16:02:01 -0400138 if (makeLinearWithSnap) {
Brian Salomonb9b13732020-06-25 11:13:49 -0400139 // The plane is subsampled and we have an overall subset on the image. We're
Brian Salomona3b02f52020-07-15 16:02:01 -0400140 // 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 Salomon0ea33072020-07-14 10:43:42 -0400148 SkRect* domainRect = domain ? &planeDomain : nullptr;
Brian Salomon0857bef2021-01-13 15:54:04 -0500149 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 Salomon0ea33072020-07-14 10:43:42 -0400159 } else if (domain) {
Brian Salomon0857bef2021-01-13 15:54:04 -0500160 planeFPs[i] = GrTextureEffect::MakeSubset(std::move(view),
161 kUnknown_SkAlphaType,
162 planeMatrix,
163 samplerState,
164 planeSubset,
165 planeDomain,
166 caps,
167 planeBorders[i]);
Brian Salomonb9b13732020-06-25 11:13:49 -0400168 } else {
Brian Salomon0857bef2021-01-13 15:54:04 -0500169 planeFPs[i] = GrTextureEffect::MakeSubset(std::move(view),
170 kUnknown_SkAlphaType,
171 planeMatrix,
172 samplerState,
173 planeSubset,
174 caps,
175 planeBorders[i]);
Brian Salomonb9b13732020-06-25 11:13:49 -0400176 }
Brian Salomonca6b2f42020-01-24 11:31:21 -0500177 } else {
Brian Salomonb9b13732020-06-25 11:13:49 -0400178 GrSamplerState planeSampler = samplerState;
Brian Salomona3b02f52020-07-15 16:02:01 -0400179 if (makeLinearWithSnap) {
180 planeSampler.setFilterMode(GrSamplerState::Filter::kLinear);
Brian Salomonb9b13732020-06-25 11:13:49 -0400181 }
Brian Salomon0857bef2021-01-13 15:54:04 -0500182 planeFPs[i] = GrTextureEffect::Make(std::move(view),
183 kUnknown_SkAlphaType,
184 planeMatrix,
185 planeSampler,
186 caps,
187 planeBorders[i]);
Brian Salomon8a99a412019-12-04 11:05:35 -0500188 }
Robert Phillips94ade752018-10-09 12:32:31 -0400189 }
Brian Salomon0857bef2021-01-13 15:54:04 -0500190 std::unique_ptr<GrFragmentProcessor> fp(
191 new GrYUVtoRGBEffect(planeFPs,
192 numPlanes,
193 yuvaProxies.yuvaLocations(),
194 snap,
195 yuvaProxies.yuvaInfo().yuvColorSpace()));
Brian Salomonb9b13732020-06-25 11:13:49 -0400196 return GrMatrixEffect::Make(localMatrix, std::move(fp));
Brian Salomonfa9232c2019-12-04 16:09:04 -0500197}
198
Brian Salomon0c0b5a62021-01-11 14:40:44 -0500199static SkAlphaType alpha_type(const SkYUVAInfo::YUVALocations locations) {
200 return locations[SkYUVAInfo::YUVAChannels::kA].fPlane >= 0 ? kPremul_SkAlphaType
201 : kOpaque_SkAlphaType;
Brian Salomonfa9232c2019-12-04 16:09:04 -0500202}
203
Brian Salomonb9b13732020-06-25 11:13:49 -0400204GrYUVtoRGBEffect::GrYUVtoRGBEffect(std::unique_ptr<GrFragmentProcessor> planeFPs[4],
205 int numPlanes,
Brian Salomon0c0b5a62021-01-11 14:40:44 -0500206 const SkYUVAInfo::YUVALocations& locations,
Brian Salomonb9b13732020-06-25 11:13:49 -0400207 const bool snap[2],
208 SkYUVColorSpace yuvColorSpace)
Brian Salomonfa9232c2019-12-04 16:09:04 -0500209 : GrFragmentProcessor(kGrYUVtoRGBEffect_ClassID,
Brian Salomon0c0b5a62021-01-11 14:40:44 -0500210 ModulateForClampedSamplerOptFlags(alpha_type(locations)))
211 , fLocations(locations)
Brian Salomonfa9232c2019-12-04 16:09:04 -0500212 , fYUVColorSpace(yuvColorSpace) {
Brian Salomonb9b13732020-06-25 11:13:49 -0400213 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 Ludwigfbe28592020-06-26 16:02:15 -0400217 this->setUsesSampleCoordsDirectly();
Brian Salomonb9b13732020-06-25 11:13:49 -0400218 for (int i = 0; i < numPlanes; ++i) {
Brian Osman1298bc42020-06-30 13:39:35 -0400219 this->registerChild(std::move(planeFPs[i]), SkSL::SampleUsage::Explicit());
Brian Salomonb9b13732020-06-25 11:13:49 -0400220 }
221 } else {
222 for (int i = 0; i < numPlanes; ++i) {
223 this->registerChild(std::move(planeFPs[i]));
224 }
225 }
Ethan Nicholas7461a4a2017-12-21 14:18:01 -0500226}
Robert Phillipsba5c4392018-07-25 12:37:14 -0400227
John Stiles8d9bf642020-08-12 15:07:45 -0400228#if GR_TEST_UTILS
John Stilescab58862020-08-12 15:47:06 -0400229SkString GrYUVtoRGBEffect::onDumpInfo() const {
230 SkString str("(");
Brian Salomon0c0b5a62021-01-11 14:40:44 -0500231 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 Phillips94ade752018-10-09 12:32:31 -0400234 }
John Stilesba1879d2020-08-11 13:58:32 -0400235 str.appendf("YUVColorSpace=%d, snap=(%d, %d))",
236 static_cast<int>(fYUVColorSpace), fSnap[0], fSnap[1]);
Robert Phillipsba5c4392018-07-25 12:37:14 -0400237 return str;
238}
Brian Osman9a390ac2018-11-12 09:47:48 -0500239#endif
240
Brian Salomon3176e862021-08-09 11:23:04 -0400241std::unique_ptr<GrFragmentProcessor::ProgramImpl> GrYUVtoRGBEffect::onMakeProgramImpl() const {
242 class GrGLSLYUVtoRGBEffect : public ProgramImpl {
Michael Ludwiga6a84002019-04-12 15:03:02 -0400243 public:
244 GrGLSLYUVtoRGBEffect() {}
245
246 void emitCode(EmitArgs& args) override {
247 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Brian Salomonfa9232c2019-12-04 16:09:04 -0500248 const GrYUVtoRGBEffect& yuvEffect = args.fFp.cast<GrYUVtoRGBEffect>();
Michael Ludwiga6a84002019-04-12 15:03:02 -0400249
Brian Salomonfa9232c2019-12-04 16:09:04 -0500250 int numPlanes = yuvEffect.numChildProcessors();
Michael Ludwiga6a84002019-04-12 15:03:02 -0400251
Brian Salomonb9b13732020-06-25 11:13:49 -0400252 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 Stiles95eca3e2021-03-18 16:00:22 -0400264 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 Ludwiga6a84002019-04-12 15:03:02 -0400286 }
287
John Stiles95eca3e2021-03-18 16:00:22 -0400288 if (!hasAlpha) {
289 fragBuilder->codeAppendf("color.a = 1;");
Michael Ludwiga6a84002019-04-12 15:03:02 -0400290 }
291
Brian Salomonfa9232c2019-12-04 16:09:04 -0500292 if (kIdentity_SkYUVColorSpace != yuvEffect.fYUVColorSpace) {
Ethan Nicholas16464c32020-04-06 13:53:05 -0400293 fColorSpaceMatrixVar = args.fUniformHandler->addUniform(&yuvEffect,
Brian Salomonfa9232c2019-12-04 16:09:04 -0500294 kFragment_GrShaderFlag, kHalf3x3_GrSLType, "colorSpaceMatrix");
Ethan Nicholas16464c32020-04-06 13:53:05 -0400295 fColorSpaceTranslateVar = args.fUniformHandler->addUniform(&yuvEffect,
Brian Salomonfa9232c2019-12-04 16:09:04 -0500296 kFragment_GrShaderFlag, kHalf3_GrSLType, "colorSpaceTranslate");
Michael Ludwiga6a84002019-04-12 15:03:02 -0400297 fragBuilder->codeAppendf(
Brian Salomonfa9232c2019-12-04 16:09:04 -0500298 "color.rgb = saturate(color.rgb * %s + %s);",
299 args.fUniformHandler->getUniformCStr(fColorSpaceMatrixVar),
300 args.fUniformHandler->getUniformCStr(fColorSpaceTranslateVar));
Michael Ludwiga6a84002019-04-12 15:03:02 -0400301 }
Brian Salomonfa9232c2019-12-04 16:09:04 -0500302 if (hasAlpha) {
Michael Ludwiga6a84002019-04-12 15:03:02 -0400303 // premultiply alpha
Brian Salomonfa9232c2019-12-04 16:09:04 -0500304 fragBuilder->codeAppendf("color.rgb *= color.a;");
Michael Ludwiga6a84002019-04-12 15:03:02 -0400305 }
John Stiles5e7c0402020-10-20 19:43:18 -0400306 fragBuilder->codeAppendf("return color;");
Michael Ludwiga6a84002019-04-12 15:03:02 -0400307 }
308
309 private:
310 void onSetData(const GrGLSLProgramDataManager& pdman,
Brian Salomonfa9232c2019-12-04 16:09:04 -0500311 const GrFragmentProcessor& proc) override {
312 const GrYUVtoRGBEffect& yuvEffect = proc.cast<GrYUVtoRGBEffect>();
Michael Ludwiga6a84002019-04-12 15:03:02 -0400313
Brian Salomonfa9232c2019-12-04 16:09:04 -0500314 if (yuvEffect.fYUVColorSpace != kIdentity_SkYUVColorSpace) {
Brian Osman904cac82019-10-30 16:57:14 -0400315 SkASSERT(fColorSpaceMatrixVar.isValid());
316 float yuvM[20];
Brian Salomonfa9232c2019-12-04 16:09:04 -0500317 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 Osman904cac82019-10-30 16:57:14 -0400328 };
Brian Salomonfa9232c2019-12-04 16:09:04 -0500329 float v[3] = {yuvM[4], yuvM[9], yuvM[14]};
330 pdman.setMatrix3f(fColorSpaceMatrixVar, mtx);
331 pdman.set3fv(fColorSpaceTranslateVar, 1, v);
Michael Ludwiga6a84002019-04-12 15:03:02 -0400332 }
333 }
334
335 UniformHandle fColorSpaceMatrixVar;
Brian Salomonfa9232c2019-12-04 16:09:04 -0500336 UniformHandle fColorSpaceTranslateVar;
Michael Ludwiga6a84002019-04-12 15:03:02 -0400337 };
338
Brian Salomon18ab2032021-02-23 10:07:05 -0500339 return std::make_unique<GrGLSLYUVtoRGBEffect>();
Ethan Nicholas7461a4a2017-12-21 14:18:01 -0500340}
Brian Salomon13b28732021-08-06 15:33:58 -0400341void GrYUVtoRGBEffect::onAddToKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const {
Robert Phillips94ade752018-10-09 12:32:31 -0400342 uint32_t packed = 0;
Brian Salomon0c0b5a62021-01-11 14:40:44 -0500343 int i = 0;
344 for (auto [plane, channel] : fLocations) {
345 if (plane < 0) {
Robert Phillips94ade752018-10-09 12:32:31 -0400346 continue;
347 }
348
Brian Salomon0c0b5a62021-01-11 14:40:44 -0500349 uint8_t chann = static_cast<int>(channel);
Robert Phillips6ba8c832018-10-10 11:43:01 -0400350
Brian Salomon0c0b5a62021-01-11 14:40:44 -0500351 SkASSERT(plane < 4 && chann < 4);
Robert Phillips94ade752018-10-09 12:32:31 -0400352
Brian Salomon0c0b5a62021-01-11 14:40:44 -0500353 packed |= (plane | (chann << 2)) << (i++ * 4);
Robert Phillips94ade752018-10-09 12:32:31 -0400354 }
Brian Salomonfa9232c2019-12-04 16:09:04 -0500355 if (fYUVColorSpace == kIdentity_SkYUVColorSpace) {
Brian Salomonb9b13732020-06-25 11:13:49 -0400356 packed |= 1 << 16;
357 }
358 if (fSnap[0]) {
359 packed |= 1 << 17;
360 }
361 if (fSnap[1]) {
362 packed |= 1 << 18;
Robert Phillipsb651aac2019-03-15 12:18:49 -0400363 }
Robert Phillips94ade752018-10-09 12:32:31 -0400364 b->add32(packed);
Ethan Nicholas7461a4a2017-12-21 14:18:01 -0500365}
Brian Salomonfa9232c2019-12-04 16:09:04 -0500366
Ethan Nicholas7461a4a2017-12-21 14:18:01 -0500367bool GrYUVtoRGBEffect::onIsEqual(const GrFragmentProcessor& other) const {
368 const GrYUVtoRGBEffect& that = other.cast<GrYUVtoRGBEffect>();
Robert Phillips6ba8c832018-10-10 11:43:01 -0400369
Brian Salomon0c0b5a62021-01-11 14:40:44 -0500370 return fLocations == that.fLocations &&
Brian Salomonb9b13732020-06-25 11:13:49 -0400371 std::equal(fSnap, fSnap + 2, that.fSnap) &&
372 fYUVColorSpace == that.fYUVColorSpace;
Ethan Nicholas7461a4a2017-12-21 14:18:01 -0500373}
Brian Salomonfa9232c2019-12-04 16:09:04 -0500374
Ethan Nicholas7461a4a2017-12-21 14:18:01 -0500375GrYUVtoRGBEffect::GrYUVtoRGBEffect(const GrYUVtoRGBEffect& src)
John Stiles307f8f52021-08-09 15:36:59 -0400376 : GrFragmentProcessor(src)
Brian Salomon0c0b5a62021-01-11 14:40:44 -0500377 , fLocations((src.fLocations))
Robert Phillipsb651aac2019-03-15 12:18:49 -0400378 , fYUVColorSpace(src.fYUVColorSpace) {
Brian Salomonb9b13732020-06-25 11:13:49 -0400379 std::copy_n(src.fSnap, 2, fSnap);
Ethan Nicholas7461a4a2017-12-21 14:18:01 -0500380}
Brian Salomonfa9232c2019-12-04 16:09:04 -0500381
Ethan Nicholas7461a4a2017-12-21 14:18:01 -0500382std::unique_ptr<GrFragmentProcessor> GrYUVtoRGBEffect::clone() const {
383 return std::unique_ptr<GrFragmentProcessor>(new GrYUVtoRGBEffect(*this));
384}