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.
8 #include "SampleCode.h"
15 // Generates y values for the chart plots.
16 static void gen_data(SkScalar yAvg, SkScalar ySpread, int count, SkTDArray<SkScalar>* dataPts) {
17 dataPts->setCount(count);
18 static SkRandom gRandom;
19 for (int i = 0; i < count; ++i) {
20 (*dataPts)[i] = gRandom.nextRangeScalar(yAvg - SkScalarHalf(ySpread),
21 yAvg + SkScalarHalf(ySpread));
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
27 // yBase if bottomData == nullptr.
28 // The plots are animated by rotating the data points by leftShift.
29 static void gen_paths(const SkTDArray<SkScalar>& topData,
30 const SkTDArray<SkScalar>* bottomData,
32 SkScalar xLeft, SkScalar xDelta,
34 SkPath* plot, SkPath* fill) {
37 plot->incReserve(topData.count());
38 if (nullptr == bottomData) {
39 fill->incReserve(topData.count() + 2);
41 fill->incReserve(2 * topData.count());
44 leftShift %= topData.count();
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]);
52 for (int i = 1; i < shiftToEndCount; ++i) {
53 plot->lineTo(x, topData[i + leftShift]);
54 fill->lineTo(x, topData[i + leftShift]);
58 for (int i = 0; i < leftShift; ++i) {
59 plot->lineTo(x, topData[i]);
60 fill->lineTo(x, topData[i]);
65 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) {
70 fill->lineTo(x, (*bottomData)[leftShift - 1 - i]);
72 for (int i = 0; i < shiftToEndCount; ++i) {
74 fill->lineTo(x, (*bottomData)[bottomData->count() - 1 - i]);
77 fill->lineTo(x - xDelta, yBase);
78 fill->lineTo(xLeft, yBase);
82 // A set of scrolling line plots with the area between each plot filled. Stresses out GPU path
84 class ChartView : public SampleView {
92 bool onQuery(SkEvent* evt) override {
93 if (SampleCode::TitleQ(*evt)) {
94 SampleCode::TitleR(evt, "Chart");
97 return this->INHERITED::onQuery(evt);
100 void onDrawContent(SkCanvas* canvas) override {
101 bool sizeChanged = false;
102 if (canvas->getBaseLayerSize() != fSize) {
103 fSize = canvas->getBaseLayerSize();
107 SkScalar ySpread = SkIntToScalar(fSize.fHeight / 20);
109 SkScalar height = SkIntToScalar(fSize.fHeight);
112 int dataPointCount = SkMax32(fSize.fWidth / kPixelsPerTick + 1, 2);
114 for (int i = 0; i < kNumGraphs; ++i) {
115 SkScalar y = (kNumGraphs - i) * (height - ySpread) / (kNumGraphs + 1);
117 gen_data(y, ySpread, dataPointCount, fData + i);
121 canvas->clear(0xFFE0F0E0);
123 static SkRandom colorRand;
124 static SkColor gColors[kNumGraphs] = { 0x0 };
125 if (0 == gColors[0]) {
126 for (int i = 0; i < kNumGraphs; ++i) {
127 gColors[i] = colorRand.nextU() | 0xff000000;
134 static const SkScalar kStrokeWidth = SkIntToScalar(2);
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);
145 SkTDArray<SkScalar>* prevData = nullptr;
146 for (int i = 0; i < kNumGraphs; ++i) {
151 SkIntToScalar(kPixelsPerTick),
156 // Make the fills partially transparent
157 fillPaint.setColor((gColors[i] & 0x00ffffff) | 0x80000000);
158 canvas->drawPath(fillPath, fillPaint);
160 plotPaint.setColor(gColors[i]);
161 canvas->drawPath(plotPath, plotPaint);
163 prevData = fData + i;
166 fShift += kShiftPerFrame;
167 this->inval(nullptr);
178 SkTDArray<SkScalar> fData[kNumGraphs];
179 typedef SampleView INHERITED;
182 //////////////////////////////////////////////////////////////////////////////
184 static SkView* MyFactory() { return new ChartView; }
185 static SkViewRegister reg(MyFactory);