blob: 37d53d1d52b302047d8364bafebfaa1d19b2cb01 [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"
Mike Reed89126e42019-01-03 12:59:14 -050010#include "SkFont.h"
sugoi@google.com580a1722013-04-23 14:20:45 +000011#include "SkDisplacementMapEffect.h"
fmalita5598b632015-09-15 11:26:13 -070012#include "SkImageSource.h"
13#include "SkSurface.h"
sugoi@google.com580a1722013-04-23 14:20:45 +000014
sugoi@google.comca425d92013-04-23 14:37:38 +000015#define FILTER_WIDTH_SMALL 32
16#define FILTER_HEIGHT_SMALL 32
17#define FILTER_WIDTH_LARGE 256
18#define FILTER_HEIGHT_LARGE 256
sugoi@google.com580a1722013-04-23 14:20:45 +000019
tfarinaf168b862014-06-19 12:32:29 -070020class DisplacementBaseBench : public Benchmark {
sugoi@google.com580a1722013-04-23 14:20:45 +000021public:
robertphillipsbfe11fc2016-04-15 07:17:36 -070022 DisplacementBaseBench(bool small) : fInitialized(false), fIsSmall(small) { }
sugoi@google.com580a1722013-04-23 14:20:45 +000023
24protected:
joshualitt8a6697a2015-09-30 12:11:07 -070025 void onDelayedSetup() 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;
sugoi@google.com580a1722013-04-23 14:20:45 +000040 paint.setColor(0xFF884422);
Mike Reed89126e42019-01-03 12:59:14 -050041
42 SkFont font;
43 font.setSize(SkIntToScalar(96));
44 canvas.drawSimpleText("g", 1, kUTF8_SkTextEncoding, SkIntToScalar(15), SkIntToScalar(55), font, paint);
sugoi@google.com580a1722013-04-23 14:20:45 +000045 }
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;
reede8f30622016-03-23 18:59:25 -070050 auto surface(SkSurface::MakeRasterN32Premul(w, h));
fmalita5598b632015-09-15 11:26:13 -070051 SkCanvas* canvas = surface->getCanvas();
52 canvas->clear(0x00000000);
sugoi@google.com580a1722013-04-23 14:20:45 +000053 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) {
fmalita5598b632015-09-15 11:26:13 -070059 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();
sugoi@google.com580a1722013-04-23 14:20:45 +000066 }
67 }
fmalita5598b632015-09-15 11:26:13 -070068
reed9ce9d672016-03-17 10:51:11 -070069 fCheckerboard = surface->makeImageSnapshot();
sugoi@google.com580a1722013-04-23 14:20:45 +000070 }
71
72 void drawClippedBitmap(SkCanvas* canvas, int x, int y, const SkPaint& paint) {
73 canvas->save();
74 canvas->clipRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y),
skia.committer@gmail.com70402c32013-10-29 07:01:50 +000075 SkIntToScalar(fBitmap.width()),
robertphillips@google.com26e30c52013-10-28 18:07:44 +000076 SkIntToScalar(fBitmap.height())));
sugoi@google.com580a1722013-04-23 14:20:45 +000077 canvas->drawBitmap(fBitmap, SkIntToScalar(x), SkIntToScalar(y), &paint);
78 canvas->restore();
79 }
80
81 inline bool isSmall() const { return fIsSmall; }
82
fmalita5598b632015-09-15 11:26:13 -070083 SkBitmap fBitmap;
reed9ce9d672016-03-17 10:51:11 -070084 sk_sp<SkImage> fCheckerboard;
fmalita5598b632015-09-15 11:26:13 -070085
sugoi@google.com580a1722013-04-23 14:20:45 +000086private:
87 bool fInitialized;
88 bool fIsSmall;
tfarinaf168b862014-06-19 12:32:29 -070089 typedef Benchmark INHERITED;
sugoi@google.com580a1722013-04-23 14:20:45 +000090};
91
92class DisplacementZeroBench : public DisplacementBaseBench {
93public:
robertphillipsbfe11fc2016-04-15 07:17:36 -070094 DisplacementZeroBench(bool small) : INHERITED(small) { }
sugoi@google.com580a1722013-04-23 14:20:45 +000095
96protected:
mtklein36352bf2015-03-25 18:17:31 -070097 const char* onGetName() override {
robertphillips@google.com26e30c52013-10-28 18:07:44 +000098 return this->isSmall() ? "displacement_zero_small" : "displacement_zero_large";
sugoi@google.com580a1722013-04-23 14:20:45 +000099 }
100
mtkleina1ebeb22015-10-01 09:43:39 -0700101 void onDraw(int loops, SkCanvas* canvas) override {
sugoi@google.com580a1722013-04-23 14:20:45 +0000102 SkPaint paint;
robertphillips549c8992016-04-01 09:28:51 -0700103 sk_sp<SkImageFilter> displ(SkImageSource::Make(fCheckerboard));
sugoi@google.com580a1722013-04-23 14:20:45 +0000104 // No displacement effect
robertphillipsbfe11fc2016-04-15 07:17:36 -0700105 paint.setImageFilter(SkDisplacementMapEffect::Make(
106 SkDisplacementMapEffect::kR_ChannelSelectorType,
107 SkDisplacementMapEffect::kG_ChannelSelectorType,
108 0.0f, std::move(displ), nullptr));
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:
robertphillipsbfe11fc2016-04-15 07:17:36 -0700121 DisplacementAlphaBench(bool small) : INHERITED(small) { }
sugoi@google.com580a1722013-04-23 14:20:45 +0000122
123protected:
mtklein36352bf2015-03-25 18:17:31 -0700124 const char* onGetName() override {
sugoi@google.com580a1722013-04-23 14:20:45 +0000125 return isSmall() ? "displacement_alpha_small" : "displacement_alpha_large";
126 }
127
mtkleina1ebeb22015-10-01 09:43:39 -0700128 void onDraw(int loops, SkCanvas* canvas) override {
sugoi@google.com580a1722013-04-23 14:20:45 +0000129 SkPaint paint;
robertphillips549c8992016-04-01 09:28:51 -0700130 sk_sp<SkImageFilter> displ(SkImageSource::Make(fCheckerboard));
sugoi@google.com580a1722013-04-23 14:20:45 +0000131 // Displacement, with 1 alpha component (which isn't pre-multiplied)
robertphillipsbfe11fc2016-04-15 07:17:36 -0700132 paint.setImageFilter(SkDisplacementMapEffect::Make(
133 SkDisplacementMapEffect::kB_ChannelSelectorType,
134 SkDisplacementMapEffect::kA_ChannelSelectorType,
135 16.0f, std::move(displ), nullptr));
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000136 for (int i = 0; i < loops; i++) {
robertphillipsbfe11fc2016-04-15 07:17:36 -0700137 this->drawClippedBitmap(canvas, 100, 0, paint);
mtklein@google.comc2897432013-09-10 19:23:38 +0000138 }
sugoi@google.com580a1722013-04-23 14:20:45 +0000139 }
140
141private:
142 typedef DisplacementBaseBench INHERITED;
143};
144
145class DisplacementFullBench : public DisplacementBaseBench {
146public:
robertphillipsbfe11fc2016-04-15 07:17:36 -0700147 DisplacementFullBench(bool small) : INHERITED(small) { }
sugoi@google.com580a1722013-04-23 14:20:45 +0000148
149protected:
mtklein36352bf2015-03-25 18:17:31 -0700150 const char* onGetName() override {
sugoi@google.com580a1722013-04-23 14:20:45 +0000151 return isSmall() ? "displacement_full_small" : "displacement_full_large";
152 }
153
mtkleina1ebeb22015-10-01 09:43:39 -0700154 void onDraw(int loops, SkCanvas* canvas) override {
sugoi@google.com580a1722013-04-23 14:20:45 +0000155 SkPaint paint;
robertphillips549c8992016-04-01 09:28:51 -0700156 sk_sp<SkImageFilter> displ(SkImageSource::Make(fCheckerboard));
sugoi@google.com580a1722013-04-23 14:20:45 +0000157 // Displacement, with 2 non-alpha components
robertphillipsbfe11fc2016-04-15 07:17:36 -0700158 paint.setImageFilter(SkDisplacementMapEffect::Make(
159 SkDisplacementMapEffect::kR_ChannelSelectorType,
160 SkDisplacementMapEffect::kB_ChannelSelectorType,
161 32.0f, std::move(displ), nullptr));
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); )