blob: b86577c1bfd7fd329274a793bc4d1f6e55d99c3f [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"
Mike Reedac9f0c92020-12-23 10:11:33 -05009#include "include/core/SkBitmap.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkCanvas.h"
11#include "include/core/SkFont.h"
12#include "include/core/SkSurface.h"
Michael Ludwig23003182019-08-05 11:25:23 -040013#include "include/effects/SkImageFilters.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;
Greg Kaiserfb7a7132019-02-08 17:00:36 -080035 const int h = this->isSmall() ? FILTER_HEIGHT_SMALL : FILTER_HEIGHT_LARGE;
Mike Reed069e4842021-01-24 11:39:58 -050036 auto surf = SkSurface::MakeRasterN32Premul(w, h);
sugoi@google.com580a1722013-04-23 14:20:45 +000037 SkPaint paint;
sugoi@google.com580a1722013-04-23 14:20:45 +000038 paint.setColor(0xFF884422);
Mike Reed89126e42019-01-03 12:59:14 -050039
40 SkFont font;
41 font.setSize(SkIntToScalar(96));
Mike Reed069e4842021-01-24 11:39:58 -050042 surf->getCanvas()->drawSimpleText("g", 1, SkTextEncoding::kUTF8, 15, 55, font, paint);
43 fImage = surf->makeImageSnapshot();
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();
Mike Reed069e4842021-01-24 11:39:58 -050073 canvas->clipIRect(fImage->bounds().makeOffset(x, y));
Mike Reed039f1362021-01-27 21:21:08 -050074 canvas->drawImage(fImage, SkIntToScalar(x), SkIntToScalar(y), SkSamplingOptions(), &paint);
sugoi@google.com580a1722013-04-23 14:20:45 +000075 canvas->restore();
76 }
77
78 inline bool isSmall() const { return fIsSmall; }
79
Mike Reed069e4842021-01-24 11:39:58 -050080 sk_sp<SkImage> fImage, fCheckerboard;
fmalita5598b632015-09-15 11:26:13 -070081
sugoi@google.com580a1722013-04-23 14:20:45 +000082private:
83 bool fInitialized;
84 bool fIsSmall;
John Stiles7571f9e2020-09-02 22:42:33 -040085 using INHERITED = Benchmark;
sugoi@google.com580a1722013-04-23 14:20:45 +000086};
87
88class DisplacementZeroBench : public DisplacementBaseBench {
89public:
robertphillipsbfe11fc2016-04-15 07:17:36 -070090 DisplacementZeroBench(bool small) : INHERITED(small) { }
sugoi@google.com580a1722013-04-23 14:20:45 +000091
92protected:
mtklein36352bf2015-03-25 18:17:31 -070093 const char* onGetName() override {
robertphillips@google.com26e30c52013-10-28 18:07:44 +000094 return this->isSmall() ? "displacement_zero_small" : "displacement_zero_large";
sugoi@google.com580a1722013-04-23 14:20:45 +000095 }
96
mtkleina1ebeb22015-10-01 09:43:39 -070097 void onDraw(int loops, SkCanvas* canvas) override {
sugoi@google.com580a1722013-04-23 14:20:45 +000098 SkPaint paint;
Michael Ludwig23003182019-08-05 11:25:23 -040099 sk_sp<SkImageFilter> displ(SkImageFilters::Image(fCheckerboard));
sugoi@google.com580a1722013-04-23 14:20:45 +0000100 // No displacement effect
Michael Ludwig23003182019-08-05 11:25:23 -0400101 paint.setImageFilter(SkImageFilters::DisplacementMap(SkColorChannel::kR, SkColorChannel::kG,
102 0.0f, std::move(displ), nullptr));
mtklein@google.comc2897432013-09-10 19:23:38 +0000103
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000104 for (int i = 0; i < loops; i++) {
robertphillips@google.com26e30c52013-10-28 18:07:44 +0000105 this->drawClippedBitmap(canvas, 0, 0, paint);
mtklein@google.comc2897432013-09-10 19:23:38 +0000106 }
sugoi@google.com580a1722013-04-23 14:20:45 +0000107 }
108
109private:
John Stiles7571f9e2020-09-02 22:42:33 -0400110 using INHERITED = DisplacementBaseBench;
sugoi@google.com580a1722013-04-23 14:20:45 +0000111};
112
113class DisplacementAlphaBench : public DisplacementBaseBench {
114public:
robertphillipsbfe11fc2016-04-15 07:17:36 -0700115 DisplacementAlphaBench(bool small) : INHERITED(small) { }
sugoi@google.com580a1722013-04-23 14:20:45 +0000116
117protected:
mtklein36352bf2015-03-25 18:17:31 -0700118 const char* onGetName() override {
sugoi@google.com580a1722013-04-23 14:20:45 +0000119 return isSmall() ? "displacement_alpha_small" : "displacement_alpha_large";
120 }
121
mtkleina1ebeb22015-10-01 09:43:39 -0700122 void onDraw(int loops, SkCanvas* canvas) override {
sugoi@google.com580a1722013-04-23 14:20:45 +0000123 SkPaint paint;
Michael Ludwig23003182019-08-05 11:25:23 -0400124 sk_sp<SkImageFilter> displ(SkImageFilters::Image(fCheckerboard));
sugoi@google.com580a1722013-04-23 14:20:45 +0000125 // Displacement, with 1 alpha component (which isn't pre-multiplied)
Michael Ludwig23003182019-08-05 11:25:23 -0400126 paint.setImageFilter(SkImageFilters::DisplacementMap(SkColorChannel::kB, SkColorChannel::kA,
127 16.0f, std::move(displ), nullptr));
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000128 for (int i = 0; i < loops; i++) {
robertphillipsbfe11fc2016-04-15 07:17:36 -0700129 this->drawClippedBitmap(canvas, 100, 0, paint);
mtklein@google.comc2897432013-09-10 19:23:38 +0000130 }
sugoi@google.com580a1722013-04-23 14:20:45 +0000131 }
132
133private:
John Stiles7571f9e2020-09-02 22:42:33 -0400134 using INHERITED = DisplacementBaseBench;
sugoi@google.com580a1722013-04-23 14:20:45 +0000135};
136
137class DisplacementFullBench : public DisplacementBaseBench {
138public:
robertphillipsbfe11fc2016-04-15 07:17:36 -0700139 DisplacementFullBench(bool small) : INHERITED(small) { }
sugoi@google.com580a1722013-04-23 14:20:45 +0000140
141protected:
mtklein36352bf2015-03-25 18:17:31 -0700142 const char* onGetName() override {
sugoi@google.com580a1722013-04-23 14:20:45 +0000143 return isSmall() ? "displacement_full_small" : "displacement_full_large";
144 }
145
mtkleina1ebeb22015-10-01 09:43:39 -0700146 void onDraw(int loops, SkCanvas* canvas) override {
sugoi@google.com580a1722013-04-23 14:20:45 +0000147 SkPaint paint;
Michael Ludwig23003182019-08-05 11:25:23 -0400148 sk_sp<SkImageFilter> displ(SkImageFilters::Image(fCheckerboard));
sugoi@google.com580a1722013-04-23 14:20:45 +0000149 // Displacement, with 2 non-alpha components
Michael Ludwig23003182019-08-05 11:25:23 -0400150 paint.setImageFilter(SkImageFilters::DisplacementMap(SkColorChannel::kR, SkColorChannel::kB,
151 32.0f, std::move(displ), nullptr));
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000152 for (int i = 0; i < loops; ++i) {
robertphillips@google.com26e30c52013-10-28 18:07:44 +0000153 this->drawClippedBitmap(canvas, 200, 0, paint);
mtklein@google.comc2897432013-09-10 19:23:38 +0000154 }
sugoi@google.com580a1722013-04-23 14:20:45 +0000155 }
156
157private:
John Stiles7571f9e2020-09-02 22:42:33 -0400158 using INHERITED = DisplacementBaseBench;
sugoi@google.com580a1722013-04-23 14:20:45 +0000159};
160
161///////////////////////////////////////////////////////////////////////////////
162
mtklein@google.com410e6e82013-09-13 19:52:27 +0000163DEF_BENCH( return new DisplacementZeroBench(true); )
164DEF_BENCH( return new DisplacementAlphaBench(true); )
165DEF_BENCH( return new DisplacementFullBench(true); )
166DEF_BENCH( return new DisplacementZeroBench(false); )
167DEF_BENCH( return new DisplacementAlphaBench(false); )
168DEF_BENCH( return new DisplacementFullBench(false); )