Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(34)

Side by Side Diff: src/effects/SkColorMatrixFilter.cpp

Issue 1047823002: back to Sk4f for SkPMColor (Closed) Base URL: https://skia.googlesource.com/skia@master
Patch Set: rebase Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/core/SkPMFloat.h ('k') | src/opts/SkPMFloat_SSSE3.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2011 Google Inc. 2 * Copyright 2011 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkColorMatrixFilter.h" 8 #include "SkColorMatrixFilter.h"
9 #include "SkColorMatrix.h" 9 #include "SkColorMatrix.h"
10 #include "SkColorPriv.h" 10 #include "SkColorPriv.h"
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after
248 * 248 *
249 * new_red = old_red * alpha / 255 249 * new_red = old_red * alpha / 255
250 * 250 *
251 * However, 1.0f/255 comes to (in hex) 0x3B808081, which is slightly bigger tha n the "actual" 251 * However, 1.0f/255 comes to (in hex) 0x3B808081, which is slightly bigger tha n the "actual"
252 * value of 0x3B808080(repeat 80)... This slightly too-big value can cause us t o compute 252 * value of 0x3B808080(repeat 80)... This slightly too-big value can cause us t o compute
253 * new_red > alpha, which is a problem (for valid premul). To fix this, we use a 253 * new_red > alpha, which is a problem (for valid premul). To fix this, we use a
254 * hand-computed value of 0x3B808080, 1 ULP smaller. This keeps our colors vali d. 254 * hand-computed value of 0x3B808080, 1 ULP smaller. This keeps our colors vali d.
255 */ 255 */
256 static const float gInv255 = 0.0039215683f; // (1.0f / 255) - ULP == SkBits2Flo at(0x3B808080) 256 static const float gInv255 = 0.0039215683f; // (1.0f / 255) - ULP == SkBits2Flo at(0x3B808080)
257 257
258 static Sk4s premul(const Sk4s& x) { 258 static Sk4f premul(const Sk4f& x) {
259 float scale = SkPMFloat(x).a() * gInv255; 259 float scale = SkPMFloat(x).a() * gInv255;
260 Sk4s pm = x * Sk4s(scale, scale, scale, 1); 260 Sk4f pm = x * Sk4f(scale, scale, scale, 1);
261 261
262 #ifdef SK_DEBUG 262 #ifdef SK_DEBUG
263 SkPMFloat pmf(pm); 263 SkPMFloat pmf(pm);
264 SkASSERT(pmf.isValid()); 264 SkASSERT(pmf.isValid());
265 #endif 265 #endif
266 266
267 return pm; 267 return pm;
268 } 268 }
269 269
270 static Sk4s unpremul(const SkPMFloat& pm) { 270 static Sk4f unpremul(const SkPMFloat& pm) {
271 float scale = 255 / pm.a(); // candidate for fast/approx invert? 271 float scale = 255 / pm.a(); // candidate for fast/approx invert?
272 return Sk4s(pm) * Sk4s(scale, scale, scale, 1); 272 return Sk4f(pm) * Sk4f(scale, scale, scale, 1);
273 } 273 }
274 274
275 static Sk4f clamp_0_255(const Sk4f& value) { 275 static Sk4f clamp_0_255(const Sk4f& value) {
276 return Sk4f::Max(Sk4f::Min(value, Sk4f(255)), Sk4f(0)); 276 return Sk4f::Max(Sk4f::Min(value, Sk4f(255)), Sk4f(0));
277 } 277 }
278 278
279 void SkColorMatrixFilter::filterSpan(const SkPMColor src[], int count, SkPMColor dst[]) const { 279 void SkColorMatrixFilter::filterSpan(const SkPMColor src[], int count, SkPMColor dst[]) const {
280 Proc proc = fProc; 280 Proc proc = fProc;
281 if (NULL == proc) { 281 if (NULL == proc) {
282 if (src != dst) { 282 if (src != dst) {
283 memcpy(dst, src, count * sizeof(SkPMColor)); 283 memcpy(dst, src, count * sizeof(SkPMColor));
284 } 284 }
285 return; 285 return;
286 } 286 }
287 287
288 #ifdef SK_SUPPORT_LEGACY_INT_COLORMATRIX 288 #ifdef SK_SUPPORT_LEGACY_INT_COLORMATRIX
289 const bool use_floats = false; 289 const bool use_floats = false;
290 #else 290 #else
291 const bool use_floats = true; 291 const bool use_floats = true;
292 #endif 292 #endif
293 293
294 if (use_floats) { 294 if (use_floats) {
295 const Sk4s c0 = Sk4s::Load(fTranspose + 0); 295 const Sk4f c0 = Sk4f::Load(fTranspose + 0);
296 const Sk4s c1 = Sk4s::Load(fTranspose + 4); 296 const Sk4f c1 = Sk4f::Load(fTranspose + 4);
297 const Sk4s c2 = Sk4s::Load(fTranspose + 8); 297 const Sk4f c2 = Sk4f::Load(fTranspose + 8);
298 const Sk4s c3 = Sk4s::Load(fTranspose + 12); 298 const Sk4f c3 = Sk4f::Load(fTranspose + 12);
299 const Sk4s c4 = Sk4s::Load(fTranspose + 16); // translates 299 const Sk4f c4 = Sk4f::Load(fTranspose + 16); // translates
300 300
301 // todo: we could cache this in the constructor... 301 // todo: we could cache this in the constructor...
302 SkPMColor matrix_translate_pmcolor = SkPMFloat(premul(clamp_0_255(c4))). clamped(); 302 SkPMColor matrix_translate_pmcolor = SkPMFloat(premul(clamp_0_255(c4))). clamped();
303 303
304 for (int i = 0; i < count; i++) { 304 for (int i = 0; i < count; i++) {
305 const SkPMColor src_c = src[i]; 305 const SkPMColor src_c = src[i];
306 if (0 == src_c) { 306 if (0 == src_c) {
307 dst[i] = matrix_translate_pmcolor; 307 dst[i] = matrix_translate_pmcolor;
308 continue; 308 continue;
309 } 309 }
310 310
311 SkPMFloat srcf(src_c); 311 SkPMFloat srcf(src_c);
312 312
313 if (0xFF != SkGetPackedA32(src_c)) { 313 if (0xFF != SkGetPackedA32(src_c)) {
314 srcf = unpremul(srcf); 314 srcf = unpremul(srcf);
315 } 315 }
316 316
317 Sk4s r4 = Sk4s(srcf.r()); 317 Sk4f r4 = Sk4f(srcf.r());
318 Sk4s g4 = Sk4s(srcf.g()); 318 Sk4f g4 = Sk4f(srcf.g());
319 Sk4s b4 = Sk4s(srcf.b()); 319 Sk4f b4 = Sk4f(srcf.b());
320 Sk4s a4 = Sk4s(srcf.a()); 320 Sk4f a4 = Sk4f(srcf.a());
321 321
322 // apply matrix 322 // apply matrix
323 Sk4s dst4 = c0 * r4 + c1 * g4 + c2 * b4 + c3 * a4 + c4; 323 Sk4f dst4 = c0 * r4 + c1 * g4 + c2 * b4 + c3 * a4 + c4;
324 324
325 // clamp, re-premul, and write 325 // clamp, re-premul, and write
326 dst[i] = SkPMFloat(premul(clamp_0_255(dst4))).get(); 326 dst[i] = SkPMFloat(premul(clamp_0_255(dst4))).get();
327 } 327 }
328 } else { 328 } else {
329 const State& state = fState; 329 const State& state = fState;
330 int32_t result[4]; 330 int32_t result[4];
331 const SkUnPreMultiply::Scale* table = SkUnPreMultiply::GetScaleTable(); 331 const SkUnPreMultiply::Scale* table = SkUnPreMultiply::GetScaleTable();
332 332
333 for (int i = 0; i < count; i++) { 333 for (int i = 0; i < count; i++) {
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after
577 str->append("matrix: ("); 577 str->append("matrix: (");
578 for (int i = 0; i < 20; ++i) { 578 for (int i = 0; i < 20; ++i) {
579 str->appendScalar(fMatrix.fMat[i]); 579 str->appendScalar(fMatrix.fMat[i]);
580 if (i < 19) { 580 if (i < 19) {
581 str->append(", "); 581 str->append(", ");
582 } 582 }
583 } 583 }
584 str->append(")"); 584 str->append(")");
585 } 585 }
586 #endif 586 #endif
OLDNEW
« no previous file with comments | « src/core/SkPMFloat.h ('k') | src/opts/SkPMFloat_SSSE3.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698