bsalomon@google.com | 4a71997 | 2013-02-22 15:10:36 +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 | */ |
| 7 | |
| 8 | #include "SkBenchmark.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkPaint.h" |
| 11 | #include "SkRandom.h" |
| 12 | |
| 13 | /** |
| 14 | * This is a conversion of samplecode/SampleChart.cpp into a bench. It sure would be nice to be able |
| 15 | * to write one subclass that can be a GM, bench, and/or Sample. |
| 16 | */ |
| 17 | |
bsalomon@google.com | 4a71997 | 2013-02-22 15:10:36 +0000 | [diff] [blame] | 18 | // Generates y values for the chart plots. |
commit-bot@chromium.org | 97b4b67 | 2013-09-26 19:23:03 +0000 | [diff] [blame] | 19 | static void gen_data(SkScalar yAvg, SkScalar ySpread, int count, SkTDArray<SkScalar>* dataPts) { |
bsalomon@google.com | 4a71997 | 2013-02-22 15:10:36 +0000 | [diff] [blame] | 20 | dataPts->setCount(count); |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 21 | static SkRandom gRandom; |
bsalomon@google.com | 4a71997 | 2013-02-22 15:10:36 +0000 | [diff] [blame] | 22 | for (int i = 0; i < count; ++i) { |
| 23 | (*dataPts)[i] = gRandom.nextRangeScalar(yAvg - SkScalarHalf(ySpread), |
| 24 | yAvg + SkScalarHalf(ySpread)); |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | // Generates a path to stroke along the top of each plot and a fill path for the area below each |
| 29 | // plot. The fill path is bounded below by the bottomData plot points or a horizontal line at |
| 30 | // yBase if bottomData == NULL. |
| 31 | // The plots are animated by rotating the data points by leftShift. |
commit-bot@chromium.org | 97b4b67 | 2013-09-26 19:23:03 +0000 | [diff] [blame] | 32 | static void gen_paths(const SkTDArray<SkScalar>& topData, |
| 33 | const SkTDArray<SkScalar>* bottomData, |
| 34 | SkScalar yBase, |
| 35 | SkScalar xLeft, SkScalar xDelta, |
| 36 | int leftShift, |
| 37 | SkPath* plot, SkPath* fill) { |
bsalomon@google.com | 4a71997 | 2013-02-22 15:10:36 +0000 | [diff] [blame] | 38 | plot->rewind(); |
| 39 | fill->rewind(); |
| 40 | plot->incReserve(topData.count()); |
| 41 | if (NULL == bottomData) { |
| 42 | fill->incReserve(topData.count() + 2); |
| 43 | } else { |
| 44 | fill->incReserve(2 * topData.count()); |
| 45 | } |
| 46 | |
| 47 | leftShift %= topData.count(); |
| 48 | SkScalar x = xLeft; |
| 49 | |
| 50 | // Account for the leftShift using two loops |
| 51 | int shiftToEndCount = topData.count() - leftShift; |
| 52 | plot->moveTo(x, topData[leftShift]); |
| 53 | fill->moveTo(x, topData[leftShift]); |
| 54 | |
| 55 | for (int i = 1; i < shiftToEndCount; ++i) { |
| 56 | plot->lineTo(x, topData[i + leftShift]); |
| 57 | fill->lineTo(x, topData[i + leftShift]); |
| 58 | x += xDelta; |
| 59 | } |
| 60 | |
| 61 | for (int i = 0; i < leftShift; ++i) { |
| 62 | plot->lineTo(x, topData[i]); |
| 63 | fill->lineTo(x, topData[i]); |
| 64 | x += xDelta; |
| 65 | } |
| 66 | |
| 67 | if (NULL != bottomData) { |
| 68 | SkASSERT(bottomData->count() == topData.count()); |
| 69 | // iterate backwards over the previous graph's data to generate the bottom of the filled |
| 70 | // area (and account for leftShift). |
| 71 | for (int i = 0; i < leftShift; ++i) { |
| 72 | x -= xDelta; |
| 73 | fill->lineTo(x, (*bottomData)[leftShift - 1 - i]); |
| 74 | } |
| 75 | for (int i = 0; i < shiftToEndCount; ++i) { |
| 76 | x -= xDelta; |
| 77 | fill->lineTo(x, (*bottomData)[bottomData->count() - 1 - i]); |
| 78 | } |
| 79 | } else { |
| 80 | fill->lineTo(x - xDelta, yBase); |
| 81 | fill->lineTo(xLeft, yBase); |
| 82 | } |
| 83 | } |
| 84 | |
bsalomon@google.com | 4a71997 | 2013-02-22 15:10:36 +0000 | [diff] [blame] | 85 | // A set of scrolling line plots with the area between each plot filled. Stresses out GPU path |
| 86 | // filling |
| 87 | class ChartBench : public SkBenchmark { |
| 88 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 89 | ChartBench(bool aa) { |
bsalomon@google.com | 4a71997 | 2013-02-22 15:10:36 +0000 | [diff] [blame] | 90 | fShift = 0; |
| 91 | fAA = aa; |
bsalomon@google.com | cd7421b | 2013-02-22 16:07:59 +0000 | [diff] [blame] | 92 | fSize.fWidth = -1; |
| 93 | fSize.fHeight = -1; |
bsalomon@google.com | 4a71997 | 2013-02-22 15:10:36 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | protected: |
| 97 | virtual const char* onGetName() SK_OVERRIDE { |
| 98 | if (fAA) { |
| 99 | return "chart_aa"; |
| 100 | } else { |
| 101 | return "chart_bw"; |
| 102 | } |
| 103 | } |
| 104 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 105 | virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { |
bsalomon@google.com | 4a71997 | 2013-02-22 15:10:36 +0000 | [diff] [blame] | 106 | bool sizeChanged = false; |
| 107 | if (canvas->getDeviceSize() != fSize) { |
| 108 | fSize = canvas->getDeviceSize(); |
| 109 | sizeChanged = true; |
| 110 | } |
| 111 | |
| 112 | SkScalar ySpread = SkIntToScalar(fSize.fHeight / 20); |
| 113 | |
| 114 | SkScalar height = SkIntToScalar(fSize.fHeight); |
bsalomon@google.com | cd7421b | 2013-02-22 16:07:59 +0000 | [diff] [blame] | 115 | if (sizeChanged) { |
| 116 | int dataPointCount = SkMax32(fSize.fWidth / kPixelsPerTick + 1, 2); |
| 117 | |
| 118 | for (int i = 0; i < kNumGraphs; ++i) { |
| 119 | SkScalar y = (kNumGraphs - i) * (height - ySpread) / (kNumGraphs + 1); |
| 120 | fData[i].reset(); |
| 121 | gen_data(y, ySpread, dataPointCount, fData + i); |
| 122 | } |
| 123 | } |
bsalomon@google.com | 4a71997 | 2013-02-22 15:10:36 +0000 | [diff] [blame] | 124 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 125 | for (int frame = 0; frame < loops; ++frame) { |
bsalomon@google.com | 4a71997 | 2013-02-22 15:10:36 +0000 | [diff] [blame] | 126 | |
| 127 | canvas->clear(0xFFE0F0E0); |
| 128 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 129 | static SkRandom colorRand; |
bsalomon@google.com | 4a71997 | 2013-02-22 15:10:36 +0000 | [diff] [blame] | 130 | static SkColor gColors[kNumGraphs] = { 0x0 }; |
| 131 | if (0 == gColors[0]) { |
| 132 | for (int i = 0; i < kNumGraphs; ++i) { |
| 133 | gColors[i] = colorRand.nextU() | 0xff000000; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | SkPath plotPath; |
| 138 | SkPath fillPath; |
| 139 | |
| 140 | static const SkScalar kStrokeWidth = SkIntToScalar(2); |
| 141 | SkPaint plotPaint; |
| 142 | SkPaint fillPaint; |
| 143 | plotPaint.setAntiAlias(fAA); |
| 144 | plotPaint.setStyle(SkPaint::kStroke_Style); |
| 145 | plotPaint.setStrokeWidth(kStrokeWidth); |
| 146 | plotPaint.setStrokeCap(SkPaint::kRound_Cap); |
| 147 | plotPaint.setStrokeJoin(SkPaint::kRound_Join); |
| 148 | fillPaint.setAntiAlias(fAA); |
| 149 | fillPaint.setStyle(SkPaint::kFill_Style); |
| 150 | |
| 151 | SkTDArray<SkScalar>* prevData = NULL; |
| 152 | for (int i = 0; i < kNumGraphs; ++i) { |
| 153 | gen_paths(fData[i], |
| 154 | prevData, |
| 155 | height, |
| 156 | 0, |
| 157 | SkIntToScalar(kPixelsPerTick), |
| 158 | fShift, |
| 159 | &plotPath, |
| 160 | &fillPath); |
| 161 | |
| 162 | // Make the fills partially transparent |
| 163 | fillPaint.setColor((gColors[i] & 0x00ffffff) | 0x80000000); |
| 164 | canvas->drawPath(fillPath, fillPaint); |
| 165 | |
| 166 | plotPaint.setColor(gColors[i]); |
| 167 | canvas->drawPath(plotPath, plotPaint); |
| 168 | |
| 169 | prevData = fData + i; |
| 170 | } |
| 171 | |
| 172 | fShift += kShiftPerFrame; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | private: |
| 177 | enum { |
| 178 | kNumGraphs = 5, |
| 179 | kPixelsPerTick = 3, |
| 180 | kShiftPerFrame = 1, |
bsalomon@google.com | 4a71997 | 2013-02-22 15:10:36 +0000 | [diff] [blame] | 181 | }; |
| 182 | int fShift; |
| 183 | SkISize fSize; |
| 184 | SkTDArray<SkScalar> fData[kNumGraphs]; |
| 185 | bool fAA; |
| 186 | |
| 187 | typedef SkBenchmark INHERITED; |
| 188 | }; |
| 189 | |
| 190 | ////////////////////////////////////////////////////////////////////////////// |
| 191 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 192 | DEF_BENCH( return new ChartBench(true); ) |
| 193 | DEF_BENCH( return new ChartBench(false); ) |