blob: 6356dec86e18d4c34c3c7eb3048b0d88631cc79a [file] [log] [blame]
bsalomon@google.com9053c2e2013-02-13 21:22:13 +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 */
commit-bot@chromium.orgab131672013-09-13 12:39:55 +00007
Ben Wagnerb2c4ea62018-08-08 11:36:17 -04008#include "Sample.h"
bsalomon@google.com9053c2e2013-02-13 21:22:13 +00009#include "SkCanvas.h"
10#include "SkPaint.h"
bungemand3ebb482015-08-05 13:57:49 -070011#include "SkPath.h"
bsalomon@google.com9053c2e2013-02-13 21:22:13 +000012#include "SkRandom.h"
bsalomon@google.com9053c2e2013-02-13 21:22:13 +000013
bsalomon@google.com9053c2e2013-02-13 21:22:13 +000014// Generates y values for the chart plots.
commit-bot@chromium.orgab131672013-09-13 12:39:55 +000015static void gen_data(SkScalar yAvg, SkScalar ySpread, int count, SkTDArray<SkScalar>* dataPts) {
bsalomon@google.com9053c2e2013-02-13 21:22:13 +000016 dataPts->setCount(count);
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000017 static SkRandom gRandom;
bsalomon@google.com9053c2e2013-02-13 21:22:13 +000018 for (int i = 0; i < count; ++i) {
19 (*dataPts)[i] = gRandom.nextRangeScalar(yAvg - SkScalarHalf(ySpread),
20 yAvg + SkScalarHalf(ySpread));
21 }
22}
23
24// Generates a path to stroke along the top of each plot and a fill path for the area below each
25// plot. The fill path is bounded below by the bottomData plot points or a horizontal line at
halcanary96fcdcc2015-08-27 07:41:13 -070026// yBase if bottomData == nullptr.
bsalomon@google.com9053c2e2013-02-13 21:22:13 +000027// The plots are animated by rotating the data points by leftShift.
commit-bot@chromium.orgab131672013-09-13 12:39:55 +000028static void gen_paths(const SkTDArray<SkScalar>& topData,
29 const SkTDArray<SkScalar>* bottomData,
30 SkScalar yBase,
31 SkScalar xLeft, SkScalar xDelta,
32 int leftShift,
33 SkPath* plot, SkPath* fill) {
bsalomon@google.com9053c2e2013-02-13 21:22:13 +000034 plot->rewind();
35 fill->rewind();
36 plot->incReserve(topData.count());
halcanary96fcdcc2015-08-27 07:41:13 -070037 if (nullptr == bottomData) {
bsalomon@google.com9053c2e2013-02-13 21:22:13 +000038 fill->incReserve(topData.count() + 2);
39 } else {
40 fill->incReserve(2 * topData.count());
41 }
42
43 leftShift %= topData.count();
44 SkScalar x = xLeft;
45
46 // Account for the leftShift using two loops
47 int shiftToEndCount = topData.count() - leftShift;
48 plot->moveTo(x, topData[leftShift]);
49 fill->moveTo(x, topData[leftShift]);
50
51 for (int i = 1; i < shiftToEndCount; ++i) {
52 plot->lineTo(x, topData[i + leftShift]);
53 fill->lineTo(x, topData[i + leftShift]);
54 x += xDelta;
55 }
56
57 for (int i = 0; i < leftShift; ++i) {
58 plot->lineTo(x, topData[i]);
59 fill->lineTo(x, topData[i]);
60 x += xDelta;
61 }
62
bsalomon49f085d2014-09-05 13:34:00 -070063 if (bottomData) {
bsalomon@google.com9053c2e2013-02-13 21:22:13 +000064 SkASSERT(bottomData->count() == topData.count());
65 // iterate backwards over the previous graph's data to generate the bottom of the filled
66 // area (and account for leftShift).
67 for (int i = 0; i < leftShift; ++i) {
68 x -= xDelta;
69 fill->lineTo(x, (*bottomData)[leftShift - 1 - i]);
70 }
71 for (int i = 0; i < shiftToEndCount; ++i) {
72 x -= xDelta;
73 fill->lineTo(x, (*bottomData)[bottomData->count() - 1 - i]);
74 }
75 } else {
76 fill->lineTo(x - xDelta, yBase);
77 fill->lineTo(xLeft, yBase);
78 }
79}
80
bsalomon@google.com9053c2e2013-02-13 21:22:13 +000081// A set of scrolling line plots with the area between each plot filled. Stresses out GPU path
82// filling
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040083class ChartView : public Sample {
bsalomon@google.com9053c2e2013-02-13 21:22:13 +000084public:
85 ChartView() {
86 fShift = 0;
87 fSize.set(-1, -1);
88 }
89
90protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040091 bool onQuery(Sample::Event* evt) override {
92 if (Sample::TitleQ(*evt)) {
93 Sample::TitleR(evt, "Chart");
bsalomon@google.com9053c2e2013-02-13 21:22:13 +000094 return true;
95 }
96 return this->INHERITED::onQuery(evt);
97 }
98
mtklein36352bf2015-03-25 18:17:31 -070099 void onDrawContent(SkCanvas* canvas) override {
bsalomon@google.com9053c2e2013-02-13 21:22:13 +0000100 bool sizeChanged = false;
Mike Reed3661bc92017-02-22 13:21:42 -0500101 if (canvas->getBaseLayerSize() != fSize) {
102 fSize = canvas->getBaseLayerSize();
bsalomon@google.com9053c2e2013-02-13 21:22:13 +0000103 sizeChanged = true;
104 }
105
106 SkScalar ySpread = SkIntToScalar(fSize.fHeight / 20);
107
108 SkScalar height = SkIntToScalar(fSize.fHeight);
109
110 if (sizeChanged) {
111 int dataPointCount = SkMax32(fSize.fWidth / kPixelsPerTick + 1, 2);
112
113 for (int i = 0; i < kNumGraphs; ++i) {
114 SkScalar y = (kNumGraphs - i) * (height - ySpread) / (kNumGraphs + 1);
115 fData[i].reset();
116 gen_data(y, ySpread, dataPointCount, fData + i);
117 }
118 }
119
120 canvas->clear(0xFFE0F0E0);
121
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000122 static SkRandom colorRand;
bsalomon@google.com9053c2e2013-02-13 21:22:13 +0000123 static SkColor gColors[kNumGraphs] = { 0x0 };
124 if (0 == gColors[0]) {
125 for (int i = 0; i < kNumGraphs; ++i) {
126 gColors[i] = colorRand.nextU() | 0xff000000;
127 }
128 }
129
130 SkPath plotPath;
131 SkPath fillPath;
132
133 static const SkScalar kStrokeWidth = SkIntToScalar(2);
134 SkPaint plotPaint;
135 SkPaint fillPaint;
136 plotPaint.setAntiAlias(true);
137 plotPaint.setStyle(SkPaint::kStroke_Style);
138 plotPaint.setStrokeWidth(kStrokeWidth);
139 plotPaint.setStrokeCap(SkPaint::kRound_Cap);
140 plotPaint.setStrokeJoin(SkPaint::kRound_Join);
141 fillPaint.setAntiAlias(true);
142 fillPaint.setStyle(SkPaint::kFill_Style);
143
halcanary96fcdcc2015-08-27 07:41:13 -0700144 SkTDArray<SkScalar>* prevData = nullptr;
bsalomon@google.com9053c2e2013-02-13 21:22:13 +0000145 for (int i = 0; i < kNumGraphs; ++i) {
146 gen_paths(fData[i],
147 prevData,
148 height,
149 0,
150 SkIntToScalar(kPixelsPerTick),
151 fShift,
152 &plotPath,
153 &fillPath);
154
155 // Make the fills partially transparent
156 fillPaint.setColor((gColors[i] & 0x00ffffff) | 0x80000000);
157 canvas->drawPath(fillPath, fillPaint);
158
159 plotPaint.setColor(gColors[i]);
160 canvas->drawPath(plotPath, plotPaint);
161
162 prevData = fData + i;
163 }
164
165 fShift += kShiftPerFrame;
bsalomon@google.com9053c2e2013-02-13 21:22:13 +0000166 }
167
168private:
169 enum {
170 kNumGraphs = 5,
171 kPixelsPerTick = 3,
172 kShiftPerFrame = 1,
173 };
174 int fShift;
175 SkISize fSize;
176 SkTDArray<SkScalar> fData[kNumGraphs];
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400177 typedef Sample INHERITED;
bsalomon@google.com9053c2e2013-02-13 21:22:13 +0000178};
179
180//////////////////////////////////////////////////////////////////////////////
181
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400182DEF_SAMPLE( return new ChartView(); )