commit-bot@chromium.org | 064779a | 2013-07-01 17:50:29 +0000 | [diff] [blame] | 1 | /* |
| 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 |
| 19 | // I can put it to avoid code duplcation? |
| 20 | static void fillRandomBits( int chars, char* bits ){ |
| 21 | SkTime time; |
| 22 | SkMWCRandom rand = SkMWCRandom( time.GetMSecs() ); |
| 23 | |
| 24 | for (int i = 0; i < chars; ++i){ |
| 25 | bits[i] = rand.nextU(); |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | static void path_proc(char* bits, SkPath* path) { |
| 30 | SkPathUtils::BitsToPath_Path(path, bits, H, W, STRIDE); |
| 31 | } |
| 32 | |
| 33 | static void region_proc(char* bits, SkPath* path) { |
| 34 | SkPathUtils::BitsToPath_Region(path, bits, H, W, STRIDE); |
| 35 | } |
| 36 | |
| 37 | /// Emulates the mix of rects blitted by gmail during scrolling |
| 38 | class PathUtilsBench : public SkBenchmark { |
| 39 | typedef void (*Proc)(char*, SkPath*); |
| 40 | |
| 41 | Proc fProc; |
| 42 | int fH, fW, fStride; |
| 43 | SkString fName; |
| 44 | char* bits[H * STRIDE]; |
| 45 | |
| 46 | enum { N = SkBENCHLOOP(20) }; |
| 47 | |
| 48 | public: |
| 49 | PathUtilsBench(void* param, Proc proc, const char name[]) : INHERITED(param) { |
| 50 | fProc = proc; |
| 51 | fName.printf("pathUtils_%s", name); |
| 52 | |
| 53 | |
| 54 | } |
| 55 | |
| 56 | protected: |
| 57 | virtual const char* onGetName() { return fName.c_str(); } |
| 58 | |
| 59 | virtual void onDraw(SkCanvas* canvas) { |
| 60 | |
| 61 | for (int i = 0; i < N; ++i){ |
| 62 | //create a random 16x16 bitmap |
| 63 | fillRandomBits(H * STRIDE, (char*) &bits); |
| 64 | |
| 65 | //use passed function pointer to handle it |
| 66 | SkPath path; |
| 67 | fProc( (char*) &bits, &path); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | private: |
| 72 | typedef SkBenchmark INHERITED; |
| 73 | }; |
| 74 | |
| 75 | static SkBenchmark* PU_path(void* p) { return SkNEW_ARGS(PathUtilsBench, (p, path_proc, "path")); } |
| 76 | static SkBenchmark* PU_region(void* p) { return SkNEW_ARGS(PathUtilsBench, (p, region_proc, "region")); } |
| 77 | |
| 78 | static BenchRegistry PU_Path(PU_path); |
| 79 | static BenchRegistry PU_Region(PU_region); |