• 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 "bench/Benchmark.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/effects/SkImageFilters.h"
12 #include "include/utils/SkRandom.h"
13 
14 #define FILTER_WIDTH_SMALL  32
15 #define FILTER_HEIGHT_SMALL 32
16 #define FILTER_WIDTH_LARGE  256
17 #define FILTER_HEIGHT_LARGE 256
18 
19 class MagnifierBench : public Benchmark {
20 public:
MagnifierBench(bool small)21     MagnifierBench(bool small) :
22         fIsSmall(small), fInitialized(false) {
23     }
24 
25 protected:
onGetName()26     const char* onGetName() override {
27         return fIsSmall ? "magnifier_small" : "magnifier_large";
28     }
29 
onDelayedSetup()30     void onDelayedSetup() override {
31         if (!fInitialized) {
32             make_checkerboard();
33             fInitialized = true;
34         }
35     }
36 
onDraw(int loops,SkCanvas * canvas)37     void onDraw(int loops, SkCanvas* canvas) override {
38         const int w = fIsSmall ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE;
39         const int h = fIsSmall ? FILTER_HEIGHT_SMALL : FILTER_HEIGHT_LARGE;
40         SkPaint paint;
41         paint.setImageFilter(
42             SkImageFilters::Magnifier(
43                 SkRect::MakeXYWH(SkIntToScalar(w / 4),
44                                  SkIntToScalar(h / 4),
45                                  SkIntToScalar(w / 2),
46                                  SkIntToScalar(h / 2)), 100, nullptr));
47 
48         for (int i = 0; i < loops; i++) {
49             canvas->drawBitmap(fCheckerboard, 0, 0, &paint);
50         }
51     }
52 
53 private:
make_checkerboard()54     void make_checkerboard() {
55         const int w = fIsSmall ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE;
56         const int h = fIsSmall ? FILTER_HEIGHT_SMALL : FILTER_HEIGHT_LARGE;
57         fCheckerboard.allocN32Pixels(w, h);
58         SkCanvas canvas(fCheckerboard);
59         canvas.clear(0x00000000);
60         SkPaint darkPaint;
61         darkPaint.setColor(0xFF804020);
62         SkPaint lightPaint;
63         lightPaint.setColor(0xFF244484);
64         for (int y = 0; y < h; y += 16) {
65             for (int x = 0; x < w; x += 16) {
66                 canvas.save();
67                 canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
68                 canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint);
69                 canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint);
70                 canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint);
71                 canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint);
72                 canvas.restore();
73             }
74         }
75     }
76 
77     bool fIsSmall;
78     bool fInitialized;
79     SkBitmap fCheckerboard;
80     typedef Benchmark INHERITED;
81 };
82 
83 ///////////////////////////////////////////////////////////////////////////////
84 
85 DEF_BENCH( return new MagnifierBench(true); )
86 DEF_BENCH( return new MagnifierBench(false); )
87