aboutsummaryrefslogtreecommitdiff
path: root/tests/gbm/gbm-multi-plane.c
blob: 1debd60b69d794f2a2052392dcc1dbbbab4783ec (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
/* Copyright (c) 2021 Collabora Ltd
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

/**
 * \file
 * \brief Tests for libgbm.
 */

#include <drm_fourcc.h>
#include <errno.h>
#include <fcntl.h>
#include <gbm.h>
#include <sys/stat.h>
#include <sys/types.h>

#include "piglit-util.h"

#define NUM_PLANES 3
#define WIDTH 512
#define HEIGHT 512
#define FORMAT GBM_FORMAT_R8

static bool
gem_handles_match(struct gbm_device *gbm, int fd, int old_handle)
{
	bool match;
	struct gbm_import_fd_data import = {
		.width = WIDTH,
		.height = HEIGHT,
		.format = FORMAT,
		.fd = fd,
	};
	struct gbm_bo *bo = gbm_bo_import(gbm, GBM_BO_IMPORT_FD, &import, 0);
	if (!bo)
		piglit_report_result(PIGLIT_FAIL);

	match = gbm_bo_get_handle(bo).u32 == old_handle;
	gbm_bo_destroy(bo);

	return match;
}

int
main(int argc, char **argv)
{
	int drm_fd;
	char *nodename;
	struct gbm_device *gbm;
	struct gbm_bo *bos[NUM_PLANES];
	struct gbm_bo *multi_plane_bo;
	struct gbm_import_fd_modifier_data import_mod = {
		.width = WIDTH,
		.height = HEIGHT,
		.format = GBM_FORMAT_YUV420,
		.num_fds = NUM_PLANES,
		.modifier = DRM_FORMAT_MOD_LINEAR,
	};

	/* Strip common piglit args. */
	piglit_strip_arg(&argc, argv, "-fbo");
	piglit_strip_arg(&argc, argv, "-auto");

	nodename = getenv("WAFFLE_GBM_DEVICE");
	if (!nodename)
		nodename = "/dev/dri/renderD128";
	drm_fd = open(nodename, O_RDWR);
	if (drm_fd == -1) {
		perror("Error opening render node");
		piglit_report_result(PIGLIT_FAIL);
	}

	gbm = gbm_create_device(drm_fd);
	if (!gbm)
		piglit_report_result(PIGLIT_FAIL);

	for (int i = 0; i < NUM_PLANES; i++) {
		bos[i] = gbm_bo_create(gbm, WIDTH, HEIGHT, FORMAT,
			GBM_BO_USE_RENDERING | GBM_BO_USE_LINEAR);
		if (!bos[i])
			piglit_report_result(PIGLIT_FAIL);

		import_mod.fds[i] = gbm_bo_get_fd(bos[i]);
		import_mod.strides[i] = gbm_bo_get_stride(bos[i]);
		import_mod.offsets[i] = gbm_bo_get_offset(bos[i], 0);
	}

	multi_plane_bo = gbm_bo_import(gbm, GBM_BO_IMPORT_FD_MODIFIER,
		&import_mod, 0);
	if (!multi_plane_bo)
		piglit_report_result(PIGLIT_FAIL);

	for (int i = 0; i < NUM_PLANES; i++) {
		int fd = gbm_bo_get_fd_for_plane(multi_plane_bo, i);
		if (fd < 0)
			piglit_report_result(PIGLIT_FAIL);

		if (fcntl(fd, F_GETFL) == -1 && errno == EBADF)
			piglit_report_result(PIGLIT_FAIL);

		if (!gem_handles_match(gbm, fd, gbm_bo_get_handle(bos[i]).u32))
			piglit_report_result(PIGLIT_FAIL);

		if (import_mod.strides[i] != gbm_bo_get_stride_for_plane(multi_plane_bo, i))
			piglit_report_result(PIGLIT_FAIL);

		if (import_mod.offsets[i] != gbm_bo_get_offset(multi_plane_bo, i))
			piglit_report_result(PIGLIT_FAIL);
	}

	piglit_report_result(PIGLIT_PASS);
}