xref: /third_party/skia/tests/SrcOverTest.cpp (revision cb93a386)
1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2011 Google Inc.
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#include "include/private/SkColorData.h"
9cb93a386Sopenharmony_ci#include "tests/Test.h"
10cb93a386Sopenharmony_ci
11cb93a386Sopenharmony_ci// our std SkAlpha255To256
12cb93a386Sopenharmony_cistatic int test_srcover0(unsigned dst, unsigned alpha) {
13cb93a386Sopenharmony_ci    return alpha + SkAlphaMul(dst, SkAlpha255To256(255 - alpha));
14cb93a386Sopenharmony_ci}
15cb93a386Sopenharmony_ci
16cb93a386Sopenharmony_ci// faster hack +1
17cb93a386Sopenharmony_cistatic int test_srcover1(unsigned dst, unsigned alpha) {
18cb93a386Sopenharmony_ci    return alpha + SkAlphaMul(dst, 256 - alpha);
19cb93a386Sopenharmony_ci}
20cb93a386Sopenharmony_ci
21cb93a386Sopenharmony_ci// slower "correct"
22cb93a386Sopenharmony_cistatic int test_srcover2(unsigned dst, unsigned alpha) {
23cb93a386Sopenharmony_ci    return alpha + SkMulDiv255Round(dst, 255 - alpha);
24cb93a386Sopenharmony_ci}
25cb93a386Sopenharmony_ci
26cb93a386Sopenharmony_ciDEF_TEST(SrcOver, reporter) {
27cb93a386Sopenharmony_ci    /*  Here's the idea. Can we ensure that when we blend on top of an opaque
28cb93a386Sopenharmony_ci        dst, that the result always stay's opaque (i.e. exactly 255)?
29cb93a386Sopenharmony_ci     */
30cb93a386Sopenharmony_ci
31cb93a386Sopenharmony_ci    unsigned i;
32cb93a386Sopenharmony_ci    int opaqueCounter0 = 0;
33cb93a386Sopenharmony_ci    int opaqueCounter1 = 0;
34cb93a386Sopenharmony_ci    int opaqueCounter2 = 0;
35cb93a386Sopenharmony_ci    for (i = 0; i <= 255; i++) {
36cb93a386Sopenharmony_ci        unsigned result0 = test_srcover0(0xFF, i);
37cb93a386Sopenharmony_ci        unsigned result1 = test_srcover1(0xFF, i);
38cb93a386Sopenharmony_ci        unsigned result2 = test_srcover2(0xFF, i);
39cb93a386Sopenharmony_ci        opaqueCounter0 += (result0 == 0xFF);
40cb93a386Sopenharmony_ci        opaqueCounter1 += (result1 == 0xFF);
41cb93a386Sopenharmony_ci        opaqueCounter2 += (result2 == 0xFF);
42cb93a386Sopenharmony_ci    }
43cb93a386Sopenharmony_ci#if 0
44cb93a386Sopenharmony_ci    INFOF(reporter, "---- opaque test: [%d %d %d]\n",
45cb93a386Sopenharmony_ci          opaqueCounter0, opaqueCounter1, opaqueCounter2);
46cb93a386Sopenharmony_ci#endif
47cb93a386Sopenharmony_ci    // we acknowledge that technique0 does not always return opaque
48cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, opaqueCounter0 == 256);
49cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, opaqueCounter1 == 256);
50cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, opaqueCounter2 == 256);
51cb93a386Sopenharmony_ci
52cb93a386Sopenharmony_ci    // Now ensure that we never over/underflow a byte
53cb93a386Sopenharmony_ci    for (i = 0; i <= 255; i++) {
54cb93a386Sopenharmony_ci        for (unsigned dst = 0; dst <= 255; dst++) {
55cb93a386Sopenharmony_ci            unsigned r0 = test_srcover0(dst, i);
56cb93a386Sopenharmony_ci            unsigned r1 = test_srcover1(dst, i);
57cb93a386Sopenharmony_ci            unsigned r2 = test_srcover2(dst, i);
58cb93a386Sopenharmony_ci            unsigned max = std::max(dst, i);
59cb93a386Sopenharmony_ci            // ignore the known failure
60cb93a386Sopenharmony_ci            if (dst != 255) {
61cb93a386Sopenharmony_ci                REPORTER_ASSERT(reporter, r0 <= 255 && r0 >= max);
62cb93a386Sopenharmony_ci            }
63cb93a386Sopenharmony_ci            REPORTER_ASSERT(reporter, r1 <= 255 && r1 >= max);
64cb93a386Sopenharmony_ci            REPORTER_ASSERT(reporter, r2 <= 255 && r2 >= max);
65cb93a386Sopenharmony_ci
66cb93a386Sopenharmony_ci#if 0
67cb93a386Sopenharmony_ci            // this shows where r1 (faster) differs from r2 (more exact)
68cb93a386Sopenharmony_ci            if (r1 != r2) {
69cb93a386Sopenharmony_ci                INFOF(reporter, "--- dst=%d i=%d r1=%d r2=%d exact=%g\n",
70cb93a386Sopenharmony_ci                      dst, i, r1, r2, i + dst - dst*i/255.0f);
71cb93a386Sopenharmony_ci            }
72cb93a386Sopenharmony_ci#endif
73cb93a386Sopenharmony_ci        }
74cb93a386Sopenharmony_ci    }
75cb93a386Sopenharmony_ci}
76