1 /*
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
8 #include "include/core/SkCanvas.h"
9 #include "include/core/SkPaint.h"
10 #include "include/core/SkPath.h"
11 #include "include/utils/SkRandom.h"
12 #include "samplecode/Sample.h"
13
14 // Generates y values for the chart plots.
gen_data(SkScalar yAvg,SkScalar ySpread,int count,SkTDArray<SkScalar> * dataPts)15 static void gen_data(SkScalar yAvg, SkScalar ySpread, int count, SkTDArray<SkScalar>* dataPts) {
16 dataPts->setCount(count);
17 static SkRandom gRandom;
18 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
26 // yBase if bottomData == nullptr.
27 // The plots are animated by rotating the data points by leftShift.
gen_paths(const SkTDArray<SkScalar> & topData,const SkTDArray<SkScalar> * bottomData,SkScalar yBase,SkScalar xLeft,SkScalar xDelta,int leftShift,SkPath * plot,SkPath * fill)28 static 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) {
34 plot->rewind();
35 fill->rewind();
36 plot->incReserve(topData.count());
37 if (nullptr == bottomData) {
38 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
63 if (bottomData) {
64 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
81 // A set of scrolling line plots with the area between each plot filled. Stresses out GPU path
82 // filling
83 class ChartView : public Sample {
84 static constexpr int kNumGraphs = 5;
85 static constexpr int kPixelsPerTick = 3;
86 static constexpr int kShiftPerFrame = 1;
87 int fShift = 0;
88 SkISize fSize = {-1, -1};
89 SkTDArray<SkScalar> fData[kNumGraphs];
90
name()91 SkString name() override { return SkString("Chart"); }
92
onDrawContent(SkCanvas * canvas)93 void onDrawContent(SkCanvas* canvas) override {
94 bool sizeChanged = false;
95 if (canvas->getBaseLayerSize() != fSize) {
96 fSize = canvas->getBaseLayerSize();
97 sizeChanged = true;
98 }
99
100 SkScalar ySpread = SkIntToScalar(fSize.fHeight / 20);
101
102 SkScalar height = SkIntToScalar(fSize.fHeight);
103
104 if (sizeChanged) {
105 int dataPointCount = SkMax32(fSize.fWidth / kPixelsPerTick + 1, 2);
106
107 for (int i = 0; i < kNumGraphs; ++i) {
108 SkScalar y = (kNumGraphs - i) * (height - ySpread) / (kNumGraphs + 1);
109 fData[i].reset();
110 gen_data(y, ySpread, dataPointCount, fData + i);
111 }
112 }
113
114 canvas->clear(0xFFE0F0E0);
115
116 static SkRandom colorRand;
117 static SkColor gColors[kNumGraphs] = { 0x0 };
118 if (0 == gColors[0]) {
119 for (int i = 0; i < kNumGraphs; ++i) {
120 gColors[i] = colorRand.nextU() | 0xff000000;
121 }
122 }
123
124 SkPath plotPath;
125 SkPath fillPath;
126
127 static const SkScalar kStrokeWidth = SkIntToScalar(2);
128 SkPaint plotPaint;
129 SkPaint fillPaint;
130 plotPaint.setAntiAlias(true);
131 plotPaint.setStyle(SkPaint::kStroke_Style);
132 plotPaint.setStrokeWidth(kStrokeWidth);
133 plotPaint.setStrokeCap(SkPaint::kRound_Cap);
134 plotPaint.setStrokeJoin(SkPaint::kRound_Join);
135 fillPaint.setAntiAlias(true);
136 fillPaint.setStyle(SkPaint::kFill_Style);
137
138 SkTDArray<SkScalar>* prevData = nullptr;
139 for (int i = 0; i < kNumGraphs; ++i) {
140 gen_paths(fData[i],
141 prevData,
142 height,
143 0,
144 SkIntToScalar(kPixelsPerTick),
145 fShift,
146 &plotPath,
147 &fillPath);
148
149 // Make the fills partially transparent
150 fillPaint.setColor((gColors[i] & 0x00ffffff) | 0x80000000);
151 canvas->drawPath(fillPath, fillPaint);
152
153 plotPaint.setColor(gColors[i]);
154 canvas->drawPath(plotPath, plotPaint);
155
156 prevData = fData + i;
157 }
158
159 fShift += kShiftPerFrame;
160 }
161 };
162
163 DEF_SAMPLE( return new ChartView(); )
164