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