1/*
2 * Copyright © 2007 Intel Corporation
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 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 *    Eric Anholt <eric@anholt.net>
25 *
26 */
27
28#include "drmtest.h"
29
30static void
31set_draw_cliprects_empty(int fd, int drawable)
32{
33	int ret;
34	struct drm_update_draw update;
35
36	update.handle = drawable;
37	update.type = DRM_DRAWABLE_CLIPRECTS;
38	update.num = 0;
39	update.data = 0;
40
41	ret = ioctl(fd, DRM_IOCTL_UPDATE_DRAW, &update);
42	assert(ret == 0);
43}
44
45static void
46set_draw_cliprects_empty_fail(int fd, int drawable)
47{
48	int ret;
49	struct drm_update_draw update;
50
51	update.handle = drawable;
52	update.type = DRM_DRAWABLE_CLIPRECTS;
53	update.num = 0;
54	update.data = 0;
55
56	ret = ioctl(fd, DRM_IOCTL_UPDATE_DRAW, &update);
57	assert(ret == -1 && errno == EINVAL);
58}
59
60static void
61set_draw_cliprects_2(int fd, int drawable)
62{
63	int ret;
64	struct drm_update_draw update;
65	drm_clip_rect_t rects[2];
66
67	rects[0].x1 = 0;
68	rects[0].y1 = 0;
69	rects[0].x2 = 10;
70	rects[0].y2 = 10;
71
72	rects[1].x1 = 10;
73	rects[1].y1 = 10;
74	rects[1].x2 = 20;
75	rects[1].y2 = 20;
76
77	update.handle = drawable;
78	update.type = DRM_DRAWABLE_CLIPRECTS;
79	update.num = 2;
80	update.data = (unsigned long long)(uintptr_t)&rects;
81
82	ret = ioctl(fd, DRM_IOCTL_UPDATE_DRAW, &update);
83	assert(ret == 0);
84}
85
86static int add_drawable(int fd)
87{
88	drm_draw_t drawarg;
89	int ret;
90
91	/* Create a drawable.
92	 * IOCTL_ADD_DRAW is RDWR, though it should really just be RD
93	 */
94	drawarg.handle = 0;
95	ret = ioctl(fd, DRM_IOCTL_ADD_DRAW, &drawarg);
96	assert(ret == 0);
97	return drawarg.handle;
98}
99
100static int rm_drawable(int fd, int drawable, int fail)
101{
102	drm_draw_t drawarg;
103	int ret;
104
105	/* Create a drawable.
106	 * IOCTL_ADD_DRAW is RDWR, though it should really just be RD
107	 */
108	drawarg.handle = drawable;
109	ret = ioctl(fd, DRM_IOCTL_RM_DRAW, &drawarg);
110	if (!fail)
111		assert(ret == 0);
112	else
113		assert(ret == -1 && errno == EINVAL);
114
115	return drawarg.handle;
116}
117
118/**
119 * Tests drawable management: adding, removing, and updating the cliprects of
120 * drawables.
121 */
122int main(int argc, char **argv)
123{
124	int fd, ret, d1, d2;
125
126	if (getuid() != 0) {
127		fprintf(stderr, "updatedraw test requires root, skipping\n");
128		return 0;
129	}
130
131	fd = drm_open_any_master();
132
133	d1 = add_drawable(fd);
134	d2 = add_drawable(fd);
135	/* Do a series of cliprect updates */
136	set_draw_cliprects_empty(fd, d1);
137	set_draw_cliprects_empty(fd, d2);
138	set_draw_cliprects_2(fd, d1);
139	set_draw_cliprects_empty(fd, d1);
140
141	/* Remove our drawables */
142	rm_drawable(fd, d1, 0);
143	rm_drawable(fd, d2, 0);
144
145	/* Check that removing an unknown drawable returns error */
146	rm_drawable(fd, 0x7fffffff, 1);
147
148	/* Attempt to set cliprects on a nonexistent drawable */
149	set_draw_cliprects_empty_fail(fd, d1);
150
151	close(fd);
152	return 0;
153}
154