reed | 37a4736 | 2015-02-06 13:04:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | */ |
| 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/SkPath.h" |
| 11 | #include "tools/ToolUtils.h" |
reed | 37a4736 | 2015-02-06 13:04:16 -0800 | [diff] [blame] | 12 | |
| 13 | enum Align { |
| 14 | kLeft_Align, |
| 15 | kMiddle_Align, |
| 16 | kRight_Align |
| 17 | }; |
| 18 | |
| 19 | const char* gAlignName[] = { "left", "middle", "right" }; |
| 20 | |
| 21 | // Inspired by crbug.com/455429 |
| 22 | class BigPathBench : public Benchmark { |
| 23 | SkPath fPath; |
| 24 | SkString fName; |
| 25 | Align fAlign; |
reed | 70a8ca8 | 2015-02-09 08:05:52 -0800 | [diff] [blame] | 26 | bool fRound; |
reed | 37a4736 | 2015-02-06 13:04:16 -0800 | [diff] [blame] | 27 | |
| 28 | public: |
reed | 70a8ca8 | 2015-02-09 08:05:52 -0800 | [diff] [blame] | 29 | BigPathBench(Align align, bool round) : fAlign(align), fRound(round) { |
reed | 37a4736 | 2015-02-06 13:04:16 -0800 | [diff] [blame] | 30 | fName.printf("bigpath_%s", gAlignName[fAlign]); |
reed | 70a8ca8 | 2015-02-09 08:05:52 -0800 | [diff] [blame] | 31 | if (round) { |
| 32 | fName.append("_round"); |
| 33 | } |
reed | 37a4736 | 2015-02-06 13:04:16 -0800 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 37 | const char* onGetName() override { |
reed | 37a4736 | 2015-02-06 13:04:16 -0800 | [diff] [blame] | 38 | return fName.c_str(); |
| 39 | } |
| 40 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 41 | SkIPoint onGetSize() override { |
reed | 37a4736 | 2015-02-06 13:04:16 -0800 | [diff] [blame] | 42 | return SkIPoint::Make(640, 100); |
| 43 | } |
| 44 | |
Mike Reed | 92f6eb1 | 2020-08-25 11:48:41 -0400 | [diff] [blame] | 45 | void onDelayedSetup() override { fPath = ToolUtils::make_big_path(); } |
reed | 37a4736 | 2015-02-06 13:04:16 -0800 | [diff] [blame] | 46 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 47 | void onDraw(int loops, SkCanvas* canvas) override { |
reed | 37a4736 | 2015-02-06 13:04:16 -0800 | [diff] [blame] | 48 | SkPaint paint; |
| 49 | paint.setAntiAlias(true); |
| 50 | paint.setStyle(SkPaint::kStroke_Style); |
| 51 | paint.setStrokeWidth(2); |
reed | 70a8ca8 | 2015-02-09 08:05:52 -0800 | [diff] [blame] | 52 | if (fRound) { |
| 53 | paint.setStrokeJoin(SkPaint::kRound_Join); |
| 54 | } |
reed | 37a4736 | 2015-02-06 13:04:16 -0800 | [diff] [blame] | 55 | this->setupPaint(&paint); |
| 56 | |
| 57 | const SkRect r = fPath.getBounds(); |
| 58 | switch (fAlign) { |
| 59 | case kLeft_Align: |
| 60 | canvas->translate(-r.left(), 0); |
| 61 | break; |
| 62 | case kMiddle_Align: |
| 63 | break; |
| 64 | case kRight_Align: |
| 65 | canvas->translate(640 - r.right(), 0); |
| 66 | break; |
| 67 | } |
| 68 | |
| 69 | for (int i = 0; i < loops; i++) { |
| 70 | canvas->drawPath(fPath, paint); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | private: |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 75 | using INHERITED = Benchmark; |
reed | 37a4736 | 2015-02-06 13:04:16 -0800 | [diff] [blame] | 76 | }; |
| 77 | |
reed | 70a8ca8 | 2015-02-09 08:05:52 -0800 | [diff] [blame] | 78 | DEF_BENCH( return new BigPathBench(kLeft_Align, false); ) |
| 79 | DEF_BENCH( return new BigPathBench(kMiddle_Align, false); ) |
| 80 | DEF_BENCH( return new BigPathBench(kRight_Align, false); ) |
| 81 | |
| 82 | DEF_BENCH( return new BigPathBench(kLeft_Align, true); ) |
| 83 | DEF_BENCH( return new BigPathBench(kMiddle_Align, true); ) |
| 84 | DEF_BENCH( return new BigPathBench(kRight_Align, true); ) |