sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 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 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 8 | #include "gl/builders/GrGLProgramBuilder.h" |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 9 | #include "GrYUVtoRGBEffect.h" |
| 10 | |
| 11 | #include "GrCoordTransform.h" |
| 12 | #include "GrEffect.h" |
| 13 | #include "gl/GrGLEffect.h" |
| 14 | #include "GrTBackendEffectFactory.h" |
| 15 | |
| 16 | namespace { |
| 17 | |
| 18 | class YUVtoRGBEffect : public GrEffect { |
| 19 | public: |
bsalomon | 97b9ab7 | 2014-07-08 06:52:35 -0700 | [diff] [blame] | 20 | static GrEffect* Create(GrTexture* yTexture, GrTexture* uTexture, GrTexture* vTexture) { |
bsalomon | 55fad7a | 2014-07-08 07:34:20 -0700 | [diff] [blame] | 21 | return SkNEW_ARGS(YUVtoRGBEffect, (yTexture, uTexture, vTexture)); |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | static const char* Name() { return "YUV to RGB"; } |
| 25 | |
| 26 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { |
| 27 | return GrTBackendEffectFactory<YUVtoRGBEffect>::getInstance(); |
| 28 | } |
| 29 | |
| 30 | virtual void getConstantColorComponents(GrColor* color, |
| 31 | uint32_t* validFlags) const SK_OVERRIDE { |
| 32 | // YUV is opaque |
| 33 | *color = 0xFF; |
| 34 | *validFlags = kA_GrColorComponentFlag; |
| 35 | } |
| 36 | |
| 37 | class GLEffect : public GrGLEffect { |
| 38 | public: |
| 39 | // this class always generates the same code. |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 40 | static void GenKey(const GrDrawEffect&, const GrGLCaps&, GrEffectKeyBuilder*) {} |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 41 | |
| 42 | GLEffect(const GrBackendEffectFactory& factory, |
| 43 | const GrDrawEffect&) |
| 44 | : INHERITED(factory) { |
| 45 | } |
| 46 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 47 | virtual void emitCode(GrGLProgramBuilder* builder, |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 48 | const GrDrawEffect&, |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 49 | const GrEffectKey&, |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 50 | const char* outputColor, |
| 51 | const char* inputColor, |
| 52 | const TransformedCoordsArray& coords, |
| 53 | const TextureSamplerArray& samplers) SK_OVERRIDE { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 54 | GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder(); |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 55 | const char* yuvMatrix = "yuvMatrix"; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 56 | fsBuilder->codeAppendf("\tconst mat4 %s = mat4(1.0, 0.0, 1.402, -0.701,\n\t\t\t" |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 57 | "1.0, -0.344, -0.714, 0.529,\n\t\t\t" |
| 58 | "1.0, 1.772, 0.0, -0.886,\n\t\t\t" |
| 59 | "0.0, 0.0, 0.0, 1.0);\n", |
| 60 | yuvMatrix); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 61 | fsBuilder->codeAppendf("\t%s = vec4(\n\t\t", outputColor); |
| 62 | fsBuilder->appendTextureLookup(samplers[0], coords[0].c_str(), coords[0].type()); |
| 63 | fsBuilder->codeAppend(".r,\n\t\t"); |
| 64 | fsBuilder->appendTextureLookup(samplers[1], coords[0].c_str(), coords[0].type()); |
| 65 | fsBuilder->codeAppend(".r,\n\t\t"); |
| 66 | fsBuilder->appendTextureLookup(samplers[2], coords[0].c_str(), coords[0].type()); |
| 67 | fsBuilder->codeAppendf(".r,\n\t\t1.0) * %s;\n", yuvMatrix); |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | typedef GrGLEffect INHERITED; |
| 71 | }; |
| 72 | |
| 73 | private: |
| 74 | YUVtoRGBEffect(GrTexture* yTexture, GrTexture* uTexture, GrTexture* vTexture) |
bsalomon | 6267f81 | 2014-08-29 15:05:53 -0700 | [diff] [blame] | 75 | : fCoordTransform(kLocal_GrCoordSet, GrCoordTransform::MakeDivByTextureWHMatrix(yTexture), |
| 76 | yTexture) |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 77 | , fYAccess(yTexture) |
| 78 | , fUAccess(uTexture) |
| 79 | , fVAccess(vTexture) { |
| 80 | this->addCoordTransform(&fCoordTransform); |
| 81 | this->addTextureAccess(&fYAccess); |
| 82 | this->addTextureAccess(&fUAccess); |
| 83 | this->addTextureAccess(&fVAccess); |
| 84 | this->setWillNotUseInputColor(); |
| 85 | } |
| 86 | |
| 87 | virtual bool onIsEqual(const GrEffect& sBase) const { |
| 88 | const YUVtoRGBEffect& s = CastEffect<YUVtoRGBEffect>(sBase); |
| 89 | return fYAccess.getTexture() == s.fYAccess.getTexture() && |
| 90 | fUAccess.getTexture() == s.fUAccess.getTexture() && |
| 91 | fVAccess.getTexture() == s.fVAccess.getTexture(); |
| 92 | } |
| 93 | |
| 94 | GrCoordTransform fCoordTransform; |
| 95 | GrTextureAccess fYAccess; |
| 96 | GrTextureAccess fUAccess; |
| 97 | GrTextureAccess fVAccess; |
| 98 | |
| 99 | typedef GrEffect INHERITED; |
| 100 | }; |
| 101 | |
| 102 | } |
| 103 | |
| 104 | ////////////////////////////////////////////////////////////////////////////// |
| 105 | |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 106 | GrEffect* GrYUVtoRGBEffect::Create(GrTexture* yTexture, GrTexture* uTexture, GrTexture* vTexture) { |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 107 | return YUVtoRGBEffect::Create(yTexture, uTexture, vTexture); |
| 108 | } |