bsalomon@google.com | 9053c2e | 2013-02-13 21:22:13 +0000 | [diff] [blame] | 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 | */ |
commit-bot@chromium.org | ab13167 | 2013-09-13 12:39:55 +0000 | [diff] [blame] | 7 | |
bsalomon@google.com | 9053c2e | 2013-02-13 21:22:13 +0000 | [diff] [blame] | 8 | #include "SampleCode.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkPaint.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 11 | #include "SkPath.h" |
bsalomon@google.com | 9053c2e | 2013-02-13 21:22:13 +0000 | [diff] [blame] | 12 | #include "SkRandom.h" |
| 13 | #include "SkView.h" |
| 14 | |
bsalomon@google.com | 9053c2e | 2013-02-13 21:22:13 +0000 | [diff] [blame] | 15 | // Generates y values for the chart plots. |
commit-bot@chromium.org | ab13167 | 2013-09-13 12:39:55 +0000 | [diff] [blame] | 16 | static void gen_data(SkScalar yAvg, SkScalar ySpread, int count, SkTDArray<SkScalar>* dataPts) { |
bsalomon@google.com | 9053c2e | 2013-02-13 21:22:13 +0000 | [diff] [blame] | 17 | dataPts->setCount(count); |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 18 | static SkRandom gRandom; |
bsalomon@google.com | 9053c2e | 2013-02-13 21:22:13 +0000 | [diff] [blame] | 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 |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 27 | // yBase if bottomData == nullptr. |
bsalomon@google.com | 9053c2e | 2013-02-13 21:22:13 +0000 | [diff] [blame] | 28 | // The plots are animated by rotating the data points by leftShift. |
commit-bot@chromium.org | ab13167 | 2013-09-13 12:39:55 +0000 | [diff] [blame] | 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 | SkPath* plot, SkPath* fill) { |
bsalomon@google.com | 9053c2e | 2013-02-13 21:22:13 +0000 | [diff] [blame] | 35 | plot->rewind(); |
| 36 | fill->rewind(); |
| 37 | plot->incReserve(topData.count()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 38 | if (nullptr == bottomData) { |
bsalomon@google.com | 9053c2e | 2013-02-13 21:22:13 +0000 | [diff] [blame] | 39 | fill->incReserve(topData.count() + 2); |
| 40 | } else { |
| 41 | fill->incReserve(2 * topData.count()); |
| 42 | } |
| 43 | |
| 44 | leftShift %= topData.count(); |
| 45 | SkScalar x = xLeft; |
| 46 | |
| 47 | // Account for the leftShift using two loops |
| 48 | int shiftToEndCount = topData.count() - leftShift; |
| 49 | plot->moveTo(x, topData[leftShift]); |
| 50 | fill->moveTo(x, topData[leftShift]); |
| 51 | |
| 52 | for (int i = 1; i < shiftToEndCount; ++i) { |
| 53 | plot->lineTo(x, topData[i + leftShift]); |
| 54 | fill->lineTo(x, topData[i + leftShift]); |
| 55 | x += xDelta; |
| 56 | } |
| 57 | |
| 58 | for (int i = 0; i < leftShift; ++i) { |
| 59 | plot->lineTo(x, topData[i]); |
| 60 | fill->lineTo(x, topData[i]); |
| 61 | x += xDelta; |
| 62 | } |
| 63 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 64 | if (bottomData) { |
bsalomon@google.com | 9053c2e | 2013-02-13 21:22:13 +0000 | [diff] [blame] | 65 | SkASSERT(bottomData->count() == topData.count()); |
| 66 | // iterate backwards over the previous graph's data to generate the bottom of the filled |
| 67 | // area (and account for leftShift). |
| 68 | for (int i = 0; i < leftShift; ++i) { |
| 69 | x -= xDelta; |
| 70 | fill->lineTo(x, (*bottomData)[leftShift - 1 - i]); |
| 71 | } |
| 72 | for (int i = 0; i < shiftToEndCount; ++i) { |
| 73 | x -= xDelta; |
| 74 | fill->lineTo(x, (*bottomData)[bottomData->count() - 1 - i]); |
| 75 | } |
| 76 | } else { |
| 77 | fill->lineTo(x - xDelta, yBase); |
| 78 | fill->lineTo(xLeft, yBase); |
| 79 | } |
| 80 | } |
| 81 | |
bsalomon@google.com | 9053c2e | 2013-02-13 21:22:13 +0000 | [diff] [blame] | 82 | // A set of scrolling line plots with the area between each plot filled. Stresses out GPU path |
| 83 | // filling |
| 84 | class ChartView : public SampleView { |
| 85 | public: |
| 86 | ChartView() { |
| 87 | fShift = 0; |
| 88 | fSize.set(-1, -1); |
| 89 | } |
| 90 | |
| 91 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 92 | bool onQuery(SkEvent* evt) override { |
bsalomon@google.com | 9053c2e | 2013-02-13 21:22:13 +0000 | [diff] [blame] | 93 | if (SampleCode::TitleQ(*evt)) { |
| 94 | SampleCode::TitleR(evt, "Chart"); |
| 95 | return true; |
| 96 | } |
| 97 | return this->INHERITED::onQuery(evt); |
| 98 | } |
| 99 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 100 | void onDrawContent(SkCanvas* canvas) override { |
bsalomon@google.com | 9053c2e | 2013-02-13 21:22:13 +0000 | [diff] [blame] | 101 | bool sizeChanged = false; |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 102 | if (canvas->getBaseLayerSize() != fSize) { |
| 103 | fSize = canvas->getBaseLayerSize(); |
bsalomon@google.com | 9053c2e | 2013-02-13 21:22:13 +0000 | [diff] [blame] | 104 | sizeChanged = true; |
| 105 | } |
| 106 | |
| 107 | SkScalar ySpread = SkIntToScalar(fSize.fHeight / 20); |
| 108 | |
| 109 | SkScalar height = SkIntToScalar(fSize.fHeight); |
| 110 | |
| 111 | if (sizeChanged) { |
| 112 | int dataPointCount = SkMax32(fSize.fWidth / kPixelsPerTick + 1, 2); |
| 113 | |
| 114 | for (int i = 0; i < kNumGraphs; ++i) { |
| 115 | SkScalar y = (kNumGraphs - i) * (height - ySpread) / (kNumGraphs + 1); |
| 116 | fData[i].reset(); |
| 117 | gen_data(y, ySpread, dataPointCount, fData + i); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | canvas->clear(0xFFE0F0E0); |
| 122 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 123 | static SkRandom colorRand; |
bsalomon@google.com | 9053c2e | 2013-02-13 21:22:13 +0000 | [diff] [blame] | 124 | static SkColor gColors[kNumGraphs] = { 0x0 }; |
| 125 | if (0 == gColors[0]) { |
| 126 | for (int i = 0; i < kNumGraphs; ++i) { |
| 127 | gColors[i] = colorRand.nextU() | 0xff000000; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | SkPath plotPath; |
| 132 | SkPath fillPath; |
| 133 | |
| 134 | static const SkScalar kStrokeWidth = SkIntToScalar(2); |
| 135 | SkPaint plotPaint; |
| 136 | SkPaint fillPaint; |
| 137 | plotPaint.setAntiAlias(true); |
| 138 | plotPaint.setStyle(SkPaint::kStroke_Style); |
| 139 | plotPaint.setStrokeWidth(kStrokeWidth); |
| 140 | plotPaint.setStrokeCap(SkPaint::kRound_Cap); |
| 141 | plotPaint.setStrokeJoin(SkPaint::kRound_Join); |
| 142 | fillPaint.setAntiAlias(true); |
| 143 | fillPaint.setStyle(SkPaint::kFill_Style); |
| 144 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 145 | SkTDArray<SkScalar>* prevData = nullptr; |
bsalomon@google.com | 9053c2e | 2013-02-13 21:22:13 +0000 | [diff] [blame] | 146 | for (int i = 0; i < kNumGraphs; ++i) { |
| 147 | gen_paths(fData[i], |
| 148 | prevData, |
| 149 | height, |
| 150 | 0, |
| 151 | SkIntToScalar(kPixelsPerTick), |
| 152 | fShift, |
| 153 | &plotPath, |
| 154 | &fillPath); |
| 155 | |
| 156 | // Make the fills partially transparent |
| 157 | fillPaint.setColor((gColors[i] & 0x00ffffff) | 0x80000000); |
| 158 | canvas->drawPath(fillPath, fillPaint); |
| 159 | |
| 160 | plotPaint.setColor(gColors[i]); |
| 161 | canvas->drawPath(plotPath, plotPaint); |
| 162 | |
| 163 | prevData = fData + i; |
| 164 | } |
| 165 | |
| 166 | fShift += kShiftPerFrame; |
bsalomon@google.com | 9053c2e | 2013-02-13 21:22:13 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | private: |
| 170 | enum { |
| 171 | kNumGraphs = 5, |
| 172 | kPixelsPerTick = 3, |
| 173 | kShiftPerFrame = 1, |
| 174 | }; |
| 175 | int fShift; |
| 176 | SkISize fSize; |
| 177 | SkTDArray<SkScalar> fData[kNumGraphs]; |
| 178 | typedef SampleView INHERITED; |
| 179 | }; |
| 180 | |
| 181 | ////////////////////////////////////////////////////////////////////////////// |
| 182 | |
| 183 | static SkView* MyFactory() { return new ChartView; } |
| 184 | static SkViewRegister reg(MyFactory); |