OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2014 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 #include "GrYUVtoRGBEffect.h" | |
9 | |
10 #include "GrCoordTransform.h" | |
11 #include "GrFragmentProcessor.h" | |
12 #include "GrInvariantOutput.h" | |
13 #include "GrProcessor.h" | |
14 #include "glsl/GrGLSLFragmentProcessor.h" | |
15 #include "glsl/GrGLSLFragmentShaderBuilder.h" | |
16 #include "glsl/GrGLSLProgramDataManager.h" | |
17 #include "glsl/GrGLSLUniformHandler.h" | |
18 | |
19 namespace { | |
20 | |
21 class YUVtoRGBEffect : public GrFragmentProcessor { | |
22 public: | |
23 static GrFragmentProcessor* Create(GrTexture* yTexture, GrTexture* uTexture, | |
24 GrTexture* vTexture, const SkISize sizes[
3], | |
25 SkYUVColorSpace colorSpace) { | |
26 SkScalar w[3], h[3]; | |
27 w[0] = SkIntToScalar(sizes[0].fWidth) / SkIntToScalar(yTexture->width()
); | |
28 h[0] = SkIntToScalar(sizes[0].fHeight) / SkIntToScalar(yTexture->height(
)); | |
29 w[1] = SkIntToScalar(sizes[1].fWidth) / SkIntToScalar(uTexture->width()
); | |
30 h[1] = SkIntToScalar(sizes[1].fHeight) / SkIntToScalar(uTexture->height(
)); | |
31 w[2] = SkIntToScalar(sizes[2].fWidth) / SkIntToScalar(vTexture->width()
); | |
32 h[2] = SkIntToScalar(sizes[2].fHeight) / SkIntToScalar(vTexture->height(
)); | |
33 SkMatrix yuvMatrix[3]; | |
34 yuvMatrix[0] = GrCoordTransform::MakeDivByTextureWHMatrix(yTexture); | |
35 yuvMatrix[1] = yuvMatrix[0]; | |
36 yuvMatrix[1].preScale(w[1] / w[0], h[1] / h[0]); | |
37 yuvMatrix[2] = yuvMatrix[0]; | |
38 yuvMatrix[2].preScale(w[2] / w[0], h[2] / h[0]); | |
39 GrTextureParams::FilterMode uvFilterMode = | |
40 ((sizes[1].fWidth != sizes[0].fWidth) || | |
41 (sizes[1].fHeight != sizes[0].fHeight) || | |
42 (sizes[2].fWidth != sizes[0].fWidth) || | |
43 (sizes[2].fHeight != sizes[0].fHeight)) ? | |
44 GrTextureParams::kBilerp_FilterMode : | |
45 GrTextureParams::kNone_FilterMode; | |
46 return new YUVtoRGBEffect(yTexture, uTexture, vTexture, yuvMatrix, uvFil
terMode, | |
47 colorSpace); | |
48 } | |
49 | |
50 const char* name() const override { return "YUV to RGB"; } | |
51 | |
52 SkYUVColorSpace getColorSpace() const { | |
53 return fColorSpace; | |
54 } | |
55 | |
56 class GLSLProcessor : public GrGLSLFragmentProcessor { | |
57 public: | |
58 static const float kJPEGConversionMatrix[16]; | |
59 static const float kRec601ConversionMatrix[16]; | |
60 static const float kRec709ConversionMatrix[16]; | |
61 | |
62 // this class always generates the same code. | |
63 static void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKey
Builder*) {} | |
64 | |
65 GLSLProcessor(const GrProcessor&) {} | |
66 | |
67 virtual void emitCode(EmitArgs& args) override { | |
68 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; | |
69 | |
70 const char* yuvMatrix = nullptr; | |
71 fMatrixUni = args.fUniformHandler->addUniform( | |
72 GrGLSLUniformHandler::k
Fragment_Visibility, | |
73 kMat44f_GrSLType, kDefa
ult_GrSLPrecision, | |
74 "YUVMatrix", &yuvMatrix
); | |
75 fragBuilder->codeAppendf("\t%s = vec4(\n\t\t", args.fOutputColor); | |
76 fragBuilder->appendTextureLookup(args.fSamplers[0], args.fCoords[0].
c_str(), | |
77 args.fCoords[0].getType()); | |
78 fragBuilder->codeAppend(".r,\n\t\t"); | |
79 fragBuilder->appendTextureLookup(args.fSamplers[1], args.fCoords[1].
c_str(), | |
80 args.fCoords[1].getType()); | |
81 fragBuilder->codeAppend(".r,\n\t\t"); | |
82 fragBuilder->appendTextureLookup(args.fSamplers[2], args.fCoords[2].
c_str(), | |
83 args.fCoords[2].getType()); | |
84 fragBuilder->codeAppendf(".r,\n\t\t1.0) * %s;\n", yuvMatrix); | |
85 } | |
86 | |
87 protected: | |
88 virtual void onSetData(const GrGLSLProgramDataManager& pdman, | |
89 const GrProcessor& processor) override { | |
90 const YUVtoRGBEffect& yuvEffect = processor.cast<YUVtoRGBEffect>(); | |
91 switch (yuvEffect.getColorSpace()) { | |
92 case kJPEG_SkYUVColorSpace: | |
93 pdman.setMatrix4f(fMatrixUni, kJPEGConversionMatrix); | |
94 break; | |
95 case kRec601_SkYUVColorSpace: | |
96 pdman.setMatrix4f(fMatrixUni, kRec601ConversionMatrix); | |
97 break; | |
98 case kRec709_SkYUVColorSpace: | |
99 pdman.setMatrix4f(fMatrixUni, kRec709ConversionMatrix); | |
100 break; | |
101 } | |
102 } | |
103 | |
104 private: | |
105 GrGLSLProgramDataManager::UniformHandle fMatrixUni; | |
106 | |
107 typedef GrGLSLFragmentProcessor INHERITED; | |
108 }; | |
109 | |
110 private: | |
111 YUVtoRGBEffect(GrTexture* yTexture, GrTexture* uTexture, GrTexture* vTexture
, | |
112 const SkMatrix yuvMatrix[3], GrTextureParams::FilterMode uvFi
lterMode, | |
113 SkYUVColorSpace colorSpace) | |
114 : fYTransform(kLocal_GrCoordSet, yuvMatrix[0], yTexture, GrTextureParams::kN
one_FilterMode) | |
115 , fYAccess(yTexture) | |
116 , fUTransform(kLocal_GrCoordSet, yuvMatrix[1], uTexture, uvFilterMode) | |
117 , fUAccess(uTexture, uvFilterMode) | |
118 , fVTransform(kLocal_GrCoordSet, yuvMatrix[2], vTexture, uvFilterMode) | |
119 , fVAccess(vTexture, uvFilterMode) | |
120 , fColorSpace(colorSpace) { | |
121 this->initClassID<YUVtoRGBEffect>(); | |
122 this->addCoordTransform(&fYTransform); | |
123 this->addTextureAccess(&fYAccess); | |
124 this->addCoordTransform(&fUTransform); | |
125 this->addTextureAccess(&fUAccess); | |
126 this->addCoordTransform(&fVTransform); | |
127 this->addTextureAccess(&fVAccess); | |
128 } | |
129 | |
130 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { | |
131 return new GLSLProcessor(*this); | |
132 } | |
133 | |
134 virtual void onGetGLSLProcessorKey(const GrGLSLCaps& caps, | |
135 GrProcessorKeyBuilder* b) const override
{ | |
136 GLSLProcessor::GenKey(*this, caps, b); | |
137 } | |
138 | |
139 bool onIsEqual(const GrFragmentProcessor& sBase) const override { | |
140 const YUVtoRGBEffect& s = sBase.cast<YUVtoRGBEffect>(); | |
141 return fColorSpace == s.getColorSpace(); | |
142 } | |
143 | |
144 void onComputeInvariantOutput(GrInvariantOutput* inout) const override { | |
145 // YUV is opaque | |
146 inout->setToOther(kA_GrColorComponentFlag, 0xFF << GrColor_SHIFT_A, | |
147 GrInvariantOutput::kWillNot_ReadInput); | |
148 } | |
149 | |
150 GrCoordTransform fYTransform; | |
151 GrTextureAccess fYAccess; | |
152 GrCoordTransform fUTransform; | |
153 GrTextureAccess fUAccess; | |
154 GrCoordTransform fVTransform; | |
155 GrTextureAccess fVAccess; | |
156 SkYUVColorSpace fColorSpace; | |
157 | |
158 typedef GrFragmentProcessor INHERITED; | |
159 }; | |
160 | |
161 const float YUVtoRGBEffect::GLSLProcessor::kJPEGConversionMatrix[16] = { | |
162 1.0f, 0.0f, 1.402f, -0.701f, | |
163 1.0f, -0.34414f, -0.71414f, 0.529f, | |
164 1.0f, 1.772f, 0.0f, -0.886f, | |
165 0.0f, 0.0f, 0.0f, 1.0}; | |
166 const float YUVtoRGBEffect::GLSLProcessor::kRec601ConversionMatrix[16] = { | |
167 1.164f, 0.0f, 1.596f, -0.87075f, | |
168 1.164f, -0.391f, -0.813f, 0.52925f, | |
169 1.164f, 2.018f, 0.0f, -1.08175f, | |
170 0.0f, 0.0f, 0.0f, 1.0}; | |
171 const float YUVtoRGBEffect::GLSLProcessor::kRec709ConversionMatrix[16] = { | |
172 1.164f, 0.0f, 1.793f, -0.96925f, | |
173 1.164f, -0.213f, -0.533f, 0.30025f, | |
174 1.164f, 2.112f, 0.0f, -1.12875f, | |
175 0.0f, 0.0f, 0.0f, 1.0f}; | |
176 } | |
177 | |
178 ////////////////////////////////////////////////////////////////////////////// | |
179 | |
180 GrFragmentProcessor* | |
181 GrYUVtoRGBEffect::Create(GrTexture* yTexture, GrTexture* uTexture, GrTexture* vT
exture, | |
182 const SkISize sizes[3], SkYUVColorSpace colorSpace) { | |
183 SkASSERT(yTexture && uTexture && vTexture && sizes); | |
184 return YUVtoRGBEffect::Create(yTexture, uTexture, vTexture, sizes, colorSpac
e); | |
185 } | |
OLD | NEW |