blob: 3d0f215aafefc0979b2ecfc1d65b0195987e4b2a [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) {
27 make_bitmap();
28 make_checkerboard();
29 fInitialized = true;
30 }
31 }
32
33 void make_bitmap() {
34 const int w = isSmall() ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE;
35 const int h = isSmall() ? FILTER_HEIGHT_LARGE : FILTER_HEIGHT_LARGE;
36 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
49 void make_checkerboard() {
50 const int w = isSmall() ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE;
51 const int h = isSmall() ? FILTER_HEIGHT_LARGE : FILTER_HEIGHT_LARGE;
52 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),
77 SkIntToScalar(fBitmap.width()), SkIntToScalar(fBitmap.height())));
78 canvas->drawBitmap(fBitmap, SkIntToScalar(x), SkIntToScalar(y), &paint);
79 canvas->restore();
80 }
81
82 inline bool isSmall() const { return fIsSmall; }
83
84 SkBitmap fBitmap, fCheckerboard;
85private:
86 bool fInitialized;
87 bool fIsSmall;
88 typedef SkBenchmark INHERITED;
89};
90
91class DisplacementZeroBench : public DisplacementBaseBench {
92public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000093 DisplacementZeroBench(bool small) : INHERITED(small) {
sugoi@google.com580a1722013-04-23 14:20:45 +000094 }
95
96protected:
97 virtual const char* onGetName() SK_OVERRIDE {
98 return isSmall() ? "displacement_zero_small" : "displacement_zero_large";
99 }
100
101 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
102 SkPaint paint;
103 SkAutoTUnref<SkImageFilter> displ(SkNEW_ARGS(SkBitmapSource, (fCheckerboard)));
104 // No displacement effect
105 paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
106 (SkDisplacementMapEffect::kR_ChannelSelectorType,
107 SkDisplacementMapEffect::kG_ChannelSelectorType, 0.0f, displ)))->unref();
mtklein@google.comc2897432013-09-10 19:23:38 +0000108
109 for (int i = 0; i < this->getLoops(); i++) {
110 drawClippedBitmap(canvas, 0, 0, paint);
111 }
sugoi@google.com580a1722013-04-23 14:20:45 +0000112 }
113
114private:
115 typedef DisplacementBaseBench INHERITED;
116};
117
118class DisplacementAlphaBench : public DisplacementBaseBench {
119public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000120 DisplacementAlphaBench(bool small) : INHERITED(small) {
sugoi@google.com580a1722013-04-23 14:20:45 +0000121 }
122
123protected:
124 virtual const char* onGetName() SK_OVERRIDE {
125 return isSmall() ? "displacement_alpha_small" : "displacement_alpha_large";
126 }
127
128 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
129 SkPaint paint;
130 SkAutoTUnref<SkImageFilter> displ(SkNEW_ARGS(SkBitmapSource, (fCheckerboard)));
131 // Displacement, with 1 alpha component (which isn't pre-multiplied)
132 paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
133 (SkDisplacementMapEffect::kB_ChannelSelectorType,
134 SkDisplacementMapEffect::kA_ChannelSelectorType, 16.0f, displ)))->unref();
mtklein@google.comc2897432013-09-10 19:23:38 +0000135 for (int i = 0; i < this->getLoops(); i++) {
136 drawClippedBitmap(canvas, 100, 0, paint);
137 }
sugoi@google.com580a1722013-04-23 14:20:45 +0000138 }
139
140private:
141 typedef DisplacementBaseBench INHERITED;
142};
143
144class DisplacementFullBench : public DisplacementBaseBench {
145public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000146 DisplacementFullBench(bool small) : INHERITED(small) {
sugoi@google.com580a1722013-04-23 14:20:45 +0000147 }
148
149protected:
150 virtual const char* onGetName() SK_OVERRIDE {
151 return isSmall() ? "displacement_full_small" : "displacement_full_large";
152 }
153
154 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
155 SkPaint paint;
156 SkAutoTUnref<SkImageFilter> displ(SkNEW_ARGS(SkBitmapSource, (fCheckerboard)));
157 // Displacement, with 2 non-alpha components
158 paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
159 (SkDisplacementMapEffect::kR_ChannelSelectorType,
160 SkDisplacementMapEffect::kB_ChannelSelectorType, 32.0f, displ)))->unref();
mtklein@google.comc2897432013-09-10 19:23:38 +0000161 for (int i = 0; i < this->getLoops(); i++) {
162 drawClippedBitmap(canvas, 200, 0, paint);
163 }
sugoi@google.com580a1722013-04-23 14:20:45 +0000164 }
165
166private:
167 typedef DisplacementBaseBench INHERITED;
168};
169
170///////////////////////////////////////////////////////////////////////////////
171
mtklein@google.com410e6e82013-09-13 19:52:27 +0000172DEF_BENCH( return new DisplacementZeroBench(true); )
173DEF_BENCH( return new DisplacementAlphaBench(true); )
174DEF_BENCH( return new DisplacementFullBench(true); )
175DEF_BENCH( return new DisplacementZeroBench(false); )
176DEF_BENCH( return new DisplacementAlphaBench(false); )
177DEF_BENCH( return new DisplacementFullBench(false); )