ref: 60d89ef1d9304427a1289174d9a311ab06e39b44 kmscube/kmscube.c -rw-r--r-- 9.3 KiB View raw
                                                                                
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
 * Copyright (c) 2012 Arvin Schnell <arvin.schnell@gmail.com>
 * Copyright (c) 2012 Rob Clark <rob@ti.com>
 *
 * 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, sub license,
 * 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 NON-INFRINGEMENT. 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.
 */

/* Based on a egl cube test app originally written by Arvin Schnell */

#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <getopt.h>
#include <pthread.h>
#include <wayland-client.h>

#include "common.h"
#include "drm-common.h"
#include "drm-lease-protocol.h"


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

static const char shortopts[] = "ArD:M:l";

static enum mode mode = SMOOTH;
static int atomic = 0;

static const struct option longopts[] = {
	{"atomic", no_argument,       0, 'A'},
	{"device", required_argument, 0, 'D'},
	{"mode",   required_argument, 0, 'M'},
	{"thread", no_argument,       0, 't'},
	{"lease", no_argument,	0, 'l'},
	{0, 0, 0, 0}
};

struct wayland_state {
	struct zwp_drm_lease_manager_v1 *lease_manager;
	struct zwp_drm_lease_connector_v1 *lease_connector;
};

static void usage(const char *name)
{
	printf("Usage: %s [-ADM]\n"
			"\n"
			"options:\n"
			"    -A, --atomic	     use atomic modesetting and fencing\n"
			"    -D, --device=DEVICE      use the given device\n"
			"    -t, --thread	     two threads and cubes on two monitors\n"
			"    -l, --lease	      lease output from the X server\n"
			"    -M, --mode=MODE	  specify mode, one of:\n"
			"	smooth    -  smooth shaded cube (default)\n"
			"	rgba      -  rgba textured cube\n"
			"	nv12-2img -  yuv textured (color conversion in shader)\n"
			"	nv12-1img -  yuv textured (single nv12 texture)\n",
			name);
}

static void dump_lessees(char *head, drmModeLesseeListPtr lessees) {
	uint32_t i;
	printf("%s lessees:", head);
	if (!lessees) {
	    printf("null\n");
	    return;
	}
	for (i = 0; i < lessees->count; i++)
		printf(" %u", lessees->lessees[i]);
	printf("\n");
}

static void dump_objects(char *head, drmModeObjectListPtr objects) {
	uint32_t i;
	printf("%s objects:", head);
	if (!objects) {
	    printf("null\n");
	    return;
	}
	for (i = 0; i < objects->count; i++)
		printf(" %u", objects->objects[i]);
	printf("\n");
}

int run(int fd, int nfd) {
	struct egl *egl;
	struct gbm *gbm;
	struct drm *drm;
	int ret;

	if (atomic)
		drm = init_drm_atomic(fd, nfd);
	else
		drm = init_drm_legacy(fd, nfd);
	if (!drm) {
		printf("failed to initialize %s DRM\n", atomic ? "atomic" : "legacy");
		return -1;
	}

	gbm = init_gbm(drm->fd, drm->mode->hdisplay, drm->mode->vdisplay);
	if (!gbm) {
		printf("failed to initialize GBM\n");
		return -1;
	}

	if (mode == SMOOTH) {
		egl = init_cube_smooth(gbm);
	} else {
		egl = init_cube_tex(gbm, mode);
	}

	if (!egl) {
		printf("failed to initialize EGL\n");
		return -1;
	}

	/* clear the color buffer */
	glClearColor(0.5, 0.5, 0.5, 1.0);
	glClear(GL_COLOR_BUFFER_BIT);

	ret = drm->run(drm, gbm, egl);
	printf ("drm->run returns %d\n", ret);
	return ret;
}

static void *thread_run(void *arg)
{
    int fd = (intptr_t) arg;

    printf("child on fd %d\n", fd);
    printf("child running now...\n");
    run(fd, -1);
    return 0;
}

void lease_handle_lease_fd(void *data,
		struct zwp_drm_lease_v1 *zwp_drm_lease_v1, int32_t leased_fd) {
	int *fd = data;
	*fd = leased_fd;
}

void lease_handle_finished(void *data,
		struct zwp_drm_lease_v1 *zwp_drm_lease_v1) {
	// Who cares
}

static const struct zwp_drm_lease_v1_listener lease_listener = {
	.lease_fd = lease_handle_lease_fd,
	.finished = lease_handle_finished,
};

static void drm_lease_manager_connector(void *data,
		struct zwp_drm_lease_manager_v1 *zwp_drm_lease_manager_v1,
		struct zwp_drm_lease_connector_v1 *conn) {
	struct wayland_state *state = data;
	if (!state->lease_connector) {
		state->lease_connector = conn;
	}
}

static const struct zwp_drm_lease_manager_v1_listener lease_manager_listener = {
	.connector = drm_lease_manager_connector,
};

