2013-02-22 15:10:36 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "bench/Benchmark.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkPath.h"
|
|
|
|
#include "include/private/SkTDArray.h"
|
|
|
|
#include "include/utils/SkRandom.h"
|
2013-02-22 15:10:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This is a conversion of samplecode/SampleChart.cpp into a bench. It sure would be nice to be able
|
|
|
|
* to write one subclass that can be a GM, bench, and/or Sample.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Generates y values for the chart plots.
|
2014-05-08 15:12:43 +00:00
|
|
|
static void gen_data(SkScalar yAvg, SkScalar ySpread, int count,
|
|
|
|
SkRandom* random, SkTDArray<SkScalar>* dataPts) {
|
2013-02-22 15:10:36 +00:00
|
|
|
dataPts->setCount(count);
|
|
|
|
for (int i = 0; i < count; ++i) {
|
2014-05-08 15:12:43 +00:00
|
|
|
(*dataPts)[i] = random->nextRangeScalar(yAvg - SkScalarHalf(ySpread),
|
2013-02-22 15:10:36 +00:00
|
|
|
yAvg + SkScalarHalf(ySpread));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generates a path to stroke along the top of each plot and a fill path for the area below each
|
|
|
|
// plot. The fill path is bounded below by the bottomData plot points or a horizontal line at
|
2015-08-27 14:41:13 +00:00
|
|
|
// yBase if bottomData == nullptr.
|
2013-02-22 15:10:36 +00:00
|
|
|
// The plots are animated by rotating the data points by leftShift.
|
2013-09-26 19:23:03 +00:00
|
|
|
static void gen_paths(const SkTDArray<SkScalar>& topData,
|
|
|
|
const SkTDArray<SkScalar>* bottomData,
|
|
|
|
SkScalar yBase,
|
|
|
|
SkScalar xLeft, SkScalar xDelta,
|
|
|
|
int leftShift,
|
|
|
|
SkPath* plot, SkPath* fill) {
|
2013-02-22 15:10:36 +00:00
|
|
|
plot->rewind();
|
|
|
|
fill->rewind();
|
|
|
|
plot->incReserve(topData.count());
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == bottomData) {
|
2013-02-22 15:10:36 +00:00
|
|
|
fill->incReserve(topData.count() + 2);
|
|
|
|
} else {
|
|
|
|
fill->incReserve(2 * topData.count());
|
|
|
|
}
|
|
|
|
|
|
|
|
leftShift %= topData.count();
|
|
|
|
SkScalar x = xLeft;
|
|
|
|
|
|
|
|
// Account for the leftShift using two loops
|
|
|
|
int shiftToEndCount = topData.count() - leftShift;
|
|
|
|
plot->moveTo(x, topData[leftShift]);
|
|
|
|
fill->moveTo(x, topData[leftShift]);
|
|
|
|
|
|
|
|
for (int i = 1; i < shiftToEndCount; ++i) {
|
|
|
|
plot->lineTo(x, topData[i + leftShift]);
|
|
|
|
fill->lineTo(x, topData[i + leftShift]);
|
|
|
|
x += xDelta;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < leftShift; ++i) {
|
|
|
|
plot->lineTo(x, topData[i]);
|
|
|
|
fill->lineTo(x, topData[i]);
|
|
|
|
x += xDelta;
|
|
|
|
}
|
|
|
|
|
2014-09-05 20:34:00 +00:00
|
|
|
if (bottomData) {
|
2013-02-22 15:10:36 +00:00
|
|
|
SkASSERT(bottomData->count() == topData.count());
|
|
|
|
// iterate backwards over the previous graph's data to generate the bottom of the filled
|
|
|
|
// area (and account for leftShift).
|
|
|
|
for (int i = 0; i < leftShift; ++i) {
|
|
|
|
x -= xDelta;
|
|
|
|
fill->lineTo(x, (*bottomData)[leftShift - 1 - i]);
|
|
|
|
}
|
|
|
|
for (int i = 0; i < shiftToEndCount; ++i) {
|
|
|
|
x -= xDelta;
|
|
|
|
fill->lineTo(x, (*bottomData)[bottomData->count() - 1 - i]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fill->lineTo(x - xDelta, yBase);
|
|
|
|
fill->lineTo(xLeft, yBase);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A set of scrolling line plots with the area between each plot filled. Stresses out GPU path
|
|
|
|
// filling
|
2014-06-19 19:32:29 +00:00
|
|
|
class ChartBench : public Benchmark {
|
2013-02-22 15:10:36 +00:00
|
|
|
public:
|
2013-09-13 19:52:27 +00:00
|
|
|
ChartBench(bool aa) {
|
2013-02-22 15:10:36 +00:00
|
|
|
fShift = 0;
|
|
|
|
fAA = aa;
|
2013-02-22 16:07:59 +00:00
|
|
|
fSize.fWidth = -1;
|
|
|
|
fSize.fHeight = -1;
|
2013-02-22 15:10:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
const char* onGetName() override {
|
2013-02-22 15:10:36 +00:00
|
|
|
if (fAA) {
|
|
|
|
return "chart_aa";
|
|
|
|
} else {
|
|
|
|
return "chart_bw";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-01 16:43:39 +00:00
|
|
|
void onDraw(int loops, SkCanvas* canvas) override {
|
2013-02-22 15:10:36 +00:00
|
|
|
bool sizeChanged = false;
|
2017-02-22 18:21:42 +00:00
|
|
|
if (canvas->getBaseLayerSize() != fSize) {
|
|
|
|
fSize = canvas->getBaseLayerSize();
|
2013-02-22 15:10:36 +00:00
|
|
|
sizeChanged = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkScalar ySpread = SkIntToScalar(fSize.fHeight / 20);
|
|
|
|
|
|
|
|
SkScalar height = SkIntToScalar(fSize.fHeight);
|
2013-02-22 16:07:59 +00:00
|
|
|
if (sizeChanged) {
|
2020-02-06 16:25:43 +00:00
|
|
|
int dataPointCount = std::max(fSize.fWidth / kPixelsPerTick + 1, 2);
|
2013-02-22 15:10:36 +00:00
|
|
|
|
2014-05-08 15:12:43 +00:00
|
|
|
SkRandom random;
|
2013-02-22 16:07:59 +00:00
|
|
|
for (int i = 0; i < kNumGraphs; ++i) {
|
|
|
|
SkScalar y = (kNumGraphs - i) * (height - ySpread) / (kNumGraphs + 1);
|
|
|
|
fData[i].reset();
|
2014-05-08 15:12:43 +00:00
|
|
|
gen_data(y, ySpread, dataPointCount, &random, fData + i);
|
2013-02-22 15:10:36 +00:00
|
|
|
}
|
2013-02-22 16:07:59 +00:00
|
|
|
}
|
|
|
|
|
2014-05-08 15:12:43 +00:00
|
|
|
SkRandom colorRand;
|
|
|
|
SkColor colors[kNumGraphs];
|
|
|
|
for (int i = 0; i < kNumGraphs; ++i) {
|
|
|
|
colors[i] = colorRand.nextU() | 0xff000000;
|
|
|
|
}
|
|
|
|
|
2013-12-03 18:17:16 +00:00
|
|
|
for (int frame = 0; frame < loops; ++frame) {
|
2013-02-22 15:10:36 +00:00
|
|
|
SkPath plotPath;
|
|
|
|
SkPath fillPath;
|
|
|
|
|
|
|
|
static const SkScalar kStrokeWidth = SkIntToScalar(2);
|
|
|
|
SkPaint plotPaint;
|
|
|
|
SkPaint fillPaint;
|
|
|
|
plotPaint.setAntiAlias(fAA);
|
|
|
|
plotPaint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
plotPaint.setStrokeWidth(kStrokeWidth);
|
|
|
|
plotPaint.setStrokeCap(SkPaint::kRound_Cap);
|
|
|
|
plotPaint.setStrokeJoin(SkPaint::kRound_Join);
|
|
|
|
fillPaint.setAntiAlias(fAA);
|
|
|
|
fillPaint.setStyle(SkPaint::kFill_Style);
|
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
SkTDArray<SkScalar>* prevData = nullptr;
|
2013-02-22 15:10:36 +00:00
|
|
|
for (int i = 0; i < kNumGraphs; ++i) {
|
|
|
|
gen_paths(fData[i],
|
|
|
|
prevData,
|
|
|
|
height,
|
|
|
|
0,
|
|
|
|
SkIntToScalar(kPixelsPerTick),
|
|
|
|
fShift,
|
|
|
|
&plotPath,
|
|
|
|
&fillPath);
|
|
|
|
|
|
|
|
// Make the fills partially transparent
|
2014-05-08 15:12:43 +00:00
|
|
|
fillPaint.setColor((colors[i] & 0x00ffffff) | 0x80000000);
|
2013-02-22 15:10:36 +00:00
|
|
|
canvas->drawPath(fillPath, fillPaint);
|
|
|
|
|
2014-05-08 15:12:43 +00:00
|
|
|
plotPaint.setColor(colors[i]);
|
2013-02-22 15:10:36 +00:00
|
|
|
canvas->drawPath(plotPath, plotPaint);
|
|
|
|
|
|
|
|
prevData = fData + i;
|
|
|
|
}
|
|
|
|
|
|
|
|
fShift += kShiftPerFrame;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
enum {
|
|
|
|
kNumGraphs = 5,
|
|
|
|
kPixelsPerTick = 3,
|
|
|
|
kShiftPerFrame = 1,
|
|
|
|
};
|
|
|
|
int fShift;
|
|
|
|
SkISize fSize;
|
|
|
|
SkTDArray<SkScalar> fData[kNumGraphs];
|
|
|
|
bool fAA;
|
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = Benchmark;
|
2013-02-22 15:10:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-09-13 19:52:27 +00:00
|
|
|
DEF_BENCH( return new ChartBench(true); )
|
|
|
|
DEF_BENCH( return new ChartBench(false); )
|