blob: 08da9b19af2ac3509280aff3131edfa6ade78b7e [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"
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00008#include "SkBitmapDevice.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
18class DisplacementBaseBench : public SkBenchmark {
19public:
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:
25 virtual void onPreDraw() SK_OVERRIDE {
26 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;
sugoi@google.com580a1722013-04-23 14:20:45 +000036 fBitmap.setConfig(SkBitmap::kARGB_8888_Config, w, h);
37 fBitmap.allocPixels();
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000038 SkBitmapDevice device(fBitmap);
sugoi@google.com580a1722013-04-23 14:20:45 +000039 SkCanvas canvas(&device);
40 canvas.clear(0x00000000);
41 SkPaint paint;
42 paint.setAntiAlias(true);
43 paint.setColor(0xFF884422);
44 paint.setTextSize(SkIntToScalar(96));
45 const char* str = "g";
46 canvas.drawText(str, strlen(str), SkIntToScalar(15), SkIntToScalar(55), paint);
47 }
48
robertphillips@google.com26e30c52013-10-28 18:07:44 +000049 void makeCheckerboard() {
50 const int w = this->isSmall() ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE;
51 const int h = this->isSmall() ? FILTER_HEIGHT_LARGE : FILTER_HEIGHT_LARGE;
sugoi@google.com580a1722013-04-23 14:20:45 +000052 fCheckerboard.setConfig(SkBitmap::kARGB_8888_Config, w, h);
53 fCheckerboard.allocPixels();
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000054 SkBitmapDevice device(fCheckerboard);
sugoi@google.com580a1722013-04-23 14:20:45 +000055 SkCanvas canvas(&device);
56 canvas.clear(0x00000000);
57 SkPaint darkPaint;
58 darkPaint.setColor(0xFF804020);
59 SkPaint lightPaint;
60 lightPaint.setColor(0xFF244484);
61 for (int y = 0; y < h; y += 16) {
62 for (int x = 0; x < w; x += 16) {
63 canvas.save();
64 canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
65 canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint);
66 canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint);
67 canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint);
68 canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint);
69 canvas.restore();
70 }
71 }
72 }
73
74 void drawClippedBitmap(SkCanvas* canvas, int x, int y, const SkPaint& paint) {
75 canvas->save();
76 canvas->clipRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y),
skia.committer@gmail.com70402c32013-10-29 07:01:50 +000077 SkIntToScalar(fBitmap.width()),
robertphillips@google.com26e30c52013-10-28 18:07:44 +000078 SkIntToScalar(fBitmap.height())));
sugoi@google.com580a1722013-04-23 14:20:45 +000079 canvas->drawBitmap(fBitmap, SkIntToScalar(x), SkIntToScalar(y), &paint);
80 canvas->restore();
81 }
82
83 inline bool isSmall() const { return fIsSmall; }
84
85 SkBitmap fBitmap, fCheckerboard;
86private:
87 bool fInitialized;
88 bool fIsSmall;
89 typedef SkBenchmark INHERITED;
90};
91
92class DisplacementZeroBench : public DisplacementBaseBench {
93public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000094 DisplacementZeroBench(bool small) : INHERITED(small) {
sugoi@google.com580a1722013-04-23 14:20:45 +000095 }
96
97protected:
98 virtual const char* onGetName() SK_OVERRIDE {
robertphillips@google.com26e30c52013-10-28 18:07:44 +000099 return this->isSmall() ? "displacement_zero_small" : "displacement_zero_large";
sugoi@google.com580a1722013-04-23 14:20:45 +0000100 }
101
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000102 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
sugoi@google.com580a1722013-04-23 14:20:45 +0000103 SkPaint paint;
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000104 SkAutoTUnref<SkImageFilter> displ(SkBitmapSource::Create(fCheckerboard));
sugoi@google.com580a1722013-04-23 14:20:45 +0000105 // No displacement effect
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000106 paint.setImageFilter(SkDisplacementMapEffect::Create(
107 SkDisplacementMapEffect::kR_ChannelSelectorType,
108 SkDisplacementMapEffect::kG_ChannelSelectorType, 0.0f, displ))->unref();
mtklein@google.comc2897432013-09-10 19:23:38 +0000109
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000110 for (int i = 0; i < loops; i++) {
robertphillips@google.com26e30c52013-10-28 18:07:44 +0000111 this->drawClippedBitmap(canvas, 0, 0, paint);
mtklein@google.comc2897432013-09-10 19:23:38 +0000112 }
sugoi@google.com580a1722013-04-23 14:20:45 +0000113 }
114
115private:
116 typedef DisplacementBaseBench INHERITED;
117};
118
119class DisplacementAlphaBench : public DisplacementBaseBench {
120public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000121 DisplacementAlphaBench(bool small) : INHERITED(small) {
sugoi@google.com580a1722013-04-23 14:20:45 +0000122 }
123
124protected:
125 virtual const char* onGetName() SK_OVERRIDE {
126 return isSmall() ? "displacement_alpha_small" : "displacement_alpha_large";
127 }
128
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000129 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
sugoi@google.com580a1722013-04-23 14:20:45 +0000130 SkPaint paint;
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000131 SkAutoTUnref<SkImageFilter> displ(SkBitmapSource::Create(fCheckerboard));
sugoi@google.com580a1722013-04-23 14:20:45 +0000132 // Displacement, with 1 alpha component (which isn't pre-multiplied)
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000133 paint.setImageFilter(SkDisplacementMapEffect::Create(
134 SkDisplacementMapEffect::kB_ChannelSelectorType,
135 SkDisplacementMapEffect::kA_ChannelSelectorType, 16.0f, displ))->unref();
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000136 for (int i = 0; i < loops; i++) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000137 drawClippedBitmap(canvas, 100, 0, paint);
138 }
sugoi@google.com580a1722013-04-23 14:20:45 +0000139 }
140
141private:
142 typedef DisplacementBaseBench INHERITED;
143};
144
145class DisplacementFullBench : public DisplacementBaseBench {
146public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000147 DisplacementFullBench(bool small) : INHERITED(small) {
sugoi@google.com580a1722013-04-23 14:20:45 +0000148 }
149
150protected:
151 virtual const char* onGetName() SK_OVERRIDE {
152 return isSmall() ? "displacement_full_small" : "displacement_full_large";
153 }
154
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000155 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
sugoi@google.com580a1722013-04-23 14:20:45 +0000156 SkPaint paint;
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000157 SkAutoTUnref<SkImageFilter> displ(SkBitmapSource::Create(fCheckerboard));
sugoi@google.com580a1722013-04-23 14:20:45 +0000158 // Displacement, with 2 non-alpha components
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000159 paint.setImageFilter(SkDisplacementMapEffect::Create(
160 SkDisplacementMapEffect::kR_ChannelSelectorType,
161 SkDisplacementMapEffect::kB_ChannelSelectorType, 32.0f, displ))->unref();
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000162 for (int i = 0; i < loops; ++i) {
robertphillips@google.com26e30c52013-10-28 18:07:44 +0000163 this->drawClippedBitmap(canvas, 200, 0, paint);
mtklein@google.comc2897432013-09-10 19:23:38 +0000164 }
sugoi@google.com580a1722013-04-23 14:20:45 +0000165 }
166
167private:
168 typedef DisplacementBaseBench INHERITED;
169};
170
171///////////////////////////////////////////////////////////////////////////////
172
mtklein@google.com410e6e82013-09-13 19:52:27 +0000173DEF_BENCH( return new DisplacementZeroBench(true); )
174DEF_BENCH( return new DisplacementAlphaBench(true); )
175DEF_BENCH( return new DisplacementFullBench(true); )
176DEF_BENCH( return new DisplacementZeroBench(false); )
177DEF_BENCH( return new DisplacementAlphaBench(false); )
178DEF_BENCH( return new DisplacementFullBench(false); )