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 | |
| 8 | #include "GrYUVtoRGBEffect.h" |
| 9 | |
| 10 | #include "GrCoordTransform.h" |
| 11 | #include "GrEffect.h" |
| 12 | #include "gl/GrGLEffect.h" |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 13 | #include "gl/GrGLShaderBuilder.h" |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 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 | |
| 47 | virtual void emitCode(GrGLShaderBuilder* builder, |
| 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 { |
| 54 | const char* yuvMatrix = "yuvMatrix"; |
| 55 | builder->fsCodeAppendf("\tconst mat4 %s = mat4(1.0, 0.0, 1.402, -0.701,\n\t\t\t" |
| 56 | "1.0, -0.344, -0.714, 0.529,\n\t\t\t" |
| 57 | "1.0, 1.772, 0.0, -0.886,\n\t\t\t" |
| 58 | "0.0, 0.0, 0.0, 1.0);\n", |
| 59 | yuvMatrix); |
| 60 | builder->fsCodeAppendf("\t%s = vec4(\n\t\t", outputColor); |
| 61 | builder->fsAppendTextureLookup(samplers[0], coords[0].c_str(), coords[0].type()); |
| 62 | builder->fsCodeAppend(".r,\n\t\t"); |
| 63 | builder->fsAppendTextureLookup(samplers[1], coords[0].c_str(), coords[0].type()); |
| 64 | builder->fsCodeAppend(".r,\n\t\t"); |
| 65 | builder->fsAppendTextureLookup(samplers[2], coords[0].c_str(), coords[0].type()); |
| 66 | builder->fsCodeAppendf(".r,\n\t\t1.0) * %s;\n", yuvMatrix); |
| 67 | } |
| 68 | |
| 69 | typedef GrGLEffect INHERITED; |
| 70 | }; |
| 71 | |
| 72 | private: |
| 73 | YUVtoRGBEffect(GrTexture* yTexture, GrTexture* uTexture, GrTexture* vTexture) |
| 74 | : fCoordTransform(kLocal_GrCoordSet, MakeDivByTextureWHMatrix(yTexture), yTexture) |
| 75 | , fYAccess(yTexture) |
| 76 | , fUAccess(uTexture) |
| 77 | , fVAccess(vTexture) { |
| 78 | this->addCoordTransform(&fCoordTransform); |
| 79 | this->addTextureAccess(&fYAccess); |
| 80 | this->addTextureAccess(&fUAccess); |
| 81 | this->addTextureAccess(&fVAccess); |
| 82 | this->setWillNotUseInputColor(); |
| 83 | } |
| 84 | |
| 85 | virtual bool onIsEqual(const GrEffect& sBase) const { |
| 86 | const YUVtoRGBEffect& s = CastEffect<YUVtoRGBEffect>(sBase); |
| 87 | return fYAccess.getTexture() == s.fYAccess.getTexture() && |
| 88 | fUAccess.getTexture() == s.fUAccess.getTexture() && |
| 89 | fVAccess.getTexture() == s.fVAccess.getTexture(); |
| 90 | } |
| 91 | |
| 92 | GrCoordTransform fCoordTransform; |
| 93 | GrTextureAccess fYAccess; |
| 94 | GrTextureAccess fUAccess; |
| 95 | GrTextureAccess fVAccess; |
| 96 | |
| 97 | typedef GrEffect INHERITED; |
| 98 | }; |
| 99 | |
| 100 | } |
| 101 | |
| 102 | ////////////////////////////////////////////////////////////////////////////// |
| 103 | |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 104 | GrEffect* GrYUVtoRGBEffect::Create(GrTexture* yTexture, GrTexture* uTexture, GrTexture* vTexture) { |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 105 | return YUVtoRGBEffect::Create(yTexture, uTexture, vTexture); |
| 106 | } |