summaryrefslogtreecommitdiff
path: root/test/uitests/benchmarktest.cc
blob: 5566942e8c50d13d002424861d36ddf44dd58317 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/* -*- Mode: c++ -*- */
/***************************************************************************
 *            benchmarktest.cc
 *
 *  Sun Feb 23 14:24:12 CET 2020
 *  Copyright 2020 Bent Bisballe Nyeng
 *  deva@aasimon.org
 ****************************************************************************/

/*
 *  This file is part of DrumGizmo.
 *
 *  DrumGizmo is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  DrumGizmo is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with DrumGizmo; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
#include <chrono>
#include <iostream>
#include <cstdint>
#include <string>

#include <dggui/image.h>
#include <dggui/painter.h>
#include <dggui/canvas.h>

class TimedCanvas
	: public GUI::Canvas
{
public:
	GUI::PixelBufferAlpha& getPixelBuffer() override
	{
		return pixbuf;
	}

	void resize(std::size_t width, std::size_t height)
	{
		pixbuf.realloc(width, height);
	}

private:
	GUI::PixelBufferAlpha pixbuf{800, 600};
};

class TimedScope
{
public:
	using clock_t = std::chrono::steady_clock;

	TimedScope(const std::string& title, std::size_t iterations)
		: title(title)
		, iterations(iterations)
		, begin(clock_t::now())
	{}

	~TimedScope()
	{
		clock_t::time_point end= std::chrono::steady_clock::now();

		auto micro_seconds =
			std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count();
		micro_seconds /= iterations;
		std::cout << title << ": " << micro_seconds << " µs " <<
			"(" << (micro_seconds / 1000) << " ms)" << std::endl;
	}

private:
	std::string title;
	std::size_t iterations;
	clock_t::time_point begin;
};

int main()
{
	TimedCanvas canvas;
	GUI::Painter painter(canvas);
	GUI::Image image_no_alpha(":benchmarktest_resources/image_no_alpha.png");
	GUI::Image image_full_alpha(":benchmarktest_resources/image_full_alpha.png");
	GUI::Image image_edge_alpha(":benchmarktest_resources/image_edge_alpha.png");
	GUI::Image image_inner_alpha(":benchmarktest_resources/image_inner_alpha.png");

	{
		TimedCanvas canvas;
		TimedScope timed("Pixelbuffer resize", 100000);
		painter.setColour(GUI::Colour(1.f, 1.f, 1.f, 1.f));
		for(int i = 0; i < 100000; ++i)
		{
			canvas.resize(i % 1000 + 100, i % 1000 + 100);
		}
	}

	{
		TimedScope timed("Filled rect, no alpha", 100000);
		for(int i = 0; i < 100000; ++i)
		{
			painter.drawRectangle(0, 0, 800, 600);
		}
	}

	{
		TimedScope timed("Filled rect, with alpha", 100000);
		painter.setColour(GUI::Colour(1.f, 1.f, 1.f, 0.5f));
		for(int i = 0; i < 100000; ++i)
		{
			painter.drawRectangle(0, 0, 800, 600);
		}
	}

	{
		TimedScope timed("No scale, no alpha", 100000);
		for(int i = 0; i < 100000; ++i)
		{
			painter.drawImage(0, 0, image_no_alpha);
		}
	}

	{
		TimedScope timed("No scale, full alpha", 1000);
		for(int i = 0; i < 1000; ++i)
		{
			painter.drawImage(0, 0, image_full_alpha);
		}
	}

	{
		TimedScope timed("No scale, edge alpha", 1000);
		for(int i = 0; i < 1000; ++i)
		{
			painter.drawImage(0, 0, image_edge_alpha);
		}
	}

	{
		TimedScope timed("No scale, inner alpha", 1000);
		for(int i = 0; i < 1000; ++i)
		{
			painter.drawImage(0, 0, image_inner_alpha);
		}
	}

	{
		GUI::PixelBuffer wpixbuf(800, 600);
		std::vector<GUI::PixelBufferAlpha*> children;
		for(int i = 0; i < 100; ++i)
		{
			auto child = new GUI::PixelBufferAlpha(300, 300);
			child->x = i * 2;
			child->y = i * 2;
			children.push_back(child);
		}

		TimedScope timed("Buffer flattening", 100);
		for(int i = 0; i < 100; ++i)
		{
			for(auto child : children)
			{
				child->dirty = true;
			}

			wpixbuf.updateBuffer(children);
		}

		for(auto child : children)
		{
			delete child;
		}
	}

	{
		TimedScope timed("Scaled 1:1 no alpha", 1000);
		for(int i = 0; i < 1000; ++i)
		{
			painter.drawImageStretched(0, 0, image_no_alpha, 370, 330);
		}
	}

	{
		TimedScope timed("Scaled 1:1 full alpha", 1000);
		for(int i = 0; i < 1000; ++i)
		{
			painter.drawImageStretched(0, 0, image_full_alpha, 370, 330);
		}
	}

	{
		TimedScope timed("Scaled 1:1 edge alpha", 1000);
		for(int i = 0; i < 1000; ++i)
		{
			painter.drawImageStretched(0, 0, image_edge_alpha, 370, 330);
		}
	}

	{
		TimedScope timed("Scaled 1:1 inner alpha", 1000);
		for(int i = 0; i < 1000; ++i)
		{
			painter.drawImageStretched(0, 0, image_inner_alpha, 370, 330);
		}
	}

	{
		TimedScope timed("Scaled to window, no alpha", 1000);
		for(int i = 0; i < 1000; ++i)
		{
			painter.drawImageStretched(0, 0, image_no_alpha, 800, 600);
		}
	}

	{
		TimedScope timed("Scaled to window, full alpha", 1000);
		for(int i = 0; i < 1000; ++i)
		{
			painter.drawImageStretched(0, 0, image_full_alpha, 800, 600);
		}
	}

	{
		TimedScope timed("Scaled to window, edge alpha", 1000);
		for(int i = 0; i < 1000; ++i)
		{
			painter.drawImageStretched(0, 0, image_edge_alpha, 800, 600);
		}
	}

	{
		TimedScope timed("Scaled to window, inner alpha", 1000);
		for(int i = 0; i < 1000; ++i)
		{
			painter.drawImageStretched(0, 0, image_inner_alpha, 800, 600);
		}
	}

	return 0;
}