epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SkColorMatrixFilter.h" |
| 9 | #include "SkColorMatrix.h" |
| 10 | #include "SkColorPriv.h" |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 11 | #include "SkFlattenableBuffers.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 12 | #include "SkUnPreMultiply.h" |
robertphillips@google.com | 0fb775c | 2013-05-23 14:11:41 +0000 | [diff] [blame] | 13 | #include "SkString.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | |
| 15 | static int32_t rowmul4(const int32_t array[], unsigned r, unsigned g, |
| 16 | unsigned b, unsigned a) { |
| 17 | return array[0] * r + array[1] * g + array[2] * b + array[3] * a + array[4]; |
| 18 | } |
| 19 | |
| 20 | static int32_t rowmul3(const int32_t array[], unsigned r, unsigned g, |
| 21 | unsigned b) { |
| 22 | return array[0] * r + array[1] * g + array[2] * b + array[4]; |
| 23 | } |
| 24 | |
reed@google.com | bada644 | 2012-12-17 20:21:44 +0000 | [diff] [blame] | 25 | static void General(const SkColorMatrixFilter::State& state, |
| 26 | unsigned r, unsigned g, unsigned b, unsigned a, |
| 27 | int32_t* SK_RESTRICT result) { |
| 28 | const int32_t* SK_RESTRICT array = state.fArray; |
| 29 | const int shift = state.fShift; |
tomhudson@google.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 30 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 31 | result[0] = rowmul4(&array[0], r, g, b, a) >> shift; |
| 32 | result[1] = rowmul4(&array[5], r, g, b, a) >> shift; |
| 33 | result[2] = rowmul4(&array[10], r, g, b, a) >> shift; |
| 34 | result[3] = rowmul4(&array[15], r, g, b, a) >> shift; |
| 35 | } |
| 36 | |
reed@google.com | bada644 | 2012-12-17 20:21:44 +0000 | [diff] [blame] | 37 | static void General16(const SkColorMatrixFilter::State& state, |
| 38 | unsigned r, unsigned g, unsigned b, unsigned a, |
| 39 | int32_t* SK_RESTRICT result) { |
| 40 | const int32_t* SK_RESTRICT array = state.fArray; |
tomhudson@google.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 41 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 42 | result[0] = rowmul4(&array[0], r, g, b, a) >> 16; |
| 43 | result[1] = rowmul4(&array[5], r, g, b, a) >> 16; |
| 44 | result[2] = rowmul4(&array[10], r, g, b, a) >> 16; |
| 45 | result[3] = rowmul4(&array[15], r, g, b, a) >> 16; |
| 46 | } |
| 47 | |
reed@google.com | bada644 | 2012-12-17 20:21:44 +0000 | [diff] [blame] | 48 | static void AffineAdd(const SkColorMatrixFilter::State& state, |
| 49 | unsigned r, unsigned g, unsigned b, unsigned a, |
| 50 | int32_t* SK_RESTRICT result) { |
| 51 | const int32_t* SK_RESTRICT array = state.fArray; |
| 52 | const int shift = state.fShift; |
tomhudson@google.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 53 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 54 | result[0] = rowmul3(&array[0], r, g, b) >> shift; |
| 55 | result[1] = rowmul3(&array[5], r, g, b) >> shift; |
| 56 | result[2] = rowmul3(&array[10], r, g, b) >> shift; |
| 57 | result[3] = a; |
| 58 | } |
| 59 | |
reed@google.com | bada644 | 2012-12-17 20:21:44 +0000 | [diff] [blame] | 60 | static void AffineAdd16(const SkColorMatrixFilter::State& state, |
| 61 | unsigned r, unsigned g, unsigned b, unsigned a, |
| 62 | int32_t* SK_RESTRICT result) { |
| 63 | const int32_t* SK_RESTRICT array = state.fArray; |
tomhudson@google.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 64 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 65 | result[0] = rowmul3(&array[0], r, g, b) >> 16; |
| 66 | result[1] = rowmul3(&array[5], r, g, b) >> 16; |
| 67 | result[2] = rowmul3(&array[10], r, g, b) >> 16; |
| 68 | result[3] = a; |
| 69 | } |
| 70 | |
reed@google.com | bada644 | 2012-12-17 20:21:44 +0000 | [diff] [blame] | 71 | static void ScaleAdd(const SkColorMatrixFilter::State& state, |
| 72 | unsigned r, unsigned g, unsigned b, unsigned a, |
| 73 | int32_t* SK_RESTRICT result) { |
| 74 | const int32_t* SK_RESTRICT array = state.fArray; |
| 75 | const int shift = state.fShift; |
tomhudson@google.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 76 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 77 | // cast to (int) to keep the expression signed for the shift |
| 78 | result[0] = (array[0] * (int)r + array[4]) >> shift; |
| 79 | result[1] = (array[6] * (int)g + array[9]) >> shift; |
| 80 | result[2] = (array[12] * (int)b + array[14]) >> shift; |
| 81 | result[3] = a; |
| 82 | } |
| 83 | |
reed@google.com | bada644 | 2012-12-17 20:21:44 +0000 | [diff] [blame] | 84 | static void ScaleAdd16(const SkColorMatrixFilter::State& state, |
| 85 | unsigned r, unsigned g, unsigned b, unsigned a, |
| 86 | int32_t* SK_RESTRICT result) { |
| 87 | const int32_t* SK_RESTRICT array = state.fArray; |
tomhudson@google.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 88 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 89 | // cast to (int) to keep the expression signed for the shift |
| 90 | result[0] = (array[0] * (int)r + array[4]) >> 16; |
| 91 | result[1] = (array[6] * (int)g + array[9]) >> 16; |
| 92 | result[2] = (array[12] * (int)b + array[14]) >> 16; |
| 93 | result[3] = a; |
| 94 | } |
| 95 | |
reed@google.com | bada644 | 2012-12-17 20:21:44 +0000 | [diff] [blame] | 96 | static void Add(const SkColorMatrixFilter::State& state, |
| 97 | unsigned r, unsigned g, unsigned b, unsigned a, |
| 98 | int32_t* SK_RESTRICT result) { |
| 99 | const int32_t* SK_RESTRICT array = state.fArray; |
| 100 | const int shift = state.fShift; |
tomhudson@google.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 101 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 102 | result[0] = r + (array[4] >> shift); |
| 103 | result[1] = g + (array[9] >> shift); |
| 104 | result[2] = b + (array[14] >> shift); |
| 105 | result[3] = a; |
| 106 | } |
| 107 | |
reed@google.com | bada644 | 2012-12-17 20:21:44 +0000 | [diff] [blame] | 108 | static void Add16(const SkColorMatrixFilter::State& state, |
| 109 | unsigned r, unsigned g, unsigned b, unsigned a, |
| 110 | int32_t* SK_RESTRICT result) { |
| 111 | const int32_t* SK_RESTRICT array = state.fArray; |
tomhudson@google.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 112 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 113 | result[0] = r + (array[4] >> 16); |
| 114 | result[1] = g + (array[9] >> 16); |
| 115 | result[2] = b + (array[14] >> 16); |
| 116 | result[3] = a; |
| 117 | } |
| 118 | |
| 119 | #define kNO_ALPHA_FLAGS (SkColorFilter::kAlphaUnchanged_Flag | \ |
| 120 | SkColorFilter::kHasFilter16_Flag) |
| 121 | |
tomhudson@google.com | a87cd2a | 2011-06-15 16:50:27 +0000 | [diff] [blame] | 122 | // src is [20] but some compilers won't accept __restrict__ on anything |
| 123 | // but an raw pointer or reference |
reed@google.com | 7b6c193 | 2012-06-04 19:07:41 +0000 | [diff] [blame] | 124 | void SkColorMatrixFilter::initState(const SkScalar* SK_RESTRICT src) { |
senorblanco@chromium.org | a0b0c12 | 2012-01-04 22:00:34 +0000 | [diff] [blame] | 125 | int32_t* array = fState.fArray; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 126 | SkFixed max = 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 127 | for (int i = 0; i < 20; i++) { |
| 128 | SkFixed value = SkScalarToFixed(src[i]); |
| 129 | array[i] = value; |
| 130 | value = SkAbs32(value); |
| 131 | max = SkMax32(max, value); |
| 132 | } |
tomhudson@google.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 133 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 134 | /* All of fArray[] values must fit in 23 bits, to safely allow me to |
| 135 | multiply them by 8bit unsigned values, and get a signed answer without |
| 136 | overflow. This means clz needs to be 9 or bigger |
| 137 | */ |
| 138 | int bits = SkCLZ(max); |
| 139 | int32_t one = SK_Fixed1; |
| 140 | |
| 141 | fState.fShift = 16; // we are starting out as fixed 16.16 |
| 142 | if (bits < 9) { |
| 143 | bits = 9 - bits; |
| 144 | fState.fShift -= bits; |
tomhudson@google.com | 5efe0cb | 2012-04-10 19:14:48 +0000 | [diff] [blame] | 145 | for (int i = 0; i < 20; i++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 146 | array[i] >>= bits; |
| 147 | } |
| 148 | one >>= bits; |
| 149 | } |
tomhudson@google.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 150 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 151 | // check if we have to munge Alpha |
| 152 | int32_t changesAlpha = (array[15] | array[16] | array[17] | |
| 153 | (array[18] - one) | array[19]); |
| 154 | int32_t usesAlpha = (array[3] | array[8] | array[13]); |
| 155 | bool shiftIs16 = (16 == fState.fShift); |
| 156 | |
| 157 | if (changesAlpha | usesAlpha) { |
| 158 | fProc = shiftIs16 ? General16 : General; |
| 159 | fFlags = changesAlpha ? 0 : SkColorFilter::kAlphaUnchanged_Flag; |
| 160 | } else { |
| 161 | fFlags = kNO_ALPHA_FLAGS; |
| 162 | |
| 163 | int32_t needsScale = (array[0] - one) | // red axis |
| 164 | (array[6] - one) | // green axis |
| 165 | (array[12] - one); // blue axis |
| 166 | |
| 167 | int32_t needs3x3 = array[1] | array[2] | // red off-axis |
| 168 | array[5] | array[7] | // green off-axis |
| 169 | array[10] | array[11]; // blue off-axis |
| 170 | |
| 171 | if (needs3x3) { |
| 172 | fProc = shiftIs16 ? AffineAdd16 : AffineAdd; |
| 173 | } else if (needsScale) { |
| 174 | fProc = shiftIs16 ? ScaleAdd16 : ScaleAdd; |
| 175 | } else if (array[4] | array[9] | array[14]) { // needs add |
| 176 | fProc = shiftIs16 ? Add16 : Add; |
| 177 | } else { |
| 178 | fProc = NULL; // identity |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | /* preround our add values so we get a rounded shift. We do this after we |
| 183 | analyze the array, so we don't miss the case where the caller has zeros |
| 184 | which could make us accidentally take the General or Add case. |
| 185 | */ |
| 186 | if (NULL != fProc) { |
| 187 | int32_t add = 1 << (fState.fShift - 1); |
| 188 | array[4] += add; |
| 189 | array[9] += add; |
| 190 | array[14] += add; |
| 191 | array[19] += add; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | /////////////////////////////////////////////////////////////////////////////// |
| 196 | |
| 197 | static int32_t pin(int32_t value, int32_t max) { |
| 198 | if (value < 0) { |
| 199 | value = 0; |
| 200 | } |
| 201 | if (value > max) { |
| 202 | value = max; |
| 203 | } |
| 204 | return value; |
| 205 | } |
| 206 | |
reed@google.com | 7b6c193 | 2012-06-04 19:07:41 +0000 | [diff] [blame] | 207 | SkColorMatrixFilter::SkColorMatrixFilter(const SkColorMatrix& cm) : fMatrix(cm) { |
| 208 | this->initState(cm.fMat); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | SkColorMatrixFilter::SkColorMatrixFilter(const SkScalar array[20]) { |
reed@google.com | 7b6c193 | 2012-06-04 19:07:41 +0000 | [diff] [blame] | 212 | memcpy(fMatrix.fMat, array, 20 * sizeof(SkScalar)); |
| 213 | this->initState(array); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 214 | } |
| 215 | |
reed@google.com | bada644 | 2012-12-17 20:21:44 +0000 | [diff] [blame] | 216 | uint32_t SkColorMatrixFilter::getFlags() const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 217 | return this->INHERITED::getFlags() | fFlags; |
| 218 | } |
| 219 | |
| 220 | void SkColorMatrixFilter::filterSpan(const SkPMColor src[], int count, |
reed@google.com | bada644 | 2012-12-17 20:21:44 +0000 | [diff] [blame] | 221 | SkPMColor dst[]) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 222 | Proc proc = fProc; |
reed@google.com | bada644 | 2012-12-17 20:21:44 +0000 | [diff] [blame] | 223 | const State& state = fState; |
| 224 | int32_t result[4]; |
tomhudson@google.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 225 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 226 | if (NULL == proc) { |
| 227 | if (src != dst) { |
| 228 | memcpy(dst, src, count * sizeof(SkPMColor)); |
| 229 | } |
| 230 | return; |
| 231 | } |
tomhudson@google.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 232 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 233 | const SkUnPreMultiply::Scale* table = SkUnPreMultiply::GetScaleTable(); |
| 234 | |
| 235 | for (int i = 0; i < count; i++) { |
| 236 | SkPMColor c = src[i]; |
tomhudson@google.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 237 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 238 | unsigned r = SkGetPackedR32(c); |
| 239 | unsigned g = SkGetPackedG32(c); |
| 240 | unsigned b = SkGetPackedB32(c); |
| 241 | unsigned a = SkGetPackedA32(c); |
tomhudson@google.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 242 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 243 | // need our components to be un-premultiplied |
| 244 | if (255 != a) { |
| 245 | SkUnPreMultiply::Scale scale = table[a]; |
| 246 | r = SkUnPreMultiply::ApplyScale(scale, r); |
| 247 | g = SkUnPreMultiply::ApplyScale(scale, g); |
| 248 | b = SkUnPreMultiply::ApplyScale(scale, b); |
tomhudson@google.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 249 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 250 | SkASSERT(r <= 255); |
| 251 | SkASSERT(g <= 255); |
| 252 | SkASSERT(b <= 255); |
| 253 | } |
tomhudson@google.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 254 | |
reed@google.com | bada644 | 2012-12-17 20:21:44 +0000 | [diff] [blame] | 255 | proc(state, r, g, b, a, result); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 256 | |
| 257 | r = pin(result[0], SK_R32_MASK); |
| 258 | g = pin(result[1], SK_G32_MASK); |
| 259 | b = pin(result[2], SK_B32_MASK); |
| 260 | a = pin(result[3], SK_A32_MASK); |
| 261 | // re-prepremultiply if needed |
senorblanco@chromium.org | df28f07 | 2012-01-03 22:42:26 +0000 | [diff] [blame] | 262 | dst[i] = SkPremultiplyARGBInline(a, r, g, b); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | |
| 266 | void SkColorMatrixFilter::filterSpan16(const uint16_t src[], int count, |
reed@google.com | bada644 | 2012-12-17 20:21:44 +0000 | [diff] [blame] | 267 | uint16_t dst[]) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 268 | SkASSERT(fFlags & SkColorFilter::kHasFilter16_Flag); |
| 269 | |
| 270 | Proc proc = fProc; |
reed@google.com | bada644 | 2012-12-17 20:21:44 +0000 | [diff] [blame] | 271 | const State& state = fState; |
| 272 | int32_t result[4]; |
tomhudson@google.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 273 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 274 | if (NULL == proc) { |
| 275 | if (src != dst) { |
| 276 | memcpy(dst, src, count * sizeof(uint16_t)); |
| 277 | } |
| 278 | return; |
| 279 | } |
| 280 | |
| 281 | for (int i = 0; i < count; i++) { |
| 282 | uint16_t c = src[i]; |
tomhudson@google.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 283 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 284 | // expand to 8bit components (since our matrix translate is 8bit biased |
| 285 | unsigned r = SkPacked16ToR32(c); |
| 286 | unsigned g = SkPacked16ToG32(c); |
| 287 | unsigned b = SkPacked16ToB32(c); |
tomhudson@google.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 288 | |
reed@google.com | bada644 | 2012-12-17 20:21:44 +0000 | [diff] [blame] | 289 | proc(state, r, g, b, 0, result); |
tomhudson@google.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 290 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 291 | r = pin(result[0], SK_R32_MASK); |
| 292 | g = pin(result[1], SK_G32_MASK); |
| 293 | b = pin(result[2], SK_B32_MASK); |
tomhudson@google.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 294 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 295 | // now packed it back down to 16bits (hmmm, could dither...) |
| 296 | dst[i] = SkPack888ToRGB16(r, g, b); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | /////////////////////////////////////////////////////////////////////////////// |
| 301 | |
djsollen@google.com | 5492424 | 2012-03-29 15:18:04 +0000 | [diff] [blame] | 302 | void SkColorMatrixFilter::flatten(SkFlattenableWriteBuffer& buffer) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 303 | this->INHERITED::flatten(buffer); |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 304 | SkASSERT(sizeof(fMatrix.fMat)/sizeof(SkScalar) == 20); |
| 305 | buffer.writeScalarArray(fMatrix.fMat, 20); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 306 | } |
tomhudson@google.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 307 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 308 | SkColorMatrixFilter::SkColorMatrixFilter(SkFlattenableReadBuffer& buffer) |
| 309 | : INHERITED(buffer) { |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 310 | SkASSERT(buffer.getArrayCount() == 20); |
commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 311 | buffer.readScalarArray(fMatrix.fMat, 20); |
reed@google.com | 7b6c193 | 2012-06-04 19:07:41 +0000 | [diff] [blame] | 312 | this->initState(fMatrix.fMat); |
commit-bot@chromium.org | c0b7e10 | 2013-10-23 17:06:21 +0000 | [diff] [blame] | 313 | for (int i = 0; i < 20; ++i) { |
| 314 | buffer.validate(SkScalarIsFinite(fMatrix.fMat[i])); |
| 315 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 316 | } |
tomhudson@google.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 317 | |
reed@google.com | bada644 | 2012-12-17 20:21:44 +0000 | [diff] [blame] | 318 | bool SkColorMatrixFilter::asColorMatrix(SkScalar matrix[20]) const { |
reed@google.com | 7b6c193 | 2012-06-04 19:07:41 +0000 | [diff] [blame] | 319 | if (matrix) { |
| 320 | memcpy(matrix, fMatrix.fMat, 20 * sizeof(SkScalar)); |
senorblanco@chromium.org | e5ff3ce | 2011-12-20 20:58:18 +0000 | [diff] [blame] | 321 | } |
| 322 | return true; |
| 323 | } |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 324 | |
| 325 | #if SK_SUPPORT_GPU |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 326 | #include "GrEffect.h" |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 327 | #include "GrTBackendEffectFactory.h" |
bsalomon@google.com | d698f77 | 2012-10-25 13:22:00 +0000 | [diff] [blame] | 328 | #include "gl/GrGLEffect.h" |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 329 | |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 330 | class ColorMatrixEffect : public GrEffect { |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 331 | public: |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 332 | static GrEffectRef* Create(const SkColorMatrix& matrix) { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 333 | AutoEffectUnref effect(SkNEW_ARGS(ColorMatrixEffect, (matrix))); |
bsalomon@google.com | a1ebbe4 | 2013-01-16 15:51:47 +0000 | [diff] [blame] | 334 | return CreateEffectRef(effect); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 335 | } |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 336 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 337 | static const char* Name() { return "Color Matrix"; } |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 338 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 339 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { |
| 340 | return GrTBackendEffectFactory<ColorMatrixEffect>::getInstance(); |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 341 | } |
| 342 | |
bsalomon@google.com | 371e105 | 2013-01-11 21:08:55 +0000 | [diff] [blame] | 343 | virtual void getConstantColorComponents(GrColor* color, |
| 344 | uint32_t* validFlags) const SK_OVERRIDE { |
| 345 | // We only bother to check whether the alpha channel will be constant. If SkColorMatrix had |
| 346 | // type flags it might be worth checking the other components. |
| 347 | |
| 348 | // The matrix is defined such the 4th row determines the output alpha. The first four |
| 349 | // columns of that row multiply the input r, g, b, and a, respectively, and the last column |
| 350 | // is the "translation". |
bsalomon@google.com | b8eb2e8 | 2013-03-28 13:46:42 +0000 | [diff] [blame] | 351 | static const uint32_t kRGBAFlags[] = { |
| 352 | kR_GrColorComponentFlag, |
| 353 | kG_GrColorComponentFlag, |
| 354 | kB_GrColorComponentFlag, |
| 355 | kA_GrColorComponentFlag |
bsalomon@google.com | 371e105 | 2013-01-11 21:08:55 +0000 | [diff] [blame] | 356 | }; |
| 357 | static const int kShifts[] = { |
| 358 | GrColor_SHIFT_R, GrColor_SHIFT_G, GrColor_SHIFT_B, GrColor_SHIFT_A, |
| 359 | }; |
| 360 | enum { |
| 361 | kAlphaRowStartIdx = 15, |
| 362 | kAlphaRowTranslateIdx = 19, |
| 363 | }; |
| 364 | |
| 365 | SkScalar outputA = 0; |
| 366 | for (int i = 0; i < 4; ++i) { |
| 367 | // If any relevant component of the color to be passed through the matrix is non-const |
| 368 | // then we can't know the final result. |
| 369 | if (0 != fMatrix.fMat[kAlphaRowStartIdx + i]) { |
| 370 | if (!(*validFlags & kRGBAFlags[i])) { |
| 371 | *validFlags = 0; |
| 372 | return; |
| 373 | } else { |
| 374 | uint32_t component = (*color >> kShifts[i]) & 0xFF; |
| 375 | outputA += fMatrix.fMat[kAlphaRowStartIdx + i] * component; |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | outputA += fMatrix.fMat[kAlphaRowTranslateIdx]; |
bsalomon@google.com | b8eb2e8 | 2013-03-28 13:46:42 +0000 | [diff] [blame] | 380 | *validFlags = kA_GrColorComponentFlag; |
bsalomon@google.com | 371e105 | 2013-01-11 21:08:55 +0000 | [diff] [blame] | 381 | // We pin the color to [0,1]. This would happen to the *final* color output from the frag |
| 382 | // shader but currently the effect does not pin its own output. So in the case of over/ |
| 383 | // underflow this may deviate from the actual result. Maybe the effect should pin its |
| 384 | // result if the matrix could over/underflow for any component? |
| 385 | *color = static_cast<uint8_t>(SkScalarPin(outputA, 0, 255)) << GrColor_SHIFT_A; |
| 386 | } |
| 387 | |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 388 | GR_DECLARE_EFFECT_TEST; |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 389 | |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 390 | class GLEffect : public GrGLEffect { |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 391 | public: |
| 392 | // this class always generates the same code. |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 393 | static EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&) { return 0; } |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 394 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 395 | GLEffect(const GrBackendEffectFactory& factory, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 396 | const GrDrawEffect&) |
commit-bot@chromium.org | 7425c12 | 2013-08-14 18:14:19 +0000 | [diff] [blame] | 397 | : INHERITED(factory) { |
| 398 | } |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 399 | |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 400 | virtual void emitCode(GrGLShaderBuilder* builder, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 401 | const GrDrawEffect&, |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 402 | EffectKey, |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 403 | const char* outputColor, |
| 404 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 405 | const TransformedCoordsArray&, |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 406 | const TextureSamplerArray&) SK_OVERRIDE { |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 407 | fMatrixHandle = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 408 | kMat44f_GrSLType, |
| 409 | "ColorMatrix"); |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 410 | fVectorHandle = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 411 | kVec4f_GrSLType, |
| 412 | "ColorMatrixVector"); |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 413 | |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 414 | if (NULL == inputColor) { |
| 415 | // could optimize this case, but we aren't for now. |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 416 | inputColor = "vec4(1)"; |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 417 | } |
skia.committer@gmail.com | 989a95e | 2012-10-18 02:01:23 +0000 | [diff] [blame] | 418 | // The max() is to guard against 0 / 0 during unpremul when the incoming color is |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 419 | // transparent black. |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 420 | builder->fsCodeAppendf("\tfloat nonZeroAlpha = max(%s.a, 0.00001);\n", inputColor); |
| 421 | builder->fsCodeAppendf("\t%s = %s * vec4(%s.rgb / nonZeroAlpha, nonZeroAlpha) + %s;\n", |
| 422 | outputColor, |
| 423 | builder->getUniformCStr(fMatrixHandle), |
| 424 | inputColor, |
| 425 | builder->getUniformCStr(fVectorHandle)); |
| 426 | builder->fsCodeAppendf("\t%s.rgb *= %s.a;\n", outputColor, outputColor); |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | virtual void setData(const GrGLUniformManager& uniManager, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 430 | const GrDrawEffect& drawEffect) SK_OVERRIDE { |
| 431 | const ColorMatrixEffect& cme = drawEffect.castEffect<ColorMatrixEffect>(); |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 432 | const float* m = cme.fMatrix.fMat; |
| 433 | // The GL matrix is transposed from SkColorMatrix. |
| 434 | GrGLfloat mt[] = { |
| 435 | m[0], m[5], m[10], m[15], |
| 436 | m[1], m[6], m[11], m[16], |
| 437 | m[2], m[7], m[12], m[17], |
| 438 | m[3], m[8], m[13], m[18], |
| 439 | }; |
| 440 | static const float kScale = 1.0f / 255.0f; |
| 441 | GrGLfloat vec[] = { |
| 442 | m[4] * kScale, m[9] * kScale, m[14] * kScale, m[19] * kScale, |
| 443 | }; |
commit-bot@chromium.org | d3baf20 | 2013-11-07 22:06:08 +0000 | [diff] [blame^] | 444 | uniManager.setMatrix4fv(fMatrixHandle, 1, mt); |
| 445 | uniManager.set4fv(fVectorHandle, 1, vec); |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | private: |
| 449 | GrGLUniformManager::UniformHandle fMatrixHandle; |
| 450 | GrGLUniformManager::UniformHandle fVectorHandle; |
| 451 | }; |
| 452 | |
| 453 | private: |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 454 | ColorMatrixEffect(const SkColorMatrix& matrix) : fMatrix(matrix) {} |
| 455 | |
bsalomon@google.com | 8a252f7 | 2013-01-22 20:35:13 +0000 | [diff] [blame] | 456 | virtual bool onIsEqual(const GrEffect& s) const { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 457 | const ColorMatrixEffect& cme = CastEffect<ColorMatrixEffect>(s); |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 458 | return cme.fMatrix == fMatrix; |
| 459 | } |
| 460 | |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 461 | SkColorMatrix fMatrix; |
bsalomon@google.com | 374e759 | 2012-10-23 17:30:45 +0000 | [diff] [blame] | 462 | |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 463 | typedef GrGLEffect INHERITED; |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 464 | }; |
| 465 | |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 466 | GR_DEFINE_EFFECT_TEST(ColorMatrixEffect); |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 467 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 468 | GrEffectRef* ColorMatrixEffect::TestCreate(SkRandom* random, |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 469 | GrContext*, |
bsalomon@google.com | c26d94f | 2013-03-25 18:19:00 +0000 | [diff] [blame] | 470 | const GrDrawTargetCaps&, |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 471 | GrTexture* dummyTextures[2]) { |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 472 | SkColorMatrix colorMatrix; |
robertphillips@google.com | 93f0332 | 2012-12-03 17:35:19 +0000 | [diff] [blame] | 473 | for (size_t i = 0; i < SK_ARRAY_COUNT(colorMatrix.fMat); ++i) { |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 474 | colorMatrix.fMat[i] = random->nextSScalar1(); |
| 475 | } |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 476 | return ColorMatrixEffect::Create(colorMatrix); |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 477 | } |
| 478 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 479 | GrEffectRef* SkColorMatrixFilter::asNewEffect(GrContext*) const { |
| 480 | return ColorMatrixEffect::Create(fMatrix); |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | #endif |
robertphillips@google.com | 1202c2a | 2013-05-23 14:00:17 +0000 | [diff] [blame] | 484 | |
| 485 | #ifdef SK_DEVELOPER |
| 486 | void SkColorMatrixFilter::toString(SkString* str) const { |
| 487 | str->append("SkColorMatrixFilter: "); |
| 488 | |
| 489 | str->append("matrix: ("); |
| 490 | for (int i = 0; i < 20; ++i) { |
| 491 | str->appendScalar(fMatrix.fMat[i]); |
| 492 | if (i < 19) { |
| 493 | str->append(", "); |
| 494 | } |
| 495 | } |
| 496 | str->append(")"); |
| 497 | } |
| 498 | #endif |