aboutsummaryrefslogtreecommitdiffhomepage
path: root/bench/SkRasterPipelineBench.cpp
blob: 0243940a106e13b94d4225c283502b41239b90a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 * Copyright 2016 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "Benchmark.h"
#include "SkRasterPipeline.h"
#include "SkSRGB.h"

static const int N = 1023;

static uint32_t dst[N],
                src[N];
static uint8_t mask[N];

// We'll build up a somewhat realistic useful pipeline:
//   - load srgb src
//   - scale src by 8-bit mask
//   - load srgb dst
//   - src = srcover(dst, src)
//   - store src back as srgb
// Every stage except for srcover interacts with memory, and so will need _tail variants.

static void SK_VECTORCALL load_s_srgb(SkRasterPipeline::Stage* st, size_t x,
                                      Sk4f  r, Sk4f  g, Sk4f  b, Sk4f  a,
                                      Sk4f dr, Sk4f dg, Sk4f db, Sk4f da) {
    auto ptr = st->ctx<const uint32_t*>() + x;

    r = Sk4f{ sk_linear_from_srgb[(ptr[0] >>  0) & 0xff],
              sk_linear_from_srgb[(ptr[1] >>  0) & 0xff],
              sk_linear_from_srgb[(ptr[2] >>  0) & 0xff],
              sk_linear_from_srgb[(ptr[3] >>  0) & 0xff] };

    g = Sk4f{ sk_linear_from_srgb[(ptr[0] >>  8) & 0xff],
              sk_linear_from_srgb[(ptr[1] >>  8) & 0xff],
              sk_linear_from_srgb[(ptr[2] >>  8) & 0xff],
              sk_linear_from_srgb[(ptr[3] >>  8) & 0xff] };

    b = Sk4f{ sk_linear_from_srgb[(ptr[0] >> 16) & 0xff],
              sk_linear_from_srgb[(ptr[1] >> 16) & 0xff],
              sk_linear_from_srgb[(ptr[2] >> 16) & 0xff],
              sk_linear_from_srgb[(ptr[3] >> 16) & 0xff] };

    a = SkNx_cast<float>((Sk4i::Load(ptr) >> 24) & 0xff) * (1/255.0f);

    st->next(x, r,g,b,a, dr,dg,db,da);
}

static void SK_VECTORCALL load_s_srgb_tail(SkRasterPipeline::Stage* st, size_t x,
                                           Sk4f  r, Sk4f  g, Sk4f  b, Sk4f  a,
                                           Sk4f dr, Sk4f dg, Sk4f db, Sk4f da) {
    auto ptr = st->ctx<const uint32_t*>() + x;

    r = Sk4f{ sk_linear_from_srgb[(*ptr >>  0) & 0xff], 0,0,0 };
    g = Sk4f{ sk_linear_from_srgb[(*ptr >>  8) & 0xff], 0,0,0 };
    b = Sk4f{ sk_linear_from_srgb[(*ptr >> 16) & 0xff], 0,0,0 };
    a = Sk4f{                (*ptr >> 24) * (1/255.0f), 0,0,0 };

    st->next(x, r,g,b,a, dr,dg,db,da);
}

static void SK_VECTORCALL load_d_srgb(SkRasterPipeline::Stage* st, size_t x,
                                      Sk4f  r, Sk4f  g, Sk4f  b, Sk4f  a,
                                      Sk4f dr, Sk4f dg, Sk4f db, Sk4f da) {
    auto ptr = st->ctx<const uint32_t*>() + x;

    dr = Sk4f{ sk_linear_from_srgb[(ptr[0] >>  0) & 0xff],
               sk_linear_from_srgb[(ptr[1] >>  0) & 0xff],
               sk_linear_from_srgb[(ptr[2] >>  0) & 0xff],
               sk_linear_from_srgb[(ptr[3] >>  0) & 0xff] };

    dg = Sk4f{ sk_linear_from_srgb[(ptr[0] >>  8) & 0xff],
               sk_linear_from_srgb[(ptr[1] >>  8) & 0xff],
               sk_linear_from_srgb[(ptr[2] >>  8) & 0xff],
               sk_linear_from_srgb[(ptr[3] >>  8) & 0xff] };

    db = Sk4f{ sk_linear_from_srgb[(ptr[0] >> 16) & 0xff],
               sk_linear_from_srgb[(ptr[1] >> 16) & 0xff],
               sk_linear_from_srgb[(ptr[2] >> 16) & 0xff],
               sk_linear_from_srgb[(ptr[3] >> 16) & 0xff] };

    da = SkNx_cast<float>((Sk4i::Load(ptr) >> 24) & 0xff) * (1/255.0f);

    st->next(x, r,g,b,a, dr,dg,db,da);
}

