commit-bot@chromium.org | c4e416c | 2014-05-20 14:54:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 8 | #include "Benchmark.h" |
commit-bot@chromium.org | c4e416c | 2014-05-20 14:54:04 +0000 | [diff] [blame] | 9 | #include "SkRandom.h" |
| 10 | #include "SkTemplates.h" |
commit-bot@chromium.org | dcba993 | 2014-05-28 22:47:26 +0000 | [diff] [blame] | 11 | #include "SkUtils.h" |
commit-bot@chromium.org | c4e416c | 2014-05-20 14:54:04 +0000 | [diff] [blame] | 12 | |
| 13 | template <typename Memcpy32> |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 14 | class Memcpy32Bench : public Benchmark { |
commit-bot@chromium.org | c4e416c | 2014-05-20 14:54:04 +0000 | [diff] [blame] | 15 | public: |
| 16 | explicit Memcpy32Bench(int count, Memcpy32 memcpy32, const char* name) |
| 17 | : fCount(count) |
| 18 | , fMemcpy32(memcpy32) |
| 19 | , fName(SkStringPrintf("%s_%d", name, count)) {} |
| 20 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 21 | const char* onGetName() SK_OVERRIDE { |
commit-bot@chromium.org | c4e416c | 2014-05-20 14:54:04 +0000 | [diff] [blame] | 22 | return fName.c_str(); |
| 23 | } |
| 24 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 25 | bool isSuitableFor(Backend backend) SK_OVERRIDE { |
commit-bot@chromium.org | c4e416c | 2014-05-20 14:54:04 +0000 | [diff] [blame] | 26 | return backend == kNonRendering_Backend; |
| 27 | } |
| 28 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 29 | void onPreDraw() SK_OVERRIDE { |
commit-bot@chromium.org | c4e416c | 2014-05-20 14:54:04 +0000 | [diff] [blame] | 30 | fDst.reset(fCount); |
| 31 | fSrc.reset(fCount); |
| 32 | |
| 33 | SkRandom rand; |
| 34 | for (int i = 0; i < fCount; i++) { |
| 35 | fSrc[i] = rand.nextU(); |
| 36 | } |
| 37 | } |
| 38 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 39 | void onDraw(const int loops, SkCanvas*) SK_OVERRIDE { |
commit-bot@chromium.org | c4e416c | 2014-05-20 14:54:04 +0000 | [diff] [blame] | 40 | for (int i = 0; i < loops; i++) { |
| 41 | fMemcpy32(fDst, fSrc, fCount); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | private: |
| 46 | SkAutoTMalloc<uint32_t> fDst, fSrc; |
| 47 | |
| 48 | int fCount; |
| 49 | Memcpy32 fMemcpy32; |
| 50 | const SkString fName; |
| 51 | }; |
| 52 | |
| 53 | template <typename Memcpy32> |
| 54 | static Memcpy32Bench<Memcpy32>* Bench(int count, Memcpy32 memcpy32, const char* name) { |
| 55 | return new Memcpy32Bench<Memcpy32>(count, memcpy32, name); |
| 56 | } |
| 57 | #define BENCH(memcpy32, count) DEF_BENCH(return Bench(count, memcpy32, #memcpy32); ) |
| 58 | |
| 59 | |
| 60 | // Let the libc developers do what they think is best. |
| 61 | static void memcpy32_memcpy(uint32_t* dst, const uint32_t* src, int count) { |
| 62 | memcpy(dst, src, sizeof(uint32_t) * count); |
| 63 | } |
| 64 | BENCH(memcpy32_memcpy, 10) |
| 65 | BENCH(memcpy32_memcpy, 100) |
| 66 | BENCH(memcpy32_memcpy, 1000) |
| 67 | BENCH(memcpy32_memcpy, 10000) |
| 68 | BENCH(memcpy32_memcpy, 100000) |
| 69 | |
commit-bot@chromium.org | dcba993 | 2014-05-28 22:47:26 +0000 | [diff] [blame] | 70 | // Test our chosen best, from SkUtils.h |
| 71 | BENCH(sk_memcpy32, 10) |
| 72 | BENCH(sk_memcpy32, 100) |
| 73 | BENCH(sk_memcpy32, 1000) |
| 74 | BENCH(sk_memcpy32, 10000) |
| 75 | BENCH(sk_memcpy32, 100000) |
| 76 | |
commit-bot@chromium.org | c4e416c | 2014-05-20 14:54:04 +0000 | [diff] [blame] | 77 | #undef BENCH |