blob: 5749aa716164951160c680780ac682287b1a4c72 [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;
1738
Pekka Paalanena3525802014-06-12 16:49:29 +03001739 /* This requires the Mesa commit in
1740 * Mesa 10.3 (08264e5dad4df448e7718e782ad9077902089a07) or
1741 * Mesa 10.2.7 (55d28925e6109a4afd61f109e845a8a51bd17652).
1742 * Otherwise Mesa closes the fd behind our back and re-importing
1743 * will fail.
1744 * https://bugs.freedesktop.org/show_bug.cgi?id=76188
1745 */
1746
1747 attribs[atti++] = EGL_WIDTH;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001748 attribs[atti++] = attributes->width;
Pekka Paalanena3525802014-06-12 16:49:29 +03001749 attribs[atti++] = EGL_HEIGHT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001750 attribs[atti++] = attributes->height;
Pekka Paalanena3525802014-06-12 16:49:29 +03001751 attribs[atti++] = EGL_LINUX_DRM_FOURCC_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001752 attribs[atti++] = attributes->format;
Pekka Paalanena3525802014-06-12 16:49:29 +03001753
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001754 if (attributes->n_planes > 0) {
Pekka Paalanena3525802014-06-12 16:49:29 +03001755 attribs[atti++] = EGL_DMA_BUF_PLANE0_FD_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001756 attribs[atti++] = attributes->fd[0];
Pekka Paalanena3525802014-06-12 16:49:29 +03001757 attribs[atti++] = EGL_DMA_BUF_PLANE0_OFFSET_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001758 attribs[atti++] = attributes->offset[0];
Pekka Paalanena3525802014-06-12 16:49:29 +03001759 attribs[atti++] = EGL_DMA_BUF_PLANE0_PITCH_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001760 attribs[atti++] = attributes->stride[0];
Varad Gautamf7da8b32016-11-23 14:03:18 +05301761 if (gr->has_dmabuf_import_modifiers) {
1762 attribs[atti++] = EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT;
1763 attribs[atti++] = attributes->modifier[0] & 0xFFFFFFFF;
1764 attribs[atti++] = EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT;
1765 attribs[atti++] = attributes->modifier[0] >> 32;
1766 }
Pekka Paalanena3525802014-06-12 16:49:29 +03001767 }
1768
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001769 if (attributes->n_planes > 1) {
Pekka Paalanena3525802014-06-12 16:49:29 +03001770 attribs[atti++] = EGL_DMA_BUF_PLANE1_FD_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001771 attribs[atti++] = attributes->fd[1];
Pekka Paalanena3525802014-06-12 16:49:29 +03001772 attribs[atti++] = EGL_DMA_BUF_PLANE1_OFFSET_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001773 attribs[atti++] = attributes->offset[1];
Pekka Paalanena3525802014-06-12 16:49:29 +03001774 attribs[atti++] = EGL_DMA_BUF_PLANE1_PITCH_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001775 attribs[atti++] = attributes->stride[1];
Varad Gautamf7da8b32016-11-23 14:03:18 +05301776 if (gr->has_dmabuf_import_modifiers) {
1777 attribs[atti++] = EGL_DMA_BUF_PLANE1_MODIFIER_LO_EXT;
1778 attribs[atti++] = attributes->modifier[1] & 0xFFFFFFFF;
1779 attribs[atti++] = EGL_DMA_BUF_PLANE1_MODIFIER_HI_EXT;
1780 attribs[atti++] = attributes->modifier[1] >> 32;
1781 }
Pekka Paalanena3525802014-06-12 16:49:29 +03001782 }
1783
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001784 if (attributes->n_planes > 2) {
Pekka Paalanena3525802014-06-12 16:49:29 +03001785 attribs[atti++] = EGL_DMA_BUF_PLANE2_FD_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001786 attribs[atti++] = attributes->fd[2];
Pekka Paalanena3525802014-06-12 16:49:29 +03001787 attribs[atti++] = EGL_DMA_BUF_PLANE2_OFFSET_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001788 attribs[atti++] = attributes->offset[2];
Pekka Paalanena3525802014-06-12 16:49:29 +03001789 attribs[atti++] = EGL_DMA_BUF_PLANE2_PITCH_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001790 attribs[atti++] = attributes->stride[2];
Varad Gautamf7da8b32016-11-23 14:03:18 +05301791 if (gr->has_dmabuf_import_modifiers) {
1792 attribs[atti++] = EGL_DMA_BUF_PLANE2_MODIFIER_LO_EXT;
1793 attribs[atti++] = attributes->modifier[2] & 0xFFFFFFFF;
1794 attribs[atti++] = EGL_DMA_BUF_PLANE2_MODIFIER_HI_EXT;
1795 attribs[atti++] = attributes->modifier[2] >> 32;
1796 }
Pekka Paalanena3525802014-06-12 16:49:29 +03001797 }
1798
Varad Gautamc32e05b2016-11-23 14:03:19 +05301799 if (gr->has_dmabuf_import_modifiers) {
1800 if (attributes->n_planes > 3) {
1801 attribs[atti++] = EGL_DMA_BUF_PLANE3_FD_EXT;
1802 attribs[atti++] = attributes->fd[3];
1803 attribs[atti++] = EGL_DMA_BUF_PLANE3_OFFSET_EXT;
1804 attribs[atti++] = attributes->offset[3];
1805 attribs[atti++] = EGL_DMA_BUF_PLANE3_PITCH_EXT;
1806 attribs[atti++] = attributes->stride[3];
1807 attribs[atti++] = EGL_DMA_BUF_PLANE3_MODIFIER_LO_EXT;
1808 attribs[atti++] = attributes->modifier[3] & 0xFFFFFFFF;
1809 attribs[atti++] = EGL_DMA_BUF_PLANE3_MODIFIER_HI_EXT;
1810 attribs[atti++] = attributes->modifier[3] >> 32;
1811 }
1812 }
1813
Pekka Paalanena3525802014-06-12 16:49:29 +03001814 attribs[atti++] = EGL_NONE;
1815
1816 image = egl_image_create(gr, EGL_LINUX_DMA_BUF_EXT, NULL,
1817 attribs);
1818
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001819 return image;
1820}
Pekka Paalanena3525802014-06-12 16:49:29 +03001821
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001822/* The kernel header drm_fourcc.h defines the DRM formats below. We duplicate
1823 * some of the definitions here so that building Weston won't require
1824 * bleeding-edge kernel headers.
1825 */
1826#ifndef DRM_FORMAT_R8
1827#define DRM_FORMAT_R8 fourcc_code('R', '8', ' ', ' ') /* [7:0] R */
1828#endif
1829
1830#ifndef DRM_FORMAT_GR88
1831#define DRM_FORMAT_GR88 fourcc_code('G', 'R', '8', '8') /* [15:0] G:R 8:8 little endian */
1832#endif
1833
1834struct yuv_format_descriptor yuv_formats[] = {
1835 {
1836 .format = DRM_FORMAT_YUYV,
1837 .input_planes = 1,
1838 .output_planes = 2,
1839 .texture_type = EGL_TEXTURE_Y_XUXV_WL,
1840 {{
1841 .width_divisor = 1,
1842 .height_divisor = 1,
1843 .format = DRM_FORMAT_GR88,
1844 .plane_index = 0
1845 }, {
1846 .width_divisor = 2,
1847 .height_divisor = 1,
1848 .format = DRM_FORMAT_ARGB8888,
1849 .plane_index = 0
1850 }}
1851 }, {
1852 .format = DRM_FORMAT_NV12,
1853 .input_planes = 2,
1854 .output_planes = 2,
1855 .texture_type = EGL_TEXTURE_Y_UV_WL,
1856 {{
1857 .width_divisor = 1,
1858 .height_divisor = 1,
1859 .format = DRM_FORMAT_R8,
1860 .plane_index = 0
1861 }, {
1862 .width_divisor = 2,
1863 .height_divisor = 2,
1864 .format = DRM_FORMAT_GR88,
1865 .plane_index = 1
1866 }}
1867 }, {
1868 .format = DRM_FORMAT_YUV420,
1869 .input_planes = 3,
1870 .output_planes = 3,
1871 .texture_type = EGL_TEXTURE_Y_U_V_WL,
1872 {{
1873 .width_divisor = 1,
1874 .height_divisor = 1,
1875 .format = DRM_FORMAT_R8,
1876 .plane_index = 0
1877 }, {
1878 .width_divisor = 2,
1879 .height_divisor = 2,
1880 .format = DRM_FORMAT_R8,
1881 .plane_index = 1
1882 }, {
1883 .width_divisor = 2,
1884 .height_divisor = 2,
1885 .format = DRM_FORMAT_R8,
1886 .plane_index = 2
1887 }}
Matthias Treydteaca3ffb2016-07-25 12:15:41 +02001888 }, {
1889 .format = DRM_FORMAT_YUV444,
1890 .input_planes = 3,
1891 .output_planes = 3,
1892 .texture_type = EGL_TEXTURE_Y_U_V_WL,
1893 {{
1894 .width_divisor = 1,
1895 .height_divisor = 1,
1896 .format = DRM_FORMAT_R8,
1897 .plane_index = 0
1898 }, {
1899 .width_divisor = 1,
1900 .height_divisor = 1,
1901 .format = DRM_FORMAT_R8,
1902 .plane_index = 1
1903 }, {
1904 .width_divisor = 1,
1905 .height_divisor = 1,
1906 .format = DRM_FORMAT_R8,
1907 .plane_index = 2
1908 }}
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001909 }
1910};
1911
1912static struct egl_image *
1913import_dmabuf_single_plane(struct gl_renderer *gr,
1914 const struct dmabuf_attributes *attributes,
1915 struct yuv_plane_descriptor *descriptor)
1916{
1917 struct dmabuf_attributes plane;
1918 struct egl_image *image;
1919 char fmt[4];
1920
1921 plane.width = attributes->width / descriptor->width_divisor;
1922 plane.height = attributes->height / descriptor->height_divisor;
1923 plane.format = descriptor->format;
1924 plane.n_planes = 1;
1925 plane.fd[0] = attributes->fd[descriptor->plane_index];
1926 plane.offset[0] = attributes->offset[descriptor->plane_index];
1927 plane.stride[0] = attributes->stride[descriptor->plane_index];
1928 plane.modifier[0] = attributes->modifier[descriptor->plane_index];
1929
1930 image = import_simple_dmabuf(gr, &plane);
1931 if (!image) {
1932 weston_log("Failed to import plane %d as %.4s\n",
1933 descriptor->plane_index,
1934 dump_format(descriptor->format, fmt));
1935 return NULL;
1936 }
1937
1938 return image;
1939}
1940
1941static bool
1942import_yuv_dmabuf(struct gl_renderer *gr,
1943 struct dmabuf_image *image)
1944{
1945 unsigned i;
1946 int j;
1947 int ret;
1948 struct yuv_format_descriptor *format = NULL;
1949 struct dmabuf_attributes *attributes = &image->dmabuf->attributes;
1950 char fmt[4];
1951
1952 for (i = 0; i < ARRAY_LENGTH(yuv_formats); ++i) {
1953 if (yuv_formats[i].format == attributes->format) {
1954 format = &yuv_formats[i];
1955 break;
1956 }
1957 }
1958
1959 if (!format) {
1960 weston_log("Error during import, and no known conversion for format "
Derek Foreman12968e32017-06-26 14:42:44 -05001961 "%.4s in the renderer\n",
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001962 dump_format(attributes->format, fmt));
1963 return false;
1964 }
1965
1966 if (attributes->n_planes != format->input_planes) {
Derek Foreman12968e32017-06-26 14:42:44 -05001967 weston_log("%.4s dmabuf must contain %d plane%s (%d provided)\n",
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001968 dump_format(format->format, fmt),
1969 format->input_planes,
1970 (format->input_planes > 1) ? "s" : "",
1971 attributes->n_planes);
1972 return false;
1973 }
1974
1975 for (j = 0; j < format->output_planes; ++j) {
1976 image->images[j] = import_dmabuf_single_plane(gr, attributes,
1977 &format->plane[j]);
1978 if (!image->images[j]) {
1979 while (j) {
1980 ret = egl_image_unref(image->images[--j]);
1981 assert(ret == 0);
1982 }
1983 return false;
1984 }
1985 }
1986
1987 image->num_images = format->output_planes;
1988
1989 switch (format->texture_type) {
1990 case EGL_TEXTURE_Y_XUXV_WL:
1991 image->shader = &gr->texture_shader_y_xuxv;
1992 break;
1993 case EGL_TEXTURE_Y_UV_WL:
1994 image->shader = &gr->texture_shader_y_uv;
1995 break;
1996 case EGL_TEXTURE_Y_U_V_WL:
1997 image->shader = &gr->texture_shader_y_u_v;
1998 break;
1999 default:
2000 assert(false);
2001 }
2002
2003 return true;
2004}
2005
2006static GLenum
2007choose_texture_target(struct dmabuf_attributes *attributes)
2008{
2009 if (attributes->n_planes > 1)
2010 return GL_TEXTURE_EXTERNAL_OES;
2011
2012 switch (attributes->format & ~DRM_FORMAT_BIG_ENDIAN) {
2013 case DRM_FORMAT_YUYV:
2014 case DRM_FORMAT_YVYU:
2015 case DRM_FORMAT_UYVY:
2016 case DRM_FORMAT_VYUY:
2017 case DRM_FORMAT_AYUV:
2018 return GL_TEXTURE_EXTERNAL_OES;
2019 default:
2020 return GL_TEXTURE_2D;
2021 }
2022}
2023
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00002024static struct dmabuf_image *
2025import_dmabuf(struct gl_renderer *gr,
2026 struct linux_dmabuf_buffer *dmabuf)
2027{
2028 struct egl_image *egl_image;
2029 struct dmabuf_image *image;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00002030
2031 image = dmabuf_image_create();
Pekka Paalanena3525802014-06-12 16:49:29 +03002032 image->dmabuf = dmabuf;
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00002033
2034 egl_image = import_simple_dmabuf(gr, &dmabuf->attributes);
2035 if (egl_image) {
2036 image->num_images = 1;
2037 image->images[0] = egl_image;
2038 image->import_type = IMPORT_TYPE_DIRECT;
2039 image->target = choose_texture_target(&dmabuf->attributes);
2040
2041 switch (image->target) {
2042 case GL_TEXTURE_2D:
2043 image->shader = &gr->texture_shader_rgba;
2044 break;
2045 default:
2046 image->shader = &gr->texture_shader_egl_external;
2047 }
2048 } else {
2049 if (!import_yuv_dmabuf(gr, image)) {
2050 dmabuf_image_destroy(image);
2051 return NULL;
2052 }
2053 image->import_type = IMPORT_TYPE_GL_CONVERSION;
2054 image->target = GL_TEXTURE_2D;
2055 }
Pekka Paalanena3525802014-06-12 16:49:29 +03002056
2057 return image;
2058}
2059
2060static bool
Varad Gautam0775cd12016-11-23 14:03:20 +05302061gl_renderer_query_dmabuf_formats(struct weston_compositor *wc,
2062 int **formats, int *num_formats)
2063{
2064 struct gl_renderer *gr = get_renderer(wc);
2065 EGLint num;
2066
2067 assert(gr->has_dmabuf_import);
2068
2069 if (!gr->has_dmabuf_import_modifiers ||
2070 !gr->query_dmabuf_formats(gr->egl_display, 0, NULL, &num)) {
2071 *num_formats = 0;
2072 return false;
2073 }
2074
2075 *formats = calloc(num, sizeof(int));
2076 if (*formats == NULL) {
2077 *num_formats = 0;
2078 return false;
2079 }
2080 if (!gr->query_dmabuf_formats(gr->egl_display, num, *formats,
2081 (EGLint*) &num)) {
2082 *num_formats = 0;
2083 free(*formats);
2084 return false;
2085 }
2086
2087 *num_formats = num;
2088 return true;
2089}
2090
2091static bool
2092gl_renderer_query_dmabuf_modifiers(struct weston_compositor *wc, int format,
2093 uint64_t **modifiers,
2094 int *num_modifiers)
2095{
2096 struct gl_renderer *gr = get_renderer(wc);
2097 int num;
2098
2099 assert(gr->has_dmabuf_import);
2100
2101 if (!gr->has_dmabuf_import_modifiers ||
2102 !gr->query_dmabuf_modifiers(gr->egl_display, format, 0, NULL,
2103 NULL, &num)) {
2104 *num_modifiers = 0;
2105 return false;
2106 }
2107
2108 *modifiers = calloc(num, sizeof(uint64_t));
2109 if (*modifiers == NULL) {
2110 *num_modifiers = 0;
2111 return false;
2112 }
2113 if (!gr->query_dmabuf_modifiers(gr->egl_display, format,
2114 num, *modifiers, NULL, &num)) {
2115 *num_modifiers = 0;
2116 free(*modifiers);
2117 return false;
2118 }
2119
2120 *num_modifiers = num;
2121 return true;
2122}
2123
2124static bool
Pekka Paalanena3525802014-06-12 16:49:29 +03002125gl_renderer_import_dmabuf(struct weston_compositor *ec,
2126 struct linux_dmabuf_buffer *dmabuf)
2127{
2128 struct gl_renderer *gr = get_renderer(ec);
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00002129 struct dmabuf_image *image;
Pekka Paalanena3525802014-06-12 16:49:29 +03002130 int i;
2131
2132 assert(gr->has_dmabuf_import);
2133
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00002134 for (i = 0; i < dmabuf->attributes.n_planes; i++) {
Varad Gautamf7da8b32016-11-23 14:03:18 +05302135 /* return if EGL doesn't support import modifiers */
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00002136 if (dmabuf->attributes.modifier[i] != 0)
Varad Gautamf7da8b32016-11-23 14:03:18 +05302137 if (!gr->has_dmabuf_import_modifiers)
2138 return false;
2139
2140 /* return if modifiers passed are unequal */
2141 if (dmabuf->attributes.modifier[i] !=
2142 dmabuf->attributes.modifier[0])
Pekka Paalanena3525802014-06-12 16:49:29 +03002143 return false;
2144 }
2145
2146 /* reject all flags we do not recognize or handle */
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00002147 if (dmabuf->attributes.flags & ~ZWP_LINUX_BUFFER_PARAMS_V1_FLAGS_Y_INVERT)
Pekka Paalanena3525802014-06-12 16:49:29 +03002148 return false;
2149
2150 image = import_dmabuf(gr, dmabuf);
2151 if (!image)
2152 return false;
2153
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00002154 wl_list_insert(&gr->dmabuf_images, &image->link);
2155 linux_dmabuf_buffer_set_user_data(dmabuf, image,
2156 gl_renderer_destroy_dmabuf);
Pekka Paalanena3525802014-06-12 16:49:29 +03002157
2158 return true;
2159}
2160
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00002161static bool
2162import_known_dmabuf(struct gl_renderer *gr,
2163 struct dmabuf_image *image)
Pekka Paalanena3525802014-06-12 16:49:29 +03002164{
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00002165 switch (image->import_type) {
2166 case IMPORT_TYPE_DIRECT:
2167 image->images[0] = import_simple_dmabuf(gr, &image->dmabuf->attributes);
2168 if (!image->images[0])
2169 return false;
2170 break;
Pekka Paalanena3525802014-06-12 16:49:29 +03002171
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00002172 case IMPORT_TYPE_GL_CONVERSION:
2173 if (!import_yuv_dmabuf(gr, image))
2174 return false;
2175 break;
2176
Pekka Paalanena3525802014-06-12 16:49:29 +03002177 default:
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00002178 weston_log("Invalid import type for dmabuf\n");
2179 return false;
Pekka Paalanena3525802014-06-12 16:49:29 +03002180 }
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00002181
2182 return true;
Pekka Paalanena3525802014-06-12 16:49:29 +03002183}
2184
2185static void
2186gl_renderer_attach_dmabuf(struct weston_surface *surface,
2187 struct weston_buffer *buffer,
2188 struct linux_dmabuf_buffer *dmabuf)
2189{
2190 struct gl_renderer *gr = get_renderer(surface->compositor);
2191 struct gl_surface_state *gs = get_surface_state(surface);
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00002192 struct dmabuf_image *image;
Pekka Paalanena3525802014-06-12 16:49:29 +03002193 int i;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00002194 int ret;
Pekka Paalanena3525802014-06-12 16:49:29 +03002195
2196 if (!gr->has_dmabuf_import) {
2197 linux_dmabuf_buffer_send_server_error(dmabuf,
2198 "EGL dmabuf import not supported");
2199 return;
2200 }
2201
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00002202 buffer->width = dmabuf->attributes.width;
2203 buffer->height = dmabuf->attributes.height;
Pekka Paalanen319397e2016-07-04 16:25:16 +03002204
2205 /*
2206 * GL-renderer uses the OpenGL convention of texture coordinates, where
2207 * the origin is at bottom-left. Because dmabuf buffers have the origin
2208 * at top-left, we must invert the Y_INVERT flag to get the image right.
2209 */
Pekka Paalanena3525802014-06-12 16:49:29 +03002210 buffer->y_inverted =
Pekka Paalanen319397e2016-07-04 16:25:16 +03002211 !(dmabuf->attributes.flags & ZWP_LINUX_BUFFER_PARAMS_V1_FLAGS_Y_INVERT);
Pekka Paalanena3525802014-06-12 16:49:29 +03002212
2213 for (i = 0; i < gs->num_images; i++)
2214 egl_image_unref(gs->images[i]);
2215 gs->num_images = 0;
2216
Pekka Paalanena3525802014-06-12 16:49:29 +03002217 /*
2218 * We try to always hold an imported EGLImage from the dmabuf
2219 * to prevent the client from preventing re-imports. But, we also
2220 * need to re-import every time the contents may change because
2221 * GL driver's caching may need flushing.
2222 *
2223 * Here we release the cache reference which has to be final.
2224 */
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00002225 image = linux_dmabuf_buffer_get_user_data(dmabuf);
Pekka Paalanena3525802014-06-12 16:49:29 +03002226
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00002227 /* The dmabuf_image should have been created during the import */
2228 assert(image != NULL);
2229
2230 for (i = 0; i < image->num_images; ++i) {
2231 ret = egl_image_unref(image->images[i]);
Pekka Paalanena3525802014-06-12 16:49:29 +03002232 assert(ret == 0);
2233 }
2234
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00002235 if (!import_known_dmabuf(gr, image)) {
2236 linux_dmabuf_buffer_send_server_error(dmabuf, "EGL dmabuf import failed");
2237 return;
Pekka Paalanena3525802014-06-12 16:49:29 +03002238 }
Pekka Paalanena3525802014-06-12 16:49:29 +03002239
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00002240 gs->num_images = image->num_images;
2241 for (i = 0; i < gs->num_images; ++i)
2242 gs->images[i] = egl_image_ref(image->images[i]);
Pekka Paalanena3525802014-06-12 16:49:29 +03002243
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00002244 gs->target = image->target;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00002245 ensure_textures(gs, gs->num_images);
2246 for (i = 0; i < gs->num_images; ++i) {
2247 glActiveTexture(GL_TEXTURE0 + i);
2248 glBindTexture(gs->target, gs->textures[i]);
2249 gr->image_target_texture_2d(gs->target, gs->images[i]->image);
2250 }
Pekka Paalanena3525802014-06-12 16:49:29 +03002251
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00002252 gs->shader = image->shader;
Pekka Paalanena3525802014-06-12 16:49:29 +03002253 gs->pitch = buffer->width;
2254 gs->height = buffer->height;
2255 gs->buffer_type = BUFFER_TYPE_EGL;
2256 gs->y_inverted = buffer->y_inverted;
2257}
2258
2259static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002260gl_renderer_attach(struct weston_surface *es, struct weston_buffer *buffer)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002261{
2262 struct weston_compositor *ec = es->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002263 struct gl_renderer *gr = get_renderer(ec);
2264 struct gl_surface_state *gs = get_surface_state(es);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002265 struct wl_shm_buffer *shm_buffer;
Pekka Paalanena3525802014-06-12 16:49:29 +03002266 struct linux_dmabuf_buffer *dmabuf;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03002267 EGLint format;
2268 int i;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002269
Pekka Paalanenfb003d32012-12-04 15:58:13 +02002270 weston_buffer_reference(&gs->buffer_ref, buffer);
2271
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002272 if (!buffer) {
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01002273 for (i = 0; i < gs->num_images; i++) {
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +03002274 egl_image_unref(gs->images[i]);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01002275 gs->images[i] = NULL;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002276 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01002277 gs->num_images = 0;
2278 glDeleteTextures(gs->num_textures, gs->textures);
2279 gs->num_textures = 0;
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +03002280 gs->buffer_type = BUFFER_TYPE_NULL;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002281 gs->y_inverted = 1;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002282 return;
2283 }
2284
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002285 shm_buffer = wl_shm_buffer_get(buffer->resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002286
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03002287 if (shm_buffer)
2288 gl_renderer_attach_shm(es, buffer, shm_buffer);
Vincent Abriou9c526e02016-11-03 11:15:32 +01002289 else if (gr->has_bind_display &&
2290 gr->query_buffer(gr->egl_display, (void *)buffer->resource,
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03002291 EGL_TEXTURE_FORMAT, &format))
2292 gl_renderer_attach_egl(es, buffer, format);
Pekka Paalanena3525802014-06-12 16:49:29 +03002293 else if ((dmabuf = linux_dmabuf_buffer_get(buffer->resource)))
2294 gl_renderer_attach_dmabuf(es, buffer, dmabuf);
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03002295 else {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002296 weston_log("unhandled buffer type!\n");
Pekka Paalanenfb003d32012-12-04 15:58:13 +02002297 weston_buffer_reference(&gs->buffer_ref, NULL);
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +03002298 gs->buffer_type = BUFFER_TYPE_NULL;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002299 gs->y_inverted = 1;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002300 }
2301}
2302
Kristian Høgsberg42263852012-09-06 21:59:29 -04002303static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002304gl_renderer_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002305 float red, float green, float blue, float alpha)
2306{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002307 struct gl_surface_state *gs = get_surface_state(surface);
2308 struct gl_renderer *gr = get_renderer(surface->compositor);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002309
2310 gs->color[0] = red;
2311 gs->color[1] = green;
2312 gs->color[2] = blue;
2313 gs->color[3] = alpha;
Pekka Paalanenaeb917e2015-02-09 13:56:56 +02002314 gs->buffer_type = BUFFER_TYPE_SOLID;
2315 gs->pitch = 1;
2316 gs->height = 1;
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002317
John Kåre Alsaker40684142012-11-13 19:10:25 +01002318 gs->shader = &gr->solid_shader;
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002319}
2320
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002321static void
Pekka Paalaneneb35cbe2015-02-09 13:37:27 +02002322gl_renderer_surface_get_content_size(struct weston_surface *surface,
2323 int *width, int *height)
2324{
2325 struct gl_surface_state *gs = get_surface_state(surface);
2326
2327 if (gs->buffer_type == BUFFER_TYPE_NULL) {
2328 *width = 0;
2329 *height = 0;
2330 } else {
2331 *width = gs->pitch;
2332 *height = gs->height;
2333 }
2334}
2335
2336static uint32_t
2337pack_color(pixman_format_code_t format, float *c)
2338{
2339 uint8_t r = round(c[0] * 255.0f);
2340 uint8_t g = round(c[1] * 255.0f);
2341 uint8_t b = round(c[2] * 255.0f);
2342 uint8_t a = round(c[3] * 255.0f);
2343
2344 switch (format) {
2345 case PIXMAN_a8b8g8r8:
2346 return (a << 24) | (b << 16) | (g << 8) | r;
2347 default:
2348 assert(0);
2349 return 0;
2350 }
2351}
2352
2353static int
2354gl_renderer_surface_copy_content(struct weston_surface *surface,
2355 void *target, size_t size,
2356 int src_x, int src_y,
2357 int width, int height)
2358{
2359 static const GLfloat verts[4 * 2] = {
2360 0.0f, 0.0f,
2361 1.0f, 0.0f,
2362 1.0f, 1.0f,
2363 0.0f, 1.0f
2364 };
2365 static const GLfloat projmat_normal[16] = { /* transpose */
2366 2.0f, 0.0f, 0.0f, 0.0f,
2367 0.0f, 2.0f, 0.0f, 0.0f,
2368 0.0f, 0.0f, 1.0f, 0.0f,
2369 -1.0f, -1.0f, 0.0f, 1.0f
2370 };
2371 static const GLfloat projmat_yinvert[16] = { /* transpose */
2372 2.0f, 0.0f, 0.0f, 0.0f,
2373 0.0f, -2.0f, 0.0f, 0.0f,
2374 0.0f, 0.0f, 1.0f, 0.0f,
2375 -1.0f, 1.0f, 0.0f, 1.0f
2376 };
2377 const pixman_format_code_t format = PIXMAN_a8b8g8r8;
2378 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
2379 const GLenum gl_format = GL_RGBA; /* PIXMAN_a8b8g8r8 little-endian */
2380 struct gl_renderer *gr = get_renderer(surface->compositor);
2381 struct gl_surface_state *gs = get_surface_state(surface);
2382 int cw, ch;
2383 GLuint fbo;
2384 GLuint tex;
2385 GLenum status;
2386 const GLfloat *proj;
2387 int i;
2388
2389 gl_renderer_surface_get_content_size(surface, &cw, &ch);
2390
2391 switch (gs->buffer_type) {
2392 case BUFFER_TYPE_NULL:
2393 return -1;
2394 case BUFFER_TYPE_SOLID:
2395 *(uint32_t *)target = pack_color(format, gs->color);
2396 return 0;
2397 case BUFFER_TYPE_SHM:
2398 gl_renderer_flush_damage(surface);
2399 /* fall through */
2400 case BUFFER_TYPE_EGL:
2401 break;
2402 }
2403
2404 glGenTextures(1, &tex);
2405 glBindTexture(GL_TEXTURE_2D, tex);
2406 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, cw, ch,
2407 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
2408 glBindTexture(GL_TEXTURE_2D, 0);
2409
2410 glGenFramebuffers(1, &fbo);
2411 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
2412 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
2413 GL_TEXTURE_2D, tex, 0);
2414
2415 status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
2416 if (status != GL_FRAMEBUFFER_COMPLETE) {
2417 weston_log("%s: fbo error: %#x\n", __func__, status);
2418 glDeleteFramebuffers(1, &fbo);
2419 glDeleteTextures(1, &tex);
2420 return -1;
2421 }
2422
2423 glViewport(0, 0, cw, ch);
2424 glDisable(GL_BLEND);
2425 use_shader(gr, gs->shader);
2426 if (gs->y_inverted)
2427 proj = projmat_normal;
2428 else
2429 proj = projmat_yinvert;
2430
2431 glUniformMatrix4fv(gs->shader->proj_uniform, 1, GL_FALSE, proj);
2432 glUniform1f(gs->shader->alpha_uniform, 1.0f);
2433
2434 for (i = 0; i < gs->num_textures; i++) {
2435 glUniform1i(gs->shader->tex_uniforms[i], i);
2436
2437 glActiveTexture(GL_TEXTURE0 + i);
2438 glBindTexture(gs->target, gs->textures[i]);
2439 glTexParameteri(gs->target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2440 glTexParameteri(gs->target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2441 }
2442
2443 /* position: */
2444 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, verts);
2445 glEnableVertexAttribArray(0);
2446
2447 /* texcoord: */
2448 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, verts);
2449 glEnableVertexAttribArray(1);
2450
2451 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2452
2453 glDisableVertexAttribArray(1);
2454 glDisableVertexAttribArray(0);
2455
2456 glPixelStorei(GL_PACK_ALIGNMENT, bytespp);
2457 glReadPixels(src_x, src_y, width, height, gl_format,
2458 GL_UNSIGNED_BYTE, target);
2459
2460 glDeleteFramebuffers(1, &fbo);
2461 glDeleteTextures(1, &tex);
2462
2463 return 0;
2464}
2465
2466static void
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002467surface_state_destroy(struct gl_surface_state *gs, struct gl_renderer *gr)
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002468{
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002469 int i;
2470
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002471 wl_list_remove(&gs->surface_destroy_listener.link);
2472 wl_list_remove(&gs->renderer_destroy_listener.link);
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002473
2474 gs->surface->renderer_state = NULL;
2475
2476 glDeleteTextures(gs->num_textures, gs->textures);
2477
2478 for (i = 0; i < gs->num_images; i++)
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +03002479 egl_image_unref(gs->images[i]);
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002480
2481 weston_buffer_reference(&gs->buffer_ref, NULL);
2482 pixman_region32_fini(&gs->texture_damage);
2483 free(gs);
2484}
2485
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002486static void
2487surface_state_handle_surface_destroy(struct wl_listener *listener, void *data)
2488{
2489 struct gl_surface_state *gs;
2490 struct gl_renderer *gr;
2491
2492 gs = container_of(listener, struct gl_surface_state,
2493 surface_destroy_listener);
2494
2495 gr = get_renderer(gs->surface->compositor);
2496
2497 surface_state_destroy(gs, gr);
2498}
2499
2500static void
2501surface_state_handle_renderer_destroy(struct wl_listener *listener, void *data)
2502{
2503 struct gl_surface_state *gs;
2504 struct gl_renderer *gr;
2505
2506 gr = data;
2507
2508 gs = container_of(listener, struct gl_surface_state,
2509 renderer_destroy_listener);
2510
2511 surface_state_destroy(gs, gr);
2512}
2513
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002514static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002515gl_renderer_create_surface(struct weston_surface *surface)
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002516{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002517 struct gl_surface_state *gs;
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002518 struct gl_renderer *gr = get_renderer(surface->compositor);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002519
Bryce Harringtonde16d892014-11-20 22:21:57 -08002520 gs = zalloc(sizeof *gs);
2521 if (gs == NULL)
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002522 return -1;
2523
Pekka Paalanen68033ac2012-12-04 15:58:15 +02002524 /* A buffer is never attached to solid color surfaces, yet
2525 * they still go through texcoord computations. Do not divide
2526 * by zero there.
2527 */
2528 gs->pitch = 1;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002529 gs->y_inverted = 1;
Pekka Paalanen68033ac2012-12-04 15:58:15 +02002530
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002531 gs->surface = surface;
2532
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02002533 pixman_region32_init(&gs->texture_damage);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002534 surface->renderer_state = gs;
2535
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002536 gs->surface_destroy_listener.notify =
2537 surface_state_handle_surface_destroy;
2538 wl_signal_add(&surface->destroy_signal,
2539 &gs->surface_destroy_listener);
2540
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002541 gs->renderer_destroy_listener.notify =
2542 surface_state_handle_renderer_destroy;
2543 wl_signal_add(&gr->destroy_signal,
2544 &gs->renderer_destroy_listener);
2545
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02002546 if (surface->buffer_ref.buffer) {
2547 gl_renderer_attach(surface, surface->buffer_ref.buffer);
2548 gl_renderer_flush_damage(surface);
2549 }
2550
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002551 return 0;
2552}
2553
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002554static const char vertex_shader[] =
2555 "uniform mat4 proj;\n"
2556 "attribute vec2 position;\n"
2557 "attribute vec2 texcoord;\n"
2558 "varying vec2 v_texcoord;\n"
2559 "void main()\n"
2560 "{\n"
2561 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
2562 " v_texcoord = texcoord;\n"
2563 "}\n";
2564
2565/* Declare common fragment shader uniforms */
2566#define FRAGMENT_CONVERT_YUV \
2567 " y *= alpha;\n" \
2568 " u *= alpha;\n" \
2569 " v *= alpha;\n" \
2570 " gl_FragColor.r = y + 1.59602678 * v;\n" \
2571 " gl_FragColor.g = y - 0.39176229 * u - 0.81296764 * v;\n" \
2572 " gl_FragColor.b = y + 2.01723214 * u;\n" \
2573 " gl_FragColor.a = alpha;\n"
2574
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002575static const char fragment_debug[] =
2576 " gl_FragColor = vec4(0.0, 0.3, 0.0, 0.2) + gl_FragColor * 0.8;\n";
2577
2578static const char fragment_brace[] =
2579 "}\n";
2580
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002581static const char texture_fragment_shader_rgba[] =
2582 "precision mediump float;\n"
2583 "varying vec2 v_texcoord;\n"
2584 "uniform sampler2D tex;\n"
2585 "uniform float alpha;\n"
2586 "void main()\n"
2587 "{\n"
2588 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002589 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002590
2591static const char texture_fragment_shader_rgbx[] =
2592 "precision mediump float;\n"
2593 "varying vec2 v_texcoord;\n"
2594 "uniform sampler2D tex;\n"
2595 "uniform float alpha;\n"
2596 "void main()\n"
2597 "{\n"
2598 " gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb\n;"
2599 " gl_FragColor.a = alpha;\n"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002600 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002601
2602static const char texture_fragment_shader_egl_external[] =
2603 "#extension GL_OES_EGL_image_external : require\n"
2604 "precision mediump float;\n"
2605 "varying vec2 v_texcoord;\n"
2606 "uniform samplerExternalOES tex;\n"
2607 "uniform float alpha;\n"
2608 "void main()\n"
2609 "{\n"
2610 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002611 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002612
2613static const char texture_fragment_shader_y_uv[] =
2614 "precision mediump float;\n"
2615 "uniform sampler2D tex;\n"
2616 "uniform sampler2D tex1;\n"
2617 "varying vec2 v_texcoord;\n"
2618 "uniform float alpha;\n"
2619 "void main() {\n"
2620 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
2621 " float u = texture2D(tex1, v_texcoord).r - 0.5;\n"
2622 " float v = texture2D(tex1, v_texcoord).g - 0.5;\n"
2623 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002624 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002625
2626static const char texture_fragment_shader_y_u_v[] =
2627 "precision mediump float;\n"
2628 "uniform sampler2D tex;\n"
2629 "uniform sampler2D tex1;\n"
2630 "uniform sampler2D tex2;\n"
2631 "varying vec2 v_texcoord;\n"
2632 "uniform float alpha;\n"
2633 "void main() {\n"
2634 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
2635 " float u = texture2D(tex1, v_texcoord).x - 0.5;\n"
2636 " float v = texture2D(tex2, v_texcoord).x - 0.5;\n"
2637 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002638 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002639
2640static const char texture_fragment_shader_y_xuxv[] =
2641 "precision mediump float;\n"
2642 "uniform sampler2D tex;\n"
2643 "uniform sampler2D tex1;\n"
2644 "varying vec2 v_texcoord;\n"
2645 "uniform float alpha;\n"
2646 "void main() {\n"
2647 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
2648 " float u = texture2D(tex1, v_texcoord).g - 0.5;\n"
2649 " float v = texture2D(tex1, v_texcoord).a - 0.5;\n"
2650 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002651 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002652
2653static const char solid_fragment_shader[] =
2654 "precision mediump float;\n"
2655 "uniform vec4 color;\n"
2656 "uniform float alpha;\n"
2657 "void main()\n"
2658 "{\n"
2659 " gl_FragColor = alpha * color\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002660 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002661
2662static int
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002663compile_shader(GLenum type, int count, const char **sources)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002664{
2665 GLuint s;
2666 char msg[512];
2667 GLint status;
2668
2669 s = glCreateShader(type);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002670 glShaderSource(s, count, sources, NULL);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002671 glCompileShader(s);
2672 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
2673 if (!status) {
2674 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
2675 weston_log("shader info: %s\n", msg);
2676 return GL_NONE;
2677 }
2678
2679 return s;
2680}
2681
2682static int
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03002683shader_init(struct gl_shader *shader, struct gl_renderer *renderer,
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002684 const char *vertex_source, const char *fragment_source)
2685{
2686 char msg[512];
2687 GLint status;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002688 int count;
2689 const char *sources[3];
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002690
2691 shader->vertex_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002692 compile_shader(GL_VERTEX_SHADER, 1, &vertex_source);
2693
2694 if (renderer->fragment_shader_debug) {
2695 sources[0] = fragment_source;
2696 sources[1] = fragment_debug;
2697 sources[2] = fragment_brace;
2698 count = 3;
2699 } else {
2700 sources[0] = fragment_source;
2701 sources[1] = fragment_brace;
2702 count = 2;
2703 }
2704
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002705 shader->fragment_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002706 compile_shader(GL_FRAGMENT_SHADER, count, sources);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002707
2708 shader->program = glCreateProgram();
2709 glAttachShader(shader->program, shader->vertex_shader);
2710 glAttachShader(shader->program, shader->fragment_shader);
2711 glBindAttribLocation(shader->program, 0, "position");
2712 glBindAttribLocation(shader->program, 1, "texcoord");
2713
2714 glLinkProgram(shader->program);
2715 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
2716 if (!status) {
2717 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
2718 weston_log("link info: %s\n", msg);
2719 return -1;
2720 }
2721
2722 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
2723 shader->tex_uniforms[0] = glGetUniformLocation(shader->program, "tex");
2724 shader->tex_uniforms[1] = glGetUniformLocation(shader->program, "tex1");
2725 shader->tex_uniforms[2] = glGetUniformLocation(shader->program, "tex2");
2726 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
2727 shader->color_uniform = glGetUniformLocation(shader->program, "color");
2728
2729 return 0;
2730}
2731
2732static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002733shader_release(struct gl_shader *shader)
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002734{
2735 glDeleteShader(shader->vertex_shader);
2736 glDeleteShader(shader->fragment_shader);
2737 glDeleteProgram(shader->program);
2738
2739 shader->vertex_shader = 0;
2740 shader->fragment_shader = 0;
2741 shader->program = 0;
2742}
2743
2744static void
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002745log_extensions(const char *name, const char *extensions)
2746{
2747 const char *p, *end;
2748 int l;
2749 int len;
2750
2751 l = weston_log("%s:", name);
2752 p = extensions;
2753 while (*p) {
2754 end = strchrnul(p, ' ');
2755 len = end - p;
2756 if (l + len > 78)
2757 l = weston_log_continue("\n" STAMP_SPACE "%.*s",
2758 len, p);
2759 else
2760 l += weston_log_continue(" %.*s", len, p);
2761 for (p = end; isspace(*p); p++)
2762 ;
2763 }
2764 weston_log_continue("\n");
2765}
2766
2767static void
2768log_egl_gl_info(EGLDisplay egldpy)
2769{
2770 const char *str;
2771
2772 str = eglQueryString(egldpy, EGL_VERSION);
2773 weston_log("EGL version: %s\n", str ? str : "(null)");
2774
2775 str = eglQueryString(egldpy, EGL_VENDOR);
2776 weston_log("EGL vendor: %s\n", str ? str : "(null)");
2777
2778 str = eglQueryString(egldpy, EGL_CLIENT_APIS);
2779 weston_log("EGL client APIs: %s\n", str ? str : "(null)");
2780
2781 str = eglQueryString(egldpy, EGL_EXTENSIONS);
2782 log_extensions("EGL extensions", str ? str : "(null)");
2783
2784 str = (char *)glGetString(GL_VERSION);
2785 weston_log("GL version: %s\n", str ? str : "(null)");
2786
2787 str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
2788 weston_log("GLSL version: %s\n", str ? str : "(null)");
2789
2790 str = (char *)glGetString(GL_VENDOR);
2791 weston_log("GL vendor: %s\n", str ? str : "(null)");
2792
2793 str = (char *)glGetString(GL_RENDERER);
2794 weston_log("GL renderer: %s\n", str ? str : "(null)");
2795
2796 str = (char *)glGetString(GL_EXTENSIONS);
2797 log_extensions("GL extensions", str ? str : "(null)");
2798}
2799
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03002800static void
2801log_egl_config_info(EGLDisplay egldpy, EGLConfig eglconfig)
2802{
2803 EGLint r, g, b, a;
2804
2805 weston_log("Chosen EGL config details:\n");
2806
2807 weston_log_continue(STAMP_SPACE "RGBA bits");
2808 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_RED_SIZE, &r) &&
2809 eglGetConfigAttrib(egldpy, eglconfig, EGL_GREEN_SIZE, &g) &&
2810 eglGetConfigAttrib(egldpy, eglconfig, EGL_BLUE_SIZE, &b) &&
2811 eglGetConfigAttrib(egldpy, eglconfig, EGL_ALPHA_SIZE, &a))
2812 weston_log_continue(": %d %d %d %d\n", r, g, b, a);
2813 else
2814 weston_log_continue(" unknown\n");
2815
2816 weston_log_continue(STAMP_SPACE "swap interval range");
2817 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_MIN_SWAP_INTERVAL, &a) &&
2818 eglGetConfigAttrib(egldpy, eglconfig, EGL_MAX_SWAP_INTERVAL, &b))
2819 weston_log_continue(": %d - %d\n", a, b);
2820 else
2821 weston_log_continue(" unknown\n");
2822}
2823
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002824static int
Derek Foremane76f1852015-05-15 12:12:39 -05002825match_config_to_visual(EGLDisplay egl_display,
2826 EGLint visual_id,
2827 EGLConfig *configs,
2828 int count)
2829{
2830 int i;
2831
2832 for (i = 0; i < count; ++i) {
2833 EGLint id;
2834
2835 if (!eglGetConfigAttrib(egl_display,
2836 configs[i], EGL_NATIVE_VISUAL_ID,
2837 &id))
2838 continue;
2839
2840 if (id == visual_id)
2841 return i;
2842 }
2843
Derek Foremane76f1852015-05-15 12:12:39 -05002844 return -1;
2845}
2846
2847static int
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002848egl_choose_config(struct gl_renderer *gr, const EGLint *attribs,
Derek Foremane76f1852015-05-15 12:12:39 -05002849 const EGLint *visual_id, const int n_ids,
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002850 EGLConfig *config_out)
2851{
2852 EGLint count = 0;
2853 EGLint matched = 0;
2854 EGLConfig *configs;
Derek Foremane76f1852015-05-15 12:12:39 -05002855 int i, config_index = -1;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002856
Derek Foremana7e19912015-05-20 14:57:58 -05002857 if (!eglGetConfigs(gr->egl_display, NULL, 0, &count) || count < 1) {
2858 weston_log("No EGL configs to choose from.\n");
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002859 return -1;
Derek Foremana7e19912015-05-20 14:57:58 -05002860 }
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002861 configs = calloc(count, sizeof *configs);
2862 if (!configs)
2863 return -1;
2864
2865 if (!eglChooseConfig(gr->egl_display, attribs, configs,
Derek Foremana7e19912015-05-20 14:57:58 -05002866 count, &matched) || !matched) {
2867 weston_log("No EGL configs with appropriate attributes.\n");
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002868 goto out;
Derek Foremana7e19912015-05-20 14:57:58 -05002869 }
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002870
Miguel A. Vico684c9f42016-05-18 17:48:47 +02002871 if (!visual_id || n_ids == 0)
Derek Foremane76f1852015-05-15 12:12:39 -05002872 config_index = 0;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002873
Derek Foremane76f1852015-05-15 12:12:39 -05002874 for (i = 0; config_index == -1 && i < n_ids; i++)
2875 config_index = match_config_to_visual(gr->egl_display,
2876 visual_id[i],
2877 configs,
2878 matched);
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002879
Derek Foremane76f1852015-05-15 12:12:39 -05002880 if (config_index != -1)
2881 *config_out = configs[config_index];
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002882
2883out:
2884 free(configs);
Derek Foremane76f1852015-05-15 12:12:39 -05002885 if (config_index == -1)
2886 return -1;
2887
Derek Foremana7e19912015-05-20 14:57:58 -05002888 if (i > 1)
2889 weston_log("Unable to use first choice EGL config with id"
2890 " 0x%x, succeeded with alternate id 0x%x.\n",
2891 visual_id[0], visual_id[i - 1]);
Derek Foremane76f1852015-05-15 12:12:39 -05002892 return 0;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002893}
2894
John Kåre Alsaker44154502012-11-13 19:10:20 +01002895static void
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05002896gl_renderer_output_set_border(struct weston_output *output,
2897 enum gl_renderer_border_side side,
2898 int32_t width, int32_t height,
2899 int32_t tex_width, unsigned char *data)
2900{
2901 struct gl_output_state *go = get_output_state(output);
2902
Jason Ekstrande5512d42014-02-04 21:36:38 -06002903 if (go->borders[side].width != width ||
2904 go->borders[side].height != height)
2905 /* In this case, we have to blow everything and do a full
2906 * repaint. */
2907 go->border_status |= BORDER_SIZE_CHANGED | BORDER_ALL_DIRTY;
2908
2909 if (data == NULL) {
2910 width = 0;
2911 height = 0;
2912 }
2913
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05002914 go->borders[side].width = width;
2915 go->borders[side].height = height;
2916 go->borders[side].tex_width = tex_width;
2917 go->borders[side].data = data;
Jason Ekstrande5512d42014-02-04 21:36:38 -06002918 go->border_status |= 1 << side;
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05002919}
2920
John Kåre Alsaker94659272012-11-13 19:10:18 +01002921static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002922gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01002923
Miguel A. Vico967b6bc2016-05-18 17:50:54 +02002924static EGLSurface
2925gl_renderer_create_window_surface(struct gl_renderer *gr,
2926 EGLNativeWindowType window_for_legacy,
2927 void *window_for_platform,
2928 const EGLint *config_attribs,
2929 const EGLint *visual_id,
2930 int n_ids)
2931{
2932 EGLSurface egl_surface = EGL_NO_SURFACE;
2933 EGLConfig egl_config;
2934
2935 if (egl_choose_config(gr, config_attribs, visual_id,
2936 n_ids, &egl_config) == -1) {
2937 weston_log("failed to choose EGL config for output\n");
2938 return EGL_NO_SURFACE;
2939 }
2940
2941 if (egl_config != gr->egl_config &&
2942 !gr->has_configless_context) {
2943 weston_log("attempted to use a different EGL config for an "
Armin Krezoviće3bfee12016-09-30 14:27:38 +02002944 "output but EGL_KHR_no_config_context or "
2945 "EGL_MESA_configless_context is not supported\n");
Miguel A. Vico967b6bc2016-05-18 17:50:54 +02002946 return EGL_NO_SURFACE;
2947 }
2948
2949 log_egl_config_info(gr->egl_display, egl_config);
2950
2951 if (gr->create_platform_window)
2952 egl_surface = gr->create_platform_window(gr->egl_display,
2953 egl_config,
2954 window_for_platform,
2955 NULL);
2956 else
2957 egl_surface = eglCreateWindowSurface(gr->egl_display,
2958 egl_config,
2959 window_for_legacy, NULL);
2960
2961 return egl_surface;
2962}
2963
2964static int
2965gl_renderer_output_create(struct weston_output *output,
2966 EGLSurface surface)
2967{
2968 struct gl_output_state *go;
2969 int i;
2970
2971 go = zalloc(sizeof *go);
2972 if (go == NULL)
2973 return -1;
2974
2975 go->egl_surface = surface;
2976
2977 for (i = 0; i < BUFFER_DAMAGE_COUNT; i++)
2978 pixman_region32_init(&go->buffer_damage[i]);
2979
Alexandros Frantzisdf0e4b92017-09-27 15:09:16 +03002980 wl_list_init(&go->timeline_render_point_list);
2981
Miguel A. Vico967b6bc2016-05-18 17:50:54 +02002982 output->renderer_state = go;
2983
2984 return 0;
2985}
2986
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002987static int
Miguel A. Vicoc095cde2016-05-18 17:43:00 +02002988gl_renderer_output_window_create(struct weston_output *output,
2989 EGLNativeWindowType window_for_legacy,
2990 void *window_for_platform,
Miguel A. Vico4057cd92016-05-18 17:44:22 +02002991 const EGLint *config_attribs,
Miguel A. Vicoc095cde2016-05-18 17:43:00 +02002992 const EGLint *visual_id,
2993 int n_ids)
John Kåre Alsaker94659272012-11-13 19:10:18 +01002994{
2995 struct weston_compositor *ec = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002996 struct gl_renderer *gr = get_renderer(ec);
Miguel A. Vico967b6bc2016-05-18 17:50:54 +02002997 EGLSurface egl_surface = EGL_NO_SURFACE;
2998 int ret = 0;
John Kåre Alsaker94659272012-11-13 19:10:18 +01002999
Miguel A. Vico967b6bc2016-05-18 17:50:54 +02003000 egl_surface = gl_renderer_create_window_surface(gr,
3001 window_for_legacy,
3002 window_for_platform,
3003 config_attribs,
3004 visual_id, n_ids);
3005 if (egl_surface == EGL_NO_SURFACE) {
John Kåre Alsaker94659272012-11-13 19:10:18 +01003006 weston_log("failed to create egl surface\n");
John Kåre Alsaker94659272012-11-13 19:10:18 +01003007 return -1;
3008 }
3009
Miguel A. Vico967b6bc2016-05-18 17:50:54 +02003010 ret = gl_renderer_output_create(output, egl_surface);
3011 if (ret < 0)
Emil Velikov02639552016-11-14 17:08:17 +00003012 weston_platform_destroy_egl_surface(gr->egl_display, egl_surface);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003013
Miguel A. Vico967b6bc2016-05-18 17:50:54 +02003014 return ret;
John Kåre Alsaker94659272012-11-13 19:10:18 +01003015}
3016
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003017static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003018gl_renderer_output_destroy(struct weston_output *output)
John Kåre Alsaker94659272012-11-13 19:10:18 +01003019{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003020 struct gl_renderer *gr = get_renderer(output->compositor);
3021 struct gl_output_state *go = get_output_state(output);
Alexandros Frantzisdf0e4b92017-09-27 15:09:16 +03003022 struct timeline_render_point *trp, *tmp;
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003023 int i;
3024
3025 for (i = 0; i < 2; i++)
3026 pixman_region32_fini(&go->buffer_damage[i]);
John Kåre Alsaker94659272012-11-13 19:10:18 +01003027
Dima Ryazanov6a38ad72016-11-23 18:41:00 -08003028 eglMakeCurrent(gr->egl_display,
3029 EGL_NO_SURFACE, EGL_NO_SURFACE,
3030 EGL_NO_CONTEXT);
3031
Emil Velikov02639552016-11-14 17:08:17 +00003032 weston_platform_destroy_egl_surface(gr->egl_display, go->egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01003033
Alexandros Frantzisdf0e4b92017-09-27 15:09:16 +03003034 if (!wl_list_empty(&go->timeline_render_point_list))
3035 weston_log("warning: discarding pending timeline render"
3036 "objects at output destruction");
3037
3038 wl_list_for_each_safe(trp, tmp, &go->timeline_render_point_list, link)
3039 timeline_render_point_destroy(trp);
3040
John Kåre Alsaker94659272012-11-13 19:10:18 +01003041 free(go);
3042}
3043
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003044static EGLSurface
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003045gl_renderer_output_surface(struct weston_output *output)
John Kåre Alsaker94659272012-11-13 19:10:18 +01003046{
3047 return get_output_state(output)->egl_surface;
3048}
3049
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03003050static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003051gl_renderer_destroy(struct weston_compositor *ec)
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04003052{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003053 struct gl_renderer *gr = get_renderer(ec);
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00003054 struct dmabuf_image *image, *next;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003055
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03003056 wl_signal_emit(&gr->destroy_signal, gr);
3057
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003058 if (gr->has_bind_display)
3059 gr->unbind_display(gr->egl_display, ec->wl_display);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003060
3061 /* Work around crash in egl_dri2.c's dri2_make_current() - when does this apply? */
3062 eglMakeCurrent(gr->egl_display,
3063 EGL_NO_SURFACE, EGL_NO_SURFACE,
3064 EGL_NO_CONTEXT);
3065
Pekka Paalanena3525802014-06-12 16:49:29 +03003066
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00003067 wl_list_for_each_safe(image, next, &gr->dmabuf_images, link)
3068 dmabuf_image_destroy(image);
Pekka Paalanena3525802014-06-12 16:49:29 +03003069
Armin Krezović28d240f2016-06-23 11:59:35 +02003070 if (gr->dummy_surface != EGL_NO_SURFACE)
Emil Velikov02639552016-11-14 17:08:17 +00003071 weston_platform_destroy_egl_surface(gr->egl_display,
3072 gr->dummy_surface);
Armin Krezović28d240f2016-06-23 11:59:35 +02003073
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003074 eglTerminate(gr->egl_display);
3075 eglReleaseThread();
Scott Moreau976a0502013-03-07 10:15:17 -07003076
Armin Krezović36d699a2016-08-05 15:28:30 +02003077 wl_list_remove(&gr->output_destroy_listener.link);
3078
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -04003079 wl_array_release(&gr->vertices);
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -04003080 wl_array_release(&gr->vtxcnt);
3081
Mariusz Ceiercbb91582014-02-08 20:11:24 +01003082 if (gr->fragment_binding)
3083 weston_binding_destroy(gr->fragment_binding);
3084 if (gr->fan_binding)
3085 weston_binding_destroy(gr->fan_binding);
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03003086
Scott Moreau976a0502013-03-07 10:15:17 -07003087 free(gr);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003088}
3089
Pekka Paalanen8b69d032015-04-08 17:02:22 +03003090static void
3091renderer_setup_egl_client_extensions(struct gl_renderer *gr)
3092{
3093 const char *extensions;
3094
3095 extensions = eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS);
3096 if (!extensions) {
3097 weston_log("Retrieving EGL client extension string failed.\n");
3098 return;
3099 }
3100
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003101 if (weston_check_egl_extension(extensions, "EGL_EXT_platform_base"))
Pekka Paalanen8b69d032015-04-08 17:02:22 +03003102 gr->create_platform_window =
3103 (void *) eglGetProcAddress("eglCreatePlatformWindowSurfaceEXT");
3104 else
3105 weston_log("warning: EGL_EXT_platform_base not supported.\n");
3106}
3107
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003108static int
Neil Robertsb7f85332014-03-07 18:05:49 +00003109gl_renderer_setup_egl_extensions(struct weston_compositor *ec)
3110{
Emil Velikov43cea542016-11-14 16:03:45 +00003111 static const struct {
3112 char *extension, *entrypoint;
3113 } swap_damage_ext_to_entrypoint[] = {
3114 {
3115 .extension = "EGL_EXT_swap_buffers_with_damage",
3116 .entrypoint = "eglSwapBuffersWithDamageEXT",
3117 },
3118 {
3119 .extension = "EGL_KHR_swap_buffers_with_damage",
3120 .entrypoint = "eglSwapBuffersWithDamageKHR",
3121 },
3122 };
Neil Robertsb7f85332014-03-07 18:05:49 +00003123 struct gl_renderer *gr = get_renderer(ec);
3124 const char *extensions;
3125 EGLBoolean ret;
Bryce Harringtonfe0410b2016-11-21 10:02:42 -08003126 unsigned i;
Neil Robertsb7f85332014-03-07 18:05:49 +00003127
3128 gr->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3129 gr->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
Varad Gautam0775cd12016-11-23 14:03:20 +05303130
Neil Robertsb7f85332014-03-07 18:05:49 +00003131 gr->bind_display =
3132 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
3133 gr->unbind_display =
3134 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
3135 gr->query_buffer =
3136 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
3137
3138 extensions =
3139 (const char *) eglQueryString(gr->egl_display, EGL_EXTENSIONS);
3140 if (!extensions) {
3141 weston_log("Retrieving EGL extension string failed.\n");
3142 return -1;
3143 }
3144
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003145 if (weston_check_egl_extension(extensions, "EGL_WL_bind_wayland_display"))
Neil Robertsb7f85332014-03-07 18:05:49 +00003146 gr->has_bind_display = 1;
3147 if (gr->has_bind_display) {
3148 ret = gr->bind_display(gr->egl_display, ec->wl_display);
3149 if (!ret)
3150 gr->has_bind_display = 0;
3151 }
3152
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003153 if (weston_check_egl_extension(extensions, "EGL_EXT_buffer_age"))
Neil Robertsb7f85332014-03-07 18:05:49 +00003154 gr->has_egl_buffer_age = 1;
3155 else
3156 weston_log("warning: EGL_EXT_buffer_age not supported. "
3157 "Performance could be affected.\n");
3158
Bryce Harringtonfe0410b2016-11-21 10:02:42 -08003159 for (i = 0; i < ARRAY_LENGTH(swap_damage_ext_to_entrypoint); i++) {
Emil Velikov43cea542016-11-14 16:03:45 +00003160 if (weston_check_egl_extension(extensions,
3161 swap_damage_ext_to_entrypoint[i].extension)) {
3162 gr->swap_buffers_with_damage =
3163 (void *) eglGetProcAddress(
3164 swap_damage_ext_to_entrypoint[i].entrypoint);
3165 break;
3166 }
3167 }
3168 if (!gr->swap_buffers_with_damage)
3169 weston_log("warning: neither %s or %s is supported. "
3170 "Performance could be affected.\n",
3171 swap_damage_ext_to_entrypoint[0].extension,
3172 swap_damage_ext_to_entrypoint[1].extension);
Neil Robertsb7f85332014-03-07 18:05:49 +00003173
Armin Krezoviće3bfee12016-09-30 14:27:38 +02003174 if (weston_check_egl_extension(extensions, "EGL_KHR_no_config_context") ||
3175 weston_check_egl_extension(extensions, "EGL_MESA_configless_context"))
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003176 gr->has_configless_context = 1;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003177
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003178 if (weston_check_egl_extension(extensions, "EGL_KHR_surfaceless_context"))
Armin Krezović28d240f2016-06-23 11:59:35 +02003179 gr->has_surfaceless_context = 1;
3180
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003181 if (weston_check_egl_extension(extensions, "EGL_EXT_image_dma_buf_import"))
Pekka Paalanena3525802014-06-12 16:49:29 +03003182 gr->has_dmabuf_import = 1;
Pekka Paalanena3525802014-06-12 16:49:29 +03003183
Varad Gautam0775cd12016-11-23 14:03:20 +05303184 if (weston_check_egl_extension(extensions,
3185 "EGL_EXT_image_dma_buf_import_modifiers")) {
3186 gr->query_dmabuf_formats =
3187 (void *) eglGetProcAddress("eglQueryDmaBufFormatsEXT");
3188 gr->query_dmabuf_modifiers =
3189 (void *) eglGetProcAddress("eglQueryDmaBufModifiersEXT");
3190 gr->has_dmabuf_import_modifiers = 1;
3191 }
3192
Vincent Abrioufdeefe42016-10-05 14:54:34 +02003193 if (weston_check_egl_extension(extensions, "GL_EXT_texture_rg"))
3194 gr->has_gl_texture_rg = 1;
3195
Alexandros Frantzis7192b172017-09-27 15:09:14 +03003196 if (weston_check_egl_extension(extensions, "EGL_KHR_fence_sync") &&
3197 weston_check_egl_extension(extensions, "EGL_ANDROID_native_fence_sync")) {
3198 gr->create_sync =
3199 (void *) eglGetProcAddress("eglCreateSyncKHR");
3200 gr->destroy_sync =
3201 (void *) eglGetProcAddress("eglDestroySyncKHR");
3202 gr->dup_native_fence_fd =
3203 (void *) eglGetProcAddress("eglDupNativeFenceFDANDROID");
3204 gr->has_native_fence_sync = 1;
Alexandros Frantzisdf0e4b92017-09-27 15:09:16 +03003205 } else {
3206 weston_log("warning: Disabling render GPU timeline due to "
3207 "missing EGL_ANDROID_native_fence_sync extension\n");
Alexandros Frantzis7192b172017-09-27 15:09:14 +03003208 }
3209
Pekka Paalanen8b69d032015-04-08 17:02:22 +03003210 renderer_setup_egl_client_extensions(gr);
3211
Neil Robertsb7f85332014-03-07 18:05:49 +00003212 return 0;
3213}
3214
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003215static const EGLint gl_renderer_opaque_attribs[] = {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003216 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
3217 EGL_RED_SIZE, 1,
3218 EGL_GREEN_SIZE, 1,
3219 EGL_BLUE_SIZE, 1,
3220 EGL_ALPHA_SIZE, 0,
3221 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
3222 EGL_NONE
3223};
3224
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003225static const EGLint gl_renderer_alpha_attribs[] = {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003226 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
3227 EGL_RED_SIZE, 1,
3228 EGL_GREEN_SIZE, 1,
3229 EGL_BLUE_SIZE, 1,
3230 EGL_ALPHA_SIZE, 1,
3231 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
3232 EGL_NONE
3233};
3234
Armin Krezović28d240f2016-06-23 11:59:35 +02003235
Jonny Lamb70eba3f2015-03-20 15:26:50 +01003236/** Checks whether a platform EGL client extension is supported
3237 *
3238 * \param ec The weston compositor
3239 * \param extension_suffix The EGL client extension suffix
3240 * \return 1 if supported, 0 if using fallbacks, -1 unsupported
3241 *
3242 * This function checks whether a specific platform_* extension is supported
3243 * by EGL.
3244 *
3245 * The extension suffix should be the suffix of the platform extension (that
3246 * specifies a <platform> argument as defined in EGL_EXT_platform_base). For
3247 * example, passing "foo" will check whether either "EGL_KHR_platform_foo",
3248 * "EGL_EXT_platform_foo", or "EGL_MESA_platform_foo" is supported.
3249 *
3250 * The return value is 1:
3251 * - if the supplied EGL client extension is supported.
3252 * The return value is 0:
3253 * - if the platform_base client extension isn't supported so will
3254 * fallback to eglGetDisplay and friends.
3255 * The return value is -1:
3256 * - if the supplied EGL client extension is not supported.
3257 */
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003258static int
Jonny Lamb70eba3f2015-03-20 15:26:50 +01003259gl_renderer_supports(struct weston_compositor *ec,
3260 const char *extension_suffix)
3261{
3262 static const char *extensions = NULL;
3263 char s[64];
3264
3265 if (!extensions) {
3266 extensions = (const char *) eglQueryString(
3267 EGL_NO_DISPLAY, EGL_EXTENSIONS);
3268
3269 if (!extensions)
3270 return 0;
3271
3272 log_extensions("EGL client extensions",
3273 extensions);
3274 }
3275
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003276 if (!weston_check_egl_extension(extensions, "EGL_EXT_platform_base"))
Pekka Paalanenf2824542015-04-08 17:02:21 +03003277 return 0;
3278
Jonny Lamb70eba3f2015-03-20 15:26:50 +01003279 snprintf(s, sizeof s, "EGL_KHR_platform_%s", extension_suffix);
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003280 if (weston_check_egl_extension(extensions, s))
Jonny Lamb70eba3f2015-03-20 15:26:50 +01003281 return 1;
3282
3283 snprintf(s, sizeof s, "EGL_EXT_platform_%s", extension_suffix);
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003284 if (weston_check_egl_extension(extensions, s))
Jonny Lamb70eba3f2015-03-20 15:26:50 +01003285 return 1;
3286
3287 snprintf(s, sizeof s, "EGL_MESA_platform_%s", extension_suffix);
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003288 if (weston_check_egl_extension(extensions, s))
Jonny Lamb70eba3f2015-03-20 15:26:50 +01003289 return 1;
3290
Pekka Paalanenf2824542015-04-08 17:02:21 +03003291 /* at this point we definitely have some platform extensions but
3292 * haven't found the supplied platform, so chances are it's
Jonny Lamb70eba3f2015-03-20 15:26:50 +01003293 * not supported. */
3294
3295 return -1;
3296}
3297
Jonny Lamb74eed312015-03-24 13:12:04 +01003298static const char *
3299platform_to_extension(EGLenum platform)
3300{
3301 switch (platform) {
3302 case EGL_PLATFORM_GBM_KHR:
3303 return "gbm";
3304 case EGL_PLATFORM_WAYLAND_KHR:
3305 return "wayland";
3306 case EGL_PLATFORM_X11_KHR:
3307 return "x11";
3308 default:
3309 assert(0 && "bad EGL platform enum");
3310 }
3311}
3312
Armin Krezović36d699a2016-08-05 15:28:30 +02003313static void
3314output_handle_destroy(struct wl_listener *listener, void *data)
3315{
3316 struct gl_renderer *gr;
3317 struct weston_output *output = data;
3318
3319 gr = container_of(listener, struct gl_renderer,
3320 output_destroy_listener);
3321
3322 if (wl_list_empty(&output->compositor->output_list))
3323 eglMakeCurrent(gr->egl_display, gr->dummy_surface,
3324 gr->dummy_surface, gr->egl_context);
3325}
3326
Jonny Lamb70eba3f2015-03-20 15:26:50 +01003327static int
Armin Krezović28d240f2016-06-23 11:59:35 +02003328gl_renderer_create_pbuffer_surface(struct gl_renderer *gr) {
3329 EGLConfig pbuffer_config;
3330
3331 static const EGLint pbuffer_config_attribs[] = {
3332 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
3333 EGL_RED_SIZE, 1,
3334 EGL_GREEN_SIZE, 1,
3335 EGL_BLUE_SIZE, 1,
3336 EGL_ALPHA_SIZE, 0,
3337 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
3338 EGL_NONE
3339 };
3340
3341 static const EGLint pbuffer_attribs[] = {
3342 EGL_WIDTH, 10,
3343 EGL_HEIGHT, 10,
3344 EGL_NONE
3345 };
3346
3347 if (egl_choose_config(gr, pbuffer_config_attribs, NULL, 0, &pbuffer_config) < 0) {
Derek Foreman12968e32017-06-26 14:42:44 -05003348 weston_log("failed to choose EGL config for PbufferSurface\n");
Armin Krezović28d240f2016-06-23 11:59:35 +02003349 return -1;
3350 }
3351
3352 gr->dummy_surface = eglCreatePbufferSurface(gr->egl_display,
3353 pbuffer_config,
3354 pbuffer_attribs);
3355
3356 if (gr->dummy_surface == EGL_NO_SURFACE) {
3357 weston_log("failed to create PbufferSurface\n");
3358 return -1;
3359 }
3360
3361 return 0;
3362}
3363
3364static int
Miguel A. Vicodddc6702016-05-18 17:41:07 +02003365gl_renderer_display_create(struct weston_compositor *ec, EGLenum platform,
Miguel A. Vico41700e32016-05-18 17:47:59 +02003366 void *native_window, const EGLint *platform_attribs,
3367 const EGLint *config_attribs, const EGLint *visual_id, int n_ids)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003368{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003369 struct gl_renderer *gr;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003370 EGLint major, minor;
Jonny Lamb74eed312015-03-24 13:12:04 +01003371 int supports = 0;
3372
3373 if (platform) {
3374 supports = gl_renderer_supports(
3375 ec, platform_to_extension(platform));
3376 if (supports < 0)
3377 return -1;
3378 }
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003379
Bryce Harringtonde16d892014-11-20 22:21:57 -08003380 gr = zalloc(sizeof *gr);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003381 if (gr == NULL)
3382 return -1;
3383
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003384 gr->base.read_pixels = gl_renderer_read_pixels;
3385 gr->base.repaint_output = gl_renderer_repaint_output;
3386 gr->base.flush_damage = gl_renderer_flush_damage;
3387 gr->base.attach = gl_renderer_attach;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003388 gr->base.surface_set_color = gl_renderer_surface_set_color;
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03003389 gr->base.destroy = gl_renderer_destroy;
Pekka Paalaneneb35cbe2015-02-09 13:37:27 +02003390 gr->base.surface_get_content_size =
3391 gl_renderer_surface_get_content_size;
3392 gr->base.surface_copy_content = gl_renderer_surface_copy_content;
Jonny Lamb74eed312015-03-24 13:12:04 +01003393 gr->egl_display = NULL;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003394
Jonny Lambf1ec5062015-03-24 13:12:05 +01003395 /* extension_suffix is supported */
Jonny Lamb74eed312015-03-24 13:12:04 +01003396 if (supports) {
3397 if (!get_platform_display) {
3398 get_platform_display = (void *) eglGetProcAddress(
3399 "eglGetPlatformDisplayEXT");
3400 }
Jonny Lamb70eba3f2015-03-20 15:26:50 +01003401
Jonny Lamb74eed312015-03-24 13:12:04 +01003402 /* also wrap this in the supports check because
3403 * eglGetProcAddress can return non-NULL and still not
3404 * support the feature at runtime, so ensure the
3405 * appropriate extension checks have been done. */
3406 if (get_platform_display && platform) {
3407 gr->egl_display = get_platform_display(platform,
3408 native_window,
Miguel A. Vico41700e32016-05-18 17:47:59 +02003409 platform_attribs);
Jonny Lamb74eed312015-03-24 13:12:04 +01003410 }
3411 }
Jonny Lamb74eed312015-03-24 13:12:04 +01003412
3413 if (!gr->egl_display) {
Jonny Lamb70eba3f2015-03-20 15:26:50 +01003414 weston_log("warning: either no EGL_EXT_platform_base "
3415 "support or specific platform support; "
3416 "falling back to eglGetDisplay.\n");
3417 gr->egl_display = eglGetDisplay(native_window);
Jonny Lamb70eba3f2015-03-20 15:26:50 +01003418 }
Jonny Lamb70eba3f2015-03-20 15:26:50 +01003419
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003420 if (gr->egl_display == EGL_NO_DISPLAY) {
3421 weston_log("failed to create display\n");
Derek Foreman066ca0c2015-06-11 12:14:45 -05003422 goto fail;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003423 }
3424
3425 if (!eglInitialize(gr->egl_display, &major, &minor)) {
3426 weston_log("failed to initialize display\n");
Derek Foreman066ca0c2015-06-11 12:14:45 -05003427 goto fail_with_error;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003428 }
3429
Miguel A. Vico4057cd92016-05-18 17:44:22 +02003430 if (egl_choose_config(gr, config_attribs, visual_id,
Derek Foremane76f1852015-05-15 12:12:39 -05003431 n_ids, &gr->egl_config) < 0) {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003432 weston_log("failed to choose EGL config\n");
Dawid Gajownik1a912a92015-08-21 00:20:54 -03003433 goto fail_terminate;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003434 }
3435
3436 ec->renderer = &gr->base;
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003437 ec->capabilities |= WESTON_CAP_ROTATION_ANY;
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003438 ec->capabilities |= WESTON_CAP_CAPTURE_YFLIP;
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +02003439 ec->capabilities |= WESTON_CAP_VIEW_CLIP_MASK;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003440
Neil Robertsb7f85332014-03-07 18:05:49 +00003441 if (gl_renderer_setup_egl_extensions(ec) < 0)
Derek Foreman066ca0c2015-06-11 12:14:45 -05003442 goto fail_with_error;
Neil Robertsb7f85332014-03-07 18:05:49 +00003443
Pekka Paalanena3525802014-06-12 16:49:29 +03003444 wl_list_init(&gr->dmabuf_images);
Varad Gautam0775cd12016-11-23 14:03:20 +05303445 if (gr->has_dmabuf_import) {
Pekka Paalanena3525802014-06-12 16:49:29 +03003446 gr->base.import_dmabuf = gl_renderer_import_dmabuf;
Varad Gautam0775cd12016-11-23 14:03:20 +05303447 gr->base.query_dmabuf_formats =
3448 gl_renderer_query_dmabuf_formats;
3449 gr->base.query_dmabuf_modifiers =
3450 gl_renderer_query_dmabuf_modifiers;
3451 }
Pekka Paalanena3525802014-06-12 16:49:29 +03003452
Armin Krezović28d240f2016-06-23 11:59:35 +02003453 if (gr->has_surfaceless_context) {
3454 weston_log("EGL_KHR_surfaceless_context available\n");
3455 gr->dummy_surface = EGL_NO_SURFACE;
3456 } else {
3457 weston_log("EGL_KHR_surfaceless_context unavailable. "
3458 "Trying PbufferSurface\n");
3459
3460 if (gl_renderer_create_pbuffer_surface(gr) < 0)
3461 goto fail_with_error;
3462 }
3463
Tomeu Vizoso12072b62013-08-06 20:05:55 +02003464 wl_display_add_shm_format(ec->wl_display, WL_SHM_FORMAT_RGB565);
Vincent Abrioufdeefe42016-10-05 14:54:34 +02003465 wl_display_add_shm_format(ec->wl_display, WL_SHM_FORMAT_YUV420);
Vincent Abriou00a03d22016-10-05 14:54:35 +02003466 wl_display_add_shm_format(ec->wl_display, WL_SHM_FORMAT_NV12);
Vincent ABRIOUe5732c72016-10-20 16:20:11 +02003467 wl_display_add_shm_format(ec->wl_display, WL_SHM_FORMAT_YUYV);
Tomeu Vizoso12072b62013-08-06 20:05:55 +02003468
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03003469 wl_signal_init(&gr->destroy_signal);
3470
Armin Krezović28d240f2016-06-23 11:59:35 +02003471 if (gl_renderer_setup(ec, gr->dummy_surface) < 0) {
3472 if (gr->dummy_surface != EGL_NO_SURFACE)
Emil Velikov02639552016-11-14 17:08:17 +00003473 weston_platform_destroy_egl_surface(gr->egl_display,
3474 gr->dummy_surface);
Armin Krezović28d240f2016-06-23 11:59:35 +02003475 goto fail_with_error;
3476 }
3477
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003478 return 0;
3479
Derek Foreman066ca0c2015-06-11 12:14:45 -05003480fail_with_error:
Pekka Paalanen326529f2012-11-27 12:25:25 +02003481 gl_renderer_print_egl_error_state();
Dawid Gajownik1a912a92015-08-21 00:20:54 -03003482fail_terminate:
3483 eglTerminate(gr->egl_display);
Derek Foreman066ca0c2015-06-11 12:14:45 -05003484fail:
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003485 free(gr);
3486 return -1;
3487}
3488
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003489static EGLDisplay
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003490gl_renderer_display(struct weston_compositor *ec)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003491{
3492 return get_renderer(ec)->egl_display;
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04003493}
3494
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003495static int
3496compile_shaders(struct weston_compositor *ec)
3497{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003498 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker40684142012-11-13 19:10:25 +01003499
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03003500 gr->texture_shader_rgba.vertex_source = vertex_shader;
3501 gr->texture_shader_rgba.fragment_source = texture_fragment_shader_rgba;
3502
3503 gr->texture_shader_rgbx.vertex_source = vertex_shader;
3504 gr->texture_shader_rgbx.fragment_source = texture_fragment_shader_rgbx;
3505
3506 gr->texture_shader_egl_external.vertex_source = vertex_shader;
3507 gr->texture_shader_egl_external.fragment_source =
3508 texture_fragment_shader_egl_external;
3509
3510 gr->texture_shader_y_uv.vertex_source = vertex_shader;
3511 gr->texture_shader_y_uv.fragment_source = texture_fragment_shader_y_uv;
3512
3513 gr->texture_shader_y_u_v.vertex_source = vertex_shader;
3514 gr->texture_shader_y_u_v.fragment_source =
3515 texture_fragment_shader_y_u_v;
3516
Ander Conselvan de Oliveira41a50ea2013-11-27 17:43:51 +02003517 gr->texture_shader_y_xuxv.vertex_source = vertex_shader;
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03003518 gr->texture_shader_y_xuxv.fragment_source =
3519 texture_fragment_shader_y_xuxv;
3520
3521 gr->solid_shader.vertex_source = vertex_shader;
3522 gr->solid_shader.fragment_source = solid_fragment_shader;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003523
3524 return 0;
3525}
3526
3527static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003528fragment_debug_binding(struct weston_keyboard *keyboard, uint32_t time,
3529 uint32_t key, void *data)
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003530{
3531 struct weston_compositor *ec = data;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003532 struct gl_renderer *gr = get_renderer(ec);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003533 struct weston_output *output;
3534
John Kåre Alsaker40684142012-11-13 19:10:25 +01003535 gr->fragment_shader_debug ^= 1;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003536
John Kåre Alsaker40684142012-11-13 19:10:25 +01003537 shader_release(&gr->texture_shader_rgba);
3538 shader_release(&gr->texture_shader_rgbx);
3539 shader_release(&gr->texture_shader_egl_external);
3540 shader_release(&gr->texture_shader_y_uv);
3541 shader_release(&gr->texture_shader_y_u_v);
3542 shader_release(&gr->texture_shader_y_xuxv);
3543 shader_release(&gr->solid_shader);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003544
Ander Conselvan de Oliveira03fb4ef2012-12-03 17:08:11 +02003545 /* Force use_shader() to call glUseProgram(), since we need to use
3546 * the recompiled version of the shader. */
3547 gr->current_shader = NULL;
3548
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003549 wl_list_for_each(output, &ec->output_list, link)
3550 weston_output_damage(output);
3551}
3552
Kristian Høgsberg8799d412013-05-07 10:50:09 -04003553static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003554fan_debug_repaint_binding(struct weston_keyboard *keyboard, uint32_t time,
3555 uint32_t key, void *data)
Kristian Høgsberg8799d412013-05-07 10:50:09 -04003556{
3557 struct weston_compositor *compositor = data;
3558 struct gl_renderer *gr = get_renderer(compositor);
3559
3560 gr->fan_debug = !gr->fan_debug;
3561 weston_compositor_damage_all(compositor);
3562}
3563
John Kåre Alsaker94659272012-11-13 19:10:18 +01003564static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003565gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003566{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003567 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003568 const char *extensions;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003569 EGLConfig context_config;
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003570 EGLBoolean ret;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003571
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003572 static const EGLint context_attribs[] = {
3573 EGL_CONTEXT_CLIENT_VERSION, 2,
3574 EGL_NONE
3575 };
3576
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003577 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
3578 weston_log("failed to bind EGL_OPENGL_ES_API\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02003579 gl_renderer_print_egl_error_state();
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003580 return -1;
3581 }
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03003582
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003583 context_config = gr->egl_config;
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03003584
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003585 if (gr->has_configless_context)
Armin Krezoviće3bfee12016-09-30 14:27:38 +02003586 context_config = EGL_NO_CONFIG_KHR;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003587
3588 gr->egl_context = eglCreateContext(gr->egl_display, context_config,
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003589 EGL_NO_CONTEXT, context_attribs);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003590 if (gr->egl_context == NULL) {
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003591 weston_log("failed to create context\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02003592 gl_renderer_print_egl_error_state();
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003593 return -1;
3594 }
3595
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003596 ret = eglMakeCurrent(gr->egl_display, egl_surface,
3597 egl_surface, gr->egl_context);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003598 if (ret == EGL_FALSE) {
3599 weston_log("Failed to make EGL context current.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02003600 gl_renderer_print_egl_error_state();
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003601 return -1;
3602 }
3603
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003604 log_egl_gl_info(gr->egl_display);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003605
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003606 gr->image_target_texture_2d =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003607 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003608
3609 extensions = (const char *) glGetString(GL_EXTENSIONS);
3610 if (!extensions) {
3611 weston_log("Retrieving GL extension string failed.\n");
3612 return -1;
3613 }
3614
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003615 if (!weston_check_egl_extension(extensions, "GL_EXT_texture_format_BGRA8888")) {
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003616 weston_log("GL_EXT_texture_format_BGRA8888 not available\n");
3617 return -1;
3618 }
3619
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003620 if (weston_check_egl_extension(extensions, "GL_EXT_read_format_bgra"))
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01003621 ec->read_format = PIXMAN_a8r8g8b8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003622 else
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01003623 ec->read_format = PIXMAN_a8b8g8r8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003624
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003625 if (weston_check_egl_extension(extensions, "GL_EXT_unpack_subimage"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003626 gr->has_unpack_subimage = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003627
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003628 if (weston_check_egl_extension(extensions, "GL_OES_EGL_image_external"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003629 gr->has_egl_image_external = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003630
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003631 glActiveTexture(GL_TEXTURE0);
3632
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003633 if (compile_shaders(ec))
3634 return -1;
3635
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03003636 gr->fragment_binding =
3637 weston_compositor_add_debug_binding(ec, KEY_S,
3638 fragment_debug_binding,
3639 ec);
3640 gr->fan_binding =
3641 weston_compositor_add_debug_binding(ec, KEY_F,
3642 fan_debug_repaint_binding,
3643 ec);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003644
Armin Krezović36d699a2016-08-05 15:28:30 +02003645 gr->output_destroy_listener.notify = output_handle_destroy;
3646 wl_signal_add(&ec->output_destroyed_signal,
3647 &gr->output_destroy_listener);
3648
Pekka Paalanen035a0322012-10-24 09:43:06 +03003649 weston_log("GL ES 2 renderer features:\n");
3650 weston_log_continue(STAMP_SPACE "read-back format: %s\n",
Pekka Paalanenfe4eacf2013-01-10 16:50:42 +02003651 ec->read_format == PIXMAN_a8r8g8b8 ? "BGRA" : "RGBA");
Pekka Paalanen035a0322012-10-24 09:43:06 +03003652 weston_log_continue(STAMP_SPACE "wl_shm sub-image to texture: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003653 gr->has_unpack_subimage ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03003654 weston_log_continue(STAMP_SPACE "EGL Wayland extension: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003655 gr->has_bind_display ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03003656
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003657
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003658 return 0;
3659}
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003660
3661WL_EXPORT struct gl_renderer_interface gl_renderer_interface = {
3662 .opaque_attribs = gl_renderer_opaque_attribs,
3663 .alpha_attribs = gl_renderer_alpha_attribs,
3664
Miguel A. Vicodddc6702016-05-18 17:41:07 +02003665 .display_create = gl_renderer_display_create,
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003666 .display = gl_renderer_display,
Miguel A. Vicoc095cde2016-05-18 17:43:00 +02003667 .output_window_create = gl_renderer_output_window_create,
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003668 .output_destroy = gl_renderer_output_destroy,
3669 .output_surface = gl_renderer_output_surface,
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05003670 .output_set_border = gl_renderer_output_set_border,
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003671 .print_egl_error_state = gl_renderer_print_egl_error_state
3672};