blob: a1ae06b6579f2bbd060b6f30c0f174b658fc3d26 [file] [log] [blame]
sugoi@google.com580a1722013-04-23 14:20:45 +00001/*
2 * Copyright 2013 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 "SkBitmapSource.h"
9#include "SkBlendImageFilter.h"
10#include "SkCanvas.h"
11#include "SkDevice.h"
12
13#define FILTER_WIDTH 256
14#define FILTER_HEIGHT 256
15
16class BlendBaseBench : public SkBenchmark {
17public:
18 BlendBaseBench(void* param) : INHERITED(param), fInitialized(false) {
19 }
20
21protected:
22 virtual void onPreDraw() SK_OVERRIDE {
23 if (!fInitialized) {
24 make_bitmap();
25 make_checkerboard();
26 fInitialized = true;
27 }
28 }
29
30 void drawClippedBitmap(SkCanvas* canvas, const SkPaint& paint, int x) {
31 canvas->save();
32 canvas->clipRect(SkRect::MakeXYWH(SkIntToScalar(x), 0,
33 SkIntToScalar(fBitmap.width()), SkIntToScalar(fBitmap.height())));
34 canvas->drawBitmap(fBitmap, SkIntToScalar(x), 0, &paint);
35 canvas->restore();
36 }
37
38 void make_bitmap() {
39 fBitmap.setConfig(SkBitmap::kARGB_8888_Config, FILTER_WIDTH, FILTER_HEIGHT);
40 fBitmap.allocPixels();
41 SkDevice device(fBitmap);
42 SkCanvas canvas(&device);
43 canvas.clear(0x00000000);
44 SkPaint paint;
45 paint.setAntiAlias(true);
46 paint.setColor(0xD000D000);
47 paint.setTextSize(SkIntToScalar(96));
48 const char* str = "e";
49 canvas.drawText(str, strlen(str), SkIntToScalar(15), SkIntToScalar(65), paint);
50 }
51
52 void make_checkerboard() {
53 fCheckerboard.setConfig(SkBitmap::kARGB_8888_Config, FILTER_WIDTH, FILTER_HEIGHT);
54 fCheckerboard.allocPixels();
55 SkDevice device(fCheckerboard);
56 SkCanvas canvas(&device);
57 canvas.clear(0x00000000);
58 SkPaint darkPaint;
59 darkPaint.setColor(0xFF404040);
60 SkPaint lightPaint;
61 lightPaint.setColor(0xFFA0A0A0);
62 for (int y = 0; y < FILTER_HEIGHT; y += 16) {
63 for (int x = 0; x < FILTER_WIDTH; x += 16) {
64 canvas.save();
65 canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
66 canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint);
67 canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint);
68 canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint);
69 canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint);
70 canvas.restore();
71 }
72 }
73 }
74
75 typedef SkBenchmark INHERITED;
76 SkBitmap fBitmap, fCheckerboard;
77 bool fInitialized;
78};
79
80class BlendNormalBench : public BlendBaseBench {
81public:
82 BlendNormalBench(void* param) : INHERITED(param) {
83 }
84
85protected:
86 virtual const char* onGetName() SK_OVERRIDE {
87 return "blend_normal";
88 }
89
90 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
91 SkPaint paint;
92 SkAutoTUnref<SkImageFilter> background(SkNEW_ARGS(SkBitmapSource, (fCheckerboard)));
93 paint.setImageFilter(SkNEW_ARGS(SkBlendImageFilter,
94 (SkBlendImageFilter::kNormal_Mode, background)))->unref();
95 drawClippedBitmap(canvas, paint, 0);
96 }
97
98private:
99 typedef BlendBaseBench INHERITED;
100};
101
102class BlendMultiplyBench : public BlendBaseBench {
103public:
104 BlendMultiplyBench(void* param) : INHERITED(param) {
105 }
106
107protected:
108 virtual const char* onGetName() SK_OVERRIDE {
109 return "blend_multiply";
110 }
111
112 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
113 SkPaint paint;
114 SkAutoTUnref<SkImageFilter> background(SkNEW_ARGS(SkBitmapSource, (fCheckerboard)));
115 paint.setImageFilter(SkNEW_ARGS(SkBlendImageFilter,
116 (SkBlendImageFilter::kMultiply_Mode, background)))->unref();
117 drawClippedBitmap(canvas, paint, 100);
118 }
119
120private:
121 typedef BlendBaseBench INHERITED;
122};
123
124class BlendScreenBench : public BlendBaseBench {
125public:
126 BlendScreenBench(void* param) : INHERITED(param) {
127 }
128
129protected:
130 virtual const char* onGetName() SK_OVERRIDE {
131 return "blend_screen";
132 }
133
134 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
135 SkPaint paint;
136 SkAutoTUnref<SkImageFilter> background(SkNEW_ARGS(SkBitmapSource, (fCheckerboard)));
137 paint.setImageFilter(SkNEW_ARGS(SkBlendImageFilter,
138 (SkBlendImageFilter::kScreen_Mode, background)))->unref();
139 drawClippedBitmap(canvas, paint, 200);
140 }
141
142private:
143 typedef BlendBaseBench INHERITED;
144};
145
146class BlendDarkenBench : public BlendBaseBench {
147public:
148 BlendDarkenBench(void* param) : INHERITED(param) {
149 }
150
151protected:
152 virtual const char* onGetName() SK_OVERRIDE {
153 return "blend_darken";
154 }
155
156 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
157 SkPaint paint;
158 SkAutoTUnref<SkImageFilter> background(SkNEW_ARGS(SkBitmapSource, (fCheckerboard)));
159 paint.setImageFilter(SkNEW_ARGS(SkBlendImageFilter,
160 (SkBlendImageFilter::kDarken_Mode, background)))->unref();
161 drawClippedBitmap(canvas, paint, 300);
162 }
163
164private:
165 typedef BlendBaseBench INHERITED;
166};
167
168class BlendLightenBench : public BlendBaseBench {
169public:
170 BlendLightenBench(void* param) : INHERITED(param) {
171 }
172
173protected:
174 virtual const char* onGetName() SK_OVERRIDE {
175 return "blend_lighten";
176 }
177
178 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
179 SkPaint paint;
180 SkAutoTUnref<SkImageFilter> background(SkNEW_ARGS(SkBitmapSource, (fCheckerboard)));
181 paint.setImageFilter(SkNEW_ARGS(SkBlendImageFilter,
182 (SkBlendImageFilter::kLighten_Mode, background)))->unref();
183 drawClippedBitmap(canvas, paint, 400);
184 }
185
186private:
187 typedef BlendBaseBench INHERITED;
188};
189
190///////////////////////////////////////////////////////////////////////////////
191
192DEF_BENCH( return new BlendNormalBench(p); )
193DEF_BENCH( return new BlendMultiplyBench(p); )
194DEF_BENCH( return new BlendScreenBench(p); )
195DEF_BENCH( return new BlendDarkenBench(p); )
196DEF_BENCH( return new BlendLightenBench(p); )