| 1 | /* | 
|---|
| 2 | * Copyright 2018 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 "src/gpu/effects/GrYUVtoRGBEffect.h" | 
|---|
| 9 |  | 
|---|
| 10 | #include "src/core/SkYUVMath.h" | 
|---|
| 11 | #include "src/gpu/GrTexture.h" | 
|---|
| 12 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" | 
|---|
| 13 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" | 
|---|
| 14 | #include "src/gpu/glsl/GrGLSLProgramBuilder.h" | 
|---|
| 15 | #include "src/sksl/SkSLCPP.h" | 
|---|
| 16 | #include "src/sksl/SkSLUtil.h" | 
|---|
| 17 |  | 
|---|
| 18 | static void border_colors(SkYUVColorSpace cs, | 
|---|
| 19 | const SkYUVAIndex yuvaIndices[4], | 
|---|
| 20 | float planeBorders[4][4]) { | 
|---|
| 21 | float m[20]; | 
|---|
| 22 | SkColorMatrix_RGB2YUV(cs, m); | 
|---|
| 23 | for (int i = 0; i < 4; ++i) { | 
|---|
| 24 | if (yuvaIndices[i].fIndex == -1) { | 
|---|
| 25 | return; | 
|---|
| 26 | } | 
|---|
| 27 | auto c = static_cast<int>(yuvaIndices[i].fChannel); | 
|---|
| 28 | planeBorders[yuvaIndices[i].fIndex][c] = m[i*5 + 4]; | 
|---|
| 29 | } | 
|---|
| 30 | } | 
|---|
| 31 |  | 
|---|
| 32 | std::unique_ptr<GrFragmentProcessor> GrYUVtoRGBEffect::Make(GrSurfaceProxyView views[], | 
|---|
| 33 | const SkYUVAIndex yuvaIndices[4], | 
|---|
| 34 | SkYUVColorSpace yuvColorSpace, | 
|---|
| 35 | GrSamplerState samplerState, | 
|---|
| 36 | const GrCaps& caps, | 
|---|
| 37 | const SkMatrix& localMatrix, | 
|---|
| 38 | const SkRect* subset) { | 
|---|
| 39 | int numPlanes; | 
|---|
| 40 | SkAssertResult(SkYUVAIndex::AreValidIndices(yuvaIndices, &numPlanes)); | 
|---|
| 41 |  | 
|---|
| 42 | const SkISize yDimensions = | 
|---|
| 43 | views[yuvaIndices[SkYUVAIndex::kY_Index].fIndex].proxy()->dimensions(); | 
|---|
| 44 |  | 
|---|
| 45 | // This promotion of nearest to bilinear for UV planes exists to mimic libjpeg[-turbo]'s | 
|---|
| 46 | // do_fancy_upsampling option. However, skbug.com/9693. | 
|---|
| 47 | GrSamplerState::Filter subsampledPlaneFilterMode = | 
|---|
| 48 | std::max(samplerState.filter(), GrSamplerState::Filter::kBilerp); | 
|---|
| 49 |  | 
|---|
| 50 | bool usesBorder = samplerState.wrapModeX() == GrSamplerState::WrapMode::kClampToBorder || | 
|---|
| 51 | samplerState.wrapModeY() == GrSamplerState::WrapMode::kClampToBorder; | 
|---|
| 52 | float planeBorders[4][4] = {}; | 
|---|
| 53 | if (usesBorder) { | 
|---|
| 54 | border_colors(yuvColorSpace, yuvaIndices, planeBorders); | 
|---|
| 55 | } | 
|---|
| 56 |  | 
|---|
| 57 | std::unique_ptr<GrFragmentProcessor> planeFPs[4]; | 
|---|
| 58 | for (int i = 0; i < numPlanes; ++i) { | 
|---|
| 59 | SkISize dimensions = views[i].proxy()->dimensions(); | 
|---|
| 60 | SkTCopyOnFirstWrite<SkMatrix> planeMatrix(&localMatrix); | 
|---|
| 61 | GrSamplerState::Filter planeFilter = samplerState.filter(); | 
|---|
| 62 | SkRect planeDomain; | 
|---|
| 63 | if (dimensions != yDimensions) { | 
|---|
| 64 | // JPEG chroma subsampling of odd dimensions produces U and V planes with the ceiling of | 
|---|
| 65 | // the image size divided by the subsampling factor (2). Our API for creating YUVA | 
|---|
| 66 | // doesn't capture the intended subsampling (and we should fix that). This fixes up 2x | 
|---|
| 67 | // subsampling for images with odd widths/heights (e.g. JPEG 420 or 422). | 
|---|
| 68 | float sx = (float)dimensions.width()  / yDimensions.width(); | 
|---|
| 69 | float sy = (float)dimensions.height() / yDimensions.height(); | 
|---|
| 70 | if ((yDimensions.width() & 0b1) && dimensions.width() == yDimensions.width() / 2 + 1) { | 
|---|
| 71 | sx = 0.5f; | 
|---|
| 72 | } | 
|---|
| 73 | if ((yDimensions.height() & 0b1) && | 
|---|
| 74 | dimensions.height() == yDimensions.height() / 2 + 1) { | 
|---|
| 75 | sy = 0.5f; | 
|---|
| 76 | } | 
|---|
| 77 | *planeMatrix.writable() = SkMatrix::MakeScale(sx, sy); | 
|---|
| 78 | planeMatrix.writable()->preConcat(localMatrix); | 
|---|
| 79 | planeFilter = subsampledPlaneFilterMode; | 
|---|
| 80 | if (subset) { | 
|---|
| 81 | planeDomain = {subset->fLeft   * sx, | 
|---|
| 82 | subset->fTop    * sy, | 
|---|
| 83 | subset->fRight  * sx, | 
|---|
| 84 | subset->fBottom * sy}; | 
|---|
| 85 | } | 
|---|
| 86 | } else if (subset) { | 
|---|
| 87 | planeDomain = *subset; | 
|---|
| 88 | } | 
|---|
| 89 | samplerState.setFilterMode(planeFilter); | 
|---|
| 90 | if (subset) { | 
|---|
| 91 | SkASSERT(planeFilter != GrSamplerState::Filter::kMipMap); | 
|---|
| 92 | planeFPs[i] = | 
|---|
| 93 | GrTextureEffect::MakeSubset(views[i], kUnknown_SkAlphaType, *planeMatrix, | 
|---|
| 94 | samplerState, planeDomain, caps, planeBorders[i]); | 
|---|
| 95 | } else { | 
|---|
| 96 | planeFPs[i] = GrTextureEffect::Make(views[i], kUnknown_SkAlphaType, *planeMatrix, | 
|---|
| 97 | samplerState, caps, planeBorders[i]); | 
|---|
| 98 | } | 
|---|
| 99 | } | 
|---|
| 100 |  | 
|---|
| 101 | return std::unique_ptr<GrFragmentProcessor>( | 
|---|
| 102 | new GrYUVtoRGBEffect(planeFPs, numPlanes, yuvaIndices, yuvColorSpace)); | 
|---|
| 103 | } | 
|---|
| 104 |  | 
|---|
| 105 | static SkAlphaType alpha_type(const SkYUVAIndex yuvaIndices[4]) { | 
|---|
| 106 | return yuvaIndices[3].fIndex >= 0 ? kPremul_SkAlphaType : kOpaque_SkAlphaType; | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 | GrYUVtoRGBEffect::GrYUVtoRGBEffect(std::unique_ptr<GrFragmentProcessor> planeFPs[4], int numPlanes, | 
|---|
| 110 | const SkYUVAIndex yuvaIndices[4], SkYUVColorSpace yuvColorSpace) | 
|---|
| 111 | : GrFragmentProcessor(kGrYUVtoRGBEffect_ClassID, | 
|---|
| 112 | ModulateForClampedSamplerOptFlags(alpha_type(yuvaIndices))) | 
|---|
| 113 | , fYUVColorSpace(yuvColorSpace) { | 
|---|
| 114 | for (int i = 0; i < numPlanes; ++i) { | 
|---|
| 115 | this->registerChildProcessor(std::move(planeFPs[i])); | 
|---|
| 116 | } | 
|---|
| 117 | std::copy_n(yuvaIndices, 4, fYUVAIndices); | 
|---|
| 118 | } | 
|---|
| 119 |  | 
|---|
| 120 | #ifdef SK_DEBUG | 
|---|
| 121 | SkString GrYUVtoRGBEffect::dumpInfo() const { | 
|---|
| 122 | SkString str; | 
|---|
| 123 | for (int i = 0; i < this->numTextureSamplers(); ++i) { | 
|---|
| 124 | str.appendf( "%d: %d %d ", i, | 
|---|
| 125 | this->textureSampler(i).view().proxy()->uniqueID().asUInt(), | 
|---|
| 126 | this->textureSampler(i).view().proxy()->underlyingUniqueID().asUInt()); | 
|---|
| 127 | } | 
|---|
| 128 | str.appendf( "\n"); | 
|---|
| 129 |  | 
|---|
| 130 | return str; | 
|---|
| 131 | } | 
|---|
| 132 | #endif | 
|---|
| 133 |  | 
|---|
| 134 | GrGLSLFragmentProcessor* GrYUVtoRGBEffect::onCreateGLSLInstance() const { | 
|---|
| 135 | class GrGLSLYUVtoRGBEffect : public GrGLSLFragmentProcessor { | 
|---|
| 136 | public: | 
|---|
| 137 | GrGLSLYUVtoRGBEffect() {} | 
|---|
| 138 |  | 
|---|
| 139 | void emitCode(EmitArgs& args) override { | 
|---|
| 140 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; | 
|---|
| 141 | const GrYUVtoRGBEffect& yuvEffect = args.fFp.cast<GrYUVtoRGBEffect>(); | 
|---|
| 142 |  | 
|---|
| 143 | int numPlanes = yuvEffect.numChildProcessors(); | 
|---|
| 144 |  | 
|---|
| 145 | SkString coords[4]; | 
|---|
| 146 | fragBuilder->codeAppendf( "half4 planes[%d];", numPlanes); | 
|---|
| 147 | for (int i = 0; i < numPlanes; ++i) { | 
|---|
| 148 | SkString tempVar = this->invokeChild(i, args); | 
|---|
| 149 | fragBuilder->codeAppendf( "planes[%d] = %s;", i, tempVar.c_str()); | 
|---|
| 150 | } | 
|---|
| 151 |  | 
|---|
| 152 | bool hasAlpha = yuvEffect.fYUVAIndices[3].fIndex >= 0; | 
|---|
| 153 | SkString rgba[4]; | 
|---|
| 154 | rgba[3] = "1"; | 
|---|
| 155 | for (int i = 0; i < (hasAlpha ? 4 : 3); ++i) { | 
|---|
| 156 | auto info = yuvEffect.fYUVAIndices[i]; | 
|---|
| 157 | auto letter = "rgba"[static_cast<int>(info.fChannel)]; | 
|---|
| 158 | rgba[i].printf( "planes[%d].%c", info.fIndex, letter); | 
|---|
| 159 | } | 
|---|
| 160 |  | 
|---|
| 161 | fragBuilder->codeAppendf( "half4 color = half4(%s, %s, %s, %s);", | 
|---|
| 162 | rgba[0].c_str(), rgba[1].c_str(), rgba[2].c_str(), rgba[3].c_str()); | 
|---|
| 163 |  | 
|---|
| 164 | if (kIdentity_SkYUVColorSpace != yuvEffect.fYUVColorSpace) { | 
|---|
| 165 | fColorSpaceMatrixVar = args.fUniformHandler->addUniform(&yuvEffect, | 
|---|
| 166 | kFragment_GrShaderFlag, kHalf3x3_GrSLType, "colorSpaceMatrix"); | 
|---|
| 167 | fColorSpaceTranslateVar = args.fUniformHandler->addUniform(&yuvEffect, | 
|---|
| 168 | kFragment_GrShaderFlag, kHalf3_GrSLType, "colorSpaceTranslate"); | 
|---|
| 169 | fragBuilder->codeAppendf( | 
|---|
| 170 | "color.rgb = saturate(color.rgb * %s + %s);", | 
|---|
| 171 | args.fUniformHandler->getUniformCStr(fColorSpaceMatrixVar), | 
|---|
| 172 | args.fUniformHandler->getUniformCStr(fColorSpaceTranslateVar)); | 
|---|
| 173 | } | 
|---|
| 174 |  | 
|---|
| 175 | if (hasAlpha) { | 
|---|
| 176 | // premultiply alpha | 
|---|
| 177 | fragBuilder->codeAppendf( "color.rgb *= color.a;"); | 
|---|
| 178 | } | 
|---|
| 179 | fragBuilder->codeAppendf( "%s = color;", args.fOutputColor); | 
|---|
| 180 | } | 
|---|
| 181 |  | 
|---|
| 182 | private: | 
|---|
| 183 | void onSetData(const GrGLSLProgramDataManager& pdman, | 
|---|
| 184 | const GrFragmentProcessor& proc) override { | 
|---|
| 185 | const GrYUVtoRGBEffect& yuvEffect = proc.cast<GrYUVtoRGBEffect>(); | 
|---|
| 186 |  | 
|---|
| 187 | if (yuvEffect.fYUVColorSpace != kIdentity_SkYUVColorSpace) { | 
|---|
| 188 | SkASSERT(fColorSpaceMatrixVar.isValid()); | 
|---|
| 189 | float yuvM[20]; | 
|---|
| 190 | SkColorMatrix_YUV2RGB(yuvEffect.fYUVColorSpace, yuvM); | 
|---|
| 191 | // We drop the fourth column entirely since the transformation | 
|---|
| 192 | // should not depend on alpha. The fifth column is sent as a separate | 
|---|
| 193 | // vector. The fourth row is also dropped entirely because alpha should | 
|---|
| 194 | // never be modified. | 
|---|
| 195 | SkASSERT(yuvM[3] == 0 && yuvM[8] == 0 && yuvM[13] == 0 && yuvM[18] == 1); | 
|---|
| 196 | SkASSERT(yuvM[15] == 0 && yuvM[16] == 0 && yuvM[17] == 0 && yuvM[19] == 0); | 
|---|
| 197 | float mtx[9] = { | 
|---|
| 198 | yuvM[ 0], yuvM[ 1], yuvM[ 2], | 
|---|
| 199 | yuvM[ 5], yuvM[ 6], yuvM[ 7], | 
|---|
| 200 | yuvM[10], yuvM[11], yuvM[12], | 
|---|
| 201 | }; | 
|---|
| 202 | float v[3] = {yuvM[4], yuvM[9], yuvM[14]}; | 
|---|
| 203 | pdman.setMatrix3f(fColorSpaceMatrixVar, mtx); | 
|---|
| 204 | pdman.set3fv(fColorSpaceTranslateVar, 1, v); | 
|---|
| 205 | } | 
|---|
| 206 | } | 
|---|
| 207 |  | 
|---|
| 208 | UniformHandle fColorSpaceMatrixVar; | 
|---|
| 209 | UniformHandle fColorSpaceTranslateVar; | 
|---|
| 210 | }; | 
|---|
| 211 |  | 
|---|
| 212 | return new GrGLSLYUVtoRGBEffect; | 
|---|
| 213 | } | 
|---|
| 214 | void GrYUVtoRGBEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, | 
|---|
| 215 | GrProcessorKeyBuilder* b) const { | 
|---|
| 216 | uint32_t packed = 0; | 
|---|
| 217 | for (int i = 0; i < 4; ++i) { | 
|---|
| 218 | if (fYUVAIndices[i].fIndex < 0) { | 
|---|
| 219 | continue; | 
|---|
| 220 | } | 
|---|
| 221 |  | 
|---|
| 222 | uint8_t index = fYUVAIndices[i].fIndex; | 
|---|
| 223 | uint8_t chann = static_cast<int>(fYUVAIndices[i].fChannel); | 
|---|
| 224 |  | 
|---|
| 225 | SkASSERT(index < 4 && chann < 4); | 
|---|
| 226 |  | 
|---|
| 227 | packed |= (index | (chann << 2)) << (i * 4); | 
|---|
| 228 | } | 
|---|
| 229 | if (fYUVColorSpace == kIdentity_SkYUVColorSpace) { | 
|---|
| 230 | packed |= 0x1 << 16; | 
|---|
| 231 | } | 
|---|
| 232 | b->add32(packed); | 
|---|
| 233 | } | 
|---|
| 234 |  | 
|---|
| 235 | bool GrYUVtoRGBEffect::onIsEqual(const GrFragmentProcessor& other) const { | 
|---|
| 236 | const GrYUVtoRGBEffect& that = other.cast<GrYUVtoRGBEffect>(); | 
|---|
| 237 |  | 
|---|
| 238 | for (int i = 0; i < 4; ++i) { | 
|---|
| 239 | if (fYUVAIndices[i] != that.fYUVAIndices[i]) { | 
|---|
| 240 | return false; | 
|---|
| 241 | } | 
|---|
| 242 | } | 
|---|
| 243 |  | 
|---|
| 244 | if (fYUVColorSpace != that.fYUVColorSpace) { | 
|---|
| 245 | return false; | 
|---|
| 246 | } | 
|---|
| 247 |  | 
|---|
| 248 | return true; | 
|---|
| 249 | } | 
|---|
| 250 |  | 
|---|
| 251 | GrYUVtoRGBEffect::GrYUVtoRGBEffect(const GrYUVtoRGBEffect& src) | 
|---|
| 252 | : GrFragmentProcessor(kGrYUVtoRGBEffect_ClassID, src.optimizationFlags()) | 
|---|
| 253 | , fYUVColorSpace(src.fYUVColorSpace) { | 
|---|
| 254 | int numPlanes = src.numChildProcessors(); | 
|---|
| 255 | for (int i = 0; i < numPlanes; ++i) { | 
|---|
| 256 | this->registerChildProcessor(this->childProcessor(i).clone()); | 
|---|
| 257 | } | 
|---|
| 258 | std::copy_n(src.fYUVAIndices, this->numChildProcessors(), fYUVAIndices); | 
|---|
| 259 | } | 
|---|
| 260 |  | 
|---|
| 261 | std::unique_ptr<GrFragmentProcessor> GrYUVtoRGBEffect::clone() const { | 
|---|
| 262 | return std::unique_ptr<GrFragmentProcessor>(new GrYUVtoRGBEffect(*this)); | 
|---|
| 263 | } | 
|---|
| 264 |  | 
|---|