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
8#include "Benchmark.h"
9#include "SkRandom.h"
10#include "SkTemplates.h"
11#include "SkUtils.h"
12
13template <typename Memcpy32>
14class Memcpy32Bench : public Benchmark {
15public:
16    explicit Memcpy32Bench(int count, Memcpy32 memcpy32, const char* name)
17        : fCount(count)
18        , fMemcpy32(memcpy32)
19        , fName(SkStringPrintf("%s_%d", name, count)) {}
20
21    virtual const char* onGetName() SK_OVERRIDE {
22        return fName.c_str();
23    }
24
25    virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
26        return backend == kNonRendering_Backend;
27    }
28
29    virtual void onPreDraw() SK_OVERRIDE {
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
39    virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
40        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
70// 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
77#undef BENCH
78