epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | */ |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 7 | #include "Benchmark.h" |
reed@android.com | 4d85059 | 2009-08-12 20:30:58 +0000 | [diff] [blame] | 8 | #include "SkBitmap.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkColorPriv.h" |
| 11 | #include "SkPaint.h" |
| 12 | #include "SkShader.h" |
| 13 | #include "SkString.h" |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 14 | #include "sk_tool_utils.h" |
reed@android.com | 4d85059 | 2009-08-12 20:30:58 +0000 | [diff] [blame] | 15 | |
commit-bot@chromium.org | 7fb83c8 | 2013-08-01 15:58:07 +0000 | [diff] [blame] | 16 | static void draw_into_bitmap(const SkBitmap& bm) { |
reed@android.com | 4d85059 | 2009-08-12 20:30:58 +0000 | [diff] [blame] | 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); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 26 | |
reed@android.com | 4d85059 | 2009-08-12 20:30:58 +0000 | [diff] [blame] | 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 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 35 | class RepeatTileBench : public Benchmark { |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 36 | const SkAlphaType fAlphaType; |
| 37 | SkPaint fPaint; |
| 38 | SkString fName; |
| 39 | SkBitmap fBitmap; |
reed@android.com | 4d85059 | 2009-08-12 20:30:58 +0000 | [diff] [blame] | 40 | public: |
Mike Reed | a920d36 | 2017-07-03 13:36:17 -0400 | [diff] [blame] | 41 | RepeatTileBench(SkColorType ct, SkAlphaType at = kPremul_SkAlphaType) : fAlphaType(at) { |
reed@android.com | 4d85059 | 2009-08-12 20:30:58 +0000 | [diff] [blame] | 42 | const int w = 50; |
| 43 | const int h = 50; |
reed@android.com | 4d85059 | 2009-08-12 20:30:58 +0000 | [diff] [blame] | 44 | |
Mike Reed | a920d36 | 2017-07-03 13:36:17 -0400 | [diff] [blame] | 45 | fBitmap.setInfo(SkImageInfo::Make(w, h, ct, at)); |
commit-bot@chromium.org | 7fb83c8 | 2013-08-01 15:58:07 +0000 | [diff] [blame] | 46 | fName.printf("repeatTile_%s_%c", |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 47 | sk_tool_utils::colortype_name(ct), kOpaque_SkAlphaType == at ? 'X' : 'A'); |
reed@android.com | 4d85059 | 2009-08-12 20:30:58 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 51 | const char* onGetName() override { |
reed@android.com | 4d85059 | 2009-08-12 20:30:58 +0000 | [diff] [blame] | 52 | return fName.c_str(); |
| 53 | } |
| 54 | |
joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame] | 55 | void onDelayedSetup() override { |
commit-bot@chromium.org | 7fb83c8 | 2013-08-01 15:58:07 +0000 | [diff] [blame] | 56 | fBitmap.allocPixels(); |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 57 | fBitmap.eraseColor(kOpaque_SkAlphaType == fAlphaType ? SK_ColorWHITE : 0); |
commit-bot@chromium.org | 7fb83c8 | 2013-08-01 15:58:07 +0000 | [diff] [blame] | 58 | |
| 59 | draw_into_bitmap(fBitmap); |
| 60 | |
reed | c6f28f7 | 2016-03-14 12:22:10 -0700 | [diff] [blame] | 61 | fPaint.setShader(SkShader::MakeBitmapShader(fBitmap, |
| 62 | SkShader::kRepeat_TileMode, |
| 63 | SkShader::kRepeat_TileMode)); |
commit-bot@chromium.org | 7fb83c8 | 2013-08-01 15:58:07 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 67 | void onDraw(int loops, SkCanvas* canvas) override { |
reed@android.com | 4d85059 | 2009-08-12 20:30:58 +0000 | [diff] [blame] | 68 | SkPaint paint(fPaint); |
| 69 | this->setupPaint(&paint); |
| 70 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 71 | for (int i = 0; i < loops; i++) { |
reed@android.com | 4d85059 | 2009-08-12 20:30:58 +0000 | [diff] [blame] | 72 | canvas->drawPaint(paint); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 77 | typedef Benchmark INHERITED; |
reed@android.com | 4d85059 | 2009-08-12 20:30:58 +0000 | [diff] [blame] | 78 | }; |
| 79 | |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 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)) |