sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 | */ |
reed | 8c0c7b0 | 2014-06-27 05:49:53 -0700 | [diff] [blame] | 7 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 8 | #include "Benchmark.h" |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 9 | #include "SkCanvas.h" |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 10 | #include "SkMagnifierImageFilter.h" |
| 11 | #include "SkRandom.h" |
| 12 | |
sugoi@google.com | ca425d9 | 2013-04-23 14:37:38 +0000 | [diff] [blame] | 13 | #define FILTER_WIDTH_SMALL 32 |
| 14 | #define FILTER_HEIGHT_SMALL 32 |
| 15 | #define FILTER_WIDTH_LARGE 256 |
| 16 | #define FILTER_HEIGHT_LARGE 256 |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 17 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 18 | class MagnifierBench : public Benchmark { |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 19 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 20 | MagnifierBench(bool small) : |
| 21 | fIsSmall(small), fInitialized(false) { |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 25 | const char* onGetName() override { |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 26 | return fIsSmall ? "magnifier_small" : "magnifier_large"; |
| 27 | } |
| 28 | |
joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame] | 29 | void onDelayedSetup() override { |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 30 | if (!fInitialized) { |
| 31 | make_checkerboard(); |
| 32 | fInitialized = true; |
| 33 | } |
| 34 | } |
| 35 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 36 | void onDraw(int loops, SkCanvas* canvas) override { |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 37 | const int w = fIsSmall ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE; |
| 38 | const int h = fIsSmall ? FILTER_HEIGHT_SMALL : FILTER_HEIGHT_LARGE; |
| 39 | SkPaint paint; |
| 40 | paint.setImageFilter( |
robertphillips | 11171f3 | 2016-04-07 07:34:15 -0700 | [diff] [blame] | 41 | SkMagnifierImageFilter::Make( |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 42 | SkRect::MakeXYWH(SkIntToScalar(w / 4), |
| 43 | SkIntToScalar(h / 4), |
| 44 | SkIntToScalar(w / 2), |
robertphillips | 11171f3 | 2016-04-07 07:34:15 -0700 | [diff] [blame] | 45 | SkIntToScalar(h / 2)), 100, nullptr)); |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 46 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 47 | for (int i = 0; i < loops; i++) { |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 48 | canvas->drawBitmap(fCheckerboard, 0, 0, &paint); |
| 49 | } |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | private: |
| 53 | void make_checkerboard() { |
| 54 | const int w = fIsSmall ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE; |
| 55 | const int h = fIsSmall ? FILTER_HEIGHT_LARGE : FILTER_HEIGHT_LARGE; |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 56 | fCheckerboard.allocN32Pixels(w, h); |
| 57 | SkCanvas canvas(fCheckerboard); |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 58 | canvas.clear(0x00000000); |
| 59 | SkPaint darkPaint; |
| 60 | darkPaint.setColor(0xFF804020); |
| 61 | SkPaint lightPaint; |
| 62 | lightPaint.setColor(0xFF244484); |
| 63 | for (int y = 0; y < h; y += 16) { |
| 64 | for (int x = 0; x < w; x += 16) { |
| 65 | canvas.save(); |
| 66 | canvas.translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 67 | canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint); |
| 68 | canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint); |
| 69 | canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint); |
| 70 | canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint); |
| 71 | canvas.restore(); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | bool fIsSmall; |
| 77 | bool fInitialized; |
| 78 | SkBitmap fCheckerboard; |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 79 | typedef Benchmark INHERITED; |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | /////////////////////////////////////////////////////////////////////////////// |
| 83 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 84 | DEF_BENCH( return new MagnifierBench(true); ) |
| 85 | DEF_BENCH( return new MagnifierBench(false); ) |