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 "Benchmark.h"
8 #include "SkBitmap.h"
9 #include "SkCanvas.h"
10 #include "SkColorPriv.h"
11 #include "SkPaint.h"
12 #include "SkShader.h"
13 #include "SkString.h"
14 #include "sk_tool_utils.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(SkMin32(w, h))*3/8, p);
26 
27     SkRect r;
28     r.set(0, 0, 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                      sk_tool_utils::colortype_name(ct), kOpaque_SkAlphaType == at ? 'X' : 'A');
48     }
49 
50 protected:
onGetName()51     const char* onGetName() override {
52         return fName.c_str();
53     }
54 
onDelayedSetup()55     void onDelayedSetup() override {
56         fBitmap.allocPixels();
57         fBitmap.eraseColor(kOpaque_SkAlphaType == fAlphaType ? SK_ColorWHITE : 0);
58 
59         draw_into_bitmap(fBitmap);
60 
61         fPaint.setShader(SkShader::MakeBitmapShader(fBitmap,
62                                                     SkShader::kRepeat_TileMode,
63                                                     SkShader::kRepeat_TileMode));
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     typedef Benchmark INHERITED;
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