gl-renderer: Make the error logging a little nicer
[platform/upstream/weston.git] / src / gl-renderer.c
1 /*
2  * Copyright © 2012 Intel Corporation
3  * Copyright © 2015 Collabora, Ltd.
4  *
5  * Permission to use, copy, modify, distribute, and sell this software and
6  * its documentation for any purpose is hereby granted without fee, provided
7  * that the above copyright notice appear in all copies and that both that
8  * copyright notice and this permission notice appear in supporting
9  * documentation, and that the name of the copyright holders not be used in
10  * advertising or publicity pertaining to distribution of the software
11  * without specific, written prior permission.  The copyright holders make
12  * no representations about the suitability of this software for any
13  * purpose.  It is provided "as is" without express or implied warranty.
14  *
15  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22  */
23
24 #include "config.h"
25
26 #include <GLES2/gl2.h>
27 #include <GLES2/gl2ext.h>
28
29 #include <stdbool.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <ctype.h>
33 #include <float.h>
34 #include <assert.h>
35 #include <linux/input.h>
36
37 #include "gl-renderer.h"
38 #include "vertex-clipping.h"
39
40 #include "weston-egl-ext.h"
41
42 struct gl_shader {
43         GLuint program;
44         GLuint vertex_shader, fragment_shader;
45         GLint proj_uniform;
46         GLint tex_uniforms[3];
47         GLint alpha_uniform;
48         GLint color_uniform;
49         const char *vertex_source, *fragment_source;
50 };
51
52 #define BUFFER_DAMAGE_COUNT 2
53
54 enum gl_border_status {
55         BORDER_STATUS_CLEAN = 0,
56         BORDER_TOP_DIRTY = 1 << GL_RENDERER_BORDER_TOP,
57         BORDER_LEFT_DIRTY = 1 << GL_RENDERER_BORDER_LEFT,
58         BORDER_RIGHT_DIRTY = 1 << GL_RENDERER_BORDER_RIGHT,
59         BORDER_BOTTOM_DIRTY = 1 << GL_RENDERER_BORDER_BOTTOM,
60         BORDER_ALL_DIRTY = 0xf,
61         BORDER_SIZE_CHANGED = 0x10
62 };
63
64 struct gl_border_image {
65         GLuint tex;
66         int32_t width, height;
67         int32_t tex_width;
68         void *data;
69 };
70
71 struct gl_output_state {
72         EGLSurface egl_surface;
73         pixman_region32_t buffer_damage[BUFFER_DAMAGE_COUNT];
74         int buffer_damage_index;
75         enum gl_border_status border_damage[BUFFER_DAMAGE_COUNT];
76         struct gl_border_image borders[4];
77         enum gl_border_status border_status;
78
79         struct weston_matrix output_matrix;
80 };
81
82 enum buffer_type {
83         BUFFER_TYPE_NULL,
84         BUFFER_TYPE_SOLID, /* internal solid color surfaces without a buffer */
85         BUFFER_TYPE_SHM,
86         BUFFER_TYPE_EGL
87 };
88
89 struct gl_surface_state {
90         GLfloat color[4];
91         struct gl_shader *shader;
92
93         GLuint textures[3];
94         int num_textures;
95         int needs_full_upload;
96         pixman_region32_t texture_damage;
97
98         /* These are only used by SHM surfaces to detect when we need
99          * to do a full upload to specify a new internal texture
100          * format */
101         GLenum gl_format;
102         GLenum gl_pixel_type;
103
104         EGLImageKHR images[3];
105         GLenum target;
106         int num_images;
107
108         struct weston_buffer_reference buffer_ref;
109         enum buffer_type buffer_type;
110         int pitch; /* in pixels */
111         int height; /* in pixels */
112         int y_inverted;
113
114         struct weston_surface *surface;
115
116         struct wl_listener surface_destroy_listener;
117         struct wl_listener renderer_destroy_listener;
118 };
119
120 struct gl_renderer {
121         struct weston_renderer base;
122         int fragment_shader_debug;
123         int fan_debug;
124         struct weston_binding *fragment_binding;
125         struct weston_binding *fan_binding;
126
127         EGLDisplay egl_display;
128         EGLContext egl_context;
129         EGLConfig egl_config;
130
131         struct wl_array vertices;
132         struct wl_array vtxcnt;
133
134         PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
135         PFNEGLCREATEIMAGEKHRPROC create_image;
136         PFNEGLDESTROYIMAGEKHRPROC destroy_image;
137
138 #ifdef EGL_EXT_swap_buffers_with_damage
139         PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC swap_buffers_with_damage;
140 #endif
141
142         PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC create_platform_window;
143
144         int has_unpack_subimage;
145
146         PFNEGLBINDWAYLANDDISPLAYWL bind_display;
147         PFNEGLUNBINDWAYLANDDISPLAYWL unbind_display;
148         PFNEGLQUERYWAYLANDBUFFERWL query_buffer;
149         int has_bind_display;
150
151         int has_egl_image_external;
152
153         int has_egl_buffer_age;
154
155         int has_configless_context;
156
157         struct gl_shader texture_shader_rgba;
158         struct gl_shader texture_shader_rgbx;
159         struct gl_shader texture_shader_egl_external;
160         struct gl_shader texture_shader_y_uv;
161         struct gl_shader texture_shader_y_u_v;
162         struct gl_shader texture_shader_y_xuxv;
163         struct gl_shader invert_color_shader;
164         struct gl_shader solid_shader;
165         struct gl_shader *current_shader;
166
167         struct wl_signal destroy_signal;
168 };
169
170 static PFNEGLGETPLATFORMDISPLAYEXTPROC get_platform_display = NULL;
171
172 static inline struct gl_output_state *
173 get_output_state(struct weston_output *output)
174 {
175         return (struct gl_output_state *)output->renderer_state;
176 }
177
178 static int
179 gl_renderer_create_surface(struct weston_surface *surface);
180
181 static inline struct gl_surface_state *
182 get_surface_state(struct weston_surface *surface)
183 {
184         if (!surface->renderer_state)
185                 gl_renderer_create_surface(surface);
186
187         return (struct gl_surface_state *)surface->renderer_state;
188 }
189
190 static inline struct gl_renderer *
191 get_renderer(struct weston_compositor *ec)
192 {
193         return (struct gl_renderer *)ec->renderer;
194 }
195
196 static const char *
197 egl_error_string(EGLint code)
198 {
199 #define MYERRCODE(x) case x: return #x;
200         switch (code) {
201         MYERRCODE(EGL_SUCCESS)
202         MYERRCODE(EGL_NOT_INITIALIZED)
203         MYERRCODE(EGL_BAD_ACCESS)
204         MYERRCODE(EGL_BAD_ALLOC)
205         MYERRCODE(EGL_BAD_ATTRIBUTE)
206         MYERRCODE(EGL_BAD_CONTEXT)
207         MYERRCODE(EGL_BAD_CONFIG)
208         MYERRCODE(EGL_BAD_CURRENT_SURFACE)
209         MYERRCODE(EGL_BAD_DISPLAY)
210         MYERRCODE(EGL_BAD_SURFACE)
211         MYERRCODE(EGL_BAD_MATCH)
212         MYERRCODE(EGL_BAD_PARAMETER)
213         MYERRCODE(EGL_BAD_NATIVE_PIXMAP)
214         MYERRCODE(EGL_BAD_NATIVE_WINDOW)
215         MYERRCODE(EGL_CONTEXT_LOST)
216         default:
217                 return "unknown";
218         }
219 #undef MYERRCODE
220 }
221
222 static void
223 gl_renderer_print_egl_error_state(void)
224 {
225         EGLint code;
226
227         code = eglGetError();
228         weston_log("EGL error state: %s (0x%04lx)\n",
229                 egl_error_string(code), (long)code);
230 }
231
232 #define max(a, b) (((a) > (b)) ? (a) : (b))
233 #define min(a, b) (((a) > (b)) ? (b) : (a))
234
235 /*
236  * Compute the boundary vertices of the intersection of the global coordinate
237  * aligned rectangle 'rect', and an arbitrary quadrilateral produced from
238  * 'surf_rect' when transformed from surface coordinates into global coordinates.
239  * The vertices are written to 'ex' and 'ey', and the return value is the
240  * number of vertices. Vertices are produced in clockwise winding order.
241  * Guarantees to produce either zero vertices, or 3-8 vertices with non-zero
242  * polygon area.
243  */
244 static int
245 calculate_edges(struct weston_view *ev, pixman_box32_t *rect,
246                 pixman_box32_t *surf_rect, GLfloat *ex, GLfloat *ey)
247 {
248
249         struct clip_context ctx;
250         int i, n;
251         GLfloat min_x, max_x, min_y, max_y;
252         struct polygon8 surf = {
253                 { surf_rect->x1, surf_rect->x2, surf_rect->x2, surf_rect->x1 },
254                 { surf_rect->y1, surf_rect->y1, surf_rect->y2, surf_rect->y2 },
255                 4
256         };
257
258         ctx.clip.x1 = rect->x1;
259         ctx.clip.y1 = rect->y1;
260         ctx.clip.x2 = rect->x2;
261         ctx.clip.y2 = rect->y2;
262
263         /* transform surface to screen space: */
264         for (i = 0; i < surf.n; i++)
265                 weston_view_to_global_float(ev, surf.x[i], surf.y[i],
266                                             &surf.x[i], &surf.y[i]);
267
268         /* find bounding box: */
269         min_x = max_x = surf.x[0];
270         min_y = max_y = surf.y[0];
271
272         for (i = 1; i < surf.n; i++) {
273                 min_x = min(min_x, surf.x[i]);
274                 max_x = max(max_x, surf.x[i]);
275                 min_y = min(min_y, surf.y[i]);
276                 max_y = max(max_y, surf.y[i]);
277         }
278
279         /* First, simple bounding box check to discard early transformed
280          * surface rects that do not intersect with the clip region:
281          */
282         if ((min_x >= ctx.clip.x2) || (max_x <= ctx.clip.x1) ||
283             (min_y >= ctx.clip.y2) || (max_y <= ctx.clip.y1))
284                 return 0;
285
286         /* Simple case, bounding box edges are parallel to surface edges,
287          * there will be only four edges.  We just need to clip the surface
288          * vertices to the clip rect bounds:
289          */
290         if (!ev->transform.enabled)
291                 return clip_simple(&ctx, &surf, ex, ey);
292
293         /* Transformed case: use a general polygon clipping algorithm to
294          * clip the surface rectangle with each side of 'rect'.
295          * The algorithm is Sutherland-Hodgman, as explained in
296          * http://www.codeguru.com/cpp/misc/misc/graphics/article.php/c8965/Polygon-Clipping.htm
297          * but without looking at any of that code.
298          */
299         n = clip_transformed(&ctx, &surf, ex, ey);
300
301         if (n < 3)
302                 return 0;
303
304         return n;
305 }
306
307 static bool
308 merge_down(pixman_box32_t *a, pixman_box32_t *b, pixman_box32_t *merge)
309 {
310         if (a->x1 == b->x1 && a->x2 == b->x2 && a->y1 == b->y2) {
311                 merge->x1 = a->x1;
312                 merge->x2 = a->x2;
313                 merge->y1 = b->y1;
314                 merge->y2 = a->y2;
315                 return true;
316         }
317         return false;
318 }
319
320 static int
321 compress_bands(pixman_box32_t *inrects, int nrects,
322                    pixman_box32_t **outrects)
323 {
324         bool merged;
325         pixman_box32_t *out, merge_rect;
326         int i, j, nout;
327
328         if (!nrects) {
329                 *outrects = NULL;
330                 return 0;
331         }
332
333         /* nrects is an upper bound - we're not too worried about
334          * allocating a little extra
335          */
336         out = malloc(sizeof(pixman_box32_t) * nrects);
337         out[0] = inrects[0];
338         nout = 1;
339         for (i = 1; i < nrects; i++) {
340                 for (j = 0; j < nout; j++) {
341                         merged = merge_down(&inrects[i], &out[j], &merge_rect);
342                         if (merged) {
343                                 out[j] = merge_rect;
344                                 break;
345                         }
346                 }
347                 if (!merged) {
348                         out[nout] = inrects[i];
349                         nout++;
350                 }
351         }
352         *outrects = out;
353         return nout;
354 }
355
356 static int
357 texture_region(struct weston_view *ev, pixman_region32_t *region,
358                 pixman_region32_t *surf_region)
359 {
360         struct gl_surface_state *gs = get_surface_state(ev->surface);
361         struct weston_compositor *ec = ev->surface->compositor;
362         struct gl_renderer *gr = get_renderer(ec);
363         GLfloat *v, inv_width, inv_height;
364         unsigned int *vtxcnt, nvtx = 0;
365         pixman_box32_t *rects, *surf_rects;
366         pixman_box32_t *raw_rects;
367         int i, j, k, nrects, nsurf, raw_nrects;
368         bool used_band_compression;
369         raw_rects = pixman_region32_rectangles(region, &raw_nrects);
370         surf_rects = pixman_region32_rectangles(surf_region, &nsurf);
371
372         if (raw_nrects < 4) {
373                 used_band_compression = false;
374                 nrects = raw_nrects;
375                 rects = raw_rects;
376         } else {
377                 nrects = compress_bands(raw_rects, raw_nrects, &rects);
378                 used_band_compression = true;
379         }
380         /* worst case we can have 8 vertices per rect (ie. clipped into
381          * an octagon):
382          */
383         v = wl_array_add(&gr->vertices, nrects * nsurf * 8 * 4 * sizeof *v);
384         vtxcnt = wl_array_add(&gr->vtxcnt, nrects * nsurf * sizeof *vtxcnt);
385
386         inv_width = 1.0 / gs->pitch;
387         inv_height = 1.0 / gs->height;
388
389         for (i = 0; i < nrects; i++) {
390                 pixman_box32_t *rect = &rects[i];
391                 for (j = 0; j < nsurf; j++) {
392                         pixman_box32_t *surf_rect = &surf_rects[j];
393                         GLfloat sx, sy, bx, by;
394                         GLfloat ex[8], ey[8];          /* edge points in screen space */
395                         int n;
396
397                         /* The transformed surface, after clipping to the clip region,
398                          * can have as many as eight sides, emitted as a triangle-fan.
399                          * The first vertex in the triangle fan can be chosen arbitrarily,
400                          * since the area is guaranteed to be convex.
401                          *
402                          * If a corner of the transformed surface falls outside of the
403                          * clip region, instead of emitting one vertex for the corner
404                          * of the surface, up to two are emitted for two corresponding
405                          * intersection point(s) between the surface and the clip region.
406                          *
407                          * To do this, we first calculate the (up to eight) points that
408                          * form the intersection of the clip rect and the transformed
409                          * surface.
410                          */
411                         n = calculate_edges(ev, rect, surf_rect, ex, ey);
412                         if (n < 3)
413                                 continue;
414
415                         /* emit edge points: */
416                         for (k = 0; k < n; k++) {
417                                 weston_view_from_global_float(ev, ex[k], ey[k],
418                                                               &sx, &sy);
419                                 /* position: */
420                                 *(v++) = ex[k];
421                                 *(v++) = ey[k];
422                                 /* texcoord: */
423                                 weston_surface_to_buffer_float(ev->surface,
424                                                                sx, sy,
425                                                                &bx, &by);
426                                 *(v++) = bx * inv_width;
427                                 if (gs->y_inverted) {
428                                         *(v++) = by * inv_height;
429                                 } else {
430                                         *(v++) = (gs->height - by) * inv_height;
431                                 }
432                         }
433
434                         vtxcnt[nvtx++] = n;
435                 }
436         }
437
438         if (used_band_compression)
439                 free(rects);
440         return nvtx;
441 }
442
443 static void
444 triangle_fan_debug(struct weston_view *view, int first, int count)
445 {
446         struct weston_compositor *compositor = view->surface->compositor;
447         struct gl_renderer *gr = get_renderer(compositor);
448         int i;
449         GLushort *buffer;
450         GLushort *index;
451         int nelems;
452         static int color_idx = 0;
453         static const GLfloat color[][4] = {
454                         { 1.0, 0.0, 0.0, 1.0 },
455                         { 0.0, 1.0, 0.0, 1.0 },
456                         { 0.0, 0.0, 1.0, 1.0 },
457                         { 1.0, 1.0, 1.0, 1.0 },
458         };
459
460         nelems = (count - 1 + count - 2) * 2;
461
462         buffer = malloc(sizeof(GLushort) * nelems);
463         index = buffer;
464
465         for (i = 1; i < count; i++) {
466                 *index++ = first;
467                 *index++ = first + i;
468         }
469
470         for (i = 2; i < count; i++) {
471                 *index++ = first + i - 1;
472                 *index++ = first + i;
473         }
474
475         glUseProgram(gr->solid_shader.program);
476         glUniform4fv(gr->solid_shader.color_uniform, 1,
477                         color[color_idx++ % ARRAY_LENGTH(color)]);
478         glDrawElements(GL_LINES, nelems, GL_UNSIGNED_SHORT, buffer);
479         glUseProgram(gr->current_shader->program);
480         free(buffer);
481 }
482
483 static void
484 repaint_region(struct weston_view *ev, pixman_region32_t *region,
485                 pixman_region32_t *surf_region)
486 {
487         struct weston_compositor *ec = ev->surface->compositor;
488         struct gl_renderer *gr = get_renderer(ec);
489         GLfloat *v;
490         unsigned int *vtxcnt;
491         int i, first, nfans;
492
493         /* The final region to be painted is the intersection of
494          * 'region' and 'surf_region'. However, 'region' is in the global
495          * coordinates, and 'surf_region' is in the surface-local
496          * coordinates. texture_region() will iterate over all pairs of
497          * rectangles from both regions, compute the intersection
498          * polygon for each pair, and store it as a triangle fan if
499          * it has a non-zero area (at least 3 vertices1, actually).
500          */
501         nfans = texture_region(ev, region, surf_region);
502
503         v = gr->vertices.data;
504         vtxcnt = gr->vtxcnt.data;
505
506         /* position: */
507         glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
508         glEnableVertexAttribArray(0);
509
510         /* texcoord: */
511         glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
512         glEnableVertexAttribArray(1);
513
514         for (i = 0, first = 0; i < nfans; i++) {
515                 glDrawArrays(GL_TRIANGLE_FAN, first, vtxcnt[i]);
516                 if (gr->fan_debug)
517                         triangle_fan_debug(ev, first, vtxcnt[i]);
518                 first += vtxcnt[i];
519         }
520
521         glDisableVertexAttribArray(1);
522         glDisableVertexAttribArray(0);
523
524         gr->vertices.size = 0;
525         gr->vtxcnt.size = 0;
526 }
527
528 static int
529 use_output(struct weston_output *output)
530 {
531         static int errored;
532         struct gl_output_state *go = get_output_state(output);
533         struct gl_renderer *gr = get_renderer(output->compositor);
534         EGLBoolean ret;
535
536         ret = eglMakeCurrent(gr->egl_display, go->egl_surface,
537                              go->egl_surface, gr->egl_context);
538
539         if (ret == EGL_FALSE) {
540                 if (errored)
541                         return -1;
542                 errored = 1;
543                 weston_log("Failed to make EGL context current.\n");
544                 gl_renderer_print_egl_error_state();
545                 return -1;
546         }
547
548         return 0;
549 }
550
551 static int
552 shader_init(struct gl_shader *shader, struct gl_renderer *gr,
553                    const char *vertex_source, const char *fragment_source);
554
555 static void
556 use_shader(struct gl_renderer *gr, struct gl_shader *shader)
557 {
558         if (!shader->program) {
559                 int ret;
560
561                 ret =  shader_init(shader, gr,
562                                    shader->vertex_source,
563                                    shader->fragment_source);
564
565                 if (ret < 0)
566                         weston_log("warning: failed to compile shader\n");
567         }
568
569         if (gr->current_shader == shader)
570                 return;
571         glUseProgram(shader->program);
572         gr->current_shader = shader;
573 }
574
575 static void
576 shader_uniforms(struct gl_shader *shader,
577                 struct weston_view *view,
578                 struct weston_output *output)
579 {
580         int i;
581         struct gl_surface_state *gs = get_surface_state(view->surface);
582         struct gl_output_state *go = get_output_state(output);
583
584         glUniformMatrix4fv(shader->proj_uniform,
585                            1, GL_FALSE, go->output_matrix.d);
586         glUniform4fv(shader->color_uniform, 1, gs->color);
587         glUniform1f(shader->alpha_uniform, view->alpha);
588
589         for (i = 0; i < gs->num_textures; i++)
590                 glUniform1i(shader->tex_uniforms[i], i);
591 }
592
593 static void
594 draw_view(struct weston_view *ev, struct weston_output *output,
595           pixman_region32_t *damage) /* in global coordinates */
596 {
597         struct weston_compositor *ec = ev->surface->compositor;
598         struct gl_renderer *gr = get_renderer(ec);
599         struct gl_surface_state *gs = get_surface_state(ev->surface);
600         /* repaint bounding region in global coordinates: */
601         pixman_region32_t repaint;
602         /* opaque region in surface coordinates: */
603         pixman_region32_t surface_opaque;
604         /* non-opaque region in surface coordinates: */
605         pixman_region32_t surface_blend;
606         GLint filter;
607         int i;
608
609         /* In case of a runtime switch of renderers, we may not have received
610          * an attach for this surface since the switch. In that case we don't
611          * have a valid buffer or a proper shader set up so skip rendering. */
612         if (!gs->shader)
613                 return;
614
615         pixman_region32_init(&repaint);
616         pixman_region32_intersect(&repaint,
617                                   &ev->transform.boundingbox, damage);
618         pixman_region32_subtract(&repaint, &repaint, &ev->clip);
619
620         if (!pixman_region32_not_empty(&repaint))
621                 goto out;
622
623         glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
624
625         if (gr->fan_debug) {
626                 use_shader(gr, &gr->solid_shader);
627                 shader_uniforms(&gr->solid_shader, ev, output);
628         }
629
630         use_shader(gr, gs->shader);
631         shader_uniforms(gs->shader, ev, output);
632
633         if (ev->transform.enabled || output->zoom.active ||
634             output->current_scale != ev->surface->buffer_viewport.buffer.scale)
635                 filter = GL_LINEAR;
636         else
637                 filter = GL_NEAREST;
638
639         for (i = 0; i < gs->num_textures; i++) {
640                 glActiveTexture(GL_TEXTURE0 + i);
641                 glBindTexture(gs->target, gs->textures[i]);
642                 glTexParameteri(gs->target, GL_TEXTURE_MIN_FILTER, filter);
643                 glTexParameteri(gs->target, GL_TEXTURE_MAG_FILTER, filter);
644         }
645
646         /* blended region is whole surface minus opaque region: */
647         pixman_region32_init_rect(&surface_blend, 0, 0,
648                                   ev->surface->width, ev->surface->height);
649         if (ev->geometry.scissor_enabled)
650                 pixman_region32_intersect(&surface_blend, &surface_blend,
651                                           &ev->geometry.scissor);
652         pixman_region32_subtract(&surface_blend, &surface_blend,
653                                  &ev->surface->opaque);
654
655         /* XXX: Should we be using ev->transform.opaque here? */
656         pixman_region32_init(&surface_opaque);
657         if (ev->geometry.scissor_enabled)
658                 pixman_region32_intersect(&surface_opaque,
659                                           &ev->surface->opaque,
660                                           &ev->geometry.scissor);
661         else
662                 pixman_region32_copy(&surface_opaque, &ev->surface->opaque);
663
664         if (pixman_region32_not_empty(&surface_opaque)) {
665                 if (gs->shader == &gr->texture_shader_rgba) {
666                         /* Special case for RGBA textures with possibly
667                          * bad data in alpha channel: use the shader
668                          * that forces texture alpha = 1.0.
669                          * Xwayland surfaces need this.
670                          */
671                         use_shader(gr, &gr->texture_shader_rgbx);
672                         shader_uniforms(&gr->texture_shader_rgbx, ev, output);
673                 }
674
675                 if (ev->alpha < 1.0)
676                         glEnable(GL_BLEND);
677                 else
678                         glDisable(GL_BLEND);
679
680                 repaint_region(ev, &repaint, &surface_opaque);
681         }
682
683         if (pixman_region32_not_empty(&surface_blend)) {
684                 use_shader(gr, gs->shader);
685                 glEnable(GL_BLEND);
686                 repaint_region(ev, &repaint, &surface_blend);
687         }
688
689         pixman_region32_fini(&surface_blend);
690         pixman_region32_fini(&surface_opaque);
691
692 out:
693         pixman_region32_fini(&repaint);
694 }
695
696 static void
697 repaint_views(struct weston_output *output, pixman_region32_t *damage)
698 {
699         struct weston_compositor *compositor = output->compositor;
700         struct weston_view *view;
701
702         wl_list_for_each_reverse(view, &compositor->view_list, link)
703                 if (view->plane == &compositor->primary_plane)
704                         draw_view(view, output, damage);
705 }
706
707 static void
708 draw_output_border_texture(struct gl_output_state *go,
709                            enum gl_renderer_border_side side,
710                            int32_t x, int32_t y,
711                            int32_t width, int32_t height)
712 {
713         struct gl_border_image *img = &go->borders[side];
714         static GLushort indices [] = { 0, 1, 3, 3, 1, 2 };
715
716         if (!img->data) {
717                 if (img->tex) {
718                         glDeleteTextures(1, &img->tex);
719                         img->tex = 0;
720                 }
721
722                 return;
723         }
724
725         if (!img->tex) {
726                 glGenTextures(1, &img->tex);
727                 glBindTexture(GL_TEXTURE_2D, img->tex);
728
729                 glTexParameteri(GL_TEXTURE_2D,
730                                 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
731                 glTexParameteri(GL_TEXTURE_2D,
732                                 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
733                 glTexParameteri(GL_TEXTURE_2D,
734                                 GL_TEXTURE_MIN_FILTER, GL_NEAREST);
735                 glTexParameteri(GL_TEXTURE_2D,
736                                 GL_TEXTURE_MAG_FILTER, GL_NEAREST);
737         } else {
738                 glBindTexture(GL_TEXTURE_2D, img->tex);
739         }
740
741         if (go->border_status & (1 << side)) {
742 #ifdef GL_EXT_unpack_subimage
743                 glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, 0);
744                 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, 0);
745                 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, 0);
746 #endif
747                 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
748                              img->tex_width, img->height, 0,
749                              GL_BGRA_EXT, GL_UNSIGNED_BYTE, img->data);
750         }
751
752         GLfloat texcoord[] = {
753                 0.0f, 0.0f,
754                 (GLfloat)img->width / (GLfloat)img->tex_width, 0.0f,
755                 (GLfloat)img->width / (GLfloat)img->tex_width, 1.0f,
756                 0.0f, 1.0f,
757         };
758
759         GLfloat verts[] = {
760                 x, y,
761                 x + width, y,
762                 x + width, y + height,
763                 x, y + height
764         };
765
766         glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, verts);
767         glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, texcoord);
768         glEnableVertexAttribArray(0);
769         glEnableVertexAttribArray(1);
770
771         glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
772
773         glDisableVertexAttribArray(1);
774         glDisableVertexAttribArray(0);
775 }
776
777 static int
778 output_has_borders(struct weston_output *output)
779 {
780         struct gl_output_state *go = get_output_state(output);
781
782         return go->borders[GL_RENDERER_BORDER_TOP].data ||
783                go->borders[GL_RENDERER_BORDER_RIGHT].data ||
784                go->borders[GL_RENDERER_BORDER_BOTTOM].data ||
785                go->borders[GL_RENDERER_BORDER_LEFT].data;
786 }
787
788 static void
789 draw_output_borders(struct weston_output *output,
790                     enum gl_border_status border_status)
791 {
792         struct gl_output_state *go = get_output_state(output);
793         struct gl_renderer *gr = get_renderer(output->compositor);
794         struct gl_shader *shader = &gr->texture_shader_rgba;
795         struct gl_border_image *top, *bottom, *left, *right;
796         struct weston_matrix matrix;
797         int full_width, full_height;
798
799         if (border_status == BORDER_STATUS_CLEAN)
800                 return; /* Clean. Nothing to do. */
801
802         top = &go->borders[GL_RENDERER_BORDER_TOP];
803         bottom = &go->borders[GL_RENDERER_BORDER_BOTTOM];
804         left = &go->borders[GL_RENDERER_BORDER_LEFT];
805         right = &go->borders[GL_RENDERER_BORDER_RIGHT];
806
807         full_width = output->current_mode->width + left->width + right->width;
808         full_height = output->current_mode->height + top->height + bottom->height;
809
810         glDisable(GL_BLEND);
811         use_shader(gr, shader);
812
813         glViewport(0, 0, full_width, full_height);
814
815         weston_matrix_init(&matrix);
816         weston_matrix_translate(&matrix, -full_width/2.0, -full_height/2.0, 0);
817         weston_matrix_scale(&matrix, 2.0/full_width, -2.0/full_height, 1);
818         glUniformMatrix4fv(shader->proj_uniform, 1, GL_FALSE, matrix.d);
819
820         glUniform1i(shader->tex_uniforms[0], 0);
821         glUniform1f(shader->alpha_uniform, 1);
822         glActiveTexture(GL_TEXTURE0);
823
824         if (border_status & BORDER_TOP_DIRTY)
825                 draw_output_border_texture(go, GL_RENDERER_BORDER_TOP,
826                                            0, 0,
827                                            full_width, top->height);
828         if (border_status & BORDER_LEFT_DIRTY)
829                 draw_output_border_texture(go, GL_RENDERER_BORDER_LEFT,
830                                            0, top->height,
831                                            left->width, output->current_mode->height);
832         if (border_status & BORDER_RIGHT_DIRTY)
833                 draw_output_border_texture(go, GL_RENDERER_BORDER_RIGHT,
834                                            full_width - right->width, top->height,
835                                            right->width, output->current_mode->height);
836         if (border_status & BORDER_BOTTOM_DIRTY)
837                 draw_output_border_texture(go, GL_RENDERER_BORDER_BOTTOM,
838                                            0, full_height - bottom->height,
839                                            full_width, bottom->height);
840 }
841
842 static void
843 output_get_border_damage(struct weston_output *output,
844                          enum gl_border_status border_status,
845                          pixman_region32_t *damage)
846 {
847         struct gl_output_state *go = get_output_state(output);
848         struct gl_border_image *top, *bottom, *left, *right;
849         int full_width, full_height;
850
851         if (border_status == BORDER_STATUS_CLEAN)
852                 return; /* Clean. Nothing to do. */
853
854         top = &go->borders[GL_RENDERER_BORDER_TOP];
855         bottom = &go->borders[GL_RENDERER_BORDER_BOTTOM];
856         left = &go->borders[GL_RENDERER_BORDER_LEFT];
857         right = &go->borders[GL_RENDERER_BORDER_RIGHT];
858
859         full_width = output->current_mode->width + left->width + right->width;
860         full_height = output->current_mode->height + top->height + bottom->height;
861         if (border_status & BORDER_TOP_DIRTY)
862                 pixman_region32_union_rect(damage, damage,
863                                            0, 0,
864                                            full_width, top->height);
865         if (border_status & BORDER_LEFT_DIRTY)
866                 pixman_region32_union_rect(damage, damage,
867                                            0, top->height,
868                                            left->width, output->current_mode->height);
869         if (border_status & BORDER_RIGHT_DIRTY)
870                 pixman_region32_union_rect(damage, damage,
871                                            full_width - right->width, top->height,
872                                            right->width, output->current_mode->height);
873         if (border_status & BORDER_BOTTOM_DIRTY)
874                 pixman_region32_union_rect(damage, damage,
875                                            0, full_height - bottom->height,
876                                            full_width, bottom->height);
877 }
878
879 static void
880 output_get_damage(struct weston_output *output,
881                   pixman_region32_t *buffer_damage, uint32_t *border_damage)
882 {
883         struct gl_output_state *go = get_output_state(output);
884         struct gl_renderer *gr = get_renderer(output->compositor);
885         EGLint buffer_age = 0;
886         EGLBoolean ret;
887         int i;
888
889         if (gr->has_egl_buffer_age) {
890                 ret = eglQuerySurface(gr->egl_display, go->egl_surface,
891                                       EGL_BUFFER_AGE_EXT, &buffer_age);
892                 if (ret == EGL_FALSE) {
893                         weston_log("buffer age query failed.\n");
894                         gl_renderer_print_egl_error_state();
895                 }
896         }
897
898         if (buffer_age == 0 || buffer_age - 1 > BUFFER_DAMAGE_COUNT) {
899                 pixman_region32_copy(buffer_damage, &output->region);
900                 *border_damage = BORDER_ALL_DIRTY;
901         } else {
902                 for (i = 0; i < buffer_age - 1; i++)
903                         *border_damage |= go->border_damage[(go->buffer_damage_index + i) % BUFFER_DAMAGE_COUNT];
904
905                 if (*border_damage & BORDER_SIZE_CHANGED) {
906                         /* If we've had a resize, we have to do a full
907                          * repaint. */
908                         *border_damage |= BORDER_ALL_DIRTY;
909                         pixman_region32_copy(buffer_damage, &output->region);
910                 } else {
911                         for (i = 0; i < buffer_age - 1; i++)
912                                 pixman_region32_union(buffer_damage,
913                                                       buffer_damage,
914                                                       &go->buffer_damage[(go->buffer_damage_index + i) % BUFFER_DAMAGE_COUNT]);
915                 }
916         }
917 }
918
919 static void
920 output_rotate_damage(struct weston_output *output,
921                      pixman_region32_t *output_damage,
922                      enum gl_border_status border_status)
923 {
924         struct gl_output_state *go = get_output_state(output);
925         struct gl_renderer *gr = get_renderer(output->compositor);
926
927         if (!gr->has_egl_buffer_age)
928                 return;
929
930         go->buffer_damage_index += BUFFER_DAMAGE_COUNT - 1;
931         go->buffer_damage_index %= BUFFER_DAMAGE_COUNT;
932
933         pixman_region32_copy(&go->buffer_damage[go->buffer_damage_index], output_damage);
934         go->border_damage[go->buffer_damage_index] = border_status;
935 }
936
937 /* NOTE: We now allow falling back to ARGB gl visuals when XRGB is
938  * unavailable, so we're assuming the background has no transparency
939  * and that everything with a blend, like drop shadows, will have something
940  * opaque (like the background) drawn underneath it.
941  *
942  * Depending on the underlying hardware, violating that assumption could
943  * result in seeing through to another display plane.
944  */
945 static void
946 gl_renderer_repaint_output(struct weston_output *output,
947                               pixman_region32_t *output_damage)
948 {
949         struct gl_output_state *go = get_output_state(output);
950         struct weston_compositor *compositor = output->compositor;
951         struct gl_renderer *gr = get_renderer(compositor);
952         EGLBoolean ret;
953         static int errored;
954 #ifdef EGL_EXT_swap_buffers_with_damage
955         int i, nrects, buffer_height;
956         EGLint *egl_damage, *d;
957         pixman_box32_t *rects;
958 #endif
959         pixman_region32_t buffer_damage, total_damage;
960         enum gl_border_status border_damage = BORDER_STATUS_CLEAN;
961
962         if (use_output(output) < 0)
963                 return;
964
965         /* Calculate the viewport */
966         glViewport(go->borders[GL_RENDERER_BORDER_LEFT].width,
967                    go->borders[GL_RENDERER_BORDER_BOTTOM].height,
968                    output->current_mode->width,
969                    output->current_mode->height);
970
971         /* Calculate the global GL matrix */
972         go->output_matrix = output->matrix;
973         weston_matrix_translate(&go->output_matrix,
974                                 -(output->current_mode->width / 2.0),
975                                 -(output->current_mode->height / 2.0), 0);
976         weston_matrix_scale(&go->output_matrix,
977                             2.0 / output->current_mode->width,
978                             -2.0 / output->current_mode->height, 1);
979
980         /* if debugging, redraw everything outside the damage to clean up
981          * debug lines from the previous draw on this buffer:
982          */
983         if (gr->fan_debug) {
984                 pixman_region32_t undamaged;
985                 pixman_region32_init(&undamaged);
986                 pixman_region32_subtract(&undamaged, &output->region,
987                                          output_damage);
988                 gr->fan_debug = 0;
989                 repaint_views(output, &undamaged);
990                 gr->fan_debug = 1;
991                 pixman_region32_fini(&undamaged);
992         }
993
994         pixman_region32_init(&total_damage);
995         pixman_region32_init(&buffer_damage);
996
997         output_get_damage(output, &buffer_damage, &border_damage);
998         output_rotate_damage(output, output_damage, go->border_status);
999
1000         pixman_region32_union(&total_damage, &buffer_damage, output_damage);
1001         border_damage |= go->border_status;
1002
1003         repaint_views(output, &total_damage);
1004
1005         pixman_region32_fini(&total_damage);
1006         pixman_region32_fini(&buffer_damage);
1007
1008         draw_output_borders(output, border_damage);
1009
1010         pixman_region32_copy(&output->previous_damage, output_damage);
1011         wl_signal_emit(&output->frame_signal, output);
1012
1013 #ifdef EGL_EXT_swap_buffers_with_damage
1014         if (gr->swap_buffers_with_damage) {
1015                 pixman_region32_init(&buffer_damage);
1016                 weston_transformed_region(output->width, output->height,
1017                                           output->transform,
1018                                           output->current_scale,
1019                                           output_damage, &buffer_damage);
1020
1021                 if (output_has_borders(output)) {
1022                         pixman_region32_translate(&buffer_damage,
1023                                                   go->borders[GL_RENDERER_BORDER_LEFT].width,
1024                                                   go->borders[GL_RENDERER_BORDER_TOP].height);
1025                         output_get_border_damage(output, go->border_status,
1026                                                  &buffer_damage);
1027                 }
1028
1029                 rects = pixman_region32_rectangles(&buffer_damage, &nrects);
1030                 egl_damage = malloc(nrects * 4 * sizeof(EGLint));
1031
1032                 buffer_height = go->borders[GL_RENDERER_BORDER_TOP].height +
1033                                 output->current_mode->height +
1034                                 go->borders[GL_RENDERER_BORDER_BOTTOM].height;
1035
1036                 d = egl_damage;
1037                 for (i = 0; i < nrects; ++i) {
1038                         *d++ = rects[i].x1;
1039                         *d++ = buffer_height - rects[i].y2;
1040                         *d++ = rects[i].x2 - rects[i].x1;
1041                         *d++ = rects[i].y2 - rects[i].y1;
1042                 }
1043                 ret = gr->swap_buffers_with_damage(gr->egl_display,
1044                                                    go->egl_surface,
1045                                                    egl_damage, nrects);
1046                 free(egl_damage);
1047                 pixman_region32_fini(&buffer_damage);
1048         } else {
1049                 ret = eglSwapBuffers(gr->egl_display, go->egl_surface);
1050         }
1051 #else /* ! defined EGL_EXT_swap_buffers_with_damage */
1052         ret = eglSwapBuffers(gr->egl_display, go->egl_surface);
1053 #endif
1054
1055         if (ret == EGL_FALSE && !errored) {
1056                 errored = 1;
1057                 weston_log("Failed in eglSwapBuffers.\n");
1058                 gl_renderer_print_egl_error_state();
1059         }
1060
1061         go->border_status = BORDER_STATUS_CLEAN;
1062 }
1063
1064 static int
1065 gl_renderer_read_pixels(struct weston_output *output,
1066                                pixman_format_code_t format, void *pixels,
1067                                uint32_t x, uint32_t y,
1068                                uint32_t width, uint32_t height)
1069 {
1070         GLenum gl_format;
1071         struct gl_output_state *go = get_output_state(output);
1072
1073         x += go->borders[GL_RENDERER_BORDER_LEFT].width;
1074         y += go->borders[GL_RENDERER_BORDER_BOTTOM].height;
1075
1076         switch (format) {
1077         case PIXMAN_a8r8g8b8:
1078                 gl_format = GL_BGRA_EXT;
1079                 break;
1080         case PIXMAN_a8b8g8r8:
1081                 gl_format = GL_RGBA;
1082                 break;
1083         default:
1084                 return -1;
1085         }
1086
1087         if (use_output(output) < 0)
1088                 return -1;
1089
1090         glPixelStorei(GL_PACK_ALIGNMENT, 1);
1091         glReadPixels(x, y, width, height, gl_format,
1092                      GL_UNSIGNED_BYTE, pixels);
1093
1094         return 0;
1095 }
1096
1097 static void
1098 gl_renderer_flush_damage(struct weston_surface *surface)
1099 {
1100         struct gl_renderer *gr = get_renderer(surface->compositor);
1101         struct gl_surface_state *gs = get_surface_state(surface);
1102         struct weston_buffer *buffer = gs->buffer_ref.buffer;
1103         struct weston_view *view;
1104         int texture_used;
1105
1106 #ifdef GL_EXT_unpack_subimage
1107         pixman_box32_t *rectangles;
1108         void *data;
1109         int i, n;
1110 #endif
1111
1112         pixman_region32_union(&gs->texture_damage,
1113                               &gs->texture_damage, &surface->damage);
1114
1115         if (!buffer)
1116                 return;
1117
1118         /* Avoid upload, if the texture won't be used this time.
1119          * We still accumulate the damage in texture_damage, and
1120          * hold the reference to the buffer, in case the surface
1121          * migrates back to the primary plane.
1122          */
1123         texture_used = 0;
1124         wl_list_for_each(view, &surface->views, surface_link) {
1125                 if (view->plane == &surface->compositor->primary_plane) {
1126                         texture_used = 1;
1127                         break;
1128                 }
1129         }
1130         if (!texture_used)
1131                 return;
1132
1133         if (!pixman_region32_not_empty(&gs->texture_damage) &&
1134             !gs->needs_full_upload)
1135                 goto done;
1136
1137         glBindTexture(GL_TEXTURE_2D, gs->textures[0]);
1138
1139         if (!gr->has_unpack_subimage) {
1140                 wl_shm_buffer_begin_access(buffer->shm_buffer);
1141                 glTexImage2D(GL_TEXTURE_2D, 0, gs->gl_format,
1142                              gs->pitch, buffer->height, 0,
1143                              gs->gl_format, gs->gl_pixel_type,
1144                              wl_shm_buffer_get_data(buffer->shm_buffer));
1145                 wl_shm_buffer_end_access(buffer->shm_buffer);
1146
1147                 goto done;
1148         }
1149
1150 #ifdef GL_EXT_unpack_subimage
1151         glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, gs->pitch);
1152         data = wl_shm_buffer_get_data(buffer->shm_buffer);
1153
1154         if (gs->needs_full_upload) {
1155                 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, 0);
1156                 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, 0);
1157                 wl_shm_buffer_begin_access(buffer->shm_buffer);
1158                 glTexImage2D(GL_TEXTURE_2D, 0, gs->gl_format,
1159                              gs->pitch, buffer->height, 0,
1160                              gs->gl_format, gs->gl_pixel_type, data);
1161                 wl_shm_buffer_end_access(buffer->shm_buffer);
1162                 goto done;
1163         }
1164
1165         rectangles = pixman_region32_rectangles(&gs->texture_damage, &n);
1166         wl_shm_buffer_begin_access(buffer->shm_buffer);
1167         for (i = 0; i < n; i++) {
1168                 pixman_box32_t r;
1169
1170                 r = weston_surface_to_buffer_rect(surface, rectangles[i]);
1171
1172                 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, r.x1);
1173                 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, r.y1);
1174                 glTexSubImage2D(GL_TEXTURE_2D, 0, r.x1, r.y1,
1175                                 r.x2 - r.x1, r.y2 - r.y1,
1176                                 gs->gl_format, gs->gl_pixel_type, data);
1177         }
1178         wl_shm_buffer_end_access(buffer->shm_buffer);
1179 #endif
1180
1181 done:
1182         pixman_region32_fini(&gs->texture_damage);
1183         pixman_region32_init(&gs->texture_damage);
1184         gs->needs_full_upload = 0;
1185
1186         weston_buffer_reference(&gs->buffer_ref, NULL);
1187 }
1188
1189 static void
1190 ensure_textures(struct gl_surface_state *gs, int num_textures)
1191 {
1192         int i;
1193
1194         if (num_textures <= gs->num_textures)
1195                 return;
1196
1197         for (i = gs->num_textures; i < num_textures; i++) {
1198                 glGenTextures(1, &gs->textures[i]);
1199                 glBindTexture(gs->target, gs->textures[i]);
1200                 glTexParameteri(gs->target,
1201                                 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1202                 glTexParameteri(gs->target,
1203                                 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1204         }
1205         gs->num_textures = num_textures;
1206         glBindTexture(gs->target, 0);
1207 }
1208
1209 static void
1210 gl_renderer_attach_shm(struct weston_surface *es, struct weston_buffer *buffer,
1211                        struct wl_shm_buffer *shm_buffer)
1212 {
1213         struct weston_compositor *ec = es->compositor;
1214         struct gl_renderer *gr = get_renderer(ec);
1215         struct gl_surface_state *gs = get_surface_state(es);
1216         GLenum gl_format, gl_pixel_type;
1217         int pitch;
1218
1219         buffer->shm_buffer = shm_buffer;
1220         buffer->width = wl_shm_buffer_get_width(shm_buffer);
1221         buffer->height = wl_shm_buffer_get_height(shm_buffer);
1222
1223         switch (wl_shm_buffer_get_format(shm_buffer)) {
1224         case WL_SHM_FORMAT_XRGB8888:
1225                 gs->shader = &gr->texture_shader_rgbx;
1226                 pitch = wl_shm_buffer_get_stride(shm_buffer) / 4;
1227                 gl_format = GL_BGRA_EXT;
1228                 gl_pixel_type = GL_UNSIGNED_BYTE;
1229                 break;
1230         case WL_SHM_FORMAT_ARGB8888:
1231                 gs->shader = &gr->texture_shader_rgba;
1232                 pitch = wl_shm_buffer_get_stride(shm_buffer) / 4;
1233                 gl_format = GL_BGRA_EXT;
1234                 gl_pixel_type = GL_UNSIGNED_BYTE;
1235                 break;
1236         case WL_SHM_FORMAT_RGB565:
1237                 gs->shader = &gr->texture_shader_rgbx;
1238                 pitch = wl_shm_buffer_get_stride(shm_buffer) / 2;
1239                 gl_format = GL_RGB;
1240                 gl_pixel_type = GL_UNSIGNED_SHORT_5_6_5;
1241                 break;
1242         default:
1243                 weston_log("warning: unknown shm buffer format: %08x\n",
1244                            wl_shm_buffer_get_format(shm_buffer));
1245                 return;
1246         }
1247
1248         /* Only allocate a texture if it doesn't match existing one.
1249          * If a switch from DRM allocated buffer to a SHM buffer is
1250          * happening, we need to allocate a new texture buffer. */
1251         if (pitch != gs->pitch ||
1252             buffer->height != gs->height ||
1253             gl_format != gs->gl_format ||
1254             gl_pixel_type != gs->gl_pixel_type ||
1255             gs->buffer_type != BUFFER_TYPE_SHM) {
1256                 gs->pitch = pitch;
1257                 gs->height = buffer->height;
1258                 gs->target = GL_TEXTURE_2D;
1259                 gs->gl_format = gl_format;
1260                 gs->gl_pixel_type = gl_pixel_type;
1261                 gs->buffer_type = BUFFER_TYPE_SHM;
1262                 gs->needs_full_upload = 1;
1263                 gs->y_inverted = 1;
1264
1265                 gs->surface = es;
1266
1267                 ensure_textures(gs, 1);
1268         }
1269 }
1270
1271 static void
1272 gl_renderer_attach_egl(struct weston_surface *es, struct weston_buffer *buffer,
1273                        uint32_t format)
1274 {
1275         struct weston_compositor *ec = es->compositor;
1276         struct gl_renderer *gr = get_renderer(ec);
1277         struct gl_surface_state *gs = get_surface_state(es);
1278         EGLint attribs[3];
1279         int i, num_planes;
1280
1281         buffer->legacy_buffer = (struct wl_buffer *)buffer->resource;
1282         gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1283                          EGL_WIDTH, &buffer->width);
1284         gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1285                          EGL_HEIGHT, &buffer->height);
1286         gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1287                          EGL_WAYLAND_Y_INVERTED_WL, &buffer->y_inverted);
1288
1289         for (i = 0; i < gs->num_images; i++)
1290                 gr->destroy_image(gr->egl_display, gs->images[i]);
1291         gs->num_images = 0;
1292         gs->target = GL_TEXTURE_2D;
1293         switch (format) {
1294         case EGL_TEXTURE_RGB:
1295         case EGL_TEXTURE_RGBA:
1296         default:
1297                 num_planes = 1;
1298                 gs->shader = &gr->texture_shader_rgba;
1299                 break;
1300         case EGL_TEXTURE_EXTERNAL_WL:
1301                 num_planes = 1;
1302                 gs->target = GL_TEXTURE_EXTERNAL_OES;
1303                 gs->shader = &gr->texture_shader_egl_external;
1304                 break;
1305         case EGL_TEXTURE_Y_UV_WL:
1306                 num_planes = 2;
1307                 gs->shader = &gr->texture_shader_y_uv;
1308                 break;
1309         case EGL_TEXTURE_Y_U_V_WL:
1310                 num_planes = 3;
1311                 gs->shader = &gr->texture_shader_y_u_v;
1312                 break;
1313         case EGL_TEXTURE_Y_XUXV_WL:
1314                 num_planes = 2;
1315                 gs->shader = &gr->texture_shader_y_xuxv;
1316                 break;
1317         }
1318
1319         ensure_textures(gs, num_planes);
1320         for (i = 0; i < num_planes; i++) {
1321                 attribs[0] = EGL_WAYLAND_PLANE_WL;
1322                 attribs[1] = i;
1323                 attribs[2] = EGL_NONE;
1324                 gs->images[i] = gr->create_image(gr->egl_display,
1325                                                  NULL,
1326                                                  EGL_WAYLAND_BUFFER_WL,
1327                                                  buffer->legacy_buffer,
1328                                                  attribs);
1329                 if (!gs->images[i]) {
1330                         weston_log("failed to create img for plane %d\n", i);
1331                         continue;
1332                 }
1333                 gs->num_images++;
1334
1335                 glActiveTexture(GL_TEXTURE0 + i);
1336                 glBindTexture(gs->target, gs->textures[i]);
1337                 gr->image_target_texture_2d(gs->target,
1338                                             gs->images[i]);
1339         }
1340
1341         gs->pitch = buffer->width;
1342         gs->height = buffer->height;
1343         gs->buffer_type = BUFFER_TYPE_EGL;
1344         gs->y_inverted = buffer->y_inverted;
1345 }
1346
1347 static void
1348 gl_renderer_attach(struct weston_surface *es, struct weston_buffer *buffer)
1349 {
1350         struct weston_compositor *ec = es->compositor;
1351         struct gl_renderer *gr = get_renderer(ec);
1352         struct gl_surface_state *gs = get_surface_state(es);
1353         struct wl_shm_buffer *shm_buffer;
1354         EGLint format;
1355         int i;
1356
1357         weston_buffer_reference(&gs->buffer_ref, buffer);
1358
1359         if (!buffer) {
1360                 for (i = 0; i < gs->num_images; i++) {
1361                         gr->destroy_image(gr->egl_display, gs->images[i]);
1362                         gs->images[i] = NULL;
1363                 }
1364                 gs->num_images = 0;
1365                 glDeleteTextures(gs->num_textures, gs->textures);
1366                 gs->num_textures = 0;
1367                 gs->buffer_type = BUFFER_TYPE_NULL;
1368                 gs->y_inverted = 1;
1369                 return;
1370         }
1371
1372         shm_buffer = wl_shm_buffer_get(buffer->resource);
1373
1374         if (shm_buffer)
1375                 gl_renderer_attach_shm(es, buffer, shm_buffer);
1376         else if (gr->query_buffer(gr->egl_display, (void *) buffer->resource,
1377                                   EGL_TEXTURE_FORMAT, &format))
1378                 gl_renderer_attach_egl(es, buffer, format);
1379         else {
1380                 weston_log("unhandled buffer type!\n");
1381                 weston_buffer_reference(&gs->buffer_ref, NULL);
1382                 gs->buffer_type = BUFFER_TYPE_NULL;
1383                 gs->y_inverted = 1;
1384         }
1385 }
1386
1387 static void
1388 gl_renderer_surface_set_color(struct weston_surface *surface,
1389                  float red, float green, float blue, float alpha)
1390 {
1391         struct gl_surface_state *gs = get_surface_state(surface);
1392         struct gl_renderer *gr = get_renderer(surface->compositor);
1393
1394         gs->color[0] = red;
1395         gs->color[1] = green;
1396         gs->color[2] = blue;
1397         gs->color[3] = alpha;
1398         gs->buffer_type = BUFFER_TYPE_SOLID;
1399         gs->pitch = 1;
1400         gs->height = 1;
1401
1402         gs->shader = &gr->solid_shader;
1403 }
1404
1405 static void
1406 gl_renderer_surface_get_content_size(struct weston_surface *surface,
1407                                      int *width, int *height)
1408 {
1409         struct gl_surface_state *gs = get_surface_state(surface);
1410
1411         if (gs->buffer_type == BUFFER_TYPE_NULL) {
1412                 *width = 0;
1413                 *height = 0;
1414         } else {
1415                 *width = gs->pitch;
1416                 *height = gs->height;
1417         }
1418 }
1419
1420 static uint32_t
1421 pack_color(pixman_format_code_t format, float *c)
1422 {
1423         uint8_t r = round(c[0] * 255.0f);
1424         uint8_t g = round(c[1] * 255.0f);
1425         uint8_t b = round(c[2] * 255.0f);
1426         uint8_t a = round(c[3] * 255.0f);
1427
1428         switch (format) {
1429         case PIXMAN_a8b8g8r8:
1430                 return (a << 24) | (b << 16) | (g << 8) | r;
1431         default:
1432                 assert(0);
1433                 return 0;
1434         }
1435 }
1436
1437 static int
1438 gl_renderer_surface_copy_content(struct weston_surface *surface,
1439                                  void *target, size_t size,
1440                                  int src_x, int src_y,
1441                                  int width, int height)
1442 {
1443         static const GLfloat verts[4 * 2] = {
1444                 0.0f, 0.0f,
1445                 1.0f, 0.0f,
1446                 1.0f, 1.0f,
1447                 0.0f, 1.0f
1448         };
1449         static const GLfloat projmat_normal[16] = { /* transpose */
1450                  2.0f,  0.0f, 0.0f, 0.0f,
1451                  0.0f,  2.0f, 0.0f, 0.0f,
1452                  0.0f,  0.0f, 1.0f, 0.0f,
1453                 -1.0f, -1.0f, 0.0f, 1.0f
1454         };
1455         static const GLfloat projmat_yinvert[16] = { /* transpose */
1456                  2.0f,  0.0f, 0.0f, 0.0f,
1457                  0.0f, -2.0f, 0.0f, 0.0f,
1458                  0.0f,  0.0f, 1.0f, 0.0f,
1459                 -1.0f,  1.0f, 0.0f, 1.0f
1460         };
1461         const pixman_format_code_t format = PIXMAN_a8b8g8r8;
1462         const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
1463         const GLenum gl_format = GL_RGBA; /* PIXMAN_a8b8g8r8 little-endian */
1464         struct gl_renderer *gr = get_renderer(surface->compositor);
1465         struct gl_surface_state *gs = get_surface_state(surface);
1466         int cw, ch;
1467         GLuint fbo;
1468         GLuint tex;
1469         GLenum status;
1470         const GLfloat *proj;
1471         int i;
1472
1473         gl_renderer_surface_get_content_size(surface, &cw, &ch);
1474
1475         switch (gs->buffer_type) {
1476         case BUFFER_TYPE_NULL:
1477                 return -1;
1478         case BUFFER_TYPE_SOLID:
1479                 *(uint32_t *)target = pack_color(format, gs->color);
1480                 return 0;
1481         case BUFFER_TYPE_SHM:
1482                 gl_renderer_flush_damage(surface);
1483                 /* fall through */
1484         case BUFFER_TYPE_EGL:
1485                 break;
1486         }
1487
1488         glGenTextures(1, &tex);
1489         glBindTexture(GL_TEXTURE_2D, tex);
1490         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, cw, ch,
1491                      0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
1492         glBindTexture(GL_TEXTURE_2D, 0);
1493
1494         glGenFramebuffers(1, &fbo);
1495         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
1496         glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
1497                                GL_TEXTURE_2D, tex, 0);
1498
1499         status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
1500         if (status != GL_FRAMEBUFFER_COMPLETE) {
1501                 weston_log("%s: fbo error: %#x\n", __func__, status);
1502                 glDeleteFramebuffers(1, &fbo);
1503                 glDeleteTextures(1, &tex);
1504                 return -1;
1505         }
1506
1507         glViewport(0, 0, cw, ch);
1508         glDisable(GL_BLEND);
1509         use_shader(gr, gs->shader);
1510         if (gs->y_inverted)
1511                 proj = projmat_normal;
1512         else
1513                 proj = projmat_yinvert;
1514
1515         glUniformMatrix4fv(gs->shader->proj_uniform, 1, GL_FALSE, proj);
1516         glUniform1f(gs->shader->alpha_uniform, 1.0f);
1517
1518         for (i = 0; i < gs->num_textures; i++) {
1519                 glUniform1i(gs->shader->tex_uniforms[i], i);
1520
1521                 glActiveTexture(GL_TEXTURE0 + i);
1522                 glBindTexture(gs->target, gs->textures[i]);
1523                 glTexParameteri(gs->target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1524                 glTexParameteri(gs->target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1525         }
1526
1527         /* position: */
1528         glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, verts);
1529         glEnableVertexAttribArray(0);
1530
1531         /* texcoord: */
1532         glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, verts);
1533         glEnableVertexAttribArray(1);
1534
1535         glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1536
1537         glDisableVertexAttribArray(1);
1538         glDisableVertexAttribArray(0);
1539
1540         glPixelStorei(GL_PACK_ALIGNMENT, bytespp);
1541         glReadPixels(src_x, src_y, width, height, gl_format,
1542                      GL_UNSIGNED_BYTE, target);
1543
1544         glDeleteFramebuffers(1, &fbo);
1545         glDeleteTextures(1, &tex);
1546
1547         return 0;
1548 }
1549
1550 static void
1551 surface_state_destroy(struct gl_surface_state *gs, struct gl_renderer *gr)
1552 {
1553         int i;
1554
1555         wl_list_remove(&gs->surface_destroy_listener.link);
1556         wl_list_remove(&gs->renderer_destroy_listener.link);
1557
1558         gs->surface->renderer_state = NULL;
1559
1560         glDeleteTextures(gs->num_textures, gs->textures);
1561
1562         for (i = 0; i < gs->num_images; i++)
1563                 gr->destroy_image(gr->egl_display, gs->images[i]);
1564
1565         weston_buffer_reference(&gs->buffer_ref, NULL);
1566         pixman_region32_fini(&gs->texture_damage);
1567         free(gs);
1568 }
1569
1570 static void
1571 surface_state_handle_surface_destroy(struct wl_listener *listener, void *data)
1572 {
1573         struct gl_surface_state *gs;
1574         struct gl_renderer *gr;
1575
1576         gs = container_of(listener, struct gl_surface_state,
1577                           surface_destroy_listener);
1578
1579         gr = get_renderer(gs->surface->compositor);
1580
1581         surface_state_destroy(gs, gr);
1582 }
1583
1584 static void
1585 surface_state_handle_renderer_destroy(struct wl_listener *listener, void *data)
1586 {
1587         struct gl_surface_state *gs;
1588         struct gl_renderer *gr;
1589
1590         gr = data;
1591
1592         gs = container_of(listener, struct gl_surface_state,
1593                           renderer_destroy_listener);
1594
1595         surface_state_destroy(gs, gr);
1596 }
1597
1598 static int
1599 gl_renderer_create_surface(struct weston_surface *surface)
1600 {
1601         struct gl_surface_state *gs;
1602         struct gl_renderer *gr = get_renderer(surface->compositor);
1603
1604         gs = zalloc(sizeof *gs);
1605         if (gs == NULL)
1606                 return -1;
1607
1608         /* A buffer is never attached to solid color surfaces, yet
1609          * they still go through texcoord computations. Do not divide
1610          * by zero there.
1611          */
1612         gs->pitch = 1;
1613         gs->y_inverted = 1;
1614
1615         gs->surface = surface;
1616
1617         pixman_region32_init(&gs->texture_damage);
1618         surface->renderer_state = gs;
1619
1620         gs->surface_destroy_listener.notify =
1621                 surface_state_handle_surface_destroy;
1622         wl_signal_add(&surface->destroy_signal,
1623                       &gs->surface_destroy_listener);
1624
1625         gs->renderer_destroy_listener.notify =
1626                 surface_state_handle_renderer_destroy;
1627         wl_signal_add(&gr->destroy_signal,
1628                       &gs->renderer_destroy_listener);
1629
1630         if (surface->buffer_ref.buffer) {
1631                 gl_renderer_attach(surface, surface->buffer_ref.buffer);
1632                 gl_renderer_flush_damage(surface);
1633         }
1634
1635         return 0;
1636 }
1637
1638 static const char vertex_shader[] =
1639         "uniform mat4 proj;\n"
1640         "attribute vec2 position;\n"
1641         "attribute vec2 texcoord;\n"
1642         "varying vec2 v_texcoord;\n"
1643         "void main()\n"
1644         "{\n"
1645         "   gl_Position = proj * vec4(position, 0.0, 1.0);\n"
1646         "   v_texcoord = texcoord;\n"
1647         "}\n";
1648
1649 /* Declare common fragment shader uniforms */
1650 #define FRAGMENT_CONVERT_YUV                                            \
1651         "  y *= alpha;\n"                                               \
1652         "  u *= alpha;\n"                                               \
1653         "  v *= alpha;\n"                                               \
1654         "  gl_FragColor.r = y + 1.59602678 * v;\n"                      \
1655         "  gl_FragColor.g = y - 0.39176229 * u - 0.81296764 * v;\n"     \
1656         "  gl_FragColor.b = y + 2.01723214 * u;\n"                      \
1657         "  gl_FragColor.a = alpha;\n"
1658
1659 static const char fragment_debug[] =
1660         "  gl_FragColor = vec4(0.0, 0.3, 0.0, 0.2) + gl_FragColor * 0.8;\n";
1661
1662 static const char fragment_brace[] =
1663         "}\n";
1664
1665 static const char texture_fragment_shader_rgba[] =
1666         "precision mediump float;\n"
1667         "varying vec2 v_texcoord;\n"
1668         "uniform sampler2D tex;\n"
1669         "uniform float alpha;\n"
1670         "void main()\n"
1671         "{\n"
1672         "   gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
1673         ;
1674
1675 static const char texture_fragment_shader_rgbx[] =
1676         "precision mediump float;\n"
1677         "varying vec2 v_texcoord;\n"
1678         "uniform sampler2D tex;\n"
1679         "uniform float alpha;\n"
1680         "void main()\n"
1681         "{\n"
1682         "   gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb\n;"
1683         "   gl_FragColor.a = alpha;\n"
1684         ;
1685
1686 static const char texture_fragment_shader_egl_external[] =
1687         "#extension GL_OES_EGL_image_external : require\n"
1688         "precision mediump float;\n"
1689         "varying vec2 v_texcoord;\n"
1690         "uniform samplerExternalOES tex;\n"
1691         "uniform float alpha;\n"
1692         "void main()\n"
1693         "{\n"
1694         "   gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
1695         ;
1696
1697 static const char texture_fragment_shader_y_uv[] =
1698         "precision mediump float;\n"
1699         "uniform sampler2D tex;\n"
1700         "uniform sampler2D tex1;\n"
1701         "varying vec2 v_texcoord;\n"
1702         "uniform float alpha;\n"
1703         "void main() {\n"
1704         "  float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1705         "  float u = texture2D(tex1, v_texcoord).r - 0.5;\n"
1706         "  float v = texture2D(tex1, v_texcoord).g - 0.5;\n"
1707         FRAGMENT_CONVERT_YUV
1708         ;
1709
1710 static const char texture_fragment_shader_y_u_v[] =
1711         "precision mediump float;\n"
1712         "uniform sampler2D tex;\n"
1713         "uniform sampler2D tex1;\n"
1714         "uniform sampler2D tex2;\n"
1715         "varying vec2 v_texcoord;\n"
1716         "uniform float alpha;\n"
1717         "void main() {\n"
1718         "  float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1719         "  float u = texture2D(tex1, v_texcoord).x - 0.5;\n"
1720         "  float v = texture2D(tex2, v_texcoord).x - 0.5;\n"
1721         FRAGMENT_CONVERT_YUV
1722         ;
1723
1724 static const char texture_fragment_shader_y_xuxv[] =
1725         "precision mediump float;\n"
1726         "uniform sampler2D tex;\n"
1727         "uniform sampler2D tex1;\n"
1728         "varying vec2 v_texcoord;\n"
1729         "uniform float alpha;\n"
1730         "void main() {\n"
1731         "  float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1732         "  float u = texture2D(tex1, v_texcoord).g - 0.5;\n"
1733         "  float v = texture2D(tex1, v_texcoord).a - 0.5;\n"
1734         FRAGMENT_CONVERT_YUV
1735         ;
1736
1737 static const char solid_fragment_shader[] =
1738         "precision mediump float;\n"
1739         "uniform vec4 color;\n"
1740         "uniform float alpha;\n"
1741         "void main()\n"
1742         "{\n"
1743         "   gl_FragColor = alpha * color\n;"
1744         ;
1745
1746 static int
1747 compile_shader(GLenum type, int count, const char **sources)
1748 {
1749         GLuint s;
1750         char msg[512];
1751         GLint status;
1752
1753         s = glCreateShader(type);
1754         glShaderSource(s, count, sources, NULL);
1755         glCompileShader(s);
1756         glGetShaderiv(s, GL_COMPILE_STATUS, &status);
1757         if (!status) {
1758                 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1759                 weston_log("shader info: %s\n", msg);
1760                 return GL_NONE;
1761         }
1762
1763         return s;
1764 }
1765
1766 static int
1767 shader_init(struct gl_shader *shader, struct gl_renderer *renderer,
1768                    const char *vertex_source, const char *fragment_source)
1769 {
1770         char msg[512];
1771         GLint status;
1772         int count;
1773         const char *sources[3];
1774
1775         shader->vertex_shader =
1776                 compile_shader(GL_VERTEX_SHADER, 1, &vertex_source);
1777
1778         if (renderer->fragment_shader_debug) {
1779                 sources[0] = fragment_source;
1780                 sources[1] = fragment_debug;
1781                 sources[2] = fragment_brace;
1782                 count = 3;
1783         } else {
1784                 sources[0] = fragment_source;
1785                 sources[1] = fragment_brace;
1786                 count = 2;
1787         }
1788
1789         shader->fragment_shader =
1790                 compile_shader(GL_FRAGMENT_SHADER, count, sources);
1791
1792         shader->program = glCreateProgram();
1793         glAttachShader(shader->program, shader->vertex_shader);
1794         glAttachShader(shader->program, shader->fragment_shader);
1795         glBindAttribLocation(shader->program, 0, "position");
1796         glBindAttribLocation(shader->program, 1, "texcoord");
1797
1798         glLinkProgram(shader->program);
1799         glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1800         if (!status) {
1801                 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1802                 weston_log("link info: %s\n", msg);
1803                 return -1;
1804         }
1805
1806         shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1807         shader->tex_uniforms[0] = glGetUniformLocation(shader->program, "tex");
1808         shader->tex_uniforms[1] = glGetUniformLocation(shader->program, "tex1");
1809         shader->tex_uniforms[2] = glGetUniformLocation(shader->program, "tex2");
1810         shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
1811         shader->color_uniform = glGetUniformLocation(shader->program, "color");
1812
1813         return 0;
1814 }
1815
1816 static void
1817 shader_release(struct gl_shader *shader)
1818 {
1819         glDeleteShader(shader->vertex_shader);
1820         glDeleteShader(shader->fragment_shader);
1821         glDeleteProgram(shader->program);
1822
1823         shader->vertex_shader = 0;
1824         shader->fragment_shader = 0;
1825         shader->program = 0;
1826 }
1827
1828 static void
1829 log_extensions(const char *name, const char *extensions)
1830 {
1831         const char *p, *end;
1832         int l;
1833         int len;
1834
1835         l = weston_log("%s:", name);
1836         p = extensions;
1837         while (*p) {
1838                 end = strchrnul(p, ' ');
1839                 len = end - p;
1840                 if (l + len > 78)
1841                         l = weston_log_continue("\n" STAMP_SPACE "%.*s",
1842                                                 len, p);
1843                 else
1844                         l += weston_log_continue(" %.*s", len, p);
1845                 for (p = end; isspace(*p); p++)
1846                         ;
1847         }
1848         weston_log_continue("\n");
1849 }
1850
1851 static void
1852 log_egl_gl_info(EGLDisplay egldpy)
1853 {
1854         const char *str;
1855
1856         str = eglQueryString(egldpy, EGL_VERSION);
1857         weston_log("EGL version: %s\n", str ? str : "(null)");
1858
1859         str = eglQueryString(egldpy, EGL_VENDOR);
1860         weston_log("EGL vendor: %s\n", str ? str : "(null)");
1861
1862         str = eglQueryString(egldpy, EGL_CLIENT_APIS);
1863         weston_log("EGL client APIs: %s\n", str ? str : "(null)");
1864
1865         str = eglQueryString(egldpy, EGL_EXTENSIONS);
1866         log_extensions("EGL extensions", str ? str : "(null)");
1867
1868         str = (char *)glGetString(GL_VERSION);
1869         weston_log("GL version: %s\n", str ? str : "(null)");
1870
1871         str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
1872         weston_log("GLSL version: %s\n", str ? str : "(null)");
1873
1874         str = (char *)glGetString(GL_VENDOR);
1875         weston_log("GL vendor: %s\n", str ? str : "(null)");
1876
1877         str = (char *)glGetString(GL_RENDERER);
1878         weston_log("GL renderer: %s\n", str ? str : "(null)");
1879
1880         str = (char *)glGetString(GL_EXTENSIONS);
1881         log_extensions("GL extensions", str ? str : "(null)");
1882 }
1883
1884 static void
1885 log_egl_config_info(EGLDisplay egldpy, EGLConfig eglconfig)
1886 {
1887         EGLint r, g, b, a;
1888
1889         weston_log("Chosen EGL config details:\n");
1890
1891         weston_log_continue(STAMP_SPACE "RGBA bits");
1892         if (eglGetConfigAttrib(egldpy, eglconfig, EGL_RED_SIZE, &r) &&
1893             eglGetConfigAttrib(egldpy, eglconfig, EGL_GREEN_SIZE, &g) &&
1894             eglGetConfigAttrib(egldpy, eglconfig, EGL_BLUE_SIZE, &b) &&
1895             eglGetConfigAttrib(egldpy, eglconfig, EGL_ALPHA_SIZE, &a))
1896                 weston_log_continue(": %d %d %d %d\n", r, g, b, a);
1897         else
1898                 weston_log_continue(" unknown\n");
1899
1900         weston_log_continue(STAMP_SPACE "swap interval range");
1901         if (eglGetConfigAttrib(egldpy, eglconfig, EGL_MIN_SWAP_INTERVAL, &a) &&
1902             eglGetConfigAttrib(egldpy, eglconfig, EGL_MAX_SWAP_INTERVAL, &b))
1903                 weston_log_continue(": %d - %d\n", a, b);
1904         else
1905                 weston_log_continue(" unknown\n");
1906 }
1907
1908 static int
1909 match_config_to_visual(EGLDisplay egl_display,
1910                        EGLint visual_id,
1911                        EGLConfig *configs,
1912                        int count)
1913 {
1914         int i;
1915
1916         for (i = 0; i < count; ++i) {
1917                 EGLint id;
1918
1919                 if (!eglGetConfigAttrib(egl_display,
1920                                 configs[i], EGL_NATIVE_VISUAL_ID,
1921                                 &id))
1922                         continue;
1923
1924                 if (id == visual_id)
1925                         return i;
1926         }
1927
1928         return -1;
1929 }
1930
1931 static int
1932 egl_choose_config(struct gl_renderer *gr, const EGLint *attribs,
1933                   const EGLint *visual_id, const int n_ids,
1934                   EGLConfig *config_out)
1935 {
1936         EGLint count = 0;
1937         EGLint matched = 0;
1938         EGLConfig *configs;
1939         int i, config_index = -1;
1940
1941         if (!eglGetConfigs(gr->egl_display, NULL, 0, &count) || count < 1) {
1942                 weston_log("No EGL configs to choose from.\n");
1943                 return -1;
1944         }
1945         configs = calloc(count, sizeof *configs);
1946         if (!configs)
1947                 return -1;
1948
1949         if (!eglChooseConfig(gr->egl_display, attribs, configs,
1950                               count, &matched) || !matched) {
1951                 weston_log("No EGL configs with appropriate attributes.\n");
1952                 goto out;
1953         }
1954
1955         if (!visual_id)
1956                 config_index = 0;
1957
1958         for (i = 0; config_index == -1 && i < n_ids; i++)
1959                 config_index = match_config_to_visual(gr->egl_display,
1960                                                       visual_id[i],
1961                                                       configs,
1962                                                       matched);
1963
1964         if (config_index != -1)
1965                 *config_out = configs[config_index];
1966
1967 out:
1968         free(configs);
1969         if (config_index == -1)
1970                 return -1;
1971
1972         if (i > 1)
1973                 weston_log("Unable to use first choice EGL config with id"
1974                            " 0x%x, succeeded with alternate id 0x%x.\n",
1975                            visual_id[0], visual_id[i - 1]);
1976         return 0;
1977 }
1978
1979 static void
1980 gl_renderer_output_set_border(struct weston_output *output,
1981                               enum gl_renderer_border_side side,
1982                               int32_t width, int32_t height,
1983                               int32_t tex_width, unsigned char *data)
1984 {
1985         struct gl_output_state *go = get_output_state(output);
1986
1987         if (go->borders[side].width != width ||
1988             go->borders[side].height != height)
1989                 /* In this case, we have to blow everything and do a full
1990                  * repaint. */
1991                 go->border_status |= BORDER_SIZE_CHANGED | BORDER_ALL_DIRTY;
1992
1993         if (data == NULL) {
1994                 width = 0;
1995                 height = 0;
1996         }
1997
1998         go->borders[side].width = width;
1999         go->borders[side].height = height;
2000         go->borders[side].tex_width = tex_width;
2001         go->borders[side].data = data;
2002         go->border_status |= 1 << side;
2003 }
2004
2005 static int
2006 gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface);
2007
2008 static int
2009 gl_renderer_output_create(struct weston_output *output,
2010                           EGLNativeWindowType window_for_legacy,
2011                           void *window_for_platform,
2012                           const EGLint *attribs,
2013                           const EGLint *visual_id,
2014                           int n_ids)
2015 {
2016         struct weston_compositor *ec = output->compositor;
2017         struct gl_renderer *gr = get_renderer(ec);
2018         struct gl_output_state *go;
2019         EGLConfig egl_config;
2020         int i;
2021
2022         if (egl_choose_config(gr, attribs, visual_id,
2023                               n_ids, &egl_config) == -1) {
2024                 weston_log("failed to choose EGL config for output\n");
2025                 return -1;
2026         }
2027
2028         if (egl_config != gr->egl_config &&
2029             !gr->has_configless_context) {
2030                 weston_log("attempted to use a different EGL config for an "
2031                            "output but EGL_MESA_configless_context is not "
2032                            "supported\n");
2033                 return -1;
2034         }
2035
2036         go = zalloc(sizeof *go);
2037         if (go == NULL)
2038                 return -1;
2039
2040         if (gr->create_platform_window) {
2041                 go->egl_surface =
2042                         gr->create_platform_window(gr->egl_display,
2043                                                    egl_config,
2044                                                    window_for_platform,
2045                                                    NULL);
2046         } else {
2047                 go->egl_surface =
2048                         eglCreateWindowSurface(gr->egl_display,
2049                                                egl_config,
2050                                                window_for_legacy, NULL);
2051         }
2052
2053         if (go->egl_surface == EGL_NO_SURFACE) {
2054                 weston_log("failed to create egl surface\n");
2055                 free(go);
2056                 return -1;
2057         }
2058
2059         if (gr->egl_context == NULL)
2060                 if (gl_renderer_setup(ec, go->egl_surface) < 0) {
2061                         free(go);
2062                         return -1;
2063                 }
2064
2065         for (i = 0; i < BUFFER_DAMAGE_COUNT; i++)
2066                 pixman_region32_init(&go->buffer_damage[i]);
2067
2068         output->renderer_state = go;
2069
2070         log_egl_config_info(gr->egl_display, egl_config);
2071
2072         return 0;
2073 }
2074
2075 static void
2076 gl_renderer_output_destroy(struct weston_output *output)
2077 {
2078         struct gl_renderer *gr = get_renderer(output->compositor);
2079         struct gl_output_state *go = get_output_state(output);
2080         int i;
2081
2082         for (i = 0; i < 2; i++)
2083                 pixman_region32_fini(&go->buffer_damage[i]);
2084
2085         eglDestroySurface(gr->egl_display, go->egl_surface);
2086
2087         free(go);
2088 }
2089
2090 static EGLSurface
2091 gl_renderer_output_surface(struct weston_output *output)
2092 {
2093         return get_output_state(output)->egl_surface;
2094 }
2095
2096 static void
2097 gl_renderer_destroy(struct weston_compositor *ec)
2098 {
2099         struct gl_renderer *gr = get_renderer(ec);
2100
2101         wl_signal_emit(&gr->destroy_signal, gr);
2102
2103         if (gr->has_bind_display)
2104                 gr->unbind_display(gr->egl_display, ec->wl_display);
2105
2106         /* Work around crash in egl_dri2.c's dri2_make_current() - when does this apply? */
2107         eglMakeCurrent(gr->egl_display,
2108                        EGL_NO_SURFACE, EGL_NO_SURFACE,
2109                        EGL_NO_CONTEXT);
2110
2111         eglTerminate(gr->egl_display);
2112         eglReleaseThread();
2113
2114         wl_array_release(&gr->vertices);
2115         wl_array_release(&gr->vtxcnt);
2116
2117         if (gr->fragment_binding)
2118                 weston_binding_destroy(gr->fragment_binding);
2119         if (gr->fan_binding)
2120                 weston_binding_destroy(gr->fan_binding);
2121
2122         free(gr);
2123 }
2124
2125 static void
2126 renderer_setup_egl_client_extensions(struct gl_renderer *gr)
2127 {
2128         const char *extensions;
2129
2130         extensions = eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS);
2131         if (!extensions) {
2132                 weston_log("Retrieving EGL client extension string failed.\n");
2133                 return;
2134         }
2135
2136         if (strstr(extensions, "EGL_EXT_platform_base"))
2137                 gr->create_platform_window =
2138                         (void *) eglGetProcAddress("eglCreatePlatformWindowSurfaceEXT");
2139         else
2140                 weston_log("warning: EGL_EXT_platform_base not supported.\n");
2141 }
2142
2143 static int
2144 gl_renderer_setup_egl_extensions(struct weston_compositor *ec)
2145 {
2146         struct gl_renderer *gr = get_renderer(ec);
2147         const char *extensions;
2148         EGLBoolean ret;
2149
2150         gr->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2151         gr->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2152         gr->bind_display =
2153                 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
2154         gr->unbind_display =
2155                 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
2156         gr->query_buffer =
2157                 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
2158
2159         extensions =
2160                 (const char *) eglQueryString(gr->egl_display, EGL_EXTENSIONS);
2161         if (!extensions) {
2162                 weston_log("Retrieving EGL extension string failed.\n");
2163                 return -1;
2164         }
2165
2166         if (strstr(extensions, "EGL_WL_bind_wayland_display"))
2167                 gr->has_bind_display = 1;
2168         if (gr->has_bind_display) {
2169                 ret = gr->bind_display(gr->egl_display, ec->wl_display);
2170                 if (!ret)
2171                         gr->has_bind_display = 0;
2172         }
2173
2174         if (strstr(extensions, "EGL_EXT_buffer_age"))
2175                 gr->has_egl_buffer_age = 1;
2176         else
2177                 weston_log("warning: EGL_EXT_buffer_age not supported. "
2178                            "Performance could be affected.\n");
2179
2180 #ifdef EGL_EXT_swap_buffers_with_damage
2181         if (strstr(extensions, "EGL_EXT_swap_buffers_with_damage"))
2182                 gr->swap_buffers_with_damage =
2183                         (void *) eglGetProcAddress("eglSwapBuffersWithDamageEXT");
2184         else
2185                 weston_log("warning: EGL_EXT_swap_buffers_with_damage not "
2186                            "supported. Performance could be affected.\n");
2187 #endif
2188
2189 #ifdef EGL_MESA_configless_context
2190         if (strstr(extensions, "EGL_MESA_configless_context"))
2191                 gr->has_configless_context = 1;
2192 #endif
2193
2194         renderer_setup_egl_client_extensions(gr);
2195
2196         return 0;
2197 }
2198
2199 static const EGLint gl_renderer_opaque_attribs[] = {
2200         EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
2201         EGL_RED_SIZE, 1,
2202         EGL_GREEN_SIZE, 1,
2203         EGL_BLUE_SIZE, 1,
2204         EGL_ALPHA_SIZE, 0,
2205         EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
2206         EGL_NONE
2207 };
2208
2209 static const EGLint gl_renderer_alpha_attribs[] = {
2210         EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
2211         EGL_RED_SIZE, 1,
2212         EGL_GREEN_SIZE, 1,
2213         EGL_BLUE_SIZE, 1,
2214         EGL_ALPHA_SIZE, 1,
2215         EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
2216         EGL_NONE
2217 };
2218
2219 /** Checks whether a platform EGL client extension is supported
2220  *
2221  * \param ec The weston compositor
2222  * \param extension_suffix The EGL client extension suffix
2223  * \return 1 if supported, 0 if using fallbacks, -1 unsupported
2224  *
2225  * This function checks whether a specific platform_* extension is supported
2226  * by EGL.
2227  *
2228  * The extension suffix should be the suffix of the platform extension (that
2229  * specifies a <platform> argument as defined in EGL_EXT_platform_base). For
2230  * example, passing "foo" will check whether either "EGL_KHR_platform_foo",
2231  * "EGL_EXT_platform_foo", or "EGL_MESA_platform_foo" is supported.
2232  *
2233  * The return value is 1:
2234  *   - if the supplied EGL client extension is supported.
2235  * The return value is 0:
2236  *   - if the platform_base client extension isn't supported so will
2237  *     fallback to eglGetDisplay and friends.
2238  * The return value is -1:
2239  *   - if the supplied EGL client extension is not supported.
2240  */
2241 static int
2242 gl_renderer_supports(struct weston_compositor *ec,
2243                      const char *extension_suffix)
2244 {
2245         static const char *extensions = NULL;
2246         char s[64];
2247
2248         if (!extensions) {
2249                 extensions = (const char *) eglQueryString(
2250                         EGL_NO_DISPLAY, EGL_EXTENSIONS);
2251
2252                 if (!extensions)
2253                         return 0;
2254
2255                 log_extensions("EGL client extensions",
2256                                extensions);
2257         }
2258
2259         if (!strstr(extensions, "EGL_EXT_platform_base"))
2260                 return 0;
2261
2262         snprintf(s, sizeof s, "EGL_KHR_platform_%s", extension_suffix);
2263         if (strstr(extensions, s))
2264                 return 1;
2265
2266         snprintf(s, sizeof s, "EGL_EXT_platform_%s", extension_suffix);
2267         if (strstr(extensions, s))
2268                 return 1;
2269
2270         snprintf(s, sizeof s, "EGL_MESA_platform_%s", extension_suffix);
2271         if (strstr(extensions, s))
2272                 return 1;
2273
2274         /* at this point we definitely have some platform extensions but
2275          * haven't found the supplied platform, so chances are it's
2276          * not supported. */
2277
2278         return -1;
2279 }
2280
2281 static const char *
2282 platform_to_extension(EGLenum platform)
2283 {
2284         switch (platform) {
2285         case EGL_PLATFORM_GBM_KHR:
2286                 return "gbm";
2287         case EGL_PLATFORM_WAYLAND_KHR:
2288                 return "wayland";
2289         case EGL_PLATFORM_X11_KHR:
2290                 return "x11";
2291         default:
2292                 assert(0 && "bad EGL platform enum");
2293         }
2294 }
2295
2296 static int
2297 gl_renderer_create(struct weston_compositor *ec, EGLenum platform,
2298         void *native_window, const EGLint *attribs,
2299         const EGLint *visual_id, int n_ids)
2300 {
2301         struct gl_renderer *gr;
2302         EGLint major, minor;
2303         int supports = 0;
2304
2305         if (platform) {
2306                 supports = gl_renderer_supports(
2307                         ec, platform_to_extension(platform));
2308                 if (supports < 0)
2309                         return -1;
2310         }
2311
2312         gr = zalloc(sizeof *gr);
2313         if (gr == NULL)
2314                 return -1;
2315
2316         gr->base.read_pixels = gl_renderer_read_pixels;
2317         gr->base.repaint_output = gl_renderer_repaint_output;
2318         gr->base.flush_damage = gl_renderer_flush_damage;
2319         gr->base.attach = gl_renderer_attach;
2320         gr->base.surface_set_color = gl_renderer_surface_set_color;
2321         gr->base.destroy = gl_renderer_destroy;
2322         gr->base.surface_get_content_size =
2323                 gl_renderer_surface_get_content_size;
2324         gr->base.surface_copy_content = gl_renderer_surface_copy_content;
2325         gr->egl_display = NULL;
2326
2327         /* extension_suffix is supported */
2328         if (supports) {
2329                 if (!get_platform_display) {
2330                         get_platform_display = (void *) eglGetProcAddress(
2331                                         "eglGetPlatformDisplayEXT");
2332                 }
2333
2334                 /* also wrap this in the supports check because
2335                  * eglGetProcAddress can return non-NULL and still not
2336                  * support the feature at runtime, so ensure the
2337                  * appropriate extension checks have been done. */
2338                 if (get_platform_display && platform) {
2339                         gr->egl_display = get_platform_display(platform,
2340                                                                native_window,
2341                                                                NULL);
2342                 }
2343         }
2344
2345         if (!gr->egl_display) {
2346                 weston_log("warning: either no EGL_EXT_platform_base "
2347                            "support or specific platform support; "
2348                            "falling back to eglGetDisplay.\n");
2349                 gr->egl_display = eglGetDisplay(native_window);
2350         }
2351
2352         if (gr->egl_display == EGL_NO_DISPLAY) {
2353                 weston_log("failed to create display\n");
2354                 goto err_egl;
2355         }
2356
2357         if (!eglInitialize(gr->egl_display, &major, &minor)) {
2358                 weston_log("failed to initialize display\n");
2359                 goto err_egl;
2360         }
2361
2362         if (egl_choose_config(gr, attribs, visual_id,
2363                               n_ids, &gr->egl_config) < 0) {
2364                 weston_log("failed to choose EGL config\n");
2365                 goto err_config;
2366         }
2367
2368         ec->renderer = &gr->base;
2369         ec->capabilities |= WESTON_CAP_ROTATION_ANY;
2370         ec->capabilities |= WESTON_CAP_CAPTURE_YFLIP;
2371         ec->capabilities |= WESTON_CAP_VIEW_CLIP_MASK;
2372
2373         if (gl_renderer_setup_egl_extensions(ec) < 0)
2374                 goto err_egl;
2375
2376         wl_display_add_shm_format(ec->wl_display, WL_SHM_FORMAT_RGB565);
2377
2378         wl_signal_init(&gr->destroy_signal);
2379
2380         return 0;
2381
2382 err_egl:
2383         gl_renderer_print_egl_error_state();
2384 err_config:
2385         free(gr);
2386         return -1;
2387 }
2388
2389 static EGLDisplay
2390 gl_renderer_display(struct weston_compositor *ec)
2391 {
2392         return get_renderer(ec)->egl_display;
2393 }
2394
2395 static int
2396 compile_shaders(struct weston_compositor *ec)
2397 {
2398         struct gl_renderer *gr = get_renderer(ec);
2399
2400         gr->texture_shader_rgba.vertex_source = vertex_shader;
2401         gr->texture_shader_rgba.fragment_source = texture_fragment_shader_rgba;
2402
2403         gr->texture_shader_rgbx.vertex_source = vertex_shader;
2404         gr->texture_shader_rgbx.fragment_source = texture_fragment_shader_rgbx;
2405
2406         gr->texture_shader_egl_external.vertex_source = vertex_shader;
2407         gr->texture_shader_egl_external.fragment_source =
2408                 texture_fragment_shader_egl_external;
2409
2410         gr->texture_shader_y_uv.vertex_source = vertex_shader;
2411         gr->texture_shader_y_uv.fragment_source = texture_fragment_shader_y_uv;
2412
2413         gr->texture_shader_y_u_v.vertex_source = vertex_shader;
2414         gr->texture_shader_y_u_v.fragment_source =
2415                 texture_fragment_shader_y_u_v;
2416
2417         gr->texture_shader_y_xuxv.vertex_source = vertex_shader;
2418         gr->texture_shader_y_xuxv.fragment_source =
2419                 texture_fragment_shader_y_xuxv;
2420
2421         gr->solid_shader.vertex_source = vertex_shader;
2422         gr->solid_shader.fragment_source = solid_fragment_shader;
2423
2424         return 0;
2425 }
2426
2427 static void
2428 fragment_debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
2429                        void *data)
2430 {
2431         struct weston_compositor *ec = data;
2432         struct gl_renderer *gr = get_renderer(ec);
2433         struct weston_output *output;
2434
2435         gr->fragment_shader_debug ^= 1;
2436
2437         shader_release(&gr->texture_shader_rgba);
2438         shader_release(&gr->texture_shader_rgbx);
2439         shader_release(&gr->texture_shader_egl_external);
2440         shader_release(&gr->texture_shader_y_uv);
2441         shader_release(&gr->texture_shader_y_u_v);
2442         shader_release(&gr->texture_shader_y_xuxv);
2443         shader_release(&gr->solid_shader);
2444
2445         /* Force use_shader() to call glUseProgram(), since we need to use
2446          * the recompiled version of the shader. */
2447         gr->current_shader = NULL;
2448
2449         wl_list_for_each(output, &ec->output_list, link)
2450                 weston_output_damage(output);
2451 }
2452
2453 static void
2454 fan_debug_repaint_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
2455                       void *data)
2456 {
2457         struct weston_compositor *compositor = data;
2458         struct gl_renderer *gr = get_renderer(compositor);
2459
2460         gr->fan_debug = !gr->fan_debug;
2461         weston_compositor_damage_all(compositor);
2462 }
2463
2464 static int
2465 gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface)
2466 {
2467         struct gl_renderer *gr = get_renderer(ec);
2468         const char *extensions;
2469         EGLConfig context_config;
2470         EGLBoolean ret;
2471
2472         static const EGLint context_attribs[] = {
2473                 EGL_CONTEXT_CLIENT_VERSION, 2,
2474                 EGL_NONE
2475         };
2476
2477         if (!eglBindAPI(EGL_OPENGL_ES_API)) {
2478                 weston_log("failed to bind EGL_OPENGL_ES_API\n");
2479                 gl_renderer_print_egl_error_state();
2480                 return -1;
2481         }
2482
2483         context_config = gr->egl_config;
2484
2485 #ifdef EGL_MESA_configless_context
2486         if (gr->has_configless_context)
2487                 context_config = EGL_NO_CONFIG_MESA;
2488 #endif
2489
2490         gr->egl_context = eglCreateContext(gr->egl_display, context_config,
2491                                            EGL_NO_CONTEXT, context_attribs);
2492         if (gr->egl_context == NULL) {
2493                 weston_log("failed to create context\n");
2494                 gl_renderer_print_egl_error_state();
2495                 return -1;
2496         }
2497
2498         ret = eglMakeCurrent(gr->egl_display, egl_surface,
2499                              egl_surface, gr->egl_context);
2500         if (ret == EGL_FALSE) {
2501                 weston_log("Failed to make EGL context current.\n");
2502                 gl_renderer_print_egl_error_state();
2503                 return -1;
2504         }
2505
2506         log_egl_gl_info(gr->egl_display);
2507
2508         gr->image_target_texture_2d =
2509                 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2510
2511         extensions = (const char *) glGetString(GL_EXTENSIONS);
2512         if (!extensions) {
2513                 weston_log("Retrieving GL extension string failed.\n");
2514                 return -1;
2515         }
2516
2517         if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
2518                 weston_log("GL_EXT_texture_format_BGRA8888 not available\n");
2519                 return -1;
2520         }
2521
2522         if (strstr(extensions, "GL_EXT_read_format_bgra"))
2523                 ec->read_format = PIXMAN_a8r8g8b8;
2524         else
2525                 ec->read_format = PIXMAN_a8b8g8r8;
2526
2527 #ifdef GL_EXT_unpack_subimage
2528         if (strstr(extensions, "GL_EXT_unpack_subimage"))
2529                 gr->has_unpack_subimage = 1;
2530 #endif
2531
2532         if (strstr(extensions, "GL_OES_EGL_image_external"))
2533                 gr->has_egl_image_external = 1;
2534
2535         glActiveTexture(GL_TEXTURE0);
2536
2537         if (compile_shaders(ec))
2538                 return -1;
2539
2540         gr->fragment_binding =
2541                 weston_compositor_add_debug_binding(ec, KEY_S,
2542                                                     fragment_debug_binding,
2543                                                     ec);
2544         gr->fan_binding =
2545                 weston_compositor_add_debug_binding(ec, KEY_F,
2546                                                     fan_debug_repaint_binding,
2547                                                     ec);
2548
2549         weston_log("GL ES 2 renderer features:\n");
2550         weston_log_continue(STAMP_SPACE "read-back format: %s\n",
2551                 ec->read_format == PIXMAN_a8r8g8b8 ? "BGRA" : "RGBA");
2552         weston_log_continue(STAMP_SPACE "wl_shm sub-image to texture: %s\n",
2553                             gr->has_unpack_subimage ? "yes" : "no");
2554         weston_log_continue(STAMP_SPACE "EGL Wayland extension: %s\n",
2555                             gr->has_bind_display ? "yes" : "no");
2556
2557
2558         return 0;
2559 }
2560
2561 WL_EXPORT struct gl_renderer_interface gl_renderer_interface = {
2562         .opaque_attribs = gl_renderer_opaque_attribs,
2563         .alpha_attribs = gl_renderer_alpha_attribs,
2564
2565         .create = gl_renderer_create,
2566         .display = gl_renderer_display,
2567         .output_create = gl_renderer_output_create,
2568         .output_destroy = gl_renderer_output_destroy,
2569         .output_surface = gl_renderer_output_surface,
2570         .output_set_border = gl_renderer_output_set_border,
2571         .print_egl_error_state = gl_renderer_print_egl_error_state
2572 };