blob: c2e88a65bc288de5886019f3fb4d783f6372fc40 [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>
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -040042
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010043#include "gl-renderer.h"
Sam Spilsbury619859c2013-09-13 10:01:21 +080044#include "vertex-clipping.h"
Pekka Paalanena3525802014-06-12 16:49:29 +030045#include "linux-dmabuf.h"
Jonas Ådahl57e48f02015-11-17 16:00:28 +080046#include "linux-dmabuf-unstable-v1-server-protocol.h"
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010047
Jon Cruz35b2eaa2015-06-15 15:37:08 -070048#include "shared/helpers.h"
Emil Velikovf0c3a1c2016-07-04 15:34:18 +010049#include "shared/platform.h"
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010050#include "weston-egl-ext.h"
Kristian Høgsbergd7c17262012-09-05 21:54:15 -040051
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010052struct gl_shader {
John Kåre Alsaker40684142012-11-13 19:10:25 +010053 GLuint program;
54 GLuint vertex_shader, fragment_shader;
55 GLint proj_uniform;
56 GLint tex_uniforms[3];
57 GLint alpha_uniform;
58 GLint color_uniform;
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +030059 const char *vertex_source, *fragment_source;
John Kåre Alsaker40684142012-11-13 19:10:25 +010060};
61
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +020062#define BUFFER_DAMAGE_COUNT 2
63
Jason Ekstrande5512d42014-02-04 21:36:38 -060064enum gl_border_status {
65 BORDER_STATUS_CLEAN = 0,
66 BORDER_TOP_DIRTY = 1 << GL_RENDERER_BORDER_TOP,
67 BORDER_LEFT_DIRTY = 1 << GL_RENDERER_BORDER_LEFT,
68 BORDER_RIGHT_DIRTY = 1 << GL_RENDERER_BORDER_RIGHT,
69 BORDER_BOTTOM_DIRTY = 1 << GL_RENDERER_BORDER_BOTTOM,
70 BORDER_ALL_DIRTY = 0xf,
71 BORDER_SIZE_CHANGED = 0x10
72};
73
Jason Ekstrand0b61bf42013-10-27 22:24:54 -050074struct gl_border_image {
75 GLuint tex;
76 int32_t width, height;
77 int32_t tex_width;
Jason Ekstrand0b61bf42013-10-27 22:24:54 -050078 void *data;
79};
80
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010081struct gl_output_state {
John Kåre Alsaker94659272012-11-13 19:10:18 +010082 EGLSurface egl_surface;
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +020083 pixman_region32_t buffer_damage[BUFFER_DAMAGE_COUNT];
Derek Foreman4c582662014-10-09 18:39:44 -050084 int buffer_damage_index;
Jason Ekstrande5512d42014-02-04 21:36:38 -060085 enum gl_border_status border_damage[BUFFER_DAMAGE_COUNT];
Jason Ekstrand0b61bf42013-10-27 22:24:54 -050086 struct gl_border_image borders[4];
Jason Ekstrande5512d42014-02-04 21:36:38 -060087 enum gl_border_status border_status;
Jason Ekstrandfb23df72014-10-16 10:55:21 -050088
89 struct weston_matrix output_matrix;
John Kåre Alsaker94659272012-11-13 19:10:18 +010090};
91
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +030092enum buffer_type {
93 BUFFER_TYPE_NULL,
Pekka Paalanenaeb917e2015-02-09 13:56:56 +020094 BUFFER_TYPE_SOLID, /* internal solid color surfaces without a buffer */
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +030095 BUFFER_TYPE_SHM,
96 BUFFER_TYPE_EGL
97};
98
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +030099struct gl_renderer;
100
101struct egl_image {
102 struct gl_renderer *renderer;
103 EGLImageKHR image;
104 int refcount;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +0000105};
Pekka Paalanena3525802014-06-12 16:49:29 +0300106
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +0000107enum import_type {
108 IMPORT_TYPE_INVALID,
109 IMPORT_TYPE_DIRECT,
110 IMPORT_TYPE_GL_CONVERSION
111};
112
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +0000113struct dmabuf_image {
Pekka Paalanena3525802014-06-12 16:49:29 +0300114 struct linux_dmabuf_buffer *dmabuf;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +0000115 int num_images;
116 struct egl_image *images[3];
Pekka Paalanena3525802014-06-12 16:49:29 +0300117 struct wl_list link;
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +0000118
119 enum import_type import_type;
120 GLenum target;
121 struct gl_shader *shader;
122};
123
124struct yuv_plane_descriptor {
125 int width_divisor;
126 int height_divisor;
127 uint32_t format;
128 int plane_index;
129};
130
131struct yuv_format_descriptor {
132 uint32_t format;
133 int input_planes;
134 int output_planes;
135 int texture_type;
136 struct yuv_plane_descriptor plane[4];
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +0300137};
138
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100139struct gl_surface_state {
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100140 GLfloat color[4];
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100141 struct gl_shader *shader;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100142
143 GLuint textures[3];
144 int num_textures;
Derek Foreman4c11fe72015-11-18 16:32:27 -0600145 bool needs_full_upload;
Pekka Paalanen81ee3f52012-12-04 15:58:16 +0200146 pixman_region32_t texture_damage;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100147
Neil Roberts4d085e72014-04-07 15:01:01 +0100148 /* These are only used by SHM surfaces to detect when we need
149 * to do a full upload to specify a new internal texture
150 * format */
Vincent Abriou00a03d22016-10-05 14:54:35 +0200151 GLenum gl_format[3];
Neil Roberts4d085e72014-04-07 15:01:01 +0100152 GLenum gl_pixel_type;
153
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +0300154 struct egl_image* images[3];
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100155 GLenum target;
156 int num_images;
Pekka Paalanenfb003d32012-12-04 15:58:13 +0200157
158 struct weston_buffer_reference buffer_ref;
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +0300159 enum buffer_type buffer_type;
Pekka Paalanen68033ac2012-12-04 15:58:15 +0200160 int pitch; /* in pixels */
Alexander Larsson4ea95522013-05-22 14:41:37 +0200161 int height; /* in pixels */
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +0400162 int y_inverted;
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300163
Vincent Abrioufdeefe42016-10-05 14:54:34 +0200164 /* Extension needed for SHM YUV texture */
165 int offset[3]; /* offset per plane */
Vincent ABRIOUe5732c72016-10-20 16:20:11 +0200166 int hsub[3]; /* horizontal subsampling per plane */
167 int vsub[3]; /* vertical subsampling per plane */
Vincent Abrioufdeefe42016-10-05 14:54:34 +0200168
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300169 struct weston_surface *surface;
170
171 struct wl_listener surface_destroy_listener;
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +0300172 struct wl_listener renderer_destroy_listener;
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100173};
174
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100175struct gl_renderer {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100176 struct weston_renderer base;
177 int fragment_shader_debug;
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400178 int fan_debug;
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +0300179 struct weston_binding *fragment_binding;
180 struct weston_binding *fan_binding;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100181
182 EGLDisplay egl_display;
183 EGLContext egl_context;
184 EGLConfig egl_config;
John Kåre Alsaker44154502012-11-13 19:10:20 +0100185
Armin Krezović28d240f2016-06-23 11:59:35 +0200186 EGLSurface dummy_surface;
187
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400188 struct wl_array vertices;
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400189 struct wl_array vtxcnt;
190
John Kåre Alsaker320711d2012-11-13 19:10:27 +0100191 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
192 PFNEGLCREATEIMAGEKHRPROC create_image;
193 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -0600194 PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC swap_buffers_with_damage;
Jonny Lamb671148f2015-03-20 15:26:52 +0100195 PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC create_platform_window;
Jonny Lamb671148f2015-03-20 15:26:52 +0100196
John Kåre Alsaker320711d2012-11-13 19:10:27 +0100197 int has_unpack_subimage;
198
199 PFNEGLBINDWAYLANDDISPLAYWL bind_display;
200 PFNEGLUNBINDWAYLANDDISPLAYWL unbind_display;
201 PFNEGLQUERYWAYLANDBUFFERWL query_buffer;
202 int has_bind_display;
203
204 int has_egl_image_external;
205
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +0200206 int has_egl_buffer_age;
207
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000208 int has_configless_context;
209
Armin Krezović28d240f2016-06-23 11:59:35 +0200210 int has_surfaceless_context;
211
Pekka Paalanena3525802014-06-12 16:49:29 +0300212 int has_dmabuf_import;
213 struct wl_list dmabuf_images;
214
Vincent Abrioufdeefe42016-10-05 14:54:34 +0200215 int has_gl_texture_rg;
216
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100217 struct gl_shader texture_shader_rgba;
218 struct gl_shader texture_shader_rgbx;
219 struct gl_shader texture_shader_egl_external;
220 struct gl_shader texture_shader_y_uv;
221 struct gl_shader texture_shader_y_u_v;
222 struct gl_shader texture_shader_y_xuxv;
223 struct gl_shader invert_color_shader;
224 struct gl_shader solid_shader;
225 struct gl_shader *current_shader;
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +0300226
227 struct wl_signal destroy_signal;
Armin Krezović36d699a2016-08-05 15:28:30 +0200228
229 struct wl_listener output_destroy_listener;
Varad Gautam0775cd12016-11-23 14:03:20 +0530230
231 int has_dmabuf_import_modifiers;
232 PFNEGLQUERYDMABUFFORMATSEXTPROC query_dmabuf_formats;
233 PFNEGLQUERYDMABUFMODIFIERSEXTPROC query_dmabuf_modifiers;
Alexandros Frantzis7192b172017-09-27 15:09:14 +0300234
235 int has_native_fence_sync;
236 PFNEGLCREATESYNCKHRPROC create_sync;
237 PFNEGLDESTROYSYNCKHRPROC destroy_sync;
238 PFNEGLDUPNATIVEFENCEFDANDROIDPROC dup_native_fence_fd;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100239};
John Kåre Alsaker94659272012-11-13 19:10:18 +0100240
Jonny Lamb70eba3f2015-03-20 15:26:50 +0100241static PFNEGLGETPLATFORMDISPLAYEXTPROC get_platform_display = NULL;
Jonny Lamb70eba3f2015-03-20 15:26:50 +0100242
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +0000243static inline const char *
244dump_format(uint32_t format, char out[4])
245{
246#if BYTE_ORDER == BIG_ENDIAN
247 format = __builtin_bswap32(format);
248#endif
249 memcpy(out, &format, 4);
250 return out;
251}
252
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100253static inline struct gl_output_state *
John Kåre Alsaker94659272012-11-13 19:10:18 +0100254get_output_state(struct weston_output *output)
255{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100256 return (struct gl_output_state *)output->renderer_state;
John Kåre Alsaker94659272012-11-13 19:10:18 +0100257}
258
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300259static int
260gl_renderer_create_surface(struct weston_surface *surface);
261
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100262static inline struct gl_surface_state *
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100263get_surface_state(struct weston_surface *surface)
264{
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300265 if (!surface->renderer_state)
266 gl_renderer_create_surface(surface);
267
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100268 return (struct gl_surface_state *)surface->renderer_state;
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100269}
270
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100271static inline struct gl_renderer *
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100272get_renderer(struct weston_compositor *ec)
273{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100274 return (struct gl_renderer *)ec->renderer;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100275}
276
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +0300277static struct egl_image*
278egl_image_create(struct gl_renderer *gr, EGLenum target,
279 EGLClientBuffer buffer, const EGLint *attribs)
280{
281 struct egl_image *img;
282
283 img = zalloc(sizeof *img);
284 img->renderer = gr;
285 img->refcount = 1;
286 img->image = gr->create_image(gr->egl_display, EGL_NO_CONTEXT,
287 target, buffer, attribs);
288
289 if (img->image == EGL_NO_IMAGE_KHR) {
290 free(img);
291 return NULL;
292 }
293
294 return img;
295}
296
297static struct egl_image*
298egl_image_ref(struct egl_image *image)
299{
300 image->refcount++;
301
302 return image;
303}
304
305static int
306egl_image_unref(struct egl_image *image)
307{
308 struct gl_renderer *gr = image->renderer;
309
310 assert(image->refcount > 0);
311
312 image->refcount--;
313 if (image->refcount > 0)
314 return image->refcount;
315
316 gr->destroy_image(gr->egl_display, image->image);
317 free(image);
318
319 return 0;
320}
321
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +0000322static struct dmabuf_image*
323dmabuf_image_create(void)
324{
325 struct dmabuf_image *img;
326
327 img = zalloc(sizeof *img);
328 wl_list_init(&img->link);
329
330 return img;
331}
332
333static void
334dmabuf_image_destroy(struct dmabuf_image *image)
335{
336 int i;
337
338 for (i = 0; i < image->num_images; ++i)
339 egl_image_unref(image->images[i]);
340
341 if (image->dmabuf)
342 linux_dmabuf_buffer_set_user_data(image->dmabuf, NULL, NULL);
343
344 wl_list_remove(&image->link);
Arnaud Vrac7b5fe9b2017-08-05 13:58:58 +0200345 free(image);
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +0000346}
347
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400348static const char *
349egl_error_string(EGLint code)
350{
351#define MYERRCODE(x) case x: return #x;
352 switch (code) {
353 MYERRCODE(EGL_SUCCESS)
354 MYERRCODE(EGL_NOT_INITIALIZED)
355 MYERRCODE(EGL_BAD_ACCESS)
356 MYERRCODE(EGL_BAD_ALLOC)
357 MYERRCODE(EGL_BAD_ATTRIBUTE)
358 MYERRCODE(EGL_BAD_CONTEXT)
359 MYERRCODE(EGL_BAD_CONFIG)
360 MYERRCODE(EGL_BAD_CURRENT_SURFACE)
361 MYERRCODE(EGL_BAD_DISPLAY)
362 MYERRCODE(EGL_BAD_SURFACE)
363 MYERRCODE(EGL_BAD_MATCH)
364 MYERRCODE(EGL_BAD_PARAMETER)
365 MYERRCODE(EGL_BAD_NATIVE_PIXMAP)
366 MYERRCODE(EGL_BAD_NATIVE_WINDOW)
367 MYERRCODE(EGL_CONTEXT_LOST)
368 default:
369 return "unknown";
370 }
371#undef MYERRCODE
372}
373
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300374static void
Pekka Paalanen326529f2012-11-27 12:25:25 +0200375gl_renderer_print_egl_error_state(void)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400376{
377 EGLint code;
378
379 code = eglGetError();
380 weston_log("EGL error state: %s (0x%04lx)\n",
381 egl_error_string(code), (long)code);
382}
383
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400384#define max(a, b) (((a) > (b)) ? (a) : (b))
385#define min(a, b) (((a) > (b)) ? (b) : (a))
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400386
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300387/*
388 * Compute the boundary vertices of the intersection of the global coordinate
389 * aligned rectangle 'rect', and an arbitrary quadrilateral produced from
390 * 'surf_rect' when transformed from surface coordinates into global coordinates.
391 * The vertices are written to 'ex' and 'ey', and the return value is the
392 * number of vertices. Vertices are produced in clockwise winding order.
393 * Guarantees to produce either zero vertices, or 3-8 vertices with non-zero
394 * polygon area.
395 */
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400396static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500397calculate_edges(struct weston_view *ev, pixman_box32_t *rect,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400398 pixman_box32_t *surf_rect, GLfloat *ex, GLfloat *ey)
399{
Sam Spilsbury619859c2013-09-13 10:01:21 +0800400
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300401 struct clip_context ctx;
402 int i, n;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400403 GLfloat min_x, max_x, min_y, max_y;
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300404 struct polygon8 surf = {
405 { surf_rect->x1, surf_rect->x2, surf_rect->x2, surf_rect->x1 },
406 { surf_rect->y1, surf_rect->y1, surf_rect->y2, surf_rect->y2 },
407 4
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400408 };
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400409
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300410 ctx.clip.x1 = rect->x1;
411 ctx.clip.y1 = rect->y1;
412 ctx.clip.x2 = rect->x2;
413 ctx.clip.y2 = rect->y2;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400414
415 /* transform surface to screen space: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300416 for (i = 0; i < surf.n; i++)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500417 weston_view_to_global_float(ev, surf.x[i], surf.y[i],
418 &surf.x[i], &surf.y[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400419
420 /* find bounding box: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300421 min_x = max_x = surf.x[0];
422 min_y = max_y = surf.y[0];
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400423
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300424 for (i = 1; i < surf.n; i++) {
425 min_x = min(min_x, surf.x[i]);
426 max_x = max(max_x, surf.x[i]);
427 min_y = min(min_y, surf.y[i]);
428 max_y = max(max_y, surf.y[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400429 }
430
431 /* First, simple bounding box check to discard early transformed
432 * surface rects that do not intersect with the clip region:
433 */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300434 if ((min_x >= ctx.clip.x2) || (max_x <= ctx.clip.x1) ||
435 (min_y >= ctx.clip.y2) || (max_y <= ctx.clip.y1))
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400436 return 0;
437
438 /* Simple case, bounding box edges are parallel to surface edges,
439 * there will be only four edges. We just need to clip the surface
440 * vertices to the clip rect bounds:
441 */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500442 if (!ev->transform.enabled)
Sam Spilsbury619859c2013-09-13 10:01:21 +0800443 return clip_simple(&ctx, &surf, ex, ey);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400444
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300445 /* Transformed case: use a general polygon clipping algorithm to
446 * clip the surface rectangle with each side of 'rect'.
447 * The algorithm is Sutherland-Hodgman, as explained in
448 * http://www.codeguru.com/cpp/misc/misc/graphics/article.php/c8965/Polygon-Clipping.htm
449 * but without looking at any of that code.
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400450 */
Sam Spilsbury619859c2013-09-13 10:01:21 +0800451 n = clip_transformed(&ctx, &surf, ex, ey);
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300452
453 if (n < 3)
454 return 0;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400455
456 return n;
457}
458
Derek Foremanf8180982014-10-16 16:37:02 -0500459static bool
460merge_down(pixman_box32_t *a, pixman_box32_t *b, pixman_box32_t *merge)
461{
462 if (a->x1 == b->x1 && a->x2 == b->x2 && a->y1 == b->y2) {
463 merge->x1 = a->x1;
464 merge->x2 = a->x2;
465 merge->y1 = b->y1;
466 merge->y2 = a->y2;
467 return true;
468 }
469 return false;
470}
471
472static int
473compress_bands(pixman_box32_t *inrects, int nrects,
474 pixman_box32_t **outrects)
475{
Quentin Glidicd84af7c2016-07-10 11:00:50 +0200476 bool merged = false;
Derek Foremanf8180982014-10-16 16:37:02 -0500477 pixman_box32_t *out, merge_rect;
478 int i, j, nout;
479
480 if (!nrects) {
481 *outrects = NULL;
482 return 0;
483 }
484
485 /* nrects is an upper bound - we're not too worried about
486 * allocating a little extra
487 */
488 out = malloc(sizeof(pixman_box32_t) * nrects);
489 out[0] = inrects[0];
490 nout = 1;
491 for (i = 1; i < nrects; i++) {
492 for (j = 0; j < nout; j++) {
493 merged = merge_down(&inrects[i], &out[j], &merge_rect);
494 if (merged) {
495 out[j] = merge_rect;
496 break;
497 }
498 }
499 if (!merged) {
500 out[nout] = inrects[i];
501 nout++;
502 }
503 }
504 *outrects = out;
505 return nout;
506}
507
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400508static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500509texture_region(struct weston_view *ev, pixman_region32_t *region,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400510 pixman_region32_t *surf_region)
511{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500512 struct gl_surface_state *gs = get_surface_state(ev->surface);
513 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400514 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400515 GLfloat *v, inv_width, inv_height;
516 unsigned int *vtxcnt, nvtx = 0;
517 pixman_box32_t *rects, *surf_rects;
Derek Foremanf8180982014-10-16 16:37:02 -0500518 pixman_box32_t *raw_rects;
519 int i, j, k, nrects, nsurf, raw_nrects;
520 bool used_band_compression;
521 raw_rects = pixman_region32_rectangles(region, &raw_nrects);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400522 surf_rects = pixman_region32_rectangles(surf_region, &nsurf);
523
Derek Foremanf8180982014-10-16 16:37:02 -0500524 if (raw_nrects < 4) {
525 used_band_compression = false;
526 nrects = raw_nrects;
527 rects = raw_rects;
528 } else {
529 nrects = compress_bands(raw_rects, raw_nrects, &rects);
530 used_band_compression = true;
531 }
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400532 /* worst case we can have 8 vertices per rect (ie. clipped into
533 * an octagon):
534 */
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400535 v = wl_array_add(&gr->vertices, nrects * nsurf * 8 * 4 * sizeof *v);
536 vtxcnt = wl_array_add(&gr->vtxcnt, nrects * nsurf * sizeof *vtxcnt);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400537
Pekka Paalanen68033ac2012-12-04 15:58:15 +0200538 inv_width = 1.0 / gs->pitch;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200539 inv_height = 1.0 / gs->height;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400540
541 for (i = 0; i < nrects; i++) {
542 pixman_box32_t *rect = &rects[i];
543 for (j = 0; j < nsurf; j++) {
544 pixman_box32_t *surf_rect = &surf_rects[j];
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200545 GLfloat sx, sy, bx, by;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400546 GLfloat ex[8], ey[8]; /* edge points in screen space */
547 int n;
548
549 /* The transformed surface, after clipping to the clip region,
550 * can have as many as eight sides, emitted as a triangle-fan.
551 * The first vertex in the triangle fan can be chosen arbitrarily,
552 * since the area is guaranteed to be convex.
553 *
554 * If a corner of the transformed surface falls outside of the
555 * clip region, instead of emitting one vertex for the corner
556 * of the surface, up to two are emitted for two corresponding
557 * intersection point(s) between the surface and the clip region.
558 *
559 * To do this, we first calculate the (up to eight) points that
560 * form the intersection of the clip rect and the transformed
561 * surface.
562 */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500563 n = calculate_edges(ev, rect, surf_rect, ex, ey);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400564 if (n < 3)
565 continue;
566
567 /* emit edge points: */
568 for (k = 0; k < n; k++) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500569 weston_view_from_global_float(ev, ex[k], ey[k],
570 &sx, &sy);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400571 /* position: */
572 *(v++) = ex[k];
573 *(v++) = ey[k];
574 /* texcoord: */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500575 weston_surface_to_buffer_float(ev->surface,
576 sx, sy,
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200577 &bx, &by);
578 *(v++) = bx * inv_width;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +0400579 if (gs->y_inverted) {
580 *(v++) = by * inv_height;
581 } else {
582 *(v++) = (gs->height - by) * inv_height;
583 }
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400584 }
585
586 vtxcnt[nvtx++] = n;
587 }
588 }
589
Derek Foremanf8180982014-10-16 16:37:02 -0500590 if (used_band_compression)
591 free(rects);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400592 return nvtx;
593}
594
595static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500596triangle_fan_debug(struct weston_view *view, int first, int count)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400597{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500598 struct weston_compositor *compositor = view->surface->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100599 struct gl_renderer *gr = get_renderer(compositor);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400600 int i;
601 GLushort *buffer;
602 GLushort *index;
603 int nelems;
604 static int color_idx = 0;
605 static const GLfloat color[][4] = {
606 { 1.0, 0.0, 0.0, 1.0 },
607 { 0.0, 1.0, 0.0, 1.0 },
608 { 0.0, 0.0, 1.0, 1.0 },
609 { 1.0, 1.0, 1.0, 1.0 },
610 };
611
612 nelems = (count - 1 + count - 2) * 2;
613
614 buffer = malloc(sizeof(GLushort) * nelems);
615 index = buffer;
616
617 for (i = 1; i < count; i++) {
618 *index++ = first;
619 *index++ = first + i;
620 }
621
622 for (i = 2; i < count; i++) {
623 *index++ = first + i - 1;
624 *index++ = first + i;
625 }
626
John Kåre Alsaker40684142012-11-13 19:10:25 +0100627 glUseProgram(gr->solid_shader.program);
628 glUniform4fv(gr->solid_shader.color_uniform, 1,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400629 color[color_idx++ % ARRAY_LENGTH(color)]);
630 glDrawElements(GL_LINES, nelems, GL_UNSIGNED_SHORT, buffer);
John Kåre Alsaker40684142012-11-13 19:10:25 +0100631 glUseProgram(gr->current_shader->program);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400632 free(buffer);
633}
634
635static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500636repaint_region(struct weston_view *ev, pixman_region32_t *region,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400637 pixman_region32_t *surf_region)
638{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500639 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400640 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400641 GLfloat *v;
642 unsigned int *vtxcnt;
643 int i, first, nfans;
644
645 /* The final region to be painted is the intersection of
646 * 'region' and 'surf_region'. However, 'region' is in the global
647 * coordinates, and 'surf_region' is in the surface-local
648 * coordinates. texture_region() will iterate over all pairs of
649 * rectangles from both regions, compute the intersection
650 * polygon for each pair, and store it as a triangle fan if
Bryce Harringtonea8fb942016-01-13 18:48:56 -0800651 * it has a non-zero area (at least 3 vertices, actually).
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400652 */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500653 nfans = texture_region(ev, region, surf_region);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400654
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400655 v = gr->vertices.data;
656 vtxcnt = gr->vtxcnt.data;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400657
658 /* position: */
659 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
660 glEnableVertexAttribArray(0);
661
662 /* texcoord: */
663 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
664 glEnableVertexAttribArray(1);
665
666 for (i = 0, first = 0; i < nfans; i++) {
667 glDrawArrays(GL_TRIANGLE_FAN, first, vtxcnt[i]);
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400668 if (gr->fan_debug)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500669 triangle_fan_debug(ev, first, vtxcnt[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400670 first += vtxcnt[i];
671 }
672
673 glDisableVertexAttribArray(1);
674 glDisableVertexAttribArray(0);
675
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400676 gr->vertices.size = 0;
677 gr->vtxcnt.size = 0;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400678}
679
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100680static int
681use_output(struct weston_output *output)
682{
683 static int errored;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100684 struct gl_output_state *go = get_output_state(output);
685 struct gl_renderer *gr = get_renderer(output->compositor);
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100686 EGLBoolean ret;
687
688 ret = eglMakeCurrent(gr->egl_display, go->egl_surface,
689 go->egl_surface, gr->egl_context);
690
691 if (ret == EGL_FALSE) {
692 if (errored)
693 return -1;
694 errored = 1;
695 weston_log("Failed to make EGL context current.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +0200696 gl_renderer_print_egl_error_state();
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100697 return -1;
698 }
699
700 return 0;
701}
702
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +0300703static int
704shader_init(struct gl_shader *shader, struct gl_renderer *gr,
705 const char *vertex_source, const char *fragment_source);
706
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400707static void
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +0300708use_shader(struct gl_renderer *gr, struct gl_shader *shader)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400709{
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +0300710 if (!shader->program) {
711 int ret;
712
713 ret = shader_init(shader, gr,
714 shader->vertex_source,
715 shader->fragment_source);
716
717 if (ret < 0)
718 weston_log("warning: failed to compile shader\n");
719 }
720
John Kåre Alsaker40684142012-11-13 19:10:25 +0100721 if (gr->current_shader == shader)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400722 return;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400723 glUseProgram(shader->program);
John Kåre Alsaker40684142012-11-13 19:10:25 +0100724 gr->current_shader = shader;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400725}
726
727static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100728shader_uniforms(struct gl_shader *shader,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500729 struct weston_view *view,
730 struct weston_output *output)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400731{
732 int i;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500733 struct gl_surface_state *gs = get_surface_state(view->surface);
Jason Ekstrandfb23df72014-10-16 10:55:21 -0500734 struct gl_output_state *go = get_output_state(output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400735
736 glUniformMatrix4fv(shader->proj_uniform,
Jason Ekstrandfb23df72014-10-16 10:55:21 -0500737 1, GL_FALSE, go->output_matrix.d);
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100738 glUniform4fv(shader->color_uniform, 1, gs->color);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500739 glUniform1f(shader->alpha_uniform, view->alpha);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400740
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100741 for (i = 0; i < gs->num_textures; i++)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400742 glUniform1i(shader->tex_uniforms[i], i);
743}
744
745static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500746draw_view(struct weston_view *ev, struct weston_output *output,
747 pixman_region32_t *damage) /* in global coordinates */
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400748{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500749 struct weston_compositor *ec = ev->surface->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100750 struct gl_renderer *gr = get_renderer(ec);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500751 struct gl_surface_state *gs = get_surface_state(ev->surface);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400752 /* repaint bounding region in global coordinates: */
753 pixman_region32_t repaint;
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +0200754 /* opaque region in surface coordinates: */
755 pixman_region32_t surface_opaque;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400756 /* non-opaque region in surface coordinates: */
757 pixman_region32_t surface_blend;
758 GLint filter;
759 int i;
760
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +0200761 /* In case of a runtime switch of renderers, we may not have received
762 * an attach for this surface since the switch. In that case we don't
763 * have a valid buffer or a proper shader set up so skip rendering. */
764 if (!gs->shader)
765 return;
766
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400767 pixman_region32_init(&repaint);
768 pixman_region32_intersect(&repaint,
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200769 &ev->transform.boundingbox, damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500770 pixman_region32_subtract(&repaint, &repaint, &ev->clip);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400771
772 if (!pixman_region32_not_empty(&repaint))
773 goto out;
774
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400775 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
776
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400777 if (gr->fan_debug) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100778 use_shader(gr, &gr->solid_shader);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500779 shader_uniforms(&gr->solid_shader, ev, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400780 }
781
John Kåre Alsaker40684142012-11-13 19:10:25 +0100782 use_shader(gr, gs->shader);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500783 shader_uniforms(gs->shader, ev, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400784
Jason Ekstranda7af7042013-10-12 22:38:11 -0500785 if (ev->transform.enabled || output->zoom.active ||
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200786 output->current_scale != ev->surface->buffer_viewport.buffer.scale)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400787 filter = GL_LINEAR;
788 else
789 filter = GL_NEAREST;
790
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100791 for (i = 0; i < gs->num_textures; i++) {
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400792 glActiveTexture(GL_TEXTURE0 + i);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100793 glBindTexture(gs->target, gs->textures[i]);
794 glTexParameteri(gs->target, GL_TEXTURE_MIN_FILTER, filter);
795 glTexParameteri(gs->target, GL_TEXTURE_MAG_FILTER, filter);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400796 }
797
798 /* blended region is whole surface minus opaque region: */
799 pixman_region32_init_rect(&surface_blend, 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600800 ev->surface->width, ev->surface->height);
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +0200801 if (ev->geometry.scissor_enabled)
802 pixman_region32_intersect(&surface_blend, &surface_blend,
803 &ev->geometry.scissor);
804 pixman_region32_subtract(&surface_blend, &surface_blend,
805 &ev->surface->opaque);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400806
Jason Ekstranda7af7042013-10-12 22:38:11 -0500807 /* XXX: Should we be using ev->transform.opaque here? */
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +0200808 pixman_region32_init(&surface_opaque);
809 if (ev->geometry.scissor_enabled)
810 pixman_region32_intersect(&surface_opaque,
811 &ev->surface->opaque,
812 &ev->geometry.scissor);
813 else
814 pixman_region32_copy(&surface_opaque, &ev->surface->opaque);
815
816 if (pixman_region32_not_empty(&surface_opaque)) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100817 if (gs->shader == &gr->texture_shader_rgba) {
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400818 /* Special case for RGBA textures with possibly
819 * bad data in alpha channel: use the shader
820 * that forces texture alpha = 1.0.
821 * Xwayland surfaces need this.
822 */
John Kåre Alsaker40684142012-11-13 19:10:25 +0100823 use_shader(gr, &gr->texture_shader_rgbx);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500824 shader_uniforms(&gr->texture_shader_rgbx, ev, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400825 }
826
Jason Ekstranda7af7042013-10-12 22:38:11 -0500827 if (ev->alpha < 1.0)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400828 glEnable(GL_BLEND);
829 else
830 glDisable(GL_BLEND);
831
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +0200832 repaint_region(ev, &repaint, &surface_opaque);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400833 }
834
835 if (pixman_region32_not_empty(&surface_blend)) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100836 use_shader(gr, gs->shader);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400837 glEnable(GL_BLEND);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500838 repaint_region(ev, &repaint, &surface_blend);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400839 }
840
841 pixman_region32_fini(&surface_blend);
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +0200842 pixman_region32_fini(&surface_opaque);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400843
844out:
845 pixman_region32_fini(&repaint);
846}
847
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400848static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500849repaint_views(struct weston_output *output, pixman_region32_t *damage)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400850{
851 struct weston_compositor *compositor = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500852 struct weston_view *view;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400853
Jason Ekstranda7af7042013-10-12 22:38:11 -0500854 wl_list_for_each_reverse(view, &compositor->view_list, link)
855 if (view->plane == &compositor->primary_plane)
856 draw_view(view, output, damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400857}
858
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500859static void
Jason Ekstrande5512d42014-02-04 21:36:38 -0600860draw_output_border_texture(struct gl_output_state *go,
861 enum gl_renderer_border_side side,
862 int32_t x, int32_t y,
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500863 int32_t width, int32_t height)
864{
Jason Ekstrande5512d42014-02-04 21:36:38 -0600865 struct gl_border_image *img = &go->borders[side];
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500866 static GLushort indices [] = { 0, 1, 3, 3, 1, 2 };
867
868 if (!img->data) {
869 if (img->tex) {
870 glDeleteTextures(1, &img->tex);
871 img->tex = 0;
872 }
873
874 return;
875 }
876
877 if (!img->tex) {
878 glGenTextures(1, &img->tex);
879 glBindTexture(GL_TEXTURE_2D, img->tex);
880
881 glTexParameteri(GL_TEXTURE_2D,
882 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
883 glTexParameteri(GL_TEXTURE_2D,
884 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
885 glTexParameteri(GL_TEXTURE_2D,
886 GL_TEXTURE_MIN_FILTER, GL_NEAREST);
887 glTexParameteri(GL_TEXTURE_2D,
888 GL_TEXTURE_MAG_FILTER, GL_NEAREST);
889 } else {
890 glBindTexture(GL_TEXTURE_2D, img->tex);
891 }
892
Jason Ekstrande5512d42014-02-04 21:36:38 -0600893 if (go->border_status & (1 << side)) {
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500894 glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, 0);
895 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, 0);
896 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, 0);
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500897 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
898 img->tex_width, img->height, 0,
899 GL_BGRA_EXT, GL_UNSIGNED_BYTE, img->data);
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500900 }
901
902 GLfloat texcoord[] = {
903 0.0f, 0.0f,
904 (GLfloat)img->width / (GLfloat)img->tex_width, 0.0f,
905 (GLfloat)img->width / (GLfloat)img->tex_width, 1.0f,
906 0.0f, 1.0f,
907 };
908
909 GLfloat verts[] = {
910 x, y,
911 x + width, y,
912 x + width, y + height,
913 x, y + height
914 };
915
916 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, verts);
917 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, texcoord);
918 glEnableVertexAttribArray(0);
919 glEnableVertexAttribArray(1);
920
921 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
922
923 glDisableVertexAttribArray(1);
924 glDisableVertexAttribArray(0);
925}
926
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -0600927static int
928output_has_borders(struct weston_output *output)
929{
930 struct gl_output_state *go = get_output_state(output);
931
932 return go->borders[GL_RENDERER_BORDER_TOP].data ||
933 go->borders[GL_RENDERER_BORDER_RIGHT].data ||
934 go->borders[GL_RENDERER_BORDER_BOTTOM].data ||
935 go->borders[GL_RENDERER_BORDER_LEFT].data;
936}
937
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500938static void
Jason Ekstrande5512d42014-02-04 21:36:38 -0600939draw_output_borders(struct weston_output *output,
940 enum gl_border_status border_status)
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500941{
942 struct gl_output_state *go = get_output_state(output);
943 struct gl_renderer *gr = get_renderer(output->compositor);
944 struct gl_shader *shader = &gr->texture_shader_rgba;
Jason Ekstrand00b84282013-10-27 22:24:59 -0500945 struct gl_border_image *top, *bottom, *left, *right;
946 struct weston_matrix matrix;
947 int full_width, full_height;
948
Jason Ekstrande5512d42014-02-04 21:36:38 -0600949 if (border_status == BORDER_STATUS_CLEAN)
950 return; /* Clean. Nothing to do. */
951
Jason Ekstrand00b84282013-10-27 22:24:59 -0500952 top = &go->borders[GL_RENDERER_BORDER_TOP];
953 bottom = &go->borders[GL_RENDERER_BORDER_BOTTOM];
954 left = &go->borders[GL_RENDERER_BORDER_LEFT];
955 right = &go->borders[GL_RENDERER_BORDER_RIGHT];
956
957 full_width = output->current_mode->width + left->width + right->width;
958 full_height = output->current_mode->height + top->height + bottom->height;
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500959
960 glDisable(GL_BLEND);
961 use_shader(gr, shader);
962
Jason Ekstrand00b84282013-10-27 22:24:59 -0500963 glViewport(0, 0, full_width, full_height);
964
965 weston_matrix_init(&matrix);
966 weston_matrix_translate(&matrix, -full_width/2.0, -full_height/2.0, 0);
967 weston_matrix_scale(&matrix, 2.0/full_width, -2.0/full_height, 1);
968 glUniformMatrix4fv(shader->proj_uniform, 1, GL_FALSE, matrix.d);
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500969
970 glUniform1i(shader->tex_uniforms[0], 0);
971 glUniform1f(shader->alpha_uniform, 1);
972 glActiveTexture(GL_TEXTURE0);
973
Jason Ekstrande5512d42014-02-04 21:36:38 -0600974 if (border_status & BORDER_TOP_DIRTY)
975 draw_output_border_texture(go, GL_RENDERER_BORDER_TOP,
976 0, 0,
977 full_width, top->height);
978 if (border_status & BORDER_LEFT_DIRTY)
979 draw_output_border_texture(go, GL_RENDERER_BORDER_LEFT,
980 0, top->height,
981 left->width, output->current_mode->height);
982 if (border_status & BORDER_RIGHT_DIRTY)
983 draw_output_border_texture(go, GL_RENDERER_BORDER_RIGHT,
984 full_width - right->width, top->height,
985 right->width, output->current_mode->height);
986 if (border_status & BORDER_BOTTOM_DIRTY)
987 draw_output_border_texture(go, GL_RENDERER_BORDER_BOTTOM,
988 0, full_height - bottom->height,
989 full_width, bottom->height);
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500990}
John Kåre Alsaker44154502012-11-13 19:10:20 +0100991
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400992static void
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -0600993output_get_border_damage(struct weston_output *output,
994 enum gl_border_status border_status,
995 pixman_region32_t *damage)
996{
997 struct gl_output_state *go = get_output_state(output);
998 struct gl_border_image *top, *bottom, *left, *right;
999 int full_width, full_height;
1000
1001 if (border_status == BORDER_STATUS_CLEAN)
1002 return; /* Clean. Nothing to do. */
1003
1004 top = &go->borders[GL_RENDERER_BORDER_TOP];
1005 bottom = &go->borders[GL_RENDERER_BORDER_BOTTOM];
1006 left = &go->borders[GL_RENDERER_BORDER_LEFT];
1007 right = &go->borders[GL_RENDERER_BORDER_RIGHT];
1008
1009 full_width = output->current_mode->width + left->width + right->width;
1010 full_height = output->current_mode->height + top->height + bottom->height;
1011 if (border_status & BORDER_TOP_DIRTY)
1012 pixman_region32_union_rect(damage, damage,
1013 0, 0,
1014 full_width, top->height);
1015 if (border_status & BORDER_LEFT_DIRTY)
1016 pixman_region32_union_rect(damage, damage,
1017 0, top->height,
1018 left->width, output->current_mode->height);
1019 if (border_status & BORDER_RIGHT_DIRTY)
1020 pixman_region32_union_rect(damage, damage,
1021 full_width - right->width, top->height,
1022 right->width, output->current_mode->height);
1023 if (border_status & BORDER_BOTTOM_DIRTY)
1024 pixman_region32_union_rect(damage, damage,
1025 0, full_height - bottom->height,
1026 full_width, bottom->height);
1027}
1028
1029static void
Jason Ekstrande5512d42014-02-04 21:36:38 -06001030output_get_damage(struct weston_output *output,
1031 pixman_region32_t *buffer_damage, uint32_t *border_damage)
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001032{
1033 struct gl_output_state *go = get_output_state(output);
1034 struct gl_renderer *gr = get_renderer(output->compositor);
1035 EGLint buffer_age = 0;
1036 EGLBoolean ret;
1037 int i;
1038
1039 if (gr->has_egl_buffer_age) {
1040 ret = eglQuerySurface(gr->egl_display, go->egl_surface,
1041 EGL_BUFFER_AGE_EXT, &buffer_age);
1042 if (ret == EGL_FALSE) {
1043 weston_log("buffer age query failed.\n");
1044 gl_renderer_print_egl_error_state();
1045 }
1046 }
1047
Jason Ekstrande5512d42014-02-04 21:36:38 -06001048 if (buffer_age == 0 || buffer_age - 1 > BUFFER_DAMAGE_COUNT) {
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001049 pixman_region32_copy(buffer_damage, &output->region);
Jason Ekstrande5512d42014-02-04 21:36:38 -06001050 *border_damage = BORDER_ALL_DIRTY;
1051 } else {
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001052 for (i = 0; i < buffer_age - 1; i++)
Derek Foreman4c582662014-10-09 18:39:44 -05001053 *border_damage |= go->border_damage[(go->buffer_damage_index + i) % BUFFER_DAMAGE_COUNT];
Jason Ekstrande5512d42014-02-04 21:36:38 -06001054
1055 if (*border_damage & BORDER_SIZE_CHANGED) {
1056 /* If we've had a resize, we have to do a full
1057 * repaint. */
1058 *border_damage |= BORDER_ALL_DIRTY;
1059 pixman_region32_copy(buffer_damage, &output->region);
1060 } else {
1061 for (i = 0; i < buffer_age - 1; i++)
1062 pixman_region32_union(buffer_damage,
1063 buffer_damage,
Derek Foreman4c582662014-10-09 18:39:44 -05001064 &go->buffer_damage[(go->buffer_damage_index + i) % BUFFER_DAMAGE_COUNT]);
Jason Ekstrande5512d42014-02-04 21:36:38 -06001065 }
1066 }
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001067}
1068
1069static void
1070output_rotate_damage(struct weston_output *output,
Jason Ekstrande5512d42014-02-04 21:36:38 -06001071 pixman_region32_t *output_damage,
1072 enum gl_border_status border_status)
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001073{
1074 struct gl_output_state *go = get_output_state(output);
1075 struct gl_renderer *gr = get_renderer(output->compositor);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001076
1077 if (!gr->has_egl_buffer_age)
1078 return;
1079
Derek Foreman4c582662014-10-09 18:39:44 -05001080 go->buffer_damage_index += BUFFER_DAMAGE_COUNT - 1;
1081 go->buffer_damage_index %= BUFFER_DAMAGE_COUNT;
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001082
Derek Foreman4c582662014-10-09 18:39:44 -05001083 pixman_region32_copy(&go->buffer_damage[go->buffer_damage_index], output_damage);
1084 go->border_damage[go->buffer_damage_index] = border_status;
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001085}
1086
Derek Foremanc4cfe852015-05-15 12:12:40 -05001087/* NOTE: We now allow falling back to ARGB gl visuals when XRGB is
1088 * unavailable, so we're assuming the background has no transparency
1089 * and that everything with a blend, like drop shadows, will have something
1090 * opaque (like the background) drawn underneath it.
1091 *
1092 * Depending on the underlying hardware, violating that assumption could
1093 * result in seeing through to another display plane.
1094 */
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001095static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001096gl_renderer_repaint_output(struct weston_output *output,
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001097 pixman_region32_t *output_damage)
1098{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001099 struct gl_output_state *go = get_output_state(output);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001100 struct weston_compositor *compositor = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001101 struct gl_renderer *gr = get_renderer(compositor);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001102 EGLBoolean ret;
1103 static int errored;
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -06001104 int i, nrects, buffer_height;
1105 EGLint *egl_damage, *d;
1106 pixman_box32_t *rects;
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001107 pixman_region32_t buffer_damage, total_damage;
Jason Ekstrande5512d42014-02-04 21:36:38 -06001108 enum gl_border_status border_damage = BORDER_STATUS_CLEAN;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001109
Jason Ekstrandae0c6e32014-10-16 10:55:20 -05001110 if (use_output(output) < 0)
1111 return;
1112
Jason Ekstrand00b84282013-10-27 22:24:59 -05001113 /* Calculate the viewport */
1114 glViewport(go->borders[GL_RENDERER_BORDER_LEFT].width,
1115 go->borders[GL_RENDERER_BORDER_BOTTOM].height,
1116 output->current_mode->width,
1117 output->current_mode->height);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001118
Jason Ekstrandfb23df72014-10-16 10:55:21 -05001119 /* Calculate the global GL matrix */
1120 go->output_matrix = output->matrix;
1121 weston_matrix_translate(&go->output_matrix,
1122 -(output->current_mode->width / 2.0),
1123 -(output->current_mode->height / 2.0), 0);
1124 weston_matrix_scale(&go->output_matrix,
1125 2.0 / output->current_mode->width,
1126 -2.0 / output->current_mode->height, 1);
1127
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001128 /* if debugging, redraw everything outside the damage to clean up
1129 * debug lines from the previous draw on this buffer:
1130 */
Kristian Høgsberg8799d412013-05-07 10:50:09 -04001131 if (gr->fan_debug) {
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001132 pixman_region32_t undamaged;
1133 pixman_region32_init(&undamaged);
1134 pixman_region32_subtract(&undamaged, &output->region,
1135 output_damage);
Kristian Høgsberg8799d412013-05-07 10:50:09 -04001136 gr->fan_debug = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001137 repaint_views(output, &undamaged);
Kristian Høgsberg8799d412013-05-07 10:50:09 -04001138 gr->fan_debug = 1;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001139 pixman_region32_fini(&undamaged);
1140 }
1141
Ander Conselvan de Oliveirab605c062013-03-05 17:30:28 +02001142 pixman_region32_init(&total_damage);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001143 pixman_region32_init(&buffer_damage);
1144
Jason Ekstrande5512d42014-02-04 21:36:38 -06001145 output_get_damage(output, &buffer_damage, &border_damage);
1146 output_rotate_damage(output, output_damage, go->border_status);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001147
1148 pixman_region32_union(&total_damage, &buffer_damage, output_damage);
Jason Ekstrande5512d42014-02-04 21:36:38 -06001149 border_damage |= go->border_status;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03001150
Jason Ekstranda7af7042013-10-12 22:38:11 -05001151 repaint_views(output, &total_damage);
Ander Conselvan de Oliveirab605c062013-03-05 17:30:28 +02001152
1153 pixman_region32_fini(&total_damage);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001154 pixman_region32_fini(&buffer_damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001155
Jason Ekstrande5512d42014-02-04 21:36:38 -06001156 draw_output_borders(output, border_damage);
John Kåre Alsaker44154502012-11-13 19:10:20 +01001157
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001158 pixman_region32_copy(&output->previous_damage, output_damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001159 wl_signal_emit(&output->frame_signal, output);
1160
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -06001161 if (gr->swap_buffers_with_damage) {
1162 pixman_region32_init(&buffer_damage);
1163 weston_transformed_region(output->width, output->height,
1164 output->transform,
1165 output->current_scale,
1166 output_damage, &buffer_damage);
1167
1168 if (output_has_borders(output)) {
1169 pixman_region32_translate(&buffer_damage,
1170 go->borders[GL_RENDERER_BORDER_LEFT].width,
1171 go->borders[GL_RENDERER_BORDER_TOP].height);
1172 output_get_border_damage(output, go->border_status,
1173 &buffer_damage);
1174 }
1175
1176 rects = pixman_region32_rectangles(&buffer_damage, &nrects);
1177 egl_damage = malloc(nrects * 4 * sizeof(EGLint));
1178
1179 buffer_height = go->borders[GL_RENDERER_BORDER_TOP].height +
1180 output->current_mode->height +
1181 go->borders[GL_RENDERER_BORDER_BOTTOM].height;
1182
1183 d = egl_damage;
1184 for (i = 0; i < nrects; ++i) {
1185 *d++ = rects[i].x1;
1186 *d++ = buffer_height - rects[i].y2;
1187 *d++ = rects[i].x2 - rects[i].x1;
1188 *d++ = rects[i].y2 - rects[i].y1;
1189 }
1190 ret = gr->swap_buffers_with_damage(gr->egl_display,
1191 go->egl_surface,
1192 egl_damage, nrects);
1193 free(egl_damage);
1194 pixman_region32_fini(&buffer_damage);
1195 } else {
1196 ret = eglSwapBuffers(gr->egl_display, go->egl_surface);
1197 }
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -06001198
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001199 if (ret == EGL_FALSE && !errored) {
1200 errored = 1;
1201 weston_log("Failed in eglSwapBuffers.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02001202 gl_renderer_print_egl_error_state();
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001203 }
1204
Jason Ekstrande5512d42014-02-04 21:36:38 -06001205 go->border_status = BORDER_STATUS_CLEAN;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001206}
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001207
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001208static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001209gl_renderer_read_pixels(struct weston_output *output,
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001210 pixman_format_code_t format, void *pixels,
1211 uint32_t x, uint32_t y,
1212 uint32_t width, uint32_t height)
1213{
1214 GLenum gl_format;
Jason Ekstrand701f6362014-04-02 19:53:59 -05001215 struct gl_output_state *go = get_output_state(output);
1216
1217 x += go->borders[GL_RENDERER_BORDER_LEFT].width;
1218 y += go->borders[GL_RENDERER_BORDER_BOTTOM].height;
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001219
1220 switch (format) {
1221 case PIXMAN_a8r8g8b8:
1222 gl_format = GL_BGRA_EXT;
1223 break;
1224 case PIXMAN_a8b8g8r8:
1225 gl_format = GL_RGBA;
1226 break;
1227 default:
1228 return -1;
1229 }
1230
1231 if (use_output(output) < 0)
1232 return -1;
1233
1234 glPixelStorei(GL_PACK_ALIGNMENT, 1);
1235 glReadPixels(x, y, width, height, gl_format,
1236 GL_UNSIGNED_BYTE, pixels);
1237
1238 return 0;
1239}
1240
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001241static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001242gl_renderer_flush_damage(struct weston_surface *surface)
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001243{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001244 struct gl_renderer *gr = get_renderer(surface->compositor);
1245 struct gl_surface_state *gs = get_surface_state(surface);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001246 struct weston_buffer *buffer = gs->buffer_ref.buffer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001247 struct weston_view *view;
Derek Foreman97746792015-11-18 16:32:28 -06001248 bool texture_used;
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001249 pixman_box32_t *rectangles;
Raúl Peñacoba5fc8d5e2017-03-28 18:17:56 +02001250 uint8_t *data;
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001251 int i, j, n;
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001252
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001253 pixman_region32_union(&gs->texture_damage,
1254 &gs->texture_damage, &surface->damage);
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001255
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001256 if (!buffer)
1257 return;
1258
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001259 /* Avoid upload, if the texture won't be used this time.
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001260 * We still accumulate the damage in texture_damage, and
1261 * hold the reference to the buffer, in case the surface
1262 * migrates back to the primary plane.
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001263 */
Derek Foreman97746792015-11-18 16:32:28 -06001264 texture_used = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001265 wl_list_for_each(view, &surface->views, surface_link) {
1266 if (view->plane == &surface->compositor->primary_plane) {
Derek Foreman97746792015-11-18 16:32:28 -06001267 texture_used = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001268 break;
1269 }
1270 }
1271 if (!texture_used)
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001272 return;
1273
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001274 if (!pixman_region32_not_empty(&gs->texture_damage) &&
1275 !gs->needs_full_upload)
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001276 goto done;
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001277
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001278 data = wl_shm_buffer_get_data(buffer->shm_buffer);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001279
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001280 if (!gr->has_unpack_subimage) {
Neil Robertse5051712013-11-13 15:44:06 +00001281 wl_shm_buffer_begin_access(buffer->shm_buffer);
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001282 for (j = 0; j < gs->num_textures; j++) {
1283 glBindTexture(GL_TEXTURE_2D, gs->textures[j]);
1284 glTexImage2D(GL_TEXTURE_2D, 0,
Vincent Abriou00a03d22016-10-05 14:54:35 +02001285 gs->gl_format[j],
Vincent ABRIOUe5732c72016-10-20 16:20:11 +02001286 gs->pitch / gs->hsub[j],
1287 buffer->height / gs->vsub[j],
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001288 0,
Vincent Abriou00a03d22016-10-05 14:54:35 +02001289 gs->gl_format[j],
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001290 gs->gl_pixel_type,
1291 data + gs->offset[j]);
1292 }
Neil Robertse5051712013-11-13 15:44:06 +00001293 wl_shm_buffer_end_access(buffer->shm_buffer);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001294
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001295 goto done;
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001296 }
1297
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -07001298 glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, gs->pitch);
Ander Conselvan de Oliveira6be5f432013-06-07 16:52:45 +03001299
1300 if (gs->needs_full_upload) {
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -07001301 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, 0);
1302 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, 0);
Neil Robertse5051712013-11-13 15:44:06 +00001303 wl_shm_buffer_begin_access(buffer->shm_buffer);
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001304 for (j = 0; j < gs->num_textures; j++) {
1305 glBindTexture(GL_TEXTURE_2D, gs->textures[j]);
1306 glTexImage2D(GL_TEXTURE_2D, 0,
Vincent Abriou00a03d22016-10-05 14:54:35 +02001307 gs->gl_format[j],
Vincent ABRIOUe5732c72016-10-20 16:20:11 +02001308 gs->pitch / gs->hsub[j],
1309 buffer->height / gs->vsub[j],
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001310 0,
Vincent Abriou00a03d22016-10-05 14:54:35 +02001311 gs->gl_format[j],
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001312 gs->gl_pixel_type,
1313 data + gs->offset[j]);
1314 }
Neil Robertse5051712013-11-13 15:44:06 +00001315 wl_shm_buffer_end_access(buffer->shm_buffer);
Ander Conselvan de Oliveira6be5f432013-06-07 16:52:45 +03001316 goto done;
1317 }
1318
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001319 rectangles = pixman_region32_rectangles(&gs->texture_damage, &n);
Neil Robertse5051712013-11-13 15:44:06 +00001320 wl_shm_buffer_begin_access(buffer->shm_buffer);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001321 for (i = 0; i < n; i++) {
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001322 pixman_box32_t r;
1323
1324 r = weston_surface_to_buffer_rect(surface, rectangles[i]);
1325
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -07001326 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, r.x1);
1327 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, r.y1);
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001328 for (j = 0; j < gs->num_textures; j++) {
1329 glBindTexture(GL_TEXTURE_2D, gs->textures[j]);
1330 glTexSubImage2D(GL_TEXTURE_2D, 0,
Vincent ABRIOUe5732c72016-10-20 16:20:11 +02001331 r.x1 / gs->hsub[j],
1332 r.y1 / gs->vsub[j],
1333 (r.x2 - r.x1) / gs->hsub[j],
1334 (r.y2 - r.y1) / gs->vsub[j],
Vincent Abriou00a03d22016-10-05 14:54:35 +02001335 gs->gl_format[j],
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001336 gs->gl_pixel_type,
1337 data + gs->offset[j]);
1338 }
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001339 }
Neil Robertse5051712013-11-13 15:44:06 +00001340 wl_shm_buffer_end_access(buffer->shm_buffer);
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001341
1342done:
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001343 pixman_region32_fini(&gs->texture_damage);
1344 pixman_region32_init(&gs->texture_damage);
Derek Foreman4c11fe72015-11-18 16:32:27 -06001345 gs->needs_full_upload = false;
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001346
1347 weston_buffer_reference(&gs->buffer_ref, NULL);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001348}
1349
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001350static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001351ensure_textures(struct gl_surface_state *gs, int num_textures)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001352{
1353 int i;
1354
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001355 if (num_textures <= gs->num_textures)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001356 return;
1357
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001358 for (i = gs->num_textures; i < num_textures; i++) {
1359 glGenTextures(1, &gs->textures[i]);
1360 glBindTexture(gs->target, gs->textures[i]);
1361 glTexParameteri(gs->target,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001362 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001363 glTexParameteri(gs->target,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001364 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1365 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001366 gs->num_textures = num_textures;
1367 glBindTexture(gs->target, 0);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001368}
1369
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001370static void
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001371gl_renderer_attach_shm(struct weston_surface *es, struct weston_buffer *buffer,
1372 struct wl_shm_buffer *shm_buffer)
1373{
1374 struct weston_compositor *ec = es->compositor;
1375 struct gl_renderer *gr = get_renderer(ec);
1376 struct gl_surface_state *gs = get_surface_state(es);
Vincent Abriou00a03d22016-10-05 14:54:35 +02001377 GLenum gl_format[3] = {0, 0, 0};
1378 GLenum gl_pixel_type;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001379 int pitch;
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001380 int num_planes;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001381
1382 buffer->shm_buffer = shm_buffer;
1383 buffer->width = wl_shm_buffer_get_width(shm_buffer);
1384 buffer->height = wl_shm_buffer_get_height(shm_buffer);
1385
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001386 num_planes = 1;
1387 gs->offset[0] = 0;
Vincent ABRIOUe5732c72016-10-20 16:20:11 +02001388 gs->hsub[0] = 1;
1389 gs->vsub[0] = 1;
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001390
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001391 switch (wl_shm_buffer_get_format(shm_buffer)) {
1392 case WL_SHM_FORMAT_XRGB8888:
1393 gs->shader = &gr->texture_shader_rgbx;
1394 pitch = wl_shm_buffer_get_stride(shm_buffer) / 4;
Vincent Abriou00a03d22016-10-05 14:54:35 +02001395 gl_format[0] = GL_BGRA_EXT;
Neil Roberts4d085e72014-04-07 15:01:01 +01001396 gl_pixel_type = GL_UNSIGNED_BYTE;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001397 break;
1398 case WL_SHM_FORMAT_ARGB8888:
1399 gs->shader = &gr->texture_shader_rgba;
1400 pitch = wl_shm_buffer_get_stride(shm_buffer) / 4;
Vincent Abriou00a03d22016-10-05 14:54:35 +02001401 gl_format[0] = GL_BGRA_EXT;
Neil Roberts4d085e72014-04-07 15:01:01 +01001402 gl_pixel_type = GL_UNSIGNED_BYTE;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001403 break;
1404 case WL_SHM_FORMAT_RGB565:
1405 gs->shader = &gr->texture_shader_rgbx;
1406 pitch = wl_shm_buffer_get_stride(shm_buffer) / 2;
Vincent Abriou00a03d22016-10-05 14:54:35 +02001407 gl_format[0] = GL_RGB;
Neil Roberts4d085e72014-04-07 15:01:01 +01001408 gl_pixel_type = GL_UNSIGNED_SHORT_5_6_5;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001409 break;
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001410 case WL_SHM_FORMAT_YUV420:
1411 gs->shader = &gr->texture_shader_y_u_v;
1412 pitch = wl_shm_buffer_get_stride(shm_buffer);
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001413 gl_pixel_type = GL_UNSIGNED_BYTE;
1414 num_planes = 3;
Vincent ABRIOUe5732c72016-10-20 16:20:11 +02001415 gs->offset[1] = gs->offset[0] + (pitch / gs->hsub[0]) *
1416 (buffer->height / gs->vsub[0]);
1417 gs->hsub[1] = 2;
1418 gs->vsub[1] = 2;
1419 gs->offset[2] = gs->offset[1] + (pitch / gs->hsub[1]) *
1420 (buffer->height / gs->vsub[1]);
1421 gs->hsub[2] = 2;
1422 gs->vsub[2] = 2;
Vincent Abriou00a03d22016-10-05 14:54:35 +02001423 if (gr->has_gl_texture_rg) {
1424 gl_format[0] = GL_R8_EXT;
1425 gl_format[1] = GL_R8_EXT;
1426 gl_format[2] = GL_R8_EXT;
1427 } else {
1428 gl_format[0] = GL_LUMINANCE;
1429 gl_format[1] = GL_LUMINANCE;
1430 gl_format[2] = GL_LUMINANCE;
1431 }
1432 break;
1433 case WL_SHM_FORMAT_NV12:
1434 gs->shader = &gr->texture_shader_y_xuxv;
1435 pitch = wl_shm_buffer_get_stride(shm_buffer);
1436 gl_pixel_type = GL_UNSIGNED_BYTE;
1437 num_planes = 2;
Vincent ABRIOUe5732c72016-10-20 16:20:11 +02001438 gs->offset[1] = gs->offset[0] + (pitch / gs->hsub[0]) *
1439 (buffer->height / gs->vsub[0]);
1440 gs->hsub[1] = 2;
1441 gs->vsub[1] = 2;
Vincent Abriou00a03d22016-10-05 14:54:35 +02001442 if (gr->has_gl_texture_rg) {
1443 gl_format[0] = GL_R8_EXT;
1444 gl_format[1] = GL_RG8_EXT;
1445 } else {
1446 gl_format[0] = GL_LUMINANCE;
1447 gl_format[1] = GL_LUMINANCE_ALPHA;
1448 }
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001449 break;
Vincent ABRIOUe5732c72016-10-20 16:20:11 +02001450 case WL_SHM_FORMAT_YUYV:
1451 gs->shader = &gr->texture_shader_y_xuxv;
1452 pitch = wl_shm_buffer_get_stride(shm_buffer) / 2;
1453 gl_pixel_type = GL_UNSIGNED_BYTE;
1454 num_planes = 2;
1455 gs->hsub[1] = 2;
1456 gs->vsub[1] = 1;
1457 if (gr->has_gl_texture_rg)
1458 gl_format[0] = GL_RG8_EXT;
1459 else
1460 gl_format[0] = GL_LUMINANCE_ALPHA;
1461 gl_format[1] = GL_BGRA_EXT;
1462 break;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001463 default:
Neil Roberts4d085e72014-04-07 15:01:01 +01001464 weston_log("warning: unknown shm buffer format: %08x\n",
1465 wl_shm_buffer_get_format(shm_buffer));
1466 return;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001467 }
1468
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001469 /* Only allocate a texture if it doesn't match existing one.
1470 * If a switch from DRM allocated buffer to a SHM buffer is
1471 * happening, we need to allocate a new texture buffer. */
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001472 if (pitch != gs->pitch ||
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001473 buffer->height != gs->height ||
Vincent Abriou00a03d22016-10-05 14:54:35 +02001474 gl_format[0] != gs->gl_format[0] ||
1475 gl_format[1] != gs->gl_format[1] ||
1476 gl_format[2] != gs->gl_format[2] ||
Neil Roberts4d085e72014-04-07 15:01:01 +01001477 gl_pixel_type != gs->gl_pixel_type ||
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001478 gs->buffer_type != BUFFER_TYPE_SHM) {
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001479 gs->pitch = pitch;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001480 gs->height = buffer->height;
1481 gs->target = GL_TEXTURE_2D;
Vincent Abriou00a03d22016-10-05 14:54:35 +02001482 gs->gl_format[0] = gl_format[0];
1483 gs->gl_format[1] = gl_format[1];
1484 gs->gl_format[2] = gl_format[2];
Neil Roberts4d085e72014-04-07 15:01:01 +01001485 gs->gl_pixel_type = gl_pixel_type;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001486 gs->buffer_type = BUFFER_TYPE_SHM;
Derek Foreman4c11fe72015-11-18 16:32:27 -06001487 gs->needs_full_upload = true;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001488 gs->y_inverted = 1;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001489
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03001490 gs->surface = es;
1491
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001492 ensure_textures(gs, num_planes);
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001493 }
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001494}
1495
1496static void
1497gl_renderer_attach_egl(struct weston_surface *es, struct weston_buffer *buffer,
1498 uint32_t format)
1499{
1500 struct weston_compositor *ec = es->compositor;
1501 struct gl_renderer *gr = get_renderer(ec);
1502 struct gl_surface_state *gs = get_surface_state(es);
1503 EGLint attribs[3];
1504 int i, num_planes;
1505
1506 buffer->legacy_buffer = (struct wl_buffer *)buffer->resource;
1507 gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1508 EGL_WIDTH, &buffer->width);
1509 gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1510 EGL_HEIGHT, &buffer->height);
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001511 gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1512 EGL_WAYLAND_Y_INVERTED_WL, &buffer->y_inverted);
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001513
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +03001514 for (i = 0; i < gs->num_images; i++) {
1515 egl_image_unref(gs->images[i]);
1516 gs->images[i] = NULL;
1517 }
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001518 gs->num_images = 0;
1519 gs->target = GL_TEXTURE_2D;
1520 switch (format) {
1521 case EGL_TEXTURE_RGB:
1522 case EGL_TEXTURE_RGBA:
1523 default:
1524 num_planes = 1;
1525 gs->shader = &gr->texture_shader_rgba;
1526 break;
1527 case EGL_TEXTURE_EXTERNAL_WL:
1528 num_planes = 1;
1529 gs->target = GL_TEXTURE_EXTERNAL_OES;
1530 gs->shader = &gr->texture_shader_egl_external;
1531 break;
1532 case EGL_TEXTURE_Y_UV_WL:
1533 num_planes = 2;
1534 gs->shader = &gr->texture_shader_y_uv;
1535 break;
1536 case EGL_TEXTURE_Y_U_V_WL:
1537 num_planes = 3;
1538 gs->shader = &gr->texture_shader_y_u_v;
1539 break;
1540 case EGL_TEXTURE_Y_XUXV_WL:
1541 num_planes = 2;
1542 gs->shader = &gr->texture_shader_y_xuxv;
1543 break;
1544 }
1545
1546 ensure_textures(gs, num_planes);
1547 for (i = 0; i < num_planes; i++) {
1548 attribs[0] = EGL_WAYLAND_PLANE_WL;
1549 attribs[1] = i;
1550 attribs[2] = EGL_NONE;
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +03001551 gs->images[i] = egl_image_create(gr,
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001552 EGL_WAYLAND_BUFFER_WL,
1553 buffer->legacy_buffer,
1554 attribs);
1555 if (!gs->images[i]) {
1556 weston_log("failed to create img for plane %d\n", i);
1557 continue;
1558 }
1559 gs->num_images++;
1560
1561 glActiveTexture(GL_TEXTURE0 + i);
1562 glBindTexture(gs->target, gs->textures[i]);
1563 gr->image_target_texture_2d(gs->target,
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +03001564 gs->images[i]->image);
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001565 }
1566
1567 gs->pitch = buffer->width;
1568 gs->height = buffer->height;
1569 gs->buffer_type = BUFFER_TYPE_EGL;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001570 gs->y_inverted = buffer->y_inverted;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001571}
1572
1573static void
Pekka Paalanena3525802014-06-12 16:49:29 +03001574gl_renderer_destroy_dmabuf(struct linux_dmabuf_buffer *dmabuf)
1575{
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001576 struct dmabuf_image *image = dmabuf->user_data;
Pekka Paalanena3525802014-06-12 16:49:29 +03001577
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001578 dmabuf_image_destroy(image);
Pekka Paalanena3525802014-06-12 16:49:29 +03001579}
1580
1581static struct egl_image *
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001582import_simple_dmabuf(struct gl_renderer *gr,
1583 struct dmabuf_attributes *attributes)
Pekka Paalanena3525802014-06-12 16:49:29 +03001584{
1585 struct egl_image *image;
Varad Gautamc32e05b2016-11-23 14:03:19 +05301586 EGLint attribs[50];
Pekka Paalanena3525802014-06-12 16:49:29 +03001587 int atti = 0;
1588
Pekka Paalanena3525802014-06-12 16:49:29 +03001589 /* This requires the Mesa commit in
1590 * Mesa 10.3 (08264e5dad4df448e7718e782ad9077902089a07) or
1591 * Mesa 10.2.7 (55d28925e6109a4afd61f109e845a8a51bd17652).
1592 * Otherwise Mesa closes the fd behind our back and re-importing
1593 * will fail.
1594 * https://bugs.freedesktop.org/show_bug.cgi?id=76188
1595 */
1596
1597 attribs[atti++] = EGL_WIDTH;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001598 attribs[atti++] = attributes->width;
Pekka Paalanena3525802014-06-12 16:49:29 +03001599 attribs[atti++] = EGL_HEIGHT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001600 attribs[atti++] = attributes->height;
Pekka Paalanena3525802014-06-12 16:49:29 +03001601 attribs[atti++] = EGL_LINUX_DRM_FOURCC_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001602 attribs[atti++] = attributes->format;
Pekka Paalanena3525802014-06-12 16:49:29 +03001603
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001604 if (attributes->n_planes > 0) {
Pekka Paalanena3525802014-06-12 16:49:29 +03001605 attribs[atti++] = EGL_DMA_BUF_PLANE0_FD_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001606 attribs[atti++] = attributes->fd[0];
Pekka Paalanena3525802014-06-12 16:49:29 +03001607 attribs[atti++] = EGL_DMA_BUF_PLANE0_OFFSET_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001608 attribs[atti++] = attributes->offset[0];
Pekka Paalanena3525802014-06-12 16:49:29 +03001609 attribs[atti++] = EGL_DMA_BUF_PLANE0_PITCH_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001610 attribs[atti++] = attributes->stride[0];
Varad Gautamf7da8b32016-11-23 14:03:18 +05301611 if (gr->has_dmabuf_import_modifiers) {
1612 attribs[atti++] = EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT;
1613 attribs[atti++] = attributes->modifier[0] & 0xFFFFFFFF;
1614 attribs[atti++] = EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT;
1615 attribs[atti++] = attributes->modifier[0] >> 32;
1616 }
Pekka Paalanena3525802014-06-12 16:49:29 +03001617 }
1618
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001619 if (attributes->n_planes > 1) {
Pekka Paalanena3525802014-06-12 16:49:29 +03001620 attribs[atti++] = EGL_DMA_BUF_PLANE1_FD_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001621 attribs[atti++] = attributes->fd[1];
Pekka Paalanena3525802014-06-12 16:49:29 +03001622 attribs[atti++] = EGL_DMA_BUF_PLANE1_OFFSET_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001623 attribs[atti++] = attributes->offset[1];
Pekka Paalanena3525802014-06-12 16:49:29 +03001624 attribs[atti++] = EGL_DMA_BUF_PLANE1_PITCH_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001625 attribs[atti++] = attributes->stride[1];
Varad Gautamf7da8b32016-11-23 14:03:18 +05301626 if (gr->has_dmabuf_import_modifiers) {
1627 attribs[atti++] = EGL_DMA_BUF_PLANE1_MODIFIER_LO_EXT;
1628 attribs[atti++] = attributes->modifier[1] & 0xFFFFFFFF;
1629 attribs[atti++] = EGL_DMA_BUF_PLANE1_MODIFIER_HI_EXT;
1630 attribs[atti++] = attributes->modifier[1] >> 32;
1631 }
Pekka Paalanena3525802014-06-12 16:49:29 +03001632 }
1633
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001634 if (attributes->n_planes > 2) {
Pekka Paalanena3525802014-06-12 16:49:29 +03001635 attribs[atti++] = EGL_DMA_BUF_PLANE2_FD_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001636 attribs[atti++] = attributes->fd[2];
Pekka Paalanena3525802014-06-12 16:49:29 +03001637 attribs[atti++] = EGL_DMA_BUF_PLANE2_OFFSET_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001638 attribs[atti++] = attributes->offset[2];
Pekka Paalanena3525802014-06-12 16:49:29 +03001639 attribs[atti++] = EGL_DMA_BUF_PLANE2_PITCH_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001640 attribs[atti++] = attributes->stride[2];
Varad Gautamf7da8b32016-11-23 14:03:18 +05301641 if (gr->has_dmabuf_import_modifiers) {
1642 attribs[atti++] = EGL_DMA_BUF_PLANE2_MODIFIER_LO_EXT;
1643 attribs[atti++] = attributes->modifier[2] & 0xFFFFFFFF;
1644 attribs[atti++] = EGL_DMA_BUF_PLANE2_MODIFIER_HI_EXT;
1645 attribs[atti++] = attributes->modifier[2] >> 32;
1646 }
Pekka Paalanena3525802014-06-12 16:49:29 +03001647 }
1648
Varad Gautamc32e05b2016-11-23 14:03:19 +05301649 if (gr->has_dmabuf_import_modifiers) {
1650 if (attributes->n_planes > 3) {
1651 attribs[atti++] = EGL_DMA_BUF_PLANE3_FD_EXT;
1652 attribs[atti++] = attributes->fd[3];
1653 attribs[atti++] = EGL_DMA_BUF_PLANE3_OFFSET_EXT;
1654 attribs[atti++] = attributes->offset[3];
1655 attribs[atti++] = EGL_DMA_BUF_PLANE3_PITCH_EXT;
1656 attribs[atti++] = attributes->stride[3];
1657 attribs[atti++] = EGL_DMA_BUF_PLANE3_MODIFIER_LO_EXT;
1658 attribs[atti++] = attributes->modifier[3] & 0xFFFFFFFF;
1659 attribs[atti++] = EGL_DMA_BUF_PLANE3_MODIFIER_HI_EXT;
1660 attribs[atti++] = attributes->modifier[3] >> 32;
1661 }
1662 }
1663
Pekka Paalanena3525802014-06-12 16:49:29 +03001664 attribs[atti++] = EGL_NONE;
1665
1666 image = egl_image_create(gr, EGL_LINUX_DMA_BUF_EXT, NULL,
1667 attribs);
1668
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001669 return image;
1670}
Pekka Paalanena3525802014-06-12 16:49:29 +03001671
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001672/* The kernel header drm_fourcc.h defines the DRM formats below. We duplicate
1673 * some of the definitions here so that building Weston won't require
1674 * bleeding-edge kernel headers.
1675 */
1676#ifndef DRM_FORMAT_R8
1677#define DRM_FORMAT_R8 fourcc_code('R', '8', ' ', ' ') /* [7:0] R */
1678#endif
1679
1680#ifndef DRM_FORMAT_GR88
1681#define DRM_FORMAT_GR88 fourcc_code('G', 'R', '8', '8') /* [15:0] G:R 8:8 little endian */
1682#endif
1683
1684struct yuv_format_descriptor yuv_formats[] = {
1685 {
1686 .format = DRM_FORMAT_YUYV,
1687 .input_planes = 1,
1688 .output_planes = 2,
1689 .texture_type = EGL_TEXTURE_Y_XUXV_WL,
1690 {{
1691 .width_divisor = 1,
1692 .height_divisor = 1,
1693 .format = DRM_FORMAT_GR88,
1694 .plane_index = 0
1695 }, {
1696 .width_divisor = 2,
1697 .height_divisor = 1,
1698 .format = DRM_FORMAT_ARGB8888,
1699 .plane_index = 0
1700 }}
1701 }, {
1702 .format = DRM_FORMAT_NV12,
1703 .input_planes = 2,
1704 .output_planes = 2,
1705 .texture_type = EGL_TEXTURE_Y_UV_WL,
1706 {{
1707 .width_divisor = 1,
1708 .height_divisor = 1,
1709 .format = DRM_FORMAT_R8,
1710 .plane_index = 0
1711 }, {
1712 .width_divisor = 2,
1713 .height_divisor = 2,
1714 .format = DRM_FORMAT_GR88,
1715 .plane_index = 1
1716 }}
1717 }, {
1718 .format = DRM_FORMAT_YUV420,
1719 .input_planes = 3,
1720 .output_planes = 3,
1721 .texture_type = EGL_TEXTURE_Y_U_V_WL,
1722 {{
1723 .width_divisor = 1,
1724 .height_divisor = 1,
1725 .format = DRM_FORMAT_R8,
1726 .plane_index = 0
1727 }, {
1728 .width_divisor = 2,
1729 .height_divisor = 2,
1730 .format = DRM_FORMAT_R8,
1731 .plane_index = 1
1732 }, {
1733 .width_divisor = 2,
1734 .height_divisor = 2,
1735 .format = DRM_FORMAT_R8,
1736 .plane_index = 2
1737 }}
Matthias Treydteaca3ffb2016-07-25 12:15:41 +02001738 }, {
1739 .format = DRM_FORMAT_YUV444,
1740 .input_planes = 3,
1741 .output_planes = 3,
1742 .texture_type = EGL_TEXTURE_Y_U_V_WL,
1743 {{
1744 .width_divisor = 1,
1745 .height_divisor = 1,
1746 .format = DRM_FORMAT_R8,
1747 .plane_index = 0
1748 }, {
1749 .width_divisor = 1,
1750 .height_divisor = 1,
1751 .format = DRM_FORMAT_R8,
1752 .plane_index = 1
1753 }, {
1754 .width_divisor = 1,
1755 .height_divisor = 1,
1756 .format = DRM_FORMAT_R8,
1757 .plane_index = 2
1758 }}
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001759 }
1760};
1761
1762static struct egl_image *
1763import_dmabuf_single_plane(struct gl_renderer *gr,
1764 const struct dmabuf_attributes *attributes,
1765 struct yuv_plane_descriptor *descriptor)
1766{
1767 struct dmabuf_attributes plane;
1768 struct egl_image *image;
1769 char fmt[4];
1770
1771 plane.width = attributes->width / descriptor->width_divisor;
1772 plane.height = attributes->height / descriptor->height_divisor;
1773 plane.format = descriptor->format;
1774 plane.n_planes = 1;
1775 plane.fd[0] = attributes->fd[descriptor->plane_index];
1776 plane.offset[0] = attributes->offset[descriptor->plane_index];
1777 plane.stride[0] = attributes->stride[descriptor->plane_index];
1778 plane.modifier[0] = attributes->modifier[descriptor->plane_index];
1779
1780 image = import_simple_dmabuf(gr, &plane);
1781 if (!image) {
1782 weston_log("Failed to import plane %d as %.4s\n",
1783 descriptor->plane_index,
1784 dump_format(descriptor->format, fmt));
1785 return NULL;
1786 }
1787
1788 return image;
1789}
1790
1791static bool
1792import_yuv_dmabuf(struct gl_renderer *gr,
1793 struct dmabuf_image *image)
1794{
1795 unsigned i;
1796 int j;
1797 int ret;
1798 struct yuv_format_descriptor *format = NULL;
1799 struct dmabuf_attributes *attributes = &image->dmabuf->attributes;
1800 char fmt[4];
1801
1802 for (i = 0; i < ARRAY_LENGTH(yuv_formats); ++i) {
1803 if (yuv_formats[i].format == attributes->format) {
1804 format = &yuv_formats[i];
1805 break;
1806 }
1807 }
1808
1809 if (!format) {
1810 weston_log("Error during import, and no known conversion for format "
Derek Foreman12968e32017-06-26 14:42:44 -05001811 "%.4s in the renderer\n",
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001812 dump_format(attributes->format, fmt));
1813 return false;
1814 }
1815
1816 if (attributes->n_planes != format->input_planes) {
Derek Foreman12968e32017-06-26 14:42:44 -05001817 weston_log("%.4s dmabuf must contain %d plane%s (%d provided)\n",
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001818 dump_format(format->format, fmt),
1819 format->input_planes,
1820 (format->input_planes > 1) ? "s" : "",
1821 attributes->n_planes);
1822 return false;
1823 }
1824
1825 for (j = 0; j < format->output_planes; ++j) {
1826 image->images[j] = import_dmabuf_single_plane(gr, attributes,
1827 &format->plane[j]);
1828 if (!image->images[j]) {
1829 while (j) {
1830 ret = egl_image_unref(image->images[--j]);
1831 assert(ret == 0);
1832 }
1833 return false;
1834 }
1835 }
1836
1837 image->num_images = format->output_planes;
1838
1839 switch (format->texture_type) {
1840 case EGL_TEXTURE_Y_XUXV_WL:
1841 image->shader = &gr->texture_shader_y_xuxv;
1842 break;
1843 case EGL_TEXTURE_Y_UV_WL:
1844 image->shader = &gr->texture_shader_y_uv;
1845 break;
1846 case EGL_TEXTURE_Y_U_V_WL:
1847 image->shader = &gr->texture_shader_y_u_v;
1848 break;
1849 default:
1850 assert(false);
1851 }
1852
1853 return true;
1854}
1855
1856static GLenum
1857choose_texture_target(struct dmabuf_attributes *attributes)
1858{
1859 if (attributes->n_planes > 1)
1860 return GL_TEXTURE_EXTERNAL_OES;
1861
1862 switch (attributes->format & ~DRM_FORMAT_BIG_ENDIAN) {
1863 case DRM_FORMAT_YUYV:
1864 case DRM_FORMAT_YVYU:
1865 case DRM_FORMAT_UYVY:
1866 case DRM_FORMAT_VYUY:
1867 case DRM_FORMAT_AYUV:
1868 return GL_TEXTURE_EXTERNAL_OES;
1869 default:
1870 return GL_TEXTURE_2D;
1871 }
1872}
1873
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001874static struct dmabuf_image *
1875import_dmabuf(struct gl_renderer *gr,
1876 struct linux_dmabuf_buffer *dmabuf)
1877{
1878 struct egl_image *egl_image;
1879 struct dmabuf_image *image;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001880
1881 image = dmabuf_image_create();
Pekka Paalanena3525802014-06-12 16:49:29 +03001882 image->dmabuf = dmabuf;
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001883
1884 egl_image = import_simple_dmabuf(gr, &dmabuf->attributes);
1885 if (egl_image) {
1886 image->num_images = 1;
1887 image->images[0] = egl_image;
1888 image->import_type = IMPORT_TYPE_DIRECT;
1889 image->target = choose_texture_target(&dmabuf->attributes);
1890
1891 switch (image->target) {
1892 case GL_TEXTURE_2D:
1893 image->shader = &gr->texture_shader_rgba;
1894 break;
1895 default:
1896 image->shader = &gr->texture_shader_egl_external;
1897 }
1898 } else {
1899 if (!import_yuv_dmabuf(gr, image)) {
1900 dmabuf_image_destroy(image);
1901 return NULL;
1902 }
1903 image->import_type = IMPORT_TYPE_GL_CONVERSION;
1904 image->target = GL_TEXTURE_2D;
1905 }
Pekka Paalanena3525802014-06-12 16:49:29 +03001906
1907 return image;
1908}
1909
1910static bool
Varad Gautam0775cd12016-11-23 14:03:20 +05301911gl_renderer_query_dmabuf_formats(struct weston_compositor *wc,
1912 int **formats, int *num_formats)
1913{
1914 struct gl_renderer *gr = get_renderer(wc);
1915 EGLint num;
1916
1917 assert(gr->has_dmabuf_import);
1918
1919 if (!gr->has_dmabuf_import_modifiers ||
1920 !gr->query_dmabuf_formats(gr->egl_display, 0, NULL, &num)) {
1921 *num_formats = 0;
1922 return false;
1923 }
1924
1925 *formats = calloc(num, sizeof(int));
1926 if (*formats == NULL) {
1927 *num_formats = 0;
1928 return false;
1929 }
1930 if (!gr->query_dmabuf_formats(gr->egl_display, num, *formats,
1931 (EGLint*) &num)) {
1932 *num_formats = 0;
1933 free(*formats);
1934 return false;
1935 }
1936
1937 *num_formats = num;
1938 return true;
1939}
1940
1941static bool
1942gl_renderer_query_dmabuf_modifiers(struct weston_compositor *wc, int format,
1943 uint64_t **modifiers,
1944 int *num_modifiers)
1945{
1946 struct gl_renderer *gr = get_renderer(wc);
1947 int num;
1948
1949 assert(gr->has_dmabuf_import);
1950
1951 if (!gr->has_dmabuf_import_modifiers ||
1952 !gr->query_dmabuf_modifiers(gr->egl_display, format, 0, NULL,
1953 NULL, &num)) {
1954 *num_modifiers = 0;
1955 return false;
1956 }
1957
1958 *modifiers = calloc(num, sizeof(uint64_t));
1959 if (*modifiers == NULL) {
1960 *num_modifiers = 0;
1961 return false;
1962 }
1963 if (!gr->query_dmabuf_modifiers(gr->egl_display, format,
1964 num, *modifiers, NULL, &num)) {
1965 *num_modifiers = 0;
1966 free(*modifiers);
1967 return false;
1968 }
1969
1970 *num_modifiers = num;
1971 return true;
1972}
1973
1974static bool
Pekka Paalanena3525802014-06-12 16:49:29 +03001975gl_renderer_import_dmabuf(struct weston_compositor *ec,
1976 struct linux_dmabuf_buffer *dmabuf)
1977{
1978 struct gl_renderer *gr = get_renderer(ec);
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001979 struct dmabuf_image *image;
Pekka Paalanena3525802014-06-12 16:49:29 +03001980 int i;
1981
1982 assert(gr->has_dmabuf_import);
1983
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001984 for (i = 0; i < dmabuf->attributes.n_planes; i++) {
Varad Gautamf7da8b32016-11-23 14:03:18 +05301985 /* return if EGL doesn't support import modifiers */
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001986 if (dmabuf->attributes.modifier[i] != 0)
Varad Gautamf7da8b32016-11-23 14:03:18 +05301987 if (!gr->has_dmabuf_import_modifiers)
1988 return false;
1989
1990 /* return if modifiers passed are unequal */
1991 if (dmabuf->attributes.modifier[i] !=
1992 dmabuf->attributes.modifier[0])
Pekka Paalanena3525802014-06-12 16:49:29 +03001993 return false;
1994 }
1995
1996 /* reject all flags we do not recognize or handle */
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001997 if (dmabuf->attributes.flags & ~ZWP_LINUX_BUFFER_PARAMS_V1_FLAGS_Y_INVERT)
Pekka Paalanena3525802014-06-12 16:49:29 +03001998 return false;
1999
2000 image = import_dmabuf(gr, dmabuf);
2001 if (!image)
2002 return false;
2003
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00002004 wl_list_insert(&gr->dmabuf_images, &image->link);
2005 linux_dmabuf_buffer_set_user_data(dmabuf, image,
2006 gl_renderer_destroy_dmabuf);
Pekka Paalanena3525802014-06-12 16:49:29 +03002007
2008 return true;
2009}
2010
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00002011static bool
2012import_known_dmabuf(struct gl_renderer *gr,
2013 struct dmabuf_image *image)
Pekka Paalanena3525802014-06-12 16:49:29 +03002014{
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00002015 switch (image->import_type) {
2016 case IMPORT_TYPE_DIRECT:
2017 image->images[0] = import_simple_dmabuf(gr, &image->dmabuf->attributes);
2018 if (!image->images[0])
2019 return false;
2020 break;
Pekka Paalanena3525802014-06-12 16:49:29 +03002021
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00002022 case IMPORT_TYPE_GL_CONVERSION:
2023 if (!import_yuv_dmabuf(gr, image))
2024 return false;
2025 break;
2026
Pekka Paalanena3525802014-06-12 16:49:29 +03002027 default:
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00002028 weston_log("Invalid import type for dmabuf\n");
2029 return false;
Pekka Paalanena3525802014-06-12 16:49:29 +03002030 }
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00002031
2032 return true;
Pekka Paalanena3525802014-06-12 16:49:29 +03002033}
2034
2035static void
2036gl_renderer_attach_dmabuf(struct weston_surface *surface,
2037 struct weston_buffer *buffer,
2038 struct linux_dmabuf_buffer *dmabuf)
2039{
2040 struct gl_renderer *gr = get_renderer(surface->compositor);
2041 struct gl_surface_state *gs = get_surface_state(surface);
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00002042 struct dmabuf_image *image;
Pekka Paalanena3525802014-06-12 16:49:29 +03002043 int i;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00002044 int ret;
Pekka Paalanena3525802014-06-12 16:49:29 +03002045
2046 if (!gr->has_dmabuf_import) {
2047 linux_dmabuf_buffer_send_server_error(dmabuf,
2048 "EGL dmabuf import not supported");
2049 return;
2050 }
2051
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00002052 buffer->width = dmabuf->attributes.width;
2053 buffer->height = dmabuf->attributes.height;
Pekka Paalanen319397e2016-07-04 16:25:16 +03002054
2055 /*
2056 * GL-renderer uses the OpenGL convention of texture coordinates, where
2057 * the origin is at bottom-left. Because dmabuf buffers have the origin
2058 * at top-left, we must invert the Y_INVERT flag to get the image right.
2059 */
Pekka Paalanena3525802014-06-12 16:49:29 +03002060 buffer->y_inverted =
Pekka Paalanen319397e2016-07-04 16:25:16 +03002061 !(dmabuf->attributes.flags & ZWP_LINUX_BUFFER_PARAMS_V1_FLAGS_Y_INVERT);
Pekka Paalanena3525802014-06-12 16:49:29 +03002062
2063 for (i = 0; i < gs->num_images; i++)
2064 egl_image_unref(gs->images[i]);
2065 gs->num_images = 0;
2066
Pekka Paalanena3525802014-06-12 16:49:29 +03002067 /*
2068 * We try to always hold an imported EGLImage from the dmabuf
2069 * to prevent the client from preventing re-imports. But, we also
2070 * need to re-import every time the contents may change because
2071 * GL driver's caching may need flushing.
2072 *
2073 * Here we release the cache reference which has to be final.
2074 */
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00002075 image = linux_dmabuf_buffer_get_user_data(dmabuf);
Pekka Paalanena3525802014-06-12 16:49:29 +03002076
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00002077 /* The dmabuf_image should have been created during the import */
2078 assert(image != NULL);
2079
2080 for (i = 0; i < image->num_images; ++i) {
2081 ret = egl_image_unref(image->images[i]);
Pekka Paalanena3525802014-06-12 16:49:29 +03002082 assert(ret == 0);
2083 }
2084
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00002085 if (!import_known_dmabuf(gr, image)) {
2086 linux_dmabuf_buffer_send_server_error(dmabuf, "EGL dmabuf import failed");
2087 return;
Pekka Paalanena3525802014-06-12 16:49:29 +03002088 }
Pekka Paalanena3525802014-06-12 16:49:29 +03002089
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00002090 gs->num_images = image->num_images;
2091 for (i = 0; i < gs->num_images; ++i)
2092 gs->images[i] = egl_image_ref(image->images[i]);
Pekka Paalanena3525802014-06-12 16:49:29 +03002093
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00002094 gs->target = image->target;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00002095 ensure_textures(gs, gs->num_images);
2096 for (i = 0; i < gs->num_images; ++i) {
2097 glActiveTexture(GL_TEXTURE0 + i);
2098 glBindTexture(gs->target, gs->textures[i]);
2099 gr->image_target_texture_2d(gs->target, gs->images[i]->image);
2100 }
Pekka Paalanena3525802014-06-12 16:49:29 +03002101
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00002102 gs->shader = image->shader;
Pekka Paalanena3525802014-06-12 16:49:29 +03002103 gs->pitch = buffer->width;
2104 gs->height = buffer->height;
2105 gs->buffer_type = BUFFER_TYPE_EGL;
2106 gs->y_inverted = buffer->y_inverted;
2107}
2108
2109static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002110gl_renderer_attach(struct weston_surface *es, struct weston_buffer *buffer)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002111{
2112 struct weston_compositor *ec = es->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002113 struct gl_renderer *gr = get_renderer(ec);
2114 struct gl_surface_state *gs = get_surface_state(es);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002115 struct wl_shm_buffer *shm_buffer;
Pekka Paalanena3525802014-06-12 16:49:29 +03002116 struct linux_dmabuf_buffer *dmabuf;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03002117 EGLint format;
2118 int i;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002119
Pekka Paalanenfb003d32012-12-04 15:58:13 +02002120 weston_buffer_reference(&gs->buffer_ref, buffer);
2121
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002122 if (!buffer) {
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01002123 for (i = 0; i < gs->num_images; i++) {
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +03002124 egl_image_unref(gs->images[i]);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01002125 gs->images[i] = NULL;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002126 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01002127 gs->num_images = 0;
2128 glDeleteTextures(gs->num_textures, gs->textures);
2129 gs->num_textures = 0;
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +03002130 gs->buffer_type = BUFFER_TYPE_NULL;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002131 gs->y_inverted = 1;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002132 return;
2133 }
2134
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002135 shm_buffer = wl_shm_buffer_get(buffer->resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002136
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03002137 if (shm_buffer)
2138 gl_renderer_attach_shm(es, buffer, shm_buffer);
Vincent Abriou9c526e02016-11-03 11:15:32 +01002139 else if (gr->has_bind_display &&
2140 gr->query_buffer(gr->egl_display, (void *)buffer->resource,
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03002141 EGL_TEXTURE_FORMAT, &format))
2142 gl_renderer_attach_egl(es, buffer, format);
Pekka Paalanena3525802014-06-12 16:49:29 +03002143 else if ((dmabuf = linux_dmabuf_buffer_get(buffer->resource)))
2144 gl_renderer_attach_dmabuf(es, buffer, dmabuf);
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03002145 else {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002146 weston_log("unhandled buffer type!\n");
Pekka Paalanenfb003d32012-12-04 15:58:13 +02002147 weston_buffer_reference(&gs->buffer_ref, NULL);
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +03002148 gs->buffer_type = BUFFER_TYPE_NULL;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002149 gs->y_inverted = 1;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002150 }
2151}
2152
Kristian Høgsberg42263852012-09-06 21:59:29 -04002153static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002154gl_renderer_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002155 float red, float green, float blue, float alpha)
2156{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002157 struct gl_surface_state *gs = get_surface_state(surface);
2158 struct gl_renderer *gr = get_renderer(surface->compositor);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002159
2160 gs->color[0] = red;
2161 gs->color[1] = green;
2162 gs->color[2] = blue;
2163 gs->color[3] = alpha;
Pekka Paalanenaeb917e2015-02-09 13:56:56 +02002164 gs->buffer_type = BUFFER_TYPE_SOLID;
2165 gs->pitch = 1;
2166 gs->height = 1;
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002167
John Kåre Alsaker40684142012-11-13 19:10:25 +01002168 gs->shader = &gr->solid_shader;
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002169}
2170
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002171static void
Pekka Paalaneneb35cbe2015-02-09 13:37:27 +02002172gl_renderer_surface_get_content_size(struct weston_surface *surface,
2173 int *width, int *height)
2174{
2175 struct gl_surface_state *gs = get_surface_state(surface);
2176
2177 if (gs->buffer_type == BUFFER_TYPE_NULL) {
2178 *width = 0;
2179 *height = 0;
2180 } else {
2181 *width = gs->pitch;
2182 *height = gs->height;
2183 }
2184}
2185
2186static uint32_t
2187pack_color(pixman_format_code_t format, float *c)
2188{
2189 uint8_t r = round(c[0] * 255.0f);
2190 uint8_t g = round(c[1] * 255.0f);
2191 uint8_t b = round(c[2] * 255.0f);
2192 uint8_t a = round(c[3] * 255.0f);
2193
2194 switch (format) {
2195 case PIXMAN_a8b8g8r8:
2196 return (a << 24) | (b << 16) | (g << 8) | r;
2197 default:
2198 assert(0);
2199 return 0;
2200 }
2201}
2202
2203static int
2204gl_renderer_surface_copy_content(struct weston_surface *surface,
2205 void *target, size_t size,
2206 int src_x, int src_y,
2207 int width, int height)
2208{
2209 static const GLfloat verts[4 * 2] = {
2210 0.0f, 0.0f,
2211 1.0f, 0.0f,
2212 1.0f, 1.0f,
2213 0.0f, 1.0f
2214 };
2215 static const GLfloat projmat_normal[16] = { /* transpose */
2216 2.0f, 0.0f, 0.0f, 0.0f,
2217 0.0f, 2.0f, 0.0f, 0.0f,
2218 0.0f, 0.0f, 1.0f, 0.0f,
2219 -1.0f, -1.0f, 0.0f, 1.0f
2220 };
2221 static const GLfloat projmat_yinvert[16] = { /* transpose */
2222 2.0f, 0.0f, 0.0f, 0.0f,
2223 0.0f, -2.0f, 0.0f, 0.0f,
2224 0.0f, 0.0f, 1.0f, 0.0f,
2225 -1.0f, 1.0f, 0.0f, 1.0f
2226 };
2227 const pixman_format_code_t format = PIXMAN_a8b8g8r8;
2228 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
2229 const GLenum gl_format = GL_RGBA; /* PIXMAN_a8b8g8r8 little-endian */
2230 struct gl_renderer *gr = get_renderer(surface->compositor);
2231 struct gl_surface_state *gs = get_surface_state(surface);
2232 int cw, ch;
2233 GLuint fbo;
2234 GLuint tex;
2235 GLenum status;
2236 const GLfloat *proj;
2237 int i;
2238
2239 gl_renderer_surface_get_content_size(surface, &cw, &ch);
2240
2241 switch (gs->buffer_type) {
2242 case BUFFER_TYPE_NULL:
2243 return -1;
2244 case BUFFER_TYPE_SOLID:
2245 *(uint32_t *)target = pack_color(format, gs->color);
2246 return 0;
2247 case BUFFER_TYPE_SHM:
2248 gl_renderer_flush_damage(surface);
2249 /* fall through */
2250 case BUFFER_TYPE_EGL:
2251 break;
2252 }
2253
2254 glGenTextures(1, &tex);
2255 glBindTexture(GL_TEXTURE_2D, tex);
2256 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, cw, ch,
2257 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
2258 glBindTexture(GL_TEXTURE_2D, 0);
2259
2260 glGenFramebuffers(1, &fbo);
2261 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
2262 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
2263 GL_TEXTURE_2D, tex, 0);
2264
2265 status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
2266 if (status != GL_FRAMEBUFFER_COMPLETE) {
2267 weston_log("%s: fbo error: %#x\n", __func__, status);
2268 glDeleteFramebuffers(1, &fbo);
2269 glDeleteTextures(1, &tex);
2270 return -1;
2271 }
2272
2273 glViewport(0, 0, cw, ch);
2274 glDisable(GL_BLEND);
2275 use_shader(gr, gs->shader);
2276 if (gs->y_inverted)
2277 proj = projmat_normal;
2278 else
2279 proj = projmat_yinvert;
2280
2281 glUniformMatrix4fv(gs->shader->proj_uniform, 1, GL_FALSE, proj);
2282 glUniform1f(gs->shader->alpha_uniform, 1.0f);
2283
2284 for (i = 0; i < gs->num_textures; i++) {
2285 glUniform1i(gs->shader->tex_uniforms[i], i);
2286
2287 glActiveTexture(GL_TEXTURE0 + i);
2288 glBindTexture(gs->target, gs->textures[i]);
2289 glTexParameteri(gs->target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2290 glTexParameteri(gs->target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2291 }
2292
2293 /* position: */
2294 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, verts);
2295 glEnableVertexAttribArray(0);
2296
2297 /* texcoord: */
2298 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, verts);
2299 glEnableVertexAttribArray(1);
2300
2301 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2302
2303 glDisableVertexAttribArray(1);
2304 glDisableVertexAttribArray(0);
2305
2306 glPixelStorei(GL_PACK_ALIGNMENT, bytespp);
2307 glReadPixels(src_x, src_y, width, height, gl_format,
2308 GL_UNSIGNED_BYTE, target);
2309
2310 glDeleteFramebuffers(1, &fbo);
2311 glDeleteTextures(1, &tex);
2312
2313 return 0;
2314}
2315
2316static void
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002317surface_state_destroy(struct gl_surface_state *gs, struct gl_renderer *gr)
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002318{
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002319 int i;
2320
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002321 wl_list_remove(&gs->surface_destroy_listener.link);
2322 wl_list_remove(&gs->renderer_destroy_listener.link);
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002323
2324 gs->surface->renderer_state = NULL;
2325
2326 glDeleteTextures(gs->num_textures, gs->textures);
2327
2328 for (i = 0; i < gs->num_images; i++)
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +03002329 egl_image_unref(gs->images[i]);
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002330
2331 weston_buffer_reference(&gs->buffer_ref, NULL);
2332 pixman_region32_fini(&gs->texture_damage);
2333 free(gs);
2334}
2335
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002336static void
2337surface_state_handle_surface_destroy(struct wl_listener *listener, void *data)
2338{
2339 struct gl_surface_state *gs;
2340 struct gl_renderer *gr;
2341
2342 gs = container_of(listener, struct gl_surface_state,
2343 surface_destroy_listener);
2344
2345 gr = get_renderer(gs->surface->compositor);
2346
2347 surface_state_destroy(gs, gr);
2348}
2349
2350static void
2351surface_state_handle_renderer_destroy(struct wl_listener *listener, void *data)
2352{
2353 struct gl_surface_state *gs;
2354 struct gl_renderer *gr;
2355
2356 gr = data;
2357
2358 gs = container_of(listener, struct gl_surface_state,
2359 renderer_destroy_listener);
2360
2361 surface_state_destroy(gs, gr);
2362}
2363
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002364static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002365gl_renderer_create_surface(struct weston_surface *surface)
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002366{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002367 struct gl_surface_state *gs;
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002368 struct gl_renderer *gr = get_renderer(surface->compositor);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002369
Bryce Harringtonde16d892014-11-20 22:21:57 -08002370 gs = zalloc(sizeof *gs);
2371 if (gs == NULL)
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002372 return -1;
2373
Pekka Paalanen68033ac2012-12-04 15:58:15 +02002374 /* A buffer is never attached to solid color surfaces, yet
2375 * they still go through texcoord computations. Do not divide
2376 * by zero there.
2377 */
2378 gs->pitch = 1;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002379 gs->y_inverted = 1;
Pekka Paalanen68033ac2012-12-04 15:58:15 +02002380
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002381 gs->surface = surface;
2382
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02002383 pixman_region32_init(&gs->texture_damage);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002384 surface->renderer_state = gs;
2385
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002386 gs->surface_destroy_listener.notify =
2387 surface_state_handle_surface_destroy;
2388 wl_signal_add(&surface->destroy_signal,
2389 &gs->surface_destroy_listener);
2390
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002391 gs->renderer_destroy_listener.notify =
2392 surface_state_handle_renderer_destroy;
2393 wl_signal_add(&gr->destroy_signal,
2394 &gs->renderer_destroy_listener);
2395
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02002396 if (surface->buffer_ref.buffer) {
2397 gl_renderer_attach(surface, surface->buffer_ref.buffer);
2398 gl_renderer_flush_damage(surface);
2399 }
2400
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002401 return 0;
2402}
2403
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002404static const char vertex_shader[] =
2405 "uniform mat4 proj;\n"
2406 "attribute vec2 position;\n"
2407 "attribute vec2 texcoord;\n"
2408 "varying vec2 v_texcoord;\n"
2409 "void main()\n"
2410 "{\n"
2411 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
2412 " v_texcoord = texcoord;\n"
2413 "}\n";
2414
2415/* Declare common fragment shader uniforms */
2416#define FRAGMENT_CONVERT_YUV \
2417 " y *= alpha;\n" \
2418 " u *= alpha;\n" \
2419 " v *= alpha;\n" \
2420 " gl_FragColor.r = y + 1.59602678 * v;\n" \
2421 " gl_FragColor.g = y - 0.39176229 * u - 0.81296764 * v;\n" \
2422 " gl_FragColor.b = y + 2.01723214 * u;\n" \
2423 " gl_FragColor.a = alpha;\n"
2424
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002425static const char fragment_debug[] =
2426 " gl_FragColor = vec4(0.0, 0.3, 0.0, 0.2) + gl_FragColor * 0.8;\n";
2427
2428static const char fragment_brace[] =
2429 "}\n";
2430
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002431static const char texture_fragment_shader_rgba[] =
2432 "precision mediump float;\n"
2433 "varying vec2 v_texcoord;\n"
2434 "uniform sampler2D tex;\n"
2435 "uniform float alpha;\n"
2436 "void main()\n"
2437 "{\n"
2438 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002439 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002440
2441static const char texture_fragment_shader_rgbx[] =
2442 "precision mediump float;\n"
2443 "varying vec2 v_texcoord;\n"
2444 "uniform sampler2D tex;\n"
2445 "uniform float alpha;\n"
2446 "void main()\n"
2447 "{\n"
2448 " gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb\n;"
2449 " gl_FragColor.a = alpha;\n"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002450 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002451
2452static const char texture_fragment_shader_egl_external[] =
2453 "#extension GL_OES_EGL_image_external : require\n"
2454 "precision mediump float;\n"
2455 "varying vec2 v_texcoord;\n"
2456 "uniform samplerExternalOES tex;\n"
2457 "uniform float alpha;\n"
2458 "void main()\n"
2459 "{\n"
2460 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002461 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002462
2463static const char texture_fragment_shader_y_uv[] =
2464 "precision mediump float;\n"
2465 "uniform sampler2D tex;\n"
2466 "uniform sampler2D tex1;\n"
2467 "varying vec2 v_texcoord;\n"
2468 "uniform float alpha;\n"
2469 "void main() {\n"
2470 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
2471 " float u = texture2D(tex1, v_texcoord).r - 0.5;\n"
2472 " float v = texture2D(tex1, v_texcoord).g - 0.5;\n"
2473 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002474 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002475
2476static const char texture_fragment_shader_y_u_v[] =
2477 "precision mediump float;\n"
2478 "uniform sampler2D tex;\n"
2479 "uniform sampler2D tex1;\n"
2480 "uniform sampler2D tex2;\n"
2481 "varying vec2 v_texcoord;\n"
2482 "uniform float alpha;\n"
2483 "void main() {\n"
2484 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
2485 " float u = texture2D(tex1, v_texcoord).x - 0.5;\n"
2486 " float v = texture2D(tex2, v_texcoord).x - 0.5;\n"
2487 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002488 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002489
2490static const char texture_fragment_shader_y_xuxv[] =
2491 "precision mediump float;\n"
2492 "uniform sampler2D tex;\n"
2493 "uniform sampler2D tex1;\n"
2494 "varying vec2 v_texcoord;\n"
2495 "uniform float alpha;\n"
2496 "void main() {\n"
2497 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
2498 " float u = texture2D(tex1, v_texcoord).g - 0.5;\n"
2499 " float v = texture2D(tex1, v_texcoord).a - 0.5;\n"
2500 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002501 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002502
2503static const char solid_fragment_shader[] =
2504 "precision mediump float;\n"
2505 "uniform vec4 color;\n"
2506 "uniform float alpha;\n"
2507 "void main()\n"
2508 "{\n"
2509 " gl_FragColor = alpha * color\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002510 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002511
2512static int
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002513compile_shader(GLenum type, int count, const char **sources)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002514{
2515 GLuint s;
2516 char msg[512];
2517 GLint status;
2518
2519 s = glCreateShader(type);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002520 glShaderSource(s, count, sources, NULL);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002521 glCompileShader(s);
2522 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
2523 if (!status) {
2524 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
2525 weston_log("shader info: %s\n", msg);
2526 return GL_NONE;
2527 }
2528
2529 return s;
2530}
2531
2532static int
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03002533shader_init(struct gl_shader *shader, struct gl_renderer *renderer,
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002534 const char *vertex_source, const char *fragment_source)
2535{
2536 char msg[512];
2537 GLint status;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002538 int count;
2539 const char *sources[3];
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002540
2541 shader->vertex_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002542 compile_shader(GL_VERTEX_SHADER, 1, &vertex_source);
2543
2544 if (renderer->fragment_shader_debug) {
2545 sources[0] = fragment_source;
2546 sources[1] = fragment_debug;
2547 sources[2] = fragment_brace;
2548 count = 3;
2549 } else {
2550 sources[0] = fragment_source;
2551 sources[1] = fragment_brace;
2552 count = 2;
2553 }
2554
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002555 shader->fragment_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002556 compile_shader(GL_FRAGMENT_SHADER, count, sources);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002557
2558 shader->program = glCreateProgram();
2559 glAttachShader(shader->program, shader->vertex_shader);
2560 glAttachShader(shader->program, shader->fragment_shader);
2561 glBindAttribLocation(shader->program, 0, "position");
2562 glBindAttribLocation(shader->program, 1, "texcoord");
2563
2564 glLinkProgram(shader->program);
2565 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
2566 if (!status) {
2567 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
2568 weston_log("link info: %s\n", msg);
2569 return -1;
2570 }
2571
2572 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
2573 shader->tex_uniforms[0] = glGetUniformLocation(shader->program, "tex");
2574 shader->tex_uniforms[1] = glGetUniformLocation(shader->program, "tex1");
2575 shader->tex_uniforms[2] = glGetUniformLocation(shader->program, "tex2");
2576 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
2577 shader->color_uniform = glGetUniformLocation(shader->program, "color");
2578
2579 return 0;
2580}
2581
2582static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002583shader_release(struct gl_shader *shader)
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002584{
2585 glDeleteShader(shader->vertex_shader);
2586 glDeleteShader(shader->fragment_shader);
2587 glDeleteProgram(shader->program);
2588
2589 shader->vertex_shader = 0;
2590 shader->fragment_shader = 0;
2591 shader->program = 0;
2592}
2593
2594static void
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002595log_extensions(const char *name, const char *extensions)
2596{
2597 const char *p, *end;
2598 int l;
2599 int len;
2600
2601 l = weston_log("%s:", name);
2602 p = extensions;
2603 while (*p) {
2604 end = strchrnul(p, ' ');
2605 len = end - p;
2606 if (l + len > 78)
2607 l = weston_log_continue("\n" STAMP_SPACE "%.*s",
2608 len, p);
2609 else
2610 l += weston_log_continue(" %.*s", len, p);
2611 for (p = end; isspace(*p); p++)
2612 ;
2613 }
2614 weston_log_continue("\n");
2615}
2616
2617static void
2618log_egl_gl_info(EGLDisplay egldpy)
2619{
2620 const char *str;
2621
2622 str = eglQueryString(egldpy, EGL_VERSION);
2623 weston_log("EGL version: %s\n", str ? str : "(null)");
2624
2625 str = eglQueryString(egldpy, EGL_VENDOR);
2626 weston_log("EGL vendor: %s\n", str ? str : "(null)");
2627
2628 str = eglQueryString(egldpy, EGL_CLIENT_APIS);
2629 weston_log("EGL client APIs: %s\n", str ? str : "(null)");
2630
2631 str = eglQueryString(egldpy, EGL_EXTENSIONS);
2632 log_extensions("EGL extensions", str ? str : "(null)");
2633
2634 str = (char *)glGetString(GL_VERSION);
2635 weston_log("GL version: %s\n", str ? str : "(null)");
2636
2637 str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
2638 weston_log("GLSL version: %s\n", str ? str : "(null)");
2639
2640 str = (char *)glGetString(GL_VENDOR);
2641 weston_log("GL vendor: %s\n", str ? str : "(null)");
2642
2643 str = (char *)glGetString(GL_RENDERER);
2644 weston_log("GL renderer: %s\n", str ? str : "(null)");
2645
2646 str = (char *)glGetString(GL_EXTENSIONS);
2647 log_extensions("GL extensions", str ? str : "(null)");
2648}
2649
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03002650static void
2651log_egl_config_info(EGLDisplay egldpy, EGLConfig eglconfig)
2652{
2653 EGLint r, g, b, a;
2654
2655 weston_log("Chosen EGL config details:\n");
2656
2657 weston_log_continue(STAMP_SPACE "RGBA bits");
2658 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_RED_SIZE, &r) &&
2659 eglGetConfigAttrib(egldpy, eglconfig, EGL_GREEN_SIZE, &g) &&
2660 eglGetConfigAttrib(egldpy, eglconfig, EGL_BLUE_SIZE, &b) &&
2661 eglGetConfigAttrib(egldpy, eglconfig, EGL_ALPHA_SIZE, &a))
2662 weston_log_continue(": %d %d %d %d\n", r, g, b, a);
2663 else
2664 weston_log_continue(" unknown\n");
2665
2666 weston_log_continue(STAMP_SPACE "swap interval range");
2667 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_MIN_SWAP_INTERVAL, &a) &&
2668 eglGetConfigAttrib(egldpy, eglconfig, EGL_MAX_SWAP_INTERVAL, &b))
2669 weston_log_continue(": %d - %d\n", a, b);
2670 else
2671 weston_log_continue(" unknown\n");
2672}
2673
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002674static int
Derek Foremane76f1852015-05-15 12:12:39 -05002675match_config_to_visual(EGLDisplay egl_display,
2676 EGLint visual_id,
2677 EGLConfig *configs,
2678 int count)
2679{
2680 int i;
2681
2682 for (i = 0; i < count; ++i) {
2683 EGLint id;
2684
2685 if (!eglGetConfigAttrib(egl_display,
2686 configs[i], EGL_NATIVE_VISUAL_ID,
2687 &id))
2688 continue;
2689
2690 if (id == visual_id)
2691 return i;
2692 }
2693
Derek Foremane76f1852015-05-15 12:12:39 -05002694 return -1;
2695}
2696
2697static int
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002698egl_choose_config(struct gl_renderer *gr, const EGLint *attribs,
Derek Foremane76f1852015-05-15 12:12:39 -05002699 const EGLint *visual_id, const int n_ids,
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002700 EGLConfig *config_out)
2701{
2702 EGLint count = 0;
2703 EGLint matched = 0;
2704 EGLConfig *configs;
Derek Foremane76f1852015-05-15 12:12:39 -05002705 int i, config_index = -1;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002706
Derek Foremana7e19912015-05-20 14:57:58 -05002707 if (!eglGetConfigs(gr->egl_display, NULL, 0, &count) || count < 1) {
2708 weston_log("No EGL configs to choose from.\n");
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002709 return -1;
Derek Foremana7e19912015-05-20 14:57:58 -05002710 }
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002711 configs = calloc(count, sizeof *configs);
2712 if (!configs)
2713 return -1;
2714
2715 if (!eglChooseConfig(gr->egl_display, attribs, configs,
Derek Foremana7e19912015-05-20 14:57:58 -05002716 count, &matched) || !matched) {
2717 weston_log("No EGL configs with appropriate attributes.\n");
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002718 goto out;
Derek Foremana7e19912015-05-20 14:57:58 -05002719 }
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002720
Miguel A. Vico684c9f42016-05-18 17:48:47 +02002721 if (!visual_id || n_ids == 0)
Derek Foremane76f1852015-05-15 12:12:39 -05002722 config_index = 0;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002723
Derek Foremane76f1852015-05-15 12:12:39 -05002724 for (i = 0; config_index == -1 && i < n_ids; i++)
2725 config_index = match_config_to_visual(gr->egl_display,
2726 visual_id[i],
2727 configs,
2728 matched);
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002729
Derek Foremane76f1852015-05-15 12:12:39 -05002730 if (config_index != -1)
2731 *config_out = configs[config_index];
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002732
2733out:
2734 free(configs);
Derek Foremane76f1852015-05-15 12:12:39 -05002735 if (config_index == -1)
2736 return -1;
2737
Derek Foremana7e19912015-05-20 14:57:58 -05002738 if (i > 1)
2739 weston_log("Unable to use first choice EGL config with id"
2740 " 0x%x, succeeded with alternate id 0x%x.\n",
2741 visual_id[0], visual_id[i - 1]);
Derek Foremane76f1852015-05-15 12:12:39 -05002742 return 0;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002743}
2744
John Kåre Alsaker44154502012-11-13 19:10:20 +01002745static void
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05002746gl_renderer_output_set_border(struct weston_output *output,
2747 enum gl_renderer_border_side side,
2748 int32_t width, int32_t height,
2749 int32_t tex_width, unsigned char *data)
2750{
2751 struct gl_output_state *go = get_output_state(output);
2752
Jason Ekstrande5512d42014-02-04 21:36:38 -06002753 if (go->borders[side].width != width ||
2754 go->borders[side].height != height)
2755 /* In this case, we have to blow everything and do a full
2756 * repaint. */
2757 go->border_status |= BORDER_SIZE_CHANGED | BORDER_ALL_DIRTY;
2758
2759 if (data == NULL) {
2760 width = 0;
2761 height = 0;
2762 }
2763
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05002764 go->borders[side].width = width;
2765 go->borders[side].height = height;
2766 go->borders[side].tex_width = tex_width;
2767 go->borders[side].data = data;
Jason Ekstrande5512d42014-02-04 21:36:38 -06002768 go->border_status |= 1 << side;
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05002769}
2770
John Kåre Alsaker94659272012-11-13 19:10:18 +01002771static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002772gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01002773
Miguel A. Vico967b6bc2016-05-18 17:50:54 +02002774static EGLSurface
2775gl_renderer_create_window_surface(struct gl_renderer *gr,
2776 EGLNativeWindowType window_for_legacy,
2777 void *window_for_platform,
2778 const EGLint *config_attribs,
2779 const EGLint *visual_id,
2780 int n_ids)
2781{
2782 EGLSurface egl_surface = EGL_NO_SURFACE;
2783 EGLConfig egl_config;
2784
2785 if (egl_choose_config(gr, config_attribs, visual_id,
2786 n_ids, &egl_config) == -1) {
2787 weston_log("failed to choose EGL config for output\n");
2788 return EGL_NO_SURFACE;
2789 }
2790
2791 if (egl_config != gr->egl_config &&
2792 !gr->has_configless_context) {
2793 weston_log("attempted to use a different EGL config for an "
Armin Krezoviće3bfee12016-09-30 14:27:38 +02002794 "output but EGL_KHR_no_config_context or "
2795 "EGL_MESA_configless_context is not supported\n");
Miguel A. Vico967b6bc2016-05-18 17:50:54 +02002796 return EGL_NO_SURFACE;
2797 }
2798
2799 log_egl_config_info(gr->egl_display, egl_config);
2800
2801 if (gr->create_platform_window)
2802 egl_surface = gr->create_platform_window(gr->egl_display,
2803 egl_config,
2804 window_for_platform,
2805 NULL);
2806 else
2807 egl_surface = eglCreateWindowSurface(gr->egl_display,
2808 egl_config,
2809 window_for_legacy, NULL);
2810
2811 return egl_surface;
2812}
2813
2814static int
2815gl_renderer_output_create(struct weston_output *output,
2816 EGLSurface surface)
2817{
2818 struct gl_output_state *go;
2819 int i;
2820
2821 go = zalloc(sizeof *go);
2822 if (go == NULL)
2823 return -1;
2824
2825 go->egl_surface = surface;
2826
2827 for (i = 0; i < BUFFER_DAMAGE_COUNT; i++)
2828 pixman_region32_init(&go->buffer_damage[i]);
2829
2830 output->renderer_state = go;
2831
2832 return 0;
2833}
2834
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002835static int
Miguel A. Vicoc095cde2016-05-18 17:43:00 +02002836gl_renderer_output_window_create(struct weston_output *output,
2837 EGLNativeWindowType window_for_legacy,
2838 void *window_for_platform,
Miguel A. Vico4057cd92016-05-18 17:44:22 +02002839 const EGLint *config_attribs,
Miguel A. Vicoc095cde2016-05-18 17:43:00 +02002840 const EGLint *visual_id,
2841 int n_ids)
John Kåre Alsaker94659272012-11-13 19:10:18 +01002842{
2843 struct weston_compositor *ec = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002844 struct gl_renderer *gr = get_renderer(ec);
Miguel A. Vico967b6bc2016-05-18 17:50:54 +02002845 EGLSurface egl_surface = EGL_NO_SURFACE;
2846 int ret = 0;
John Kåre Alsaker94659272012-11-13 19:10:18 +01002847
Miguel A. Vico967b6bc2016-05-18 17:50:54 +02002848 egl_surface = gl_renderer_create_window_surface(gr,
2849 window_for_legacy,
2850 window_for_platform,
2851 config_attribs,
2852 visual_id, n_ids);
2853 if (egl_surface == EGL_NO_SURFACE) {
John Kåre Alsaker94659272012-11-13 19:10:18 +01002854 weston_log("failed to create egl surface\n");
John Kåre Alsaker94659272012-11-13 19:10:18 +01002855 return -1;
2856 }
2857
Miguel A. Vico967b6bc2016-05-18 17:50:54 +02002858 ret = gl_renderer_output_create(output, egl_surface);
2859 if (ret < 0)
Emil Velikov02639552016-11-14 17:08:17 +00002860 weston_platform_destroy_egl_surface(gr->egl_display, egl_surface);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002861
Miguel A. Vico967b6bc2016-05-18 17:50:54 +02002862 return ret;
John Kåre Alsaker94659272012-11-13 19:10:18 +01002863}
2864
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002865static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002866gl_renderer_output_destroy(struct weston_output *output)
John Kåre Alsaker94659272012-11-13 19:10:18 +01002867{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002868 struct gl_renderer *gr = get_renderer(output->compositor);
2869 struct gl_output_state *go = get_output_state(output);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002870 int i;
2871
2872 for (i = 0; i < 2; i++)
2873 pixman_region32_fini(&go->buffer_damage[i]);
John Kåre Alsaker94659272012-11-13 19:10:18 +01002874
Dima Ryazanov6a38ad72016-11-23 18:41:00 -08002875 eglMakeCurrent(gr->egl_display,
2876 EGL_NO_SURFACE, EGL_NO_SURFACE,
2877 EGL_NO_CONTEXT);
2878
Emil Velikov02639552016-11-14 17:08:17 +00002879 weston_platform_destroy_egl_surface(gr->egl_display, go->egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01002880
2881 free(go);
2882}
2883
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002884static EGLSurface
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002885gl_renderer_output_surface(struct weston_output *output)
John Kåre Alsaker94659272012-11-13 19:10:18 +01002886{
2887 return get_output_state(output)->egl_surface;
2888}
2889
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03002890static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002891gl_renderer_destroy(struct weston_compositor *ec)
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04002892{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002893 struct gl_renderer *gr = get_renderer(ec);
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00002894 struct dmabuf_image *image, *next;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002895
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002896 wl_signal_emit(&gr->destroy_signal, gr);
2897
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002898 if (gr->has_bind_display)
2899 gr->unbind_display(gr->egl_display, ec->wl_display);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002900
2901 /* Work around crash in egl_dri2.c's dri2_make_current() - when does this apply? */
2902 eglMakeCurrent(gr->egl_display,
2903 EGL_NO_SURFACE, EGL_NO_SURFACE,
2904 EGL_NO_CONTEXT);
2905
Pekka Paalanena3525802014-06-12 16:49:29 +03002906
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00002907 wl_list_for_each_safe(image, next, &gr->dmabuf_images, link)
2908 dmabuf_image_destroy(image);
Pekka Paalanena3525802014-06-12 16:49:29 +03002909
Armin Krezović28d240f2016-06-23 11:59:35 +02002910 if (gr->dummy_surface != EGL_NO_SURFACE)
Emil Velikov02639552016-11-14 17:08:17 +00002911 weston_platform_destroy_egl_surface(gr->egl_display,
2912 gr->dummy_surface);
Armin Krezović28d240f2016-06-23 11:59:35 +02002913
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002914 eglTerminate(gr->egl_display);
2915 eglReleaseThread();
Scott Moreau976a0502013-03-07 10:15:17 -07002916
Armin Krezović36d699a2016-08-05 15:28:30 +02002917 wl_list_remove(&gr->output_destroy_listener.link);
2918
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -04002919 wl_array_release(&gr->vertices);
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -04002920 wl_array_release(&gr->vtxcnt);
2921
Mariusz Ceiercbb91582014-02-08 20:11:24 +01002922 if (gr->fragment_binding)
2923 weston_binding_destroy(gr->fragment_binding);
2924 if (gr->fan_binding)
2925 weston_binding_destroy(gr->fan_binding);
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03002926
Scott Moreau976a0502013-03-07 10:15:17 -07002927 free(gr);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002928}
2929
Pekka Paalanen8b69d032015-04-08 17:02:22 +03002930static void
2931renderer_setup_egl_client_extensions(struct gl_renderer *gr)
2932{
2933 const char *extensions;
2934
2935 extensions = eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS);
2936 if (!extensions) {
2937 weston_log("Retrieving EGL client extension string failed.\n");
2938 return;
2939 }
2940
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002941 if (weston_check_egl_extension(extensions, "EGL_EXT_platform_base"))
Pekka Paalanen8b69d032015-04-08 17:02:22 +03002942 gr->create_platform_window =
2943 (void *) eglGetProcAddress("eglCreatePlatformWindowSurfaceEXT");
2944 else
2945 weston_log("warning: EGL_EXT_platform_base not supported.\n");
2946}
2947
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002948static int
Neil Robertsb7f85332014-03-07 18:05:49 +00002949gl_renderer_setup_egl_extensions(struct weston_compositor *ec)
2950{
Emil Velikov43cea542016-11-14 16:03:45 +00002951 static const struct {
2952 char *extension, *entrypoint;
2953 } swap_damage_ext_to_entrypoint[] = {
2954 {
2955 .extension = "EGL_EXT_swap_buffers_with_damage",
2956 .entrypoint = "eglSwapBuffersWithDamageEXT",
2957 },
2958 {
2959 .extension = "EGL_KHR_swap_buffers_with_damage",
2960 .entrypoint = "eglSwapBuffersWithDamageKHR",
2961 },
2962 };
Neil Robertsb7f85332014-03-07 18:05:49 +00002963 struct gl_renderer *gr = get_renderer(ec);
2964 const char *extensions;
2965 EGLBoolean ret;
Bryce Harringtonfe0410b2016-11-21 10:02:42 -08002966 unsigned i;
Neil Robertsb7f85332014-03-07 18:05:49 +00002967
2968 gr->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2969 gr->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
Varad Gautam0775cd12016-11-23 14:03:20 +05302970
Neil Robertsb7f85332014-03-07 18:05:49 +00002971 gr->bind_display =
2972 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
2973 gr->unbind_display =
2974 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
2975 gr->query_buffer =
2976 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
2977
2978 extensions =
2979 (const char *) eglQueryString(gr->egl_display, EGL_EXTENSIONS);
2980 if (!extensions) {
2981 weston_log("Retrieving EGL extension string failed.\n");
2982 return -1;
2983 }
2984
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002985 if (weston_check_egl_extension(extensions, "EGL_WL_bind_wayland_display"))
Neil Robertsb7f85332014-03-07 18:05:49 +00002986 gr->has_bind_display = 1;
2987 if (gr->has_bind_display) {
2988 ret = gr->bind_display(gr->egl_display, ec->wl_display);
2989 if (!ret)
2990 gr->has_bind_display = 0;
2991 }
2992
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002993 if (weston_check_egl_extension(extensions, "EGL_EXT_buffer_age"))
Neil Robertsb7f85332014-03-07 18:05:49 +00002994 gr->has_egl_buffer_age = 1;
2995 else
2996 weston_log("warning: EGL_EXT_buffer_age not supported. "
2997 "Performance could be affected.\n");
2998
Bryce Harringtonfe0410b2016-11-21 10:02:42 -08002999 for (i = 0; i < ARRAY_LENGTH(swap_damage_ext_to_entrypoint); i++) {
Emil Velikov43cea542016-11-14 16:03:45 +00003000 if (weston_check_egl_extension(extensions,
3001 swap_damage_ext_to_entrypoint[i].extension)) {
3002 gr->swap_buffers_with_damage =
3003 (void *) eglGetProcAddress(
3004 swap_damage_ext_to_entrypoint[i].entrypoint);
3005 break;
3006 }
3007 }
3008 if (!gr->swap_buffers_with_damage)
3009 weston_log("warning: neither %s or %s is supported. "
3010 "Performance could be affected.\n",
3011 swap_damage_ext_to_entrypoint[0].extension,
3012 swap_damage_ext_to_entrypoint[1].extension);
Neil Robertsb7f85332014-03-07 18:05:49 +00003013
Armin Krezoviće3bfee12016-09-30 14:27:38 +02003014 if (weston_check_egl_extension(extensions, "EGL_KHR_no_config_context") ||
3015 weston_check_egl_extension(extensions, "EGL_MESA_configless_context"))
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003016 gr->has_configless_context = 1;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003017
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003018 if (weston_check_egl_extension(extensions, "EGL_KHR_surfaceless_context"))
Armin Krezović28d240f2016-06-23 11:59:35 +02003019 gr->has_surfaceless_context = 1;
3020
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003021 if (weston_check_egl_extension(extensions, "EGL_EXT_image_dma_buf_import"))
Pekka Paalanena3525802014-06-12 16:49:29 +03003022 gr->has_dmabuf_import = 1;
Pekka Paalanena3525802014-06-12 16:49:29 +03003023
Varad Gautam0775cd12016-11-23 14:03:20 +05303024 if (weston_check_egl_extension(extensions,
3025 "EGL_EXT_image_dma_buf_import_modifiers")) {
3026 gr->query_dmabuf_formats =
3027 (void *) eglGetProcAddress("eglQueryDmaBufFormatsEXT");
3028 gr->query_dmabuf_modifiers =
3029 (void *) eglGetProcAddress("eglQueryDmaBufModifiersEXT");
3030 gr->has_dmabuf_import_modifiers = 1;
3031 }
3032
Vincent Abrioufdeefe42016-10-05 14:54:34 +02003033 if (weston_check_egl_extension(extensions, "GL_EXT_texture_rg"))
3034 gr->has_gl_texture_rg = 1;
3035
Alexandros Frantzis7192b172017-09-27 15:09:14 +03003036 if (weston_check_egl_extension(extensions, "EGL_KHR_fence_sync") &&
3037 weston_check_egl_extension(extensions, "EGL_ANDROID_native_fence_sync")) {
3038 gr->create_sync =
3039 (void *) eglGetProcAddress("eglCreateSyncKHR");
3040 gr->destroy_sync =
3041 (void *) eglGetProcAddress("eglDestroySyncKHR");
3042 gr->dup_native_fence_fd =
3043 (void *) eglGetProcAddress("eglDupNativeFenceFDANDROID");
3044 gr->has_native_fence_sync = 1;
3045 }
3046
Pekka Paalanen8b69d032015-04-08 17:02:22 +03003047 renderer_setup_egl_client_extensions(gr);
3048
Neil Robertsb7f85332014-03-07 18:05:49 +00003049 return 0;
3050}
3051
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003052static const EGLint gl_renderer_opaque_attribs[] = {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003053 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
3054 EGL_RED_SIZE, 1,
3055 EGL_GREEN_SIZE, 1,
3056 EGL_BLUE_SIZE, 1,
3057 EGL_ALPHA_SIZE, 0,
3058 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
3059 EGL_NONE
3060};
3061
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003062static const EGLint gl_renderer_alpha_attribs[] = {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003063 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
3064 EGL_RED_SIZE, 1,
3065 EGL_GREEN_SIZE, 1,
3066 EGL_BLUE_SIZE, 1,
3067 EGL_ALPHA_SIZE, 1,
3068 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
3069 EGL_NONE
3070};
3071
Armin Krezović28d240f2016-06-23 11:59:35 +02003072
Jonny Lamb70eba3f2015-03-20 15:26:50 +01003073/** Checks whether a platform EGL client extension is supported
3074 *
3075 * \param ec The weston compositor
3076 * \param extension_suffix The EGL client extension suffix
3077 * \return 1 if supported, 0 if using fallbacks, -1 unsupported
3078 *
3079 * This function checks whether a specific platform_* extension is supported
3080 * by EGL.
3081 *
3082 * The extension suffix should be the suffix of the platform extension (that
3083 * specifies a <platform> argument as defined in EGL_EXT_platform_base). For
3084 * example, passing "foo" will check whether either "EGL_KHR_platform_foo",
3085 * "EGL_EXT_platform_foo", or "EGL_MESA_platform_foo" is supported.
3086 *
3087 * The return value is 1:
3088 * - if the supplied EGL client extension is supported.
3089 * The return value is 0:
3090 * - if the platform_base client extension isn't supported so will
3091 * fallback to eglGetDisplay and friends.
3092 * The return value is -1:
3093 * - if the supplied EGL client extension is not supported.
3094 */
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003095static int
Jonny Lamb70eba3f2015-03-20 15:26:50 +01003096gl_renderer_supports(struct weston_compositor *ec,
3097 const char *extension_suffix)
3098{
3099 static const char *extensions = NULL;
3100 char s[64];
3101
3102 if (!extensions) {
3103 extensions = (const char *) eglQueryString(
3104 EGL_NO_DISPLAY, EGL_EXTENSIONS);
3105
3106 if (!extensions)
3107 return 0;
3108
3109 log_extensions("EGL client extensions",
3110 extensions);
3111 }
3112
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003113 if (!weston_check_egl_extension(extensions, "EGL_EXT_platform_base"))
Pekka Paalanenf2824542015-04-08 17:02:21 +03003114 return 0;
3115
Jonny Lamb70eba3f2015-03-20 15:26:50 +01003116 snprintf(s, sizeof s, "EGL_KHR_platform_%s", extension_suffix);
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003117 if (weston_check_egl_extension(extensions, s))
Jonny Lamb70eba3f2015-03-20 15:26:50 +01003118 return 1;
3119
3120 snprintf(s, sizeof s, "EGL_EXT_platform_%s", extension_suffix);
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003121 if (weston_check_egl_extension(extensions, s))
Jonny Lamb70eba3f2015-03-20 15:26:50 +01003122 return 1;
3123
3124 snprintf(s, sizeof s, "EGL_MESA_platform_%s", extension_suffix);
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003125 if (weston_check_egl_extension(extensions, s))
Jonny Lamb70eba3f2015-03-20 15:26:50 +01003126 return 1;
3127
Pekka Paalanenf2824542015-04-08 17:02:21 +03003128 /* at this point we definitely have some platform extensions but
3129 * haven't found the supplied platform, so chances are it's
Jonny Lamb70eba3f2015-03-20 15:26:50 +01003130 * not supported. */
3131
3132 return -1;
3133}
3134
Jonny Lamb74eed312015-03-24 13:12:04 +01003135static const char *
3136platform_to_extension(EGLenum platform)
3137{
3138 switch (platform) {
3139 case EGL_PLATFORM_GBM_KHR:
3140 return "gbm";
3141 case EGL_PLATFORM_WAYLAND_KHR:
3142 return "wayland";
3143 case EGL_PLATFORM_X11_KHR:
3144 return "x11";
3145 default:
3146 assert(0 && "bad EGL platform enum");
3147 }
3148}
3149
Armin Krezović36d699a2016-08-05 15:28:30 +02003150static void
3151output_handle_destroy(struct wl_listener *listener, void *data)
3152{
3153 struct gl_renderer *gr;
3154 struct weston_output *output = data;
3155
3156 gr = container_of(listener, struct gl_renderer,
3157 output_destroy_listener);
3158
3159 if (wl_list_empty(&output->compositor->output_list))
3160 eglMakeCurrent(gr->egl_display, gr->dummy_surface,
3161 gr->dummy_surface, gr->egl_context);
3162}
3163
Jonny Lamb70eba3f2015-03-20 15:26:50 +01003164static int
Armin Krezović28d240f2016-06-23 11:59:35 +02003165gl_renderer_create_pbuffer_surface(struct gl_renderer *gr) {
3166 EGLConfig pbuffer_config;
3167
3168 static const EGLint pbuffer_config_attribs[] = {
3169 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
3170 EGL_RED_SIZE, 1,
3171 EGL_GREEN_SIZE, 1,
3172 EGL_BLUE_SIZE, 1,
3173 EGL_ALPHA_SIZE, 0,
3174 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
3175 EGL_NONE
3176 };
3177
3178 static const EGLint pbuffer_attribs[] = {
3179 EGL_WIDTH, 10,
3180 EGL_HEIGHT, 10,
3181 EGL_NONE
3182 };
3183
3184 if (egl_choose_config(gr, pbuffer_config_attribs, NULL, 0, &pbuffer_config) < 0) {
Derek Foreman12968e32017-06-26 14:42:44 -05003185 weston_log("failed to choose EGL config for PbufferSurface\n");
Armin Krezović28d240f2016-06-23 11:59:35 +02003186 return -1;
3187 }
3188
3189 gr->dummy_surface = eglCreatePbufferSurface(gr->egl_display,
3190 pbuffer_config,
3191 pbuffer_attribs);
3192
3193 if (gr->dummy_surface == EGL_NO_SURFACE) {
3194 weston_log("failed to create PbufferSurface\n");
3195 return -1;
3196 }
3197
3198 return 0;
3199}
3200
3201static int
Miguel A. Vicodddc6702016-05-18 17:41:07 +02003202gl_renderer_display_create(struct weston_compositor *ec, EGLenum platform,
Miguel A. Vico41700e32016-05-18 17:47:59 +02003203 void *native_window, const EGLint *platform_attribs,
3204 const EGLint *config_attribs, const EGLint *visual_id, int n_ids)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003205{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003206 struct gl_renderer *gr;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003207 EGLint major, minor;
Jonny Lamb74eed312015-03-24 13:12:04 +01003208 int supports = 0;
3209
3210 if (platform) {
3211 supports = gl_renderer_supports(
3212 ec, platform_to_extension(platform));
3213 if (supports < 0)
3214 return -1;
3215 }
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003216
Bryce Harringtonde16d892014-11-20 22:21:57 -08003217 gr = zalloc(sizeof *gr);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003218 if (gr == NULL)
3219 return -1;
3220
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003221 gr->base.read_pixels = gl_renderer_read_pixels;
3222 gr->base.repaint_output = gl_renderer_repaint_output;
3223 gr->base.flush_damage = gl_renderer_flush_damage;
3224 gr->base.attach = gl_renderer_attach;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003225 gr->base.surface_set_color = gl_renderer_surface_set_color;
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03003226 gr->base.destroy = gl_renderer_destroy;
Pekka Paalaneneb35cbe2015-02-09 13:37:27 +02003227 gr->base.surface_get_content_size =
3228 gl_renderer_surface_get_content_size;
3229 gr->base.surface_copy_content = gl_renderer_surface_copy_content;
Jonny Lamb74eed312015-03-24 13:12:04 +01003230 gr->egl_display = NULL;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003231
Jonny Lambf1ec5062015-03-24 13:12:05 +01003232 /* extension_suffix is supported */
Jonny Lamb74eed312015-03-24 13:12:04 +01003233 if (supports) {
3234 if (!get_platform_display) {
3235 get_platform_display = (void *) eglGetProcAddress(
3236 "eglGetPlatformDisplayEXT");
3237 }
Jonny Lamb70eba3f2015-03-20 15:26:50 +01003238
Jonny Lamb74eed312015-03-24 13:12:04 +01003239 /* also wrap this in the supports check because
3240 * eglGetProcAddress can return non-NULL and still not
3241 * support the feature at runtime, so ensure the
3242 * appropriate extension checks have been done. */
3243 if (get_platform_display && platform) {
3244 gr->egl_display = get_platform_display(platform,
3245 native_window,
Miguel A. Vico41700e32016-05-18 17:47:59 +02003246 platform_attribs);
Jonny Lamb74eed312015-03-24 13:12:04 +01003247 }
3248 }
Jonny Lamb74eed312015-03-24 13:12:04 +01003249
3250 if (!gr->egl_display) {
Jonny Lamb70eba3f2015-03-20 15:26:50 +01003251 weston_log("warning: either no EGL_EXT_platform_base "
3252 "support or specific platform support; "
3253 "falling back to eglGetDisplay.\n");
3254 gr->egl_display = eglGetDisplay(native_window);
Jonny Lamb70eba3f2015-03-20 15:26:50 +01003255 }
Jonny Lamb70eba3f2015-03-20 15:26:50 +01003256
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003257 if (gr->egl_display == EGL_NO_DISPLAY) {
3258 weston_log("failed to create display\n");
Derek Foreman066ca0c2015-06-11 12:14:45 -05003259 goto fail;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003260 }
3261
3262 if (!eglInitialize(gr->egl_display, &major, &minor)) {
3263 weston_log("failed to initialize display\n");
Derek Foreman066ca0c2015-06-11 12:14:45 -05003264 goto fail_with_error;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003265 }
3266
Miguel A. Vico4057cd92016-05-18 17:44:22 +02003267 if (egl_choose_config(gr, config_attribs, visual_id,
Derek Foremane76f1852015-05-15 12:12:39 -05003268 n_ids, &gr->egl_config) < 0) {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003269 weston_log("failed to choose EGL config\n");
Dawid Gajownik1a912a92015-08-21 00:20:54 -03003270 goto fail_terminate;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003271 }
3272
3273 ec->renderer = &gr->base;
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003274 ec->capabilities |= WESTON_CAP_ROTATION_ANY;
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003275 ec->capabilities |= WESTON_CAP_CAPTURE_YFLIP;
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +02003276 ec->capabilities |= WESTON_CAP_VIEW_CLIP_MASK;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003277
Neil Robertsb7f85332014-03-07 18:05:49 +00003278 if (gl_renderer_setup_egl_extensions(ec) < 0)
Derek Foreman066ca0c2015-06-11 12:14:45 -05003279 goto fail_with_error;
Neil Robertsb7f85332014-03-07 18:05:49 +00003280
Pekka Paalanena3525802014-06-12 16:49:29 +03003281 wl_list_init(&gr->dmabuf_images);
Varad Gautam0775cd12016-11-23 14:03:20 +05303282 if (gr->has_dmabuf_import) {
Pekka Paalanena3525802014-06-12 16:49:29 +03003283 gr->base.import_dmabuf = gl_renderer_import_dmabuf;
Varad Gautam0775cd12016-11-23 14:03:20 +05303284 gr->base.query_dmabuf_formats =
3285 gl_renderer_query_dmabuf_formats;
3286 gr->base.query_dmabuf_modifiers =
3287 gl_renderer_query_dmabuf_modifiers;
3288 }
Pekka Paalanena3525802014-06-12 16:49:29 +03003289
Armin Krezović28d240f2016-06-23 11:59:35 +02003290 if (gr->has_surfaceless_context) {
3291 weston_log("EGL_KHR_surfaceless_context available\n");
3292 gr->dummy_surface = EGL_NO_SURFACE;
3293 } else {
3294 weston_log("EGL_KHR_surfaceless_context unavailable. "
3295 "Trying PbufferSurface\n");
3296
3297 if (gl_renderer_create_pbuffer_surface(gr) < 0)
3298 goto fail_with_error;
3299 }
3300
Tomeu Vizoso12072b62013-08-06 20:05:55 +02003301 wl_display_add_shm_format(ec->wl_display, WL_SHM_FORMAT_RGB565);
Vincent Abrioufdeefe42016-10-05 14:54:34 +02003302 wl_display_add_shm_format(ec->wl_display, WL_SHM_FORMAT_YUV420);
Vincent Abriou00a03d22016-10-05 14:54:35 +02003303 wl_display_add_shm_format(ec->wl_display, WL_SHM_FORMAT_NV12);
Vincent ABRIOUe5732c72016-10-20 16:20:11 +02003304 wl_display_add_shm_format(ec->wl_display, WL_SHM_FORMAT_YUYV);
Tomeu Vizoso12072b62013-08-06 20:05:55 +02003305
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03003306 wl_signal_init(&gr->destroy_signal);
3307
Armin Krezović28d240f2016-06-23 11:59:35 +02003308 if (gl_renderer_setup(ec, gr->dummy_surface) < 0) {
3309 if (gr->dummy_surface != EGL_NO_SURFACE)
Emil Velikov02639552016-11-14 17:08:17 +00003310 weston_platform_destroy_egl_surface(gr->egl_display,
3311 gr->dummy_surface);
Armin Krezović28d240f2016-06-23 11:59:35 +02003312 goto fail_with_error;
3313 }
3314
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003315 return 0;
3316
Derek Foreman066ca0c2015-06-11 12:14:45 -05003317fail_with_error:
Pekka Paalanen326529f2012-11-27 12:25:25 +02003318 gl_renderer_print_egl_error_state();
Dawid Gajownik1a912a92015-08-21 00:20:54 -03003319fail_terminate:
3320 eglTerminate(gr->egl_display);
Derek Foreman066ca0c2015-06-11 12:14:45 -05003321fail:
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003322 free(gr);
3323 return -1;
3324}
3325
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003326static EGLDisplay
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003327gl_renderer_display(struct weston_compositor *ec)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003328{
3329 return get_renderer(ec)->egl_display;
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04003330}
3331
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003332static int
3333compile_shaders(struct weston_compositor *ec)
3334{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003335 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker40684142012-11-13 19:10:25 +01003336
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03003337 gr->texture_shader_rgba.vertex_source = vertex_shader;
3338 gr->texture_shader_rgba.fragment_source = texture_fragment_shader_rgba;
3339
3340 gr->texture_shader_rgbx.vertex_source = vertex_shader;
3341 gr->texture_shader_rgbx.fragment_source = texture_fragment_shader_rgbx;
3342
3343 gr->texture_shader_egl_external.vertex_source = vertex_shader;
3344 gr->texture_shader_egl_external.fragment_source =
3345 texture_fragment_shader_egl_external;
3346
3347 gr->texture_shader_y_uv.vertex_source = vertex_shader;
3348 gr->texture_shader_y_uv.fragment_source = texture_fragment_shader_y_uv;
3349
3350 gr->texture_shader_y_u_v.vertex_source = vertex_shader;
3351 gr->texture_shader_y_u_v.fragment_source =
3352 texture_fragment_shader_y_u_v;
3353
Ander Conselvan de Oliveira41a50ea2013-11-27 17:43:51 +02003354 gr->texture_shader_y_xuxv.vertex_source = vertex_shader;
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03003355 gr->texture_shader_y_xuxv.fragment_source =
3356 texture_fragment_shader_y_xuxv;
3357
3358 gr->solid_shader.vertex_source = vertex_shader;
3359 gr->solid_shader.fragment_source = solid_fragment_shader;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003360
3361 return 0;
3362}
3363
3364static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003365fragment_debug_binding(struct weston_keyboard *keyboard, uint32_t time,
3366 uint32_t key, void *data)
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003367{
3368 struct weston_compositor *ec = data;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003369 struct gl_renderer *gr = get_renderer(ec);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003370 struct weston_output *output;
3371
John Kåre Alsaker40684142012-11-13 19:10:25 +01003372 gr->fragment_shader_debug ^= 1;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003373
John Kåre Alsaker40684142012-11-13 19:10:25 +01003374 shader_release(&gr->texture_shader_rgba);
3375 shader_release(&gr->texture_shader_rgbx);
3376 shader_release(&gr->texture_shader_egl_external);
3377 shader_release(&gr->texture_shader_y_uv);
3378 shader_release(&gr->texture_shader_y_u_v);
3379 shader_release(&gr->texture_shader_y_xuxv);
3380 shader_release(&gr->solid_shader);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003381
Ander Conselvan de Oliveira03fb4ef2012-12-03 17:08:11 +02003382 /* Force use_shader() to call glUseProgram(), since we need to use
3383 * the recompiled version of the shader. */
3384 gr->current_shader = NULL;
3385
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003386 wl_list_for_each(output, &ec->output_list, link)
3387 weston_output_damage(output);
3388}
3389
Kristian Høgsberg8799d412013-05-07 10:50:09 -04003390static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003391fan_debug_repaint_binding(struct weston_keyboard *keyboard, uint32_t time,
3392 uint32_t key, void *data)
Kristian Høgsberg8799d412013-05-07 10:50:09 -04003393{
3394 struct weston_compositor *compositor = data;
3395 struct gl_renderer *gr = get_renderer(compositor);
3396
3397 gr->fan_debug = !gr->fan_debug;
3398 weston_compositor_damage_all(compositor);
3399}
3400
John Kåre Alsaker94659272012-11-13 19:10:18 +01003401static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003402gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003403{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003404 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003405 const char *extensions;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003406 EGLConfig context_config;
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003407 EGLBoolean ret;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003408
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003409 static const EGLint context_attribs[] = {
3410 EGL_CONTEXT_CLIENT_VERSION, 2,
3411 EGL_NONE
3412 };
3413
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003414 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
3415 weston_log("failed to bind EGL_OPENGL_ES_API\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02003416 gl_renderer_print_egl_error_state();
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003417 return -1;
3418 }
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03003419
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003420 context_config = gr->egl_config;
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03003421
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003422 if (gr->has_configless_context)
Armin Krezoviće3bfee12016-09-30 14:27:38 +02003423 context_config = EGL_NO_CONFIG_KHR;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003424
3425 gr->egl_context = eglCreateContext(gr->egl_display, context_config,
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003426 EGL_NO_CONTEXT, context_attribs);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003427 if (gr->egl_context == NULL) {
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003428 weston_log("failed to create context\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02003429 gl_renderer_print_egl_error_state();
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003430 return -1;
3431 }
3432
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003433 ret = eglMakeCurrent(gr->egl_display, egl_surface,
3434 egl_surface, gr->egl_context);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003435 if (ret == EGL_FALSE) {
3436 weston_log("Failed to make EGL context current.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02003437 gl_renderer_print_egl_error_state();
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003438 return -1;
3439 }
3440
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003441 log_egl_gl_info(gr->egl_display);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003442
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003443 gr->image_target_texture_2d =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003444 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003445
3446 extensions = (const char *) glGetString(GL_EXTENSIONS);
3447 if (!extensions) {
3448 weston_log("Retrieving GL extension string failed.\n");
3449 return -1;
3450 }
3451
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003452 if (!weston_check_egl_extension(extensions, "GL_EXT_texture_format_BGRA8888")) {
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003453 weston_log("GL_EXT_texture_format_BGRA8888 not available\n");
3454 return -1;
3455 }
3456
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003457 if (weston_check_egl_extension(extensions, "GL_EXT_read_format_bgra"))
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01003458 ec->read_format = PIXMAN_a8r8g8b8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003459 else
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01003460 ec->read_format = PIXMAN_a8b8g8r8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003461
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003462 if (weston_check_egl_extension(extensions, "GL_EXT_unpack_subimage"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003463 gr->has_unpack_subimage = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003464
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003465 if (weston_check_egl_extension(extensions, "GL_OES_EGL_image_external"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003466 gr->has_egl_image_external = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003467
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003468 glActiveTexture(GL_TEXTURE0);
3469
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003470 if (compile_shaders(ec))
3471 return -1;
3472
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03003473 gr->fragment_binding =
3474 weston_compositor_add_debug_binding(ec, KEY_S,
3475 fragment_debug_binding,
3476 ec);
3477 gr->fan_binding =
3478 weston_compositor_add_debug_binding(ec, KEY_F,
3479 fan_debug_repaint_binding,
3480 ec);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003481
Armin Krezović36d699a2016-08-05 15:28:30 +02003482 gr->output_destroy_listener.notify = output_handle_destroy;
3483 wl_signal_add(&ec->output_destroyed_signal,
3484 &gr->output_destroy_listener);
3485
Pekka Paalanen035a0322012-10-24 09:43:06 +03003486 weston_log("GL ES 2 renderer features:\n");
3487 weston_log_continue(STAMP_SPACE "read-back format: %s\n",
Pekka Paalanenfe4eacf2013-01-10 16:50:42 +02003488 ec->read_format == PIXMAN_a8r8g8b8 ? "BGRA" : "RGBA");
Pekka Paalanen035a0322012-10-24 09:43:06 +03003489 weston_log_continue(STAMP_SPACE "wl_shm sub-image to texture: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003490 gr->has_unpack_subimage ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03003491 weston_log_continue(STAMP_SPACE "EGL Wayland extension: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003492 gr->has_bind_display ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03003493
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003494
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003495 return 0;
3496}
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003497
3498WL_EXPORT struct gl_renderer_interface gl_renderer_interface = {
3499 .opaque_attribs = gl_renderer_opaque_attribs,
3500 .alpha_attribs = gl_renderer_alpha_attribs,
3501
Miguel A. Vicodddc6702016-05-18 17:41:07 +02003502 .display_create = gl_renderer_display_create,
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003503 .display = gl_renderer_display,
Miguel A. Vicoc095cde2016-05-18 17:43:00 +02003504 .output_window_create = gl_renderer_output_window_create,
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003505 .output_destroy = gl_renderer_output_destroy,
3506 .output_surface = gl_renderer_output_surface,
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05003507 .output_set_border = gl_renderer_output_set_border,
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003508 .print_egl_error_state = gl_renderer_print_egl_error_state
3509};