halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 | */ |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 7 | #include "Benchmark.h" |
commit-bot@chromium.org | c25d221 | 2013-12-02 22:32:47 +0000 | [diff] [blame] | 8 | #include "SkColorPriv.h" |
| 9 | #include "SkRandom.h" |
| 10 | #include "SkString.h" |
| 11 | |
| 12 | template <bool kFast, bool kScale> |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 13 | class FourByteInterpBench : public Benchmark { |
commit-bot@chromium.org | c25d221 | 2013-12-02 22:32:47 +0000 | [diff] [blame] | 14 | public: |
| 15 | FourByteInterpBench() { |
| 16 | fName.set("four_byte_interp"); |
| 17 | fName.append(kFast ? "_fast" : "_slow"); |
| 18 | fName.append(kScale ? "_255" : "_256"); |
commit-bot@chromium.org | c25d221 | 2013-12-02 22:32:47 +0000 | [diff] [blame] | 19 | } |
| 20 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 21 | bool isSuitableFor(Backend backend) override { |
commit-bot@chromium.org | c25d221 | 2013-12-02 22:32:47 +0000 | [diff] [blame] | 22 | return backend == kNonRendering_Backend; |
| 23 | } |
| 24 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 25 | const char* onGetName() override { return fName.c_str(); } |
commit-bot@chromium.org | c25d221 | 2013-12-02 22:32:47 +0000 | [diff] [blame] | 26 | |
joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame] | 27 | void onDelayedSetup() override { |
commit-bot@chromium.org | cc6db40 | 2013-12-05 16:43:08 +0000 | [diff] [blame] | 28 | // A handful of random srcs and dsts. |
| 29 | SkRandom rand; |
| 30 | for (int i = 0; i < kInputs; i++) { |
| 31 | fSrcs[i] = SkPreMultiplyColor(rand.nextU()); |
| 32 | fDsts[i] = SkPreMultiplyColor(rand.nextU()); |
| 33 | } |
| 34 | |
| 35 | // We'll exhaustively test all scales instead of using random numbers. |
| 36 | for (int i = 0; i <= 256; i++) { |
| 37 | fScales[i] = i; |
| 38 | } |
| 39 | if (kScale) fScales[256] = 255; // We'll just do 255 twice if we're limited to [0,255]. |
| 40 | } |
| 41 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 42 | void onDraw(int loops, SkCanvas*) override { |
commit-bot@chromium.org | cc6db40 | 2013-12-05 16:43:08 +0000 | [diff] [blame] | 43 | // We xor results of FourByteInterp into junk to make sure the function runs. |
commit-bot@chromium.org | c25d221 | 2013-12-02 22:32:47 +0000 | [diff] [blame] | 44 | volatile SkPMColor junk = 0; |
commit-bot@chromium.org | cc6db40 | 2013-12-05 16:43:08 +0000 | [diff] [blame] | 45 | |
| 46 | for (int loop = 0; loop < loops; loop++) { |
| 47 | for (int i = 0; i < kInputs; i++) { |
| 48 | for (size_t j = 0; j <= 256; j++) { |
| 49 | // Note: we really want to load src and dst here and not outside in the i-loop. |
| 50 | // If we put the loads there, a clever compiler will do the not-insignificant |
| 51 | // work in the FourByteInterps that depends only on src and dst outside this |
| 52 | // loop, so we'd only be benchmarking the back half of those functions that also |
| 53 | // depends on scale. Even here, these must be volatile arrays to prevent that |
| 54 | // clever compiler from hoisting the loads out of the loop on its own. |
| 55 | const SkPMColor src = fSrcs[i]; |
| 56 | const SkPMColor dst = fDsts[i]; |
| 57 | |
| 58 | const unsigned scale = fScales[j]; |
| 59 | |
| 60 | if (kFast && kScale) { |
| 61 | junk ^= SkFastFourByteInterp(src, dst, scale); |
| 62 | } else if (kFast) { |
| 63 | junk ^= SkFastFourByteInterp256(src, dst, scale); |
| 64 | } else if (kScale) { |
| 65 | junk ^= SkFourByteInterp(src, dst, scale); |
| 66 | } else { |
| 67 | junk ^= SkFourByteInterp256(src, dst, scale); |
| 68 | } |
commit-bot@chromium.org | c25d221 | 2013-12-02 22:32:47 +0000 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | private: |
| 75 | SkString fName; |
commit-bot@chromium.org | cc6db40 | 2013-12-05 16:43:08 +0000 | [diff] [blame] | 76 | static const int kInputs = 10; // Arbitrary. |
| 77 | volatile unsigned fSrcs[kInputs]; |
| 78 | volatile unsigned fDsts[kInputs]; |
commit-bot@chromium.org | c25d221 | 2013-12-02 22:32:47 +0000 | [diff] [blame] | 79 | unsigned fScales[257]; // We need space for [0, 256]. |
| 80 | }; |
| 81 | |
| 82 | #define COMMA , |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 83 | DEF_BENCH(return (new FourByteInterpBench<true COMMA true>);) |
| 84 | DEF_BENCH(return (new FourByteInterpBench<true COMMA false>);) |
| 85 | DEF_BENCH(return (new FourByteInterpBench<false COMMA true>);) |
| 86 | DEF_BENCH(return (new FourByteInterpBench<false COMMA false>);) |
commit-bot@chromium.org | c25d221 | 2013-12-02 22:32:47 +0000 | [diff] [blame] | 87 | #undef COMMA |