tomhudson@google.com | f2e91a3 | 2012-06-11 19:22:01 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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 | */ |
commit-bot@chromium.org | 97b4b67 | 2013-09-26 19:23:03 +0000 | [diff] [blame] | 7 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 8 | #include "Benchmark.h" |
tomhudson@google.com | f2e91a3 | 2012-06-11 19:22:01 +0000 | [diff] [blame] | 9 | #include "SkCanvas.h" |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 10 | #include "SkMorphologyImageFilter.h" |
tomhudson@google.com | f2e91a3 | 2012-06-11 19:22:01 +0000 | [diff] [blame] | 11 | #include "SkPaint.h" |
| 12 | #include "SkRandom.h" |
| 13 | #include "SkShader.h" |
| 14 | #include "SkString.h" |
tomhudson@google.com | f2e91a3 | 2012-06-11 19:22:01 +0000 | [diff] [blame] | 15 | |
| 16 | #define SMALL SkIntToScalar(2) |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 17 | #define REAL 1.5f |
tomhudson@google.com | f2e91a3 | 2012-06-11 19:22:01 +0000 | [diff] [blame] | 18 | #define BIG SkIntToScalar(10) |
| 19 | |
tomhudson@google.com | f2e91a3 | 2012-06-11 19:22:01 +0000 | [diff] [blame] | 20 | enum MorphologyType { |
| 21 | kErode_MT, |
| 22 | kDilate_MT |
| 23 | }; |
| 24 | |
tomhudson@google.com | f2e91a3 | 2012-06-11 19:22:01 +0000 | [diff] [blame] | 25 | static const char* gStyleName[] = { |
| 26 | "erode", |
| 27 | "dilate" |
| 28 | }; |
| 29 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 30 | class MorphologyBench : public Benchmark { |
tomhudson@google.com | f2e91a3 | 2012-06-11 19:22:01 +0000 | [diff] [blame] | 31 | SkScalar fRadius; |
| 32 | MorphologyType fStyle; |
| 33 | SkString fName; |
| 34 | |
| 35 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 36 | MorphologyBench(SkScalar rad, MorphologyType style) |
| 37 | { |
tomhudson@google.com | f2e91a3 | 2012-06-11 19:22:01 +0000 | [diff] [blame] | 38 | fRadius = rad; |
| 39 | fStyle = style; |
| 40 | const char* name = rad > 0 ? gStyleName[style] : "none"; |
| 41 | if (SkScalarFraction(rad) != 0) { |
| 42 | fName.printf("morph_%.2f_%s", SkScalarToFloat(rad), name); |
| 43 | } else { |
reed@google.com | e1ca705 | 2013-12-17 19:22:07 +0000 | [diff] [blame] | 44 | fName.printf("morph_%d_%s", SkScalarRoundToInt(rad), name); |
tomhudson@google.com | f2e91a3 | 2012-06-11 19:22:01 +0000 | [diff] [blame] | 45 | } |
| 46 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 47 | |
tomhudson@google.com | f2e91a3 | 2012-06-11 19:22:01 +0000 | [diff] [blame] | 48 | protected: |
robertphillips | fc11b0a | 2016-04-05 09:09:36 -0700 | [diff] [blame] | 49 | const char* onGetName() override { |
tomhudson@google.com | f2e91a3 | 2012-06-11 19:22:01 +0000 | [diff] [blame] | 50 | return fName.c_str(); |
| 51 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 52 | |
robertphillips | fc11b0a | 2016-04-05 09:09:36 -0700 | [diff] [blame] | 53 | void onDraw(int loops, SkCanvas* canvas) override { |
tomhudson@google.com | f2e91a3 | 2012-06-11 19:22:01 +0000 | [diff] [blame] | 54 | SkPaint paint; |
| 55 | this->setupPaint(&paint); |
| 56 | |
| 57 | paint.setAntiAlias(true); |
| 58 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 59 | SkRandom rand; |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 60 | for (int i = 0; i < loops; i++) { |
tomhudson@google.com | f2e91a3 | 2012-06-11 19:22:01 +0000 | [diff] [blame] | 61 | SkRect r = SkRect::MakeWH(rand.nextUScalar1() * 400, |
| 62 | rand.nextUScalar1() * 400); |
| 63 | r.offset(fRadius, fRadius); |
| 64 | |
| 65 | if (fRadius > 0) { |
robertphillips | fc11b0a | 2016-04-05 09:09:36 -0700 | [diff] [blame] | 66 | sk_sp<SkImageFilter> mf; |
tomhudson@google.com | f2e91a3 | 2012-06-11 19:22:01 +0000 | [diff] [blame] | 67 | switch (fStyle) { |
| 68 | case kDilate_MT: |
robertphillips | fc11b0a | 2016-04-05 09:09:36 -0700 | [diff] [blame] | 69 | mf = SkDilateImageFilter::Make(SkScalarFloorToInt(fRadius), |
| 70 | SkScalarFloorToInt(fRadius), |
| 71 | nullptr); |
tomhudson@google.com | f2e91a3 | 2012-06-11 19:22:01 +0000 | [diff] [blame] | 72 | break; |
| 73 | case kErode_MT: |
robertphillips | fc11b0a | 2016-04-05 09:09:36 -0700 | [diff] [blame] | 74 | mf = SkErodeImageFilter::Make(SkScalarFloorToInt(fRadius), |
| 75 | SkScalarFloorToInt(fRadius), |
| 76 | nullptr); |
tomhudson@google.com | f2e91a3 | 2012-06-11 19:22:01 +0000 | [diff] [blame] | 77 | break; |
| 78 | } |
robertphillips | fc11b0a | 2016-04-05 09:09:36 -0700 | [diff] [blame] | 79 | paint.setImageFilter(std::move(mf)); |
tomhudson@google.com | f2e91a3 | 2012-06-11 19:22:01 +0000 | [diff] [blame] | 80 | } |
| 81 | canvas->drawOval(r, paint); |
| 82 | } |
| 83 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 84 | |
tomhudson@google.com | f2e91a3 | 2012-06-11 19:22:01 +0000 | [diff] [blame] | 85 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 86 | typedef Benchmark INHERITED; |
tomhudson@google.com | f2e91a3 | 2012-06-11 19:22:01 +0000 | [diff] [blame] | 87 | }; |
| 88 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 89 | DEF_BENCH( return new MorphologyBench(SMALL, kErode_MT); ) |
| 90 | DEF_BENCH( return new MorphologyBench(SMALL, kDilate_MT); ) |
tomhudson@google.com | a0116d5 | 2012-06-11 20:48:18 +0000 | [diff] [blame] | 91 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 92 | DEF_BENCH( return new MorphologyBench(BIG, kErode_MT); ) |
| 93 | DEF_BENCH( return new MorphologyBench(BIG, kDilate_MT); ) |
tomhudson@google.com | f2e91a3 | 2012-06-11 19:22:01 +0000 | [diff] [blame] | 94 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 95 | DEF_BENCH( return new MorphologyBench(REAL, kErode_MT); ) |
| 96 | DEF_BENCH( return new MorphologyBench(REAL, kDilate_MT); ) |
tomhudson@google.com | f2e91a3 | 2012-06-11 19:22:01 +0000 | [diff] [blame] | 97 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 98 | DEF_BENCH( return new MorphologyBench(0, kErode_MT); ) |