blob: 6c9a6bf7bdd5cd886b777540299764c843360793 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "bench/Benchmark.h"
9#include "include/core/SkCanvas.h"
10#include "include/core/SkFont.h"
11#include "include/core/SkSurface.h"
Michael Ludwig23003182019-08-05 11:25:23 -040012#include "include/effects/SkImageFilters.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;
Greg Kaiserfb7a7132019-02-08 17:00:36 -080034 const int h = this->isSmall() ? FILTER_HEIGHT_SMALL : 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;
sugoi@google.com580a1722013-04-23 14:20:45 +000039 paint.setColor(0xFF884422);
Mike Reed89126e42019-01-03 12:59:14 -050040
41 SkFont font;
42 font.setSize(SkIntToScalar(96));
Ben Wagner51e15a62019-05-07 15:38:46 -040043 canvas.drawSimpleText("g", 1, SkTextEncoding::kUTF8, SkIntToScalar(15), SkIntToScalar(55), font, 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;
Greg Kaiserfb7a7132019-02-08 17:00:36 -080048 const int h = this->isSmall() ? FILTER_HEIGHT_SMALL : 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;
Michael Ludwig23003182019-08-05 11:25:23 -0400102 sk_sp<SkImageFilter> displ(SkImageFilters::Image(fCheckerboard));
sugoi@google.com580a1722013-04-23 14:20:45 +0000103 // No displacement effect
Michael Ludwig23003182019-08-05 11:25:23 -0400104 paint.setImageFilter(SkImageFilters::DisplacementMap(SkColorChannel::kR, SkColorChannel::kG,
105 0.0f, std::move(displ), nullptr));
mtklein@google.comc2897432013-09-10 19:23:38 +0000106
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000107 for (int i = 0; i < loops; i++) {
robertphillips@google.com26e30c52013-10-28 18:07:44 +0000108 this->drawClippedBitmap(canvas, 0, 0, paint);
mtklein@google.comc2897432013-09-10 19:23:38 +0000109 }
sugoi@google.com580a1722013-04-23 14:20:45 +0000110 }
111
112private:
113 typedef DisplacementBaseBench INHERITED;
114};
115
116class DisplacementAlphaBench : public DisplacementBaseBench {
117public:
robertphillipsbfe11fc2016-04-15 07:17:36 -0700118 DisplacementAlphaBench(bool small) : INHERITED(small) { }
sugoi@google.com580a1722013-04-23 14:20:45 +0000119
120protected:
mtklein36352bf2015-03-25 18:17:31 -0700121 const char* onGetName() override {
sugoi@google.com580a1722013-04-23 14:20:45 +0000122 return isSmall() ? "displacement_alpha_small" : "displacement_alpha_large";
123 }
124
mtkleina1ebeb22015-10-01 09:43:39 -0700125 void onDraw(int loops, SkCanvas* canvas) override {
sugoi@google.com580a1722013-04-23 14:20:45 +0000126 SkPaint paint;
Michael Ludwig23003182019-08-05 11:25:23 -0400127 sk_sp<SkImageFilter> displ(SkImageFilters::Image(fCheckerboard));
sugoi@google.com580a1722013-04-23 14:20:45 +0000128 // Displacement, with 1 alpha component (which isn't pre-multiplied)
Michael Ludwig23003182019-08-05 11:25:23 -0400129 paint.setImageFilter(SkImageFilters::DisplacementMap(SkColorChannel::kB, SkColorChannel::kA,
130 16.0f, std::move(displ), nullptr));
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000131 for (int i = 0; i < loops; i++) {
robertphillipsbfe11fc2016-04-15 07:17:36 -0700132 this->drawClippedBitmap(canvas, 100, 0, paint);
mtklein@google.comc2897432013-09-10 19:23:38 +0000133 }
sugoi@google.com580a1722013-04-23 14:20:45 +0000134 }
135
136private:
137 typedef DisplacementBaseBench INHERITED;
138};
139
140class DisplacementFullBench : public DisplacementBaseBench {
141public:
robertphillipsbfe11fc2016-04-15 07:17:36 -0700142 DisplacementFullBench(bool small) : INHERITED(small) { }
sugoi@google.com580a1722013-04-23 14:20:45 +0000143
144protected:
mtklein36352bf2015-03-25 18:17:31 -0700145 const char* onGetName() override {
sugoi@google.com580a1722013-04-23 14:20:45 +0000146 return isSmall() ? "displacement_full_small" : "displacement_full_large";
147 }
148
mtkleina1ebeb22015-10-01 09:43:39 -0700149 void onDraw(int loops, SkCanvas* canvas) override {
sugoi@google.com580a1722013-04-23 14:20:45 +0000150 SkPaint paint;
Michael Ludwig23003182019-08-05 11:25:23 -0400151 sk_sp<SkImageFilter> displ(SkImageFilters::Image(fCheckerboard));
sugoi@google.com580a1722013-04-23 14:20:45 +0000152 // Displacement, with 2 non-alpha components
Michael Ludwig23003182019-08-05 11:25:23 -0400153 paint.setImageFilter(SkImageFilters::DisplacementMap(SkColorChannel::kR, SkColorChannel::kB,
154 32.0f, std::move(displ), nullptr));
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000155 for (int i = 0; i < loops; ++i) {
robertphillips@google.com26e30c52013-10-28 18:07:44 +0000156 this->drawClippedBitmap(canvas, 200, 0, paint);
mtklein@google.comc2897432013-09-10 19:23:38 +0000157 }
sugoi@google.com580a1722013-04-23 14:20:45 +0000158 }
159
160private:
161 typedef DisplacementBaseBench INHERITED;
162};
163
164///////////////////////////////////////////////////////////////////////////////
165
mtklein@google.com410e6e82013-09-13 19:52:27 +0000166DEF_BENCH( return new DisplacementZeroBench(true); )
167DEF_BENCH( return new DisplacementAlphaBench(true); )
168DEF_BENCH( return new DisplacementFullBench(true); )
169DEF_BENCH( return new DisplacementZeroBench(false); )
170DEF_BENCH( return new DisplacementAlphaBench(false); )
171DEF_BENCH( return new DisplacementFullBench(false); )