blob: 3e663ec4e22582ca0d66584c5029e1d6adb9ef13 [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
bsalomon@google.com9053c2e2013-02-13 21:22:13 +00008#include "SampleCode.h"
9#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"
13#include "SkView.h"
14
bsalomon@google.com9053c2e2013-02-13 21:22:13 +000015// Generates y values for the chart plots.
commit-bot@chromium.orgab131672013-09-13 12:39:55 +000016static void gen_data(SkScalar yAvg, SkScalar ySpread, int count, SkTDArray<SkScalar>* dataPts) {
bsalomon@google.com9053c2e2013-02-13 21:22:13 +000017 dataPts->setCount(count);
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000018 static SkRandom gRandom;
bsalomon@google.com9053c2e2013-02-13 21:22:13 +000019 for (int i = 0; i < count; ++i) {
20 (*dataPts)[i] = gRandom.nextRangeScalar(yAvg - SkScalarHalf(ySpread),
21 yAvg + SkScalarHalf(ySpread));
22 }
23}
24
25// Generates a path to stroke along the top of each plot and a fill path for the area below each
26// plot. The fill path is bounded below by the bottomData plot points or a horizontal line at
halcanary96fcdcc2015-08-27 07:41:13 -070027// yBase if bottomData == nullptr.
bsalomon@google.com9053c2e2013-02-13 21:22:13 +000028// The plots are animated by rotating the data points by leftShift.
commit-bot@chromium.orgab131672013-09-13 12:39:55 +000029static void gen_paths(const SkTDArray<SkScalar>& topData,
30 const SkTDArray<SkScalar>* bottomData,
31 SkScalar yBase,
32 SkScalar xLeft, SkScalar xDelta,
33 int leftShift,
34 SkPath* plot, SkPath* fill) {
bsalomon@google.com9053c2e2013-02-13 21:22:13 +000035 plot->rewind();
36 fill->rewind();
37 plot->incReserve(topData.count());
halcanary96fcdcc2015-08-27 07:41:13 -070038 if (nullptr == bottomData) {
bsalomon@google.com9053c2e2013-02-13 21:22:13 +000039 fill->incReserve(topData.count() + 2);
40 } else {
41 fill->incReserve(2 * topData.count());
42 }
43
44 leftShift %= topData.count();
45 SkScalar x = xLeft;
46
47 // Account for the leftShift using two loops
48 int shiftToEndCount = topData.count() - leftShift;
49 plot->moveTo(x, topData[leftShift]);
50 fill->moveTo(x, topData[leftShift]);
51
52 for (int i = 1; i < shiftToEndCount; ++i) {
53 plot->lineTo(x, topData[i + leftShift]);
54 fill->lineTo(x, topData[i + leftShift]);
55 x += xDelta;
56 }
57
58 for (int i = 0; i < leftShift; ++i) {
59 plot->lineTo(x, topData[i]);
60 fill->lineTo(x, topData[i]);
61 x += xDelta;
62 }
63
bsalomon49f085d2014-09-05 13:34:00 -070064 if (bottomData) {
bsalomon@google.com9053c2e2013-02-13 21:22:13 +000065 SkASSERT(bottomData->count() == topData.count());
66 // iterate backwards over the previous graph's data to generate the bottom of the filled
67 // area (and account for leftShift).
68 for (int i = 0; i < leftShift; ++i) {
69 x -= xDelta;
70 fill->lineTo(x, (*bottomData)[leftShift - 1 - i]);
71 }
72 for (int i = 0; i < shiftToEndCount; ++i) {
73 x -= xDelta;
74 fill->lineTo(x, (*bottomData)[bottomData->count() - 1 - i]);
75 }
76 } else {
77 fill->lineTo(x - xDelta, yBase);
78 fill->lineTo(xLeft, yBase);
79 }
80}
81
bsalomon@google.com9053c2e2013-02-13 21:22:13 +000082// A set of scrolling line plots with the area between each plot filled. Stresses out GPU path
83// filling
84class ChartView : public SampleView {
85public:
86 ChartView() {
87 fShift = 0;
88 fSize.set(-1, -1);
89 }
90
91protected:
mtklein36352bf2015-03-25 18:17:31 -070092 bool onQuery(SkEvent* evt) override {
bsalomon@google.com9053c2e2013-02-13 21:22:13 +000093 if (SampleCode::TitleQ(*evt)) {
94 SampleCode::TitleR(evt, "Chart");
95 return true;
96 }
97 return this->INHERITED::onQuery(evt);
98 }
99
mtklein36352bf2015-03-25 18:17:31 -0700100 void onDrawContent(SkCanvas* canvas) override {
bsalomon@google.com9053c2e2013-02-13 21:22:13 +0000101 bool sizeChanged = false;
Mike Reed3661bc92017-02-22 13:21:42 -0500102 if (canvas->getBaseLayerSize() != fSize) {
103 fSize = canvas->getBaseLayerSize();
bsalomon@google.com9053c2e2013-02-13 21:22:13 +0000104 sizeChanged = true;
105 }
106
107 SkScalar ySpread = SkIntToScalar(fSize.fHeight / 20);
108
109 SkScalar height = SkIntToScalar(fSize.fHeight);
110
111 if (sizeChanged) {
112 int dataPointCount = SkMax32(fSize.fWidth / kPixelsPerTick + 1, 2);
113
114 for (int i = 0; i < kNumGraphs; ++i) {
115 SkScalar y = (kNumGraphs - i) * (height - ySpread) / (kNumGraphs + 1);
116 fData[i].reset();
117 gen_data(y, ySpread, dataPointCount, fData + i);
118 }
119 }
120
121 canvas->clear(0xFFE0F0E0);
122
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000123 static SkRandom colorRand;
bsalomon@google.com9053c2e2013-02-13 21:22:13 +0000124 static SkColor gColors[kNumGraphs] = { 0x0 };
125 if (0 == gColors[0]) {
126 for (int i = 0; i < kNumGraphs; ++i) {
127 gColors[i] = colorRand.nextU() | 0xff000000;
128 }
129 }
130
131 SkPath plotPath;
132 SkPath fillPath;
133
134 static const SkScalar kStrokeWidth = SkIntToScalar(2);
135 SkPaint plotPaint;
136 SkPaint fillPaint;
137 plotPaint.setAntiAlias(true);
138 plotPaint.setStyle(SkPaint::kStroke_Style);
139 plotPaint.setStrokeWidth(kStrokeWidth);
140 plotPaint.setStrokeCap(SkPaint::kRound_Cap);
141 plotPaint.setStrokeJoin(SkPaint::kRound_Join);
142 fillPaint.setAntiAlias(true);
143 fillPaint.setStyle(SkPaint::kFill_Style);
144
halcanary96fcdcc2015-08-27 07:41:13 -0700145 SkTDArray<SkScalar>* prevData = nullptr;
bsalomon@google.com9053c2e2013-02-13 21:22:13 +0000146 for (int i = 0; i < kNumGraphs; ++i) {
147 gen_paths(fData[i],
148 prevData,
149 height,
150 0,
151 SkIntToScalar(kPixelsPerTick),
152 fShift,
153 &plotPath,
154 &fillPath);
155
156 // Make the fills partially transparent
157 fillPaint.setColor((gColors[i] & 0x00ffffff) | 0x80000000);
158 canvas->drawPath(fillPath, fillPaint);
159
160 plotPaint.setColor(gColors[i]);
161 canvas->drawPath(plotPath, plotPaint);
162
163 prevData = fData + i;
164 }
165
166 fShift += kShiftPerFrame;
bsalomon@google.com9053c2e2013-02-13 21:22:13 +0000167 }
168
169private:
170 enum {
171 kNumGraphs = 5,
172 kPixelsPerTick = 3,
173 kShiftPerFrame = 1,
174 };
175 int fShift;
176 SkISize fSize;
177 SkTDArray<SkScalar> fData[kNumGraphs];
178 typedef SampleView INHERITED;
179};
180
181//////////////////////////////////////////////////////////////////////////////
182
183static SkView* MyFactory() { return new ChartView; }
184static SkViewRegister reg(MyFactory);