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 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 8 | #include "Benchmark.h" |
bsalomon@google.com | 4a71997 | 2013-02-22 15:10:36 +0000 | [diff] [blame] | 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 | 8a2151f | 2014-05-08 15:12:43 +0000 | [diff] [blame] | 19 | static void gen_data(SkScalar yAvg, SkScalar ySpread, int count, |
| 20 | SkRandom* random, SkTDArray<SkScalar>* dataPts) { |
bsalomon@google.com | 4a71997 | 2013-02-22 15:10:36 +0000 | [diff] [blame] | 21 | dataPts->setCount(count); |
bsalomon@google.com | 4a71997 | 2013-02-22 15:10:36 +0000 | [diff] [blame] | 22 | for (int i = 0; i < count; ++i) { |
commit-bot@chromium.org | 8a2151f | 2014-05-08 15:12:43 +0000 | [diff] [blame] | 23 | (*dataPts)[i] = random->nextRangeScalar(yAvg - SkScalarHalf(ySpread), |
bsalomon@google.com | 4a71997 | 2013-02-22 15:10:36 +0000 | [diff] [blame] | 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 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 67 | if (bottomData) { |
bsalomon@google.com | 4a71997 | 2013-02-22 15:10:36 +0000 | [diff] [blame] | 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 |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 87 | class ChartBench : public Benchmark { |
bsalomon@google.com | 4a71997 | 2013-02-22 15:10:36 +0000 | [diff] [blame] | 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 | |
commit-bot@chromium.org | 8a2151f | 2014-05-08 15:12:43 +0000 | [diff] [blame] | 118 | SkRandom random; |
bsalomon@google.com | cd7421b | 2013-02-22 16:07:59 +0000 | [diff] [blame] | 119 | for (int i = 0; i < kNumGraphs; ++i) { |
| 120 | SkScalar y = (kNumGraphs - i) * (height - ySpread) / (kNumGraphs + 1); |
| 121 | fData[i].reset(); |
commit-bot@chromium.org | 8a2151f | 2014-05-08 15:12:43 +0000 | [diff] [blame] | 122 | gen_data(y, ySpread, dataPointCount, &random, fData + i); |
bsalomon@google.com | cd7421b | 2013-02-22 16:07:59 +0000 | [diff] [blame] | 123 | } |
| 124 | } |
bsalomon@google.com | 4a71997 | 2013-02-22 15:10:36 +0000 | [diff] [blame] | 125 | |
commit-bot@chromium.org | 8a2151f | 2014-05-08 15:12:43 +0000 | [diff] [blame] | 126 | SkRandom colorRand; |
| 127 | SkColor colors[kNumGraphs]; |
| 128 | for (int i = 0; i < kNumGraphs; ++i) { |
| 129 | colors[i] = colorRand.nextU() | 0xff000000; |
| 130 | } |
| 131 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 132 | for (int frame = 0; frame < loops; ++frame) { |
bsalomon@google.com | 4a71997 | 2013-02-22 15:10:36 +0000 | [diff] [blame] | 133 | |
| 134 | canvas->clear(0xFFE0F0E0); |
| 135 | |
bsalomon@google.com | 4a71997 | 2013-02-22 15:10:36 +0000 | [diff] [blame] | 136 | SkPath plotPath; |
| 137 | SkPath fillPath; |
| 138 | |
| 139 | static const SkScalar kStrokeWidth = SkIntToScalar(2); |
| 140 | SkPaint plotPaint; |
| 141 | SkPaint fillPaint; |
| 142 | plotPaint.setAntiAlias(fAA); |
| 143 | plotPaint.setStyle(SkPaint::kStroke_Style); |
| 144 | plotPaint.setStrokeWidth(kStrokeWidth); |
| 145 | plotPaint.setStrokeCap(SkPaint::kRound_Cap); |
| 146 | plotPaint.setStrokeJoin(SkPaint::kRound_Join); |
| 147 | fillPaint.setAntiAlias(fAA); |
| 148 | fillPaint.setStyle(SkPaint::kFill_Style); |
| 149 | |
| 150 | SkTDArray<SkScalar>* prevData = NULL; |
| 151 | for (int i = 0; i < kNumGraphs; ++i) { |
| 152 | gen_paths(fData[i], |
| 153 | prevData, |
| 154 | height, |
| 155 | 0, |
| 156 | SkIntToScalar(kPixelsPerTick), |
| 157 | fShift, |
| 158 | &plotPath, |
| 159 | &fillPath); |
| 160 | |
| 161 | // Make the fills partially transparent |
commit-bot@chromium.org | 8a2151f | 2014-05-08 15:12:43 +0000 | [diff] [blame] | 162 | fillPaint.setColor((colors[i] & 0x00ffffff) | 0x80000000); |
bsalomon@google.com | 4a71997 | 2013-02-22 15:10:36 +0000 | [diff] [blame] | 163 | canvas->drawPath(fillPath, fillPaint); |
| 164 | |
commit-bot@chromium.org | 8a2151f | 2014-05-08 15:12:43 +0000 | [diff] [blame] | 165 | plotPaint.setColor(colors[i]); |
bsalomon@google.com | 4a71997 | 2013-02-22 15:10:36 +0000 | [diff] [blame] | 166 | canvas->drawPath(plotPath, plotPaint); |
| 167 | |
| 168 | prevData = fData + i; |
| 169 | } |
| 170 | |
| 171 | fShift += kShiftPerFrame; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | private: |
| 176 | enum { |
| 177 | kNumGraphs = 5, |
| 178 | kPixelsPerTick = 3, |
| 179 | kShiftPerFrame = 1, |
bsalomon@google.com | 4a71997 | 2013-02-22 15:10:36 +0000 | [diff] [blame] | 180 | }; |
| 181 | int fShift; |
| 182 | SkISize fSize; |
| 183 | SkTDArray<SkScalar> fData[kNumGraphs]; |
| 184 | bool fAA; |
| 185 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 186 | typedef Benchmark INHERITED; |
bsalomon@google.com | 4a71997 | 2013-02-22 15:10:36 +0000 | [diff] [blame] | 187 | }; |
| 188 | |
| 189 | ////////////////////////////////////////////////////////////////////////////// |
| 190 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 191 | DEF_BENCH( return new ChartBench(true); ) |
| 192 | DEF_BENCH( return new ChartBench(false); ) |