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