blob: 4231b72fab58370b69c3902a78ccd8df96e7aac3 [file] [log] [blame]
commit-bot@chromium.org064779a2013-07-01 17:50:29 +00001/*
2 * Copyright 2011 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#include "SkBenchmark.h"
8#include "SkCanvas.h"
9#include "SkPathUtils.h"
10#include "SkRandom.h"
11#include "SkTime.h"
12#include "SkString.h"
13
14#define H 16
15#define W 16
16#define STRIDE 2
17
18//this function is redefined for sample, test, and bench. is there anywhere
skia.committer@gmail.com0d55dd72013-07-02 07:00:59 +000019// I can put it to avoid code duplcation?
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000020static void fillRandomBits( int chars, char* bits ){
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000021 SkRandom rand(SkTime::GetMSecs());
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000022
23 for (int i = 0; i < chars; ++i){
24 bits[i] = rand.nextU();
25 }
skia.committer@gmail.com0d55dd72013-07-02 07:00:59 +000026}
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000027
28static void path_proc(char* bits, SkPath* path) {
29 SkPathUtils::BitsToPath_Path(path, bits, H, W, STRIDE);
30}
31
32static void region_proc(char* bits, SkPath* path) {
33 SkPathUtils::BitsToPath_Region(path, bits, H, W, STRIDE);
34}
35
36/// Emulates the mix of rects blitted by gmail during scrolling
37class PathUtilsBench : public SkBenchmark {
38 typedef void (*Proc)(char*, SkPath*);
39
40 Proc fProc;
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000041 SkString fName;
42 char* bits[H * STRIDE];
skia.committer@gmail.com0d55dd72013-07-02 07:00:59 +000043
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000044public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000045 PathUtilsBench(Proc proc, const char name[]) {
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000046 fProc = proc;
47 fName.printf("pathUtils_%s", name);
skia.committer@gmail.com0d55dd72013-07-02 07:00:59 +000048
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000049
50 }
51
52protected:
53 virtual const char* onGetName() { return fName.c_str(); }
54
commit-bot@chromium.org33614712013-12-03 18:17:16 +000055 virtual void onDraw(const int loops, SkCanvas* canvas) {
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000056
commit-bot@chromium.org33614712013-12-03 18:17:16 +000057 for (int i = 0; i < loops; ++i){
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000058 //create a random 16x16 bitmap
59 fillRandomBits(H * STRIDE, (char*) &bits);
skia.committer@gmail.com0d55dd72013-07-02 07:00:59 +000060
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000061 //use passed function pointer to handle it
62 SkPath path;
63 fProc( (char*) &bits, &path);
64 }
65 }
66
67private:
68 typedef SkBenchmark INHERITED;
69};
70
mtklein@google.com410e6e82013-09-13 19:52:27 +000071DEF_BENCH( return SkNEW_ARGS(PathUtilsBench, (path_proc, "path")); )
72DEF_BENCH( return SkNEW_ARGS(PathUtilsBench, (region_proc, "region")); )