ImageCacheBench.cpp revision 805ef159d197007b9529e60e5b918ee0f9d3802d
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 */
7
8#include "Benchmark.h"
9#include "SkScaledImageCache.h"
10
11class ImageCacheBench : public Benchmark {
12    SkScaledImageCache  fCache;
13    SkBitmap            fBM;
14
15    enum {
16        DIM = 1,
17        CACHE_COUNT = 500
18    };
19public:
20    ImageCacheBench()  : fCache(CACHE_COUNT * 100) {
21        fBM.allocN32Pixels(DIM, DIM);
22    }
23
24    void populateCache() {
25        SkScalar scale = 1;
26        for (int i = 0; i < CACHE_COUNT; ++i) {
27            SkBitmap tmp;
28            tmp.allocN32Pixels(1, 1);
29            fCache.unlock(fCache.addAndLock(fBM, scale, scale, tmp));
30            scale += 1;
31        }
32    }
33
34protected:
35    virtual const char* onGetName() SK_OVERRIDE {
36        return "imagecache";
37    }
38
39    virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
40        if (fCache.getTotalBytesUsed() == 0) {
41            this->populateCache();
42        }
43
44        SkBitmap tmp;
45        // search for a miss (-1 scale)
46        for (int i = 0; i < loops; ++i) {
47            (void)fCache.findAndLock(fBM, -1, -1, &tmp);
48        }
49    }
50
51private:
52    typedef Benchmark INHERITED;
53};
54
55///////////////////////////////////////////////////////////////////////////////
56
57DEF_BENCH( return new ImageCacheBench(); )
58