HardStopGradientBench_ScaleNumHardStops.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #include "bench/Benchmark.h"
  8. #include "include/core/SkCanvas.h"
  9. #include "include/core/SkColor.h"
  10. #include "include/core/SkPaint.h"
  11. #include "include/core/SkShader.h"
  12. #include "include/core/SkString.h"
  13. #include "include/effects/SkGradientShader.h"
  14. class HardStopGradientBench_ScaleNumHardStops : public Benchmark {
  15. public:
  16. HardStopGradientBench_ScaleNumHardStops(int colorCount, int hardStopCount) {
  17. SkASSERT(hardStopCount <= colorCount/2);
  18. fName.printf("hardstop_scale_num_hard_stops_%03d_colors_%03d_hard_stops",
  19. colorCount, hardStopCount);
  20. fColorCount = colorCount;
  21. fHardStopCount = hardStopCount;
  22. }
  23. const char* onGetName() override {
  24. return fName.c_str();
  25. }
  26. SkIPoint onGetSize() override {
  27. return SkIPoint::Make(kSize, kSize);
  28. }
  29. void onPreDraw(SkCanvas* canvas) override {
  30. // Left to right
  31. SkPoint points[2] = {
  32. SkPoint::Make(0, kSize/2),
  33. SkPoint::Make(kSize-1, kSize/2),
  34. };
  35. constexpr int kNumColorChoices = 4;
  36. SkColor color_choices[kNumColorChoices] = {
  37. SK_ColorRED,
  38. SK_ColorGREEN,
  39. SK_ColorBLUE,
  40. SK_ColorYELLOW,
  41. };
  42. // Alternate between different choices
  43. SkAutoTArray<SkColor> colors(fColorCount);
  44. for (int i = 0; i < fColorCount; i++) {
  45. colors[i] = color_choices[i % kNumColorChoices];
  46. }
  47. // Create requisite number of hard stops, and evenly
  48. // space positions after that
  49. SkAutoTArray<SkScalar> positions(fColorCount);
  50. int k = 0;
  51. for (int i = 0; i < fHardStopCount; i++) {
  52. float val = k/2.0f;
  53. positions[k++] = val / fColorCount;
  54. positions[k++] = val / fColorCount;
  55. }
  56. for (int i = k; i < fColorCount; i++) {
  57. positions[i] = i / (fColorCount - 1.0f);
  58. }
  59. fPaint.setShader(SkGradientShader::MakeLinear(points,
  60. colors.get(),
  61. positions.get(),
  62. fColorCount,
  63. SkTileMode::kClamp,
  64. 0,
  65. nullptr));
  66. }
  67. /*
  68. * Draw simple linear gradient from left to right
  69. */
  70. void onDraw(int loops, SkCanvas* canvas) override {
  71. for (int i = 0; i < loops; i++) {
  72. canvas->drawPaint(fPaint);
  73. }
  74. }
  75. private:
  76. static const int kSize = 500;
  77. SkString fName;
  78. int fColorCount;
  79. int fHardStopCount;
  80. SkPaint fPaint;
  81. typedef Benchmark INHERITED;
  82. };
  83. DEF_BENCH(return new HardStopGradientBench_ScaleNumHardStops(10, 1);)
  84. DEF_BENCH(return new HardStopGradientBench_ScaleNumHardStops(10, 2);)
  85. DEF_BENCH(return new HardStopGradientBench_ScaleNumHardStops(10, 5);)
  86. DEF_BENCH(return new HardStopGradientBench_ScaleNumHardStops(20, 1);)
  87. DEF_BENCH(return new HardStopGradientBench_ScaleNumHardStops(20, 5);)
  88. DEF_BENCH(return new HardStopGradientBench_ScaleNumHardStops(20, 10);)
  89. DEF_BENCH(return new HardStopGradientBench_ScaleNumHardStops(50, 1);)
  90. DEF_BENCH(return new HardStopGradientBench_ScaleNumHardStops(50, 10);)
  91. DEF_BENCH(return new HardStopGradientBench_ScaleNumHardStops(50, 25);)
  92. DEF_BENCH(return new HardStopGradientBench_ScaleNumHardStops(100, 1);)
  93. DEF_BENCH(return new HardStopGradientBench_ScaleNumHardStops(100, 25);)
  94. DEF_BENCH(return new HardStopGradientBench_ScaleNumHardStops(100, 50);)