bsalomon | 8610002 | 2016-02-01 12:09:07 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2011 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 "SkColorMatrixFilterRowMajor255.h" |
| 9 | #include "SkColorPriv.h" |
| 10 | #include "SkNx.h" |
| 11 | #include "SkReadBuffer.h" |
| 12 | #include "SkWriteBuffer.h" |
| 13 | #include "SkUnPreMultiply.h" |
| 14 | #include "SkString.h" |
| 15 | |
| 16 | #define SK_PMORDER_INDEX_A (SK_A32_SHIFT / 8) |
| 17 | #define SK_PMORDER_INDEX_R (SK_R32_SHIFT / 8) |
| 18 | #define SK_PMORDER_INDEX_G (SK_G32_SHIFT / 8) |
| 19 | #define SK_PMORDER_INDEX_B (SK_B32_SHIFT / 8) |
| 20 | |
| 21 | static void transpose_to_pmorder(float dst[20], const float src[20]) { |
| 22 | const float* srcR = src + 0; |
| 23 | const float* srcG = src + 5; |
| 24 | const float* srcB = src + 10; |
| 25 | const float* srcA = src + 15; |
| 26 | |
| 27 | for (int i = 0; i < 20; i += 4) { |
| 28 | dst[i + SK_PMORDER_INDEX_A] = *srcA++; |
| 29 | dst[i + SK_PMORDER_INDEX_R] = *srcR++; |
| 30 | dst[i + SK_PMORDER_INDEX_G] = *srcG++; |
| 31 | dst[i + SK_PMORDER_INDEX_B] = *srcB++; |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | // src is [20] but some compilers won't accept __restrict__ on anything |
| 36 | // but an raw pointer or reference |
| 37 | void SkColorMatrixFilterRowMajor255::initState(const SkScalar* SK_RESTRICT src) { |
| 38 | transpose_to_pmorder(fTranspose, src); |
| 39 | |
| 40 | const float* array = fMatrix; |
| 41 | |
| 42 | // check if we have to munge Alpha |
| 43 | bool changesAlpha = (array[15] || array[16] || array[17] || (array[18] - 1) || array[19]); |
| 44 | bool usesAlpha = (array[3] || array[8] || array[13]); |
| 45 | |
| 46 | if (changesAlpha || usesAlpha) { |
| 47 | fFlags = changesAlpha ? 0 : kAlphaUnchanged_Flag; |
| 48 | } else { |
| 49 | fFlags = kAlphaUnchanged_Flag; |
| 50 | } |
| 51 | fFlags |= kSupports4f_Flag; |
| 52 | } |
| 53 | |
| 54 | /////////////////////////////////////////////////////////////////////////////// |
| 55 | |
| 56 | SkColorMatrixFilterRowMajor255::SkColorMatrixFilterRowMajor255(const SkScalar array[20]) { |
| 57 | memcpy(fMatrix, array, 20 * sizeof(SkScalar)); |
| 58 | this->initState(array); |
| 59 | } |
| 60 | |
| 61 | uint32_t SkColorMatrixFilterRowMajor255::getFlags() const { |
| 62 | return this->INHERITED::getFlags() | fFlags; |
| 63 | } |
| 64 | |
| 65 | static Sk4f scale_rgb(float scale) { |
| 66 | static_assert(SkPM4f::A == 3, "Alpha is lane 3"); |
| 67 | return Sk4f(scale, scale, scale, 1); |
| 68 | } |
| 69 | |
| 70 | static Sk4f premul(const Sk4f& x) { |
| 71 | return x * scale_rgb(x.kth<SkPM4f::A>()); |
| 72 | } |
| 73 | |
| 74 | static Sk4f unpremul(const Sk4f& x) { |
| 75 | return x * scale_rgb(1 / x.kth<SkPM4f::A>()); // TODO: fast/approx invert? |
| 76 | } |
| 77 | |
| 78 | static Sk4f clamp_0_1(const Sk4f& x) { |
| 79 | return Sk4f::Max(Sk4f::Min(x, Sk4f(1)), Sk4f(0)); |
| 80 | } |
| 81 | |
| 82 | static SkPMColor round(const Sk4f& x) { |
| 83 | SkPMColor c; |
| 84 | SkNx_cast<uint8_t>(x * Sk4f(255) + Sk4f(0.5f)).store(&c); |
| 85 | return c; |
| 86 | } |
| 87 | |
| 88 | template <typename Adaptor, typename T> |
| 89 | void filter_span(const float array[], const T src[], int count, T dst[]) { |
| 90 | // c0-c3 are already in [0,1]. |
| 91 | const Sk4f c0 = Sk4f::Load(array + 0); |
| 92 | const Sk4f c1 = Sk4f::Load(array + 4); |
| 93 | const Sk4f c2 = Sk4f::Load(array + 8); |
| 94 | const Sk4f c3 = Sk4f::Load(array + 12); |
| 95 | // c4 (the translate vector) is in [0, 255]. Bring it back to [0,1]. |
| 96 | const Sk4f c4 = Sk4f::Load(array + 16)*Sk4f(1.0f/255); |
| 97 | |
| 98 | // todo: we could cache this in the constructor... |
| 99 | T matrix_translate_pmcolor = Adaptor::From4f(premul(clamp_0_1(c4))); |
| 100 | |
| 101 | for (int i = 0; i < count; i++) { |
| 102 | Sk4f srcf = Adaptor::To4f(src[i]); |
| 103 | float srcA = srcf.kth<SkPM4f::A>(); |
| 104 | |
| 105 | if (0 == srcA) { |
| 106 | dst[i] = matrix_translate_pmcolor; |
| 107 | continue; |
| 108 | } |
| 109 | if (1 != srcA) { |
| 110 | srcf = unpremul(srcf); |
| 111 | } |
| 112 | |
| 113 | Sk4f r4 = SkNx_dup<SK_R32_SHIFT/8>(srcf); |
| 114 | Sk4f g4 = SkNx_dup<SK_G32_SHIFT/8>(srcf); |
| 115 | Sk4f b4 = SkNx_dup<SK_B32_SHIFT/8>(srcf); |
| 116 | Sk4f a4 = SkNx_dup<SK_A32_SHIFT/8>(srcf); |
| 117 | |
| 118 | // apply matrix |
| 119 | Sk4f dst4 = c0 * r4 + c1 * g4 + c2 * b4 + c3 * a4 + c4; |
| 120 | |
| 121 | dst[i] = Adaptor::From4f(premul(clamp_0_1(dst4))); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | struct SkPMColorAdaptor { |
| 126 | static SkPMColor From4f(const Sk4f& c4) { |
| 127 | return round(c4); |
| 128 | } |
| 129 | static Sk4f To4f(SkPMColor c) { |
| 130 | return SkNx_cast<float>(Sk4b::Load(&c)) * Sk4f(1.0f/255); |
| 131 | } |
| 132 | }; |
| 133 | void SkColorMatrixFilterRowMajor255::filterSpan(const SkPMColor src[], int count, SkPMColor dst[]) const { |
| 134 | filter_span<SkPMColorAdaptor>(fTranspose, src, count, dst); |
| 135 | } |
| 136 | |
| 137 | struct SkPM4fAdaptor { |
| 138 | static SkPM4f From4f(const Sk4f& c4) { |
| 139 | SkPM4f c; |
| 140 | c4.store(&c); |
| 141 | return c; |
| 142 | } |
| 143 | static Sk4f To4f(const SkPM4f& c) { |
| 144 | return Sk4f::Load(&c); |
| 145 | } |
| 146 | }; |
| 147 | void SkColorMatrixFilterRowMajor255::filterSpan4f(const SkPM4f src[], int count, SkPM4f dst[]) const { |
| 148 | filter_span<SkPM4fAdaptor>(fTranspose, src, count, dst); |
| 149 | } |
| 150 | |
| 151 | /////////////////////////////////////////////////////////////////////////////// |
| 152 | |
| 153 | void SkColorMatrixFilterRowMajor255::flatten(SkWriteBuffer& buffer) const { |
| 154 | SkASSERT(sizeof(fMatrix)/sizeof(SkScalar) == 20); |
| 155 | buffer.writeScalarArray(fMatrix, 20); |
| 156 | } |
| 157 | |
| 158 | SkFlattenable* SkColorMatrixFilterRowMajor255::CreateProc(SkReadBuffer& buffer) { |
| 159 | SkScalar matrix[20]; |
| 160 | if (buffer.readScalarArray(matrix, 20)) { |
| 161 | return new SkColorMatrixFilterRowMajor255(matrix); |
| 162 | } |
| 163 | return nullptr; |
| 164 | } |
| 165 | |
| 166 | bool SkColorMatrixFilterRowMajor255::asColorMatrix(SkScalar matrix[20]) const { |
| 167 | if (matrix) { |
| 168 | memcpy(matrix, fMatrix, 20 * sizeof(SkScalar)); |
| 169 | } |
| 170 | return true; |
| 171 | } |
| 172 | |
| 173 | /////////////////////////////////////////////////////////////////////////////// |
| 174 | // This code was duplicated from src/effects/SkColorMatrixc.cpp in order to be used in core. |
| 175 | ////// |
| 176 | |
| 177 | // To detect if we need to apply clamping after applying a matrix, we check if |
| 178 | // any output component might go outside of [0, 255] for any combination of |
| 179 | // input components in [0..255]. |
| 180 | // Each output component is an affine transformation of the input component, so |
| 181 | // the minimum and maximum values are for any combination of minimum or maximum |
| 182 | // values of input components (i.e. 0 or 255). |
| 183 | // E.g. if R' = x*R + y*G + z*B + w*A + t |
| 184 | // Then the maximum value will be for R=255 if x>0 or R=0 if x<0, and the |
| 185 | // minimum value will be for R=0 if x>0 or R=255 if x<0. |
| 186 | // Same goes for all components. |
| 187 | static bool component_needs_clamping(const SkScalar row[5]) { |
| 188 | SkScalar maxValue = row[4] / 255; |
| 189 | SkScalar minValue = row[4] / 255; |
| 190 | for (int i = 0; i < 4; ++i) { |
| 191 | if (row[i] > 0) |
| 192 | maxValue += row[i]; |
| 193 | else |
| 194 | minValue += row[i]; |
| 195 | } |
| 196 | return (maxValue > 1) || (minValue < 0); |
| 197 | } |
| 198 | |
| 199 | static bool needs_clamping(const SkScalar matrix[20]) { |
| 200 | return component_needs_clamping(matrix) |
| 201 | || component_needs_clamping(matrix+5) |
| 202 | || component_needs_clamping(matrix+10) |
| 203 | || component_needs_clamping(matrix+15); |
| 204 | } |
| 205 | |
| 206 | static void set_concat(SkScalar result[20], const SkScalar outer[20], const SkScalar inner[20]) { |
| 207 | int index = 0; |
| 208 | for (int j = 0; j < 20; j += 5) { |
| 209 | for (int i = 0; i < 4; i++) { |
| 210 | result[index++] = outer[j + 0] * inner[i + 0] + |
| 211 | outer[j + 1] * inner[i + 5] + |
| 212 | outer[j + 2] * inner[i + 10] + |
| 213 | outer[j + 3] * inner[i + 15]; |
| 214 | } |
| 215 | result[index++] = outer[j + 0] * inner[4] + |
| 216 | outer[j + 1] * inner[9] + |
| 217 | outer[j + 2] * inner[14] + |
| 218 | outer[j + 3] * inner[19] + |
| 219 | outer[j + 4]; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | /////////////////////////////////////////////////////////////////////////////// |
| 224 | // End duplication |
| 225 | ////// |
| 226 | |
| 227 | SkColorFilter* SkColorMatrixFilterRowMajor255::newComposed(const SkColorFilter* innerFilter) const { |
| 228 | SkScalar innerMatrix[20]; |
| 229 | if (innerFilter->asColorMatrix(innerMatrix) && !needs_clamping(innerMatrix)) { |
| 230 | SkScalar concat[20]; |
| 231 | set_concat(concat, fMatrix, innerMatrix); |
| 232 | return new SkColorMatrixFilterRowMajor255(concat); |
| 233 | } |
| 234 | return nullptr; |
| 235 | } |
| 236 | |
| 237 | #if SK_SUPPORT_GPU |
| 238 | #include "GrFragmentProcessor.h" |
| 239 | #include "GrInvariantOutput.h" |
| 240 | #include "glsl/GrGLSLFragmentProcessor.h" |
| 241 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
| 242 | #include "glsl/GrGLSLProgramDataManager.h" |
| 243 | #include "glsl/GrGLSLUniformHandler.h" |
| 244 | |
| 245 | class ColorMatrixEffect : public GrFragmentProcessor { |
| 246 | public: |
| 247 | static const GrFragmentProcessor* Create(const SkScalar matrix[20]) { |
| 248 | return new ColorMatrixEffect(matrix); |
| 249 | } |
| 250 | |
| 251 | const char* name() const override { return "Color Matrix"; } |
| 252 | |
| 253 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
| 254 | |
| 255 | class GLSLProcessor : public GrGLSLFragmentProcessor { |
| 256 | public: |
| 257 | // this class always generates the same code. |
| 258 | static void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder* b) {} |
| 259 | |
| 260 | GLSLProcessor(const GrProcessor&) {} |
| 261 | |
| 262 | virtual void emitCode(EmitArgs& args) override { |
| 263 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
| 264 | fMatrixHandle = uniformHandler->addUniform(GrGLSLUniformHandler::kFragment_Visibility, |
| 265 | kMat44f_GrSLType, kDefault_GrSLPrecision, |
| 266 | "ColorMatrix"); |
| 267 | fVectorHandle = uniformHandler->addUniform(GrGLSLUniformHandler::kFragment_Visibility, |
| 268 | kVec4f_GrSLType, kDefault_GrSLPrecision, |
| 269 | "ColorMatrixVector"); |
| 270 | |
| 271 | if (nullptr == args.fInputColor) { |
| 272 | // could optimize this case, but we aren't for now. |
| 273 | args.fInputColor = "vec4(1)"; |
| 274 | } |
| 275 | GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 276 | // The max() is to guard against 0 / 0 during unpremul when the incoming color is |
| 277 | // transparent black. |
| 278 | fragBuilder->codeAppendf("\tfloat nonZeroAlpha = max(%s.a, 0.00001);\n", |
| 279 | args.fInputColor); |
| 280 | fragBuilder->codeAppendf("\t%s = %s * vec4(%s.rgb / nonZeroAlpha, nonZeroAlpha) + %s;\n", |
| 281 | args.fOutputColor, |
| 282 | uniformHandler->getUniformCStr(fMatrixHandle), |
| 283 | args.fInputColor, |
| 284 | uniformHandler->getUniformCStr(fVectorHandle)); |
| 285 | fragBuilder->codeAppendf("\t%s = clamp(%s, 0.0, 1.0);\n", |
| 286 | args.fOutputColor, args.fOutputColor); |
| 287 | fragBuilder->codeAppendf("\t%s.rgb *= %s.a;\n", args.fOutputColor, args.fOutputColor); |
| 288 | } |
| 289 | |
| 290 | protected: |
| 291 | virtual void onSetData(const GrGLSLProgramDataManager& uniManager, |
| 292 | const GrProcessor& proc) override { |
| 293 | const ColorMatrixEffect& cme = proc.cast<ColorMatrixEffect>(); |
| 294 | const float* m = cme.fMatrix; |
| 295 | // The GL matrix is transposed from SkColorMatrix. |
| 296 | float mt[] = { |
| 297 | m[0], m[5], m[10], m[15], |
| 298 | m[1], m[6], m[11], m[16], |
| 299 | m[2], m[7], m[12], m[17], |
| 300 | m[3], m[8], m[13], m[18], |
| 301 | }; |
| 302 | static const float kScale = 1.0f / 255.0f; |
| 303 | float vec[] = { |
| 304 | m[4] * kScale, m[9] * kScale, m[14] * kScale, m[19] * kScale, |
| 305 | }; |
| 306 | uniManager.setMatrix4fv(fMatrixHandle, 1, mt); |
| 307 | uniManager.set4fv(fVectorHandle, 1, vec); |
| 308 | } |
| 309 | |
| 310 | private: |
| 311 | GrGLSLProgramDataManager::UniformHandle fMatrixHandle; |
| 312 | GrGLSLProgramDataManager::UniformHandle fVectorHandle; |
| 313 | |
| 314 | typedef GrGLSLFragmentProcessor INHERITED; |
| 315 | }; |
| 316 | |
| 317 | private: |
| 318 | ColorMatrixEffect(const SkScalar matrix[20]) { |
| 319 | memcpy(fMatrix, matrix, sizeof(SkScalar) * 20); |
| 320 | this->initClassID<ColorMatrixEffect>(); |
| 321 | } |
| 322 | |
| 323 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { |
| 324 | return new GLSLProcessor(*this); |
| 325 | } |
| 326 | |
| 327 | virtual void onGetGLSLProcessorKey(const GrGLSLCaps& caps, |
| 328 | GrProcessorKeyBuilder* b) const override { |
| 329 | GLSLProcessor::GenKey(*this, caps, b); |
| 330 | } |
| 331 | |
| 332 | bool onIsEqual(const GrFragmentProcessor& s) const override { |
| 333 | const ColorMatrixEffect& cme = s.cast<ColorMatrixEffect>(); |
| 334 | return 0 == memcmp(fMatrix, cme.fMatrix, sizeof(fMatrix)); |
| 335 | } |
| 336 | |
| 337 | void onComputeInvariantOutput(GrInvariantOutput* inout) const override { |
| 338 | // We only bother to check whether the alpha channel will be constant. If SkColorMatrix had |
| 339 | // type flags it might be worth checking the other components. |
| 340 | |
| 341 | // The matrix is defined such the 4th row determines the output alpha. The first four |
| 342 | // columns of that row multiply the input r, g, b, and a, respectively, and the last column |
| 343 | // is the "translation". |
| 344 | static const uint32_t kRGBAFlags[] = { |
| 345 | kR_GrColorComponentFlag, |
| 346 | kG_GrColorComponentFlag, |
| 347 | kB_GrColorComponentFlag, |
| 348 | kA_GrColorComponentFlag |
| 349 | }; |
| 350 | static const int kShifts[] = { |
| 351 | GrColor_SHIFT_R, GrColor_SHIFT_G, GrColor_SHIFT_B, GrColor_SHIFT_A, |
| 352 | }; |
| 353 | enum { |
| 354 | kAlphaRowStartIdx = 15, |
| 355 | kAlphaRowTranslateIdx = 19, |
| 356 | }; |
| 357 | |
| 358 | SkScalar outputA = 0; |
| 359 | for (int i = 0; i < 4; ++i) { |
| 360 | // If any relevant component of the color to be passed through the matrix is non-const |
| 361 | // then we can't know the final result. |
| 362 | if (0 != fMatrix[kAlphaRowStartIdx + i]) { |
| 363 | if (!(inout->validFlags() & kRGBAFlags[i])) { |
| 364 | inout->setToUnknown(GrInvariantOutput::kWill_ReadInput); |
| 365 | return; |
| 366 | } else { |
| 367 | uint32_t component = (inout->color() >> kShifts[i]) & 0xFF; |
| 368 | outputA += fMatrix[kAlphaRowStartIdx + i] * component; |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | outputA += fMatrix[kAlphaRowTranslateIdx]; |
| 373 | // We pin the color to [0,1]. This would happen to the *final* color output from the frag |
| 374 | // shader but currently the effect does not pin its own output. So in the case of over/ |
| 375 | // underflow this may deviate from the actual result. Maybe the effect should pin its |
| 376 | // result if the matrix could over/underflow for any component? |
| 377 | inout->setToOther(kA_GrColorComponentFlag, |
| 378 | static_cast<uint8_t>(SkScalarPin(outputA, 0, 255)) << GrColor_SHIFT_A, |
| 379 | GrInvariantOutput::kWill_ReadInput); |
| 380 | } |
| 381 | |
| 382 | SkScalar fMatrix[20]; |
| 383 | |
| 384 | typedef GrFragmentProcessor INHERITED; |
| 385 | }; |
| 386 | |
| 387 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(ColorMatrixEffect); |
| 388 | |
| 389 | const GrFragmentProcessor* ColorMatrixEffect::TestCreate(GrProcessorTestData* d) { |
| 390 | SkScalar colorMatrix[20]; |
| 391 | for (size_t i = 0; i < SK_ARRAY_COUNT(colorMatrix); ++i) { |
| 392 | colorMatrix[i] = d->fRandom->nextSScalar1(); |
| 393 | } |
| 394 | return ColorMatrixEffect::Create(colorMatrix); |
| 395 | } |
| 396 | |
| 397 | const GrFragmentProcessor* SkColorMatrixFilterRowMajor255::asFragmentProcessor(GrContext*) const { |
| 398 | return ColorMatrixEffect::Create(fMatrix); |
| 399 | } |
| 400 | |
| 401 | #endif |
| 402 | |
| 403 | #ifndef SK_IGNORE_TO_STRING |
| 404 | void SkColorMatrixFilterRowMajor255::toString(SkString* str) const { |
| 405 | str->append("SkColorMatrixFilterRowMajor255: "); |
| 406 | |
| 407 | str->append("matrix: ("); |
| 408 | for (int i = 0; i < 20; ++i) { |
| 409 | str->appendScalar(fMatrix[i]); |
| 410 | if (i < 19) { |
| 411 | str->append(", "); |
| 412 | } |
| 413 | } |
| 414 | str->append(")"); |
| 415 | } |
| 416 | #endif |
| 417 | |
| 418 | /////////////////////////////////////////////////////////////////////////////// |
| 419 | |
| 420 | SkColorFilter* SkColorFilter::CreateMatrixFilterRowMajor255(const SkScalar array[20]) { |
| 421 | return new SkColorMatrixFilterRowMajor255(array); |
| 422 | } |