djsollen@google.com | dde718c | 2012-05-30 16:50:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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 "SkColor.h" |
| 10 | #include "SkPaint.h" |
| 11 | #include "SkPicture.h" |
| 12 | #include "SkPoint.h" |
reed@google.com | 7fe6409 | 2012-07-10 13:17:45 +0000 | [diff] [blame] | 13 | #include "SkRandom.h" |
djsollen@google.com | dde718c | 2012-05-30 16:50:11 +0000 | [diff] [blame] | 14 | #include "SkRect.h" |
| 15 | #include "SkString.h" |
| 16 | |
| 17 | class PictureRecordBench : public SkBenchmark { |
| 18 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 19 | PictureRecordBench(const char name[]) { |
djsollen@google.com | dde718c | 2012-05-30 16:50:11 +0000 | [diff] [blame] | 20 | fName.printf("picture_record_%s", name); |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | virtual bool isSuitableFor(Backend backend) SK_OVERRIDE { |
| 24 | return backend == kNonRendering_Backend; |
djsollen@google.com | dde718c | 2012-05-30 16:50:11 +0000 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | enum { |
djsollen@google.com | dde718c | 2012-05-30 16:50:11 +0000 | [diff] [blame] | 28 | PICTURE_WIDTH = 1000, |
| 29 | PICTURE_HEIGHT = 4000, |
| 30 | }; |
| 31 | protected: |
mtklein@google.com | 93d2f55 | 2013-09-17 18:58:53 +0000 | [diff] [blame] | 32 | virtual const char* onGetName() SK_OVERRIDE { |
djsollen@google.com | dde718c | 2012-05-30 16:50:11 +0000 | [diff] [blame] | 33 | return fName.c_str(); |
| 34 | } |
djsollen@google.com | dde718c | 2012-05-30 16:50:11 +0000 | [diff] [blame] | 35 | private: |
mtklein@google.com | 93d2f55 | 2013-09-17 18:58:53 +0000 | [diff] [blame] | 36 | SkString fName; |
djsollen@google.com | dde718c | 2012-05-30 16:50:11 +0000 | [diff] [blame] | 37 | typedef SkBenchmark INHERITED; |
| 38 | }; |
| 39 | |
mtklein@google.com | 93d2f55 | 2013-09-17 18:58:53 +0000 | [diff] [blame] | 40 | |
| 41 | static const int kMaxLoopsPerCanvas = 10000; |
| 42 | |
djsollen@google.com | dde718c | 2012-05-30 16:50:11 +0000 | [diff] [blame] | 43 | /* |
| 44 | * An SkPicture has internal dictionaries to store bitmaps, matrices, paints, |
| 45 | * and regions. This bench populates those dictionaries to test the speed of |
| 46 | * reading and writing to those particular dictionary data structures. |
| 47 | */ |
| 48 | class DictionaryRecordBench : public PictureRecordBench { |
| 49 | public: |
mtklein@google.com | 93d2f55 | 2013-09-17 18:58:53 +0000 | [diff] [blame] | 50 | DictionaryRecordBench() : INHERITED("dictionaries") {} |
djsollen@google.com | dde718c | 2012-05-30 16:50:11 +0000 | [diff] [blame] | 51 | |
djsollen@google.com | dde718c | 2012-05-30 16:50:11 +0000 | [diff] [blame] | 52 | protected: |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 53 | virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE { |
mtklein@google.com | 93d2f55 | 2013-09-17 18:58:53 +0000 | [diff] [blame] | 54 | SkAutoTDelete<SkPicture> picture; |
| 55 | SkCanvas* canvas = NULL; |
djsollen@google.com | dde718c | 2012-05-30 16:50:11 +0000 | [diff] [blame] | 56 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 57 | const SkPoint translateDelta = getTranslateDelta(loops); |
djsollen@google.com | dde718c | 2012-05-30 16:50:11 +0000 | [diff] [blame] | 58 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 59 | for (int i = 0; i < loops; i++) { |
mtklein@google.com | 93d2f55 | 2013-09-17 18:58:53 +0000 | [diff] [blame] | 60 | if (0 == i % kMaxLoopsPerCanvas) { |
| 61 | picture.reset(SkNEW(SkPicture)); |
| 62 | canvas = picture->beginRecording(PICTURE_WIDTH, PICTURE_HEIGHT); |
| 63 | } |
djsollen@google.com | dde718c | 2012-05-30 16:50:11 +0000 | [diff] [blame] | 64 | |
| 65 | SkColor color = SK_ColorYELLOW + (i % 255); |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 66 | SkIRect rect = SkIRect::MakeWH(i % PICTURE_WIDTH, i % PICTURE_HEIGHT); |
djsollen@google.com | dde718c | 2012-05-30 16:50:11 +0000 | [diff] [blame] | 67 | |
| 68 | canvas->save(); |
| 69 | |
| 70 | // set the clip to the given region |
| 71 | SkRegion region; |
| 72 | region.setRect(rect); |
| 73 | canvas->clipRegion(region); |
| 74 | |
| 75 | // fill the clip with a color |
| 76 | SkPaint paint; |
| 77 | paint.setColor(color); |
| 78 | canvas->drawPaint(paint); |
| 79 | |
| 80 | // set a matrix on the canvas |
| 81 | SkMatrix matrix; |
| 82 | matrix.setRotate(SkIntToScalar(i % 360)); |
| 83 | canvas->setMatrix(matrix); |
| 84 | |
| 85 | // create a simple bitmap |
| 86 | SkBitmap bitmap; |
| 87 | bitmap.setConfig(SkBitmap::kRGB_565_Config, 10, 10); |
| 88 | bitmap.allocPixels(); |
| 89 | |
| 90 | // draw a single color into the bitmap |
| 91 | SkCanvas bitmapCanvas(bitmap); |
| 92 | bitmapCanvas.drawColor(SkColorSetA(color, i % 255)); |
| 93 | |
| 94 | // draw the bitmap onto the canvas |
| 95 | canvas->drawBitmapMatrix(bitmap, matrix); |
| 96 | |
| 97 | canvas->restore(); |
| 98 | canvas->translate(translateDelta.fX, translateDelta.fY); |
| 99 | } |
| 100 | } |
| 101 | |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 102 | SkPoint getTranslateDelta(int M) { |
djsollen@google.com | dde718c | 2012-05-30 16:50:11 +0000 | [diff] [blame] | 103 | SkIPoint canvasSize = onGetSize(); |
| 104 | return SkPoint::Make(SkIntToScalar((PICTURE_WIDTH - canvasSize.fX)/M), |
| 105 | SkIntToScalar((PICTURE_HEIGHT- canvasSize.fY)/M)); |
| 106 | } |
| 107 | private: |
| 108 | typedef PictureRecordBench INHERITED; |
| 109 | }; |
| 110 | |
junov@chromium.org | ef76060 | 2012-06-27 20:03:16 +0000 | [diff] [blame] | 111 | /* |
| 112 | * Populates the SkPaint dictionary with a large number of unique paint |
| 113 | * objects that differ only by color |
| 114 | */ |
| 115 | class UniquePaintDictionaryRecordBench : public PictureRecordBench { |
| 116 | public: |
mtklein@google.com | 93d2f55 | 2013-09-17 18:58:53 +0000 | [diff] [blame] | 117 | UniquePaintDictionaryRecordBench() : INHERITED("unique_paint_dictionary") { } |
junov@chromium.org | ef76060 | 2012-06-27 20:03:16 +0000 | [diff] [blame] | 118 | |
junov@chromium.org | ef76060 | 2012-06-27 20:03:16 +0000 | [diff] [blame] | 119 | protected: |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 120 | virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 121 | SkRandom rand; |
mtklein@google.com | 5e5239e | 2013-09-17 17:14:25 +0000 | [diff] [blame] | 122 | SkPaint paint; |
| 123 | SkAutoTDelete<SkPicture> picture; |
| 124 | SkCanvas* canvas = NULL; |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 125 | for (int i = 0; i < loops; i++) { |
mtklein@google.com | 93d2f55 | 2013-09-17 18:58:53 +0000 | [diff] [blame] | 126 | if (0 == i % kMaxLoopsPerCanvas) { |
mtklein@google.com | 5e5239e | 2013-09-17 17:14:25 +0000 | [diff] [blame] | 127 | picture.reset(SkNEW(SkPicture)); |
| 128 | canvas = picture->beginRecording(PICTURE_WIDTH, PICTURE_HEIGHT); |
| 129 | } |
reed@google.com | 7fe6409 | 2012-07-10 13:17:45 +0000 | [diff] [blame] | 130 | paint.setColor(rand.nextU()); |
junov@chromium.org | ef76060 | 2012-06-27 20:03:16 +0000 | [diff] [blame] | 131 | canvas->drawPaint(paint); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | private: |
| 136 | typedef PictureRecordBench INHERITED; |
| 137 | }; |
| 138 | |
| 139 | /* |
| 140 | * Populates the SkPaint dictionary with a number of unique paint |
commit-bot@chromium.org | 0c4e21d | 2013-07-11 14:26:09 +0000 | [diff] [blame] | 141 | * objects that get reused repeatedly. |
| 142 | * |
| 143 | * Re-creating the paint objects in the inner loop slows the benchmark down 10%. |
| 144 | * Using setColor(i % objCount) instead of a random color creates a very high rate |
| 145 | * of hash conflicts, slowing us down 12%. |
junov@chromium.org | ef76060 | 2012-06-27 20:03:16 +0000 | [diff] [blame] | 146 | */ |
| 147 | class RecurringPaintDictionaryRecordBench : public PictureRecordBench { |
| 148 | public: |
mtklein@google.com | 93d2f55 | 2013-09-17 18:58:53 +0000 | [diff] [blame] | 149 | RecurringPaintDictionaryRecordBench() : INHERITED("recurring_paint_dictionary") { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 150 | SkRandom rand; |
commit-bot@chromium.org | 0c4e21d | 2013-07-11 14:26:09 +0000 | [diff] [blame] | 151 | for (int i = 0; i < ObjCount; i++) { |
| 152 | fPaint[i].setColor(rand.nextU()); |
| 153 | } |
| 154 | } |
junov@chromium.org | ef76060 | 2012-06-27 20:03:16 +0000 | [diff] [blame] | 155 | |
| 156 | enum { |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 157 | ObjCount = 100, // number of unique paint objects |
junov@chromium.org | ef76060 | 2012-06-27 20:03:16 +0000 | [diff] [blame] | 158 | }; |
| 159 | protected: |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 160 | virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE { |
mtklein@google.com | 93d2f55 | 2013-09-17 18:58:53 +0000 | [diff] [blame] | 161 | SkPicture picture; |
| 162 | SkCanvas* canvas = picture.beginRecording(PICTURE_WIDTH, PICTURE_HEIGHT); |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 163 | for (int i = 0; i < loops; i++) { |
commit-bot@chromium.org | 0c4e21d | 2013-07-11 14:26:09 +0000 | [diff] [blame] | 164 | canvas->drawPaint(fPaint[i % ObjCount]); |
junov@chromium.org | ef76060 | 2012-06-27 20:03:16 +0000 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
| 168 | private: |
commit-bot@chromium.org | 0c4e21d | 2013-07-11 14:26:09 +0000 | [diff] [blame] | 169 | SkPaint fPaint [ObjCount]; |
junov@chromium.org | ef76060 | 2012-06-27 20:03:16 +0000 | [diff] [blame] | 170 | typedef PictureRecordBench INHERITED; |
| 171 | }; |
| 172 | |
djsollen@google.com | dde718c | 2012-05-30 16:50:11 +0000 | [diff] [blame] | 173 | /////////////////////////////////////////////////////////////////////////////// |
| 174 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 175 | DEF_BENCH( return new DictionaryRecordBench(); ) |
| 176 | DEF_BENCH( return new UniquePaintDictionaryRecordBench(); ) |
| 177 | DEF_BENCH( return new RecurringPaintDictionaryRecordBench(); ) |