| 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 |
|
| 8 |
#include "bench/Benchmark.h" |
| 9 |
|
| 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/SkGradient.h" |
| 16 |
|
| 17 |
#include "tools/ToolUtils.h" |
| 18 |
|
| 19 |
class HardStopGradientBench_ScaleNumColors : public Benchmark { |
| 20 |
public: |
| 21 |
HardStopGradientBench_ScaleNumColors(SkTileMode tilemode, int count) { |
| 22 |
fName.printf("hardstop_scale_num_colors_%s_%03d_colors", |
| 23 |
ToolUtils::tilemode_name(tilemode), count); |
| 24 |
|
| 25 |
fTileMode = tilemode; |
| 26 |
fColorCount = count; |
| 27 |
} |
| 28 |
|
| 29 |
const char* onGetName() override { |
| 30 |
return fName.c_str(); |
| 31 |
} |
| 32 |
|
| 33 |
SkISize onGetSize() override { |
| 34 |
return SkISize::Make(kSize, kSize); |
| 35 |
} |
| 36 |
|
| 37 |
/* |
| 38 |
* Set up a linear gradient from left to right with |
| 39 |
* fColorCount colors alternating between four |
| 40 |
* different colors. The positions are evenly spaced, |
| 41 |
* with the exception of the first two; these create a |
| 42 |
* hard stop in order to trigger the hard stop code. |
| 43 |
*/ |
| 44 |
void onPreDraw(SkCanvas* canvas) override { |
| 45 |
// Left to right |
| 46 |
SkPoint points[2] = { |
| 47 |
SkPoint::Make(0, kSize/2), |
| 48 |
SkPoint::Make(kSize-1, kSize/2), |
| 49 |
}; |
| 50 |
|
| 51 |
constexpr int kNumColorChoices = 4; |
| 52 |
SkColor4f color_choices[kNumColorChoices] = { |
| 53 |
SkColors::kRed, |
| 54 |
SkColors::kGreen, |
| 55 |
SkColors::kBlue, |
| 56 |
SkColors::kYellow, |
| 57 |
}; |
| 58 |
|
| 59 |
// Alternate between different choices |
| 60 |
SkColor4f colors[100]; |
| 61 |
for (int i = 0; i < fColorCount; i++) { |
| 62 |
colors[i] = color_choices[i % kNumColorChoices]; |
| 63 |
} |
| 64 |
|
| 65 |
// Create a hard stop |
| 66 |
float positions[100]; |
| 67 |
positions[0] = 0.0f; |
| 68 |
positions[1] = 0.0f; |
| 69 |
for (int i = 2; i < fColorCount; i++) { |
| 70 |
// Evenly spaced afterwards |
| 71 |
positions[i] = i / (fColorCount - 1.0f); |
| 72 |
} |
| 73 |
|
| 74 |
fPaint.setShader(SkShaders::LinearGradient(points, |
| 75 |
{{{colors, fColorCount}, {positions, fColorCount}, fTileMode}, {}})); |
| 76 |
} |
| 77 |
|
| 78 |
/* |
| 79 |
* Draw simple linear gradient from left to right |
| 80 |
*/ |
| 81 |
void onDraw(int loops, SkCanvas* canvas) override { |
| 82 |
for (int i = 0; i < loops; i++) { |
| 83 |
canvas->drawPaint(fPaint); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
private: |
| 88 |
static const int kSize = 500; |
| 89 |
|
| 90 |
SkTileMode fTileMode; |
| 91 |
SkString fName; |
| 92 |
int fColorCount; |
| 93 |
SkPaint fPaint; |
| 94 |
|
| 95 |
using INHERITED = Benchmark; |
| 96 |
}; |
| 97 |
|
| 98 |
// Clamp |
| 99 |
DEF_BENCH(return new HardStopGradientBench_ScaleNumColors(SkTileMode::kClamp, 3);) |
| 100 |
DEF_BENCH(return new HardStopGradientBench_ScaleNumColors(SkTileMode::kClamp, 4);) |
| 101 |
DEF_BENCH(return new HardStopGradientBench_ScaleNumColors(SkTileMode::kClamp, 5);) |
| 102 |
DEF_BENCH(return new HardStopGradientBench_ScaleNumColors(SkTileMode::kClamp, 10);) |
| 103 |
DEF_BENCH(return new HardStopGradientBench_ScaleNumColors(SkTileMode::kClamp, 25);) |
| 104 |
DEF_BENCH(return new HardStopGradientBench_ScaleNumColors(SkTileMode::kClamp, 50);) |
| 105 |
DEF_BENCH(return new HardStopGradientBench_ScaleNumColors(SkTileMode::kClamp, 100);) |
| 106 |
|
| 107 |
// Repeat |
| 108 |
DEF_BENCH(return new HardStopGradientBench_ScaleNumColors(SkTileMode::kRepeat, 3);) |
| 109 |
DEF_BENCH(return new HardStopGradientBench_ScaleNumColors(SkTileMode::kRepeat, 4);) |
| 110 |
DEF_BENCH(return new HardStopGradientBench_ScaleNumColors(SkTileMode::kRepeat, 5);) |
| 111 |
DEF_BENCH(return new HardStopGradientBench_ScaleNumColors(SkTileMode::kRepeat, 10);) |
| 112 |
DEF_BENCH(return new HardStopGradientBench_ScaleNumColors(SkTileMode::kRepeat, 25);) |
| 113 |
DEF_BENCH(return new HardStopGradientBench_ScaleNumColors(SkTileMode::kRepeat, 50);) |
| 114 |
DEF_BENCH(return new HardStopGradientBench_ScaleNumColors(SkTileMode::kRepeat, 100);) |
| 115 |
|
| 116 |
// Mirror |
| 117 |
DEF_BENCH(return new HardStopGradientBench_ScaleNumColors(SkTileMode::kMirror, 3);) |
| 118 |
DEF_BENCH(return new HardStopGradientBench_ScaleNumColors(SkTileMode::kMirror, 4);) |
| 119 |
DEF_BENCH(return new HardStopGradientBench_ScaleNumColors(SkTileMode::kMirror, 5);) |
| 120 |
DEF_BENCH(return new HardStopGradientBench_ScaleNumColors(SkTileMode::kMirror, 10);) |
| 121 |
DEF_BENCH(return new HardStopGradientBench_ScaleNumColors(SkTileMode::kMirror, 25);) |
| 122 |
DEF_BENCH(return new HardStopGradientBench_ScaleNumColors(SkTileMode::kMirror, 50);) |
| 123 |
DEF_BENCH(return new HardStopGradientBench_ScaleNumColors(SkTileMode::kMirror, 100);) |