blob: 991bfac5d39ce7e98753f0dc45e78d2b45fb9268 [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;
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));
Ben Wagner51e15a62019-05-07 15:38:46 -040044 canvas.drawSimpleText("g", 1, SkTextEncoding::kUTF8, 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;
Greg Kaiserfb7a7132019-02-08 17:00:36 -080049 const int h = this->isSmall() ? FILTER_HEIGHT_SMALL : 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;
John Stiles7571f9e2020-09-02 22:42:33 -040089 using INHERITED = Benchmark;
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;
Michael Ludwig23003182019-08-05 11:25:23 -0400103 sk_sp<SkImageFilter> displ(SkImageFilters::Image(fCheckerboard));
sugoi@google.com580a1722013-04-23 14:20:45 +0000104 // No displacement effect
Michael Ludwig23003182019-08-05 11:25:23 -0400105 paint.setImageFilter(SkImageFilters::DisplacementMap(SkColorChannel::kR, SkColorChannel::kG,
106 0.0f, std::move(displ), nullptr));
mtklein@google.comc2897432013-09-10 19:23:38 +0000107
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000108 for (int i = 0; i < loops; i++) {
robertphillips@google.com26e30c52013-10-28 18:07:44 +0000109 this->drawClippedBitmap(canvas, 0, 0, paint);
mtklein@google.comc2897432013-09-10 19:23:38 +0000110 }
sugoi@google.com580a1722013-04-23 14:20:45 +0000111 }
112
113private:
John Stiles7571f9e2020-09-02 22:42:33 -0400114 using INHERITED = DisplacementBaseBench;
sugoi@google.com580a1722013-04-23 14:20:45 +0000115};
116
117class DisplacementAlphaBench : public DisplacementBaseBench {
118public:
robertphillipsbfe11fc2016-04-15 07:17:36 -0700119 DisplacementAlphaBench(bool small) : INHERITED(small) { }
sugoi@google.com580a1722013-04-23 14:20:45 +0000120
121protected:
mtklein36352bf2015-03-25 18:17:31 -0700122 const char* onGetName() override {
sugoi@google.com580a1722013-04-23 14:20:45 +0000123 return isSmall() ? "displacement_alpha_small" : "displacement_alpha_large";
124 }
125
mtkleina1ebeb22015-10-01 09:43:39 -0700126 void onDraw(int loops, SkCanvas* canvas) override {
sugoi@google.com580a1722013-04-23 14:20:45 +0000127 SkPaint paint;
Michael Ludwig23003182019-08-05 11:25:23 -0400128 sk_sp<SkImageFilter> displ(SkImageFilters::Image(fCheckerboard));
sugoi@google.com580a1722013-04-23 14:20:45 +0000129 // Displacement, with 1 alpha component (which isn't pre-multiplied)
Michael Ludwig23003182019-08-05 11:25:23 -0400130 paint.setImageFilter(SkImageFilters::DisplacementMap(SkColorChannel::kB, SkColorChannel::kA,
131 16.0f, std::move(displ), nullptr));
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000132 for (int i = 0; i < loops; i++) {
robertphillipsbfe11fc2016-04-15 07:17:36 -0700133 this->drawClippedBitmap(canvas, 100, 0, paint);
mtklein@google.comc2897432013-09-10 19:23:38 +0000134 }
sugoi@google.com580a1722013-04-23 14:20:45 +0000135 }
136
137private:
John Stiles7571f9e2020-09-02 22:42:33 -0400138 using INHERITED = DisplacementBaseBench;
sugoi@google.com580a1722013-04-23 14:20:45 +0000139};
140
141class DisplacementFullBench : public DisplacementBaseBench {
142public:
robertphillipsbfe11fc2016-04-15 07:17:36 -0700143 DisplacementFullBench(bool small) : INHERITED(small) { }
sugoi@google.com580a1722013-04-23 14:20:45 +0000144
145protected:
mtklein36352bf2015-03-25 18:17:31 -0700146 const char* onGetName() override {
sugoi@google.com580a1722013-04-23 14:20:45 +0000147 return isSmall() ? "displacement_full_small" : "displacement_full_large";
148 }
149
mtkleina1ebeb22015-10-01 09:43:39 -0700150 void onDraw(int loops, SkCanvas* canvas) override {
sugoi@google.com580a1722013-04-23 14:20:45 +0000151 SkPaint paint;
Michael Ludwig23003182019-08-05 11:25:23 -0400152 sk_sp<SkImageFilter> displ(SkImageFilters::Image(fCheckerboard));
sugoi@google.com580a1722013-04-23 14:20:45 +0000153 // Displacement, with 2 non-alpha components
Michael Ludwig23003182019-08-05 11:25:23 -0400154 paint.setImageFilter(SkImageFilters::DisplacementMap(SkColorChannel::kR, SkColorChannel::kB,
155 32.0f, std::move(displ), nullptr));
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000156 for (int i = 0; i < loops; ++i) {
robertphillips@google.com26e30c52013-10-28 18:07:44 +0000157 this->drawClippedBitmap(canvas, 200, 0, paint);
mtklein@google.comc2897432013-09-10 19:23:38 +0000158 }
sugoi@google.com580a1722013-04-23 14:20:45 +0000159 }
160
161private:
John Stiles7571f9e2020-09-02 22:42:33 -0400162 using INHERITED = DisplacementBaseBench;
sugoi@google.com580a1722013-04-23 14:20:45 +0000163};
164
165///////////////////////////////////////////////////////////////////////////////
166
mtklein@google.com410e6e82013-09-13 19:52:27 +0000167DEF_BENCH( return new DisplacementZeroBench(true); )
168DEF_BENCH( return new DisplacementAlphaBench(true); )
169DEF_BENCH( return new DisplacementFullBench(true); )
170DEF_BENCH( return new DisplacementZeroBench(false); )
171DEF_BENCH( return new DisplacementAlphaBench(false); )
172DEF_BENCH( return new DisplacementFullBench(false); )