blob: c5a3a5b3ad05b104fa205b1b0231d11d16c31396 [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"
bungemand3ebb482015-08-05 13:57:49 -070011#include "SkPath.h"
bsalomon@google.com4a719972013-02-22 15:10:36 +000012#include "SkRandom.h"
bungemand3ebb482015-08-05 13:57:49 -070013#include "SkTDArray.h"
bsalomon@google.com4a719972013-02-22 15:10:36 +000014
15/**
16 * This is a conversion of samplecode/SampleChart.cpp into a bench. It sure would be nice to be able
17 * to write one subclass that can be a GM, bench, and/or Sample.
18 */
19
bsalomon@google.com4a719972013-02-22 15:10:36 +000020// Generates y values for the chart plots.
commit-bot@chromium.org8a2151f2014-05-08 15:12:43 +000021static void gen_data(SkScalar yAvg, SkScalar ySpread, int count,
22 SkRandom* random, SkTDArray<SkScalar>* dataPts) {
bsalomon@google.com4a719972013-02-22 15:10:36 +000023 dataPts->setCount(count);
bsalomon@google.com4a719972013-02-22 15:10:36 +000024 for (int i = 0; i < count; ++i) {
commit-bot@chromium.org8a2151f2014-05-08 15:12:43 +000025 (*dataPts)[i] = random->nextRangeScalar(yAvg - SkScalarHalf(ySpread),
bsalomon@google.com4a719972013-02-22 15:10:36 +000026 yAvg + SkScalarHalf(ySpread));
27 }
28}
29
30// Generates a path to stroke along the top of each plot and a fill path for the area below each
31// plot. The fill path is bounded below by the bottomData plot points or a horizontal line at
halcanary96fcdcc2015-08-27 07:41:13 -070032// yBase if bottomData == nullptr.
bsalomon@google.com4a719972013-02-22 15:10:36 +000033// The plots are animated by rotating the data points by leftShift.
commit-bot@chromium.org97b4b672013-09-26 19:23:03 +000034static void gen_paths(const SkTDArray<SkScalar>& topData,
35 const SkTDArray<SkScalar>* bottomData,
36 SkScalar yBase,
37 SkScalar xLeft, SkScalar xDelta,
38 int leftShift,
39 SkPath* plot, SkPath* fill) {
bsalomon@google.com4a719972013-02-22 15:10:36 +000040 plot->rewind();
41 fill->rewind();
42 plot->incReserve(topData.count());
halcanary96fcdcc2015-08-27 07:41:13 -070043 if (nullptr == bottomData) {
bsalomon@google.com4a719972013-02-22 15:10:36 +000044 fill->incReserve(topData.count() + 2);
45 } else {
46 fill->incReserve(2 * topData.count());
47 }
48
49 leftShift %= topData.count();
50 SkScalar x = xLeft;
51
52 // Account for the leftShift using two loops
53 int shiftToEndCount = topData.count() - 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
bsalomon49f085d2014-09-05 13:34:00 -070069 if (bottomData) {
bsalomon@google.com4a719972013-02-22 15:10:36 +000070 SkASSERT(bottomData->count() == topData.count());
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->count() - 1 - i]);
80 }
81 } else {
82 fill->lineTo(x - xDelta, yBase);
83 fill->lineTo(xLeft, yBase);
84 }
85}
86
bsalomon@google.com4a719972013-02-22 15:10:36 +000087// A set of scrolling line plots with the area between each plot filled. Stresses out GPU path
88// filling
tfarinaf168b862014-06-19 12:32:29 -070089class ChartBench : public Benchmark {
bsalomon@google.com4a719972013-02-22 15:10:36 +000090public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000091 ChartBench(bool aa) {
bsalomon@google.com4a719972013-02-22 15:10:36 +000092 fShift = 0;
93 fAA = aa;
bsalomon@google.comcd7421b2013-02-22 16:07:59 +000094 fSize.fWidth = -1;
95 fSize.fHeight = -1;
bsalomon@google.com4a719972013-02-22 15:10:36 +000096 }
97
98protected:
mtklein36352bf2015-03-25 18:17:31 -070099 const char* onGetName() override {
bsalomon@google.com4a719972013-02-22 15:10:36 +0000100 if (fAA) {
101 return "chart_aa";
102 } else {
103 return "chart_bw";
104 }
105 }
106
mtkleina1ebeb22015-10-01 09:43:39 -0700107 void onDraw(int loops, SkCanvas* canvas) override {
bsalomon@google.com4a719972013-02-22 15:10:36 +0000108 bool sizeChanged = false;
109 if (canvas->getDeviceSize() != fSize) {
110 fSize = canvas->getDeviceSize();
111 sizeChanged = true;
112 }
113
114 SkScalar ySpread = SkIntToScalar(fSize.fHeight / 20);
115
116 SkScalar height = SkIntToScalar(fSize.fHeight);
bsalomon@google.comcd7421b2013-02-22 16:07:59 +0000117 if (sizeChanged) {
118 int dataPointCount = SkMax32(fSize.fWidth / kPixelsPerTick + 1, 2);
119
commit-bot@chromium.org8a2151f2014-05-08 15:12:43 +0000120 SkRandom random;
bsalomon@google.comcd7421b2013-02-22 16:07:59 +0000121 for (int i = 0; i < kNumGraphs; ++i) {
122 SkScalar y = (kNumGraphs - i) * (height - ySpread) / (kNumGraphs + 1);
123 fData[i].reset();
commit-bot@chromium.org8a2151f2014-05-08 15:12:43 +0000124 gen_data(y, ySpread, dataPointCount, &random, fData + i);
bsalomon@google.comcd7421b2013-02-22 16:07:59 +0000125 }
126 }
bsalomon@google.com4a719972013-02-22 15:10:36 +0000127
commit-bot@chromium.org8a2151f2014-05-08 15:12:43 +0000128 SkRandom colorRand;
129 SkColor colors[kNumGraphs];
130 for (int i = 0; i < kNumGraphs; ++i) {
131 colors[i] = colorRand.nextU() | 0xff000000;
132 }
133
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000134 for (int frame = 0; frame < loops; ++frame) {
bsalomon@google.com4a719972013-02-22 15:10:36 +0000135
136 canvas->clear(0xFFE0F0E0);
137
bsalomon@google.com4a719972013-02-22 15:10:36 +0000138 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
halcanary96fcdcc2015-08-27 07:41:13 -0700152 SkTDArray<SkScalar>* prevData = nullptr;
bsalomon@google.com4a719972013-02-22 15:10:36 +0000153 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
commit-bot@chromium.org8a2151f2014-05-08 15:12:43 +0000164 fillPaint.setColor((colors[i] & 0x00ffffff) | 0x80000000);
bsalomon@google.com4a719972013-02-22 15:10:36 +0000165 canvas->drawPath(fillPath, fillPaint);
166
commit-bot@chromium.org8a2151f2014-05-08 15:12:43 +0000167 plotPaint.setColor(colors[i]);
bsalomon@google.com4a719972013-02-22 15:10:36 +0000168 canvas->drawPath(plotPath, plotPaint);
169
170 prevData = fData + i;
171 }
172
173 fShift += kShiftPerFrame;
174 }
175 }
176
177private:
178 enum {
179 kNumGraphs = 5,
180 kPixelsPerTick = 3,
181 kShiftPerFrame = 1,
bsalomon@google.com4a719972013-02-22 15:10:36 +0000182 };
183 int fShift;
184 SkISize fSize;
185 SkTDArray<SkScalar> fData[kNumGraphs];
186 bool fAA;
187
tfarinaf168b862014-06-19 12:32:29 -0700188 typedef Benchmark INHERITED;
bsalomon@google.com4a719972013-02-22 15:10:36 +0000189};
190
191//////////////////////////////////////////////////////////////////////////////
192
mtklein@google.com410e6e82013-09-13 19:52:27 +0000193DEF_BENCH( return new ChartBench(true); )
194DEF_BENCH( return new ChartBench(false); )