summarylogtreecommitdiffstats
path: root/0006-compositor-Process-stream-attach-requests-with-wl_eg.patch
blob: ac73e3238faf188ff7376ded8efbf5c4bc6d53e7 (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
From b2d91b7d969be5b7c42274fbf81093006d2edfef Mon Sep 17 00:00:00 2001
From: "Miguel A. Vico" <mvicomoya@nvidia.com>
Date: Thu, 18 May 2017 14:35:36 -0700
Subject: [PATCH 6/6] compositor: Process stream attach requests with
 wl_eglstream_controller
X-NVConfidentiality: public

So far, the EGLStream implementation of the Wayland client-side driver
has been using wl_surface_attach + commit in order to make the server
create its stream endpoint and attach a consumer to it.

However, no actual buffer would be actually shared between client and
server, which goes against many of the assumptions behind
wl_surface_attach + commit.

This has caused different interaction issues in the past.

In order to properly resolve this once and for all, a new
wl_eglstream_controller protocol has been added which will let clients
request the compositor to create its stream.

This change adds the required code for weston to create a
wl_eglstream_controller global and process attach_eglstream_consumer
requests.

[mvicomoya: - Dynamically load libnvidia-egl-wayland.so.1 instead linking
              against it
            - Add wayland-eglstream-protocols package dependency and
              generate server header for wayland-eglstream-controller at
              build time]

Signed-off-by: Ashutosh Agarwal <asagarwal@nvidia.com>
Signed-off-by: Miguel A Vico Moya <mvicomoya@nvidia.com>
---
 Makefile.am             |  14 ++++
 compositor/meson.build  |   1 +
 configure.ac            |   4 ++
 libweston/compositor.c  |  78 +++++++++++++++++++++
 libweston/compositor.h  |   4 ++
 libweston/gl-renderer.c | 147 +++++++++++++++++++++++++---------------
 protocol/meson.build    |   7 ++
 7 files changed, 201 insertions(+), 54 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 5407b593..0bad6976 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -192,6 +192,11 @@ nodist_libweston_@LIBWESTON_MAJOR@_la_SOURCES =				\
 
 BUILT_SOURCES += $(nodist_libweston_@LIBWESTON_MAJOR@_la_SOURCES)
 
+nodist_libweston_eglstreams_@LIBWESTON_MAJOR@_la_SOURCES =		\
+	protocol/wayland-eglstream-controller-server-protocol.h
+
+BUILT_SOURCES += $(nodist_libweston_eglstreams_@LIBWESTON_MAJOR@_la_SOURCES)
+
 bin_PROGRAMS += weston
 
 weston_LDFLAGS = -export-dynamic
@@ -1736,6 +1741,15 @@ protocol/%-server-protocol.h : $(WAYLAND_PROTOCOLS_DATADIR)/$$(call protostabili
 protocol/%-client-protocol.h : $(WAYLAND_PROTOCOLS_DATADIR)/$$(call protostability,$$*)/$$(call protoname,$$*)/$$*.xml
 	$(AM_V_GEN)$(MKDIR_P) $(dir $@) && $(wayland_scanner) client-header < $< > $@
 
+protocol/%-protocol.c : $(WAYLAND_EGLSTREAM_PROTOCOLS_DATADIR)/%.xml
+	$(AM_V_GEN)$(MKDIR_P) $(dir $@) && $(wayland_scanner) code < $< > $@
+
+protocol/%-server-protocol.h : $(WAYLAND_EGLSTREAM_PROTOCOLS_DATADIR)/%.xml
+	$(AM_V_GEN)$(MKDIR_P) $(dir $@) && $(wayland_scanner) server-header < $< > $@
+
+protocol/%-client-protocol.h : $(WAYLAND_EGLSTREAM_PROTOCOLS_DATADIR)/%.xml
+	$(AM_V_GEN)$(MKDIR_P) $(dir $@) && $(wayland_scanner) client-header < $< > $@
+
 protocol/%-protocol.c : $(top_srcdir)/protocol/%.xml
 	$(AM_V_GEN)$(MKDIR_P) $(dir $@) && $(wayland_scanner) code < $< > $@
 
diff --git a/compositor/meson.build b/compositor/meson.build
index 3824d6ff..075b0bcd 100644
--- a/compositor/meson.build
+++ b/compositor/meson.build
@@ -9,6 +9,7 @@ srcs_weston = [
 	input_method_unstable_v1_protocol_c,
 	weston_screenshooter_server_protocol_h,
 	weston_screenshooter_protocol_c,
+	wayland_eglstream_controller_server_protocol_h,
 ]
 deps_weston = [
 	dep_libshared,
diff --git a/configure.ac b/configure.ac
index 68cdc86c..53b64bea 100644
--- a/configure.ac
+++ b/configure.ac
@@ -261,6 +261,10 @@ PKG_CHECK_MODULES(WAYLAND_PROTOCOLS, [wayland-protocols >= 1.17],
 		  [ac_wayland_protocols_pkgdatadir=`$PKG_CONFIG --variable=pkgdatadir wayland-protocols`])
 AC_SUBST(WAYLAND_PROTOCOLS_DATADIR, $ac_wayland_protocols_pkgdatadir)
 
+PKG_CHECK_MODULES(WAYLAND_EGLSTREAM_PROTOCOLS, [wayland-eglstream-protocols],
+		  [ac_wayland_eglstream_protocols_pkgdatadir=`$PKG_CONFIG --variable=pkgdatadir wayland-eglstream-protocols`])
+AC_SUBST(WAYLAND_EGLSTREAM_PROTOCOLS_DATADIR, $ac_wayland_eglstream_protocols_pkgdatadir)
+
 AC_ARG_ENABLE(wayland-compositor, [  --enable-wayland-compositor],,
 	      enable_wayland_compositor=yes)
 AM_CONDITIONAL(ENABLE_WAYLAND_COMPOSITOR,
diff --git a/libweston/compositor.c b/libweston/compositor.c
index e117479c..3a7b4cec 100644
--- a/libweston/compositor.c
+++ b/libweston/compositor.c
@@ -3,6 +3,7 @@
  * Copyright © 2008-2011 Kristian Høgsberg
  * Copyright © 2012-2018 Collabora, Ltd.
  * Copyright © 2017, 2018 General Electric Company
+ * Copyright © 2017-2018 NVIDIA Corporation
  *
  * Permission is hereby granted, free of charge, to any person obtaining
  * a copy of this software and associated documentation files (the
@@ -71,6 +72,7 @@
 #include "version.h"
 #include "plugin-registry.h"
 #include "pixel-formats.h"
+#include "wayland-eglstream-controller-server-protocol.h"
 
 #define DEFAULT_REPAINT_WINDOW 7 /* milliseconds */
 
@@ -6473,6 +6475,80 @@ bind_presentation(struct wl_client *client,
 	wp_presentation_send_clock_id(resource, compositor->presentation_clock);
 }
 
+static struct wl_interface *eglstream_controller_interface = NULL;
+
+static void
+attach_eglstream_consumer(struct wl_client *client,
+			  struct wl_resource *resource,
+			  struct wl_resource *wl_surface,
+			  struct wl_resource *wl_eglstream)
+{
+	struct weston_surface *surface =
+		wl_resource_get_user_data(wl_surface);
+
+	surface->compositor->renderer->attach_eglstream_consumer(surface,
+								 wl_eglstream);
+}
+
+static const struct wl_eglstream_controller_interface
+eglstream_controller_implementation = {
+	attach_eglstream_consumer
+};
+
+static void
+bind_eglstream_controller(struct wl_client *client,
+			  void *data, uint32_t version, uint32_t id)
+{
+	struct wl_resource *resource;
+
+	resource = wl_resource_create(client, eglstream_controller_interface,
+				      version, id);
+
+	if (resource == NULL) {
+		wl_client_post_no_memory(client);
+		return;
+	}
+	wl_resource_set_implementation(resource,
+				       &eglstream_controller_implementation,
+				       data,
+				       NULL);
+}
+
+static void
+init_eglstream_controller(struct wl_display *display)
+{
+	/*
+	 * wl_eglstream_controller_interface is provided by
+	 * libnvidia-egl-wayland.so.1
+	 *
+	 * Since it might not be available on the
+	 * system, dynamically load it at runtime and resolve the needed
+	 * symbols. If available, it should be found under any of the search
+	 * directories of dlopen()
+	 *
+	 * Failure to initialize wl_eglstream_controller is non-fatal
+	 */
+
+	void *lib = dlopen("libnvidia-egl-wayland.so.1", RTLD_NOW | RTLD_LAZY);
+	if (!lib)
+		goto fail;
+
+	eglstream_controller_interface =
+		dlsym(lib, "wl_eglstream_controller_interface");
+
+	if (!eglstream_controller_interface)
+		goto fail;
+
+	if (wl_global_create(display,
+			     eglstream_controller_interface, 1,
+			     NULL, bind_eglstream_controller))
+		return; /* success */
+fail:
+	if (lib)
+		dlclose(lib);
+	weston_log("warning: Unable to initialize wl_eglstream_controller.\n");
+}
+
 static void
 compositor_bind(struct wl_client *client,
 		void *data, uint32_t version, uint32_t id)
@@ -6840,6 +6916,8 @@ weston_compositor_create(struct wl_display *display, void *user_data)
 			      ec, bind_presentation))
 		goto fail;
 
+	init_eglstream_controller(ec->wl_display);
+
 	if (weston_debug_compositor_create(ec) < 0)
 		goto fail;
 
diff --git a/libweston/compositor.h b/libweston/compositor.h
index 3af1443a..d7ed3ff2 100644
--- a/libweston/compositor.h
+++ b/libweston/compositor.h
@@ -926,6 +926,10 @@ struct weston_renderer {
 	void (*query_dmabuf_modifiers)(struct weston_compositor *ec,
 				int format, uint64_t **modifiers,
 				int *num_modifiers);
+
+	/** Create weston endpoint of an EGLStream & attach a consumer to it */
+	bool (*attach_eglstream_consumer)(struct weston_surface *es,
+					  struct wl_resource *wl_eglstream);
 };
 
 enum weston_capability {
diff --git a/libweston/gl-renderer.c b/libweston/gl-renderer.c
index 4bae6546..e66bbd4c 100644
--- a/libweston/gl-renderer.c
+++ b/libweston/gl-renderer.c
@@ -1,7 +1,7 @@
 /*
  * Copyright © 2012 Intel Corporation
  * Copyright © 2015 Collabora, Ltd.
- * Copyright © 2016 NVIDIA Corporation
+ * Copyright © 2016-2017 NVIDIA Corporation
  *
  * Permission is hereby granted, free of charge, to any person obtaining
  * a copy of this software and associated documentation files (the
@@ -2537,18 +2537,9 @@ gl_renderer_attach_dmabuf(struct weston_surface *surface,
 	surface->is_opaque = dmabuf_is_opaque(dmabuf);
 }
 
-/*
- * gl_renderer_attach_stream_texture
- *
- * Try to bind given <buffer> to an EGLStream. If the given buffer was already
- * bound, it will acquire next frame on the stream.
- *
- * Return true if the given <buffer> corresponds to an EGLStream; otherwise,
- * return false (if might be another kind of buffer).
- */
-static bool
-gl_renderer_attach_stream_texture(struct weston_surface *es,
-				  struct weston_buffer *buffer)
+static EGLint
+gl_renderer_attach_eglstream_consumer_common(struct weston_surface *es,
+					     struct wl_resource *wl_eglstream)
 {
 #ifdef EGL_NV_stream_attrib
 	struct weston_compositor *ec = es->compositor;
@@ -2557,65 +2548,111 @@ gl_renderer_attach_stream_texture(struct weston_surface *es,
 	EGLStreamKHR stream = EGL_NO_STREAM_KHR;
 	EGLAttrib stream_attribs[] = {
 #ifdef EGL_WL_wayland_eglstream
-		EGL_WAYLAND_EGLSTREAM_WL, (EGLAttrib)buffer->resource,
+		EGL_WAYLAND_EGLSTREAM_WL, (EGLAttrib)wl_eglstream,
 #endif
 		EGL_NONE
 	};
-	EGLint stream_state = EGL_STREAM_STATE_EMPTY_KHR;
 
 	/* Check for required extensions. If they arent supported, there's no
-	 * way the given buffer corresponds to an EGLStream */
+	 *  way the given resource corresponds to an EGLStream */
 	if (!gr->has_egl_stream_attrib ||
 	    !gr->has_egl_stream_consumer_gltexture ||
 	    !gr->has_egl_wayland_eglstream)
-		return false;
+		return EGL_BAD_ACCESS;
 
 	stream = gr->create_stream_attrib(gr->egl_display, stream_attribs);
-	if (stream == EGL_NO_STREAM_KHR) {
+
+	if (stream == EGL_NO_STREAM_KHR)
+		return eglGetError();
+
+	if (gs->egl_stream != EGL_NO_STREAM_KHR)
+		gr->destroy_stream(gr->egl_display, gs->egl_stream);
+
+	gs->egl_stream = stream;
+	gs->shader = &gr->texture_shader_egl_external;
+	gs->target = GL_TEXTURE_EXTERNAL_OES;
+
+	glActiveTexture(GL_TEXTURE0);
+	ensure_textures(gs, 2);
+	glBindTexture(gs->target, gs->textures[1]);
+
+	gs->new_stream = (gr->stream_consumer_gltexture(
+					gr->egl_display,
+					gs->egl_stream) == EGL_TRUE);
+
+	if (!gs->new_stream) {
 		EGLint err = eglGetError();
 
-		switch (err) {
-		case EGL_BAD_ACCESS:
-			/* EGL_BAD_ACCESS is generated whenever buffer->resource
-			 * does not corresponds to a stream */
-			return false;
+		weston_log("failed to set stream consumer\n");
+		weston_log("EGL error state: %s (0x%04lx)\n",
+			   egl_error_string(err), (long)err);
 
-		case EGL_BAD_STREAM_KHR:
-			/* EGL_BAD_STREAM_KHR is generated whenever
-			 * buffer->resource corresponds to a previously created
-			 * stream so we must have a valid stream handle already
-			 * we can use to acquire next frame */
-			break;
+		gr->destroy_stream(gr->egl_display, gs->egl_stream);
+		gs->egl_stream = EGL_NO_STREAM_KHR;
+		return err;
+	}
 
-		default:
-			/* An unknown error was generated */
-			assert(0);
-			return false;
-		}
-	} else {
-		/* Clean up current stream resources if needed */
-		if (gs->egl_stream != EGL_NO_STREAM_KHR)
-			gr->destroy_stream(gr->egl_display, gs->egl_stream);
+	return EGL_SUCCESS;
+#else
+	return EGL_BAD_ACCESS;
+#endif
+}
 
-		gs->egl_stream = stream;
-		gs->shader = &gr->texture_shader_egl_external;
-		gs->target = GL_TEXTURE_EXTERNAL_OES;
+static bool
+gl_renderer_attach_eglstream_consumer(struct weston_surface *es,
+				      struct wl_resource *stream)
+{
+	EGLint err = gl_renderer_attach_eglstream_consumer_common(es, stream);
+	return (err == EGL_SUCCESS);
+}
+
+/*
+ * gl_renderer_attach_stream_texture
+ *
+ * Try to bind given <buffer> to an EGLStream. If the given buffer was already
+ * bound, it will acquire next frame on the stream.
+ *
+ * Return true if the given <buffer> corresponds to an EGLStream; otherwise,
+ * return false (if might be another kind of buffer).
+ */
+static bool
+gl_renderer_attach_stream_texture(struct weston_surface *es,
+				  struct weston_buffer *buffer)
+{
+#ifdef EGL_NV_stream_attrib
+	struct weston_compositor *ec = es->compositor;
+	struct gl_renderer *gr = get_renderer(ec);
+	struct gl_surface_state *gs = get_surface_state(es);
+	EGLint stream_state = EGL_STREAM_STATE_EMPTY_KHR;
 
-		glActiveTexture(GL_TEXTURE0);
-		ensure_textures(gs, 2);
-		glBindTexture(gs->target, gs->textures[1]);
+	EGLint err;
 
-		gs->new_stream = (gr->stream_consumer_gltexture(
-						gr->egl_display,
-						gs->egl_stream) == EGL_TRUE);
+	err = gl_renderer_attach_eglstream_consumer_common(es, buffer->resource);
 
-		if (!gs->new_stream) {
-			weston_log("failed to set stream consumer\n");
-			gl_renderer_print_egl_error_state();
-			gr->destroy_stream(gr->egl_display, gs->egl_stream);
-			gs->egl_stream = EGL_NO_STREAM_KHR;
-			return true; /* buffer->resource is EGLStream */
-		}
+	switch (err) {
+	case EGL_BAD_ACCESS:
+		/* EGL_BAD_ACCESS is generated whenever buffer->resource
+		 * does not corresponds to a stream */
+		return false;
+
+	case EGL_BAD_STREAM_KHR:
+		/* EGL_BAD_STREAM_KHR is generated whenever
+		 * buffer->resource corresponds to a previously created
+		 * stream so we must have a valid stream handle already
+		 * we can use to acquire next frame */
+		break;
+
+	case EGL_SUCCESS:
+		/* EGL_SUCCESS is returned when the server stream endpoint is
+		 * created and a consumer is attached successfully. This path is
+		 * hit with old drivers which use wl_surface::attach + commit to
+		 * request stream creation. */
+		return true; /* buffer->resource is EGLStream */
+
+	default:
+		/* An unknown error was generated */
+		assert(0);
+		return false;
 	}
 
 	/* At this point we should have a valid stream handle */
@@ -4044,6 +4081,8 @@ gl_renderer_display_create(struct weston_compositor *ec, EGLenum platform,
 	gr->base.surface_get_content_size =
 		gl_renderer_surface_get_content_size;
 	gr->base.surface_copy_content = gl_renderer_surface_copy_content;
+	gr->base.attach_eglstream_consumer =
+		gl_renderer_attach_eglstream_consumer;
 	gr->egl_display = NULL;
 
 	/* extension_suffix is supported */
diff --git a/protocol/meson.build b/protocol/meson.build
index 34026ff9..37642298 100644
--- a/protocol/meson.build
+++ b/protocol/meson.build
@@ -7,6 +7,9 @@ prog_scanner = find_program(dep_scanner.get_pkgconfig_variable('wayland_scanner'
 dep_wp = dependency('wayland-protocols', version: '>= 1.17')
 dir_wp_base = dep_wp.get_pkgconfig_variable('pkgdatadir')
 
+dep_wep = dependency('wayland-eglstream-protocols')
+dir_wep_base = dep_wep.get_pkgconfig_variable('pkgdatadir')
+
 install_data(
 	[
 		'weston-debug.xml',
@@ -37,6 +40,7 @@ generated_protocols = [
 	[ 'xdg-output', 'v1' ],
 	[ 'xdg-shell', 'v6' ],
 	[ 'xdg-shell', 'stable' ],
+	[ 'wayland-eglstream-controller', '3rdparty', dir_wep_base ],
 ]
 
 foreach proto: generated_protocols
@@ -44,6 +48,9 @@ foreach proto: generated_protocols
 	if proto[1] == 'internal'
 		base_file = proto_name
 		xml_path = '@0@.xml'.format(proto_name)
+	elif proto[1] == '3rdparty'
+		base_file = proto_name
+		xml_path = '@0@/@1@.xml'.format(proto[2], base_file)
 	elif proto[1] == 'stable'
 		base_file = proto_name
 		xml_path = '@0@/stable/@1@/@1@.xml'.format(dir_wp_base, base_file)
-- 
2.21.0