xref: /aosp_15_r20/external/skia/tools/viewer/ChartSlide.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 
8 #include "include/core/SkCanvas.h"
9 #include "include/core/SkPaint.h"
10 #include "include/core/SkPathBuilder.h"
11 #include "include/private/base/SkTDArray.h"
12 #include "src/base/SkRandom.h"
13 #include "tools/viewer/Slide.h"
14 
15 // Generates y values for the chart plots.
gen_data(SkScalar yAvg,SkScalar ySpread,int count,SkTDArray<SkScalar> * dataPts)16 static 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.
gen_paths(const SkTDArray<SkScalar> & topData,const SkTDArray<SkScalar> * bottomData,SkScalar yBase,SkScalar xLeft,SkScalar xDelta,int leftShift,SkPathBuilder * plot,SkPathBuilder * fill)29 static void gen_paths(const SkTDArray<SkScalar>& topData,
30                       const SkTDArray<SkScalar>* bottomData,
31                       SkScalar yBase,
32                       SkScalar xLeft, SkScalar xDelta,
33                       int leftShift,
34                       SkPathBuilder* plot, SkPathBuilder* fill) {
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
82 class 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 
90 public:
ChartSlide()91     ChartSlide() { fName = "Chart"; }
92 
draw(SkCanvas * canvas)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);
128         plotPaint.setStyle(SkPaint::kStroke_Style);
129         plotPaint.setStrokeWidth(kStrokeWidth);
130         plotPaint.setStrokeCap(SkPaint::kRound_Cap);
131         plotPaint.setStrokeJoin(SkPaint::kRound_Join);
132         fillPaint.setAntiAlias(true);
133         fillPaint.setStyle(SkPaint::kFill_Style);
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 
162 DEF_SLIDE( return new ChartSlide(); )
163