epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [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 | */ |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 7 | |
reed@android.com | d66efc2 | 2009-03-03 18:35:18 +0000 | [diff] [blame] | 8 | #include "SkColorPriv.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 9 | #include "Test.h" |
reed@android.com | d66efc2 | 2009-03-03 18:35:18 +0000 | [diff] [blame] | 10 | |
| 11 | // our std SkAlpha255To256 |
| 12 | static int test_srcover0(unsigned dst, unsigned alpha) { |
| 13 | return alpha + SkAlphaMul(dst, SkAlpha255To256(255 - alpha)); |
| 14 | } |
| 15 | |
| 16 | // faster hack +1 |
| 17 | static int test_srcover1(unsigned dst, unsigned alpha) { |
| 18 | return alpha + SkAlphaMul(dst, 256 - alpha); |
| 19 | } |
| 20 | |
| 21 | // slower "correct" |
| 22 | static int test_srcover2(unsigned dst, unsigned alpha) { |
| 23 | return alpha + SkMulDiv255Round(dst, 255 - alpha); |
| 24 | } |
| 25 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 26 | DEF_TEST(SrcOver, reporter) { |
reed@android.com | d66efc2 | 2009-03-03 18:35:18 +0000 | [diff] [blame] | 27 | /* Here's the idea. Can we ensure that when we blend on top of an opaque |
| 28 | dst, that the result always stay's opaque (i.e. exactly 255)? |
| 29 | */ |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 30 | |
reed@android.com | d66efc2 | 2009-03-03 18:35:18 +0000 | [diff] [blame] | 31 | unsigned i; |
| 32 | int opaqueCounter0 = 0; |
| 33 | int opaqueCounter1 = 0; |
| 34 | int opaqueCounter2 = 0; |
| 35 | for (i = 0; i <= 255; i++) { |
| 36 | unsigned result0 = test_srcover0(0xFF, i); |
| 37 | unsigned result1 = test_srcover1(0xFF, i); |
| 38 | unsigned result2 = test_srcover2(0xFF, i); |
| 39 | opaqueCounter0 += (result0 == 0xFF); |
| 40 | opaqueCounter1 += (result1 == 0xFF); |
| 41 | opaqueCounter2 += (result2 == 0xFF); |
| 42 | } |
| 43 | #if 0 |
halcanary | 7d57124 | 2016-02-24 17:59:16 -0800 | [diff] [blame] | 44 | INFOF(reporter, "---- opaque test: [%d %d %d]\n", |
| 45 | opaqueCounter0, opaqueCounter1, opaqueCounter2); |
reed@android.com | d66efc2 | 2009-03-03 18:35:18 +0000 | [diff] [blame] | 46 | #endif |
| 47 | // we acknowledge that technique0 does not always return opaque |
reed@android.com | 9781ca5 | 2009-04-14 14:28:22 +0000 | [diff] [blame] | 48 | REPORTER_ASSERT(reporter, opaqueCounter0 == 256); |
reed@android.com | d66efc2 | 2009-03-03 18:35:18 +0000 | [diff] [blame] | 49 | REPORTER_ASSERT(reporter, opaqueCounter1 == 256); |
| 50 | REPORTER_ASSERT(reporter, opaqueCounter2 == 256); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 51 | |
reed@android.com | d66efc2 | 2009-03-03 18:35:18 +0000 | [diff] [blame] | 52 | // Now ensure that we never over/underflow a byte |
| 53 | for (i = 0; i <= 255; i++) { |
| 54 | for (unsigned dst = 0; dst <= 255; dst++) { |
| 55 | unsigned r0 = test_srcover0(dst, i); |
| 56 | unsigned r1 = test_srcover1(dst, i); |
| 57 | unsigned r2 = test_srcover2(dst, i); |
| 58 | unsigned max = SkMax32(dst, i); |
| 59 | // ignore the known failure |
| 60 | if (dst != 255) { |
| 61 | REPORTER_ASSERT(reporter, r0 <= 255 && r0 >= max); |
| 62 | } |
| 63 | REPORTER_ASSERT(reporter, r1 <= 255 && r1 >= max); |
| 64 | REPORTER_ASSERT(reporter, r2 <= 255 && r2 >= max); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 65 | |
reed@android.com | d66efc2 | 2009-03-03 18:35:18 +0000 | [diff] [blame] | 66 | #if 0 |
| 67 | // this shows where r1 (faster) differs from r2 (more exact) |
| 68 | if (r1 != r2) { |
halcanary | 7d57124 | 2016-02-24 17:59:16 -0800 | [diff] [blame] | 69 | INFOF(reporter, "--- dst=%d i=%d r1=%d r2=%d exact=%g\n", |
| 70 | dst, i, r1, r2, i + dst - dst*i/255.0f); |
reed@android.com | d66efc2 | 2009-03-03 18:35:18 +0000 | [diff] [blame] | 71 | } |
| 72 | #endif |
| 73 | } |
| 74 | } |
| 75 | } |