static void SK_VECTORCALL load_d_srgb_tail(SkRasterPipeline::Stage* st, size_t x,
                                           Sk4f  r, Sk4f  g, Sk4f  b, Sk4f  a,
                                           Sk4f dr, Sk4f dg, Sk4f db, Sk4f da) {
    auto ptr = st->ctx<const uint32_t*>() + x;

    dr = Sk4f{ sk_linear_from_srgb[(*ptr >>  0) & 0xff], 0,0,0 };
    dg = Sk4f{ sk_linear_from_srgb[(*ptr >>  8) & 0xff], 0,0,0 };
    db = Sk4f{ sk_linear_from_srgb[(*ptr >> 16) & 0xff], 0,0,0 };
    da = Sk4f{                (*ptr >> 24) * (1/255.0f), 0,0,0 };

    st->next(x, r,g,b,a, dr,dg,db,da);
}

static void SK_VECTORCALL scale_u8(SkRasterPipeline::Stage* st, size_t x,
                                   Sk4f  r, Sk4f  g, Sk4f  b, Sk4f  a,
                                   Sk4f dr, Sk4f dg, Sk4f db, Sk4f da) {
    auto ptr = st->ctx<const uint8_t*>() + x;

    auto c = SkNx_cast<float>(Sk4b::Load(ptr)) * (1/255.0f);
    r *= c;
    g *= c;
    b *= c;
    a *= c;

    st->next(x, r,g,b,a, dr,dg,db,da);
}

static void SK_VECTORCALL scale_u8_tail(SkRasterPipeline::Stage* st, size_t x,
                                        Sk4f  r, Sk4f  g, Sk4f  b, Sk4f  a,
                                        Sk4f dr, Sk4f dg, Sk4f db, Sk4f da) {
    auto ptr = st->ctx<const uint8_t*>() + x;

    auto c = *ptr * (1/255.0f);
    r *= c;
    g *= c;
    b *= c;
    a *= c;

    st->next(x, r,g,b,a, dr,dg,db,da);
}

static void SK_VECTORCALL srcover(SkRasterPipeline::Stage* st, size_t x,
                                  Sk4f  r, Sk4f  g, Sk4f  b, Sk4f  a,
                                  Sk4f dr, Sk4f dg, Sk4f db, Sk4f da) {
    auto A = 1.0f - a;
    r += dr * A;
    g += dg * A;
    b += db * A;
    a += da * A;

    st->next(x, r,g,b,a, dr,dg,db,da);
}

static void SK_VECTORCALL store_srgb(SkRasterPipeline::Stage* st, size_t x,
                                     Sk4f  r, Sk4f  g, Sk4f  b, Sk4f  a,
                                     Sk4f dr, Sk4f dg, Sk4f db, Sk4f da) {
    auto ptr = st->ctx<uint32_t*>() + x;

    ( sk_linear_to_srgb(r)
    | sk_linear_to_srgb(g) << 8
    | sk_linear_to_srgb(b) << 16
    | Sk4f_round(255.0f*a) << 24).store(ptr);
}

static void SK_VECTORCALL store_srgb_tail(SkRasterPipeline::Stage* st, size_t x,
                                          Sk4f  r, Sk4f  g, Sk4f  b, Sk4f  a,
                                          Sk4f dr, Sk4f dg, Sk4f db, Sk4f da) {
    auto ptr = st->ctx<uint32_t*>() + x;

    Sk4i rgba = sk_linear_to_srgb({r[0], g[0], b[0], 0});
    rgba = {rgba[0], rgba[1], rgba[2], (int)(255.0f * a[0] + 0.5f)};

    SkNx_cast<uint8_t>(rgba).store(ptr);
}

class SkRasterPipelineBench : public Benchmark {
public:
    SkRasterPipelineBench() {}

    bool isSuitableFor(Backend backend) override { return backend == kNonRendering_Backend; }
    const char* onGetName() override { return "SkRasterPipelineBench"; }

    void onDraw(int loops, SkCanvas*) override {
        SkRasterPipeline p;
        p.append(load_s_srgb, load_s_srgb_tail,  src);
        p.append(   scale_u8,    scale_u8_tail, mask);
        p.append(load_d_srgb, load_d_srgb_tail,  dst);
        p.append(srcover);
        p.append( store_srgb,  store_srgb_tail,  dst);

        while (loops --> 0) {
            p.run(N);
        }
    }
};

DEF_BENCH( return new SkRasterPipelineBench; )