blob: c4d58a70a4df5535a4157213f2629f169e98010d [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
Ethan Nicholas7461a4a2017-12-21 14:18:01 -05008#include "GrYUVtoRGBEffect.h"
Ethan Nicholas7461a4a2017-12-21 14:18:01 -05009
10static const float kJPEGConversionMatrix[16] = {
Robert Phillips94ade752018-10-09 12:32:31 -040011 1.0f, 0.0f, 1.402f, -0.703749f,
12 1.0f, -0.344136f, -0.714136f, 0.531211f,
13 1.0f, 1.772f, 0.0f, -0.889475f,
14 0.0f, 0.0f, 0.0f, 1.0
15};
Ethan Nicholas7461a4a2017-12-21 14:18:01 -050016
17static const float kRec601ConversionMatrix[16] = {
Robert Phillips94ade752018-10-09 12:32:31 -040018 1.164f, 0.0f, 1.596f, -0.87075f,
19 1.164f, -0.391f, -0.813f, 0.52925f,
20 1.164f, 2.018f, 0.0f, -1.08175f,
21 0.0f, 0.0f, 0.0f, 1.0
22};
Ethan Nicholas7461a4a2017-12-21 14:18:01 -050023
24static const float kRec709ConversionMatrix[16] = {
Robert Phillips94ade752018-10-09 12:32:31 -040025 1.164f, 0.0f, 1.793f, -0.96925f,
26 1.164f, -0.213f, -0.533f, 0.30025f,
27 1.164f, 2.112f, 0.0f, -1.12875f,
28 0.0f, 0.0f, 0.0f, 1.0f
29};
Ethan Nicholas7461a4a2017-12-21 14:18:01 -050030
Robert Phillips94ade752018-10-09 12:32:31 -040031std::unique_ptr<GrFragmentProcessor> GrYUVtoRGBEffect::Make(const sk_sp<GrTextureProxy> proxies[],
32 const SkYUVAIndex yuvaIndices[4],
33 SkYUVColorSpace yuvColorSpace) {
34 int numPlanes;
Jim Van Verthf00b1622018-10-10 13:03:23 -040035 SkAssertResult(SkYUVAIndex::AreValidIndices(yuvaIndices, &numPlanes));
Robert Phillips94ade752018-10-09 12:32:31 -040036
37 const SkISize YSize = proxies[yuvaIndices[SkYUVAIndex::kY_Index].fIndex]->isize();
38
39 GrSamplerState::Filter filterModes[4];
40 SkSize scales[4];
41 for (int i = 0; i < numPlanes; ++i) {
42 SkISize size = proxies[i]->isize();
43 scales[i] = SkSize::Make(SkIntToScalar(size.width()) / SkIntToScalar(YSize.width()),
44 SkIntToScalar(size.height()) / SkIntToScalar(YSize.height()));
45 filterModes[i] = (size == YSize) ? GrSamplerState::Filter::kNearest
46 : GrSamplerState::Filter::kBilerp;
47 }
48
Mike Klein4429a4f2018-10-04 09:06:00 -040049 SkMatrix44 mat;
Robert Phillips94ade752018-10-09 12:32:31 -040050 switch (yuvColorSpace) {
Ethan Nicholas7461a4a2017-12-21 14:18:01 -050051 case kJPEG_SkYUVColorSpace:
52 mat.setColMajorf(kJPEGConversionMatrix);
53 break;
54 case kRec601_SkYUVColorSpace:
55 mat.setColMajorf(kRec601ConversionMatrix);
56 break;
57 case kRec709_SkYUVColorSpace:
58 mat.setColMajorf(kRec709ConversionMatrix);
59 break;
60 }
61 return std::unique_ptr<GrFragmentProcessor>(new GrYUVtoRGBEffect(
Robert Phillips94ade752018-10-09 12:32:31 -040062 proxies, scales, filterModes, numPlanes, yuvaIndices, mat));
Ethan Nicholas7461a4a2017-12-21 14:18:01 -050063}
Robert Phillipsba5c4392018-07-25 12:37:14 -040064
65SkString GrYUVtoRGBEffect::dumpInfo() const {
66 SkString str;
Robert Phillips94ade752018-10-09 12:32:31 -040067 for (int i = 0; i < this->numTextureSamplers(); ++i) {
68 str.appendf("%d: %d %d ", i,
69 this->textureSampler(i).proxy()->uniqueID().asUInt(),
70 this->textureSampler(i).proxy()->underlyingUniqueID().asUInt());
71 }
72 str.appendf("\n");
Robert Phillipsba5c4392018-07-25 12:37:14 -040073
74 return str;
75}
Ethan Nicholas7461a4a2017-12-21 14:18:01 -050076#include "glsl/GrGLSLFragmentProcessor.h"
77#include "glsl/GrGLSLFragmentShaderBuilder.h"
78#include "glsl/GrGLSLProgramBuilder.h"
79#include "GrTexture.h"
80#include "SkSLCPP.h"
81#include "SkSLUtil.h"
82class GrGLSLYUVtoRGBEffect : public GrGLSLFragmentProcessor {
83public:
84 GrGLSLYUVtoRGBEffect() {}
85 void emitCode(EmitArgs& args) override {
86 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
87 const GrYUVtoRGBEffect& _outer = args.fFp.cast<GrYUVtoRGBEffect>();
88 (void)_outer;
Robert Phillips94ade752018-10-09 12:32:31 -040089
Ethan Nicholas7461a4a2017-12-21 14:18:01 -050090 auto colorSpaceMatrix = _outer.colorSpaceMatrix();
91 (void)colorSpaceMatrix;
Ethan Nicholas7461a4a2017-12-21 14:18:01 -050092 fColorSpaceMatrixVar =
93 args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf4x4_GrSLType,
94 kDefault_GrSLPrecision, "colorSpaceMatrix");
Robert Phillips94ade752018-10-09 12:32:31 -040095
96 int numSamplers = args.fTexSamplers.count();
97
98 SkString coords[4];
99 for (int i = 0; i < numSamplers; ++i) {
100 coords[i] = fragBuilder->ensureCoords2D(args.fTransformedCoords[i]);
101 }
102
103 for (int i = 0; i < numSamplers; ++i) {
104 fragBuilder->codeAppendf(
105 "half4 tmp%d = texture(%s, %s).%s;",
106 i,
107 fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[i]).c_str(),
108 coords[i].c_str(),
109 fragBuilder->getProgramBuilder()->samplerSwizzle(args.fTexSamplers[i]).c_str());
110 }
111
112 static const char kChannelToChar[4] = { 'x', 'y', 'z', 'w' };
113
Ethan Nicholas7461a4a2017-12-21 14:18:01 -0500114 fragBuilder->codeAppendf(
Robert Phillips94ade752018-10-09 12:32:31 -0400115 "half4 yuvOne = half4(tmp%d.%c, tmp%d.%c, tmp%d.%c, 1.0) * %s;",
116 _outer.yuvaIndex(0).fIndex, kChannelToChar[(int)_outer.yuvaIndex(0).fChannel],
117 _outer.yuvaIndex(1).fIndex, kChannelToChar[(int)_outer.yuvaIndex(1).fChannel],
118 _outer.yuvaIndex(2).fIndex, kChannelToChar[(int)_outer.yuvaIndex(2).fChannel],
Ethan Nicholas7461a4a2017-12-21 14:18:01 -0500119 args.fUniformHandler->getUniformCStr(fColorSpaceMatrixVar));
Robert Phillips94ade752018-10-09 12:32:31 -0400120
121
122 if (_outer.yuvaIndex(3).fIndex >= 0) {
123 fragBuilder->codeAppendf(
124 "float a = tmp%d.%c;", _outer.yuvaIndex(3).fIndex,
125 kChannelToChar[(int)_outer.yuvaIndex(3).fChannel]);
126 } else {
127 fragBuilder->codeAppendf("float a = 1.0;");
128 }
129
130 fragBuilder->codeAppendf("%s = half4(yuvOne.xyz, a);", args.fOutputColor);
Ethan Nicholas7461a4a2017-12-21 14:18:01 -0500131 }
132
133private:
134 void onSetData(const GrGLSLProgramDataManager& pdman,
135 const GrFragmentProcessor& _proc) override {
136 const GrYUVtoRGBEffect& _outer = _proc.cast<GrYUVtoRGBEffect>();
Michael Ludwiga4275592018-08-31 10:52:47 -0400137 { pdman.setSkMatrix44(fColorSpaceMatrixVar, (_outer.colorSpaceMatrix())); }
Ethan Nicholas7461a4a2017-12-21 14:18:01 -0500138 }
139 UniformHandle fColorSpaceMatrixVar;
140};
141GrGLSLFragmentProcessor* GrYUVtoRGBEffect::onCreateGLSLInstance() const {
142 return new GrGLSLYUVtoRGBEffect();
143}
144void GrYUVtoRGBEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
145 GrProcessorKeyBuilder* b) const {
Robert Phillips94ade752018-10-09 12:32:31 -0400146 b->add32(this->numTextureSamplers());
147
148 uint32_t packed = 0;
149 for (int i = 0; i < 4; ++i) {
150 if (this->yuvaIndex(i).fIndex < 0) {
151 continue;
152 }
153
154 uint8_t index = this->yuvaIndex(i).fIndex;
155 uint8_t chann = (uint8_t) this->yuvaIndex(i).fChannel;
Robert Phillips6ba8c832018-10-10 11:43:01 -0400156
Robert Phillips94ade752018-10-09 12:32:31 -0400157 SkASSERT(index < 4 && chann < 4);
158
159 packed |= (index | (chann << 2)) << (i * 4);
160 }
161 b->add32(packed);
Ethan Nicholas7461a4a2017-12-21 14:18:01 -0500162}
163bool GrYUVtoRGBEffect::onIsEqual(const GrFragmentProcessor& other) const {
164 const GrYUVtoRGBEffect& that = other.cast<GrYUVtoRGBEffect>();
Robert Phillips6ba8c832018-10-10 11:43:01 -0400165
166 for (int i = 0; i < 4; ++i) {
167 if (fYUVAIndices[i] != that.fYUVAIndices[i]) {
168 return false;
169 }
170 }
Robert Phillips94ade752018-10-09 12:32:31 -0400171
172 for (int i = 0; i < this->numTextureSamplers(); ++i) {
173 // 'fSamplers' is checked by the base class
174 if (fSamplerTransforms[i] != that.fSamplerTransforms[i]) {
175 return false;
176 }
177 }
178
179 if (fColorSpaceMatrix != that.fColorSpaceMatrix) {
180 return false;
181 }
182
Ethan Nicholas7461a4a2017-12-21 14:18:01 -0500183 return true;
184}
185GrYUVtoRGBEffect::GrYUVtoRGBEffect(const GrYUVtoRGBEffect& src)
186 : INHERITED(kGrYUVtoRGBEffect_ClassID, src.optimizationFlags())
Robert Phillips94ade752018-10-09 12:32:31 -0400187 , fColorSpaceMatrix(src.fColorSpaceMatrix) {
188 int numPlanes = src.numTextureSamplers();
189 for (int i = 0; i < numPlanes; ++i) {
190 fSamplers[i].reset(sk_ref_sp(src.fSamplers[i].proxy()), src.fSamplers[i].samplerState());
191 fSamplerTransforms[i] = src.fSamplerTransforms[i];
192 fSamplerCoordTransforms[i] = src.fSamplerCoordTransforms[i];
193 }
194
195 this->setTextureSamplerCnt(numPlanes);
196 for (int i = 0; i < numPlanes; ++i) {
197 this->addCoordTransform(&fSamplerCoordTransforms[i]);
198 }
199
200 memcpy(fYUVAIndices, src.fYUVAIndices, sizeof(fYUVAIndices));
Ethan Nicholas7461a4a2017-12-21 14:18:01 -0500201}
202std::unique_ptr<GrFragmentProcessor> GrYUVtoRGBEffect::clone() const {
203 return std::unique_ptr<GrFragmentProcessor>(new GrYUVtoRGBEffect(*this));
204}
Brian Salomonf7dcd762018-07-30 14:48:15 -0400205const GrFragmentProcessor::TextureSampler& GrYUVtoRGBEffect::onTextureSampler(int index) const {
Robert Phillips94ade752018-10-09 12:32:31 -0400206 SkASSERT(index < this->numTextureSamplers());
207 return fSamplers[index];
Brian Salomonf7dcd762018-07-30 14:48:15 -0400208}