blob: 29fe59d31bd92763b3bb2d5f5d874991a8cc7c46 [file] [log] [blame]
mtklein281b33f2016-07-12 15:01:26 -07001/*
2 * Copyright 2016 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 */
7
8#include "Test.h"
9#include "SkRasterPipeline.h"
10
11// load needs two variants, one to load 4 values...
mtkleinfe2042e2016-07-29 14:27:41 -070012SK_RASTER_STAGE(load) {
13 auto ptr = (const float*)ctx + x;
14 r = Sk4f{ptr[0]};
15 g = Sk4f{ptr[1]};
16 b = Sk4f{ptr[2]};
17 a = Sk4f{ptr[3]};
mtklein281b33f2016-07-12 15:01:26 -070018}
19
20// ...and one to load a single value.
mtkleinfe2042e2016-07-29 14:27:41 -070021SK_RASTER_STAGE(load_tail) {
22 auto ptr = (const float*)ctx + x;
23 r = Sk4f{*ptr};
mtklein281b33f2016-07-12 15:01:26 -070024}
25
26// square doesn't really care how many of its inputs are active, nor does it need a context.
mtkleinfe2042e2016-07-29 14:27:41 -070027SK_RASTER_STAGE(square) {
28 r *= r;
29 g *= g;
30 b *= b;
31 a *= a;
mtklein281b33f2016-07-12 15:01:26 -070032}
33
mtkleinfe2042e2016-07-29 14:27:41 -070034// Like load, store has a _tail variant.
35SK_RASTER_STAGE(store) {
36 auto ptr = (float*)ctx + x;
37 ptr[0] = r[0];
38 ptr[1] = g[0];
39 ptr[2] = b[0];
40 ptr[3] = a[0];
mtklein281b33f2016-07-12 15:01:26 -070041}
42
mtkleinfe2042e2016-07-29 14:27:41 -070043SK_RASTER_STAGE(store_tail) {
44 auto ptr = (float*)ctx + x;
45 *ptr = r[0];
mtklein281b33f2016-07-12 15:01:26 -070046}
47
48DEF_TEST(SkRasterPipeline, r) {
49 // We'll build up and run a simple pipeline that exercises the salient
50 // mechanics of SkRasterPipeline:
51 // - context pointers
52 // - stages sensitive to the number of pixels
53 // - stages insensitive to the number of pixels
54 //
55 // This pipeline loads up some values, squares them, then writes them back to memory.
56
57 const float src_vals[] = { 1,2,3,4,5 };
58 float dst_vals[] = { 0,0,0,0,0 };
59
60 SkRasterPipeline p;
mtkleinfe2042e2016-07-29 14:27:41 -070061 p.append<load, load_tail>(src_vals);
62 p.append<square>();
63 p.append<store, store_tail>(dst_vals);
mtklein281b33f2016-07-12 15:01:26 -070064
65 p.run(5);
66
67 REPORTER_ASSERT(r, dst_vals[0] == 1);
68 REPORTER_ASSERT(r, dst_vals[1] == 4);
69 REPORTER_ASSERT(r, dst_vals[2] == 9);
70 REPORTER_ASSERT(r, dst_vals[3] == 16);
71 REPORTER_ASSERT(r, dst_vals[4] == 25);
72}
mtklein0abddf72016-07-13 08:22:20 -070073
74DEF_TEST(SkRasterPipeline_empty, r) {
75 // No asserts... just a test that this is safe to run.
76 SkRasterPipeline p;
77 p.run(20);
78}
79
80DEF_TEST(SkRasterPipeline_nonsense, r) {
81 // No asserts... just a test that this is safe to run and terminates.
82 // square() always calls st->next(); this makes sure we've always got something there to call.
83 SkRasterPipeline p;
mtkleinfe2042e2016-07-29 14:27:41 -070084 p.append<square>();
mtklein0abddf72016-07-13 08:22:20 -070085 p.run(20);
86}