blob: 1463de1f748e1a40ebec4f74ff0a70d2608f338b [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.
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04004 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07005 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
Kristian Høgsbergd7c17262012-09-05 21:54:15 -040012 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070013 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
Kristian Høgsbergd7c17262012-09-05 21:54:15 -040025 */
26
Daniel Stonec228e232013-05-22 18:03:19 +030027#include "config.h"
Kristian Høgsberg25894fc2012-09-05 22:06:26 -040028
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010029#include <GLES2/gl2.h>
30#include <GLES2/gl2ext.h>
31
Derek Foremanf8180982014-10-16 16:37:02 -050032#include <stdbool.h>
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030033#include <stdint.h>
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -040034#include <stdlib.h>
Kristian Høgsberg25894fc2012-09-05 22:06:26 -040035#include <string.h>
36#include <ctype.h>
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +030037#include <float.h>
38#include <assert.h>
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +020039#include <linux/input.h>
Pekka Paalanena3525802014-06-12 16:49:29 +030040#include <drm_fourcc.h>
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -040041
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010042#include "gl-renderer.h"
Sam Spilsbury619859c2013-09-13 10:01:21 +080043#include "vertex-clipping.h"
Pekka Paalanena3525802014-06-12 16:49:29 +030044#include "linux-dmabuf.h"
Jonas Ådahl57e48f02015-11-17 16:00:28 +080045#include "linux-dmabuf-unstable-v1-server-protocol.h"
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010046
Jon Cruz35b2eaa2015-06-15 15:37:08 -070047#include "shared/helpers.h"
Emil Velikovf0c3a1c2016-07-04 15:34:18 +010048#include "shared/platform.h"
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010049#include "weston-egl-ext.h"
Kristian Høgsbergd7c17262012-09-05 21:54:15 -040050
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010051struct gl_shader {
John Kåre Alsaker40684142012-11-13 19:10:25 +010052 GLuint program;
53 GLuint vertex_shader, fragment_shader;
54 GLint proj_uniform;
55 GLint tex_uniforms[3];
56 GLint alpha_uniform;
57 GLint color_uniform;
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +030058 const char *vertex_source, *fragment_source;
John Kåre Alsaker40684142012-11-13 19:10:25 +010059};
60
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +020061#define BUFFER_DAMAGE_COUNT 2
62
Jason Ekstrande5512d42014-02-04 21:36:38 -060063enum gl_border_status {
64 BORDER_STATUS_CLEAN = 0,
65 BORDER_TOP_DIRTY = 1 << GL_RENDERER_BORDER_TOP,
66 BORDER_LEFT_DIRTY = 1 << GL_RENDERER_BORDER_LEFT,
67 BORDER_RIGHT_DIRTY = 1 << GL_RENDERER_BORDER_RIGHT,
68 BORDER_BOTTOM_DIRTY = 1 << GL_RENDERER_BORDER_BOTTOM,
69 BORDER_ALL_DIRTY = 0xf,
70 BORDER_SIZE_CHANGED = 0x10
71};
72
Jason Ekstrand0b61bf42013-10-27 22:24:54 -050073struct gl_border_image {
74 GLuint tex;
75 int32_t width, height;
76 int32_t tex_width;
Jason Ekstrand0b61bf42013-10-27 22:24:54 -050077 void *data;
78};
79
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010080struct gl_output_state {
John Kåre Alsaker94659272012-11-13 19:10:18 +010081 EGLSurface egl_surface;
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +020082 pixman_region32_t buffer_damage[BUFFER_DAMAGE_COUNT];
Derek Foreman4c582662014-10-09 18:39:44 -050083 int buffer_damage_index;
Jason Ekstrande5512d42014-02-04 21:36:38 -060084 enum gl_border_status border_damage[BUFFER_DAMAGE_COUNT];
Jason Ekstrand0b61bf42013-10-27 22:24:54 -050085 struct gl_border_image borders[4];
Jason Ekstrande5512d42014-02-04 21:36:38 -060086 enum gl_border_status border_status;
Jason Ekstrandfb23df72014-10-16 10:55:21 -050087
88 struct weston_matrix output_matrix;
John Kåre Alsaker94659272012-11-13 19:10:18 +010089};
90
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +030091enum buffer_type {
92 BUFFER_TYPE_NULL,
Pekka Paalanenaeb917e2015-02-09 13:56:56 +020093 BUFFER_TYPE_SOLID, /* internal solid color surfaces without a buffer */
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +030094 BUFFER_TYPE_SHM,
95 BUFFER_TYPE_EGL
96};
97
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +030098struct gl_renderer;
99
100struct egl_image {
101 struct gl_renderer *renderer;
102 EGLImageKHR image;
103 int refcount;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +0000104};
Pekka Paalanena3525802014-06-12 16:49:29 +0300105
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +0000106enum import_type {
107 IMPORT_TYPE_INVALID,
108 IMPORT_TYPE_DIRECT,
109 IMPORT_TYPE_GL_CONVERSION
110};
111
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +0000112struct dmabuf_image {
Pekka Paalanena3525802014-06-12 16:49:29 +0300113 struct linux_dmabuf_buffer *dmabuf;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +0000114 int num_images;
115 struct egl_image *images[3];
Pekka Paalanena3525802014-06-12 16:49:29 +0300116 struct wl_list link;
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +0000117
118 enum import_type import_type;
119 GLenum target;
120 struct gl_shader *shader;
121};
122
123struct yuv_plane_descriptor {
124 int width_divisor;
125 int height_divisor;
126 uint32_t format;
127 int plane_index;
128};
129
130struct yuv_format_descriptor {
131 uint32_t format;
132 int input_planes;
133 int output_planes;
134 int texture_type;
135 struct yuv_plane_descriptor plane[4];
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +0300136};
137
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100138struct gl_surface_state {
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100139 GLfloat color[4];
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100140 struct gl_shader *shader;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100141
142 GLuint textures[3];
143 int num_textures;
Derek Foreman4c11fe72015-11-18 16:32:27 -0600144 bool needs_full_upload;
Pekka Paalanen81ee3f52012-12-04 15:58:16 +0200145 pixman_region32_t texture_damage;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100146
Neil Roberts4d085e72014-04-07 15:01:01 +0100147 /* These are only used by SHM surfaces to detect when we need
148 * to do a full upload to specify a new internal texture
149 * format */
Vincent Abriou00a03d22016-10-05 14:54:35 +0200150 GLenum gl_format[3];
Neil Roberts4d085e72014-04-07 15:01:01 +0100151 GLenum gl_pixel_type;
152
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +0300153 struct egl_image* images[3];
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100154 GLenum target;
155 int num_images;
Pekka Paalanenfb003d32012-12-04 15:58:13 +0200156
157 struct weston_buffer_reference buffer_ref;
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +0300158 enum buffer_type buffer_type;
Pekka Paalanen68033ac2012-12-04 15:58:15 +0200159 int pitch; /* in pixels */
Alexander Larsson4ea95522013-05-22 14:41:37 +0200160 int height; /* in pixels */
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +0400161 int y_inverted;
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300162
Vincent Abrioufdeefe42016-10-05 14:54:34 +0200163 /* Extension needed for SHM YUV texture */
164 int offset[3]; /* offset per plane */
165 int hvsub[3]; /* horizontal vertical subsampling per plane */
166
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300167 struct weston_surface *surface;
168
169 struct wl_listener surface_destroy_listener;
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +0300170 struct wl_listener renderer_destroy_listener;
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100171};
172
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100173struct gl_renderer {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100174 struct weston_renderer base;
175 int fragment_shader_debug;
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400176 int fan_debug;
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +0300177 struct weston_binding *fragment_binding;
178 struct weston_binding *fan_binding;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100179
180 EGLDisplay egl_display;
181 EGLContext egl_context;
182 EGLConfig egl_config;
John Kåre Alsaker44154502012-11-13 19:10:20 +0100183
Armin Krezović28d240f2016-06-23 11:59:35 +0200184 EGLSurface dummy_surface;
185
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400186 struct wl_array vertices;
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400187 struct wl_array vtxcnt;
188
John Kåre Alsaker320711d2012-11-13 19:10:27 +0100189 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
190 PFNEGLCREATEIMAGEKHRPROC create_image;
191 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -0600192 PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC swap_buffers_with_damage;
Jonny Lamb671148f2015-03-20 15:26:52 +0100193 PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC create_platform_window;
Jonny Lamb671148f2015-03-20 15:26:52 +0100194
John Kåre Alsaker320711d2012-11-13 19:10:27 +0100195 int has_unpack_subimage;
196
197 PFNEGLBINDWAYLANDDISPLAYWL bind_display;
198 PFNEGLUNBINDWAYLANDDISPLAYWL unbind_display;
199 PFNEGLQUERYWAYLANDBUFFERWL query_buffer;
200 int has_bind_display;
201
202 int has_egl_image_external;
203
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +0200204 int has_egl_buffer_age;
205
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000206 int has_configless_context;
207
Armin Krezović28d240f2016-06-23 11:59:35 +0200208 int has_surfaceless_context;
209
Pekka Paalanena3525802014-06-12 16:49:29 +0300210 int has_dmabuf_import;
211 struct wl_list dmabuf_images;
212
Vincent Abrioufdeefe42016-10-05 14:54:34 +0200213 int has_gl_texture_rg;
214
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100215 struct gl_shader texture_shader_rgba;
216 struct gl_shader texture_shader_rgbx;
217 struct gl_shader texture_shader_egl_external;
218 struct gl_shader texture_shader_y_uv;
219 struct gl_shader texture_shader_y_u_v;
220 struct gl_shader texture_shader_y_xuxv;
221 struct gl_shader invert_color_shader;
222 struct gl_shader solid_shader;
223 struct gl_shader *current_shader;
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +0300224
225 struct wl_signal destroy_signal;
Armin Krezović36d699a2016-08-05 15:28:30 +0200226
227 struct wl_listener output_destroy_listener;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100228};
John Kåre Alsaker94659272012-11-13 19:10:18 +0100229
Jonny Lamb70eba3f2015-03-20 15:26:50 +0100230static PFNEGLGETPLATFORMDISPLAYEXTPROC get_platform_display = NULL;
Jonny Lamb70eba3f2015-03-20 15:26:50 +0100231
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +0000232static inline const char *
233dump_format(uint32_t format, char out[4])
234{
235#if BYTE_ORDER == BIG_ENDIAN
236 format = __builtin_bswap32(format);
237#endif
238 memcpy(out, &format, 4);
239 return out;
240}
241
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100242static inline struct gl_output_state *
John Kåre Alsaker94659272012-11-13 19:10:18 +0100243get_output_state(struct weston_output *output)
244{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100245 return (struct gl_output_state *)output->renderer_state;
John Kåre Alsaker94659272012-11-13 19:10:18 +0100246}
247
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300248static int
249gl_renderer_create_surface(struct weston_surface *surface);
250
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100251static inline struct gl_surface_state *
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100252get_surface_state(struct weston_surface *surface)
253{
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300254 if (!surface->renderer_state)
255 gl_renderer_create_surface(surface);
256
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100257 return (struct gl_surface_state *)surface->renderer_state;
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100258}
259
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100260static inline struct gl_renderer *
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100261get_renderer(struct weston_compositor *ec)
262{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100263 return (struct gl_renderer *)ec->renderer;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100264}
265
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +0300266static struct egl_image*
267egl_image_create(struct gl_renderer *gr, EGLenum target,
268 EGLClientBuffer buffer, const EGLint *attribs)
269{
270 struct egl_image *img;
271
272 img = zalloc(sizeof *img);
273 img->renderer = gr;
274 img->refcount = 1;
275 img->image = gr->create_image(gr->egl_display, EGL_NO_CONTEXT,
276 target, buffer, attribs);
277
278 if (img->image == EGL_NO_IMAGE_KHR) {
279 free(img);
280 return NULL;
281 }
282
283 return img;
284}
285
286static struct egl_image*
287egl_image_ref(struct egl_image *image)
288{
289 image->refcount++;
290
291 return image;
292}
293
294static int
295egl_image_unref(struct egl_image *image)
296{
297 struct gl_renderer *gr = image->renderer;
298
299 assert(image->refcount > 0);
300
301 image->refcount--;
302 if (image->refcount > 0)
303 return image->refcount;
304
305 gr->destroy_image(gr->egl_display, image->image);
306 free(image);
307
308 return 0;
309}
310
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +0000311static struct dmabuf_image*
312dmabuf_image_create(void)
313{
314 struct dmabuf_image *img;
315
316 img = zalloc(sizeof *img);
317 wl_list_init(&img->link);
318
319 return img;
320}
321
322static void
323dmabuf_image_destroy(struct dmabuf_image *image)
324{
325 int i;
326
327 for (i = 0; i < image->num_images; ++i)
328 egl_image_unref(image->images[i]);
329
330 if (image->dmabuf)
331 linux_dmabuf_buffer_set_user_data(image->dmabuf, NULL, NULL);
332
333 wl_list_remove(&image->link);
334}
335
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400336static const char *
337egl_error_string(EGLint code)
338{
339#define MYERRCODE(x) case x: return #x;
340 switch (code) {
341 MYERRCODE(EGL_SUCCESS)
342 MYERRCODE(EGL_NOT_INITIALIZED)
343 MYERRCODE(EGL_BAD_ACCESS)
344 MYERRCODE(EGL_BAD_ALLOC)
345 MYERRCODE(EGL_BAD_ATTRIBUTE)
346 MYERRCODE(EGL_BAD_CONTEXT)
347 MYERRCODE(EGL_BAD_CONFIG)
348 MYERRCODE(EGL_BAD_CURRENT_SURFACE)
349 MYERRCODE(EGL_BAD_DISPLAY)
350 MYERRCODE(EGL_BAD_SURFACE)
351 MYERRCODE(EGL_BAD_MATCH)
352 MYERRCODE(EGL_BAD_PARAMETER)
353 MYERRCODE(EGL_BAD_NATIVE_PIXMAP)
354 MYERRCODE(EGL_BAD_NATIVE_WINDOW)
355 MYERRCODE(EGL_CONTEXT_LOST)
356 default:
357 return "unknown";
358 }
359#undef MYERRCODE
360}
361
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300362static void
Pekka Paalanen326529f2012-11-27 12:25:25 +0200363gl_renderer_print_egl_error_state(void)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400364{
365 EGLint code;
366
367 code = eglGetError();
368 weston_log("EGL error state: %s (0x%04lx)\n",
369 egl_error_string(code), (long)code);
370}
371
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400372#define max(a, b) (((a) > (b)) ? (a) : (b))
373#define min(a, b) (((a) > (b)) ? (b) : (a))
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400374
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300375/*
376 * Compute the boundary vertices of the intersection of the global coordinate
377 * aligned rectangle 'rect', and an arbitrary quadrilateral produced from
378 * 'surf_rect' when transformed from surface coordinates into global coordinates.
379 * The vertices are written to 'ex' and 'ey', and the return value is the
380 * number of vertices. Vertices are produced in clockwise winding order.
381 * Guarantees to produce either zero vertices, or 3-8 vertices with non-zero
382 * polygon area.
383 */
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400384static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500385calculate_edges(struct weston_view *ev, pixman_box32_t *rect,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400386 pixman_box32_t *surf_rect, GLfloat *ex, GLfloat *ey)
387{
Sam Spilsbury619859c2013-09-13 10:01:21 +0800388
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300389 struct clip_context ctx;
390 int i, n;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400391 GLfloat min_x, max_x, min_y, max_y;
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300392 struct polygon8 surf = {
393 { surf_rect->x1, surf_rect->x2, surf_rect->x2, surf_rect->x1 },
394 { surf_rect->y1, surf_rect->y1, surf_rect->y2, surf_rect->y2 },
395 4
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400396 };
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400397
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300398 ctx.clip.x1 = rect->x1;
399 ctx.clip.y1 = rect->y1;
400 ctx.clip.x2 = rect->x2;
401 ctx.clip.y2 = rect->y2;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400402
403 /* transform surface to screen space: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300404 for (i = 0; i < surf.n; i++)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500405 weston_view_to_global_float(ev, surf.x[i], surf.y[i],
406 &surf.x[i], &surf.y[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400407
408 /* find bounding box: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300409 min_x = max_x = surf.x[0];
410 min_y = max_y = surf.y[0];
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400411
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300412 for (i = 1; i < surf.n; i++) {
413 min_x = min(min_x, surf.x[i]);
414 max_x = max(max_x, surf.x[i]);
415 min_y = min(min_y, surf.y[i]);
416 max_y = max(max_y, surf.y[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400417 }
418
419 /* First, simple bounding box check to discard early transformed
420 * surface rects that do not intersect with the clip region:
421 */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300422 if ((min_x >= ctx.clip.x2) || (max_x <= ctx.clip.x1) ||
423 (min_y >= ctx.clip.y2) || (max_y <= ctx.clip.y1))
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400424 return 0;
425
426 /* Simple case, bounding box edges are parallel to surface edges,
427 * there will be only four edges. We just need to clip the surface
428 * vertices to the clip rect bounds:
429 */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500430 if (!ev->transform.enabled)
Sam Spilsbury619859c2013-09-13 10:01:21 +0800431 return clip_simple(&ctx, &surf, ex, ey);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400432
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300433 /* Transformed case: use a general polygon clipping algorithm to
434 * clip the surface rectangle with each side of 'rect'.
435 * The algorithm is Sutherland-Hodgman, as explained in
436 * http://www.codeguru.com/cpp/misc/misc/graphics/article.php/c8965/Polygon-Clipping.htm
437 * but without looking at any of that code.
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400438 */
Sam Spilsbury619859c2013-09-13 10:01:21 +0800439 n = clip_transformed(&ctx, &surf, ex, ey);
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300440
441 if (n < 3)
442 return 0;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400443
444 return n;
445}
446
Derek Foremanf8180982014-10-16 16:37:02 -0500447static bool
448merge_down(pixman_box32_t *a, pixman_box32_t *b, pixman_box32_t *merge)
449{
450 if (a->x1 == b->x1 && a->x2 == b->x2 && a->y1 == b->y2) {
451 merge->x1 = a->x1;
452 merge->x2 = a->x2;
453 merge->y1 = b->y1;
454 merge->y2 = a->y2;
455 return true;
456 }
457 return false;
458}
459
460static int
461compress_bands(pixman_box32_t *inrects, int nrects,
462 pixman_box32_t **outrects)
463{
Quentin Glidicd84af7c2016-07-10 11:00:50 +0200464 bool merged = false;
Derek Foremanf8180982014-10-16 16:37:02 -0500465 pixman_box32_t *out, merge_rect;
466 int i, j, nout;
467
468 if (!nrects) {
469 *outrects = NULL;
470 return 0;
471 }
472
473 /* nrects is an upper bound - we're not too worried about
474 * allocating a little extra
475 */
476 out = malloc(sizeof(pixman_box32_t) * nrects);
477 out[0] = inrects[0];
478 nout = 1;
479 for (i = 1; i < nrects; i++) {
480 for (j = 0; j < nout; j++) {
481 merged = merge_down(&inrects[i], &out[j], &merge_rect);
482 if (merged) {
483 out[j] = merge_rect;
484 break;
485 }
486 }
487 if (!merged) {
488 out[nout] = inrects[i];
489 nout++;
490 }
491 }
492 *outrects = out;
493 return nout;
494}
495
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400496static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500497texture_region(struct weston_view *ev, pixman_region32_t *region,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400498 pixman_region32_t *surf_region)
499{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500500 struct gl_surface_state *gs = get_surface_state(ev->surface);
501 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400502 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400503 GLfloat *v, inv_width, inv_height;
504 unsigned int *vtxcnt, nvtx = 0;
505 pixman_box32_t *rects, *surf_rects;
Derek Foremanf8180982014-10-16 16:37:02 -0500506 pixman_box32_t *raw_rects;
507 int i, j, k, nrects, nsurf, raw_nrects;
508 bool used_band_compression;
509 raw_rects = pixman_region32_rectangles(region, &raw_nrects);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400510 surf_rects = pixman_region32_rectangles(surf_region, &nsurf);
511
Derek Foremanf8180982014-10-16 16:37:02 -0500512 if (raw_nrects < 4) {
513 used_band_compression = false;
514 nrects = raw_nrects;
515 rects = raw_rects;
516 } else {
517 nrects = compress_bands(raw_rects, raw_nrects, &rects);
518 used_band_compression = true;
519 }
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400520 /* worst case we can have 8 vertices per rect (ie. clipped into
521 * an octagon):
522 */
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400523 v = wl_array_add(&gr->vertices, nrects * nsurf * 8 * 4 * sizeof *v);
524 vtxcnt = wl_array_add(&gr->vtxcnt, nrects * nsurf * sizeof *vtxcnt);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400525
Pekka Paalanen68033ac2012-12-04 15:58:15 +0200526 inv_width = 1.0 / gs->pitch;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200527 inv_height = 1.0 / gs->height;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400528
529 for (i = 0; i < nrects; i++) {
530 pixman_box32_t *rect = &rects[i];
531 for (j = 0; j < nsurf; j++) {
532 pixman_box32_t *surf_rect = &surf_rects[j];
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200533 GLfloat sx, sy, bx, by;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400534 GLfloat ex[8], ey[8]; /* edge points in screen space */
535 int n;
536
537 /* The transformed surface, after clipping to the clip region,
538 * can have as many as eight sides, emitted as a triangle-fan.
539 * The first vertex in the triangle fan can be chosen arbitrarily,
540 * since the area is guaranteed to be convex.
541 *
542 * If a corner of the transformed surface falls outside of the
543 * clip region, instead of emitting one vertex for the corner
544 * of the surface, up to two are emitted for two corresponding
545 * intersection point(s) between the surface and the clip region.
546 *
547 * To do this, we first calculate the (up to eight) points that
548 * form the intersection of the clip rect and the transformed
549 * surface.
550 */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500551 n = calculate_edges(ev, rect, surf_rect, ex, ey);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400552 if (n < 3)
553 continue;
554
555 /* emit edge points: */
556 for (k = 0; k < n; k++) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500557 weston_view_from_global_float(ev, ex[k], ey[k],
558 &sx, &sy);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400559 /* position: */
560 *(v++) = ex[k];
561 *(v++) = ey[k];
562 /* texcoord: */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500563 weston_surface_to_buffer_float(ev->surface,
564 sx, sy,
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200565 &bx, &by);
566 *(v++) = bx * inv_width;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +0400567 if (gs->y_inverted) {
568 *(v++) = by * inv_height;
569 } else {
570 *(v++) = (gs->height - by) * inv_height;
571 }
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400572 }
573
574 vtxcnt[nvtx++] = n;
575 }
576 }
577
Derek Foremanf8180982014-10-16 16:37:02 -0500578 if (used_band_compression)
579 free(rects);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400580 return nvtx;
581}
582
583static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500584triangle_fan_debug(struct weston_view *view, int first, int count)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400585{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500586 struct weston_compositor *compositor = view->surface->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100587 struct gl_renderer *gr = get_renderer(compositor);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400588 int i;
589 GLushort *buffer;
590 GLushort *index;
591 int nelems;
592 static int color_idx = 0;
593 static const GLfloat color[][4] = {
594 { 1.0, 0.0, 0.0, 1.0 },
595 { 0.0, 1.0, 0.0, 1.0 },
596 { 0.0, 0.0, 1.0, 1.0 },
597 { 1.0, 1.0, 1.0, 1.0 },
598 };
599
600 nelems = (count - 1 + count - 2) * 2;
601
602 buffer = malloc(sizeof(GLushort) * nelems);
603 index = buffer;
604
605 for (i = 1; i < count; i++) {
606 *index++ = first;
607 *index++ = first + i;
608 }
609
610 for (i = 2; i < count; i++) {
611 *index++ = first + i - 1;
612 *index++ = first + i;
613 }
614
John Kåre Alsaker40684142012-11-13 19:10:25 +0100615 glUseProgram(gr->solid_shader.program);
616 glUniform4fv(gr->solid_shader.color_uniform, 1,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400617 color[color_idx++ % ARRAY_LENGTH(color)]);
618 glDrawElements(GL_LINES, nelems, GL_UNSIGNED_SHORT, buffer);
John Kåre Alsaker40684142012-11-13 19:10:25 +0100619 glUseProgram(gr->current_shader->program);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400620 free(buffer);
621}
622
623static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500624repaint_region(struct weston_view *ev, pixman_region32_t *region,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400625 pixman_region32_t *surf_region)
626{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500627 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400628 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400629 GLfloat *v;
630 unsigned int *vtxcnt;
631 int i, first, nfans;
632
633 /* The final region to be painted is the intersection of
634 * 'region' and 'surf_region'. However, 'region' is in the global
635 * coordinates, and 'surf_region' is in the surface-local
636 * coordinates. texture_region() will iterate over all pairs of
637 * rectangles from both regions, compute the intersection
638 * polygon for each pair, and store it as a triangle fan if
Bryce Harringtonea8fb942016-01-13 18:48:56 -0800639 * it has a non-zero area (at least 3 vertices, actually).
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400640 */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500641 nfans = texture_region(ev, region, surf_region);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400642
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400643 v = gr->vertices.data;
644 vtxcnt = gr->vtxcnt.data;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400645
646 /* position: */
647 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
648 glEnableVertexAttribArray(0);
649
650 /* texcoord: */
651 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
652 glEnableVertexAttribArray(1);
653
654 for (i = 0, first = 0; i < nfans; i++) {
655 glDrawArrays(GL_TRIANGLE_FAN, first, vtxcnt[i]);
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400656 if (gr->fan_debug)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500657 triangle_fan_debug(ev, first, vtxcnt[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400658 first += vtxcnt[i];
659 }
660
661 glDisableVertexAttribArray(1);
662 glDisableVertexAttribArray(0);
663
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400664 gr->vertices.size = 0;
665 gr->vtxcnt.size = 0;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400666}
667
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100668static int
669use_output(struct weston_output *output)
670{
671 static int errored;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100672 struct gl_output_state *go = get_output_state(output);
673 struct gl_renderer *gr = get_renderer(output->compositor);
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100674 EGLBoolean ret;
675
676 ret = eglMakeCurrent(gr->egl_display, go->egl_surface,
677 go->egl_surface, gr->egl_context);
678
679 if (ret == EGL_FALSE) {
680 if (errored)
681 return -1;
682 errored = 1;
683 weston_log("Failed to make EGL context current.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +0200684 gl_renderer_print_egl_error_state();
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100685 return -1;
686 }
687
688 return 0;
689}
690
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +0300691static int
692shader_init(struct gl_shader *shader, struct gl_renderer *gr,
693 const char *vertex_source, const char *fragment_source);
694
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400695static void
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +0300696use_shader(struct gl_renderer *gr, struct gl_shader *shader)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400697{
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +0300698 if (!shader->program) {
699 int ret;
700
701 ret = shader_init(shader, gr,
702 shader->vertex_source,
703 shader->fragment_source);
704
705 if (ret < 0)
706 weston_log("warning: failed to compile shader\n");
707 }
708
John Kåre Alsaker40684142012-11-13 19:10:25 +0100709 if (gr->current_shader == shader)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400710 return;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400711 glUseProgram(shader->program);
John Kåre Alsaker40684142012-11-13 19:10:25 +0100712 gr->current_shader = shader;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400713}
714
715static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100716shader_uniforms(struct gl_shader *shader,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500717 struct weston_view *view,
718 struct weston_output *output)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400719{
720 int i;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500721 struct gl_surface_state *gs = get_surface_state(view->surface);
Jason Ekstrandfb23df72014-10-16 10:55:21 -0500722 struct gl_output_state *go = get_output_state(output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400723
724 glUniformMatrix4fv(shader->proj_uniform,
Jason Ekstrandfb23df72014-10-16 10:55:21 -0500725 1, GL_FALSE, go->output_matrix.d);
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100726 glUniform4fv(shader->color_uniform, 1, gs->color);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500727 glUniform1f(shader->alpha_uniform, view->alpha);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400728
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100729 for (i = 0; i < gs->num_textures; i++)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400730 glUniform1i(shader->tex_uniforms[i], i);
731}
732
733static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500734draw_view(struct weston_view *ev, struct weston_output *output,
735 pixman_region32_t *damage) /* in global coordinates */
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400736{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500737 struct weston_compositor *ec = ev->surface->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100738 struct gl_renderer *gr = get_renderer(ec);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500739 struct gl_surface_state *gs = get_surface_state(ev->surface);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400740 /* repaint bounding region in global coordinates: */
741 pixman_region32_t repaint;
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +0200742 /* opaque region in surface coordinates: */
743 pixman_region32_t surface_opaque;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400744 /* non-opaque region in surface coordinates: */
745 pixman_region32_t surface_blend;
746 GLint filter;
747 int i;
748
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +0200749 /* In case of a runtime switch of renderers, we may not have received
750 * an attach for this surface since the switch. In that case we don't
751 * have a valid buffer or a proper shader set up so skip rendering. */
752 if (!gs->shader)
753 return;
754
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400755 pixman_region32_init(&repaint);
756 pixman_region32_intersect(&repaint,
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200757 &ev->transform.boundingbox, damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500758 pixman_region32_subtract(&repaint, &repaint, &ev->clip);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400759
760 if (!pixman_region32_not_empty(&repaint))
761 goto out;
762
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400763 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
764
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400765 if (gr->fan_debug) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100766 use_shader(gr, &gr->solid_shader);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500767 shader_uniforms(&gr->solid_shader, ev, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400768 }
769
John Kåre Alsaker40684142012-11-13 19:10:25 +0100770 use_shader(gr, gs->shader);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500771 shader_uniforms(gs->shader, ev, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400772
Jason Ekstranda7af7042013-10-12 22:38:11 -0500773 if (ev->transform.enabled || output->zoom.active ||
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200774 output->current_scale != ev->surface->buffer_viewport.buffer.scale)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400775 filter = GL_LINEAR;
776 else
777 filter = GL_NEAREST;
778
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100779 for (i = 0; i < gs->num_textures; i++) {
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400780 glActiveTexture(GL_TEXTURE0 + i);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100781 glBindTexture(gs->target, gs->textures[i]);
782 glTexParameteri(gs->target, GL_TEXTURE_MIN_FILTER, filter);
783 glTexParameteri(gs->target, GL_TEXTURE_MAG_FILTER, filter);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400784 }
785
786 /* blended region is whole surface minus opaque region: */
787 pixman_region32_init_rect(&surface_blend, 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600788 ev->surface->width, ev->surface->height);
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +0200789 if (ev->geometry.scissor_enabled)
790 pixman_region32_intersect(&surface_blend, &surface_blend,
791 &ev->geometry.scissor);
792 pixman_region32_subtract(&surface_blend, &surface_blend,
793 &ev->surface->opaque);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400794
Jason Ekstranda7af7042013-10-12 22:38:11 -0500795 /* XXX: Should we be using ev->transform.opaque here? */
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +0200796 pixman_region32_init(&surface_opaque);
797 if (ev->geometry.scissor_enabled)
798 pixman_region32_intersect(&surface_opaque,
799 &ev->surface->opaque,
800 &ev->geometry.scissor);
801 else
802 pixman_region32_copy(&surface_opaque, &ev->surface->opaque);
803
804 if (pixman_region32_not_empty(&surface_opaque)) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100805 if (gs->shader == &gr->texture_shader_rgba) {
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400806 /* Special case for RGBA textures with possibly
807 * bad data in alpha channel: use the shader
808 * that forces texture alpha = 1.0.
809 * Xwayland surfaces need this.
810 */
John Kåre Alsaker40684142012-11-13 19:10:25 +0100811 use_shader(gr, &gr->texture_shader_rgbx);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500812 shader_uniforms(&gr->texture_shader_rgbx, ev, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400813 }
814
Jason Ekstranda7af7042013-10-12 22:38:11 -0500815 if (ev->alpha < 1.0)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400816 glEnable(GL_BLEND);
817 else
818 glDisable(GL_BLEND);
819
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +0200820 repaint_region(ev, &repaint, &surface_opaque);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400821 }
822
823 if (pixman_region32_not_empty(&surface_blend)) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100824 use_shader(gr, gs->shader);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400825 glEnable(GL_BLEND);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500826 repaint_region(ev, &repaint, &surface_blend);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400827 }
828
829 pixman_region32_fini(&surface_blend);
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +0200830 pixman_region32_fini(&surface_opaque);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400831
832out:
833 pixman_region32_fini(&repaint);
834}
835
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400836static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500837repaint_views(struct weston_output *output, pixman_region32_t *damage)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400838{
839 struct weston_compositor *compositor = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500840 struct weston_view *view;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400841
Jason Ekstranda7af7042013-10-12 22:38:11 -0500842 wl_list_for_each_reverse(view, &compositor->view_list, link)
843 if (view->plane == &compositor->primary_plane)
844 draw_view(view, output, damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400845}
846
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500847static void
Jason Ekstrande5512d42014-02-04 21:36:38 -0600848draw_output_border_texture(struct gl_output_state *go,
849 enum gl_renderer_border_side side,
850 int32_t x, int32_t y,
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500851 int32_t width, int32_t height)
852{
Jason Ekstrande5512d42014-02-04 21:36:38 -0600853 struct gl_border_image *img = &go->borders[side];
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500854 static GLushort indices [] = { 0, 1, 3, 3, 1, 2 };
855
856 if (!img->data) {
857 if (img->tex) {
858 glDeleteTextures(1, &img->tex);
859 img->tex = 0;
860 }
861
862 return;
863 }
864
865 if (!img->tex) {
866 glGenTextures(1, &img->tex);
867 glBindTexture(GL_TEXTURE_2D, img->tex);
868
869 glTexParameteri(GL_TEXTURE_2D,
870 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
871 glTexParameteri(GL_TEXTURE_2D,
872 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
873 glTexParameteri(GL_TEXTURE_2D,
874 GL_TEXTURE_MIN_FILTER, GL_NEAREST);
875 glTexParameteri(GL_TEXTURE_2D,
876 GL_TEXTURE_MAG_FILTER, GL_NEAREST);
877 } else {
878 glBindTexture(GL_TEXTURE_2D, img->tex);
879 }
880
Jason Ekstrande5512d42014-02-04 21:36:38 -0600881 if (go->border_status & (1 << side)) {
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500882 glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, 0);
883 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, 0);
884 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, 0);
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500885 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
886 img->tex_width, img->height, 0,
887 GL_BGRA_EXT, GL_UNSIGNED_BYTE, img->data);
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500888 }
889
890 GLfloat texcoord[] = {
891 0.0f, 0.0f,
892 (GLfloat)img->width / (GLfloat)img->tex_width, 0.0f,
893 (GLfloat)img->width / (GLfloat)img->tex_width, 1.0f,
894 0.0f, 1.0f,
895 };
896
897 GLfloat verts[] = {
898 x, y,
899 x + width, y,
900 x + width, y + height,
901 x, y + height
902 };
903
904 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, verts);
905 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, texcoord);
906 glEnableVertexAttribArray(0);
907 glEnableVertexAttribArray(1);
908
909 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
910
911 glDisableVertexAttribArray(1);
912 glDisableVertexAttribArray(0);
913}
914
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -0600915static int
916output_has_borders(struct weston_output *output)
917{
918 struct gl_output_state *go = get_output_state(output);
919
920 return go->borders[GL_RENDERER_BORDER_TOP].data ||
921 go->borders[GL_RENDERER_BORDER_RIGHT].data ||
922 go->borders[GL_RENDERER_BORDER_BOTTOM].data ||
923 go->borders[GL_RENDERER_BORDER_LEFT].data;
924}
925
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500926static void
Jason Ekstrande5512d42014-02-04 21:36:38 -0600927draw_output_borders(struct weston_output *output,
928 enum gl_border_status border_status)
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500929{
930 struct gl_output_state *go = get_output_state(output);
931 struct gl_renderer *gr = get_renderer(output->compositor);
932 struct gl_shader *shader = &gr->texture_shader_rgba;
Jason Ekstrand00b84282013-10-27 22:24:59 -0500933 struct gl_border_image *top, *bottom, *left, *right;
934 struct weston_matrix matrix;
935 int full_width, full_height;
936
Jason Ekstrande5512d42014-02-04 21:36:38 -0600937 if (border_status == BORDER_STATUS_CLEAN)
938 return; /* Clean. Nothing to do. */
939
Jason Ekstrand00b84282013-10-27 22:24:59 -0500940 top = &go->borders[GL_RENDERER_BORDER_TOP];
941 bottom = &go->borders[GL_RENDERER_BORDER_BOTTOM];
942 left = &go->borders[GL_RENDERER_BORDER_LEFT];
943 right = &go->borders[GL_RENDERER_BORDER_RIGHT];
944
945 full_width = output->current_mode->width + left->width + right->width;
946 full_height = output->current_mode->height + top->height + bottom->height;
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500947
948 glDisable(GL_BLEND);
949 use_shader(gr, shader);
950
Jason Ekstrand00b84282013-10-27 22:24:59 -0500951 glViewport(0, 0, full_width, full_height);
952
953 weston_matrix_init(&matrix);
954 weston_matrix_translate(&matrix, -full_width/2.0, -full_height/2.0, 0);
955 weston_matrix_scale(&matrix, 2.0/full_width, -2.0/full_height, 1);
956 glUniformMatrix4fv(shader->proj_uniform, 1, GL_FALSE, matrix.d);
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500957
958 glUniform1i(shader->tex_uniforms[0], 0);
959 glUniform1f(shader->alpha_uniform, 1);
960 glActiveTexture(GL_TEXTURE0);
961
Jason Ekstrande5512d42014-02-04 21:36:38 -0600962 if (border_status & BORDER_TOP_DIRTY)
963 draw_output_border_texture(go, GL_RENDERER_BORDER_TOP,
964 0, 0,
965 full_width, top->height);
966 if (border_status & BORDER_LEFT_DIRTY)
967 draw_output_border_texture(go, GL_RENDERER_BORDER_LEFT,
968 0, top->height,
969 left->width, output->current_mode->height);
970 if (border_status & BORDER_RIGHT_DIRTY)
971 draw_output_border_texture(go, GL_RENDERER_BORDER_RIGHT,
972 full_width - right->width, top->height,
973 right->width, output->current_mode->height);
974 if (border_status & BORDER_BOTTOM_DIRTY)
975 draw_output_border_texture(go, GL_RENDERER_BORDER_BOTTOM,
976 0, full_height - bottom->height,
977 full_width, bottom->height);
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500978}
John Kåre Alsaker44154502012-11-13 19:10:20 +0100979
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400980static void
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -0600981output_get_border_damage(struct weston_output *output,
982 enum gl_border_status border_status,
983 pixman_region32_t *damage)
984{
985 struct gl_output_state *go = get_output_state(output);
986 struct gl_border_image *top, *bottom, *left, *right;
987 int full_width, full_height;
988
989 if (border_status == BORDER_STATUS_CLEAN)
990 return; /* Clean. Nothing to do. */
991
992 top = &go->borders[GL_RENDERER_BORDER_TOP];
993 bottom = &go->borders[GL_RENDERER_BORDER_BOTTOM];
994 left = &go->borders[GL_RENDERER_BORDER_LEFT];
995 right = &go->borders[GL_RENDERER_BORDER_RIGHT];
996
997 full_width = output->current_mode->width + left->width + right->width;
998 full_height = output->current_mode->height + top->height + bottom->height;
999 if (border_status & BORDER_TOP_DIRTY)
1000 pixman_region32_union_rect(damage, damage,
1001 0, 0,
1002 full_width, top->height);
1003 if (border_status & BORDER_LEFT_DIRTY)
1004 pixman_region32_union_rect(damage, damage,
1005 0, top->height,
1006 left->width, output->current_mode->height);
1007 if (border_status & BORDER_RIGHT_DIRTY)
1008 pixman_region32_union_rect(damage, damage,
1009 full_width - right->width, top->height,
1010 right->width, output->current_mode->height);
1011 if (border_status & BORDER_BOTTOM_DIRTY)
1012 pixman_region32_union_rect(damage, damage,
1013 0, full_height - bottom->height,
1014 full_width, bottom->height);
1015}
1016
1017static void
Jason Ekstrande5512d42014-02-04 21:36:38 -06001018output_get_damage(struct weston_output *output,
1019 pixman_region32_t *buffer_damage, uint32_t *border_damage)
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001020{
1021 struct gl_output_state *go = get_output_state(output);
1022 struct gl_renderer *gr = get_renderer(output->compositor);
1023 EGLint buffer_age = 0;
1024 EGLBoolean ret;
1025 int i;
1026
1027 if (gr->has_egl_buffer_age) {
1028 ret = eglQuerySurface(gr->egl_display, go->egl_surface,
1029 EGL_BUFFER_AGE_EXT, &buffer_age);
1030 if (ret == EGL_FALSE) {
1031 weston_log("buffer age query failed.\n");
1032 gl_renderer_print_egl_error_state();
1033 }
1034 }
1035
Jason Ekstrande5512d42014-02-04 21:36:38 -06001036 if (buffer_age == 0 || buffer_age - 1 > BUFFER_DAMAGE_COUNT) {
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001037 pixman_region32_copy(buffer_damage, &output->region);
Jason Ekstrande5512d42014-02-04 21:36:38 -06001038 *border_damage = BORDER_ALL_DIRTY;
1039 } else {
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001040 for (i = 0; i < buffer_age - 1; i++)
Derek Foreman4c582662014-10-09 18:39:44 -05001041 *border_damage |= go->border_damage[(go->buffer_damage_index + i) % BUFFER_DAMAGE_COUNT];
Jason Ekstrande5512d42014-02-04 21:36:38 -06001042
1043 if (*border_damage & BORDER_SIZE_CHANGED) {
1044 /* If we've had a resize, we have to do a full
1045 * repaint. */
1046 *border_damage |= BORDER_ALL_DIRTY;
1047 pixman_region32_copy(buffer_damage, &output->region);
1048 } else {
1049 for (i = 0; i < buffer_age - 1; i++)
1050 pixman_region32_union(buffer_damage,
1051 buffer_damage,
Derek Foreman4c582662014-10-09 18:39:44 -05001052 &go->buffer_damage[(go->buffer_damage_index + i) % BUFFER_DAMAGE_COUNT]);
Jason Ekstrande5512d42014-02-04 21:36:38 -06001053 }
1054 }
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001055}
1056
1057static void
1058output_rotate_damage(struct weston_output *output,
Jason Ekstrande5512d42014-02-04 21:36:38 -06001059 pixman_region32_t *output_damage,
1060 enum gl_border_status border_status)
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001061{
1062 struct gl_output_state *go = get_output_state(output);
1063 struct gl_renderer *gr = get_renderer(output->compositor);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001064
1065 if (!gr->has_egl_buffer_age)
1066 return;
1067
Derek Foreman4c582662014-10-09 18:39:44 -05001068 go->buffer_damage_index += BUFFER_DAMAGE_COUNT - 1;
1069 go->buffer_damage_index %= BUFFER_DAMAGE_COUNT;
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001070
Derek Foreman4c582662014-10-09 18:39:44 -05001071 pixman_region32_copy(&go->buffer_damage[go->buffer_damage_index], output_damage);
1072 go->border_damage[go->buffer_damage_index] = border_status;
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001073}
1074
Derek Foremanc4cfe852015-05-15 12:12:40 -05001075/* NOTE: We now allow falling back to ARGB gl visuals when XRGB is
1076 * unavailable, so we're assuming the background has no transparency
1077 * and that everything with a blend, like drop shadows, will have something
1078 * opaque (like the background) drawn underneath it.
1079 *
1080 * Depending on the underlying hardware, violating that assumption could
1081 * result in seeing through to another display plane.
1082 */
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001083static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001084gl_renderer_repaint_output(struct weston_output *output,
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001085 pixman_region32_t *output_damage)
1086{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001087 struct gl_output_state *go = get_output_state(output);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001088 struct weston_compositor *compositor = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001089 struct gl_renderer *gr = get_renderer(compositor);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001090 EGLBoolean ret;
1091 static int errored;
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -06001092 int i, nrects, buffer_height;
1093 EGLint *egl_damage, *d;
1094 pixman_box32_t *rects;
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001095 pixman_region32_t buffer_damage, total_damage;
Jason Ekstrande5512d42014-02-04 21:36:38 -06001096 enum gl_border_status border_damage = BORDER_STATUS_CLEAN;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001097
Jason Ekstrandae0c6e32014-10-16 10:55:20 -05001098 if (use_output(output) < 0)
1099 return;
1100
Jason Ekstrand00b84282013-10-27 22:24:59 -05001101 /* Calculate the viewport */
1102 glViewport(go->borders[GL_RENDERER_BORDER_LEFT].width,
1103 go->borders[GL_RENDERER_BORDER_BOTTOM].height,
1104 output->current_mode->width,
1105 output->current_mode->height);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001106
Jason Ekstrandfb23df72014-10-16 10:55:21 -05001107 /* Calculate the global GL matrix */
1108 go->output_matrix = output->matrix;
1109 weston_matrix_translate(&go->output_matrix,
1110 -(output->current_mode->width / 2.0),
1111 -(output->current_mode->height / 2.0), 0);
1112 weston_matrix_scale(&go->output_matrix,
1113 2.0 / output->current_mode->width,
1114 -2.0 / output->current_mode->height, 1);
1115
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001116 /* if debugging, redraw everything outside the damage to clean up
1117 * debug lines from the previous draw on this buffer:
1118 */
Kristian Høgsberg8799d412013-05-07 10:50:09 -04001119 if (gr->fan_debug) {
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001120 pixman_region32_t undamaged;
1121 pixman_region32_init(&undamaged);
1122 pixman_region32_subtract(&undamaged, &output->region,
1123 output_damage);
Kristian Høgsberg8799d412013-05-07 10:50:09 -04001124 gr->fan_debug = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001125 repaint_views(output, &undamaged);
Kristian Høgsberg8799d412013-05-07 10:50:09 -04001126 gr->fan_debug = 1;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001127 pixman_region32_fini(&undamaged);
1128 }
1129
Ander Conselvan de Oliveirab605c062013-03-05 17:30:28 +02001130 pixman_region32_init(&total_damage);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001131 pixman_region32_init(&buffer_damage);
1132
Jason Ekstrande5512d42014-02-04 21:36:38 -06001133 output_get_damage(output, &buffer_damage, &border_damage);
1134 output_rotate_damage(output, output_damage, go->border_status);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001135
1136 pixman_region32_union(&total_damage, &buffer_damage, output_damage);
Jason Ekstrande5512d42014-02-04 21:36:38 -06001137 border_damage |= go->border_status;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03001138
Jason Ekstranda7af7042013-10-12 22:38:11 -05001139 repaint_views(output, &total_damage);
Ander Conselvan de Oliveirab605c062013-03-05 17:30:28 +02001140
1141 pixman_region32_fini(&total_damage);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001142 pixman_region32_fini(&buffer_damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001143
Jason Ekstrande5512d42014-02-04 21:36:38 -06001144 draw_output_borders(output, border_damage);
John Kåre Alsaker44154502012-11-13 19:10:20 +01001145
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001146 pixman_region32_copy(&output->previous_damage, output_damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001147 wl_signal_emit(&output->frame_signal, output);
1148
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -06001149 if (gr->swap_buffers_with_damage) {
1150 pixman_region32_init(&buffer_damage);
1151 weston_transformed_region(output->width, output->height,
1152 output->transform,
1153 output->current_scale,
1154 output_damage, &buffer_damage);
1155
1156 if (output_has_borders(output)) {
1157 pixman_region32_translate(&buffer_damage,
1158 go->borders[GL_RENDERER_BORDER_LEFT].width,
1159 go->borders[GL_RENDERER_BORDER_TOP].height);
1160 output_get_border_damage(output, go->border_status,
1161 &buffer_damage);
1162 }
1163
1164 rects = pixman_region32_rectangles(&buffer_damage, &nrects);
1165 egl_damage = malloc(nrects * 4 * sizeof(EGLint));
1166
1167 buffer_height = go->borders[GL_RENDERER_BORDER_TOP].height +
1168 output->current_mode->height +
1169 go->borders[GL_RENDERER_BORDER_BOTTOM].height;
1170
1171 d = egl_damage;
1172 for (i = 0; i < nrects; ++i) {
1173 *d++ = rects[i].x1;
1174 *d++ = buffer_height - rects[i].y2;
1175 *d++ = rects[i].x2 - rects[i].x1;
1176 *d++ = rects[i].y2 - rects[i].y1;
1177 }
1178 ret = gr->swap_buffers_with_damage(gr->egl_display,
1179 go->egl_surface,
1180 egl_damage, nrects);
1181 free(egl_damage);
1182 pixman_region32_fini(&buffer_damage);
1183 } else {
1184 ret = eglSwapBuffers(gr->egl_display, go->egl_surface);
1185 }
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -06001186
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001187 if (ret == EGL_FALSE && !errored) {
1188 errored = 1;
1189 weston_log("Failed in eglSwapBuffers.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02001190 gl_renderer_print_egl_error_state();
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001191 }
1192
Jason Ekstrande5512d42014-02-04 21:36:38 -06001193 go->border_status = BORDER_STATUS_CLEAN;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001194}
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001195
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001196static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001197gl_renderer_read_pixels(struct weston_output *output,
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001198 pixman_format_code_t format, void *pixels,
1199 uint32_t x, uint32_t y,
1200 uint32_t width, uint32_t height)
1201{
1202 GLenum gl_format;
Jason Ekstrand701f6362014-04-02 19:53:59 -05001203 struct gl_output_state *go = get_output_state(output);
1204
1205 x += go->borders[GL_RENDERER_BORDER_LEFT].width;
1206 y += go->borders[GL_RENDERER_BORDER_BOTTOM].height;
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001207
1208 switch (format) {
1209 case PIXMAN_a8r8g8b8:
1210 gl_format = GL_BGRA_EXT;
1211 break;
1212 case PIXMAN_a8b8g8r8:
1213 gl_format = GL_RGBA;
1214 break;
1215 default:
1216 return -1;
1217 }
1218
1219 if (use_output(output) < 0)
1220 return -1;
1221
1222 glPixelStorei(GL_PACK_ALIGNMENT, 1);
1223 glReadPixels(x, y, width, height, gl_format,
1224 GL_UNSIGNED_BYTE, pixels);
1225
1226 return 0;
1227}
1228
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001229static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001230gl_renderer_flush_damage(struct weston_surface *surface)
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001231{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001232 struct gl_renderer *gr = get_renderer(surface->compositor);
1233 struct gl_surface_state *gs = get_surface_state(surface);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001234 struct weston_buffer *buffer = gs->buffer_ref.buffer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001235 struct weston_view *view;
Derek Foreman97746792015-11-18 16:32:28 -06001236 bool texture_used;
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001237 pixman_box32_t *rectangles;
1238 void *data;
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001239 int i, j, n;
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001240
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001241 pixman_region32_union(&gs->texture_damage,
1242 &gs->texture_damage, &surface->damage);
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001243
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001244 if (!buffer)
1245 return;
1246
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001247 /* Avoid upload, if the texture won't be used this time.
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001248 * We still accumulate the damage in texture_damage, and
1249 * hold the reference to the buffer, in case the surface
1250 * migrates back to the primary plane.
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001251 */
Derek Foreman97746792015-11-18 16:32:28 -06001252 texture_used = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001253 wl_list_for_each(view, &surface->views, surface_link) {
1254 if (view->plane == &surface->compositor->primary_plane) {
Derek Foreman97746792015-11-18 16:32:28 -06001255 texture_used = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001256 break;
1257 }
1258 }
1259 if (!texture_used)
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001260 return;
1261
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001262 if (!pixman_region32_not_empty(&gs->texture_damage) &&
1263 !gs->needs_full_upload)
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001264 goto done;
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001265
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001266 data = wl_shm_buffer_get_data(buffer->shm_buffer);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001267
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001268 if (!gr->has_unpack_subimage) {
Neil Robertse5051712013-11-13 15:44:06 +00001269 wl_shm_buffer_begin_access(buffer->shm_buffer);
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001270 for (j = 0; j < gs->num_textures; j++) {
1271 glBindTexture(GL_TEXTURE_2D, gs->textures[j]);
1272 glTexImage2D(GL_TEXTURE_2D, 0,
Vincent Abriou00a03d22016-10-05 14:54:35 +02001273 gs->gl_format[j],
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001274 gs->pitch / gs->hvsub[j],
1275 buffer->height / gs->hvsub[j],
1276 0,
Vincent Abriou00a03d22016-10-05 14:54:35 +02001277 gs->gl_format[j],
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001278 gs->gl_pixel_type,
1279 data + gs->offset[j]);
1280 }
Neil Robertse5051712013-11-13 15:44:06 +00001281 wl_shm_buffer_end_access(buffer->shm_buffer);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001282
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001283 goto done;
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001284 }
1285
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -07001286 glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, gs->pitch);
Ander Conselvan de Oliveira6be5f432013-06-07 16:52:45 +03001287
1288 if (gs->needs_full_upload) {
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -07001289 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, 0);
1290 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, 0);
Neil Robertse5051712013-11-13 15:44:06 +00001291 wl_shm_buffer_begin_access(buffer->shm_buffer);
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001292 for (j = 0; j < gs->num_textures; j++) {
1293 glBindTexture(GL_TEXTURE_2D, gs->textures[j]);
1294 glTexImage2D(GL_TEXTURE_2D, 0,
Vincent Abriou00a03d22016-10-05 14:54:35 +02001295 gs->gl_format[j],
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001296 gs->pitch / gs->hvsub[j],
1297 buffer->height / gs->hvsub[j],
1298 0,
Vincent Abriou00a03d22016-10-05 14:54:35 +02001299 gs->gl_format[j],
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001300 gs->gl_pixel_type,
1301 data + gs->offset[j]);
1302 }
Neil Robertse5051712013-11-13 15:44:06 +00001303 wl_shm_buffer_end_access(buffer->shm_buffer);
Ander Conselvan de Oliveira6be5f432013-06-07 16:52:45 +03001304 goto done;
1305 }
1306
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001307 rectangles = pixman_region32_rectangles(&gs->texture_damage, &n);
Neil Robertse5051712013-11-13 15:44:06 +00001308 wl_shm_buffer_begin_access(buffer->shm_buffer);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001309 for (i = 0; i < n; i++) {
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001310 pixman_box32_t r;
1311
1312 r = weston_surface_to_buffer_rect(surface, rectangles[i]);
1313
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -07001314 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, r.x1);
1315 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, r.y1);
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001316 for (j = 0; j < gs->num_textures; j++) {
1317 glBindTexture(GL_TEXTURE_2D, gs->textures[j]);
1318 glTexSubImage2D(GL_TEXTURE_2D, 0,
1319 r.x1 / gs->hvsub[j],
1320 r.y1 / gs->hvsub[j],
1321 (r.x2 - r.x1) / gs->hvsub[j],
1322 (r.y2 - r.y1) / gs->hvsub[j],
Vincent Abriou00a03d22016-10-05 14:54:35 +02001323 gs->gl_format[j],
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001324 gs->gl_pixel_type,
1325 data + gs->offset[j]);
1326 }
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001327 }
Neil Robertse5051712013-11-13 15:44:06 +00001328 wl_shm_buffer_end_access(buffer->shm_buffer);
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001329
1330done:
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001331 pixman_region32_fini(&gs->texture_damage);
1332 pixman_region32_init(&gs->texture_damage);
Derek Foreman4c11fe72015-11-18 16:32:27 -06001333 gs->needs_full_upload = false;
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001334
1335 weston_buffer_reference(&gs->buffer_ref, NULL);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001336}
1337
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001338static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001339ensure_textures(struct gl_surface_state *gs, int num_textures)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001340{
1341 int i;
1342
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001343 if (num_textures <= gs->num_textures)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001344 return;
1345
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001346 for (i = gs->num_textures; i < num_textures; i++) {
1347 glGenTextures(1, &gs->textures[i]);
1348 glBindTexture(gs->target, gs->textures[i]);
1349 glTexParameteri(gs->target,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001350 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001351 glTexParameteri(gs->target,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001352 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1353 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001354 gs->num_textures = num_textures;
1355 glBindTexture(gs->target, 0);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001356}
1357
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001358static void
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001359gl_renderer_attach_shm(struct weston_surface *es, struct weston_buffer *buffer,
1360 struct wl_shm_buffer *shm_buffer)
1361{
1362 struct weston_compositor *ec = es->compositor;
1363 struct gl_renderer *gr = get_renderer(ec);
1364 struct gl_surface_state *gs = get_surface_state(es);
Vincent Abriou00a03d22016-10-05 14:54:35 +02001365 GLenum gl_format[3] = {0, 0, 0};
1366 GLenum gl_pixel_type;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001367 int pitch;
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001368 int num_planes;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001369
1370 buffer->shm_buffer = shm_buffer;
1371 buffer->width = wl_shm_buffer_get_width(shm_buffer);
1372 buffer->height = wl_shm_buffer_get_height(shm_buffer);
1373
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001374 num_planes = 1;
1375 gs->offset[0] = 0;
1376 gs->hvsub[0] = 1;
1377
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001378 switch (wl_shm_buffer_get_format(shm_buffer)) {
1379 case WL_SHM_FORMAT_XRGB8888:
1380 gs->shader = &gr->texture_shader_rgbx;
1381 pitch = wl_shm_buffer_get_stride(shm_buffer) / 4;
Vincent Abriou00a03d22016-10-05 14:54:35 +02001382 gl_format[0] = GL_BGRA_EXT;
Neil Roberts4d085e72014-04-07 15:01:01 +01001383 gl_pixel_type = GL_UNSIGNED_BYTE;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001384 break;
1385 case WL_SHM_FORMAT_ARGB8888:
1386 gs->shader = &gr->texture_shader_rgba;
1387 pitch = wl_shm_buffer_get_stride(shm_buffer) / 4;
Vincent Abriou00a03d22016-10-05 14:54:35 +02001388 gl_format[0] = GL_BGRA_EXT;
Neil Roberts4d085e72014-04-07 15:01:01 +01001389 gl_pixel_type = GL_UNSIGNED_BYTE;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001390 break;
1391 case WL_SHM_FORMAT_RGB565:
1392 gs->shader = &gr->texture_shader_rgbx;
1393 pitch = wl_shm_buffer_get_stride(shm_buffer) / 2;
Vincent Abriou00a03d22016-10-05 14:54:35 +02001394 gl_format[0] = GL_RGB;
Neil Roberts4d085e72014-04-07 15:01:01 +01001395 gl_pixel_type = GL_UNSIGNED_SHORT_5_6_5;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001396 break;
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001397 case WL_SHM_FORMAT_YUV420:
1398 gs->shader = &gr->texture_shader_y_u_v;
1399 pitch = wl_shm_buffer_get_stride(shm_buffer);
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001400 gl_pixel_type = GL_UNSIGNED_BYTE;
1401 num_planes = 3;
1402 gs->offset[1] = gs->offset[0] + (pitch / gs->hvsub[0]) *
1403 (buffer->height / gs->hvsub[0]);
1404 gs->hvsub[1] = 2;
1405 gs->offset[2] = gs->offset[1] + (pitch / gs->hvsub[1]) *
1406 (buffer->height / gs->hvsub[1]);
1407 gs->hvsub[2] = 2;
Vincent Abriou00a03d22016-10-05 14:54:35 +02001408 if (gr->has_gl_texture_rg) {
1409 gl_format[0] = GL_R8_EXT;
1410 gl_format[1] = GL_R8_EXT;
1411 gl_format[2] = GL_R8_EXT;
1412 } else {
1413 gl_format[0] = GL_LUMINANCE;
1414 gl_format[1] = GL_LUMINANCE;
1415 gl_format[2] = GL_LUMINANCE;
1416 }
1417 break;
1418 case WL_SHM_FORMAT_NV12:
1419 gs->shader = &gr->texture_shader_y_xuxv;
1420 pitch = wl_shm_buffer_get_stride(shm_buffer);
1421 gl_pixel_type = GL_UNSIGNED_BYTE;
1422 num_planes = 2;
1423 gs->offset[1] = gs->offset[0] + (pitch / gs->hvsub[0]) * (buffer->height / gs->hvsub[0]);
1424 gs->hvsub[1] = 2;
1425 if (gr->has_gl_texture_rg) {
1426 gl_format[0] = GL_R8_EXT;
1427 gl_format[1] = GL_RG8_EXT;
1428 } else {
1429 gl_format[0] = GL_LUMINANCE;
1430 gl_format[1] = GL_LUMINANCE_ALPHA;
1431 }
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001432 break;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001433 default:
Neil Roberts4d085e72014-04-07 15:01:01 +01001434 weston_log("warning: unknown shm buffer format: %08x\n",
1435 wl_shm_buffer_get_format(shm_buffer));
1436 return;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001437 }
1438
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001439 /* Only allocate a texture if it doesn't match existing one.
1440 * If a switch from DRM allocated buffer to a SHM buffer is
1441 * happening, we need to allocate a new texture buffer. */
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001442 if (pitch != gs->pitch ||
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001443 buffer->height != gs->height ||
Vincent Abriou00a03d22016-10-05 14:54:35 +02001444 gl_format[0] != gs->gl_format[0] ||
1445 gl_format[1] != gs->gl_format[1] ||
1446 gl_format[2] != gs->gl_format[2] ||
Neil Roberts4d085e72014-04-07 15:01:01 +01001447 gl_pixel_type != gs->gl_pixel_type ||
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001448 gs->buffer_type != BUFFER_TYPE_SHM) {
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001449 gs->pitch = pitch;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001450 gs->height = buffer->height;
1451 gs->target = GL_TEXTURE_2D;
Vincent Abriou00a03d22016-10-05 14:54:35 +02001452 gs->gl_format[0] = gl_format[0];
1453 gs->gl_format[1] = gl_format[1];
1454 gs->gl_format[2] = gl_format[2];
Neil Roberts4d085e72014-04-07 15:01:01 +01001455 gs->gl_pixel_type = gl_pixel_type;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001456 gs->buffer_type = BUFFER_TYPE_SHM;
Derek Foreman4c11fe72015-11-18 16:32:27 -06001457 gs->needs_full_upload = true;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001458 gs->y_inverted = 1;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001459
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03001460 gs->surface = es;
1461
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001462 ensure_textures(gs, num_planes);
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001463 }
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001464}
1465
1466static void
1467gl_renderer_attach_egl(struct weston_surface *es, struct weston_buffer *buffer,
1468 uint32_t format)
1469{
1470 struct weston_compositor *ec = es->compositor;
1471 struct gl_renderer *gr = get_renderer(ec);
1472 struct gl_surface_state *gs = get_surface_state(es);
1473 EGLint attribs[3];
1474 int i, num_planes;
1475
1476 buffer->legacy_buffer = (struct wl_buffer *)buffer->resource;
1477 gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1478 EGL_WIDTH, &buffer->width);
1479 gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1480 EGL_HEIGHT, &buffer->height);
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001481 gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1482 EGL_WAYLAND_Y_INVERTED_WL, &buffer->y_inverted);
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001483
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +03001484 for (i = 0; i < gs->num_images; i++) {
1485 egl_image_unref(gs->images[i]);
1486 gs->images[i] = NULL;
1487 }
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001488 gs->num_images = 0;
1489 gs->target = GL_TEXTURE_2D;
1490 switch (format) {
1491 case EGL_TEXTURE_RGB:
1492 case EGL_TEXTURE_RGBA:
1493 default:
1494 num_planes = 1;
1495 gs->shader = &gr->texture_shader_rgba;
1496 break;
1497 case EGL_TEXTURE_EXTERNAL_WL:
1498 num_planes = 1;
1499 gs->target = GL_TEXTURE_EXTERNAL_OES;
1500 gs->shader = &gr->texture_shader_egl_external;
1501 break;
1502 case EGL_TEXTURE_Y_UV_WL:
1503 num_planes = 2;
1504 gs->shader = &gr->texture_shader_y_uv;
1505 break;
1506 case EGL_TEXTURE_Y_U_V_WL:
1507 num_planes = 3;
1508 gs->shader = &gr->texture_shader_y_u_v;
1509 break;
1510 case EGL_TEXTURE_Y_XUXV_WL:
1511 num_planes = 2;
1512 gs->shader = &gr->texture_shader_y_xuxv;
1513 break;
1514 }
1515
1516 ensure_textures(gs, num_planes);
1517 for (i = 0; i < num_planes; i++) {
1518 attribs[0] = EGL_WAYLAND_PLANE_WL;
1519 attribs[1] = i;
1520 attribs[2] = EGL_NONE;
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +03001521 gs->images[i] = egl_image_create(gr,
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001522 EGL_WAYLAND_BUFFER_WL,
1523 buffer->legacy_buffer,
1524 attribs);
1525 if (!gs->images[i]) {
1526 weston_log("failed to create img for plane %d\n", i);
1527 continue;
1528 }
1529 gs->num_images++;
1530
1531 glActiveTexture(GL_TEXTURE0 + i);
1532 glBindTexture(gs->target, gs->textures[i]);
1533 gr->image_target_texture_2d(gs->target,
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +03001534 gs->images[i]->image);
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001535 }
1536
1537 gs->pitch = buffer->width;
1538 gs->height = buffer->height;
1539 gs->buffer_type = BUFFER_TYPE_EGL;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001540 gs->y_inverted = buffer->y_inverted;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001541}
1542
1543static void
Pekka Paalanena3525802014-06-12 16:49:29 +03001544gl_renderer_destroy_dmabuf(struct linux_dmabuf_buffer *dmabuf)
1545{
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001546 struct dmabuf_image *image = dmabuf->user_data;
Pekka Paalanena3525802014-06-12 16:49:29 +03001547
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001548 dmabuf_image_destroy(image);
Pekka Paalanena3525802014-06-12 16:49:29 +03001549}
1550
1551static struct egl_image *
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001552import_simple_dmabuf(struct gl_renderer *gr,
1553 struct dmabuf_attributes *attributes)
Pekka Paalanena3525802014-06-12 16:49:29 +03001554{
1555 struct egl_image *image;
1556 EGLint attribs[30];
1557 int atti = 0;
1558
Pekka Paalanena3525802014-06-12 16:49:29 +03001559 /* This requires the Mesa commit in
1560 * Mesa 10.3 (08264e5dad4df448e7718e782ad9077902089a07) or
1561 * Mesa 10.2.7 (55d28925e6109a4afd61f109e845a8a51bd17652).
1562 * Otherwise Mesa closes the fd behind our back and re-importing
1563 * will fail.
1564 * https://bugs.freedesktop.org/show_bug.cgi?id=76188
1565 */
1566
1567 attribs[atti++] = EGL_WIDTH;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001568 attribs[atti++] = attributes->width;
Pekka Paalanena3525802014-06-12 16:49:29 +03001569 attribs[atti++] = EGL_HEIGHT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001570 attribs[atti++] = attributes->height;
Pekka Paalanena3525802014-06-12 16:49:29 +03001571 attribs[atti++] = EGL_LINUX_DRM_FOURCC_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001572 attribs[atti++] = attributes->format;
Pekka Paalanena3525802014-06-12 16:49:29 +03001573 /* XXX: Add modifier here when supported */
1574
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001575 if (attributes->n_planes > 0) {
Pekka Paalanena3525802014-06-12 16:49:29 +03001576 attribs[atti++] = EGL_DMA_BUF_PLANE0_FD_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001577 attribs[atti++] = attributes->fd[0];
Pekka Paalanena3525802014-06-12 16:49:29 +03001578 attribs[atti++] = EGL_DMA_BUF_PLANE0_OFFSET_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001579 attribs[atti++] = attributes->offset[0];
Pekka Paalanena3525802014-06-12 16:49:29 +03001580 attribs[atti++] = EGL_DMA_BUF_PLANE0_PITCH_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001581 attribs[atti++] = attributes->stride[0];
Pekka Paalanena3525802014-06-12 16:49:29 +03001582 }
1583
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001584 if (attributes->n_planes > 1) {
Pekka Paalanena3525802014-06-12 16:49:29 +03001585 attribs[atti++] = EGL_DMA_BUF_PLANE1_FD_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001586 attribs[atti++] = attributes->fd[1];
Pekka Paalanena3525802014-06-12 16:49:29 +03001587 attribs[atti++] = EGL_DMA_BUF_PLANE1_OFFSET_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001588 attribs[atti++] = attributes->offset[1];
Pekka Paalanena3525802014-06-12 16:49:29 +03001589 attribs[atti++] = EGL_DMA_BUF_PLANE1_PITCH_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001590 attribs[atti++] = attributes->stride[1];
Pekka Paalanena3525802014-06-12 16:49:29 +03001591 }
1592
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001593 if (attributes->n_planes > 2) {
Pekka Paalanena3525802014-06-12 16:49:29 +03001594 attribs[atti++] = EGL_DMA_BUF_PLANE2_FD_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001595 attribs[atti++] = attributes->fd[2];
Pekka Paalanena3525802014-06-12 16:49:29 +03001596 attribs[atti++] = EGL_DMA_BUF_PLANE2_OFFSET_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001597 attribs[atti++] = attributes->offset[2];
Pekka Paalanena3525802014-06-12 16:49:29 +03001598 attribs[atti++] = EGL_DMA_BUF_PLANE2_PITCH_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001599 attribs[atti++] = attributes->stride[2];
Pekka Paalanena3525802014-06-12 16:49:29 +03001600 }
1601
1602 attribs[atti++] = EGL_NONE;
1603
1604 image = egl_image_create(gr, EGL_LINUX_DMA_BUF_EXT, NULL,
1605 attribs);
1606
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001607 return image;
1608}
Pekka Paalanena3525802014-06-12 16:49:29 +03001609
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001610/* The kernel header drm_fourcc.h defines the DRM formats below. We duplicate
1611 * some of the definitions here so that building Weston won't require
1612 * bleeding-edge kernel headers.
1613 */
1614#ifndef DRM_FORMAT_R8
1615#define DRM_FORMAT_R8 fourcc_code('R', '8', ' ', ' ') /* [7:0] R */
1616#endif
1617
1618#ifndef DRM_FORMAT_GR88
1619#define DRM_FORMAT_GR88 fourcc_code('G', 'R', '8', '8') /* [15:0] G:R 8:8 little endian */
1620#endif
1621
1622struct yuv_format_descriptor yuv_formats[] = {
1623 {
1624 .format = DRM_FORMAT_YUYV,
1625 .input_planes = 1,
1626 .output_planes = 2,
1627 .texture_type = EGL_TEXTURE_Y_XUXV_WL,
1628 {{
1629 .width_divisor = 1,
1630 .height_divisor = 1,
1631 .format = DRM_FORMAT_GR88,
1632 .plane_index = 0
1633 }, {
1634 .width_divisor = 2,
1635 .height_divisor = 1,
1636 .format = DRM_FORMAT_ARGB8888,
1637 .plane_index = 0
1638 }}
1639 }, {
1640 .format = DRM_FORMAT_NV12,
1641 .input_planes = 2,
1642 .output_planes = 2,
1643 .texture_type = EGL_TEXTURE_Y_UV_WL,
1644 {{
1645 .width_divisor = 1,
1646 .height_divisor = 1,
1647 .format = DRM_FORMAT_R8,
1648 .plane_index = 0
1649 }, {
1650 .width_divisor = 2,
1651 .height_divisor = 2,
1652 .format = DRM_FORMAT_GR88,
1653 .plane_index = 1
1654 }}
1655 }, {
1656 .format = DRM_FORMAT_YUV420,
1657 .input_planes = 3,
1658 .output_planes = 3,
1659 .texture_type = EGL_TEXTURE_Y_U_V_WL,
1660 {{
1661 .width_divisor = 1,
1662 .height_divisor = 1,
1663 .format = DRM_FORMAT_R8,
1664 .plane_index = 0
1665 }, {
1666 .width_divisor = 2,
1667 .height_divisor = 2,
1668 .format = DRM_FORMAT_R8,
1669 .plane_index = 1
1670 }, {
1671 .width_divisor = 2,
1672 .height_divisor = 2,
1673 .format = DRM_FORMAT_R8,
1674 .plane_index = 2
1675 }}
Matthias Treydteaca3ffb2016-07-25 12:15:41 +02001676 }, {
1677 .format = DRM_FORMAT_YUV444,
1678 .input_planes = 3,
1679 .output_planes = 3,
1680 .texture_type = EGL_TEXTURE_Y_U_V_WL,
1681 {{
1682 .width_divisor = 1,
1683 .height_divisor = 1,
1684 .format = DRM_FORMAT_R8,
1685 .plane_index = 0
1686 }, {
1687 .width_divisor = 1,
1688 .height_divisor = 1,
1689 .format = DRM_FORMAT_R8,
1690 .plane_index = 1
1691 }, {
1692 .width_divisor = 1,
1693 .height_divisor = 1,
1694 .format = DRM_FORMAT_R8,
1695 .plane_index = 2
1696 }}
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001697 }
1698};
1699
1700static struct egl_image *
1701import_dmabuf_single_plane(struct gl_renderer *gr,
1702 const struct dmabuf_attributes *attributes,
1703 struct yuv_plane_descriptor *descriptor)
1704{
1705 struct dmabuf_attributes plane;
1706 struct egl_image *image;
1707 char fmt[4];
1708
1709 plane.width = attributes->width / descriptor->width_divisor;
1710 plane.height = attributes->height / descriptor->height_divisor;
1711 plane.format = descriptor->format;
1712 plane.n_planes = 1;
1713 plane.fd[0] = attributes->fd[descriptor->plane_index];
1714 plane.offset[0] = attributes->offset[descriptor->plane_index];
1715 plane.stride[0] = attributes->stride[descriptor->plane_index];
1716 plane.modifier[0] = attributes->modifier[descriptor->plane_index];
1717
1718 image = import_simple_dmabuf(gr, &plane);
1719 if (!image) {
1720 weston_log("Failed to import plane %d as %.4s\n",
1721 descriptor->plane_index,
1722 dump_format(descriptor->format, fmt));
1723 return NULL;
1724 }
1725
1726 return image;
1727}
1728
1729static bool
1730import_yuv_dmabuf(struct gl_renderer *gr,
1731 struct dmabuf_image *image)
1732{
1733 unsigned i;
1734 int j;
1735 int ret;
1736 struct yuv_format_descriptor *format = NULL;
1737 struct dmabuf_attributes *attributes = &image->dmabuf->attributes;
1738 char fmt[4];
1739
1740 for (i = 0; i < ARRAY_LENGTH(yuv_formats); ++i) {
1741 if (yuv_formats[i].format == attributes->format) {
1742 format = &yuv_formats[i];
1743 break;
1744 }
1745 }
1746
1747 if (!format) {
1748 weston_log("Error during import, and no known conversion for format "
1749 "%.4s in the renderer",
1750 dump_format(attributes->format, fmt));
1751 return false;
1752 }
1753
1754 if (attributes->n_planes != format->input_planes) {
1755 weston_log("%.4s dmabuf must contain %d plane%s (%d provided)",
1756 dump_format(format->format, fmt),
1757 format->input_planes,
1758 (format->input_planes > 1) ? "s" : "",
1759 attributes->n_planes);
1760 return false;
1761 }
1762
1763 for (j = 0; j < format->output_planes; ++j) {
1764 image->images[j] = import_dmabuf_single_plane(gr, attributes,
1765 &format->plane[j]);
1766 if (!image->images[j]) {
1767 while (j) {
1768 ret = egl_image_unref(image->images[--j]);
1769 assert(ret == 0);
1770 }
1771 return false;
1772 }
1773 }
1774
1775 image->num_images = format->output_planes;
1776
1777 switch (format->texture_type) {
1778 case EGL_TEXTURE_Y_XUXV_WL:
1779 image->shader = &gr->texture_shader_y_xuxv;
1780 break;
1781 case EGL_TEXTURE_Y_UV_WL:
1782 image->shader = &gr->texture_shader_y_uv;
1783 break;
1784 case EGL_TEXTURE_Y_U_V_WL:
1785 image->shader = &gr->texture_shader_y_u_v;
1786 break;
1787 default:
1788 assert(false);
1789 }
1790
1791 return true;
1792}
1793
1794static GLenum
1795choose_texture_target(struct dmabuf_attributes *attributes)
1796{
1797 if (attributes->n_planes > 1)
1798 return GL_TEXTURE_EXTERNAL_OES;
1799
1800 switch (attributes->format & ~DRM_FORMAT_BIG_ENDIAN) {
1801 case DRM_FORMAT_YUYV:
1802 case DRM_FORMAT_YVYU:
1803 case DRM_FORMAT_UYVY:
1804 case DRM_FORMAT_VYUY:
1805 case DRM_FORMAT_AYUV:
1806 return GL_TEXTURE_EXTERNAL_OES;
1807 default:
1808 return GL_TEXTURE_2D;
1809 }
1810}
1811
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001812static struct dmabuf_image *
1813import_dmabuf(struct gl_renderer *gr,
1814 struct linux_dmabuf_buffer *dmabuf)
1815{
1816 struct egl_image *egl_image;
1817 struct dmabuf_image *image;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001818
1819 image = dmabuf_image_create();
Pekka Paalanena3525802014-06-12 16:49:29 +03001820 image->dmabuf = dmabuf;
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001821
1822 egl_image = import_simple_dmabuf(gr, &dmabuf->attributes);
1823 if (egl_image) {
1824 image->num_images = 1;
1825 image->images[0] = egl_image;
1826 image->import_type = IMPORT_TYPE_DIRECT;
1827 image->target = choose_texture_target(&dmabuf->attributes);
1828
1829 switch (image->target) {
1830 case GL_TEXTURE_2D:
1831 image->shader = &gr->texture_shader_rgba;
1832 break;
1833 default:
1834 image->shader = &gr->texture_shader_egl_external;
1835 }
1836 } else {
1837 if (!import_yuv_dmabuf(gr, image)) {
1838 dmabuf_image_destroy(image);
1839 return NULL;
1840 }
1841 image->import_type = IMPORT_TYPE_GL_CONVERSION;
1842 image->target = GL_TEXTURE_2D;
1843 }
Pekka Paalanena3525802014-06-12 16:49:29 +03001844
1845 return image;
1846}
1847
1848static bool
1849gl_renderer_import_dmabuf(struct weston_compositor *ec,
1850 struct linux_dmabuf_buffer *dmabuf)
1851{
1852 struct gl_renderer *gr = get_renderer(ec);
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001853 struct dmabuf_image *image;
Pekka Paalanena3525802014-06-12 16:49:29 +03001854 int i;
1855
1856 assert(gr->has_dmabuf_import);
1857
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001858 for (i = 0; i < dmabuf->attributes.n_planes; i++) {
Pekka Paalanena3525802014-06-12 16:49:29 +03001859 /* EGL import does not have modifiers */
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001860 if (dmabuf->attributes.modifier[i] != 0)
Pekka Paalanena3525802014-06-12 16:49:29 +03001861 return false;
1862 }
1863
1864 /* reject all flags we do not recognize or handle */
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001865 if (dmabuf->attributes.flags & ~ZWP_LINUX_BUFFER_PARAMS_V1_FLAGS_Y_INVERT)
Pekka Paalanena3525802014-06-12 16:49:29 +03001866 return false;
1867
1868 image = import_dmabuf(gr, dmabuf);
1869 if (!image)
1870 return false;
1871
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001872 wl_list_insert(&gr->dmabuf_images, &image->link);
1873 linux_dmabuf_buffer_set_user_data(dmabuf, image,
1874 gl_renderer_destroy_dmabuf);
Pekka Paalanena3525802014-06-12 16:49:29 +03001875
1876 return true;
1877}
1878
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001879static bool
1880import_known_dmabuf(struct gl_renderer *gr,
1881 struct dmabuf_image *image)
Pekka Paalanena3525802014-06-12 16:49:29 +03001882{
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001883 switch (image->import_type) {
1884 case IMPORT_TYPE_DIRECT:
1885 image->images[0] = import_simple_dmabuf(gr, &image->dmabuf->attributes);
1886 if (!image->images[0])
1887 return false;
1888 break;
Pekka Paalanena3525802014-06-12 16:49:29 +03001889
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001890 case IMPORT_TYPE_GL_CONVERSION:
1891 if (!import_yuv_dmabuf(gr, image))
1892 return false;
1893 break;
1894
Pekka Paalanena3525802014-06-12 16:49:29 +03001895 default:
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001896 weston_log("Invalid import type for dmabuf\n");
1897 return false;
Pekka Paalanena3525802014-06-12 16:49:29 +03001898 }
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001899
1900 return true;
Pekka Paalanena3525802014-06-12 16:49:29 +03001901}
1902
1903static void
1904gl_renderer_attach_dmabuf(struct weston_surface *surface,
1905 struct weston_buffer *buffer,
1906 struct linux_dmabuf_buffer *dmabuf)
1907{
1908 struct gl_renderer *gr = get_renderer(surface->compositor);
1909 struct gl_surface_state *gs = get_surface_state(surface);
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001910 struct dmabuf_image *image;
Pekka Paalanena3525802014-06-12 16:49:29 +03001911 int i;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001912 int ret;
Pekka Paalanena3525802014-06-12 16:49:29 +03001913
1914 if (!gr->has_dmabuf_import) {
1915 linux_dmabuf_buffer_send_server_error(dmabuf,
1916 "EGL dmabuf import not supported");
1917 return;
1918 }
1919
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001920 buffer->width = dmabuf->attributes.width;
1921 buffer->height = dmabuf->attributes.height;
Pekka Paalanen319397e2016-07-04 16:25:16 +03001922
1923 /*
1924 * GL-renderer uses the OpenGL convention of texture coordinates, where
1925 * the origin is at bottom-left. Because dmabuf buffers have the origin
1926 * at top-left, we must invert the Y_INVERT flag to get the image right.
1927 */
Pekka Paalanena3525802014-06-12 16:49:29 +03001928 buffer->y_inverted =
Pekka Paalanen319397e2016-07-04 16:25:16 +03001929 !(dmabuf->attributes.flags & ZWP_LINUX_BUFFER_PARAMS_V1_FLAGS_Y_INVERT);
Pekka Paalanena3525802014-06-12 16:49:29 +03001930
1931 for (i = 0; i < gs->num_images; i++)
1932 egl_image_unref(gs->images[i]);
1933 gs->num_images = 0;
1934
Pekka Paalanena3525802014-06-12 16:49:29 +03001935 /*
1936 * We try to always hold an imported EGLImage from the dmabuf
1937 * to prevent the client from preventing re-imports. But, we also
1938 * need to re-import every time the contents may change because
1939 * GL driver's caching may need flushing.
1940 *
1941 * Here we release the cache reference which has to be final.
1942 */
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001943 image = linux_dmabuf_buffer_get_user_data(dmabuf);
Pekka Paalanena3525802014-06-12 16:49:29 +03001944
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001945 /* The dmabuf_image should have been created during the import */
1946 assert(image != NULL);
1947
1948 for (i = 0; i < image->num_images; ++i) {
1949 ret = egl_image_unref(image->images[i]);
Pekka Paalanena3525802014-06-12 16:49:29 +03001950 assert(ret == 0);
1951 }
1952
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001953 if (!import_known_dmabuf(gr, image)) {
1954 linux_dmabuf_buffer_send_server_error(dmabuf, "EGL dmabuf import failed");
1955 return;
Pekka Paalanena3525802014-06-12 16:49:29 +03001956 }
Pekka Paalanena3525802014-06-12 16:49:29 +03001957
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001958 gs->num_images = image->num_images;
1959 for (i = 0; i < gs->num_images; ++i)
1960 gs->images[i] = egl_image_ref(image->images[i]);
Pekka Paalanena3525802014-06-12 16:49:29 +03001961
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001962 gs->target = image->target;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001963 ensure_textures(gs, gs->num_images);
1964 for (i = 0; i < gs->num_images; ++i) {
1965 glActiveTexture(GL_TEXTURE0 + i);
1966 glBindTexture(gs->target, gs->textures[i]);
1967 gr->image_target_texture_2d(gs->target, gs->images[i]->image);
1968 }
Pekka Paalanena3525802014-06-12 16:49:29 +03001969
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001970 gs->shader = image->shader;
Pekka Paalanena3525802014-06-12 16:49:29 +03001971 gs->pitch = buffer->width;
1972 gs->height = buffer->height;
1973 gs->buffer_type = BUFFER_TYPE_EGL;
1974 gs->y_inverted = buffer->y_inverted;
1975}
1976
1977static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001978gl_renderer_attach(struct weston_surface *es, struct weston_buffer *buffer)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001979{
1980 struct weston_compositor *ec = es->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001981 struct gl_renderer *gr = get_renderer(ec);
1982 struct gl_surface_state *gs = get_surface_state(es);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001983 struct wl_shm_buffer *shm_buffer;
Pekka Paalanena3525802014-06-12 16:49:29 +03001984 struct linux_dmabuf_buffer *dmabuf;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001985 EGLint format;
1986 int i;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001987
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001988 weston_buffer_reference(&gs->buffer_ref, buffer);
1989
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001990 if (!buffer) {
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001991 for (i = 0; i < gs->num_images; i++) {
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +03001992 egl_image_unref(gs->images[i]);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001993 gs->images[i] = NULL;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001994 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001995 gs->num_images = 0;
1996 glDeleteTextures(gs->num_textures, gs->textures);
1997 gs->num_textures = 0;
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +03001998 gs->buffer_type = BUFFER_TYPE_NULL;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001999 gs->y_inverted = 1;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002000 return;
2001 }
2002
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002003 shm_buffer = wl_shm_buffer_get(buffer->resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002004
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03002005 if (shm_buffer)
2006 gl_renderer_attach_shm(es, buffer, shm_buffer);
Vincent Abriou9c526e02016-11-03 11:15:32 +01002007 else if (gr->has_bind_display &&
2008 gr->query_buffer(gr->egl_display, (void *)buffer->resource,
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03002009 EGL_TEXTURE_FORMAT, &format))
2010 gl_renderer_attach_egl(es, buffer, format);
Pekka Paalanena3525802014-06-12 16:49:29 +03002011 else if ((dmabuf = linux_dmabuf_buffer_get(buffer->resource)))
2012 gl_renderer_attach_dmabuf(es, buffer, dmabuf);
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03002013 else {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002014 weston_log("unhandled buffer type!\n");
Pekka Paalanenfb003d32012-12-04 15:58:13 +02002015 weston_buffer_reference(&gs->buffer_ref, NULL);
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +03002016 gs->buffer_type = BUFFER_TYPE_NULL;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002017 gs->y_inverted = 1;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002018 }
2019}
2020
Kristian Høgsberg42263852012-09-06 21:59:29 -04002021static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002022gl_renderer_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002023 float red, float green, float blue, float alpha)
2024{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002025 struct gl_surface_state *gs = get_surface_state(surface);
2026 struct gl_renderer *gr = get_renderer(surface->compositor);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002027
2028 gs->color[0] = red;
2029 gs->color[1] = green;
2030 gs->color[2] = blue;
2031 gs->color[3] = alpha;
Pekka Paalanenaeb917e2015-02-09 13:56:56 +02002032 gs->buffer_type = BUFFER_TYPE_SOLID;
2033 gs->pitch = 1;
2034 gs->height = 1;
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002035
John Kåre Alsaker40684142012-11-13 19:10:25 +01002036 gs->shader = &gr->solid_shader;
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002037}
2038
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002039static void
Pekka Paalaneneb35cbe2015-02-09 13:37:27 +02002040gl_renderer_surface_get_content_size(struct weston_surface *surface,
2041 int *width, int *height)
2042{
2043 struct gl_surface_state *gs = get_surface_state(surface);
2044
2045 if (gs->buffer_type == BUFFER_TYPE_NULL) {
2046 *width = 0;
2047 *height = 0;
2048 } else {
2049 *width = gs->pitch;
2050 *height = gs->height;
2051 }
2052}
2053
2054static uint32_t
2055pack_color(pixman_format_code_t format, float *c)
2056{
2057 uint8_t r = round(c[0] * 255.0f);
2058 uint8_t g = round(c[1] * 255.0f);
2059 uint8_t b = round(c[2] * 255.0f);
2060 uint8_t a = round(c[3] * 255.0f);
2061
2062 switch (format) {
2063 case PIXMAN_a8b8g8r8:
2064 return (a << 24) | (b << 16) | (g << 8) | r;
2065 default:
2066 assert(0);
2067 return 0;
2068 }
2069}
2070
2071static int
2072gl_renderer_surface_copy_content(struct weston_surface *surface,
2073 void *target, size_t size,
2074 int src_x, int src_y,
2075 int width, int height)
2076{
2077 static const GLfloat verts[4 * 2] = {
2078 0.0f, 0.0f,
2079 1.0f, 0.0f,
2080 1.0f, 1.0f,
2081 0.0f, 1.0f
2082 };
2083 static const GLfloat projmat_normal[16] = { /* transpose */
2084 2.0f, 0.0f, 0.0f, 0.0f,
2085 0.0f, 2.0f, 0.0f, 0.0f,
2086 0.0f, 0.0f, 1.0f, 0.0f,
2087 -1.0f, -1.0f, 0.0f, 1.0f
2088 };
2089 static const GLfloat projmat_yinvert[16] = { /* transpose */
2090 2.0f, 0.0f, 0.0f, 0.0f,
2091 0.0f, -2.0f, 0.0f, 0.0f,
2092 0.0f, 0.0f, 1.0f, 0.0f,
2093 -1.0f, 1.0f, 0.0f, 1.0f
2094 };
2095 const pixman_format_code_t format = PIXMAN_a8b8g8r8;
2096 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
2097 const GLenum gl_format = GL_RGBA; /* PIXMAN_a8b8g8r8 little-endian */
2098 struct gl_renderer *gr = get_renderer(surface->compositor);
2099 struct gl_surface_state *gs = get_surface_state(surface);
2100 int cw, ch;
2101 GLuint fbo;
2102 GLuint tex;
2103 GLenum status;
2104 const GLfloat *proj;
2105 int i;
2106
2107 gl_renderer_surface_get_content_size(surface, &cw, &ch);
2108
2109 switch (gs->buffer_type) {
2110 case BUFFER_TYPE_NULL:
2111 return -1;
2112 case BUFFER_TYPE_SOLID:
2113 *(uint32_t *)target = pack_color(format, gs->color);
2114 return 0;
2115 case BUFFER_TYPE_SHM:
2116 gl_renderer_flush_damage(surface);
2117 /* fall through */
2118 case BUFFER_TYPE_EGL:
2119 break;
2120 }
2121
2122 glGenTextures(1, &tex);
2123 glBindTexture(GL_TEXTURE_2D, tex);
2124 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, cw, ch,
2125 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
2126 glBindTexture(GL_TEXTURE_2D, 0);
2127
2128 glGenFramebuffers(1, &fbo);
2129 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
2130 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
2131 GL_TEXTURE_2D, tex, 0);
2132
2133 status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
2134 if (status != GL_FRAMEBUFFER_COMPLETE) {
2135 weston_log("%s: fbo error: %#x\n", __func__, status);
2136 glDeleteFramebuffers(1, &fbo);
2137 glDeleteTextures(1, &tex);
2138 return -1;
2139 }
2140
2141 glViewport(0, 0, cw, ch);
2142 glDisable(GL_BLEND);
2143 use_shader(gr, gs->shader);
2144 if (gs->y_inverted)
2145 proj = projmat_normal;
2146 else
2147 proj = projmat_yinvert;
2148
2149 glUniformMatrix4fv(gs->shader->proj_uniform, 1, GL_FALSE, proj);
2150 glUniform1f(gs->shader->alpha_uniform, 1.0f);
2151
2152 for (i = 0; i < gs->num_textures; i++) {
2153 glUniform1i(gs->shader->tex_uniforms[i], i);
2154
2155 glActiveTexture(GL_TEXTURE0 + i);
2156 glBindTexture(gs->target, gs->textures[i]);
2157 glTexParameteri(gs->target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2158 glTexParameteri(gs->target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2159 }
2160
2161 /* position: */
2162 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, verts);
2163 glEnableVertexAttribArray(0);
2164
2165 /* texcoord: */
2166 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, verts);
2167 glEnableVertexAttribArray(1);
2168
2169 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2170
2171 glDisableVertexAttribArray(1);
2172 glDisableVertexAttribArray(0);
2173
2174 glPixelStorei(GL_PACK_ALIGNMENT, bytespp);
2175 glReadPixels(src_x, src_y, width, height, gl_format,
2176 GL_UNSIGNED_BYTE, target);
2177
2178 glDeleteFramebuffers(1, &fbo);
2179 glDeleteTextures(1, &tex);
2180
2181 return 0;
2182}
2183
2184static void
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002185surface_state_destroy(struct gl_surface_state *gs, struct gl_renderer *gr)
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002186{
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002187 int i;
2188
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002189 wl_list_remove(&gs->surface_destroy_listener.link);
2190 wl_list_remove(&gs->renderer_destroy_listener.link);
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002191
2192 gs->surface->renderer_state = NULL;
2193
2194 glDeleteTextures(gs->num_textures, gs->textures);
2195
2196 for (i = 0; i < gs->num_images; i++)
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +03002197 egl_image_unref(gs->images[i]);
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002198
2199 weston_buffer_reference(&gs->buffer_ref, NULL);
2200 pixman_region32_fini(&gs->texture_damage);
2201 free(gs);
2202}
2203
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002204static void
2205surface_state_handle_surface_destroy(struct wl_listener *listener, void *data)
2206{
2207 struct gl_surface_state *gs;
2208 struct gl_renderer *gr;
2209
2210 gs = container_of(listener, struct gl_surface_state,
2211 surface_destroy_listener);
2212
2213 gr = get_renderer(gs->surface->compositor);
2214
2215 surface_state_destroy(gs, gr);
2216}
2217
2218static void
2219surface_state_handle_renderer_destroy(struct wl_listener *listener, void *data)
2220{
2221 struct gl_surface_state *gs;
2222 struct gl_renderer *gr;
2223
2224 gr = data;
2225
2226 gs = container_of(listener, struct gl_surface_state,
2227 renderer_destroy_listener);
2228
2229 surface_state_destroy(gs, gr);
2230}
2231
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002232static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002233gl_renderer_create_surface(struct weston_surface *surface)
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002234{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002235 struct gl_surface_state *gs;
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002236 struct gl_renderer *gr = get_renderer(surface->compositor);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002237
Bryce Harringtonde16d892014-11-20 22:21:57 -08002238 gs = zalloc(sizeof *gs);
2239 if (gs == NULL)
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002240 return -1;
2241
Pekka Paalanen68033ac2012-12-04 15:58:15 +02002242 /* A buffer is never attached to solid color surfaces, yet
2243 * they still go through texcoord computations. Do not divide
2244 * by zero there.
2245 */
2246 gs->pitch = 1;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002247 gs->y_inverted = 1;
Pekka Paalanen68033ac2012-12-04 15:58:15 +02002248
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002249 gs->surface = surface;
2250
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02002251 pixman_region32_init(&gs->texture_damage);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002252 surface->renderer_state = gs;
2253
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002254 gs->surface_destroy_listener.notify =
2255 surface_state_handle_surface_destroy;
2256 wl_signal_add(&surface->destroy_signal,
2257 &gs->surface_destroy_listener);
2258
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002259 gs->renderer_destroy_listener.notify =
2260 surface_state_handle_renderer_destroy;
2261 wl_signal_add(&gr->destroy_signal,
2262 &gs->renderer_destroy_listener);
2263
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02002264 if (surface->buffer_ref.buffer) {
2265 gl_renderer_attach(surface, surface->buffer_ref.buffer);
2266 gl_renderer_flush_damage(surface);
2267 }
2268
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002269 return 0;
2270}
2271
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002272static const char vertex_shader[] =
2273 "uniform mat4 proj;\n"
2274 "attribute vec2 position;\n"
2275 "attribute vec2 texcoord;\n"
2276 "varying vec2 v_texcoord;\n"
2277 "void main()\n"
2278 "{\n"
2279 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
2280 " v_texcoord = texcoord;\n"
2281 "}\n";
2282
2283/* Declare common fragment shader uniforms */
2284#define FRAGMENT_CONVERT_YUV \
2285 " y *= alpha;\n" \
2286 " u *= alpha;\n" \
2287 " v *= alpha;\n" \
2288 " gl_FragColor.r = y + 1.59602678 * v;\n" \
2289 " gl_FragColor.g = y - 0.39176229 * u - 0.81296764 * v;\n" \
2290 " gl_FragColor.b = y + 2.01723214 * u;\n" \
2291 " gl_FragColor.a = alpha;\n"
2292
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002293static const char fragment_debug[] =
2294 " gl_FragColor = vec4(0.0, 0.3, 0.0, 0.2) + gl_FragColor * 0.8;\n";
2295
2296static const char fragment_brace[] =
2297 "}\n";
2298
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002299static const char texture_fragment_shader_rgba[] =
2300 "precision mediump float;\n"
2301 "varying vec2 v_texcoord;\n"
2302 "uniform sampler2D tex;\n"
2303 "uniform float alpha;\n"
2304 "void main()\n"
2305 "{\n"
2306 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002307 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002308
2309static const char texture_fragment_shader_rgbx[] =
2310 "precision mediump float;\n"
2311 "varying vec2 v_texcoord;\n"
2312 "uniform sampler2D tex;\n"
2313 "uniform float alpha;\n"
2314 "void main()\n"
2315 "{\n"
2316 " gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb\n;"
2317 " gl_FragColor.a = alpha;\n"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002318 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002319
2320static const char texture_fragment_shader_egl_external[] =
2321 "#extension GL_OES_EGL_image_external : require\n"
2322 "precision mediump float;\n"
2323 "varying vec2 v_texcoord;\n"
2324 "uniform samplerExternalOES tex;\n"
2325 "uniform float alpha;\n"
2326 "void main()\n"
2327 "{\n"
2328 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002329 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002330
2331static const char texture_fragment_shader_y_uv[] =
2332 "precision mediump float;\n"
2333 "uniform sampler2D tex;\n"
2334 "uniform sampler2D tex1;\n"
2335 "varying vec2 v_texcoord;\n"
2336 "uniform float alpha;\n"
2337 "void main() {\n"
2338 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
2339 " float u = texture2D(tex1, v_texcoord).r - 0.5;\n"
2340 " float v = texture2D(tex1, v_texcoord).g - 0.5;\n"
2341 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002342 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002343
2344static const char texture_fragment_shader_y_u_v[] =
2345 "precision mediump float;\n"
2346 "uniform sampler2D tex;\n"
2347 "uniform sampler2D tex1;\n"
2348 "uniform sampler2D tex2;\n"
2349 "varying vec2 v_texcoord;\n"
2350 "uniform float alpha;\n"
2351 "void main() {\n"
2352 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
2353 " float u = texture2D(tex1, v_texcoord).x - 0.5;\n"
2354 " float v = texture2D(tex2, v_texcoord).x - 0.5;\n"
2355 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002356 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002357
2358static const char texture_fragment_shader_y_xuxv[] =
2359 "precision mediump float;\n"
2360 "uniform sampler2D tex;\n"
2361 "uniform sampler2D tex1;\n"
2362 "varying vec2 v_texcoord;\n"
2363 "uniform float alpha;\n"
2364 "void main() {\n"
2365 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
2366 " float u = texture2D(tex1, v_texcoord).g - 0.5;\n"
2367 " float v = texture2D(tex1, v_texcoord).a - 0.5;\n"
2368 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002369 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002370
2371static const char solid_fragment_shader[] =
2372 "precision mediump float;\n"
2373 "uniform vec4 color;\n"
2374 "uniform float alpha;\n"
2375 "void main()\n"
2376 "{\n"
2377 " gl_FragColor = alpha * color\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002378 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002379
2380static int
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002381compile_shader(GLenum type, int count, const char **sources)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002382{
2383 GLuint s;
2384 char msg[512];
2385 GLint status;
2386
2387 s = glCreateShader(type);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002388 glShaderSource(s, count, sources, NULL);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002389 glCompileShader(s);
2390 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
2391 if (!status) {
2392 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
2393 weston_log("shader info: %s\n", msg);
2394 return GL_NONE;
2395 }
2396
2397 return s;
2398}
2399
2400static int
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03002401shader_init(struct gl_shader *shader, struct gl_renderer *renderer,
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002402 const char *vertex_source, const char *fragment_source)
2403{
2404 char msg[512];
2405 GLint status;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002406 int count;
2407 const char *sources[3];
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002408
2409 shader->vertex_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002410 compile_shader(GL_VERTEX_SHADER, 1, &vertex_source);
2411
2412 if (renderer->fragment_shader_debug) {
2413 sources[0] = fragment_source;
2414 sources[1] = fragment_debug;
2415 sources[2] = fragment_brace;
2416 count = 3;
2417 } else {
2418 sources[0] = fragment_source;
2419 sources[1] = fragment_brace;
2420 count = 2;
2421 }
2422
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002423 shader->fragment_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002424 compile_shader(GL_FRAGMENT_SHADER, count, sources);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002425
2426 shader->program = glCreateProgram();
2427 glAttachShader(shader->program, shader->vertex_shader);
2428 glAttachShader(shader->program, shader->fragment_shader);
2429 glBindAttribLocation(shader->program, 0, "position");
2430 glBindAttribLocation(shader->program, 1, "texcoord");
2431
2432 glLinkProgram(shader->program);
2433 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
2434 if (!status) {
2435 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
2436 weston_log("link info: %s\n", msg);
2437 return -1;
2438 }
2439
2440 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
2441 shader->tex_uniforms[0] = glGetUniformLocation(shader->program, "tex");
2442 shader->tex_uniforms[1] = glGetUniformLocation(shader->program, "tex1");
2443 shader->tex_uniforms[2] = glGetUniformLocation(shader->program, "tex2");
2444 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
2445 shader->color_uniform = glGetUniformLocation(shader->program, "color");
2446
2447 return 0;
2448}
2449
2450static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002451shader_release(struct gl_shader *shader)
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002452{
2453 glDeleteShader(shader->vertex_shader);
2454 glDeleteShader(shader->fragment_shader);
2455 glDeleteProgram(shader->program);
2456
2457 shader->vertex_shader = 0;
2458 shader->fragment_shader = 0;
2459 shader->program = 0;
2460}
2461
2462static void
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002463log_extensions(const char *name, const char *extensions)
2464{
2465 const char *p, *end;
2466 int l;
2467 int len;
2468
2469 l = weston_log("%s:", name);
2470 p = extensions;
2471 while (*p) {
2472 end = strchrnul(p, ' ');
2473 len = end - p;
2474 if (l + len > 78)
2475 l = weston_log_continue("\n" STAMP_SPACE "%.*s",
2476 len, p);
2477 else
2478 l += weston_log_continue(" %.*s", len, p);
2479 for (p = end; isspace(*p); p++)
2480 ;
2481 }
2482 weston_log_continue("\n");
2483}
2484
2485static void
2486log_egl_gl_info(EGLDisplay egldpy)
2487{
2488 const char *str;
2489
2490 str = eglQueryString(egldpy, EGL_VERSION);
2491 weston_log("EGL version: %s\n", str ? str : "(null)");
2492
2493 str = eglQueryString(egldpy, EGL_VENDOR);
2494 weston_log("EGL vendor: %s\n", str ? str : "(null)");
2495
2496 str = eglQueryString(egldpy, EGL_CLIENT_APIS);
2497 weston_log("EGL client APIs: %s\n", str ? str : "(null)");
2498
2499 str = eglQueryString(egldpy, EGL_EXTENSIONS);
2500 log_extensions("EGL extensions", str ? str : "(null)");
2501
2502 str = (char *)glGetString(GL_VERSION);
2503 weston_log("GL version: %s\n", str ? str : "(null)");
2504
2505 str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
2506 weston_log("GLSL version: %s\n", str ? str : "(null)");
2507
2508 str = (char *)glGetString(GL_VENDOR);
2509 weston_log("GL vendor: %s\n", str ? str : "(null)");
2510
2511 str = (char *)glGetString(GL_RENDERER);
2512 weston_log("GL renderer: %s\n", str ? str : "(null)");
2513
2514 str = (char *)glGetString(GL_EXTENSIONS);
2515 log_extensions("GL extensions", str ? str : "(null)");
2516}
2517
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03002518static void
2519log_egl_config_info(EGLDisplay egldpy, EGLConfig eglconfig)
2520{
2521 EGLint r, g, b, a;
2522
2523 weston_log("Chosen EGL config details:\n");
2524
2525 weston_log_continue(STAMP_SPACE "RGBA bits");
2526 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_RED_SIZE, &r) &&
2527 eglGetConfigAttrib(egldpy, eglconfig, EGL_GREEN_SIZE, &g) &&
2528 eglGetConfigAttrib(egldpy, eglconfig, EGL_BLUE_SIZE, &b) &&
2529 eglGetConfigAttrib(egldpy, eglconfig, EGL_ALPHA_SIZE, &a))
2530 weston_log_continue(": %d %d %d %d\n", r, g, b, a);
2531 else
2532 weston_log_continue(" unknown\n");
2533
2534 weston_log_continue(STAMP_SPACE "swap interval range");
2535 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_MIN_SWAP_INTERVAL, &a) &&
2536 eglGetConfigAttrib(egldpy, eglconfig, EGL_MAX_SWAP_INTERVAL, &b))
2537 weston_log_continue(": %d - %d\n", a, b);
2538 else
2539 weston_log_continue(" unknown\n");
2540}
2541
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002542static int
Derek Foremane76f1852015-05-15 12:12:39 -05002543match_config_to_visual(EGLDisplay egl_display,
2544 EGLint visual_id,
2545 EGLConfig *configs,
2546 int count)
2547{
2548 int i;
2549
2550 for (i = 0; i < count; ++i) {
2551 EGLint id;
2552
2553 if (!eglGetConfigAttrib(egl_display,
2554 configs[i], EGL_NATIVE_VISUAL_ID,
2555 &id))
2556 continue;
2557
2558 if (id == visual_id)
2559 return i;
2560 }
2561
Derek Foremane76f1852015-05-15 12:12:39 -05002562 return -1;
2563}
2564
2565static int
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002566egl_choose_config(struct gl_renderer *gr, const EGLint *attribs,
Derek Foremane76f1852015-05-15 12:12:39 -05002567 const EGLint *visual_id, const int n_ids,
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002568 EGLConfig *config_out)
2569{
2570 EGLint count = 0;
2571 EGLint matched = 0;
2572 EGLConfig *configs;
Derek Foremane76f1852015-05-15 12:12:39 -05002573 int i, config_index = -1;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002574
Derek Foremana7e19912015-05-20 14:57:58 -05002575 if (!eglGetConfigs(gr->egl_display, NULL, 0, &count) || count < 1) {
2576 weston_log("No EGL configs to choose from.\n");
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002577 return -1;
Derek Foremana7e19912015-05-20 14:57:58 -05002578 }
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002579 configs = calloc(count, sizeof *configs);
2580 if (!configs)
2581 return -1;
2582
2583 if (!eglChooseConfig(gr->egl_display, attribs, configs,
Derek Foremana7e19912015-05-20 14:57:58 -05002584 count, &matched) || !matched) {
2585 weston_log("No EGL configs with appropriate attributes.\n");
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002586 goto out;
Derek Foremana7e19912015-05-20 14:57:58 -05002587 }
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002588
Miguel A. Vico684c9f42016-05-18 17:48:47 +02002589 if (!visual_id || n_ids == 0)
Derek Foremane76f1852015-05-15 12:12:39 -05002590 config_index = 0;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002591
Derek Foremane76f1852015-05-15 12:12:39 -05002592 for (i = 0; config_index == -1 && i < n_ids; i++)
2593 config_index = match_config_to_visual(gr->egl_display,
2594 visual_id[i],
2595 configs,
2596 matched);
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002597
Derek Foremane76f1852015-05-15 12:12:39 -05002598 if (config_index != -1)
2599 *config_out = configs[config_index];
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002600
2601out:
2602 free(configs);
Derek Foremane76f1852015-05-15 12:12:39 -05002603 if (config_index == -1)
2604 return -1;
2605
Derek Foremana7e19912015-05-20 14:57:58 -05002606 if (i > 1)
2607 weston_log("Unable to use first choice EGL config with id"
2608 " 0x%x, succeeded with alternate id 0x%x.\n",
2609 visual_id[0], visual_id[i - 1]);
Derek Foremane76f1852015-05-15 12:12:39 -05002610 return 0;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002611}
2612
John Kåre Alsaker44154502012-11-13 19:10:20 +01002613static void
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05002614gl_renderer_output_set_border(struct weston_output *output,
2615 enum gl_renderer_border_side side,
2616 int32_t width, int32_t height,
2617 int32_t tex_width, unsigned char *data)
2618{
2619 struct gl_output_state *go = get_output_state(output);
2620
Jason Ekstrande5512d42014-02-04 21:36:38 -06002621 if (go->borders[side].width != width ||
2622 go->borders[side].height != height)
2623 /* In this case, we have to blow everything and do a full
2624 * repaint. */
2625 go->border_status |= BORDER_SIZE_CHANGED | BORDER_ALL_DIRTY;
2626
2627 if (data == NULL) {
2628 width = 0;
2629 height = 0;
2630 }
2631
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05002632 go->borders[side].width = width;
2633 go->borders[side].height = height;
2634 go->borders[side].tex_width = tex_width;
2635 go->borders[side].data = data;
Jason Ekstrande5512d42014-02-04 21:36:38 -06002636 go->border_status |= 1 << side;
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05002637}
2638
John Kåre Alsaker94659272012-11-13 19:10:18 +01002639static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002640gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01002641
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002642static int
Miguel A. Vicoc095cde2016-05-18 17:43:00 +02002643gl_renderer_output_window_create(struct weston_output *output,
2644 EGLNativeWindowType window_for_legacy,
2645 void *window_for_platform,
Miguel A. Vico4057cd92016-05-18 17:44:22 +02002646 const EGLint *config_attribs,
Miguel A. Vicoc095cde2016-05-18 17:43:00 +02002647 const EGLint *visual_id,
2648 int n_ids)
John Kåre Alsaker94659272012-11-13 19:10:18 +01002649{
2650 struct weston_compositor *ec = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002651 struct gl_renderer *gr = get_renderer(ec);
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002652 struct gl_output_state *go;
2653 EGLConfig egl_config;
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002654 int i;
John Kåre Alsaker94659272012-11-13 19:10:18 +01002655
Miguel A. Vico4057cd92016-05-18 17:44:22 +02002656 if (egl_choose_config(gr, config_attribs, visual_id,
Derek Foremane76f1852015-05-15 12:12:39 -05002657 n_ids, &egl_config) == -1) {
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002658 weston_log("failed to choose EGL config for output\n");
2659 return -1;
2660 }
2661
2662 if (egl_config != gr->egl_config &&
2663 !gr->has_configless_context) {
2664 weston_log("attempted to use a different EGL config for an "
2665 "output but EGL_MESA_configless_context is not "
2666 "supported\n");
2667 return -1;
2668 }
2669
Bryce Harringtonde16d892014-11-20 22:21:57 -08002670 go = zalloc(sizeof *go);
2671 if (go == NULL)
John Kåre Alsaker94659272012-11-13 19:10:18 +01002672 return -1;
2673
Jonny Lamb671148f2015-03-20 15:26:52 +01002674 if (gr->create_platform_window) {
2675 go->egl_surface =
2676 gr->create_platform_window(gr->egl_display,
2677 egl_config,
2678 window_for_platform,
2679 NULL);
Jonny Lambf1ec5062015-03-24 13:12:05 +01002680 } else {
Jonny Lamb671148f2015-03-20 15:26:52 +01002681 go->egl_surface =
2682 eglCreateWindowSurface(gr->egl_display,
2683 egl_config,
2684 window_for_legacy, NULL);
Jonny Lambf1ec5062015-03-24 13:12:05 +01002685 }
John Kåre Alsaker94659272012-11-13 19:10:18 +01002686
2687 if (go->egl_surface == EGL_NO_SURFACE) {
2688 weston_log("failed to create egl surface\n");
2689 free(go);
2690 return -1;
2691 }
2692
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02002693 for (i = 0; i < BUFFER_DAMAGE_COUNT; i++)
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002694 pixman_region32_init(&go->buffer_damage[i]);
2695
John Kåre Alsaker94659272012-11-13 19:10:18 +01002696 output->renderer_state = go;
2697
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002698 log_egl_config_info(gr->egl_display, egl_config);
2699
John Kåre Alsaker94659272012-11-13 19:10:18 +01002700 return 0;
2701}
2702
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002703static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002704gl_renderer_output_destroy(struct weston_output *output)
John Kåre Alsaker94659272012-11-13 19:10:18 +01002705{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002706 struct gl_renderer *gr = get_renderer(output->compositor);
2707 struct gl_output_state *go = get_output_state(output);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002708 int i;
2709
2710 for (i = 0; i < 2; i++)
2711 pixman_region32_fini(&go->buffer_damage[i]);
John Kåre Alsaker94659272012-11-13 19:10:18 +01002712
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002713 eglDestroySurface(gr->egl_display, go->egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01002714
2715 free(go);
2716}
2717
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002718static EGLSurface
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002719gl_renderer_output_surface(struct weston_output *output)
John Kåre Alsaker94659272012-11-13 19:10:18 +01002720{
2721 return get_output_state(output)->egl_surface;
2722}
2723
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03002724static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002725gl_renderer_destroy(struct weston_compositor *ec)
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04002726{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002727 struct gl_renderer *gr = get_renderer(ec);
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00002728 struct dmabuf_image *image, *next;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002729
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002730 wl_signal_emit(&gr->destroy_signal, gr);
2731
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002732 if (gr->has_bind_display)
2733 gr->unbind_display(gr->egl_display, ec->wl_display);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002734
2735 /* Work around crash in egl_dri2.c's dri2_make_current() - when does this apply? */
2736 eglMakeCurrent(gr->egl_display,
2737 EGL_NO_SURFACE, EGL_NO_SURFACE,
2738 EGL_NO_CONTEXT);
2739
Pekka Paalanena3525802014-06-12 16:49:29 +03002740
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00002741 wl_list_for_each_safe(image, next, &gr->dmabuf_images, link)
2742 dmabuf_image_destroy(image);
Pekka Paalanena3525802014-06-12 16:49:29 +03002743
Armin Krezović28d240f2016-06-23 11:59:35 +02002744 if (gr->dummy_surface != EGL_NO_SURFACE)
2745 eglDestroySurface(gr->egl_display, gr->dummy_surface);
2746
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002747 eglTerminate(gr->egl_display);
2748 eglReleaseThread();
Scott Moreau976a0502013-03-07 10:15:17 -07002749
Armin Krezović36d699a2016-08-05 15:28:30 +02002750 wl_list_remove(&gr->output_destroy_listener.link);
2751
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -04002752 wl_array_release(&gr->vertices);
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -04002753 wl_array_release(&gr->vtxcnt);
2754
Mariusz Ceiercbb91582014-02-08 20:11:24 +01002755 if (gr->fragment_binding)
2756 weston_binding_destroy(gr->fragment_binding);
2757 if (gr->fan_binding)
2758 weston_binding_destroy(gr->fan_binding);
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03002759
Scott Moreau976a0502013-03-07 10:15:17 -07002760 free(gr);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002761}
2762
Pekka Paalanen8b69d032015-04-08 17:02:22 +03002763static void
2764renderer_setup_egl_client_extensions(struct gl_renderer *gr)
2765{
2766 const char *extensions;
2767
2768 extensions = eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS);
2769 if (!extensions) {
2770 weston_log("Retrieving EGL client extension string failed.\n");
2771 return;
2772 }
2773
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002774 if (weston_check_egl_extension(extensions, "EGL_EXT_platform_base"))
Pekka Paalanen8b69d032015-04-08 17:02:22 +03002775 gr->create_platform_window =
2776 (void *) eglGetProcAddress("eglCreatePlatformWindowSurfaceEXT");
2777 else
2778 weston_log("warning: EGL_EXT_platform_base not supported.\n");
2779}
2780
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002781static int
Neil Robertsb7f85332014-03-07 18:05:49 +00002782gl_renderer_setup_egl_extensions(struct weston_compositor *ec)
2783{
2784 struct gl_renderer *gr = get_renderer(ec);
2785 const char *extensions;
2786 EGLBoolean ret;
2787
2788 gr->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2789 gr->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2790 gr->bind_display =
2791 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
2792 gr->unbind_display =
2793 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
2794 gr->query_buffer =
2795 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
2796
2797 extensions =
2798 (const char *) eglQueryString(gr->egl_display, EGL_EXTENSIONS);
2799 if (!extensions) {
2800 weston_log("Retrieving EGL extension string failed.\n");
2801 return -1;
2802 }
2803
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002804 if (weston_check_egl_extension(extensions, "EGL_WL_bind_wayland_display"))
Neil Robertsb7f85332014-03-07 18:05:49 +00002805 gr->has_bind_display = 1;
2806 if (gr->has_bind_display) {
2807 ret = gr->bind_display(gr->egl_display, ec->wl_display);
2808 if (!ret)
2809 gr->has_bind_display = 0;
2810 }
2811
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002812 if (weston_check_egl_extension(extensions, "EGL_EXT_buffer_age"))
Neil Robertsb7f85332014-03-07 18:05:49 +00002813 gr->has_egl_buffer_age = 1;
2814 else
2815 weston_log("warning: EGL_EXT_buffer_age not supported. "
2816 "Performance could be affected.\n");
2817
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002818 if (weston_check_egl_extension(extensions, "EGL_EXT_swap_buffers_with_damage"))
Neil Robertsb7f85332014-03-07 18:05:49 +00002819 gr->swap_buffers_with_damage =
2820 (void *) eglGetProcAddress("eglSwapBuffersWithDamageEXT");
2821 else
2822 weston_log("warning: EGL_EXT_swap_buffers_with_damage not "
2823 "supported. Performance could be affected.\n");
Neil Robertsb7f85332014-03-07 18:05:49 +00002824
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002825 if (weston_check_egl_extension(extensions, "EGL_MESA_configless_context"))
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002826 gr->has_configless_context = 1;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002827
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002828 if (weston_check_egl_extension(extensions, "EGL_KHR_surfaceless_context"))
Armin Krezović28d240f2016-06-23 11:59:35 +02002829 gr->has_surfaceless_context = 1;
2830
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002831 if (weston_check_egl_extension(extensions, "EGL_EXT_image_dma_buf_import"))
Pekka Paalanena3525802014-06-12 16:49:29 +03002832 gr->has_dmabuf_import = 1;
Pekka Paalanena3525802014-06-12 16:49:29 +03002833
Vincent Abrioufdeefe42016-10-05 14:54:34 +02002834 if (weston_check_egl_extension(extensions, "GL_EXT_texture_rg"))
2835 gr->has_gl_texture_rg = 1;
2836
Pekka Paalanen8b69d032015-04-08 17:02:22 +03002837 renderer_setup_egl_client_extensions(gr);
2838
Neil Robertsb7f85332014-03-07 18:05:49 +00002839 return 0;
2840}
2841
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002842static const EGLint gl_renderer_opaque_attribs[] = {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002843 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
2844 EGL_RED_SIZE, 1,
2845 EGL_GREEN_SIZE, 1,
2846 EGL_BLUE_SIZE, 1,
2847 EGL_ALPHA_SIZE, 0,
2848 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
2849 EGL_NONE
2850};
2851
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002852static const EGLint gl_renderer_alpha_attribs[] = {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002853 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
2854 EGL_RED_SIZE, 1,
2855 EGL_GREEN_SIZE, 1,
2856 EGL_BLUE_SIZE, 1,
2857 EGL_ALPHA_SIZE, 1,
2858 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
2859 EGL_NONE
2860};
2861
Armin Krezović28d240f2016-06-23 11:59:35 +02002862
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002863/** Checks whether a platform EGL client extension is supported
2864 *
2865 * \param ec The weston compositor
2866 * \param extension_suffix The EGL client extension suffix
2867 * \return 1 if supported, 0 if using fallbacks, -1 unsupported
2868 *
2869 * This function checks whether a specific platform_* extension is supported
2870 * by EGL.
2871 *
2872 * The extension suffix should be the suffix of the platform extension (that
2873 * specifies a <platform> argument as defined in EGL_EXT_platform_base). For
2874 * example, passing "foo" will check whether either "EGL_KHR_platform_foo",
2875 * "EGL_EXT_platform_foo", or "EGL_MESA_platform_foo" is supported.
2876 *
2877 * The return value is 1:
2878 * - if the supplied EGL client extension is supported.
2879 * The return value is 0:
2880 * - if the platform_base client extension isn't supported so will
2881 * fallback to eglGetDisplay and friends.
2882 * The return value is -1:
2883 * - if the supplied EGL client extension is not supported.
2884 */
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002885static int
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002886gl_renderer_supports(struct weston_compositor *ec,
2887 const char *extension_suffix)
2888{
2889 static const char *extensions = NULL;
2890 char s[64];
2891
2892 if (!extensions) {
2893 extensions = (const char *) eglQueryString(
2894 EGL_NO_DISPLAY, EGL_EXTENSIONS);
2895
2896 if (!extensions)
2897 return 0;
2898
2899 log_extensions("EGL client extensions",
2900 extensions);
2901 }
2902
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002903 if (!weston_check_egl_extension(extensions, "EGL_EXT_platform_base"))
Pekka Paalanenf2824542015-04-08 17:02:21 +03002904 return 0;
2905
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002906 snprintf(s, sizeof s, "EGL_KHR_platform_%s", extension_suffix);
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002907 if (weston_check_egl_extension(extensions, s))
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002908 return 1;
2909
2910 snprintf(s, sizeof s, "EGL_EXT_platform_%s", extension_suffix);
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002911 if (weston_check_egl_extension(extensions, s))
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002912 return 1;
2913
2914 snprintf(s, sizeof s, "EGL_MESA_platform_%s", extension_suffix);
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002915 if (weston_check_egl_extension(extensions, s))
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002916 return 1;
2917
Pekka Paalanenf2824542015-04-08 17:02:21 +03002918 /* at this point we definitely have some platform extensions but
2919 * haven't found the supplied platform, so chances are it's
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002920 * not supported. */
2921
2922 return -1;
2923}
2924
Jonny Lamb74eed312015-03-24 13:12:04 +01002925static const char *
2926platform_to_extension(EGLenum platform)
2927{
2928 switch (platform) {
2929 case EGL_PLATFORM_GBM_KHR:
2930 return "gbm";
2931 case EGL_PLATFORM_WAYLAND_KHR:
2932 return "wayland";
2933 case EGL_PLATFORM_X11_KHR:
2934 return "x11";
2935 default:
2936 assert(0 && "bad EGL platform enum");
2937 }
2938}
2939
Armin Krezović36d699a2016-08-05 15:28:30 +02002940static void
2941output_handle_destroy(struct wl_listener *listener, void *data)
2942{
2943 struct gl_renderer *gr;
2944 struct weston_output *output = data;
2945
2946 gr = container_of(listener, struct gl_renderer,
2947 output_destroy_listener);
2948
2949 if (wl_list_empty(&output->compositor->output_list))
2950 eglMakeCurrent(gr->egl_display, gr->dummy_surface,
2951 gr->dummy_surface, gr->egl_context);
2952}
2953
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002954static int
Armin Krezović28d240f2016-06-23 11:59:35 +02002955gl_renderer_create_pbuffer_surface(struct gl_renderer *gr) {
2956 EGLConfig pbuffer_config;
2957
2958 static const EGLint pbuffer_config_attribs[] = {
2959 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
2960 EGL_RED_SIZE, 1,
2961 EGL_GREEN_SIZE, 1,
2962 EGL_BLUE_SIZE, 1,
2963 EGL_ALPHA_SIZE, 0,
2964 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
2965 EGL_NONE
2966 };
2967
2968 static const EGLint pbuffer_attribs[] = {
2969 EGL_WIDTH, 10,
2970 EGL_HEIGHT, 10,
2971 EGL_NONE
2972 };
2973
2974 if (egl_choose_config(gr, pbuffer_config_attribs, NULL, 0, &pbuffer_config) < 0) {
2975 weston_log("failed to choose EGL config for PbufferSurface");
2976 return -1;
2977 }
2978
2979 gr->dummy_surface = eglCreatePbufferSurface(gr->egl_display,
2980 pbuffer_config,
2981 pbuffer_attribs);
2982
2983 if (gr->dummy_surface == EGL_NO_SURFACE) {
2984 weston_log("failed to create PbufferSurface\n");
2985 return -1;
2986 }
2987
2988 return 0;
2989}
2990
2991static int
Miguel A. Vicodddc6702016-05-18 17:41:07 +02002992gl_renderer_display_create(struct weston_compositor *ec, EGLenum platform,
Miguel A. Vico4057cd92016-05-18 17:44:22 +02002993 void *native_window, const EGLint *config_attribs,
Derek Foremane76f1852015-05-15 12:12:39 -05002994 const EGLint *visual_id, int n_ids)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002995{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002996 struct gl_renderer *gr;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002997 EGLint major, minor;
Jonny Lamb74eed312015-03-24 13:12:04 +01002998 int supports = 0;
2999
3000 if (platform) {
3001 supports = gl_renderer_supports(
3002 ec, platform_to_extension(platform));
3003 if (supports < 0)
3004 return -1;
3005 }
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003006
Bryce Harringtonde16d892014-11-20 22:21:57 -08003007 gr = zalloc(sizeof *gr);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003008 if (gr == NULL)
3009 return -1;
3010
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003011 gr->base.read_pixels = gl_renderer_read_pixels;
3012 gr->base.repaint_output = gl_renderer_repaint_output;
3013 gr->base.flush_damage = gl_renderer_flush_damage;
3014 gr->base.attach = gl_renderer_attach;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003015 gr->base.surface_set_color = gl_renderer_surface_set_color;
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03003016 gr->base.destroy = gl_renderer_destroy;
Pekka Paalaneneb35cbe2015-02-09 13:37:27 +02003017 gr->base.surface_get_content_size =
3018 gl_renderer_surface_get_content_size;
3019 gr->base.surface_copy_content = gl_renderer_surface_copy_content;
Jonny Lamb74eed312015-03-24 13:12:04 +01003020 gr->egl_display = NULL;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003021
Jonny Lambf1ec5062015-03-24 13:12:05 +01003022 /* extension_suffix is supported */
Jonny Lamb74eed312015-03-24 13:12:04 +01003023 if (supports) {
3024 if (!get_platform_display) {
3025 get_platform_display = (void *) eglGetProcAddress(
3026 "eglGetPlatformDisplayEXT");
3027 }
Jonny Lamb70eba3f2015-03-20 15:26:50 +01003028
Jonny Lamb74eed312015-03-24 13:12:04 +01003029 /* also wrap this in the supports check because
3030 * eglGetProcAddress can return non-NULL and still not
3031 * support the feature at runtime, so ensure the
3032 * appropriate extension checks have been done. */
3033 if (get_platform_display && platform) {
3034 gr->egl_display = get_platform_display(platform,
3035 native_window,
3036 NULL);
3037 }
3038 }
Jonny Lamb74eed312015-03-24 13:12:04 +01003039
3040 if (!gr->egl_display) {
Jonny Lamb70eba3f2015-03-20 15:26:50 +01003041 weston_log("warning: either no EGL_EXT_platform_base "
3042 "support or specific platform support; "
3043 "falling back to eglGetDisplay.\n");
3044 gr->egl_display = eglGetDisplay(native_window);
Jonny Lamb70eba3f2015-03-20 15:26:50 +01003045 }
Jonny Lamb70eba3f2015-03-20 15:26:50 +01003046
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003047 if (gr->egl_display == EGL_NO_DISPLAY) {
3048 weston_log("failed to create display\n");
Derek Foreman066ca0c2015-06-11 12:14:45 -05003049 goto fail;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003050 }
3051
3052 if (!eglInitialize(gr->egl_display, &major, &minor)) {
3053 weston_log("failed to initialize display\n");
Derek Foreman066ca0c2015-06-11 12:14:45 -05003054 goto fail_with_error;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003055 }
3056
Miguel A. Vico4057cd92016-05-18 17:44:22 +02003057 if (egl_choose_config(gr, config_attribs, visual_id,
Derek Foremane76f1852015-05-15 12:12:39 -05003058 n_ids, &gr->egl_config) < 0) {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003059 weston_log("failed to choose EGL config\n");
Dawid Gajownik1a912a92015-08-21 00:20:54 -03003060 goto fail_terminate;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003061 }
3062
3063 ec->renderer = &gr->base;
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003064 ec->capabilities |= WESTON_CAP_ROTATION_ANY;
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003065 ec->capabilities |= WESTON_CAP_CAPTURE_YFLIP;
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +02003066 ec->capabilities |= WESTON_CAP_VIEW_CLIP_MASK;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003067
Neil Robertsb7f85332014-03-07 18:05:49 +00003068 if (gl_renderer_setup_egl_extensions(ec) < 0)
Derek Foreman066ca0c2015-06-11 12:14:45 -05003069 goto fail_with_error;
Neil Robertsb7f85332014-03-07 18:05:49 +00003070
Pekka Paalanena3525802014-06-12 16:49:29 +03003071 wl_list_init(&gr->dmabuf_images);
3072 if (gr->has_dmabuf_import)
3073 gr->base.import_dmabuf = gl_renderer_import_dmabuf;
3074
Armin Krezović28d240f2016-06-23 11:59:35 +02003075 if (gr->has_surfaceless_context) {
3076 weston_log("EGL_KHR_surfaceless_context available\n");
3077 gr->dummy_surface = EGL_NO_SURFACE;
3078 } else {
3079 weston_log("EGL_KHR_surfaceless_context unavailable. "
3080 "Trying PbufferSurface\n");
3081
3082 if (gl_renderer_create_pbuffer_surface(gr) < 0)
3083 goto fail_with_error;
3084 }
3085
Tomeu Vizoso12072b62013-08-06 20:05:55 +02003086 wl_display_add_shm_format(ec->wl_display, WL_SHM_FORMAT_RGB565);
Vincent Abrioufdeefe42016-10-05 14:54:34 +02003087 wl_display_add_shm_format(ec->wl_display, WL_SHM_FORMAT_YUV420);
Vincent Abriou00a03d22016-10-05 14:54:35 +02003088 wl_display_add_shm_format(ec->wl_display, WL_SHM_FORMAT_NV12);
Tomeu Vizoso12072b62013-08-06 20:05:55 +02003089
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03003090 wl_signal_init(&gr->destroy_signal);
3091
Armin Krezović28d240f2016-06-23 11:59:35 +02003092 if (gl_renderer_setup(ec, gr->dummy_surface) < 0) {
3093 if (gr->dummy_surface != EGL_NO_SURFACE)
3094 eglDestroySurface(gr->egl_display, gr->dummy_surface);
3095 goto fail_with_error;
3096 }
3097
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003098 return 0;
3099
Derek Foreman066ca0c2015-06-11 12:14:45 -05003100fail_with_error:
Pekka Paalanen326529f2012-11-27 12:25:25 +02003101 gl_renderer_print_egl_error_state();
Dawid Gajownik1a912a92015-08-21 00:20:54 -03003102fail_terminate:
3103 eglTerminate(gr->egl_display);
Derek Foreman066ca0c2015-06-11 12:14:45 -05003104fail:
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003105 free(gr);
3106 return -1;
3107}
3108
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003109static EGLDisplay
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003110gl_renderer_display(struct weston_compositor *ec)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003111{
3112 return get_renderer(ec)->egl_display;
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04003113}
3114
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003115static int
3116compile_shaders(struct weston_compositor *ec)
3117{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003118 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker40684142012-11-13 19:10:25 +01003119
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03003120 gr->texture_shader_rgba.vertex_source = vertex_shader;
3121 gr->texture_shader_rgba.fragment_source = texture_fragment_shader_rgba;
3122
3123 gr->texture_shader_rgbx.vertex_source = vertex_shader;
3124 gr->texture_shader_rgbx.fragment_source = texture_fragment_shader_rgbx;
3125
3126 gr->texture_shader_egl_external.vertex_source = vertex_shader;
3127 gr->texture_shader_egl_external.fragment_source =
3128 texture_fragment_shader_egl_external;
3129
3130 gr->texture_shader_y_uv.vertex_source = vertex_shader;
3131 gr->texture_shader_y_uv.fragment_source = texture_fragment_shader_y_uv;
3132
3133 gr->texture_shader_y_u_v.vertex_source = vertex_shader;
3134 gr->texture_shader_y_u_v.fragment_source =
3135 texture_fragment_shader_y_u_v;
3136
Ander Conselvan de Oliveira41a50ea2013-11-27 17:43:51 +02003137 gr->texture_shader_y_xuxv.vertex_source = vertex_shader;
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03003138 gr->texture_shader_y_xuxv.fragment_source =
3139 texture_fragment_shader_y_xuxv;
3140
3141 gr->solid_shader.vertex_source = vertex_shader;
3142 gr->solid_shader.fragment_source = solid_fragment_shader;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003143
3144 return 0;
3145}
3146
3147static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003148fragment_debug_binding(struct weston_keyboard *keyboard, uint32_t time,
3149 uint32_t key, void *data)
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003150{
3151 struct weston_compositor *ec = data;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003152 struct gl_renderer *gr = get_renderer(ec);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003153 struct weston_output *output;
3154
John Kåre Alsaker40684142012-11-13 19:10:25 +01003155 gr->fragment_shader_debug ^= 1;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003156
John Kåre Alsaker40684142012-11-13 19:10:25 +01003157 shader_release(&gr->texture_shader_rgba);
3158 shader_release(&gr->texture_shader_rgbx);
3159 shader_release(&gr->texture_shader_egl_external);
3160 shader_release(&gr->texture_shader_y_uv);
3161 shader_release(&gr->texture_shader_y_u_v);
3162 shader_release(&gr->texture_shader_y_xuxv);
3163 shader_release(&gr->solid_shader);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003164
Ander Conselvan de Oliveira03fb4ef2012-12-03 17:08:11 +02003165 /* Force use_shader() to call glUseProgram(), since we need to use
3166 * the recompiled version of the shader. */
3167 gr->current_shader = NULL;
3168
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003169 wl_list_for_each(output, &ec->output_list, link)
3170 weston_output_damage(output);
3171}
3172
Kristian Høgsberg8799d412013-05-07 10:50:09 -04003173static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003174fan_debug_repaint_binding(struct weston_keyboard *keyboard, uint32_t time,
3175 uint32_t key, void *data)
Kristian Høgsberg8799d412013-05-07 10:50:09 -04003176{
3177 struct weston_compositor *compositor = data;
3178 struct gl_renderer *gr = get_renderer(compositor);
3179
3180 gr->fan_debug = !gr->fan_debug;
3181 weston_compositor_damage_all(compositor);
3182}
3183
John Kåre Alsaker94659272012-11-13 19:10:18 +01003184static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003185gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003186{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003187 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003188 const char *extensions;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003189 EGLConfig context_config;
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003190 EGLBoolean ret;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003191
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003192 static const EGLint context_attribs[] = {
3193 EGL_CONTEXT_CLIENT_VERSION, 2,
3194 EGL_NONE
3195 };
3196
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003197 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
3198 weston_log("failed to bind EGL_OPENGL_ES_API\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02003199 gl_renderer_print_egl_error_state();
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003200 return -1;
3201 }
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03003202
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003203 context_config = gr->egl_config;
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03003204
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003205 if (gr->has_configless_context)
3206 context_config = EGL_NO_CONFIG_MESA;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003207
3208 gr->egl_context = eglCreateContext(gr->egl_display, context_config,
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003209 EGL_NO_CONTEXT, context_attribs);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003210 if (gr->egl_context == NULL) {
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003211 weston_log("failed to create context\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02003212 gl_renderer_print_egl_error_state();
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003213 return -1;
3214 }
3215
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003216 ret = eglMakeCurrent(gr->egl_display, egl_surface,
3217 egl_surface, gr->egl_context);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003218 if (ret == EGL_FALSE) {
3219 weston_log("Failed to make EGL context current.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02003220 gl_renderer_print_egl_error_state();
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003221 return -1;
3222 }
3223
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003224 log_egl_gl_info(gr->egl_display);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003225
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003226 gr->image_target_texture_2d =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003227 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003228
3229 extensions = (const char *) glGetString(GL_EXTENSIONS);
3230 if (!extensions) {
3231 weston_log("Retrieving GL extension string failed.\n");
3232 return -1;
3233 }
3234
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003235 if (!weston_check_egl_extension(extensions, "GL_EXT_texture_format_BGRA8888")) {
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003236 weston_log("GL_EXT_texture_format_BGRA8888 not available\n");
3237 return -1;
3238 }
3239
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003240 if (weston_check_egl_extension(extensions, "GL_EXT_read_format_bgra"))
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01003241 ec->read_format = PIXMAN_a8r8g8b8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003242 else
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01003243 ec->read_format = PIXMAN_a8b8g8r8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003244
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003245 if (weston_check_egl_extension(extensions, "GL_EXT_unpack_subimage"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003246 gr->has_unpack_subimage = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003247
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003248 if (weston_check_egl_extension(extensions, "GL_OES_EGL_image_external"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003249 gr->has_egl_image_external = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003250
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003251 glActiveTexture(GL_TEXTURE0);
3252
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003253 if (compile_shaders(ec))
3254 return -1;
3255
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03003256 gr->fragment_binding =
3257 weston_compositor_add_debug_binding(ec, KEY_S,
3258 fragment_debug_binding,
3259 ec);
3260 gr->fan_binding =
3261 weston_compositor_add_debug_binding(ec, KEY_F,
3262 fan_debug_repaint_binding,
3263 ec);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003264
Armin Krezović36d699a2016-08-05 15:28:30 +02003265 gr->output_destroy_listener.notify = output_handle_destroy;
3266 wl_signal_add(&ec->output_destroyed_signal,
3267 &gr->output_destroy_listener);
3268
Pekka Paalanen035a0322012-10-24 09:43:06 +03003269 weston_log("GL ES 2 renderer features:\n");
3270 weston_log_continue(STAMP_SPACE "read-back format: %s\n",
Pekka Paalanenfe4eacf2013-01-10 16:50:42 +02003271 ec->read_format == PIXMAN_a8r8g8b8 ? "BGRA" : "RGBA");
Pekka Paalanen035a0322012-10-24 09:43:06 +03003272 weston_log_continue(STAMP_SPACE "wl_shm sub-image to texture: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003273 gr->has_unpack_subimage ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03003274 weston_log_continue(STAMP_SPACE "EGL Wayland extension: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003275 gr->has_bind_display ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03003276
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003277
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003278 return 0;
3279}
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003280
3281WL_EXPORT struct gl_renderer_interface gl_renderer_interface = {
3282 .opaque_attribs = gl_renderer_opaque_attribs,
3283 .alpha_attribs = gl_renderer_alpha_attribs,
3284
Miguel A. Vicodddc6702016-05-18 17:41:07 +02003285 .display_create = gl_renderer_display_create,
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003286 .display = gl_renderer_display,
Miguel A. Vicoc095cde2016-05-18 17:43:00 +02003287 .output_window_create = gl_renderer_output_window_create,
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003288 .output_destroy = gl_renderer_output_destroy,
3289 .output_surface = gl_renderer_output_surface,
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05003290 .output_set_border = gl_renderer_output_set_border,
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003291 .print_egl_error_state = gl_renderer_print_egl_error_state
3292};