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