blob: 3dd79658f2942738a45966ba7679b7bec46af309 [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:
robertphillipsbfe11fc2016-04-15 07:17:36 -070021 DisplacementBaseBench(bool small) : fInitialized(false), fIsSmall(small) { }
sugoi@google.com580a1722013-04-23 14:20:45 +000022
23protected:
joshualitt8a6697a2015-09-30 12:11:07 -070024 void onDelayedSetup() override {
sugoi@google.com580a1722013-04-23 14:20:45 +000025 if (!fInitialized) {
robertphillips@google.com26e30c52013-10-28 18:07:44 +000026 this->makeBitmap();
27 this->makeCheckerboard();
sugoi@google.com580a1722013-04-23 14:20:45 +000028 fInitialized = true;
29 }
30 }
31
robertphillips@google.com26e30c52013-10-28 18:07:44 +000032 void makeBitmap() {
33 const int w = this->isSmall() ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE;
34 const int h = this->isSmall() ? FILTER_HEIGHT_LARGE : FILTER_HEIGHT_LARGE;
reed6c225732014-06-09 19:52:07 -070035 fBitmap.allocN32Pixels(w, h);
36 SkCanvas canvas(fBitmap);
sugoi@google.com580a1722013-04-23 14:20:45 +000037 canvas.clear(0x00000000);
38 SkPaint paint;
39 paint.setAntiAlias(true);
40 paint.setColor(0xFF884422);
41 paint.setTextSize(SkIntToScalar(96));
42 const char* str = "g";
Cary Clark2a475ea2017-04-28 15:35:12 -040043 canvas.drawString(str, SkIntToScalar(15), SkIntToScalar(55), paint);
sugoi@google.com580a1722013-04-23 14:20:45 +000044 }
45
robertphillips@google.com26e30c52013-10-28 18:07:44 +000046 void makeCheckerboard() {
47 const int w = this->isSmall() ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE;
48 const int h = this->isSmall() ? FILTER_HEIGHT_LARGE : FILTER_HEIGHT_LARGE;
reede8f30622016-03-23 18:59:25 -070049 auto surface(SkSurface::MakeRasterN32Premul(w, h));
fmalita5598b632015-09-15 11:26:13 -070050 SkCanvas* canvas = surface->getCanvas();
51 canvas->clear(0x00000000);
sugoi@google.com580a1722013-04-23 14:20:45 +000052 SkPaint darkPaint;
53 darkPaint.setColor(0xFF804020);
54 SkPaint lightPaint;
55 lightPaint.setColor(0xFF244484);
56 for (int y = 0; y < h; y += 16) {
57 for (int x = 0; x < w; x += 16) {
fmalita5598b632015-09-15 11:26:13 -070058 canvas->save();
59 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
60 canvas->drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint);
61 canvas->drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint);
62 canvas->drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint);
63 canvas->drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint);
64 canvas->restore();
sugoi@google.com580a1722013-04-23 14:20:45 +000065 }
66 }
fmalita5598b632015-09-15 11:26:13 -070067
reed9ce9d672016-03-17 10:51:11 -070068 fCheckerboard = surface->makeImageSnapshot();
sugoi@google.com580a1722013-04-23 14:20:45 +000069 }
70
71 void drawClippedBitmap(SkCanvas* canvas, int x, int y, const SkPaint& paint) {
72 canvas->save();
73 canvas->clipRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y),
skia.committer@gmail.com70402c32013-10-29 07:01:50 +000074 SkIntToScalar(fBitmap.width()),
robertphillips@google.com26e30c52013-10-28 18:07:44 +000075 SkIntToScalar(fBitmap.height())));
sugoi@google.com580a1722013-04-23 14:20:45 +000076 canvas->drawBitmap(fBitmap, SkIntToScalar(x), SkIntToScalar(y), &paint);
77 canvas->restore();
78 }
79
80 inline bool isSmall() const { return fIsSmall; }
81
fmalita5598b632015-09-15 11:26:13 -070082 SkBitmap fBitmap;
reed9ce9d672016-03-17 10:51:11 -070083 sk_sp<SkImage> fCheckerboard;
fmalita5598b632015-09-15 11:26:13 -070084
sugoi@google.com580a1722013-04-23 14:20:45 +000085private:
86 bool fInitialized;
87 bool fIsSmall;
tfarinaf168b862014-06-19 12:32:29 -070088 typedef Benchmark INHERITED;
sugoi@google.com580a1722013-04-23 14:20:45 +000089};
90
91class DisplacementZeroBench : public DisplacementBaseBench {
92public:
robertphillipsbfe11fc2016-04-15 07:17:36 -070093 DisplacementZeroBench(bool small) : INHERITED(small) { }
sugoi@google.com580a1722013-04-23 14:20:45 +000094
95protected:
mtklein36352bf2015-03-25 18:17:31 -070096 const char* onGetName() override {
robertphillips@google.com26e30c52013-10-28 18:07:44 +000097 return this->isSmall() ? "displacement_zero_small" : "displacement_zero_large";
sugoi@google.com580a1722013-04-23 14:20:45 +000098 }
99
mtkleina1ebeb22015-10-01 09:43:39 -0700100 void onDraw(int loops, SkCanvas* canvas) override {
sugoi@google.com580a1722013-04-23 14:20:45 +0000101 SkPaint paint;
robertphillips549c8992016-04-01 09:28:51 -0700102 sk_sp<SkImageFilter> displ(SkImageSource::Make(fCheckerboard));
sugoi@google.com580a1722013-04-23 14:20:45 +0000103 // No displacement effect
robertphillipsbfe11fc2016-04-15 07:17:36 -0700104 paint.setImageFilter(SkDisplacementMapEffect::Make(
105 SkDisplacementMapEffect::kR_ChannelSelectorType,
106 SkDisplacementMapEffect::kG_ChannelSelectorType,
107 0.0f, std::move(displ), nullptr));
mtklein@google.comc2897432013-09-10 19:23:38 +0000108
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000109 for (int i = 0; i < loops; i++) {
robertphillips@google.com26e30c52013-10-28 18:07:44 +0000110 this->drawClippedBitmap(canvas, 0, 0, paint);
mtklein@google.comc2897432013-09-10 19:23:38 +0000111 }
sugoi@google.com580a1722013-04-23 14:20:45 +0000112 }
113
114private:
115 typedef DisplacementBaseBench INHERITED;
116};
117
118class DisplacementAlphaBench : public DisplacementBaseBench {
119public:
robertphillipsbfe11fc2016-04-15 07:17:36 -0700120 DisplacementAlphaBench(bool small) : INHERITED(small) { }
sugoi@google.com580a1722013-04-23 14:20:45 +0000121
122protected:
mtklein36352bf2015-03-25 18:17:31 -0700123 const char* onGetName() override {
sugoi@google.com580a1722013-04-23 14:20:45 +0000124 return isSmall() ? "displacement_alpha_small" : "displacement_alpha_large";
125 }
126
mtkleina1ebeb22015-10-01 09:43:39 -0700127 void onDraw(int loops, SkCanvas* canvas) override {
sugoi@google.com580a1722013-04-23 14:20:45 +0000128 SkPaint paint;
robertphillips549c8992016-04-01 09:28:51 -0700129 sk_sp<SkImageFilter> displ(SkImageSource::Make(fCheckerboard));
sugoi@google.com580a1722013-04-23 14:20:45 +0000130 // Displacement, with 1 alpha component (which isn't pre-multiplied)
robertphillipsbfe11fc2016-04-15 07:17:36 -0700131 paint.setImageFilter(SkDisplacementMapEffect::Make(
132 SkDisplacementMapEffect::kB_ChannelSelectorType,
133 SkDisplacementMapEffect::kA_ChannelSelectorType,
134 16.0f, std::move(displ), nullptr));
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000135 for (int i = 0; i < loops; i++) {
robertphillipsbfe11fc2016-04-15 07:17:36 -0700136 this->drawClippedBitmap(canvas, 100, 0, paint);
mtklein@google.comc2897432013-09-10 19:23:38 +0000137 }
sugoi@google.com580a1722013-04-23 14:20:45 +0000138 }
139
140private:
141 typedef DisplacementBaseBench INHERITED;
142};
143
144class DisplacementFullBench : public DisplacementBaseBench {
145public:
robertphillipsbfe11fc2016-04-15 07:17:36 -0700146 DisplacementFullBench(bool small) : INHERITED(small) { }
sugoi@google.com580a1722013-04-23 14:20:45 +0000147
148protected:
mtklein36352bf2015-03-25 18:17:31 -0700149 const char* onGetName() override {
sugoi@google.com580a1722013-04-23 14:20:45 +0000150 return isSmall() ? "displacement_full_small" : "displacement_full_large";
151 }
152
mtkleina1ebeb22015-10-01 09:43:39 -0700153 void onDraw(int loops, SkCanvas* canvas) override {
sugoi@google.com580a1722013-04-23 14:20:45 +0000154 SkPaint paint;
robertphillips549c8992016-04-01 09:28:51 -0700155 sk_sp<SkImageFilter> displ(SkImageSource::Make(fCheckerboard));
sugoi@google.com580a1722013-04-23 14:20:45 +0000156 // Displacement, with 2 non-alpha components
robertphillipsbfe11fc2016-04-15 07:17:36 -0700157 paint.setImageFilter(SkDisplacementMapEffect::Make(
158 SkDisplacementMapEffect::kR_ChannelSelectorType,
159 SkDisplacementMapEffect::kB_ChannelSelectorType,
160 32.0f, std::move(displ), nullptr));
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000161 for (int i = 0; i < loops; ++i) {
robertphillips@google.com26e30c52013-10-28 18:07:44 +0000162 this->drawClippedBitmap(canvas, 200, 0, paint);
mtklein@google.comc2897432013-09-10 19:23:38 +0000163 }
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); )