ref: 7f065bcb2106e44cacdb01f2648365840e67f5bb kmscube/kmscube.c -rw-r--r-- 12.1 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
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
 * 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 <xcb/xcb.h>
#include <xcb/randr.h>

#include "common.h"
#include "drm-common.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}
};

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

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) {
                xcb_connection_t        *connection;
                int                     screen;

                connection = xcb_connect(NULL, &screen);
                if (!connection) {
                        printf("Connection to X server failed\n");
                        exit(1);
                }
                xcb_randr_query_version_cookie_t rqv_c = xcb_randr_query_version(connection,
                                                                                 XCB_RANDR_MAJOR_VERSION,
                                                                                 XCB_RANDR_MINOR_VERSION);
                xcb_randr_query_version_reply_t *rqv_r = xcb_randr_query_version_reply(connection, rqv_c, NULL);

                if (!rqv_r || rqv_r->minor_version < 6) {
                        printf("No new-enough RandR version\n");
                        exit(1);
                }

                xcb_screen_iterator_t s_i;

                int i_s = 0;

                for (s_i = xcb_setup_roots_iterator(xcb_get_setup(connection));
                     s_i.rem;
                     xcb_screen_next(&s_i), i_s++) {
                        printf ("index %d screen %d\n", s_i.index, screen);
                        if (i_s == screen)
                                break;
                }

                xcb_window_t root = s_i.data->root;

                printf("root %x\n", root);

                xcb_randr_get_screen_resources_cookie_t gsr_c = xcb_randr_get_screen_resources(connection, root);

                xcb_randr_get_screen_resources_reply_t *gsr_r = xcb_randr_get_screen_resources_reply(connection, gsr_c, NULL);

                if (!gsr_r) {
                        printf("get_screen_resources failed\n");
                        exit(1);
                }

                xcb_randr_output_t *ro = xcb_randr_get_screen_resources_outputs(gsr_r);
                int o, c;

                xcb_randr_output_t output = 0;

                /* Find a connected but idle output */
                for (o = 0; output == 0 && o < gsr_r->num_outputs; o++) {
                        xcb_randr_get_output_info_cookie_t goi_c = xcb_randr_get_output_info(connection, ro[o], gsr_r->config_timestamp);

                        xcb_randr_get_output_info_reply_t *goi_r = xcb_randr_get_output_info_reply(connection, goi_c, NULL);

                        /* Find the first connected but unused output */
                        if (goi_r->connection == XCB_RANDR_CONNECTION_CONNECTED &&
                            goi_r->crtc == 0) {
                                output = ro[o];
                        }

                        free(goi_r);
                }

                xcb_randr_crtc_t *rc = xcb_randr_get_screen_resources_crtcs(gsr_r);

                xcb_randr_crtc_t crtc = 0;

                /* Find an idle crtc */
                for (c = 0; crtc == 0 && c < gsr_r->num_crtcs; c++) {
                        xcb_randr_get_crtc_info_cookie_t gci_c = xcb_randr_get_crtc_info(connection, rc[c], gsr_r->config_timestamp);

                        xcb_randr_get_crtc_info_reply_t *gci_r = xcb_randr_get_crtc_info_reply(connection, gci_c, NULL);

                        /* Find the first connected but unused crtc */
                        if (gci_r->mode == 0)
                                crtc = rc[c];

                        free(gci_r);
                }

                free(gsr_r);

                printf("output %x crtc %x\n", output, crtc);

                xcb_randr_lease_t lease = xcb_generate_id(connection);

                xcb_randr_create_lease_cookie_t rcl_c = xcb_randr_create_lease(connection,
                                                                               root,
                                                                               lease,
                                                                               1,
                                                                               1,
                                                                               &crtc,
                                                                               &output);
                xcb_randr_create_lease_reply_t *rcl_r = xcb_randr_create_lease_reply(connection, rcl_c, NULL);

                if (!rcl_r) {
                        printf("create_lease failed\n");
                        exit(1);
                }

                int *rcl_f = xcb_randr_create_lease_reply_fds(connection, rcl_r);

                fd = rcl_f[0];

                printf("fd %d\n", fd);

        } 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 encoder %d crtc %d\n", resources.connector, resources.encoder, resources.crtc);

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

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

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