Chromium Code Reviews| Index: bench/HardStopGradientBench.cpp |
| diff --git a/bench/HardStopGradientBench.cpp b/bench/HardStopGradientBench.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..40dd95d891566663c9e3310e6583b4b5b003a476 |
| --- /dev/null |
| +++ b/bench/HardStopGradientBench.cpp |
| @@ -0,0 +1,128 @@ |
| +/* |
| + * Copyright 2016 Google Inc. |
| + * |
| + * Use of this source code is governed by a BSD-style license that can be |
| + * found in the LICENSE file. |
| + */ |
| + |
| +#include "Benchmark.h" |
| + |
| +#include "SkCanvas.h" |
| +#include "SkShader.h" |
| +#include "SkGradientShader.h" |
| +#include "SkString.h" |
| +#include "SkColor.h" |
| +#include "SkPaint.h" |
| + |
| +struct GradData { |
|
bsalomon
2016/07/07 18:36:32
Is this used?
|
| + int fCount; |
| + const SkColor* fColors; |
| + const SkScalar* fPos; |
| + const char* fName; |
| +}; |
| + |
| +static constexpr int kMaxCount = 50; |
| + |
| +static const char* get_tilemode_name(SkShader::TileMode tilemode) { |
| + switch (tilemode) { |
| + case SkShader::kClamp_TileMode: |
| + return "clamp"; |
| + case SkShader::kRepeat_TileMode: |
| + return "repeat"; |
| + case SkShader::kMirror_TileMode: |
| + return "mirror"; |
| + default: |
| + SkDEBUGFAIL("Unknown tilemode"); |
| + return "error"; |
| + } |
| +} |
| + |
| +class HardStopGradientBench : public Benchmark { |
| +public: |
| + HardStopGradientBench(SkShader::TileMode tilemode, int count) { |
| + count = std::max(0, std::min(count, kMaxCount)); |
|
bsalomon
2016/07/07 18:36:32
Not sure I understand the purpose of kMaxCount.
|
| + |
| + fName.printf("hardstop_%s_%02d_colors", get_tilemode_name(tilemode), count); |
| + |
| + fTileMode = tilemode; |
| + fColorCount = count; |
| + |
| + // "Evenly spaced" colors |
|
bsalomon
2016/07/07 18:36:32
Could from here onward and createShaderPaint be do
|
| + for (int i = 0; i < count; i++) { |
| + fColors[i] = i * (0xffffffff / count); |
| + } |
| + |
| + // Create a hard stop |
| + fPositions[0] = 0.0f; |
| + fPositions[1] = 0.0f; |
| + for (int i = 2; i < count; i++) { |
| + // Evenly spaced afterwards |
| + fPositions[i] = i / (count - 1.0f); |
| + } |
| + } |
| + |
| + const char* onGetName() override { |
| + return fName.c_str(); |
| + } |
| + |
| + SkIPoint onGetSize() override { |
| + return SkIPoint::Make(kSize, kSize); |
| + } |
| + |
| + /* |
| + * Draw simple linear gradient from left to right |
| + */ |
| + void onDraw(int loops, SkCanvas* canvas) override { |
| + SkPoint points[2] = { |
| + SkPoint::Make(0, kSize/2), |
| + SkPoint::Make(kSize-1, kSize/2), |
| + }; |
| + |
| + SkPaint paint = this->createShaderPaint(points); |
| + |
| + for (int i = 0; i < loops; i++) { |
| + canvas->drawPaint(paint); |
| + } |
| + } |
| + |
| +private: |
| + static const int kSize = 500; |
| + |
| + SkShader::TileMode fTileMode; |
| + SkString fName; |
| + int fColorCount; |
| + SkScalar fPositions[kMaxCount]; |
| + SkColor fColors[kMaxCount]; |
| + |
| + SkPaint createShaderPaint(SkPoint points[2]) { |
| + SkPaint p; |
| + p.setShader(SkGradientShader::MakeLinear(points, |
| + fColors, |
| + fPositions, |
| + fColorCount, |
| + fTileMode, |
| + 0, |
| + nullptr)); |
| + return p; |
| + } |
| + |
| + typedef Benchmark INHERITED; |
| +}; |
| + |
| +// Clamp |
| +DEF_BENCH(return new HardStopGradientBench(SkShader::kClamp_TileMode, kMaxCount/10);) |
| +DEF_BENCH(return new HardStopGradientBench(SkShader::kClamp_TileMode, kMaxCount/5);) |
| +DEF_BENCH(return new HardStopGradientBench(SkShader::kClamp_TileMode, kMaxCount/2);) |
| +DEF_BENCH(return new HardStopGradientBench(SkShader::kClamp_TileMode, kMaxCount);) |
| + |
| +// Repeat |
| +DEF_BENCH(return new HardStopGradientBench(SkShader::kRepeat_TileMode, kMaxCount/10);) |
| +DEF_BENCH(return new HardStopGradientBench(SkShader::kRepeat_TileMode, kMaxCount/5);) |
| +DEF_BENCH(return new HardStopGradientBench(SkShader::kRepeat_TileMode, kMaxCount/2);) |
| +DEF_BENCH(return new HardStopGradientBench(SkShader::kRepeat_TileMode, kMaxCount);) |
| + |
| +// Mirror |
| +DEF_BENCH(return new HardStopGradientBench(SkShader::kMirror_TileMode, kMaxCount/10);) |
| +DEF_BENCH(return new HardStopGradientBench(SkShader::kMirror_TileMode, kMaxCount/5);) |
| +DEF_BENCH(return new HardStopGradientBench(SkShader::kMirror_TileMode, kMaxCount/2);) |
| +DEF_BENCH(return new HardStopGradientBench(SkShader::kMirror_TileMode, kMaxCount);) |