summaryrefslogtreecommitdiff
path: root/test/setup.c
blob: 55d16885a840f1768271878f464f9e22a6b00820 (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
#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 <xf86drmMode.h>
#include <sys/ioctl.h>
#include <i915_drm.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 void run(struct state *state, render_func_t render)
{
	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");

	render(state);
	eglTerminate(state->display);
}

static const EGLint config_attribs[] = {
	EGL_DEPTH_SIZE, 24,
	EGL_CONFIG_CAVEAT, EGL_NONE,
	EGL_NONE		
};

static void run_dri2(int x, int y, int width, int height, 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");

	run(&state, render);
}

static uint32_t
create_frontbuffer(EGLDisplay display, int *width, int *height, int *stride)
{
	drmModeConnector *connector;
	drmModeRes *resources;
	drmModeEncoder *encoder;
	drmModeModeInfo *mode;
	struct drm_i915_gem_create create;
	struct drm_gem_flink flink;
	unsigned int fb_id;
	int i, ret, fd;

	fd = eglGetDisplayFD(display);

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

	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 -1;
	}

	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);
	}

	/* Mode size at 32 bpp */
	create.size = mode->hdisplay * mode->vdisplay * 4;
	if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) {
		fprintf(stderr, "gem create failed: %m\n");
		return 0;
	}

	ret = drmModeAddFB(fd, mode->hdisplay, mode->vdisplay,
			   32, 32, mode->hdisplay * 4, create.handle, &fb_id);
	if (ret) {
		fprintf(stderr, "failed to add fb: %m\n");
		return 0;
	}

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

	flink.handle = create.handle;
	if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) {
		fprintf(stderr, "gem flink failed: %m\n");
		return 0;
	}

	*width = mode->hdisplay;
	*height = mode->vdisplay;
	*stride = mode->hdisplay * 4;

	return flink.name;
}

static void run_native(int x, int y, int width, int height, render_func_t render)
{
	EGLint major, minor;
	uint32_t name;
	int fb_width, fb_height, fb_stride;
	struct udev *udev;
	struct udev_device *device;
	struct state state;
	const static EGLint attribs[] =
		{ EGL_RENDER_BUFFER, EGL_BACK_BUFFER, EGL_NONE };

	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");

	name = create_frontbuffer(state.display, &fb_width, &fb_height, &fb_stride);
	state.surface = eglCreateSurfaceForName(state.display, state.config,
						name, width, height,
						fb_stride, attribs);
	if (state.surface == NULL)
		die("failed to create surface\n");

	run(&state, render);
}

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

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

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

	return 0;
}