blob: f3f9472061fd8eb61ef8626e252d497c75c420ee [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 "SkCanvas.h"
sugoi@google.com580a1722013-04-23 14:20:45 +000010#include "SkDisplacementMapEffect.h"
fmalita5598b632015-09-15 11:26:13 -070011#include "SkImageSource.h"
12#include "SkSurface.h"
sugoi@google.com580a1722013-04-23 14:20:45 +000013
sugoi@google.comca425d92013-04-23 14:37:38 +000014#define FILTER_WIDTH_SMALL 32
15#define FILTER_HEIGHT_SMALL 32
16#define FILTER_WIDTH_LARGE 256
17#define FILTER_HEIGHT_LARGE 256
sugoi@google.com580a1722013-04-23 14:20:45 +000018
tfarinaf168b862014-06-19 12:32:29 -070019class DisplacementBaseBench : public Benchmark {
sugoi@google.com580a1722013-04-23 14:20:45 +000020public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000021 DisplacementBaseBench(bool small) :
22 fInitialized(false), fIsSmall(small) {
sugoi@google.com580a1722013-04-23 14:20:45 +000023 }
24
25protected:
joshualitt8a6697a2015-09-30 12:11:07 -070026 void onDelayedSetup() override {
sugoi@google.com580a1722013-04-23 14:20:45 +000027 if (!fInitialized) {
robertphillips@google.com26e30c52013-10-28 18:07:44 +000028 this->makeBitmap();
29 this->makeCheckerboard();
sugoi@google.com580a1722013-04-23 14:20:45 +000030 fInitialized = true;
31 }
32 }
33
robertphillips@google.com26e30c52013-10-28 18:07:44 +000034 void makeBitmap() {
35 const int w = this->isSmall() ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE;
36 const int h = this->isSmall() ? FILTER_HEIGHT_LARGE : FILTER_HEIGHT_LARGE;
reed6c225732014-06-09 19:52:07 -070037 fBitmap.allocN32Pixels(w, h);
38 SkCanvas canvas(fBitmap);
sugoi@google.com580a1722013-04-23 14:20:45 +000039 canvas.clear(0x00000000);
40 SkPaint paint;
41 paint.setAntiAlias(true);
42 paint.setColor(0xFF884422);
43 paint.setTextSize(SkIntToScalar(96));
44 const char* str = "g";
45 canvas.drawText(str, strlen(str), SkIntToScalar(15), SkIntToScalar(55), paint);
46 }
47
robertphillips@google.com26e30c52013-10-28 18:07:44 +000048 void makeCheckerboard() {
49 const int w = this->isSmall() ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE;
50 const int h = this->isSmall() ? FILTER_HEIGHT_LARGE : FILTER_HEIGHT_LARGE;
fmalita5598b632015-09-15 11:26:13 -070051 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(w, h));
52 SkCanvas* canvas = surface->getCanvas();
53 canvas->clear(0x00000000);
sugoi@google.com580a1722013-04-23 14:20:45 +000054 SkPaint darkPaint;
55 darkPaint.setColor(0xFF804020);
56 SkPaint lightPaint;
57 lightPaint.setColor(0xFF244484);
58 for (int y = 0; y < h; y += 16) {
59 for (int x = 0; x < w; x += 16) {
fmalita5598b632015-09-15 11:26:13 -070060 canvas->save();
61 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
62 canvas->drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint);
63 canvas->drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint);
64 canvas->drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint);
65 canvas->drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint);
66 canvas->restore();
sugoi@google.com580a1722013-04-23 14:20:45 +000067 }
68 }
fmalita5598b632015-09-15 11:26:13 -070069
70 fCheckerboard.reset(surface->newImageSnapshot());
sugoi@google.com580a1722013-04-23 14:20:45 +000071 }
72
73 void drawClippedBitmap(SkCanvas* canvas, int x, int y, const SkPaint& paint) {
74 canvas->save();
75 canvas->clipRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y),
skia.committer@gmail.com70402c32013-10-29 07:01:50 +000076 SkIntToScalar(fBitmap.width()),
robertphillips@google.com26e30c52013-10-28 18:07:44 +000077 SkIntToScalar(fBitmap.height())));
sugoi@google.com580a1722013-04-23 14:20:45 +000078 canvas->drawBitmap(fBitmap, SkIntToScalar(x), SkIntToScalar(y), &paint);
79 canvas->restore();
80 }
81
82 inline bool isSmall() const { return fIsSmall; }
83
fmalita5598b632015-09-15 11:26:13 -070084 SkBitmap fBitmap;
85 SkAutoTUnref<SkImage> fCheckerboard;
86
sugoi@google.com580a1722013-04-23 14:20:45 +000087private:
88 bool fInitialized;
89 bool fIsSmall;
tfarinaf168b862014-06-19 12:32:29 -070090 typedef Benchmark INHERITED;
sugoi@google.com580a1722013-04-23 14:20:45 +000091};
92
93class DisplacementZeroBench : public DisplacementBaseBench {
94public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000095 DisplacementZeroBench(bool small) : INHERITED(small) {
sugoi@google.com580a1722013-04-23 14:20:45 +000096 }
97
98protected:
mtklein36352bf2015-03-25 18:17:31 -070099 const char* onGetName() override {
robertphillips@google.com26e30c52013-10-28 18:07:44 +0000100 return this->isSmall() ? "displacement_zero_small" : "displacement_zero_large";
sugoi@google.com580a1722013-04-23 14:20:45 +0000101 }
102
mtkleina1ebeb22015-10-01 09:43:39 -0700103 void onDraw(int loops, SkCanvas* canvas) override {
sugoi@google.com580a1722013-04-23 14:20:45 +0000104 SkPaint paint;
fmalita5598b632015-09-15 11:26:13 -0700105 SkAutoTUnref<SkImageFilter> displ(SkImageSource::Create(fCheckerboard));
sugoi@google.com580a1722013-04-23 14:20:45 +0000106 // No displacement effect
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000107 paint.setImageFilter(SkDisplacementMapEffect::Create(
108 SkDisplacementMapEffect::kR_ChannelSelectorType,
109 SkDisplacementMapEffect::kG_ChannelSelectorType, 0.0f, displ))->unref();
mtklein@google.comc2897432013-09-10 19:23:38 +0000110
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000111 for (int i = 0; i < loops; i++) {
robertphillips@google.com26e30c52013-10-28 18:07:44 +0000112 this->drawClippedBitmap(canvas, 0, 0, paint);
mtklein@google.comc2897432013-09-10 19:23:38 +0000113 }
sugoi@google.com580a1722013-04-23 14:20:45 +0000114 }
115
116private:
117 typedef DisplacementBaseBench INHERITED;
118};
119
120class DisplacementAlphaBench : public DisplacementBaseBench {
121public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000122 DisplacementAlphaBench(bool small) : INHERITED(small) {
sugoi@google.com580a1722013-04-23 14:20:45 +0000123 }
124
125protected:
mtklein36352bf2015-03-25 18:17:31 -0700126 const char* onGetName() override {
sugoi@google.com580a1722013-04-23 14:20:45 +0000127 return isSmall() ? "displacement_alpha_small" : "displacement_alpha_large";
128 }
129
mtkleina1ebeb22015-10-01 09:43:39 -0700130 void onDraw(int loops, SkCanvas* canvas) override {
sugoi@google.com580a1722013-04-23 14:20:45 +0000131 SkPaint paint;
fmalita5598b632015-09-15 11:26:13 -0700132 SkAutoTUnref<SkImageFilter> displ(SkImageSource::Create(fCheckerboard));
sugoi@google.com580a1722013-04-23 14:20:45 +0000133 // Displacement, with 1 alpha component (which isn't pre-multiplied)
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000134 paint.setImageFilter(SkDisplacementMapEffect::Create(
135 SkDisplacementMapEffect::kB_ChannelSelectorType,
136 SkDisplacementMapEffect::kA_ChannelSelectorType, 16.0f, displ))->unref();
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000137 for (int i = 0; i < loops; i++) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000138 drawClippedBitmap(canvas, 100, 0, paint);
139 }
sugoi@google.com580a1722013-04-23 14:20:45 +0000140 }
141
142private:
143 typedef DisplacementBaseBench INHERITED;
144};
145
146class DisplacementFullBench : public DisplacementBaseBench {
147public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000148 DisplacementFullBench(bool small) : INHERITED(small) {
sugoi@google.com580a1722013-04-23 14:20:45 +0000149 }
150
151protected:
mtklein36352bf2015-03-25 18:17:31 -0700152 const char* onGetName() override {
sugoi@google.com580a1722013-04-23 14:20:45 +0000153 return isSmall() ? "displacement_full_small" : "displacement_full_large";
154 }
155
mtkleina1ebeb22015-10-01 09:43:39 -0700156 void onDraw(int loops, SkCanvas* canvas) override {
sugoi@google.com580a1722013-04-23 14:20:45 +0000157 SkPaint paint;
fmalita5598b632015-09-15 11:26:13 -0700158 SkAutoTUnref<SkImageFilter> displ(SkImageSource::Create(fCheckerboard));
sugoi@google.com580a1722013-04-23 14:20:45 +0000159 // Displacement, with 2 non-alpha components
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000160 paint.setImageFilter(SkDisplacementMapEffect::Create(
161 SkDisplacementMapEffect::kR_ChannelSelectorType,
162 SkDisplacementMapEffect::kB_ChannelSelectorType, 32.0f, displ))->unref();
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000163 for (int i = 0; i < loops; ++i) {
robertphillips@google.com26e30c52013-10-28 18:07:44 +0000164 this->drawClippedBitmap(canvas, 200, 0, paint);
mtklein@google.comc2897432013-09-10 19:23:38 +0000165 }
sugoi@google.com580a1722013-04-23 14:20:45 +0000166 }
167
168private:
169 typedef DisplacementBaseBench INHERITED;
170};
171
172///////////////////////////////////////////////////////////////////////////////
173
mtklein@google.com410e6e82013-09-13 19:52:27 +0000174DEF_BENCH( return new DisplacementZeroBench(true); )
175DEF_BENCH( return new DisplacementAlphaBench(true); )
176DEF_BENCH( return new DisplacementFullBench(true); )
177DEF_BENCH( return new DisplacementZeroBench(false); )
178DEF_BENCH( return new DisplacementAlphaBench(false); )
179DEF_BENCH( return new DisplacementFullBench(false); )