1 /*
2 * Copyright 2018 Google Inc.
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
8 /**************************************************************************************************
9 *** This file was autogenerated from GrYUVtoRGBEffect.fp; do not modify.
10 **************************************************************************************************/
11 #include "GrYUVtoRGBEffect.h"
12 #if SK_SUPPORT_GPU
13
14 static const float kJPEGConversionMatrix[16] = {
15 1.0f, 0.0f, 1.402f, -0.703749f, 1.0f, -0.344136f, -0.714136f, 0.531211f,
16 1.0f, 1.772f, 0.0f, -0.889475f, 0.0f, 0.0f, 0.0f, 1.0};
17
18 static const float kRec601ConversionMatrix[16] = {
19 1.164f, 0.0f, 1.596f, -0.87075f, 1.164f, -0.391f, -0.813f, 0.52925f,
20 1.164f, 2.018f, 0.0f, -1.08175f, 0.0f, 0.0f, 0.0f, 1.0};
21
22 static const float kRec709ConversionMatrix[16] = {
23 1.164f, 0.0f, 1.793f, -0.96925f, 1.164f, -0.213f, -0.533f, 0.30025f,
24 1.164f, 2.112f, 0.0f, -1.12875f, 0.0f, 0.0f, 0.0f, 1.0f};
25
Make(sk_sp<GrTextureProxy> yProxy,sk_sp<GrTextureProxy> uProxy,sk_sp<GrTextureProxy> vProxy,const SkISize sizes[3],SkYUVColorSpace colorSpace,bool nv12)26 std::unique_ptr<GrFragmentProcessor> GrYUVtoRGBEffect::Make(sk_sp<GrTextureProxy> yProxy,
27 sk_sp<GrTextureProxy>
28 uProxy,
29 sk_sp<GrTextureProxy>
30 vProxy,
31 const SkISize sizes[3],
32 SkYUVColorSpace colorSpace,
33 bool nv12) {
34 SkScalar w[3], h[3];
35 w[0] = SkIntToScalar(sizes[0].fWidth);
36 h[0] = SkIntToScalar(sizes[0].fHeight);
37 w[1] = SkIntToScalar(sizes[1].fWidth);
38 h[1] = SkIntToScalar(sizes[1].fHeight);
39 w[2] = SkIntToScalar(sizes[2].fWidth);
40 h[2] = SkIntToScalar(sizes[2].fHeight);
41 SkMatrix yTransform = SkMatrix::I();
42 SkMatrix uTransform = SkMatrix::MakeScale(w[1] / w[0], h[1] / h[0]);
43 SkMatrix vTransform = SkMatrix::MakeScale(w[2] / w[0], h[2] / h[0]);
44 GrSamplerState::Filter uvFilterMode =
45 ((sizes[1].fWidth != sizes[0].fWidth) || (sizes[1].fHeight != sizes[0].fHeight) ||
46 (sizes[2].fWidth != sizes[0].fWidth) || (sizes[2].fHeight != sizes[0].fHeight))
47 ? GrSamplerState::Filter::kBilerp
48 : GrSamplerState::Filter::kNearest;
49 SkMatrix44 mat(SkMatrix44::kUninitialized_Constructor);
50 switch (colorSpace) {
51 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(
62 std::move(yProxy), yTransform, std::move(uProxy), uTransform, std::move(vProxy),
63 vTransform, mat, nv12, GrSamplerState(GrSamplerState::WrapMode::kClamp, uvFilterMode)));
64 }
65 #include "glsl/GrGLSLFragmentProcessor.h"
66 #include "glsl/GrGLSLFragmentShaderBuilder.h"
67 #include "glsl/GrGLSLProgramBuilder.h"
68 #include "GrTexture.h"
69 #include "SkSLCPP.h"
70 #include "SkSLUtil.h"
71 class GrGLSLYUVtoRGBEffect : public GrGLSLFragmentProcessor {
72 public:
GrGLSLYUVtoRGBEffect()73 GrGLSLYUVtoRGBEffect() {}
emitCode(EmitArgs & args)74 void emitCode(EmitArgs& args) override {
75 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
76 const GrYUVtoRGBEffect& _outer = args.fFp.cast<GrYUVtoRGBEffect>();
77 (void)_outer;
78 auto ySamplerTransform = _outer.ySamplerTransform();
79 (void)ySamplerTransform;
80 auto uSamplerTransform = _outer.uSamplerTransform();
81 (void)uSamplerTransform;
82 auto vSamplerTransform = _outer.vSamplerTransform();
83 (void)vSamplerTransform;
84 auto colorSpaceMatrix = _outer.colorSpaceMatrix();
85 (void)colorSpaceMatrix;
86 auto nv12 = _outer.nv12();
87 (void)nv12;
88 fColorSpaceMatrixVar =
89 args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf4x4_GrSLType,
90 kDefault_GrSLPrecision, "colorSpaceMatrix");
91 SkString sk_TransformedCoords2D_0 = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]);
92 SkString sk_TransformedCoords2D_1 = fragBuilder->ensureCoords2D(args.fTransformedCoords[1]);
93 SkString sk_TransformedCoords2D_2 = fragBuilder->ensureCoords2D(args.fTransformedCoords[2]);
94 fragBuilder->codeAppendf(
95 "@if (%s) {\n %s = half4(texture(%s, %s).%s.x, texture(%s, %s).%s.xy, 1.0) * "
96 "%s;\n} else {\n %s = half4(texture(%s, %s).%s.x, texture(%s, %s).%s.x, "
97 "texture(%s, %s).%s.x, 1.0) * %s;\n}\n",
98 (_outer.nv12() ? "true" : "false"), args.fOutputColor,
99 fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[0]).c_str(),
100 sk_TransformedCoords2D_0.c_str(),
101 fragBuilder->getProgramBuilder()->samplerSwizzle(args.fTexSamplers[0]).c_str(),
102 fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[1]).c_str(),
103 sk_TransformedCoords2D_1.c_str(),
104 fragBuilder->getProgramBuilder()->samplerSwizzle(args.fTexSamplers[1]).c_str(),
105 args.fUniformHandler->getUniformCStr(fColorSpaceMatrixVar), args.fOutputColor,
106 fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[0]).c_str(),
107 sk_TransformedCoords2D_0.c_str(),
108 fragBuilder->getProgramBuilder()->samplerSwizzle(args.fTexSamplers[0]).c_str(),
109 fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[1]).c_str(),
110 sk_TransformedCoords2D_1.c_str(),
111 fragBuilder->getProgramBuilder()->samplerSwizzle(args.fTexSamplers[1]).c_str(),
112 fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[2]).c_str(),
113 sk_TransformedCoords2D_2.c_str(),
114 fragBuilder->getProgramBuilder()->samplerSwizzle(args.fTexSamplers[2]).c_str(),
115 args.fUniformHandler->getUniformCStr(fColorSpaceMatrixVar));
116 }
117
118 private:
onSetData(const GrGLSLProgramDataManager & pdman,const GrFragmentProcessor & _proc)119 void onSetData(const GrGLSLProgramDataManager& pdman,
120 const GrFragmentProcessor& _proc) override {
121 const GrYUVtoRGBEffect& _outer = _proc.cast<GrYUVtoRGBEffect>();
122 {
123 float colorSpaceMatrixValue[16];
124 _outer.colorSpaceMatrix().asColMajorf(colorSpaceMatrixValue);
125 pdman.setMatrix4f(fColorSpaceMatrixVar, colorSpaceMatrixValue);
126 }
127 }
128 UniformHandle fColorSpaceMatrixVar;
129 };
onCreateGLSLInstance() const130 GrGLSLFragmentProcessor* GrYUVtoRGBEffect::onCreateGLSLInstance() const {
131 return new GrGLSLYUVtoRGBEffect();
132 }
onGetGLSLProcessorKey(const GrShaderCaps & caps,GrProcessorKeyBuilder * b) const133 void GrYUVtoRGBEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
134 GrProcessorKeyBuilder* b) const {
135 b->add32((int32_t)fNv12);
136 }
onIsEqual(const GrFragmentProcessor & other) const137 bool GrYUVtoRGBEffect::onIsEqual(const GrFragmentProcessor& other) const {
138 const GrYUVtoRGBEffect& that = other.cast<GrYUVtoRGBEffect>();
139 (void)that;
140 if (fYSampler != that.fYSampler) return false;
141 if (fYSamplerTransform != that.fYSamplerTransform) return false;
142 if (fUSampler != that.fUSampler) return false;
143 if (fUSamplerTransform != that.fUSamplerTransform) return false;
144 if (fVSampler != that.fVSampler) return false;
145 if (fVSamplerTransform != that.fVSamplerTransform) return false;
146 if (fColorSpaceMatrix != that.fColorSpaceMatrix) return false;
147 if (fNv12 != that.fNv12) return false;
148 return true;
149 }
GrYUVtoRGBEffect(const GrYUVtoRGBEffect & src)150 GrYUVtoRGBEffect::GrYUVtoRGBEffect(const GrYUVtoRGBEffect& src)
151 : INHERITED(kGrYUVtoRGBEffect_ClassID, src.optimizationFlags())
152 , fYSampler(src.fYSampler)
153 , fYSamplerTransform(src.fYSamplerTransform)
154 , fUSampler(src.fUSampler)
155 , fUSamplerTransform(src.fUSamplerTransform)
156 , fVSampler(src.fVSampler)
157 , fVSamplerTransform(src.fVSamplerTransform)
158 , fColorSpaceMatrix(src.fColorSpaceMatrix)
159 , fNv12(src.fNv12)
160 , fYSamplerCoordTransform(src.fYSamplerCoordTransform)
161 , fUSamplerCoordTransform(src.fUSamplerCoordTransform)
162 , fVSamplerCoordTransform(src.fVSamplerCoordTransform) {
163 this->addTextureSampler(&fYSampler);
164 this->addTextureSampler(&fUSampler);
165 this->addTextureSampler(&fVSampler);
166 this->addCoordTransform(&fYSamplerCoordTransform);
167 this->addCoordTransform(&fUSamplerCoordTransform);
168 this->addCoordTransform(&fVSamplerCoordTransform);
169 }
clone() const170 std::unique_ptr<GrFragmentProcessor> GrYUVtoRGBEffect::clone() const {
171 return std::unique_ptr<GrFragmentProcessor>(new GrYUVtoRGBEffect(*this));
172 }
173 #endif
174