1/*
2 * Copyright (C) 2015 - Tobias Jakobi
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24#include <stdlib.h>
25#include <stdio.h>
26#include <time.h>
27#include <getopt.h>
28#include <errno.h>
29
30#include <xf86drm.h>
31
32#include "exynos_drm.h"
33#include "exynos_drmif.h"
34#include "exynos_fimg2d.h"
35
36static int output_mathematica = 0;
37
38static int fimg2d_perf_simple(struct exynos_bo *bo, struct g2d_context *ctx,
39			unsigned buf_width, unsigned buf_height, unsigned iterations)
40{
41	struct timespec tspec = { 0 };
42	struct g2d_image img = { 0 };
43
44	unsigned long long g2d_time;
45	unsigned i;
46	int ret = 0;
47
48	img.width = buf_width;
49	img.height = buf_height;
50	img.stride = buf_width * 4;
51	img.color_mode = G2D_COLOR_FMT_ARGB8888 | G2D_ORDER_AXRGB;
52	img.buf_type = G2D_IMGBUF_GEM;
53	img.bo[0] = bo->handle;
54
55	srand(time(NULL));
56
57	printf("starting simple G2D performance test\n");
58	printf("buffer width = %u, buffer height = %u, iterations = %u\n",
59		buf_width, buf_height, iterations);
60
61	if (output_mathematica)
62		putchar('{');
63
64	for (i = 0; i < iterations; ++i) {
65		unsigned x, y, w, h;
66
67		x = rand() % buf_width;
68		y = rand() % buf_height;
69
70		if (x == (buf_width - 1))
71			x -= 1;
72		if (y == (buf_height - 1))
73			y -= 1;
74
75		w = rand() % (buf_width - x);
76		h = rand() % (buf_height - y);
77
78		if (w == 0) w = 1;
79		if (h == 0) h = 1;
80
81		img.color = rand();
82
83		ret = g2d_solid_fill(ctx, &img, x, y, w, h);
84
85		clock_gettime(CLOCK_MONOTONIC, &tspec);
86
87		if (ret == 0)
88			ret = g2d_exec(ctx);
89
90		if (ret != 0) {
91			fprintf(stderr, "error: iteration %u failed (x = %u, y = %u, w = %u, h = %u)\n",
92				i, x, y, w, h);
93			break;
94		} else {
95			struct timespec end = { 0 };
96			clock_gettime(CLOCK_MONOTONIC, &end);
97
98			g2d_time = (end.tv_sec - tspec.tv_sec) * 1000000000ULL;
99			g2d_time += (end.tv_nsec - tspec.tv_nsec);
100
101			if (output_mathematica) {
102				if (i != 0) putchar(',');
103				printf("{%u,%llu}", w * h, g2d_time);
104			} else {
105				printf("num_pixels = %u, usecs = %llu\n", w * h, g2d_time);
106			}
107		}
108	}
109
110	if (output_mathematica)
111		printf("}\n");
112
113	return ret;
114}
115
116static int fimg2d_perf_multi(struct exynos_bo *bo, struct g2d_context *ctx,
117			unsigned buf_width, unsigned buf_height, unsigned iterations, unsigned batch)
118{
119	struct timespec tspec = { 0 };
120	struct g2d_image *images;
121
122	unsigned long long g2d_time;
123	unsigned i, j;
124	int ret = 0;
125
126	images = calloc(batch, sizeof(struct g2d_image));
127	if (images == NULL) {
128		fprintf(stderr, "error: failed to allocate G2D images.\n");
129		return -ENOMEM;
130	}
131
132	for (i = 0; i < batch; ++i) {
133		images[i].width = buf_width;
134		images[i].height = buf_height;
135		images[i].stride = buf_width * 4;
136		images[i].color_mode = G2D_COLOR_FMT_ARGB8888 | G2D_ORDER_AXRGB;
137		images[i].buf_type = G2D_IMGBUF_GEM;
138		images[i].bo[0] = bo->handle;
139	}
140
141	srand(time(NULL));
142
143	printf("starting multi G2D performance test (batch size = %u)\n", batch);
144	printf("buffer width = %u, buffer height = %u, iterations = %u\n",
145		buf_width, buf_height, iterations);
146
147	if (output_mathematica)
148		putchar('{');
149
150	for (i = 0; i < iterations; ++i) {
151		unsigned num_pixels = 0;
152
153		for (j = 0; j < batch; ++j) {
154			unsigned x, y, w, h;
155
156			x = rand() % buf_width;
157			y = rand() % buf_height;
158
159			if (x == (buf_width - 1))
160				x -= 1;
161			if (y == (buf_height - 1))
162				y -= 1;
163
164			w = rand() % (buf_width - x);
165			h = rand() % (buf_height - y);
166
167			if (w == 0) w = 1;
168			if (h == 0) h = 1;
169
170			images[j].color = rand();
171
172			num_pixels += w * h;
173
174			ret = g2d_solid_fill(ctx, &images[j], x, y, w, h);
175			if (ret != 0)
176				break;
177		}
178
179		clock_gettime(CLOCK_MONOTONIC, &tspec);
180
181		if (ret == 0)
182			ret = g2d_exec(ctx);
183
184		if (ret != 0) {
185			fprintf(stderr, "error: iteration %u failed (num_pixels = %u)\n", i, num_pixels);
186			break;
187		} else {
188			struct timespec end = { 0 };
189			clock_gettime(CLOCK_MONOTONIC, &end);
190
191			g2d_time = (end.tv_sec - tspec.tv_sec) * 1000000000ULL;
192			g2d_time += (end.tv_nsec - tspec.tv_nsec);
193
194			if (output_mathematica) {
195				if (i != 0) putchar(',');
196				printf("{%u,%llu}", num_pixels, g2d_time);
197			} else {
198				printf("num_pixels = %u, usecs = %llu\n", num_pixels, g2d_time);
199			}
200		}
201	}
202
203	if (output_mathematica)
204		printf("}\n");
205
206	free(images);
207
208	return ret;
209}
210
211static void usage(const char *name)
212{
213	fprintf(stderr, "usage: %s [-ibwh]\n\n", name);
214
215	fprintf(stderr, "\t-i <number of iterations>\n");
216	fprintf(stderr, "\t-b <size of a batch> (default = 3)\n\n");
217
218	fprintf(stderr, "\t-w <buffer width> (default = 4096)\n");
219	fprintf(stderr, "\t-h <buffer height> (default = 4096)\n\n");
220
221	fprintf(stderr, "\t-M <enable Mathematica styled output>\n");
222
223	exit(0);
224}
225
226int main(int argc, char **argv)
227{
228	int fd, ret, c, parsefail;
229
230	struct exynos_device *dev;
231	struct g2d_context *ctx;
232	struct exynos_bo *bo;
233
234	unsigned int iters = 0, batch = 3;
235	unsigned int bufw = 4096, bufh = 4096;
236
237	ret = 0;
238	parsefail = 0;
239
240	while ((c = getopt(argc, argv, "i:b:w:h:M")) != -1) {
241		switch (c) {
242		case 'i':
243			if (sscanf(optarg, "%u", &iters) != 1)
244				parsefail = 1;
245			break;
246		case 'b':
247			if (sscanf(optarg, "%u", &batch) != 1)
248				parsefail = 1;
249			break;
250		case 'w':
251			if (sscanf(optarg, "%u", &bufw) != 1)
252				parsefail = 1;
253			break;
254		case 'h':
255			if (sscanf(optarg, "%u", &bufh) != 1)
256				parsefail = 1;
257			break;
258		case 'M':
259			output_mathematica = 1;
260			break;
261		default:
262			parsefail = 1;
263			break;
264		}
265	}
266
267	if (parsefail || (argc == 1) || (iters == 0))
268		usage(argv[0]);
269
270	if (bufw < 2 || bufw > 4096 || bufh < 2 || bufh > 4096) {
271		fprintf(stderr, "error: buffer width/height should be in the range 2 to 4096.\n");
272		ret = -1;
273
274		goto out;
275	}
276
277	fd = drmOpen("exynos", NULL);
278	if (fd < 0) {
279		fprintf(stderr, "error: failed to open drm\n");
280		ret = -1;
281
282		goto out;
283	}
284
285	dev = exynos_device_create(fd);
286	if (dev == NULL) {
287		fprintf(stderr, "error: failed to create device\n");
288		ret = -2;
289
290		goto fail;
291	}
292
293	ctx = g2d_init(fd);
294	if (ctx == NULL) {
295		fprintf(stderr, "error: failed to init G2D\n");
296		ret = -3;
297
298		goto g2d_fail;
299	}
300
301	bo = exynos_bo_create(dev, bufw * bufh * 4, 0);
302	if (bo == NULL) {
303		fprintf(stderr, "error: failed to create bo\n");
304		ret = -4;
305
306		goto bo_fail;
307	}
308
309	ret = fimg2d_perf_simple(bo, ctx, bufw, bufh, iters);
310
311	if (ret == 0)
312		ret = fimg2d_perf_multi(bo, ctx, bufw, bufh, iters, batch);
313
314	exynos_bo_destroy(bo);
315
316bo_fail:
317	g2d_fini(ctx);
318
319g2d_fail:
320	exynos_device_destroy(dev);
321
322fail:
323	drmClose(fd);
324
325out:
326	return ret;
327}
328