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.
31 #include "compositor.h"
32 #include "weston-egl-ext.h"
35 egl_error_string(EGLint code)
37 #define MYERRCODE(x) case x: return #x;
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)
61 print_egl_error_state(void)
66 weston_log("EGL error state: %s (0x%04lx)\n",
67 egl_error_string(code), (long)code);
94 float_difference(GLfloat a, GLfloat b)
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;
100 GLfloat adiff = fabsf(diff);
102 if (adiff <= max_diff)
107 if (adiff <= (a > b ? a : b) * max_rel_diff)
113 /* A line segment (p1x, p1y)-(p2x, p2y) intersects the line x = x_arg.
114 * Compute the y coordinate of the intersection.
117 clip_intersect_y(GLfloat p1x, GLfloat p1y, GLfloat p2x, GLfloat p2y,
121 GLfloat diff = float_difference(p1x, p2x);
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.
131 a = (x_arg - p2x) / diff;
132 return p2y + (p1y - p2y) * a;
135 /* A line segment (p1x, p1y)-(p2x, p2y) intersects the line y = y_arg.
136 * Compute the x coordinate of the intersection.
139 clip_intersect_x(GLfloat p1x, GLfloat p1y, GLfloat p2x, GLfloat p2y,
143 GLfloat diff = float_difference(p1y, p2y);
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.
153 a = (y_arg - p2y) / diff;
154 return p2x + (p1x - p2x) * a;
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,
165 clip_append_vertex(struct clip_context *ctx, GLfloat x, GLfloat y)
167 *ctx->vertices.x++ = x;
168 *ctx->vertices.y++ = y;
171 static enum path_transition
172 path_transition_left_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
174 return ((ctx->prev.x >= ctx->clip.x1) << 1) | (x >= ctx->clip.x1);
177 static enum path_transition
178 path_transition_right_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
180 return ((ctx->prev.x < ctx->clip.x2) << 1) | (x < ctx->clip.x2);
183 static enum path_transition
184 path_transition_top_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
186 return ((ctx->prev.y >= ctx->clip.y1) << 1) | (y >= ctx->clip.y1);
189 static enum path_transition
190 path_transition_bottom_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
192 return ((ctx->prev.y < ctx->clip.y2) << 1) | (y < ctx->clip.y2);
196 clip_polygon_leftright(struct clip_context *ctx,
197 enum path_transition transition,
198 GLfloat x, GLfloat y, GLfloat clip_x)
202 switch (transition) {
203 case PATH_TRANSITION_IN_TO_IN:
204 clip_append_vertex(ctx, x, y);
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);
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);
215 case PATH_TRANSITION_OUT_TO_OUT:
219 assert(0 && "bad enum path_transition");
227 clip_polygon_topbottom(struct clip_context *ctx,
228 enum path_transition transition,
229 GLfloat x, GLfloat y, GLfloat clip_y)
233 switch (transition) {
234 case PATH_TRANSITION_IN_TO_IN:
235 clip_append_vertex(ctx, x, y);
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);
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);
246 case PATH_TRANSITION_OUT_TO_OUT:
250 assert(0 && "bad enum path_transition");
258 clip_context_prepare(struct clip_context *ctx, const struct polygon8 *src,
259 GLfloat *dst_x, GLfloat *dst_y)
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;
268 clip_polygon_left(struct clip_context *ctx, const struct polygon8 *src,
269 GLfloat *dst_x, GLfloat *dst_y)
271 enum path_transition trans;
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],
280 return ctx->vertices.x - dst_x;
284 clip_polygon_right(struct clip_context *ctx, const struct polygon8 *src,
285 GLfloat *dst_x, GLfloat *dst_y)
287 enum path_transition trans;
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],
296 return ctx->vertices.x - dst_x;
300 clip_polygon_top(struct clip_context *ctx, const struct polygon8 *src,
301 GLfloat *dst_x, GLfloat *dst_y)
303 enum path_transition trans;
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],
312 return ctx->vertices.x - dst_x;
316 clip_polygon_bottom(struct clip_context *ctx, const struct polygon8 *src,
317 GLfloat *dst_x, GLfloat *dst_y)
319 enum path_transition trans;
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],
328 return ctx->vertices.x - dst_x;
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)
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
345 calculate_edges(struct weston_surface *es, pixman_box32_t *rect,
346 pixman_box32_t *surf_rect, GLfloat *ex, GLfloat *ey)
348 struct polygon8 polygon;
349 struct clip_context ctx;
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 },
358 ctx.clip.x1 = rect->x1;
359 ctx.clip.y1 = rect->y1;
360 ctx.clip.x2 = rect->x2;
361 ctx.clip.y2 = rect->y2;
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]);
368 /* find bounding box: */
369 min_x = max_x = surf.x[0];
370 min_y = max_y = surf.y[0];
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]);
379 /* First, simple bounding box check to discard early transformed
380 * surface rects that do not intersect with the clip region:
382 if ((min_x >= ctx.clip.x2) || (max_x <= ctx.clip.x1) ||
383 (min_y >= ctx.clip.y2) || (max_y <= ctx.clip.y1))
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:
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);
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.
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);
409 /* Get rid of duplicate vertices */
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)
421 if (float_difference(ex[n - 1], surf.x[0]) == 0.0f &&
422 float_difference(ey[n - 1], surf.y[0]) == 0.0f)
432 texture_region(struct weston_surface *es, pixman_region32_t *region,
433 pixman_region32_t *surf_region)
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;
441 rects = pixman_region32_rectangles(region, &nrects);
442 surf_rects = pixman_region32_rectangles(surf_region, &nsurf);
444 /* worst case we can have 8 vertices per rect (ie. clipped into
447 v = wl_array_add(&ec->vertices, nrects * nsurf * 8 * 4 * sizeof *v);
448 vtxcnt = wl_array_add(&ec->vtxcnt, nrects * nsurf * sizeof *vtxcnt);
450 inv_width = 1.0 / es->pitch;
451 inv_height = 1.0 / es->geometry.height;
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];
458 GLfloat ex[8], ey[8]; /* edge points in screen space */
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.
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.
471 * To do this, we first calculate the (up to eight) points that
472 * form the intersection of the clip rect and the transformed
475 n = calculate_edges(es, rect, surf_rect, ex, ey);
479 /* emit edge points: */
480 for (k = 0; k < n; k++) {
481 weston_surface_from_global_float(es, ex[k], ey[k], &sx, &sy);
486 *(v++) = sx * inv_width;
487 *(v++) = sy * inv_height;
498 triangle_fan_debug(struct weston_surface *surface, int first, int count)
500 struct weston_compositor *compositor = surface->compositor;
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 },
513 nelems = (count - 1 + count - 2) * 2;
515 buffer = malloc(sizeof(GLushort) * nelems);
518 for (i = 1; i < count; i++) {
520 *index++ = first + i;
523 for (i = 2; i < count; i++) {
524 *index++ = first + i - 1;
525 *index++ = first + i;
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);
537 repaint_region(struct weston_surface *es, pixman_region32_t *region,
538 pixman_region32_t *surf_region)
540 struct weston_compositor *ec = es->compositor;
542 unsigned int *vtxcnt;
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).
553 nfans = texture_region(es, region, surf_region);
555 v = ec->vertices.data;
556 vtxcnt = ec->vtxcnt.data;
559 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
560 glEnableVertexAttribArray(0);
563 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
564 glEnableVertexAttribArray(1);
566 for (i = 0, first = 0; i < nfans; i++) {
567 glDrawArrays(GL_TRIANGLE_FAN, first, vtxcnt[i]);
569 triangle_fan_debug(es, first, vtxcnt[i]);
573 glDisableVertexAttribArray(1);
574 glDisableVertexAttribArray(0);
576 ec->vertices.size = 0;
581 weston_compositor_use_shader(struct weston_compositor *compositor,
582 struct weston_shader *shader)
584 if (compositor->current_shader == shader)
587 glUseProgram(shader->program);
588 compositor->current_shader = shader;
592 weston_shader_uniforms(struct weston_shader *shader,
593 struct weston_surface *surface,
594 struct weston_output *output)
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);
603 for (i = 0; i < surface->num_textures; i++)
604 glUniform1i(shader->tex_uniforms[i], i);
608 draw_surface(struct weston_surface *es, struct weston_output *output,
609 pixman_region32_t *damage) /* in global coordinates */
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;
620 pixman_region32_init(&repaint);
621 pixman_region32_intersect(&repaint,
622 &es->transform.boundingbox, damage);
623 pixman_region32_subtract(&repaint, &repaint, &es->clip);
625 if (!pixman_region32_not_empty(&repaint))
628 buffer_damage = &output->buffer_damage[output->current_buffer];
629 pixman_region32_subtract(buffer_damage, buffer_damage, &repaint);
631 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
634 weston_compositor_use_shader(ec, &ec->solid_shader);
635 weston_shader_uniforms(&ec->solid_shader, es, output);
638 weston_compositor_use_shader(ec, es->shader);
639 weston_shader_uniforms(es->shader, es, output);
641 if (es->transform.enabled || output->zoom.active)
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);
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);
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.
665 weston_compositor_use_shader(ec, &ec->texture_shader_rgbx);
666 weston_shader_uniforms(&ec->texture_shader_rgbx, es, output);
674 repaint_region(es, &repaint, &es->opaque);
677 if (pixman_region32_not_empty(&surface_blend)) {
678 weston_compositor_use_shader(ec, es->shader);
680 repaint_region(es, &repaint, &surface_blend);
683 pixman_region32_fini(&surface_blend);
686 pixman_region32_fini(&repaint);
690 repaint_surfaces(struct weston_output *output, pixman_region32_t *damage)
692 struct weston_compositor *compositor = output->compositor;
693 struct weston_surface *surface;
695 wl_list_for_each_reverse(surface, &compositor->surface_list, link)
696 if (surface->plane == &compositor->primary_plane)
697 draw_surface(surface, output, damage);
701 gles2_renderer_repaint_output(struct weston_output *output,
702 pixman_region32_t *output_damage)
704 struct weston_compositor *compositor = output->compositor;
707 int32_t width, height, i;
709 width = output->current->width +
710 output->border.left + output->border.right;
711 height = output->current->height +
712 output->border.top + output->border.bottom;
714 glViewport(0, 0, width, height);
716 ret = eglMakeCurrent(compositor->egl_display, output->egl_surface,
717 output->egl_surface, compositor->egl_context);
718 if (ret == EGL_FALSE) {
722 weston_log("Failed to make EGL context current.\n");
723 print_egl_error_state();
727 /* if debugging, redraw everything outside the damage to clean up
728 * debug lines from the previous draw on this buffer:
730 if (compositor->fan_debug) {
731 pixman_region32_t undamaged;
732 pixman_region32_init(&undamaged);
733 pixman_region32_subtract(&undamaged, &output->region,
735 compositor->fan_debug = 0;
736 repaint_surfaces(output, &undamaged);
737 compositor->fan_debug = 1;
738 pixman_region32_fini(&undamaged);
741 for (i = 0; i < 2; i++)
742 pixman_region32_union(&output->buffer_damage[i],
743 &output->buffer_damage[i],
746 pixman_region32_union(output_damage, output_damage,
747 &output->buffer_damage[output->current_buffer]);
749 repaint_surfaces(output, output_damage);
751 wl_signal_emit(&output->frame_signal, output);
753 ret = eglSwapBuffers(compositor->egl_display, output->egl_surface);
754 if (ret == EGL_FALSE && !errored) {
756 weston_log("Failed in eglSwapBuffers.\n");
757 print_egl_error_state();
760 output->current_buffer ^= 1;
765 gles2_renderer_flush_damage(struct weston_surface *surface)
767 #ifdef GL_UNPACK_ROW_LENGTH
768 pixman_box32_t *rectangles;
773 glBindTexture(GL_TEXTURE_2D, surface->textures[0]);
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));
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);
802 ensure_textures(struct weston_surface *es, int num_textures)
806 if (num_textures <= es->num_textures)
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);
817 es->num_textures = num_textures;
818 glBindTexture(es->target, 0);
822 gles2_renderer_attach(struct weston_surface *es, struct wl_buffer *buffer)
824 struct weston_compositor *ec = es->compositor;
825 EGLint attribs[3], format;
829 for (i = 0; i < es->num_images; i++) {
830 ec->destroy_image(ec->egl_display, es->images[i]);
831 es->images[i] = NULL;
834 glDeleteTextures(es->num_textures, es->textures);
835 es->num_textures = 0;
839 if (wl_buffer_is_shm(buffer)) {
840 es->pitch = wl_shm_buffer_get_stride(buffer) / 4;
841 es->target = GL_TEXTURE_2D;
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;
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]);
857 es->target = GL_TEXTURE_2D;
859 case EGL_TEXTURE_RGB:
860 case EGL_TEXTURE_RGBA:
863 es->shader = &ec->texture_shader_rgba;
865 case EGL_TEXTURE_EXTERNAL_WL:
867 es->target = GL_TEXTURE_EXTERNAL_OES;
868 es->shader = &ec->texture_shader_egl_external;
870 case EGL_TEXTURE_Y_UV_WL:
872 es->shader = &ec->texture_shader_y_uv;
874 case EGL_TEXTURE_Y_U_V_WL:
876 es->shader = &ec->texture_shader_y_u_v;
878 case EGL_TEXTURE_Y_XUXV_WL:
880 es->shader = &ec->texture_shader_y_xuxv;
884 ensure_textures(es, num_planes);
885 for (i = 0; i < num_planes; i++) {
886 attribs[0] = EGL_WAYLAND_PLANE_WL;
888 attribs[2] = EGL_NONE;
889 es->images[i] = ec->create_image(ec->egl_display,
891 EGL_WAYLAND_BUFFER_WL,
893 if (!es->images[i]) {
894 weston_log("failed to create img for plane %d\n", i);
899 glActiveTexture(GL_TEXTURE0 + i);
900 glBindTexture(es->target, es->textures[i]);
901 ec->image_target_texture_2d(es->target,
905 es->pitch = buffer->width;
907 weston_log("unhandled buffer type!\n");
912 gles2_renderer_destroy_surface(struct weston_surface *surface)
914 struct weston_compositor *ec = surface->compositor;
917 glDeleteTextures(surface->num_textures, surface->textures);
919 for (i = 0; i < surface->num_images; i++)
920 ec->destroy_image(ec->egl_display, surface->images[i]);
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"
930 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
931 " v_texcoord = texcoord;\n"
934 /* Declare common fragment shader uniforms */
935 #define FRAGMENT_CONVERT_YUV \
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"
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"
951 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
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"
961 " gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb\n;"
962 " gl_FragColor.a = alpha;\n"
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"
973 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
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"
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"
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"
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
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"
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
1016 static const char solid_fragment_shader[] =
1017 "precision mediump float;\n"
1018 "uniform vec4 color;\n"
1019 "uniform float alpha;\n"
1022 " gl_FragColor = alpha * color\n;"
1026 compile_shader(GLenum type, const char *source)
1032 s = glCreateShader(type);
1033 glShaderSource(s, 1, &source, NULL);
1035 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
1037 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1038 weston_log("shader info: %s\n", msg);
1046 weston_shader_init(struct weston_shader *shader,
1047 const char *vertex_source, const char *fragment_source)
1052 shader->vertex_shader =
1053 compile_shader(GL_VERTEX_SHADER, vertex_source);
1054 shader->fragment_shader =
1055 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
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");
1063 glLinkProgram(shader->program);
1064 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1066 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1067 weston_log("link info: %s\n", msg);
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");
1082 log_extensions(const char *name, const char *extensions)
1084 const char *p, *end;
1088 l = weston_log("%s:", name);
1091 end = strchrnul(p, ' ');
1094 l = weston_log_continue("\n" STAMP_SPACE "%.*s",
1097 l += weston_log_continue(" %.*s", len, p);
1098 for (p = end; isspace(*p); p++)
1101 weston_log_continue("\n");
1105 log_egl_gl_info(EGLDisplay egldpy)
1109 str = eglQueryString(egldpy, EGL_VERSION);
1110 weston_log("EGL version: %s\n", str ? str : "(null)");
1112 str = eglQueryString(egldpy, EGL_VENDOR);
1113 weston_log("EGL vendor: %s\n", str ? str : "(null)");
1115 str = eglQueryString(egldpy, EGL_CLIENT_APIS);
1116 weston_log("EGL client APIs: %s\n", str ? str : "(null)");
1118 str = eglQueryString(egldpy, EGL_EXTENSIONS);
1119 log_extensions("EGL extensions", str ? str : "(null)");
1121 str = (char *)glGetString(GL_VERSION);
1122 weston_log("GL version: %s\n", str ? str : "(null)");
1124 str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
1125 weston_log("GLSL version: %s\n", str ? str : "(null)");
1127 str = (char *)glGetString(GL_VENDOR);
1128 weston_log("GL vendor: %s\n", str ? str : "(null)");
1130 str = (char *)glGetString(GL_RENDERER);
1131 weston_log("GL renderer: %s\n", str ? str : "(null)");
1133 str = (char *)glGetString(GL_EXTENSIONS);
1134 log_extensions("GL extensions", str ? str : "(null)");
1138 log_egl_config_info(EGLDisplay egldpy, EGLConfig eglconfig)
1142 weston_log("Chosen EGL config details:\n");
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);
1151 weston_log_continue(" unknown\n");
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);
1158 weston_log_continue(" unknown\n");
1161 struct gles2_renderer {
1162 struct weston_renderer base;
1166 gles2_renderer_destroy(struct weston_compositor *ec)
1168 if (ec->has_bind_display)
1169 ec->unbind_display(ec->egl_display, ec->wl_display);
1173 gles2_renderer_init(struct weston_compositor *ec)
1175 struct gles2_renderer *renderer;
1176 const char *extensions;
1177 int has_egl_image_external = 0;
1178 struct weston_output *output;
1181 static const EGLint context_attribs[] = {
1182 EGL_CONTEXT_CLIENT_VERSION, 2,
1186 renderer = malloc(sizeof *renderer);
1187 if (renderer == NULL)
1190 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
1191 weston_log("failed to bind EGL_OPENGL_ES_API\n");
1192 print_egl_error_state();
1196 log_egl_config_info(ec->egl_display, ec->egl_config);
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();
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();
1216 log_egl_gl_info(ec->egl_display);
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");
1225 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
1226 ec->unbind_display =
1227 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
1229 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
1231 extensions = (const char *) glGetString(GL_EXTENSIONS);
1233 weston_log("Retrieving GL extension string failed.\n");
1237 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
1238 weston_log("GL_EXT_texture_format_BGRA8888 not available\n");
1242 if (strstr(extensions, "GL_EXT_read_format_bgra"))
1243 ec->read_format = GL_BGRA_EXT;
1245 ec->read_format = GL_RGBA;
1247 if (strstr(extensions, "GL_EXT_unpack_subimage"))
1248 ec->has_unpack_subimage = 1;
1250 if (strstr(extensions, "GL_OES_EGL_image_external"))
1251 has_egl_image_external = 1;
1254 (const char *) eglQueryString(ec->egl_display, EGL_EXTENSIONS);
1256 weston_log("Retrieving EGL extension string failed.\n");
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);
1265 ec->has_bind_display = 0;
1268 glActiveTexture(GL_TEXTURE0);
1270 if (weston_shader_init(&ec->texture_shader_rgba,
1271 vertex_shader, texture_fragment_shader_rgba) < 0)
1273 if (weston_shader_init(&ec->texture_shader_rgbx,
1274 vertex_shader, texture_fragment_shader_rgbx) < 0)
1276 if (has_egl_image_external &&
1277 weston_shader_init(&ec->texture_shader_egl_external,
1278 vertex_shader, texture_fragment_shader_egl_external) < 0)
1280 if (weston_shader_init(&ec->texture_shader_y_uv,
1281 vertex_shader, texture_fragment_shader_y_uv) < 0)
1283 if (weston_shader_init(&ec->texture_shader_y_u_v,
1284 vertex_shader, texture_fragment_shader_y_u_v) < 0)
1286 if (weston_shader_init(&ec->texture_shader_y_xuxv,
1287 vertex_shader, texture_fragment_shader_y_xuxv) < 0)
1289 if (weston_shader_init(&ec->solid_shader,
1290 vertex_shader, solid_fragment_shader) < 0)
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;
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");