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" |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 12 | #include "GrInvariantOutput.h" |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 13 | #include "GrProcessor.h" |
| 14 | #include "gl/GrGLProcessor.h" |
| 15 | #include "GrTBackendProcessorFactory.h" |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 16 | |
| 17 | namespace { |
| 18 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 19 | class YUVtoRGBEffect : public GrFragmentProcessor { |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 20 | public: |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 21 | static GrFragmentProcessor* Create(GrTexture* yTexture, GrTexture* uTexture, |
| 22 | GrTexture* vTexture, SkYUVColorSpace colorSpace) { |
rileya | abaef86 | 2014-09-12 17:45:58 -0700 | [diff] [blame] | 23 | return SkNEW_ARGS(YUVtoRGBEffect, (yTexture, uTexture, vTexture, colorSpace)); |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | static const char* Name() { return "YUV to RGB"; } |
| 27 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 28 | virtual const GrBackendFragmentProcessorFactory& getFactory() const SK_OVERRIDE { |
| 29 | return GrTBackendFragmentProcessorFactory<YUVtoRGBEffect>::getInstance(); |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 30 | } |
| 31 | |
rileya | abaef86 | 2014-09-12 17:45:58 -0700 | [diff] [blame] | 32 | SkYUVColorSpace getColorSpace() const { |
| 33 | return fColorSpace; |
| 34 | } |
| 35 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 36 | class GLProcessor : public GrGLFragmentProcessor { |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 37 | public: |
rileya | abaef86 | 2014-09-12 17:45:58 -0700 | [diff] [blame] | 38 | static const GrGLfloat kJPEGConversionMatrix[16]; |
| 39 | static const GrGLfloat kRec601ConversionMatrix[16]; |
| 40 | |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 41 | // this class always generates the same code. |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 42 | static void GenKey(const GrProcessor&, const GrGLCaps&, GrProcessorKeyBuilder*) {} |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 43 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 44 | GLProcessor(const GrBackendProcessorFactory& factory, |
| 45 | const GrProcessor&) |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 46 | : INHERITED(factory) { |
| 47 | } |
| 48 | |
joshualitt | 1598899 | 2014-10-09 15:04:05 -0700 | [diff] [blame] | 49 | virtual void emitCode(GrGLFPBuilder* builder, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 50 | const GrFragmentProcessor&, |
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); |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 102 | } |
| 103 | |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 104 | virtual bool onIsEqual(const GrFragmentProcessor& sBase) const { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 105 | const YUVtoRGBEffect& s = sBase.cast<YUVtoRGBEffect>(); |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 106 | return fColorSpace == s.getColorSpace(); |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 107 | } |
| 108 | |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 109 | virtual void onComputeInvariantOutput(GrInvariantOutput* inout) const SK_OVERRIDE { |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 110 | // YUV is opaque |
egdaniel | 9e4d6d1 | 2014-10-15 13:49:02 -0700 | [diff] [blame] | 111 | inout->setToOther(kA_GrColorComponentFlag, 0xFF << GrColor_SHIFT_A, |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 112 | GrInvariantOutput::kWillNot_ReadInput); |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 113 | } |
| 114 | |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 115 | GrCoordTransform fCoordTransform; |
| 116 | GrTextureAccess fYAccess; |
| 117 | GrTextureAccess fUAccess; |
| 118 | GrTextureAccess fVAccess; |
rileya | abaef86 | 2014-09-12 17:45:58 -0700 | [diff] [blame] | 119 | SkYUVColorSpace fColorSpace; |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 120 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 121 | typedef GrFragmentProcessor INHERITED; |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 122 | }; |
| 123 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 124 | const GrGLfloat YUVtoRGBEffect::GLProcessor::kJPEGConversionMatrix[16] = { |
rileya | abaef86 | 2014-09-12 17:45:58 -0700 | [diff] [blame] | 125 | 1.0f, 0.0f, 1.402f, -0.701f, |
| 126 | 1.0f, -0.34414f, -0.71414f, 0.529f, |
| 127 | 1.0f, 1.772f, 0.0f, -0.886f, |
| 128 | 0.0f, 0.0f, 0.0f, 1.0}; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 129 | const GrGLfloat YUVtoRGBEffect::GLProcessor::kRec601ConversionMatrix[16] = { |
rileya | 223ba62 | 2014-09-16 06:23:50 -0700 | [diff] [blame] | 130 | 1.164f, 0.0f, 1.596f, -0.87075f, |
| 131 | 1.164f, -0.391f, -0.813f, 0.52925f, |
rileya | abaef86 | 2014-09-12 17:45:58 -0700 | [diff] [blame] | 132 | 1.164f, 2.018f, 0.0f, -1.08175f, |
| 133 | 0.0f, 0.0f, 0.0f, 1.0}; |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | ////////////////////////////////////////////////////////////////////////////// |
| 137 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 138 | GrFragmentProcessor* |
| 139 | GrYUVtoRGBEffect::Create(GrTexture* yTexture, GrTexture* uTexture, GrTexture* vTexture, |
| 140 | SkYUVColorSpace colorSpace) { |
rileya | abaef86 | 2014-09-12 17:45:58 -0700 | [diff] [blame] | 141 | return YUVtoRGBEffect::Create(yTexture, uTexture, vTexture, colorSpace); |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 142 | } |