Debian Codegraph @

bench/ChartBench.cpp

raw
1 /*
2 * Copyright 2013 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 #include "include/core/SkCanvas.h"
10 #include "include/core/SkPaint.h"
11 #include "include/core/SkPath.h"
12 #include "include/core/SkPathBuilder.h"
13 #include "include/private/base/SkTDArray.h"
14 #include "src/base/SkRandom.h"
15
16 /**
17 * This is a conversion of samplecode/SampleChart.cpp into a bench. It sure would be nice to be able
18 * to write one subclass that can be a GM, bench, and/or Sample.
19 */
20
21 // Generates y values for the chart plots.
22 static void gen_data(SkScalar yAvg, SkScalar ySpread, int count,
23 SkRandom* random, SkTDArray<SkScalar>* dataPts) {
24 dataPts->resize(count);
25 for (int i = 0; i < count; ++i) {
26 (*dataPts)[i] = random->nextRangeScalar(yAvg - SkScalarHalf(ySpread),
27 yAvg + SkScalarHalf(ySpread));
28 }
29 }
30
31 // Generates a path to stroke along the top of each plot and a fill path for the area below each
32 // plot. The fill path is bounded below by the bottomData plot points or a horizontal line at
33 // yBase if bottomData == nullptr.
34 // The plots are animated by rotating the data points by leftShift.
35 static void gen_paths(const SkTDArray<SkScalar>& topData,
36 const SkTDArray<SkScalar>* bottomData,
37 SkScalar yBase,
38 SkScalar xLeft, SkScalar xDelta,
39 int leftShift,
40 SkPath* plotOut, SkPath* fillOut) {
41 SkPathBuilder plot, fill;
42 plot.incReserve(topData.size());
43 if (nullptr == bottomData) {
44 fill.incReserve(topData.size() + 2);
45 } else {
46 fill.incReserve(2 * topData.size());
47 }
48
49 leftShift %= topData.size();
50 SkScalar x = xLeft;
51
52 // Account for the leftShift using two loops
53 int shiftToEndCount = topData.size() - leftShift;
54 plot.moveTo(x, topData[leftShift]);
55 fill.moveTo(x, topData[leftShift]);
56
57 for (int i = 1; i < shiftToEndCount; ++i) {
58 plot.lineTo(x, topData[i + leftShift]);
59 fill.lineTo(x, topData[i + leftShift]);
60 x += xDelta;
61 }
62
63 for (int i = 0; i < leftShift; ++i) {
64 plot.lineTo(x, topData[i]);
65 fill.lineTo(x, topData[i]);
66 x += xDelta;
67 }
68
69 if (bottomData) {
70 SkASSERT(bottomData->size() == topData.size());
71 // iterate backwards over the previous graph's data to generate the bottom of the filled
72 // area (and account for leftShift).
73 for (int i = 0; i < leftShift; ++i) {
74 x -= xDelta;
75 fill.lineTo(x, (*bottomData)[leftShift - 1 - i]);
76 }
77 for (int i = 0; i < shiftToEndCount; ++i) {
78 x -= xDelta;
79 fill.lineTo(x, (*bottomData)[bottomData->size() - 1 - i]);
80 }
81 } else {
82 fill.lineTo(x - xDelta, yBase);
83 fill.lineTo(xLeft, yBase);
84 }
85
86 *plotOut = plot.detach();
87 *fillOut = fill.detach();
88 }
89
90 // A set of scrolling line plots with the area between each plot filled. Stresses out GPU path
91 // filling
92 class ChartBench : public Benchmark {
93 public:
94 ChartBench(bool aa) {
95 fShift = 0;
96 fAA = aa;
97 fSize.fWidth = -1;
98 fSize.fHeight = -1;
99 }
100
101 protected:
102 const char* onGetName() override {
103 if (fAA) {
104 return "chart_aa";
105 } else {
106 return "chart_bw";
107 }
108 }
109
110 void onDraw(int loops, SkCanvas* canvas) override {
111 bool sizeChanged = false;
112 if (canvas->getBaseLayerSize() != fSize) {
113 fSize = canvas->getBaseLayerSize();
114 sizeChanged = true;
115 }
116
117 SkScalar ySpread = SkIntToScalar(fSize.fHeight / 20);
118
119 SkScalar height = SkIntToScalar(fSize.fHeight);
120 if (sizeChanged) {
121 int dataPointCount = std::max(fSize.fWidth / kPixelsPerTick + 1, 2);
122
123 SkRandom random;
124 for (int i = 0; i < kNumGraphs; ++i) {
125 SkScalar y = (kNumGraphs - i) * (height - ySpread) / (kNumGraphs + 1);
126 fData[i].reset();
127 gen_data(y, ySpread, dataPointCount, &random, fData + i);
128 }
129 }
130
131 SkRandom colorRand;
132 SkColor colors[kNumGraphs];
133 for (int i = 0; i < kNumGraphs; ++i) {
134 colors[i] = colorRand.nextU() | 0xff000000;
135 }
136
137 for (int frame = 0; frame < loops; ++frame) {
138 SkPath plotPath;
139 SkPath fillPath;
140
141 static const SkScalar kStrokeWidth = SkIntToScalar(2);
142 SkPaint plotPaint;
143 SkPaint fillPaint;
144 plotPaint.setAntiAlias(fAA);
145 plotPaint.setStyle(SkPaint::kStroke_Style);
146 plotPaint.setStrokeWidth(kStrokeWidth);
147 plotPaint.setStrokeCap(SkPaint::kRound_Cap);
148 plotPaint.setStrokeJoin(SkPaint::kRound_Join);
149 fillPaint.setAntiAlias(fAA);
150 fillPaint.setStyle(SkPaint::kFill_Style);
151
152 SkTDArray<SkScalar>* prevData = nullptr;
153 for (int i = 0; i < kNumGraphs; ++i) {
154 gen_paths(fData[i],
155 prevData,
156 height,
157 0,
158 SkIntToScalar(kPixelsPerTick),
159 fShift,
160 &plotPath,
161 &fillPath);
162
163 // Make the fills partially transparent
164 fillPaint.setColor((colors[i] & 0x00ffffff) | 0x80000000);
165 canvas->drawPath(fillPath, fillPaint);
166
167 plotPaint.setColor(colors[i]);
168 canvas->drawPath(plotPath, plotPaint);
169
170 prevData = fData + i;
171 }
172
173 fShift += kShiftPerFrame;
174 }
175 }
176
177 private:
178 enum {
179 kNumGraphs = 5,
180 kPixelsPerTick = 3,
181 kShiftPerFrame = 1,
182 };
183 int fShift;
184 SkISize fSize;
185 SkTDArray<SkScalar> fData[kNumGraphs];
186 bool fAA;
187
188 using INHERITED = Benchmark;
189 };
190
191 //////////////////////////////////////////////////////////////////////////////
192
193 DEF_BENCH( return new ChartBench(true); )
194 DEF_BENCH( return new ChartBench(false); )