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 "SkBitmapSource.h" |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 11 | #include "SkMergeImageFilter.h" |
| 12 | |
| 13 | #define FILTER_WIDTH_SMALL SkIntToScalar(32) |
| 14 | #define FILTER_HEIGHT_SMALL SkIntToScalar(32) |
| 15 | #define FILTER_WIDTH_LARGE SkIntToScalar(256) |
| 16 | #define FILTER_HEIGHT_LARGE SkIntToScalar(256) |
| 17 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 18 | class MergeBench : 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 | MergeBench(bool small) : fIsSmall(small), fInitialized(false) { } |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 21 | |
| 22 | protected: |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 23 | const char* onGetName() SK_OVERRIDE { |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 24 | return fIsSmall ? "merge_small" : "merge_large"; |
| 25 | } |
| 26 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 27 | void onPreDraw() SK_OVERRIDE { |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 28 | if (!fInitialized) { |
| 29 | make_bitmap(); |
| 30 | make_checkerboard(); |
| 31 | fInitialized = true; |
| 32 | } |
| 33 | } |
| 34 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 35 | void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 36 | SkRect r = fIsSmall ? SkRect::MakeWH(FILTER_WIDTH_SMALL, FILTER_HEIGHT_SMALL) : |
| 37 | SkRect::MakeWH(FILTER_WIDTH_LARGE, FILTER_HEIGHT_LARGE); |
| 38 | SkPaint paint; |
| 39 | paint.setImageFilter(mergeBitmaps())->unref(); |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 40 | for (int i = 0; i < loops; i++) { |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 41 | canvas->drawRect(r, paint); |
| 42 | } |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | private: |
| 46 | SkImageFilter* mergeBitmaps() { |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 47 | SkImageFilter* first = SkBitmapSource::Create(fCheckerboard); |
| 48 | SkImageFilter* second = SkBitmapSource::Create(fBitmap); |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 49 | SkAutoUnref aur0(first); |
| 50 | SkAutoUnref aur1(second); |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 51 | return SkMergeImageFilter::Create(first, second); |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | void make_bitmap() { |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 55 | fBitmap.allocN32Pixels(80, 80); |
| 56 | SkCanvas canvas(fBitmap); |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 57 | canvas.clear(0x00000000); |
| 58 | SkPaint paint; |
| 59 | paint.setAntiAlias(true); |
| 60 | paint.setColor(0xFF884422); |
| 61 | paint.setTextSize(SkIntToScalar(96)); |
| 62 | const char* str = "g"; |
| 63 | canvas.drawText(str, strlen(str), SkIntToScalar(15), SkIntToScalar(55), paint); |
| 64 | } |
| 65 | |
| 66 | void make_checkerboard() { |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 67 | fCheckerboard.allocN32Pixels(80, 80); |
| 68 | SkCanvas canvas(fCheckerboard); |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 69 | canvas.clear(0x00000000); |
| 70 | SkPaint darkPaint; |
| 71 | darkPaint.setColor(0xFF804020); |
| 72 | SkPaint lightPaint; |
| 73 | lightPaint.setColor(0xFF244484); |
| 74 | for (int y = 0; y < 80; y += 16) { |
| 75 | for (int x = 0; x < 80; x += 16) { |
| 76 | canvas.save(); |
| 77 | canvas.translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 78 | canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint); |
| 79 | canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint); |
| 80 | canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint); |
| 81 | canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint); |
| 82 | canvas.restore(); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | bool fIsSmall; |
| 88 | bool fInitialized; |
| 89 | SkBitmap fBitmap, fCheckerboard; |
| 90 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 91 | typedef Benchmark INHERITED; |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | /////////////////////////////////////////////////////////////////////////////// |
| 95 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 96 | DEF_BENCH( return new MergeBench(true); ) |
| 97 | DEF_BENCH( return new MergeBench(false); ) |