OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "Benchmark.h" | 8 #include "Benchmark.h" |
9 #include "SkColor.h" | 9 #include "SkColor.h" |
10 #include "SkNx.h" | 10 #include "SkNx.h" |
11 | 11 |
12 // Used to prevent the compiler from optimizing away the whole loop. | 12 // Used to prevent the compiler from optimizing away the whole loop. |
13 volatile uint32_t blackhole = 0; | 13 volatile uint32_t blackhole = 0; |
14 | 14 |
15 // Not a great random number generator, but it's very fast. | 15 // Not a great random number generator, but it's very fast. |
16 // The code we're measuring is quite fast, so low overhead is essential. | 16 // The code we're measuring is quite fast, so low overhead is essential. |
17 static uint32_t lcg_rand(uint32_t* seed) { | 17 static uint32_t lcg_rand(uint32_t* seed) { |
18 *seed *= 1664525; | 18 *seed *= 1664525; |
19 *seed += 1013904223; | 19 *seed += 1013904223; |
20 return *seed; | 20 return *seed; |
21 } | 21 } |
22 | 22 |
23 struct Sk4fBytesRoundtripBench : public Benchmark { | 23 struct Sk4fBytesRoundtripBench : public Benchmark { |
24 Sk4fBytesRoundtripBench() {} | 24 Sk4fBytesRoundtripBench() {} |
25 | 25 |
26 const char* onGetName() override { return "Sk4f_roundtrip"; } | 26 const char* onGetName() override { return "Sk4f_roundtrip"; } |
27 bool isSuitableFor(Backend backend) override { return backend == kNonRenderi
ng_Backend; } | 27 bool isSuitableFor(Backend backend) override { return backend == kNonRenderi
ng_Backend; } |
28 | 28 |
29 void onDraw(const int loops, SkCanvas* canvas) override { | 29 void onDraw(int loops, SkCanvas* canvas) override { |
30 // Unlike blackhole, junk can and probably will be a register. | 30 // Unlike blackhole, junk can and probably will be a register. |
31 uint32_t junk = 0; | 31 uint32_t junk = 0; |
32 uint32_t seed = 0; | 32 uint32_t seed = 0; |
33 for (int i = 0; i < loops; i++) { | 33 for (int i = 0; i < loops; i++) { |
34 uint32_t color = lcg_rand(&seed), | 34 uint32_t color = lcg_rand(&seed), |
35 back; | 35 back; |
36 auto f = Sk4f::FromBytes((const uint8_t*)&color); | 36 auto f = Sk4f::FromBytes((const uint8_t*)&color); |
37 f.toBytes((uint8_t*)&back); | 37 f.toBytes((uint8_t*)&back); |
38 junk ^= back; | 38 junk ^= back; |
39 } | 39 } |
40 blackhole ^= junk; | 40 blackhole ^= junk; |
41 } | 41 } |
42 }; | 42 }; |
43 DEF_BENCH(return new Sk4fBytesRoundtripBench;) | 43 DEF_BENCH(return new Sk4fBytesRoundtripBench;) |
44 | 44 |
45 struct Sk4fGradientBench : public Benchmark { | 45 struct Sk4fGradientBench : public Benchmark { |
46 const char* onGetName() override { return "Sk4f_gradient"; } | 46 const char* onGetName() override { return "Sk4f_gradient"; } |
47 bool isSuitableFor(Backend backend) override { return backend == kNonRenderi
ng_Backend; } | 47 bool isSuitableFor(Backend backend) override { return backend == kNonRenderi
ng_Backend; } |
48 | 48 |
49 SkPMColor fDevice[100]; | 49 SkPMColor fDevice[100]; |
50 void onDraw(const int loops, SkCanvas*) override { | 50 void onDraw(int loops, SkCanvas*) override { |
51 Sk4f c0(0,0,255,255), | 51 Sk4f c0(0,0,255,255), |
52 c1(255,0,0,255), | 52 c1(255,0,0,255), |
53 dc = c1 - c0, | 53 dc = c1 - c0, |
54 fx(0.1f), | 54 fx(0.1f), |
55 dx(0.002f), | 55 dx(0.002f), |
56 dcdx(dc*dx), | 56 dcdx(dc*dx), |
57 dcdx4(dcdx+dcdx+dcdx+dcdx); | 57 dcdx4(dcdx+dcdx+dcdx+dcdx); |
58 | 58 |
59 for (int n = 0; n < loops; n++) { | 59 for (int n = 0; n < loops; n++) { |
60 Sk4f a = c0 + dc*fx + Sk4f(0.5f), // add an extra 0.5f to get round
ing for free. | 60 Sk4f a = c0 + dc*fx + Sk4f(0.5f), // add an extra 0.5f to get round
ing for free. |
61 b = a + dcdx, | 61 b = a + dcdx, |
62 c = b + dcdx, | 62 c = b + dcdx, |
63 d = c + dcdx; | 63 d = c + dcdx; |
64 for (size_t i = 0; i < SK_ARRAY_COUNT(fDevice); i += 4) { | 64 for (size_t i = 0; i < SK_ARRAY_COUNT(fDevice); i += 4) { |
65 a.toBytes((uint8_t*)(fDevice+i+0)); | 65 a.toBytes((uint8_t*)(fDevice+i+0)); |
66 b.toBytes((uint8_t*)(fDevice+i+1)); | 66 b.toBytes((uint8_t*)(fDevice+i+1)); |
67 c.toBytes((uint8_t*)(fDevice+i+2)); | 67 c.toBytes((uint8_t*)(fDevice+i+2)); |
68 d.toBytes((uint8_t*)(fDevice+i+3)); | 68 d.toBytes((uint8_t*)(fDevice+i+3)); |
69 a = a + dcdx4; | 69 a = a + dcdx4; |
70 b = b + dcdx4; | 70 b = b + dcdx4; |
71 c = c + dcdx4; | 71 c = c + dcdx4; |
72 d = d + dcdx4; | 72 d = d + dcdx4; |
73 } | 73 } |
74 } | 74 } |
75 } | 75 } |
76 }; | 76 }; |
77 DEF_BENCH(return new Sk4fGradientBench;) | 77 DEF_BENCH(return new Sk4fGradientBench;) |
OLD | NEW |