2 * Copyright © 2012 Intel Corporation
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.
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.
25 #include <GLES2/gl2.h>
26 #include <GLES2/gl2ext.h>
33 #include <linux/input.h>
35 #include "gl-renderer.h"
37 #include <EGL/eglext.h>
38 #include "weston-egl-ext.h"
42 GLuint vertex_shader, fragment_shader;
44 GLint tex_uniforms[3];
49 #define BUFFER_DAMAGE_COUNT 2
51 struct gl_output_state {
52 EGLSurface egl_surface;
53 pixman_region32_t buffer_damage[BUFFER_DAMAGE_COUNT];
56 struct gl_surface_state {
58 struct gl_shader *shader;
62 pixman_region32_t texture_damage;
64 EGLImageKHR images[3];
68 struct weston_buffer_reference buffer_ref;
69 int pitch; /* in pixels */
73 struct weston_renderer base;
74 int fragment_shader_debug;
76 EGLDisplay egl_display;
77 EGLContext egl_context;
81 int32_t top, bottom, left, right;
83 int32_t width, height;
86 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
87 PFNEGLCREATEIMAGEKHRPROC create_image;
88 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
90 int has_unpack_subimage;
92 PFNEGLBINDWAYLANDDISPLAYWL bind_display;
93 PFNEGLUNBINDWAYLANDDISPLAYWL unbind_display;
94 PFNEGLQUERYWAYLANDBUFFERWL query_buffer;
97 int has_egl_image_external;
99 int has_egl_buffer_age;
101 struct gl_shader texture_shader_rgba;
102 struct gl_shader texture_shader_rgbx;
103 struct gl_shader texture_shader_egl_external;
104 struct gl_shader texture_shader_y_uv;
105 struct gl_shader texture_shader_y_u_v;
106 struct gl_shader texture_shader_y_xuxv;
107 struct gl_shader invert_color_shader;
108 struct gl_shader solid_shader;
109 struct gl_shader *current_shader;
112 static inline struct gl_output_state *
113 get_output_state(struct weston_output *output)
115 return (struct gl_output_state *)output->renderer_state;
118 static inline struct gl_surface_state *
119 get_surface_state(struct weston_surface *surface)
121 return (struct gl_surface_state *)surface->renderer_state;
124 static inline struct gl_renderer *
125 get_renderer(struct weston_compositor *ec)
127 return (struct gl_renderer *)ec->renderer;
131 egl_error_string(EGLint code)
133 #define MYERRCODE(x) case x: return #x;
135 MYERRCODE(EGL_SUCCESS)
136 MYERRCODE(EGL_NOT_INITIALIZED)
137 MYERRCODE(EGL_BAD_ACCESS)
138 MYERRCODE(EGL_BAD_ALLOC)
139 MYERRCODE(EGL_BAD_ATTRIBUTE)
140 MYERRCODE(EGL_BAD_CONTEXT)
141 MYERRCODE(EGL_BAD_CONFIG)
142 MYERRCODE(EGL_BAD_CURRENT_SURFACE)
143 MYERRCODE(EGL_BAD_DISPLAY)
144 MYERRCODE(EGL_BAD_SURFACE)
145 MYERRCODE(EGL_BAD_MATCH)
146 MYERRCODE(EGL_BAD_PARAMETER)
147 MYERRCODE(EGL_BAD_NATIVE_PIXMAP)
148 MYERRCODE(EGL_BAD_NATIVE_WINDOW)
149 MYERRCODE(EGL_CONTEXT_LOST)
157 gl_renderer_print_egl_error_state(void)
161 code = eglGetError();
162 weston_log("EGL error state: %s (0x%04lx)\n",
163 egl_error_string(code), (long)code);
172 struct clip_context {
190 float_difference(GLfloat a, GLfloat b)
192 /* http://www.altdevblogaday.com/2012/02/22/comparing-floating-point-numbers-2012-edition/ */
193 static const GLfloat max_diff = 4.0f * FLT_MIN;
194 static const GLfloat max_rel_diff = 4.0e-5;
195 GLfloat diff = a - b;
196 GLfloat adiff = fabsf(diff);
198 if (adiff <= max_diff)
203 if (adiff <= (a > b ? a : b) * max_rel_diff)
209 /* A line segment (p1x, p1y)-(p2x, p2y) intersects the line x = x_arg.
210 * Compute the y coordinate of the intersection.
213 clip_intersect_y(GLfloat p1x, GLfloat p1y, GLfloat p2x, GLfloat p2y,
217 GLfloat diff = float_difference(p1x, p2x);
219 /* Practically vertical line segment, yet the end points have already
220 * been determined to be on different sides of the line. Therefore
221 * the line segment is part of the line and intersects everywhere.
222 * Return the end point, so we use the whole line segment.
227 a = (x_arg - p2x) / diff;
228 return p2y + (p1y - p2y) * a;
231 /* A line segment (p1x, p1y)-(p2x, p2y) intersects the line y = y_arg.
232 * Compute the x coordinate of the intersection.
235 clip_intersect_x(GLfloat p1x, GLfloat p1y, GLfloat p2x, GLfloat p2y,
239 GLfloat diff = float_difference(p1y, p2y);
241 /* Practically horizontal line segment, yet the end points have already
242 * been determined to be on different sides of the line. Therefore
243 * the line segment is part of the line and intersects everywhere.
244 * Return the end point, so we use the whole line segment.
249 a = (y_arg - p2y) / diff;
250 return p2x + (p1x - p2x) * a;
253 enum path_transition {
254 PATH_TRANSITION_OUT_TO_OUT = 0,
255 PATH_TRANSITION_OUT_TO_IN = 1,
256 PATH_TRANSITION_IN_TO_OUT = 2,
257 PATH_TRANSITION_IN_TO_IN = 3,
261 clip_append_vertex(struct clip_context *ctx, GLfloat x, GLfloat y)
263 *ctx->vertices.x++ = x;
264 *ctx->vertices.y++ = y;
267 static enum path_transition
268 path_transition_left_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
270 return ((ctx->prev.x >= ctx->clip.x1) << 1) | (x >= ctx->clip.x1);
273 static enum path_transition
274 path_transition_right_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
276 return ((ctx->prev.x < ctx->clip.x2) << 1) | (x < ctx->clip.x2);
279 static enum path_transition
280 path_transition_top_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
282 return ((ctx->prev.y >= ctx->clip.y1) << 1) | (y >= ctx->clip.y1);
285 static enum path_transition
286 path_transition_bottom_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
288 return ((ctx->prev.y < ctx->clip.y2) << 1) | (y < ctx->clip.y2);
292 clip_polygon_leftright(struct clip_context *ctx,
293 enum path_transition transition,
294 GLfloat x, GLfloat y, GLfloat clip_x)
298 switch (transition) {
299 case PATH_TRANSITION_IN_TO_IN:
300 clip_append_vertex(ctx, x, y);
302 case PATH_TRANSITION_IN_TO_OUT:
303 yi = clip_intersect_y(ctx->prev.x, ctx->prev.y, x, y, clip_x);
304 clip_append_vertex(ctx, clip_x, yi);
306 case PATH_TRANSITION_OUT_TO_IN:
307 yi = clip_intersect_y(ctx->prev.x, ctx->prev.y, x, y, clip_x);
308 clip_append_vertex(ctx, clip_x, yi);
309 clip_append_vertex(ctx, x, y);
311 case PATH_TRANSITION_OUT_TO_OUT:
315 assert(0 && "bad enum path_transition");
323 clip_polygon_topbottom(struct clip_context *ctx,
324 enum path_transition transition,
325 GLfloat x, GLfloat y, GLfloat clip_y)
329 switch (transition) {
330 case PATH_TRANSITION_IN_TO_IN:
331 clip_append_vertex(ctx, x, y);
333 case PATH_TRANSITION_IN_TO_OUT:
334 xi = clip_intersect_x(ctx->prev.x, ctx->prev.y, x, y, clip_y);
335 clip_append_vertex(ctx, xi, clip_y);
337 case PATH_TRANSITION_OUT_TO_IN:
338 xi = clip_intersect_x(ctx->prev.x, ctx->prev.y, x, y, clip_y);
339 clip_append_vertex(ctx, xi, clip_y);
340 clip_append_vertex(ctx, x, y);
342 case PATH_TRANSITION_OUT_TO_OUT:
346 assert(0 && "bad enum path_transition");
354 clip_context_prepare(struct clip_context *ctx, const struct polygon8 *src,
355 GLfloat *dst_x, GLfloat *dst_y)
357 ctx->prev.x = src->x[src->n - 1];
358 ctx->prev.y = src->y[src->n - 1];
359 ctx->vertices.x = dst_x;
360 ctx->vertices.y = dst_y;
364 clip_polygon_left(struct clip_context *ctx, const struct polygon8 *src,
365 GLfloat *dst_x, GLfloat *dst_y)
367 enum path_transition trans;
370 clip_context_prepare(ctx, src, dst_x, dst_y);
371 for (i = 0; i < src->n; i++) {
372 trans = path_transition_left_edge(ctx, src->x[i], src->y[i]);
373 clip_polygon_leftright(ctx, trans, src->x[i], src->y[i],
376 return ctx->vertices.x - dst_x;
380 clip_polygon_right(struct clip_context *ctx, const struct polygon8 *src,
381 GLfloat *dst_x, GLfloat *dst_y)
383 enum path_transition trans;
386 clip_context_prepare(ctx, src, dst_x, dst_y);
387 for (i = 0; i < src->n; i++) {
388 trans = path_transition_right_edge(ctx, src->x[i], src->y[i]);
389 clip_polygon_leftright(ctx, trans, src->x[i], src->y[i],
392 return ctx->vertices.x - dst_x;
396 clip_polygon_top(struct clip_context *ctx, const struct polygon8 *src,
397 GLfloat *dst_x, GLfloat *dst_y)
399 enum path_transition trans;
402 clip_context_prepare(ctx, src, dst_x, dst_y);
403 for (i = 0; i < src->n; i++) {
404 trans = path_transition_top_edge(ctx, src->x[i], src->y[i]);
405 clip_polygon_topbottom(ctx, trans, src->x[i], src->y[i],
408 return ctx->vertices.x - dst_x;
412 clip_polygon_bottom(struct clip_context *ctx, const struct polygon8 *src,
413 GLfloat *dst_x, GLfloat *dst_y)
415 enum path_transition trans;
418 clip_context_prepare(ctx, src, dst_x, dst_y);
419 for (i = 0; i < src->n; i++) {
420 trans = path_transition_bottom_edge(ctx, src->x[i], src->y[i]);
421 clip_polygon_topbottom(ctx, trans, src->x[i], src->y[i],
424 return ctx->vertices.x - dst_x;
427 #define max(a, b) (((a) > (b)) ? (a) : (b))
428 #define min(a, b) (((a) > (b)) ? (b) : (a))
429 #define clip(x, a, b) min(max(x, a), b)
432 * Compute the boundary vertices of the intersection of the global coordinate
433 * aligned rectangle 'rect', and an arbitrary quadrilateral produced from
434 * 'surf_rect' when transformed from surface coordinates into global coordinates.
435 * The vertices are written to 'ex' and 'ey', and the return value is the
436 * number of vertices. Vertices are produced in clockwise winding order.
437 * Guarantees to produce either zero vertices, or 3-8 vertices with non-zero
441 calculate_edges(struct weston_surface *es, pixman_box32_t *rect,
442 pixman_box32_t *surf_rect, GLfloat *ex, GLfloat *ey)
444 struct polygon8 polygon;
445 struct clip_context ctx;
447 GLfloat min_x, max_x, min_y, max_y;
448 struct polygon8 surf = {
449 { surf_rect->x1, surf_rect->x2, surf_rect->x2, surf_rect->x1 },
450 { surf_rect->y1, surf_rect->y1, surf_rect->y2, surf_rect->y2 },
454 ctx.clip.x1 = rect->x1;
455 ctx.clip.y1 = rect->y1;
456 ctx.clip.x2 = rect->x2;
457 ctx.clip.y2 = rect->y2;
459 /* transform surface to screen space: */
460 for (i = 0; i < surf.n; i++)
461 weston_surface_to_global_float(es, surf.x[i], surf.y[i],
462 &surf.x[i], &surf.y[i]);
464 /* find bounding box: */
465 min_x = max_x = surf.x[0];
466 min_y = max_y = surf.y[0];
468 for (i = 1; i < surf.n; i++) {
469 min_x = min(min_x, surf.x[i]);
470 max_x = max(max_x, surf.x[i]);
471 min_y = min(min_y, surf.y[i]);
472 max_y = max(max_y, surf.y[i]);
475 /* First, simple bounding box check to discard early transformed
476 * surface rects that do not intersect with the clip region:
478 if ((min_x >= ctx.clip.x2) || (max_x <= ctx.clip.x1) ||
479 (min_y >= ctx.clip.y2) || (max_y <= ctx.clip.y1))
482 /* Simple case, bounding box edges are parallel to surface edges,
483 * there will be only four edges. We just need to clip the surface
484 * vertices to the clip rect bounds:
486 if (!es->transform.enabled) {
487 for (i = 0; i < surf.n; i++) {
488 ex[i] = clip(surf.x[i], ctx.clip.x1, ctx.clip.x2);
489 ey[i] = clip(surf.y[i], ctx.clip.y1, ctx.clip.y2);
494 /* Transformed case: use a general polygon clipping algorithm to
495 * clip the surface rectangle with each side of 'rect'.
496 * The algorithm is Sutherland-Hodgman, as explained in
497 * http://www.codeguru.com/cpp/misc/misc/graphics/article.php/c8965/Polygon-Clipping.htm
498 * but without looking at any of that code.
500 polygon.n = clip_polygon_left(&ctx, &surf, polygon.x, polygon.y);
501 surf.n = clip_polygon_right(&ctx, &polygon, surf.x, surf.y);
502 polygon.n = clip_polygon_top(&ctx, &surf, polygon.x, polygon.y);
503 surf.n = clip_polygon_bottom(&ctx, &polygon, surf.x, surf.y);
505 /* Get rid of duplicate vertices */
509 for (i = 1; i < surf.n; i++) {
510 if (float_difference(ex[n - 1], surf.x[i]) == 0.0f &&
511 float_difference(ey[n - 1], surf.y[i]) == 0.0f)
517 if (float_difference(ex[n - 1], surf.x[0]) == 0.0f &&
518 float_difference(ey[n - 1], surf.y[0]) == 0.0f)
528 texture_region(struct weston_surface *es, pixman_region32_t *region,
529 pixman_region32_t *surf_region)
531 struct gl_surface_state *gs = get_surface_state(es);
532 struct weston_compositor *ec = es->compositor;
533 GLfloat *v, inv_width, inv_height;
534 unsigned int *vtxcnt, nvtx = 0;
535 pixman_box32_t *rects, *surf_rects;
536 int i, j, k, nrects, nsurf;
538 rects = pixman_region32_rectangles(region, &nrects);
539 surf_rects = pixman_region32_rectangles(surf_region, &nsurf);
541 /* worst case we can have 8 vertices per rect (ie. clipped into
544 v = wl_array_add(&ec->vertices, nrects * nsurf * 8 * 4 * sizeof *v);
545 vtxcnt = wl_array_add(&ec->vtxcnt, nrects * nsurf * sizeof *vtxcnt);
547 inv_width = 1.0 / gs->pitch;
549 switch (es->buffer_transform) {
550 case WL_OUTPUT_TRANSFORM_90:
551 case WL_OUTPUT_TRANSFORM_270:
552 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
553 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
554 inv_height = 1.0 / es->geometry.width;
557 inv_height = 1.0 / es->geometry.height;
560 for (i = 0; i < nrects; i++) {
561 pixman_box32_t *rect = &rects[i];
562 for (j = 0; j < nsurf; j++) {
563 pixman_box32_t *surf_rect = &surf_rects[j];
564 GLfloat sx, sy, bx, by;
565 GLfloat ex[8], ey[8]; /* edge points in screen space */
568 /* The transformed surface, after clipping to the clip region,
569 * can have as many as eight sides, emitted as a triangle-fan.
570 * The first vertex in the triangle fan can be chosen arbitrarily,
571 * since the area is guaranteed to be convex.
573 * If a corner of the transformed surface falls outside of the
574 * clip region, instead of emitting one vertex for the corner
575 * of the surface, up to two are emitted for two corresponding
576 * intersection point(s) between the surface and the clip region.
578 * To do this, we first calculate the (up to eight) points that
579 * form the intersection of the clip rect and the transformed
582 n = calculate_edges(es, rect, surf_rect, ex, ey);
586 /* emit edge points: */
587 for (k = 0; k < n; k++) {
588 weston_surface_from_global_float(es, ex[k], ey[k], &sx, &sy);
593 weston_surface_to_buffer_float(es, sx, sy,
595 *(v++) = bx * inv_width;
596 *(v++) = by * inv_height;
607 triangle_fan_debug(struct weston_surface *surface, int first, int count)
609 struct weston_compositor *compositor = surface->compositor;
610 struct gl_renderer *gr = get_renderer(compositor);
615 static int color_idx = 0;
616 static const GLfloat color[][4] = {
617 { 1.0, 0.0, 0.0, 1.0 },
618 { 0.0, 1.0, 0.0, 1.0 },
619 { 0.0, 0.0, 1.0, 1.0 },
620 { 1.0, 1.0, 1.0, 1.0 },
623 nelems = (count - 1 + count - 2) * 2;
625 buffer = malloc(sizeof(GLushort) * nelems);
628 for (i = 1; i < count; i++) {
630 *index++ = first + i;
633 for (i = 2; i < count; i++) {
634 *index++ = first + i - 1;
635 *index++ = first + i;
638 glUseProgram(gr->solid_shader.program);
639 glUniform4fv(gr->solid_shader.color_uniform, 1,
640 color[color_idx++ % ARRAY_LENGTH(color)]);
641 glDrawElements(GL_LINES, nelems, GL_UNSIGNED_SHORT, buffer);
642 glUseProgram(gr->current_shader->program);
647 repaint_region(struct weston_surface *es, pixman_region32_t *region,
648 pixman_region32_t *surf_region)
650 struct weston_compositor *ec = es->compositor;
652 unsigned int *vtxcnt;
655 /* The final region to be painted is the intersection of
656 * 'region' and 'surf_region'. However, 'region' is in the global
657 * coordinates, and 'surf_region' is in the surface-local
658 * coordinates. texture_region() will iterate over all pairs of
659 * rectangles from both regions, compute the intersection
660 * polygon for each pair, and store it as a triangle fan if
661 * it has a non-zero area (at least 3 vertices, actually).
663 nfans = texture_region(es, region, surf_region);
665 v = ec->vertices.data;
666 vtxcnt = ec->vtxcnt.data;
669 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
670 glEnableVertexAttribArray(0);
673 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
674 glEnableVertexAttribArray(1);
676 for (i = 0, first = 0; i < nfans; i++) {
677 glDrawArrays(GL_TRIANGLE_FAN, first, vtxcnt[i]);
679 triangle_fan_debug(es, first, vtxcnt[i]);
683 glDisableVertexAttribArray(1);
684 glDisableVertexAttribArray(0);
686 ec->vertices.size = 0;
691 use_output(struct weston_output *output)
694 struct gl_output_state *go = get_output_state(output);
695 struct gl_renderer *gr = get_renderer(output->compositor);
698 ret = eglMakeCurrent(gr->egl_display, go->egl_surface,
699 go->egl_surface, gr->egl_context);
701 if (ret == EGL_FALSE) {
705 weston_log("Failed to make EGL context current.\n");
706 gl_renderer_print_egl_error_state();
714 use_shader(struct gl_renderer *gr,
715 struct gl_shader *shader)
717 if (gr->current_shader == shader)
720 glUseProgram(shader->program);
721 gr->current_shader = shader;
725 shader_uniforms(struct gl_shader *shader,
726 struct weston_surface *surface,
727 struct weston_output *output)
730 struct gl_surface_state *gs = get_surface_state(surface);
732 glUniformMatrix4fv(shader->proj_uniform,
733 1, GL_FALSE, output->matrix.d);
734 glUniform4fv(shader->color_uniform, 1, gs->color);
735 glUniform1f(shader->alpha_uniform, surface->alpha);
737 for (i = 0; i < gs->num_textures; i++)
738 glUniform1i(shader->tex_uniforms[i], i);
742 draw_surface(struct weston_surface *es, struct weston_output *output,
743 pixman_region32_t *damage) /* in global coordinates */
745 struct weston_compositor *ec = es->compositor;
746 struct gl_renderer *gr = get_renderer(ec);
747 struct gl_surface_state *gs = get_surface_state(es);
748 /* repaint bounding region in global coordinates: */
749 pixman_region32_t repaint;
750 /* non-opaque region in surface coordinates: */
751 pixman_region32_t surface_blend;
755 pixman_region32_init(&repaint);
756 pixman_region32_intersect(&repaint,
757 &es->transform.boundingbox, damage);
758 pixman_region32_subtract(&repaint, &repaint, &es->clip);
760 if (!pixman_region32_not_empty(&repaint))
763 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
766 use_shader(gr, &gr->solid_shader);
767 shader_uniforms(&gr->solid_shader, es, output);
770 use_shader(gr, gs->shader);
771 shader_uniforms(gs->shader, es, output);
773 if (es->transform.enabled || output->zoom.active)
778 for (i = 0; i < gs->num_textures; i++) {
779 glActiveTexture(GL_TEXTURE0 + i);
780 glBindTexture(gs->target, gs->textures[i]);
781 glTexParameteri(gs->target, GL_TEXTURE_MIN_FILTER, filter);
782 glTexParameteri(gs->target, GL_TEXTURE_MAG_FILTER, filter);
785 /* blended region is whole surface minus opaque region: */
786 pixman_region32_init_rect(&surface_blend, 0, 0,
787 es->geometry.width, es->geometry.height);
788 pixman_region32_subtract(&surface_blend, &surface_blend, &es->opaque);
790 if (pixman_region32_not_empty(&es->opaque)) {
791 if (gs->shader == &gr->texture_shader_rgba) {
792 /* Special case for RGBA textures with possibly
793 * bad data in alpha channel: use the shader
794 * that forces texture alpha = 1.0.
795 * Xwayland surfaces need this.
797 use_shader(gr, &gr->texture_shader_rgbx);
798 shader_uniforms(&gr->texture_shader_rgbx, es, output);
806 repaint_region(es, &repaint, &es->opaque);
809 if (pixman_region32_not_empty(&surface_blend)) {
810 use_shader(gr, gs->shader);
812 repaint_region(es, &repaint, &surface_blend);
815 pixman_region32_fini(&surface_blend);
818 pixman_region32_fini(&repaint);
822 repaint_surfaces(struct weston_output *output, pixman_region32_t *damage)
824 struct weston_compositor *compositor = output->compositor;
825 struct weston_surface *surface;
827 wl_list_for_each_reverse(surface, &compositor->surface_list, link)
828 if (surface->plane == &compositor->primary_plane)
829 draw_surface(surface, output, damage);
834 texture_border(struct weston_output *output)
836 struct weston_compositor *ec = output->compositor;
837 struct gl_renderer *gr = get_renderer(ec);
841 GLfloat x[4], y[4], u[4], v[4];
843 x[0] = -gr->border.left;
845 x[2] = output->current->width;
846 x[3] = output->current->width + gr->border.right;
848 y[0] = -gr->border.top;
850 y[2] = output->current->height;
851 y[3] = output->current->height + gr->border.bottom;
854 u[1] = (GLfloat) gr->border.left / gr->border.width;
855 u[2] = (GLfloat) (gr->border.width - gr->border.right) / gr->border.width;
859 v[1] = (GLfloat) gr->border.top / gr->border.height;
860 v[2] = (GLfloat) (gr->border.height - gr->border.bottom) / gr->border.height;
864 d = wl_array_add(&ec->vertices, n * 16 * sizeof *d);
865 p = wl_array_add(&ec->indices, n * 6 * sizeof *p);
868 for (i = 0; i < 3; i++)
869 for (j = 0; j < 3; j++) {
871 if (i == 1 && j == 1)
910 draw_border(struct weston_output *output)
912 struct weston_compositor *ec = output->compositor;
913 struct gl_renderer *gr = get_renderer(ec);
914 struct gl_shader *shader = &gr->texture_shader_rgba;
919 use_shader(gr, shader);
921 glUniformMatrix4fv(shader->proj_uniform,
922 1, GL_FALSE, output->matrix.d);
924 glUniform1i(shader->tex_uniforms[0], 0);
925 glUniform1f(shader->alpha_uniform, 1);
927 n = texture_border(output);
929 glActiveTexture(GL_TEXTURE0);
930 glBindTexture(GL_TEXTURE_2D, gr->border.texture);
932 v = ec->vertices.data;
933 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
934 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
935 glEnableVertexAttribArray(0);
936 glEnableVertexAttribArray(1);
938 glDrawElements(GL_TRIANGLES, n * 6,
939 GL_UNSIGNED_INT, ec->indices.data);
941 glDisableVertexAttribArray(1);
942 glDisableVertexAttribArray(0);
944 ec->vertices.size = 0;
945 ec->indices.size = 0;
949 output_get_buffer_damage(struct weston_output *output,
950 pixman_region32_t *buffer_damage)
952 struct gl_output_state *go = get_output_state(output);
953 struct gl_renderer *gr = get_renderer(output->compositor);
954 EGLint buffer_age = 0;
958 if (gr->has_egl_buffer_age) {
959 ret = eglQuerySurface(gr->egl_display, go->egl_surface,
960 EGL_BUFFER_AGE_EXT, &buffer_age);
961 if (ret == EGL_FALSE) {
962 weston_log("buffer age query failed.\n");
963 gl_renderer_print_egl_error_state();
967 if (buffer_age == 0 || buffer_age - 1 > BUFFER_DAMAGE_COUNT)
968 pixman_region32_copy(buffer_damage, &output->region);
970 for (i = 0; i < buffer_age - 1; i++)
971 pixman_region32_union(buffer_damage, buffer_damage,
972 &go->buffer_damage[i]);
976 output_rotate_damage(struct weston_output *output,
977 pixman_region32_t *output_damage)
979 struct gl_output_state *go = get_output_state(output);
980 struct gl_renderer *gr = get_renderer(output->compositor);
983 if (!gr->has_egl_buffer_age)
986 for (i = BUFFER_DAMAGE_COUNT - 1; i >= 1; i--)
987 pixman_region32_copy(&go->buffer_damage[i],
988 &go->buffer_damage[i - 1]);
990 pixman_region32_copy(&go->buffer_damage[0], output_damage);
994 gl_renderer_repaint_output(struct weston_output *output,
995 pixman_region32_t *output_damage)
997 struct gl_output_state *go = get_output_state(output);
998 struct weston_compositor *compositor = output->compositor;
999 struct gl_renderer *gr = get_renderer(compositor);
1002 int32_t width, height;
1003 pixman_region32_t buffer_damage, total_damage;
1005 width = output->current->width +
1006 output->border.left + output->border.right;
1007 height = output->current->height +
1008 output->border.top + output->border.bottom;
1010 glViewport(0, 0, width, height);
1012 if (use_output(output) < 0)
1015 /* if debugging, redraw everything outside the damage to clean up
1016 * debug lines from the previous draw on this buffer:
1018 if (compositor->fan_debug) {
1019 pixman_region32_t undamaged;
1020 pixman_region32_init(&undamaged);
1021 pixman_region32_subtract(&undamaged, &output->region,
1023 compositor->fan_debug = 0;
1024 repaint_surfaces(output, &undamaged);
1025 compositor->fan_debug = 1;
1026 pixman_region32_fini(&undamaged);
1029 pixman_region32_init(&total_damage);
1030 pixman_region32_init(&buffer_damage);
1032 output_get_buffer_damage(output, &buffer_damage);
1033 output_rotate_damage(output, output_damage);
1035 pixman_region32_union(&total_damage, &buffer_damage, output_damage);
1037 repaint_surfaces(output, &total_damage);
1039 pixman_region32_fini(&total_damage);
1040 pixman_region32_fini(&buffer_damage);
1042 if (gr->border.texture)
1043 draw_border(output);
1045 pixman_region32_copy(&output->previous_damage, output_damage);
1046 wl_signal_emit(&output->frame_signal, output);
1048 ret = eglSwapBuffers(gr->egl_display, go->egl_surface);
1049 if (ret == EGL_FALSE && !errored) {
1051 weston_log("Failed in eglSwapBuffers.\n");
1052 gl_renderer_print_egl_error_state();
1058 gl_renderer_read_pixels(struct weston_output *output,
1059 pixman_format_code_t format, void *pixels,
1060 uint32_t x, uint32_t y,
1061 uint32_t width, uint32_t height)
1066 case PIXMAN_a8r8g8b8:
1067 gl_format = GL_BGRA_EXT;
1069 case PIXMAN_a8b8g8r8:
1070 gl_format = GL_RGBA;
1076 if (use_output(output) < 0)
1079 glPixelStorei(GL_PACK_ALIGNMENT, 1);
1080 glReadPixels(x, y, width, height, gl_format,
1081 GL_UNSIGNED_BYTE, pixels);
1087 gl_renderer_flush_damage(struct weston_surface *surface)
1089 struct gl_renderer *gr = get_renderer(surface->compositor);
1090 struct gl_surface_state *gs = get_surface_state(surface);
1091 struct wl_buffer *buffer = gs->buffer_ref.buffer;
1093 #ifdef GL_UNPACK_ROW_LENGTH
1094 pixman_box32_t *rectangles;
1099 pixman_region32_union(&gs->texture_damage,
1100 &gs->texture_damage, &surface->damage);
1105 /* Avoid upload, if the texture won't be used this time.
1106 * We still accumulate the damage in texture_damage, and
1107 * hold the reference to the buffer, in case the surface
1108 * migrates back to the primary plane.
1110 if (surface->plane != &surface->compositor->primary_plane)
1113 if (!pixman_region32_not_empty(&gs->texture_damage))
1116 glBindTexture(GL_TEXTURE_2D, gs->textures[0]);
1118 if (!gr->has_unpack_subimage) {
1119 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1120 gs->pitch, buffer->height, 0,
1121 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1122 wl_shm_buffer_get_data(buffer));
1127 #ifdef GL_UNPACK_ROW_LENGTH
1128 /* Mesa does not define GL_EXT_unpack_subimage */
1129 glPixelStorei(GL_UNPACK_ROW_LENGTH, gs->pitch);
1130 data = wl_shm_buffer_get_data(buffer);
1131 rectangles = pixman_region32_rectangles(&gs->texture_damage, &n);
1132 for (i = 0; i < n; i++) {
1135 r = weston_surface_to_buffer_rect(surface, rectangles[i]);
1137 glPixelStorei(GL_UNPACK_SKIP_PIXELS, r.x1);
1138 glPixelStorei(GL_UNPACK_SKIP_ROWS, r.y1);
1139 glTexSubImage2D(GL_TEXTURE_2D, 0, r.x1, r.y1,
1140 r.x2 - r.x1, r.y2 - r.y1,
1141 GL_BGRA_EXT, GL_UNSIGNED_BYTE, data);
1146 pixman_region32_fini(&gs->texture_damage);
1147 pixman_region32_init(&gs->texture_damage);
1149 weston_buffer_reference(&gs->buffer_ref, NULL);
1153 ensure_textures(struct gl_surface_state *gs, int num_textures)
1157 if (num_textures <= gs->num_textures)
1160 for (i = gs->num_textures; i < num_textures; i++) {
1161 glGenTextures(1, &gs->textures[i]);
1162 glBindTexture(gs->target, gs->textures[i]);
1163 glTexParameteri(gs->target,
1164 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1165 glTexParameteri(gs->target,
1166 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1168 gs->num_textures = num_textures;
1169 glBindTexture(gs->target, 0);
1173 gl_renderer_attach(struct weston_surface *es, struct wl_buffer *buffer)
1175 struct weston_compositor *ec = es->compositor;
1176 struct gl_renderer *gr = get_renderer(ec);
1177 struct gl_surface_state *gs = get_surface_state(es);
1178 EGLint attribs[3], format;
1181 weston_buffer_reference(&gs->buffer_ref, buffer);
1184 for (i = 0; i < gs->num_images; i++) {
1185 gr->destroy_image(gr->egl_display, gs->images[i]);
1186 gs->images[i] = NULL;
1189 glDeleteTextures(gs->num_textures, gs->textures);
1190 gs->num_textures = 0;
1194 if (wl_buffer_is_shm(buffer)) {
1195 gs->pitch = wl_shm_buffer_get_stride(buffer) / 4;
1196 gs->target = GL_TEXTURE_2D;
1198 ensure_textures(gs, 1);
1199 glBindTexture(GL_TEXTURE_2D, gs->textures[0]);
1200 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1201 gs->pitch, buffer->height, 0,
1202 GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
1203 if (wl_shm_buffer_get_format(buffer) == WL_SHM_FORMAT_XRGB8888)
1204 gs->shader = &gr->texture_shader_rgbx;
1206 gs->shader = &gr->texture_shader_rgba;
1207 } else if (gr->query_buffer(gr->egl_display, buffer,
1208 EGL_TEXTURE_FORMAT, &format)) {
1209 for (i = 0; i < gs->num_images; i++)
1210 gr->destroy_image(gr->egl_display, gs->images[i]);
1212 gs->target = GL_TEXTURE_2D;
1214 case EGL_TEXTURE_RGB:
1215 case EGL_TEXTURE_RGBA:
1218 gs->shader = &gr->texture_shader_rgba;
1220 case EGL_TEXTURE_EXTERNAL_WL:
1222 gs->target = GL_TEXTURE_EXTERNAL_OES;
1223 gs->shader = &gr->texture_shader_egl_external;
1225 case EGL_TEXTURE_Y_UV_WL:
1227 gs->shader = &gr->texture_shader_y_uv;
1229 case EGL_TEXTURE_Y_U_V_WL:
1231 gs->shader = &gr->texture_shader_y_u_v;
1233 case EGL_TEXTURE_Y_XUXV_WL:
1235 gs->shader = &gr->texture_shader_y_xuxv;
1239 ensure_textures(gs, num_planes);
1240 for (i = 0; i < num_planes; i++) {
1241 attribs[0] = EGL_WAYLAND_PLANE_WL;
1243 attribs[2] = EGL_NONE;
1244 gs->images[i] = gr->create_image(gr->egl_display,
1246 EGL_WAYLAND_BUFFER_WL,
1248 if (!gs->images[i]) {
1249 weston_log("failed to create img for plane %d\n", i);
1254 glActiveTexture(GL_TEXTURE0 + i);
1255 glBindTexture(gs->target, gs->textures[i]);
1256 gr->image_target_texture_2d(gs->target,
1260 gs->pitch = buffer->width;
1262 weston_log("unhandled buffer type!\n");
1263 weston_buffer_reference(&gs->buffer_ref, NULL);
1268 gl_renderer_surface_set_color(struct weston_surface *surface,
1269 float red, float green, float blue, float alpha)
1271 struct gl_surface_state *gs = get_surface_state(surface);
1272 struct gl_renderer *gr = get_renderer(surface->compositor);
1275 gs->color[1] = green;
1276 gs->color[2] = blue;
1277 gs->color[3] = alpha;
1279 gs->shader = &gr->solid_shader;
1283 gl_renderer_create_surface(struct weston_surface *surface)
1285 struct gl_surface_state *gs;
1287 gs = calloc(1, sizeof *gs);
1291 /* A buffer is never attached to solid color surfaces, yet
1292 * they still go through texcoord computations. Do not divide
1297 pixman_region32_init(&gs->texture_damage);
1298 surface->renderer_state = gs;
1304 gl_renderer_destroy_surface(struct weston_surface *surface)
1306 struct gl_surface_state *gs = get_surface_state(surface);
1307 struct gl_renderer *gr = get_renderer(surface->compositor);
1310 glDeleteTextures(gs->num_textures, gs->textures);
1312 for (i = 0; i < gs->num_images; i++)
1313 gr->destroy_image(gr->egl_display, gs->images[i]);
1315 weston_buffer_reference(&gs->buffer_ref, NULL);
1316 pixman_region32_fini(&gs->texture_damage);
1320 static const char vertex_shader[] =
1321 "uniform mat4 proj;\n"
1322 "attribute vec2 position;\n"
1323 "attribute vec2 texcoord;\n"
1324 "varying vec2 v_texcoord;\n"
1327 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
1328 " v_texcoord = texcoord;\n"
1331 /* Declare common fragment shader uniforms */
1332 #define FRAGMENT_CONVERT_YUV \
1336 " gl_FragColor.r = y + 1.59602678 * v;\n" \
1337 " gl_FragColor.g = y - 0.39176229 * u - 0.81296764 * v;\n" \
1338 " gl_FragColor.b = y + 2.01723214 * u;\n" \
1339 " gl_FragColor.a = alpha;\n"
1341 static const char fragment_debug[] =
1342 " gl_FragColor = vec4(0.0, 0.3, 0.0, 0.2) + gl_FragColor * 0.8;\n";
1344 static const char fragment_brace[] =
1347 static const char texture_fragment_shader_rgba[] =
1348 "precision mediump float;\n"
1349 "varying vec2 v_texcoord;\n"
1350 "uniform sampler2D tex;\n"
1351 "uniform float alpha;\n"
1354 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
1357 static const char texture_fragment_shader_rgbx[] =
1358 "precision mediump float;\n"
1359 "varying vec2 v_texcoord;\n"
1360 "uniform sampler2D tex;\n"
1361 "uniform float alpha;\n"
1364 " gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb\n;"
1365 " gl_FragColor.a = alpha;\n"
1368 static const char texture_fragment_shader_egl_external[] =
1369 "#extension GL_OES_EGL_image_external : require\n"
1370 "precision mediump float;\n"
1371 "varying vec2 v_texcoord;\n"
1372 "uniform samplerExternalOES tex;\n"
1373 "uniform float alpha;\n"
1376 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
1379 static const char texture_fragment_shader_y_uv[] =
1380 "precision mediump float;\n"
1381 "uniform sampler2D tex;\n"
1382 "uniform sampler2D tex1;\n"
1383 "varying vec2 v_texcoord;\n"
1384 "uniform float alpha;\n"
1386 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1387 " float u = texture2D(tex1, v_texcoord).r - 0.5;\n"
1388 " float v = texture2D(tex1, v_texcoord).g - 0.5;\n"
1389 FRAGMENT_CONVERT_YUV
1392 static const char texture_fragment_shader_y_u_v[] =
1393 "precision mediump float;\n"
1394 "uniform sampler2D tex;\n"
1395 "uniform sampler2D tex1;\n"
1396 "uniform sampler2D tex2;\n"
1397 "varying vec2 v_texcoord;\n"
1398 "uniform float alpha;\n"
1400 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1401 " float u = texture2D(tex1, v_texcoord).x - 0.5;\n"
1402 " float v = texture2D(tex2, v_texcoord).x - 0.5;\n"
1403 FRAGMENT_CONVERT_YUV
1406 static const char texture_fragment_shader_y_xuxv[] =
1407 "precision mediump float;\n"
1408 "uniform sampler2D tex;\n"
1409 "uniform sampler2D tex1;\n"
1410 "varying vec2 v_texcoord;\n"
1411 "uniform float alpha;\n"
1413 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1414 " float u = texture2D(tex1, v_texcoord).g - 0.5;\n"
1415 " float v = texture2D(tex1, v_texcoord).a - 0.5;\n"
1416 FRAGMENT_CONVERT_YUV
1419 static const char solid_fragment_shader[] =
1420 "precision mediump float;\n"
1421 "uniform vec4 color;\n"
1422 "uniform float alpha;\n"
1425 " gl_FragColor = alpha * color\n;"
1429 compile_shader(GLenum type, int count, const char **sources)
1435 s = glCreateShader(type);
1436 glShaderSource(s, count, sources, NULL);
1438 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
1440 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1441 weston_log("shader info: %s\n", msg);
1449 shader_init(struct gl_shader *shader, struct weston_compositor *ec,
1450 const char *vertex_source, const char *fragment_source)
1455 const char *sources[3];
1456 struct gl_renderer *renderer = get_renderer(ec);
1458 shader->vertex_shader =
1459 compile_shader(GL_VERTEX_SHADER, 1, &vertex_source);
1461 if (renderer->fragment_shader_debug) {
1462 sources[0] = fragment_source;
1463 sources[1] = fragment_debug;
1464 sources[2] = fragment_brace;
1467 sources[0] = fragment_source;
1468 sources[1] = fragment_brace;
1472 shader->fragment_shader =
1473 compile_shader(GL_FRAGMENT_SHADER, count, sources);
1475 shader->program = glCreateProgram();
1476 glAttachShader(shader->program, shader->vertex_shader);
1477 glAttachShader(shader->program, shader->fragment_shader);
1478 glBindAttribLocation(shader->program, 0, "position");
1479 glBindAttribLocation(shader->program, 1, "texcoord");
1481 glLinkProgram(shader->program);
1482 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1484 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1485 weston_log("link info: %s\n", msg);
1489 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1490 shader->tex_uniforms[0] = glGetUniformLocation(shader->program, "tex");
1491 shader->tex_uniforms[1] = glGetUniformLocation(shader->program, "tex1");
1492 shader->tex_uniforms[2] = glGetUniformLocation(shader->program, "tex2");
1493 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
1494 shader->color_uniform = glGetUniformLocation(shader->program, "color");
1500 shader_release(struct gl_shader *shader)
1502 glDeleteShader(shader->vertex_shader);
1503 glDeleteShader(shader->fragment_shader);
1504 glDeleteProgram(shader->program);
1506 shader->vertex_shader = 0;
1507 shader->fragment_shader = 0;
1508 shader->program = 0;
1512 log_extensions(const char *name, const char *extensions)
1514 const char *p, *end;
1518 l = weston_log("%s:", name);
1521 end = strchrnul(p, ' ');
1524 l = weston_log_continue("\n" STAMP_SPACE "%.*s",
1527 l += weston_log_continue(" %.*s", len, p);
1528 for (p = end; isspace(*p); p++)
1531 weston_log_continue("\n");
1535 log_egl_gl_info(EGLDisplay egldpy)
1539 str = eglQueryString(egldpy, EGL_VERSION);
1540 weston_log("EGL version: %s\n", str ? str : "(null)");
1542 str = eglQueryString(egldpy, EGL_VENDOR);
1543 weston_log("EGL vendor: %s\n", str ? str : "(null)");
1545 str = eglQueryString(egldpy, EGL_CLIENT_APIS);
1546 weston_log("EGL client APIs: %s\n", str ? str : "(null)");
1548 str = eglQueryString(egldpy, EGL_EXTENSIONS);
1549 log_extensions("EGL extensions", str ? str : "(null)");
1551 str = (char *)glGetString(GL_VERSION);
1552 weston_log("GL version: %s\n", str ? str : "(null)");
1554 str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
1555 weston_log("GLSL version: %s\n", str ? str : "(null)");
1557 str = (char *)glGetString(GL_VENDOR);
1558 weston_log("GL vendor: %s\n", str ? str : "(null)");
1560 str = (char *)glGetString(GL_RENDERER);
1561 weston_log("GL renderer: %s\n", str ? str : "(null)");
1563 str = (char *)glGetString(GL_EXTENSIONS);
1564 log_extensions("GL extensions", str ? str : "(null)");
1568 log_egl_config_info(EGLDisplay egldpy, EGLConfig eglconfig)
1572 weston_log("Chosen EGL config details:\n");
1574 weston_log_continue(STAMP_SPACE "RGBA bits");
1575 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_RED_SIZE, &r) &&
1576 eglGetConfigAttrib(egldpy, eglconfig, EGL_GREEN_SIZE, &g) &&
1577 eglGetConfigAttrib(egldpy, eglconfig, EGL_BLUE_SIZE, &b) &&
1578 eglGetConfigAttrib(egldpy, eglconfig, EGL_ALPHA_SIZE, &a))
1579 weston_log_continue(": %d %d %d %d\n", r, g, b, a);
1581 weston_log_continue(" unknown\n");
1583 weston_log_continue(STAMP_SPACE "swap interval range");
1584 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_MIN_SWAP_INTERVAL, &a) &&
1585 eglGetConfigAttrib(egldpy, eglconfig, EGL_MAX_SWAP_INTERVAL, &b))
1586 weston_log_continue(": %d - %d\n", a, b);
1588 weston_log_continue(" unknown\n");
1592 output_apply_border(struct weston_output *output, struct gl_renderer *gr)
1594 output->border.top = gr->border.top;
1595 output->border.bottom = gr->border.bottom;
1596 output->border.left = gr->border.left;
1597 output->border.right = gr->border.right;
1601 gl_renderer_set_border(struct weston_compositor *ec, int32_t width, int32_t height, void *data,
1604 struct gl_renderer *gr = get_renderer(ec);
1605 struct weston_output *output;
1607 gr->border.left = edges[0];
1608 gr->border.right = edges[1];
1609 gr->border.top = edges[2];
1610 gr->border.bottom = edges[3];
1612 gr->border.width = width;
1613 gr->border.height = height;
1615 glGenTextures(1, &gr->border.texture);
1616 glBindTexture(GL_TEXTURE_2D, gr->border.texture);
1617 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1618 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1619 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1620 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1622 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1625 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1628 wl_list_for_each(output, &ec->output_list, link)
1629 output_apply_border(output, gr);
1633 gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface);
1636 gl_renderer_output_create(struct weston_output *output,
1637 EGLNativeWindowType window)
1639 struct weston_compositor *ec = output->compositor;
1640 struct gl_renderer *gr = get_renderer(ec);
1641 struct gl_output_state *go = calloc(1, sizeof *go);
1648 eglCreateWindowSurface(gr->egl_display,
1652 if (go->egl_surface == EGL_NO_SURFACE) {
1653 weston_log("failed to create egl surface\n");
1658 if (gr->egl_context == NULL)
1659 if (gl_renderer_setup(ec, go->egl_surface) < 0) {
1664 for (i = 0; i < BUFFER_DAMAGE_COUNT; i++)
1665 pixman_region32_init(&go->buffer_damage[i]);
1667 output->renderer_state = go;
1669 output_apply_border(output, gr);
1675 gl_renderer_output_destroy(struct weston_output *output)
1677 struct gl_renderer *gr = get_renderer(output->compositor);
1678 struct gl_output_state *go = get_output_state(output);
1681 for (i = 0; i < 2; i++)
1682 pixman_region32_fini(&go->buffer_damage[i]);
1684 eglDestroySurface(gr->egl_display, go->egl_surface);
1689 WL_EXPORT EGLSurface
1690 gl_renderer_output_surface(struct weston_output *output)
1692 return get_output_state(output)->egl_surface;
1696 gl_renderer_destroy(struct weston_compositor *ec)
1698 struct gl_renderer *gr = get_renderer(ec);
1700 if (gr->has_bind_display)
1701 gr->unbind_display(gr->egl_display, ec->wl_display);
1703 /* Work around crash in egl_dri2.c's dri2_make_current() - when does this apply? */
1704 eglMakeCurrent(gr->egl_display,
1705 EGL_NO_SURFACE, EGL_NO_SURFACE,
1708 eglTerminate(gr->egl_display);
1715 egl_choose_config(struct gl_renderer *gr, const EGLint *attribs,
1716 const EGLint *visual_id)
1723 if (!eglGetConfigs(gr->egl_display, NULL, 0, &count) || count < 1)
1726 configs = calloc(count, sizeof *configs);
1730 if (!eglChooseConfig(gr->egl_display, attribs, configs,
1734 for (i = 0; i < matched; ++i) {
1738 if (!eglGetConfigAttrib(gr->egl_display,
1739 configs[i], EGL_NATIVE_VISUAL_ID,
1743 if (id != *visual_id)
1747 gr->egl_config = configs[i];
1758 WL_EXPORT const EGLint gl_renderer_opaque_attribs[] = {
1759 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1764 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1768 WL_EXPORT const EGLint gl_renderer_alpha_attribs[] = {
1769 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1774 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1779 gl_renderer_create(struct weston_compositor *ec, EGLNativeDisplayType display,
1780 const EGLint *attribs, const EGLint *visual_id)
1782 struct gl_renderer *gr;
1783 EGLint major, minor;
1785 gr = calloc(1, sizeof *gr);
1790 gr->base.read_pixels = gl_renderer_read_pixels;
1791 gr->base.repaint_output = gl_renderer_repaint_output;
1792 gr->base.flush_damage = gl_renderer_flush_damage;
1793 gr->base.attach = gl_renderer_attach;
1794 gr->base.create_surface = gl_renderer_create_surface;
1795 gr->base.surface_set_color = gl_renderer_surface_set_color;
1796 gr->base.destroy_surface = gl_renderer_destroy_surface;
1797 gr->base.destroy = gl_renderer_destroy;
1799 gr->egl_display = eglGetDisplay(display);
1800 if (gr->egl_display == EGL_NO_DISPLAY) {
1801 weston_log("failed to create display\n");
1805 if (!eglInitialize(gr->egl_display, &major, &minor)) {
1806 weston_log("failed to initialize display\n");
1810 if (egl_choose_config(gr, attribs, visual_id) < 0) {
1811 weston_log("failed to choose EGL config\n");
1815 ec->renderer = &gr->base;
1820 gl_renderer_print_egl_error_state();
1825 WL_EXPORT EGLDisplay
1826 gl_renderer_display(struct weston_compositor *ec)
1828 return get_renderer(ec)->egl_display;
1832 compile_shaders(struct weston_compositor *ec)
1834 struct gl_renderer *gr = get_renderer(ec);
1836 if (shader_init(&gr->texture_shader_rgba, ec,
1837 vertex_shader, texture_fragment_shader_rgba) < 0)
1839 if (shader_init(&gr->texture_shader_rgbx, ec,
1840 vertex_shader, texture_fragment_shader_rgbx) < 0)
1842 if (gr->has_egl_image_external &&
1843 shader_init(&gr->texture_shader_egl_external, ec,
1844 vertex_shader, texture_fragment_shader_egl_external) < 0)
1846 if (shader_init(&gr->texture_shader_y_uv, ec,
1847 vertex_shader, texture_fragment_shader_y_uv) < 0)
1849 if (shader_init(&gr->texture_shader_y_u_v, ec,
1850 vertex_shader, texture_fragment_shader_y_u_v) < 0)
1852 if (shader_init(&gr->texture_shader_y_xuxv, ec,
1853 vertex_shader, texture_fragment_shader_y_xuxv) < 0)
1855 if (shader_init(&gr->solid_shader, ec,
1856 vertex_shader, solid_fragment_shader) < 0)
1863 fragment_debug_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
1866 struct weston_compositor *ec = data;
1867 struct gl_renderer *gr = get_renderer(ec);
1868 struct weston_output *output;
1870 gr->fragment_shader_debug ^= 1;
1872 shader_release(&gr->texture_shader_rgba);
1873 shader_release(&gr->texture_shader_rgbx);
1874 shader_release(&gr->texture_shader_egl_external);
1875 shader_release(&gr->texture_shader_y_uv);
1876 shader_release(&gr->texture_shader_y_u_v);
1877 shader_release(&gr->texture_shader_y_xuxv);
1878 shader_release(&gr->solid_shader);
1880 compile_shaders(ec);
1882 /* Force use_shader() to call glUseProgram(), since we need to use
1883 * the recompiled version of the shader. */
1884 gr->current_shader = NULL;
1886 wl_list_for_each(output, &ec->output_list, link)
1887 weston_output_damage(output);
1891 gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface)
1893 struct gl_renderer *gr = get_renderer(ec);
1894 const char *extensions;
1897 static const EGLint context_attribs[] = {
1898 EGL_CONTEXT_CLIENT_VERSION, 2,
1902 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
1903 weston_log("failed to bind EGL_OPENGL_ES_API\n");
1904 gl_renderer_print_egl_error_state();
1908 log_egl_config_info(gr->egl_display, gr->egl_config);
1910 gr->egl_context = eglCreateContext(gr->egl_display, gr->egl_config,
1911 EGL_NO_CONTEXT, context_attribs);
1912 if (gr->egl_context == NULL) {
1913 weston_log("failed to create context\n");
1914 gl_renderer_print_egl_error_state();
1918 ret = eglMakeCurrent(gr->egl_display, egl_surface,
1919 egl_surface, gr->egl_context);
1920 if (ret == EGL_FALSE) {
1921 weston_log("Failed to make EGL context current.\n");
1922 gl_renderer_print_egl_error_state();
1926 log_egl_gl_info(gr->egl_display);
1928 gr->image_target_texture_2d =
1929 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
1930 gr->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1931 gr->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1933 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
1934 gr->unbind_display =
1935 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
1937 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
1939 extensions = (const char *) glGetString(GL_EXTENSIONS);
1941 weston_log("Retrieving GL extension string failed.\n");
1945 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
1946 weston_log("GL_EXT_texture_format_BGRA8888 not available\n");
1950 if (strstr(extensions, "GL_EXT_read_format_bgra"))
1951 ec->read_format = PIXMAN_a8r8g8b8;
1953 ec->read_format = PIXMAN_a8b8g8r8;
1955 if (strstr(extensions, "GL_EXT_unpack_subimage"))
1956 gr->has_unpack_subimage = 1;
1958 if (strstr(extensions, "GL_OES_EGL_image_external"))
1959 gr->has_egl_image_external = 1;
1962 (const char *) eglQueryString(gr->egl_display, EGL_EXTENSIONS);
1964 weston_log("Retrieving EGL extension string failed.\n");
1968 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
1969 gr->has_bind_display = 1;
1970 if (gr->has_bind_display) {
1971 ret = gr->bind_display(gr->egl_display, ec->wl_display);
1973 gr->has_bind_display = 0;
1976 if (strstr(extensions, "EGL_EXT_buffer_age"))
1977 gr->has_egl_buffer_age = 1;
1979 weston_log("warning: EGL_EXT_buffer_age not supported. "
1980 "Performance could be affected.\n");
1982 glActiveTexture(GL_TEXTURE0);
1984 if (compile_shaders(ec))
1987 weston_compositor_add_debug_binding(ec, KEY_S,
1988 fragment_debug_binding, ec);
1990 weston_log("GL ES 2 renderer features:\n");
1991 weston_log_continue(STAMP_SPACE "read-back format: %s\n",
1992 ec->read_format == PIXMAN_a8r8g8b8 ? "BGRA" : "RGBA");
1993 weston_log_continue(STAMP_SPACE "wl_shm sub-image to texture: %s\n",
1994 gr->has_unpack_subimage ? "yes" : "no");
1995 weston_log_continue(STAMP_SPACE "EGL Wayland extension: %s\n",
1996 gr->has_bind_display ? "yes" : "no");