blob: fa693c2468a1abb6d6ac00afbd66e54f4f21020d [file] [log] [blame]
djsollen@google.com809a2a92012-02-23 20:57:09 +00001/*
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 "SkColor.h"
10#include "SkPaint.h"
11#include "SkPicture.h"
12#include "SkPoint.h"
13#include "SkRect.h"
14#include "SkString.h"
15
16// This is designed to emulate about 4 screens of textual content
17
18
19class PicturePlaybackBench : public SkBenchmark {
20public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000021 PicturePlaybackBench(const char name[]) {
djsollen@google.com809a2a92012-02-23 20:57:09 +000022 fName.printf("picture_playback_%s", name);
23 fPictureWidth = SkIntToScalar(PICTURE_WIDTH);
24 fPictureHeight = SkIntToScalar(PICTURE_HEIGHT);
25 fTextSize = SkIntToScalar(TEXT_SIZE);
26 }
27
28 enum {
djsollen@google.com809a2a92012-02-23 20:57:09 +000029 PICTURE_WIDTH = 1000,
30 PICTURE_HEIGHT = 4000,
31 TEXT_SIZE = 10
32 };
33protected:
34 virtual const char* onGetName() {
35 return fName.c_str();
36 }
37
commit-bot@chromium.org33614712013-12-03 18:17:16 +000038 virtual void onDraw(const int loops, SkCanvas* canvas) {
djsollen@google.com809a2a92012-02-23 20:57:09 +000039
robertphillips@google.com84b18c72014-04-13 19:09:42 +000040 SkPictureRecorder recorder;
41 SkCanvas* pCanvas = recorder.beginRecording(PICTURE_WIDTH, PICTURE_HEIGHT);
42 this->recordCanvas(pCanvas);
43 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
djsollen@google.com809a2a92012-02-23 20:57:09 +000044
commit-bot@chromium.org33614712013-12-03 18:17:16 +000045 const SkPoint translateDelta = getTranslateDelta(loops);
djsollen@google.com809a2a92012-02-23 20:57:09 +000046
commit-bot@chromium.org33614712013-12-03 18:17:16 +000047 for (int i = 0; i < loops; i++) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +000048 picture->draw(canvas);
djsollen@google.com809a2a92012-02-23 20:57:09 +000049 canvas->translate(translateDelta.fX, translateDelta.fY);
50 }
51 }
52
53 virtual void recordCanvas(SkCanvas* canvas) = 0;
mtklein@google.comc2897432013-09-10 19:23:38 +000054 virtual SkPoint getTranslateDelta(int N) {
djsollen@google.com809a2a92012-02-23 20:57:09 +000055 SkIPoint canvasSize = onGetSize();
56 return SkPoint::Make(SkIntToScalar((PICTURE_WIDTH - canvasSize.fX)/N),
57 SkIntToScalar((PICTURE_HEIGHT- canvasSize.fY)/N));
58 }
59
60 SkString fName;
61 SkScalar fPictureWidth;
62 SkScalar fPictureHeight;
63 SkScalar fTextSize;
64private:
65 typedef SkBenchmark INHERITED;
66};
67
68
69class TextPlaybackBench : public PicturePlaybackBench {
70public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000071 TextPlaybackBench() : INHERITED("drawText") { }
djsollen@google.com809a2a92012-02-23 20:57:09 +000072protected:
robertphillips@google.com84b18c72014-04-13 19:09:42 +000073 virtual void recordCanvas(SkCanvas* canvas) SK_OVERRIDE {
djsollen@google.com809a2a92012-02-23 20:57:09 +000074 SkPaint paint;
75 paint.setTextSize(fTextSize);
76 paint.setColor(SK_ColorBLACK);
77
78 const char* text = "Hamburgefons";
79 size_t len = strlen(text);
80 const SkScalar textWidth = paint.measureText(text, len);
81
82 for (SkScalar x = 0; x < fPictureWidth; x += textWidth) {
83 for (SkScalar y = 0; y < fPictureHeight; y += fTextSize) {
84 canvas->drawText(text, len, x, y, paint);
85 }
86 }
87 }
88private:
89 typedef PicturePlaybackBench INHERITED;
90};
91
92class PosTextPlaybackBench : public PicturePlaybackBench {
93public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000094 PosTextPlaybackBench(bool drawPosH)
95 : INHERITED(drawPosH ? "drawPosTextH" : "drawPosText")
djsollen@google.com710c2692012-02-27 16:22:48 +000096 , fDrawPosH(drawPosH) { }
djsollen@google.com809a2a92012-02-23 20:57:09 +000097protected:
robertphillips@google.com84b18c72014-04-13 19:09:42 +000098 virtual void recordCanvas(SkCanvas* canvas) SK_OVERRIDE {
djsollen@google.com809a2a92012-02-23 20:57:09 +000099 SkPaint paint;
100 paint.setTextSize(fTextSize);
101 paint.setColor(SK_ColorBLACK);
102
103 const char* text = "Hamburgefons";
104 size_t len = strlen(text);
105 const SkScalar textWidth = paint.measureText(text, len);
106
107 SkScalar* adv = new SkScalar[len];
108 paint.getTextWidths(text, len, adv);
109
110 for (SkScalar x = 0; x < fPictureWidth; x += textWidth) {
111 for (SkScalar y = 0; y < fPictureHeight; y += fTextSize) {
112
113 SkPoint* pos = new SkPoint[len];
114 SkScalar advX = 0;
115
116 for (size_t i = 0; i < len; i++) {
117 if (fDrawPosH)
118 pos[i].set(x + advX, y);
119 else
borenet@google.comb7961192012-08-20 18:58:26 +0000120 pos[i].set(x + advX, y + i);
djsollen@google.com809a2a92012-02-23 20:57:09 +0000121 advX += adv[i];
122 }
123
124 canvas->drawPosText(text, len, pos, paint);
125 delete[] pos;
126 }
127 }
128 delete[] adv;
129 }
130private:
131 bool fDrawPosH;
132 typedef PicturePlaybackBench INHERITED;
133};
134
135
136///////////////////////////////////////////////////////////////////////////////
137
mtklein@google.com410e6e82013-09-13 19:52:27 +0000138DEF_BENCH( return new TextPlaybackBench(); )
139DEF_BENCH( return new PosTextPlaybackBench(true); )
140DEF_BENCH( return new PosTextPlaybackBench(false); )