summaryrefslogtreecommitdiff
path: root/test/setup.c
blob: 35070b5c1191b98affaf5b799885ad536623c925 (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
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <GL/gl.h>
#include "../eagle.h"
#include <sys/time.h>
#include <drm_mode.h>
#include <xf86drm.h>
#include <xf86drmMode.h>
#include <sys/ioctl.h>
#include <i915_drm.h>
#include <string.h>

#include "setup.h"

#define ARRAY_SIZE(a) (sizeof (a) / sizeof (a)[0])

void die(const char *msg)
{
	fprintf(stderr, "%s", msg);
	exit(EXIT_FAILURE);
}

static const EGLint config_attribs[] = {
	EGL_DEPTH_SIZE, 24,
	EGL_RED_SIZE, 8,
	EGL_GREEN_SIZE, 8,
	EGL_BLUE_SIZE, 8,
	EGL_BUFFER_SIZE, 32,
	EGL_CONFIG_CAVEAT, EGL_NONE,
	EGL_NONE		
};

static void run_dri2(int x, int y, int width, int height, init_func_t init, render_func_t render)
{
	EGLint major, minor;
	Display *x11_display;
	Window root, window;
	XSetWindowAttributes attributes;
	XSizeHints sizehints;
	int screen;
	unsigned long mask;
	struct state state;
	static const char name[] = "eglgears";

	x11_display = XOpenDisplay(NULL);
	if (x11_display == NULL)
		return;

	screen = DefaultScreen(x11_display);
	root = RootWindow(x11_display, screen);
	state.width = width;
	state.height = height;
	state.display = eglCreateDisplayX11(x11_display, root);
	if (state.display == NULL)
		die("failed to create display\n");

	attributes.event_mask =
		StructureNotifyMask | ExposureMask | KeyPressMask;
	mask = CWEventMask;
	window = XCreateWindow(x11_display, root, x, y, width, height,
			       0, DefaultDepth(x11_display, screen),
			       InputOutput,
			       DefaultVisual(x11_display, screen),
			       mask, &attributes);

	sizehints.x = x;
	sizehints.y = y;
	sizehints.width  = width;
	sizehints.height = height;
	sizehints.flags = USSize | USPosition;
	XSetNormalHints(x11_display, window, &sizehints);
	XSetStandardProperties(x11_display, window, name, name,
			       None, (char **)NULL, 0, &sizehints);

	XMapWindow(x11_display, window);

	if (!eglInitialize(state.display, &major, &minor))
		die("failed to initialize display\n");

	if (!eglChooseConfig(state.display, config_attribs, &state.config, 1, NULL))
		die("failed to pick a config\n");

	state.surface = eglCreateSurfaceX11(state.display, state.config, window, width, height);
	if (state.surface == NULL)
		die("failed to create surface\n");

	state.context = eglCreateContext(state.display, state.config, NULL, NULL);
	if (state.context == NULL)
		die("failed to create context\n");

	if (!eglMakeCurrent(state.display, state.surface, state.surface, state.context))
		die("failed to make context current\n");

	init(&state);

	render(&state);

	eglTerminate(state.display);
}

static void
page_flip_handler(int fd, unsigned int frame,
		  unsigned int sec, unsigned int usec, void *data)
{
	int *done = data;

	*done = 1;
}

static void run_native(int x, int y, int width, int height, init_func_t init, render_func_t render)
{
	EGLint major, minor;
	struct udev *udev;
	struct udev_device *device;
	struct state state;
	drmModeConnector *connector;
	drmModeRes *resources;
	drmModeEncoder *encoder;
	drmModeModeInfo *mode;
	int i, ret, fd, done;
	drmEventContext evctx;
	struct buffer {
		unsigned int fb_id;
		uint32_t name, handle, stride;
	} b[2];

	udev = udev_new();
	device = udev_device_new_from_syspath(udev, "/sys/class/drm/card0");
	state.width = width;
	state.height = height;
	state.display = eglCreateDisplayNative(device);
	if (state.display == NULL)
		die("failed to create display\n");

	if (!eglInitialize(state.display, &major, &minor))
		die("failed to initialize display\n");

	if (!eglChooseConfig(state.display, config_attribs, &state.config, 1, NULL))
		die("failed to pick a config\n");

	fd = eglGetDisplayFD(state.display);

	resources = drmModeGetResources(fd);
	if (!resources) {
		fprintf(stderr, "drmModeGetResources failed\n");
		return;
	}

	for (i = 0; i < resources->count_connectors; i++) {
		connector = drmModeGetConnector(fd, resources->connectors[i]);
		if (connector == NULL)
			continue;

		if (connector->connection == DRM_MODE_CONNECTED &&
		    connector->count_modes > 0)
			break;

		drmModeFreeConnector(connector);
	}

	if (i == resources->count_connectors) {
		fprintf(stderr, "No currently active connector found.\n");
		return;
	}

	mode = &connector->modes[0];

	for (i = 0; i < resources->count_encoders; i++) {
		encoder = drmModeGetEncoder(fd, resources->encoders[i]);

		if (encoder == NULL)
			continue;

		if (encoder->encoder_id == connector->encoder_id)
			break;

		drmModeFreeEncoder(encoder);
	}

	state.surface = eglCreateSurface(state.display, state.config,
					 mode->hdisplay, mode->vdisplay, 2, NULL);
	for (i = 0; i < 2; i++) {
		eglGetColorBuffer(state.surface,
				  i, &b[i].name, &b[i].handle, &b[i].stride);

		ret = drmModeAddFB(fd, mode->hdisplay, mode->vdisplay,
				   32, 32, b[i].stride, b[i].handle, &b[i].fb_id);
		if (ret) {
			fprintf(stderr, "failed to add fb %d: %m\n", i);
			return;
		}
	}

	ret = drmModeSetCrtc(fd, encoder->crtc_id, b[1].fb_id, 0, 0,
			     &connector->connector_id, 1, mode);
	if (ret) {
		fprintf(stderr, "failed to set mode: %m\n");
		return;
	}

	state.context = eglCreateContext(state.display, state.config, NULL, NULL);
	if (state.context == NULL)
		die("failed to create context\n");

	if (!eglMakeCurrent(state.display, state.surface, state.surface, state.context))
		die("failed to make context current\n");

	init(&state);

	memset(&evctx, 0, sizeof evctx);
	evctx.version = DRM_EVENT_CONTEXT_VERSION;
	evctx.page_flip_handler = page_flip_handler;
	i = 0;
	while (1) {
		render(&state);
		eglBindColorBuffer(state.display, state.surface, i ^ 1);
		drmModePageFlip(fd, encoder->crtc_id, b[i].fb_id, &done);
		i ^= 1;
		done = 0;
		while (!done)
			drmHandleEvent(fd, &evctx);
	}		

	eglTerminate(state.display);
}

int setup(int argc, char *argv[], init_func_t init, render_func_t render)
{
	const int x = 100, y = 100, width = 300, height = 300;

	run_dri2(x, y, width, height, init, render);

	fprintf(stderr, "open x11 display failed, trying drmfb\n");
	run_native(x, y, width, height, init, render);

	return 0;
}