• 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 #include "include/private/SkTemplates.h"
17 
18 class HardStopGradientBench_ScaleNumHardStops : public Benchmark {
19 public:
HardStopGradientBench_ScaleNumHardStops(int colorCount,int hardStopCount)20     HardStopGradientBench_ScaleNumHardStops(int colorCount, int hardStopCount) {
21         SkASSERT(hardStopCount <= colorCount/2);
22 
23         fName.printf("hardstop_scale_num_hard_stops_%03d_colors_%03d_hard_stops",
24                      colorCount, hardStopCount);
25 
26         fColorCount    = colorCount;
27         fHardStopCount = hardStopCount;
28     }
29 
onGetName()30     const char* onGetName() override {
31         return fName.c_str();
32     }
33 
onGetSize()34     SkIPoint onGetSize() override {
35         return SkIPoint::Make(kSize, kSize);
36     }
37 
onPreDraw(SkCanvas * canvas)38     void onPreDraw(SkCanvas* canvas) override {
39         // Left to right
40         SkPoint points[2] = {
41             SkPoint::Make(0,        kSize/2),
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,
76                                                       SkTileMode::kClamp,
77                                                       0,
78                                                       nullptr));
79     }
80 
81     /*
82      * Draw simple linear gradient from left to right
83      */
onDraw(int loops,SkCanvas * canvas)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 
98     using INHERITED = Benchmark;
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);)
116