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