blob: 46e4dec372ba0b95b4c7db9d803492014a4b37cd [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"
Mike Reedd5b51a02020-07-30 21:50:30 -040010#include "include/core/SkPathBuilder.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#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,
Mike Reedd5b51a02020-07-30 21:50:30 -040033 SkPathBuilder* plot, SkPathBuilder* fill) {
bsalomon@google.com9053c2e2013-02-13 21:22:13 +000034 plot->incReserve(topData.count());
halcanary96fcdcc2015-08-27 07:41:13 -070035 if (nullptr == bottomData) {
bsalomon@google.com9053c2e2013-02-13 21:22:13 +000036 fill->incReserve(topData.count() + 2);
37 } else {
38 fill->incReserve(2 * topData.count());
39 }
40
41 leftShift %= topData.count();
42 SkScalar x = xLeft;
43
44 // Account for the leftShift using two loops
45 int shiftToEndCount = topData.count() - leftShift;
46 plot->moveTo(x, topData[leftShift]);
47 fill->moveTo(x, topData[leftShift]);
48
49 for (int i = 1; i < shiftToEndCount; ++i) {
50 plot->lineTo(x, topData[i + leftShift]);
51 fill->lineTo(x, topData[i + leftShift]);
52 x += xDelta;
53 }
54
55 for (int i = 0; i < leftShift; ++i) {
56 plot->lineTo(x, topData[i]);
57 fill->lineTo(x, topData[i]);
58 x += xDelta;
59 }
60
bsalomon49f085d2014-09-05 13:34:00 -070061 if (bottomData) {
bsalomon@google.com9053c2e2013-02-13 21:22:13 +000062 SkASSERT(bottomData->count() == topData.count());
63 // iterate backwards over the previous graph's data to generate the bottom of the filled
64 // area (and account for leftShift).
65 for (int i = 0; i < leftShift; ++i) {
66 x -= xDelta;
67 fill->lineTo(x, (*bottomData)[leftShift - 1 - i]);
68 }
69 for (int i = 0; i < shiftToEndCount; ++i) {
70 x -= xDelta;
71 fill->lineTo(x, (*bottomData)[bottomData->count() - 1 - i]);
72 }
73 } else {
74 fill->lineTo(x - xDelta, yBase);
75 fill->lineTo(xLeft, yBase);
76 }
77}
78
bsalomon@google.com9053c2e2013-02-13 21:22:13 +000079// A set of scrolling line plots with the area between each plot filled. Stresses out GPU path
80// filling
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040081class ChartView : public Sample {
Hal Canaryd7639af2019-07-17 09:08:11 -040082 static constexpr int kNumGraphs = 5;
83 static constexpr int kPixelsPerTick = 3;
84 static constexpr int kShiftPerFrame = 1;
85 int fShift = 0;
86 SkISize fSize = {-1, -1};
87 SkTDArray<SkScalar> fData[kNumGraphs];
bsalomon@google.com9053c2e2013-02-13 21:22:13 +000088
Hal Canary8a027312019-07-03 10:55:44 -040089 SkString name() override { return SkString("Chart"); }
bsalomon@google.com9053c2e2013-02-13 21:22:13 +000090
mtklein36352bf2015-03-25 18:17:31 -070091 void onDrawContent(SkCanvas* canvas) override {
bsalomon@google.com9053c2e2013-02-13 21:22:13 +000092 bool sizeChanged = false;
Mike Reed3661bc92017-02-22 13:21:42 -050093 if (canvas->getBaseLayerSize() != fSize) {
94 fSize = canvas->getBaseLayerSize();
bsalomon@google.com9053c2e2013-02-13 21:22:13 +000095 sizeChanged = true;
96 }
97
98 SkScalar ySpread = SkIntToScalar(fSize.fHeight / 20);
99
100 SkScalar height = SkIntToScalar(fSize.fHeight);
101
102 if (sizeChanged) {
Brian Osman7f364052020-02-06 11:25:43 -0500103 int dataPointCount = std::max(fSize.fWidth / kPixelsPerTick + 1, 2);
bsalomon@google.com9053c2e2013-02-13 21:22:13 +0000104
105 for (int i = 0; i < kNumGraphs; ++i) {
106 SkScalar y = (kNumGraphs - i) * (height - ySpread) / (kNumGraphs + 1);
107 fData[i].reset();
108 gen_data(y, ySpread, dataPointCount, fData + i);
109 }
110 }
111
112 canvas->clear(0xFFE0F0E0);
113
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000114 static SkRandom colorRand;
bsalomon@google.com9053c2e2013-02-13 21:22:13 +0000115 static SkColor gColors[kNumGraphs] = { 0x0 };
116 if (0 == gColors[0]) {
117 for (int i = 0; i < kNumGraphs; ++i) {
118 gColors[i] = colorRand.nextU() | 0xff000000;
119 }
120 }
121
bsalomon@google.com9053c2e2013-02-13 21:22:13 +0000122 static const SkScalar kStrokeWidth = SkIntToScalar(2);
123 SkPaint plotPaint;
124 SkPaint fillPaint;
125 plotPaint.setAntiAlias(true);
126 plotPaint.setStyle(SkPaint::kStroke_Style);
127 plotPaint.setStrokeWidth(kStrokeWidth);
128 plotPaint.setStrokeCap(SkPaint::kRound_Cap);
129 plotPaint.setStrokeJoin(SkPaint::kRound_Join);
130 fillPaint.setAntiAlias(true);
131 fillPaint.setStyle(SkPaint::kFill_Style);
132
Mike Reedd5b51a02020-07-30 21:50:30 -0400133 SkPathBuilder plotPath, fillPath;
halcanary96fcdcc2015-08-27 07:41:13 -0700134 SkTDArray<SkScalar>* prevData = nullptr;
Mike Reedd5b51a02020-07-30 21:50:30 -0400135
bsalomon@google.com9053c2e2013-02-13 21:22:13 +0000136 for (int i = 0; i < kNumGraphs; ++i) {
137 gen_paths(fData[i],
138 prevData,
139 height,
140 0,
141 SkIntToScalar(kPixelsPerTick),
142 fShift,
143 &plotPath,
144 &fillPath);
145
146 // Make the fills partially transparent
147 fillPaint.setColor((gColors[i] & 0x00ffffff) | 0x80000000);
Mike Reedd5b51a02020-07-30 21:50:30 -0400148 canvas->drawPath(fillPath.detach(), fillPaint);
bsalomon@google.com9053c2e2013-02-13 21:22:13 +0000149
150 plotPaint.setColor(gColors[i]);
Mike Reedd5b51a02020-07-30 21:50:30 -0400151 canvas->drawPath(plotPath.detach(), plotPaint);
bsalomon@google.com9053c2e2013-02-13 21:22:13 +0000152
153 prevData = fData + i;
154 }
155
156 fShift += kShiftPerFrame;
bsalomon@google.com9053c2e2013-02-13 21:22:13 +0000157 }
bsalomon@google.com9053c2e2013-02-13 21:22:13 +0000158};
159
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400160DEF_SAMPLE( return new ChartView(); )