aboutsummaryrefslogtreecommitdiffstats
blob: 1699bba7bb1c70902d8a5e725b52b652530e4bcf (plain) (blame)
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
 * Copyright (C) 2015 - Tobias Jakobi
 *
 * This is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation, either version 2 of the License,
 * or (at your option) any later version.
 *
 * It 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 General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with it. If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <getopt.h>
#include <errno.h>

#include <xf86drm.h>

#include "exynos_drm.h"
#include "exynos_drmif.h"
#include "exynos_fimg2d.h"

static int output_mathematica = 0;

static int fimg2d_perf_simple(struct exynos_bo *bo, struct g2d_context *ctx,
			unsigned buf_width, unsigned buf_height, unsigned iterations)
{
	struct timespec tspec = { 0 };
	struct g2d_image img = { 0 };

	unsigned long long g2d_time;
	unsigned i;
	int ret = 0;

	img.width = buf_width;
	img.height = buf_height;
	img.stride = buf_width * 4;
	img.color_mode = G2D_COLOR_FMT_ARGB8888 | G2D_ORDER_AXRGB;
	img.buf_type = G2D_IMGBUF_GEM;
	img.bo[0] = bo->handle;

	srand(time(NULL));

	printf("starting simple G2D performance test\n");
	printf("buffer width = %u, buffer height = %u, iterations = %u\n",
		buf_width, buf_height, iterations);

	if (output_mathematica)
		putchar('{');

	for (i = 0; i < iterations; ++i) {
		unsigned x, y, w, h;

		x = rand() % buf_width;
		y = rand() % buf_height;

		if (x == (buf_width - 1))
			x -= 1;
		if (y == (buf_height - 1))
			y -= 1;

		w = rand() % (buf_width - x);
		h = rand() % (buf_height - y);

		if (w == 0) w = 1;
		if (h == 0) h = 1;

		img.color = rand();

		ret = g2d_solid_fill(ctx, &img, x, y, w, h);

		clock_gettime(CLOCK_MONOTONIC, &tspec);

		if (ret == 0)
			ret = g2d_exec(ctx);

		if (ret != 0) {
			fprintf(stderr, "error: iteration %u failed (x = %u, y = %u, w = %u, h = %u)\n",
				i, x, y, w, h);
			break;
		} else {
			struct timespec end = { 0 };
			clock_gettime(CLOCK_MONOTONIC, &end);

			g2d_time = (end.tv_sec - tspec.tv_sec) * 1000000000ULL;
			g2d_time += (end.tv_nsec - tspec.tv_nsec);

			if (output_mathematica) {
				if (i != 0) putchar(',');
				printf("{%u,%llu}", w * h, g2d_time);
			} else {
				printf("num_pixels = %u, usecs = %llu\n", w * h, g2d_time);
			}
		}
	}

	if (output_mathematica)
		printf("}\n");

	return ret;
}

static int fimg2d_perf_multi(struct exynos_bo *bo, struct g2d_context *ctx,
			unsigned buf_width, unsigned buf_height, unsigned iterations, unsigned batch)
{
	struct timespec tspec = { 0 };
	struct g2d_image *images;

	unsigned long long g2d_time;
	unsigned i, j;
	int ret = 0;

	images = calloc(batch, sizeof(struct g2d_image));
	if (images == NULL) {
		fprintf(stderr, "error: failed to allocate G2D images.\n");
		return -ENOMEM;
	}

	for (i = 0; i < batch; ++i) {
		images[i].width = buf_width;
		images[i].height = buf_height;
		images[i].stride = buf_width * 4;
		images[i].color_mode = G2D_COLOR_FMT_ARGB8888 | G2D_ORDER_AXRGB;
		images[i].buf_type = G2D_IMGBUF_GEM;
		images[i].bo[0] = bo->handle;
	}

	srand(time(NULL));

	printf("starting multi G2D performance test (batch size = %u)\n", batch);
	printf("buffer width = %u, buffer height = %u, iterations = %u\n",
		buf_width, buf_height, iterations);

	if (output_mathematica)
		putchar('{');

	for (i = 0; i < iterations; ++i) {
		unsigned num_pixels = 0;

		for (j = 0; j < batch; ++j) {
			unsigned x, y, w, h;

			x = rand() % buf_width;
			y = rand() % buf_height;

			if (x == (buf_width - 1))
				x -= 1;
			if (y == (buf_height - 1))
				y -= 1;

			w = rand() % (buf_width - x);
			h = rand() % (buf_height - y);

			if (w == 0) w = 1;
			if (h == 0) h = 1;

			images[j].color = rand();

			num_pixels += w * h;

			ret = g2d_solid_fill(ctx, &images[j], x, y, w, h);
			if (ret != 0)
				break;
		}

		clock_gettime(CLOCK_MONOTONIC, &tspec);

		if (ret == 0)
			ret = g2d_exec(ctx);

		if (ret != 0) {
			fprintf(stderr, "error: iteration %u failed (num_pixels = %u)\n", i, num_pixels);
			break;
		} else {
			struct timespec end = { 0 };
			clock_gettime(CLOCK_MONOTONIC, &end);

			g2d_time = (end.tv_sec - tspec.tv_sec) * 1000000000ULL;
			g2d_time += (end.tv_nsec - tspec.tv_nsec);

			if (output_mathematica) {
				if (i != 0) putchar(',');
				printf("{%u,%llu}", num_pixels, g2d_time);
			} else {
				printf("num_pixels = %u, usecs = %llu\n", num_pixels, g2d_time);
			}
		}
	}

	if (output_mathematica)
		printf("}\n");

	free(images);

	return ret;
}

