Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 1 | /* |
Ethan Nicholas | 130fb3f | 2018-02-01 12:14:34 -0500 | [diff] [blame] | 2 | * Copyright 2018 Google Inc. |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 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 | |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 8 | #include "GrYUVtoRGBEffect.h" |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 9 | |
| 10 | static const float kJPEGConversionMatrix[16] = { |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 11 | 1.0f, 0.0f, 1.402f, -0.703749f, |
| 12 | 1.0f, -0.344136f, -0.714136f, 0.531211f, |
| 13 | 1.0f, 1.772f, 0.0f, -0.889475f, |
| 14 | 0.0f, 0.0f, 0.0f, 1.0 |
| 15 | }; |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 16 | |
| 17 | static const float kRec601ConversionMatrix[16] = { |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 18 | 1.164f, 0.0f, 1.596f, -0.87075f, |
| 19 | 1.164f, -0.391f, -0.813f, 0.52925f, |
| 20 | 1.164f, 2.018f, 0.0f, -1.08175f, |
| 21 | 0.0f, 0.0f, 0.0f, 1.0 |
| 22 | }; |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 23 | |
| 24 | static const float kRec709ConversionMatrix[16] = { |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 25 | 1.164f, 0.0f, 1.793f, -0.96925f, |
| 26 | 1.164f, -0.213f, -0.533f, 0.30025f, |
| 27 | 1.164f, 2.112f, 0.0f, -1.12875f, |
| 28 | 0.0f, 0.0f, 0.0f, 1.0f |
| 29 | }; |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 30 | |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 31 | std::unique_ptr<GrFragmentProcessor> GrYUVtoRGBEffect::Make(const sk_sp<GrTextureProxy> proxies[], |
| 32 | const SkYUVAIndex yuvaIndices[4], |
| 33 | SkYUVColorSpace yuvColorSpace) { |
| 34 | int numPlanes; |
Jim Van Verth | f00b162 | 2018-10-10 13:03:23 -0400 | [diff] [blame^] | 35 | SkAssertResult(SkYUVAIndex::AreValidIndices(yuvaIndices, &numPlanes)); |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 36 | |
| 37 | const SkISize YSize = proxies[yuvaIndices[SkYUVAIndex::kY_Index].fIndex]->isize(); |
| 38 | |
| 39 | GrSamplerState::Filter filterModes[4]; |
| 40 | SkSize scales[4]; |
| 41 | for (int i = 0; i < numPlanes; ++i) { |
| 42 | SkISize size = proxies[i]->isize(); |
| 43 | scales[i] = SkSize::Make(SkIntToScalar(size.width()) / SkIntToScalar(YSize.width()), |
| 44 | SkIntToScalar(size.height()) / SkIntToScalar(YSize.height())); |
| 45 | filterModes[i] = (size == YSize) ? GrSamplerState::Filter::kNearest |
| 46 | : GrSamplerState::Filter::kBilerp; |
| 47 | } |
| 48 | |
Mike Klein | 4429a4f | 2018-10-04 09:06:00 -0400 | [diff] [blame] | 49 | SkMatrix44 mat; |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 50 | switch (yuvColorSpace) { |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 51 | case kJPEG_SkYUVColorSpace: |
| 52 | mat.setColMajorf(kJPEGConversionMatrix); |
| 53 | break; |
| 54 | case kRec601_SkYUVColorSpace: |
| 55 | mat.setColMajorf(kRec601ConversionMatrix); |
| 56 | break; |
| 57 | case kRec709_SkYUVColorSpace: |
| 58 | mat.setColMajorf(kRec709ConversionMatrix); |
| 59 | break; |
| 60 | } |
| 61 | return std::unique_ptr<GrFragmentProcessor>(new GrYUVtoRGBEffect( |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 62 | proxies, scales, filterModes, numPlanes, yuvaIndices, mat)); |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 63 | } |
Robert Phillips | ba5c439 | 2018-07-25 12:37:14 -0400 | [diff] [blame] | 64 | |
| 65 | SkString GrYUVtoRGBEffect::dumpInfo() const { |
| 66 | SkString str; |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 67 | for (int i = 0; i < this->numTextureSamplers(); ++i) { |
| 68 | str.appendf("%d: %d %d ", i, |
| 69 | this->textureSampler(i).proxy()->uniqueID().asUInt(), |
| 70 | this->textureSampler(i).proxy()->underlyingUniqueID().asUInt()); |
| 71 | } |
| 72 | str.appendf("\n"); |
Robert Phillips | ba5c439 | 2018-07-25 12:37:14 -0400 | [diff] [blame] | 73 | |
| 74 | return str; |
| 75 | } |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 76 | #include "glsl/GrGLSLFragmentProcessor.h" |
| 77 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
| 78 | #include "glsl/GrGLSLProgramBuilder.h" |
| 79 | #include "GrTexture.h" |
| 80 | #include "SkSLCPP.h" |
| 81 | #include "SkSLUtil.h" |
| 82 | class GrGLSLYUVtoRGBEffect : public GrGLSLFragmentProcessor { |
| 83 | public: |
| 84 | GrGLSLYUVtoRGBEffect() {} |
| 85 | void emitCode(EmitArgs& args) override { |
| 86 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 87 | const GrYUVtoRGBEffect& _outer = args.fFp.cast<GrYUVtoRGBEffect>(); |
| 88 | (void)_outer; |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 89 | |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 90 | auto colorSpaceMatrix = _outer.colorSpaceMatrix(); |
| 91 | (void)colorSpaceMatrix; |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 92 | fColorSpaceMatrixVar = |
| 93 | args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf4x4_GrSLType, |
| 94 | kDefault_GrSLPrecision, "colorSpaceMatrix"); |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 95 | |
| 96 | int numSamplers = args.fTexSamplers.count(); |
| 97 | |
| 98 | SkString coords[4]; |
| 99 | for (int i = 0; i < numSamplers; ++i) { |
| 100 | coords[i] = fragBuilder->ensureCoords2D(args.fTransformedCoords[i]); |
| 101 | } |
| 102 | |
| 103 | for (int i = 0; i < numSamplers; ++i) { |
| 104 | fragBuilder->codeAppendf( |
| 105 | "half4 tmp%d = texture(%s, %s).%s;", |
| 106 | i, |
| 107 | fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[i]).c_str(), |
| 108 | coords[i].c_str(), |
| 109 | fragBuilder->getProgramBuilder()->samplerSwizzle(args.fTexSamplers[i]).c_str()); |
| 110 | } |
| 111 | |
| 112 | static const char kChannelToChar[4] = { 'x', 'y', 'z', 'w' }; |
| 113 | |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 114 | fragBuilder->codeAppendf( |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 115 | "half4 yuvOne = half4(tmp%d.%c, tmp%d.%c, tmp%d.%c, 1.0) * %s;", |
| 116 | _outer.yuvaIndex(0).fIndex, kChannelToChar[(int)_outer.yuvaIndex(0).fChannel], |
| 117 | _outer.yuvaIndex(1).fIndex, kChannelToChar[(int)_outer.yuvaIndex(1).fChannel], |
| 118 | _outer.yuvaIndex(2).fIndex, kChannelToChar[(int)_outer.yuvaIndex(2).fChannel], |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 119 | args.fUniformHandler->getUniformCStr(fColorSpaceMatrixVar)); |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 120 | |
| 121 | |
| 122 | if (_outer.yuvaIndex(3).fIndex >= 0) { |
| 123 | fragBuilder->codeAppendf( |
| 124 | "float a = tmp%d.%c;", _outer.yuvaIndex(3).fIndex, |
| 125 | kChannelToChar[(int)_outer.yuvaIndex(3).fChannel]); |
| 126 | } else { |
| 127 | fragBuilder->codeAppendf("float a = 1.0;"); |
| 128 | } |
| 129 | |
| 130 | fragBuilder->codeAppendf("%s = half4(yuvOne.xyz, a);", args.fOutputColor); |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | private: |
| 134 | void onSetData(const GrGLSLProgramDataManager& pdman, |
| 135 | const GrFragmentProcessor& _proc) override { |
| 136 | const GrYUVtoRGBEffect& _outer = _proc.cast<GrYUVtoRGBEffect>(); |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 137 | { pdman.setSkMatrix44(fColorSpaceMatrixVar, (_outer.colorSpaceMatrix())); } |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 138 | } |
| 139 | UniformHandle fColorSpaceMatrixVar; |
| 140 | }; |
| 141 | GrGLSLFragmentProcessor* GrYUVtoRGBEffect::onCreateGLSLInstance() const { |
| 142 | return new GrGLSLYUVtoRGBEffect(); |
| 143 | } |
| 144 | void GrYUVtoRGBEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
| 145 | GrProcessorKeyBuilder* b) const { |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 146 | b->add32(this->numTextureSamplers()); |
| 147 | |
| 148 | uint32_t packed = 0; |
| 149 | for (int i = 0; i < 4; ++i) { |
| 150 | if (this->yuvaIndex(i).fIndex < 0) { |
| 151 | continue; |
| 152 | } |
| 153 | |
| 154 | uint8_t index = this->yuvaIndex(i).fIndex; |
| 155 | uint8_t chann = (uint8_t) this->yuvaIndex(i).fChannel; |
Robert Phillips | 6ba8c83 | 2018-10-10 11:43:01 -0400 | [diff] [blame] | 156 | |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 157 | SkASSERT(index < 4 && chann < 4); |
| 158 | |
| 159 | packed |= (index | (chann << 2)) << (i * 4); |
| 160 | } |
| 161 | b->add32(packed); |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 162 | } |
| 163 | bool GrYUVtoRGBEffect::onIsEqual(const GrFragmentProcessor& other) const { |
| 164 | const GrYUVtoRGBEffect& that = other.cast<GrYUVtoRGBEffect>(); |
Robert Phillips | 6ba8c83 | 2018-10-10 11:43:01 -0400 | [diff] [blame] | 165 | |
| 166 | for (int i = 0; i < 4; ++i) { |
| 167 | if (fYUVAIndices[i] != that.fYUVAIndices[i]) { |
| 168 | return false; |
| 169 | } |
| 170 | } |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 171 | |
| 172 | for (int i = 0; i < this->numTextureSamplers(); ++i) { |
| 173 | // 'fSamplers' is checked by the base class |
| 174 | if (fSamplerTransforms[i] != that.fSamplerTransforms[i]) { |
| 175 | return false; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | if (fColorSpaceMatrix != that.fColorSpaceMatrix) { |
| 180 | return false; |
| 181 | } |
| 182 | |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 183 | return true; |
| 184 | } |
| 185 | GrYUVtoRGBEffect::GrYUVtoRGBEffect(const GrYUVtoRGBEffect& src) |
| 186 | : INHERITED(kGrYUVtoRGBEffect_ClassID, src.optimizationFlags()) |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 187 | , fColorSpaceMatrix(src.fColorSpaceMatrix) { |
| 188 | int numPlanes = src.numTextureSamplers(); |
| 189 | for (int i = 0; i < numPlanes; ++i) { |
| 190 | fSamplers[i].reset(sk_ref_sp(src.fSamplers[i].proxy()), src.fSamplers[i].samplerState()); |
| 191 | fSamplerTransforms[i] = src.fSamplerTransforms[i]; |
| 192 | fSamplerCoordTransforms[i] = src.fSamplerCoordTransforms[i]; |
| 193 | } |
| 194 | |
| 195 | this->setTextureSamplerCnt(numPlanes); |
| 196 | for (int i = 0; i < numPlanes; ++i) { |
| 197 | this->addCoordTransform(&fSamplerCoordTransforms[i]); |
| 198 | } |
| 199 | |
| 200 | memcpy(fYUVAIndices, src.fYUVAIndices, sizeof(fYUVAIndices)); |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 201 | } |
| 202 | std::unique_ptr<GrFragmentProcessor> GrYUVtoRGBEffect::clone() const { |
| 203 | return std::unique_ptr<GrFragmentProcessor>(new GrYUVtoRGBEffect(*this)); |
| 204 | } |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 205 | const GrFragmentProcessor::TextureSampler& GrYUVtoRGBEffect::onTextureSampler(int index) const { |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 206 | SkASSERT(index < this->numTextureSamplers()); |
| 207 | return fSamplers[index]; |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 208 | } |