blob: 815ef0b777f2cde92a5956ac6e8d0c492a9648da [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
40 SkPicture picture;
41
42 SkCanvas* pCanvas = picture.beginRecording(PICTURE_WIDTH, PICTURE_HEIGHT);
43 recordCanvas(pCanvas);
44 picture.endRecording();
45
commit-bot@chromium.org33614712013-12-03 18:17:16 +000046 const SkPoint translateDelta = getTranslateDelta(loops);
djsollen@google.com809a2a92012-02-23 20:57:09 +000047
commit-bot@chromium.org33614712013-12-03 18:17:16 +000048 for (int i = 0; i < loops; i++) {
djsollen@google.com809a2a92012-02-23 20:57:09 +000049 picture.draw(canvas);
50 canvas->translate(translateDelta.fX, translateDelta.fY);
51 }
52 }
53
54 virtual void recordCanvas(SkCanvas* canvas) = 0;
mtklein@google.comc2897432013-09-10 19:23:38 +000055 virtual SkPoint getTranslateDelta(int N) {
djsollen@google.com809a2a92012-02-23 20:57:09 +000056 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;
65private:
66 typedef SkBenchmark INHERITED;
67};
68
69
70class TextPlaybackBench : public PicturePlaybackBench {
71public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000072 TextPlaybackBench() : INHERITED("drawText") { }
djsollen@google.com809a2a92012-02-23 20:57:09 +000073protected:
74 virtual void recordCanvas(SkCanvas* canvas) {
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 }
89private:
90 typedef PicturePlaybackBench INHERITED;
91};
92
93class PosTextPlaybackBench : public PicturePlaybackBench {
94public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000095 PosTextPlaybackBench(bool drawPosH)
96 : INHERITED(drawPosH ? "drawPosTextH" : "drawPosText")
djsollen@google.com710c2692012-02-27 16:22:48 +000097 , fDrawPosH(drawPosH) { }
djsollen@google.com809a2a92012-02-23 20:57:09 +000098protected:
99 virtual void recordCanvas(SkCanvas* canvas) {
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.comb7961192012-08-20 18:58:26 +0000121 pos[i].set(x + advX, y + i);
djsollen@google.com809a2a92012-02-23 20:57:09 +0000122 advX += adv[i];
123 }
124
125 canvas->drawPosText(text, len, pos, paint);
126 delete[] pos;
127 }
128 }
129 delete[] adv;
130 }
131private:
132 bool fDrawPosH;
133 typedef PicturePlaybackBench INHERITED;
134};
135
136
137///////////////////////////////////////////////////////////////////////////////
138
mtklein@google.com410e6e82013-09-13 19:52:27 +0000139DEF_BENCH( return new TextPlaybackBench(); )
140DEF_BENCH( return new PosTextPlaybackBench(true); )
141DEF_BENCH( return new PosTextPlaybackBench(false); )