OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "gl/builders/GrGLProgramBuilder.h" | 8 #include "gl/builders/GrGLProgramBuilder.h" |
9 #include "GrYUVtoRGBEffect.h" | 9 #include "GrYUVtoRGBEffect.h" |
10 | 10 |
11 #include "GrCoordTransform.h" | 11 #include "GrCoordTransform.h" |
12 #include "GrEffect.h" | 12 #include "GrEffect.h" |
13 #include "gl/GrGLEffect.h" | 13 #include "gl/GrGLEffect.h" |
14 #include "GrTBackendEffectFactory.h" | 14 #include "GrTBackendEffectFactory.h" |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 class YUVtoRGBEffect : public GrEffect { | 18 class YUVtoRGBEffect : public GrEffect { |
19 public: | 19 public: |
20 static GrEffect* Create(GrTexture* yTexture, GrTexture* uTexture, GrTexture* vTexture) { | 20 static GrEffect* Create(GrTexture* yTexture, GrTexture* uTexture, GrTexture* vTexture, |
21 return SkNEW_ARGS(YUVtoRGBEffect, (yTexture, uTexture, vTexture)); | 21 SkPixelRef::YUVColorSpace colorSpace) { |
22 return SkNEW_ARGS(YUVtoRGBEffect, (yTexture, uTexture, vTexture, colorSp ace)); | |
22 } | 23 } |
23 | 24 |
24 static const char* Name() { return "YUV to RGB"; } | 25 static const char* Name() { return "YUV to RGB"; } |
25 | 26 |
26 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { | 27 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { |
27 return GrTBackendEffectFactory<YUVtoRGBEffect>::getInstance(); | 28 return GrTBackendEffectFactory<YUVtoRGBEffect>::getInstance(); |
28 } | 29 } |
29 | 30 |
30 virtual void getConstantColorComponents(GrColor* color, | 31 virtual void getConstantColorComponents(GrColor* color, |
31 uint32_t* validFlags) const SK_OVERR IDE { | 32 uint32_t* validFlags) const SK_OVERR IDE { |
32 // YUV is opaque | 33 // YUV is opaque |
33 *color = 0xFF; | 34 *color = 0xFF; |
34 *validFlags = kA_GrColorComponentFlag; | 35 *validFlags = kA_GrColorComponentFlag; |
35 } | 36 } |
36 | 37 |
38 SkPixelRef::YUVColorSpace getColorSpace() const { | |
39 return fColorSpace; | |
40 } | |
41 | |
37 class GLEffect : public GrGLEffect { | 42 class GLEffect : public GrGLEffect { |
38 public: | 43 public: |
44 static const GrGLfloat kJPEGConversionMatrix[16]; | |
45 static const GrGLfloat kRec601ConversionMatrix[16]; | |
46 | |
39 // this class always generates the same code. | 47 // this class always generates the same code. |
40 static void GenKey(const GrDrawEffect&, const GrGLCaps&, GrEffectKeyBuil der*) {} | 48 static void GenKey(const GrDrawEffect&, const GrGLCaps&, GrEffectKeyBuil der*) {} |
41 | 49 |
42 GLEffect(const GrBackendEffectFactory& factory, | 50 GLEffect(const GrBackendEffectFactory& factory, |
43 const GrDrawEffect&) | 51 const GrDrawEffect&) |
44 : INHERITED(factory) { | 52 : INHERITED(factory) { |
45 } | 53 } |
46 | 54 |
47 virtual void emitCode(GrGLProgramBuilder* builder, | 55 virtual void emitCode(GrGLProgramBuilder* builder, |
48 const GrDrawEffect&, | 56 const GrDrawEffect& drawEffect, |
49 const GrEffectKey&, | 57 const GrEffectKey&, |
50 const char* outputColor, | 58 const char* outputColor, |
51 const char* inputColor, | 59 const char* inputColor, |
52 const TransformedCoordsArray& coords, | 60 const TransformedCoordsArray& coords, |
53 const TextureSamplerArray& samplers) SK_OVERRIDE { | 61 const TextureSamplerArray& samplers) SK_OVERRIDE { |
54 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBui lder(); | 62 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBui lder(); |
55 const char* yuvMatrix = "yuvMatrix"; | 63 |
56 fsBuilder->codeAppendf("\tconst mat4 %s = mat4(1.0, 0.0, 1.402, -0.701,\n\t\t\t" | 64 const char* yuvMatrix = NULL; |
57 "1.0, -0.344, -0.714, 0.529,\n\t\t\t" | 65 fMatrixUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visib ility, |
sugoi1
2014/09/11 17:57:23
Hi, sorry for the late comment:
Instead of adding
| |
58 "1.0, 1.772, 0.0, -0.886,\n\t\t\t" | 66 kMat44f_GrSLType, "YUVMatrix", |
59 "0.0, 0.0, 0.0, 1.0);\n", | 67 &yuvMatrix); |
60 yuvMatrix); | |
61 fsBuilder->codeAppendf("\t%s = vec4(\n\t\t", outputColor); | 68 fsBuilder->codeAppendf("\t%s = vec4(\n\t\t", outputColor); |
62 fsBuilder->appendTextureLookup(samplers[0], coords[0].c_str(), coord s[0].type()); | 69 fsBuilder->appendTextureLookup(samplers[0], coords[0].c_str(), coord s[0].type()); |
63 fsBuilder->codeAppend(".r,\n\t\t"); | 70 fsBuilder->codeAppend(".r,\n\t\t"); |
64 fsBuilder->appendTextureLookup(samplers[1], coords[0].c_str(), coord s[0].type()); | 71 fsBuilder->appendTextureLookup(samplers[1], coords[0].c_str(), coord s[0].type()); |
65 fsBuilder->codeAppend(".r,\n\t\t"); | 72 fsBuilder->codeAppend(".r,\n\t\t"); |
66 fsBuilder->appendTextureLookup(samplers[2], coords[0].c_str(), coord s[0].type()); | 73 fsBuilder->appendTextureLookup(samplers[2], coords[0].c_str(), coord s[0].type()); |
67 fsBuilder->codeAppendf(".r,\n\t\t1.0) * %s;\n", yuvMatrix); | 74 fsBuilder->codeAppendf(".r,\n\t\t1.0) * %s;\n", yuvMatrix); |
68 } | 75 } |
69 | 76 |
77 virtual void setData(const GrGLProgramDataManager& pdman, | |
78 const GrDrawEffect& drawEffect) SK_OVERRIDE { | |
79 const YUVtoRGBEffect& yuvEffect = drawEffect.castEffect<YUVtoRGBEffe ct>(); | |
80 switch (yuvEffect.getColorSpace()) { | |
81 case SkPixelRef::kJPEG_YUVColorSpace: | |
82 pdman.setMatrix4f(fMatrixUni, kJPEGConversionMatrix); | |
83 break; | |
84 case SkPixelRef::kRec601_YUVColorSpace: | |
85 pdman.setMatrix4f(fMatrixUni, kRec601ConversionMatrix); | |
86 break; | |
87 } | |
88 } | |
89 | |
90 private: | |
91 GrGLProgramDataManager::UniformHandle fMatrixUni; | |
92 | |
70 typedef GrGLEffect INHERITED; | 93 typedef GrGLEffect INHERITED; |
71 }; | 94 }; |
72 | 95 |
73 private: | 96 private: |
74 YUVtoRGBEffect(GrTexture* yTexture, GrTexture* uTexture, GrTexture* vTexture ) | 97 YUVtoRGBEffect(GrTexture* yTexture, GrTexture* uTexture, GrTexture* vTexture , |
75 : fCoordTransform(kLocal_GrCoordSet, GrCoordTransform::MakeDivByTextureWHMat rix(yTexture), | 98 SkPixelRef::YUVColorSpace colorSpace) |
76 yTexture) | 99 : fCoordTransform(kLocal_GrCoordSet, GrCoordTransform::MakeDivByTextureWHMa trix(yTexture), |
100 yTexture) | |
77 , fYAccess(yTexture) | 101 , fYAccess(yTexture) |
78 , fUAccess(uTexture) | 102 , fUAccess(uTexture) |
79 , fVAccess(vTexture) { | 103 , fVAccess(vTexture) |
104 , fColorSpace(colorSpace) { | |
80 this->addCoordTransform(&fCoordTransform); | 105 this->addCoordTransform(&fCoordTransform); |
81 this->addTextureAccess(&fYAccess); | 106 this->addTextureAccess(&fYAccess); |
82 this->addTextureAccess(&fUAccess); | 107 this->addTextureAccess(&fUAccess); |
83 this->addTextureAccess(&fVAccess); | 108 this->addTextureAccess(&fVAccess); |
84 this->setWillNotUseInputColor(); | 109 this->setWillNotUseInputColor(); |
85 } | 110 } |
86 | 111 |
87 virtual bool onIsEqual(const GrEffect& sBase) const { | 112 virtual bool onIsEqual(const GrEffect& sBase) const { |
88 const YUVtoRGBEffect& s = CastEffect<YUVtoRGBEffect>(sBase); | 113 const YUVtoRGBEffect& s = CastEffect<YUVtoRGBEffect>(sBase); |
89 return fYAccess.getTexture() == s.fYAccess.getTexture() && | 114 return fYAccess.getTexture() == s.fYAccess.getTexture() && |
90 fUAccess.getTexture() == s.fUAccess.getTexture() && | 115 fUAccess.getTexture() == s.fUAccess.getTexture() && |
91 fVAccess.getTexture() == s.fVAccess.getTexture(); | 116 fVAccess.getTexture() == s.fVAccess.getTexture() && |
117 fColorSpace == s.getColorSpace(); | |
92 } | 118 } |
93 | 119 |
94 GrCoordTransform fCoordTransform; | 120 GrCoordTransform fCoordTransform; |
95 GrTextureAccess fYAccess; | 121 GrTextureAccess fYAccess; |
96 GrTextureAccess fUAccess; | 122 GrTextureAccess fUAccess; |
97 GrTextureAccess fVAccess; | 123 GrTextureAccess fVAccess; |
124 SkPixelRef::YUVColorSpace fColorSpace; | |
98 | 125 |
99 typedef GrEffect INHERITED; | 126 typedef GrEffect INHERITED; |
100 }; | 127 }; |
101 | 128 |
129 const GrGLfloat YUVtoRGBEffect::GLEffect::kJPEGConversionMatrix[16] = { | |
130 1.0, 0.0, 1.402, -0.701, | |
131 1.0, -0.34414, -0.71414, 0.529, | |
132 1.0, 1.772, 0.0, -0.886, | |
133 0.0, 0.0, 0.0, 1.0}; | |
134 const GrGLfloat YUVtoRGBEffect::GLEffect::kRec601ConversionMatrix[16] = { | |
135 1.164, 0.0, 1.596, -1.08175, | |
136 1.164, -0.391, -0.813, 0.529, | |
137 1.164, 2.018, 0.0, -1.08175, | |
138 0.0, 0.0, 0.0, 1.0}; | |
102 } | 139 } |
103 | 140 |
104 ////////////////////////////////////////////////////////////////////////////// | 141 ////////////////////////////////////////////////////////////////////////////// |
105 | 142 |
106 GrEffect* GrYUVtoRGBEffect::Create(GrTexture* yTexture, GrTexture* uTexture, GrT exture* vTexture) { | 143 GrEffect* GrYUVtoRGBEffect::Create(GrTexture* yTexture, GrTexture* uTexture, GrT exture* vTexture, |
107 return YUVtoRGBEffect::Create(yTexture, uTexture, vTexture); | 144 SkPixelRef::YUVColorSpace colorSpace) { |
145 return YUVtoRGBEffect::Create(yTexture, uTexture, vTexture, colorSpace); | |
108 } | 146 } |
OLD | NEW |