static void registry_global(void *data, struct wl_registry *wl_registry,
		uint32_t name, const char *interface, uint32_t version) {
	struct wayland_state *state = data;
	if (strcmp(interface, zwp_drm_lease_manager_v1_interface.name) == 0) {
		state->lease_manager = wl_registry_bind(wl_registry, name,
				&zwp_drm_lease_manager_v1_interface, 1);
		zwp_drm_lease_manager_v1_add_listener(state->lease_manager,
				&lease_manager_listener, state);
	}
}

static void registry_global_remove(
		void *data, struct wl_registry *wl_registry, uint32_t name) {
	// who cares
}

static const struct wl_registry_listener registry_listener = {
	.global = registry_global,
	.global_remove = registry_global_remove,
};

int main(int argc, char *argv[])
{
	const char *device = "/dev/dri/card0";
	int do_thread = 0;
	int do_lease = 0;
	int opt;
	int fd;
	int nfd = -1;

	while ((opt = getopt_long_only(argc, argv, shortopts, longopts, NULL)) != -1) {
		switch (opt) {
		case 'A':
			atomic = 1;
			break;
		case 'D':
			device = optarg;
			break;
		case 'M':
			if (strcmp(optarg, "smooth") == 0) {
				mode = SMOOTH;
			} else if (strcmp(optarg, "rgba") == 0) {
				mode = RGBA;
			} else if (strcmp(optarg, "nv12-2img") == 0) {
				mode = NV12_2IMG;
			} else if (strcmp(optarg, "nv12-1img") == 0) {
				mode = NV12_1IMG;
			} else {
				printf("invalid mode: %s\n", optarg);
				usage(argv[0]);
				return -1;
			}
			break;
		case 't':
			do_thread = 1;
			break;
		case 'l':
			do_lease = 1;
			break;
		default:
			usage(argv[0]);
			return -1;
		}
	}

	if (do_lease) {
		struct wayland_state state = { 0 };
		struct wl_display *display = wl_display_connect(NULL);
		if (!display) {
			fprintf(stderr, "Failed to establish Wayland connection\n");
			return -1;
		}
		struct wl_registry *registry = wl_display_get_registry(display);
		wl_registry_add_listener(registry, &registry_listener, &state);
		wl_display_dispatch(display);
		wl_display_roundtrip(display);
		if (!state.lease_manager) {
			fprintf(stderr, "zwp_lease_manager_v1 global not available\n");
			return -1;
		}
		if (!state.lease_connector) {
			fprintf(stderr, "no connectors offered for lease\n");
			return -1;
		}
		struct zwp_drm_lease_request_v1 *req =
			zwp_drm_lease_manager_v1_create_lease_request(state.lease_manager);
		zwp_drm_lease_request_v1_request_connector(req, state.lease_connector);
		struct zwp_drm_lease_v1 *lease = zwp_drm_lease_request_v1_submit(req);
		zwp_drm_lease_v1_add_listener(lease, &lease_listener, &fd);
		wl_display_dispatch(display);
		wl_display_roundtrip(display);
		if (fd < 0) {
			fprintf(stderr, "Failed to acquire lease\n");
			return -1;
		}
	} else {
		fd = open(device, O_RDWR);
		if (fd < 0) {
			printf("could not open drm device\n");
			return -1;
		}
	}
	if (do_thread) {
		struct drm_resources resources;
		uint32_t objects[3];
		int nobjects;
		uint32_t lessee;
		pthread_t thread;

		drmModeLesseeListPtr l_list;
		drmModeObjectListPtr o_list;

		if (find_drm(&resources, fd, -1) != 0) {
			printf("Could not get DRM resources for fork\n");
			return -1;
		}

		printf("connector %d crtc %d plane %d\n",
						resources.connector, resources.crtc, resources.plane);

		nobjects = 0;
		if (resources.connector)
			objects[nobjects++] = resources.connector;
		if (resources.crtc)
			objects[nobjects++] = resources.crtc;
		if (resources.plane)
			objects[nobjects++] = resources.plane;

		printf("objects to lease: %d\n", nobjects);
		for (int i = 0; i < nobjects; ++i) {
			printf("%d\n", objects[i]);
		}

		o_list = drmModeGetLease(fd);

		dump_objects("owner before lease", o_list);
		free(o_list);

		l_list = drmModeListLessees(fd);

		dump_lessees("owner before lease", l_list);

		free(l_list);

		nfd = drmModeCreateLease(fd, objects, nobjects, 0, &lessee);
		if (nfd < 0) {
			fprintf(stderr, "drmModeCreateLease: %s\n", strerror(errno));
		}

		o_list = drmModeGetLease(fd);

		dump_objects("owner after lease", o_list);
		free(o_list);

		l_list = drmModeListLessees(fd);

		dump_lessees("owner after lease", l_list);

		free(l_list);

		o_list = drmModeGetLease(nfd);

		dump_objects("lessee after lease", o_list);
		free(o_list);

		l_list = drmModeListLessees(nfd);

		dump_lessees("lessee after lease", l_list);

		free(l_list);

#if 0
		printf("closing lease\n");
		close(nfd);
		printf("closing owner\n");
		close(fd);
		exit(0);
#endif

		pthread_create(&thread, NULL, thread_run, (void *) (intptr_t) nfd);
		printf("parent running on owner %d\n", fd);
		printf("parent running now...\n");
	}

	return run(fd, nfd);
}