blob: 244ce30999c94543318e38bd8021f834fe92daff [file] [log] [blame]
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001/*
2 * Copyright © 2012 Intel Corporation
Pekka Paalaneneb35cbe2015-02-09 13:37:27 +02003 * Copyright © 2015 Collabora, Ltd.
Miguel A. Vico967b6bc2016-05-18 17:50:54 +02004 * Copyright © 2016 NVIDIA Corporation
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04005 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07006 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
Kristian Høgsbergd7c17262012-09-05 21:54:15 -040013 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070014 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial
16 * portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
Kristian Høgsbergd7c17262012-09-05 21:54:15 -040026 */
27
Daniel Stonec228e232013-05-22 18:03:19 +030028#include "config.h"
Kristian Høgsberg25894fc2012-09-05 22:06:26 -040029
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010030#include <GLES2/gl2.h>
31#include <GLES2/gl2ext.h>
32
Derek Foremanf8180982014-10-16 16:37:02 -050033#include <stdbool.h>
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030034#include <stdint.h>
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -040035#include <stdlib.h>
Kristian Høgsberg25894fc2012-09-05 22:06:26 -040036#include <string.h>
37#include <ctype.h>
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +030038#include <float.h>
39#include <assert.h>
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +020040#include <linux/input.h>
Pekka Paalanena3525802014-06-12 16:49:29 +030041#include <drm_fourcc.h>
Alexandros Frantzisdf0e4b92017-09-27 15:09:16 +030042#include <unistd.h>
43#include <sys/ioctl.h>
44
45#ifdef HAVE_LINUX_SYNC_FILE_H
46#include <linux/sync_file.h>
47#else
48#include "weston-sync-file.h"
49#endif
50
51#include "timeline.h"
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -040052
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010053#include "gl-renderer.h"
Sam Spilsbury619859c2013-09-13 10:01:21 +080054#include "vertex-clipping.h"
Pekka Paalanena3525802014-06-12 16:49:29 +030055#include "linux-dmabuf.h"
Jonas Ådahl57e48f02015-11-17 16:00:28 +080056#include "linux-dmabuf-unstable-v1-server-protocol.h"
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010057
Jon Cruz35b2eaa2015-06-15 15:37:08 -070058#include "shared/helpers.h"
Emil Velikovf0c3a1c2016-07-04 15:34:18 +010059#include "shared/platform.h"
Alexandros Frantzisdf0e4b92017-09-27 15:09:16 +030060#include "shared/timespec-util.h"
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010061#include "weston-egl-ext.h"
Kristian Høgsbergd7c17262012-09-05 21:54:15 -040062
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010063struct gl_shader {
John Kåre Alsaker40684142012-11-13 19:10:25 +010064 GLuint program;
65 GLuint vertex_shader, fragment_shader;
66 GLint proj_uniform;
67 GLint tex_uniforms[3];
68 GLint alpha_uniform;
69 GLint color_uniform;
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +030070 const char *vertex_source, *fragment_source;
John Kåre Alsaker40684142012-11-13 19:10:25 +010071};
72
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +020073#define BUFFER_DAMAGE_COUNT 2
74
Jason Ekstrande5512d42014-02-04 21:36:38 -060075enum gl_border_status {
76 BORDER_STATUS_CLEAN = 0,
77 BORDER_TOP_DIRTY = 1 << GL_RENDERER_BORDER_TOP,
78 BORDER_LEFT_DIRTY = 1 << GL_RENDERER_BORDER_LEFT,
79 BORDER_RIGHT_DIRTY = 1 << GL_RENDERER_BORDER_RIGHT,
80 BORDER_BOTTOM_DIRTY = 1 << GL_RENDERER_BORDER_BOTTOM,
81 BORDER_ALL_DIRTY = 0xf,
82 BORDER_SIZE_CHANGED = 0x10
83};
84
Jason Ekstrand0b61bf42013-10-27 22:24:54 -050085struct gl_border_image {
86 GLuint tex;
87 int32_t width, height;
88 int32_t tex_width;
Jason Ekstrand0b61bf42013-10-27 22:24:54 -050089 void *data;
90};
91
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010092struct gl_output_state {
John Kåre Alsaker94659272012-11-13 19:10:18 +010093 EGLSurface egl_surface;
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +020094 pixman_region32_t buffer_damage[BUFFER_DAMAGE_COUNT];
Derek Foreman4c582662014-10-09 18:39:44 -050095 int buffer_damage_index;
Jason Ekstrande5512d42014-02-04 21:36:38 -060096 enum gl_border_status border_damage[BUFFER_DAMAGE_COUNT];
Jason Ekstrand0b61bf42013-10-27 22:24:54 -050097 struct gl_border_image borders[4];
Jason Ekstrande5512d42014-02-04 21:36:38 -060098 enum gl_border_status border_status;
Jason Ekstrandfb23df72014-10-16 10:55:21 -050099
100 struct weston_matrix output_matrix;
Alexandros Frantzisdf0e4b92017-09-27 15:09:16 +0300101
102 /* struct timeline_render_point::link */
103 struct wl_list timeline_render_point_list;
John Kåre Alsaker94659272012-11-13 19:10:18 +0100104};
105
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +0300106enum buffer_type {
107 BUFFER_TYPE_NULL,
Pekka Paalanenaeb917e2015-02-09 13:56:56 +0200108 BUFFER_TYPE_SOLID, /* internal solid color surfaces without a buffer */
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +0300109 BUFFER_TYPE_SHM,
110 BUFFER_TYPE_EGL
111};
112
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +0300113struct gl_renderer;
114
115struct egl_image {
116 struct gl_renderer *renderer;
117 EGLImageKHR image;
118 int refcount;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +0000119};
Pekka Paalanena3525802014-06-12 16:49:29 +0300120
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +0000121enum import_type {
122 IMPORT_TYPE_INVALID,
123 IMPORT_TYPE_DIRECT,
124 IMPORT_TYPE_GL_CONVERSION
125};
126
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +0000127struct dmabuf_image {
Pekka Paalanena3525802014-06-12 16:49:29 +0300128 struct linux_dmabuf_buffer *dmabuf;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +0000129 int num_images;
130 struct egl_image *images[3];
Pekka Paalanena3525802014-06-12 16:49:29 +0300131 struct wl_list link;
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +0000132
133 enum import_type import_type;
134 GLenum target;
135 struct gl_shader *shader;
136};
137
138struct yuv_plane_descriptor {
139 int width_divisor;
140 int height_divisor;
141 uint32_t format;
142 int plane_index;
143};
144
145struct yuv_format_descriptor {
146 uint32_t format;
147 int input_planes;
148 int output_planes;
149 int texture_type;
150 struct yuv_plane_descriptor plane[4];
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +0300151};
152
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100153struct gl_surface_state {
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100154 GLfloat color[4];
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100155 struct gl_shader *shader;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100156
157 GLuint textures[3];
158 int num_textures;
Derek Foreman4c11fe72015-11-18 16:32:27 -0600159 bool needs_full_upload;
Pekka Paalanen81ee3f52012-12-04 15:58:16 +0200160 pixman_region32_t texture_damage;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100161
Neil Roberts4d085e72014-04-07 15:01:01 +0100162 /* These are only used by SHM surfaces to detect when we need
163 * to do a full upload to specify a new internal texture
164 * format */
Vincent Abriou00a03d22016-10-05 14:54:35 +0200165 GLenum gl_format[3];
Neil Roberts4d085e72014-04-07 15:01:01 +0100166 GLenum gl_pixel_type;
167
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +0300168 struct egl_image* images[3];
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100169 GLenum target;
170 int num_images;
Pekka Paalanenfb003d32012-12-04 15:58:13 +0200171
172 struct weston_buffer_reference buffer_ref;
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +0300173 enum buffer_type buffer_type;
Pekka Paalanen68033ac2012-12-04 15:58:15 +0200174 int pitch; /* in pixels */
Alexander Larsson4ea95522013-05-22 14:41:37 +0200175 int height; /* in pixels */
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +0400176 int y_inverted;
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300177
Vincent Abrioufdeefe42016-10-05 14:54:34 +0200178 /* Extension needed for SHM YUV texture */
179 int offset[3]; /* offset per plane */
Vincent ABRIOUe5732c72016-10-20 16:20:11 +0200180 int hsub[3]; /* horizontal subsampling per plane */
181 int vsub[3]; /* vertical subsampling per plane */
Vincent Abrioufdeefe42016-10-05 14:54:34 +0200182
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300183 struct weston_surface *surface;
184
185 struct wl_listener surface_destroy_listener;
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +0300186 struct wl_listener renderer_destroy_listener;
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100187};
188
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100189struct gl_renderer {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100190 struct weston_renderer base;
191 int fragment_shader_debug;
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400192 int fan_debug;
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +0300193 struct weston_binding *fragment_binding;
194 struct weston_binding *fan_binding;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100195
196 EGLDisplay egl_display;
197 EGLContext egl_context;
198 EGLConfig egl_config;
John Kåre Alsaker44154502012-11-13 19:10:20 +0100199
Armin Krezović28d240f2016-06-23 11:59:35 +0200200 EGLSurface dummy_surface;
201
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400202 struct wl_array vertices;
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400203 struct wl_array vtxcnt;
204
John Kåre Alsaker320711d2012-11-13 19:10:27 +0100205 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
206 PFNEGLCREATEIMAGEKHRPROC create_image;
207 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -0600208 PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC swap_buffers_with_damage;
Jonny Lamb671148f2015-03-20 15:26:52 +0100209 PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC create_platform_window;
Jonny Lamb671148f2015-03-20 15:26:52 +0100210
John Kåre Alsaker320711d2012-11-13 19:10:27 +0100211 int has_unpack_subimage;
212
213 PFNEGLBINDWAYLANDDISPLAYWL bind_display;
214 PFNEGLUNBINDWAYLANDDISPLAYWL unbind_display;
215 PFNEGLQUERYWAYLANDBUFFERWL query_buffer;
216 int has_bind_display;
217
218 int has_egl_image_external;
219
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +0200220 int has_egl_buffer_age;
221
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000222 int has_configless_context;
223
Armin Krezović28d240f2016-06-23 11:59:35 +0200224 int has_surfaceless_context;
225
Pekka Paalanena3525802014-06-12 16:49:29 +0300226 int has_dmabuf_import;
227 struct wl_list dmabuf_images;
228
Vincent Abrioufdeefe42016-10-05 14:54:34 +0200229 int has_gl_texture_rg;
230
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100231 struct gl_shader texture_shader_rgba;
232 struct gl_shader texture_shader_rgbx;
233 struct gl_shader texture_shader_egl_external;
234 struct gl_shader texture_shader_y_uv;
235 struct gl_shader texture_shader_y_u_v;
236 struct gl_shader texture_shader_y_xuxv;
237 struct gl_shader invert_color_shader;
238 struct gl_shader solid_shader;
239 struct gl_shader *current_shader;
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +0300240
241 struct wl_signal destroy_signal;
Armin Krezović36d699a2016-08-05 15:28:30 +0200242
243 struct wl_listener output_destroy_listener;
Varad Gautam0775cd12016-11-23 14:03:20 +0530244
245 int has_dmabuf_import_modifiers;
246 PFNEGLQUERYDMABUFFORMATSEXTPROC query_dmabuf_formats;
247 PFNEGLQUERYDMABUFMODIFIERSEXTPROC query_dmabuf_modifiers;
Alexandros Frantzis7192b172017-09-27 15:09:14 +0300248
249 int has_native_fence_sync;
250 PFNEGLCREATESYNCKHRPROC create_sync;
251 PFNEGLDESTROYSYNCKHRPROC destroy_sync;
252 PFNEGLDUPNATIVEFENCEFDANDROIDPROC dup_native_fence_fd;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100253};
John Kåre Alsaker94659272012-11-13 19:10:18 +0100254
Alexandros Frantzisdf0e4b92017-09-27 15:09:16 +0300255enum timeline_render_point_type {
256 TIMELINE_RENDER_POINT_TYPE_BEGIN,
257 TIMELINE_RENDER_POINT_TYPE_END
258};
259
260struct timeline_render_point {
261 struct wl_list link; /* gl_output_state::timeline_render_point_list */
262
263 enum timeline_render_point_type type;
264 int fd;
265 struct weston_output *output;
266 struct wl_event_source *event_source;
267};
268
Jonny Lamb70eba3f2015-03-20 15:26:50 +0100269static PFNEGLGETPLATFORMDISPLAYEXTPROC get_platform_display = NULL;
Jonny Lamb70eba3f2015-03-20 15:26:50 +0100270
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +0000271static inline const char *
272dump_format(uint32_t format, char out[4])
273{
274#if BYTE_ORDER == BIG_ENDIAN
275 format = __builtin_bswap32(format);
276#endif
277 memcpy(out, &format, 4);
278 return out;
279}
280
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100281static inline struct gl_output_state *
John Kåre Alsaker94659272012-11-13 19:10:18 +0100282get_output_state(struct weston_output *output)
283{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100284 return (struct gl_output_state *)output->renderer_state;
John Kåre Alsaker94659272012-11-13 19:10:18 +0100285}
286
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300287static int
288gl_renderer_create_surface(struct weston_surface *surface);
289
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100290static inline struct gl_surface_state *
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100291get_surface_state(struct weston_surface *surface)
292{
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300293 if (!surface->renderer_state)
294 gl_renderer_create_surface(surface);
295
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100296 return (struct gl_surface_state *)surface->renderer_state;
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100297}
298
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100299static inline struct gl_renderer *
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100300get_renderer(struct weston_compositor *ec)
301{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100302 return (struct gl_renderer *)ec->renderer;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100303}
304
Alexandros Frantzisdf0e4b92017-09-27 15:09:16 +0300305static int
306linux_sync_file_read_timestamp(int fd, uint64_t *ts)
307{
308 struct sync_file_info file_info = { { 0 } };
309 struct sync_fence_info fence_info = { { 0 } };
310
311 assert(ts != NULL);
312
313 file_info.sync_fence_info = (uint64_t)(uintptr_t)&fence_info;
314 file_info.num_fences = 1;
315
316 if (ioctl(fd, SYNC_IOC_FILE_INFO, &file_info) < 0)
317 return -1;
318
319 *ts = fence_info.timestamp_ns;
320
321 return 0;
322}
323
324static void
325timeline_render_point_destroy(struct timeline_render_point *trp)
326{
327 wl_list_remove(&trp->link);
328 wl_event_source_remove(trp->event_source);
329 close(trp->fd);
330 free(trp);
331}
332
333static int
334timeline_render_point_handler(int fd, uint32_t mask, void *data)
335{
336 struct timeline_render_point *trp = data;
337 const char *tp_name = trp->type == TIMELINE_RENDER_POINT_TYPE_BEGIN ?
338 "renderer_gpu_begin" : "renderer_gpu_end";
339
340 if (mask & WL_EVENT_READABLE) {
341 uint64_t ts;
342
343 if (linux_sync_file_read_timestamp(trp->fd, &ts) == 0) {
344 struct timespec tspec = { 0 };
345
346 timespec_add_nsec(&tspec, &tspec, ts);
347
348 TL_POINT(tp_name, TLP_GPU(&tspec),
349 TLP_OUTPUT(trp->output), TLP_END);
350 }
351 }
352
353 timeline_render_point_destroy(trp);
354
355 return 0;
356}
357
358static EGLSyncKHR
359timeline_create_render_sync(struct gl_renderer *gr)
360{
361 static const EGLint attribs[] = { EGL_NONE };
362
363 if (!weston_timeline_enabled_ || !gr->has_native_fence_sync)
364 return EGL_NO_SYNC_KHR;
365
366 return gr->create_sync(gr->egl_display, EGL_SYNC_NATIVE_FENCE_ANDROID,
367 attribs);
368}
369
370static void
371timeline_submit_render_sync(struct gl_renderer *gr,
372 struct weston_compositor *ec,
373 struct weston_output *output,
374 EGLSyncKHR sync,
375 enum timeline_render_point_type type)
376{
377 struct gl_output_state *go;
378 struct wl_event_loop *loop;
379 int fd;
380 struct timeline_render_point *trp;
381
382 if (!weston_timeline_enabled_ ||
383 !gr->has_native_fence_sync ||
384 sync == EGL_NO_SYNC_KHR)
385 return;
386
387 go = get_output_state(output);
388 loop = wl_display_get_event_loop(ec->wl_display);
389
390 fd = gr->dup_native_fence_fd(gr->egl_display, sync);
391 if (fd == EGL_NO_NATIVE_FENCE_FD_ANDROID)
392 goto out;
393
394 trp = zalloc(sizeof *trp);
395 if (trp == NULL) {
396 close(fd);
397 goto out;
398 }
399
400 trp->type = type;
401 trp->fd = fd;
402 trp->output = output;
403 trp->event_source = wl_event_loop_add_fd(loop, fd,
404 WL_EVENT_READABLE,
405 timeline_render_point_handler,
406 trp);
407
408 wl_list_insert(&go->timeline_render_point_list, &trp->link);
409
410out:
411 gr->destroy_sync(gr->egl_display, sync);
412}
413
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +0300414static struct egl_image*
415egl_image_create(struct gl_renderer *gr, EGLenum target,
416 EGLClientBuffer buffer, const EGLint *attribs)
417{
418 struct egl_image *img;
419
420 img = zalloc(sizeof *img);
421 img->renderer = gr;
422 img->refcount = 1;
423 img->image = gr->create_image(gr->egl_display, EGL_NO_CONTEXT,
424 target, buffer, attribs);
425
426 if (img->image == EGL_NO_IMAGE_KHR) {
427 free(img);
428 return NULL;
429 }
430
431 return img;
432}
433
434static struct egl_image*
435egl_image_ref(struct egl_image *image)
436{
437 image->refcount++;
438
439 return image;
440}
441
442static int
443egl_image_unref(struct egl_image *image)
444{
445 struct gl_renderer *gr = image->renderer;
446
447 assert(image->refcount > 0);
448
449 image->refcount--;
450 if (image->refcount > 0)
451 return image->refcount;
452
453 gr->destroy_image(gr->egl_display, image->image);
454 free(image);
455
456 return 0;
457}
458
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +0000459static struct dmabuf_image*
460dmabuf_image_create(void)
461{
462 struct dmabuf_image *img;
463
464 img = zalloc(sizeof *img);
465 wl_list_init(&img->link);
466
467 return img;
468}
469
470static void
471dmabuf_image_destroy(struct dmabuf_image *image)
472{
473 int i;
474
475 for (i = 0; i < image->num_images; ++i)
476 egl_image_unref(image->images[i]);
477
478 if (image->dmabuf)
479 linux_dmabuf_buffer_set_user_data(image->dmabuf, NULL, NULL);
480
481 wl_list_remove(&image->link);
Arnaud Vrac7b5fe9b2017-08-05 13:58:58 +0200482 free(image);
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +0000483}
484
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400485static const char *
486egl_error_string(EGLint code)
487{
488#define MYERRCODE(x) case x: return #x;
489 switch (code) {
490 MYERRCODE(EGL_SUCCESS)
491 MYERRCODE(EGL_NOT_INITIALIZED)
492 MYERRCODE(EGL_BAD_ACCESS)
493 MYERRCODE(EGL_BAD_ALLOC)
494 MYERRCODE(EGL_BAD_ATTRIBUTE)
495 MYERRCODE(EGL_BAD_CONTEXT)
496 MYERRCODE(EGL_BAD_CONFIG)
497 MYERRCODE(EGL_BAD_CURRENT_SURFACE)
498 MYERRCODE(EGL_BAD_DISPLAY)
499 MYERRCODE(EGL_BAD_SURFACE)
500 MYERRCODE(EGL_BAD_MATCH)
501 MYERRCODE(EGL_BAD_PARAMETER)
502 MYERRCODE(EGL_BAD_NATIVE_PIXMAP)
503 MYERRCODE(EGL_BAD_NATIVE_WINDOW)
504 MYERRCODE(EGL_CONTEXT_LOST)
505 default:
506 return "unknown";
507 }
508#undef MYERRCODE
509}
510
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300511static void
Pekka Paalanen326529f2012-11-27 12:25:25 +0200512gl_renderer_print_egl_error_state(void)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400513{
514 EGLint code;
515
516 code = eglGetError();
517 weston_log("EGL error state: %s (0x%04lx)\n",
518 egl_error_string(code), (long)code);
519}
520
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400521#define max(a, b) (((a) > (b)) ? (a) : (b))
522#define min(a, b) (((a) > (b)) ? (b) : (a))
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400523
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300524/*
525 * Compute the boundary vertices of the intersection of the global coordinate
526 * aligned rectangle 'rect', and an arbitrary quadrilateral produced from
527 * 'surf_rect' when transformed from surface coordinates into global coordinates.
528 * The vertices are written to 'ex' and 'ey', and the return value is the
529 * number of vertices. Vertices are produced in clockwise winding order.
530 * Guarantees to produce either zero vertices, or 3-8 vertices with non-zero
531 * polygon area.
532 */
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400533static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500534calculate_edges(struct weston_view *ev, pixman_box32_t *rect,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400535 pixman_box32_t *surf_rect, GLfloat *ex, GLfloat *ey)
536{
Sam Spilsbury619859c2013-09-13 10:01:21 +0800537
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300538 struct clip_context ctx;
539 int i, n;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400540 GLfloat min_x, max_x, min_y, max_y;
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300541 struct polygon8 surf = {
542 { surf_rect->x1, surf_rect->x2, surf_rect->x2, surf_rect->x1 },
543 { surf_rect->y1, surf_rect->y1, surf_rect->y2, surf_rect->y2 },
544 4
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400545 };
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400546
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300547 ctx.clip.x1 = rect->x1;
548 ctx.clip.y1 = rect->y1;
549 ctx.clip.x2 = rect->x2;
550 ctx.clip.y2 = rect->y2;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400551
552 /* transform surface to screen space: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300553 for (i = 0; i < surf.n; i++)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500554 weston_view_to_global_float(ev, surf.x[i], surf.y[i],
555 &surf.x[i], &surf.y[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400556
557 /* find bounding box: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300558 min_x = max_x = surf.x[0];
559 min_y = max_y = surf.y[0];
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400560
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300561 for (i = 1; i < surf.n; i++) {
562 min_x = min(min_x, surf.x[i]);
563 max_x = max(max_x, surf.x[i]);
564 min_y = min(min_y, surf.y[i]);
565 max_y = max(max_y, surf.y[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400566 }
567
568 /* First, simple bounding box check to discard early transformed
569 * surface rects that do not intersect with the clip region:
570 */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300571 if ((min_x >= ctx.clip.x2) || (max_x <= ctx.clip.x1) ||
572 (min_y >= ctx.clip.y2) || (max_y <= ctx.clip.y1))
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400573 return 0;
574
575 /* Simple case, bounding box edges are parallel to surface edges,
576 * there will be only four edges. We just need to clip the surface
577 * vertices to the clip rect bounds:
578 */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500579 if (!ev->transform.enabled)
Sam Spilsbury619859c2013-09-13 10:01:21 +0800580 return clip_simple(&ctx, &surf, ex, ey);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400581
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300582 /* Transformed case: use a general polygon clipping algorithm to
583 * clip the surface rectangle with each side of 'rect'.
584 * The algorithm is Sutherland-Hodgman, as explained in
585 * http://www.codeguru.com/cpp/misc/misc/graphics/article.php/c8965/Polygon-Clipping.htm
586 * but without looking at any of that code.
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400587 */
Sam Spilsbury619859c2013-09-13 10:01:21 +0800588 n = clip_transformed(&ctx, &surf, ex, ey);
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300589
590 if (n < 3)
591 return 0;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400592
593 return n;
594}
595
Derek Foremanf8180982014-10-16 16:37:02 -0500596static bool
597merge_down(pixman_box32_t *a, pixman_box32_t *b, pixman_box32_t *merge)
598{
599 if (a->x1 == b->x1 && a->x2 == b->x2 && a->y1 == b->y2) {
600 merge->x1 = a->x1;
601 merge->x2 = a->x2;
602 merge->y1 = b->y1;
603 merge->y2 = a->y2;
604 return true;
605 }
606 return false;
607}
608
609static int
610compress_bands(pixman_box32_t *inrects, int nrects,
611 pixman_box32_t **outrects)
612{
Quentin Glidicd84af7c2016-07-10 11:00:50 +0200613 bool merged = false;
Derek Foremanf8180982014-10-16 16:37:02 -0500614 pixman_box32_t *out, merge_rect;
615 int i, j, nout;
616
617 if (!nrects) {
618 *outrects = NULL;
619 return 0;
620 }
621
622 /* nrects is an upper bound - we're not too worried about
623 * allocating a little extra
624 */
625 out = malloc(sizeof(pixman_box32_t) * nrects);
626 out[0] = inrects[0];
627 nout = 1;
628 for (i = 1; i < nrects; i++) {
629 for (j = 0; j < nout; j++) {
630 merged = merge_down(&inrects[i], &out[j], &merge_rect);
631 if (merged) {
632 out[j] = merge_rect;
633 break;
634 }
635 }
636 if (!merged) {
637 out[nout] = inrects[i];
638 nout++;
639 }
640 }
641 *outrects = out;
642 return nout;
643}
644
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400645static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500646texture_region(struct weston_view *ev, pixman_region32_t *region,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400647 pixman_region32_t *surf_region)
648{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500649 struct gl_surface_state *gs = get_surface_state(ev->surface);
650 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400651 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400652 GLfloat *v, inv_width, inv_height;
653 unsigned int *vtxcnt, nvtx = 0;
654 pixman_box32_t *rects, *surf_rects;
Derek Foremanf8180982014-10-16 16:37:02 -0500655 pixman_box32_t *raw_rects;
656 int i, j, k, nrects, nsurf, raw_nrects;
657 bool used_band_compression;
658 raw_rects = pixman_region32_rectangles(region, &raw_nrects);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400659 surf_rects = pixman_region32_rectangles(surf_region, &nsurf);
660
Derek Foremanf8180982014-10-16 16:37:02 -0500661 if (raw_nrects < 4) {
662 used_band_compression = false;
663 nrects = raw_nrects;
664 rects = raw_rects;
665 } else {
666 nrects = compress_bands(raw_rects, raw_nrects, &rects);
667 used_band_compression = true;
668 }
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400669 /* worst case we can have 8 vertices per rect (ie. clipped into
670 * an octagon):
671 */
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400672 v = wl_array_add(&gr->vertices, nrects * nsurf * 8 * 4 * sizeof *v);
673 vtxcnt = wl_array_add(&gr->vtxcnt, nrects * nsurf * sizeof *vtxcnt);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400674
Pekka Paalanen68033ac2012-12-04 15:58:15 +0200675 inv_width = 1.0 / gs->pitch;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200676 inv_height = 1.0 / gs->height;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400677
678 for (i = 0; i < nrects; i++) {
679 pixman_box32_t *rect = &rects[i];
680 for (j = 0; j < nsurf; j++) {
681 pixman_box32_t *surf_rect = &surf_rects[j];
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200682 GLfloat sx, sy, bx, by;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400683 GLfloat ex[8], ey[8]; /* edge points in screen space */
684 int n;
685
686 /* The transformed surface, after clipping to the clip region,
687 * can have as many as eight sides, emitted as a triangle-fan.
688 * The first vertex in the triangle fan can be chosen arbitrarily,
689 * since the area is guaranteed to be convex.
690 *
691 * If a corner of the transformed surface falls outside of the
692 * clip region, instead of emitting one vertex for the corner
693 * of the surface, up to two are emitted for two corresponding
694 * intersection point(s) between the surface and the clip region.
695 *
696 * To do this, we first calculate the (up to eight) points that
697 * form the intersection of the clip rect and the transformed
698 * surface.
699 */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500700 n = calculate_edges(ev, rect, surf_rect, ex, ey);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400701 if (n < 3)
702 continue;
703
704 /* emit edge points: */
705 for (k = 0; k < n; k++) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500706 weston_view_from_global_float(ev, ex[k], ey[k],
707 &sx, &sy);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400708 /* position: */
709 *(v++) = ex[k];
710 *(v++) = ey[k];
711 /* texcoord: */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500712 weston_surface_to_buffer_float(ev->surface,
713 sx, sy,
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200714 &bx, &by);
715 *(v++) = bx * inv_width;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +0400716 if (gs->y_inverted) {
717 *(v++) = by * inv_height;
718 } else {
719 *(v++) = (gs->height - by) * inv_height;
720 }
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400721 }
722
723 vtxcnt[nvtx++] = n;
724 }
725 }
726
Derek Foremanf8180982014-10-16 16:37:02 -0500727 if (used_band_compression)
728 free(rects);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400729 return nvtx;
730}
731
732static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500733triangle_fan_debug(struct weston_view *view, int first, int count)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400734{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500735 struct weston_compositor *compositor = view->surface->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100736 struct gl_renderer *gr = get_renderer(compositor);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400737 int i;
738 GLushort *buffer;
739 GLushort *index;
740 int nelems;
741 static int color_idx = 0;
742 static const GLfloat color[][4] = {
743 { 1.0, 0.0, 0.0, 1.0 },
744 { 0.0, 1.0, 0.0, 1.0 },
745 { 0.0, 0.0, 1.0, 1.0 },
746 { 1.0, 1.0, 1.0, 1.0 },
747 };
748
749 nelems = (count - 1 + count - 2) * 2;
750
751 buffer = malloc(sizeof(GLushort) * nelems);
752 index = buffer;
753
754 for (i = 1; i < count; i++) {
755 *index++ = first;
756 *index++ = first + i;
757 }
758
759 for (i = 2; i < count; i++) {
760 *index++ = first + i - 1;
761 *index++ = first + i;
762 }
763
John Kåre Alsaker40684142012-11-13 19:10:25 +0100764 glUseProgram(gr->solid_shader.program);
765 glUniform4fv(gr->solid_shader.color_uniform, 1,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400766 color[color_idx++ % ARRAY_LENGTH(color)]);
767 glDrawElements(GL_LINES, nelems, GL_UNSIGNED_SHORT, buffer);
John Kåre Alsaker40684142012-11-13 19:10:25 +0100768 glUseProgram(gr->current_shader->program);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400769 free(buffer);
770}
771
772static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500773repaint_region(struct weston_view *ev, pixman_region32_t *region,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400774 pixman_region32_t *surf_region)
775{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500776 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400777 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400778 GLfloat *v;
779 unsigned int *vtxcnt;
780 int i, first, nfans;
781
782 /* The final region to be painted is the intersection of
783 * 'region' and 'surf_region'. However, 'region' is in the global
784 * coordinates, and 'surf_region' is in the surface-local
785 * coordinates. texture_region() will iterate over all pairs of
786 * rectangles from both regions, compute the intersection
787 * polygon for each pair, and store it as a triangle fan if
Bryce Harringtonea8fb942016-01-13 18:48:56 -0800788 * it has a non-zero area (at least 3 vertices, actually).
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400789 */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500790 nfans = texture_region(ev, region, surf_region);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400791
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400792 v = gr->vertices.data;
793 vtxcnt = gr->vtxcnt.data;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400794
795 /* position: */
796 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
797 glEnableVertexAttribArray(0);
798
799 /* texcoord: */
800 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
801 glEnableVertexAttribArray(1);
802
803 for (i = 0, first = 0; i < nfans; i++) {
804 glDrawArrays(GL_TRIANGLE_FAN, first, vtxcnt[i]);
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400805 if (gr->fan_debug)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500806 triangle_fan_debug(ev, first, vtxcnt[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400807 first += vtxcnt[i];
808 }
809
810 glDisableVertexAttribArray(1);
811 glDisableVertexAttribArray(0);
812
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400813 gr->vertices.size = 0;
814 gr->vtxcnt.size = 0;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400815}
816
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100817static int
818use_output(struct weston_output *output)
819{
820 static int errored;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100821 struct gl_output_state *go = get_output_state(output);
822 struct gl_renderer *gr = get_renderer(output->compositor);
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100823 EGLBoolean ret;
824
825 ret = eglMakeCurrent(gr->egl_display, go->egl_surface,
826 go->egl_surface, gr->egl_context);
827
828 if (ret == EGL_FALSE) {
829 if (errored)
830 return -1;
831 errored = 1;
832 weston_log("Failed to make EGL context current.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +0200833 gl_renderer_print_egl_error_state();
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100834 return -1;
835 }
836
837 return 0;
838}
839
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +0300840static int
841shader_init(struct gl_shader *shader, struct gl_renderer *gr,
842 const char *vertex_source, const char *fragment_source);
843
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400844static void
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +0300845use_shader(struct gl_renderer *gr, struct gl_shader *shader)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400846{
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +0300847 if (!shader->program) {
848 int ret;
849
850 ret = shader_init(shader, gr,
851 shader->vertex_source,
852 shader->fragment_source);
853
854 if (ret < 0)
855 weston_log("warning: failed to compile shader\n");
856 }
857
John Kåre Alsaker40684142012-11-13 19:10:25 +0100858 if (gr->current_shader == shader)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400859 return;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400860 glUseProgram(shader->program);
John Kåre Alsaker40684142012-11-13 19:10:25 +0100861 gr->current_shader = shader;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400862}
863
864static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100865shader_uniforms(struct gl_shader *shader,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500866 struct weston_view *view,
867 struct weston_output *output)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400868{
869 int i;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500870 struct gl_surface_state *gs = get_surface_state(view->surface);
Jason Ekstrandfb23df72014-10-16 10:55:21 -0500871 struct gl_output_state *go = get_output_state(output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400872
873 glUniformMatrix4fv(shader->proj_uniform,
Jason Ekstrandfb23df72014-10-16 10:55:21 -0500874 1, GL_FALSE, go->output_matrix.d);
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100875 glUniform4fv(shader->color_uniform, 1, gs->color);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500876 glUniform1f(shader->alpha_uniform, view->alpha);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400877
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100878 for (i = 0; i < gs->num_textures; i++)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400879 glUniform1i(shader->tex_uniforms[i], i);
880}
881
882static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500883draw_view(struct weston_view *ev, struct weston_output *output,
884 pixman_region32_t *damage) /* in global coordinates */
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400885{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500886 struct weston_compositor *ec = ev->surface->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100887 struct gl_renderer *gr = get_renderer(ec);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500888 struct gl_surface_state *gs = get_surface_state(ev->surface);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400889 /* repaint bounding region in global coordinates: */
890 pixman_region32_t repaint;
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +0200891 /* opaque region in surface coordinates: */
892 pixman_region32_t surface_opaque;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400893 /* non-opaque region in surface coordinates: */
894 pixman_region32_t surface_blend;
895 GLint filter;
896 int i;
897
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +0200898 /* In case of a runtime switch of renderers, we may not have received
899 * an attach for this surface since the switch. In that case we don't
900 * have a valid buffer or a proper shader set up so skip rendering. */
901 if (!gs->shader)
902 return;
903
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400904 pixman_region32_init(&repaint);
905 pixman_region32_intersect(&repaint,
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200906 &ev->transform.boundingbox, damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500907 pixman_region32_subtract(&repaint, &repaint, &ev->clip);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400908
909 if (!pixman_region32_not_empty(&repaint))
910 goto out;
911
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400912 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
913
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400914 if (gr->fan_debug) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100915 use_shader(gr, &gr->solid_shader);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500916 shader_uniforms(&gr->solid_shader, ev, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400917 }
918
John Kåre Alsaker40684142012-11-13 19:10:25 +0100919 use_shader(gr, gs->shader);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500920 shader_uniforms(gs->shader, ev, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400921
Jason Ekstranda7af7042013-10-12 22:38:11 -0500922 if (ev->transform.enabled || output->zoom.active ||
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200923 output->current_scale != ev->surface->buffer_viewport.buffer.scale)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400924 filter = GL_LINEAR;
925 else
926 filter = GL_NEAREST;
927
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100928 for (i = 0; i < gs->num_textures; i++) {
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400929 glActiveTexture(GL_TEXTURE0 + i);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100930 glBindTexture(gs->target, gs->textures[i]);
931 glTexParameteri(gs->target, GL_TEXTURE_MIN_FILTER, filter);
932 glTexParameteri(gs->target, GL_TEXTURE_MAG_FILTER, filter);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400933 }
934
935 /* blended region is whole surface minus opaque region: */
936 pixman_region32_init_rect(&surface_blend, 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600937 ev->surface->width, ev->surface->height);
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +0200938 if (ev->geometry.scissor_enabled)
939 pixman_region32_intersect(&surface_blend, &surface_blend,
940 &ev->geometry.scissor);
941 pixman_region32_subtract(&surface_blend, &surface_blend,
942 &ev->surface->opaque);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400943
Jason Ekstranda7af7042013-10-12 22:38:11 -0500944 /* XXX: Should we be using ev->transform.opaque here? */
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +0200945 pixman_region32_init(&surface_opaque);
946 if (ev->geometry.scissor_enabled)
947 pixman_region32_intersect(&surface_opaque,
948 &ev->surface->opaque,
949 &ev->geometry.scissor);
950 else
951 pixman_region32_copy(&surface_opaque, &ev->surface->opaque);
952
953 if (pixman_region32_not_empty(&surface_opaque)) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100954 if (gs->shader == &gr->texture_shader_rgba) {
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400955 /* Special case for RGBA textures with possibly
956 * bad data in alpha channel: use the shader
957 * that forces texture alpha = 1.0.
958 * Xwayland surfaces need this.
959 */
John Kåre Alsaker40684142012-11-13 19:10:25 +0100960 use_shader(gr, &gr->texture_shader_rgbx);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500961 shader_uniforms(&gr->texture_shader_rgbx, ev, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400962 }
963
Jason Ekstranda7af7042013-10-12 22:38:11 -0500964 if (ev->alpha < 1.0)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400965 glEnable(GL_BLEND);
966 else
967 glDisable(GL_BLEND);
968
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +0200969 repaint_region(ev, &repaint, &surface_opaque);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400970 }
971
972 if (pixman_region32_not_empty(&surface_blend)) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100973 use_shader(gr, gs->shader);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400974 glEnable(GL_BLEND);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500975 repaint_region(ev, &repaint, &surface_blend);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400976 }
977
978 pixman_region32_fini(&surface_blend);
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +0200979 pixman_region32_fini(&surface_opaque);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400980
981out:
982 pixman_region32_fini(&repaint);
983}
984
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400985static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500986repaint_views(struct weston_output *output, pixman_region32_t *damage)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400987{
988 struct weston_compositor *compositor = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500989 struct weston_view *view;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400990
Jason Ekstranda7af7042013-10-12 22:38:11 -0500991 wl_list_for_each_reverse(view, &compositor->view_list, link)
992 if (view->plane == &compositor->primary_plane)
993 draw_view(view, output, damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400994}
995
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500996static void
Jason Ekstrande5512d42014-02-04 21:36:38 -0600997draw_output_border_texture(struct gl_output_state *go,
998 enum gl_renderer_border_side side,
999 int32_t x, int32_t y,
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05001000 int32_t width, int32_t height)
1001{
Jason Ekstrande5512d42014-02-04 21:36:38 -06001002 struct gl_border_image *img = &go->borders[side];
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05001003 static GLushort indices [] = { 0, 1, 3, 3, 1, 2 };
1004
1005 if (!img->data) {
1006 if (img->tex) {
1007 glDeleteTextures(1, &img->tex);
1008 img->tex = 0;
1009 }
1010
1011 return;
1012 }
1013
1014 if (!img->tex) {
1015 glGenTextures(1, &img->tex);
1016 glBindTexture(GL_TEXTURE_2D, img->tex);
1017
1018 glTexParameteri(GL_TEXTURE_2D,
1019 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1020 glTexParameteri(GL_TEXTURE_2D,
1021 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1022 glTexParameteri(GL_TEXTURE_2D,
1023 GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1024 glTexParameteri(GL_TEXTURE_2D,
1025 GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1026 } else {
1027 glBindTexture(GL_TEXTURE_2D, img->tex);
1028 }
1029
Jason Ekstrande5512d42014-02-04 21:36:38 -06001030 if (go->border_status & (1 << side)) {
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05001031 glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, 0);
1032 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, 0);
1033 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, 0);
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05001034 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1035 img->tex_width, img->height, 0,
1036 GL_BGRA_EXT, GL_UNSIGNED_BYTE, img->data);
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05001037 }
1038
1039 GLfloat texcoord[] = {
1040 0.0f, 0.0f,
1041 (GLfloat)img->width / (GLfloat)img->tex_width, 0.0f,
1042 (GLfloat)img->width / (GLfloat)img->tex_width, 1.0f,
1043 0.0f, 1.0f,
1044 };
1045
1046 GLfloat verts[] = {
1047 x, y,
1048 x + width, y,
1049 x + width, y + height,
1050 x, y + height
1051 };
1052
1053 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, verts);
1054 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, texcoord);
1055 glEnableVertexAttribArray(0);
1056 glEnableVertexAttribArray(1);
1057
1058 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
1059
1060 glDisableVertexAttribArray(1);
1061 glDisableVertexAttribArray(0);
1062}
1063
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -06001064static int
1065output_has_borders(struct weston_output *output)
1066{
1067 struct gl_output_state *go = get_output_state(output);
1068
1069 return go->borders[GL_RENDERER_BORDER_TOP].data ||
1070 go->borders[GL_RENDERER_BORDER_RIGHT].data ||
1071 go->borders[GL_RENDERER_BORDER_BOTTOM].data ||
1072 go->borders[GL_RENDERER_BORDER_LEFT].data;
1073}
1074
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05001075static void
Jason Ekstrande5512d42014-02-04 21:36:38 -06001076draw_output_borders(struct weston_output *output,
1077 enum gl_border_status border_status)
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05001078{
1079 struct gl_output_state *go = get_output_state(output);
1080 struct gl_renderer *gr = get_renderer(output->compositor);
1081 struct gl_shader *shader = &gr->texture_shader_rgba;
Jason Ekstrand00b84282013-10-27 22:24:59 -05001082 struct gl_border_image *top, *bottom, *left, *right;
1083 struct weston_matrix matrix;
1084 int full_width, full_height;
1085
Jason Ekstrande5512d42014-02-04 21:36:38 -06001086 if (border_status == BORDER_STATUS_CLEAN)
1087 return; /* Clean. Nothing to do. */
1088
Jason Ekstrand00b84282013-10-27 22:24:59 -05001089 top = &go->borders[GL_RENDERER_BORDER_TOP];
1090 bottom = &go->borders[GL_RENDERER_BORDER_BOTTOM];
1091 left = &go->borders[GL_RENDERER_BORDER_LEFT];
1092 right = &go->borders[GL_RENDERER_BORDER_RIGHT];
1093
1094 full_width = output->current_mode->width + left->width + right->width;
1095 full_height = output->current_mode->height + top->height + bottom->height;
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05001096
1097 glDisable(GL_BLEND);
1098 use_shader(gr, shader);
1099
Jason Ekstrand00b84282013-10-27 22:24:59 -05001100 glViewport(0, 0, full_width, full_height);
1101
1102 weston_matrix_init(&matrix);
1103 weston_matrix_translate(&matrix, -full_width/2.0, -full_height/2.0, 0);
1104 weston_matrix_scale(&matrix, 2.0/full_width, -2.0/full_height, 1);
1105 glUniformMatrix4fv(shader->proj_uniform, 1, GL_FALSE, matrix.d);
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05001106
1107 glUniform1i(shader->tex_uniforms[0], 0);
1108 glUniform1f(shader->alpha_uniform, 1);
1109 glActiveTexture(GL_TEXTURE0);
1110
Jason Ekstrande5512d42014-02-04 21:36:38 -06001111 if (border_status & BORDER_TOP_DIRTY)
1112 draw_output_border_texture(go, GL_RENDERER_BORDER_TOP,
1113 0, 0,
1114 full_width, top->height);
1115 if (border_status & BORDER_LEFT_DIRTY)
1116 draw_output_border_texture(go, GL_RENDERER_BORDER_LEFT,
1117 0, top->height,
1118 left->width, output->current_mode->height);
1119 if (border_status & BORDER_RIGHT_DIRTY)
1120 draw_output_border_texture(go, GL_RENDERER_BORDER_RIGHT,
1121 full_width - right->width, top->height,
1122 right->width, output->current_mode->height);
1123 if (border_status & BORDER_BOTTOM_DIRTY)
1124 draw_output_border_texture(go, GL_RENDERER_BORDER_BOTTOM,
1125 0, full_height - bottom->height,
1126 full_width, bottom->height);
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05001127}
John Kåre Alsaker44154502012-11-13 19:10:20 +01001128
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001129static void
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -06001130output_get_border_damage(struct weston_output *output,
1131 enum gl_border_status border_status,
1132 pixman_region32_t *damage)
1133{
1134 struct gl_output_state *go = get_output_state(output);
1135 struct gl_border_image *top, *bottom, *left, *right;
1136 int full_width, full_height;
1137
1138 if (border_status == BORDER_STATUS_CLEAN)
1139 return; /* Clean. Nothing to do. */
1140
1141 top = &go->borders[GL_RENDERER_BORDER_TOP];
1142 bottom = &go->borders[GL_RENDERER_BORDER_BOTTOM];
1143 left = &go->borders[GL_RENDERER_BORDER_LEFT];
1144 right = &go->borders[GL_RENDERER_BORDER_RIGHT];
1145
1146 full_width = output->current_mode->width + left->width + right->width;
1147 full_height = output->current_mode->height + top->height + bottom->height;
1148 if (border_status & BORDER_TOP_DIRTY)
1149 pixman_region32_union_rect(damage, damage,
1150 0, 0,
1151 full_width, top->height);
1152 if (border_status & BORDER_LEFT_DIRTY)
1153 pixman_region32_union_rect(damage, damage,
1154 0, top->height,
1155 left->width, output->current_mode->height);
1156 if (border_status & BORDER_RIGHT_DIRTY)
1157 pixman_region32_union_rect(damage, damage,
1158 full_width - right->width, top->height,
1159 right->width, output->current_mode->height);
1160 if (border_status & BORDER_BOTTOM_DIRTY)
1161 pixman_region32_union_rect(damage, damage,
1162 0, full_height - bottom->height,
1163 full_width, bottom->height);
1164}
1165
1166static void
Jason Ekstrande5512d42014-02-04 21:36:38 -06001167output_get_damage(struct weston_output *output,
1168 pixman_region32_t *buffer_damage, uint32_t *border_damage)
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001169{
1170 struct gl_output_state *go = get_output_state(output);
1171 struct gl_renderer *gr = get_renderer(output->compositor);
1172 EGLint buffer_age = 0;
1173 EGLBoolean ret;
1174 int i;
1175
1176 if (gr->has_egl_buffer_age) {
1177 ret = eglQuerySurface(gr->egl_display, go->egl_surface,
1178 EGL_BUFFER_AGE_EXT, &buffer_age);
1179 if (ret == EGL_FALSE) {
1180 weston_log("buffer age query failed.\n");
1181 gl_renderer_print_egl_error_state();
1182 }
1183 }
1184
Jason Ekstrande5512d42014-02-04 21:36:38 -06001185 if (buffer_age == 0 || buffer_age - 1 > BUFFER_DAMAGE_COUNT) {
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001186 pixman_region32_copy(buffer_damage, &output->region);
Jason Ekstrande5512d42014-02-04 21:36:38 -06001187 *border_damage = BORDER_ALL_DIRTY;
1188 } else {
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001189 for (i = 0; i < buffer_age - 1; i++)
Derek Foreman4c582662014-10-09 18:39:44 -05001190 *border_damage |= go->border_damage[(go->buffer_damage_index + i) % BUFFER_DAMAGE_COUNT];
Jason Ekstrande5512d42014-02-04 21:36:38 -06001191
1192 if (*border_damage & BORDER_SIZE_CHANGED) {
1193 /* If we've had a resize, we have to do a full
1194 * repaint. */
1195 *border_damage |= BORDER_ALL_DIRTY;
1196 pixman_region32_copy(buffer_damage, &output->region);
1197 } else {
1198 for (i = 0; i < buffer_age - 1; i++)
1199 pixman_region32_union(buffer_damage,
1200 buffer_damage,
Derek Foreman4c582662014-10-09 18:39:44 -05001201 &go->buffer_damage[(go->buffer_damage_index + i) % BUFFER_DAMAGE_COUNT]);
Jason Ekstrande5512d42014-02-04 21:36:38 -06001202 }
1203 }
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001204}
1205
1206static void
1207output_rotate_damage(struct weston_output *output,
Jason Ekstrande5512d42014-02-04 21:36:38 -06001208 pixman_region32_t *output_damage,
1209 enum gl_border_status border_status)
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001210{
1211 struct gl_output_state *go = get_output_state(output);
1212 struct gl_renderer *gr = get_renderer(output->compositor);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001213
1214 if (!gr->has_egl_buffer_age)
1215 return;
1216
Derek Foreman4c582662014-10-09 18:39:44 -05001217 go->buffer_damage_index += BUFFER_DAMAGE_COUNT - 1;
1218 go->buffer_damage_index %= BUFFER_DAMAGE_COUNT;
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001219
Derek Foreman4c582662014-10-09 18:39:44 -05001220 pixman_region32_copy(&go->buffer_damage[go->buffer_damage_index], output_damage);
1221 go->border_damage[go->buffer_damage_index] = border_status;
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001222}
1223
Derek Foremanc4cfe852015-05-15 12:12:40 -05001224/* NOTE: We now allow falling back to ARGB gl visuals when XRGB is
1225 * unavailable, so we're assuming the background has no transparency
1226 * and that everything with a blend, like drop shadows, will have something
1227 * opaque (like the background) drawn underneath it.
1228 *
1229 * Depending on the underlying hardware, violating that assumption could
1230 * result in seeing through to another display plane.
1231 */
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001232static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001233gl_renderer_repaint_output(struct weston_output *output,
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001234 pixman_region32_t *output_damage)
1235{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001236 struct gl_output_state *go = get_output_state(output);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001237 struct weston_compositor *compositor = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001238 struct gl_renderer *gr = get_renderer(compositor);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001239 EGLBoolean ret;
1240 static int errored;
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -06001241 int i, nrects, buffer_height;
1242 EGLint *egl_damage, *d;
1243 pixman_box32_t *rects;
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001244 pixman_region32_t buffer_damage, total_damage;
Jason Ekstrande5512d42014-02-04 21:36:38 -06001245 enum gl_border_status border_damage = BORDER_STATUS_CLEAN;
Alexandros Frantzisdf0e4b92017-09-27 15:09:16 +03001246 EGLSyncKHR begin_render_sync, end_render_sync;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001247
Jason Ekstrandae0c6e32014-10-16 10:55:20 -05001248 if (use_output(output) < 0)
1249 return;
1250
Alexandros Frantzisdf0e4b92017-09-27 15:09:16 +03001251 begin_render_sync = timeline_create_render_sync(gr);
1252
Jason Ekstrand00b84282013-10-27 22:24:59 -05001253 /* Calculate the viewport */
1254 glViewport(go->borders[GL_RENDERER_BORDER_LEFT].width,
1255 go->borders[GL_RENDERER_BORDER_BOTTOM].height,
1256 output->current_mode->width,
1257 output->current_mode->height);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001258
Jason Ekstrandfb23df72014-10-16 10:55:21 -05001259 /* Calculate the global GL matrix */
1260 go->output_matrix = output->matrix;
1261 weston_matrix_translate(&go->output_matrix,
1262 -(output->current_mode->width / 2.0),
1263 -(output->current_mode->height / 2.0), 0);
1264 weston_matrix_scale(&go->output_matrix,
1265 2.0 / output->current_mode->width,
1266 -2.0 / output->current_mode->height, 1);
1267
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001268 /* if debugging, redraw everything outside the damage to clean up
1269 * debug lines from the previous draw on this buffer:
1270 */
Kristian Høgsberg8799d412013-05-07 10:50:09 -04001271 if (gr->fan_debug) {
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001272 pixman_region32_t undamaged;
1273 pixman_region32_init(&undamaged);
1274 pixman_region32_subtract(&undamaged, &output->region,
1275 output_damage);
Kristian Høgsberg8799d412013-05-07 10:50:09 -04001276 gr->fan_debug = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001277 repaint_views(output, &undamaged);
Kristian Høgsberg8799d412013-05-07 10:50:09 -04001278 gr->fan_debug = 1;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001279 pixman_region32_fini(&undamaged);
1280 }
1281
Ander Conselvan de Oliveirab605c062013-03-05 17:30:28 +02001282 pixman_region32_init(&total_damage);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001283 pixman_region32_init(&buffer_damage);
1284
Jason Ekstrande5512d42014-02-04 21:36:38 -06001285 output_get_damage(output, &buffer_damage, &border_damage);
1286 output_rotate_damage(output, output_damage, go->border_status);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001287
1288 pixman_region32_union(&total_damage, &buffer_damage, output_damage);
Jason Ekstrande5512d42014-02-04 21:36:38 -06001289 border_damage |= go->border_status;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03001290
Jason Ekstranda7af7042013-10-12 22:38:11 -05001291 repaint_views(output, &total_damage);
Ander Conselvan de Oliveirab605c062013-03-05 17:30:28 +02001292
1293 pixman_region32_fini(&total_damage);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001294 pixman_region32_fini(&buffer_damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001295
Jason Ekstrande5512d42014-02-04 21:36:38 -06001296 draw_output_borders(output, border_damage);
John Kåre Alsaker44154502012-11-13 19:10:20 +01001297
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001298 pixman_region32_copy(&output->previous_damage, output_damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001299 wl_signal_emit(&output->frame_signal, output);
1300
Alexandros Frantzisdf0e4b92017-09-27 15:09:16 +03001301 end_render_sync = timeline_create_render_sync(gr);
1302
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -06001303 if (gr->swap_buffers_with_damage) {
1304 pixman_region32_init(&buffer_damage);
1305 weston_transformed_region(output->width, output->height,
1306 output->transform,
1307 output->current_scale,
1308 output_damage, &buffer_damage);
1309
1310 if (output_has_borders(output)) {
1311 pixman_region32_translate(&buffer_damage,
1312 go->borders[GL_RENDERER_BORDER_LEFT].width,
1313 go->borders[GL_RENDERER_BORDER_TOP].height);
1314 output_get_border_damage(output, go->border_status,
1315 &buffer_damage);
1316 }
1317
1318 rects = pixman_region32_rectangles(&buffer_damage, &nrects);
1319 egl_damage = malloc(nrects * 4 * sizeof(EGLint));
1320
1321 buffer_height = go->borders[GL_RENDERER_BORDER_TOP].height +
1322 output->current_mode->height +
1323 go->borders[GL_RENDERER_BORDER_BOTTOM].height;
1324
1325 d = egl_damage;
1326 for (i = 0; i < nrects; ++i) {
1327 *d++ = rects[i].x1;
1328 *d++ = buffer_height - rects[i].y2;
1329 *d++ = rects[i].x2 - rects[i].x1;
1330 *d++ = rects[i].y2 - rects[i].y1;
1331 }
1332 ret = gr->swap_buffers_with_damage(gr->egl_display,
1333 go->egl_surface,
1334 egl_damage, nrects);
1335 free(egl_damage);
1336 pixman_region32_fini(&buffer_damage);
1337 } else {
1338 ret = eglSwapBuffers(gr->egl_display, go->egl_surface);
1339 }
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -06001340
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001341 if (ret == EGL_FALSE && !errored) {
1342 errored = 1;
1343 weston_log("Failed in eglSwapBuffers.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02001344 gl_renderer_print_egl_error_state();
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001345 }
1346
Jason Ekstrande5512d42014-02-04 21:36:38 -06001347 go->border_status = BORDER_STATUS_CLEAN;
Alexandros Frantzisdf0e4b92017-09-27 15:09:16 +03001348
1349 /* We have to submit the render sync objects after swap buffers, since
1350 * the objects get assigned a valid sync file fd only after a gl flush.
1351 */
1352 timeline_submit_render_sync(gr, compositor, output, begin_render_sync,
1353 TIMELINE_RENDER_POINT_TYPE_BEGIN);
1354 timeline_submit_render_sync(gr, compositor, output, end_render_sync,
1355 TIMELINE_RENDER_POINT_TYPE_END);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001356}
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001357
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001358static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001359gl_renderer_read_pixels(struct weston_output *output,
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001360 pixman_format_code_t format, void *pixels,
1361 uint32_t x, uint32_t y,
1362 uint32_t width, uint32_t height)
1363{
1364 GLenum gl_format;
Jason Ekstrand701f6362014-04-02 19:53:59 -05001365 struct gl_output_state *go = get_output_state(output);
1366
1367 x += go->borders[GL_RENDERER_BORDER_LEFT].width;
1368 y += go->borders[GL_RENDERER_BORDER_BOTTOM].height;
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001369
1370 switch (format) {
1371 case PIXMAN_a8r8g8b8:
1372 gl_format = GL_BGRA_EXT;
1373 break;
1374 case PIXMAN_a8b8g8r8:
1375 gl_format = GL_RGBA;
1376 break;
1377 default:
1378 return -1;
1379 }
1380
1381 if (use_output(output) < 0)
1382 return -1;
1383
1384 glPixelStorei(GL_PACK_ALIGNMENT, 1);
1385 glReadPixels(x, y, width, height, gl_format,
1386 GL_UNSIGNED_BYTE, pixels);
1387
1388 return 0;
1389}
1390
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001391static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001392gl_renderer_flush_damage(struct weston_surface *surface)
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001393{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001394 struct gl_renderer *gr = get_renderer(surface->compositor);
1395 struct gl_surface_state *gs = get_surface_state(surface);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001396 struct weston_buffer *buffer = gs->buffer_ref.buffer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001397 struct weston_view *view;
Derek Foreman97746792015-11-18 16:32:28 -06001398 bool texture_used;
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001399 pixman_box32_t *rectangles;
Raúl Peñacoba5fc8d5e2017-03-28 18:17:56 +02001400 uint8_t *data;
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001401 int i, j, n;
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001402
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001403 pixman_region32_union(&gs->texture_damage,
1404 &gs->texture_damage, &surface->damage);
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001405
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001406 if (!buffer)
1407 return;
1408
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001409 /* Avoid upload, if the texture won't be used this time.
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001410 * We still accumulate the damage in texture_damage, and
1411 * hold the reference to the buffer, in case the surface
1412 * migrates back to the primary plane.
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001413 */
Derek Foreman97746792015-11-18 16:32:28 -06001414 texture_used = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001415 wl_list_for_each(view, &surface->views, surface_link) {
1416 if (view->plane == &surface->compositor->primary_plane) {
Derek Foreman97746792015-11-18 16:32:28 -06001417 texture_used = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001418 break;
1419 }
1420 }
1421 if (!texture_used)
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001422 return;
1423
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001424 if (!pixman_region32_not_empty(&gs->texture_damage) &&
1425 !gs->needs_full_upload)
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001426 goto done;
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001427
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001428 data = wl_shm_buffer_get_data(buffer->shm_buffer);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001429
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001430 if (!gr->has_unpack_subimage) {
Neil Robertse5051712013-11-13 15:44:06 +00001431 wl_shm_buffer_begin_access(buffer->shm_buffer);
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001432 for (j = 0; j < gs->num_textures; j++) {
1433 glBindTexture(GL_TEXTURE_2D, gs->textures[j]);
1434 glTexImage2D(GL_TEXTURE_2D, 0,
Vincent Abriou00a03d22016-10-05 14:54:35 +02001435 gs->gl_format[j],
Vincent ABRIOUe5732c72016-10-20 16:20:11 +02001436 gs->pitch / gs->hsub[j],
1437 buffer->height / gs->vsub[j],
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001438 0,
Vincent Abriou00a03d22016-10-05 14:54:35 +02001439 gs->gl_format[j],
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001440 gs->gl_pixel_type,
1441 data + gs->offset[j]);
1442 }
Neil Robertse5051712013-11-13 15:44:06 +00001443 wl_shm_buffer_end_access(buffer->shm_buffer);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001444
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001445 goto done;
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001446 }
1447
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -07001448 glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, gs->pitch);
Ander Conselvan de Oliveira6be5f432013-06-07 16:52:45 +03001449
1450 if (gs->needs_full_upload) {
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -07001451 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, 0);
1452 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, 0);
Neil Robertse5051712013-11-13 15:44:06 +00001453 wl_shm_buffer_begin_access(buffer->shm_buffer);
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001454 for (j = 0; j < gs->num_textures; j++) {
1455 glBindTexture(GL_TEXTURE_2D, gs->textures[j]);
1456 glTexImage2D(GL_TEXTURE_2D, 0,
Vincent Abriou00a03d22016-10-05 14:54:35 +02001457 gs->gl_format[j],
Vincent ABRIOUe5732c72016-10-20 16:20:11 +02001458 gs->pitch / gs->hsub[j],
1459 buffer->height / gs->vsub[j],
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001460 0,
Vincent Abriou00a03d22016-10-05 14:54:35 +02001461 gs->gl_format[j],
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001462 gs->gl_pixel_type,
1463 data + gs->offset[j]);
1464 }
Neil Robertse5051712013-11-13 15:44:06 +00001465 wl_shm_buffer_end_access(buffer->shm_buffer);
Ander Conselvan de Oliveira6be5f432013-06-07 16:52:45 +03001466 goto done;
1467 }
1468
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001469 rectangles = pixman_region32_rectangles(&gs->texture_damage, &n);
Neil Robertse5051712013-11-13 15:44:06 +00001470 wl_shm_buffer_begin_access(buffer->shm_buffer);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001471 for (i = 0; i < n; i++) {
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001472 pixman_box32_t r;
1473
1474 r = weston_surface_to_buffer_rect(surface, rectangles[i]);
1475
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -07001476 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, r.x1);
1477 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, r.y1);
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001478 for (j = 0; j < gs->num_textures; j++) {
1479 glBindTexture(GL_TEXTURE_2D, gs->textures[j]);
1480 glTexSubImage2D(GL_TEXTURE_2D, 0,
Vincent ABRIOUe5732c72016-10-20 16:20:11 +02001481 r.x1 / gs->hsub[j],
1482 r.y1 / gs->vsub[j],
1483 (r.x2 - r.x1) / gs->hsub[j],
1484 (r.y2 - r.y1) / gs->vsub[j],
Vincent Abriou00a03d22016-10-05 14:54:35 +02001485 gs->gl_format[j],
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001486 gs->gl_pixel_type,
1487 data + gs->offset[j]);
1488 }
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001489 }
Neil Robertse5051712013-11-13 15:44:06 +00001490 wl_shm_buffer_end_access(buffer->shm_buffer);
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001491
1492done:
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001493 pixman_region32_fini(&gs->texture_damage);
1494 pixman_region32_init(&gs->texture_damage);
Derek Foreman4c11fe72015-11-18 16:32:27 -06001495 gs->needs_full_upload = false;
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001496
1497 weston_buffer_reference(&gs->buffer_ref, NULL);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001498}
1499
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001500static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001501ensure_textures(struct gl_surface_state *gs, int num_textures)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001502{
1503 int i;
1504
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001505 if (num_textures <= gs->num_textures)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001506 return;
1507
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001508 for (i = gs->num_textures; i < num_textures; i++) {
1509 glGenTextures(1, &gs->textures[i]);
1510 glBindTexture(gs->target, gs->textures[i]);
1511 glTexParameteri(gs->target,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001512 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001513 glTexParameteri(gs->target,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001514 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1515 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001516 gs->num_textures = num_textures;
1517 glBindTexture(gs->target, 0);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001518}
1519
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001520static void
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001521gl_renderer_attach_shm(struct weston_surface *es, struct weston_buffer *buffer,
1522 struct wl_shm_buffer *shm_buffer)
1523{
1524 struct weston_compositor *ec = es->compositor;
1525 struct gl_renderer *gr = get_renderer(ec);
1526 struct gl_surface_state *gs = get_surface_state(es);
Vincent Abriou00a03d22016-10-05 14:54:35 +02001527 GLenum gl_format[3] = {0, 0, 0};
1528 GLenum gl_pixel_type;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001529 int pitch;
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001530 int num_planes;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001531
1532 buffer->shm_buffer = shm_buffer;
1533 buffer->width = wl_shm_buffer_get_width(shm_buffer);
1534 buffer->height = wl_shm_buffer_get_height(shm_buffer);
1535
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001536 num_planes = 1;
1537 gs->offset[0] = 0;
Vincent ABRIOUe5732c72016-10-20 16:20:11 +02001538 gs->hsub[0] = 1;
1539 gs->vsub[0] = 1;
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001540
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001541 switch (wl_shm_buffer_get_format(shm_buffer)) {
1542 case WL_SHM_FORMAT_XRGB8888:
1543 gs->shader = &gr->texture_shader_rgbx;
1544 pitch = wl_shm_buffer_get_stride(shm_buffer) / 4;
Vincent Abriou00a03d22016-10-05 14:54:35 +02001545 gl_format[0] = GL_BGRA_EXT;
Neil Roberts4d085e72014-04-07 15:01:01 +01001546 gl_pixel_type = GL_UNSIGNED_BYTE;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001547 break;
1548 case WL_SHM_FORMAT_ARGB8888:
1549 gs->shader = &gr->texture_shader_rgba;
1550 pitch = wl_shm_buffer_get_stride(shm_buffer) / 4;
Vincent Abriou00a03d22016-10-05 14:54:35 +02001551 gl_format[0] = GL_BGRA_EXT;
Neil Roberts4d085e72014-04-07 15:01:01 +01001552 gl_pixel_type = GL_UNSIGNED_BYTE;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001553 break;
1554 case WL_SHM_FORMAT_RGB565:
1555 gs->shader = &gr->texture_shader_rgbx;
1556 pitch = wl_shm_buffer_get_stride(shm_buffer) / 2;
Vincent Abriou00a03d22016-10-05 14:54:35 +02001557 gl_format[0] = GL_RGB;
Neil Roberts4d085e72014-04-07 15:01:01 +01001558 gl_pixel_type = GL_UNSIGNED_SHORT_5_6_5;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001559 break;
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001560 case WL_SHM_FORMAT_YUV420:
1561 gs->shader = &gr->texture_shader_y_u_v;
1562 pitch = wl_shm_buffer_get_stride(shm_buffer);
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001563 gl_pixel_type = GL_UNSIGNED_BYTE;
1564 num_planes = 3;
Vincent ABRIOUe5732c72016-10-20 16:20:11 +02001565 gs->offset[1] = gs->offset[0] + (pitch / gs->hsub[0]) *
1566 (buffer->height / gs->vsub[0]);
1567 gs->hsub[1] = 2;
1568 gs->vsub[1] = 2;
1569 gs->offset[2] = gs->offset[1] + (pitch / gs->hsub[1]) *
1570 (buffer->height / gs->vsub[1]);
1571 gs->hsub[2] = 2;
1572 gs->vsub[2] = 2;
Vincent Abriou00a03d22016-10-05 14:54:35 +02001573 if (gr->has_gl_texture_rg) {
1574 gl_format[0] = GL_R8_EXT;
1575 gl_format[1] = GL_R8_EXT;
1576 gl_format[2] = GL_R8_EXT;
1577 } else {
1578 gl_format[0] = GL_LUMINANCE;
1579 gl_format[1] = GL_LUMINANCE;
1580 gl_format[2] = GL_LUMINANCE;
1581 }
1582 break;
1583 case WL_SHM_FORMAT_NV12:
1584 gs->shader = &gr->texture_shader_y_xuxv;
1585 pitch = wl_shm_buffer_get_stride(shm_buffer);
1586 gl_pixel_type = GL_UNSIGNED_BYTE;
1587 num_planes = 2;
Vincent ABRIOUe5732c72016-10-20 16:20:11 +02001588 gs->offset[1] = gs->offset[0] + (pitch / gs->hsub[0]) *
1589 (buffer->height / gs->vsub[0]);
1590 gs->hsub[1] = 2;
1591 gs->vsub[1] = 2;
Vincent Abriou00a03d22016-10-05 14:54:35 +02001592 if (gr->has_gl_texture_rg) {
1593 gl_format[0] = GL_R8_EXT;
1594 gl_format[1] = GL_RG8_EXT;
1595 } else {
1596 gl_format[0] = GL_LUMINANCE;
1597 gl_format[1] = GL_LUMINANCE_ALPHA;
1598 }
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001599 break;
Vincent ABRIOUe5732c72016-10-20 16:20:11 +02001600 case WL_SHM_FORMAT_YUYV:
1601 gs->shader = &gr->texture_shader_y_xuxv;
1602 pitch = wl_shm_buffer_get_stride(shm_buffer) / 2;
1603 gl_pixel_type = GL_UNSIGNED_BYTE;
1604 num_planes = 2;
1605 gs->hsub[1] = 2;
1606 gs->vsub[1] = 1;
1607 if (gr->has_gl_texture_rg)
1608 gl_format[0] = GL_RG8_EXT;
1609 else
1610 gl_format[0] = GL_LUMINANCE_ALPHA;
1611 gl_format[1] = GL_BGRA_EXT;
1612 break;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001613 default:
Neil Roberts4d085e72014-04-07 15:01:01 +01001614 weston_log("warning: unknown shm buffer format: %08x\n",
1615 wl_shm_buffer_get_format(shm_buffer));
1616 return;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001617 }
1618
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001619 /* Only allocate a texture if it doesn't match existing one.
1620 * If a switch from DRM allocated buffer to a SHM buffer is
1621 * happening, we need to allocate a new texture buffer. */
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001622 if (pitch != gs->pitch ||
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001623 buffer->height != gs->height ||
Vincent Abriou00a03d22016-10-05 14:54:35 +02001624 gl_format[0] != gs->gl_format[0] ||
1625 gl_format[1] != gs->gl_format[1] ||
1626 gl_format[2] != gs->gl_format[2] ||
Neil Roberts4d085e72014-04-07 15:01:01 +01001627 gl_pixel_type != gs->gl_pixel_type ||
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001628 gs->buffer_type != BUFFER_TYPE_SHM) {
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001629 gs->pitch = pitch;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001630 gs->height = buffer->height;
1631 gs->target = GL_TEXTURE_2D;
Vincent Abriou00a03d22016-10-05 14:54:35 +02001632 gs->gl_format[0] = gl_format[0];
1633 gs->gl_format[1] = gl_format[1];
1634 gs->gl_format[2] = gl_format[2];
Neil Roberts4d085e72014-04-07 15:01:01 +01001635 gs->gl_pixel_type = gl_pixel_type;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001636 gs->buffer_type = BUFFER_TYPE_SHM;
Derek Foreman4c11fe72015-11-18 16:32:27 -06001637 gs->needs_full_upload = true;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001638 gs->y_inverted = 1;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001639
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03001640 gs->surface = es;
1641
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001642 ensure_textures(gs, num_planes);
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001643 }
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001644}
1645
1646static void
1647gl_renderer_attach_egl(struct weston_surface *es, struct weston_buffer *buffer,
1648 uint32_t format)
1649{
1650 struct weston_compositor *ec = es->compositor;
1651 struct gl_renderer *gr = get_renderer(ec);
1652 struct gl_surface_state *gs = get_surface_state(es);
1653 EGLint attribs[3];
1654 int i, num_planes;
1655
1656 buffer->legacy_buffer = (struct wl_buffer *)buffer->resource;
1657 gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1658 EGL_WIDTH, &buffer->width);
1659 gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1660 EGL_HEIGHT, &buffer->height);
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001661 gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1662 EGL_WAYLAND_Y_INVERTED_WL, &buffer->y_inverted);
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001663
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +03001664 for (i = 0; i < gs->num_images; i++) {
1665 egl_image_unref(gs->images[i]);
1666 gs->images[i] = NULL;
1667 }
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001668 gs->num_images = 0;
1669 gs->target = GL_TEXTURE_2D;
1670 switch (format) {
1671 case EGL_TEXTURE_RGB:
1672 case EGL_TEXTURE_RGBA:
1673 default:
1674 num_planes = 1;
1675 gs->shader = &gr->texture_shader_rgba;
1676 break;
1677 case EGL_TEXTURE_EXTERNAL_WL:
1678 num_planes = 1;
1679 gs->target = GL_TEXTURE_EXTERNAL_OES;
1680 gs->shader = &gr->texture_shader_egl_external;
1681 break;
1682 case EGL_TEXTURE_Y_UV_WL:
1683 num_planes = 2;
1684 gs->shader = &gr->texture_shader_y_uv;
1685 break;
1686 case EGL_TEXTURE_Y_U_V_WL:
1687 num_planes = 3;
1688 gs->shader = &gr->texture_shader_y_u_v;
1689 break;
1690 case EGL_TEXTURE_Y_XUXV_WL:
1691 num_planes = 2;
1692 gs->shader = &gr->texture_shader_y_xuxv;
1693 break;
1694 }
1695
1696 ensure_textures(gs, num_planes);
1697 for (i = 0; i < num_planes; i++) {
1698 attribs[0] = EGL_WAYLAND_PLANE_WL;
1699 attribs[1] = i;
1700 attribs[2] = EGL_NONE;
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +03001701 gs->images[i] = egl_image_create(gr,
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001702 EGL_WAYLAND_BUFFER_WL,
1703 buffer->legacy_buffer,
1704 attribs);
1705 if (!gs->images[i]) {
1706 weston_log("failed to create img for plane %d\n", i);
1707 continue;
1708 }
1709 gs->num_images++;
1710
1711 glActiveTexture(GL_TEXTURE0 + i);
1712 glBindTexture(gs->target, gs->textures[i]);
1713 gr->image_target_texture_2d(gs->target,
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +03001714 gs->images[i]->image);
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001715 }
1716
1717 gs->pitch = buffer->width;
1718 gs->height = buffer->height;
1719 gs->buffer_type = BUFFER_TYPE_EGL;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001720 gs->y_inverted = buffer->y_inverted;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001721}
1722
1723static void
Pekka Paalanena3525802014-06-12 16:49:29 +03001724gl_renderer_destroy_dmabuf(struct linux_dmabuf_buffer *dmabuf)
1725{
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001726 struct dmabuf_image *image = dmabuf->user_data;
Pekka Paalanena3525802014-06-12 16:49:29 +03001727
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001728 dmabuf_image_destroy(image);
Pekka Paalanena3525802014-06-12 16:49:29 +03001729}
1730
1731static struct egl_image *
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001732import_simple_dmabuf(struct gl_renderer *gr,
1733 struct dmabuf_attributes *attributes)
Pekka Paalanena3525802014-06-12 16:49:29 +03001734{
1735 struct egl_image *image;
Varad Gautamc32e05b2016-11-23 14:03:19 +05301736 EGLint attribs[50];
Pekka Paalanena3525802014-06-12 16:49:29 +03001737 int atti = 0;
Daniel Stoneb138d7a2017-10-03 12:58:53 +01001738 bool has_modifier;
Pekka Paalanena3525802014-06-12 16:49:29 +03001739
Pekka Paalanena3525802014-06-12 16:49:29 +03001740 /* This requires the Mesa commit in
1741 * Mesa 10.3 (08264e5dad4df448e7718e782ad9077902089a07) or
1742 * Mesa 10.2.7 (55d28925e6109a4afd61f109e845a8a51bd17652).
1743 * Otherwise Mesa closes the fd behind our back and re-importing
1744 * will fail.
1745 * https://bugs.freedesktop.org/show_bug.cgi?id=76188
1746 */
1747
1748 attribs[atti++] = EGL_WIDTH;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001749 attribs[atti++] = attributes->width;
Pekka Paalanena3525802014-06-12 16:49:29 +03001750 attribs[atti++] = EGL_HEIGHT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001751 attribs[atti++] = attributes->height;
Pekka Paalanena3525802014-06-12 16:49:29 +03001752 attribs[atti++] = EGL_LINUX_DRM_FOURCC_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001753 attribs[atti++] = attributes->format;
Pekka Paalanena3525802014-06-12 16:49:29 +03001754
Daniel Stoneb138d7a2017-10-03 12:58:53 +01001755 if (attributes->modifier[0] != DRM_FORMAT_MOD_INVALID) {
1756 if (!gr->has_dmabuf_import_modifiers)
1757 return NULL;
1758 has_modifier = true;
1759 } else {
1760 has_modifier = false;
1761 }
1762
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001763 if (attributes->n_planes > 0) {
Pekka Paalanena3525802014-06-12 16:49:29 +03001764 attribs[atti++] = EGL_DMA_BUF_PLANE0_FD_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001765 attribs[atti++] = attributes->fd[0];
Pekka Paalanena3525802014-06-12 16:49:29 +03001766 attribs[atti++] = EGL_DMA_BUF_PLANE0_OFFSET_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001767 attribs[atti++] = attributes->offset[0];
Pekka Paalanena3525802014-06-12 16:49:29 +03001768 attribs[atti++] = EGL_DMA_BUF_PLANE0_PITCH_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001769 attribs[atti++] = attributes->stride[0];
Daniel Stoneb138d7a2017-10-03 12:58:53 +01001770 if (has_modifier) {
Varad Gautamf7da8b32016-11-23 14:03:18 +05301771 attribs[atti++] = EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT;
1772 attribs[atti++] = attributes->modifier[0] & 0xFFFFFFFF;
1773 attribs[atti++] = EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT;
1774 attribs[atti++] = attributes->modifier[0] >> 32;
1775 }
Pekka Paalanena3525802014-06-12 16:49:29 +03001776 }
1777
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001778 if (attributes->n_planes > 1) {
Pekka Paalanena3525802014-06-12 16:49:29 +03001779 attribs[atti++] = EGL_DMA_BUF_PLANE1_FD_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001780 attribs[atti++] = attributes->fd[1];
Pekka Paalanena3525802014-06-12 16:49:29 +03001781 attribs[atti++] = EGL_DMA_BUF_PLANE1_OFFSET_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001782 attribs[atti++] = attributes->offset[1];
Pekka Paalanena3525802014-06-12 16:49:29 +03001783 attribs[atti++] = EGL_DMA_BUF_PLANE1_PITCH_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001784 attribs[atti++] = attributes->stride[1];
Daniel Stoneb138d7a2017-10-03 12:58:53 +01001785 if (has_modifier) {
Varad Gautamf7da8b32016-11-23 14:03:18 +05301786 attribs[atti++] = EGL_DMA_BUF_PLANE1_MODIFIER_LO_EXT;
1787 attribs[atti++] = attributes->modifier[1] & 0xFFFFFFFF;
1788 attribs[atti++] = EGL_DMA_BUF_PLANE1_MODIFIER_HI_EXT;
1789 attribs[atti++] = attributes->modifier[1] >> 32;
1790 }
Pekka Paalanena3525802014-06-12 16:49:29 +03001791 }
1792
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001793 if (attributes->n_planes > 2) {
Pekka Paalanena3525802014-06-12 16:49:29 +03001794 attribs[atti++] = EGL_DMA_BUF_PLANE2_FD_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001795 attribs[atti++] = attributes->fd[2];
Pekka Paalanena3525802014-06-12 16:49:29 +03001796 attribs[atti++] = EGL_DMA_BUF_PLANE2_OFFSET_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001797 attribs[atti++] = attributes->offset[2];
Pekka Paalanena3525802014-06-12 16:49:29 +03001798 attribs[atti++] = EGL_DMA_BUF_PLANE2_PITCH_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001799 attribs[atti++] = attributes->stride[2];
Daniel Stoneb138d7a2017-10-03 12:58:53 +01001800 if (has_modifier) {
Varad Gautamf7da8b32016-11-23 14:03:18 +05301801 attribs[atti++] = EGL_DMA_BUF_PLANE2_MODIFIER_LO_EXT;
1802 attribs[atti++] = attributes->modifier[2] & 0xFFFFFFFF;
1803 attribs[atti++] = EGL_DMA_BUF_PLANE2_MODIFIER_HI_EXT;
1804 attribs[atti++] = attributes->modifier[2] >> 32;
1805 }
Pekka Paalanena3525802014-06-12 16:49:29 +03001806 }
1807
Varad Gautamc32e05b2016-11-23 14:03:19 +05301808 if (gr->has_dmabuf_import_modifiers) {
1809 if (attributes->n_planes > 3) {
1810 attribs[atti++] = EGL_DMA_BUF_PLANE3_FD_EXT;
1811 attribs[atti++] = attributes->fd[3];
1812 attribs[atti++] = EGL_DMA_BUF_PLANE3_OFFSET_EXT;
1813 attribs[atti++] = attributes->offset[3];
1814 attribs[atti++] = EGL_DMA_BUF_PLANE3_PITCH_EXT;
1815 attribs[atti++] = attributes->stride[3];
1816 attribs[atti++] = EGL_DMA_BUF_PLANE3_MODIFIER_LO_EXT;
1817 attribs[atti++] = attributes->modifier[3] & 0xFFFFFFFF;
1818 attribs[atti++] = EGL_DMA_BUF_PLANE3_MODIFIER_HI_EXT;
1819 attribs[atti++] = attributes->modifier[3] >> 32;
1820 }
1821 }
1822
Pekka Paalanena3525802014-06-12 16:49:29 +03001823 attribs[atti++] = EGL_NONE;
1824
1825 image = egl_image_create(gr, EGL_LINUX_DMA_BUF_EXT, NULL,
1826 attribs);
1827
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001828 return image;
1829}
Pekka Paalanena3525802014-06-12 16:49:29 +03001830
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001831/* The kernel header drm_fourcc.h defines the DRM formats below. We duplicate
1832 * some of the definitions here so that building Weston won't require
1833 * bleeding-edge kernel headers.
1834 */
1835#ifndef DRM_FORMAT_R8
1836#define DRM_FORMAT_R8 fourcc_code('R', '8', ' ', ' ') /* [7:0] R */
1837#endif
1838
1839#ifndef DRM_FORMAT_GR88
1840#define DRM_FORMAT_GR88 fourcc_code('G', 'R', '8', '8') /* [15:0] G:R 8:8 little endian */
1841#endif
1842
1843struct yuv_format_descriptor yuv_formats[] = {
1844 {
1845 .format = DRM_FORMAT_YUYV,
1846 .input_planes = 1,
1847 .output_planes = 2,
1848 .texture_type = EGL_TEXTURE_Y_XUXV_WL,
1849 {{
1850 .width_divisor = 1,
1851 .height_divisor = 1,
1852 .format = DRM_FORMAT_GR88,
1853 .plane_index = 0
1854 }, {
1855 .width_divisor = 2,
1856 .height_divisor = 1,
1857 .format = DRM_FORMAT_ARGB8888,
1858 .plane_index = 0
1859 }}
1860 }, {
1861 .format = DRM_FORMAT_NV12,
1862 .input_planes = 2,
1863 .output_planes = 2,
1864 .texture_type = EGL_TEXTURE_Y_UV_WL,
1865 {{
1866 .width_divisor = 1,
1867 .height_divisor = 1,
1868 .format = DRM_FORMAT_R8,
1869 .plane_index = 0
1870 }, {
1871 .width_divisor = 2,
1872 .height_divisor = 2,
1873 .format = DRM_FORMAT_GR88,
1874 .plane_index = 1
1875 }}
1876 }, {
1877 .format = DRM_FORMAT_YUV420,
1878 .input_planes = 3,
1879 .output_planes = 3,
1880 .texture_type = EGL_TEXTURE_Y_U_V_WL,
1881 {{
1882 .width_divisor = 1,
1883 .height_divisor = 1,
1884 .format = DRM_FORMAT_R8,
1885 .plane_index = 0
1886 }, {
1887 .width_divisor = 2,
1888 .height_divisor = 2,
1889 .format = DRM_FORMAT_R8,
1890 .plane_index = 1
1891 }, {
1892 .width_divisor = 2,
1893 .height_divisor = 2,
1894 .format = DRM_FORMAT_R8,
1895 .plane_index = 2
1896 }}
Matthias Treydteaca3ffb2016-07-25 12:15:41 +02001897 }, {
1898 .format = DRM_FORMAT_YUV444,
1899 .input_planes = 3,
1900 .output_planes = 3,
1901 .texture_type = EGL_TEXTURE_Y_U_V_WL,
1902 {{
1903 .width_divisor = 1,
1904 .height_divisor = 1,
1905 .format = DRM_FORMAT_R8,
1906 .plane_index = 0
1907 }, {
1908 .width_divisor = 1,
1909 .height_divisor = 1,
1910 .format = DRM_FORMAT_R8,
1911 .plane_index = 1
1912 }, {
1913 .width_divisor = 1,
1914 .height_divisor = 1,
1915 .format = DRM_FORMAT_R8,
1916 .plane_index = 2
1917 }}
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001918 }
1919};
1920
1921static struct egl_image *
1922import_dmabuf_single_plane(struct gl_renderer *gr,
1923 const struct dmabuf_attributes *attributes,
1924 struct yuv_plane_descriptor *descriptor)
1925{
1926 struct dmabuf_attributes plane;
1927 struct egl_image *image;
1928 char fmt[4];
1929
1930 plane.width = attributes->width / descriptor->width_divisor;
1931 plane.height = attributes->height / descriptor->height_divisor;
1932 plane.format = descriptor->format;
1933 plane.n_planes = 1;
1934 plane.fd[0] = attributes->fd[descriptor->plane_index];
1935 plane.offset[0] = attributes->offset[descriptor->plane_index];
1936 plane.stride[0] = attributes->stride[descriptor->plane_index];
1937 plane.modifier[0] = attributes->modifier[descriptor->plane_index];
1938
1939 image = import_simple_dmabuf(gr, &plane);
1940 if (!image) {
1941 weston_log("Failed to import plane %d as %.4s\n",
1942 descriptor->plane_index,
1943 dump_format(descriptor->format, fmt));
1944 return NULL;
1945 }
1946
1947 return image;
1948}
1949
1950static bool
1951import_yuv_dmabuf(struct gl_renderer *gr,
1952 struct dmabuf_image *image)
1953{
1954 unsigned i;
1955 int j;
1956 int ret;
1957 struct yuv_format_descriptor *format = NULL;
1958 struct dmabuf_attributes *attributes = &image->dmabuf->attributes;
1959 char fmt[4];
1960
1961 for (i = 0; i < ARRAY_LENGTH(yuv_formats); ++i) {
1962 if (yuv_formats[i].format == attributes->format) {
1963 format = &yuv_formats[i];
1964 break;
1965 }
1966 }
1967
1968 if (!format) {
1969 weston_log("Error during import, and no known conversion for format "
Derek Foreman12968e32017-06-26 14:42:44 -05001970 "%.4s in the renderer\n",
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001971 dump_format(attributes->format, fmt));
1972 return false;
1973 }
1974
1975 if (attributes->n_planes != format->input_planes) {
Derek Foreman12968e32017-06-26 14:42:44 -05001976 weston_log("%.4s dmabuf must contain %d plane%s (%d provided)\n",
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001977 dump_format(format->format, fmt),
1978 format->input_planes,
1979 (format->input_planes > 1) ? "s" : "",
1980 attributes->n_planes);
1981 return false;
1982 }
1983
1984 for (j = 0; j < format->output_planes; ++j) {
1985 image->images[j] = import_dmabuf_single_plane(gr, attributes,
1986 &format->plane[j]);
1987 if (!image->images[j]) {
1988 while (j) {
1989 ret = egl_image_unref(image->images[--j]);
1990 assert(ret == 0);
1991 }
1992 return false;
1993 }
1994 }
1995
1996 image->num_images = format->output_planes;
1997
1998 switch (format->texture_type) {
1999 case EGL_TEXTURE_Y_XUXV_WL:
2000 image->shader = &gr->texture_shader_y_xuxv;
2001 break;
2002 case EGL_TEXTURE_Y_UV_WL:
2003 image->shader = &gr->texture_shader_y_uv;
2004 break;
2005 case EGL_TEXTURE_Y_U_V_WL:
2006 image->shader = &gr->texture_shader_y_u_v;
2007 break;
2008 default:
2009 assert(false);
2010 }
2011
2012 return true;
2013}
2014
2015static GLenum
2016choose_texture_target(struct dmabuf_attributes *attributes)
2017{
2018 if (attributes->n_planes > 1)
2019 return GL_TEXTURE_EXTERNAL_OES;
2020
2021 switch (attributes->format & ~DRM_FORMAT_BIG_ENDIAN) {
2022 case DRM_FORMAT_YUYV:
2023 case DRM_FORMAT_YVYU:
2024 case DRM_FORMAT_UYVY:
2025 case DRM_FORMAT_VYUY:
2026 case DRM_FORMAT_AYUV:
2027 return GL_TEXTURE_EXTERNAL_OES;
2028 default:
2029 return GL_TEXTURE_2D;
2030 }
2031}
2032
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00002033static struct dmabuf_image *
2034import_dmabuf(struct gl_renderer *gr,
2035 struct linux_dmabuf_buffer *dmabuf)
2036{
2037 struct egl_image *egl_image;
2038 struct dmabuf_image *image;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00002039
2040 image = dmabuf_image_create();
Pekka Paalanena3525802014-06-12 16:49:29 +03002041 image->dmabuf = dmabuf;
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00002042
2043 egl_image = import_simple_dmabuf(gr, &dmabuf->attributes);
2044 if (egl_image) {
2045 image->num_images = 1;
2046 image->images[0] = egl_image;
2047 image->import_type = IMPORT_TYPE_DIRECT;
2048 image->target = choose_texture_target(&dmabuf->attributes);
2049
2050 switch (image->target) {
2051 case GL_TEXTURE_2D:
2052 image->shader = &gr->texture_shader_rgba;
2053 break;
2054 default:
2055 image->shader = &gr->texture_shader_egl_external;
2056 }
2057 } else {
2058 if (!import_yuv_dmabuf(gr, image)) {
2059 dmabuf_image_destroy(image);
2060 return NULL;
2061 }
2062 image->import_type = IMPORT_TYPE_GL_CONVERSION;
2063 image->target = GL_TEXTURE_2D;
2064 }
Pekka Paalanena3525802014-06-12 16:49:29 +03002065
2066 return image;
2067}
2068
2069static bool
Varad Gautam0775cd12016-11-23 14:03:20 +05302070gl_renderer_query_dmabuf_formats(struct weston_compositor *wc,
2071 int **formats, int *num_formats)
2072{
2073 struct gl_renderer *gr = get_renderer(wc);
2074 EGLint num;
2075
2076 assert(gr->has_dmabuf_import);
2077
2078 if (!gr->has_dmabuf_import_modifiers ||
2079 !gr->query_dmabuf_formats(gr->egl_display, 0, NULL, &num)) {
2080 *num_formats = 0;
2081 return false;
2082 }
2083
2084 *formats = calloc(num, sizeof(int));
2085 if (*formats == NULL) {
2086 *num_formats = 0;
2087 return false;
2088 }
2089 if (!gr->query_dmabuf_formats(gr->egl_display, num, *formats,
2090 (EGLint*) &num)) {
2091 *num_formats = 0;
2092 free(*formats);
2093 return false;
2094 }
2095
2096 *num_formats = num;
2097 return true;
2098}
2099
2100static bool
2101gl_renderer_query_dmabuf_modifiers(struct weston_compositor *wc, int format,
2102 uint64_t **modifiers,
2103 int *num_modifiers)
2104{
2105 struct gl_renderer *gr = get_renderer(wc);
2106 int num;
2107
2108 assert(gr->has_dmabuf_import);
2109
2110 if (!gr->has_dmabuf_import_modifiers ||
2111 !gr->query_dmabuf_modifiers(gr->egl_display, format, 0, NULL,
2112 NULL, &num)) {
2113 *num_modifiers = 0;
2114 return false;
2115 }
2116
2117 *modifiers = calloc(num, sizeof(uint64_t));
2118 if (*modifiers == NULL) {
2119 *num_modifiers = 0;
2120 return false;
2121 }
2122 if (!gr->query_dmabuf_modifiers(gr->egl_display, format,
2123 num, *modifiers, NULL, &num)) {
2124 *num_modifiers = 0;
2125 free(*modifiers);
2126 return false;
2127 }
2128
2129 *num_modifiers = num;
2130 return true;
2131}
2132
2133static bool
Pekka Paalanena3525802014-06-12 16:49:29 +03002134gl_renderer_import_dmabuf(struct weston_compositor *ec,
2135 struct linux_dmabuf_buffer *dmabuf)
2136{
2137 struct gl_renderer *gr = get_renderer(ec);
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00002138 struct dmabuf_image *image;
Pekka Paalanena3525802014-06-12 16:49:29 +03002139 int i;
2140
2141 assert(gr->has_dmabuf_import);
2142
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00002143 for (i = 0; i < dmabuf->attributes.n_planes; i++) {
Varad Gautamf7da8b32016-11-23 14:03:18 +05302144 /* return if EGL doesn't support import modifiers */
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00002145 if (dmabuf->attributes.modifier[i] != 0)
Varad Gautamf7da8b32016-11-23 14:03:18 +05302146 if (!gr->has_dmabuf_import_modifiers)
2147 return false;
2148
2149 /* return if modifiers passed are unequal */
2150 if (dmabuf->attributes.modifier[i] !=
2151 dmabuf->attributes.modifier[0])
Pekka Paalanena3525802014-06-12 16:49:29 +03002152 return false;
2153 }
2154
2155 /* reject all flags we do not recognize or handle */
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00002156 if (dmabuf->attributes.flags & ~ZWP_LINUX_BUFFER_PARAMS_V1_FLAGS_Y_INVERT)
Pekka Paalanena3525802014-06-12 16:49:29 +03002157 return false;
2158
2159 image = import_dmabuf(gr, dmabuf);
2160 if (!image)
2161 return false;
2162
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00002163 wl_list_insert(&gr->dmabuf_images, &image->link);
2164 linux_dmabuf_buffer_set_user_data(dmabuf, image,
2165 gl_renderer_destroy_dmabuf);
Pekka Paalanena3525802014-06-12 16:49:29 +03002166
2167 return true;
2168}
2169
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00002170static bool
2171import_known_dmabuf(struct gl_renderer *gr,
2172 struct dmabuf_image *image)
Pekka Paalanena3525802014-06-12 16:49:29 +03002173{
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00002174 switch (image->import_type) {
2175 case IMPORT_TYPE_DIRECT:
2176 image->images[0] = import_simple_dmabuf(gr, &image->dmabuf->attributes);
2177 if (!image->images[0])
2178 return false;
2179 break;
Pekka Paalanena3525802014-06-12 16:49:29 +03002180
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00002181 case IMPORT_TYPE_GL_CONVERSION:
2182 if (!import_yuv_dmabuf(gr, image))
2183 return false;
2184 break;
2185
Pekka Paalanena3525802014-06-12 16:49:29 +03002186 default:
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00002187 weston_log("Invalid import type for dmabuf\n");
2188 return false;
Pekka Paalanena3525802014-06-12 16:49:29 +03002189 }
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00002190
2191 return true;
Pekka Paalanena3525802014-06-12 16:49:29 +03002192}
2193
2194static void
2195gl_renderer_attach_dmabuf(struct weston_surface *surface,
2196 struct weston_buffer *buffer,
2197 struct linux_dmabuf_buffer *dmabuf)
2198{
2199 struct gl_renderer *gr = get_renderer(surface->compositor);
2200 struct gl_surface_state *gs = get_surface_state(surface);
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00002201 struct dmabuf_image *image;
Pekka Paalanena3525802014-06-12 16:49:29 +03002202 int i;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00002203 int ret;
Pekka Paalanena3525802014-06-12 16:49:29 +03002204
2205 if (!gr->has_dmabuf_import) {
2206 linux_dmabuf_buffer_send_server_error(dmabuf,
2207 "EGL dmabuf import not supported");
2208 return;
2209 }
2210
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00002211 buffer->width = dmabuf->attributes.width;
2212 buffer->height = dmabuf->attributes.height;
Pekka Paalanen319397e2016-07-04 16:25:16 +03002213
2214 /*
2215 * GL-renderer uses the OpenGL convention of texture coordinates, where
2216 * the origin is at bottom-left. Because dmabuf buffers have the origin
2217 * at top-left, we must invert the Y_INVERT flag to get the image right.
2218 */
Pekka Paalanena3525802014-06-12 16:49:29 +03002219 buffer->y_inverted =
Pekka Paalanen319397e2016-07-04 16:25:16 +03002220 !(dmabuf->attributes.flags & ZWP_LINUX_BUFFER_PARAMS_V1_FLAGS_Y_INVERT);
Pekka Paalanena3525802014-06-12 16:49:29 +03002221
2222 for (i = 0; i < gs->num_images; i++)
2223 egl_image_unref(gs->images[i]);
2224 gs->num_images = 0;
2225
Pekka Paalanena3525802014-06-12 16:49:29 +03002226 /*
2227 * We try to always hold an imported EGLImage from the dmabuf
2228 * to prevent the client from preventing re-imports. But, we also
2229 * need to re-import every time the contents may change because
2230 * GL driver's caching may need flushing.
2231 *
2232 * Here we release the cache reference which has to be final.
2233 */
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00002234 image = linux_dmabuf_buffer_get_user_data(dmabuf);
Pekka Paalanena3525802014-06-12 16:49:29 +03002235
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00002236 /* The dmabuf_image should have been created during the import */
2237 assert(image != NULL);
2238
2239 for (i = 0; i < image->num_images; ++i) {
2240 ret = egl_image_unref(image->images[i]);
Pekka Paalanena3525802014-06-12 16:49:29 +03002241 assert(ret == 0);
2242 }
2243
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00002244 if (!import_known_dmabuf(gr, image)) {
2245 linux_dmabuf_buffer_send_server_error(dmabuf, "EGL dmabuf import failed");
2246 return;
Pekka Paalanena3525802014-06-12 16:49:29 +03002247 }
Pekka Paalanena3525802014-06-12 16:49:29 +03002248
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00002249 gs->num_images = image->num_images;
2250 for (i = 0; i < gs->num_images; ++i)
2251 gs->images[i] = egl_image_ref(image->images[i]);
Pekka Paalanena3525802014-06-12 16:49:29 +03002252
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00002253 gs->target = image->target;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00002254 ensure_textures(gs, gs->num_images);
2255 for (i = 0; i < gs->num_images; ++i) {
2256 glActiveTexture(GL_TEXTURE0 + i);
2257 glBindTexture(gs->target, gs->textures[i]);
2258 gr->image_target_texture_2d(gs->target, gs->images[i]->image);
2259 }
Pekka Paalanena3525802014-06-12 16:49:29 +03002260
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00002261 gs->shader = image->shader;
Pekka Paalanena3525802014-06-12 16:49:29 +03002262 gs->pitch = buffer->width;
2263 gs->height = buffer->height;
2264 gs->buffer_type = BUFFER_TYPE_EGL;
2265 gs->y_inverted = buffer->y_inverted;
2266}
2267
2268static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002269gl_renderer_attach(struct weston_surface *es, struct weston_buffer *buffer)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002270{
2271 struct weston_compositor *ec = es->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002272 struct gl_renderer *gr = get_renderer(ec);
2273 struct gl_surface_state *gs = get_surface_state(es);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002274 struct wl_shm_buffer *shm_buffer;
Pekka Paalanena3525802014-06-12 16:49:29 +03002275 struct linux_dmabuf_buffer *dmabuf;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03002276 EGLint format;
2277 int i;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002278
Pekka Paalanenfb003d32012-12-04 15:58:13 +02002279 weston_buffer_reference(&gs->buffer_ref, buffer);
2280
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002281 if (!buffer) {
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01002282 for (i = 0; i < gs->num_images; i++) {
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +03002283 egl_image_unref(gs->images[i]);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01002284 gs->images[i] = NULL;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002285 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01002286 gs->num_images = 0;
2287 glDeleteTextures(gs->num_textures, gs->textures);
2288 gs->num_textures = 0;
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +03002289 gs->buffer_type = BUFFER_TYPE_NULL;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002290 gs->y_inverted = 1;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002291 return;
2292 }
2293
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002294 shm_buffer = wl_shm_buffer_get(buffer->resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002295
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03002296 if (shm_buffer)
2297 gl_renderer_attach_shm(es, buffer, shm_buffer);
Vincent Abriou9c526e02016-11-03 11:15:32 +01002298 else if (gr->has_bind_display &&
2299 gr->query_buffer(gr->egl_display, (void *)buffer->resource,
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03002300 EGL_TEXTURE_FORMAT, &format))
2301 gl_renderer_attach_egl(es, buffer, format);
Pekka Paalanena3525802014-06-12 16:49:29 +03002302 else if ((dmabuf = linux_dmabuf_buffer_get(buffer->resource)))
2303 gl_renderer_attach_dmabuf(es, buffer, dmabuf);
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03002304 else {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002305 weston_log("unhandled buffer type!\n");
Pekka Paalanenfb003d32012-12-04 15:58:13 +02002306 weston_buffer_reference(&gs->buffer_ref, NULL);
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +03002307 gs->buffer_type = BUFFER_TYPE_NULL;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002308 gs->y_inverted = 1;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002309 }
2310}
2311
Kristian Høgsberg42263852012-09-06 21:59:29 -04002312static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002313gl_renderer_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002314 float red, float green, float blue, float alpha)
2315{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002316 struct gl_surface_state *gs = get_surface_state(surface);
2317 struct gl_renderer *gr = get_renderer(surface->compositor);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002318
2319 gs->color[0] = red;
2320 gs->color[1] = green;
2321 gs->color[2] = blue;
2322 gs->color[3] = alpha;
Pekka Paalanenaeb917e2015-02-09 13:56:56 +02002323 gs->buffer_type = BUFFER_TYPE_SOLID;
2324 gs->pitch = 1;
2325 gs->height = 1;
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002326
John Kåre Alsaker40684142012-11-13 19:10:25 +01002327 gs->shader = &gr->solid_shader;
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002328}
2329
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002330static void
Pekka Paalaneneb35cbe2015-02-09 13:37:27 +02002331gl_renderer_surface_get_content_size(struct weston_surface *surface,
2332 int *width, int *height)
2333{
2334 struct gl_surface_state *gs = get_surface_state(surface);
2335
2336 if (gs->buffer_type == BUFFER_TYPE_NULL) {
2337 *width = 0;
2338 *height = 0;
2339 } else {
2340 *width = gs->pitch;
2341 *height = gs->height;
2342 }
2343}
2344
2345static uint32_t
2346pack_color(pixman_format_code_t format, float *c)
2347{
2348 uint8_t r = round(c[0] * 255.0f);
2349 uint8_t g = round(c[1] * 255.0f);
2350 uint8_t b = round(c[2] * 255.0f);
2351 uint8_t a = round(c[3] * 255.0f);
2352
2353 switch (format) {
2354 case PIXMAN_a8b8g8r8:
2355 return (a << 24) | (b << 16) | (g << 8) | r;
2356 default:
2357 assert(0);
2358 return 0;
2359 }
2360}
2361
2362static int
2363gl_renderer_surface_copy_content(struct weston_surface *surface,
2364 void *target, size_t size,
2365 int src_x, int src_y,
2366 int width, int height)
2367{
2368 static const GLfloat verts[4 * 2] = {
2369 0.0f, 0.0f,
2370 1.0f, 0.0f,
2371 1.0f, 1.0f,
2372 0.0f, 1.0f
2373 };
2374 static const GLfloat projmat_normal[16] = { /* transpose */
2375 2.0f, 0.0f, 0.0f, 0.0f,
2376 0.0f, 2.0f, 0.0f, 0.0f,
2377 0.0f, 0.0f, 1.0f, 0.0f,
2378 -1.0f, -1.0f, 0.0f, 1.0f
2379 };
2380 static const GLfloat projmat_yinvert[16] = { /* transpose */
2381 2.0f, 0.0f, 0.0f, 0.0f,
2382 0.0f, -2.0f, 0.0f, 0.0f,
2383 0.0f, 0.0f, 1.0f, 0.0f,
2384 -1.0f, 1.0f, 0.0f, 1.0f
2385 };
2386 const pixman_format_code_t format = PIXMAN_a8b8g8r8;
2387 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
2388 const GLenum gl_format = GL_RGBA; /* PIXMAN_a8b8g8r8 little-endian */
2389 struct gl_renderer *gr = get_renderer(surface->compositor);
2390 struct gl_surface_state *gs = get_surface_state(surface);
2391 int cw, ch;
2392 GLuint fbo;
2393 GLuint tex;
2394 GLenum status;
2395 const GLfloat *proj;
2396 int i;
2397
2398 gl_renderer_surface_get_content_size(surface, &cw, &ch);
2399
2400 switch (gs->buffer_type) {
2401 case BUFFER_TYPE_NULL:
2402 return -1;
2403 case BUFFER_TYPE_SOLID:
2404 *(uint32_t *)target = pack_color(format, gs->color);
2405 return 0;
2406 case BUFFER_TYPE_SHM:
2407 gl_renderer_flush_damage(surface);
2408 /* fall through */
2409 case BUFFER_TYPE_EGL:
2410 break;
2411 }
2412
2413 glGenTextures(1, &tex);
2414 glBindTexture(GL_TEXTURE_2D, tex);
2415 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, cw, ch,
2416 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
2417 glBindTexture(GL_TEXTURE_2D, 0);
2418
2419 glGenFramebuffers(1, &fbo);
2420 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
2421 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
2422 GL_TEXTURE_2D, tex, 0);
2423
2424 status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
2425 if (status != GL_FRAMEBUFFER_COMPLETE) {
2426 weston_log("%s: fbo error: %#x\n", __func__, status);
2427 glDeleteFramebuffers(1, &fbo);
2428 glDeleteTextures(1, &tex);
2429 return -1;
2430 }
2431
2432 glViewport(0, 0, cw, ch);
2433 glDisable(GL_BLEND);
2434 use_shader(gr, gs->shader);
2435 if (gs->y_inverted)
2436 proj = projmat_normal;
2437 else
2438 proj = projmat_yinvert;
2439
2440 glUniformMatrix4fv(gs->shader->proj_uniform, 1, GL_FALSE, proj);
2441 glUniform1f(gs->shader->alpha_uniform, 1.0f);
2442
2443 for (i = 0; i < gs->num_textures; i++) {
2444 glUniform1i(gs->shader->tex_uniforms[i], i);
2445
2446 glActiveTexture(GL_TEXTURE0 + i);
2447 glBindTexture(gs->target, gs->textures[i]);
2448 glTexParameteri(gs->target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2449 glTexParameteri(gs->target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2450 }
2451
2452 /* position: */
2453 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, verts);
2454 glEnableVertexAttribArray(0);
2455
2456 /* texcoord: */
2457 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, verts);
2458 glEnableVertexAttribArray(1);
2459
2460 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2461
2462 glDisableVertexAttribArray(1);
2463 glDisableVertexAttribArray(0);
2464
2465 glPixelStorei(GL_PACK_ALIGNMENT, bytespp);
2466 glReadPixels(src_x, src_y, width, height, gl_format,
2467 GL_UNSIGNED_BYTE, target);
2468
2469 glDeleteFramebuffers(1, &fbo);
2470 glDeleteTextures(1, &tex);
2471
2472 return 0;
2473}
2474
2475static void
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002476surface_state_destroy(struct gl_surface_state *gs, struct gl_renderer *gr)
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002477{
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002478 int i;
2479
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002480 wl_list_remove(&gs->surface_destroy_listener.link);
2481 wl_list_remove(&gs->renderer_destroy_listener.link);
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002482
2483 gs->surface->renderer_state = NULL;
2484
2485 glDeleteTextures(gs->num_textures, gs->textures);
2486
2487 for (i = 0; i < gs->num_images; i++)
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +03002488 egl_image_unref(gs->images[i]);
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002489
2490 weston_buffer_reference(&gs->buffer_ref, NULL);
2491 pixman_region32_fini(&gs->texture_damage);
2492 free(gs);
2493}
2494
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002495static void
2496surface_state_handle_surface_destroy(struct wl_listener *listener, void *data)
2497{
2498 struct gl_surface_state *gs;
2499 struct gl_renderer *gr;
2500
2501 gs = container_of(listener, struct gl_surface_state,
2502 surface_destroy_listener);
2503
2504 gr = get_renderer(gs->surface->compositor);
2505
2506 surface_state_destroy(gs, gr);
2507}
2508
2509static void
2510surface_state_handle_renderer_destroy(struct wl_listener *listener, void *data)
2511{
2512 struct gl_surface_state *gs;
2513 struct gl_renderer *gr;
2514
2515 gr = data;
2516
2517 gs = container_of(listener, struct gl_surface_state,
2518 renderer_destroy_listener);
2519
2520 surface_state_destroy(gs, gr);
2521}
2522
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002523static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002524gl_renderer_create_surface(struct weston_surface *surface)
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002525{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002526 struct gl_surface_state *gs;
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002527 struct gl_renderer *gr = get_renderer(surface->compositor);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002528
Bryce Harringtonde16d892014-11-20 22:21:57 -08002529 gs = zalloc(sizeof *gs);
2530 if (gs == NULL)
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002531 return -1;
2532
Pekka Paalanen68033ac2012-12-04 15:58:15 +02002533 /* A buffer is never attached to solid color surfaces, yet
2534 * they still go through texcoord computations. Do not divide
2535 * by zero there.
2536 */
2537 gs->pitch = 1;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002538 gs->y_inverted = 1;
Pekka Paalanen68033ac2012-12-04 15:58:15 +02002539
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002540 gs->surface = surface;
2541
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02002542 pixman_region32_init(&gs->texture_damage);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002543 surface->renderer_state = gs;
2544
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002545 gs->surface_destroy_listener.notify =
2546 surface_state_handle_surface_destroy;
2547 wl_signal_add(&surface->destroy_signal,
2548 &gs->surface_destroy_listener);
2549
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002550 gs->renderer_destroy_listener.notify =
2551 surface_state_handle_renderer_destroy;
2552 wl_signal_add(&gr->destroy_signal,
2553 &gs->renderer_destroy_listener);
2554
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02002555 if (surface->buffer_ref.buffer) {
2556 gl_renderer_attach(surface, surface->buffer_ref.buffer);
2557 gl_renderer_flush_damage(surface);
2558 }
2559
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002560 return 0;
2561}
2562
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002563static const char vertex_shader[] =
2564 "uniform mat4 proj;\n"
2565 "attribute vec2 position;\n"
2566 "attribute vec2 texcoord;\n"
2567 "varying vec2 v_texcoord;\n"
2568 "void main()\n"
2569 "{\n"
2570 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
2571 " v_texcoord = texcoord;\n"
2572 "}\n";
2573
2574/* Declare common fragment shader uniforms */
2575#define FRAGMENT_CONVERT_YUV \
2576 " y *= alpha;\n" \
2577 " u *= alpha;\n" \
2578 " v *= alpha;\n" \
2579 " gl_FragColor.r = y + 1.59602678 * v;\n" \
2580 " gl_FragColor.g = y - 0.39176229 * u - 0.81296764 * v;\n" \
2581 " gl_FragColor.b = y + 2.01723214 * u;\n" \
2582 " gl_FragColor.a = alpha;\n"
2583
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002584static const char fragment_debug[] =
2585 " gl_FragColor = vec4(0.0, 0.3, 0.0, 0.2) + gl_FragColor * 0.8;\n";
2586
2587static const char fragment_brace[] =
2588 "}\n";
2589
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002590static const char texture_fragment_shader_rgba[] =
2591 "precision mediump float;\n"
2592 "varying vec2 v_texcoord;\n"
2593 "uniform sampler2D tex;\n"
2594 "uniform float alpha;\n"
2595 "void main()\n"
2596 "{\n"
2597 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002598 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002599
2600static const char texture_fragment_shader_rgbx[] =
2601 "precision mediump float;\n"
2602 "varying vec2 v_texcoord;\n"
2603 "uniform sampler2D tex;\n"
2604 "uniform float alpha;\n"
2605 "void main()\n"
2606 "{\n"
2607 " gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb\n;"
2608 " gl_FragColor.a = alpha;\n"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002609 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002610
2611static const char texture_fragment_shader_egl_external[] =
2612 "#extension GL_OES_EGL_image_external : require\n"
2613 "precision mediump float;\n"
2614 "varying vec2 v_texcoord;\n"
2615 "uniform samplerExternalOES tex;\n"
2616 "uniform float alpha;\n"
2617 "void main()\n"
2618 "{\n"
2619 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002620 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002621
2622static const char texture_fragment_shader_y_uv[] =
2623 "precision mediump float;\n"
2624 "uniform sampler2D tex;\n"
2625 "uniform sampler2D tex1;\n"
2626 "varying vec2 v_texcoord;\n"
2627 "uniform float alpha;\n"
2628 "void main() {\n"
2629 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
2630 " float u = texture2D(tex1, v_texcoord).r - 0.5;\n"
2631 " float v = texture2D(tex1, v_texcoord).g - 0.5;\n"
2632 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002633 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002634
2635static const char texture_fragment_shader_y_u_v[] =
2636 "precision mediump float;\n"
2637 "uniform sampler2D tex;\n"
2638 "uniform sampler2D tex1;\n"
2639 "uniform sampler2D tex2;\n"
2640 "varying vec2 v_texcoord;\n"
2641 "uniform float alpha;\n"
2642 "void main() {\n"
2643 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
2644 " float u = texture2D(tex1, v_texcoord).x - 0.5;\n"
2645 " float v = texture2D(tex2, v_texcoord).x - 0.5;\n"
2646 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002647 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002648
2649static const char texture_fragment_shader_y_xuxv[] =
2650 "precision mediump float;\n"
2651 "uniform sampler2D tex;\n"
2652 "uniform sampler2D tex1;\n"
2653 "varying vec2 v_texcoord;\n"
2654 "uniform float alpha;\n"
2655 "void main() {\n"
2656 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
2657 " float u = texture2D(tex1, v_texcoord).g - 0.5;\n"
2658 " float v = texture2D(tex1, v_texcoord).a - 0.5;\n"
2659 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002660 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002661
2662static const char solid_fragment_shader[] =
2663 "precision mediump float;\n"
2664 "uniform vec4 color;\n"
2665 "uniform float alpha;\n"
2666 "void main()\n"
2667 "{\n"
2668 " gl_FragColor = alpha * color\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002669 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002670
2671static int
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002672compile_shader(GLenum type, int count, const char **sources)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002673{
2674 GLuint s;
2675 char msg[512];
2676 GLint status;
2677
2678 s = glCreateShader(type);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002679 glShaderSource(s, count, sources, NULL);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002680 glCompileShader(s);
2681 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
2682 if (!status) {
2683 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
2684 weston_log("shader info: %s\n", msg);
2685 return GL_NONE;
2686 }
2687
2688 return s;
2689}
2690
2691static int
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03002692shader_init(struct gl_shader *shader, struct gl_renderer *renderer,
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002693 const char *vertex_source, const char *fragment_source)
2694{
2695 char msg[512];
2696 GLint status;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002697 int count;
2698 const char *sources[3];
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002699
2700 shader->vertex_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002701 compile_shader(GL_VERTEX_SHADER, 1, &vertex_source);
2702
2703 if (renderer->fragment_shader_debug) {
2704 sources[0] = fragment_source;
2705 sources[1] = fragment_debug;
2706 sources[2] = fragment_brace;
2707 count = 3;
2708 } else {
2709 sources[0] = fragment_source;
2710 sources[1] = fragment_brace;
2711 count = 2;
2712 }
2713
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002714 shader->fragment_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002715 compile_shader(GL_FRAGMENT_SHADER, count, sources);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002716
2717 shader->program = glCreateProgram();
2718 glAttachShader(shader->program, shader->vertex_shader);
2719 glAttachShader(shader->program, shader->fragment_shader);
2720 glBindAttribLocation(shader->program, 0, "position");
2721 glBindAttribLocation(shader->program, 1, "texcoord");
2722
2723 glLinkProgram(shader->program);
2724 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
2725 if (!status) {
2726 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
2727 weston_log("link info: %s\n", msg);
2728 return -1;
2729 }
2730
2731 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
2732 shader->tex_uniforms[0] = glGetUniformLocation(shader->program, "tex");
2733 shader->tex_uniforms[1] = glGetUniformLocation(shader->program, "tex1");
2734 shader->tex_uniforms[2] = glGetUniformLocation(shader->program, "tex2");
2735 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
2736 shader->color_uniform = glGetUniformLocation(shader->program, "color");
2737
2738 return 0;
2739}
2740
2741static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002742shader_release(struct gl_shader *shader)
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002743{
2744 glDeleteShader(shader->vertex_shader);
2745 glDeleteShader(shader->fragment_shader);
2746 glDeleteProgram(shader->program);
2747
2748 shader->vertex_shader = 0;
2749 shader->fragment_shader = 0;
2750 shader->program = 0;
2751}
2752
2753static void
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002754log_extensions(const char *name, const char *extensions)
2755{
2756 const char *p, *end;
2757 int l;
2758 int len;
2759
2760 l = weston_log("%s:", name);
2761 p = extensions;
2762 while (*p) {
2763 end = strchrnul(p, ' ');
2764 len = end - p;
2765 if (l + len > 78)
2766 l = weston_log_continue("\n" STAMP_SPACE "%.*s",
2767 len, p);
2768 else
2769 l += weston_log_continue(" %.*s", len, p);
2770 for (p = end; isspace(*p); p++)
2771 ;
2772 }
2773 weston_log_continue("\n");
2774}
2775
2776static void
2777log_egl_gl_info(EGLDisplay egldpy)
2778{
2779 const char *str;
2780
2781 str = eglQueryString(egldpy, EGL_VERSION);
2782 weston_log("EGL version: %s\n", str ? str : "(null)");
2783
2784 str = eglQueryString(egldpy, EGL_VENDOR);
2785 weston_log("EGL vendor: %s\n", str ? str : "(null)");
2786
2787 str = eglQueryString(egldpy, EGL_CLIENT_APIS);
2788 weston_log("EGL client APIs: %s\n", str ? str : "(null)");
2789
2790 str = eglQueryString(egldpy, EGL_EXTENSIONS);
2791 log_extensions("EGL extensions", str ? str : "(null)");
2792
2793 str = (char *)glGetString(GL_VERSION);
2794 weston_log("GL version: %s\n", str ? str : "(null)");
2795
2796 str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
2797 weston_log("GLSL version: %s\n", str ? str : "(null)");
2798
2799 str = (char *)glGetString(GL_VENDOR);
2800 weston_log("GL vendor: %s\n", str ? str : "(null)");
2801
2802 str = (char *)glGetString(GL_RENDERER);
2803 weston_log("GL renderer: %s\n", str ? str : "(null)");
2804
2805 str = (char *)glGetString(GL_EXTENSIONS);
2806 log_extensions("GL extensions", str ? str : "(null)");
2807}
2808
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03002809static void
2810log_egl_config_info(EGLDisplay egldpy, EGLConfig eglconfig)
2811{
2812 EGLint r, g, b, a;
2813
2814 weston_log("Chosen EGL config details:\n");
2815
2816 weston_log_continue(STAMP_SPACE "RGBA bits");
2817 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_RED_SIZE, &r) &&
2818 eglGetConfigAttrib(egldpy, eglconfig, EGL_GREEN_SIZE, &g) &&
2819 eglGetConfigAttrib(egldpy, eglconfig, EGL_BLUE_SIZE, &b) &&
2820 eglGetConfigAttrib(egldpy, eglconfig, EGL_ALPHA_SIZE, &a))
2821 weston_log_continue(": %d %d %d %d\n", r, g, b, a);
2822 else
2823 weston_log_continue(" unknown\n");
2824
2825 weston_log_continue(STAMP_SPACE "swap interval range");
2826 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_MIN_SWAP_INTERVAL, &a) &&
2827 eglGetConfigAttrib(egldpy, eglconfig, EGL_MAX_SWAP_INTERVAL, &b))
2828 weston_log_continue(": %d - %d\n", a, b);
2829 else
2830 weston_log_continue(" unknown\n");
2831}
2832
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002833static int
Derek Foremane76f1852015-05-15 12:12:39 -05002834match_config_to_visual(EGLDisplay egl_display,
2835 EGLint visual_id,
2836 EGLConfig *configs,
2837 int count)
2838{
2839 int i;
2840
2841 for (i = 0; i < count; ++i) {
2842 EGLint id;
2843
2844 if (!eglGetConfigAttrib(egl_display,
2845 configs[i], EGL_NATIVE_VISUAL_ID,
2846 &id))
2847 continue;
2848
2849 if (id == visual_id)
2850 return i;
2851 }
2852
Derek Foremane76f1852015-05-15 12:12:39 -05002853 return -1;
2854}
2855
2856static int
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002857egl_choose_config(struct gl_renderer *gr, const EGLint *attribs,
Derek Foremane76f1852015-05-15 12:12:39 -05002858 const EGLint *visual_id, const int n_ids,
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002859 EGLConfig *config_out)
2860{
2861 EGLint count = 0;
2862 EGLint matched = 0;
2863 EGLConfig *configs;
Derek Foremane76f1852015-05-15 12:12:39 -05002864 int i, config_index = -1;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002865
Derek Foremana7e19912015-05-20 14:57:58 -05002866 if (!eglGetConfigs(gr->egl_display, NULL, 0, &count) || count < 1) {
2867 weston_log("No EGL configs to choose from.\n");
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002868 return -1;
Derek Foremana7e19912015-05-20 14:57:58 -05002869 }
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002870 configs = calloc(count, sizeof *configs);
2871 if (!configs)
2872 return -1;
2873
2874 if (!eglChooseConfig(gr->egl_display, attribs, configs,
Derek Foremana7e19912015-05-20 14:57:58 -05002875 count, &matched) || !matched) {
2876 weston_log("No EGL configs with appropriate attributes.\n");
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002877 goto out;
Derek Foremana7e19912015-05-20 14:57:58 -05002878 }
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002879
Miguel A. Vico684c9f42016-05-18 17:48:47 +02002880 if (!visual_id || n_ids == 0)
Derek Foremane76f1852015-05-15 12:12:39 -05002881 config_index = 0;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002882
Derek Foremane76f1852015-05-15 12:12:39 -05002883 for (i = 0; config_index == -1 && i < n_ids; i++)
2884 config_index = match_config_to_visual(gr->egl_display,
2885 visual_id[i],
2886 configs,
2887 matched);
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002888
Derek Foremane76f1852015-05-15 12:12:39 -05002889 if (config_index != -1)
2890 *config_out = configs[config_index];
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002891
2892out:
2893 free(configs);
Derek Foremane76f1852015-05-15 12:12:39 -05002894 if (config_index == -1)
2895 return -1;
2896
Derek Foremana7e19912015-05-20 14:57:58 -05002897 if (i > 1)
2898 weston_log("Unable to use first choice EGL config with id"
2899 " 0x%x, succeeded with alternate id 0x%x.\n",
2900 visual_id[0], visual_id[i - 1]);
Derek Foremane76f1852015-05-15 12:12:39 -05002901 return 0;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002902}
2903
John Kåre Alsaker44154502012-11-13 19:10:20 +01002904static void
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05002905gl_renderer_output_set_border(struct weston_output *output,
2906 enum gl_renderer_border_side side,
2907 int32_t width, int32_t height,
2908 int32_t tex_width, unsigned char *data)
2909{
2910 struct gl_output_state *go = get_output_state(output);
2911
Jason Ekstrande5512d42014-02-04 21:36:38 -06002912 if (go->borders[side].width != width ||
2913 go->borders[side].height != height)
2914 /* In this case, we have to blow everything and do a full
2915 * repaint. */
2916 go->border_status |= BORDER_SIZE_CHANGED | BORDER_ALL_DIRTY;
2917
2918 if (data == NULL) {
2919 width = 0;
2920 height = 0;
2921 }
2922
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05002923 go->borders[side].width = width;
2924 go->borders[side].height = height;
2925 go->borders[side].tex_width = tex_width;
2926 go->borders[side].data = data;
Jason Ekstrande5512d42014-02-04 21:36:38 -06002927 go->border_status |= 1 << side;
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05002928}
2929
John Kåre Alsaker94659272012-11-13 19:10:18 +01002930static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002931gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01002932
Miguel A. Vico967b6bc2016-05-18 17:50:54 +02002933static EGLSurface
2934gl_renderer_create_window_surface(struct gl_renderer *gr,
2935 EGLNativeWindowType window_for_legacy,
2936 void *window_for_platform,
2937 const EGLint *config_attribs,
2938 const EGLint *visual_id,
2939 int n_ids)
2940{
2941 EGLSurface egl_surface = EGL_NO_SURFACE;
2942 EGLConfig egl_config;
2943
2944 if (egl_choose_config(gr, config_attribs, visual_id,
2945 n_ids, &egl_config) == -1) {
2946 weston_log("failed to choose EGL config for output\n");
2947 return EGL_NO_SURFACE;
2948 }
2949
2950 if (egl_config != gr->egl_config &&
2951 !gr->has_configless_context) {
2952 weston_log("attempted to use a different EGL config for an "
Armin Krezoviće3bfee12016-09-30 14:27:38 +02002953 "output but EGL_KHR_no_config_context or "
2954 "EGL_MESA_configless_context is not supported\n");
Miguel A. Vico967b6bc2016-05-18 17:50:54 +02002955 return EGL_NO_SURFACE;
2956 }
2957
2958 log_egl_config_info(gr->egl_display, egl_config);
2959
2960 if (gr->create_platform_window)
2961 egl_surface = gr->create_platform_window(gr->egl_display,
2962 egl_config,
2963 window_for_platform,
2964 NULL);
2965 else
2966 egl_surface = eglCreateWindowSurface(gr->egl_display,
2967 egl_config,
2968 window_for_legacy, NULL);
2969
2970 return egl_surface;
2971}
2972
2973static int
2974gl_renderer_output_create(struct weston_output *output,
2975 EGLSurface surface)
2976{
2977 struct gl_output_state *go;
2978 int i;
2979
2980 go = zalloc(sizeof *go);
2981 if (go == NULL)
2982 return -1;
2983
2984 go->egl_surface = surface;
2985
2986 for (i = 0; i < BUFFER_DAMAGE_COUNT; i++)
2987 pixman_region32_init(&go->buffer_damage[i]);
2988
Alexandros Frantzisdf0e4b92017-09-27 15:09:16 +03002989 wl_list_init(&go->timeline_render_point_list);
2990
Miguel A. Vico967b6bc2016-05-18 17:50:54 +02002991 output->renderer_state = go;
2992
2993 return 0;
2994}
2995
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002996static int
Miguel A. Vicoc095cde2016-05-18 17:43:00 +02002997gl_renderer_output_window_create(struct weston_output *output,
2998 EGLNativeWindowType window_for_legacy,
2999 void *window_for_platform,
Miguel A. Vico4057cd92016-05-18 17:44:22 +02003000 const EGLint *config_attribs,
Miguel A. Vicoc095cde2016-05-18 17:43:00 +02003001 const EGLint *visual_id,
3002 int n_ids)
John Kåre Alsaker94659272012-11-13 19:10:18 +01003003{
3004 struct weston_compositor *ec = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003005 struct gl_renderer *gr = get_renderer(ec);
Miguel A. Vico967b6bc2016-05-18 17:50:54 +02003006 EGLSurface egl_surface = EGL_NO_SURFACE;
3007 int ret = 0;
John Kåre Alsaker94659272012-11-13 19:10:18 +01003008
Miguel A. Vico967b6bc2016-05-18 17:50:54 +02003009 egl_surface = gl_renderer_create_window_surface(gr,
3010 window_for_legacy,
3011 window_for_platform,
3012 config_attribs,
3013 visual_id, n_ids);
3014 if (egl_surface == EGL_NO_SURFACE) {
John Kåre Alsaker94659272012-11-13 19:10:18 +01003015 weston_log("failed to create egl surface\n");
John Kåre Alsaker94659272012-11-13 19:10:18 +01003016 return -1;
3017 }
3018
Miguel A. Vico967b6bc2016-05-18 17:50:54 +02003019 ret = gl_renderer_output_create(output, egl_surface);
3020 if (ret < 0)
Emil Velikov02639552016-11-14 17:08:17 +00003021 weston_platform_destroy_egl_surface(gr->egl_display, egl_surface);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003022
Miguel A. Vico967b6bc2016-05-18 17:50:54 +02003023 return ret;
John Kåre Alsaker94659272012-11-13 19:10:18 +01003024}
3025
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003026static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003027gl_renderer_output_destroy(struct weston_output *output)
John Kåre Alsaker94659272012-11-13 19:10:18 +01003028{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003029 struct gl_renderer *gr = get_renderer(output->compositor);
3030 struct gl_output_state *go = get_output_state(output);
Alexandros Frantzisdf0e4b92017-09-27 15:09:16 +03003031 struct timeline_render_point *trp, *tmp;
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003032 int i;
3033
3034 for (i = 0; i < 2; i++)
3035 pixman_region32_fini(&go->buffer_damage[i]);
John Kåre Alsaker94659272012-11-13 19:10:18 +01003036
Dima Ryazanov6a38ad72016-11-23 18:41:00 -08003037 eglMakeCurrent(gr->egl_display,
3038 EGL_NO_SURFACE, EGL_NO_SURFACE,
3039 EGL_NO_CONTEXT);
3040
Emil Velikov02639552016-11-14 17:08:17 +00003041 weston_platform_destroy_egl_surface(gr->egl_display, go->egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01003042
Alexandros Frantzisdf0e4b92017-09-27 15:09:16 +03003043 if (!wl_list_empty(&go->timeline_render_point_list))
3044 weston_log("warning: discarding pending timeline render"
3045 "objects at output destruction");
3046
3047 wl_list_for_each_safe(trp, tmp, &go->timeline_render_point_list, link)
3048 timeline_render_point_destroy(trp);
3049
John Kåre Alsaker94659272012-11-13 19:10:18 +01003050 free(go);
3051}
3052
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003053static EGLSurface
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003054gl_renderer_output_surface(struct weston_output *output)
John Kåre Alsaker94659272012-11-13 19:10:18 +01003055{
3056 return get_output_state(output)->egl_surface;
3057}
3058
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03003059static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003060gl_renderer_destroy(struct weston_compositor *ec)
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04003061{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003062 struct gl_renderer *gr = get_renderer(ec);
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00003063 struct dmabuf_image *image, *next;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003064
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03003065 wl_signal_emit(&gr->destroy_signal, gr);
3066
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003067 if (gr->has_bind_display)
3068 gr->unbind_display(gr->egl_display, ec->wl_display);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003069
3070 /* Work around crash in egl_dri2.c's dri2_make_current() - when does this apply? */
3071 eglMakeCurrent(gr->egl_display,
3072 EGL_NO_SURFACE, EGL_NO_SURFACE,
3073 EGL_NO_CONTEXT);
3074
Pekka Paalanena3525802014-06-12 16:49:29 +03003075
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00003076 wl_list_for_each_safe(image, next, &gr->dmabuf_images, link)
3077 dmabuf_image_destroy(image);
Pekka Paalanena3525802014-06-12 16:49:29 +03003078
Armin Krezović28d240f2016-06-23 11:59:35 +02003079 if (gr->dummy_surface != EGL_NO_SURFACE)
Emil Velikov02639552016-11-14 17:08:17 +00003080 weston_platform_destroy_egl_surface(gr->egl_display,
3081 gr->dummy_surface);
Armin Krezović28d240f2016-06-23 11:59:35 +02003082
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003083 eglTerminate(gr->egl_display);
3084 eglReleaseThread();
Scott Moreau976a0502013-03-07 10:15:17 -07003085
Armin Krezović36d699a2016-08-05 15:28:30 +02003086 wl_list_remove(&gr->output_destroy_listener.link);
3087
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -04003088 wl_array_release(&gr->vertices);
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -04003089 wl_array_release(&gr->vtxcnt);
3090
Mariusz Ceiercbb91582014-02-08 20:11:24 +01003091 if (gr->fragment_binding)
3092 weston_binding_destroy(gr->fragment_binding);
3093 if (gr->fan_binding)
3094 weston_binding_destroy(gr->fan_binding);
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03003095
Scott Moreau976a0502013-03-07 10:15:17 -07003096 free(gr);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003097}
3098
Pekka Paalanen8b69d032015-04-08 17:02:22 +03003099static void
3100renderer_setup_egl_client_extensions(struct gl_renderer *gr)
3101{
3102 const char *extensions;
3103
3104 extensions = eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS);
3105 if (!extensions) {
3106 weston_log("Retrieving EGL client extension string failed.\n");
3107 return;
3108 }
3109
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003110 if (weston_check_egl_extension(extensions, "EGL_EXT_platform_base"))
Pekka Paalanen8b69d032015-04-08 17:02:22 +03003111 gr->create_platform_window =
3112 (void *) eglGetProcAddress("eglCreatePlatformWindowSurfaceEXT");
3113 else
3114 weston_log("warning: EGL_EXT_platform_base not supported.\n");
3115}
3116
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003117static int
Neil Robertsb7f85332014-03-07 18:05:49 +00003118gl_renderer_setup_egl_extensions(struct weston_compositor *ec)
3119{
Emil Velikov43cea542016-11-14 16:03:45 +00003120 static const struct {
3121 char *extension, *entrypoint;
3122 } swap_damage_ext_to_entrypoint[] = {
3123 {
3124 .extension = "EGL_EXT_swap_buffers_with_damage",
3125 .entrypoint = "eglSwapBuffersWithDamageEXT",
3126 },
3127 {
3128 .extension = "EGL_KHR_swap_buffers_with_damage",
3129 .entrypoint = "eglSwapBuffersWithDamageKHR",
3130 },
3131 };
Neil Robertsb7f85332014-03-07 18:05:49 +00003132 struct gl_renderer *gr = get_renderer(ec);
3133 const char *extensions;
3134 EGLBoolean ret;
Bryce Harringtonfe0410b2016-11-21 10:02:42 -08003135 unsigned i;
Neil Robertsb7f85332014-03-07 18:05:49 +00003136
3137 gr->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3138 gr->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
Varad Gautam0775cd12016-11-23 14:03:20 +05303139
Neil Robertsb7f85332014-03-07 18:05:49 +00003140 gr->bind_display =
3141 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
3142 gr->unbind_display =
3143 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
3144 gr->query_buffer =
3145 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
3146
3147 extensions =
3148 (const char *) eglQueryString(gr->egl_display, EGL_EXTENSIONS);
3149 if (!extensions) {
3150 weston_log("Retrieving EGL extension string failed.\n");
3151 return -1;
3152 }
3153
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003154 if (weston_check_egl_extension(extensions, "EGL_WL_bind_wayland_display"))
Neil Robertsb7f85332014-03-07 18:05:49 +00003155 gr->has_bind_display = 1;
3156 if (gr->has_bind_display) {
3157 ret = gr->bind_display(gr->egl_display, ec->wl_display);
3158 if (!ret)
3159 gr->has_bind_display = 0;
3160 }
3161
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003162 if (weston_check_egl_extension(extensions, "EGL_EXT_buffer_age"))
Neil Robertsb7f85332014-03-07 18:05:49 +00003163 gr->has_egl_buffer_age = 1;
3164 else
3165 weston_log("warning: EGL_EXT_buffer_age not supported. "
3166 "Performance could be affected.\n");
3167
Bryce Harringtonfe0410b2016-11-21 10:02:42 -08003168 for (i = 0; i < ARRAY_LENGTH(swap_damage_ext_to_entrypoint); i++) {
Emil Velikov43cea542016-11-14 16:03:45 +00003169 if (weston_check_egl_extension(extensions,
3170 swap_damage_ext_to_entrypoint[i].extension)) {
3171 gr->swap_buffers_with_damage =
3172 (void *) eglGetProcAddress(
3173 swap_damage_ext_to_entrypoint[i].entrypoint);
3174 break;
3175 }
3176 }
3177 if (!gr->swap_buffers_with_damage)
3178 weston_log("warning: neither %s or %s is supported. "
3179 "Performance could be affected.\n",
3180 swap_damage_ext_to_entrypoint[0].extension,
3181 swap_damage_ext_to_entrypoint[1].extension);
Neil Robertsb7f85332014-03-07 18:05:49 +00003182
Armin Krezoviće3bfee12016-09-30 14:27:38 +02003183 if (weston_check_egl_extension(extensions, "EGL_KHR_no_config_context") ||
3184 weston_check_egl_extension(extensions, "EGL_MESA_configless_context"))
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003185 gr->has_configless_context = 1;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003186
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003187 if (weston_check_egl_extension(extensions, "EGL_KHR_surfaceless_context"))
Armin Krezović28d240f2016-06-23 11:59:35 +02003188 gr->has_surfaceless_context = 1;
3189
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003190 if (weston_check_egl_extension(extensions, "EGL_EXT_image_dma_buf_import"))
Pekka Paalanena3525802014-06-12 16:49:29 +03003191 gr->has_dmabuf_import = 1;
Pekka Paalanena3525802014-06-12 16:49:29 +03003192
Varad Gautam0775cd12016-11-23 14:03:20 +05303193 if (weston_check_egl_extension(extensions,
3194 "EGL_EXT_image_dma_buf_import_modifiers")) {
3195 gr->query_dmabuf_formats =
3196 (void *) eglGetProcAddress("eglQueryDmaBufFormatsEXT");
3197 gr->query_dmabuf_modifiers =
3198 (void *) eglGetProcAddress("eglQueryDmaBufModifiersEXT");
3199 gr->has_dmabuf_import_modifiers = 1;
3200 }
3201
Vincent Abrioufdeefe42016-10-05 14:54:34 +02003202 if (weston_check_egl_extension(extensions, "GL_EXT_texture_rg"))
3203 gr->has_gl_texture_rg = 1;
3204
Alexandros Frantzis7192b172017-09-27 15:09:14 +03003205 if (weston_check_egl_extension(extensions, "EGL_KHR_fence_sync") &&
3206 weston_check_egl_extension(extensions, "EGL_ANDROID_native_fence_sync")) {
3207 gr->create_sync =
3208 (void *) eglGetProcAddress("eglCreateSyncKHR");
3209 gr->destroy_sync =
3210 (void *) eglGetProcAddress("eglDestroySyncKHR");
3211 gr->dup_native_fence_fd =
3212 (void *) eglGetProcAddress("eglDupNativeFenceFDANDROID");
3213 gr->has_native_fence_sync = 1;
Alexandros Frantzisdf0e4b92017-09-27 15:09:16 +03003214 } else {
3215 weston_log("warning: Disabling render GPU timeline due to "
3216 "missing EGL_ANDROID_native_fence_sync extension\n");
Alexandros Frantzis7192b172017-09-27 15:09:14 +03003217 }
3218
Pekka Paalanen8b69d032015-04-08 17:02:22 +03003219 renderer_setup_egl_client_extensions(gr);
3220
Neil Robertsb7f85332014-03-07 18:05:49 +00003221 return 0;
3222}
3223
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003224static const EGLint gl_renderer_opaque_attribs[] = {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003225 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
3226 EGL_RED_SIZE, 1,
3227 EGL_GREEN_SIZE, 1,
3228 EGL_BLUE_SIZE, 1,
3229 EGL_ALPHA_SIZE, 0,
3230 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
3231 EGL_NONE
3232};
3233
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003234static const EGLint gl_renderer_alpha_attribs[] = {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003235 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
3236 EGL_RED_SIZE, 1,
3237 EGL_GREEN_SIZE, 1,
3238 EGL_BLUE_SIZE, 1,
3239 EGL_ALPHA_SIZE, 1,
3240 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
3241 EGL_NONE
3242};
3243
Armin Krezović28d240f2016-06-23 11:59:35 +02003244
Jonny Lamb70eba3f2015-03-20 15:26:50 +01003245/** Checks whether a platform EGL client extension is supported
3246 *
3247 * \param ec The weston compositor
3248 * \param extension_suffix The EGL client extension suffix
3249 * \return 1 if supported, 0 if using fallbacks, -1 unsupported
3250 *
3251 * This function checks whether a specific platform_* extension is supported
3252 * by EGL.
3253 *
3254 * The extension suffix should be the suffix of the platform extension (that
3255 * specifies a <platform> argument as defined in EGL_EXT_platform_base). For
3256 * example, passing "foo" will check whether either "EGL_KHR_platform_foo",
3257 * "EGL_EXT_platform_foo", or "EGL_MESA_platform_foo" is supported.
3258 *
3259 * The return value is 1:
3260 * - if the supplied EGL client extension is supported.
3261 * The return value is 0:
3262 * - if the platform_base client extension isn't supported so will
3263 * fallback to eglGetDisplay and friends.
3264 * The return value is -1:
3265 * - if the supplied EGL client extension is not supported.
3266 */
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003267static int
Jonny Lamb70eba3f2015-03-20 15:26:50 +01003268gl_renderer_supports(struct weston_compositor *ec,
3269 const char *extension_suffix)
3270{
3271 static const char *extensions = NULL;
3272 char s[64];
3273
3274 if (!extensions) {
3275 extensions = (const char *) eglQueryString(
3276 EGL_NO_DISPLAY, EGL_EXTENSIONS);
3277
3278 if (!extensions)
3279 return 0;
3280
3281 log_extensions("EGL client extensions",
3282 extensions);
3283 }
3284
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003285 if (!weston_check_egl_extension(extensions, "EGL_EXT_platform_base"))
Pekka Paalanenf2824542015-04-08 17:02:21 +03003286 return 0;
3287
Jonny Lamb70eba3f2015-03-20 15:26:50 +01003288 snprintf(s, sizeof s, "EGL_KHR_platform_%s", extension_suffix);
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003289 if (weston_check_egl_extension(extensions, s))
Jonny Lamb70eba3f2015-03-20 15:26:50 +01003290 return 1;
3291
3292 snprintf(s, sizeof s, "EGL_EXT_platform_%s", extension_suffix);
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003293 if (weston_check_egl_extension(extensions, s))
Jonny Lamb70eba3f2015-03-20 15:26:50 +01003294 return 1;
3295
3296 snprintf(s, sizeof s, "EGL_MESA_platform_%s", extension_suffix);
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003297 if (weston_check_egl_extension(extensions, s))
Jonny Lamb70eba3f2015-03-20 15:26:50 +01003298 return 1;
3299
Pekka Paalanenf2824542015-04-08 17:02:21 +03003300 /* at this point we definitely have some platform extensions but
3301 * haven't found the supplied platform, so chances are it's
Jonny Lamb70eba3f2015-03-20 15:26:50 +01003302 * not supported. */
3303
3304 return -1;
3305}
3306
Jonny Lamb74eed312015-03-24 13:12:04 +01003307static const char *
3308platform_to_extension(EGLenum platform)
3309{
3310 switch (platform) {
3311 case EGL_PLATFORM_GBM_KHR:
3312 return "gbm";
3313 case EGL_PLATFORM_WAYLAND_KHR:
3314 return "wayland";
3315 case EGL_PLATFORM_X11_KHR:
3316 return "x11";
3317 default:
3318 assert(0 && "bad EGL platform enum");
3319 }
3320}
3321
Armin Krezović36d699a2016-08-05 15:28:30 +02003322static void
3323output_handle_destroy(struct wl_listener *listener, void *data)
3324{
3325 struct gl_renderer *gr;
3326 struct weston_output *output = data;
3327
3328 gr = container_of(listener, struct gl_renderer,
3329 output_destroy_listener);
3330
3331 if (wl_list_empty(&output->compositor->output_list))
3332 eglMakeCurrent(gr->egl_display, gr->dummy_surface,
3333 gr->dummy_surface, gr->egl_context);
3334}
3335
Jonny Lamb70eba3f2015-03-20 15:26:50 +01003336static int
Armin Krezović28d240f2016-06-23 11:59:35 +02003337gl_renderer_create_pbuffer_surface(struct gl_renderer *gr) {
3338 EGLConfig pbuffer_config;
3339
3340 static const EGLint pbuffer_config_attribs[] = {
3341 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
3342 EGL_RED_SIZE, 1,
3343 EGL_GREEN_SIZE, 1,
3344 EGL_BLUE_SIZE, 1,
3345 EGL_ALPHA_SIZE, 0,
3346 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
3347 EGL_NONE
3348 };
3349
3350 static const EGLint pbuffer_attribs[] = {
3351 EGL_WIDTH, 10,
3352 EGL_HEIGHT, 10,
3353 EGL_NONE
3354 };
3355
3356 if (egl_choose_config(gr, pbuffer_config_attribs, NULL, 0, &pbuffer_config) < 0) {
Derek Foreman12968e32017-06-26 14:42:44 -05003357 weston_log("failed to choose EGL config for PbufferSurface\n");
Armin Krezović28d240f2016-06-23 11:59:35 +02003358 return -1;
3359 }
3360
3361 gr->dummy_surface = eglCreatePbufferSurface(gr->egl_display,
3362 pbuffer_config,
3363 pbuffer_attribs);
3364
3365 if (gr->dummy_surface == EGL_NO_SURFACE) {
3366 weston_log("failed to create PbufferSurface\n");
3367 return -1;
3368 }
3369
3370 return 0;
3371}
3372
3373static int
Miguel A. Vicodddc6702016-05-18 17:41:07 +02003374gl_renderer_display_create(struct weston_compositor *ec, EGLenum platform,
Miguel A. Vico41700e32016-05-18 17:47:59 +02003375 void *native_window, const EGLint *platform_attribs,
3376 const EGLint *config_attribs, const EGLint *visual_id, int n_ids)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003377{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003378 struct gl_renderer *gr;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003379 EGLint major, minor;
Jonny Lamb74eed312015-03-24 13:12:04 +01003380 int supports = 0;
3381
3382 if (platform) {
3383 supports = gl_renderer_supports(
3384 ec, platform_to_extension(platform));
3385 if (supports < 0)
3386 return -1;
3387 }
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003388
Bryce Harringtonde16d892014-11-20 22:21:57 -08003389 gr = zalloc(sizeof *gr);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003390 if (gr == NULL)
3391 return -1;
3392
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003393 gr->base.read_pixels = gl_renderer_read_pixels;
3394 gr->base.repaint_output = gl_renderer_repaint_output;
3395 gr->base.flush_damage = gl_renderer_flush_damage;
3396 gr->base.attach = gl_renderer_attach;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003397 gr->base.surface_set_color = gl_renderer_surface_set_color;
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03003398 gr->base.destroy = gl_renderer_destroy;
Pekka Paalaneneb35cbe2015-02-09 13:37:27 +02003399 gr->base.surface_get_content_size =
3400 gl_renderer_surface_get_content_size;
3401 gr->base.surface_copy_content = gl_renderer_surface_copy_content;
Jonny Lamb74eed312015-03-24 13:12:04 +01003402 gr->egl_display = NULL;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003403
Jonny Lambf1ec5062015-03-24 13:12:05 +01003404 /* extension_suffix is supported */
Jonny Lamb74eed312015-03-24 13:12:04 +01003405 if (supports) {
3406 if (!get_platform_display) {
3407 get_platform_display = (void *) eglGetProcAddress(
3408 "eglGetPlatformDisplayEXT");
3409 }
Jonny Lamb70eba3f2015-03-20 15:26:50 +01003410
Jonny Lamb74eed312015-03-24 13:12:04 +01003411 /* also wrap this in the supports check because
3412 * eglGetProcAddress can return non-NULL and still not
3413 * support the feature at runtime, so ensure the
3414 * appropriate extension checks have been done. */
3415 if (get_platform_display && platform) {
3416 gr->egl_display = get_platform_display(platform,
3417 native_window,
Miguel A. Vico41700e32016-05-18 17:47:59 +02003418 platform_attribs);
Jonny Lamb74eed312015-03-24 13:12:04 +01003419 }
3420 }
Jonny Lamb74eed312015-03-24 13:12:04 +01003421
3422 if (!gr->egl_display) {
Jonny Lamb70eba3f2015-03-20 15:26:50 +01003423 weston_log("warning: either no EGL_EXT_platform_base "
3424 "support or specific platform support; "
3425 "falling back to eglGetDisplay.\n");
3426 gr->egl_display = eglGetDisplay(native_window);
Jonny Lamb70eba3f2015-03-20 15:26:50 +01003427 }
Jonny Lamb70eba3f2015-03-20 15:26:50 +01003428
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003429 if (gr->egl_display == EGL_NO_DISPLAY) {
3430 weston_log("failed to create display\n");
Derek Foreman066ca0c2015-06-11 12:14:45 -05003431 goto fail;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003432 }
3433
3434 if (!eglInitialize(gr->egl_display, &major, &minor)) {
3435 weston_log("failed to initialize display\n");
Derek Foreman066ca0c2015-06-11 12:14:45 -05003436 goto fail_with_error;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003437 }
3438
Miguel A. Vico4057cd92016-05-18 17:44:22 +02003439 if (egl_choose_config(gr, config_attribs, visual_id,
Derek Foremane76f1852015-05-15 12:12:39 -05003440 n_ids, &gr->egl_config) < 0) {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003441 weston_log("failed to choose EGL config\n");
Dawid Gajownik1a912a92015-08-21 00:20:54 -03003442 goto fail_terminate;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003443 }
3444
3445 ec->renderer = &gr->base;
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003446 ec->capabilities |= WESTON_CAP_ROTATION_ANY;
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003447 ec->capabilities |= WESTON_CAP_CAPTURE_YFLIP;
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +02003448 ec->capabilities |= WESTON_CAP_VIEW_CLIP_MASK;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003449
Neil Robertsb7f85332014-03-07 18:05:49 +00003450 if (gl_renderer_setup_egl_extensions(ec) < 0)
Derek Foreman066ca0c2015-06-11 12:14:45 -05003451 goto fail_with_error;
Neil Robertsb7f85332014-03-07 18:05:49 +00003452
Pekka Paalanena3525802014-06-12 16:49:29 +03003453 wl_list_init(&gr->dmabuf_images);
Varad Gautam0775cd12016-11-23 14:03:20 +05303454 if (gr->has_dmabuf_import) {
Pekka Paalanena3525802014-06-12 16:49:29 +03003455 gr->base.import_dmabuf = gl_renderer_import_dmabuf;
Varad Gautam0775cd12016-11-23 14:03:20 +05303456 gr->base.query_dmabuf_formats =
3457 gl_renderer_query_dmabuf_formats;
3458 gr->base.query_dmabuf_modifiers =
3459 gl_renderer_query_dmabuf_modifiers;
3460 }
Pekka Paalanena3525802014-06-12 16:49:29 +03003461
Armin Krezović28d240f2016-06-23 11:59:35 +02003462 if (gr->has_surfaceless_context) {
3463 weston_log("EGL_KHR_surfaceless_context available\n");
3464 gr->dummy_surface = EGL_NO_SURFACE;
3465 } else {
3466 weston_log("EGL_KHR_surfaceless_context unavailable. "
3467 "Trying PbufferSurface\n");
3468
3469 if (gl_renderer_create_pbuffer_surface(gr) < 0)
3470 goto fail_with_error;
3471 }
3472
Tomeu Vizoso12072b62013-08-06 20:05:55 +02003473 wl_display_add_shm_format(ec->wl_display, WL_SHM_FORMAT_RGB565);
Vincent Abrioufdeefe42016-10-05 14:54:34 +02003474 wl_display_add_shm_format(ec->wl_display, WL_SHM_FORMAT_YUV420);
Vincent Abriou00a03d22016-10-05 14:54:35 +02003475 wl_display_add_shm_format(ec->wl_display, WL_SHM_FORMAT_NV12);
Vincent ABRIOUe5732c72016-10-20 16:20:11 +02003476 wl_display_add_shm_format(ec->wl_display, WL_SHM_FORMAT_YUYV);
Tomeu Vizoso12072b62013-08-06 20:05:55 +02003477
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03003478 wl_signal_init(&gr->destroy_signal);
3479
Armin Krezović28d240f2016-06-23 11:59:35 +02003480 if (gl_renderer_setup(ec, gr->dummy_surface) < 0) {
3481 if (gr->dummy_surface != EGL_NO_SURFACE)
Emil Velikov02639552016-11-14 17:08:17 +00003482 weston_platform_destroy_egl_surface(gr->egl_display,
3483 gr->dummy_surface);
Armin Krezović28d240f2016-06-23 11:59:35 +02003484 goto fail_with_error;
3485 }
3486
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003487 return 0;
3488
Derek Foreman066ca0c2015-06-11 12:14:45 -05003489fail_with_error:
Pekka Paalanen326529f2012-11-27 12:25:25 +02003490 gl_renderer_print_egl_error_state();
Dawid Gajownik1a912a92015-08-21 00:20:54 -03003491fail_terminate:
3492 eglTerminate(gr->egl_display);
Derek Foreman066ca0c2015-06-11 12:14:45 -05003493fail:
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003494 free(gr);
3495 return -1;
3496}
3497
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003498static EGLDisplay
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003499gl_renderer_display(struct weston_compositor *ec)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003500{
3501 return get_renderer(ec)->egl_display;
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04003502}
3503
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003504static int
3505compile_shaders(struct weston_compositor *ec)
3506{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003507 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker40684142012-11-13 19:10:25 +01003508
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03003509 gr->texture_shader_rgba.vertex_source = vertex_shader;
3510 gr->texture_shader_rgba.fragment_source = texture_fragment_shader_rgba;
3511
3512 gr->texture_shader_rgbx.vertex_source = vertex_shader;
3513 gr->texture_shader_rgbx.fragment_source = texture_fragment_shader_rgbx;
3514
3515 gr->texture_shader_egl_external.vertex_source = vertex_shader;
3516 gr->texture_shader_egl_external.fragment_source =
3517 texture_fragment_shader_egl_external;
3518
3519 gr->texture_shader_y_uv.vertex_source = vertex_shader;
3520 gr->texture_shader_y_uv.fragment_source = texture_fragment_shader_y_uv;
3521
3522 gr->texture_shader_y_u_v.vertex_source = vertex_shader;
3523 gr->texture_shader_y_u_v.fragment_source =
3524 texture_fragment_shader_y_u_v;
3525
Ander Conselvan de Oliveira41a50ea2013-11-27 17:43:51 +02003526 gr->texture_shader_y_xuxv.vertex_source = vertex_shader;
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03003527 gr->texture_shader_y_xuxv.fragment_source =
3528 texture_fragment_shader_y_xuxv;
3529
3530 gr->solid_shader.vertex_source = vertex_shader;
3531 gr->solid_shader.fragment_source = solid_fragment_shader;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003532
3533 return 0;
3534}
3535
3536static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003537fragment_debug_binding(struct weston_keyboard *keyboard, uint32_t time,
3538 uint32_t key, void *data)
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003539{
3540 struct weston_compositor *ec = data;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003541 struct gl_renderer *gr = get_renderer(ec);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003542 struct weston_output *output;
3543
John Kåre Alsaker40684142012-11-13 19:10:25 +01003544 gr->fragment_shader_debug ^= 1;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003545
John Kåre Alsaker40684142012-11-13 19:10:25 +01003546 shader_release(&gr->texture_shader_rgba);
3547 shader_release(&gr->texture_shader_rgbx);
3548 shader_release(&gr->texture_shader_egl_external);
3549 shader_release(&gr->texture_shader_y_uv);
3550 shader_release(&gr->texture_shader_y_u_v);
3551 shader_release(&gr->texture_shader_y_xuxv);
3552 shader_release(&gr->solid_shader);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003553
Ander Conselvan de Oliveira03fb4ef2012-12-03 17:08:11 +02003554 /* Force use_shader() to call glUseProgram(), since we need to use
3555 * the recompiled version of the shader. */
3556 gr->current_shader = NULL;
3557
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003558 wl_list_for_each(output, &ec->output_list, link)
3559 weston_output_damage(output);
3560}
3561
Kristian Høgsberg8799d412013-05-07 10:50:09 -04003562static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003563fan_debug_repaint_binding(struct weston_keyboard *keyboard, uint32_t time,
3564 uint32_t key, void *data)
Kristian Høgsberg8799d412013-05-07 10:50:09 -04003565{
3566 struct weston_compositor *compositor = data;
3567 struct gl_renderer *gr = get_renderer(compositor);
3568
3569 gr->fan_debug = !gr->fan_debug;
3570 weston_compositor_damage_all(compositor);
3571}
3572
John Kåre Alsaker94659272012-11-13 19:10:18 +01003573static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003574gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003575{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003576 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003577 const char *extensions;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003578 EGLConfig context_config;
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003579 EGLBoolean ret;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003580
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003581 static const EGLint context_attribs[] = {
3582 EGL_CONTEXT_CLIENT_VERSION, 2,
3583 EGL_NONE
3584 };
3585
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003586 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
3587 weston_log("failed to bind EGL_OPENGL_ES_API\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02003588 gl_renderer_print_egl_error_state();
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003589 return -1;
3590 }
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03003591
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003592 context_config = gr->egl_config;
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03003593
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003594 if (gr->has_configless_context)
Armin Krezoviće3bfee12016-09-30 14:27:38 +02003595 context_config = EGL_NO_CONFIG_KHR;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003596
3597 gr->egl_context = eglCreateContext(gr->egl_display, context_config,
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003598 EGL_NO_CONTEXT, context_attribs);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003599 if (gr->egl_context == NULL) {
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003600 weston_log("failed to create context\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02003601 gl_renderer_print_egl_error_state();
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003602 return -1;
3603 }
3604
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003605 ret = eglMakeCurrent(gr->egl_display, egl_surface,
3606 egl_surface, gr->egl_context);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003607 if (ret == EGL_FALSE) {
3608 weston_log("Failed to make EGL context current.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02003609 gl_renderer_print_egl_error_state();
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003610 return -1;
3611 }
3612
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003613 log_egl_gl_info(gr->egl_display);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003614
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003615 gr->image_target_texture_2d =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003616 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003617
3618 extensions = (const char *) glGetString(GL_EXTENSIONS);
3619 if (!extensions) {
3620 weston_log("Retrieving GL extension string failed.\n");
3621 return -1;
3622 }
3623
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003624 if (!weston_check_egl_extension(extensions, "GL_EXT_texture_format_BGRA8888")) {
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003625 weston_log("GL_EXT_texture_format_BGRA8888 not available\n");
3626 return -1;
3627 }
3628
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003629 if (weston_check_egl_extension(extensions, "GL_EXT_read_format_bgra"))
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01003630 ec->read_format = PIXMAN_a8r8g8b8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003631 else
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01003632 ec->read_format = PIXMAN_a8b8g8r8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003633
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003634 if (weston_check_egl_extension(extensions, "GL_EXT_unpack_subimage"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003635 gr->has_unpack_subimage = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003636
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003637 if (weston_check_egl_extension(extensions, "GL_OES_EGL_image_external"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003638 gr->has_egl_image_external = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003639
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003640 glActiveTexture(GL_TEXTURE0);
3641
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003642 if (compile_shaders(ec))
3643 return -1;
3644
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03003645 gr->fragment_binding =
3646 weston_compositor_add_debug_binding(ec, KEY_S,
3647 fragment_debug_binding,
3648 ec);
3649 gr->fan_binding =
3650 weston_compositor_add_debug_binding(ec, KEY_F,
3651 fan_debug_repaint_binding,
3652 ec);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003653
Armin Krezović36d699a2016-08-05 15:28:30 +02003654 gr->output_destroy_listener.notify = output_handle_destroy;
3655 wl_signal_add(&ec->output_destroyed_signal,
3656 &gr->output_destroy_listener);
3657
Pekka Paalanen035a0322012-10-24 09:43:06 +03003658 weston_log("GL ES 2 renderer features:\n");
3659 weston_log_continue(STAMP_SPACE "read-back format: %s\n",
Pekka Paalanenfe4eacf2013-01-10 16:50:42 +02003660 ec->read_format == PIXMAN_a8r8g8b8 ? "BGRA" : "RGBA");
Pekka Paalanen035a0322012-10-24 09:43:06 +03003661 weston_log_continue(STAMP_SPACE "wl_shm sub-image to texture: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003662 gr->has_unpack_subimage ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03003663 weston_log_continue(STAMP_SPACE "EGL Wayland extension: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003664 gr->has_bind_display ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03003665
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003666
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003667 return 0;
3668}
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003669
3670WL_EXPORT struct gl_renderer_interface gl_renderer_interface = {
3671 .opaque_attribs = gl_renderer_opaque_attribs,
3672 .alpha_attribs = gl_renderer_alpha_attribs,
3673
Miguel A. Vicodddc6702016-05-18 17:41:07 +02003674 .display_create = gl_renderer_display_create,
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003675 .display = gl_renderer_display,
Miguel A. Vicoc095cde2016-05-18 17:43:00 +02003676 .output_window_create = gl_renderer_output_window_create,
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003677 .output_destroy = gl_renderer_output_destroy,
3678 .output_surface = gl_renderer_output_surface,
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05003679 .output_set_border = gl_renderer_output_set_border,
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003680 .print_egl_error_state = gl_renderer_print_egl_error_state
3681};