2012-06-27 19:33:29 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "BenchTimer.h"
|
2012-07-09 19:37:40 +00:00
|
|
|
#include "SamplePipeControllers.h"
|
2012-06-27 19:33:29 +00:00
|
|
|
#include "SkBitmap.h"
|
|
|
|
#include "SkCanvas.h"
|
2012-07-09 19:37:40 +00:00
|
|
|
#include "SkGPipe.h"
|
2012-06-27 19:33:29 +00:00
|
|
|
#include "SkOSFile.h"
|
|
|
|
#include "SkPicture.h"
|
|
|
|
#include "SkStream.h"
|
|
|
|
#include "SkTArray.h"
|
|
|
|
#include "picture_utils.h"
|
|
|
|
|
|
|
|
const int DEFAULT_REPEATS = 100;
|
|
|
|
const int DEFAULT_TILE_WIDTH = 256;
|
|
|
|
const int DEFAULT_TILE_HEIGHT = 256;
|
|
|
|
|
|
|
|
struct Options;
|
2012-07-10 19:53:59 +00:00
|
|
|
static void run_simple_benchmark(SkPicture* picture, const Options&);
|
2012-06-27 19:33:29 +00:00
|
|
|
|
|
|
|
struct Options {
|
|
|
|
int fRepeats;
|
2012-07-10 19:53:59 +00:00
|
|
|
void (*fBenchmark) (SkPicture*, const Options& options);
|
2012-06-27 19:33:29 +00:00
|
|
|
int fTileWidth;
|
|
|
|
int fTileHeight;
|
2012-07-10 18:30:18 +00:00
|
|
|
double fTileWidthPercentage;
|
|
|
|
double fTileHeightPercentage;
|
2012-06-27 19:33:29 +00:00
|
|
|
|
|
|
|
Options() : fRepeats(DEFAULT_REPEATS), fBenchmark(run_simple_benchmark),
|
2012-07-10 18:30:18 +00:00
|
|
|
fTileWidth(DEFAULT_TILE_WIDTH), fTileHeight(DEFAULT_TILE_HEIGHT),
|
|
|
|
fTileWidthPercentage(0), fTileHeightPercentage(0){}
|
2012-06-27 19:33:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void usage(const char* argv0) {
|
|
|
|
SkDebugf("SkPicture benchmarking tool\n");
|
|
|
|
SkDebugf("\n"
|
|
|
|
"Usage: \n"
|
2012-07-09 18:32:08 +00:00
|
|
|
" %s <inputDir>...\n"
|
2012-06-27 19:33:29 +00:00
|
|
|
" [--repeat] [--tile width height]"
|
|
|
|
, argv0);
|
|
|
|
SkDebugf("\n\n");
|
|
|
|
SkDebugf(
|
2012-07-09 18:32:08 +00:00
|
|
|
" inputDir: A list of directories and files to use as input.\n"
|
|
|
|
" Files are expected to have the .skp extension.\n\n");
|
2012-06-27 19:33:29 +00:00
|
|
|
SkDebugf(
|
2012-07-09 19:37:40 +00:00
|
|
|
" --pipe : "
|
|
|
|
"Set to use piping."
|
|
|
|
" Default is to not use piping.\n");
|
|
|
|
SkDebugf(
|
2012-07-16 19:20:14 +00:00
|
|
|
" --record : "
|
|
|
|
"Set to do a picture recording benchmark. Default is not to do this.\n");
|
|
|
|
SkDebugf(
|
2012-06-27 19:33:29 +00:00
|
|
|
" --repeat : "
|
|
|
|
"Set the number of times to repeat each test."
|
|
|
|
" Default is %i.\n", DEFAULT_REPEATS);
|
|
|
|
SkDebugf(
|
2012-07-10 18:30:18 +00:00
|
|
|
" --tile width[%] height[%]: "
|
|
|
|
"Set to use the tiling size and specify the dimensions of each tile.\n"
|
|
|
|
" Default is to not use tiling\n");
|
2012-07-12 19:19:55 +00:00
|
|
|
SkDebugf(
|
|
|
|
" --unflatten: "
|
|
|
|
"Set to do a picture unflattening benchmark. Default is not to do this.\n");
|
2012-06-27 19:33:29 +00:00
|
|
|
}
|
|
|
|
|
2012-07-10 19:53:59 +00:00
|
|
|
static void run_simple_benchmark(SkPicture* picture, const Options& options) {
|
|
|
|
SkBitmap bitmap;
|
|
|
|
sk_tools::setup_bitmap(&bitmap, picture->width(), picture->height());
|
|
|
|
|
2012-06-27 19:33:29 +00:00
|
|
|
SkCanvas canvas(bitmap);
|
|
|
|
|
|
|
|
// We throw this away to remove first time effects (such as paging in this
|
|
|
|
// program)
|
|
|
|
canvas.drawPicture(*picture);
|
|
|
|
|
|
|
|
BenchTimer timer = BenchTimer(NULL);
|
|
|
|
timer.start();
|
|
|
|
for (int i = 0; i < options.fRepeats; ++i) {
|
|
|
|
canvas.drawPicture(*picture);
|
|
|
|
}
|
|
|
|
timer.end();
|
|
|
|
|
2012-07-12 21:16:49 +00:00
|
|
|
printf("simple: msecs = %6.2f\n", timer.fWall / options.fRepeats);
|
2012-06-27 19:33:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct TileInfo {
|
|
|
|
SkBitmap* fBitmap;
|
|
|
|
SkCanvas* fCanvas;
|
|
|
|
};
|
|
|
|
|
2012-07-09 19:51:05 +00:00
|
|
|
static void clip_tile(SkPicture* picture, const TileInfo& tile) {
|
2012-07-13 18:15:04 +00:00
|
|
|
SkRect clip = SkRect::MakeWH(SkIntToScalar(picture->width()),
|
2012-07-10 17:30:58 +00:00
|
|
|
SkIntToScalar(picture->height()));
|
2012-07-09 19:51:05 +00:00
|
|
|
tile.fCanvas->clipRect(clip);
|
|
|
|
}
|
|
|
|
|
2012-07-13 18:15:04 +00:00
|
|
|
static void setup_single_tile(SkPicture* picture, const Options& options,
|
|
|
|
SkTArray<TileInfo>* tiles, int tile_x_start, int tile_y_start) {
|
2012-06-27 19:33:29 +00:00
|
|
|
TileInfo& tile = tiles->push_back();
|
2012-07-13 18:15:04 +00:00
|
|
|
|
2012-06-27 19:33:29 +00:00
|
|
|
tile.fBitmap = new SkBitmap();
|
2012-07-13 18:15:04 +00:00
|
|
|
sk_tools::setup_bitmap(tile.fBitmap, options.fTileWidth, options.fTileHeight);
|
2012-07-09 19:51:05 +00:00
|
|
|
|
2012-07-13 18:15:04 +00:00
|
|
|
tile.fCanvas = new SkCanvas(*(tile.fBitmap));
|
|
|
|
tile.fCanvas->translate(SkIntToScalar(-tile_x_start), SkIntToScalar(-tile_y_start));
|
2012-07-09 19:51:05 +00:00
|
|
|
clip_tile(picture, tile);
|
2012-06-27 19:33:29 +00:00
|
|
|
}
|
|
|
|
|
2012-07-13 18:15:04 +00:00
|
|
|
static void setup_tiles(SkPicture* picture, const Options& options, SkTArray<TileInfo>* tiles) {
|
2012-06-27 19:33:29 +00:00
|
|
|
for (int tile_y_start = 0; tile_y_start < picture->height();
|
|
|
|
tile_y_start += options.fTileHeight) {
|
|
|
|
for (int tile_x_start = 0; tile_x_start < picture->width();
|
|
|
|
tile_x_start += options.fTileWidth) {
|
2012-07-13 18:15:04 +00:00
|
|
|
setup_single_tile(picture, options, tiles, tile_x_start, tile_y_start);
|
2012-06-27 19:33:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-07-10 19:53:59 +00:00
|
|
|
static void run_tile_benchmark(SkPicture* picture, const Options& options) {
|
2012-06-27 19:33:29 +00:00
|
|
|
SkTArray<TileInfo> tiles;
|
2012-07-13 18:15:04 +00:00
|
|
|
setup_tiles(picture, options, &tiles);
|
2012-06-27 19:33:29 +00:00
|
|
|
|
|
|
|
// We throw this away to remove first time effects (such as paging in this
|
|
|
|
// program)
|
|
|
|
for (int j = 0; j < tiles.count(); ++j) {
|
|
|
|
tiles[j].fCanvas->drawPicture(*picture);
|
|
|
|
}
|
|
|
|
|
|
|
|
BenchTimer timer = BenchTimer(NULL);
|
|
|
|
timer.start();
|
|
|
|
for (int i = 0; i < options.fRepeats; ++i) {
|
|
|
|
for (int j = 0; j < tiles.count(); ++j) {
|
|
|
|
tiles[j].fCanvas->drawPicture(*picture);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
timer.end();
|
|
|
|
|
|
|
|
for (int i = 0; i < tiles.count(); ++i) {
|
|
|
|
delete tiles[i].fCanvas;
|
|
|
|
delete tiles[i].fBitmap;
|
|
|
|
}
|
|
|
|
|
2012-07-12 21:16:49 +00:00
|
|
|
printf("%i_tiles_%ix%i: msecs = %6.2f\n", tiles.count(), options.fTileWidth,
|
2012-06-27 19:33:29 +00:00
|
|
|
options.fTileHeight, timer.fWall / options.fRepeats);
|
|
|
|
}
|
|
|
|
|
2012-07-09 19:37:40 +00:00
|
|
|
static void pipe_run(SkPicture* picture, SkCanvas* canvas) {
|
|
|
|
PipeController pipeController(canvas);
|
|
|
|
SkGPipeWriter writer;
|
|
|
|
SkCanvas* pipeCanvas = writer.startRecording(&pipeController);
|
|
|
|
pipeCanvas->drawPicture(*picture);
|
|
|
|
writer.endRecording();
|
|
|
|
}
|
|
|
|
|
2012-07-10 19:53:59 +00:00
|
|
|
static void run_pipe_benchmark(SkPicture* picture, const Options& options) {
|
|
|
|
SkBitmap bitmap;
|
|
|
|
sk_tools::setup_bitmap(&bitmap, picture->width(), picture->height());
|
|
|
|
|
2012-07-09 19:37:40 +00:00
|
|
|
SkCanvas canvas(bitmap);
|
|
|
|
|
|
|
|
// We throw this away to remove first time effects (such as paging in this
|
|
|
|
// program)
|
|
|
|
pipe_run(picture, &canvas);
|
|
|
|
|
|
|
|
BenchTimer timer = BenchTimer(NULL);
|
|
|
|
timer.start();
|
|
|
|
for (int i = 0; i < options.fRepeats; ++i) {
|
|
|
|
pipe_run(picture, &canvas);
|
|
|
|
}
|
|
|
|
timer.end();
|
|
|
|
|
2012-07-12 21:16:49 +00:00
|
|
|
printf("pipe: msecs = %6.2f\n", timer.fWall / options.fRepeats);
|
2012-07-09 19:37:40 +00:00
|
|
|
}
|
|
|
|
|
2012-07-12 19:19:55 +00:00
|
|
|
static void run_unflatten_benchmark(SkPicture* commands, const Options& options) {
|
|
|
|
BenchTimer timer = BenchTimer(NULL);
|
|
|
|
double wall_time = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < options.fRepeats + 1; ++i) {
|
|
|
|
SkPicture replayer;
|
|
|
|
SkCanvas* recorder = replayer.beginRecording(commands->width(), commands->height());
|
|
|
|
|
|
|
|
recorder->drawPicture(*commands);
|
|
|
|
|
|
|
|
timer.start();
|
|
|
|
replayer.endRecording();
|
|
|
|
timer.end();
|
|
|
|
|
|
|
|
// We want to ignore first time effects
|
|
|
|
if (i > 0) {
|
|
|
|
wall_time += timer.fWall;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-12 21:16:49 +00:00
|
|
|
printf("unflatten: msecs = %6.4f\n", wall_time / options.fRepeats);
|
2012-07-12 19:19:55 +00:00
|
|
|
}
|
|
|
|
|
2012-07-16 19:20:14 +00:00
|
|
|
static void run_record_benchmark(SkPicture* commands, const Options& options) {
|
|
|
|
BenchTimer timer = BenchTimer(NULL);
|
|
|
|
double wall_time = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < options.fRepeats + 1; ++i) {
|
|
|
|
SkPicture replayer;
|
|
|
|
SkCanvas* recorder = replayer.beginRecording(commands->width(), commands->height());
|
|
|
|
|
|
|
|
timer.start();
|
|
|
|
recorder->drawPicture(*commands);
|
|
|
|
timer.end();
|
|
|
|
|
|
|
|
// We want to ignore first time effects
|
|
|
|
if (i > 0) {
|
|
|
|
wall_time += timer.fWall;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("record: msecs = %6.5f\n", wall_time / options.fRepeats);
|
|
|
|
}
|
|
|
|
|
2012-07-09 18:32:08 +00:00
|
|
|
static void run_single_benchmark(const SkString& inputPath,
|
2012-07-10 18:30:18 +00:00
|
|
|
Options* options) {
|
2012-06-27 19:33:29 +00:00
|
|
|
SkFILEStream inputStream;
|
|
|
|
|
|
|
|
inputStream.setPath(inputPath.c_str());
|
|
|
|
if (!inputStream.isValid()) {
|
|
|
|
SkDebugf("Could not open file %s\n", inputPath.c_str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkPicture picture(&inputStream);
|
|
|
|
|
2012-07-09 18:32:08 +00:00
|
|
|
SkString filename;
|
|
|
|
sk_tools::get_basename(&filename, inputPath);
|
2012-06-27 19:33:29 +00:00
|
|
|
printf("running bench [%i %i] %s ", picture.width(), picture.height(),
|
2012-07-09 18:32:08 +00:00
|
|
|
filename.c_str());
|
|
|
|
|
2012-07-10 18:30:18 +00:00
|
|
|
if (options->fTileWidthPercentage > 0) {
|
2012-07-26 21:18:31 +00:00
|
|
|
options->fTileWidth = sk_float_ceil2int(
|
|
|
|
float(options->fTileWidthPercentage * picture.width() / 100));
|
2012-07-10 18:30:18 +00:00
|
|
|
}
|
|
|
|
if (options->fTileHeightPercentage > 0) {
|
2012-07-26 21:18:31 +00:00
|
|
|
options->fTileHeight = sk_float_ceil2int(
|
|
|
|
float(options->fTileHeightPercentage * picture.height() / 100));
|
2012-07-10 18:30:18 +00:00
|
|
|
}
|
|
|
|
|
2012-07-10 19:53:59 +00:00
|
|
|
options->fBenchmark(&picture, *options);
|
2012-07-10 18:30:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool is_percentage(char* const string) {
|
|
|
|
SkString skString(string);
|
|
|
|
return skString.endsWith("%");
|
2012-06-27 19:33:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void parse_commandline(int argc, char* const argv[],
|
2012-07-09 18:32:08 +00:00
|
|
|
SkTArray<SkString>* inputs, Options* options) {
|
2012-06-27 19:33:29 +00:00
|
|
|
const char* argv0 = argv[0];
|
|
|
|
char* const* stop = argv + argc;
|
|
|
|
|
|
|
|
for (++argv; argv < stop; ++argv) {
|
|
|
|
if (0 == strcmp(*argv, "--repeat")) {
|
|
|
|
++argv;
|
|
|
|
if (argv < stop) {
|
|
|
|
options->fRepeats = atoi(*argv);
|
|
|
|
if (options->fRepeats < 1) {
|
|
|
|
SkDebugf("--repeat must be given a value > 0\n");
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
SkDebugf("Missing arg for --repeat\n");
|
|
|
|
usage(argv0);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
} else if (0 == strcmp(*argv, "--tile")) {
|
|
|
|
options->fBenchmark = run_tile_benchmark;
|
|
|
|
++argv;
|
|
|
|
if (argv < stop) {
|
2012-07-10 18:30:18 +00:00
|
|
|
if (is_percentage(*argv)) {
|
|
|
|
options->fTileWidthPercentage = atof(*argv);
|
|
|
|
if (!(options->fTileWidthPercentage > 0)) {
|
|
|
|
SkDebugf("--tile must be given a width percentage > 0\n");
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
options->fTileWidth = atoi(*argv);
|
|
|
|
if (!(options->fTileWidth > 0)) {
|
|
|
|
SkDebugf("--tile must be given a width > 0\n");
|
|
|
|
exit(-1);
|
|
|
|
}
|
2012-06-27 19:33:29 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
SkDebugf("Missing width for --tile\n");
|
|
|
|
usage(argv0);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
++argv;
|
|
|
|
if (argv < stop) {
|
2012-07-10 18:30:18 +00:00
|
|
|
if (is_percentage(*argv)) {
|
|
|
|
options->fTileHeightPercentage = atof(*argv);
|
|
|
|
if (!(options->fTileHeightPercentage > 0)) {
|
|
|
|
SkDebugf(
|
|
|
|
"--tile must be given a height percentage > 0\n");
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
options->fTileHeight = atoi(*argv);
|
|
|
|
if (!(options->fTileHeight > 0)) {
|
|
|
|
SkDebugf("--tile must be given a height > 0\n");
|
|
|
|
exit(-1);
|
|
|
|
}
|
2012-06-27 19:33:29 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
SkDebugf("Missing height for --tile\n");
|
2012-07-10 18:30:18 +00:00
|
|
|
usage(argv0);
|
2012-06-27 19:33:29 +00:00
|
|
|
exit(-1);
|
|
|
|
}
|
2012-07-09 19:37:40 +00:00
|
|
|
} else if (0 == strcmp(*argv, "--pipe")) {
|
|
|
|
options->fBenchmark = run_pipe_benchmark;
|
2012-07-16 19:20:14 +00:00
|
|
|
} else if (0 == strcmp(*argv, "--record")) {
|
|
|
|
options->fBenchmark = run_record_benchmark;
|
2012-07-12 19:19:55 +00:00
|
|
|
} else if (0 == strcmp(*argv, "--unflatten")) {
|
|
|
|
options->fBenchmark = run_unflatten_benchmark;
|
2012-06-27 19:33:29 +00:00
|
|
|
} else if (0 == strcmp(*argv, "--help") || 0 == strcmp(*argv, "-h")) {
|
|
|
|
usage(argv0);
|
|
|
|
exit(0);
|
|
|
|
} else {
|
2012-07-09 18:32:08 +00:00
|
|
|
inputs->push_back(SkString(*argv));
|
2012-06-27 19:33:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-09 18:32:08 +00:00
|
|
|
if (inputs->count() < 1) {
|
2012-06-27 19:33:29 +00:00
|
|
|
usage(argv0);
|
|
|
|
exit(-1);
|
|
|
|
}
|
2012-07-09 18:32:08 +00:00
|
|
|
}
|
|
|
|
|
2012-07-10 18:30:18 +00:00
|
|
|
static void process_input(const SkString& input, Options* options) {
|
2012-07-09 18:32:08 +00:00
|
|
|
SkOSFile::Iter iter(input.c_str(), "skp");
|
|
|
|
SkString inputFilename;
|
2012-06-27 19:33:29 +00:00
|
|
|
|
2012-07-09 18:32:08 +00:00
|
|
|
if (iter.next(&inputFilename)) {
|
|
|
|
do {
|
|
|
|
SkString inputPath;
|
2012-07-13 18:22:59 +00:00
|
|
|
sk_tools::make_filepath(&inputPath, input,
|
2012-07-09 18:32:08 +00:00
|
|
|
inputFilename);
|
|
|
|
run_single_benchmark(inputPath, options);
|
|
|
|
} while(iter.next(&inputFilename));
|
|
|
|
} else {
|
|
|
|
run_single_benchmark(input, options);
|
|
|
|
}
|
2012-06-27 19:33:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char* const argv[]) {
|
2012-07-09 18:32:08 +00:00
|
|
|
SkTArray<SkString> inputs;
|
2012-06-27 19:33:29 +00:00
|
|
|
Options options;
|
|
|
|
|
2012-07-09 18:32:08 +00:00
|
|
|
parse_commandline(argc, argv, &inputs, &options);
|
2012-06-27 19:33:29 +00:00
|
|
|
|
2012-07-09 18:32:08 +00:00
|
|
|
for (int i = 0; i < inputs.count(); ++i) {
|
2012-07-10 18:30:18 +00:00
|
|
|
process_input(inputs[i], &options);
|
2012-06-27 19:33:29 +00:00
|
|
|
}
|
|
|
|
}
|