fmenozzi | 17e8297 | 2016-07-28 10:59:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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" |
fmenozzi | 17e8297 | 2016-07-28 10:59:49 -0700 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkCanvas.h" |
| 11 | #include "include/core/SkColor.h" |
| 12 | #include "include/core/SkPaint.h" |
| 13 | #include "include/core/SkShader.h" |
| 14 | #include "include/core/SkString.h" |
| 15 | #include "include/effects/SkGradientShader.h" |
John Stiles | 9d5461f | 2020-07-27 15:53:49 -0400 | [diff] [blame] | 16 | #include "include/private/SkTemplates.h" |
fmenozzi | 17e8297 | 2016-07-28 10:59:49 -0700 | [diff] [blame] | 17 | |
| 18 | class HardStopGradientBench_ScaleNumHardStops : public Benchmark { |
| 19 | public: |
| 20 | HardStopGradientBench_ScaleNumHardStops(int colorCount, int hardStopCount) { |
| 21 | SkASSERT(hardStopCount <= colorCount/2); |
| 22 | |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 23 | fName.printf("hardstop_scale_num_hard_stops_%03d_colors_%03d_hard_stops", |
fmenozzi | 17e8297 | 2016-07-28 10:59:49 -0700 | [diff] [blame] | 24 | colorCount, hardStopCount); |
| 25 | |
| 26 | fColorCount = colorCount; |
| 27 | fHardStopCount = hardStopCount; |
| 28 | } |
| 29 | |
| 30 | const char* onGetName() override { |
| 31 | return fName.c_str(); |
| 32 | } |
| 33 | |
| 34 | SkIPoint onGetSize() override { |
| 35 | return SkIPoint::Make(kSize, kSize); |
| 36 | } |
| 37 | |
| 38 | void onPreDraw(SkCanvas* canvas) override { |
| 39 | // Left to right |
| 40 | SkPoint points[2] = { |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 41 | SkPoint::Make(0, kSize/2), |
fmenozzi | 17e8297 | 2016-07-28 10:59:49 -0700 | [diff] [blame] | 42 | SkPoint::Make(kSize-1, kSize/2), |
| 43 | }; |
| 44 | |
| 45 | constexpr int kNumColorChoices = 4; |
| 46 | SkColor color_choices[kNumColorChoices] = { |
| 47 | SK_ColorRED, |
| 48 | SK_ColorGREEN, |
| 49 | SK_ColorBLUE, |
| 50 | SK_ColorYELLOW, |
| 51 | }; |
| 52 | |
| 53 | // Alternate between different choices |
| 54 | SkAutoTArray<SkColor> colors(fColorCount); |
| 55 | for (int i = 0; i < fColorCount; i++) { |
| 56 | colors[i] = color_choices[i % kNumColorChoices]; |
| 57 | } |
| 58 | |
| 59 | // Create requisite number of hard stops, and evenly |
| 60 | // space positions after that |
| 61 | SkAutoTArray<SkScalar> positions(fColorCount); |
| 62 | int k = 0; |
| 63 | for (int i = 0; i < fHardStopCount; i++) { |
| 64 | float val = k/2.0f; |
| 65 | positions[k++] = val / fColorCount; |
| 66 | positions[k++] = val / fColorCount; |
| 67 | } |
| 68 | for (int i = k; i < fColorCount; i++) { |
| 69 | positions[i] = i / (fColorCount - 1.0f); |
| 70 | } |
| 71 | |
| 72 | fPaint.setShader(SkGradientShader::MakeLinear(points, |
| 73 | colors.get(), |
| 74 | positions.get(), |
| 75 | fColorCount, |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 76 | SkTileMode::kClamp, |
fmenozzi | 17e8297 | 2016-07-28 10:59:49 -0700 | [diff] [blame] | 77 | 0, |
| 78 | nullptr)); |
| 79 | } |
| 80 | |
| 81 | /* |
| 82 | * Draw simple linear gradient from left to right |
| 83 | */ |
| 84 | void onDraw(int loops, SkCanvas* canvas) override { |
| 85 | for (int i = 0; i < loops; i++) { |
| 86 | canvas->drawPaint(fPaint); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | private: |
| 91 | static const int kSize = 500; |
| 92 | |
| 93 | SkString fName; |
| 94 | int fColorCount; |
| 95 | int fHardStopCount; |
| 96 | SkPaint fPaint; |
| 97 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 98 | using INHERITED = Benchmark; |
fmenozzi | 17e8297 | 2016-07-28 10:59:49 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | DEF_BENCH(return new HardStopGradientBench_ScaleNumHardStops(10, 1);) |
| 102 | DEF_BENCH(return new HardStopGradientBench_ScaleNumHardStops(10, 2);) |
| 103 | DEF_BENCH(return new HardStopGradientBench_ScaleNumHardStops(10, 5);) |
| 104 | |
| 105 | DEF_BENCH(return new HardStopGradientBench_ScaleNumHardStops(20, 1);) |
| 106 | DEF_BENCH(return new HardStopGradientBench_ScaleNumHardStops(20, 5);) |
| 107 | DEF_BENCH(return new HardStopGradientBench_ScaleNumHardStops(20, 10);) |
| 108 | |
| 109 | DEF_BENCH(return new HardStopGradientBench_ScaleNumHardStops(50, 1);) |
| 110 | DEF_BENCH(return new HardStopGradientBench_ScaleNumHardStops(50, 10);) |
| 111 | DEF_BENCH(return new HardStopGradientBench_ScaleNumHardStops(50, 25);) |
| 112 | |
| 113 | DEF_BENCH(return new HardStopGradientBench_ScaleNumHardStops(100, 1);) |
| 114 | DEF_BENCH(return new HardStopGradientBench_ScaleNumHardStops(100, 25);) |
| 115 | DEF_BENCH(return new HardStopGradientBench_ScaleNumHardStops(100, 50);) |