blob: 83ba3f12aafd98b49f90da4108aefa6ced6b72f7 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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 */
tfarinaf168b862014-06-19 12:32:29 -07007#include "Benchmark.h"
reed@android.com4d850592009-08-12 20:30:58 +00008#include "SkBitmap.h"
9#include "SkCanvas.h"
10#include "SkColorPriv.h"
11#include "SkPaint.h"
12#include "SkShader.h"
13#include "SkString.h"
reed6c225732014-06-09 19:52:07 -070014#include "sk_tool_utils.h"
reed@android.com4d850592009-08-12 20:30:58 +000015
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000016static void draw_into_bitmap(const SkBitmap& bm) {
reed@android.com4d850592009-08-12 20:30:58 +000017 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);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000026
reed@android.com4d850592009-08-12 20:30:58 +000027 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
tfarinaf168b862014-06-19 12:32:29 -070035class RepeatTileBench : public Benchmark {
reed6c225732014-06-09 19:52:07 -070036 const SkAlphaType fAlphaType;
37 SkPaint fPaint;
38 SkString fName;
39 SkBitmap fBitmap;
reed@android.com4d850592009-08-12 20:30:58 +000040public:
Mike Reeda920d362017-07-03 13:36:17 -040041 RepeatTileBench(SkColorType ct, SkAlphaType at = kPremul_SkAlphaType) : fAlphaType(at) {
reed@android.com4d850592009-08-12 20:30:58 +000042 const int w = 50;
43 const int h = 50;
reed@android.com4d850592009-08-12 20:30:58 +000044
Mike Reeda920d362017-07-03 13:36:17 -040045 fBitmap.setInfo(SkImageInfo::Make(w, h, ct, at));
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000046 fName.printf("repeatTile_%s_%c",
reed6c225732014-06-09 19:52:07 -070047 sk_tool_utils::colortype_name(ct), kOpaque_SkAlphaType == at ? 'X' : 'A');
reed@android.com4d850592009-08-12 20:30:58 +000048 }
49
50protected:
mtklein36352bf2015-03-25 18:17:31 -070051 const char* onGetName() override {
reed@android.com4d850592009-08-12 20:30:58 +000052 return fName.c_str();
53 }
54
joshualitt8a6697a2015-09-30 12:11:07 -070055 void onDelayedSetup() override {
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000056 fBitmap.allocPixels();
reed6c225732014-06-09 19:52:07 -070057 fBitmap.eraseColor(kOpaque_SkAlphaType == fAlphaType ? SK_ColorWHITE : 0);
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000058
59 draw_into_bitmap(fBitmap);
60
reedc6f28f72016-03-14 12:22:10 -070061 fPaint.setShader(SkShader::MakeBitmapShader(fBitmap,
62 SkShader::kRepeat_TileMode,
63 SkShader::kRepeat_TileMode));
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000064 }
65
66
mtkleina1ebeb22015-10-01 09:43:39 -070067 void onDraw(int loops, SkCanvas* canvas) override {
reed@android.com4d850592009-08-12 20:30:58 +000068 SkPaint paint(fPaint);
69 this->setupPaint(&paint);
70
commit-bot@chromium.org33614712013-12-03 18:17:16 +000071 for (int i = 0; i < loops; i++) {
reed@android.com4d850592009-08-12 20:30:58 +000072 canvas->drawPaint(paint);
73 }
74 }
75
76private:
tfarinaf168b862014-06-19 12:32:29 -070077 typedef Benchmark INHERITED;
reed@android.com4d850592009-08-12 20:30:58 +000078};
79
reed6c225732014-06-09 19:52:07 -070080DEF_BENCH(return new RepeatTileBench(kN32_SkColorType, kOpaque_SkAlphaType))
81DEF_BENCH(return new RepeatTileBench(kN32_SkColorType, kPremul_SkAlphaType))
82DEF_BENCH(return new RepeatTileBench(kRGB_565_SkColorType, kOpaque_SkAlphaType))