| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkBenchmark.h" | 8 #include "SkBenchmark.h" |
| 9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
| 10 #include "SkPaint.h" | 10 #include "SkPaint.h" |
| 11 #include "SkRandom.h" | 11 #include "SkRandom.h" |
| 12 | 12 |
| 13 /** | 13 /** |
| 14 * This is a conversion of samplecode/SampleChart.cpp into a bench. It sure woul
d be nice to be able | 14 * This is a conversion of samplecode/SampleChart.cpp into a bench. It sure woul
d be nice to be able |
| 15 * to write one subclass that can be a GM, bench, and/or Sample. | 15 * to write one subclass that can be a GM, bench, and/or Sample. |
| 16 */ | 16 */ |
| 17 | 17 |
| 18 // Generates y values for the chart plots. | 18 // Generates y values for the chart plots. |
| 19 static void gen_data(SkScalar yAvg, SkScalar ySpread, int count, SkTDArray<SkSca
lar>* dataPts) { | 19 static void gen_data(SkScalar yAvg, SkScalar ySpread, int count, |
| 20 SkRandom* random, SkTDArray<SkScalar>* dataPts) { |
| 20 dataPts->setCount(count); | 21 dataPts->setCount(count); |
| 21 static SkRandom gRandom; | |
| 22 for (int i = 0; i < count; ++i) { | 22 for (int i = 0; i < count; ++i) { |
| 23 (*dataPts)[i] = gRandom.nextRangeScalar(yAvg - SkScalarHalf(ySpread), | 23 (*dataPts)[i] = random->nextRangeScalar(yAvg - SkScalarHalf(ySpread), |
| 24 yAvg + SkScalarHalf(ySpread)); | 24 yAvg + SkScalarHalf(ySpread)); |
| 25 } | 25 } |
| 26 } | 26 } |
| 27 | 27 |
| 28 // Generates a path to stroke along the top of each plot and a fill path for the
area below each | 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 horiz
ontal line at | 29 // plot. The fill path is bounded below by the bottomData plot points or a horiz
ontal line at |
| 30 // yBase if bottomData == NULL. | 30 // yBase if bottomData == NULL. |
| 31 // The plots are animated by rotating the data points by leftShift. | 31 // The plots are animated by rotating the data points by leftShift. |
| 32 static void gen_paths(const SkTDArray<SkScalar>& topData, | 32 static void gen_paths(const SkTDArray<SkScalar>& topData, |
| 33 const SkTDArray<SkScalar>* bottomData, | 33 const SkTDArray<SkScalar>* bottomData, |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 fSize = canvas->getDeviceSize(); | 108 fSize = canvas->getDeviceSize(); |
| 109 sizeChanged = true; | 109 sizeChanged = true; |
| 110 } | 110 } |
| 111 | 111 |
| 112 SkScalar ySpread = SkIntToScalar(fSize.fHeight / 20); | 112 SkScalar ySpread = SkIntToScalar(fSize.fHeight / 20); |
| 113 | 113 |
| 114 SkScalar height = SkIntToScalar(fSize.fHeight); | 114 SkScalar height = SkIntToScalar(fSize.fHeight); |
| 115 if (sizeChanged) { | 115 if (sizeChanged) { |
| 116 int dataPointCount = SkMax32(fSize.fWidth / kPixelsPerTick + 1, 2); | 116 int dataPointCount = SkMax32(fSize.fWidth / kPixelsPerTick + 1, 2); |
| 117 | 117 |
| 118 SkRandom random; |
| 118 for (int i = 0; i < kNumGraphs; ++i) { | 119 for (int i = 0; i < kNumGraphs; ++i) { |
| 119 SkScalar y = (kNumGraphs - i) * (height - ySpread) / (kNumGraphs
+ 1); | 120 SkScalar y = (kNumGraphs - i) * (height - ySpread) / (kNumGraphs
+ 1); |
| 120 fData[i].reset(); | 121 fData[i].reset(); |
| 121 gen_data(y, ySpread, dataPointCount, fData + i); | 122 gen_data(y, ySpread, dataPointCount, &random, fData + i); |
| 122 } | 123 } |
| 123 } | 124 } |
| 124 | 125 |
| 126 SkRandom colorRand; |
| 127 SkColor colors[kNumGraphs]; |
| 128 for (int i = 0; i < kNumGraphs; ++i) { |
| 129 colors[i] = colorRand.nextU() | 0xff000000; |
| 130 } |
| 131 |
| 125 for (int frame = 0; frame < loops; ++frame) { | 132 for (int frame = 0; frame < loops; ++frame) { |
| 126 | 133 |
| 127 canvas->clear(0xFFE0F0E0); | 134 canvas->clear(0xFFE0F0E0); |
| 128 | 135 |
| 129 static SkRandom colorRand; | |
| 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; | 136 SkPath plotPath; |
| 138 SkPath fillPath; | 137 SkPath fillPath; |
| 139 | 138 |
| 140 static const SkScalar kStrokeWidth = SkIntToScalar(2); | 139 static const SkScalar kStrokeWidth = SkIntToScalar(2); |
| 141 SkPaint plotPaint; | 140 SkPaint plotPaint; |
| 142 SkPaint fillPaint; | 141 SkPaint fillPaint; |
| 143 plotPaint.setAntiAlias(fAA); | 142 plotPaint.setAntiAlias(fAA); |
| 144 plotPaint.setStyle(SkPaint::kStroke_Style); | 143 plotPaint.setStyle(SkPaint::kStroke_Style); |
| 145 plotPaint.setStrokeWidth(kStrokeWidth); | 144 plotPaint.setStrokeWidth(kStrokeWidth); |
| 146 plotPaint.setStrokeCap(SkPaint::kRound_Cap); | 145 plotPaint.setStrokeCap(SkPaint::kRound_Cap); |
| 147 plotPaint.setStrokeJoin(SkPaint::kRound_Join); | 146 plotPaint.setStrokeJoin(SkPaint::kRound_Join); |
| 148 fillPaint.setAntiAlias(fAA); | 147 fillPaint.setAntiAlias(fAA); |
| 149 fillPaint.setStyle(SkPaint::kFill_Style); | 148 fillPaint.setStyle(SkPaint::kFill_Style); |
| 150 | 149 |
| 151 SkTDArray<SkScalar>* prevData = NULL; | 150 SkTDArray<SkScalar>* prevData = NULL; |
| 152 for (int i = 0; i < kNumGraphs; ++i) { | 151 for (int i = 0; i < kNumGraphs; ++i) { |
| 153 gen_paths(fData[i], | 152 gen_paths(fData[i], |
| 154 prevData, | 153 prevData, |
| 155 height, | 154 height, |
| 156 0, | 155 0, |
| 157 SkIntToScalar(kPixelsPerTick), | 156 SkIntToScalar(kPixelsPerTick), |
| 158 fShift, | 157 fShift, |
| 159 &plotPath, | 158 &plotPath, |
| 160 &fillPath); | 159 &fillPath); |
| 161 | 160 |
| 162 // Make the fills partially transparent | 161 // Make the fills partially transparent |
| 163 fillPaint.setColor((gColors[i] & 0x00ffffff) | 0x80000000); | 162 fillPaint.setColor((colors[i] & 0x00ffffff) | 0x80000000); |
| 164 canvas->drawPath(fillPath, fillPaint); | 163 canvas->drawPath(fillPath, fillPaint); |
| 165 | 164 |
| 166 plotPaint.setColor(gColors[i]); | 165 plotPaint.setColor(colors[i]); |
| 167 canvas->drawPath(plotPath, plotPaint); | 166 canvas->drawPath(plotPath, plotPaint); |
| 168 | 167 |
| 169 prevData = fData + i; | 168 prevData = fData + i; |
| 170 } | 169 } |
| 171 | 170 |
| 172 fShift += kShiftPerFrame; | 171 fShift += kShiftPerFrame; |
| 173 } | 172 } |
| 174 } | 173 } |
| 175 | 174 |
| 176 private: | 175 private: |
| 177 enum { | 176 enum { |
| 178 kNumGraphs = 5, | 177 kNumGraphs = 5, |
| 179 kPixelsPerTick = 3, | 178 kPixelsPerTick = 3, |
| 180 kShiftPerFrame = 1, | 179 kShiftPerFrame = 1, |
| 181 }; | 180 }; |
| 182 int fShift; | 181 int fShift; |
| 183 SkISize fSize; | 182 SkISize fSize; |
| 184 SkTDArray<SkScalar> fData[kNumGraphs]; | 183 SkTDArray<SkScalar> fData[kNumGraphs]; |
| 185 bool fAA; | 184 bool fAA; |
| 186 | 185 |
| 187 typedef SkBenchmark INHERITED; | 186 typedef SkBenchmark INHERITED; |
| 188 }; | 187 }; |
| 189 | 188 |
| 190 ////////////////////////////////////////////////////////////////////////////// | 189 ////////////////////////////////////////////////////////////////////////////// |
| 191 | 190 |
| 192 DEF_BENCH( return new ChartBench(true); ) | 191 DEF_BENCH( return new ChartBench(true); ) |
| 193 DEF_BENCH( return new ChartBench(false); ) | 192 DEF_BENCH( return new ChartBench(false); ) |
| OLD | NEW |