summarylogtreecommitdiffstats
path: root/0003-gl-renderer-Add-EGL-client-support-for-EGLStream-fra.patch
blob: 3b385346c138570c0075570617d956b7c87ce66d (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
From ef918a27b0134d44d680c2cd034a1b50a87edc3e Mon Sep 17 00:00:00 2001
From: "Miguel A. Vico" <mvicomoya@nvidia.com>
Date: Thu, 29 Mar 2018 00:15:49 -0700
Subject: [PATCH 3/6] gl-renderer: Add EGL client support for EGLStream frame
 presentation
X-NVConfidentiality: public

By attaching a GLTexture consumer to a stream, a producer (wayland EGL
client) could feed frames to a texture, which in turn can be used by a
compositor to prepare the final frame to be presented.

This change adds required logic to support presentation approach
described above.

Note that some unpublished EGL extensions were needed:

 - EGL_NV_stream_attrib:
   https://github.com/aritger/eglstreams-kms-example/blob/master/proposed-extensions/EGL_NV_stream_attrib.txt

 - EGL_WL_wayland_eglstream:
   https://github.com/aritger/eglstreams-kms-example/blob/master/proposed-extensions/EGL_WL_wayland_eglstream.txt

[ekurzinger: - Try realizing EGLStream before EGLImage buffer since
               libnvidia-egl-wayland now reports EGL_TEXTURE_FORMAT]

Signed-off-by: Miguel A Vico Moya <mvicomoya@nvidia.com>
Signed-off-by: Erik Kurzinger <ekurzinger@nvidia.com>
Reviewed-by: Adam Cheney <acheney@nvidia.com>
Reviewed-by: James Jones <jajones@nvidia.com>
---
 libweston/renderer-gl/gl-renderer.c | 184 ++++++++++++++++++++++++++++
 shared/weston-egl-ext.h             |   5 +
 2 files changed, 189 insertions(+)

diff --git a/libweston/renderer-gl/gl-renderer.c b/libweston/renderer-gl/gl-renderer.c
index 215ed9b3..22a64f81 100644
--- a/libweston/renderer-gl/gl-renderer.c
+++ b/libweston/renderer-gl/gl-renderer.c
@@ -189,6 +189,9 @@ struct gl_surface_state {
 	int hsub[3];  /* horizontal subsampling per plane */
 	int vsub[3];  /* vertical subsampling per plane */
 
+	EGLStreamKHR egl_stream;
+	bool new_stream;
+
 	struct weston_surface *surface;
 
 	/* Whether this surface was used in the current output repaint.
@@ -253,6 +256,7 @@ struct gl_renderer {
 
 	PFNEGLCREATESTREAMKHRPROC create_stream;
 	PFNEGLDESTROYSTREAMKHRPROC destroy_stream;
+	PFNEGLQUERYSTREAMKHRPROC query_stream;
 	bool has_egl_stream;
 
 	PFNEGLCREATESTREAMPRODUCERSURFACEKHRPROC create_stream_producer_surface;
@@ -262,11 +266,16 @@ struct gl_renderer {
 	bool has_egl_stream_consumer_egloutput;
 
 #ifdef EGL_NV_stream_attrib
+	PFNEGLCREATESTREAMATTRIBNVPROC create_stream_attrib;
 	PFNEGLSTREAMCONSUMERACQUIREATTRIBNVPROC stream_consumer_acquire_attrib;
 #endif
 	bool has_egl_stream_attrib;
 	bool has_egl_stream_acquire_mode;
 
+	PFNEGLSTREAMCONSUMERGLTEXTUREEXTERNALKHRPROC stream_consumer_gltexture;
+	bool has_egl_stream_consumer_gltexture;
+	bool has_egl_wayland_eglstream;
+
 	bool has_gl_texture_rg;
 
 	struct gl_shader texture_shader_rgba;
@@ -2617,6 +2626,145 @@ 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)
+{
+#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);
+	EGLStreamKHR stream = EGL_NO_STREAM_KHR;
+	EGLAttrib stream_attribs[] = {
+#ifdef EGL_WL_wayland_eglstream
+		EGL_WAYLAND_EGLSTREAM_WL, (EGLAttrib)buffer->resource,
+#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 */
+	if (!gr->has_egl_stream_attrib ||
+	    !gr->has_egl_stream_consumer_gltexture ||
+	    !gr->has_egl_wayland_eglstream)
+		return false;
+
+	stream = gr->create_stream_attrib(gr->egl_display, stream_attribs);
+	if (stream == EGL_NO_STREAM_KHR) {
+		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;
+
+		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;
+
+		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);
+
+		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) {
+			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 */
+		}
+	}
+
+	/* At this point we should have a valid stream handle */
+	assert(gs->egl_stream != EGL_NO_STREAM_KHR);
+
+	/* Check whether there are new frames available */
+	if (gr->query_stream(gr->egl_display,
+			     gs->egl_stream,
+			     EGL_STREAM_STATE_KHR,
+			     &stream_state) != EGL_TRUE) {
+		weston_log("failed to query stream state\n");
+		gl_renderer_print_egl_error_state();
+		return true; /* buffer->resource is EGLStream */
+	}
+
+	/* If no new frame available, re-use last one */
+	if (stream_state != EGL_STREAM_STATE_NEW_FRAME_AVAILABLE_KHR) {
+		/* Fake size of last frame */
+		buffer->width = gs->pitch;
+		buffer->height = gs->height;
+		return true; /* buffer->resource is EGLStream */
+	}
+
+	if (gr->stream_consumer_acquire_attrib(gr->egl_display,
+					       gs->egl_stream,
+					       NULL) != EGL_TRUE) {
+		weston_log("failed to acquire buffer\n");
+		gl_renderer_print_egl_error_state();
+		return true; /* buffer->resource is EGLStream */
+	}
+
+	/* Swap textures if new stream was created */
+	if (gs->new_stream) {
+		GLuint tmp = gs->textures[0];
+
+		gs->textures[0] = gs->textures[1];
+		gs->textures[1] = tmp;
+		gs->new_stream = false;
+	}
+
+	/* Update buffer and surface data */
+	buffer->legacy_buffer = (void *)buffer->resource;
+	gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
+			 EGL_WIDTH, &buffer->width);
+	gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
+			 EGL_HEIGHT, &buffer->height);
+	gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
+			 EGL_WAYLAND_Y_INVERTED_WL, &buffer->y_inverted);
+
+	gs->pitch = buffer->width;
+	gs->height = buffer->height;
+	gs->buffer_type = BUFFER_TYPE_EGL;
+	gs->y_inverted = buffer->y_inverted;
+
+	return true; /* buffer->resource is EGLStream */
+#else
+	return false;
+#endif
+}
+
 static void
 gl_renderer_attach(struct weston_surface *es, struct weston_buffer *buffer)
 {
@@ -2643,6 +2791,12 @@ gl_renderer_attach(struct weston_surface *es, struct weston_buffer *buffer)
 		gs->buffer_type = BUFFER_TYPE_NULL;
 		gs->y_inverted = true;
 		es->is_opaque = false;
+
+		if (gs->egl_stream != EGL_NO_STREAM_KHR) {
+			gr->destroy_stream(gr->egl_display, gs->egl_stream);
+			gs->egl_stream = EGL_NO_STREAM_KHR;
+		}
+
 		return;
 	}
 
