blob: be6b11eb1307fdc4004bc139b51e6bc47a14174b [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>
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -040033#include <stdlib.h>
Kristian Høgsberg25894fc2012-09-05 22:06:26 -040034#include <string.h>
35#include <ctype.h>
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +030036#include <float.h>
37#include <assert.h>
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +020038#include <linux/input.h>
Pekka Paalanena3525802014-06-12 16:49:29 +030039#include <drm_fourcc.h>
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -040040
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010041#include "gl-renderer.h"
Sam Spilsbury619859c2013-09-13 10:01:21 +080042#include "vertex-clipping.h"
Pekka Paalanena3525802014-06-12 16:49:29 +030043#include "linux-dmabuf.h"
Jonas Ådahl57e48f02015-11-17 16:00:28 +080044#include "linux-dmabuf-unstable-v1-server-protocol.h"
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010045
Jon Cruz35b2eaa2015-06-15 15:37:08 -070046#include "shared/helpers.h"
Emil Velikovf0c3a1c2016-07-04 15:34:18 +010047#include "shared/platform.h"
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010048#include "weston-egl-ext.h"
Kristian Høgsbergd7c17262012-09-05 21:54:15 -040049
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010050struct gl_shader {
John Kåre Alsaker40684142012-11-13 19:10:25 +010051 GLuint program;
52 GLuint vertex_shader, fragment_shader;
53 GLint proj_uniform;
54 GLint tex_uniforms[3];
55 GLint alpha_uniform;
56 GLint color_uniform;
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +030057 const char *vertex_source, *fragment_source;
John Kåre Alsaker40684142012-11-13 19:10:25 +010058};
59
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +020060#define BUFFER_DAMAGE_COUNT 2
61
Jason Ekstrande5512d42014-02-04 21:36:38 -060062enum gl_border_status {
63 BORDER_STATUS_CLEAN = 0,
64 BORDER_TOP_DIRTY = 1 << GL_RENDERER_BORDER_TOP,
65 BORDER_LEFT_DIRTY = 1 << GL_RENDERER_BORDER_LEFT,
66 BORDER_RIGHT_DIRTY = 1 << GL_RENDERER_BORDER_RIGHT,
67 BORDER_BOTTOM_DIRTY = 1 << GL_RENDERER_BORDER_BOTTOM,
68 BORDER_ALL_DIRTY = 0xf,
69 BORDER_SIZE_CHANGED = 0x10
70};
71
Jason Ekstrand0b61bf42013-10-27 22:24:54 -050072struct gl_border_image {
73 GLuint tex;
74 int32_t width, height;
75 int32_t tex_width;
Jason Ekstrand0b61bf42013-10-27 22:24:54 -050076 void *data;
77};
78
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010079struct gl_output_state {
John Kåre Alsaker94659272012-11-13 19:10:18 +010080 EGLSurface egl_surface;
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +020081 pixman_region32_t buffer_damage[BUFFER_DAMAGE_COUNT];
Derek Foreman4c582662014-10-09 18:39:44 -050082 int buffer_damage_index;
Jason Ekstrande5512d42014-02-04 21:36:38 -060083 enum gl_border_status border_damage[BUFFER_DAMAGE_COUNT];
Jason Ekstrand0b61bf42013-10-27 22:24:54 -050084 struct gl_border_image borders[4];
Jason Ekstrande5512d42014-02-04 21:36:38 -060085 enum gl_border_status border_status;
Jason Ekstrandfb23df72014-10-16 10:55:21 -050086
87 struct weston_matrix output_matrix;
John Kåre Alsaker94659272012-11-13 19:10:18 +010088};
89
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +030090enum buffer_type {
91 BUFFER_TYPE_NULL,
Pekka Paalanenaeb917e2015-02-09 13:56:56 +020092 BUFFER_TYPE_SOLID, /* internal solid color surfaces without a buffer */
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +030093 BUFFER_TYPE_SHM,
94 BUFFER_TYPE_EGL
95};
96
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +030097struct gl_renderer;
98
99struct egl_image {
100 struct gl_renderer *renderer;
101 EGLImageKHR image;
102 int refcount;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +0000103};
Pekka Paalanena3525802014-06-12 16:49:29 +0300104
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +0000105enum import_type {
106 IMPORT_TYPE_INVALID,
107 IMPORT_TYPE_DIRECT,
108 IMPORT_TYPE_GL_CONVERSION
109};
110
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +0000111struct dmabuf_image {
Pekka Paalanena3525802014-06-12 16:49:29 +0300112 struct linux_dmabuf_buffer *dmabuf;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +0000113 int num_images;
114 struct egl_image *images[3];
Pekka Paalanena3525802014-06-12 16:49:29 +0300115 struct wl_list link;
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +0000116
117 enum import_type import_type;
118 GLenum target;
119 struct gl_shader *shader;
120};
121
122struct yuv_plane_descriptor {
123 int width_divisor;
124 int height_divisor;
125 uint32_t format;
126 int plane_index;
127};
128
129struct yuv_format_descriptor {
130 uint32_t format;
131 int input_planes;
132 int output_planes;
133 int texture_type;
134 struct yuv_plane_descriptor plane[4];
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +0300135};
136
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100137struct gl_surface_state {
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100138 GLfloat color[4];
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100139 struct gl_shader *shader;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100140
141 GLuint textures[3];
142 int num_textures;
Derek Foreman4c11fe72015-11-18 16:32:27 -0600143 bool needs_full_upload;
Pekka Paalanen81ee3f52012-12-04 15:58:16 +0200144 pixman_region32_t texture_damage;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100145
Neil Roberts4d085e72014-04-07 15:01:01 +0100146 /* These are only used by SHM surfaces to detect when we need
147 * to do a full upload to specify a new internal texture
148 * format */
149 GLenum gl_format;
150 GLenum gl_pixel_type;
151
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +0300152 struct egl_image* images[3];
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100153 GLenum target;
154 int num_images;
Pekka Paalanenfb003d32012-12-04 15:58:13 +0200155
156 struct weston_buffer_reference buffer_ref;
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +0300157 enum buffer_type buffer_type;
Pekka Paalanen68033ac2012-12-04 15:58:15 +0200158 int pitch; /* in pixels */
Alexander Larsson4ea95522013-05-22 14:41:37 +0200159 int height; /* in pixels */
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +0400160 int y_inverted;
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300161
162 struct weston_surface *surface;
163
164 struct wl_listener surface_destroy_listener;
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +0300165 struct wl_listener renderer_destroy_listener;
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100166};
167
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100168struct gl_renderer {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100169 struct weston_renderer base;
170 int fragment_shader_debug;
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400171 int fan_debug;
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +0300172 struct weston_binding *fragment_binding;
173 struct weston_binding *fan_binding;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100174
175 EGLDisplay egl_display;
176 EGLContext egl_context;
177 EGLConfig egl_config;
John Kåre Alsaker44154502012-11-13 19:10:20 +0100178
Armin Krezović28d240f2016-06-23 11:59:35 +0200179 EGLSurface dummy_surface;
180
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400181 struct wl_array vertices;
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400182 struct wl_array vtxcnt;
183
John Kåre Alsaker320711d2012-11-13 19:10:27 +0100184 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
185 PFNEGLCREATEIMAGEKHRPROC create_image;
186 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -0600187 PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC swap_buffers_with_damage;
Jonny Lamb671148f2015-03-20 15:26:52 +0100188 PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC create_platform_window;
Jonny Lamb671148f2015-03-20 15:26:52 +0100189
John Kåre Alsaker320711d2012-11-13 19:10:27 +0100190 int has_unpack_subimage;
191
192 PFNEGLBINDWAYLANDDISPLAYWL bind_display;
193 PFNEGLUNBINDWAYLANDDISPLAYWL unbind_display;
194 PFNEGLQUERYWAYLANDBUFFERWL query_buffer;
195 int has_bind_display;
196
197 int has_egl_image_external;
198
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +0200199 int has_egl_buffer_age;
200
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000201 int has_configless_context;
202
Armin Krezović28d240f2016-06-23 11:59:35 +0200203 int has_surfaceless_context;
204
Pekka Paalanena3525802014-06-12 16:49:29 +0300205 int has_dmabuf_import;
206 struct wl_list dmabuf_images;
207
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100208 struct gl_shader texture_shader_rgba;
209 struct gl_shader texture_shader_rgbx;
210 struct gl_shader texture_shader_egl_external;
211 struct gl_shader texture_shader_y_uv;
212 struct gl_shader texture_shader_y_u_v;
213 struct gl_shader texture_shader_y_xuxv;
214 struct gl_shader invert_color_shader;
215 struct gl_shader solid_shader;
216 struct gl_shader *current_shader;
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +0300217
218 struct wl_signal destroy_signal;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100219};
John Kåre Alsaker94659272012-11-13 19:10:18 +0100220
Jonny Lamb70eba3f2015-03-20 15:26:50 +0100221static PFNEGLGETPLATFORMDISPLAYEXTPROC get_platform_display = NULL;
Jonny Lamb70eba3f2015-03-20 15:26:50 +0100222
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +0000223static inline const char *
224dump_format(uint32_t format, char out[4])
225{
226#if BYTE_ORDER == BIG_ENDIAN
227 format = __builtin_bswap32(format);
228#endif
229 memcpy(out, &format, 4);
230 return out;
231}
232
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100233static inline struct gl_output_state *
John Kåre Alsaker94659272012-11-13 19:10:18 +0100234get_output_state(struct weston_output *output)
235{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100236 return (struct gl_output_state *)output->renderer_state;
John Kåre Alsaker94659272012-11-13 19:10:18 +0100237}
238
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300239static int
240gl_renderer_create_surface(struct weston_surface *surface);
241
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100242static inline struct gl_surface_state *
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100243get_surface_state(struct weston_surface *surface)
244{
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300245 if (!surface->renderer_state)
246 gl_renderer_create_surface(surface);
247
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100248 return (struct gl_surface_state *)surface->renderer_state;
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100249}
250
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100251static inline struct gl_renderer *
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100252get_renderer(struct weston_compositor *ec)
253{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100254 return (struct gl_renderer *)ec->renderer;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100255}
256
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +0300257static struct egl_image*
258egl_image_create(struct gl_renderer *gr, EGLenum target,
259 EGLClientBuffer buffer, const EGLint *attribs)
260{
261 struct egl_image *img;
262
263 img = zalloc(sizeof *img);
264 img->renderer = gr;
265 img->refcount = 1;
266 img->image = gr->create_image(gr->egl_display, EGL_NO_CONTEXT,
267 target, buffer, attribs);
268
269 if (img->image == EGL_NO_IMAGE_KHR) {
270 free(img);
271 return NULL;
272 }
273
274 return img;
275}
276
277static struct egl_image*
278egl_image_ref(struct egl_image *image)
279{
280 image->refcount++;
281
282 return image;
283}
284
285static int
286egl_image_unref(struct egl_image *image)
287{
288 struct gl_renderer *gr = image->renderer;
289
290 assert(image->refcount > 0);
291
292 image->refcount--;
293 if (image->refcount > 0)
294 return image->refcount;
295
296 gr->destroy_image(gr->egl_display, image->image);
297 free(image);
298
299 return 0;
300}
301
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +0000302static struct dmabuf_image*
303dmabuf_image_create(void)
304{
305 struct dmabuf_image *img;
306
307 img = zalloc(sizeof *img);
308 wl_list_init(&img->link);
309
310 return img;
311}
312
313static void
314dmabuf_image_destroy(struct dmabuf_image *image)
315{
316 int i;
317
318 for (i = 0; i < image->num_images; ++i)
319 egl_image_unref(image->images[i]);
320
321 if (image->dmabuf)
322 linux_dmabuf_buffer_set_user_data(image->dmabuf, NULL, NULL);
323
324 wl_list_remove(&image->link);
325}
326
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400327static const char *
328egl_error_string(EGLint code)
329{
330#define MYERRCODE(x) case x: return #x;
331 switch (code) {
332 MYERRCODE(EGL_SUCCESS)
333 MYERRCODE(EGL_NOT_INITIALIZED)
334 MYERRCODE(EGL_BAD_ACCESS)
335 MYERRCODE(EGL_BAD_ALLOC)
336 MYERRCODE(EGL_BAD_ATTRIBUTE)
337 MYERRCODE(EGL_BAD_CONTEXT)
338 MYERRCODE(EGL_BAD_CONFIG)
339 MYERRCODE(EGL_BAD_CURRENT_SURFACE)
340 MYERRCODE(EGL_BAD_DISPLAY)
341 MYERRCODE(EGL_BAD_SURFACE)
342 MYERRCODE(EGL_BAD_MATCH)
343 MYERRCODE(EGL_BAD_PARAMETER)
344 MYERRCODE(EGL_BAD_NATIVE_PIXMAP)
345 MYERRCODE(EGL_BAD_NATIVE_WINDOW)
346 MYERRCODE(EGL_CONTEXT_LOST)
347 default:
348 return "unknown";
349 }
350#undef MYERRCODE
351}
352
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300353static void
Pekka Paalanen326529f2012-11-27 12:25:25 +0200354gl_renderer_print_egl_error_state(void)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400355{
356 EGLint code;
357
358 code = eglGetError();
359 weston_log("EGL error state: %s (0x%04lx)\n",
360 egl_error_string(code), (long)code);
361}
362
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400363#define max(a, b) (((a) > (b)) ? (a) : (b))
364#define min(a, b) (((a) > (b)) ? (b) : (a))
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400365
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300366/*
367 * Compute the boundary vertices of the intersection of the global coordinate
368 * aligned rectangle 'rect', and an arbitrary quadrilateral produced from
369 * 'surf_rect' when transformed from surface coordinates into global coordinates.
370 * The vertices are written to 'ex' and 'ey', and the return value is the
371 * number of vertices. Vertices are produced in clockwise winding order.
372 * Guarantees to produce either zero vertices, or 3-8 vertices with non-zero
373 * polygon area.
374 */
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400375static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500376calculate_edges(struct weston_view *ev, pixman_box32_t *rect,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400377 pixman_box32_t *surf_rect, GLfloat *ex, GLfloat *ey)
378{
Sam Spilsbury619859c2013-09-13 10:01:21 +0800379
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300380 struct clip_context ctx;
381 int i, n;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400382 GLfloat min_x, max_x, min_y, max_y;
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300383 struct polygon8 surf = {
384 { surf_rect->x1, surf_rect->x2, surf_rect->x2, surf_rect->x1 },
385 { surf_rect->y1, surf_rect->y1, surf_rect->y2, surf_rect->y2 },
386 4
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400387 };
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400388
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300389 ctx.clip.x1 = rect->x1;
390 ctx.clip.y1 = rect->y1;
391 ctx.clip.x2 = rect->x2;
392 ctx.clip.y2 = rect->y2;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400393
394 /* transform surface to screen space: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300395 for (i = 0; i < surf.n; i++)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500396 weston_view_to_global_float(ev, surf.x[i], surf.y[i],
397 &surf.x[i], &surf.y[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400398
399 /* find bounding box: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300400 min_x = max_x = surf.x[0];
401 min_y = max_y = surf.y[0];
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400402
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300403 for (i = 1; i < surf.n; i++) {
404 min_x = min(min_x, surf.x[i]);
405 max_x = max(max_x, surf.x[i]);
406 min_y = min(min_y, surf.y[i]);
407 max_y = max(max_y, surf.y[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400408 }
409
410 /* First, simple bounding box check to discard early transformed
411 * surface rects that do not intersect with the clip region:
412 */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300413 if ((min_x >= ctx.clip.x2) || (max_x <= ctx.clip.x1) ||
414 (min_y >= ctx.clip.y2) || (max_y <= ctx.clip.y1))
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400415 return 0;
416
417 /* Simple case, bounding box edges are parallel to surface edges,
418 * there will be only four edges. We just need to clip the surface
419 * vertices to the clip rect bounds:
420 */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500421 if (!ev->transform.enabled)
Sam Spilsbury619859c2013-09-13 10:01:21 +0800422 return clip_simple(&ctx, &surf, ex, ey);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400423
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300424 /* Transformed case: use a general polygon clipping algorithm to
425 * clip the surface rectangle with each side of 'rect'.
426 * The algorithm is Sutherland-Hodgman, as explained in
427 * http://www.codeguru.com/cpp/misc/misc/graphics/article.php/c8965/Polygon-Clipping.htm
428 * but without looking at any of that code.
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400429 */
Sam Spilsbury619859c2013-09-13 10:01:21 +0800430 n = clip_transformed(&ctx, &surf, ex, ey);
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300431
432 if (n < 3)
433 return 0;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400434
435 return n;
436}
437
Derek Foremanf8180982014-10-16 16:37:02 -0500438static bool
439merge_down(pixman_box32_t *a, pixman_box32_t *b, pixman_box32_t *merge)
440{
441 if (a->x1 == b->x1 && a->x2 == b->x2 && a->y1 == b->y2) {
442 merge->x1 = a->x1;
443 merge->x2 = a->x2;
444 merge->y1 = b->y1;
445 merge->y2 = a->y2;
446 return true;
447 }
448 return false;
449}
450
451static int
452compress_bands(pixman_box32_t *inrects, int nrects,
453 pixman_box32_t **outrects)
454{
455 bool merged;
456 pixman_box32_t *out, merge_rect;
457 int i, j, nout;
458
459 if (!nrects) {
460 *outrects = NULL;
461 return 0;
462 }
463
464 /* nrects is an upper bound - we're not too worried about
465 * allocating a little extra
466 */
467 out = malloc(sizeof(pixman_box32_t) * nrects);
468 out[0] = inrects[0];
469 nout = 1;
470 for (i = 1; i < nrects; i++) {
471 for (j = 0; j < nout; j++) {
472 merged = merge_down(&inrects[i], &out[j], &merge_rect);
473 if (merged) {
474 out[j] = merge_rect;
475 break;
476 }
477 }
478 if (!merged) {
479 out[nout] = inrects[i];
480 nout++;
481 }
482 }
483 *outrects = out;
484 return nout;
485}
486
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400487static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500488texture_region(struct weston_view *ev, pixman_region32_t *region,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400489 pixman_region32_t *surf_region)
490{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500491 struct gl_surface_state *gs = get_surface_state(ev->surface);
492 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400493 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400494 GLfloat *v, inv_width, inv_height;
495 unsigned int *vtxcnt, nvtx = 0;
496 pixman_box32_t *rects, *surf_rects;
Derek Foremanf8180982014-10-16 16:37:02 -0500497 pixman_box32_t *raw_rects;
498 int i, j, k, nrects, nsurf, raw_nrects;
499 bool used_band_compression;
500 raw_rects = pixman_region32_rectangles(region, &raw_nrects);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400501 surf_rects = pixman_region32_rectangles(surf_region, &nsurf);
502
Derek Foremanf8180982014-10-16 16:37:02 -0500503 if (raw_nrects < 4) {
504 used_band_compression = false;
505 nrects = raw_nrects;
506 rects = raw_rects;
507 } else {
508 nrects = compress_bands(raw_rects, raw_nrects, &rects);
509 used_band_compression = true;
510 }
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400511 /* worst case we can have 8 vertices per rect (ie. clipped into
512 * an octagon):
513 */
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400514 v = wl_array_add(&gr->vertices, nrects * nsurf * 8 * 4 * sizeof *v);
515 vtxcnt = wl_array_add(&gr->vtxcnt, nrects * nsurf * sizeof *vtxcnt);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400516
Pekka Paalanen68033ac2012-12-04 15:58:15 +0200517 inv_width = 1.0 / gs->pitch;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200518 inv_height = 1.0 / gs->height;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400519
520 for (i = 0; i < nrects; i++) {
521 pixman_box32_t *rect = &rects[i];
522 for (j = 0; j < nsurf; j++) {
523 pixman_box32_t *surf_rect = &surf_rects[j];
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200524 GLfloat sx, sy, bx, by;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400525 GLfloat ex[8], ey[8]; /* edge points in screen space */
526 int n;
527
528 /* The transformed surface, after clipping to the clip region,
529 * can have as many as eight sides, emitted as a triangle-fan.
530 * The first vertex in the triangle fan can be chosen arbitrarily,
531 * since the area is guaranteed to be convex.
532 *
533 * If a corner of the transformed surface falls outside of the
534 * clip region, instead of emitting one vertex for the corner
535 * of the surface, up to two are emitted for two corresponding
536 * intersection point(s) between the surface and the clip region.
537 *
538 * To do this, we first calculate the (up to eight) points that
539 * form the intersection of the clip rect and the transformed
540 * surface.
541 */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500542 n = calculate_edges(ev, rect, surf_rect, ex, ey);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400543 if (n < 3)
544 continue;
545
546 /* emit edge points: */
547 for (k = 0; k < n; k++) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500548 weston_view_from_global_float(ev, ex[k], ey[k],
549 &sx, &sy);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400550 /* position: */
551 *(v++) = ex[k];
552 *(v++) = ey[k];
553 /* texcoord: */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500554 weston_surface_to_buffer_float(ev->surface,
555 sx, sy,
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200556 &bx, &by);
557 *(v++) = bx * inv_width;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +0400558 if (gs->y_inverted) {
559 *(v++) = by * inv_height;
560 } else {
561 *(v++) = (gs->height - by) * inv_height;
562 }
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400563 }
564
565 vtxcnt[nvtx++] = n;
566 }
567 }
568
Derek Foremanf8180982014-10-16 16:37:02 -0500569 if (used_band_compression)
570 free(rects);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400571 return nvtx;
572}
573
574static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500575triangle_fan_debug(struct weston_view *view, int first, int count)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400576{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500577 struct weston_compositor *compositor = view->surface->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100578 struct gl_renderer *gr = get_renderer(compositor);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400579 int i;
580 GLushort *buffer;
581 GLushort *index;
582 int nelems;
583 static int color_idx = 0;
584 static const GLfloat color[][4] = {
585 { 1.0, 0.0, 0.0, 1.0 },
586 { 0.0, 1.0, 0.0, 1.0 },
587 { 0.0, 0.0, 1.0, 1.0 },
588 { 1.0, 1.0, 1.0, 1.0 },
589 };
590
591 nelems = (count - 1 + count - 2) * 2;
592
593 buffer = malloc(sizeof(GLushort) * nelems);
594 index = buffer;
595
596 for (i = 1; i < count; i++) {
597 *index++ = first;
598 *index++ = first + i;
599 }
600
601 for (i = 2; i < count; i++) {
602 *index++ = first + i - 1;
603 *index++ = first + i;
604 }
605
John Kåre Alsaker40684142012-11-13 19:10:25 +0100606 glUseProgram(gr->solid_shader.program);
607 glUniform4fv(gr->solid_shader.color_uniform, 1,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400608 color[color_idx++ % ARRAY_LENGTH(color)]);
609 glDrawElements(GL_LINES, nelems, GL_UNSIGNED_SHORT, buffer);
John Kåre Alsaker40684142012-11-13 19:10:25 +0100610 glUseProgram(gr->current_shader->program);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400611 free(buffer);
612}
613
614static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500615repaint_region(struct weston_view *ev, pixman_region32_t *region,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400616 pixman_region32_t *surf_region)
617{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500618 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400619 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400620 GLfloat *v;
621 unsigned int *vtxcnt;
622 int i, first, nfans;
623
624 /* The final region to be painted is the intersection of
625 * 'region' and 'surf_region'. However, 'region' is in the global
626 * coordinates, and 'surf_region' is in the surface-local
627 * coordinates. texture_region() will iterate over all pairs of
628 * rectangles from both regions, compute the intersection
629 * polygon for each pair, and store it as a triangle fan if
Bryce Harringtonea8fb942016-01-13 18:48:56 -0800630 * it has a non-zero area (at least 3 vertices, actually).
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400631 */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500632 nfans = texture_region(ev, region, surf_region);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400633
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400634 v = gr->vertices.data;
635 vtxcnt = gr->vtxcnt.data;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400636
637 /* position: */
638 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
639 glEnableVertexAttribArray(0);
640
641 /* texcoord: */
642 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
643 glEnableVertexAttribArray(1);
644
645 for (i = 0, first = 0; i < nfans; i++) {
646 glDrawArrays(GL_TRIANGLE_FAN, first, vtxcnt[i]);
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400647 if (gr->fan_debug)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500648 triangle_fan_debug(ev, first, vtxcnt[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400649 first += vtxcnt[i];
650 }
651
652 glDisableVertexAttribArray(1);
653 glDisableVertexAttribArray(0);
654
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400655 gr->vertices.size = 0;
656 gr->vtxcnt.size = 0;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400657}
658
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100659static int
660use_output(struct weston_output *output)
661{
662 static int errored;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100663 struct gl_output_state *go = get_output_state(output);
664 struct gl_renderer *gr = get_renderer(output->compositor);
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100665 EGLBoolean ret;
666
667 ret = eglMakeCurrent(gr->egl_display, go->egl_surface,
668 go->egl_surface, gr->egl_context);
669
670 if (ret == EGL_FALSE) {
671 if (errored)
672 return -1;
673 errored = 1;
674 weston_log("Failed to make EGL context current.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +0200675 gl_renderer_print_egl_error_state();
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100676 return -1;
677 }
678
679 return 0;
680}
681
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +0300682static int
683shader_init(struct gl_shader *shader, struct gl_renderer *gr,
684 const char *vertex_source, const char *fragment_source);
685
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400686static void
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +0300687use_shader(struct gl_renderer *gr, struct gl_shader *shader)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400688{
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +0300689 if (!shader->program) {
690 int ret;
691
692 ret = shader_init(shader, gr,
693 shader->vertex_source,
694 shader->fragment_source);
695
696 if (ret < 0)
697 weston_log("warning: failed to compile shader\n");
698 }
699
John Kåre Alsaker40684142012-11-13 19:10:25 +0100700 if (gr->current_shader == shader)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400701 return;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400702 glUseProgram(shader->program);
John Kåre Alsaker40684142012-11-13 19:10:25 +0100703 gr->current_shader = shader;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400704}
705
706static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100707shader_uniforms(struct gl_shader *shader,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500708 struct weston_view *view,
709 struct weston_output *output)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400710{
711 int i;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500712 struct gl_surface_state *gs = get_surface_state(view->surface);
Jason Ekstrandfb23df72014-10-16 10:55:21 -0500713 struct gl_output_state *go = get_output_state(output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400714
715 glUniformMatrix4fv(shader->proj_uniform,
Jason Ekstrandfb23df72014-10-16 10:55:21 -0500716 1, GL_FALSE, go->output_matrix.d);
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100717 glUniform4fv(shader->color_uniform, 1, gs->color);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500718 glUniform1f(shader->alpha_uniform, view->alpha);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400719
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100720 for (i = 0; i < gs->num_textures; i++)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400721 glUniform1i(shader->tex_uniforms[i], i);
722}
723
724static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500725draw_view(struct weston_view *ev, struct weston_output *output,
726 pixman_region32_t *damage) /* in global coordinates */
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400727{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500728 struct weston_compositor *ec = ev->surface->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100729 struct gl_renderer *gr = get_renderer(ec);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500730 struct gl_surface_state *gs = get_surface_state(ev->surface);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400731 /* repaint bounding region in global coordinates: */
732 pixman_region32_t repaint;
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +0200733 /* opaque region in surface coordinates: */
734 pixman_region32_t surface_opaque;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400735 /* non-opaque region in surface coordinates: */
736 pixman_region32_t surface_blend;
737 GLint filter;
738 int i;
739
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +0200740 /* In case of a runtime switch of renderers, we may not have received
741 * an attach for this surface since the switch. In that case we don't
742 * have a valid buffer or a proper shader set up so skip rendering. */
743 if (!gs->shader)
744 return;
745
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400746 pixman_region32_init(&repaint);
747 pixman_region32_intersect(&repaint,
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200748 &ev->transform.boundingbox, damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500749 pixman_region32_subtract(&repaint, &repaint, &ev->clip);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400750
751 if (!pixman_region32_not_empty(&repaint))
752 goto out;
753
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400754 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
755
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400756 if (gr->fan_debug) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100757 use_shader(gr, &gr->solid_shader);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500758 shader_uniforms(&gr->solid_shader, ev, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400759 }
760
John Kåre Alsaker40684142012-11-13 19:10:25 +0100761 use_shader(gr, gs->shader);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500762 shader_uniforms(gs->shader, ev, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400763
Jason Ekstranda7af7042013-10-12 22:38:11 -0500764 if (ev->transform.enabled || output->zoom.active ||
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200765 output->current_scale != ev->surface->buffer_viewport.buffer.scale)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400766 filter = GL_LINEAR;
767 else
768 filter = GL_NEAREST;
769
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100770 for (i = 0; i < gs->num_textures; i++) {
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400771 glActiveTexture(GL_TEXTURE0 + i);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100772 glBindTexture(gs->target, gs->textures[i]);
773 glTexParameteri(gs->target, GL_TEXTURE_MIN_FILTER, filter);
774 glTexParameteri(gs->target, GL_TEXTURE_MAG_FILTER, filter);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400775 }
776
777 /* blended region is whole surface minus opaque region: */
778 pixman_region32_init_rect(&surface_blend, 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600779 ev->surface->width, ev->surface->height);
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +0200780 if (ev->geometry.scissor_enabled)
781 pixman_region32_intersect(&surface_blend, &surface_blend,
782 &ev->geometry.scissor);
783 pixman_region32_subtract(&surface_blend, &surface_blend,
784 &ev->surface->opaque);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400785
Jason Ekstranda7af7042013-10-12 22:38:11 -0500786 /* XXX: Should we be using ev->transform.opaque here? */
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +0200787 pixman_region32_init(&surface_opaque);
788 if (ev->geometry.scissor_enabled)
789 pixman_region32_intersect(&surface_opaque,
790 &ev->surface->opaque,
791 &ev->geometry.scissor);
792 else
793 pixman_region32_copy(&surface_opaque, &ev->surface->opaque);
794
795 if (pixman_region32_not_empty(&surface_opaque)) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100796 if (gs->shader == &gr->texture_shader_rgba) {
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400797 /* Special case for RGBA textures with possibly
798 * bad data in alpha channel: use the shader
799 * that forces texture alpha = 1.0.
800 * Xwayland surfaces need this.
801 */
John Kåre Alsaker40684142012-11-13 19:10:25 +0100802 use_shader(gr, &gr->texture_shader_rgbx);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500803 shader_uniforms(&gr->texture_shader_rgbx, ev, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400804 }
805
Jason Ekstranda7af7042013-10-12 22:38:11 -0500806 if (ev->alpha < 1.0)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400807 glEnable(GL_BLEND);
808 else
809 glDisable(GL_BLEND);
810
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +0200811 repaint_region(ev, &repaint, &surface_opaque);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400812 }
813
814 if (pixman_region32_not_empty(&surface_blend)) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100815 use_shader(gr, gs->shader);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400816 glEnable(GL_BLEND);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500817 repaint_region(ev, &repaint, &surface_blend);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400818 }
819
820 pixman_region32_fini(&surface_blend);
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +0200821 pixman_region32_fini(&surface_opaque);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400822
823out:
824 pixman_region32_fini(&repaint);
825}
826
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400827static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500828repaint_views(struct weston_output *output, pixman_region32_t *damage)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400829{
830 struct weston_compositor *compositor = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500831 struct weston_view *view;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400832
Jason Ekstranda7af7042013-10-12 22:38:11 -0500833 wl_list_for_each_reverse(view, &compositor->view_list, link)
834 if (view->plane == &compositor->primary_plane)
835 draw_view(view, output, damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400836}
837
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500838static void
Jason Ekstrande5512d42014-02-04 21:36:38 -0600839draw_output_border_texture(struct gl_output_state *go,
840 enum gl_renderer_border_side side,
841 int32_t x, int32_t y,
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500842 int32_t width, int32_t height)
843{
Jason Ekstrande5512d42014-02-04 21:36:38 -0600844 struct gl_border_image *img = &go->borders[side];
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500845 static GLushort indices [] = { 0, 1, 3, 3, 1, 2 };
846
847 if (!img->data) {
848 if (img->tex) {
849 glDeleteTextures(1, &img->tex);
850 img->tex = 0;
851 }
852
853 return;
854 }
855
856 if (!img->tex) {
857 glGenTextures(1, &img->tex);
858 glBindTexture(GL_TEXTURE_2D, img->tex);
859
860 glTexParameteri(GL_TEXTURE_2D,
861 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
862 glTexParameteri(GL_TEXTURE_2D,
863 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
864 glTexParameteri(GL_TEXTURE_2D,
865 GL_TEXTURE_MIN_FILTER, GL_NEAREST);
866 glTexParameteri(GL_TEXTURE_2D,
867 GL_TEXTURE_MAG_FILTER, GL_NEAREST);
868 } else {
869 glBindTexture(GL_TEXTURE_2D, img->tex);
870 }
871
Jason Ekstrande5512d42014-02-04 21:36:38 -0600872 if (go->border_status & (1 << side)) {
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500873 glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, 0);
874 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, 0);
875 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, 0);
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500876 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
877 img->tex_width, img->height, 0,
878 GL_BGRA_EXT, GL_UNSIGNED_BYTE, img->data);
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500879 }
880
881 GLfloat texcoord[] = {
882 0.0f, 0.0f,
883 (GLfloat)img->width / (GLfloat)img->tex_width, 0.0f,
884 (GLfloat)img->width / (GLfloat)img->tex_width, 1.0f,
885 0.0f, 1.0f,
886 };
887
888 GLfloat verts[] = {
889 x, y,
890 x + width, y,
891 x + width, y + height,
892 x, y + height
893 };
894
895 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, verts);
896 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, texcoord);
897 glEnableVertexAttribArray(0);
898 glEnableVertexAttribArray(1);
899
900 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
901
902 glDisableVertexAttribArray(1);
903 glDisableVertexAttribArray(0);
904}
905
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -0600906static int
907output_has_borders(struct weston_output *output)
908{
909 struct gl_output_state *go = get_output_state(output);
910
911 return go->borders[GL_RENDERER_BORDER_TOP].data ||
912 go->borders[GL_RENDERER_BORDER_RIGHT].data ||
913 go->borders[GL_RENDERER_BORDER_BOTTOM].data ||
914 go->borders[GL_RENDERER_BORDER_LEFT].data;
915}
916
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500917static void
Jason Ekstrande5512d42014-02-04 21:36:38 -0600918draw_output_borders(struct weston_output *output,
919 enum gl_border_status border_status)
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500920{
921 struct gl_output_state *go = get_output_state(output);
922 struct gl_renderer *gr = get_renderer(output->compositor);
923 struct gl_shader *shader = &gr->texture_shader_rgba;
Jason Ekstrand00b84282013-10-27 22:24:59 -0500924 struct gl_border_image *top, *bottom, *left, *right;
925 struct weston_matrix matrix;
926 int full_width, full_height;
927
Jason Ekstrande5512d42014-02-04 21:36:38 -0600928 if (border_status == BORDER_STATUS_CLEAN)
929 return; /* Clean. Nothing to do. */
930
Jason Ekstrand00b84282013-10-27 22:24:59 -0500931 top = &go->borders[GL_RENDERER_BORDER_TOP];
932 bottom = &go->borders[GL_RENDERER_BORDER_BOTTOM];
933 left = &go->borders[GL_RENDERER_BORDER_LEFT];
934 right = &go->borders[GL_RENDERER_BORDER_RIGHT];
935
936 full_width = output->current_mode->width + left->width + right->width;
937 full_height = output->current_mode->height + top->height + bottom->height;
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500938
939 glDisable(GL_BLEND);
940 use_shader(gr, shader);
941
Jason Ekstrand00b84282013-10-27 22:24:59 -0500942 glViewport(0, 0, full_width, full_height);
943
944 weston_matrix_init(&matrix);
945 weston_matrix_translate(&matrix, -full_width/2.0, -full_height/2.0, 0);
946 weston_matrix_scale(&matrix, 2.0/full_width, -2.0/full_height, 1);
947 glUniformMatrix4fv(shader->proj_uniform, 1, GL_FALSE, matrix.d);
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500948
949 glUniform1i(shader->tex_uniforms[0], 0);
950 glUniform1f(shader->alpha_uniform, 1);
951 glActiveTexture(GL_TEXTURE0);
952
Jason Ekstrande5512d42014-02-04 21:36:38 -0600953 if (border_status & BORDER_TOP_DIRTY)
954 draw_output_border_texture(go, GL_RENDERER_BORDER_TOP,
955 0, 0,
956 full_width, top->height);
957 if (border_status & BORDER_LEFT_DIRTY)
958 draw_output_border_texture(go, GL_RENDERER_BORDER_LEFT,
959 0, top->height,
960 left->width, output->current_mode->height);
961 if (border_status & BORDER_RIGHT_DIRTY)
962 draw_output_border_texture(go, GL_RENDERER_BORDER_RIGHT,
963 full_width - right->width, top->height,
964 right->width, output->current_mode->height);
965 if (border_status & BORDER_BOTTOM_DIRTY)
966 draw_output_border_texture(go, GL_RENDERER_BORDER_BOTTOM,
967 0, full_height - bottom->height,
968 full_width, bottom->height);
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500969}
John Kåre Alsaker44154502012-11-13 19:10:20 +0100970
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400971static void
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -0600972output_get_border_damage(struct weston_output *output,
973 enum gl_border_status border_status,
974 pixman_region32_t *damage)
975{
976 struct gl_output_state *go = get_output_state(output);
977 struct gl_border_image *top, *bottom, *left, *right;
978 int full_width, full_height;
979
980 if (border_status == BORDER_STATUS_CLEAN)
981 return; /* Clean. Nothing to do. */
982
983 top = &go->borders[GL_RENDERER_BORDER_TOP];
984 bottom = &go->borders[GL_RENDERER_BORDER_BOTTOM];
985 left = &go->borders[GL_RENDERER_BORDER_LEFT];
986 right = &go->borders[GL_RENDERER_BORDER_RIGHT];
987
988 full_width = output->current_mode->width + left->width + right->width;
989 full_height = output->current_mode->height + top->height + bottom->height;
990 if (border_status & BORDER_TOP_DIRTY)
991 pixman_region32_union_rect(damage, damage,
992 0, 0,
993 full_width, top->height);
994 if (border_status & BORDER_LEFT_DIRTY)
995 pixman_region32_union_rect(damage, damage,
996 0, top->height,
997 left->width, output->current_mode->height);
998 if (border_status & BORDER_RIGHT_DIRTY)
999 pixman_region32_union_rect(damage, damage,
1000 full_width - right->width, top->height,
1001 right->width, output->current_mode->height);
1002 if (border_status & BORDER_BOTTOM_DIRTY)
1003 pixman_region32_union_rect(damage, damage,
1004 0, full_height - bottom->height,
1005 full_width, bottom->height);
1006}
1007
1008static void
Jason Ekstrande5512d42014-02-04 21:36:38 -06001009output_get_damage(struct weston_output *output,
1010 pixman_region32_t *buffer_damage, uint32_t *border_damage)
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001011{
1012 struct gl_output_state *go = get_output_state(output);
1013 struct gl_renderer *gr = get_renderer(output->compositor);
1014 EGLint buffer_age = 0;
1015 EGLBoolean ret;
1016 int i;
1017
1018 if (gr->has_egl_buffer_age) {
1019 ret = eglQuerySurface(gr->egl_display, go->egl_surface,
1020 EGL_BUFFER_AGE_EXT, &buffer_age);
1021 if (ret == EGL_FALSE) {
1022 weston_log("buffer age query failed.\n");
1023 gl_renderer_print_egl_error_state();
1024 }
1025 }
1026
Jason Ekstrande5512d42014-02-04 21:36:38 -06001027 if (buffer_age == 0 || buffer_age - 1 > BUFFER_DAMAGE_COUNT) {
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001028 pixman_region32_copy(buffer_damage, &output->region);
Jason Ekstrande5512d42014-02-04 21:36:38 -06001029 *border_damage = BORDER_ALL_DIRTY;
1030 } else {
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001031 for (i = 0; i < buffer_age - 1; i++)
Derek Foreman4c582662014-10-09 18:39:44 -05001032 *border_damage |= go->border_damage[(go->buffer_damage_index + i) % BUFFER_DAMAGE_COUNT];
Jason Ekstrande5512d42014-02-04 21:36:38 -06001033
1034 if (*border_damage & BORDER_SIZE_CHANGED) {
1035 /* If we've had a resize, we have to do a full
1036 * repaint. */
1037 *border_damage |= BORDER_ALL_DIRTY;
1038 pixman_region32_copy(buffer_damage, &output->region);
1039 } else {
1040 for (i = 0; i < buffer_age - 1; i++)
1041 pixman_region32_union(buffer_damage,
1042 buffer_damage,
Derek Foreman4c582662014-10-09 18:39:44 -05001043 &go->buffer_damage[(go->buffer_damage_index + i) % BUFFER_DAMAGE_COUNT]);
Jason Ekstrande5512d42014-02-04 21:36:38 -06001044 }
1045 }
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001046}
1047
1048static void
1049output_rotate_damage(struct weston_output *output,
Jason Ekstrande5512d42014-02-04 21:36:38 -06001050 pixman_region32_t *output_damage,
1051 enum gl_border_status border_status)
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001052{
1053 struct gl_output_state *go = get_output_state(output);
1054 struct gl_renderer *gr = get_renderer(output->compositor);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001055
1056 if (!gr->has_egl_buffer_age)
1057 return;
1058
Derek Foreman4c582662014-10-09 18:39:44 -05001059 go->buffer_damage_index += BUFFER_DAMAGE_COUNT - 1;
1060 go->buffer_damage_index %= BUFFER_DAMAGE_COUNT;
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001061
Derek Foreman4c582662014-10-09 18:39:44 -05001062 pixman_region32_copy(&go->buffer_damage[go->buffer_damage_index], output_damage);
1063 go->border_damage[go->buffer_damage_index] = border_status;
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001064}
1065
Derek Foremanc4cfe852015-05-15 12:12:40 -05001066/* NOTE: We now allow falling back to ARGB gl visuals when XRGB is
1067 * unavailable, so we're assuming the background has no transparency
1068 * and that everything with a blend, like drop shadows, will have something
1069 * opaque (like the background) drawn underneath it.
1070 *
1071 * Depending on the underlying hardware, violating that assumption could
1072 * result in seeing through to another display plane.
1073 */
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001074static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001075gl_renderer_repaint_output(struct weston_output *output,
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001076 pixman_region32_t *output_damage)
1077{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001078 struct gl_output_state *go = get_output_state(output);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001079 struct weston_compositor *compositor = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001080 struct gl_renderer *gr = get_renderer(compositor);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001081 EGLBoolean ret;
1082 static int errored;
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -06001083 int i, nrects, buffer_height;
1084 EGLint *egl_damage, *d;
1085 pixman_box32_t *rects;
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001086 pixman_region32_t buffer_damage, total_damage;
Jason Ekstrande5512d42014-02-04 21:36:38 -06001087 enum gl_border_status border_damage = BORDER_STATUS_CLEAN;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001088
Jason Ekstrandae0c6e32014-10-16 10:55:20 -05001089 if (use_output(output) < 0)
1090 return;
1091
Jason Ekstrand00b84282013-10-27 22:24:59 -05001092 /* Calculate the viewport */
1093 glViewport(go->borders[GL_RENDERER_BORDER_LEFT].width,
1094 go->borders[GL_RENDERER_BORDER_BOTTOM].height,
1095 output->current_mode->width,
1096 output->current_mode->height);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001097
Jason Ekstrandfb23df72014-10-16 10:55:21 -05001098 /* Calculate the global GL matrix */
1099 go->output_matrix = output->matrix;
1100 weston_matrix_translate(&go->output_matrix,
1101 -(output->current_mode->width / 2.0),
1102 -(output->current_mode->height / 2.0), 0);
1103 weston_matrix_scale(&go->output_matrix,
1104 2.0 / output->current_mode->width,
1105 -2.0 / output->current_mode->height, 1);
1106
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001107 /* if debugging, redraw everything outside the damage to clean up
1108 * debug lines from the previous draw on this buffer:
1109 */
Kristian Høgsberg8799d412013-05-07 10:50:09 -04001110 if (gr->fan_debug) {
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001111 pixman_region32_t undamaged;
1112 pixman_region32_init(&undamaged);
1113 pixman_region32_subtract(&undamaged, &output->region,
1114 output_damage);
Kristian Høgsberg8799d412013-05-07 10:50:09 -04001115 gr->fan_debug = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001116 repaint_views(output, &undamaged);
Kristian Høgsberg8799d412013-05-07 10:50:09 -04001117 gr->fan_debug = 1;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001118 pixman_region32_fini(&undamaged);
1119 }
1120
Ander Conselvan de Oliveirab605c062013-03-05 17:30:28 +02001121 pixman_region32_init(&total_damage);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001122 pixman_region32_init(&buffer_damage);
1123
Jason Ekstrande5512d42014-02-04 21:36:38 -06001124 output_get_damage(output, &buffer_damage, &border_damage);
1125 output_rotate_damage(output, output_damage, go->border_status);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001126
1127 pixman_region32_union(&total_damage, &buffer_damage, output_damage);
Jason Ekstrande5512d42014-02-04 21:36:38 -06001128 border_damage |= go->border_status;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03001129
Jason Ekstranda7af7042013-10-12 22:38:11 -05001130 repaint_views(output, &total_damage);
Ander Conselvan de Oliveirab605c062013-03-05 17:30:28 +02001131
1132 pixman_region32_fini(&total_damage);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001133 pixman_region32_fini(&buffer_damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001134
Jason Ekstrande5512d42014-02-04 21:36:38 -06001135 draw_output_borders(output, border_damage);
John Kåre Alsaker44154502012-11-13 19:10:20 +01001136
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001137 pixman_region32_copy(&output->previous_damage, output_damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001138 wl_signal_emit(&output->frame_signal, output);
1139
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -06001140 if (gr->swap_buffers_with_damage) {
1141 pixman_region32_init(&buffer_damage);
1142 weston_transformed_region(output->width, output->height,
1143 output->transform,
1144 output->current_scale,
1145 output_damage, &buffer_damage);
1146
1147 if (output_has_borders(output)) {
1148 pixman_region32_translate(&buffer_damage,
1149 go->borders[GL_RENDERER_BORDER_LEFT].width,
1150 go->borders[GL_RENDERER_BORDER_TOP].height);
1151 output_get_border_damage(output, go->border_status,
1152 &buffer_damage);
1153 }
1154
1155 rects = pixman_region32_rectangles(&buffer_damage, &nrects);
1156 egl_damage = malloc(nrects * 4 * sizeof(EGLint));
1157
1158 buffer_height = go->borders[GL_RENDERER_BORDER_TOP].height +
1159 output->current_mode->height +
1160 go->borders[GL_RENDERER_BORDER_BOTTOM].height;
1161
1162 d = egl_damage;
1163 for (i = 0; i < nrects; ++i) {
1164 *d++ = rects[i].x1;
1165 *d++ = buffer_height - rects[i].y2;
1166 *d++ = rects[i].x2 - rects[i].x1;
1167 *d++ = rects[i].y2 - rects[i].y1;
1168 }
1169 ret = gr->swap_buffers_with_damage(gr->egl_display,
1170 go->egl_surface,
1171 egl_damage, nrects);
1172 free(egl_damage);
1173 pixman_region32_fini(&buffer_damage);
1174 } else {
1175 ret = eglSwapBuffers(gr->egl_display, go->egl_surface);
1176 }
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -06001177
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001178 if (ret == EGL_FALSE && !errored) {
1179 errored = 1;
1180 weston_log("Failed in eglSwapBuffers.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02001181 gl_renderer_print_egl_error_state();
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001182 }
1183
Jason Ekstrande5512d42014-02-04 21:36:38 -06001184 go->border_status = BORDER_STATUS_CLEAN;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001185}
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001186
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001187static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001188gl_renderer_read_pixels(struct weston_output *output,
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001189 pixman_format_code_t format, void *pixels,
1190 uint32_t x, uint32_t y,
1191 uint32_t width, uint32_t height)
1192{
1193 GLenum gl_format;
Jason Ekstrand701f6362014-04-02 19:53:59 -05001194 struct gl_output_state *go = get_output_state(output);
1195
1196 x += go->borders[GL_RENDERER_BORDER_LEFT].width;
1197 y += go->borders[GL_RENDERER_BORDER_BOTTOM].height;
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001198
1199 switch (format) {
1200 case PIXMAN_a8r8g8b8:
1201 gl_format = GL_BGRA_EXT;
1202 break;
1203 case PIXMAN_a8b8g8r8:
1204 gl_format = GL_RGBA;
1205 break;
1206 default:
1207 return -1;
1208 }
1209
1210 if (use_output(output) < 0)
1211 return -1;
1212
1213 glPixelStorei(GL_PACK_ALIGNMENT, 1);
1214 glReadPixels(x, y, width, height, gl_format,
1215 GL_UNSIGNED_BYTE, pixels);
1216
1217 return 0;
1218}
1219
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001220static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001221gl_renderer_flush_damage(struct weston_surface *surface)
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001222{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001223 struct gl_renderer *gr = get_renderer(surface->compositor);
1224 struct gl_surface_state *gs = get_surface_state(surface);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001225 struct weston_buffer *buffer = gs->buffer_ref.buffer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001226 struct weston_view *view;
Derek Foreman97746792015-11-18 16:32:28 -06001227 bool texture_used;
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001228 pixman_box32_t *rectangles;
1229 void *data;
1230 int i, n;
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001231
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001232 pixman_region32_union(&gs->texture_damage,
1233 &gs->texture_damage, &surface->damage);
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001234
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001235 if (!buffer)
1236 return;
1237
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001238 /* Avoid upload, if the texture won't be used this time.
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001239 * We still accumulate the damage in texture_damage, and
1240 * hold the reference to the buffer, in case the surface
1241 * migrates back to the primary plane.
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001242 */
Derek Foreman97746792015-11-18 16:32:28 -06001243 texture_used = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001244 wl_list_for_each(view, &surface->views, surface_link) {
1245 if (view->plane == &surface->compositor->primary_plane) {
Derek Foreman97746792015-11-18 16:32:28 -06001246 texture_used = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001247 break;
1248 }
1249 }
1250 if (!texture_used)
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001251 return;
1252
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001253 if (!pixman_region32_not_empty(&gs->texture_damage) &&
1254 !gs->needs_full_upload)
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001255 goto done;
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001256
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001257 glBindTexture(GL_TEXTURE_2D, gs->textures[0]);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001258
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001259 if (!gr->has_unpack_subimage) {
Neil Robertse5051712013-11-13 15:44:06 +00001260 wl_shm_buffer_begin_access(buffer->shm_buffer);
Neil Roberts4d085e72014-04-07 15:01:01 +01001261 glTexImage2D(GL_TEXTURE_2D, 0, gs->gl_format,
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001262 gs->pitch, buffer->height, 0,
Neil Roberts4d085e72014-04-07 15:01:01 +01001263 gs->gl_format, gs->gl_pixel_type,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001264 wl_shm_buffer_get_data(buffer->shm_buffer));
Neil Robertse5051712013-11-13 15:44:06 +00001265 wl_shm_buffer_end_access(buffer->shm_buffer);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001266
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001267 goto done;
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001268 }
1269
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -07001270 glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, gs->pitch);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001271 data = wl_shm_buffer_get_data(buffer->shm_buffer);
Ander Conselvan de Oliveira6be5f432013-06-07 16:52:45 +03001272
1273 if (gs->needs_full_upload) {
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -07001274 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, 0);
1275 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, 0);
Neil Robertse5051712013-11-13 15:44:06 +00001276 wl_shm_buffer_begin_access(buffer->shm_buffer);
Neil Roberts4d085e72014-04-07 15:01:01 +01001277 glTexImage2D(GL_TEXTURE_2D, 0, gs->gl_format,
Neil Roberts39a443f2014-04-04 16:24:54 +01001278 gs->pitch, buffer->height, 0,
Neil Roberts4d085e72014-04-07 15:01:01 +01001279 gs->gl_format, gs->gl_pixel_type, data);
Neil Robertse5051712013-11-13 15:44:06 +00001280 wl_shm_buffer_end_access(buffer->shm_buffer);
Ander Conselvan de Oliveira6be5f432013-06-07 16:52:45 +03001281 goto done;
1282 }
1283
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001284 rectangles = pixman_region32_rectangles(&gs->texture_damage, &n);
Neil Robertse5051712013-11-13 15:44:06 +00001285 wl_shm_buffer_begin_access(buffer->shm_buffer);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001286 for (i = 0; i < n; i++) {
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001287 pixman_box32_t r;
1288
1289 r = weston_surface_to_buffer_rect(surface, rectangles[i]);
1290
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -07001291 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, r.x1);
1292 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, r.y1);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001293 glTexSubImage2D(GL_TEXTURE_2D, 0, r.x1, r.y1,
1294 r.x2 - r.x1, r.y2 - r.y1,
Neil Roberts4d085e72014-04-07 15:01:01 +01001295 gs->gl_format, gs->gl_pixel_type, data);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001296 }
Neil Robertse5051712013-11-13 15:44:06 +00001297 wl_shm_buffer_end_access(buffer->shm_buffer);
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001298
1299done:
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001300 pixman_region32_fini(&gs->texture_damage);
1301 pixman_region32_init(&gs->texture_damage);
Derek Foreman4c11fe72015-11-18 16:32:27 -06001302 gs->needs_full_upload = false;
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001303
1304 weston_buffer_reference(&gs->buffer_ref, NULL);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001305}
1306
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001307static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001308ensure_textures(struct gl_surface_state *gs, int num_textures)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001309{
1310 int i;
1311
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001312 if (num_textures <= gs->num_textures)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001313 return;
1314
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001315 for (i = gs->num_textures; i < num_textures; i++) {
1316 glGenTextures(1, &gs->textures[i]);
1317 glBindTexture(gs->target, gs->textures[i]);
1318 glTexParameteri(gs->target,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001319 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001320 glTexParameteri(gs->target,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001321 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1322 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001323 gs->num_textures = num_textures;
1324 glBindTexture(gs->target, 0);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001325}
1326
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001327static void
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001328gl_renderer_attach_shm(struct weston_surface *es, struct weston_buffer *buffer,
1329 struct wl_shm_buffer *shm_buffer)
1330{
1331 struct weston_compositor *ec = es->compositor;
1332 struct gl_renderer *gr = get_renderer(ec);
1333 struct gl_surface_state *gs = get_surface_state(es);
Neil Roberts4d085e72014-04-07 15:01:01 +01001334 GLenum gl_format, gl_pixel_type;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001335 int pitch;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001336
1337 buffer->shm_buffer = shm_buffer;
1338 buffer->width = wl_shm_buffer_get_width(shm_buffer);
1339 buffer->height = wl_shm_buffer_get_height(shm_buffer);
1340
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001341 switch (wl_shm_buffer_get_format(shm_buffer)) {
1342 case WL_SHM_FORMAT_XRGB8888:
1343 gs->shader = &gr->texture_shader_rgbx;
1344 pitch = wl_shm_buffer_get_stride(shm_buffer) / 4;
Neil Roberts4d085e72014-04-07 15:01:01 +01001345 gl_format = GL_BGRA_EXT;
1346 gl_pixel_type = GL_UNSIGNED_BYTE;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001347 break;
1348 case WL_SHM_FORMAT_ARGB8888:
1349 gs->shader = &gr->texture_shader_rgba;
1350 pitch = wl_shm_buffer_get_stride(shm_buffer) / 4;
Neil Roberts4d085e72014-04-07 15:01:01 +01001351 gl_format = GL_BGRA_EXT;
1352 gl_pixel_type = GL_UNSIGNED_BYTE;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001353 break;
1354 case WL_SHM_FORMAT_RGB565:
1355 gs->shader = &gr->texture_shader_rgbx;
1356 pitch = wl_shm_buffer_get_stride(shm_buffer) / 2;
Neil Roberts4d085e72014-04-07 15:01:01 +01001357 gl_format = GL_RGB;
1358 gl_pixel_type = GL_UNSIGNED_SHORT_5_6_5;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001359 break;
1360 default:
Neil Roberts4d085e72014-04-07 15:01:01 +01001361 weston_log("warning: unknown shm buffer format: %08x\n",
1362 wl_shm_buffer_get_format(shm_buffer));
1363 return;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001364 }
1365
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001366 /* Only allocate a texture if it doesn't match existing one.
1367 * If a switch from DRM allocated buffer to a SHM buffer is
1368 * happening, we need to allocate a new texture buffer. */
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001369 if (pitch != gs->pitch ||
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001370 buffer->height != gs->height ||
Neil Roberts4d085e72014-04-07 15:01:01 +01001371 gl_format != gs->gl_format ||
1372 gl_pixel_type != gs->gl_pixel_type ||
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001373 gs->buffer_type != BUFFER_TYPE_SHM) {
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001374 gs->pitch = pitch;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001375 gs->height = buffer->height;
1376 gs->target = GL_TEXTURE_2D;
Neil Roberts4d085e72014-04-07 15:01:01 +01001377 gs->gl_format = gl_format;
1378 gs->gl_pixel_type = gl_pixel_type;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001379 gs->buffer_type = BUFFER_TYPE_SHM;
Derek Foreman4c11fe72015-11-18 16:32:27 -06001380 gs->needs_full_upload = true;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001381 gs->y_inverted = 1;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001382
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03001383 gs->surface = es;
1384
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001385 ensure_textures(gs, 1);
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001386 }
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001387}
1388
1389static void
1390gl_renderer_attach_egl(struct weston_surface *es, struct weston_buffer *buffer,
1391 uint32_t format)
1392{
1393 struct weston_compositor *ec = es->compositor;
1394 struct gl_renderer *gr = get_renderer(ec);
1395 struct gl_surface_state *gs = get_surface_state(es);
1396 EGLint attribs[3];
1397 int i, num_planes;
1398
1399 buffer->legacy_buffer = (struct wl_buffer *)buffer->resource;
1400 gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1401 EGL_WIDTH, &buffer->width);
1402 gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1403 EGL_HEIGHT, &buffer->height);
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001404 gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1405 EGL_WAYLAND_Y_INVERTED_WL, &buffer->y_inverted);
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001406
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +03001407 for (i = 0; i < gs->num_images; i++) {
1408 egl_image_unref(gs->images[i]);
1409 gs->images[i] = NULL;
1410 }
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001411 gs->num_images = 0;
1412 gs->target = GL_TEXTURE_2D;
1413 switch (format) {
1414 case EGL_TEXTURE_RGB:
1415 case EGL_TEXTURE_RGBA:
1416 default:
1417 num_planes = 1;
1418 gs->shader = &gr->texture_shader_rgba;
1419 break;
1420 case EGL_TEXTURE_EXTERNAL_WL:
1421 num_planes = 1;
1422 gs->target = GL_TEXTURE_EXTERNAL_OES;
1423 gs->shader = &gr->texture_shader_egl_external;
1424 break;
1425 case EGL_TEXTURE_Y_UV_WL:
1426 num_planes = 2;
1427 gs->shader = &gr->texture_shader_y_uv;
1428 break;
1429 case EGL_TEXTURE_Y_U_V_WL:
1430 num_planes = 3;
1431 gs->shader = &gr->texture_shader_y_u_v;
1432 break;
1433 case EGL_TEXTURE_Y_XUXV_WL:
1434 num_planes = 2;
1435 gs->shader = &gr->texture_shader_y_xuxv;
1436 break;
1437 }
1438
1439 ensure_textures(gs, num_planes);
1440 for (i = 0; i < num_planes; i++) {
1441 attribs[0] = EGL_WAYLAND_PLANE_WL;
1442 attribs[1] = i;
1443 attribs[2] = EGL_NONE;
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +03001444 gs->images[i] = egl_image_create(gr,
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001445 EGL_WAYLAND_BUFFER_WL,
1446 buffer->legacy_buffer,
1447 attribs);
1448 if (!gs->images[i]) {
1449 weston_log("failed to create img for plane %d\n", i);
1450 continue;
1451 }
1452 gs->num_images++;
1453
1454 glActiveTexture(GL_TEXTURE0 + i);
1455 glBindTexture(gs->target, gs->textures[i]);
1456 gr->image_target_texture_2d(gs->target,
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +03001457 gs->images[i]->image);
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001458 }
1459
1460 gs->pitch = buffer->width;
1461 gs->height = buffer->height;
1462 gs->buffer_type = BUFFER_TYPE_EGL;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001463 gs->y_inverted = buffer->y_inverted;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001464}
1465
1466static void
Pekka Paalanena3525802014-06-12 16:49:29 +03001467gl_renderer_destroy_dmabuf(struct linux_dmabuf_buffer *dmabuf)
1468{
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001469 struct dmabuf_image *image = dmabuf->user_data;
Pekka Paalanena3525802014-06-12 16:49:29 +03001470
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001471 dmabuf_image_destroy(image);
Pekka Paalanena3525802014-06-12 16:49:29 +03001472}
1473
1474static struct egl_image *
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001475import_simple_dmabuf(struct gl_renderer *gr,
1476 struct dmabuf_attributes *attributes)
Pekka Paalanena3525802014-06-12 16:49:29 +03001477{
1478 struct egl_image *image;
1479 EGLint attribs[30];
1480 int atti = 0;
1481
Pekka Paalanena3525802014-06-12 16:49:29 +03001482 /* This requires the Mesa commit in
1483 * Mesa 10.3 (08264e5dad4df448e7718e782ad9077902089a07) or
1484 * Mesa 10.2.7 (55d28925e6109a4afd61f109e845a8a51bd17652).
1485 * Otherwise Mesa closes the fd behind our back and re-importing
1486 * will fail.
1487 * https://bugs.freedesktop.org/show_bug.cgi?id=76188
1488 */
1489
1490 attribs[atti++] = EGL_WIDTH;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001491 attribs[atti++] = attributes->width;
Pekka Paalanena3525802014-06-12 16:49:29 +03001492 attribs[atti++] = EGL_HEIGHT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001493 attribs[atti++] = attributes->height;
Pekka Paalanena3525802014-06-12 16:49:29 +03001494 attribs[atti++] = EGL_LINUX_DRM_FOURCC_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001495 attribs[atti++] = attributes->format;
Pekka Paalanena3525802014-06-12 16:49:29 +03001496 /* XXX: Add modifier here when supported */
1497
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001498 if (attributes->n_planes > 0) {
Pekka Paalanena3525802014-06-12 16:49:29 +03001499 attribs[atti++] = EGL_DMA_BUF_PLANE0_FD_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001500 attribs[atti++] = attributes->fd[0];
Pekka Paalanena3525802014-06-12 16:49:29 +03001501 attribs[atti++] = EGL_DMA_BUF_PLANE0_OFFSET_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001502 attribs[atti++] = attributes->offset[0];
Pekka Paalanena3525802014-06-12 16:49:29 +03001503 attribs[atti++] = EGL_DMA_BUF_PLANE0_PITCH_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001504 attribs[atti++] = attributes->stride[0];
Pekka Paalanena3525802014-06-12 16:49:29 +03001505 }
1506
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001507 if (attributes->n_planes > 1) {
Pekka Paalanena3525802014-06-12 16:49:29 +03001508 attribs[atti++] = EGL_DMA_BUF_PLANE1_FD_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001509 attribs[atti++] = attributes->fd[1];
Pekka Paalanena3525802014-06-12 16:49:29 +03001510 attribs[atti++] = EGL_DMA_BUF_PLANE1_OFFSET_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001511 attribs[atti++] = attributes->offset[1];
Pekka Paalanena3525802014-06-12 16:49:29 +03001512 attribs[atti++] = EGL_DMA_BUF_PLANE1_PITCH_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001513 attribs[atti++] = attributes->stride[1];
Pekka Paalanena3525802014-06-12 16:49:29 +03001514 }
1515
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001516 if (attributes->n_planes > 2) {
Pekka Paalanena3525802014-06-12 16:49:29 +03001517 attribs[atti++] = EGL_DMA_BUF_PLANE2_FD_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001518 attribs[atti++] = attributes->fd[2];
Pekka Paalanena3525802014-06-12 16:49:29 +03001519 attribs[atti++] = EGL_DMA_BUF_PLANE2_OFFSET_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001520 attribs[atti++] = attributes->offset[2];
Pekka Paalanena3525802014-06-12 16:49:29 +03001521 attribs[atti++] = EGL_DMA_BUF_PLANE2_PITCH_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001522 attribs[atti++] = attributes->stride[2];
Pekka Paalanena3525802014-06-12 16:49:29 +03001523 }
1524
1525 attribs[atti++] = EGL_NONE;
1526
1527 image = egl_image_create(gr, EGL_LINUX_DMA_BUF_EXT, NULL,
1528 attribs);
1529
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001530 return image;
1531}
Pekka Paalanena3525802014-06-12 16:49:29 +03001532
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001533/* The kernel header drm_fourcc.h defines the DRM formats below. We duplicate
1534 * some of the definitions here so that building Weston won't require
1535 * bleeding-edge kernel headers.
1536 */
1537#ifndef DRM_FORMAT_R8
1538#define DRM_FORMAT_R8 fourcc_code('R', '8', ' ', ' ') /* [7:0] R */
1539#endif
1540
1541#ifndef DRM_FORMAT_GR88
1542#define DRM_FORMAT_GR88 fourcc_code('G', 'R', '8', '8') /* [15:0] G:R 8:8 little endian */
1543#endif
1544
1545struct yuv_format_descriptor yuv_formats[] = {
1546 {
1547 .format = DRM_FORMAT_YUYV,
1548 .input_planes = 1,
1549 .output_planes = 2,
1550 .texture_type = EGL_TEXTURE_Y_XUXV_WL,
1551 {{
1552 .width_divisor = 1,
1553 .height_divisor = 1,
1554 .format = DRM_FORMAT_GR88,
1555 .plane_index = 0
1556 }, {
1557 .width_divisor = 2,
1558 .height_divisor = 1,
1559 .format = DRM_FORMAT_ARGB8888,
1560 .plane_index = 0
1561 }}
1562 }, {
1563 .format = DRM_FORMAT_NV12,
1564 .input_planes = 2,
1565 .output_planes = 2,
1566 .texture_type = EGL_TEXTURE_Y_UV_WL,
1567 {{
1568 .width_divisor = 1,
1569 .height_divisor = 1,
1570 .format = DRM_FORMAT_R8,
1571 .plane_index = 0
1572 }, {
1573 .width_divisor = 2,
1574 .height_divisor = 2,
1575 .format = DRM_FORMAT_GR88,
1576 .plane_index = 1
1577 }}
1578 }, {
1579 .format = DRM_FORMAT_YUV420,
1580 .input_planes = 3,
1581 .output_planes = 3,
1582 .texture_type = EGL_TEXTURE_Y_U_V_WL,
1583 {{
1584 .width_divisor = 1,
1585 .height_divisor = 1,
1586 .format = DRM_FORMAT_R8,
1587 .plane_index = 0
1588 }, {
1589 .width_divisor = 2,
1590 .height_divisor = 2,
1591 .format = DRM_FORMAT_R8,
1592 .plane_index = 1
1593 }, {
1594 .width_divisor = 2,
1595 .height_divisor = 2,
1596 .format = DRM_FORMAT_R8,
1597 .plane_index = 2
1598 }}
1599 }
1600};
1601
1602static struct egl_image *
1603import_dmabuf_single_plane(struct gl_renderer *gr,
1604 const struct dmabuf_attributes *attributes,
1605 struct yuv_plane_descriptor *descriptor)
1606{
1607 struct dmabuf_attributes plane;
1608 struct egl_image *image;
1609 char fmt[4];
1610
1611 plane.width = attributes->width / descriptor->width_divisor;
1612 plane.height = attributes->height / descriptor->height_divisor;
1613 plane.format = descriptor->format;
1614 plane.n_planes = 1;
1615 plane.fd[0] = attributes->fd[descriptor->plane_index];
1616 plane.offset[0] = attributes->offset[descriptor->plane_index];
1617 plane.stride[0] = attributes->stride[descriptor->plane_index];
1618 plane.modifier[0] = attributes->modifier[descriptor->plane_index];
1619
1620 image = import_simple_dmabuf(gr, &plane);
1621 if (!image) {
1622 weston_log("Failed to import plane %d as %.4s\n",
1623 descriptor->plane_index,
1624 dump_format(descriptor->format, fmt));
1625 return NULL;
1626 }
1627
1628 return image;
1629}
1630
1631static bool
1632import_yuv_dmabuf(struct gl_renderer *gr,
1633 struct dmabuf_image *image)
1634{
1635 unsigned i;
1636 int j;
1637 int ret;
1638 struct yuv_format_descriptor *format = NULL;
1639 struct dmabuf_attributes *attributes = &image->dmabuf->attributes;
1640 char fmt[4];
1641
1642 for (i = 0; i < ARRAY_LENGTH(yuv_formats); ++i) {
1643 if (yuv_formats[i].format == attributes->format) {
1644 format = &yuv_formats[i];
1645 break;
1646 }
1647 }
1648
1649 if (!format) {
1650 weston_log("Error during import, and no known conversion for format "
1651 "%.4s in the renderer",
1652 dump_format(attributes->format, fmt));
1653 return false;
1654 }
1655
1656 if (attributes->n_planes != format->input_planes) {
1657 weston_log("%.4s dmabuf must contain %d plane%s (%d provided)",
1658 dump_format(format->format, fmt),
1659 format->input_planes,
1660 (format->input_planes > 1) ? "s" : "",
1661 attributes->n_planes);
1662 return false;
1663 }
1664
1665 for (j = 0; j < format->output_planes; ++j) {
1666 image->images[j] = import_dmabuf_single_plane(gr, attributes,
1667 &format->plane[j]);
1668 if (!image->images[j]) {
1669 while (j) {
1670 ret = egl_image_unref(image->images[--j]);
1671 assert(ret == 0);
1672 }
1673 return false;
1674 }
1675 }
1676
1677 image->num_images = format->output_planes;
1678
1679 switch (format->texture_type) {
1680 case EGL_TEXTURE_Y_XUXV_WL:
1681 image->shader = &gr->texture_shader_y_xuxv;
1682 break;
1683 case EGL_TEXTURE_Y_UV_WL:
1684 image->shader = &gr->texture_shader_y_uv;
1685 break;
1686 case EGL_TEXTURE_Y_U_V_WL:
1687 image->shader = &gr->texture_shader_y_u_v;
1688 break;
1689 default:
1690 assert(false);
1691 }
1692
1693 return true;
1694}
1695
1696static GLenum
1697choose_texture_target(struct dmabuf_attributes *attributes)
1698{
1699 if (attributes->n_planes > 1)
1700 return GL_TEXTURE_EXTERNAL_OES;
1701
1702 switch (attributes->format & ~DRM_FORMAT_BIG_ENDIAN) {
1703 case DRM_FORMAT_YUYV:
1704 case DRM_FORMAT_YVYU:
1705 case DRM_FORMAT_UYVY:
1706 case DRM_FORMAT_VYUY:
1707 case DRM_FORMAT_AYUV:
1708 return GL_TEXTURE_EXTERNAL_OES;
1709 default:
1710 return GL_TEXTURE_2D;
1711 }
1712}
1713
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001714static struct dmabuf_image *
1715import_dmabuf(struct gl_renderer *gr,
1716 struct linux_dmabuf_buffer *dmabuf)
1717{
1718 struct egl_image *egl_image;
1719 struct dmabuf_image *image;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001720
1721 image = dmabuf_image_create();
Pekka Paalanena3525802014-06-12 16:49:29 +03001722 image->dmabuf = dmabuf;
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001723
1724 egl_image = import_simple_dmabuf(gr, &dmabuf->attributes);
1725 if (egl_image) {
1726 image->num_images = 1;
1727 image->images[0] = egl_image;
1728 image->import_type = IMPORT_TYPE_DIRECT;
1729 image->target = choose_texture_target(&dmabuf->attributes);
1730
1731 switch (image->target) {
1732 case GL_TEXTURE_2D:
1733 image->shader = &gr->texture_shader_rgba;
1734 break;
1735 default:
1736 image->shader = &gr->texture_shader_egl_external;
1737 }
1738 } else {
1739 if (!import_yuv_dmabuf(gr, image)) {
1740 dmabuf_image_destroy(image);
1741 return NULL;
1742 }
1743 image->import_type = IMPORT_TYPE_GL_CONVERSION;
1744 image->target = GL_TEXTURE_2D;
1745 }
Pekka Paalanena3525802014-06-12 16:49:29 +03001746
1747 return image;
1748}
1749
1750static bool
1751gl_renderer_import_dmabuf(struct weston_compositor *ec,
1752 struct linux_dmabuf_buffer *dmabuf)
1753{
1754 struct gl_renderer *gr = get_renderer(ec);
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001755 struct dmabuf_image *image;
Pekka Paalanena3525802014-06-12 16:49:29 +03001756 int i;
1757
1758 assert(gr->has_dmabuf_import);
1759
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001760 for (i = 0; i < dmabuf->attributes.n_planes; i++) {
Pekka Paalanena3525802014-06-12 16:49:29 +03001761 /* EGL import does not have modifiers */
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001762 if (dmabuf->attributes.modifier[i] != 0)
Pekka Paalanena3525802014-06-12 16:49:29 +03001763 return false;
1764 }
1765
1766 /* reject all flags we do not recognize or handle */
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001767 if (dmabuf->attributes.flags & ~ZWP_LINUX_BUFFER_PARAMS_V1_FLAGS_Y_INVERT)
Pekka Paalanena3525802014-06-12 16:49:29 +03001768 return false;
1769
1770 image = import_dmabuf(gr, dmabuf);
1771 if (!image)
1772 return false;
1773
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001774 wl_list_insert(&gr->dmabuf_images, &image->link);
1775 linux_dmabuf_buffer_set_user_data(dmabuf, image,
1776 gl_renderer_destroy_dmabuf);
Pekka Paalanena3525802014-06-12 16:49:29 +03001777
1778 return true;
1779}
1780
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001781static bool
1782import_known_dmabuf(struct gl_renderer *gr,
1783 struct dmabuf_image *image)
Pekka Paalanena3525802014-06-12 16:49:29 +03001784{
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001785 switch (image->import_type) {
1786 case IMPORT_TYPE_DIRECT:
1787 image->images[0] = import_simple_dmabuf(gr, &image->dmabuf->attributes);
1788 if (!image->images[0])
1789 return false;
1790 break;
Pekka Paalanena3525802014-06-12 16:49:29 +03001791
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001792 case IMPORT_TYPE_GL_CONVERSION:
1793 if (!import_yuv_dmabuf(gr, image))
1794 return false;
1795 break;
1796
Pekka Paalanena3525802014-06-12 16:49:29 +03001797 default:
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001798 weston_log("Invalid import type for dmabuf\n");
1799 return false;
Pekka Paalanena3525802014-06-12 16:49:29 +03001800 }
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001801
1802 return true;
Pekka Paalanena3525802014-06-12 16:49:29 +03001803}
1804
1805static void
1806gl_renderer_attach_dmabuf(struct weston_surface *surface,
1807 struct weston_buffer *buffer,
1808 struct linux_dmabuf_buffer *dmabuf)
1809{
1810 struct gl_renderer *gr = get_renderer(surface->compositor);
1811 struct gl_surface_state *gs = get_surface_state(surface);
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001812 struct dmabuf_image *image;
Pekka Paalanena3525802014-06-12 16:49:29 +03001813 int i;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001814 int ret;
Pekka Paalanena3525802014-06-12 16:49:29 +03001815
1816 if (!gr->has_dmabuf_import) {
1817 linux_dmabuf_buffer_send_server_error(dmabuf,
1818 "EGL dmabuf import not supported");
1819 return;
1820 }
1821
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001822 buffer->width = dmabuf->attributes.width;
1823 buffer->height = dmabuf->attributes.height;
Pekka Paalanena3525802014-06-12 16:49:29 +03001824 buffer->y_inverted =
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001825 !!(dmabuf->attributes.flags & ZWP_LINUX_BUFFER_PARAMS_V1_FLAGS_Y_INVERT);
Pekka Paalanena3525802014-06-12 16:49:29 +03001826
1827 for (i = 0; i < gs->num_images; i++)
1828 egl_image_unref(gs->images[i]);
1829 gs->num_images = 0;
1830
Pekka Paalanena3525802014-06-12 16:49:29 +03001831 /*
1832 * We try to always hold an imported EGLImage from the dmabuf
1833 * to prevent the client from preventing re-imports. But, we also
1834 * need to re-import every time the contents may change because
1835 * GL driver's caching may need flushing.
1836 *
1837 * Here we release the cache reference which has to be final.
1838 */
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001839 image = linux_dmabuf_buffer_get_user_data(dmabuf);
Pekka Paalanena3525802014-06-12 16:49:29 +03001840
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001841 /* The dmabuf_image should have been created during the import */
1842 assert(image != NULL);
1843
1844 for (i = 0; i < image->num_images; ++i) {
1845 ret = egl_image_unref(image->images[i]);
Pekka Paalanena3525802014-06-12 16:49:29 +03001846 assert(ret == 0);
1847 }
1848
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001849 if (!import_known_dmabuf(gr, image)) {
1850 linux_dmabuf_buffer_send_server_error(dmabuf, "EGL dmabuf import failed");
1851 return;
Pekka Paalanena3525802014-06-12 16:49:29 +03001852 }
Pekka Paalanena3525802014-06-12 16:49:29 +03001853
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001854 gs->num_images = image->num_images;
1855 for (i = 0; i < gs->num_images; ++i)
1856 gs->images[i] = egl_image_ref(image->images[i]);
Pekka Paalanena3525802014-06-12 16:49:29 +03001857
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001858 gs->target = image->target;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001859 ensure_textures(gs, gs->num_images);
1860 for (i = 0; i < gs->num_images; ++i) {
1861 glActiveTexture(GL_TEXTURE0 + i);
1862 glBindTexture(gs->target, gs->textures[i]);
1863 gr->image_target_texture_2d(gs->target, gs->images[i]->image);
1864 }
Pekka Paalanena3525802014-06-12 16:49:29 +03001865
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001866 gs->shader = image->shader;
Pekka Paalanena3525802014-06-12 16:49:29 +03001867 gs->pitch = buffer->width;
1868 gs->height = buffer->height;
1869 gs->buffer_type = BUFFER_TYPE_EGL;
1870 gs->y_inverted = buffer->y_inverted;
1871}
1872
1873static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001874gl_renderer_attach(struct weston_surface *es, struct weston_buffer *buffer)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001875{
1876 struct weston_compositor *ec = es->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001877 struct gl_renderer *gr = get_renderer(ec);
1878 struct gl_surface_state *gs = get_surface_state(es);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001879 struct wl_shm_buffer *shm_buffer;
Pekka Paalanena3525802014-06-12 16:49:29 +03001880 struct linux_dmabuf_buffer *dmabuf;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001881 EGLint format;
1882 int i;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001883
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001884 weston_buffer_reference(&gs->buffer_ref, buffer);
1885
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001886 if (!buffer) {
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001887 for (i = 0; i < gs->num_images; i++) {
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +03001888 egl_image_unref(gs->images[i]);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001889 gs->images[i] = NULL;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001890 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001891 gs->num_images = 0;
1892 glDeleteTextures(gs->num_textures, gs->textures);
1893 gs->num_textures = 0;
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +03001894 gs->buffer_type = BUFFER_TYPE_NULL;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001895 gs->y_inverted = 1;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001896 return;
1897 }
1898
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001899 shm_buffer = wl_shm_buffer_get(buffer->resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001900
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001901 if (shm_buffer)
1902 gl_renderer_attach_shm(es, buffer, shm_buffer);
Kristian Høgsberg47229392013-08-07 11:59:54 -07001903 else if (gr->query_buffer(gr->egl_display, (void *) buffer->resource,
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001904 EGL_TEXTURE_FORMAT, &format))
1905 gl_renderer_attach_egl(es, buffer, format);
Pekka Paalanena3525802014-06-12 16:49:29 +03001906 else if ((dmabuf = linux_dmabuf_buffer_get(buffer->resource)))
1907 gl_renderer_attach_dmabuf(es, buffer, dmabuf);
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001908 else {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001909 weston_log("unhandled buffer type!\n");
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001910 weston_buffer_reference(&gs->buffer_ref, NULL);
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +03001911 gs->buffer_type = BUFFER_TYPE_NULL;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001912 gs->y_inverted = 1;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001913 }
1914}
1915
Kristian Høgsberg42263852012-09-06 21:59:29 -04001916static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001917gl_renderer_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001918 float red, float green, float blue, float alpha)
1919{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001920 struct gl_surface_state *gs = get_surface_state(surface);
1921 struct gl_renderer *gr = get_renderer(surface->compositor);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001922
1923 gs->color[0] = red;
1924 gs->color[1] = green;
1925 gs->color[2] = blue;
1926 gs->color[3] = alpha;
Pekka Paalanenaeb917e2015-02-09 13:56:56 +02001927 gs->buffer_type = BUFFER_TYPE_SOLID;
1928 gs->pitch = 1;
1929 gs->height = 1;
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001930
John Kåre Alsaker40684142012-11-13 19:10:25 +01001931 gs->shader = &gr->solid_shader;
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001932}
1933
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03001934static void
Pekka Paalaneneb35cbe2015-02-09 13:37:27 +02001935gl_renderer_surface_get_content_size(struct weston_surface *surface,
1936 int *width, int *height)
1937{
1938 struct gl_surface_state *gs = get_surface_state(surface);
1939
1940 if (gs->buffer_type == BUFFER_TYPE_NULL) {
1941 *width = 0;
1942 *height = 0;
1943 } else {
1944 *width = gs->pitch;
1945 *height = gs->height;
1946 }
1947}
1948
1949static uint32_t
1950pack_color(pixman_format_code_t format, float *c)
1951{
1952 uint8_t r = round(c[0] * 255.0f);
1953 uint8_t g = round(c[1] * 255.0f);
1954 uint8_t b = round(c[2] * 255.0f);
1955 uint8_t a = round(c[3] * 255.0f);
1956
1957 switch (format) {
1958 case PIXMAN_a8b8g8r8:
1959 return (a << 24) | (b << 16) | (g << 8) | r;
1960 default:
1961 assert(0);
1962 return 0;
1963 }
1964}
1965
1966static int
1967gl_renderer_surface_copy_content(struct weston_surface *surface,
1968 void *target, size_t size,
1969 int src_x, int src_y,
1970 int width, int height)
1971{
1972 static const GLfloat verts[4 * 2] = {
1973 0.0f, 0.0f,
1974 1.0f, 0.0f,
1975 1.0f, 1.0f,
1976 0.0f, 1.0f
1977 };
1978 static const GLfloat projmat_normal[16] = { /* transpose */
1979 2.0f, 0.0f, 0.0f, 0.0f,
1980 0.0f, 2.0f, 0.0f, 0.0f,
1981 0.0f, 0.0f, 1.0f, 0.0f,
1982 -1.0f, -1.0f, 0.0f, 1.0f
1983 };
1984 static const GLfloat projmat_yinvert[16] = { /* transpose */
1985 2.0f, 0.0f, 0.0f, 0.0f,
1986 0.0f, -2.0f, 0.0f, 0.0f,
1987 0.0f, 0.0f, 1.0f, 0.0f,
1988 -1.0f, 1.0f, 0.0f, 1.0f
1989 };
1990 const pixman_format_code_t format = PIXMAN_a8b8g8r8;
1991 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
1992 const GLenum gl_format = GL_RGBA; /* PIXMAN_a8b8g8r8 little-endian */
1993 struct gl_renderer *gr = get_renderer(surface->compositor);
1994 struct gl_surface_state *gs = get_surface_state(surface);
1995 int cw, ch;
1996 GLuint fbo;
1997 GLuint tex;
1998 GLenum status;
1999 const GLfloat *proj;
2000 int i;
2001
2002 gl_renderer_surface_get_content_size(surface, &cw, &ch);
2003
2004 switch (gs->buffer_type) {
2005 case BUFFER_TYPE_NULL:
2006 return -1;
2007 case BUFFER_TYPE_SOLID:
2008 *(uint32_t *)target = pack_color(format, gs->color);
2009 return 0;
2010 case BUFFER_TYPE_SHM:
2011 gl_renderer_flush_damage(surface);
2012 /* fall through */
2013 case BUFFER_TYPE_EGL:
2014 break;
2015 }
2016
2017 glGenTextures(1, &tex);
2018 glBindTexture(GL_TEXTURE_2D, tex);
2019 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, cw, ch,
2020 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
2021 glBindTexture(GL_TEXTURE_2D, 0);
2022
2023 glGenFramebuffers(1, &fbo);
2024 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
2025 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
2026 GL_TEXTURE_2D, tex, 0);
2027
2028 status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
2029 if (status != GL_FRAMEBUFFER_COMPLETE) {
2030 weston_log("%s: fbo error: %#x\n", __func__, status);
2031 glDeleteFramebuffers(1, &fbo);
2032 glDeleteTextures(1, &tex);
2033 return -1;
2034 }
2035
2036 glViewport(0, 0, cw, ch);
2037 glDisable(GL_BLEND);
2038 use_shader(gr, gs->shader);
2039 if (gs->y_inverted)
2040 proj = projmat_normal;
2041 else
2042 proj = projmat_yinvert;
2043
2044 glUniformMatrix4fv(gs->shader->proj_uniform, 1, GL_FALSE, proj);
2045 glUniform1f(gs->shader->alpha_uniform, 1.0f);
2046
2047 for (i = 0; i < gs->num_textures; i++) {
2048 glUniform1i(gs->shader->tex_uniforms[i], i);
2049
2050 glActiveTexture(GL_TEXTURE0 + i);
2051 glBindTexture(gs->target, gs->textures[i]);
2052 glTexParameteri(gs->target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2053 glTexParameteri(gs->target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2054 }
2055
2056 /* position: */
2057 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, verts);
2058 glEnableVertexAttribArray(0);
2059
2060 /* texcoord: */
2061 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, verts);
2062 glEnableVertexAttribArray(1);
2063
2064 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2065
2066 glDisableVertexAttribArray(1);
2067 glDisableVertexAttribArray(0);
2068
2069 glPixelStorei(GL_PACK_ALIGNMENT, bytespp);
2070 glReadPixels(src_x, src_y, width, height, gl_format,
2071 GL_UNSIGNED_BYTE, target);
2072
2073 glDeleteFramebuffers(1, &fbo);
2074 glDeleteTextures(1, &tex);
2075
2076 return 0;
2077}
2078
2079static void
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002080surface_state_destroy(struct gl_surface_state *gs, struct gl_renderer *gr)
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002081{
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002082 int i;
2083
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002084 wl_list_remove(&gs->surface_destroy_listener.link);
2085 wl_list_remove(&gs->renderer_destroy_listener.link);
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002086
2087 gs->surface->renderer_state = NULL;
2088
2089 glDeleteTextures(gs->num_textures, gs->textures);
2090
2091 for (i = 0; i < gs->num_images; i++)
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +03002092 egl_image_unref(gs->images[i]);
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002093
2094 weston_buffer_reference(&gs->buffer_ref, NULL);
2095 pixman_region32_fini(&gs->texture_damage);
2096 free(gs);
2097}
2098
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002099static void
2100surface_state_handle_surface_destroy(struct wl_listener *listener, void *data)
2101{
2102 struct gl_surface_state *gs;
2103 struct gl_renderer *gr;
2104
2105 gs = container_of(listener, struct gl_surface_state,
2106 surface_destroy_listener);
2107
2108 gr = get_renderer(gs->surface->compositor);
2109
2110 surface_state_destroy(gs, gr);
2111}
2112
2113static void
2114surface_state_handle_renderer_destroy(struct wl_listener *listener, void *data)
2115{
2116 struct gl_surface_state *gs;
2117 struct gl_renderer *gr;
2118
2119 gr = data;
2120
2121 gs = container_of(listener, struct gl_surface_state,
2122 renderer_destroy_listener);
2123
2124 surface_state_destroy(gs, gr);
2125}
2126
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002127static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002128gl_renderer_create_surface(struct weston_surface *surface)
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002129{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002130 struct gl_surface_state *gs;
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002131 struct gl_renderer *gr = get_renderer(surface->compositor);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002132
Bryce Harringtonde16d892014-11-20 22:21:57 -08002133 gs = zalloc(sizeof *gs);
2134 if (gs == NULL)
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002135 return -1;
2136
Pekka Paalanen68033ac2012-12-04 15:58:15 +02002137 /* A buffer is never attached to solid color surfaces, yet
2138 * they still go through texcoord computations. Do not divide
2139 * by zero there.
2140 */
2141 gs->pitch = 1;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002142 gs->y_inverted = 1;
Pekka Paalanen68033ac2012-12-04 15:58:15 +02002143
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002144 gs->surface = surface;
2145
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02002146 pixman_region32_init(&gs->texture_damage);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002147 surface->renderer_state = gs;
2148
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002149 gs->surface_destroy_listener.notify =
2150 surface_state_handle_surface_destroy;
2151 wl_signal_add(&surface->destroy_signal,
2152 &gs->surface_destroy_listener);
2153
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002154 gs->renderer_destroy_listener.notify =
2155 surface_state_handle_renderer_destroy;
2156 wl_signal_add(&gr->destroy_signal,
2157 &gs->renderer_destroy_listener);
2158
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02002159 if (surface->buffer_ref.buffer) {
2160 gl_renderer_attach(surface, surface->buffer_ref.buffer);
2161 gl_renderer_flush_damage(surface);
2162 }
2163
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002164 return 0;
2165}
2166
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002167static const char vertex_shader[] =
2168 "uniform mat4 proj;\n"
2169 "attribute vec2 position;\n"
2170 "attribute vec2 texcoord;\n"
2171 "varying vec2 v_texcoord;\n"
2172 "void main()\n"
2173 "{\n"
2174 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
2175 " v_texcoord = texcoord;\n"
2176 "}\n";
2177
2178/* Declare common fragment shader uniforms */
2179#define FRAGMENT_CONVERT_YUV \
2180 " y *= alpha;\n" \
2181 " u *= alpha;\n" \
2182 " v *= alpha;\n" \
2183 " gl_FragColor.r = y + 1.59602678 * v;\n" \
2184 " gl_FragColor.g = y - 0.39176229 * u - 0.81296764 * v;\n" \
2185 " gl_FragColor.b = y + 2.01723214 * u;\n" \
2186 " gl_FragColor.a = alpha;\n"
2187
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002188static const char fragment_debug[] =
2189 " gl_FragColor = vec4(0.0, 0.3, 0.0, 0.2) + gl_FragColor * 0.8;\n";
2190
2191static const char fragment_brace[] =
2192 "}\n";
2193
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002194static const char texture_fragment_shader_rgba[] =
2195 "precision mediump float;\n"
2196 "varying vec2 v_texcoord;\n"
2197 "uniform sampler2D tex;\n"
2198 "uniform float alpha;\n"
2199 "void main()\n"
2200 "{\n"
2201 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002202 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002203
2204static const char texture_fragment_shader_rgbx[] =
2205 "precision mediump float;\n"
2206 "varying vec2 v_texcoord;\n"
2207 "uniform sampler2D tex;\n"
2208 "uniform float alpha;\n"
2209 "void main()\n"
2210 "{\n"
2211 " gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb\n;"
2212 " gl_FragColor.a = alpha;\n"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002213 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002214
2215static const char texture_fragment_shader_egl_external[] =
2216 "#extension GL_OES_EGL_image_external : require\n"
2217 "precision mediump float;\n"
2218 "varying vec2 v_texcoord;\n"
2219 "uniform samplerExternalOES tex;\n"
2220 "uniform float alpha;\n"
2221 "void main()\n"
2222 "{\n"
2223 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002224 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002225
2226static const char texture_fragment_shader_y_uv[] =
2227 "precision mediump float;\n"
2228 "uniform sampler2D tex;\n"
2229 "uniform sampler2D tex1;\n"
2230 "varying vec2 v_texcoord;\n"
2231 "uniform float alpha;\n"
2232 "void main() {\n"
2233 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
2234 " float u = texture2D(tex1, v_texcoord).r - 0.5;\n"
2235 " float v = texture2D(tex1, v_texcoord).g - 0.5;\n"
2236 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002237 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002238
2239static const char texture_fragment_shader_y_u_v[] =
2240 "precision mediump float;\n"
2241 "uniform sampler2D tex;\n"
2242 "uniform sampler2D tex1;\n"
2243 "uniform sampler2D tex2;\n"
2244 "varying vec2 v_texcoord;\n"
2245 "uniform float alpha;\n"
2246 "void main() {\n"
2247 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
2248 " float u = texture2D(tex1, v_texcoord).x - 0.5;\n"
2249 " float v = texture2D(tex2, v_texcoord).x - 0.5;\n"
2250 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002251 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002252
2253static const char texture_fragment_shader_y_xuxv[] =
2254 "precision mediump float;\n"
2255 "uniform sampler2D tex;\n"
2256 "uniform sampler2D tex1;\n"
2257 "varying vec2 v_texcoord;\n"
2258 "uniform float alpha;\n"
2259 "void main() {\n"
2260 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
2261 " float u = texture2D(tex1, v_texcoord).g - 0.5;\n"
2262 " float v = texture2D(tex1, v_texcoord).a - 0.5;\n"
2263 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002264 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002265
2266static const char solid_fragment_shader[] =
2267 "precision mediump float;\n"
2268 "uniform vec4 color;\n"
2269 "uniform float alpha;\n"
2270 "void main()\n"
2271 "{\n"
2272 " gl_FragColor = alpha * color\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002273 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002274
2275static int
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002276compile_shader(GLenum type, int count, const char **sources)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002277{
2278 GLuint s;
2279 char msg[512];
2280 GLint status;
2281
2282 s = glCreateShader(type);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002283 glShaderSource(s, count, sources, NULL);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002284 glCompileShader(s);
2285 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
2286 if (!status) {
2287 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
2288 weston_log("shader info: %s\n", msg);
2289 return GL_NONE;
2290 }
2291
2292 return s;
2293}
2294
2295static int
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03002296shader_init(struct gl_shader *shader, struct gl_renderer *renderer,
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002297 const char *vertex_source, const char *fragment_source)
2298{
2299 char msg[512];
2300 GLint status;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002301 int count;
2302 const char *sources[3];
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002303
2304 shader->vertex_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002305 compile_shader(GL_VERTEX_SHADER, 1, &vertex_source);
2306
2307 if (renderer->fragment_shader_debug) {
2308 sources[0] = fragment_source;
2309 sources[1] = fragment_debug;
2310 sources[2] = fragment_brace;
2311 count = 3;
2312 } else {
2313 sources[0] = fragment_source;
2314 sources[1] = fragment_brace;
2315 count = 2;
2316 }
2317
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002318 shader->fragment_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002319 compile_shader(GL_FRAGMENT_SHADER, count, sources);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002320
2321 shader->program = glCreateProgram();
2322 glAttachShader(shader->program, shader->vertex_shader);
2323 glAttachShader(shader->program, shader->fragment_shader);
2324 glBindAttribLocation(shader->program, 0, "position");
2325 glBindAttribLocation(shader->program, 1, "texcoord");
2326
2327 glLinkProgram(shader->program);
2328 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
2329 if (!status) {
2330 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
2331 weston_log("link info: %s\n", msg);
2332 return -1;
2333 }
2334
2335 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
2336 shader->tex_uniforms[0] = glGetUniformLocation(shader->program, "tex");
2337 shader->tex_uniforms[1] = glGetUniformLocation(shader->program, "tex1");
2338 shader->tex_uniforms[2] = glGetUniformLocation(shader->program, "tex2");
2339 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
2340 shader->color_uniform = glGetUniformLocation(shader->program, "color");
2341
2342 return 0;
2343}
2344
2345static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002346shader_release(struct gl_shader *shader)
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002347{
2348 glDeleteShader(shader->vertex_shader);
2349 glDeleteShader(shader->fragment_shader);
2350 glDeleteProgram(shader->program);
2351
2352 shader->vertex_shader = 0;
2353 shader->fragment_shader = 0;
2354 shader->program = 0;
2355}
2356
2357static void
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002358log_extensions(const char *name, const char *extensions)
2359{
2360 const char *p, *end;
2361 int l;
2362 int len;
2363
2364 l = weston_log("%s:", name);
2365 p = extensions;
2366 while (*p) {
2367 end = strchrnul(p, ' ');
2368 len = end - p;
2369 if (l + len > 78)
2370 l = weston_log_continue("\n" STAMP_SPACE "%.*s",
2371 len, p);
2372 else
2373 l += weston_log_continue(" %.*s", len, p);
2374 for (p = end; isspace(*p); p++)
2375 ;
2376 }
2377 weston_log_continue("\n");
2378}
2379
2380static void
2381log_egl_gl_info(EGLDisplay egldpy)
2382{
2383 const char *str;
2384
2385 str = eglQueryString(egldpy, EGL_VERSION);
2386 weston_log("EGL version: %s\n", str ? str : "(null)");
2387
2388 str = eglQueryString(egldpy, EGL_VENDOR);
2389 weston_log("EGL vendor: %s\n", str ? str : "(null)");
2390
2391 str = eglQueryString(egldpy, EGL_CLIENT_APIS);
2392 weston_log("EGL client APIs: %s\n", str ? str : "(null)");
2393
2394 str = eglQueryString(egldpy, EGL_EXTENSIONS);
2395 log_extensions("EGL extensions", str ? str : "(null)");
2396
2397 str = (char *)glGetString(GL_VERSION);
2398 weston_log("GL version: %s\n", str ? str : "(null)");
2399
2400 str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
2401 weston_log("GLSL version: %s\n", str ? str : "(null)");
2402
2403 str = (char *)glGetString(GL_VENDOR);
2404 weston_log("GL vendor: %s\n", str ? str : "(null)");
2405
2406 str = (char *)glGetString(GL_RENDERER);
2407 weston_log("GL renderer: %s\n", str ? str : "(null)");
2408
2409 str = (char *)glGetString(GL_EXTENSIONS);
2410 log_extensions("GL extensions", str ? str : "(null)");
2411}
2412
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03002413static void
2414log_egl_config_info(EGLDisplay egldpy, EGLConfig eglconfig)
2415{
2416 EGLint r, g, b, a;
2417
2418 weston_log("Chosen EGL config details:\n");
2419
2420 weston_log_continue(STAMP_SPACE "RGBA bits");
2421 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_RED_SIZE, &r) &&
2422 eglGetConfigAttrib(egldpy, eglconfig, EGL_GREEN_SIZE, &g) &&
2423 eglGetConfigAttrib(egldpy, eglconfig, EGL_BLUE_SIZE, &b) &&
2424 eglGetConfigAttrib(egldpy, eglconfig, EGL_ALPHA_SIZE, &a))
2425 weston_log_continue(": %d %d %d %d\n", r, g, b, a);
2426 else
2427 weston_log_continue(" unknown\n");
2428
2429 weston_log_continue(STAMP_SPACE "swap interval range");
2430 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_MIN_SWAP_INTERVAL, &a) &&
2431 eglGetConfigAttrib(egldpy, eglconfig, EGL_MAX_SWAP_INTERVAL, &b))
2432 weston_log_continue(": %d - %d\n", a, b);
2433 else
2434 weston_log_continue(" unknown\n");
2435}
2436
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002437static int
Derek Foremane76f1852015-05-15 12:12:39 -05002438match_config_to_visual(EGLDisplay egl_display,
2439 EGLint visual_id,
2440 EGLConfig *configs,
2441 int count)
2442{
2443 int i;
2444
2445 for (i = 0; i < count; ++i) {
2446 EGLint id;
2447
2448 if (!eglGetConfigAttrib(egl_display,
2449 configs[i], EGL_NATIVE_VISUAL_ID,
2450 &id))
2451 continue;
2452
2453 if (id == visual_id)
2454 return i;
2455 }
2456
Derek Foremane76f1852015-05-15 12:12:39 -05002457 return -1;
2458}
2459
2460static int
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002461egl_choose_config(struct gl_renderer *gr, const EGLint *attribs,
Derek Foremane76f1852015-05-15 12:12:39 -05002462 const EGLint *visual_id, const int n_ids,
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002463 EGLConfig *config_out)
2464{
2465 EGLint count = 0;
2466 EGLint matched = 0;
2467 EGLConfig *configs;
Derek Foremane76f1852015-05-15 12:12:39 -05002468 int i, config_index = -1;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002469
Derek Foremana7e19912015-05-20 14:57:58 -05002470 if (!eglGetConfigs(gr->egl_display, NULL, 0, &count) || count < 1) {
2471 weston_log("No EGL configs to choose from.\n");
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002472 return -1;
Derek Foremana7e19912015-05-20 14:57:58 -05002473 }
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002474 configs = calloc(count, sizeof *configs);
2475 if (!configs)
2476 return -1;
2477
2478 if (!eglChooseConfig(gr->egl_display, attribs, configs,
Derek Foremana7e19912015-05-20 14:57:58 -05002479 count, &matched) || !matched) {
2480 weston_log("No EGL configs with appropriate attributes.\n");
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002481 goto out;
Derek Foremana7e19912015-05-20 14:57:58 -05002482 }
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002483
Derek Foremane76f1852015-05-15 12:12:39 -05002484 if (!visual_id)
2485 config_index = 0;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002486
Derek Foremane76f1852015-05-15 12:12:39 -05002487 for (i = 0; config_index == -1 && i < n_ids; i++)
2488 config_index = match_config_to_visual(gr->egl_display,
2489 visual_id[i],
2490 configs,
2491 matched);
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002492
Derek Foremane76f1852015-05-15 12:12:39 -05002493 if (config_index != -1)
2494 *config_out = configs[config_index];
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002495
2496out:
2497 free(configs);
Derek Foremane76f1852015-05-15 12:12:39 -05002498 if (config_index == -1)
2499 return -1;
2500
Derek Foremana7e19912015-05-20 14:57:58 -05002501 if (i > 1)
2502 weston_log("Unable to use first choice EGL config with id"
2503 " 0x%x, succeeded with alternate id 0x%x.\n",
2504 visual_id[0], visual_id[i - 1]);
Derek Foremane76f1852015-05-15 12:12:39 -05002505 return 0;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002506}
2507
John Kåre Alsaker44154502012-11-13 19:10:20 +01002508static void
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05002509gl_renderer_output_set_border(struct weston_output *output,
2510 enum gl_renderer_border_side side,
2511 int32_t width, int32_t height,
2512 int32_t tex_width, unsigned char *data)
2513{
2514 struct gl_output_state *go = get_output_state(output);
2515
Jason Ekstrande5512d42014-02-04 21:36:38 -06002516 if (go->borders[side].width != width ||
2517 go->borders[side].height != height)
2518 /* In this case, we have to blow everything and do a full
2519 * repaint. */
2520 go->border_status |= BORDER_SIZE_CHANGED | BORDER_ALL_DIRTY;
2521
2522 if (data == NULL) {
2523 width = 0;
2524 height = 0;
2525 }
2526
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05002527 go->borders[side].width = width;
2528 go->borders[side].height = height;
2529 go->borders[side].tex_width = tex_width;
2530 go->borders[side].data = data;
Jason Ekstrande5512d42014-02-04 21:36:38 -06002531 go->border_status |= 1 << side;
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05002532}
2533
John Kåre Alsaker94659272012-11-13 19:10:18 +01002534static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002535gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01002536
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002537static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002538gl_renderer_output_create(struct weston_output *output,
Jonny Lamb671148f2015-03-20 15:26:52 +01002539 EGLNativeWindowType window_for_legacy,
2540 void *window_for_platform,
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002541 const EGLint *attribs,
Derek Foremane76f1852015-05-15 12:12:39 -05002542 const EGLint *visual_id,
2543 int n_ids)
John Kåre Alsaker94659272012-11-13 19:10:18 +01002544{
2545 struct weston_compositor *ec = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002546 struct gl_renderer *gr = get_renderer(ec);
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002547 struct gl_output_state *go;
2548 EGLConfig egl_config;
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002549 int i;
John Kåre Alsaker94659272012-11-13 19:10:18 +01002550
Derek Foremane76f1852015-05-15 12:12:39 -05002551 if (egl_choose_config(gr, attribs, visual_id,
2552 n_ids, &egl_config) == -1) {
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002553 weston_log("failed to choose EGL config for output\n");
2554 return -1;
2555 }
2556
2557 if (egl_config != gr->egl_config &&
2558 !gr->has_configless_context) {
2559 weston_log("attempted to use a different EGL config for an "
2560 "output but EGL_MESA_configless_context is not "
2561 "supported\n");
2562 return -1;
2563 }
2564
Bryce Harringtonde16d892014-11-20 22:21:57 -08002565 go = zalloc(sizeof *go);
2566 if (go == NULL)
John Kåre Alsaker94659272012-11-13 19:10:18 +01002567 return -1;
2568
Jonny Lamb671148f2015-03-20 15:26:52 +01002569 if (gr->create_platform_window) {
2570 go->egl_surface =
2571 gr->create_platform_window(gr->egl_display,
2572 egl_config,
2573 window_for_platform,
2574 NULL);
Jonny Lambf1ec5062015-03-24 13:12:05 +01002575 } else {
Jonny Lamb671148f2015-03-20 15:26:52 +01002576 go->egl_surface =
2577 eglCreateWindowSurface(gr->egl_display,
2578 egl_config,
2579 window_for_legacy, NULL);
Jonny Lambf1ec5062015-03-24 13:12:05 +01002580 }
John Kåre Alsaker94659272012-11-13 19:10:18 +01002581
2582 if (go->egl_surface == EGL_NO_SURFACE) {
2583 weston_log("failed to create egl surface\n");
2584 free(go);
2585 return -1;
2586 }
2587
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02002588 for (i = 0; i < BUFFER_DAMAGE_COUNT; i++)
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002589 pixman_region32_init(&go->buffer_damage[i]);
2590
John Kåre Alsaker94659272012-11-13 19:10:18 +01002591 output->renderer_state = go;
2592
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002593 log_egl_config_info(gr->egl_display, egl_config);
2594
John Kåre Alsaker94659272012-11-13 19:10:18 +01002595 return 0;
2596}
2597
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002598static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002599gl_renderer_output_destroy(struct weston_output *output)
John Kåre Alsaker94659272012-11-13 19:10:18 +01002600{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002601 struct gl_renderer *gr = get_renderer(output->compositor);
2602 struct gl_output_state *go = get_output_state(output);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002603 int i;
2604
2605 for (i = 0; i < 2; i++)
2606 pixman_region32_fini(&go->buffer_damage[i]);
John Kåre Alsaker94659272012-11-13 19:10:18 +01002607
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002608 eglDestroySurface(gr->egl_display, go->egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01002609
2610 free(go);
2611}
2612
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002613static EGLSurface
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002614gl_renderer_output_surface(struct weston_output *output)
John Kåre Alsaker94659272012-11-13 19:10:18 +01002615{
2616 return get_output_state(output)->egl_surface;
2617}
2618
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03002619static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002620gl_renderer_destroy(struct weston_compositor *ec)
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04002621{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002622 struct gl_renderer *gr = get_renderer(ec);
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00002623 struct dmabuf_image *image, *next;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002624
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002625 wl_signal_emit(&gr->destroy_signal, gr);
2626
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002627 if (gr->has_bind_display)
2628 gr->unbind_display(gr->egl_display, ec->wl_display);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002629
2630 /* Work around crash in egl_dri2.c's dri2_make_current() - when does this apply? */
2631 eglMakeCurrent(gr->egl_display,
2632 EGL_NO_SURFACE, EGL_NO_SURFACE,
2633 EGL_NO_CONTEXT);
2634
Pekka Paalanena3525802014-06-12 16:49:29 +03002635
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00002636 wl_list_for_each_safe(image, next, &gr->dmabuf_images, link)
2637 dmabuf_image_destroy(image);
Pekka Paalanena3525802014-06-12 16:49:29 +03002638
Armin Krezović28d240f2016-06-23 11:59:35 +02002639 if (gr->dummy_surface != EGL_NO_SURFACE)
2640 eglDestroySurface(gr->egl_display, gr->dummy_surface);
2641
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002642 eglTerminate(gr->egl_display);
2643 eglReleaseThread();
Scott Moreau976a0502013-03-07 10:15:17 -07002644
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -04002645 wl_array_release(&gr->vertices);
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -04002646 wl_array_release(&gr->vtxcnt);
2647
Mariusz Ceiercbb91582014-02-08 20:11:24 +01002648 if (gr->fragment_binding)
2649 weston_binding_destroy(gr->fragment_binding);
2650 if (gr->fan_binding)
2651 weston_binding_destroy(gr->fan_binding);
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03002652
Scott Moreau976a0502013-03-07 10:15:17 -07002653 free(gr);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002654}
2655
Pekka Paalanen8b69d032015-04-08 17:02:22 +03002656static void
2657renderer_setup_egl_client_extensions(struct gl_renderer *gr)
2658{
2659 const char *extensions;
2660
2661 extensions = eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS);
2662 if (!extensions) {
2663 weston_log("Retrieving EGL client extension string failed.\n");
2664 return;
2665 }
2666
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002667 if (weston_check_egl_extension(extensions, "EGL_EXT_platform_base"))
Pekka Paalanen8b69d032015-04-08 17:02:22 +03002668 gr->create_platform_window =
2669 (void *) eglGetProcAddress("eglCreatePlatformWindowSurfaceEXT");
2670 else
2671 weston_log("warning: EGL_EXT_platform_base not supported.\n");
2672}
2673
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002674static int
Neil Robertsb7f85332014-03-07 18:05:49 +00002675gl_renderer_setup_egl_extensions(struct weston_compositor *ec)
2676{
2677 struct gl_renderer *gr = get_renderer(ec);
2678 const char *extensions;
2679 EGLBoolean ret;
2680
2681 gr->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2682 gr->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2683 gr->bind_display =
2684 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
2685 gr->unbind_display =
2686 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
2687 gr->query_buffer =
2688 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
2689
2690 extensions =
2691 (const char *) eglQueryString(gr->egl_display, EGL_EXTENSIONS);
2692 if (!extensions) {
2693 weston_log("Retrieving EGL extension string failed.\n");
2694 return -1;
2695 }
2696
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002697 if (weston_check_egl_extension(extensions, "EGL_WL_bind_wayland_display"))
Neil Robertsb7f85332014-03-07 18:05:49 +00002698 gr->has_bind_display = 1;
2699 if (gr->has_bind_display) {
2700 ret = gr->bind_display(gr->egl_display, ec->wl_display);
2701 if (!ret)
2702 gr->has_bind_display = 0;
2703 }
2704
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002705 if (weston_check_egl_extension(extensions, "EGL_EXT_buffer_age"))
Neil Robertsb7f85332014-03-07 18:05:49 +00002706 gr->has_egl_buffer_age = 1;
2707 else
2708 weston_log("warning: EGL_EXT_buffer_age not supported. "
2709 "Performance could be affected.\n");
2710
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002711 if (weston_check_egl_extension(extensions, "EGL_EXT_swap_buffers_with_damage"))
Neil Robertsb7f85332014-03-07 18:05:49 +00002712 gr->swap_buffers_with_damage =
2713 (void *) eglGetProcAddress("eglSwapBuffersWithDamageEXT");
2714 else
2715 weston_log("warning: EGL_EXT_swap_buffers_with_damage not "
2716 "supported. Performance could be affected.\n");
Neil Robertsb7f85332014-03-07 18:05:49 +00002717
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002718 if (weston_check_egl_extension(extensions, "EGL_MESA_configless_context"))
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002719 gr->has_configless_context = 1;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002720
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002721 if (weston_check_egl_extension(extensions, "EGL_KHR_surfaceless_context"))
Armin Krezović28d240f2016-06-23 11:59:35 +02002722 gr->has_surfaceless_context = 1;
2723
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002724 if (weston_check_egl_extension(extensions, "EGL_EXT_image_dma_buf_import"))
Pekka Paalanena3525802014-06-12 16:49:29 +03002725 gr->has_dmabuf_import = 1;
Pekka Paalanena3525802014-06-12 16:49:29 +03002726
Pekka Paalanen8b69d032015-04-08 17:02:22 +03002727 renderer_setup_egl_client_extensions(gr);
2728
Neil Robertsb7f85332014-03-07 18:05:49 +00002729 return 0;
2730}
2731
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002732static const EGLint gl_renderer_opaque_attribs[] = {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002733 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
2734 EGL_RED_SIZE, 1,
2735 EGL_GREEN_SIZE, 1,
2736 EGL_BLUE_SIZE, 1,
2737 EGL_ALPHA_SIZE, 0,
2738 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
2739 EGL_NONE
2740};
2741
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002742static const EGLint gl_renderer_alpha_attribs[] = {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002743 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
2744 EGL_RED_SIZE, 1,
2745 EGL_GREEN_SIZE, 1,
2746 EGL_BLUE_SIZE, 1,
2747 EGL_ALPHA_SIZE, 1,
2748 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
2749 EGL_NONE
2750};
2751
Armin Krezović28d240f2016-06-23 11:59:35 +02002752
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002753/** Checks whether a platform EGL client extension is supported
2754 *
2755 * \param ec The weston compositor
2756 * \param extension_suffix The EGL client extension suffix
2757 * \return 1 if supported, 0 if using fallbacks, -1 unsupported
2758 *
2759 * This function checks whether a specific platform_* extension is supported
2760 * by EGL.
2761 *
2762 * The extension suffix should be the suffix of the platform extension (that
2763 * specifies a <platform> argument as defined in EGL_EXT_platform_base). For
2764 * example, passing "foo" will check whether either "EGL_KHR_platform_foo",
2765 * "EGL_EXT_platform_foo", or "EGL_MESA_platform_foo" is supported.
2766 *
2767 * The return value is 1:
2768 * - if the supplied EGL client extension is supported.
2769 * The return value is 0:
2770 * - if the platform_base client extension isn't supported so will
2771 * fallback to eglGetDisplay and friends.
2772 * The return value is -1:
2773 * - if the supplied EGL client extension is not supported.
2774 */
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002775static int
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002776gl_renderer_supports(struct weston_compositor *ec,
2777 const char *extension_suffix)
2778{
2779 static const char *extensions = NULL;
2780 char s[64];
2781
2782 if (!extensions) {
2783 extensions = (const char *) eglQueryString(
2784 EGL_NO_DISPLAY, EGL_EXTENSIONS);
2785
2786 if (!extensions)
2787 return 0;
2788
2789 log_extensions("EGL client extensions",
2790 extensions);
2791 }
2792
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002793 if (!weston_check_egl_extension(extensions, "EGL_EXT_platform_base"))
Pekka Paalanenf2824542015-04-08 17:02:21 +03002794 return 0;
2795
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002796 snprintf(s, sizeof s, "EGL_KHR_platform_%s", extension_suffix);
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002797 if (weston_check_egl_extension(extensions, s))
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002798 return 1;
2799
2800 snprintf(s, sizeof s, "EGL_EXT_platform_%s", extension_suffix);
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002801 if (weston_check_egl_extension(extensions, s))
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002802 return 1;
2803
2804 snprintf(s, sizeof s, "EGL_MESA_platform_%s", extension_suffix);
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002805 if (weston_check_egl_extension(extensions, s))
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002806 return 1;
2807
Pekka Paalanenf2824542015-04-08 17:02:21 +03002808 /* at this point we definitely have some platform extensions but
2809 * haven't found the supplied platform, so chances are it's
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002810 * not supported. */
2811
2812 return -1;
2813}
2814
Jonny Lamb74eed312015-03-24 13:12:04 +01002815static const char *
2816platform_to_extension(EGLenum platform)
2817{
2818 switch (platform) {
2819 case EGL_PLATFORM_GBM_KHR:
2820 return "gbm";
2821 case EGL_PLATFORM_WAYLAND_KHR:
2822 return "wayland";
2823 case EGL_PLATFORM_X11_KHR:
2824 return "x11";
2825 default:
2826 assert(0 && "bad EGL platform enum");
2827 }
2828}
2829
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002830static int
Armin Krezović28d240f2016-06-23 11:59:35 +02002831gl_renderer_create_pbuffer_surface(struct gl_renderer *gr) {
2832 EGLConfig pbuffer_config;
2833
2834 static const EGLint pbuffer_config_attribs[] = {
2835 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
2836 EGL_RED_SIZE, 1,
2837 EGL_GREEN_SIZE, 1,
2838 EGL_BLUE_SIZE, 1,
2839 EGL_ALPHA_SIZE, 0,
2840 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
2841 EGL_NONE
2842 };
2843
2844 static const EGLint pbuffer_attribs[] = {
2845 EGL_WIDTH, 10,
2846 EGL_HEIGHT, 10,
2847 EGL_NONE
2848 };
2849
2850 if (egl_choose_config(gr, pbuffer_config_attribs, NULL, 0, &pbuffer_config) < 0) {
2851 weston_log("failed to choose EGL config for PbufferSurface");
2852 return -1;
2853 }
2854
2855 gr->dummy_surface = eglCreatePbufferSurface(gr->egl_display,
2856 pbuffer_config,
2857 pbuffer_attribs);
2858
2859 if (gr->dummy_surface == EGL_NO_SURFACE) {
2860 weston_log("failed to create PbufferSurface\n");
2861 return -1;
2862 }
2863
2864 return 0;
2865}
2866
2867static int
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002868gl_renderer_create(struct weston_compositor *ec, EGLenum platform,
2869 void *native_window, const EGLint *attribs,
Derek Foremane76f1852015-05-15 12:12:39 -05002870 const EGLint *visual_id, int n_ids)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002871{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002872 struct gl_renderer *gr;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002873 EGLint major, minor;
Jonny Lamb74eed312015-03-24 13:12:04 +01002874 int supports = 0;
2875
2876 if (platform) {
2877 supports = gl_renderer_supports(
2878 ec, platform_to_extension(platform));
2879 if (supports < 0)
2880 return -1;
2881 }
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002882
Bryce Harringtonde16d892014-11-20 22:21:57 -08002883 gr = zalloc(sizeof *gr);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002884 if (gr == NULL)
2885 return -1;
2886
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002887 gr->base.read_pixels = gl_renderer_read_pixels;
2888 gr->base.repaint_output = gl_renderer_repaint_output;
2889 gr->base.flush_damage = gl_renderer_flush_damage;
2890 gr->base.attach = gl_renderer_attach;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002891 gr->base.surface_set_color = gl_renderer_surface_set_color;
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03002892 gr->base.destroy = gl_renderer_destroy;
Pekka Paalaneneb35cbe2015-02-09 13:37:27 +02002893 gr->base.surface_get_content_size =
2894 gl_renderer_surface_get_content_size;
2895 gr->base.surface_copy_content = gl_renderer_surface_copy_content;
Jonny Lamb74eed312015-03-24 13:12:04 +01002896 gr->egl_display = NULL;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002897
Jonny Lambf1ec5062015-03-24 13:12:05 +01002898 /* extension_suffix is supported */
Jonny Lamb74eed312015-03-24 13:12:04 +01002899 if (supports) {
2900 if (!get_platform_display) {
2901 get_platform_display = (void *) eglGetProcAddress(
2902 "eglGetPlatformDisplayEXT");
2903 }
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002904
Jonny Lamb74eed312015-03-24 13:12:04 +01002905 /* also wrap this in the supports check because
2906 * eglGetProcAddress can return non-NULL and still not
2907 * support the feature at runtime, so ensure the
2908 * appropriate extension checks have been done. */
2909 if (get_platform_display && platform) {
2910 gr->egl_display = get_platform_display(platform,
2911 native_window,
2912 NULL);
2913 }
2914 }
Jonny Lamb74eed312015-03-24 13:12:04 +01002915
2916 if (!gr->egl_display) {
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002917 weston_log("warning: either no EGL_EXT_platform_base "
2918 "support or specific platform support; "
2919 "falling back to eglGetDisplay.\n");
2920 gr->egl_display = eglGetDisplay(native_window);
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002921 }
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002922
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002923 if (gr->egl_display == EGL_NO_DISPLAY) {
2924 weston_log("failed to create display\n");
Derek Foreman066ca0c2015-06-11 12:14:45 -05002925 goto fail;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002926 }
2927
2928 if (!eglInitialize(gr->egl_display, &major, &minor)) {
2929 weston_log("failed to initialize display\n");
Derek Foreman066ca0c2015-06-11 12:14:45 -05002930 goto fail_with_error;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002931 }
2932
Derek Foremane76f1852015-05-15 12:12:39 -05002933 if (egl_choose_config(gr, attribs, visual_id,
2934 n_ids, &gr->egl_config) < 0) {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002935 weston_log("failed to choose EGL config\n");
Dawid Gajownik1a912a92015-08-21 00:20:54 -03002936 goto fail_terminate;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002937 }
2938
2939 ec->renderer = &gr->base;
Pekka Paalanen7bb65102013-05-22 18:03:04 +03002940 ec->capabilities |= WESTON_CAP_ROTATION_ANY;
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03002941 ec->capabilities |= WESTON_CAP_CAPTURE_YFLIP;
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +02002942 ec->capabilities |= WESTON_CAP_VIEW_CLIP_MASK;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002943
Neil Robertsb7f85332014-03-07 18:05:49 +00002944 if (gl_renderer_setup_egl_extensions(ec) < 0)
Derek Foreman066ca0c2015-06-11 12:14:45 -05002945 goto fail_with_error;
Neil Robertsb7f85332014-03-07 18:05:49 +00002946
Pekka Paalanena3525802014-06-12 16:49:29 +03002947 wl_list_init(&gr->dmabuf_images);
2948 if (gr->has_dmabuf_import)
2949 gr->base.import_dmabuf = gl_renderer_import_dmabuf;
2950
Armin Krezović28d240f2016-06-23 11:59:35 +02002951 if (gr->has_surfaceless_context) {
2952 weston_log("EGL_KHR_surfaceless_context available\n");
2953 gr->dummy_surface = EGL_NO_SURFACE;
2954 } else {
2955 weston_log("EGL_KHR_surfaceless_context unavailable. "
2956 "Trying PbufferSurface\n");
2957
2958 if (gl_renderer_create_pbuffer_surface(gr) < 0)
2959 goto fail_with_error;
2960 }
2961
Tomeu Vizoso12072b62013-08-06 20:05:55 +02002962 wl_display_add_shm_format(ec->wl_display, WL_SHM_FORMAT_RGB565);
2963
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002964 wl_signal_init(&gr->destroy_signal);
2965
Armin Krezović28d240f2016-06-23 11:59:35 +02002966 if (gl_renderer_setup(ec, gr->dummy_surface) < 0) {
2967 if (gr->dummy_surface != EGL_NO_SURFACE)
2968 eglDestroySurface(gr->egl_display, gr->dummy_surface);
2969 goto fail_with_error;
2970 }
2971
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002972 return 0;
2973
Derek Foreman066ca0c2015-06-11 12:14:45 -05002974fail_with_error:
Pekka Paalanen326529f2012-11-27 12:25:25 +02002975 gl_renderer_print_egl_error_state();
Dawid Gajownik1a912a92015-08-21 00:20:54 -03002976fail_terminate:
2977 eglTerminate(gr->egl_display);
Derek Foreman066ca0c2015-06-11 12:14:45 -05002978fail:
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002979 free(gr);
2980 return -1;
2981}
2982
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002983static EGLDisplay
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002984gl_renderer_display(struct weston_compositor *ec)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002985{
2986 return get_renderer(ec)->egl_display;
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04002987}
2988
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002989static int
2990compile_shaders(struct weston_compositor *ec)
2991{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002992 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker40684142012-11-13 19:10:25 +01002993
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03002994 gr->texture_shader_rgba.vertex_source = vertex_shader;
2995 gr->texture_shader_rgba.fragment_source = texture_fragment_shader_rgba;
2996
2997 gr->texture_shader_rgbx.vertex_source = vertex_shader;
2998 gr->texture_shader_rgbx.fragment_source = texture_fragment_shader_rgbx;
2999
3000 gr->texture_shader_egl_external.vertex_source = vertex_shader;
3001 gr->texture_shader_egl_external.fragment_source =
3002 texture_fragment_shader_egl_external;
3003
3004 gr->texture_shader_y_uv.vertex_source = vertex_shader;
3005 gr->texture_shader_y_uv.fragment_source = texture_fragment_shader_y_uv;
3006
3007 gr->texture_shader_y_u_v.vertex_source = vertex_shader;
3008 gr->texture_shader_y_u_v.fragment_source =
3009 texture_fragment_shader_y_u_v;
3010
Ander Conselvan de Oliveira41a50ea2013-11-27 17:43:51 +02003011 gr->texture_shader_y_xuxv.vertex_source = vertex_shader;
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03003012 gr->texture_shader_y_xuxv.fragment_source =
3013 texture_fragment_shader_y_xuxv;
3014
3015 gr->solid_shader.vertex_source = vertex_shader;
3016 gr->solid_shader.fragment_source = solid_fragment_shader;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003017
3018 return 0;
3019}
3020
3021static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003022fragment_debug_binding(struct weston_keyboard *keyboard, uint32_t time,
3023 uint32_t key, void *data)
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003024{
3025 struct weston_compositor *ec = data;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003026 struct gl_renderer *gr = get_renderer(ec);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003027 struct weston_output *output;
3028
John Kåre Alsaker40684142012-11-13 19:10:25 +01003029 gr->fragment_shader_debug ^= 1;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003030
John Kåre Alsaker40684142012-11-13 19:10:25 +01003031 shader_release(&gr->texture_shader_rgba);
3032 shader_release(&gr->texture_shader_rgbx);
3033 shader_release(&gr->texture_shader_egl_external);
3034 shader_release(&gr->texture_shader_y_uv);
3035 shader_release(&gr->texture_shader_y_u_v);
3036 shader_release(&gr->texture_shader_y_xuxv);
3037 shader_release(&gr->solid_shader);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003038
Ander Conselvan de Oliveira03fb4ef2012-12-03 17:08:11 +02003039 /* Force use_shader() to call glUseProgram(), since we need to use
3040 * the recompiled version of the shader. */
3041 gr->current_shader = NULL;
3042
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003043 wl_list_for_each(output, &ec->output_list, link)
3044 weston_output_damage(output);
3045}
3046
Kristian Høgsberg8799d412013-05-07 10:50:09 -04003047static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003048fan_debug_repaint_binding(struct weston_keyboard *keyboard, uint32_t time,
3049 uint32_t key, void *data)
Kristian Høgsberg8799d412013-05-07 10:50:09 -04003050{
3051 struct weston_compositor *compositor = data;
3052 struct gl_renderer *gr = get_renderer(compositor);
3053
3054 gr->fan_debug = !gr->fan_debug;
3055 weston_compositor_damage_all(compositor);
3056}
3057
John Kåre Alsaker94659272012-11-13 19:10:18 +01003058static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003059gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003060{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003061 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003062 const char *extensions;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003063 EGLConfig context_config;
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003064 EGLBoolean ret;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003065
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003066 static const EGLint context_attribs[] = {
3067 EGL_CONTEXT_CLIENT_VERSION, 2,
3068 EGL_NONE
3069 };
3070
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003071 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
3072 weston_log("failed to bind EGL_OPENGL_ES_API\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02003073 gl_renderer_print_egl_error_state();
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003074 return -1;
3075 }
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03003076
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003077 context_config = gr->egl_config;
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03003078
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003079 if (gr->has_configless_context)
3080 context_config = EGL_NO_CONFIG_MESA;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003081
3082 gr->egl_context = eglCreateContext(gr->egl_display, context_config,
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003083 EGL_NO_CONTEXT, context_attribs);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003084 if (gr->egl_context == NULL) {
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003085 weston_log("failed to create context\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02003086 gl_renderer_print_egl_error_state();
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003087 return -1;
3088 }
3089
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003090 ret = eglMakeCurrent(gr->egl_display, egl_surface,
3091 egl_surface, gr->egl_context);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003092 if (ret == EGL_FALSE) {
3093 weston_log("Failed to make EGL context current.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02003094 gl_renderer_print_egl_error_state();
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003095 return -1;
3096 }
3097
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003098 log_egl_gl_info(gr->egl_display);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003099
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003100 gr->image_target_texture_2d =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003101 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003102
3103 extensions = (const char *) glGetString(GL_EXTENSIONS);
3104 if (!extensions) {
3105 weston_log("Retrieving GL extension string failed.\n");
3106 return -1;
3107 }
3108
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003109 if (!weston_check_egl_extension(extensions, "GL_EXT_texture_format_BGRA8888")) {
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003110 weston_log("GL_EXT_texture_format_BGRA8888 not available\n");
3111 return -1;
3112 }
3113
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003114 if (weston_check_egl_extension(extensions, "GL_EXT_read_format_bgra"))
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01003115 ec->read_format = PIXMAN_a8r8g8b8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003116 else
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01003117 ec->read_format = PIXMAN_a8b8g8r8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003118
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003119 if (weston_check_egl_extension(extensions, "GL_EXT_unpack_subimage"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003120 gr->has_unpack_subimage = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003121
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003122 if (weston_check_egl_extension(extensions, "GL_OES_EGL_image_external"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003123 gr->has_egl_image_external = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003124
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003125 glActiveTexture(GL_TEXTURE0);
3126
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003127 if (compile_shaders(ec))
3128 return -1;
3129
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03003130 gr->fragment_binding =
3131 weston_compositor_add_debug_binding(ec, KEY_S,
3132 fragment_debug_binding,
3133 ec);
3134 gr->fan_binding =
3135 weston_compositor_add_debug_binding(ec, KEY_F,
3136 fan_debug_repaint_binding,
3137 ec);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003138
Pekka Paalanen035a0322012-10-24 09:43:06 +03003139 weston_log("GL ES 2 renderer features:\n");
3140 weston_log_continue(STAMP_SPACE "read-back format: %s\n",
Pekka Paalanenfe4eacf2013-01-10 16:50:42 +02003141 ec->read_format == PIXMAN_a8r8g8b8 ? "BGRA" : "RGBA");
Pekka Paalanen035a0322012-10-24 09:43:06 +03003142 weston_log_continue(STAMP_SPACE "wl_shm sub-image to texture: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003143 gr->has_unpack_subimage ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03003144 weston_log_continue(STAMP_SPACE "EGL Wayland extension: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003145 gr->has_bind_display ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03003146
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003147
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003148 return 0;
3149}
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003150
3151WL_EXPORT struct gl_renderer_interface gl_renderer_interface = {
3152 .opaque_attribs = gl_renderer_opaque_attribs,
3153 .alpha_attribs = gl_renderer_alpha_attribs,
3154
3155 .create = gl_renderer_create,
3156 .display = gl_renderer_display,
3157 .output_create = gl_renderer_output_create,
3158 .output_destroy = gl_renderer_output_destroy,
3159 .output_surface = gl_renderer_output_surface,
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05003160 .output_set_border = gl_renderer_output_set_border,
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003161 .print_egl_error_state = gl_renderer_print_egl_error_state
3162};