djsollen@google.com | 809a2a9 | 2012-02-23 20:57:09 +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 | */ |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 7 | #include "Benchmark.h" |
djsollen@google.com | 809a2a9 | 2012-02-23 20:57:09 +0000 | [diff] [blame] | 8 | #include "SkCanvas.h" |
| 9 | #include "SkColor.h" |
| 10 | #include "SkPaint.h" |
| 11 | #include "SkPicture.h" |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 12 | #include "SkPictureRecorder.h" |
djsollen@google.com | 809a2a9 | 2012-02-23 20:57:09 +0000 | [diff] [blame] | 13 | #include "SkPoint.h" |
mtklein | bf5dd41 | 2014-11-11 10:39:27 -0800 | [diff] [blame] | 14 | #include "SkRandom.h" |
djsollen@google.com | 809a2a9 | 2012-02-23 20:57:09 +0000 | [diff] [blame] | 15 | #include "SkRect.h" |
| 16 | #include "SkString.h" |
| 17 | |
| 18 | // This is designed to emulate about 4 screens of textual content |
| 19 | |
| 20 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 21 | class PicturePlaybackBench : public Benchmark { |
djsollen@google.com | 809a2a9 | 2012-02-23 20:57:09 +0000 | [diff] [blame] | 22 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 23 | PicturePlaybackBench(const char name[]) { |
djsollen@google.com | 809a2a9 | 2012-02-23 20:57:09 +0000 | [diff] [blame] | 24 | fName.printf("picture_playback_%s", name); |
| 25 | fPictureWidth = SkIntToScalar(PICTURE_WIDTH); |
| 26 | fPictureHeight = SkIntToScalar(PICTURE_HEIGHT); |
| 27 | fTextSize = SkIntToScalar(TEXT_SIZE); |
| 28 | } |
| 29 | |
| 30 | enum { |
djsollen@google.com | 809a2a9 | 2012-02-23 20:57:09 +0000 | [diff] [blame] | 31 | PICTURE_WIDTH = 1000, |
| 32 | PICTURE_HEIGHT = 4000, |
| 33 | TEXT_SIZE = 10 |
| 34 | }; |
| 35 | protected: |
| 36 | virtual const char* onGetName() { |
| 37 | return fName.c_str(); |
| 38 | } |
| 39 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 40 | virtual void onDraw(int loops, SkCanvas* canvas) { |
djsollen@google.com | 809a2a9 | 2012-02-23 20:57:09 +0000 | [diff] [blame] | 41 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 42 | SkPictureRecorder recorder; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 43 | SkCanvas* pCanvas = recorder.beginRecording(PICTURE_WIDTH, PICTURE_HEIGHT, nullptr, 0); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 44 | this->recordCanvas(pCanvas); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 45 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
djsollen@google.com | 809a2a9 | 2012-02-23 20:57:09 +0000 | [diff] [blame] | 46 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 47 | const SkPoint translateDelta = getTranslateDelta(loops); |
djsollen@google.com | 809a2a9 | 2012-02-23 20:57:09 +0000 | [diff] [blame] | 48 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 49 | for (int i = 0; i < loops; i++) { |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame] | 50 | picture->playback(canvas); |
djsollen@google.com | 809a2a9 | 2012-02-23 20:57:09 +0000 | [diff] [blame] | 51 | canvas->translate(translateDelta.fX, translateDelta.fY); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | virtual void recordCanvas(SkCanvas* canvas) = 0; |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 56 | virtual SkPoint getTranslateDelta(int N) { |
djsollen@google.com | 809a2a9 | 2012-02-23 20:57:09 +0000 | [diff] [blame] | 57 | SkIPoint canvasSize = onGetSize(); |
| 58 | return SkPoint::Make(SkIntToScalar((PICTURE_WIDTH - canvasSize.fX)/N), |
| 59 | SkIntToScalar((PICTURE_HEIGHT- canvasSize.fY)/N)); |
| 60 | } |
| 61 | |
| 62 | SkString fName; |
| 63 | SkScalar fPictureWidth; |
| 64 | SkScalar fPictureHeight; |
| 65 | SkScalar fTextSize; |
| 66 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 67 | typedef Benchmark INHERITED; |
djsollen@google.com | 809a2a9 | 2012-02-23 20:57:09 +0000 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | |
| 71 | class TextPlaybackBench : public PicturePlaybackBench { |
| 72 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 73 | TextPlaybackBench() : INHERITED("drawText") { } |
djsollen@google.com | 809a2a9 | 2012-02-23 20:57:09 +0000 | [diff] [blame] | 74 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 75 | void recordCanvas(SkCanvas* canvas) override { |
djsollen@google.com | 809a2a9 | 2012-02-23 20:57:09 +0000 | [diff] [blame] | 76 | SkPaint paint; |
| 77 | paint.setTextSize(fTextSize); |
| 78 | paint.setColor(SK_ColorBLACK); |
| 79 | |
| 80 | const char* text = "Hamburgefons"; |
| 81 | size_t len = strlen(text); |
| 82 | const SkScalar textWidth = paint.measureText(text, len); |
| 83 | |
| 84 | for (SkScalar x = 0; x < fPictureWidth; x += textWidth) { |
| 85 | for (SkScalar y = 0; y < fPictureHeight; y += fTextSize) { |
| 86 | canvas->drawText(text, len, x, y, paint); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | private: |
| 91 | typedef PicturePlaybackBench INHERITED; |
| 92 | }; |
| 93 | |
| 94 | class PosTextPlaybackBench : public PicturePlaybackBench { |
| 95 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 96 | PosTextPlaybackBench(bool drawPosH) |
| 97 | : INHERITED(drawPosH ? "drawPosTextH" : "drawPosText") |
djsollen@google.com | 710c269 | 2012-02-27 16:22:48 +0000 | [diff] [blame] | 98 | , fDrawPosH(drawPosH) { } |
djsollen@google.com | 809a2a9 | 2012-02-23 20:57:09 +0000 | [diff] [blame] | 99 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 100 | void recordCanvas(SkCanvas* canvas) override { |
djsollen@google.com | 809a2a9 | 2012-02-23 20:57:09 +0000 | [diff] [blame] | 101 | SkPaint paint; |
| 102 | paint.setTextSize(fTextSize); |
| 103 | paint.setColor(SK_ColorBLACK); |
| 104 | |
| 105 | const char* text = "Hamburgefons"; |
| 106 | size_t len = strlen(text); |
| 107 | const SkScalar textWidth = paint.measureText(text, len); |
| 108 | |
| 109 | SkScalar* adv = new SkScalar[len]; |
| 110 | paint.getTextWidths(text, len, adv); |
| 111 | |
| 112 | for (SkScalar x = 0; x < fPictureWidth; x += textWidth) { |
| 113 | for (SkScalar y = 0; y < fPictureHeight; y += fTextSize) { |
| 114 | |
| 115 | SkPoint* pos = new SkPoint[len]; |
| 116 | SkScalar advX = 0; |
| 117 | |
| 118 | for (size_t i = 0; i < len; i++) { |
| 119 | if (fDrawPosH) |
| 120 | pos[i].set(x + advX, y); |
| 121 | else |
borenet@google.com | b796119 | 2012-08-20 18:58:26 +0000 | [diff] [blame] | 122 | pos[i].set(x + advX, y + i); |
djsollen@google.com | 809a2a9 | 2012-02-23 20:57:09 +0000 | [diff] [blame] | 123 | advX += adv[i]; |
| 124 | } |
| 125 | |
| 126 | canvas->drawPosText(text, len, pos, paint); |
| 127 | delete[] pos; |
| 128 | } |
| 129 | } |
| 130 | delete[] adv; |
| 131 | } |
| 132 | private: |
| 133 | bool fDrawPosH; |
| 134 | typedef PicturePlaybackBench INHERITED; |
| 135 | }; |
| 136 | |
| 137 | |
| 138 | /////////////////////////////////////////////////////////////////////////////// |
| 139 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 140 | DEF_BENCH( return new TextPlaybackBench(); ) |
| 141 | DEF_BENCH( return new PosTextPlaybackBench(true); ) |
| 142 | DEF_BENCH( return new PosTextPlaybackBench(false); ) |
mtklein | bf5dd41 | 2014-11-11 10:39:27 -0800 | [diff] [blame] | 143 | |
| 144 | // Chrome draws into small tiles with impl-side painting. |
| 145 | // This benchmark measures the relative performance of our bounding-box hierarchies, |
| 146 | // both when querying tiles perfectly and when not. |
mtklein | 703dd2e | 2015-01-09 06:41:48 -0800 | [diff] [blame] | 147 | enum BBH { kNone, kRTree }; |
mtklein | bf5dd41 | 2014-11-11 10:39:27 -0800 | [diff] [blame] | 148 | enum Mode { kTiled, kRandom }; |
| 149 | class TiledPlaybackBench : public Benchmark { |
| 150 | public: |
| 151 | TiledPlaybackBench(BBH bbh, Mode mode) : fBBH(bbh), fMode(mode), fName("tiled_playback") { |
| 152 | switch (fBBH) { |
| 153 | case kNone: fName.append("_none" ); break; |
| 154 | case kRTree: fName.append("_rtree" ); break; |
mtklein | bf5dd41 | 2014-11-11 10:39:27 -0800 | [diff] [blame] | 155 | } |
| 156 | switch (fMode) { |
| 157 | case kTiled: fName.append("_tiled" ); break; |
| 158 | case kRandom: fName.append("_random"); break; |
| 159 | } |
| 160 | } |
| 161 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 162 | const char* onGetName() override { return fName.c_str(); } |
| 163 | SkIPoint onGetSize() override { return SkIPoint::Make(1024,1024); } |
mtklein | bf5dd41 | 2014-11-11 10:39:27 -0800 | [diff] [blame] | 164 | |
joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame] | 165 | void onDelayedSetup() override { |
mtklein | bf5dd41 | 2014-11-11 10:39:27 -0800 | [diff] [blame] | 166 | SkAutoTDelete<SkBBHFactory> factory; |
| 167 | switch (fBBH) { |
| 168 | case kNone: break; |
| 169 | case kRTree: factory.reset(new SkRTreeFactory); break; |
mtklein | bf5dd41 | 2014-11-11 10:39:27 -0800 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | SkPictureRecorder recorder; |
| 173 | SkCanvas* canvas = recorder.beginRecording(1024, 1024, factory); |
| 174 | SkRandom rand; |
| 175 | for (int i = 0; i < 10000; i++) { |
| 176 | SkScalar x = rand.nextRangeScalar(0, 1024), |
| 177 | y = rand.nextRangeScalar(0, 1024), |
| 178 | w = rand.nextRangeScalar(0, 128), |
| 179 | h = rand.nextRangeScalar(0, 128); |
| 180 | SkPaint paint; |
| 181 | paint.setColor(rand.nextU()); |
| 182 | paint.setAlpha(0xFF); |
| 183 | canvas->drawRect(SkRect::MakeXYWH(x,y,w,h), paint); |
| 184 | } |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 185 | fPic = recorder.finishRecordingAsPicture(); |
mtklein | bf5dd41 | 2014-11-11 10:39:27 -0800 | [diff] [blame] | 186 | } |
| 187 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 188 | void onDraw(int loops, SkCanvas* canvas) override { |
mtklein | bf5dd41 | 2014-11-11 10:39:27 -0800 | [diff] [blame] | 189 | for (int i = 0; i < loops; i++) { |
| 190 | // This inner loop guarantees we make the same choices for all bench variants. |
| 191 | SkRandom rand; |
| 192 | for (int j = 0; j < 10; j++) { |
| 193 | SkScalar x = 0, y = 0; |
| 194 | switch (fMode) { |
| 195 | case kTiled: x = SkScalar(256 * rand.nextULessThan(4)); |
| 196 | y = SkScalar(256 * rand.nextULessThan(4)); |
| 197 | break; |
| 198 | case kRandom: x = rand.nextRangeScalar(0, 768); |
| 199 | y = rand.nextRangeScalar(0, 768); |
| 200 | break; |
| 201 | } |
| 202 | SkAutoCanvasRestore ar(canvas, true/*save now*/); |
| 203 | canvas->clipRect(SkRect::MakeXYWH(x,y,256,256)); |
| 204 | fPic->playback(canvas); |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | private: |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 210 | BBH fBBH; |
| 211 | Mode fMode; |
| 212 | SkString fName; |
| 213 | sk_sp<SkPicture> fPic; |
mtklein | bf5dd41 | 2014-11-11 10:39:27 -0800 | [diff] [blame] | 214 | }; |
| 215 | |
| 216 | DEF_BENCH( return new TiledPlaybackBench(kNone, kRandom); ) |
| 217 | DEF_BENCH( return new TiledPlaybackBench(kNone, kTiled ); ) |
| 218 | DEF_BENCH( return new TiledPlaybackBench(kRTree, kRandom); ) |
| 219 | DEF_BENCH( return new TiledPlaybackBench(kRTree, kTiled ); ) |