@@ -2650,6 +2804,9 @@ gl_renderer_attach(struct weston_surface *es, struct weston_buffer *buffer)
 
 	if (shm_buffer)
 		gl_renderer_attach_shm(es, buffer, shm_buffer);
+	else if (gl_renderer_attach_stream_texture(es, buffer))
+        /* The stream texture is attached. Nothing else to be done here */
+        ((void)0);
 	else if (gr->has_bind_display &&
 		 gr->query_buffer(gr->egl_display, (void *)buffer->resource,
 				  EGL_TEXTURE_FORMAT, &format))
@@ -2853,6 +3010,10 @@ surface_state_destroy(struct gl_surface_state *gs, struct gl_renderer *gr)
 	weston_buffer_reference(&gs->buffer_ref, NULL);
 	weston_buffer_release_reference(&gs->buffer_release_ref, NULL);
 	pixman_region32_fini(&gs->texture_damage);
+
+	if (gs->egl_stream != EGL_NO_STREAM_KHR)
+		gr->destroy_stream(gr->egl_display, gs->egl_stream);
+
 	free(gs);
 }
 
@@ -2903,6 +3064,8 @@ gl_renderer_create_surface(struct weston_surface *surface)
 
 	gs->surface = surface;
 
+	gs->egl_stream = EGL_NO_STREAM_KHR;
+
 	pixman_region32_init(&gs->texture_damage);
 	surface->renderer_state = gs;
 
