blob: 448e1d7e06c8410ea8d0628f3595b0218249b9d0 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#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"
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 {
Hal Canaryd7639af2019-07-17 09:08:11 -040084 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];
bsalomon@google.com9053c2e2013-02-13 21:22:13 +000090
Hal Canary8a027312019-07-03 10:55:44 -040091 SkString name() override { return SkString("Chart"); }
bsalomon@google.com9053c2e2013-02-13 21:22:13 +000092
mtklein36352bf2015-03-25 18:17:31 -070093 void onDrawContent(SkCanvas* canvas) override {
bsalomon@google.com9053c2e2013-02-13 21:22:13 +000094 bool sizeChanged = false;
Mike Reed3661bc92017-02-22 13:21:42 -050095 if (canvas->getBaseLayerSize() != fSize) {
96 fSize = canvas->getBaseLayerSize();
bsalomon@google.com9053c2e2013-02-13 21:22:13 +000097 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
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000116 static SkRandom colorRand;
bsalomon@google.com9053c2e2013-02-13 21:22:13 +0000117 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
halcanary96fcdcc2015-08-27 07:41:13 -0700138 SkTDArray<SkScalar>* prevData = nullptr;
bsalomon@google.com9053c2e2013-02-13 21:22:13 +0000139 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;
bsalomon@google.com9053c2e2013-02-13 21:22:13 +0000160 }
bsalomon@google.com9053c2e2013-02-13 21:22:13 +0000161};
162
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400163DEF_SAMPLE( return new ChartView(); )