summarylogtreecommitdiffstats
path: root/0005-compositor-drm-Add-support-for-EGLDevice-EGLOutput.patch
blob: 7c1fe10bd5b5fc7bf7dee85b237d32bfb524d1cc (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
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
From b1db106d3c91c1b35a01573a455c5661a17a06ee Mon Sep 17 00:00:00 2001
From: "Miguel A. Vico" <mvicomoya@nvidia.com>
Date: Thu, 28 Jan 2016 19:37:10 +0100
Subject: [PATCH 5/7] compositor-drm: Add support for EGLDevice+EGLOutput
X-NVConfidentiality: public

As previously stated, EGLDevice and EGLOutput will provide means
to access native device objects and different portions of display
control hardware respectively.

Whenever EGL_EXT_device_drm extension is present, EGLDevice can
be used to enumerate and access DRM KMS devices, and EGLOutputLayer
to enumerate and access DRM KMS crtcs and planes.

By using EGLStreams and attaching an EGLOutputLayer consumer
(representing a DRM KMS crtc or plane) to it, compositor-drm can
produce final composition frames and present them on a DRM device.

This change adds required logic to support presentation through
EGLDevice+EGLOutput+EGLStream. Whether GBM or EGLDevice should be
used can be controlled by --use-egldevice backend argument.

Signed-off-by: Miguel A Vico Moya <mvicomoya@nvidia.com>
Reviewed-by: Andy Ritger <aritger@nvidia.com>
Reviewed-by: Adam Cheney <acheney@nvidia.com>
Reviewed-by: James Jones <jajones@nvidia.com>
---
 compositor/main.c          |   2 +
 libweston/compositor-drm.c | 303 ++++++++++++++++++++++++++++++++-------------
 libweston/compositor-drm.h |   4 +
 libweston/gl-renderer.h    |   1 +
 shared/weston-egl-ext.h    |   9 ++
 5 files changed, 230 insertions(+), 89 deletions(-)

diff --git a/compositor/main.c b/compositor/main.c
index 72c3cd10..ad348d82 100644
--- a/compositor/main.c
+++ b/compositor/main.c
@@ -566,6 +566,7 @@ usage(int error_code)
 		"  --seat=SEAT\t\tThe seat that weston should run on\n"
 		"  --tty=TTY\t\tThe tty to use\n"
 		"  --use-pixman\t\tUse the pixman (CPU) renderer\n"
+		"  --use-egldevice\tUse EGLDevice and EGLOutput with the GL renderer\n"
 		"  --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
 #endif
 
@@ -1227,6 +1228,7 @@ load_drm_backend(struct weston_compositor *c,
 		{ WESTON_OPTION_INTEGER, "tty", 0, &config.tty },
 		{ WESTON_OPTION_BOOLEAN, "current-mode", 0, &wet->drm_use_current_mode },
 		{ WESTON_OPTION_BOOLEAN, "use-pixman", 0, &config.use_pixman },
+		{ WESTON_OPTION_BOOLEAN, "use-egldevice", 0, &config.use_egldevice },
 	};
 
 	parse_options(options, ARRAY_LENGTH(options), argc, argv);
diff --git a/libweston/compositor-drm.c b/libweston/compositor-drm.c
index 12c1b8b5..0236d196 100644
--- a/libweston/compositor-drm.c
+++ b/libweston/compositor-drm.c
@@ -94,7 +94,10 @@ struct drm_backend {
 		int fd;
 		char *filename;
 	} drm;
+
+	EGLDeviceEXT egldevice;
 	struct gbm_device *gbm;
+
 	struct wl_listener session_listener;
 	uint32_t gbm_format;
 
@@ -113,6 +116,7 @@ struct drm_backend {
 	int cursors_are_broken;
 
 	int use_pixman;
+	int use_egldevice;
 
 	struct udev_input input;
 
@@ -595,17 +599,21 @@ drm_output_render_gl(struct drm_output *output, pixman_region32_t *damage)
 	output->base.compositor->renderer->repaint_output(&output->base,
 							  damage);
 
-	bo = gbm_surface_lock_front_buffer(output->gbm_surface);
-	if (!bo) {
-		weston_log("failed to lock front buffer: %m\n");
-		return;
-	}
+	if (b->use_egldevice)
+		output->next = output->dumb[0];
+	else {
+		bo = gbm_surface_lock_front_buffer(output->gbm_surface);
+		if (!bo) {
+			weston_log("failed to lock front buffer: %m\n");
+			return;
+		}
 
-	output->next = drm_fb_get_from_bo(bo, b, output->gbm_format);
-	if (!output->next) {
-		weston_log("failed to get drm_fb for bo\n");
-		gbm_surface_release_buffer(output->gbm_surface, bo);
-		return;
+		output->next = drm_fb_get_from_bo(bo, b, output->gbm_format);
+		if (!output->next) {
+			weston_log("failed to get drm_fb for bo\n");
+			gbm_surface_release_buffer(output->gbm_surface, bo);
+			return;
+		}
 	}
 }
 
@@ -730,9 +738,14 @@ drm_output_repaint(struct weston_output *output_base,
 		output_base->set_dpms(output_base, WESTON_DPMS_ON);
 	}
 
-	if (drmModePageFlip(backend->drm.fd, output->crtc_id,
-			    output->next->fb_id,
-			    DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
+	if (backend->use_egldevice)
+		ret = gl_renderer->output_stream_flip(&output->base, output);
+	else
+		ret = drmModePageFlip(backend->drm.fd, output->crtc_id,
+				      output->next->fb_id,
+				      DRM_MODE_PAGE_FLIP_EVENT, output);
+
+	if (ret < 0) {
 		weston_log("queueing pageflip failed: %m\n");
 		goto err_pageflip;
 	}
@@ -803,7 +816,6 @@ drm_output_start_repaint_loop(struct weston_output *output_base)
 	struct drm_output *output = to_drm_output(output_base);
 	struct drm_backend *backend =
 		to_drm_backend(output_base->compositor);
-	uint32_t fb_id;
 	struct timespec ts, tnow;
 	struct timespec vbl2now;
 	int64_t refresh_nsec;
@@ -859,10 +871,14 @@ drm_output_start_repaint_loop(struct weston_output *output_base)
 	/* Immediate query succeeded, but didn't provide valid timestamp.
 	 * Use pageflip fallback.
 	 */
-	fb_id = output->current->fb_id;
+	if (backend->use_egldevice)
+		ret = gl_renderer->output_stream_flip(&output->base, output);
+	else
+		ret = drmModePageFlip(backend->drm.fd, output->crtc_id,
+				      output->current->fb_id,
+				      DRM_MODE_PAGE_FLIP_EVENT, output);
 
-	if (drmModePageFlip(backend->drm.fd, output->crtc_id, fb_id,
-			    DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
+	if (ret < 0) {
 		weston_log("queueing pageflip failed: %m\n");
 		goto finish_frame;
 	}
@@ -1589,11 +1605,6 @@ create_gbm_device(int fd)
 {
 	struct gbm_device *gbm;
 
-	gl_renderer = weston_load_module("gl-renderer.so",
-					 "gl_renderer_interface");
-	if (!gl_renderer)
-		return NULL;
-
 	/* GBM will load a dri driver, but even though they need symbols from
 	 * libglapi, in some version of Mesa they are not linked to it. Since
 	 * only the gl-renderer module links to it, the call above won't make
@@ -1606,6 +1617,40 @@ create_gbm_device(int fd)
 	return gbm;
 }
 
+static EGLDeviceEXT
+find_egldevice(const char *filename)
+{
+	EGLDeviceEXT egldevice = EGL_NO_DEVICE_EXT;
+	EGLDeviceEXT *devices;
+	EGLint num_devices;
+	const char *drm_path;
+	int i;
+
+	if (gl_renderer->get_devices(0, NULL, &num_devices) < 0 ||
+	    num_devices < 1)
+		return EGL_NO_DEVICE_EXT;
+
+	devices = zalloc(num_devices * sizeof *devices);
+	if (!devices)
+		return EGL_NO_DEVICE_EXT;
+
+	if (gl_renderer->get_devices(num_devices, devices, &num_devices) < 0) {
+		free(devices);
+		return EGL_NO_DEVICE_EXT;
+	}
+
+	for (i = 0; i < num_devices; i++)
+		if (gl_renderer->get_drm_device_file(devices[i],
+						     &drm_path) == 0 &&
+		    strcmp(filename, drm_path) == 0) {
+			egldevice = devices[i];
+			break;
+		}
+
+	free(devices);
+	return egldevice;
+}
+
 /* When initializing EGL, if the preferred buffer format isn't available
  * we may be able to substitute an ARGB format for an XRGB one.
  *
@@ -1632,38 +1677,62 @@ fallback_format_for(uint32_t format)
 static int
 drm_backend_create_gl_renderer(struct drm_backend *b)
 {
-	EGLint format[3] = {
-		b->gbm_format,
-		fallback_format_for(b->gbm_format),
-		0,
-	};
-	int n_formats = 2;
+	if (b->use_egldevice) {
+		EGLint device_platform_attribs[] = {
+			EGL_DRM_MASTER_FD_EXT, b->drm.fd,
+			EGL_NONE
+		};
 
-	if (format[1])
-		n_formats = 3;
-	if (gl_renderer->display_create(b->compositor,
-					EGL_PLATFORM_GBM_KHR,
-					(void *)b->gbm,
+		return gl_renderer->display_create(
+					b->compositor,
+					EGL_PLATFORM_DEVICE_EXT,
+					(void *)b->egldevice,
+					device_platform_attribs,
+					gl_renderer->opaque_stream_attribs,
 					NULL,
-					gl_renderer->opaque_attribs,
-					format,
-					n_formats) < 0) {
-		return -1;
-	}
+					0);
+	} else {
+		EGLint format[3] = {
+			b->gbm_format,
+			fallback_format_for(b->gbm_format),
+			0,
+		};
+		int n_formats = 2;
 
-	return 0;
+		if (format[1])
+			n_formats = 3;
+
+		return gl_renderer->display_create(b->compositor,
+						   EGL_PLATFORM_GBM_KHR,
+						   (void *)b->gbm,
+						   NULL,
+						   gl_renderer->opaque_attribs,
+						   format,
+						   n_formats);
+	}
 }
 
 static int
 init_egl(struct drm_backend *b)
 {
-	b->gbm = create_gbm_device(b->drm.fd);
-
-	if (!b->gbm)
+	gl_renderer = weston_load_module("gl-renderer.so",
+					 "gl_renderer_interface");
+	if (!gl_renderer)
 		return -1;
 
+	if (b->use_egldevice) {
+		b->egldevice = find_egldevice(b->drm.filename);
+		if (b->egldevice == EGL_NO_DEVICE_EXT)
+			return -1;
+	} else {
+		b->gbm = create_gbm_device(b->drm.fd);
+		if (!b->gbm)
+			return -1;
+	}
+
 	if (drm_backend_create_gl_renderer(b) < 0) {
-		gbm_device_destroy(b->gbm);
+		if (b->gbm)
+			gbm_device_destroy(b->gbm);
 		return -1;
 	}
 
@@ -1894,50 +1963,83 @@ find_crtc_for_connector(struct drm_backend *b,
 static int
 drm_output_init_egl(struct drm_output *output, struct drm_backend *b)
 {
-	EGLint format[2] = {
-		output->gbm_format,
-		fallback_format_for(output->gbm_format),
-	};
-	int i, flags, n_formats = 1;
-
-	output->gbm_surface = gbm_surface_create(b->gbm,
-					     output->base.current_mode->width,
-					     output->base.current_mode->height,
-					     format[0],
-					     GBM_BO_USE_SCANOUT |
-					     GBM_BO_USE_RENDERING);
-	if (!output->gbm_surface) {
-		weston_log("failed to create gbm surface\n");
-		return -1;
-	}
+	if (b->use_egldevice) {
+		int w = output->base.current_mode->width;
+		int h = output->base.current_mode->height;
 
-	if (format[1])
-		n_formats = 2;
-	if (gl_renderer->output_window_create(&output->base,
-					      (EGLNativeWindowType)output->gbm_surface,
-					      output->gbm_surface,
-					      gl_renderer->opaque_attribs,
-					      format,
-					      n_formats) < 0) {
-		weston_log("failed to create gl renderer output state\n");
-		gbm_surface_destroy(output->gbm_surface);
-		return -1;
-	}
+		/* Create a black dumb fb for modesetting */
+		output->dumb[0] = drm_fb_create_dumb(b, w, h,
+						     DRM_FORMAT_XRGB8888);
+		if (!output->dumb[0]) {
+			weston_log("failed to create dumb framebuffer\n");
+			return -1;
+		}
+		memset(output->dumb[0]->map, 0, output->dumb[0]->size);
+
+		if (gl_renderer->output_stream_create(&output->base, ~0u,
+						      output->crtc_id) < 0) {
+			weston_log("failed to create gl renderer output stream "
+				   "state\n");
+			drm_fb_destroy_dumb(output->dumb[0]);
+			output->dumb[0] = NULL;
+			return -1;
+		}
 
-	flags = GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE;
+		/* FIXME: Add hw planes and cursors for EGL device when supported */
+		b->sprites_are_broken = 1;
+		b->cursors_are_broken = 1;
+	} else {
+		EGLint format[2] = {
+			output->gbm_format,
+			fallback_format_for(output->gbm_format),
+		};
+		int i, flags, n_formats = 1;
+
+		output->gbm_surface = gbm_surface_create(
+					b->gbm,
+					output->base.current_mode->width,
+					output->base.current_mode->height,
+					format[0],
+					GBM_BO_USE_SCANOUT |
+					GBM_BO_USE_RENDERING);
+		if (!output->gbm_surface) {
+			weston_log("failed to create gbm surface\n");
+			return -1;
+		}
 
-	for (i = 0; i < 2; i++) {
-		if (output->gbm_cursor_bo[i])
-			continue;
+		if (format[1])
+			n_formats = 2;
+		if (gl_renderer->output_window_create(
+				&output->base,
+				(EGLNativeWindowType)output->gbm_surface,
+				output->gbm_surface,
+				gl_renderer->opaque_attribs,
+				format,
+				n_formats) < 0) {
+			weston_log("failed to create gl renderer output "
+				   "state\n");
+			gbm_surface_destroy(output->gbm_surface);
+			return -1;
+		}
 
-		output->gbm_cursor_bo[i] =
-			gbm_bo_create(b->gbm, b->cursor_width, b->cursor_height,
-				GBM_FORMAT_ARGB8888, flags);
-	}
+		flags = GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE;
 
-	if (output->gbm_cursor_bo[0] == NULL || output->gbm_cursor_bo[1] == NULL) {
-		weston_log("cursor buffers unavailable, using gl cursors\n");
-		b->cursors_are_broken = 1;
+		for (i = 0; i < 2; i++) {
+			if (output->gbm_cursor_bo[i])
+				continue;
+
+			output->gbm_cursor_bo[i] =
+				gbm_bo_create(b->gbm,
+					      b->cursor_width,
+					      b->cursor_height,
+					      GBM_FORMAT_ARGB8888,
+					      flags);
+		}
+
+		if (output->gbm_cursor_bo[0] == NULL || output->gbm_cursor_bo[1] == NULL) {
+			weston_log("cursor buffers unavailable, using gl cursors\n");
+			b->cursors_are_broken = 1;
+		}
 	}
 
 	return 0;
@@ -1947,7 +2049,14 @@ static void
 drm_output_fini_egl(struct drm_output *output)
 {
 	gl_renderer->output_destroy(&output->base);
-	gbm_surface_destroy(output->gbm_surface);
+
+	if (output->dumb[0]) {
+		drm_fb_destroy_dumb(output->dumb[0]);
+		output->dumb[0] = NULL;
+	}
+
+	if (output->gbm_surface)
+		gbm_surface_destroy(output->gbm_surface);
 }
 
 static int
@@ -3107,6 +3216,11 @@ recorder_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
 	struct drm_output *output;
 	int width, height;
 
+	if (b->use_egldevice) {
+		weston_log("recorder not supported with EGL device\n");
+		return;
+	}
+
 	output = container_of(b->compositor->output_list.next,
 			      struct drm_output, base.link);
 
@@ -3162,11 +3276,20 @@ switch_to_gl_renderer(struct drm_backend *b)
 
 	weston_log("Switching to GL renderer\n");
 
-	b->gbm = create_gbm_device(b->drm.fd);
-	if (!b->gbm) {
-		weston_log("Failed to create gbm device. "
-			   "Aborting renderer switch\n");
-		return;
+	if (b->use_egldevice) {
+		b->egldevice = find_egldevice(b->drm.filename);
+		if (b->egldevice == EGL_NO_DEVICE_EXT) {
+			weston_log("Failed to create EGL device. "
+				   "Aborting renderer switch\n");
+			return;
+		}
+	} else {
+		b->gbm = create_gbm_device(b->drm.fd);
+		if (!b->gbm) {
+			weston_log("Failed to create gbm device. "
+				   "Aborting renderer switch\n");
+			return;
+		}
 	}
 
 	wl_list_for_each(output, &b->compositor->output_list, base.link)
@@ -3175,7 +3298,8 @@ switch_to_gl_renderer(struct drm_backend *b)
 	b->compositor->renderer->destroy(b->compositor);
 
 	if (drm_backend_create_gl_renderer(b) < 0) {
-		gbm_device_destroy(b->gbm);
+		if (b->gbm)
+			gbm_device_destroy(b->gbm);
 		weston_log("Failed to create GL renderer. Quitting.\n");
 		/* FIXME: we need a function to shutdown cleanly */
 		assert(0);
@@ -3239,6 +3363,7 @@ drm_backend_create(struct weston_compositor *compositor,
 	b->sprites_are_broken = 1;
 	b->compositor = compositor;
 	b->use_pixman = config->use_pixman;
+	b->use_egldevice = config->use_egldevice;
 
 	if (parse_gbm_format(config->gbm_format, GBM_FORMAT_XRGB8888, &b->gbm_format) < 0)
 		goto err_compositor;
diff --git a/libweston/compositor-drm.h b/libweston/compositor-drm.h
index 2e2995a2..9cd0eef6 100644
--- a/libweston/compositor-drm.h
+++ b/libweston/compositor-drm.h
@@ -110,6 +110,10 @@ struct weston_drm_backend_config {
 	/** Whether to use the pixman renderer instead of the OpenGL ES renderer. */
 	bool use_pixman;
 
+	/** Whether to use the GL composition based off EGLDevice & friends instead
+	 * of GBM. */
+	bool use_egldevice;
+
 	/** The seat to be used for input and output.
 	 *
 	 * If NULL the default "seat0" will be used.  The backend will
diff --git a/libweston/gl-renderer.h b/libweston/gl-renderer.h
index 39ea3b42..37252492 100644
--- a/libweston/gl-renderer.h
+++ b/libweston/gl-renderer.h
@@ -45,6 +45,7 @@ typedef void *EGLConfig;
 typedef intptr_t EGLNativeDisplayType;
 typedef intptr_t EGLNativeWindowType;
 #define EGL_DEFAULT_DISPLAY ((EGLNativeDisplayType)0)
+#define EGL_NO_DEVICE_EXT   ((EGLDeviceEXT)0)
 
 #endif /* ENABLE_EGL */
 
diff --git a/shared/weston-egl-ext.h b/shared/weston-egl-ext.h
index 11e9ccc1..de82310f 100644
--- a/shared/weston-egl-ext.h
+++ b/shared/weston-egl-ext.h
@@ -162,6 +162,10 @@ typedef EGLSurface (EGLAPIENTRYP PFNEGLCREATEPLATFORMPIXMAPSURFACEEXTPROC) (EGLD
 #define EGL_PLATFORM_DEVICE_EXT 0x313F
 #endif
 
+#ifndef EGL_DRM_MASTER_FD_EXT
+#define EGL_DRM_MASTER_FD_EXT 0x333C
+#endif
+
 /*
  * FIXME: Remove both EGL_EXT_stream_acquire_mode and
  *        EGL_NV_output_drm_flip_event definitions below once both extensions
@@ -210,6 +214,11 @@ EGLAPI EGLBoolean EGLAPIENTRY eglStreamConsumerAcquireAttribEXT (EGLDisplay dpy,
 #define EGL_PLATFORM_X11_KHR     0x31D5
 #define EGL_PLATFORM_DEVICE_EXT  0x313F
 
+/* EGL_DRM_MASTER_FD_EXT and EGL_NONE enum values are also kept to allow
+ * compositor-drm.c to build with EGLDevice and EGLStream support */
+#define EGL_DRM_MASTER_FD_EXT    0x333C
+#define EGL_NONE                 0x3038
+
 #endif /* ENABLE_EGL */
 
 #endif
-- 
2.11.1