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: |
rileya | abaef86 | 2014-09-12 17:45:58 -0700 | [diff] [blame] | 20 | static GrEffect* Create(GrTexture* yTexture, GrTexture* uTexture, GrTexture* vTexture, |
| 21 | SkYUVColorSpace colorSpace) { |
| 22 | return SkNEW_ARGS(YUVtoRGBEffect, (yTexture, uTexture, vTexture, colorSpace)); |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | static const char* Name() { return "YUV to RGB"; } |
| 26 | |
| 27 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { |
| 28 | return GrTBackendEffectFactory<YUVtoRGBEffect>::getInstance(); |
| 29 | } |
| 30 | |
| 31 | virtual void getConstantColorComponents(GrColor* color, |
| 32 | uint32_t* validFlags) const SK_OVERRIDE { |
| 33 | // YUV is opaque |
| 34 | *color = 0xFF; |
| 35 | *validFlags = kA_GrColorComponentFlag; |
| 36 | } |
| 37 | |
rileya | abaef86 | 2014-09-12 17:45:58 -0700 | [diff] [blame] | 38 | SkYUVColorSpace getColorSpace() const { |
| 39 | return fColorSpace; |
| 40 | } |
| 41 | |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 42 | class GLEffect : public GrGLEffect { |
| 43 | public: |
rileya | abaef86 | 2014-09-12 17:45:58 -0700 | [diff] [blame] | 44 | static const GrGLfloat kJPEGConversionMatrix[16]; |
| 45 | static const GrGLfloat kRec601ConversionMatrix[16]; |
| 46 | |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 47 | // this class always generates the same code. |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 48 | static void GenKey(const GrEffect&, const GrGLCaps&, GrEffectKeyBuilder*) {} |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 49 | |
| 50 | GLEffect(const GrBackendEffectFactory& factory, |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 51 | const GrEffect&) |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 52 | : INHERITED(factory) { |
| 53 | } |
| 54 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 55 | virtual void emitCode(GrGLProgramBuilder* builder, |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 56 | const GrEffect&, |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 57 | const GrEffectKey&, |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 58 | const char* outputColor, |
| 59 | const char* inputColor, |
| 60 | const TransformedCoordsArray& coords, |
| 61 | const TextureSamplerArray& samplers) SK_OVERRIDE { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 62 | GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder(); |
rileya | abaef86 | 2014-09-12 17:45:58 -0700 | [diff] [blame] | 63 | |
| 64 | const char* yuvMatrix = NULL; |
| 65 | fMatrixUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
| 66 | kMat44f_GrSLType, "YUVMatrix", |
| 67 | &yuvMatrix); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 68 | fsBuilder->codeAppendf("\t%s = vec4(\n\t\t", outputColor); |
| 69 | fsBuilder->appendTextureLookup(samplers[0], coords[0].c_str(), coords[0].type()); |
| 70 | fsBuilder->codeAppend(".r,\n\t\t"); |
| 71 | fsBuilder->appendTextureLookup(samplers[1], coords[0].c_str(), coords[0].type()); |
| 72 | fsBuilder->codeAppend(".r,\n\t\t"); |
| 73 | fsBuilder->appendTextureLookup(samplers[2], coords[0].c_str(), coords[0].type()); |
| 74 | fsBuilder->codeAppendf(".r,\n\t\t1.0) * %s;\n", yuvMatrix); |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 75 | } |
| 76 | |
rileya | abaef86 | 2014-09-12 17:45:58 -0700 | [diff] [blame] | 77 | virtual void setData(const GrGLProgramDataManager& pdman, |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 78 | const GrEffect& effect) SK_OVERRIDE { |
| 79 | const YUVtoRGBEffect& yuvEffect = effect.cast<YUVtoRGBEffect>(); |
rileya | abaef86 | 2014-09-12 17:45:58 -0700 | [diff] [blame] | 80 | switch (yuvEffect.getColorSpace()) { |
| 81 | case kJPEG_SkYUVColorSpace: |
| 82 | pdman.setMatrix4f(fMatrixUni, kJPEGConversionMatrix); |
| 83 | break; |
| 84 | case kRec601_SkYUVColorSpace: |
| 85 | pdman.setMatrix4f(fMatrixUni, kRec601ConversionMatrix); |
| 86 | break; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | private: |
| 91 | GrGLProgramDataManager::UniformHandle fMatrixUni; |
| 92 | |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 93 | typedef GrGLEffect INHERITED; |
| 94 | }; |
| 95 | |
| 96 | private: |
rileya | abaef86 | 2014-09-12 17:45:58 -0700 | [diff] [blame] | 97 | YUVtoRGBEffect(GrTexture* yTexture, GrTexture* uTexture, GrTexture* vTexture, |
| 98 | SkYUVColorSpace colorSpace) |
| 99 | : fCoordTransform(kLocal_GrCoordSet, GrCoordTransform::MakeDivByTextureWHMatrix(yTexture), |
| 100 | yTexture) |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 101 | , fYAccess(yTexture) |
| 102 | , fUAccess(uTexture) |
rileya | abaef86 | 2014-09-12 17:45:58 -0700 | [diff] [blame] | 103 | , fVAccess(vTexture) |
| 104 | , fColorSpace(colorSpace) { |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 105 | this->addCoordTransform(&fCoordTransform); |
| 106 | this->addTextureAccess(&fYAccess); |
| 107 | this->addTextureAccess(&fUAccess); |
| 108 | this->addTextureAccess(&fVAccess); |
| 109 | this->setWillNotUseInputColor(); |
| 110 | } |
| 111 | |
| 112 | virtual bool onIsEqual(const GrEffect& sBase) const { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 113 | const YUVtoRGBEffect& s = sBase.cast<YUVtoRGBEffect>(); |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 114 | return fYAccess.getTexture() == s.fYAccess.getTexture() && |
| 115 | fUAccess.getTexture() == s.fUAccess.getTexture() && |
rileya | abaef86 | 2014-09-12 17:45:58 -0700 | [diff] [blame] | 116 | fVAccess.getTexture() == s.fVAccess.getTexture() && |
| 117 | fColorSpace == s.getColorSpace(); |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | GrCoordTransform fCoordTransform; |
| 121 | GrTextureAccess fYAccess; |
| 122 | GrTextureAccess fUAccess; |
| 123 | GrTextureAccess fVAccess; |
rileya | abaef86 | 2014-09-12 17:45:58 -0700 | [diff] [blame] | 124 | SkYUVColorSpace fColorSpace; |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 125 | |
| 126 | typedef GrEffect INHERITED; |
| 127 | }; |
| 128 | |
rileya | abaef86 | 2014-09-12 17:45:58 -0700 | [diff] [blame] | 129 | const GrGLfloat YUVtoRGBEffect::GLEffect::kJPEGConversionMatrix[16] = { |
| 130 | 1.0f, 0.0f, 1.402f, -0.701f, |
| 131 | 1.0f, -0.34414f, -0.71414f, 0.529f, |
| 132 | 1.0f, 1.772f, 0.0f, -0.886f, |
| 133 | 0.0f, 0.0f, 0.0f, 1.0}; |
| 134 | const GrGLfloat YUVtoRGBEffect::GLEffect::kRec601ConversionMatrix[16] = { |
rileya | 223ba62 | 2014-09-16 06:23:50 -0700 | [diff] [blame] | 135 | 1.164f, 0.0f, 1.596f, -0.87075f, |
| 136 | 1.164f, -0.391f, -0.813f, 0.52925f, |
rileya | abaef86 | 2014-09-12 17:45:58 -0700 | [diff] [blame] | 137 | 1.164f, 2.018f, 0.0f, -1.08175f, |
| 138 | 0.0f, 0.0f, 0.0f, 1.0}; |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | ////////////////////////////////////////////////////////////////////////////// |
| 142 | |
rileya | abaef86 | 2014-09-12 17:45:58 -0700 | [diff] [blame] | 143 | GrEffect* GrYUVtoRGBEffect::Create(GrTexture* yTexture, GrTexture* uTexture, GrTexture* vTexture, |
| 144 | SkYUVColorSpace colorSpace) { |
| 145 | return YUVtoRGBEffect::Create(yTexture, uTexture, vTexture, colorSpace); |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 146 | } |