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