2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
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.
18 // Generates y values for the chart plots.
19 static void gen_data(SkScalar yAvg, SkScalar ySpread, int count,
20 SkRandom* random, SkTDArray<SkScalar>* dataPts) {
21 dataPts->setCount(count);
22 for (int i = 0; i < count; ++i) {
23 (*dataPts)[i] = random->nextRangeScalar(yAvg - SkScalarHalf(ySpread),
24 yAvg + SkScalarHalf(ySpread));
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.
32 static void gen_paths(const SkTDArray<SkScalar>& topData,
33 const SkTDArray<SkScalar>* bottomData,
35 SkScalar xLeft, SkScalar xDelta,
37 SkPath* plot, SkPath* fill) {
40 plot->incReserve(topData.count());
41 if (NULL == bottomData) {
42 fill->incReserve(topData.count() + 2);
44 fill->incReserve(2 * topData.count());
47 leftShift %= topData.count();
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]);
55 for (int i = 1; i < shiftToEndCount; ++i) {
56 plot->lineTo(x, topData[i + leftShift]);
57 fill->lineTo(x, topData[i + leftShift]);
61 for (int i = 0; i < leftShift; ++i) {
62 plot->lineTo(x, topData[i]);
63 fill->lineTo(x, topData[i]);
68 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) {
73 fill->lineTo(x, (*bottomData)[leftShift - 1 - i]);
75 for (int i = 0; i < shiftToEndCount; ++i) {
77 fill->lineTo(x, (*bottomData)[bottomData->count() - 1 - i]);
80 fill->lineTo(x - xDelta, yBase);
81 fill->lineTo(xLeft, yBase);
85 // A set of scrolling line plots with the area between each plot filled. Stresses out GPU path
87 class ChartBench : public Benchmark {
97 const char* onGetName() override {
105 void onDraw(const int loops, SkCanvas* canvas) override {
106 bool sizeChanged = false;
107 if (canvas->getDeviceSize() != fSize) {
108 fSize = canvas->getDeviceSize();
112 SkScalar ySpread = SkIntToScalar(fSize.fHeight / 20);
114 SkScalar height = SkIntToScalar(fSize.fHeight);
116 int dataPointCount = SkMax32(fSize.fWidth / kPixelsPerTick + 1, 2);
119 for (int i = 0; i < kNumGraphs; ++i) {
120 SkScalar y = (kNumGraphs - i) * (height - ySpread) / (kNumGraphs + 1);
122 gen_data(y, ySpread, dataPointCount, &random, fData + i);
127 SkColor colors[kNumGraphs];
128 for (int i = 0; i < kNumGraphs; ++i) {
129 colors[i] = colorRand.nextU() | 0xff000000;
132 for (int frame = 0; frame < loops; ++frame) {
134 canvas->clear(0xFFE0F0E0);
139 static const SkScalar kStrokeWidth = SkIntToScalar(2);
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);
150 SkTDArray<SkScalar>* prevData = NULL;
151 for (int i = 0; i < kNumGraphs; ++i) {
156 SkIntToScalar(kPixelsPerTick),
161 // Make the fills partially transparent
162 fillPaint.setColor((colors[i] & 0x00ffffff) | 0x80000000);
163 canvas->drawPath(fillPath, fillPaint);
165 plotPaint.setColor(colors[i]);
166 canvas->drawPath(plotPath, plotPaint);
168 prevData = fData + i;
171 fShift += kShiftPerFrame;
183 SkTDArray<SkScalar> fData[kNumGraphs];
186 typedef Benchmark INHERITED;
189 //////////////////////////////////////////////////////////////////////////////
191 DEF_BENCH( return new ChartBench(true); )
192 DEF_BENCH( return new ChartBench(false); )