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 | |
mtklein | fe2042e | 2016-07-29 14:27:41 -0700 | [diff] [blame] | 11 | SK_RASTER_STAGE(load) { |
| 12 | auto ptr = (const float*)ctx + x; |
Mike Klein | c8dd6bc | 2016-09-28 10:43:53 -0400 | [diff] [blame] | 13 | switch(tail&3) { |
| 14 | case 0: a = Sk4f{ptr[3]}; |
| 15 | case 3: b = Sk4f{ptr[2]}; |
| 16 | case 2: g = Sk4f{ptr[1]}; |
| 17 | case 1: r = Sk4f{ptr[0]}; |
| 18 | } |
mtklein | 281b33f | 2016-07-12 15:01:26 -0700 | [diff] [blame] | 19 | } |
| 20 | |
mtklein | fe2042e | 2016-07-29 14:27:41 -0700 | [diff] [blame] | 21 | SK_RASTER_STAGE(square) { |
| 22 | r *= r; |
| 23 | g *= g; |
| 24 | b *= b; |
| 25 | a *= a; |
mtklein | 281b33f | 2016-07-12 15:01:26 -0700 | [diff] [blame] | 26 | } |
| 27 | |
mtklein | fe2042e | 2016-07-29 14:27:41 -0700 | [diff] [blame] | 28 | SK_RASTER_STAGE(store) { |
| 29 | auto ptr = (float*)ctx + x; |
Mike Klein | c8dd6bc | 2016-09-28 10:43:53 -0400 | [diff] [blame] | 30 | switch (tail&3) { |
| 31 | case 0: ptr[3] = a[0]; |
| 32 | case 3: ptr[2] = b[0]; |
| 33 | case 2: ptr[1] = g[0]; |
| 34 | case 1: ptr[0] = r[0]; |
| 35 | } |
mtklein | 281b33f | 2016-07-12 15:01:26 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | DEF_TEST(SkRasterPipeline, r) { |
| 39 | // We'll build up and run a simple pipeline that exercises the salient |
| 40 | // mechanics of SkRasterPipeline: |
Mike Klein | c8dd6bc | 2016-09-28 10:43:53 -0400 | [diff] [blame] | 41 | // - context pointers (load,store) |
| 42 | // - stages sensitive to the number of pixels (load,store) |
| 43 | // - stages insensitive to the number of pixels (square) |
mtklein | 281b33f | 2016-07-12 15:01:26 -0700 | [diff] [blame] | 44 | // |
| 45 | // This pipeline loads up some values, squares them, then writes them back to memory. |
| 46 | |
| 47 | const float src_vals[] = { 1,2,3,4,5 }; |
| 48 | float dst_vals[] = { 0,0,0,0,0 }; |
| 49 | |
| 50 | SkRasterPipeline p; |
Mike Klein | c8dd6bc | 2016-09-28 10:43:53 -0400 | [diff] [blame] | 51 | p.append<load>(src_vals); |
mtklein | fe2042e | 2016-07-29 14:27:41 -0700 | [diff] [blame] | 52 | p.append<square>(); |
Mike Klein | c8dd6bc | 2016-09-28 10:43:53 -0400 | [diff] [blame] | 53 | p.append<store>(dst_vals); |
mtklein | 281b33f | 2016-07-12 15:01:26 -0700 | [diff] [blame] | 54 | |
| 55 | p.run(5); |
| 56 | |
| 57 | REPORTER_ASSERT(r, dst_vals[0] == 1); |
| 58 | REPORTER_ASSERT(r, dst_vals[1] == 4); |
| 59 | REPORTER_ASSERT(r, dst_vals[2] == 9); |
| 60 | REPORTER_ASSERT(r, dst_vals[3] == 16); |
| 61 | REPORTER_ASSERT(r, dst_vals[4] == 25); |
| 62 | } |
mtklein | 0abddf7 | 2016-07-13 08:22:20 -0700 | [diff] [blame] | 63 | |
| 64 | DEF_TEST(SkRasterPipeline_empty, r) { |
| 65 | // No asserts... just a test that this is safe to run. |
| 66 | SkRasterPipeline p; |
| 67 | p.run(20); |
| 68 | } |
| 69 | |
| 70 | DEF_TEST(SkRasterPipeline_nonsense, r) { |
| 71 | // No asserts... just a test that this is safe to run and terminates. |
| 72 | // square() always calls st->next(); this makes sure we've always got something there to call. |
| 73 | SkRasterPipeline p; |
mtklein | fe2042e | 2016-07-29 14:27:41 -0700 | [diff] [blame] | 74 | p.append<square>(); |
mtklein | 0abddf7 | 2016-07-13 08:22:20 -0700 | [diff] [blame] | 75 | p.run(20); |
| 76 | } |