• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2011 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/core/SkColorPriv.h"
12 #include "include/core/SkPaint.h"
13 #include "include/core/SkString.h"
14 #include "include/private/SkTo.h"
15 #include "include/utils/SkRandom.h"
16 
draw_into_bitmap(const SkBitmap & bm)17 static void draw_into_bitmap(const SkBitmap& bm) {
18     const int w = bm.width();
19     const int h = bm.height();
20 
21     SkCanvas canvas(bm);
22     SkPaint p;
23     p.setAntiAlias(true);
24     p.setColor(SK_ColorRED);
25     canvas.drawCircle(SkIntToScalar(w)/2, SkIntToScalar(h)/2,
26                       SkIntToScalar(std::min(w, h))*3/8, p);
27 
28     SkRect r;
29     r.setWH(SkIntToScalar(w), SkIntToScalar(h));
30     p.setStyle(SkPaint::kStroke_Style);
31     p.setStrokeWidth(SkIntToScalar(4));
32     p.setColor(SK_ColorBLUE);
33     canvas.drawRect(r, p);
34 }
35 
36 /*  Variants for bitmaprect
37     src : entire bitmap, subset, fractional subset
38     dst : same size as src, diff size
39     paint : filter-p
40  */
41 
42 class BitmapRectBench : public Benchmark {
43     SkBitmap                fBitmap;
44     bool                    fSlightMatrix;
45     uint8_t                 fAlpha;
46     SkFilterQuality         fFilterQuality;
47     SkString                fName;
48     SkRect                  fSrcR, fDstR;
49 
50     static const int kWidth = 128;
51     static const int kHeight = 128;
52 public:
BitmapRectBench(U8CPU alpha,SkFilterQuality filterQuality,bool slightMatrix)53     BitmapRectBench(U8CPU alpha, SkFilterQuality filterQuality,
54                     bool slightMatrix)  {
55         fAlpha = SkToU8(alpha);
56         fFilterQuality = filterQuality;
57         fSlightMatrix = slightMatrix;
58 
59         fBitmap.setInfo(SkImageInfo::MakeN32Premul(kWidth, kHeight));
60     }
61 
62 protected:
onGetName()63     const char* onGetName() override {
64         fName.printf("bitmaprect_%02X_%sfilter_%s",
65                      fAlpha,
66                      kNone_SkFilterQuality == fFilterQuality ? "no" : "",
67                      fSlightMatrix ? "trans" : "identity");
68         return fName.c_str();
69     }
70 
onDelayedSetup()71     void onDelayedSetup() override {
72         fBitmap.allocPixels();
73         fBitmap.setAlphaType(kOpaque_SkAlphaType);
74         fBitmap.eraseColor(SK_ColorBLACK);
75         draw_into_bitmap(fBitmap);
76 
77         fSrcR.setWH(SkIntToScalar(kWidth), SkIntToScalar(kHeight));
78         fDstR.setWH(SkIntToScalar(kWidth), SkIntToScalar(kHeight));
79 
80         if (fSlightMatrix) {
81             // want fractional translate
82             fDstR.offset(SK_Scalar1 / 3, SK_Scalar1 * 5 / 7);
83             // want enough to create a scale matrix, but not enough to scare
84             // off our sniffer which tries to see if the matrix is "effectively"
85             // translate-only.
86             fDstR.fRight += SK_Scalar1 / (kWidth * 60);
87         }
88     }
89 
90 
onDraw(int loops,SkCanvas * canvas)91     void onDraw(int loops, SkCanvas* canvas) override {
92         SkRandom rand;
93 
94         SkPaint paint;
95         this->setupPaint(&paint);
96         paint.setFilterQuality(fFilterQuality);
97         paint.setAlpha(fAlpha);
98 
99         for (int i = 0; i < loops; i++) {
100             canvas->drawBitmapRect(fBitmap, fSrcR, fDstR, &paint,
101                                    SkCanvas::kStrict_SrcRectConstraint);
102         }
103     }
104 
105 private:
106     typedef Benchmark INHERITED;
107 };
108 
109 DEF_BENCH(return new BitmapRectBench(0xFF, kNone_SkFilterQuality, false))
110 DEF_BENCH(return new BitmapRectBench(0x80, kNone_SkFilterQuality, false))
111 DEF_BENCH(return new BitmapRectBench(0xFF, kLow_SkFilterQuality, false))
112 DEF_BENCH(return new BitmapRectBench(0x80, kLow_SkFilterQuality, false))
113 
114 DEF_BENCH(return new BitmapRectBench(0xFF, kNone_SkFilterQuality, true))
115 DEF_BENCH(return new BitmapRectBench(0xFF, kLow_SkFilterQuality, true))
116