Update package changelog.
[profile/ivi/weston.git] / src / gles2-renderer.c
1 /*
2  * Copyright © 2012 Intel Corporation
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and
5  * its documentation for any purpose is hereby granted without fee, provided
6  * that the above copyright notice appear in all copies and that both that
7  * copyright notice and this permission notice appear in supporting
8  * documentation, and that the name of the copyright holders not be used in
9  * advertising or publicity pertaining to distribution of the software
10  * without specific, written prior permission.  The copyright holders make
11  * no representations about the suitability of this software for any
12  * purpose.  It is provided "as is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21  */
22
23 #define _GNU_SOURCE
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <ctype.h>
28 #include <float.h>
29 #include <assert.h>
30
31 #include "compositor.h"
32 #include "weston-egl-ext.h"
33
34 static const char *
35 egl_error_string(EGLint code)
36 {
37 #define MYERRCODE(x) case x: return #x;
38         switch (code) {
39         MYERRCODE(EGL_SUCCESS)
40         MYERRCODE(EGL_NOT_INITIALIZED)
41         MYERRCODE(EGL_BAD_ACCESS)
42         MYERRCODE(EGL_BAD_ALLOC)
43         MYERRCODE(EGL_BAD_ATTRIBUTE)
44         MYERRCODE(EGL_BAD_CONTEXT)
45         MYERRCODE(EGL_BAD_CONFIG)
46         MYERRCODE(EGL_BAD_CURRENT_SURFACE)
47         MYERRCODE(EGL_BAD_DISPLAY)
48         MYERRCODE(EGL_BAD_SURFACE)
49         MYERRCODE(EGL_BAD_MATCH)
50         MYERRCODE(EGL_BAD_PARAMETER)
51         MYERRCODE(EGL_BAD_NATIVE_PIXMAP)
52         MYERRCODE(EGL_BAD_NATIVE_WINDOW)
53         MYERRCODE(EGL_CONTEXT_LOST)
54         default:
55                 return "unknown";
56         }
57 #undef MYERRCODE
58 }
59
60 static void
61 print_egl_error_state(void)
62 {
63         EGLint code;
64
65         code = eglGetError();
66         weston_log("EGL error state: %s (0x%04lx)\n",
67                 egl_error_string(code), (long)code);
68 }
69
70 struct polygon8 {
71         GLfloat x[8];
72         GLfloat y[8];
73         int n;
74 };
75
76 struct clip_context {
77         struct {
78                 GLfloat x;
79                 GLfloat y;
80         } prev;
81
82         struct {
83                 GLfloat x1, y1;
84                 GLfloat x2, y2;
85         } clip;
86
87         struct {
88                 GLfloat *x;
89                 GLfloat *y;
90         } vertices;
91 };
92
93 static GLfloat
94 float_difference(GLfloat a, GLfloat b)
95 {
96         /* http://www.altdevblogaday.com/2012/02/22/comparing-floating-point-numbers-2012-edition/ */
97         static const GLfloat max_diff = 4.0f * FLT_MIN;
98         static const GLfloat max_rel_diff = 4.0e-5;
99         GLfloat diff = a - b;
100         GLfloat adiff = fabsf(diff);
101
102         if (adiff <= max_diff)
103                 return 0.0f;
104
105         a = fabsf(a);
106         b = fabsf(b);
107         if (adiff <= (a > b ? a : b) * max_rel_diff)
108                 return 0.0f;
109
110         return diff;
111 }
112
113 /* A line segment (p1x, p1y)-(p2x, p2y) intersects the line x = x_arg.
114  * Compute the y coordinate of the intersection.
115  */
116 static GLfloat
117 clip_intersect_y(GLfloat p1x, GLfloat p1y, GLfloat p2x, GLfloat p2y,
118                  GLfloat x_arg)
119 {
120         GLfloat a;
121         GLfloat diff = float_difference(p1x, p2x);
122
123         /* Practically vertical line segment, yet the end points have already
124          * been determined to be on different sides of the line. Therefore
125          * the line segment is part of the line and intersects everywhere.
126          * Return the end point, so we use the whole line segment.
127          */
128         if (diff == 0.0f)
129                 return p2y;
130
131         a = (x_arg - p2x) / diff;
132         return p2y + (p1y - p2y) * a;
133 }
134
135 /* A line segment (p1x, p1y)-(p2x, p2y) intersects the line y = y_arg.
136  * Compute the x coordinate of the intersection.
137  */
138 static GLfloat
139 clip_intersect_x(GLfloat p1x, GLfloat p1y, GLfloat p2x, GLfloat p2y,
140                  GLfloat y_arg)
141 {
142         GLfloat a;
143         GLfloat diff = float_difference(p1y, p2y);
144
145         /* Practically horizontal line segment, yet the end points have already
146          * been determined to be on different sides of the line. Therefore
147          * the line segment is part of the line and intersects everywhere.
148          * Return the end point, so we use the whole line segment.
149          */
150         if (diff == 0.0f)
151                 return p2x;
152
153         a = (y_arg - p2y) / diff;
154         return p2x + (p1x - p2x) * a;
155 }
156
157 enum path_transition {
158         PATH_TRANSITION_OUT_TO_OUT = 0,
159         PATH_TRANSITION_OUT_TO_IN = 1,
160         PATH_TRANSITION_IN_TO_OUT = 2,
161         PATH_TRANSITION_IN_TO_IN = 3,
162 };
163
164 static void
165 clip_append_vertex(struct clip_context *ctx, GLfloat x, GLfloat y)
166 {
167         *ctx->vertices.x++ = x;
168         *ctx->vertices.y++ = y;
169 }
170
171 static enum path_transition
172 path_transition_left_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
173 {
174         return ((ctx->prev.x >= ctx->clip.x1) << 1) | (x >= ctx->clip.x1);
175 }
176
177 static enum path_transition
178 path_transition_right_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
179 {
180         return ((ctx->prev.x < ctx->clip.x2) << 1) | (x < ctx->clip.x2);
181 }
182
183 static enum path_transition
184 path_transition_top_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
185 {
186         return ((ctx->prev.y >= ctx->clip.y1) << 1) | (y >= ctx->clip.y1);
187 }
188
189 static enum path_transition
190 path_transition_bottom_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
191 {
192         return ((ctx->prev.y < ctx->clip.y2) << 1) | (y < ctx->clip.y2);
193 }
194
195 static void
196 clip_polygon_leftright(struct clip_context *ctx,
197                        enum path_transition transition,
198                        GLfloat x, GLfloat y, GLfloat clip_x)
199 {
200         GLfloat yi;
201
202         switch (transition) {
203         case PATH_TRANSITION_IN_TO_IN:
204                 clip_append_vertex(ctx, x, y);
205                 break;
206         case PATH_TRANSITION_IN_TO_OUT:
207                 yi = clip_intersect_y(ctx->prev.x, ctx->prev.y, x, y, clip_x);
208                 clip_append_vertex(ctx, clip_x, yi);
209                 break;
210         case PATH_TRANSITION_OUT_TO_IN:
211                 yi = clip_intersect_y(ctx->prev.x, ctx->prev.y, x, y, clip_x);
212                 clip_append_vertex(ctx, clip_x, yi);
213                 clip_append_vertex(ctx, x, y);
214                 break;
215         case PATH_TRANSITION_OUT_TO_OUT:
216                 /* nothing */
217                 break;
218         default:
219                 assert(0 && "bad enum path_transition");
220         }
221
222         ctx->prev.x = x;
223         ctx->prev.y = y;
224 }
225
226 static void
227 clip_polygon_topbottom(struct clip_context *ctx,
228                        enum path_transition transition,
229                        GLfloat x, GLfloat y, GLfloat clip_y)
230 {
231         GLfloat xi;
232
233         switch (transition) {
234         case PATH_TRANSITION_IN_TO_IN:
235                 clip_append_vertex(ctx, x, y);
236                 break;
237         case PATH_TRANSITION_IN_TO_OUT:
238                 xi = clip_intersect_x(ctx->prev.x, ctx->prev.y, x, y, clip_y);
239                 clip_append_vertex(ctx, xi, clip_y);
240                 break;
241         case PATH_TRANSITION_OUT_TO_IN:
242                 xi = clip_intersect_x(ctx->prev.x, ctx->prev.y, x, y, clip_y);
243                 clip_append_vertex(ctx, xi, clip_y);
244                 clip_append_vertex(ctx, x, y);
245                 break;
246         case PATH_TRANSITION_OUT_TO_OUT:
247                 /* nothing */
248                 break;
249         default:
250                 assert(0 && "bad enum path_transition");
251         }
252
253         ctx->prev.x = x;
254         ctx->prev.y = y;
255 }
256
257 static void
258 clip_context_prepare(struct clip_context *ctx, const struct polygon8 *src,
259                       GLfloat *dst_x, GLfloat *dst_y)
260 {
261         ctx->prev.x = src->x[src->n - 1];
262         ctx->prev.y = src->y[src->n - 1];
263         ctx->vertices.x = dst_x;
264         ctx->vertices.y = dst_y;
265 }
266
267 static int
268 clip_polygon_left(struct clip_context *ctx, const struct polygon8 *src,
269                   GLfloat *dst_x, GLfloat *dst_y)
270 {
271         enum path_transition trans;
272         int i;
273
274         clip_context_prepare(ctx, src, dst_x, dst_y);
275         for (i = 0; i < src->n; i++) {
276                 trans = path_transition_left_edge(ctx, src->x[i], src->y[i]);
277                 clip_polygon_leftright(ctx, trans, src->x[i], src->y[i],
278                                        ctx->clip.x1);
279         }
280         return ctx->vertices.x - dst_x;
281 }
282
283 static int
284 clip_polygon_right(struct clip_context *ctx, const struct polygon8 *src,
285                    GLfloat *dst_x, GLfloat *dst_y)
286 {
287         enum path_transition trans;
288         int i;
289
290         clip_context_prepare(ctx, src, dst_x, dst_y);
291         for (i = 0; i < src->n; i++) {
292                 trans = path_transition_right_edge(ctx, src->x[i], src->y[i]);
293                 clip_polygon_leftright(ctx, trans, src->x[i], src->y[i],
294                                        ctx->clip.x2);
295         }
296         return ctx->vertices.x - dst_x;
297 }
298
299 static int
300 clip_polygon_top(struct clip_context *ctx, const struct polygon8 *src,
301                  GLfloat *dst_x, GLfloat *dst_y)
302 {
303         enum path_transition trans;
304         int i;
305
306         clip_context_prepare(ctx, src, dst_x, dst_y);
307         for (i = 0; i < src->n; i++) {
308                 trans = path_transition_top_edge(ctx, src->x[i], src->y[i]);
309                 clip_polygon_topbottom(ctx, trans, src->x[i], src->y[i],
310                                        ctx->clip.y1);
311         }
312         return ctx->vertices.x - dst_x;
313 }
314
315 static int
316 clip_polygon_bottom(struct clip_context *ctx, const struct polygon8 *src,
317                     GLfloat *dst_x, GLfloat *dst_y)
318 {
319         enum path_transition trans;
320         int i;
321
322         clip_context_prepare(ctx, src, dst_x, dst_y);
323         for (i = 0; i < src->n; i++) {
324                 trans = path_transition_bottom_edge(ctx, src->x[i], src->y[i]);
325                 clip_polygon_topbottom(ctx, trans, src->x[i], src->y[i],
326                                        ctx->clip.y2);
327         }
328         return ctx->vertices.x - dst_x;
329 }
330
331 #define max(a, b) (((a) > (b)) ? (a) : (b))
332 #define min(a, b) (((a) > (b)) ? (b) : (a))
333 #define clip(x, a, b)  min(max(x, a), b)
334
335 /*
336  * Compute the boundary vertices of the intersection of the global coordinate
337  * aligned rectangle 'rect', and an arbitrary quadrilateral produced from
338  * 'surf_rect' when transformed from surface coordinates into global coordinates.
339  * The vertices are written to 'ex' and 'ey', and the return value is the
340  * number of vertices. Vertices are produced in clockwise winding order.
341  * Guarantees to produce either zero vertices, or 3-8 vertices with non-zero
342  * polygon area.
343  */
344 static int
345 calculate_edges(struct weston_surface *es, pixman_box32_t *rect,
346                 pixman_box32_t *surf_rect, GLfloat *ex, GLfloat *ey)
347 {
348         struct polygon8 polygon;
349         struct clip_context ctx;
350         int i, n;
351         GLfloat min_x, max_x, min_y, max_y;
352         struct polygon8 surf = {
353                 { surf_rect->x1, surf_rect->x2, surf_rect->x2, surf_rect->x1 },
354                 { surf_rect->y1, surf_rect->y1, surf_rect->y2, surf_rect->y2 },
355                 4
356         };
357
358         ctx.clip.x1 = rect->x1;
359         ctx.clip.y1 = rect->y1;
360         ctx.clip.x2 = rect->x2;
361         ctx.clip.y2 = rect->y2;
362
363         /* transform surface to screen space: */
364         for (i = 0; i < surf.n; i++)
365                 weston_surface_to_global_float(es, surf.x[i], surf.y[i],
366                                                &surf.x[i], &surf.y[i]);
367
368         /* find bounding box: */
369         min_x = max_x = surf.x[0];
370         min_y = max_y = surf.y[0];
371
372         for (i = 1; i < surf.n; i++) {
373                 min_x = min(min_x, surf.x[i]);
374                 max_x = max(max_x, surf.x[i]);
375                 min_y = min(min_y, surf.y[i]);
376                 max_y = max(max_y, surf.y[i]);
377         }
378
379         /* First, simple bounding box check to discard early transformed
380          * surface rects that do not intersect with the clip region:
381          */
382         if ((min_x >= ctx.clip.x2) || (max_x <= ctx.clip.x1) ||
383             (min_y >= ctx.clip.y2) || (max_y <= ctx.clip.y1))
384                 return 0;
385
386         /* Simple case, bounding box edges are parallel to surface edges,
387          * there will be only four edges.  We just need to clip the surface
388          * vertices to the clip rect bounds:
389          */
390         if (!es->transform.enabled) {
391                 for (i = 0; i < surf.n; i++) {
392                         ex[i] = clip(surf.x[i], ctx.clip.x1, ctx.clip.x2);
393                         ey[i] = clip(surf.y[i], ctx.clip.y1, ctx.clip.y2);
394                 }
395                 return surf.n;
396         }
397
398         /* Transformed case: use a general polygon clipping algorithm to
399          * clip the surface rectangle with each side of 'rect'.
400          * The algorithm is Sutherland-Hodgman, as explained in
401          * http://www.codeguru.com/cpp/misc/misc/graphics/article.php/c8965/Polygon-Clipping.htm
402          * but without looking at any of that code.
403          */
404         polygon.n = clip_polygon_left(&ctx, &surf, polygon.x, polygon.y);
405         surf.n = clip_polygon_right(&ctx, &polygon, surf.x, surf.y);
406         polygon.n = clip_polygon_top(&ctx, &surf, polygon.x, polygon.y);
407         surf.n = clip_polygon_bottom(&ctx, &polygon, surf.x, surf.y);
408
409         /* Get rid of duplicate vertices */
410         ex[0] = surf.x[0];
411         ey[0] = surf.y[0];
412         n = 1;
413         for (i = 1; i < surf.n; i++) {
414                 if (float_difference(ex[n - 1], surf.x[i]) == 0.0f &&
415                     float_difference(ey[n - 1], surf.y[i]) == 0.0f)
416                         continue;
417                 ex[n] = surf.x[i];
418                 ey[n] = surf.y[i];
419                 n++;
420         }
421         if (float_difference(ex[n - 1], surf.x[0]) == 0.0f &&
422             float_difference(ey[n - 1], surf.y[0]) == 0.0f)
423                 n--;
424
425         if (n < 3)
426                 return 0;
427
428         return n;
429 }
430
431 static int
432 texture_region(struct weston_surface *es, pixman_region32_t *region,
433                 pixman_region32_t *surf_region)
434 {
435         struct weston_compositor *ec = es->compositor;
436         GLfloat *v, inv_width, inv_height;
437         unsigned int *vtxcnt, nvtx = 0;
438         pixman_box32_t *rects, *surf_rects;
439         int i, j, k, nrects, nsurf;
440
441         rects = pixman_region32_rectangles(region, &nrects);
442         surf_rects = pixman_region32_rectangles(surf_region, &nsurf);
443
444         /* worst case we can have 8 vertices per rect (ie. clipped into
445          * an octagon):
446          */
447         v = wl_array_add(&ec->vertices, nrects * nsurf * 8 * 4 * sizeof *v);
448         vtxcnt = wl_array_add(&ec->vtxcnt, nrects * nsurf * sizeof *vtxcnt);
449
450         inv_width = 1.0 / es->pitch;
451         inv_height = 1.0 / es->geometry.height;
452
453         for (i = 0; i < nrects; i++) {
454                 pixman_box32_t *rect = &rects[i];
455                 for (j = 0; j < nsurf; j++) {
456                         pixman_box32_t *surf_rect = &surf_rects[j];
457                         GLfloat sx, sy;
458                         GLfloat ex[8], ey[8];          /* edge points in screen space */
459                         int n;
460
461                         /* The transformed surface, after clipping to the clip region,
462                          * can have as many as eight sides, emitted as a triangle-fan.
463                          * The first vertex in the triangle fan can be chosen arbitrarily,
464                          * since the area is guaranteed to be convex.
465                          *
466                          * If a corner of the transformed surface falls outside of the
467                          * clip region, instead of emitting one vertex for the corner
468                          * of the surface, up to two are emitted for two corresponding
469                          * intersection point(s) between the surface and the clip region.
470                          *
471                          * To do this, we first calculate the (up to eight) points that
472                          * form the intersection of the clip rect and the transformed
473                          * surface.
474                          */
475                         n = calculate_edges(es, rect, surf_rect, ex, ey);
476                         if (n < 3)
477                                 continue;
478
479                         /* emit edge points: */
480                         for (k = 0; k < n; k++) {
481                                 weston_surface_from_global_float(es, ex[k], ey[k], &sx, &sy);
482                                 /* position: */
483                                 *(v++) = ex[k];
484                                 *(v++) = ey[k];
485                                 /* texcoord: */
486                                 *(v++) = sx * inv_width;
487                                 *(v++) = sy * inv_height;
488                         }
489
490                         vtxcnt[nvtx++] = n;
491                 }
492         }
493
494         return nvtx;
495 }
496
497 static void
498 triangle_fan_debug(struct weston_surface *surface, int first, int count)
499 {
500         struct weston_compositor *compositor = surface->compositor;
501         int i;
502         GLushort *buffer;
503         GLushort *index;
504         int nelems;
505         static int color_idx = 0;
506         static const GLfloat color[][4] = {
507                         { 1.0, 0.0, 0.0, 1.0 },
508                         { 0.0, 1.0, 0.0, 1.0 },
509                         { 0.0, 0.0, 1.0, 1.0 },
510                         { 1.0, 1.0, 1.0, 1.0 },
511         };
512
513         nelems = (count - 1 + count - 2) * 2;
514
515         buffer = malloc(sizeof(GLushort) * nelems);
516         index = buffer;
517
518         for (i = 1; i < count; i++) {
519                 *index++ = first;
520                 *index++ = first + i;
521         }
522
523         for (i = 2; i < count; i++) {
524                 *index++ = first + i - 1;
525                 *index++ = first + i;
526         }
527
528         glUseProgram(compositor->solid_shader.program);
529         glUniform4fv(compositor->solid_shader.color_uniform, 1,
530                         color[color_idx++ % ARRAY_LENGTH(color)]);
531         glDrawElements(GL_LINES, nelems, GL_UNSIGNED_SHORT, buffer);
532         glUseProgram(compositor->current_shader->program);
533         free(buffer);
534 }
535
536 static void
537 repaint_region(struct weston_surface *es, pixman_region32_t *region,
538                 pixman_region32_t *surf_region)
539 {
540         struct weston_compositor *ec = es->compositor;
541         GLfloat *v;
542         unsigned int *vtxcnt;
543         int i, first, nfans;
544
545         /* The final region to be painted is the intersection of
546          * 'region' and 'surf_region'. However, 'region' is in the global
547          * coordinates, and 'surf_region' is in the surface-local
548          * coordinates. texture_region() will iterate over all pairs of
549          * rectangles from both regions, compute the intersection
550          * polygon for each pair, and store it as a triangle fan if
551          * it has a non-zero area (at least 3 vertices, actually).
552          */
553         nfans = texture_region(es, region, surf_region);
554
555         v = ec->vertices.data;
556         vtxcnt = ec->vtxcnt.data;
557
558         /* position: */
559         glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
560         glEnableVertexAttribArray(0);
561
562         /* texcoord: */
563         glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
564         glEnableVertexAttribArray(1);
565
566         for (i = 0, first = 0; i < nfans; i++) {
567                 glDrawArrays(GL_TRIANGLE_FAN, first, vtxcnt[i]);
568                 if (ec->fan_debug)
569                         triangle_fan_debug(es, first, vtxcnt[i]);
570                 first += vtxcnt[i];
571         }
572
573         glDisableVertexAttribArray(1);
574         glDisableVertexAttribArray(0);
575
576         ec->vertices.size = 0;
577         ec->vtxcnt.size = 0;
578 }
579
580 static void
581 weston_compositor_use_shader(struct weston_compositor *compositor,
582                              struct weston_shader *shader)
583 {
584         if (compositor->current_shader == shader)
585                 return;
586
587         glUseProgram(shader->program);
588         compositor->current_shader = shader;
589 }
590
591 static void
592 weston_shader_uniforms(struct weston_shader *shader,
593                        struct weston_surface *surface,
594                        struct weston_output *output)
595 {
596         int i;
597
598         glUniformMatrix4fv(shader->proj_uniform,
599                            1, GL_FALSE, output->matrix.d);
600         glUniform4fv(shader->color_uniform, 1, surface->color);
601         glUniform1f(shader->alpha_uniform, surface->alpha);
602
603         for (i = 0; i < surface->num_textures; i++)
604                 glUniform1i(shader->tex_uniforms[i], i);
605 }
606
607 static void
608 draw_surface(struct weston_surface *es, struct weston_output *output,
609              pixman_region32_t *damage) /* in global coordinates */
610 {
611         struct weston_compositor *ec = es->compositor;
612         /* repaint bounding region in global coordinates: */
613         pixman_region32_t repaint;
614         /* non-opaque region in surface coordinates: */
615         pixman_region32_t surface_blend;
616         pixman_region32_t *buffer_damage;
617         GLint filter;
618         int i;
619
620         pixman_region32_init(&repaint);
621         pixman_region32_intersect(&repaint,
622                                   &es->transform.boundingbox, damage);
623         pixman_region32_subtract(&repaint, &repaint, &es->clip);
624
625         if (!pixman_region32_not_empty(&repaint))
626                 goto out;
627
628         buffer_damage = &output->buffer_damage[output->current_buffer];
629         pixman_region32_subtract(buffer_damage, buffer_damage, &repaint);
630
631         glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
632
633         if (ec->fan_debug) {
634                 weston_compositor_use_shader(ec, &ec->solid_shader);
635                 weston_shader_uniforms(&ec->solid_shader, es, output);
636         }
637
638         weston_compositor_use_shader(ec, es->shader);
639         weston_shader_uniforms(es->shader, es, output);
640
641         if (es->transform.enabled || output->zoom.active)
642                 filter = GL_LINEAR;
643         else
644                 filter = GL_NEAREST;
645
646         for (i = 0; i < es->num_textures; i++) {
647                 glActiveTexture(GL_TEXTURE0 + i);
648                 glBindTexture(es->target, es->textures[i]);
649                 glTexParameteri(es->target, GL_TEXTURE_MIN_FILTER, filter);
650                 glTexParameteri(es->target, GL_TEXTURE_MAG_FILTER, filter);
651         }
652
653         /* blended region is whole surface minus opaque region: */
654         pixman_region32_init_rect(&surface_blend, 0, 0,
655                                   es->geometry.width, es->geometry.height);
656         pixman_region32_subtract(&surface_blend, &surface_blend, &es->opaque);
657
658         if (pixman_region32_not_empty(&es->opaque)) {
659                 if (es->shader == &ec->texture_shader_rgba) {
660                         /* Special case for RGBA textures with possibly
661                          * bad data in alpha channel: use the shader
662                          * that forces texture alpha = 1.0.
663                          * Xwayland surfaces need this.
664                          */
665                         weston_compositor_use_shader(ec, &ec->texture_shader_rgbx);
666                         weston_shader_uniforms(&ec->texture_shader_rgbx, es, output);
667                 }
668
669                 if (es->alpha < 1.0)
670                         glEnable(GL_BLEND);
671                 else
672                         glDisable(GL_BLEND);
673
674                 repaint_region(es, &repaint, &es->opaque);
675         }
676
677         if (pixman_region32_not_empty(&surface_blend)) {
678                 weston_compositor_use_shader(ec, es->shader);
679                 glEnable(GL_BLEND);
680                 repaint_region(es, &repaint, &surface_blend);
681         }
682
683         pixman_region32_fini(&surface_blend);
684
685 out:
686         pixman_region32_fini(&repaint);
687 }
688
689 static void
690 repaint_surfaces(struct weston_output *output, pixman_region32_t *damage)
691 {
692         struct weston_compositor *compositor = output->compositor;
693         struct weston_surface *surface;
694
695         wl_list_for_each_reverse(surface, &compositor->surface_list, link)
696                 if (surface->plane == &compositor->primary_plane)
697                         draw_surface(surface, output, damage);
698 }
699
700 static void
701 gles2_renderer_repaint_output(struct weston_output *output,
702                               pixman_region32_t *output_damage)
703 {
704         struct weston_compositor *compositor = output->compositor;
705         EGLBoolean ret;
706         static int errored;
707         int32_t width, height, i;
708
709         width = output->current->width +
710                 output->border.left + output->border.right;
711         height = output->current->height +
712                 output->border.top + output->border.bottom;
713
714         glViewport(0, 0, width, height);
715
716         ret = eglMakeCurrent(compositor->egl_display, output->egl_surface,
717                              output->egl_surface, compositor->egl_context);
718         if (ret == EGL_FALSE) {
719                 if (errored)
720                         return;
721                 errored = 1;
722                 weston_log("Failed to make EGL context current.\n");
723                 print_egl_error_state();
724                 return;
725         }
726
727         /* if debugging, redraw everything outside the damage to clean up
728          * debug lines from the previous draw on this buffer:
729          */
730         if (compositor->fan_debug) {
731                 pixman_region32_t undamaged;
732                 pixman_region32_init(&undamaged);
733                 pixman_region32_subtract(&undamaged, &output->region,
734                                          output_damage);
735                 compositor->fan_debug = 0;
736                 repaint_surfaces(output, &undamaged);
737                 compositor->fan_debug = 1;
738                 pixman_region32_fini(&undamaged);
739         }
740
741         for (i = 0; i < 2; i++)
742                 pixman_region32_union(&output->buffer_damage[i],
743                                       &output->buffer_damage[i],
744                                       output_damage);
745
746         pixman_region32_union(output_damage, output_damage,
747                               &output->buffer_damage[output->current_buffer]);
748
749         repaint_surfaces(output, output_damage);
750
751         wl_signal_emit(&output->frame_signal, output);
752
753         ret = eglSwapBuffers(compositor->egl_display, output->egl_surface);
754         if (ret == EGL_FALSE && !errored) {
755                 errored = 1;
756                 weston_log("Failed in eglSwapBuffers.\n");
757                 print_egl_error_state();
758         }
759
760         output->current_buffer ^= 1;
761
762 }
763
764 static void
765 gles2_renderer_flush_damage(struct weston_surface *surface)
766 {
767 #ifdef GL_UNPACK_ROW_LENGTH
768         pixman_box32_t *rectangles;
769         void *data;
770         int i, n;
771 #endif
772
773         glBindTexture(GL_TEXTURE_2D, surface->textures[0]);
774
775         if (!surface->compositor->has_unpack_subimage) {
776                 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
777                              surface->pitch, surface->buffer->height, 0,
778                              GL_BGRA_EXT, GL_UNSIGNED_BYTE,
779                              wl_shm_buffer_get_data(surface->buffer));
780
781                 return;
782         }
783
784 #ifdef GL_UNPACK_ROW_LENGTH
785         /* Mesa does not define GL_EXT_unpack_subimage */
786         glPixelStorei(GL_UNPACK_ROW_LENGTH, surface->pitch);
787         data = wl_shm_buffer_get_data(surface->buffer);
788         rectangles = pixman_region32_rectangles(&surface->damage, &n);
789         for (i = 0; i < n; i++) {
790                 glPixelStorei(GL_UNPACK_SKIP_PIXELS, rectangles[i].x1);
791                 glPixelStorei(GL_UNPACK_SKIP_ROWS, rectangles[i].y1);
792                 glTexSubImage2D(GL_TEXTURE_2D, 0,
793                                 rectangles[i].x1, rectangles[i].y1,
794                                 rectangles[i].x2 - rectangles[i].x1,
795                                 rectangles[i].y2 - rectangles[i].y1,
796                                 GL_BGRA_EXT, GL_UNSIGNED_BYTE, data);
797         }
798 #endif
799 }
800
801 static void
802 ensure_textures(struct weston_surface *es, int num_textures)
803 {
804         int i;
805
806         if (num_textures <= es->num_textures)
807                 return;
808
809         for (i = es->num_textures; i < num_textures; i++) {
810                 glGenTextures(1, &es->textures[i]);
811                 glBindTexture(es->target, es->textures[i]);
812                 glTexParameteri(es->target,
813                                 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
814                 glTexParameteri(es->target,
815                                 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
816         }
817         es->num_textures = num_textures;
818         glBindTexture(es->target, 0);
819 }
820
821 static void
822 gles2_renderer_attach(struct weston_surface *es, struct wl_buffer *buffer)
823 {
824         struct weston_compositor *ec = es->compositor;
825         EGLint attribs[3], format;
826         int i, num_planes;
827
828         if (!buffer) {
829                 for (i = 0; i < es->num_images; i++) {
830                         ec->destroy_image(ec->egl_display, es->images[i]);
831                         es->images[i] = NULL;
832                 }
833                 es->num_images = 0;
834                 glDeleteTextures(es->num_textures, es->textures);
835                 es->num_textures = 0;
836                 return;
837         }
838
839         if (wl_buffer_is_shm(buffer)) {
840                 es->pitch = wl_shm_buffer_get_stride(buffer) / 4;
841                 es->target = GL_TEXTURE_2D;
842
843                 ensure_textures(es, 1);
844                 glBindTexture(GL_TEXTURE_2D, es->textures[0]);
845                 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
846                              es->pitch, buffer->height, 0,
847                              GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
848                 if (wl_shm_buffer_get_format(buffer) == WL_SHM_FORMAT_XRGB8888)
849                         es->shader = &ec->texture_shader_rgbx;
850                 else
851                         es->shader = &ec->texture_shader_rgba;
852         } else if (ec->query_buffer(ec->egl_display, buffer,
853                                     EGL_TEXTURE_FORMAT, &format)) {
854                 for (i = 0; i < es->num_images; i++)
855                         ec->destroy_image(ec->egl_display, es->images[i]);
856                 es->num_images = 0;
857                 es->target = GL_TEXTURE_2D;
858                 switch (format) {
859                 case EGL_TEXTURE_RGB:
860                 case EGL_TEXTURE_RGBA:
861                 default:
862                         num_planes = 1;
863                         es->shader = &ec->texture_shader_rgba;
864                         break;
865                 case EGL_TEXTURE_EXTERNAL_WL:
866                         num_planes = 1;
867                         es->target = GL_TEXTURE_EXTERNAL_OES;
868                         es->shader = &ec->texture_shader_egl_external;
869                         break;
870                 case EGL_TEXTURE_Y_UV_WL:
871                         num_planes = 2;
872                         es->shader = &ec->texture_shader_y_uv;
873                         break;
874                 case EGL_TEXTURE_Y_U_V_WL:
875                         num_planes = 3;
876                         es->shader = &ec->texture_shader_y_u_v;
877                         break;
878                 case EGL_TEXTURE_Y_XUXV_WL:
879                         num_planes = 2;
880                         es->shader = &ec->texture_shader_y_xuxv;
881                         break;
882                 }
883
884                 ensure_textures(es, num_planes);
885                 for (i = 0; i < num_planes; i++) {
886                         attribs[0] = EGL_WAYLAND_PLANE_WL;
887                         attribs[1] = i;
888                         attribs[2] = EGL_NONE;
889                         es->images[i] = ec->create_image(ec->egl_display,
890                                                          NULL,
891                                                          EGL_WAYLAND_BUFFER_WL,
892                                                          buffer, attribs);
893                         if (!es->images[i]) {
894                                 weston_log("failed to create img for plane %d\n", i);
895                                 continue;
896                         }
897                         es->num_images++;
898
899                         glActiveTexture(GL_TEXTURE0 + i);
900                         glBindTexture(es->target, es->textures[i]);
901                         ec->image_target_texture_2d(es->target,
902                                                     es->images[i]);
903                 }
904
905                 es->pitch = buffer->width;
906         } else {
907                 weston_log("unhandled buffer type!\n");
908         }
909 }
910
911 static void
912 gles2_renderer_destroy_surface(struct weston_surface *surface)
913 {
914         struct weston_compositor *ec = surface->compositor;
915         int i;
916
917         glDeleteTextures(surface->num_textures, surface->textures);
918
919         for (i = 0; i < surface->num_images; i++)
920                 ec->destroy_image(ec->egl_display, surface->images[i]);
921 }
922
923 static const char vertex_shader[] =
924         "uniform mat4 proj;\n"
925         "attribute vec2 position;\n"
926         "attribute vec2 texcoord;\n"
927         "varying vec2 v_texcoord;\n"
928         "void main()\n"
929         "{\n"
930         "   gl_Position = proj * vec4(position, 0.0, 1.0);\n"
931         "   v_texcoord = texcoord;\n"
932         "}\n";
933
934 /* Declare common fragment shader uniforms */
935 #define FRAGMENT_CONVERT_YUV                                            \
936         "  y *= alpha;\n"                                               \
937         "  u *= alpha;\n"                                               \
938         "  v *= alpha;\n"                                               \
939         "  gl_FragColor.r = y + 1.59602678 * v;\n"                      \
940         "  gl_FragColor.g = y - 0.39176229 * u - 0.81296764 * v;\n"     \
941         "  gl_FragColor.b = y + 2.01723214 * u;\n"                      \
942         "  gl_FragColor.a = alpha;\n"
943
944 static const char texture_fragment_shader_rgba[] =
945         "precision mediump float;\n"
946         "varying vec2 v_texcoord;\n"
947         "uniform sampler2D tex;\n"
948         "uniform float alpha;\n"
949         "void main()\n"
950         "{\n"
951         "   gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
952         "}\n";
953
954 static const char texture_fragment_shader_rgbx[] =
955         "precision mediump float;\n"
956         "varying vec2 v_texcoord;\n"
957         "uniform sampler2D tex;\n"
958         "uniform float alpha;\n"
959         "void main()\n"
960         "{\n"
961         "   gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb\n;"
962         "   gl_FragColor.a = alpha;\n"
963         "}\n";
964
965 static const char texture_fragment_shader_egl_external[] =
966         "#extension GL_OES_EGL_image_external : require\n"
967         "precision mediump float;\n"
968         "varying vec2 v_texcoord;\n"
969         "uniform samplerExternalOES tex;\n"
970         "uniform float alpha;\n"
971         "void main()\n"
972         "{\n"
973         "   gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
974         "}\n";
975
976 static const char texture_fragment_shader_y_uv[] =
977         "precision mediump float;\n"
978         "uniform sampler2D tex;\n"
979         "uniform sampler2D tex1;\n"
980         "varying vec2 v_texcoord;\n"
981         "uniform float alpha;\n"
982         "void main() {\n"
983         "  float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
984         "  float u = texture2D(tex1, v_texcoord).r - 0.5;\n"
985         "  float v = texture2D(tex1, v_texcoord).g - 0.5;\n"
986         FRAGMENT_CONVERT_YUV
987         "}\n";
988
989 static const char texture_fragment_shader_y_u_v[] =
990         "precision mediump float;\n"
991         "uniform sampler2D tex;\n"
992         "uniform sampler2D tex1;\n"
993         "uniform sampler2D tex2;\n"
994         "varying vec2 v_texcoord;\n"
995         "uniform float alpha;\n"
996         "void main() {\n"
997         "  float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
998         "  float u = texture2D(tex1, v_texcoord).x - 0.5;\n"
999         "  float v = texture2D(tex2, v_texcoord).x - 0.5;\n"
1000         FRAGMENT_CONVERT_YUV
1001         "}\n";
1002
1003 static const char texture_fragment_shader_y_xuxv[] =
1004         "precision mediump float;\n"
1005         "uniform sampler2D tex;\n"
1006         "uniform sampler2D tex1;\n"
1007         "varying vec2 v_texcoord;\n"
1008         "uniform float alpha;\n"
1009         "void main() {\n"
1010         "  float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1011         "  float u = texture2D(tex1, v_texcoord).g - 0.5;\n"
1012         "  float v = texture2D(tex1, v_texcoord).a - 0.5;\n"
1013         FRAGMENT_CONVERT_YUV
1014         "}\n";
1015
1016 static const char solid_fragment_shader[] =
1017         "precision mediump float;\n"
1018         "uniform vec4 color;\n"
1019         "uniform float alpha;\n"
1020         "void main()\n"
1021         "{\n"
1022         "   gl_FragColor = alpha * color\n;"
1023         "}\n";
1024
1025 static int
1026 compile_shader(GLenum type, const char *source)
1027 {
1028         GLuint s;
1029         char msg[512];
1030         GLint status;
1031
1032         s = glCreateShader(type);
1033         glShaderSource(s, 1, &source, NULL);
1034         glCompileShader(s);
1035         glGetShaderiv(s, GL_COMPILE_STATUS, &status);
1036         if (!status) {
1037                 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1038                 weston_log("shader info: %s\n", msg);
1039                 return GL_NONE;
1040         }
1041
1042         return s;
1043 }
1044
1045 static int
1046 weston_shader_init(struct weston_shader *shader,
1047                    const char *vertex_source, const char *fragment_source)
1048 {
1049         char msg[512];
1050         GLint status;
1051
1052         shader->vertex_shader =
1053                 compile_shader(GL_VERTEX_SHADER, vertex_source);
1054         shader->fragment_shader =
1055                 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
1056
1057         shader->program = glCreateProgram();
1058         glAttachShader(shader->program, shader->vertex_shader);
1059         glAttachShader(shader->program, shader->fragment_shader);
1060         glBindAttribLocation(shader->program, 0, "position");
1061         glBindAttribLocation(shader->program, 1, "texcoord");
1062
1063         glLinkProgram(shader->program);
1064         glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1065         if (!status) {
1066                 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1067                 weston_log("link info: %s\n", msg);
1068                 return -1;
1069         }
1070
1071         shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1072         shader->tex_uniforms[0] = glGetUniformLocation(shader->program, "tex");
1073         shader->tex_uniforms[1] = glGetUniformLocation(shader->program, "tex1");
1074         shader->tex_uniforms[2] = glGetUniformLocation(shader->program, "tex2");
1075         shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
1076         shader->color_uniform = glGetUniformLocation(shader->program, "color");
1077
1078         return 0;
1079 }
1080
1081 static void
1082 log_extensions(const char *name, const char *extensions)
1083 {
1084         const char *p, *end;
1085         int l;
1086         int len;
1087
1088         l = weston_log("%s:", name);
1089         p = extensions;
1090         while (*p) {
1091                 end = strchrnul(p, ' ');
1092                 len = end - p;
1093                 if (l + len > 78)
1094                         l = weston_log_continue("\n" STAMP_SPACE "%.*s",
1095                                                 len, p);
1096                 else
1097                         l += weston_log_continue(" %.*s", len, p);
1098                 for (p = end; isspace(*p); p++)
1099                         ;
1100         }
1101         weston_log_continue("\n");
1102 }
1103
1104 static void
1105 log_egl_gl_info(EGLDisplay egldpy)
1106 {
1107         const char *str;
1108
1109         str = eglQueryString(egldpy, EGL_VERSION);
1110         weston_log("EGL version: %s\n", str ? str : "(null)");
1111
1112         str = eglQueryString(egldpy, EGL_VENDOR);
1113         weston_log("EGL vendor: %s\n", str ? str : "(null)");
1114
1115         str = eglQueryString(egldpy, EGL_CLIENT_APIS);
1116         weston_log("EGL client APIs: %s\n", str ? str : "(null)");
1117
1118         str = eglQueryString(egldpy, EGL_EXTENSIONS);
1119         log_extensions("EGL extensions", str ? str : "(null)");
1120
1121         str = (char *)glGetString(GL_VERSION);
1122         weston_log("GL version: %s\n", str ? str : "(null)");
1123
1124         str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
1125         weston_log("GLSL version: %s\n", str ? str : "(null)");
1126
1127         str = (char *)glGetString(GL_VENDOR);
1128         weston_log("GL vendor: %s\n", str ? str : "(null)");
1129
1130         str = (char *)glGetString(GL_RENDERER);
1131         weston_log("GL renderer: %s\n", str ? str : "(null)");
1132
1133         str = (char *)glGetString(GL_EXTENSIONS);
1134         log_extensions("GL extensions", str ? str : "(null)");
1135 }
1136
1137 static void
1138 log_egl_config_info(EGLDisplay egldpy, EGLConfig eglconfig)
1139 {
1140         EGLint r, g, b, a;
1141
1142         weston_log("Chosen EGL config details:\n");
1143
1144         weston_log_continue(STAMP_SPACE "RGBA bits");
1145         if (eglGetConfigAttrib(egldpy, eglconfig, EGL_RED_SIZE, &r) &&
1146             eglGetConfigAttrib(egldpy, eglconfig, EGL_GREEN_SIZE, &g) &&
1147             eglGetConfigAttrib(egldpy, eglconfig, EGL_BLUE_SIZE, &b) &&
1148             eglGetConfigAttrib(egldpy, eglconfig, EGL_ALPHA_SIZE, &a))
1149                 weston_log_continue(": %d %d %d %d\n", r, g, b, a);
1150         else
1151                 weston_log_continue(" unknown\n");
1152
1153         weston_log_continue(STAMP_SPACE "swap interval range");
1154         if (eglGetConfigAttrib(egldpy, eglconfig, EGL_MIN_SWAP_INTERVAL, &a) &&
1155             eglGetConfigAttrib(egldpy, eglconfig, EGL_MAX_SWAP_INTERVAL, &b))
1156                 weston_log_continue(": %d - %d\n", a, b);
1157         else
1158                 weston_log_continue(" unknown\n");
1159 }
1160
1161 struct gles2_renderer {
1162         struct weston_renderer base;
1163 };
1164
1165 WL_EXPORT void
1166 gles2_renderer_destroy(struct weston_compositor *ec)
1167 {
1168         if (ec->has_bind_display)
1169                 ec->unbind_display(ec->egl_display, ec->wl_display);
1170 }
1171
1172 WL_EXPORT int
1173 gles2_renderer_init(struct weston_compositor *ec)
1174 {
1175         struct gles2_renderer *renderer;
1176         const char *extensions;
1177         int has_egl_image_external = 0;
1178         struct weston_output *output;
1179         EGLBoolean ret;
1180
1181         static const EGLint context_attribs[] = {
1182                 EGL_CONTEXT_CLIENT_VERSION, 2,
1183                 EGL_NONE
1184         };
1185
1186         renderer = malloc(sizeof *renderer);
1187         if (renderer == NULL)
1188                 return -1;
1189
1190         if (!eglBindAPI(EGL_OPENGL_ES_API)) {
1191                 weston_log("failed to bind EGL_OPENGL_ES_API\n");
1192                 print_egl_error_state();
1193                 return -1;
1194         }
1195
1196         log_egl_config_info(ec->egl_display, ec->egl_config);
1197
1198         ec->egl_context = eglCreateContext(ec->egl_display, ec->egl_config,
1199                                            EGL_NO_CONTEXT, context_attribs);
1200         if (ec->egl_context == NULL) {
1201                 weston_log("failed to create context\n");
1202                 print_egl_error_state();
1203                 return -1;
1204         }
1205
1206         output = container_of(ec->output_list.next,
1207                               struct weston_output, link);
1208         ret = eglMakeCurrent(ec->egl_display, output->egl_surface,
1209                              output->egl_surface, ec->egl_context);
1210         if (ret == EGL_FALSE) {
1211                 weston_log("Failed to make EGL context current.\n");
1212                 print_egl_error_state();
1213                 return -1;
1214         }
1215
1216         log_egl_gl_info(ec->egl_display);
1217
1218         ec->image_target_texture_2d =
1219                 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
1220         ec->image_target_renderbuffer_storage = (void *)
1221                 eglGetProcAddress("glEGLImageTargetRenderbufferStorageOES");
1222         ec->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1223         ec->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1224         ec->bind_display =
1225                 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
1226         ec->unbind_display =
1227                 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
1228         ec->query_buffer =
1229                 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
1230
1231         extensions = (const char *) glGetString(GL_EXTENSIONS);
1232         if (!extensions) {
1233                 weston_log("Retrieving GL extension string failed.\n");
1234                 return -1;
1235         }
1236
1237         if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
1238                 weston_log("GL_EXT_texture_format_BGRA8888 not available\n");
1239                 return -1;
1240         }
1241
1242         if (strstr(extensions, "GL_EXT_read_format_bgra"))
1243                 ec->read_format = GL_BGRA_EXT;
1244         else
1245                 ec->read_format = GL_RGBA;
1246
1247         if (strstr(extensions, "GL_EXT_unpack_subimage"))
1248                 ec->has_unpack_subimage = 1;
1249
1250         if (strstr(extensions, "GL_OES_EGL_image_external"))
1251                 has_egl_image_external = 1;
1252
1253         extensions =
1254                 (const char *) eglQueryString(ec->egl_display, EGL_EXTENSIONS);
1255         if (!extensions) {
1256                 weston_log("Retrieving EGL extension string failed.\n");
1257                 return -1;
1258         }
1259
1260         if (strstr(extensions, "EGL_WL_bind_wayland_display"))
1261                 ec->has_bind_display = 1;
1262         if (ec->has_bind_display) {
1263                 ret = ec->bind_display(ec->egl_display, ec->wl_display);
1264                 if (!ret)
1265                         ec->has_bind_display = 0;
1266         }
1267
1268         glActiveTexture(GL_TEXTURE0);
1269
1270         if (weston_shader_init(&ec->texture_shader_rgba,
1271                              vertex_shader, texture_fragment_shader_rgba) < 0)
1272                 return -1;
1273         if (weston_shader_init(&ec->texture_shader_rgbx,
1274                              vertex_shader, texture_fragment_shader_rgbx) < 0)
1275                 return -1;
1276         if (has_egl_image_external &&
1277                         weston_shader_init(&ec->texture_shader_egl_external,
1278                                 vertex_shader, texture_fragment_shader_egl_external) < 0)
1279                 return -1;
1280         if (weston_shader_init(&ec->texture_shader_y_uv,
1281                                vertex_shader, texture_fragment_shader_y_uv) < 0)
1282                 return -1;
1283         if (weston_shader_init(&ec->texture_shader_y_u_v,
1284                                vertex_shader, texture_fragment_shader_y_u_v) < 0)
1285                 return -1;
1286         if (weston_shader_init(&ec->texture_shader_y_xuxv,
1287                                vertex_shader, texture_fragment_shader_y_xuxv) < 0)
1288                 return -1;
1289         if (weston_shader_init(&ec->solid_shader,
1290                              vertex_shader, solid_fragment_shader) < 0)
1291                 return -1;
1292
1293         renderer->base.repaint_output = gles2_renderer_repaint_output;
1294         renderer->base.flush_damage = gles2_renderer_flush_damage;
1295         renderer->base.attach = gles2_renderer_attach;
1296         renderer->base.destroy_surface = gles2_renderer_destroy_surface;
1297         ec->renderer = &renderer->base;
1298
1299         weston_log("GL ES 2 renderer features:\n");
1300         weston_log_continue(STAMP_SPACE "read-back format: %s\n",
1301                             ec->read_format == GL_BGRA_EXT ? "BGRA" : "RGBA");
1302         weston_log_continue(STAMP_SPACE "wl_shm sub-image to texture: %s\n",
1303                             ec->has_unpack_subimage ? "yes" : "no");
1304         weston_log_continue(STAMP_SPACE "EGL Wayland extension: %s\n",
1305                             ec->has_bind_display ? "yes" : "no");
1306
1307         return 0;
1308 }