blob: 2f9a69e0f82650e9de2dfa90705511cd94629407 [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 */
reed8c0c7b02014-06-27 05:49:53 -07007
tfarinaf168b862014-06-19 12:32:29 -07008#include "Benchmark.h"
sugoi@google.com580a1722013-04-23 14:20:45 +00009#include "SkBitmapSource.h"
10#include "SkCanvas.h"
sugoi@google.com580a1722013-04-23 14:20:45 +000011#include "SkDisplacementMapEffect.h"
12
sugoi@google.comca425d92013-04-23 14:37:38 +000013#define FILTER_WIDTH_SMALL 32
14#define FILTER_HEIGHT_SMALL 32
15#define FILTER_WIDTH_LARGE 256
16#define FILTER_HEIGHT_LARGE 256
sugoi@google.com580a1722013-04-23 14:20:45 +000017
tfarinaf168b862014-06-19 12:32:29 -070018class DisplacementBaseBench : public Benchmark {
sugoi@google.com580a1722013-04-23 14:20:45 +000019public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000020 DisplacementBaseBench(bool small) :
21 fInitialized(false), fIsSmall(small) {
sugoi@google.com580a1722013-04-23 14:20:45 +000022 }
23
24protected:
mtklein72c9faa2015-01-09 10:06:39 -080025 void onPreDraw() SK_OVERRIDE {
sugoi@google.com580a1722013-04-23 14:20:45 +000026 if (!fInitialized) {
robertphillips@google.com26e30c52013-10-28 18:07:44 +000027 this->makeBitmap();
28 this->makeCheckerboard();
sugoi@google.com580a1722013-04-23 14:20:45 +000029 fInitialized = true;
30 }
31 }
32
robertphillips@google.com26e30c52013-10-28 18:07:44 +000033 void makeBitmap() {
34 const int w = this->isSmall() ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE;
35 const int h = this->isSmall() ? FILTER_HEIGHT_LARGE : FILTER_HEIGHT_LARGE;
reed6c225732014-06-09 19:52:07 -070036 fBitmap.allocN32Pixels(w, h);
37 SkCanvas canvas(fBitmap);
sugoi@google.com580a1722013-04-23 14:20:45 +000038 canvas.clear(0x00000000);
39 SkPaint paint;
40 paint.setAntiAlias(true);
41 paint.setColor(0xFF884422);
42 paint.setTextSize(SkIntToScalar(96));
43 const char* str = "g";
44 canvas.drawText(str, strlen(str), SkIntToScalar(15), SkIntToScalar(55), paint);
45 }
46
robertphillips@google.com26e30c52013-10-28 18:07:44 +000047 void makeCheckerboard() {
48 const int w = this->isSmall() ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE;
49 const int h = this->isSmall() ? FILTER_HEIGHT_LARGE : FILTER_HEIGHT_LARGE;
reed6c225732014-06-09 19:52:07 -070050 fCheckerboard.allocN32Pixels(w, h);
51 SkCanvas canvas(fCheckerboard);
sugoi@google.com580a1722013-04-23 14:20:45 +000052 canvas.clear(0x00000000);
53 SkPaint darkPaint;
54 darkPaint.setColor(0xFF804020);
55 SkPaint lightPaint;
56 lightPaint.setColor(0xFF244484);
57 for (int y = 0; y < h; y += 16) {
58 for (int x = 0; x < w; x += 16) {
59 canvas.save();
60 canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
61 canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint);
62 canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint);
63 canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint);
64 canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint);
65 canvas.restore();
66 }
67 }
68 }
69
70 void drawClippedBitmap(SkCanvas* canvas, int x, int y, const SkPaint& paint) {
71 canvas->save();
72 canvas->clipRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y),
skia.committer@gmail.com70402c32013-10-29 07:01:50 +000073 SkIntToScalar(fBitmap.width()),
robertphillips@google.com26e30c52013-10-28 18:07:44 +000074 SkIntToScalar(fBitmap.height())));
sugoi@google.com580a1722013-04-23 14:20:45 +000075 canvas->drawBitmap(fBitmap, SkIntToScalar(x), SkIntToScalar(y), &paint);
76 canvas->restore();
77 }
78
79 inline bool isSmall() const { return fIsSmall; }
80
81 SkBitmap fBitmap, fCheckerboard;
82private:
83 bool fInitialized;
84 bool fIsSmall;
tfarinaf168b862014-06-19 12:32:29 -070085 typedef Benchmark INHERITED;
sugoi@google.com580a1722013-04-23 14:20:45 +000086};
87
88class DisplacementZeroBench : public DisplacementBaseBench {
89public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000090 DisplacementZeroBench(bool small) : INHERITED(small) {
sugoi@google.com580a1722013-04-23 14:20:45 +000091 }
92
93protected:
mtklein72c9faa2015-01-09 10:06:39 -080094 const char* onGetName() SK_OVERRIDE {
robertphillips@google.com26e30c52013-10-28 18:07:44 +000095 return this->isSmall() ? "displacement_zero_small" : "displacement_zero_large";
sugoi@google.com580a1722013-04-23 14:20:45 +000096 }
97
mtklein72c9faa2015-01-09 10:06:39 -080098 void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
sugoi@google.com580a1722013-04-23 14:20:45 +000099 SkPaint paint;
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000100 SkAutoTUnref<SkImageFilter> displ(SkBitmapSource::Create(fCheckerboard));
sugoi@google.com580a1722013-04-23 14:20:45 +0000101 // No displacement effect
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000102 paint.setImageFilter(SkDisplacementMapEffect::Create(
103 SkDisplacementMapEffect::kR_ChannelSelectorType,
104 SkDisplacementMapEffect::kG_ChannelSelectorType, 0.0f, displ))->unref();
mtklein@google.comc2897432013-09-10 19:23:38 +0000105
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000106 for (int i = 0; i < loops; i++) {
robertphillips@google.com26e30c52013-10-28 18:07:44 +0000107 this->drawClippedBitmap(canvas, 0, 0, paint);
mtklein@google.comc2897432013-09-10 19:23:38 +0000108 }
sugoi@google.com580a1722013-04-23 14:20:45 +0000109 }
110
111private:
112 typedef DisplacementBaseBench INHERITED;
113};
114
115class DisplacementAlphaBench : public DisplacementBaseBench {
116public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000117 DisplacementAlphaBench(bool small) : INHERITED(small) {
sugoi@google.com580a1722013-04-23 14:20:45 +0000118 }
119
120protected:
mtklein72c9faa2015-01-09 10:06:39 -0800121 const char* onGetName() SK_OVERRIDE {
sugoi@google.com580a1722013-04-23 14:20:45 +0000122 return isSmall() ? "displacement_alpha_small" : "displacement_alpha_large";
123 }
124
mtklein72c9faa2015-01-09 10:06:39 -0800125 void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
sugoi@google.com580a1722013-04-23 14:20:45 +0000126 SkPaint paint;
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000127 SkAutoTUnref<SkImageFilter> displ(SkBitmapSource::Create(fCheckerboard));
sugoi@google.com580a1722013-04-23 14:20:45 +0000128 // Displacement, with 1 alpha component (which isn't pre-multiplied)
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000129 paint.setImageFilter(SkDisplacementMapEffect::Create(
130 SkDisplacementMapEffect::kB_ChannelSelectorType,
131 SkDisplacementMapEffect::kA_ChannelSelectorType, 16.0f, displ))->unref();
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000132 for (int i = 0; i < loops; i++) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000133 drawClippedBitmap(canvas, 100, 0, paint);
134 }
sugoi@google.com580a1722013-04-23 14:20:45 +0000135 }
136
137private:
138 typedef DisplacementBaseBench INHERITED;
139};
140
141class DisplacementFullBench : public DisplacementBaseBench {
142public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000143 DisplacementFullBench(bool small) : INHERITED(small) {
sugoi@google.com580a1722013-04-23 14:20:45 +0000144 }
145
146protected:
mtklein72c9faa2015-01-09 10:06:39 -0800147 const char* onGetName() SK_OVERRIDE {
sugoi@google.com580a1722013-04-23 14:20:45 +0000148 return isSmall() ? "displacement_full_small" : "displacement_full_large";
149 }
150
mtklein72c9faa2015-01-09 10:06:39 -0800151 void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
sugoi@google.com580a1722013-04-23 14:20:45 +0000152 SkPaint paint;
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000153 SkAutoTUnref<SkImageFilter> displ(SkBitmapSource::Create(fCheckerboard));
sugoi@google.com580a1722013-04-23 14:20:45 +0000154 // Displacement, with 2 non-alpha components
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000155 paint.setImageFilter(SkDisplacementMapEffect::Create(
156 SkDisplacementMapEffect::kR_ChannelSelectorType,
157 SkDisplacementMapEffect::kB_ChannelSelectorType, 32.0f, displ))->unref();
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000158 for (int i = 0; i < loops; ++i) {
robertphillips@google.com26e30c52013-10-28 18:07:44 +0000159 this->drawClippedBitmap(canvas, 200, 0, paint);
mtklein@google.comc2897432013-09-10 19:23:38 +0000160 }
sugoi@google.com580a1722013-04-23 14:20:45 +0000161 }
162
163private:
164 typedef DisplacementBaseBench INHERITED;
165};
166
167///////////////////////////////////////////////////////////////////////////////
168
mtklein@google.com410e6e82013-09-13 19:52:27 +0000169DEF_BENCH( return new DisplacementZeroBench(true); )
170DEF_BENCH( return new DisplacementAlphaBench(true); )
171DEF_BENCH( return new DisplacementFullBench(true); )
172DEF_BENCH( return new DisplacementZeroBench(false); )
173DEF_BENCH( return new DisplacementAlphaBench(false); )
174DEF_BENCH( return new DisplacementFullBench(false); )