Flutter Engine
The Flutter Engine
ChartSlide.cpp
Go to the documentation of this file.
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
12#include "src/base/SkRandom.h"
13#include "tools/viewer/Slide.h"
14
15// Generates y values for the chart plots.
16static void gen_data(SkScalar yAvg, SkScalar ySpread, int count, SkTDArray<SkScalar>* dataPts) {
17 dataPts->resize(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));
22 }
23}
24
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.
29static void gen_paths(const SkTDArray<SkScalar>& topData,
30 const SkTDArray<SkScalar>* bottomData,
31 SkScalar yBase,
32 SkScalar xLeft, SkScalar xDelta,
33 int leftShift,
35 plot->incReserve(topData.size());
36 if (nullptr == bottomData) {
37 fill->incReserve(topData.size() + 2);
38 } else {
39 fill->incReserve(2 * topData.size());
40 }
41
42 leftShift %= topData.size();
43 SkScalar x = xLeft;
44
45 // Account for the leftShift using two loops
46 int shiftToEndCount = topData.size() - leftShift;
47 plot->moveTo(x, topData[leftShift]);
48 fill->moveTo(x, topData[leftShift]);
49
50 for (int i = 1; i < shiftToEndCount; ++i) {
51 plot->lineTo(x, topData[i + leftShift]);
52 fill->lineTo(x, topData[i + leftShift]);
53 x += xDelta;
54 }
55
56 for (int i = 0; i < leftShift; ++i) {
57 plot->lineTo(x, topData[i]);
58 fill->lineTo(x, topData[i]);
59 x += xDelta;
60 }
61
62 if (bottomData) {
63 SkASSERT(bottomData->size() == topData.size());
64 // iterate backwards over the previous graph's data to generate the bottom of the filled
65 // area (and account for leftShift).
66 for (int i = 0; i < leftShift; ++i) {
67 x -= xDelta;
68 fill->lineTo(x, (*bottomData)[leftShift - 1 - i]);
69 }
70 for (int i = 0; i < shiftToEndCount; ++i) {
71 x -= xDelta;
72 fill->lineTo(x, (*bottomData)[bottomData->size() - 1 - i]);
73 }
74 } else {
75 fill->lineTo(x - xDelta, yBase);
76 fill->lineTo(xLeft, yBase);
77 }
78}
79
80// A set of scrolling line plots with the area between each plot filled. Stresses out GPU path
81// filling
82class ChartSlide : public Slide {
83 inline static constexpr int kNumGraphs = 5;
84 inline static constexpr int kPixelsPerTick = 3;
85 inline static constexpr int kShiftPerFrame = 1;
86 int fShift = 0;
87 SkISize fSize = {-1, -1};
88 SkTDArray<SkScalar> fData[kNumGraphs];
89
90public:
91 ChartSlide() { fName = "Chart"; }
92
93 void draw(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 = std::max(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 static const SkScalar kStrokeWidth = SkIntToScalar(2);
125 SkPaint plotPaint;
126 SkPaint fillPaint;
127 plotPaint.setAntiAlias(true);
129 plotPaint.setStrokeWidth(kStrokeWidth);
132 fillPaint.setAntiAlias(true);
134
135 SkPathBuilder plotPath, fillPath;
136 SkTDArray<SkScalar>* prevData = nullptr;
137
138 for (int i = 0; i < kNumGraphs; ++i) {
139 gen_paths(fData[i],
140 prevData,
141 height,
142 0,
143 SkIntToScalar(kPixelsPerTick),
144 fShift,
145 &plotPath,
146 &fillPath);
147
148 // Make the fills partially transparent
149 fillPaint.setColor((gColors[i] & 0x00ffffff) | 0x80000000);
150 canvas->drawPath(fillPath.detach(), fillPaint);
151
152 plotPaint.setColor(gColors[i]);
153 canvas->drawPath(plotPath.detach(), plotPaint);
154
155 prevData = fData + i;
156 }
157
158 fShift += kShiftPerFrame;
159 }
160};
161
162DEF_SLIDE( return new ChartSlide(); )
static void gen_paths(const SkTDArray< SkScalar > &topData, const SkTDArray< SkScalar > *bottomData, SkScalar yBase, SkScalar xLeft, SkScalar xDelta, int leftShift, SkPathBuilder *plot, SkPathBuilder *fill)
Definition: ChartSlide.cpp:29
static void gen_data(SkScalar yAvg, SkScalar ySpread, int count, SkTDArray< SkScalar > *dataPts)
Definition: ChartSlide.cpp:16
int count
Definition: FontMgrTest.cpp:50
static const SkColor gColors[]
#define SkASSERT(cond)
Definition: SkAssert.h:116
uint32_t SkColor
Definition: SkColor.h:37
#define SkScalarHalf(a)
Definition: SkScalar.h:75
#define SkIntToScalar(x)
Definition: SkScalar.h:57
#define DEF_SLIDE(code)
Definition: Slide.h:25
void draw(SkCanvas *canvas) override
Definition: ChartSlide.cpp:93
virtual SkISize getBaseLayerSize() const
Definition: SkCanvas.cpp:369
void clear(SkColor color)
Definition: SkCanvas.h:1199
void drawPath(const SkPath &path, const SkPaint &paint)
Definition: SkCanvas.cpp:1747
@ kRound_Cap
adds circle
Definition: SkPaint.h:335
void setStyle(Style style)
Definition: SkPaint.cpp:105
void setColor(SkColor color)
Definition: SkPaint.cpp:119
void setAntiAlias(bool aa)
Definition: SkPaint.h:170
void setStrokeCap(Cap cap)
Definition: SkPaint.cpp:179
@ kStroke_Style
set to stroke geometry
Definition: SkPaint.h:194
@ kFill_Style
set to fill geometry
Definition: SkPaint.h:193
void setStrokeJoin(Join join)
Definition: SkPaint.cpp:189
@ kRound_Join
adds circle
Definition: SkPaint.h:360
void setStrokeWidth(SkScalar width)
Definition: SkPaint.cpp:159
SkPathBuilder & lineTo(SkPoint pt)
void incReserve(int extraPtCount, int extraVerbCount)
SkPathBuilder & moveTo(SkPoint pt)
uint32_t nextU()
Definition: SkRandom.h:42
SkScalar nextRangeScalar(SkScalar min, SkScalar max)
Definition: SkRandom.h:106
int size() const
Definition: SkTDArray.h:138
void reset()
Definition: SkTDArray.h:171
void resize(int count)
Definition: SkTDArray.h:183
Definition: Slide.h:29
SkString fName
Definition: Slide.h:54
float SkScalar
Definition: extension.cpp:12
static float max(float r, float g, float b)
Definition: hsl.cpp:49
double y
double x
static void plot(SkCanvas *canvas, const char *fn, float xMin, float xMax, float yMin, float yMax, const char *label=nullptr, bool requireES3=false)
int32_t height
Definition: SkSize.h:16