• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/SkGradientShader.h"
16 
17 #include "tools/ToolUtils.h"
18 
19 class HardStopGradientBench_ScaleNumColors : public Benchmark {
20 public:
HardStopGradientBench_ScaleNumColors(SkTileMode tilemode,int count)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 
onGetName()29     const char* onGetName() override {
30         return fName.c_str();
31     }
32 
onGetSize()33     SkIPoint onGetSize() override {
34         return SkIPoint::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      */
onPreDraw(SkCanvas * canvas)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         SkColor color_choices[kNumColorChoices] = {
53             SK_ColorRED,
54             SK_ColorGREEN,
55             SK_ColorBLUE,
56             SK_ColorYELLOW,
57         };
58 
59         // Alternate between different choices
60         SkColor  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         SkScalar 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(SkGradientShader::MakeLinear(points,
75                                                       colors,
76                                                       positions,
77                                                       fColorCount,
78                                                       fTileMode,
79                                                       0,
80                                                       nullptr));
81     }
82 
83     /*
84      * Draw simple linear gradient from left to right
85      */
onDraw(int loops,SkCanvas * canvas)86     void onDraw(int loops, SkCanvas* canvas) override {
87         for (int i = 0; i < loops; i++) {
88             canvas->drawPaint(fPaint);
89         }
90     }
91 
92 private:
93     static const int kSize = 500;
94 
95     SkTileMode  fTileMode;
96     SkString    fName;
97     int         fColorCount;
98     SkPaint     fPaint;
99 
100     using INHERITED = Benchmark;
101 };
102 
103 // Clamp
104 DEF_BENCH(return new HardStopGradientBench_ScaleNumColors(SkTileMode::kClamp,   3);)
105 DEF_BENCH(return new HardStopGradientBench_ScaleNumColors(SkTileMode::kClamp,   4);)
106 DEF_BENCH(return new HardStopGradientBench_ScaleNumColors(SkTileMode::kClamp,   5);)
107 DEF_BENCH(return new HardStopGradientBench_ScaleNumColors(SkTileMode::kClamp,  10);)
108 DEF_BENCH(return new HardStopGradientBench_ScaleNumColors(SkTileMode::kClamp,  25);)
109 DEF_BENCH(return new HardStopGradientBench_ScaleNumColors(SkTileMode::kClamp,  50);)
110 DEF_BENCH(return new HardStopGradientBench_ScaleNumColors(SkTileMode::kClamp, 100);)
111 
112 // Repeat
113 DEF_BENCH(return new HardStopGradientBench_ScaleNumColors(SkTileMode::kRepeat,   3);)
114 DEF_BENCH(return new HardStopGradientBench_ScaleNumColors(SkTileMode::kRepeat,   4);)
115 DEF_BENCH(return new HardStopGradientBench_ScaleNumColors(SkTileMode::kRepeat,   5);)
116 DEF_BENCH(return new HardStopGradientBench_ScaleNumColors(SkTileMode::kRepeat,  10);)
117 DEF_BENCH(return new HardStopGradientBench_ScaleNumColors(SkTileMode::kRepeat,  25);)
118 DEF_BENCH(return new HardStopGradientBench_ScaleNumColors(SkTileMode::kRepeat,  50);)
119 DEF_BENCH(return new HardStopGradientBench_ScaleNumColors(SkTileMode::kRepeat, 100);)
120 
121 // Mirror
122 DEF_BENCH(return new HardStopGradientBench_ScaleNumColors(SkTileMode::kMirror,   3);)
123 DEF_BENCH(return new HardStopGradientBench_ScaleNumColors(SkTileMode::kMirror,   4);)
124 DEF_BENCH(return new HardStopGradientBench_ScaleNumColors(SkTileMode::kMirror,   5);)
125 DEF_BENCH(return new HardStopGradientBench_ScaleNumColors(SkTileMode::kMirror,  10);)
126 DEF_BENCH(return new HardStopGradientBench_ScaleNumColors(SkTileMode::kMirror,  25);)
127 DEF_BENCH(return new HardStopGradientBench_ScaleNumColors(SkTileMode::kMirror,  50);)
128 DEF_BENCH(return new HardStopGradientBench_ScaleNumColors(SkTileMode::kMirror, 100);)
129