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 | |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 8 | #include "../src/jumper/SkJumper.h" |
Mike Klein | 9161ef0 | 2016-10-04 14:03:27 -0400 | [diff] [blame] | 9 | #include "SkHalf.h" |
mtklein | 281b33f | 2016-07-12 15:01:26 -0700 | [diff] [blame] | 10 | #include "SkRasterPipeline.h" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 11 | #include "SkTo.h" |
| 12 | #include "Test.h" |
mtklein | 281b33f | 2016-07-12 15:01:26 -0700 | [diff] [blame] | 13 | |
mtklein | 281b33f | 2016-07-12 15:01:26 -0700 | [diff] [blame] | 14 | DEF_TEST(SkRasterPipeline, r) { |
Mike Klein | 9161ef0 | 2016-10-04 14:03:27 -0400 | [diff] [blame] | 15 | // Build and run a simple pipeline to exercise SkRasterPipeline, |
| 16 | // drawing 50% transparent blue over opaque red in half-floats. |
Mike Klein | 26bea5d | 2016-10-05 10:36:38 -0400 | [diff] [blame] | 17 | uint64_t red = 0x3c00000000003c00ull, |
| 18 | blue = 0x3800380000000000ull, |
| 19 | result; |
Mike Klein | d0ccb57 | 2016-10-05 09:36:26 -0400 | [diff] [blame] | 20 | |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 21 | SkJumper_MemoryCtx load_s_ctx = { &blue, 0 }, |
| 22 | load_d_ctx = { &red, 0 }, |
| 23 | store_ctx = { &result, 0 }; |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 24 | |
Mike Klein | b24704d | 2017-05-24 07:53:00 -0400 | [diff] [blame] | 25 | SkRasterPipeline_<256> p; |
Mike Klein | 9f2b3d1 | 2017-06-27 17:26:50 -0400 | [diff] [blame] | 26 | p.append(SkRasterPipeline::load_f16, &load_s_ctx); |
| 27 | p.append(SkRasterPipeline::load_f16_dst, &load_d_ctx); |
Mike Klein | 9161ef0 | 2016-10-04 14:03:27 -0400 | [diff] [blame] | 28 | p.append(SkRasterPipeline::srcover); |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 29 | p.append(SkRasterPipeline::store_f16, &store_ctx); |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 30 | p.run(0,0,1,1); |
mtklein | 281b33f | 2016-07-12 15:01:26 -0700 | [diff] [blame] | 31 | |
Mike Klein | 9161ef0 | 2016-10-04 14:03:27 -0400 | [diff] [blame] | 32 | // We should see half-intensity magenta. |
Mike Klein | 26bea5d | 2016-10-05 10:36:38 -0400 | [diff] [blame] | 33 | REPORTER_ASSERT(r, ((result >> 0) & 0xffff) == 0x3800); |
| 34 | REPORTER_ASSERT(r, ((result >> 16) & 0xffff) == 0x0000); |
| 35 | REPORTER_ASSERT(r, ((result >> 32) & 0xffff) == 0x3800); |
| 36 | REPORTER_ASSERT(r, ((result >> 48) & 0xffff) == 0x3c00); |
mtklein | 281b33f | 2016-07-12 15:01:26 -0700 | [diff] [blame] | 37 | } |
mtklein | 0abddf7 | 2016-07-13 08:22:20 -0700 | [diff] [blame] | 38 | |
| 39 | DEF_TEST(SkRasterPipeline_empty, r) { |
| 40 | // No asserts... just a test that this is safe to run. |
Mike Klein | b24704d | 2017-05-24 07:53:00 -0400 | [diff] [blame] | 41 | SkRasterPipeline_<256> p; |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 42 | p.run(0,0,20,1); |
mtklein | 0abddf7 | 2016-07-13 08:22:20 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | DEF_TEST(SkRasterPipeline_nonsense, r) { |
| 46 | // No asserts... just a test that this is safe to run and terminates. |
Mike Klein | 9161ef0 | 2016-10-04 14:03:27 -0400 | [diff] [blame] | 47 | // srcover() calls st->next(); this makes sure we've always got something there to call. |
Mike Klein | b24704d | 2017-05-24 07:53:00 -0400 | [diff] [blame] | 48 | SkRasterPipeline_<256> p; |
Mike Klein | 9161ef0 | 2016-10-04 14:03:27 -0400 | [diff] [blame] | 49 | p.append(SkRasterPipeline::srcover); |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 50 | p.run(0,0,20,1); |
mtklein | 0abddf7 | 2016-07-13 08:22:20 -0700 | [diff] [blame] | 51 | } |
Mike Klein | 7ba89a1 | 2017-01-10 13:42:51 -0500 | [diff] [blame] | 52 | |
| 53 | DEF_TEST(SkRasterPipeline_JIT, r) { |
| 54 | // This tests a couple odd corners that a JIT backend can stumble over. |
| 55 | |
| 56 | uint32_t buf[72] = { |
| 57 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 58 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, |
| 59 | 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, |
| 60 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 61 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 62 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 63 | }; |
| 64 | |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 65 | SkJumper_MemoryCtx src = { buf + 0, 0 }, |
| 66 | dst = { buf + 36, 0 }; |
Mike Klein | 7ba89a1 | 2017-01-10 13:42:51 -0500 | [diff] [blame] | 67 | |
| 68 | // Copy buf[x] to buf[x+36] for x in [15,35). |
Mike Klein | b24704d | 2017-05-24 07:53:00 -0400 | [diff] [blame] | 69 | SkRasterPipeline_<256> p; |
Mike Klein | 7ba89a1 | 2017-01-10 13:42:51 -0500 | [diff] [blame] | 70 | p.append(SkRasterPipeline:: load_8888, &src); |
| 71 | p.append(SkRasterPipeline::store_8888, &dst); |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 72 | p.run(15,0, 20,1); |
Mike Klein | 7ba89a1 | 2017-01-10 13:42:51 -0500 | [diff] [blame] | 73 | |
| 74 | for (int i = 0; i < 36; i++) { |
| 75 | if (i < 15 || i == 35) { |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 76 | REPORTER_ASSERT(r, buf[i+36] == 0); |
Mike Klein | 7ba89a1 | 2017-01-10 13:42:51 -0500 | [diff] [blame] | 77 | } else { |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 78 | REPORTER_ASSERT(r, buf[i+36] == (uint32_t)(i - 11)); |
Mike Klein | 7ba89a1 | 2017-01-10 13:42:51 -0500 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | } |
Herb Derby | d1f0830 | 2017-05-26 15:42:28 -0400 | [diff] [blame] | 82 | |
| 83 | static uint16_t h(float f) { |
| 84 | // Remember, a float is 1-8-23 (sign-exponent-mantissa) with 127 exponent bias. |
| 85 | uint32_t sem; |
| 86 | memcpy(&sem, &f, sizeof(sem)); |
| 87 | uint32_t s = sem & 0x80000000, |
| 88 | em = sem ^ s; |
| 89 | |
| 90 | // Convert to 1-5-10 half with 15 bias, flushing denorm halfs (including zero) to zero. |
| 91 | auto denorm = (int32_t)em < 0x38800000; // I32 comparison is often quicker, and always safe |
| 92 | // here. |
| 93 | return denorm ? SkTo<uint16_t>(0) |
| 94 | : SkTo<uint16_t>((s>>16) + (em>>13) - ((127-15)<<10)); |
| 95 | } |
| 96 | |
Herb Derby | d1f0830 | 2017-05-26 15:42:28 -0400 | [diff] [blame] | 97 | DEF_TEST(SkRasterPipeline_tail, r) { |
| 98 | { |
| 99 | float data[][4] = { |
| 100 | {00, 01, 02, 03}, |
| 101 | {10, 11, 12, 13}, |
| 102 | {20, 21, 22, 23}, |
| 103 | {30, 31, 32, 33}, |
| 104 | }; |
| 105 | |
| 106 | float buffer[4][4]; |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 107 | |
| 108 | SkJumper_MemoryCtx src = { &data[0][0], 0 }, |
| 109 | dst = { &buffer[0][0], 0 }; |
Herb Derby | d1f0830 | 2017-05-26 15:42:28 -0400 | [diff] [blame] | 110 | |
Herb Derby | 84dcac3 | 2017-05-30 16:56:32 -0400 | [diff] [blame] | 111 | for (unsigned i = 1; i <= 4; i++) { |
Herb Derby | d1f0830 | 2017-05-26 15:42:28 -0400 | [diff] [blame] | 112 | memset(buffer, 0xff, sizeof(buffer)); |
| 113 | SkRasterPipeline_<256> p; |
| 114 | p.append(SkRasterPipeline::load_f32, &src); |
| 115 | p.append(SkRasterPipeline::store_f32, &dst); |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 116 | p.run(0,0, i,1); |
Herb Derby | d1f0830 | 2017-05-26 15:42:28 -0400 | [diff] [blame] | 117 | for (unsigned j = 0; j < i; j++) { |
Herb Derby | 84dcac3 | 2017-05-30 16:56:32 -0400 | [diff] [blame] | 118 | for (unsigned k = 0; k < 4; k++) { |
| 119 | if (buffer[j][k] != data[j][k]) { |
| 120 | ERRORF(r, "(%u, %u) - a: %g r: %g\n", j, k, data[j][k], buffer[j][k]); |
| 121 | } |
| 122 | } |
Herb Derby | d1f0830 | 2017-05-26 15:42:28 -0400 | [diff] [blame] | 123 | } |
| 124 | for (int j = i; j < 4; j++) { |
| 125 | for (auto f : buffer[j]) { |
| 126 | REPORTER_ASSERT(r, SkScalarIsNaN(f)); |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | { |
Mike Klein | 0bf8926 | 2018-02-26 13:34:21 -0500 | [diff] [blame] | 133 | alignas(8) uint16_t data[][4] = { |
Herb Derby | d1f0830 | 2017-05-26 15:42:28 -0400 | [diff] [blame] | 134 | {h(00), h(01), h(02), h(03)}, |
| 135 | {h(10), h(11), h(12), h(13)}, |
| 136 | {h(20), h(21), h(22), h(23)}, |
| 137 | {h(30), h(31), h(32), h(33)}, |
| 138 | }; |
Mike Klein | 0bf8926 | 2018-02-26 13:34:21 -0500 | [diff] [blame] | 139 | alignas(8) uint16_t buffer[4][4]; |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 140 | SkJumper_MemoryCtx src = { &data[0][0], 0 }, |
| 141 | dst = { &buffer[0][0], 0 }; |
Herb Derby | d1f0830 | 2017-05-26 15:42:28 -0400 | [diff] [blame] | 142 | |
Herb Derby | 84dcac3 | 2017-05-30 16:56:32 -0400 | [diff] [blame] | 143 | for (unsigned i = 1; i <= 4; i++) { |
Herb Derby | d1f0830 | 2017-05-26 15:42:28 -0400 | [diff] [blame] | 144 | memset(buffer, 0xff, sizeof(buffer)); |
| 145 | SkRasterPipeline_<256> p; |
| 146 | p.append(SkRasterPipeline::load_f16, &src); |
| 147 | p.append(SkRasterPipeline::store_f16, &dst); |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 148 | p.run(0,0, i,1); |
Herb Derby | d1f0830 | 2017-05-26 15:42:28 -0400 | [diff] [blame] | 149 | for (unsigned j = 0; j < i; j++) { |
| 150 | REPORTER_ASSERT(r, |
| 151 | !memcmp(&data[j][0], &buffer[j][0], sizeof(buffer[j]))); |
| 152 | } |
| 153 | for (int j = i; j < 4; j++) { |
| 154 | for (auto f : buffer[j]) { |
| 155 | REPORTER_ASSERT(r, f == 0xffff); |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | } |
Herb Derby | d1f0830 | 2017-05-26 15:42:28 -0400 | [diff] [blame] | 160 | } |
Mike Klein | bba02c2 | 2017-06-02 10:03:30 -0400 | [diff] [blame] | 161 | |
| 162 | DEF_TEST(SkRasterPipeline_lowp, r) { |
| 163 | uint32_t rgba[64]; |
| 164 | for (int i = 0; i < 64; i++) { |
| 165 | rgba[i] = (4*i+0) << 0 |
| 166 | | (4*i+1) << 8 |
| 167 | | (4*i+2) << 16 |
| 168 | | (4*i+3) << 24; |
| 169 | } |
| 170 | |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 171 | SkJumper_MemoryCtx ptr = { rgba, 0 }; |
Mike Klein | bba02c2 | 2017-06-02 10:03:30 -0400 | [diff] [blame] | 172 | |
| 173 | SkRasterPipeline_<256> p; |
Mike Klein | c2d2076 | 2017-06-27 19:53:21 -0400 | [diff] [blame] | 174 | p.append(SkRasterPipeline::load_bgra, &ptr); |
Mike Klein | bba02c2 | 2017-06-02 10:03:30 -0400 | [diff] [blame] | 175 | p.append(SkRasterPipeline::store_8888, &ptr); |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 176 | p.run(0,0,64,1); |
Mike Klein | bba02c2 | 2017-06-02 10:03:30 -0400 | [diff] [blame] | 177 | |
| 178 | for (int i = 0; i < 64; i++) { |
| 179 | uint32_t want = (4*i+0) << 16 |
| 180 | | (4*i+1) << 8 |
| 181 | | (4*i+2) << 0 |
| 182 | | (4*i+3) << 24; |
| 183 | if (rgba[i] != want) { |
| 184 | ERRORF(r, "got %08x, want %08x\n", rgba[i], want); |
| 185 | } |
| 186 | } |
| 187 | } |
Mike Klein | ea045b5 | 2018-08-23 12:13:58 -0400 | [diff] [blame] | 188 | |
| 189 | DEF_TEST(SkRasterPipeline_lowp_clamp01, r) { |
| 190 | // This may seem like a funny pipeline to create, |
| 191 | // but it certainly shouldn't crash when you run it. |
| 192 | |
| 193 | uint32_t rgba = 0xff00ff00; |
| 194 | |
| 195 | SkJumper_MemoryCtx ptr = { &rgba, 0 }; |
| 196 | |
| 197 | SkRasterPipeline_<256> p; |
| 198 | p.append(SkRasterPipeline::load_bgra, &ptr); |
| 199 | p.append(SkRasterPipeline::clamp_0); |
| 200 | p.append(SkRasterPipeline::clamp_1); |
| 201 | p.append(SkRasterPipeline::store_8888, &ptr); |
| 202 | p.run(0,0,1,1); |
| 203 | } |