• 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 #include "bench/Benchmark.h"
8 #include "include/core/SkBitmap.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColorPriv.h"
11 #include "include/core/SkPaint.h"
12 #include "include/core/SkShader.h"
13 #include "include/core/SkString.h"
14 #include "tools/ToolUtils.h"
15 
draw_into_bitmap(const SkBitmap & bm)16 static void draw_into_bitmap(const SkBitmap& bm) {
17     const int w = bm.width();
18     const int h = bm.height();
19 
20     SkCanvas canvas(bm);
21     SkPaint p;
22     p.setAntiAlias(true);
23     p.setColor(SK_ColorRED);
24     canvas.drawCircle(SkIntToScalar(w)/2, SkIntToScalar(h)/2,
25                       SkIntToScalar(std::min(w, h))*3/8, p);
26 
27     SkRect r;
28     r.setWH(SkIntToScalar(w), SkIntToScalar(h));
29     p.setStyle(SkPaint::kStroke_Style);
30     p.setStrokeWidth(SkIntToScalar(4));
31     p.setColor(SK_ColorBLUE);
32     canvas.drawRect(r, p);
33 }
34 
35 class RepeatTileBench : public Benchmark {
36     const SkAlphaType   fAlphaType;
37     SkPaint             fPaint;
38     SkString            fName;
39     SkBitmap            fBitmap;
40 public:
RepeatTileBench(SkColorType ct,SkAlphaType at=kPremul_SkAlphaType)41     RepeatTileBench(SkColorType ct, SkAlphaType at = kPremul_SkAlphaType) : fAlphaType(at) {
42         const int w = 50;
43         const int h = 50;
44 
45         fBitmap.setInfo(SkImageInfo::Make(w, h, ct, at));
46         fName.printf("repeatTile_%s_%c",
47                      ToolUtils::colortype_name(ct),
48                      kOpaque_SkAlphaType == at ? 'X' : 'A');
49     }
50 
51 protected:
onGetName()52     const char* onGetName() override {
53         return fName.c_str();
54     }
55 
onDelayedSetup()56     void onDelayedSetup() override {
57         fBitmap.allocPixels();
58         fBitmap.eraseColor(kOpaque_SkAlphaType == fAlphaType ? SK_ColorWHITE : 0);
59 
60         draw_into_bitmap(fBitmap);
61 
62         fPaint.setShader(fBitmap.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
63                                             SkSamplingOptions()));
64     }
65 
66 
onDraw(int loops,SkCanvas * canvas)67     void onDraw(int loops, SkCanvas* canvas) override {
68         SkPaint paint(fPaint);
69         this->setupPaint(&paint);
70 
71         for (int i = 0; i < loops; i++) {
72             canvas->drawPaint(paint);
73         }
74     }
75 
76 private:
77     using INHERITED = Benchmark;
78 };
79 
80 DEF_BENCH(return new RepeatTileBench(kN32_SkColorType, kOpaque_SkAlphaType))
81 DEF_BENCH(return new RepeatTileBench(kN32_SkColorType, kPremul_SkAlphaType))
82 DEF_BENCH(return new RepeatTileBench(kRGB_565_SkColorType, kOpaque_SkAlphaType))
83