blob: 0cc02b0a860debbb7b12ddbdfbe5ea398a71558d [file] [log] [blame]
bsalomon@google.com4a719972013-02-22 15:10:36 +00001/*
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
tfarinaf168b862014-06-19 12:32:29 -07008#include "Benchmark.h"
bsalomon@google.com4a719972013-02-22 15:10:36 +00009#include "SkCanvas.h"
10#include "SkPaint.h"
11#include "SkRandom.h"
12
13/**
14 * This is a conversion of samplecode/SampleChart.cpp into a bench. It sure would be nice to be able
15 * to write one subclass that can be a GM, bench, and/or Sample.
16 */
17
bsalomon@google.com4a719972013-02-22 15:10:36 +000018// Generates y values for the chart plots.
commit-bot@chromium.org8a2151f2014-05-08 15:12:43 +000019static void gen_data(SkScalar yAvg, SkScalar ySpread, int count,
20 SkRandom* random, SkTDArray<SkScalar>* dataPts) {
bsalomon@google.com4a719972013-02-22 15:10:36 +000021 dataPts->setCount(count);
bsalomon@google.com4a719972013-02-22 15:10:36 +000022 for (int i = 0; i < count; ++i) {
commit-bot@chromium.org8a2151f2014-05-08 15:12:43 +000023 (*dataPts)[i] = random->nextRangeScalar(yAvg - SkScalarHalf(ySpread),
bsalomon@google.com4a719972013-02-22 15:10:36 +000024 yAvg + SkScalarHalf(ySpread));
25 }
26}
27
28// Generates a path to stroke along the top of each plot and a fill path for the area below each
29// plot. The fill path is bounded below by the bottomData plot points or a horizontal line at
30// yBase if bottomData == NULL.
31// The plots are animated by rotating the data points by leftShift.
commit-bot@chromium.org97b4b672013-09-26 19:23:03 +000032static void gen_paths(const SkTDArray<SkScalar>& topData,
33 const SkTDArray<SkScalar>* bottomData,
34 SkScalar yBase,
35 SkScalar xLeft, SkScalar xDelta,
36 int leftShift,
37 SkPath* plot, SkPath* fill) {
bsalomon@google.com4a719972013-02-22 15:10:36 +000038 plot->rewind();
39 fill->rewind();
40 plot->incReserve(topData.count());
41 if (NULL == bottomData) {
42 fill->incReserve(topData.count() + 2);
43 } else {
44 fill->incReserve(2 * topData.count());
45 }
46
47 leftShift %= topData.count();
48 SkScalar x = xLeft;
49
50 // Account for the leftShift using two loops
51 int shiftToEndCount = topData.count() - leftShift;
52 plot->moveTo(x, topData[leftShift]);
53 fill->moveTo(x, topData[leftShift]);
54
55 for (int i = 1; i < shiftToEndCount; ++i) {
56 plot->lineTo(x, topData[i + leftShift]);
57 fill->lineTo(x, topData[i + leftShift]);
58 x += xDelta;
59 }
60
61 for (int i = 0; i < leftShift; ++i) {
62 plot->lineTo(x, topData[i]);
63 fill->lineTo(x, topData[i]);
64 x += xDelta;
65 }
66
bsalomon49f085d2014-09-05 13:34:00 -070067 if (bottomData) {
bsalomon@google.com4a719972013-02-22 15:10:36 +000068 SkASSERT(bottomData->count() == topData.count());
69 // iterate backwards over the previous graph's data to generate the bottom of the filled
70 // area (and account for leftShift).
71 for (int i = 0; i < leftShift; ++i) {
72 x -= xDelta;
73 fill->lineTo(x, (*bottomData)[leftShift - 1 - i]);
74 }
75 for (int i = 0; i < shiftToEndCount; ++i) {
76 x -= xDelta;
77 fill->lineTo(x, (*bottomData)[bottomData->count() - 1 - i]);
78 }
79 } else {
80 fill->lineTo(x - xDelta, yBase);
81 fill->lineTo(xLeft, yBase);
82 }
83}
84
bsalomon@google.com4a719972013-02-22 15:10:36 +000085// A set of scrolling line plots with the area between each plot filled. Stresses out GPU path
86// filling
tfarinaf168b862014-06-19 12:32:29 -070087class ChartBench : public Benchmark {
bsalomon@google.com4a719972013-02-22 15:10:36 +000088public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000089 ChartBench(bool aa) {
bsalomon@google.com4a719972013-02-22 15:10:36 +000090 fShift = 0;
91 fAA = aa;
bsalomon@google.comcd7421b2013-02-22 16:07:59 +000092 fSize.fWidth = -1;
93 fSize.fHeight = -1;
bsalomon@google.com4a719972013-02-22 15:10:36 +000094 }
95
96protected:
mtklein36352bf2015-03-25 18:17:31 -070097 const char* onGetName() override {
bsalomon@google.com4a719972013-02-22 15:10:36 +000098 if (fAA) {
99 return "chart_aa";
100 } else {
101 return "chart_bw";
102 }
103 }
104
mtklein36352bf2015-03-25 18:17:31 -0700105 void onDraw(const int loops, SkCanvas* canvas) override {
bsalomon@google.com4a719972013-02-22 15:10:36 +0000106 bool sizeChanged = false;
107 if (canvas->getDeviceSize() != fSize) {
108 fSize = canvas->getDeviceSize();
109 sizeChanged = true;
110 }
111
112 SkScalar ySpread = SkIntToScalar(fSize.fHeight / 20);
113
114 SkScalar height = SkIntToScalar(fSize.fHeight);
bsalomon@google.comcd7421b2013-02-22 16:07:59 +0000115 if (sizeChanged) {
116 int dataPointCount = SkMax32(fSize.fWidth / kPixelsPerTick + 1, 2);
117
commit-bot@chromium.org8a2151f2014-05-08 15:12:43 +0000118 SkRandom random;
bsalomon@google.comcd7421b2013-02-22 16:07:59 +0000119 for (int i = 0; i < kNumGraphs; ++i) {
120 SkScalar y = (kNumGraphs - i) * (height - ySpread) / (kNumGraphs + 1);
121 fData[i].reset();
commit-bot@chromium.org8a2151f2014-05-08 15:12:43 +0000122 gen_data(y, ySpread, dataPointCount, &random, fData + i);
bsalomon@google.comcd7421b2013-02-22 16:07:59 +0000123 }
124 }
bsalomon@google.com4a719972013-02-22 15:10:36 +0000125
commit-bot@chromium.org8a2151f2014-05-08 15:12:43 +0000126 SkRandom colorRand;
127 SkColor colors[kNumGraphs];
128 for (int i = 0; i < kNumGraphs; ++i) {
129 colors[i] = colorRand.nextU() | 0xff000000;
130 }
131
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000132 for (int frame = 0; frame < loops; ++frame) {
bsalomon@google.com4a719972013-02-22 15:10:36 +0000133
134 canvas->clear(0xFFE0F0E0);
135
bsalomon@google.com4a719972013-02-22 15:10:36 +0000136 SkPath plotPath;
137 SkPath fillPath;
138
139 static const SkScalar kStrokeWidth = SkIntToScalar(2);
140 SkPaint plotPaint;
141 SkPaint fillPaint;
142 plotPaint.setAntiAlias(fAA);
143 plotPaint.setStyle(SkPaint::kStroke_Style);
144 plotPaint.setStrokeWidth(kStrokeWidth);
145 plotPaint.setStrokeCap(SkPaint::kRound_Cap);
146 plotPaint.setStrokeJoin(SkPaint::kRound_Join);
147 fillPaint.setAntiAlias(fAA);
148 fillPaint.setStyle(SkPaint::kFill_Style);
149
150 SkTDArray<SkScalar>* prevData = NULL;
151 for (int i = 0; i < kNumGraphs; ++i) {
152 gen_paths(fData[i],
153 prevData,
154 height,
155 0,
156 SkIntToScalar(kPixelsPerTick),
157 fShift,
158 &plotPath,
159 &fillPath);
160
161 // Make the fills partially transparent
commit-bot@chromium.org8a2151f2014-05-08 15:12:43 +0000162 fillPaint.setColor((colors[i] & 0x00ffffff) | 0x80000000);
bsalomon@google.com4a719972013-02-22 15:10:36 +0000163 canvas->drawPath(fillPath, fillPaint);
164
commit-bot@chromium.org8a2151f2014-05-08 15:12:43 +0000165 plotPaint.setColor(colors[i]);
bsalomon@google.com4a719972013-02-22 15:10:36 +0000166 canvas->drawPath(plotPath, plotPaint);
167
168 prevData = fData + i;
169 }
170
171 fShift += kShiftPerFrame;
172 }
173 }
174
175private:
176 enum {
177 kNumGraphs = 5,
178 kPixelsPerTick = 3,
179 kShiftPerFrame = 1,
bsalomon@google.com4a719972013-02-22 15:10:36 +0000180 };
181 int fShift;
182 SkISize fSize;
183 SkTDArray<SkScalar> fData[kNumGraphs];
184 bool fAA;
185
tfarinaf168b862014-06-19 12:32:29 -0700186 typedef Benchmark INHERITED;
bsalomon@google.com4a719972013-02-22 15:10:36 +0000187};
188
189//////////////////////////////////////////////////////////////////////////////
190
mtklein@google.com410e6e82013-09-13 19:52:27 +0000191DEF_BENCH( return new ChartBench(true); )
192DEF_BENCH( return new ChartBench(false); )