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