static void usage(const char *name)
{
	fprintf(stderr, "usage: %s [-ibwh]\n\n", name);

	fprintf(stderr, "\t-i <number of iterations>\n");
	fprintf(stderr, "\t-b <size of a batch> (default = 3)\n\n");

	fprintf(stderr, "\t-w <buffer width> (default = 4096)\n");
	fprintf(stderr, "\t-h <buffer height> (default = 4096)\n\n");

	fprintf(stderr, "\t-M <enable Mathematica styled output>\n");

	exit(0);
}

int main(int argc, char **argv)
{
	int fd, ret, c, parsefail;

	struct exynos_device *dev;
	struct g2d_context *ctx;
	struct exynos_bo *bo;

	unsigned int iters = 0, batch = 3;
	unsigned int bufw = 4096, bufh = 4096;

	ret = 0;
	parsefail = 0;

	while ((c = getopt(argc, argv, "i:b:w:h:M")) != -1) {
		switch (c) {
		case 'i':
			if (sscanf(optarg, "%u", &iters) != 1)
				parsefail = 1;
			break;
		case 'b':
			if (sscanf(optarg, "%u", &batch) != 1)
				parsefail = 1;
			break;
		case 'w':
			if (sscanf(optarg, "%u", &bufw) != 1)
				parsefail = 1;
			break;
		case 'h':
			if (sscanf(optarg, "%u", &bufh) != 1)
				parsefail = 1;
			break;
		case 'M':
			output_mathematica = 1;
			break;
		default:
			parsefail = 1;
			break;
		}
	}

	if (parsefail || (argc == 1) || (iters == 0))
		usage(argv[0]);

	if (bufw < 2 || bufw > 4096 || bufh < 2 || bufh > 4096) {
		fprintf(stderr, "error: buffer width/height should be in the range 2 to 4096.\n");
		ret = -1;

		goto out;
	}

	if (bufw == 0 || bufh == 0) {
		fprintf(stderr, "error: buffer width/height should be non-zero.\n");
		ret = -1;

		goto out;
	}

	fd = drmOpen("exynos", NULL);
	if (fd < 0) {
		fprintf(stderr, "error: failed to open drm\n");
		ret = -1;

		goto out;
	}

	dev = exynos_device_create(fd);
	if (dev == NULL) {
		fprintf(stderr, "error: failed to create device\n");
		ret = -2;

		goto fail;
	}

	ctx = g2d_init(fd);
	if (ctx == NULL) {
		fprintf(stderr, "error: failed to init G2D\n");
		ret = -3;

		goto g2d_fail;
	}

	bo = exynos_bo_create(dev, bufw * bufh * 4, 0);
	if (bo == NULL) {
		fprintf(stderr, "error: failed to create bo\n");
		ret = -4;

		goto bo_fail;
	}

	ret = fimg2d_perf_simple(bo, ctx, bufw, bufh, iters);

	if (ret == 0)
		ret = fimg2d_perf_multi(bo, ctx, bufw, bufh, iters, batch);

	exynos_bo_destroy(bo);

bo_fail:
	g2d_fini(ctx);

g2d_fail:
	exynos_device_destroy(dev);

fail:
	drmClose(fd);

out:
	return ret;
}