| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "GrYUVtoRGBEffect.h" | 8 #include "GrYUVtoRGBEffect.h" |
| 9 | 9 |
| 10 #include "GrCoordTransform.h" | 10 #include "GrCoordTransform.h" |
| 11 #include "GrInvariantOutput.h" | 11 #include "GrInvariantOutput.h" |
| 12 #include "GrProcessor.h" | 12 #include "GrProcessor.h" |
| 13 #include "gl/GrGLProcessor.h" | 13 #include "gl/GrGLProcessor.h" |
| 14 #include "gl/builders/GrGLProgramBuilder.h" | 14 #include "gl/builders/GrGLProgramBuilder.h" |
| 15 | 15 |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 class YUVtoRGBEffect : public GrFragmentProcessor { | 18 class YUVtoRGBEffect : public GrFragmentProcessor { |
| 19 public: | 19 public: |
| 20 static GrFragmentProcessor* Create(GrTexture* yTexture, GrTexture* uTexture, | 20 static GrFragmentProcessor* Create(GrTexture* yTexture, GrTexture* uTexture, |
| 21 GrTexture* vTexture, SkISize sizes[3], | 21 GrTexture* vTexture, SkYUVColorSpace colo
rSpace) { |
| 22 SkYUVColorSpace colorSpace) { | 22 return SkNEW_ARGS(YUVtoRGBEffect, (yTexture, uTexture, vTexture, colorSp
ace)); |
| 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]); | |
| 36 return SkNEW_ARGS(YUVtoRGBEffect, (yTexture, uTexture, vTexture, yuvMatr
ix, colorSpace)); | |
| 37 } | 23 } |
| 38 | 24 |
| 39 const char* name() const SK_OVERRIDE { return "YUV to RGB"; } | 25 const char* name() const SK_OVERRIDE { return "YUV to RGB"; } |
| 40 | 26 |
| 41 SkYUVColorSpace getColorSpace() const { | 27 SkYUVColorSpace getColorSpace() const { |
| 42 return fColorSpace; | 28 return fColorSpace; |
| 43 } | 29 } |
| 44 | 30 |
| 45 class GLProcessor : public GrGLFragmentProcessor { | 31 class GLProcessor : public GrGLFragmentProcessor { |
| 46 public: | 32 public: |
| (...skipping 13 matching lines...) Expand all Loading... |
| 60 const TextureSamplerArray& samplers) SK_OVERRIDE { | 46 const TextureSamplerArray& samplers) SK_OVERRIDE { |
| 61 GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder
(); | 47 GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder
(); |
| 62 | 48 |
| 63 const char* yuvMatrix = NULL; | 49 const char* yuvMatrix = NULL; |
| 64 fMatrixUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visib
ility, | 50 fMatrixUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visib
ility, |
| 65 kMat44f_GrSLType, kDefault_GrSLPrec
ision, | 51 kMat44f_GrSLType, kDefault_GrSLPrec
ision, |
| 66 "YUVMatrix", &yuvMatrix); | 52 "YUVMatrix", &yuvMatrix); |
| 67 fsBuilder->codeAppendf("\t%s = vec4(\n\t\t", outputColor); | 53 fsBuilder->codeAppendf("\t%s = vec4(\n\t\t", outputColor); |
| 68 fsBuilder->appendTextureLookup(samplers[0], coords[0].c_str(), coord
s[0].getType()); | 54 fsBuilder->appendTextureLookup(samplers[0], coords[0].c_str(), coord
s[0].getType()); |
| 69 fsBuilder->codeAppend(".r,\n\t\t"); | 55 fsBuilder->codeAppend(".r,\n\t\t"); |
| 70 fsBuilder->appendTextureLookup(samplers[1], coords[1].c_str(), coord
s[1].getType()); | 56 fsBuilder->appendTextureLookup(samplers[1], coords[0].c_str(), coord
s[0].getType()); |
| 71 fsBuilder->codeAppend(".r,\n\t\t"); | 57 fsBuilder->codeAppend(".r,\n\t\t"); |
| 72 fsBuilder->appendTextureLookup(samplers[2], coords[2].c_str(), coord
s[2].getType()); | 58 fsBuilder->appendTextureLookup(samplers[2], coords[0].c_str(), coord
s[0].getType()); |
| 73 fsBuilder->codeAppendf(".r,\n\t\t1.0) * %s;\n", yuvMatrix); | 59 fsBuilder->codeAppendf(".r,\n\t\t1.0) * %s;\n", yuvMatrix); |
| 74 } | 60 } |
| 75 | 61 |
| 76 virtual void setData(const GrGLProgramDataManager& pdman, | 62 virtual void setData(const GrGLProgramDataManager& pdman, |
| 77 const GrProcessor& processor) SK_OVERRIDE { | 63 const GrProcessor& processor) SK_OVERRIDE { |
| 78 const YUVtoRGBEffect& yuvEffect = processor.cast<YUVtoRGBEffect>(); | 64 const YUVtoRGBEffect& yuvEffect = processor.cast<YUVtoRGBEffect>(); |
| 79 switch (yuvEffect.getColorSpace()) { | 65 switch (yuvEffect.getColorSpace()) { |
| 80 case kJPEG_SkYUVColorSpace: | 66 case kJPEG_SkYUVColorSpace: |
| 81 pdman.setMatrix4f(fMatrixUni, kJPEGConversionMatrix); | 67 pdman.setMatrix4f(fMatrixUni, kJPEGConversionMatrix); |
| 82 break; | 68 break; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 96 GrProcessorKeyBuilder* b) const SK_OVERRIDE { | 82 GrProcessorKeyBuilder* b) const SK_OVERRIDE { |
| 97 GLProcessor::GenKey(*this, caps, b); | 83 GLProcessor::GenKey(*this, caps, b); |
| 98 } | 84 } |
| 99 | 85 |
| 100 GrGLFragmentProcessor* createGLInstance() const SK_OVERRIDE { | 86 GrGLFragmentProcessor* createGLInstance() const SK_OVERRIDE { |
| 101 return SkNEW_ARGS(GLProcessor, (*this)); | 87 return SkNEW_ARGS(GLProcessor, (*this)); |
| 102 } | 88 } |
| 103 | 89 |
| 104 private: | 90 private: |
| 105 YUVtoRGBEffect(GrTexture* yTexture, GrTexture* uTexture, GrTexture* vTexture
, | 91 YUVtoRGBEffect(GrTexture* yTexture, GrTexture* uTexture, GrTexture* vTexture
, |
| 106 SkMatrix yuvMatrix[3], SkYUVColorSpace colorSpace) | 92 SkYUVColorSpace colorSpace) |
| 107 : fYTransform(kLocal_GrCoordSet, yuvMatrix[0], yTexture, GrTextureParams::kN
one_FilterMode) | 93 : fCoordTransform(kLocal_GrCoordSet, |
| 94 GrCoordTransform::MakeDivByTextureWHMatrix(yTexture), |
| 95 yTexture, GrTextureParams::kNone_FilterMode) |
| 108 , fYAccess(yTexture) | 96 , fYAccess(yTexture) |
| 109 , fUTransform(kLocal_GrCoordSet, yuvMatrix[1], uTexture, GrTextureParams::kN
one_FilterMode) | |
| 110 , fUAccess(uTexture) | 97 , fUAccess(uTexture) |
| 111 , fVTransform(kLocal_GrCoordSet, yuvMatrix[2], vTexture, GrTextureParams::kN
one_FilterMode) | |
| 112 , fVAccess(vTexture) | 98 , fVAccess(vTexture) |
| 113 , fColorSpace(colorSpace) { | 99 , fColorSpace(colorSpace) { |
| 114 this->initClassID<YUVtoRGBEffect>(); | 100 this->initClassID<YUVtoRGBEffect>(); |
| 115 this->addCoordTransform(&fYTransform); | 101 this->addCoordTransform(&fCoordTransform); |
| 116 this->addTextureAccess(&fYAccess); | 102 this->addTextureAccess(&fYAccess); |
| 117 this->addCoordTransform(&fUTransform); | |
| 118 this->addTextureAccess(&fUAccess); | 103 this->addTextureAccess(&fUAccess); |
| 119 this->addCoordTransform(&fVTransform); | |
| 120 this->addTextureAccess(&fVAccess); | 104 this->addTextureAccess(&fVAccess); |
| 121 } | 105 } |
| 122 | 106 |
| 123 bool onIsEqual(const GrFragmentProcessor& sBase) const SK_OVERRIDE { | 107 bool onIsEqual(const GrFragmentProcessor& sBase) const SK_OVERRIDE { |
| 124 const YUVtoRGBEffect& s = sBase.cast<YUVtoRGBEffect>(); | 108 const YUVtoRGBEffect& s = sBase.cast<YUVtoRGBEffect>(); |
| 125 return fColorSpace == s.getColorSpace(); | 109 return fColorSpace == s.getColorSpace(); |
| 126 } | 110 } |
| 127 | 111 |
| 128 void onComputeInvariantOutput(GrInvariantOutput* inout) const SK_OVERRIDE { | 112 void onComputeInvariantOutput(GrInvariantOutput* inout) const SK_OVERRIDE { |
| 129 // YUV is opaque | 113 // YUV is opaque |
| 130 inout->setToOther(kA_GrColorComponentFlag, 0xFF << GrColor_SHIFT_A, | 114 inout->setToOther(kA_GrColorComponentFlag, 0xFF << GrColor_SHIFT_A, |
| 131 GrInvariantOutput::kWillNot_ReadInput); | 115 GrInvariantOutput::kWillNot_ReadInput); |
| 132 } | 116 } |
| 133 | 117 |
| 134 GrCoordTransform fYTransform; | 118 GrCoordTransform fCoordTransform; |
| 135 GrTextureAccess fYAccess; | 119 GrTextureAccess fYAccess; |
| 136 GrCoordTransform fUTransform; | |
| 137 GrTextureAccess fUAccess; | 120 GrTextureAccess fUAccess; |
| 138 GrCoordTransform fVTransform; | |
| 139 GrTextureAccess fVAccess; | 121 GrTextureAccess fVAccess; |
| 140 SkYUVColorSpace fColorSpace; | 122 SkYUVColorSpace fColorSpace; |
| 141 | 123 |
| 142 typedef GrFragmentProcessor INHERITED; | 124 typedef GrFragmentProcessor INHERITED; |
| 143 }; | 125 }; |
| 144 | 126 |
| 145 const GrGLfloat YUVtoRGBEffect::GLProcessor::kJPEGConversionMatrix[16] = { | 127 const GrGLfloat YUVtoRGBEffect::GLProcessor::kJPEGConversionMatrix[16] = { |
| 146 1.0f, 0.0f, 1.402f, -0.701f, | 128 1.0f, 0.0f, 1.402f, -0.701f, |
| 147 1.0f, -0.34414f, -0.71414f, 0.529f, | 129 1.0f, -0.34414f, -0.71414f, 0.529f, |
| 148 1.0f, 1.772f, 0.0f, -0.886f, | 130 1.0f, 1.772f, 0.0f, -0.886f, |
| 149 0.0f, 0.0f, 0.0f, 1.0}; | 131 0.0f, 0.0f, 0.0f, 1.0}; |
| 150 const GrGLfloat YUVtoRGBEffect::GLProcessor::kRec601ConversionMatrix[16] = { | 132 const GrGLfloat YUVtoRGBEffect::GLProcessor::kRec601ConversionMatrix[16] = { |
| 151 1.164f, 0.0f, 1.596f, -0.87075f, | 133 1.164f, 0.0f, 1.596f, -0.87075f, |
| 152 1.164f, -0.391f, -0.813f, 0.52925f, | 134 1.164f, -0.391f, -0.813f, 0.52925f, |
| 153 1.164f, 2.018f, 0.0f, -1.08175f, | 135 1.164f, 2.018f, 0.0f, -1.08175f, |
| 154 0.0f, 0.0f, 0.0f, 1.0}; | 136 0.0f, 0.0f, 0.0f, 1.0}; |
| 155 } | 137 } |
| 156 | 138 |
| 157 ////////////////////////////////////////////////////////////////////////////// | 139 ////////////////////////////////////////////////////////////////////////////// |
| 158 | 140 |
| 159 GrFragmentProcessor* | 141 GrFragmentProcessor* |
| 160 GrYUVtoRGBEffect::Create(GrTexture* yTexture, GrTexture* uTexture, GrTexture* vT
exture, | 142 GrYUVtoRGBEffect::Create(GrTexture* yTexture, GrTexture* uTexture, GrTexture* vT
exture, |
| 161 SkISize sizes[3], SkYUVColorSpace colorSpace) { | 143 SkYUVColorSpace colorSpace) { |
| 162 SkASSERT(yTexture && uTexture && vTexture && sizes); | 144 return YUVtoRGBEffect::Create(yTexture, uTexture, vTexture, colorSpace); |
| 163 return YUVtoRGBEffect::Create(yTexture, uTexture, vTexture, sizes, colorSpac
e); | |
| 164 } | 145 } |
| OLD | NEW |