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" |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 10 | #include "SkImageSource.h" |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 11 | #include "SkMergeImageFilter.h" |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 12 | #include "SkSurface.h" |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 13 | |
| 14 | #define FILTER_WIDTH_SMALL SkIntToScalar(32) |
| 15 | #define FILTER_HEIGHT_SMALL SkIntToScalar(32) |
| 16 | #define FILTER_WIDTH_LARGE SkIntToScalar(256) |
| 17 | #define FILTER_HEIGHT_LARGE SkIntToScalar(256) |
| 18 | |
robertphillips | 2238c9d | 2016-03-30 13:34:16 -0700 | [diff] [blame] | 19 | static sk_sp<SkImage> make_bitmap() { |
| 20 | sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(80, 80)); |
| 21 | surface->getCanvas()->clear(0x00000000); |
| 22 | SkPaint paint; |
| 23 | paint.setAntiAlias(true); |
| 24 | paint.setColor(0xFF884422); |
| 25 | paint.setTextSize(SkIntToScalar(96)); |
| 26 | const char* str = "g"; |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 27 | surface->getCanvas()->drawString(str, 15, 55, paint); |
robertphillips | 2238c9d | 2016-03-30 13:34:16 -0700 | [diff] [blame] | 28 | return surface->makeImageSnapshot(); |
| 29 | } |
| 30 | |
| 31 | static sk_sp<SkImage> make_checkerboard() { |
| 32 | sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(80, 80)); |
| 33 | SkCanvas* canvas = surface->getCanvas(); |
| 34 | canvas->clear(0x00000000); |
| 35 | SkPaint darkPaint; |
| 36 | darkPaint.setColor(0xFF804020); |
| 37 | SkPaint lightPaint; |
| 38 | lightPaint.setColor(0xFF244484); |
| 39 | for (int y = 0; y < 80; y += 16) { |
| 40 | for (int x = 0; x < 80; x += 16) { |
| 41 | canvas->save(); |
| 42 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 43 | canvas->drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint); |
| 44 | canvas->drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint); |
| 45 | canvas->drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint); |
| 46 | canvas->drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint); |
| 47 | canvas->restore(); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | return surface->makeImageSnapshot(); |
| 52 | } |
| 53 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 54 | class MergeBench : public Benchmark { |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 55 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 56 | MergeBench(bool small) : fIsSmall(small), fInitialized(false) { } |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 57 | |
| 58 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 59 | const char* onGetName() override { |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 60 | return fIsSmall ? "merge_small" : "merge_large"; |
| 61 | } |
| 62 | |
joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame] | 63 | void onDelayedSetup() override { |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 64 | if (!fInitialized) { |
robertphillips | 2238c9d | 2016-03-30 13:34:16 -0700 | [diff] [blame] | 65 | fImage = make_bitmap(); |
| 66 | fCheckerboard = make_checkerboard(); |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 67 | fInitialized = true; |
| 68 | } |
| 69 | } |
| 70 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 71 | void onDraw(int loops, SkCanvas* canvas) override { |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 72 | SkRect r = fIsSmall ? SkRect::MakeWH(FILTER_WIDTH_SMALL, FILTER_HEIGHT_SMALL) : |
| 73 | SkRect::MakeWH(FILTER_WIDTH_LARGE, FILTER_HEIGHT_LARGE); |
| 74 | SkPaint paint; |
robertphillips | 2238c9d | 2016-03-30 13:34:16 -0700 | [diff] [blame] | 75 | paint.setImageFilter(this->mergeBitmaps()); |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 76 | for (int i = 0; i < loops; i++) { |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 77 | canvas->drawRect(r, paint); |
| 78 | } |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | private: |
robertphillips | 2238c9d | 2016-03-30 13:34:16 -0700 | [diff] [blame] | 82 | sk_sp<SkImageFilter> mergeBitmaps() { |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 83 | return SkMergeImageFilter::Make(SkImageSource::Make(fCheckerboard), |
Mike Reed | 0bdaf05 | 2017-06-18 23:35:57 -0400 | [diff] [blame] | 84 | SkImageSource::Make(fImage)); |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | bool fIsSmall; |
| 88 | bool fInitialized; |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 89 | sk_sp<SkImage> fImage, fCheckerboard; |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 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); ) |