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