xref: /aosp_15_r20/external/skia/bench/RepeatTileBench.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 "include/core/SkTileMode.h"
15 #include "tools/ToolUtils.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 class RepeatTileBench : public Benchmark {
37     const SkAlphaType   fAlphaType;
38     SkPaint             fPaint;
39     SkString            fName;
40     SkBitmap            fBitmap;
41 public:
RepeatTileBench(SkColorType ct,SkAlphaType at=kPremul_SkAlphaType)42     RepeatTileBench(SkColorType ct, SkAlphaType at = kPremul_SkAlphaType) : fAlphaType(at) {
43         const int w = 50;
44         const int h = 50;
45 
46         fBitmap.setInfo(SkImageInfo::Make(w, h, ct, at));
47         fName.printf("repeatTile_%s_%c",
48                      ToolUtils::colortype_name(ct),
49                      kOpaque_SkAlphaType == at ? 'X' : 'A');
50     }
51 
52 protected:
onGetName()53     const char* onGetName() override {
54         return fName.c_str();
55     }
56 
onDelayedSetup()57     void onDelayedSetup() override {
58         fBitmap.allocPixels();
59         fBitmap.eraseColor(kOpaque_SkAlphaType == fAlphaType ? SK_ColorWHITE : 0);
60 
61         draw_into_bitmap(fBitmap);
62 
63         fPaint.setShader(fBitmap.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
64                                             SkSamplingOptions()));
65     }
66 
67 
onDraw(int loops,SkCanvas * canvas)68     void onDraw(int loops, SkCanvas* canvas) override {
69         SkPaint paint(fPaint);
70         this->setupPaint(&paint);
71 
72         for (int i = 0; i < loops; i++) {
73             canvas->drawPaint(paint);
74         }
75     }
76 
77 private:
78     using INHERITED = Benchmark;
79 };
80 
81 DEF_BENCH(return new RepeatTileBench(kN32_SkColorType, kOpaque_SkAlphaType))
82 DEF_BENCH(return new RepeatTileBench(kN32_SkColorType, kPremul_SkAlphaType))
83 DEF_BENCH(return new RepeatTileBench(kRGB_565_SkColorType, kOpaque_SkAlphaType))
84