@@ -3661,14 +3824,19 @@ gl_renderer_setup_egl_extensions(struct weston_compositor *ec)
 		(void *) eglGetProcAddress("eglQueryOutputLayerAttribEXT");
 	gr->create_stream = (void *) eglGetProcAddress("eglCreateStreamKHR");
 	gr->destroy_stream = (void *) eglGetProcAddress("eglDestroyStreamKHR");
+	gr->query_stream = (void *) eglGetProcAddress("eglQueryStreamKHR");
 	gr->create_stream_producer_surface =
 		(void *) eglGetProcAddress("eglCreateStreamProducerSurfaceKHR");
 	gr->stream_consumer_output =
 		(void *) eglGetProcAddress("eglStreamConsumerOutputEXT");
 #ifdef EGL_NV_stream_attrib
+	gr->create_stream_attrib =
+		(void *) eglGetProcAddress("eglCreateStreamAttribNV");
 	gr->stream_consumer_acquire_attrib =
 		(void *) eglGetProcAddress("eglStreamConsumerAcquireAttribNV");
 #endif
+	gr->stream_consumer_gltexture =
+		(void *) eglGetProcAddress("eglStreamConsumerGLTextureExternalKHR");
 
 	extensions =
 		(const char *) eglQueryString(gr->egl_display, EGL_EXTENSIONS);
@@ -3770,6 +3938,12 @@ gl_renderer_setup_egl_extensions(struct weston_compositor *ec)
 	if (weston_check_egl_extension(extensions, "EGL_EXT_stream_acquire_mode"))
 		gr->has_egl_stream_acquire_mode = true;
 
+	if (weston_check_egl_extension(extensions, "EGL_KHR_stream_consumer_gltexture"))
+		gr->has_egl_stream_consumer_gltexture = true;
+
+	if (weston_check_egl_extension(extensions, "EGL_WL_wayland_eglstream"))
+		gr->has_egl_wayland_eglstream = true;
+
 	renderer_setup_egl_client_extensions(gr);
 
 	return 0;
@@ -4047,6 +4221,16 @@ gl_renderer_display_create(struct weston_compositor *ec, EGLenum platform,
 			goto fail_terminate;
 		}
 
+		if (!gr->has_egl_stream_consumer_gltexture ||
+		    !gr->has_egl_wayland_eglstream)
+			weston_log("warning: following required extensions for "
+				"EGL client frame presentation through "
+				"EGLDevice not supported:\n%s%s",
+				(gr->has_egl_stream_consumer_gltexture ? "" :
+					"    EGL_KHR_stream_consumer_gltexture\n"),
+				(gr->has_egl_wayland_eglstream         ? "" :
+					"    EGL_WL_wayland_eglstream\n"));
+
 		if (!gr->has_egl_output_drm_flip_event)
 			weston_log("warning: EGL page flip event notification "
 				   "not supported\n");
diff --git a/shared/weston-egl-ext.h b/shared/weston-egl-ext.h
index f39990ed..96982e2d 100644
--- a/shared/weston-egl-ext.h
+++ b/shared/weston-egl-ext.h
@@ -243,6 +243,11 @@ EGLAPI EGLBoolean EGLAPIENTRY eglStreamConsumerAcquireAttribEXT (EGLDisplay dpy,
 #define EGL_DRM_FLIP_EVENT_DATA_NV            0x333E
 #endif /* EGL_NV_output_drm_flip_event */
 
+#ifndef EGL_WL_wayland_eglstream
+#define EGL_WL_wayland_eglstream 1
+#define EGL_WAYLAND_EGLSTREAM_WL              0x334B
+#endif /* EGL_WL_wayland_eglstream */
+
 #else /* ENABLE_EGL */
 
 /* EGL platform definition are keept to allow compositor-xx.c to build */
-- 
2.21.0