blob: 6aea6bfed5dccbc027f813c56d9ef5a5a90208e7 [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 */
Mike Kleinc0bd9f92019-04-23 12:05:21 -05007#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"
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,
Brian Osman7f364052020-02-06 11:25:43 -050025 SkIntToScalar(std::min(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;
Mike Reed92b33352019-08-24 19:39:13 -040028 r.setWH(SkIntToScalar(w), SkIntToScalar(h));
reed@android.com4d850592009-08-12 20:30:58 +000029 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",
Mike Kleinea3f0142019-03-20 11:12:10 -050047 ToolUtils::colortype_name(ct),
48 kOpaque_SkAlphaType == at ? 'X' : 'A');
reed@android.com4d850592009-08-12 20:30:58 +000049 }
50
51protected:
mtklein36352bf2015-03-25 18:17:31 -070052 const char* onGetName() override {
reed@android.com4d850592009-08-12 20:30:58 +000053 return fName.c_str();
54 }
55
joshualitt8a6697a2015-09-30 12:11:07 -070056 void onDelayedSetup() override {
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000057 fBitmap.allocPixels();
reed6c225732014-06-09 19:52:07 -070058 fBitmap.eraseColor(kOpaque_SkAlphaType == fAlphaType ? SK_ColorWHITE : 0);
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000059
60 draw_into_bitmap(fBitmap);
61
Mike Reed50acf8f2019-04-08 13:20:23 -040062 fPaint.setShader(fBitmap.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat));
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000063 }
64
65
mtkleina1ebeb22015-10-01 09:43:39 -070066 void onDraw(int loops, SkCanvas* canvas) override {
reed@android.com4d850592009-08-12 20:30:58 +000067 SkPaint paint(fPaint);
68 this->setupPaint(&paint);
69
commit-bot@chromium.org33614712013-12-03 18:17:16 +000070 for (int i = 0; i < loops; i++) {
reed@android.com4d850592009-08-12 20:30:58 +000071 canvas->drawPaint(paint);
72 }
73 }
74
75private:
tfarinaf168b862014-06-19 12:32:29 -070076 typedef Benchmark INHERITED;
reed@android.com4d850592009-08-12 20:30:58 +000077};
78
reed6c225732014-06-09 19:52:07 -070079DEF_BENCH(return new RepeatTileBench(kN32_SkColorType, kOpaque_SkAlphaType))
80DEF_BENCH(return new RepeatTileBench(kN32_SkColorType, kPremul_SkAlphaType))
81DEF_BENCH(return new RepeatTileBench(kRGB_565_SkColorType, kOpaque_SkAlphaType))