aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/SkRasterPipelineTest.cpp
blob: beb517b3f7aca5cc7c45dd86eaab1e2aef530093 (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
/*
 * 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 "Test.h"
#include "SkRasterPipeline.h"

// load needs two variants, one to load 4 values...
static void SK_VECTORCALL load(SkRasterPipeline::Stage* st, size_t x,
                               Sk4f v0, Sk4f v1, Sk4f v2, Sk4f v3,
                               Sk4f v4, Sk4f v5, Sk4f v6, Sk4f v7) {
    auto ptr = st->ctx<const float*>();
    v0 = Sk4f{ptr[x+0]};
    v1 = Sk4f{ptr[x+1]};
    v2 = Sk4f{ptr[x+2]};
    v3 = Sk4f{ptr[x+3]};

    st->next(x, v0,v1,v2,v3, v4,v5,v6,v7);
}

// ...and one to load a single value.
static void SK_VECTORCALL load_tail(SkRasterPipeline::Stage* st, size_t x,
                                    Sk4f v0, Sk4f v1, Sk4f v2, Sk4f v3,
                                    Sk4f v4, Sk4f v5, Sk4f v6, Sk4f v7) {
    auto ptr = st->ctx<const float*>();
    v0 = Sk4f{ptr[x]};

    st->next(x, v0,v1,v2,v3, v4,v5,v6,v7);
}

// square doesn't really care how many of its inputs are active, nor does it need a context.
static void SK_VECTORCALL square(SkRasterPipeline::Stage* st, size_t x,
                                 Sk4f v0, Sk4f v1, Sk4f v2, Sk4f v3,
                                 Sk4f v4, Sk4f v5, Sk4f v6, Sk4f v7) {
    v0 *= v0;
    v1 *= v1;
    v2 *= v2;
    v3 *= v3;
    st->next(x, v0,v1,v2,v3, v4,v5,v6,v7);
}

// Like load, store has a _tail variant.  It ends the pipeline by returning.
static void SK_VECTORCALL store(SkRasterPipeline::Stage* st, size_t x,
                                Sk4f v0, Sk4f v1, Sk4f v2, Sk4f v3,
                                Sk4f v4, Sk4f v5, Sk4f v6, Sk4f v7) {
    auto ptr = st->ctx<float*>();
    ptr[x+0] = v0[0];
    ptr[x+1] = v1[0];
    ptr[x+2] = v2[0];
    ptr[x+3] = v3[0];
}

static void SK_VECTORCALL store_tail(SkRasterPipeline::Stage* st, size_t x,
                                     Sk4f v0, Sk4f v1, Sk4f v2, Sk4f v3,
                                     Sk4f v4, Sk4f v5, Sk4f v6, Sk4f v7) {
    auto ptr = st->ctx<float*>();
    ptr[x+0] = v0[0];
}

DEF_TEST(SkRasterPipeline, r) {
    // We'll build up and run a simple pipeline that exercises the salient
    // mechanics of SkRasterPipeline:
    //    - context pointers
    //    - stages sensitive to the number of pixels
    //    - stages insensitive to the number of pixels
    //
    // This pipeline loads up some values, squares them, then writes them back to memory.

    const float src_vals[] = { 1,2,3,4,5 };
    float       dst_vals[] = { 0,0,0,0,0 };

    SkRasterPipeline p;
    p.append(load, load_tail, src_vals);
    p.append(square);
    p.append(store, store_tail, dst_vals);

    p.run(5);

    REPORTER_ASSERT(r, dst_vals[0] ==  1);
    REPORTER_ASSERT(r, dst_vals[1] ==  4);
    REPORTER_ASSERT(r, dst_vals[2] ==  9);
    REPORTER_ASSERT(r, dst_vals[3] == 16);
    REPORTER_ASSERT(r, dst_vals[4] == 25);
}

DEF_TEST(SkRasterPipeline_empty, r) {
    // No asserts... just a test that this is safe to run.
    SkRasterPipeline p;
    p.run(20);
}

DEF_TEST(SkRasterPipeline_nonsense, r) {
    // No asserts... just a test that this is safe to run and terminates.
    // square() always calls st->next(); this makes sure we've always got something there to call.
    SkRasterPipeline p;
    p.append(square);
    p.run(20);
}