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 "GrInvariantOutput.h"
12 #include "GrProcessor.h"
13 #include "gl/GrGLProcessor.h"
14 #include "gl/builders/GrGLProgramBuilder.h"
15 
16 namespace {
17 
18 class YUVtoRGBEffect : public GrFragmentProcessor {
19 public:
Create(GrTexture * yTexture,GrTexture * uTexture,GrTexture * vTexture,SkISize sizes[3],SkYUVColorSpace colorSpace)20     static GrFragmentProcessor* Create(GrTexture* yTexture, GrTexture* uTexture,
21                                        GrTexture* vTexture, SkISize sizes[3],
22                                        SkYUVColorSpace colorSpace) {
23         SkScalar w[3], h[3];
24         w[0] = SkIntToScalar(sizes[0].fWidth)  / SkIntToScalar(yTexture->width());
25         h[0] = SkIntToScalar(sizes[0].fHeight) / SkIntToScalar(yTexture->height());
26         w[1] = SkIntToScalar(sizes[1].fWidth)  / SkIntToScalar(uTexture->width());
27         h[1] = SkIntToScalar(sizes[1].fHeight) / SkIntToScalar(uTexture->height());
28         w[2] = SkIntToScalar(sizes[2].fWidth)  / SkIntToScalar(vTexture->width());
29         h[2] = SkIntToScalar(sizes[2].fHeight) / SkIntToScalar(vTexture->height());
30         SkMatrix yuvMatrix[3];
31         yuvMatrix[0] = GrCoordTransform::MakeDivByTextureWHMatrix(yTexture);
32         yuvMatrix[1] = yuvMatrix[0];
33         yuvMatrix[1].preScale(w[1] / w[0], h[1] / h[0]);
34         yuvMatrix[2] = yuvMatrix[0];
35         yuvMatrix[2].preScale(w[2] / w[0], h[2] / h[0]);
36         GrTextureParams::FilterMode uvFilterMode =
37             ((sizes[1].fWidth  != sizes[0].fWidth) ||
38              (sizes[1].fHeight != sizes[0].fHeight) ||
39              (sizes[2].fWidth  != sizes[0].fWidth) ||
40              (sizes[2].fHeight != sizes[0].fHeight)) ?
41             GrTextureParams::kBilerp_FilterMode :
42             GrTextureParams::kNone_FilterMode;
43         return SkNEW_ARGS(YUVtoRGBEffect, (yTexture, uTexture, vTexture, yuvMatrix,
44                                            uvFilterMode, colorSpace));
45     }
46 
name() const47     const char* name() const override { return "YUV to RGB"; }
48 
getColorSpace() const49     SkYUVColorSpace getColorSpace() const {
50         return fColorSpace;
51     }
52 
53     class GLProcessor : public GrGLFragmentProcessor {
54     public:
55         static const GrGLfloat kJPEGConversionMatrix[16];
56         static const GrGLfloat kRec601ConversionMatrix[16];
57 
58         // this class always generates the same code.
GenKey(const GrProcessor &,const GrGLSLCaps &,GrProcessorKeyBuilder *)59         static void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*) {}
60 
GLProcessor(const GrProcessor &)61         GLProcessor(const GrProcessor&) {}
62 
emitCode(GrGLFPBuilder * builder,const GrFragmentProcessor &,const char * outputColor,const char * inputColor,const TransformedCoordsArray & coords,const TextureSamplerArray & samplers)63         virtual void emitCode(GrGLFPBuilder* builder,
64                               const GrFragmentProcessor&,
65                               const char* outputColor,
66                               const char* inputColor,
67                               const TransformedCoordsArray& coords,
68                               const TextureSamplerArray& samplers) override {
69             GrGLFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
70 
71             const char* yuvMatrix   = NULL;
72             fMatrixUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
73                                              kMat44f_GrSLType, kDefault_GrSLPrecision,
74                                              "YUVMatrix", &yuvMatrix);
75             fsBuilder->codeAppendf("\t%s = vec4(\n\t\t", outputColor);
76             fsBuilder->appendTextureLookup(samplers[0], coords[0].c_str(), coords[0].getType());
77             fsBuilder->codeAppend(".r,\n\t\t");
78             fsBuilder->appendTextureLookup(samplers[1], coords[1].c_str(), coords[1].getType());
79             fsBuilder->codeAppend(".r,\n\t\t");
80             fsBuilder->appendTextureLookup(samplers[2], coords[2].c_str(), coords[2].getType());
81             fsBuilder->codeAppendf(".r,\n\t\t1.0) * %s;\n", yuvMatrix);
82         }
83 
setData(const GrGLProgramDataManager & pdman,const GrProcessor & processor)84         virtual void setData(const GrGLProgramDataManager& pdman,
85                              const GrProcessor& processor) override {
86             const YUVtoRGBEffect& yuvEffect = processor.cast<YUVtoRGBEffect>();
87             switch (yuvEffect.getColorSpace()) {
88                 case kJPEG_SkYUVColorSpace:
89                     pdman.setMatrix4f(fMatrixUni, kJPEGConversionMatrix);
90                     break;
91                 case kRec601_SkYUVColorSpace:
92                     pdman.setMatrix4f(fMatrixUni, kRec601ConversionMatrix);
93                     break;
94             }
95         }
96 
97     private:
98         GrGLProgramDataManager::UniformHandle fMatrixUni;
99 
100         typedef GrGLFragmentProcessor INHERITED;
101     };
102 
getGLProcessorKey(const GrGLSLCaps & caps,GrProcessorKeyBuilder * b) const103     virtual void getGLProcessorKey(const GrGLSLCaps& caps,
104                                    GrProcessorKeyBuilder* b) const override {
105         GLProcessor::GenKey(*this, caps, b);
106     }
107 
createGLInstance() const108     GrGLFragmentProcessor* createGLInstance() const override {
109         return SkNEW_ARGS(GLProcessor, (*this));
110     }
111 
112 private:
YUVtoRGBEffect(GrTexture * yTexture,GrTexture * uTexture,GrTexture * vTexture,SkMatrix yuvMatrix[3],GrTextureParams::FilterMode uvFilterMode,SkYUVColorSpace colorSpace)113     YUVtoRGBEffect(GrTexture* yTexture, GrTexture* uTexture, GrTexture* vTexture,
114                    SkMatrix yuvMatrix[3], GrTextureParams::FilterMode uvFilterMode,
115                    SkYUVColorSpace colorSpace)
116     : fYTransform(kLocal_GrCoordSet, yuvMatrix[0], yTexture, GrTextureParams::kNone_FilterMode)
117     , fYAccess(yTexture)
118     , fUTransform(kLocal_GrCoordSet, yuvMatrix[1], uTexture, uvFilterMode)
119     , fUAccess(uTexture, uvFilterMode)
120     , fVTransform(kLocal_GrCoordSet, yuvMatrix[2], vTexture, uvFilterMode)
121     , fVAccess(vTexture, uvFilterMode)
122     , fColorSpace(colorSpace) {
123         this->initClassID<YUVtoRGBEffect>();
124         this->addCoordTransform(&fYTransform);
125         this->addTextureAccess(&fYAccess);
126         this->addCoordTransform(&fUTransform);
127         this->addTextureAccess(&fUAccess);
128         this->addCoordTransform(&fVTransform);
129         this->addTextureAccess(&fVAccess);
130     }
131 
onIsEqual(const GrFragmentProcessor & sBase) const132     bool onIsEqual(const GrFragmentProcessor& sBase) const override {
133         const YUVtoRGBEffect& s = sBase.cast<YUVtoRGBEffect>();
134         return fColorSpace == s.getColorSpace();
135     }
136 
onComputeInvariantOutput(GrInvariantOutput * inout) const137     void onComputeInvariantOutput(GrInvariantOutput* inout) const override {
138         // YUV is opaque
139         inout->setToOther(kA_GrColorComponentFlag, 0xFF << GrColor_SHIFT_A,
140                           GrInvariantOutput::kWillNot_ReadInput);
141     }
142 
143     GrCoordTransform fYTransform;
144     GrTextureAccess fYAccess;
145     GrCoordTransform fUTransform;
146     GrTextureAccess fUAccess;
147     GrCoordTransform fVTransform;
148     GrTextureAccess fVAccess;
149     SkYUVColorSpace fColorSpace;
150 
151     typedef GrFragmentProcessor INHERITED;
152 };
153 
154 const GrGLfloat YUVtoRGBEffect::GLProcessor::kJPEGConversionMatrix[16] = {
155     1.0f,  0.0f,      1.402f,  -0.701f,
156     1.0f, -0.34414f, -0.71414f, 0.529f,
157     1.0f,  1.772f,    0.0f,    -0.886f,
158     0.0f,  0.0f,      0.0f,     1.0};
159 const GrGLfloat YUVtoRGBEffect::GLProcessor::kRec601ConversionMatrix[16] = {
160     1.164f,  0.0f,    1.596f, -0.87075f,
161     1.164f, -0.391f, -0.813f,  0.52925f,
162     1.164f,  2.018f,  0.0f,   -1.08175f,
163     0.0f,    0.0f,    0.0f,    1.0};
164 }
165 
166 //////////////////////////////////////////////////////////////////////////////
167 
168 GrFragmentProcessor*
Create(GrTexture * yTexture,GrTexture * uTexture,GrTexture * vTexture,SkISize sizes[3],SkYUVColorSpace colorSpace)169 GrYUVtoRGBEffect::Create(GrTexture* yTexture, GrTexture* uTexture, GrTexture* vTexture,
170                          SkISize sizes[3], SkYUVColorSpace colorSpace) {
171     SkASSERT(yTexture && uTexture && vTexture && sizes);
172     return YUVtoRGBEffect::Create(yTexture, uTexture, vTexture, sizes, colorSpace);
173 }
174