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