mtklein | 281b33f | 2016-07-12 15:01:26 -0700 | [diff] [blame] | 1 | /* |
| 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... |
mtklein | fe2042e | 2016-07-29 14:27:41 -0700 | [diff] [blame] | 12 | SK_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]}; |
mtklein | 281b33f | 2016-07-12 15:01:26 -0700 | [diff] [blame] | 18 | } |
| 19 | |
| 20 | // ...and one to load a single value. |
mtklein | fe2042e | 2016-07-29 14:27:41 -0700 | [diff] [blame] | 21 | SK_RASTER_STAGE(load_tail) { |
| 22 | auto ptr = (const float*)ctx + x; |
| 23 | r = Sk4f{*ptr}; |
mtklein | 281b33f | 2016-07-12 15:01:26 -0700 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | // square doesn't really care how many of its inputs are active, nor does it need a context. |
mtklein | fe2042e | 2016-07-29 14:27:41 -0700 | [diff] [blame] | 27 | SK_RASTER_STAGE(square) { |
| 28 | r *= r; |
| 29 | g *= g; |
| 30 | b *= b; |
| 31 | a *= a; |
mtklein | 281b33f | 2016-07-12 15:01:26 -0700 | [diff] [blame] | 32 | } |
| 33 | |
mtklein | fe2042e | 2016-07-29 14:27:41 -0700 | [diff] [blame] | 34 | // Like load, store has a _tail variant. |
| 35 | SK_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]; |
mtklein | 281b33f | 2016-07-12 15:01:26 -0700 | [diff] [blame] | 41 | } |
| 42 | |
mtklein | fe2042e | 2016-07-29 14:27:41 -0700 | [diff] [blame] | 43 | SK_RASTER_STAGE(store_tail) { |
| 44 | auto ptr = (float*)ctx + x; |
| 45 | *ptr = r[0]; |
mtklein | 281b33f | 2016-07-12 15:01:26 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | DEF_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; |
mtklein | fe2042e | 2016-07-29 14:27:41 -0700 | [diff] [blame] | 61 | p.append<load, load_tail>(src_vals); |
| 62 | p.append<square>(); |
| 63 | p.append<store, store_tail>(dst_vals); |
mtklein | 281b33f | 2016-07-12 15:01:26 -0700 | [diff] [blame] | 64 | |
| 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 | } |
mtklein | 0abddf7 | 2016-07-13 08:22:20 -0700 | [diff] [blame] | 73 | |
| 74 | DEF_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 | |
| 80 | DEF_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; |
mtklein | fe2042e | 2016-07-29 14:27:41 -0700 | [diff] [blame] | 84 | p.append<square>(); |
mtklein | 0abddf7 | 2016-07-13 08:22:20 -0700 | [diff] [blame] | 85 | p.run(20); |
| 86 | } |