blob: 4bc128560741431ebb313d6fe3e476752f709d07 [file] [log] [blame]
commit-bot@chromium.orgc4e416c2014-05-20 14:54:04 +00001/*
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
tfarinaf168b862014-06-19 12:32:29 -07008#include "Benchmark.h"
commit-bot@chromium.orgc4e416c2014-05-20 14:54:04 +00009#include "SkRandom.h"
10#include "SkTemplates.h"
commit-bot@chromium.orgdcba9932014-05-28 22:47:26 +000011#include "SkUtils.h"
commit-bot@chromium.orgc4e416c2014-05-20 14:54:04 +000012
13template <typename Memcpy32>
tfarinaf168b862014-06-19 12:32:29 -070014class Memcpy32Bench : public Benchmark {
commit-bot@chromium.orgc4e416c2014-05-20 14:54:04 +000015public:
16 explicit Memcpy32Bench(int count, Memcpy32 memcpy32, const char* name)
17 : fCount(count)
18 , fMemcpy32(memcpy32)
19 , fName(SkStringPrintf("%s_%d", name, count)) {}
20
mtklein36352bf2015-03-25 18:17:31 -070021 const char* onGetName() override {
commit-bot@chromium.orgc4e416c2014-05-20 14:54:04 +000022 return fName.c_str();
23 }
24
mtklein36352bf2015-03-25 18:17:31 -070025 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.orgc4e416c2014-05-20 14:54:04 +000026 return backend == kNonRendering_Backend;
27 }
28
mtklein36352bf2015-03-25 18:17:31 -070029 void onPreDraw() override {
commit-bot@chromium.orgc4e416c2014-05-20 14:54:04 +000030 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
mtklein36352bf2015-03-25 18:17:31 -070039 void onDraw(const int loops, SkCanvas*) override {
commit-bot@chromium.orgc4e416c2014-05-20 14:54:04 +000040 for (int i = 0; i < loops; i++) {
41 fMemcpy32(fDst, fSrc, fCount);
42 }
43 }
44
45private:
46 SkAutoTMalloc<uint32_t> fDst, fSrc;
47
48 int fCount;
49 Memcpy32 fMemcpy32;
50 const SkString fName;
51};
52
53template <typename Memcpy32>
54static 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.
61static void memcpy32_memcpy(uint32_t* dst, const uint32_t* src, int count) {
62 memcpy(dst, src, sizeof(uint32_t) * count);
63}
64BENCH(memcpy32_memcpy, 10)
65BENCH(memcpy32_memcpy, 100)
66BENCH(memcpy32_memcpy, 1000)
67BENCH(memcpy32_memcpy, 10000)
68BENCH(memcpy32_memcpy, 100000)
69
commit-bot@chromium.orgdcba9932014-05-28 22:47:26 +000070// Test our chosen best, from SkUtils.h
71BENCH(sk_memcpy32, 10)
72BENCH(sk_memcpy32, 100)
73BENCH(sk_memcpy32, 1000)
74BENCH(sk_memcpy32, 10000)
75BENCH(sk_memcpy32, 100000)
76
commit-bot@chromium.orgc4e416c2014-05-20 14:54:04 +000077#undef BENCH