blob: cf4c941ed7a4a4d2e2fe5f14357489cfcf7b062a [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],
Jim Van Verth30e0d7f2018-11-02 13:36:42 -040033 SkYUVColorSpace yuvColorSpace,
34 GrSamplerState::Filter filterMode) {
Robert Phillips94ade752018-10-09 12:32:31 -040035 int numPlanes;
Jim Van Verthf00b1622018-10-10 13:03:23 -040036 SkAssertResult(SkYUVAIndex::AreValidIndices(yuvaIndices, &numPlanes));
Robert Phillips94ade752018-10-09 12:32:31 -040037
38 const SkISize YSize = proxies[yuvaIndices[SkYUVAIndex::kY_Index].fIndex]->isize();
39
Jim Van Verth30e0d7f2018-11-02 13:36:42 -040040 GrSamplerState::Filter minimizeFilterMode = GrSamplerState::Filter::kMipMap == filterMode ?
41 GrSamplerState::Filter::kMipMap :
42 GrSamplerState::Filter::kBilerp;
43
Robert Phillips94ade752018-10-09 12:32:31 -040044 GrSamplerState::Filter filterModes[4];
45 SkSize scales[4];
46 for (int i = 0; i < numPlanes; ++i) {
47 SkISize size = proxies[i]->isize();
48 scales[i] = SkSize::Make(SkIntToScalar(size.width()) / SkIntToScalar(YSize.width()),
49 SkIntToScalar(size.height()) / SkIntToScalar(YSize.height()));
Jim Van Verth30e0d7f2018-11-02 13:36:42 -040050 filterModes[i] = (size == YSize) ? filterMode : minimizeFilterMode;
Robert Phillips94ade752018-10-09 12:32:31 -040051 }
52
Mike Klein4429a4f2018-10-04 09:06:00 -040053 SkMatrix44 mat;
Robert Phillips94ade752018-10-09 12:32:31 -040054 switch (yuvColorSpace) {
Ethan Nicholas7461a4a2017-12-21 14:18:01 -050055 case kJPEG_SkYUVColorSpace:
56 mat.setColMajorf(kJPEGConversionMatrix);
57 break;
58 case kRec601_SkYUVColorSpace:
59 mat.setColMajorf(kRec601ConversionMatrix);
60 break;
61 case kRec709_SkYUVColorSpace:
62 mat.setColMajorf(kRec709ConversionMatrix);
63 break;
64 }
65 return std::unique_ptr<GrFragmentProcessor>(new GrYUVtoRGBEffect(
Robert Phillips94ade752018-10-09 12:32:31 -040066 proxies, scales, filterModes, numPlanes, yuvaIndices, mat));
Ethan Nicholas7461a4a2017-12-21 14:18:01 -050067}
Robert Phillipsba5c4392018-07-25 12:37:14 -040068
Brian Osman9a390ac2018-11-12 09:47:48 -050069#ifdef SK_DEBUG
Robert Phillipsba5c4392018-07-25 12:37:14 -040070SkString GrYUVtoRGBEffect::dumpInfo() const {
71 SkString str;
Robert Phillips94ade752018-10-09 12:32:31 -040072 for (int i = 0; i < this->numTextureSamplers(); ++i) {
73 str.appendf("%d: %d %d ", i,
74 this->textureSampler(i).proxy()->uniqueID().asUInt(),
75 this->textureSampler(i).proxy()->underlyingUniqueID().asUInt());
76 }
77 str.appendf("\n");
Robert Phillipsba5c4392018-07-25 12:37:14 -040078
79 return str;
80}
Brian Osman9a390ac2018-11-12 09:47:48 -050081#endif
82
Ethan Nicholas7461a4a2017-12-21 14:18:01 -050083#include "glsl/GrGLSLFragmentProcessor.h"
84#include "glsl/GrGLSLFragmentShaderBuilder.h"
85#include "glsl/GrGLSLProgramBuilder.h"
86#include "GrTexture.h"
87#include "SkSLCPP.h"
88#include "SkSLUtil.h"
89class GrGLSLYUVtoRGBEffect : public GrGLSLFragmentProcessor {
90public:
91 GrGLSLYUVtoRGBEffect() {}
92 void emitCode(EmitArgs& args) override {
93 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
94 const GrYUVtoRGBEffect& _outer = args.fFp.cast<GrYUVtoRGBEffect>();
95 (void)_outer;
Robert Phillips94ade752018-10-09 12:32:31 -040096
Ethan Nicholas7461a4a2017-12-21 14:18:01 -050097 auto colorSpaceMatrix = _outer.colorSpaceMatrix();
98 (void)colorSpaceMatrix;
Ethan Nicholas7461a4a2017-12-21 14:18:01 -050099 fColorSpaceMatrixVar =
100 args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf4x4_GrSLType,
101 kDefault_GrSLPrecision, "colorSpaceMatrix");
Robert Phillips94ade752018-10-09 12:32:31 -0400102
103 int numSamplers = args.fTexSamplers.count();
104
105 SkString coords[4];
106 for (int i = 0; i < numSamplers; ++i) {
107 coords[i] = fragBuilder->ensureCoords2D(args.fTransformedCoords[i]);
108 }
109
110 for (int i = 0; i < numSamplers; ++i) {
111 fragBuilder->codeAppendf(
112 "half4 tmp%d = texture(%s, %s).%s;",
113 i,
114 fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[i]).c_str(),
115 coords[i].c_str(),
116 fragBuilder->getProgramBuilder()->samplerSwizzle(args.fTexSamplers[i]).c_str());
117 }
118
119 static const char kChannelToChar[4] = { 'x', 'y', 'z', 'w' };
120
Ethan Nicholas7461a4a2017-12-21 14:18:01 -0500121 fragBuilder->codeAppendf(
Ethan Nicholase1f55022019-02-05 17:17:40 -0500122 "half4 yuvOne = half4(half(tmp%d.%c), half(tmp%d.%c), half(tmp%d.%c), 1.0) * %s;",
Robert Phillips94ade752018-10-09 12:32:31 -0400123 _outer.yuvaIndex(0).fIndex, kChannelToChar[(int)_outer.yuvaIndex(0).fChannel],
124 _outer.yuvaIndex(1).fIndex, kChannelToChar[(int)_outer.yuvaIndex(1).fChannel],
125 _outer.yuvaIndex(2).fIndex, kChannelToChar[(int)_outer.yuvaIndex(2).fChannel],
Ethan Nicholas7461a4a2017-12-21 14:18:01 -0500126 args.fUniformHandler->getUniformCStr(fColorSpaceMatrixVar));
Robert Phillips94ade752018-10-09 12:32:31 -0400127
128
129 if (_outer.yuvaIndex(3).fIndex >= 0) {
130 fragBuilder->codeAppendf(
Ethan Nicholase1f55022019-02-05 17:17:40 -0500131 "half a = tmp%d.%c;", _outer.yuvaIndex(3).fIndex,
Robert Phillips94ade752018-10-09 12:32:31 -0400132 kChannelToChar[(int)_outer.yuvaIndex(3).fChannel]);
Jim Van Verth53275362018-11-09 15:42:35 -0500133 // premultiply alpha
134 fragBuilder->codeAppend("yuvOne *= a;");
Robert Phillips94ade752018-10-09 12:32:31 -0400135 } else {
Ethan Nicholase1f55022019-02-05 17:17:40 -0500136 fragBuilder->codeAppendf("half a = 1.0;");
Robert Phillips94ade752018-10-09 12:32:31 -0400137 }
138
139 fragBuilder->codeAppendf("%s = half4(yuvOne.xyz, a);", args.fOutputColor);
Ethan Nicholas7461a4a2017-12-21 14:18:01 -0500140 }
141
142private:
143 void onSetData(const GrGLSLProgramDataManager& pdman,
144 const GrFragmentProcessor& _proc) override {
145 const GrYUVtoRGBEffect& _outer = _proc.cast<GrYUVtoRGBEffect>();
Michael Ludwiga4275592018-08-31 10:52:47 -0400146 { pdman.setSkMatrix44(fColorSpaceMatrixVar, (_outer.colorSpaceMatrix())); }
Ethan Nicholas7461a4a2017-12-21 14:18:01 -0500147 }
148 UniformHandle fColorSpaceMatrixVar;
149};
150GrGLSLFragmentProcessor* GrYUVtoRGBEffect::onCreateGLSLInstance() const {
151 return new GrGLSLYUVtoRGBEffect();
152}
153void GrYUVtoRGBEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
154 GrProcessorKeyBuilder* b) const {
Robert Phillips94ade752018-10-09 12:32:31 -0400155 b->add32(this->numTextureSamplers());
156
157 uint32_t packed = 0;
158 for (int i = 0; i < 4; ++i) {
159 if (this->yuvaIndex(i).fIndex < 0) {
160 continue;
161 }
162
163 uint8_t index = this->yuvaIndex(i).fIndex;
164 uint8_t chann = (uint8_t) this->yuvaIndex(i).fChannel;
Robert Phillips6ba8c832018-10-10 11:43:01 -0400165
Robert Phillips94ade752018-10-09 12:32:31 -0400166 SkASSERT(index < 4 && chann < 4);
167
168 packed |= (index | (chann << 2)) << (i * 4);
169 }
170 b->add32(packed);
Ethan Nicholas7461a4a2017-12-21 14:18:01 -0500171}
172bool GrYUVtoRGBEffect::onIsEqual(const GrFragmentProcessor& other) const {
173 const GrYUVtoRGBEffect& that = other.cast<GrYUVtoRGBEffect>();
Robert Phillips6ba8c832018-10-10 11:43:01 -0400174
175 for (int i = 0; i < 4; ++i) {
176 if (fYUVAIndices[i] != that.fYUVAIndices[i]) {
177 return false;
178 }
179 }
Robert Phillips94ade752018-10-09 12:32:31 -0400180
181 for (int i = 0; i < this->numTextureSamplers(); ++i) {
182 // 'fSamplers' is checked by the base class
183 if (fSamplerTransforms[i] != that.fSamplerTransforms[i]) {
184 return false;
185 }
186 }
187
188 if (fColorSpaceMatrix != that.fColorSpaceMatrix) {
189 return false;
190 }
191
Ethan Nicholas7461a4a2017-12-21 14:18:01 -0500192 return true;
193}
194GrYUVtoRGBEffect::GrYUVtoRGBEffect(const GrYUVtoRGBEffect& src)
195 : INHERITED(kGrYUVtoRGBEffect_ClassID, src.optimizationFlags())
Robert Phillips94ade752018-10-09 12:32:31 -0400196 , fColorSpaceMatrix(src.fColorSpaceMatrix) {
197 int numPlanes = src.numTextureSamplers();
198 for (int i = 0; i < numPlanes; ++i) {
199 fSamplers[i].reset(sk_ref_sp(src.fSamplers[i].proxy()), src.fSamplers[i].samplerState());
200 fSamplerTransforms[i] = src.fSamplerTransforms[i];
201 fSamplerCoordTransforms[i] = src.fSamplerCoordTransforms[i];
202 }
203
204 this->setTextureSamplerCnt(numPlanes);
205 for (int i = 0; i < numPlanes; ++i) {
206 this->addCoordTransform(&fSamplerCoordTransforms[i]);
207 }
208
209 memcpy(fYUVAIndices, src.fYUVAIndices, sizeof(fYUVAIndices));
Ethan Nicholas7461a4a2017-12-21 14:18:01 -0500210}
211std::unique_ptr<GrFragmentProcessor> GrYUVtoRGBEffect::clone() const {
212 return std::unique_ptr<GrFragmentProcessor>(new GrYUVtoRGBEffect(*this));
213}
Brian Salomonf7dcd762018-07-30 14:48:15 -0400214const GrFragmentProcessor::TextureSampler& GrYUVtoRGBEffect::onTextureSampler(int index) const {
Robert Phillips94ade752018-10-09 12:32:31 -0400215 SkASSERT(index < this->numTextureSamplers());
216 return fSamplers[index];
Brian Salomonf7dcd762018-07-30 14:48:15 -0400217}