| 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 |  | 
| Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "bench/Benchmark.h" | 
 | 9 | #include "include/core/SkCanvas.h" | 
 | 10 | #include "include/core/SkFont.h" | 
 | 11 | #include "include/core/SkSurface.h" | 
| Michael Ludwig | 2300318 | 2019-08-05 11:25:23 -0400 | [diff] [blame] | 12 | #include "include/effects/SkImageFilters.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; | 
| robertphillips | 2238c9d | 2016-03-30 13:34:16 -0700 | [diff] [blame] | 23 |     paint.setColor(0xFF884422); | 
| Mike Reed | 89126e4 | 2019-01-03 12:59:14 -0500 | [diff] [blame] | 24 |     SkFont font; | 
 | 25 |     font.setSize(SkIntToScalar(96)); | 
| Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 26 |     surface->getCanvas()->drawSimpleText("g", 1, SkTextEncoding::kUTF8, 15, 55, font, paint); | 
| robertphillips | 2238c9d | 2016-03-30 13:34:16 -0700 | [diff] [blame] | 27 |     return surface->makeImageSnapshot(); | 
 | 28 | } | 
 | 29 |  | 
 | 30 | static sk_sp<SkImage> make_checkerboard() { | 
 | 31 |     sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(80, 80)); | 
 | 32 |     SkCanvas* canvas = surface->getCanvas(); | 
 | 33 |     canvas->clear(0x00000000); | 
 | 34 |     SkPaint darkPaint; | 
 | 35 |     darkPaint.setColor(0xFF804020); | 
 | 36 |     SkPaint lightPaint; | 
 | 37 |     lightPaint.setColor(0xFF244484); | 
 | 38 |     for (int y = 0; y < 80; y += 16) { | 
 | 39 |         for (int x = 0; x < 80; x += 16) { | 
 | 40 |             canvas->save(); | 
 | 41 |             canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); | 
 | 42 |             canvas->drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint); | 
 | 43 |             canvas->drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint); | 
 | 44 |             canvas->drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint); | 
 | 45 |             canvas->drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint); | 
 | 46 |             canvas->restore(); | 
 | 47 |         } | 
 | 48 |     } | 
 | 49 |  | 
 | 50 |     return surface->makeImageSnapshot(); | 
 | 51 | } | 
 | 52 |  | 
| tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 53 | class MergeBench : public Benchmark { | 
| sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 54 | public: | 
| mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 55 |     MergeBench(bool small) : fIsSmall(small), fInitialized(false) { } | 
| sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 56 |  | 
 | 57 | protected: | 
| mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 58 |     const char* onGetName() override { | 
| sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 59 |         return fIsSmall ? "merge_small" : "merge_large"; | 
 | 60 |     } | 
 | 61 |  | 
| joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame] | 62 |     void onDelayedSetup() override { | 
| sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 63 |         if (!fInitialized) { | 
| robertphillips | 2238c9d | 2016-03-30 13:34:16 -0700 | [diff] [blame] | 64 |             fImage = make_bitmap(); | 
 | 65 |             fCheckerboard = make_checkerboard(); | 
| sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 66 |             fInitialized = true; | 
 | 67 |         } | 
 | 68 |     } | 
 | 69 |  | 
| mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 70 |     void onDraw(int loops, SkCanvas* canvas) override { | 
| sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 71 |         SkRect r = fIsSmall ? SkRect::MakeWH(FILTER_WIDTH_SMALL, FILTER_HEIGHT_SMALL) : | 
 | 72 |                               SkRect::MakeWH(FILTER_WIDTH_LARGE, FILTER_HEIGHT_LARGE); | 
 | 73 |         SkPaint paint; | 
| Michael Ludwig | 2300318 | 2019-08-05 11:25:23 -0400 | [diff] [blame] | 74 |         paint.setImageFilter(SkImageFilters::Merge(SkImageFilters::Image(fCheckerboard), | 
 | 75 |                                                    SkImageFilters::Image(fImage))); | 
| 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: | 
| sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 82 |     bool fIsSmall; | 
 | 83 |     bool fInitialized; | 
| reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 84 |     sk_sp<SkImage> fImage, fCheckerboard; | 
| sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 85 |  | 
| John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 86 |     using INHERITED = Benchmark; | 
| sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 87 | }; | 
 | 88 |  | 
 | 89 | /////////////////////////////////////////////////////////////////////////////// | 
 | 90 |  | 
| mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 91 | DEF_BENCH( return new MergeBench(true); ) | 
 | 92 | DEF_BENCH( return new MergeBench(false); ) |