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"
14 // Generates y values for the chart plots.
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));
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 == NULL.
27 // The plots are animated by rotating the data points by leftShift.
28 static void gen_paths(const SkTDArray<SkScalar>& topData,
29 const SkTDArray<SkScalar>* bottomData,
31 SkScalar xLeft, SkScalar xDelta,
33 SkPath* plot, SkPath* fill) {
36 plot->incReserve(topData.count());
37 if (NULL == bottomData) {
38 fill->incReserve(topData.count() + 2);
40 fill->incReserve(2 * topData.count());
43 leftShift %= topData.count();
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]);
51 for (int i = 1; i < shiftToEndCount; ++i) {
52 plot->lineTo(x, topData[i + leftShift]);
53 fill->lineTo(x, topData[i + leftShift]);
57 for (int i = 0; i < leftShift; ++i) {
58 plot->lineTo(x, topData[i]);
59 fill->lineTo(x, topData[i]);
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) {
69 fill->lineTo(x, (*bottomData)[leftShift - 1 - i]);
71 for (int i = 0; i < shiftToEndCount; ++i) {
73 fill->lineTo(x, (*bottomData)[bottomData->count() - 1 - i]);
76 fill->lineTo(x - xDelta, yBase);
77 fill->lineTo(xLeft, yBase);
81 // A set of scrolling line plots with the area between each plot filled. Stresses out GPU path
83 class ChartView : public SampleView {
91 bool onQuery(SkEvent* evt) SK_OVERRIDE {
92 if (SampleCode::TitleQ(*evt)) {
93 SampleCode::TitleR(evt, "Chart");
96 return this->INHERITED::onQuery(evt);
99 void onDrawContent(SkCanvas* canvas) SK_OVERRIDE {
100 bool sizeChanged = false;
101 if (canvas->getDeviceSize() != fSize) {
102 fSize = canvas->getDeviceSize();
106 SkScalar ySpread = SkIntToScalar(fSize.fHeight / 20);
108 SkScalar height = SkIntToScalar(fSize.fHeight);
111 int dataPointCount = SkMax32(fSize.fWidth / kPixelsPerTick + 1, 2);
113 for (int i = 0; i < kNumGraphs; ++i) {
114 SkScalar y = (kNumGraphs - i) * (height - ySpread) / (kNumGraphs + 1);
116 gen_data(y, ySpread, dataPointCount, fData + i);
120 canvas->clear(0xFFE0F0E0);
122 static SkRandom colorRand;
123 static SkColor gColors[kNumGraphs] = { 0x0 };
124 if (0 == gColors[0]) {
125 for (int i = 0; i < kNumGraphs; ++i) {
126 gColors[i] = colorRand.nextU() | 0xff000000;
133 static const SkScalar kStrokeWidth = SkIntToScalar(2);
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);
144 SkTDArray<SkScalar>* prevData = NULL;
145 for (int i = 0; i < kNumGraphs; ++i) {
150 SkIntToScalar(kPixelsPerTick),
155 // Make the fills partially transparent
156 fillPaint.setColor((gColors[i] & 0x00ffffff) | 0x80000000);
157 canvas->drawPath(fillPath, fillPaint);
159 plotPaint.setColor(gColors[i]);
160 canvas->drawPath(plotPath, plotPaint);
162 prevData = fData + i;
165 fShift += kShiftPerFrame;
177 SkTDArray<SkScalar> fData[kNumGraphs];
178 typedef SampleView INHERITED;
181 //////////////////////////////////////////////////////////////////////////////
183 static SkView* MyFactory() { return new ChartView; }
184 static SkViewRegister reg(MyFactory);