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" |
| 14 | #include "SkRect.h" |
| 15 | #include "SkString.h" |
| 16 | |
| 17 | // This is designed to emulate about 4 screens of textual content |
| 18 | |
| 19 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 20 | class PicturePlaybackBench : public Benchmark { |
djsollen@google.com | 809a2a9 | 2012-02-23 20:57:09 +0000 | [diff] [blame] | 21 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 22 | PicturePlaybackBench(const char name[]) { |
djsollen@google.com | 809a2a9 | 2012-02-23 20:57:09 +0000 | [diff] [blame] | 23 | fName.printf("picture_playback_%s", name); |
| 24 | fPictureWidth = SkIntToScalar(PICTURE_WIDTH); |
| 25 | fPictureHeight = SkIntToScalar(PICTURE_HEIGHT); |
| 26 | fTextSize = SkIntToScalar(TEXT_SIZE); |
| 27 | } |
| 28 | |
| 29 | enum { |
djsollen@google.com | 809a2a9 | 2012-02-23 20:57:09 +0000 | [diff] [blame] | 30 | PICTURE_WIDTH = 1000, |
| 31 | PICTURE_HEIGHT = 4000, |
| 32 | TEXT_SIZE = 10 |
| 33 | }; |
| 34 | protected: |
| 35 | virtual const char* onGetName() { |
| 36 | return fName.c_str(); |
| 37 | } |
| 38 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 39 | virtual void onDraw(const int loops, SkCanvas* canvas) { |
djsollen@google.com | 809a2a9 | 2012-02-23 20:57:09 +0000 | [diff] [blame] | 40 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 41 | SkPictureRecorder recorder; |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 42 | SkCanvas* pCanvas = recorder.beginRecording(PICTURE_WIDTH, PICTURE_HEIGHT, NULL, 0); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 43 | this->recordCanvas(pCanvas); |
| 44 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
djsollen@google.com | 809a2a9 | 2012-02-23 20:57:09 +0000 | [diff] [blame] | 45 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 46 | const SkPoint translateDelta = getTranslateDelta(loops); |
djsollen@google.com | 809a2a9 | 2012-02-23 20:57:09 +0000 | [diff] [blame] | 47 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 48 | for (int i = 0; i < loops; i++) { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 49 | picture->draw(canvas); |
djsollen@google.com | 809a2a9 | 2012-02-23 20:57:09 +0000 | [diff] [blame] | 50 | canvas->translate(translateDelta.fX, translateDelta.fY); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | virtual void recordCanvas(SkCanvas* canvas) = 0; |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 55 | virtual SkPoint getTranslateDelta(int N) { |
djsollen@google.com | 809a2a9 | 2012-02-23 20:57:09 +0000 | [diff] [blame] | 56 | SkIPoint canvasSize = onGetSize(); |
| 57 | return SkPoint::Make(SkIntToScalar((PICTURE_WIDTH - canvasSize.fX)/N), |
| 58 | SkIntToScalar((PICTURE_HEIGHT- canvasSize.fY)/N)); |
| 59 | } |
| 60 | |
| 61 | SkString fName; |
| 62 | SkScalar fPictureWidth; |
| 63 | SkScalar fPictureHeight; |
| 64 | SkScalar fTextSize; |
| 65 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 66 | typedef Benchmark INHERITED; |
djsollen@google.com | 809a2a9 | 2012-02-23 20:57:09 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | |
| 70 | class TextPlaybackBench : public PicturePlaybackBench { |
| 71 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 72 | TextPlaybackBench() : INHERITED("drawText") { } |
djsollen@google.com | 809a2a9 | 2012-02-23 20:57:09 +0000 | [diff] [blame] | 73 | protected: |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 74 | virtual void recordCanvas(SkCanvas* canvas) SK_OVERRIDE { |
djsollen@google.com | 809a2a9 | 2012-02-23 20:57:09 +0000 | [diff] [blame] | 75 | SkPaint paint; |
| 76 | paint.setTextSize(fTextSize); |
| 77 | paint.setColor(SK_ColorBLACK); |
| 78 | |
| 79 | const char* text = "Hamburgefons"; |
| 80 | size_t len = strlen(text); |
| 81 | const SkScalar textWidth = paint.measureText(text, len); |
| 82 | |
| 83 | for (SkScalar x = 0; x < fPictureWidth; x += textWidth) { |
| 84 | for (SkScalar y = 0; y < fPictureHeight; y += fTextSize) { |
| 85 | canvas->drawText(text, len, x, y, paint); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | private: |
| 90 | typedef PicturePlaybackBench INHERITED; |
| 91 | }; |
| 92 | |
| 93 | class PosTextPlaybackBench : public PicturePlaybackBench { |
| 94 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 95 | PosTextPlaybackBench(bool drawPosH) |
| 96 | : INHERITED(drawPosH ? "drawPosTextH" : "drawPosText") |
djsollen@google.com | 710c269 | 2012-02-27 16:22:48 +0000 | [diff] [blame] | 97 | , fDrawPosH(drawPosH) { } |
djsollen@google.com | 809a2a9 | 2012-02-23 20:57:09 +0000 | [diff] [blame] | 98 | protected: |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 99 | virtual void recordCanvas(SkCanvas* canvas) SK_OVERRIDE { |
djsollen@google.com | 809a2a9 | 2012-02-23 20:57:09 +0000 | [diff] [blame] | 100 | SkPaint paint; |
| 101 | paint.setTextSize(fTextSize); |
| 102 | paint.setColor(SK_ColorBLACK); |
| 103 | |
| 104 | const char* text = "Hamburgefons"; |
| 105 | size_t len = strlen(text); |
| 106 | const SkScalar textWidth = paint.measureText(text, len); |
| 107 | |
| 108 | SkScalar* adv = new SkScalar[len]; |
| 109 | paint.getTextWidths(text, len, adv); |
| 110 | |
| 111 | for (SkScalar x = 0; x < fPictureWidth; x += textWidth) { |
| 112 | for (SkScalar y = 0; y < fPictureHeight; y += fTextSize) { |
| 113 | |
| 114 | SkPoint* pos = new SkPoint[len]; |
| 115 | SkScalar advX = 0; |
| 116 | |
| 117 | for (size_t i = 0; i < len; i++) { |
| 118 | if (fDrawPosH) |
| 119 | pos[i].set(x + advX, y); |
| 120 | else |
borenet@google.com | b796119 | 2012-08-20 18:58:26 +0000 | [diff] [blame] | 121 | pos[i].set(x + advX, y + i); |
djsollen@google.com | 809a2a9 | 2012-02-23 20:57:09 +0000 | [diff] [blame] | 122 | advX += adv[i]; |
| 123 | } |
| 124 | |
| 125 | canvas->drawPosText(text, len, pos, paint); |
| 126 | delete[] pos; |
| 127 | } |
| 128 | } |
| 129 | delete[] adv; |
| 130 | } |
| 131 | private: |
| 132 | bool fDrawPosH; |
| 133 | typedef PicturePlaybackBench INHERITED; |
| 134 | }; |
| 135 | |
| 136 | |
| 137 | /////////////////////////////////////////////////////////////////////////////// |
| 138 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 139 | DEF_BENCH( return new TextPlaybackBench(); ) |
| 140 | DEF_BENCH( return new PosTextPlaybackBench(true); ) |
| 141 | DEF_BENCH( return new PosTextPlaybackBench(false); ) |