aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/bench_pictures_main.cpp
blob: a9cca9658f96e0a36a565cfc8ff5b62e1bb34d37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
 * 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"
#include "SamplePipeControllers.h"
#include "SkBitmap.h"
#include "SkCanvas.h"
#include "SkGPipe.h"
#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;
static void run_simple_benchmark(SkPicture* picture, const Options&);

struct Options {
    int fRepeats;
    void (*fBenchmark) (SkPicture*, const Options& options);
    int fTileWidth;
    int fTileHeight;
    double fTileWidthPercentage;
    double fTileHeightPercentage;

    Options() : fRepeats(DEFAULT_REPEATS), fBenchmark(run_simple_benchmark),
    fTileWidth(DEFAULT_TILE_WIDTH), fTileHeight(DEFAULT_TILE_HEIGHT),
    fTileWidthPercentage(0), fTileHeightPercentage(0){}
};

static void usage(const char* argv0) {
    SkDebugf("SkPicture benchmarking tool\n");
    SkDebugf("\n"
"Usage: \n"
"     %s <inputDir>...\n"
"     [--repeat] [--tile width height]"
, argv0);
    SkDebugf("\n\n");
    SkDebugf(
"     inputDir:  A list of directories and files to use as input.\n"
"                    Files are expected to have the .skp extension.\n\n");
    SkDebugf(
"     --pipe : "
"Set to use piping."
" Default is to not use piping.\n");
    SkDebugf(
"    --record : "
"Set to do a picture recording benchmark. Default is not to do this.\n");
    SkDebugf(
"     --repeat : "
"Set the number of times to repeat each test."
" Default is %i.\n", DEFAULT_REPEATS);
    SkDebugf(
"     --tile width[%] height[%]: "
"Set to use the tiling size and specify the dimensions of each tile.\n"
"                                     Default is to not use tiling\n");
    SkDebugf(
"     --unflatten: "
"Set to do a picture unflattening benchmark. Default is not to do this.\n");
}

static void run_simple_benchmark(SkPicture* picture, const Options& options) {
    SkBitmap bitmap;
    sk_tools::setup_bitmap(&bitmap, picture->width(), picture->height());

    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();

    printf("simple: msecs = %6.2f\n", timer.fWall / options.fRepeats);
}

struct TileInfo {
    SkBitmap* fBitmap;
    SkCanvas* fCanvas;
};

static void clip_tile(SkPicture* picture, const TileInfo& tile) {
    SkRect clip = SkRect::MakeWH(SkIntToScalar(picture->width()),
                                 SkIntToScalar(picture->height()));
    tile.fCanvas->clipRect(clip);
}

static void setup_single_tile(SkPicture* picture, const Options& options,
                              SkTArray<TileInfo>* tiles, int tile_x_start, int tile_y_start) {
    TileInfo& tile = tiles->push_back();

    tile.fBitmap = new SkBitmap();
    sk_tools::setup_bitmap(tile.fBitmap, options.fTileWidth, options.fTileHeight);

    tile.fCanvas = new SkCanvas(*(tile.fBitmap));
    tile.fCanvas->translate(SkIntToScalar(-tile_x_start), SkIntToScalar(-tile_y_start));
    clip_tile(picture, tile);
}

static void setup_tiles(SkPicture* picture, const Options& options, SkTArray<TileInfo>* tiles) {
    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) {
            setup_single_tile(picture, options, tiles, tile_x_start, tile_y_start);
        }
    }

}

static void run_tile_benchmark(SkPicture* picture, const Options& options) {
    SkTArray<TileInfo> tiles;
    setup_tiles(picture, options, &tiles);

    // 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;
    }

    printf("%i_tiles_%ix%i: msecs = %6.2f\n", tiles.count(), options.fTileWidth,
           options.fTileHeight, timer.fWall / options.fRepeats);
}

static void pipe_run(SkPicture* picture, SkCanvas* canvas) {
    PipeController pipeController(canvas);
    SkGPipeWriter writer;
    SkCanvas* pipeCanvas = writer.startRecording(&pipeController);
    pipeCanvas->drawPicture(*picture);
    writer.endRecording();
}

static void run_pipe_benchmark(SkPicture* picture, const Options& options) {
    SkBitmap bitmap;
    sk_tools::setup_bitmap(&bitmap, picture->width(), picture->height());

    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();

    printf("pipe: msecs = %6.2f\n", timer.fWall / options.fRepeats);
}

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;
        }
    }

    printf("unflatten: msecs = %6.4f\n", wall_time / options.fRepeats);
}

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);
}

static void run_single_benchmark(const SkString& inputPath,
                                 Options* options) {
    SkFILEStream inputStream;

    inputStream.setPath(inputPath.c_str());
    if (!inputStream.isValid()) {
        SkDebugf("Could not open file %s\n", inputPath.c_str());
        return;
    }

    SkPicture picture(&inputStream);

    SkString filename;
    sk_tools::get_basename(&filename, inputPath);
    printf("running bench [%i %i] %s ", picture.width(), picture.height(),
           filename.c_str());

    if (options->fTileWidthPercentage > 0) {
        options->fTileWidth = sk_float_ceil2int(options->fTileWidthPercentage * picture.width()
                                                / 100);
    }
    if (options->fTileHeightPercentage > 0) {
        options->fTileHeight = sk_float_ceil2int(options->fTileHeightPercentage * picture.height()
                                                 / 100);
    }

    options->fBenchmark(&picture, *options);
}

static bool is_percentage(char* const string) {
    SkString skString(string);
    return skString.endsWith("%");
}

static void parse_commandline(int argc, char* const argv[],
                              SkTArray<SkString>* inputs, Options* options) {
    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) {
                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);
                    }
                }
            } else {
                SkDebugf("Missing width for --tile\n");
                usage(argv0);
                exit(-1);
            }
            ++argv;
            if (argv < stop) {
                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);
                    }
                }
            } else {
                SkDebugf("Missing height for --tile\n");
                usage(argv0);
                exit(-1);
            }
        } else if (0 == strcmp(*argv, "--pipe")) {
            options->fBenchmark = run_pipe_benchmark;
        } else if (0 == strcmp(*argv, "--record")) {
            options->fBenchmark = run_record_benchmark;
        } else if (0 == strcmp(*argv, "--unflatten")) {
            options->fBenchmark = run_unflatten_benchmark;
        } else if (0 == strcmp(*argv, "--help") || 0 == strcmp(*argv, "-h")) {
            usage(argv0);
            exit(0);
        } else {
            inputs->push_back(SkString(*argv));
        }
    }

    if (inputs->count() < 1) {
        usage(argv0);
        exit(-1);
    }
}

static void process_input(const SkString& input, Options* options) {
    SkOSFile::Iter iter(input.c_str(), "skp");
    SkString inputFilename;

    if (iter.next(&inputFilename)) {
        do {
            SkString inputPath;
            sk_tools::make_filepath(&inputPath, input,
                                    inputFilename);
            run_single_benchmark(inputPath, options);
        } while(iter.next(&inputFilename));
    } else {
          run_single_benchmark(input, options);
    }
}

int main(int argc, char* const argv[]) {
    SkTArray<SkString> inputs;
    Options options;

    parse_commandline(argc, argv, &inputs, &options);

    for (int i = 0; i < inputs.count(); ++i) {
        process_input(inputs[i], &options);
    }
}