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"
36 #include "vertex-clipping.h"
38 #include <EGL/eglext.h>
39 #include "weston-egl-ext.h"
43 GLuint vertex_shader, fragment_shader;
45 GLint tex_uniforms[3];
48 const char *vertex_source, *fragment_source;
51 #define BUFFER_DAMAGE_COUNT 2
53 enum gl_border_status {
54 BORDER_STATUS_CLEAN = 0,
55 BORDER_TOP_DIRTY = 1 << GL_RENDERER_BORDER_TOP,
56 BORDER_LEFT_DIRTY = 1 << GL_RENDERER_BORDER_LEFT,
57 BORDER_RIGHT_DIRTY = 1 << GL_RENDERER_BORDER_RIGHT,
58 BORDER_BOTTOM_DIRTY = 1 << GL_RENDERER_BORDER_BOTTOM,
59 BORDER_ALL_DIRTY = 0xf,
60 BORDER_SIZE_CHANGED = 0x10
63 struct gl_border_image {
65 int32_t width, height;
70 struct gl_output_state {
71 EGLSurface egl_surface;
72 pixman_region32_t buffer_damage[BUFFER_DAMAGE_COUNT];
73 enum gl_border_status border_damage[BUFFER_DAMAGE_COUNT];
74 struct gl_border_image borders[4];
75 enum gl_border_status border_status;
84 struct gl_surface_state {
86 struct gl_shader *shader;
90 int needs_full_upload;
91 pixman_region32_t texture_damage;
93 /* These are only used by SHM surfaces to detect when we need
94 * to do a full upload to specify a new internal texture
99 EGLImageKHR images[3];
103 struct weston_buffer_reference buffer_ref;
104 enum buffer_type buffer_type;
105 int pitch; /* in pixels */
106 int height; /* in pixels */
109 struct weston_surface *surface;
111 struct wl_listener surface_destroy_listener;
112 struct wl_listener renderer_destroy_listener;
116 struct weston_renderer base;
117 int fragment_shader_debug;
119 struct weston_binding *fragment_binding;
120 struct weston_binding *fan_binding;
122 EGLDisplay egl_display;
123 EGLContext egl_context;
124 EGLConfig egl_config;
128 struct wl_array vertices;
129 struct wl_array vtxcnt;
131 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
132 PFNEGLCREATEIMAGEKHRPROC create_image;
133 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
135 #ifdef EGL_EXT_swap_buffers_with_damage
136 PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC swap_buffers_with_damage;
139 int has_unpack_subimage;
141 PFNEGLBINDWAYLANDDISPLAYWL bind_display;
142 PFNEGLUNBINDWAYLANDDISPLAYWL unbind_display;
143 PFNEGLQUERYWAYLANDBUFFERWL query_buffer;
144 int has_bind_display;
146 int has_egl_image_external;
148 int has_egl_buffer_age;
150 int has_configless_context;
152 struct gl_shader texture_shader_rgba;
153 struct gl_shader texture_shader_rgbx;
154 struct gl_shader texture_shader_egl_external;
155 struct gl_shader texture_shader_y_uv;
156 struct gl_shader texture_shader_y_u_v;
157 struct gl_shader texture_shader_y_xuxv;
158 struct gl_shader invert_color_shader;
159 struct gl_shader solid_shader;
160 struct gl_shader *current_shader;
162 struct wl_signal destroy_signal;
165 static inline struct gl_output_state *
166 get_output_state(struct weston_output *output)
168 return (struct gl_output_state *)output->renderer_state;
172 gl_renderer_create_surface(struct weston_surface *surface);
174 static inline struct gl_surface_state *
175 get_surface_state(struct weston_surface *surface)
177 if (!surface->renderer_state)
178 gl_renderer_create_surface(surface);
180 return (struct gl_surface_state *)surface->renderer_state;
183 static inline struct gl_renderer *
184 get_renderer(struct weston_compositor *ec)
186 return (struct gl_renderer *)ec->renderer;
190 egl_error_string(EGLint code)
192 #define MYERRCODE(x) case x: return #x;
194 MYERRCODE(EGL_SUCCESS)
195 MYERRCODE(EGL_NOT_INITIALIZED)
196 MYERRCODE(EGL_BAD_ACCESS)
197 MYERRCODE(EGL_BAD_ALLOC)
198 MYERRCODE(EGL_BAD_ATTRIBUTE)
199 MYERRCODE(EGL_BAD_CONTEXT)
200 MYERRCODE(EGL_BAD_CONFIG)
201 MYERRCODE(EGL_BAD_CURRENT_SURFACE)
202 MYERRCODE(EGL_BAD_DISPLAY)
203 MYERRCODE(EGL_BAD_SURFACE)
204 MYERRCODE(EGL_BAD_MATCH)
205 MYERRCODE(EGL_BAD_PARAMETER)
206 MYERRCODE(EGL_BAD_NATIVE_PIXMAP)
207 MYERRCODE(EGL_BAD_NATIVE_WINDOW)
208 MYERRCODE(EGL_CONTEXT_LOST)
216 gl_renderer_print_egl_error_state(void)
220 code = eglGetError();
221 weston_log("EGL error state: %s (0x%04lx)\n",
222 egl_error_string(code), (long)code);
225 #define max(a, b) (((a) > (b)) ? (a) : (b))
226 #define min(a, b) (((a) > (b)) ? (b) : (a))
229 * Compute the boundary vertices of the intersection of the global coordinate
230 * aligned rectangle 'rect', and an arbitrary quadrilateral produced from
231 * 'surf_rect' when transformed from surface coordinates into global coordinates.
232 * The vertices are written to 'ex' and 'ey', and the return value is the
233 * number of vertices. Vertices are produced in clockwise winding order.
234 * Guarantees to produce either zero vertices, or 3-8 vertices with non-zero
238 calculate_edges(struct weston_view *ev, pixman_box32_t *rect,
239 pixman_box32_t *surf_rect, GLfloat *ex, GLfloat *ey)
242 struct clip_context ctx;
244 GLfloat min_x, max_x, min_y, max_y;
245 struct polygon8 surf = {
246 { surf_rect->x1, surf_rect->x2, surf_rect->x2, surf_rect->x1 },
247 { surf_rect->y1, surf_rect->y1, surf_rect->y2, surf_rect->y2 },
251 ctx.clip.x1 = rect->x1;
252 ctx.clip.y1 = rect->y1;
253 ctx.clip.x2 = rect->x2;
254 ctx.clip.y2 = rect->y2;
256 /* transform surface to screen space: */
257 for (i = 0; i < surf.n; i++)
258 weston_view_to_global_float(ev, surf.x[i], surf.y[i],
259 &surf.x[i], &surf.y[i]);
261 /* find bounding box: */
262 min_x = max_x = surf.x[0];
263 min_y = max_y = surf.y[0];
265 for (i = 1; i < surf.n; i++) {
266 min_x = min(min_x, surf.x[i]);
267 max_x = max(max_x, surf.x[i]);
268 min_y = min(min_y, surf.y[i]);
269 max_y = max(max_y, surf.y[i]);
272 /* First, simple bounding box check to discard early transformed
273 * surface rects that do not intersect with the clip region:
275 if ((min_x >= ctx.clip.x2) || (max_x <= ctx.clip.x1) ||
276 (min_y >= ctx.clip.y2) || (max_y <= ctx.clip.y1))
279 /* Simple case, bounding box edges are parallel to surface edges,
280 * there will be only four edges. We just need to clip the surface
281 * vertices to the clip rect bounds:
283 if (!ev->transform.enabled)
284 return clip_simple(&ctx, &surf, ex, ey);
286 /* Transformed case: use a general polygon clipping algorithm to
287 * clip the surface rectangle with each side of 'rect'.
288 * The algorithm is Sutherland-Hodgman, as explained in
289 * http://www.codeguru.com/cpp/misc/misc/graphics/article.php/c8965/Polygon-Clipping.htm
290 * but without looking at any of that code.
292 n = clip_transformed(&ctx, &surf, ex, ey);
301 texture_region(struct weston_view *ev, pixman_region32_t *region,
302 pixman_region32_t *surf_region)
304 struct gl_surface_state *gs = get_surface_state(ev->surface);
305 struct weston_compositor *ec = ev->surface->compositor;
306 struct gl_renderer *gr = get_renderer(ec);
307 GLfloat *v, inv_width, inv_height;
308 unsigned int *vtxcnt, nvtx = 0;
309 pixman_box32_t *rects, *surf_rects;
310 int i, j, k, nrects, nsurf;
312 rects = pixman_region32_rectangles(region, &nrects);
313 surf_rects = pixman_region32_rectangles(surf_region, &nsurf);
315 /* worst case we can have 8 vertices per rect (ie. clipped into
318 v = wl_array_add(&gr->vertices, nrects * nsurf * 8 * 4 * sizeof *v);
319 vtxcnt = wl_array_add(&gr->vtxcnt, nrects * nsurf * sizeof *vtxcnt);
321 inv_width = 1.0 / gs->pitch;
322 inv_height = 1.0 / gs->height;
324 for (i = 0; i < nrects; i++) {
325 pixman_box32_t *rect = &rects[i];
326 for (j = 0; j < nsurf; j++) {
327 pixman_box32_t *surf_rect = &surf_rects[j];
328 GLfloat sx, sy, bx, by;
329 GLfloat ex[8], ey[8]; /* edge points in screen space */
332 /* The transformed surface, after clipping to the clip region,
333 * can have as many as eight sides, emitted as a triangle-fan.
334 * The first vertex in the triangle fan can be chosen arbitrarily,
335 * since the area is guaranteed to be convex.
337 * If a corner of the transformed surface falls outside of the
338 * clip region, instead of emitting one vertex for the corner
339 * of the surface, up to two are emitted for two corresponding
340 * intersection point(s) between the surface and the clip region.
342 * To do this, we first calculate the (up to eight) points that
343 * form the intersection of the clip rect and the transformed
346 n = calculate_edges(ev, rect, surf_rect, ex, ey);
350 /* emit edge points: */
351 for (k = 0; k < n; k++) {
352 weston_view_from_global_float(ev, ex[k], ey[k],
358 weston_surface_to_buffer_float(ev->surface,
361 *(v++) = bx * inv_width;
362 if (gs->y_inverted) {
363 *(v++) = by * inv_height;
365 *(v++) = (gs->height - by) * inv_height;
377 triangle_fan_debug(struct weston_view *view, int first, int count)
379 struct weston_compositor *compositor = view->surface->compositor;
380 struct gl_renderer *gr = get_renderer(compositor);
385 static int color_idx = 0;
386 static const GLfloat color[][4] = {
387 { 1.0, 0.0, 0.0, 1.0 },
388 { 0.0, 1.0, 0.0, 1.0 },
389 { 0.0, 0.0, 1.0, 1.0 },
390 { 1.0, 1.0, 1.0, 1.0 },
393 nelems = (count - 1 + count - 2) * 2;
395 buffer = malloc(sizeof(GLushort) * nelems);
398 for (i = 1; i < count; i++) {
400 *index++ = first + i;
403 for (i = 2; i < count; i++) {
404 *index++ = first + i - 1;
405 *index++ = first + i;
408 glUseProgram(gr->solid_shader.program);
409 glUniform4fv(gr->solid_shader.color_uniform, 1,
410 color[color_idx++ % ARRAY_LENGTH(color)]);
411 glDrawElements(GL_LINES, nelems, GL_UNSIGNED_SHORT, buffer);
412 glUseProgram(gr->current_shader->program);
417 repaint_region(struct weston_view *ev, pixman_region32_t *region,
418 pixman_region32_t *surf_region)
420 struct weston_compositor *ec = ev->surface->compositor;
421 struct gl_renderer *gr = get_renderer(ec);
423 unsigned int *vtxcnt;
426 /* The final region to be painted is the intersection of
427 * 'region' and 'surf_region'. However, 'region' is in the global
428 * coordinates, and 'surf_region' is in the surface-local
429 * coordinates. texture_region() will iterate over all pairs of
430 * rectangles from both regions, compute the intersection
431 * polygon for each pair, and store it as a triangle fan if
432 * it has a non-zero area (at least 3 vertices1, actually).
434 nfans = texture_region(ev, region, surf_region);
436 v = gr->vertices.data;
437 vtxcnt = gr->vtxcnt.data;
440 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
441 glEnableVertexAttribArray(0);
444 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
445 glEnableVertexAttribArray(1);
447 for (i = 0, first = 0; i < nfans; i++) {
448 glDrawArrays(GL_TRIANGLE_FAN, first, vtxcnt[i]);
450 triangle_fan_debug(ev, first, vtxcnt[i]);
454 glDisableVertexAttribArray(1);
455 glDisableVertexAttribArray(0);
457 gr->vertices.size = 0;
462 use_output(struct weston_output *output)
465 struct gl_output_state *go = get_output_state(output);
466 struct gl_renderer *gr = get_renderer(output->compositor);
469 ret = eglMakeCurrent(gr->egl_display, go->egl_surface,
470 go->egl_surface, gr->egl_context);
472 if (ret == EGL_FALSE) {
476 weston_log("Failed to make EGL context current.\n");
477 gl_renderer_print_egl_error_state();
485 shader_init(struct gl_shader *shader, struct gl_renderer *gr,
486 const char *vertex_source, const char *fragment_source);
489 use_shader(struct gl_renderer *gr, struct gl_shader *shader)
491 if (!shader->program) {
494 ret = shader_init(shader, gr,
495 shader->vertex_source,
496 shader->fragment_source);
499 weston_log("warning: failed to compile shader\n");
502 if (gr->current_shader == shader)
504 glUseProgram(shader->program);
505 gr->current_shader = shader;
509 shader_uniforms(struct gl_shader *shader,
510 struct weston_view *view,
511 struct weston_output *output)
514 struct gl_surface_state *gs = get_surface_state(view->surface);
516 glUniformMatrix4fv(shader->proj_uniform,
517 1, GL_FALSE, output->matrix.d);
518 glUniform4fv(shader->color_uniform, 1, gs->color);
519 glUniform1f(shader->alpha_uniform, view->alpha);
521 for (i = 0; i < gs->num_textures; i++)
522 glUniform1i(shader->tex_uniforms[i], i);
526 draw_view(struct weston_view *ev, struct weston_output *output,
527 pixman_region32_t *damage) /* in global coordinates */
529 struct weston_compositor *ec = ev->surface->compositor;
530 struct gl_renderer *gr = get_renderer(ec);
531 struct gl_surface_state *gs = get_surface_state(ev->surface);
532 /* repaint bounding region in global coordinates: */
533 pixman_region32_t repaint;
534 /* non-opaque region in surface coordinates: */
535 pixman_region32_t surface_blend;
539 /* In case of a runtime switch of renderers, we may not have received
540 * an attach for this surface since the switch. In that case we don't
541 * have a valid buffer or a proper shader set up so skip rendering. */
545 pixman_region32_init(&repaint);
546 pixman_region32_intersect(&repaint,
547 &ev->transform.masked_boundingbox, damage);
548 pixman_region32_subtract(&repaint, &repaint, &ev->clip);
550 if (!pixman_region32_not_empty(&repaint))
553 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
556 use_shader(gr, &gr->solid_shader);
557 shader_uniforms(&gr->solid_shader, ev, output);
560 use_shader(gr, gs->shader);
561 shader_uniforms(gs->shader, ev, output);
563 if (ev->transform.enabled || output->zoom.active ||
564 output->current_scale != ev->surface->buffer_viewport.buffer.scale)
569 for (i = 0; i < gs->num_textures; i++) {
570 glActiveTexture(GL_TEXTURE0 + i);
571 glBindTexture(gs->target, gs->textures[i]);
572 glTexParameteri(gs->target, GL_TEXTURE_MIN_FILTER, filter);
573 glTexParameteri(gs->target, GL_TEXTURE_MAG_FILTER, filter);
576 /* blended region is whole surface minus opaque region: */
577 pixman_region32_init_rect(&surface_blend, 0, 0,
578 ev->surface->width, ev->surface->height);
579 pixman_region32_subtract(&surface_blend, &surface_blend, &ev->surface->opaque);
581 /* XXX: Should we be using ev->transform.opaque here? */
582 if (pixman_region32_not_empty(&ev->surface->opaque)) {
583 if (gs->shader == &gr->texture_shader_rgba) {
584 /* Special case for RGBA textures with possibly
585 * bad data in alpha channel: use the shader
586 * that forces texture alpha = 1.0.
587 * Xwayland surfaces need this.
589 use_shader(gr, &gr->texture_shader_rgbx);
590 shader_uniforms(&gr->texture_shader_rgbx, ev, output);
598 repaint_region(ev, &repaint, &ev->surface->opaque);
601 if (pixman_region32_not_empty(&surface_blend)) {
602 use_shader(gr, gs->shader);
604 repaint_region(ev, &repaint, &surface_blend);
607 pixman_region32_fini(&surface_blend);
610 pixman_region32_fini(&repaint);
614 gl_renderer_read_surface_pixels(struct weston_surface *es,
615 pixman_format_code_t format, void *pixels,
616 int x, int y, int width, int height)
618 struct weston_buffer *buffer = es->buffer_ref.buffer;
619 struct weston_compositor *ec = es->compositor;
620 struct gl_renderer *gr = get_renderer(ec);
621 struct gl_surface_state *gs = get_surface_state(es);
624 struct wl_shm_buffer *shm_buffer = NULL;
627 case PIXMAN_a8r8g8b8:
628 gl_format = GL_BGRA_EXT;
630 case PIXMAN_a8b8g8r8:
638 shm_buffer = wl_shm_buffer_get(buffer->resource);
641 size = buffer->width * 4 * buffer->height;
642 memcpy(pixels, wl_shm_buffer_get_data(shm_buffer), size);
645 glGenFramebuffers(1, &gr->fbo);
646 glBindFramebuffer(GL_FRAMEBUFFER, gr->fbo);
647 glFramebufferTexture2D(GL_FRAMEBUFFER,
648 GL_COLOR_ATTACHMENT0,
652 glReadPixels(x, y, width, height,
653 gl_format, GL_UNSIGNED_BYTE, pixels);
655 glBindFramebuffer(GL_FRAMEBUFFER, 0);
662 repaint_views(struct weston_output *output, pixman_region32_t *damage)
664 struct weston_compositor *compositor = output->compositor;
665 struct weston_view *view;
667 wl_list_for_each_reverse(view, &compositor->view_list, link)
668 if (view->plane == &compositor->primary_plane)
669 draw_view(view, output, damage);
673 draw_output_border_texture(struct gl_output_state *go,
674 enum gl_renderer_border_side side,
675 int32_t x, int32_t y,
676 int32_t width, int32_t height)
678 struct gl_border_image *img = &go->borders[side];
679 static GLushort indices [] = { 0, 1, 3, 3, 1, 2 };
683 glDeleteTextures(1, &img->tex);
691 glGenTextures(1, &img->tex);
692 glBindTexture(GL_TEXTURE_2D, img->tex);
694 glTexParameteri(GL_TEXTURE_2D,
695 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
696 glTexParameteri(GL_TEXTURE_2D,
697 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
698 glTexParameteri(GL_TEXTURE_2D,
699 GL_TEXTURE_MIN_FILTER, GL_NEAREST);
700 glTexParameteri(GL_TEXTURE_2D,
701 GL_TEXTURE_MAG_FILTER, GL_NEAREST);
703 glBindTexture(GL_TEXTURE_2D, img->tex);
706 if (go->border_status & (1 << side)) {
707 #ifdef GL_EXT_unpack_subimage
708 glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, 0);
709 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, 0);
710 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, 0);
712 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
713 img->tex_width, img->height, 0,
714 GL_BGRA_EXT, GL_UNSIGNED_BYTE, img->data);
717 GLfloat texcoord[] = {
719 (GLfloat)img->width / (GLfloat)img->tex_width, 0.0f,
720 (GLfloat)img->width / (GLfloat)img->tex_width, 1.0f,
727 x + width, y + height,
731 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, verts);
732 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, texcoord);
733 glEnableVertexAttribArray(0);
734 glEnableVertexAttribArray(1);
736 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
738 glDisableVertexAttribArray(1);
739 glDisableVertexAttribArray(0);
743 output_has_borders(struct weston_output *output)
745 struct gl_output_state *go = get_output_state(output);
747 return go->borders[GL_RENDERER_BORDER_TOP].data ||
748 go->borders[GL_RENDERER_BORDER_RIGHT].data ||
749 go->borders[GL_RENDERER_BORDER_BOTTOM].data ||
750 go->borders[GL_RENDERER_BORDER_LEFT].data;
754 draw_output_borders(struct weston_output *output,
755 enum gl_border_status border_status)
757 struct gl_output_state *go = get_output_state(output);
758 struct gl_renderer *gr = get_renderer(output->compositor);
759 struct gl_shader *shader = &gr->texture_shader_rgba;
760 struct gl_border_image *top, *bottom, *left, *right;
761 struct weston_matrix matrix;
762 int full_width, full_height;
764 if (border_status == BORDER_STATUS_CLEAN)
765 return; /* Clean. Nothing to do. */
767 top = &go->borders[GL_RENDERER_BORDER_TOP];
768 bottom = &go->borders[GL_RENDERER_BORDER_BOTTOM];
769 left = &go->borders[GL_RENDERER_BORDER_LEFT];
770 right = &go->borders[GL_RENDERER_BORDER_RIGHT];
772 full_width = output->current_mode->width + left->width + right->width;
773 full_height = output->current_mode->height + top->height + bottom->height;
776 use_shader(gr, shader);
778 glViewport(0, 0, full_width, full_height);
780 weston_matrix_init(&matrix);
781 weston_matrix_translate(&matrix, -full_width/2.0, -full_height/2.0, 0);
782 weston_matrix_scale(&matrix, 2.0/full_width, -2.0/full_height, 1);
783 glUniformMatrix4fv(shader->proj_uniform, 1, GL_FALSE, matrix.d);
785 glUniform1i(shader->tex_uniforms[0], 0);
786 glUniform1f(shader->alpha_uniform, 1);
787 glActiveTexture(GL_TEXTURE0);
789 if (border_status & BORDER_TOP_DIRTY)
790 draw_output_border_texture(go, GL_RENDERER_BORDER_TOP,
792 full_width, top->height);
793 if (border_status & BORDER_LEFT_DIRTY)
794 draw_output_border_texture(go, GL_RENDERER_BORDER_LEFT,
796 left->width, output->current_mode->height);
797 if (border_status & BORDER_RIGHT_DIRTY)
798 draw_output_border_texture(go, GL_RENDERER_BORDER_RIGHT,
799 full_width - right->width, top->height,
800 right->width, output->current_mode->height);
801 if (border_status & BORDER_BOTTOM_DIRTY)
802 draw_output_border_texture(go, GL_RENDERER_BORDER_BOTTOM,
803 0, full_height - bottom->height,
804 full_width, bottom->height);
808 output_get_border_damage(struct weston_output *output,
809 enum gl_border_status border_status,
810 pixman_region32_t *damage)
812 struct gl_output_state *go = get_output_state(output);
813 struct gl_border_image *top, *bottom, *left, *right;
814 int full_width, full_height;
816 if (border_status == BORDER_STATUS_CLEAN)
817 return; /* Clean. Nothing to do. */
819 top = &go->borders[GL_RENDERER_BORDER_TOP];
820 bottom = &go->borders[GL_RENDERER_BORDER_BOTTOM];
821 left = &go->borders[GL_RENDERER_BORDER_LEFT];
822 right = &go->borders[GL_RENDERER_BORDER_RIGHT];
824 full_width = output->current_mode->width + left->width + right->width;
825 full_height = output->current_mode->height + top->height + bottom->height;
826 if (border_status & BORDER_TOP_DIRTY)
827 pixman_region32_union_rect(damage, damage,
829 full_width, top->height);
830 if (border_status & BORDER_LEFT_DIRTY)
831 pixman_region32_union_rect(damage, damage,
833 left->width, output->current_mode->height);
834 if (border_status & BORDER_RIGHT_DIRTY)
835 pixman_region32_union_rect(damage, damage,
836 full_width - right->width, top->height,
837 right->width, output->current_mode->height);
838 if (border_status & BORDER_BOTTOM_DIRTY)
839 pixman_region32_union_rect(damage, damage,
840 0, full_height - bottom->height,
841 full_width, bottom->height);
845 output_get_damage(struct weston_output *output,
846 pixman_region32_t *buffer_damage, uint32_t *border_damage)
848 struct gl_output_state *go = get_output_state(output);
849 struct gl_renderer *gr = get_renderer(output->compositor);
850 EGLint buffer_age = 0;
854 if (gr->has_egl_buffer_age) {
855 ret = eglQuerySurface(gr->egl_display, go->egl_surface,
856 EGL_BUFFER_AGE_EXT, &buffer_age);
857 if (ret == EGL_FALSE) {
858 weston_log("buffer age query failed.\n");
859 gl_renderer_print_egl_error_state();
863 if (buffer_age == 0 || buffer_age - 1 > BUFFER_DAMAGE_COUNT) {
864 pixman_region32_copy(buffer_damage, &output->region);
865 *border_damage = BORDER_ALL_DIRTY;
867 for (i = 0; i < buffer_age - 1; i++)
868 *border_damage |= go->border_damage[i];
870 if (*border_damage & BORDER_SIZE_CHANGED) {
871 /* If we've had a resize, we have to do a full
873 *border_damage |= BORDER_ALL_DIRTY;
874 pixman_region32_copy(buffer_damage, &output->region);
876 for (i = 0; i < buffer_age - 1; i++)
877 pixman_region32_union(buffer_damage,
879 &go->buffer_damage[i]);
885 output_rotate_damage(struct weston_output *output,
886 pixman_region32_t *output_damage,
887 enum gl_border_status border_status)
889 struct gl_output_state *go = get_output_state(output);
890 struct gl_renderer *gr = get_renderer(output->compositor);
893 if (!gr->has_egl_buffer_age)
896 for (i = BUFFER_DAMAGE_COUNT - 1; i >= 1; i--) {
897 go->border_damage[i] = go->border_damage[i - 1];
898 pixman_region32_copy(&go->buffer_damage[i],
899 &go->buffer_damage[i - 1]);
902 go->border_damage[0] = border_status;
903 pixman_region32_copy(&go->buffer_damage[0], output_damage);
907 gl_renderer_repaint_output(struct weston_output *output,
908 pixman_region32_t *output_damage)
910 struct gl_output_state *go = get_output_state(output);
911 struct weston_compositor *compositor = output->compositor;
912 struct gl_renderer *gr = get_renderer(compositor);
915 #ifdef EGL_EXT_swap_buffers_with_damage
916 int i, nrects, buffer_height;
917 EGLint *egl_damage, *d;
918 pixman_box32_t *rects;
920 pixman_region32_t buffer_damage, total_damage;
921 enum gl_border_status border_damage = BORDER_STATUS_CLEAN;
923 /* Calculate the viewport */
924 glViewport(go->borders[GL_RENDERER_BORDER_LEFT].width,
925 go->borders[GL_RENDERER_BORDER_BOTTOM].height,
926 output->current_mode->width,
927 output->current_mode->height);
929 if (use_output(output) < 0)
932 /* if debugging, redraw everything outside the damage to clean up
933 * debug lines from the previous draw on this buffer:
936 pixman_region32_t undamaged;
937 pixman_region32_init(&undamaged);
938 pixman_region32_subtract(&undamaged, &output->region,
941 repaint_views(output, &undamaged);
943 pixman_region32_fini(&undamaged);
946 pixman_region32_init(&total_damage);
947 pixman_region32_init(&buffer_damage);
949 output_get_damage(output, &buffer_damage, &border_damage);
950 output_rotate_damage(output, output_damage, go->border_status);
952 pixman_region32_union(&total_damage, &buffer_damage, output_damage);
953 border_damage |= go->border_status;
955 repaint_views(output, &total_damage);
957 pixman_region32_fini(&total_damage);
958 pixman_region32_fini(&buffer_damage);
960 draw_output_borders(output, border_damage);
962 pixman_region32_copy(&output->previous_damage, output_damage);
963 wl_signal_emit(&output->frame_signal, output);
965 #ifdef EGL_EXT_swap_buffers_with_damage
966 if (gr->swap_buffers_with_damage) {
967 pixman_region32_init(&buffer_damage);
968 weston_transformed_region(output->width, output->height,
970 output->current_scale,
971 output_damage, &buffer_damage);
973 if (output_has_borders(output)) {
974 pixman_region32_translate(&buffer_damage,
975 go->borders[GL_RENDERER_BORDER_LEFT].width,
976 go->borders[GL_RENDERER_BORDER_TOP].height);
977 output_get_border_damage(output, go->border_status,
981 rects = pixman_region32_rectangles(&buffer_damage, &nrects);
982 egl_damage = malloc(nrects * 4 * sizeof(EGLint));
984 buffer_height = go->borders[GL_RENDERER_BORDER_TOP].height +
985 output->current_mode->height +
986 go->borders[GL_RENDERER_BORDER_BOTTOM].height;
989 for (i = 0; i < nrects; ++i) {
991 *d++ = buffer_height - rects[i].y2;
992 *d++ = rects[i].x2 - rects[i].x1;
993 *d++ = rects[i].y2 - rects[i].y1;
995 ret = gr->swap_buffers_with_damage(gr->egl_display,
999 pixman_region32_fini(&buffer_damage);
1001 ret = eglSwapBuffers(gr->egl_display, go->egl_surface);
1003 #else /* ! defined EGL_EXT_swap_buffers_with_damage */
1004 ret = eglSwapBuffers(gr->egl_display, go->egl_surface);
1007 if (ret == EGL_FALSE && !errored) {
1009 weston_log("Failed in eglSwapBuffers.\n");
1010 gl_renderer_print_egl_error_state();
1013 go->border_status = BORDER_STATUS_CLEAN;
1017 gl_renderer_read_pixels(struct weston_output *output,
1018 pixman_format_code_t format, void *pixels,
1019 uint32_t x, uint32_t y,
1020 uint32_t width, uint32_t height)
1023 struct gl_output_state *go = get_output_state(output);
1025 x += go->borders[GL_RENDERER_BORDER_LEFT].width;
1026 y += go->borders[GL_RENDERER_BORDER_BOTTOM].height;
1029 case PIXMAN_a8r8g8b8:
1030 gl_format = GL_BGRA_EXT;
1032 case PIXMAN_a8b8g8r8:
1033 gl_format = GL_RGBA;
1039 if (use_output(output) < 0)
1042 glPixelStorei(GL_PACK_ALIGNMENT, 1);
1043 glReadPixels(x, y, width, height, gl_format,
1044 GL_UNSIGNED_BYTE, pixels);
1050 gl_renderer_flush_damage(struct weston_surface *surface)
1052 struct gl_renderer *gr = get_renderer(surface->compositor);
1053 struct gl_surface_state *gs = get_surface_state(surface);
1054 struct weston_buffer *buffer = gs->buffer_ref.buffer;
1055 struct weston_view *view;
1058 #ifdef GL_EXT_unpack_subimage
1059 pixman_box32_t *rectangles;
1064 pixman_region32_union(&gs->texture_damage,
1065 &gs->texture_damage, &surface->damage);
1070 /* Avoid upload, if the texture won't be used this time.
1071 * We still accumulate the damage in texture_damage, and
1072 * hold the reference to the buffer, in case the surface
1073 * migrates back to the primary plane.
1076 wl_list_for_each(view, &surface->views, surface_link) {
1077 if (view->plane == &surface->compositor->primary_plane) {
1085 if (!pixman_region32_not_empty(&gs->texture_damage) &&
1086 !gs->needs_full_upload)
1089 glBindTexture(GL_TEXTURE_2D, gs->textures[0]);
1091 if (!gr->has_unpack_subimage) {
1092 wl_shm_buffer_begin_access(buffer->shm_buffer);
1093 glTexImage2D(GL_TEXTURE_2D, 0, gs->gl_format,
1094 gs->pitch, buffer->height, 0,
1095 gs->gl_format, gs->gl_pixel_type,
1096 wl_shm_buffer_get_data(buffer->shm_buffer));
1097 wl_shm_buffer_end_access(buffer->shm_buffer);
1102 #ifdef GL_EXT_unpack_subimage
1103 glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, gs->pitch);
1104 data = wl_shm_buffer_get_data(buffer->shm_buffer);
1106 if (gs->needs_full_upload) {
1107 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, 0);
1108 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, 0);
1109 wl_shm_buffer_begin_access(buffer->shm_buffer);
1110 glTexImage2D(GL_TEXTURE_2D, 0, gs->gl_format,
1111 gs->pitch, buffer->height, 0,
1112 gs->gl_format, gs->gl_pixel_type, data);
1113 wl_shm_buffer_end_access(buffer->shm_buffer);
1117 rectangles = pixman_region32_rectangles(&gs->texture_damage, &n);
1118 wl_shm_buffer_begin_access(buffer->shm_buffer);
1119 for (i = 0; i < n; i++) {
1122 r = weston_surface_to_buffer_rect(surface, rectangles[i]);
1124 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, r.x1);
1125 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, r.y1);
1126 glTexSubImage2D(GL_TEXTURE_2D, 0, r.x1, r.y1,
1127 r.x2 - r.x1, r.y2 - r.y1,
1128 gs->gl_format, gs->gl_pixel_type, data);
1130 wl_shm_buffer_end_access(buffer->shm_buffer);
1134 pixman_region32_fini(&gs->texture_damage);
1135 pixman_region32_init(&gs->texture_damage);
1136 gs->needs_full_upload = 0;
1138 weston_buffer_reference(&gs->buffer_ref, NULL);
1142 ensure_textures(struct gl_surface_state *gs, int num_textures)
1146 if (num_textures <= gs->num_textures)
1149 for (i = gs->num_textures; i < num_textures; i++) {
1150 glGenTextures(1, &gs->textures[i]);
1151 glBindTexture(gs->target, gs->textures[i]);
1152 glTexParameteri(gs->target,
1153 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1154 glTexParameteri(gs->target,
1155 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1157 gs->num_textures = num_textures;
1158 glBindTexture(gs->target, 0);
1162 gl_renderer_attach_shm(struct weston_surface *es, struct weston_buffer *buffer,
1163 struct wl_shm_buffer *shm_buffer)
1165 struct weston_compositor *ec = es->compositor;
1166 struct gl_renderer *gr = get_renderer(ec);
1167 struct gl_surface_state *gs = get_surface_state(es);
1168 GLenum gl_format, gl_pixel_type;
1171 buffer->shm_buffer = shm_buffer;
1172 buffer->width = wl_shm_buffer_get_width(shm_buffer);
1173 buffer->height = wl_shm_buffer_get_height(shm_buffer);
1175 switch (wl_shm_buffer_get_format(shm_buffer)) {
1176 case WL_SHM_FORMAT_XRGB8888:
1177 gs->shader = &gr->texture_shader_rgbx;
1178 pitch = wl_shm_buffer_get_stride(shm_buffer) / 4;
1179 gl_format = GL_BGRA_EXT;
1180 gl_pixel_type = GL_UNSIGNED_BYTE;
1182 case WL_SHM_FORMAT_ARGB8888:
1183 gs->shader = &gr->texture_shader_rgba;
1184 pitch = wl_shm_buffer_get_stride(shm_buffer) / 4;
1185 gl_format = GL_BGRA_EXT;
1186 gl_pixel_type = GL_UNSIGNED_BYTE;
1188 case WL_SHM_FORMAT_RGB565:
1189 gs->shader = &gr->texture_shader_rgbx;
1190 pitch = wl_shm_buffer_get_stride(shm_buffer) / 2;
1192 gl_pixel_type = GL_UNSIGNED_SHORT_5_6_5;
1195 weston_log("warning: unknown shm buffer format: %08x\n",
1196 wl_shm_buffer_get_format(shm_buffer));
1200 /* Only allocate a texture if it doesn't match existing one.
1201 * If a switch from DRM allocated buffer to a SHM buffer is
1202 * happening, we need to allocate a new texture buffer. */
1203 if (pitch != gs->pitch ||
1204 buffer->height != gs->height ||
1205 gl_format != gs->gl_format ||
1206 gl_pixel_type != gs->gl_pixel_type ||
1207 gs->buffer_type != BUFFER_TYPE_SHM) {
1209 gs->height = buffer->height;
1210 gs->target = GL_TEXTURE_2D;
1211 gs->gl_format = gl_format;
1212 gs->gl_pixel_type = gl_pixel_type;
1213 gs->buffer_type = BUFFER_TYPE_SHM;
1214 gs->needs_full_upload = 1;
1219 ensure_textures(gs, 1);
1224 gl_renderer_attach_egl(struct weston_surface *es, struct weston_buffer *buffer,
1227 struct weston_compositor *ec = es->compositor;
1228 struct gl_renderer *gr = get_renderer(ec);
1229 struct gl_surface_state *gs = get_surface_state(es);
1233 buffer->legacy_buffer = (struct wl_buffer *)buffer->resource;
1234 gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1235 EGL_WIDTH, &buffer->width);
1236 gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1237 EGL_HEIGHT, &buffer->height);
1238 gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1239 EGL_WAYLAND_Y_INVERTED_WL, &buffer->y_inverted);
1241 for (i = 0; i < gs->num_images; i++)
1242 gr->destroy_image(gr->egl_display, gs->images[i]);
1244 gs->target = GL_TEXTURE_2D;
1246 case EGL_TEXTURE_RGB:
1247 case EGL_TEXTURE_RGBA:
1250 gs->shader = &gr->texture_shader_rgba;
1252 case EGL_TEXTURE_EXTERNAL_WL:
1254 gs->target = GL_TEXTURE_EXTERNAL_OES;
1255 gs->shader = &gr->texture_shader_egl_external;
1257 case EGL_TEXTURE_Y_UV_WL:
1259 gs->shader = &gr->texture_shader_y_uv;
1261 case EGL_TEXTURE_Y_U_V_WL:
1263 gs->shader = &gr->texture_shader_y_u_v;
1265 case EGL_TEXTURE_Y_XUXV_WL:
1267 gs->shader = &gr->texture_shader_y_xuxv;
1271 ensure_textures(gs, num_planes);
1272 for (i = 0; i < num_planes; i++) {
1273 attribs[0] = EGL_WAYLAND_PLANE_WL;
1275 attribs[2] = EGL_NONE;
1276 gs->images[i] = gr->create_image(gr->egl_display,
1278 EGL_WAYLAND_BUFFER_WL,
1279 buffer->legacy_buffer,
1281 if (!gs->images[i]) {
1282 weston_log("failed to create img for plane %d\n", i);
1287 glActiveTexture(GL_TEXTURE0 + i);
1288 glBindTexture(gs->target, gs->textures[i]);
1289 gr->image_target_texture_2d(gs->target,
1293 gs->pitch = buffer->width;
1294 gs->height = buffer->height;
1295 gs->buffer_type = BUFFER_TYPE_EGL;
1296 gs->y_inverted = buffer->y_inverted;
1300 gl_renderer_attach(struct weston_surface *es, struct weston_buffer *buffer)
1302 struct weston_compositor *ec = es->compositor;
1303 struct gl_renderer *gr = get_renderer(ec);
1304 struct gl_surface_state *gs = get_surface_state(es);
1305 struct wl_shm_buffer *shm_buffer;
1309 weston_buffer_reference(&gs->buffer_ref, buffer);
1312 for (i = 0; i < gs->num_images; i++) {
1313 gr->destroy_image(gr->egl_display, gs->images[i]);
1314 gs->images[i] = NULL;
1317 glDeleteTextures(gs->num_textures, gs->textures);
1318 gs->num_textures = 0;
1319 gs->buffer_type = BUFFER_TYPE_NULL;
1324 shm_buffer = wl_shm_buffer_get(buffer->resource);
1327 gl_renderer_attach_shm(es, buffer, shm_buffer);
1328 else if (gr->query_buffer(gr->egl_display, (void *) buffer->resource,
1329 EGL_TEXTURE_FORMAT, &format))
1330 gl_renderer_attach_egl(es, buffer, format);
1332 weston_log("unhandled buffer type!\n");
1333 weston_buffer_reference(&gs->buffer_ref, NULL);
1334 gs->buffer_type = BUFFER_TYPE_NULL;
1340 gl_renderer_surface_set_color(struct weston_surface *surface,
1341 float red, float green, float blue, float alpha)
1343 struct gl_surface_state *gs = get_surface_state(surface);
1344 struct gl_renderer *gr = get_renderer(surface->compositor);
1347 gs->color[1] = green;
1348 gs->color[2] = blue;
1349 gs->color[3] = alpha;
1351 gs->shader = &gr->solid_shader;
1355 surface_state_destroy(struct gl_surface_state *gs, struct gl_renderer *gr)
1359 wl_list_remove(&gs->surface_destroy_listener.link);
1360 wl_list_remove(&gs->renderer_destroy_listener.link);
1362 gs->surface->renderer_state = NULL;
1364 glDeleteTextures(gs->num_textures, gs->textures);
1366 for (i = 0; i < gs->num_images; i++)
1367 gr->destroy_image(gr->egl_display, gs->images[i]);
1369 weston_buffer_reference(&gs->buffer_ref, NULL);
1370 pixman_region32_fini(&gs->texture_damage);
1375 surface_state_handle_surface_destroy(struct wl_listener *listener, void *data)
1377 struct gl_surface_state *gs;
1378 struct gl_renderer *gr;
1380 gs = container_of(listener, struct gl_surface_state,
1381 surface_destroy_listener);
1383 gr = get_renderer(gs->surface->compositor);
1385 surface_state_destroy(gs, gr);
1389 surface_state_handle_renderer_destroy(struct wl_listener *listener, void *data)
1391 struct gl_surface_state *gs;
1392 struct gl_renderer *gr;
1396 gs = container_of(listener, struct gl_surface_state,
1397 renderer_destroy_listener);
1399 surface_state_destroy(gs, gr);
1403 gl_renderer_create_surface(struct weston_surface *surface)
1405 struct gl_surface_state *gs;
1406 struct gl_renderer *gr = get_renderer(surface->compositor);
1408 gs = calloc(1, sizeof *gs);
1412 /* A buffer is never attached to solid color surfaces, yet
1413 * they still go through texcoord computations. Do not divide
1419 gs->surface = surface;
1421 pixman_region32_init(&gs->texture_damage);
1422 surface->renderer_state = gs;
1424 gs->surface_destroy_listener.notify =
1425 surface_state_handle_surface_destroy;
1426 wl_signal_add(&surface->destroy_signal,
1427 &gs->surface_destroy_listener);
1429 gs->renderer_destroy_listener.notify =
1430 surface_state_handle_renderer_destroy;
1431 wl_signal_add(&gr->destroy_signal,
1432 &gs->renderer_destroy_listener);
1434 if (surface->buffer_ref.buffer) {
1435 gl_renderer_attach(surface, surface->buffer_ref.buffer);
1436 gl_renderer_flush_damage(surface);
1442 static const char vertex_shader[] =
1443 "uniform mat4 proj;\n"
1444 "attribute vec2 position;\n"
1445 "attribute vec2 texcoord;\n"
1446 "varying vec2 v_texcoord;\n"
1449 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
1450 " v_texcoord = texcoord;\n"
1453 /* Declare common fragment shader uniforms */
1454 #define FRAGMENT_CONVERT_YUV \
1458 " gl_FragColor.r = y + 1.59602678 * v;\n" \
1459 " gl_FragColor.g = y - 0.39176229 * u - 0.81296764 * v;\n" \
1460 " gl_FragColor.b = y + 2.01723214 * u;\n" \
1461 " gl_FragColor.a = alpha;\n"
1463 static const char fragment_debug[] =
1464 " gl_FragColor = vec4(0.0, 0.3, 0.0, 0.2) + gl_FragColor * 0.8;\n";
1466 static const char fragment_brace[] =
1469 static const char texture_fragment_shader_rgba[] =
1470 "precision mediump float;\n"
1471 "varying vec2 v_texcoord;\n"
1472 "uniform sampler2D tex;\n"
1473 "uniform float alpha;\n"
1476 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
1479 static const char texture_fragment_shader_rgbx[] =
1480 "precision mediump float;\n"
1481 "varying vec2 v_texcoord;\n"
1482 "uniform sampler2D tex;\n"
1483 "uniform float alpha;\n"
1486 " gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb\n;"
1487 " gl_FragColor.a = alpha;\n"
1490 static const char texture_fragment_shader_egl_external[] =
1491 "#extension GL_OES_EGL_image_external : require\n"
1492 "precision mediump float;\n"
1493 "varying vec2 v_texcoord;\n"
1494 "uniform samplerExternalOES tex;\n"
1495 "uniform float alpha;\n"
1498 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
1501 static const char texture_fragment_shader_y_uv[] =
1502 "precision mediump float;\n"
1503 "uniform sampler2D tex;\n"
1504 "uniform sampler2D tex1;\n"
1505 "varying vec2 v_texcoord;\n"
1506 "uniform float alpha;\n"
1508 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1509 " float u = texture2D(tex1, v_texcoord).r - 0.5;\n"
1510 " float v = texture2D(tex1, v_texcoord).g - 0.5;\n"
1511 FRAGMENT_CONVERT_YUV
1514 static const char texture_fragment_shader_y_u_v[] =
1515 "precision mediump float;\n"
1516 "uniform sampler2D tex;\n"
1517 "uniform sampler2D tex1;\n"
1518 "uniform sampler2D tex2;\n"
1519 "varying vec2 v_texcoord;\n"
1520 "uniform float alpha;\n"
1522 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1523 " float u = texture2D(tex1, v_texcoord).x - 0.5;\n"
1524 " float v = texture2D(tex2, v_texcoord).x - 0.5;\n"
1525 FRAGMENT_CONVERT_YUV
1528 static const char texture_fragment_shader_y_xuxv[] =
1529 "precision mediump float;\n"
1530 "uniform sampler2D tex;\n"
1531 "uniform sampler2D tex1;\n"
1532 "varying vec2 v_texcoord;\n"
1533 "uniform float alpha;\n"
1535 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1536 " float u = texture2D(tex1, v_texcoord).g - 0.5;\n"
1537 " float v = texture2D(tex1, v_texcoord).a - 0.5;\n"
1538 FRAGMENT_CONVERT_YUV
1541 static const char solid_fragment_shader[] =
1542 "precision mediump float;\n"
1543 "uniform vec4 color;\n"
1544 "uniform float alpha;\n"
1547 " gl_FragColor = alpha * color\n;"
1551 compile_shader(GLenum type, int count, const char **sources)
1557 s = glCreateShader(type);
1558 glShaderSource(s, count, sources, NULL);
1560 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
1562 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1563 weston_log("shader info: %s\n", msg);
1571 shader_init(struct gl_shader *shader, struct gl_renderer *renderer,
1572 const char *vertex_source, const char *fragment_source)
1577 const char *sources[3];
1579 shader->vertex_shader =
1580 compile_shader(GL_VERTEX_SHADER, 1, &vertex_source);
1582 if (renderer->fragment_shader_debug) {
1583 sources[0] = fragment_source;
1584 sources[1] = fragment_debug;
1585 sources[2] = fragment_brace;
1588 sources[0] = fragment_source;
1589 sources[1] = fragment_brace;
1593 shader->fragment_shader =
1594 compile_shader(GL_FRAGMENT_SHADER, count, sources);
1596 shader->program = glCreateProgram();
1597 glAttachShader(shader->program, shader->vertex_shader);
1598 glAttachShader(shader->program, shader->fragment_shader);
1599 glBindAttribLocation(shader->program, 0, "position");
1600 glBindAttribLocation(shader->program, 1, "texcoord");
1602 glLinkProgram(shader->program);
1603 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1605 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1606 weston_log("link info: %s\n", msg);
1610 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1611 shader->tex_uniforms[0] = glGetUniformLocation(shader->program, "tex");
1612 shader->tex_uniforms[1] = glGetUniformLocation(shader->program, "tex1");
1613 shader->tex_uniforms[2] = glGetUniformLocation(shader->program, "tex2");
1614 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
1615 shader->color_uniform = glGetUniformLocation(shader->program, "color");
1621 shader_release(struct gl_shader *shader)
1623 glDeleteShader(shader->vertex_shader);
1624 glDeleteShader(shader->fragment_shader);
1625 glDeleteProgram(shader->program);
1627 shader->vertex_shader = 0;
1628 shader->fragment_shader = 0;
1629 shader->program = 0;
1633 log_extensions(const char *name, const char *extensions)
1635 const char *p, *end;
1639 l = weston_log("%s:", name);
1642 end = strchrnul(p, ' ');
1645 l = weston_log_continue("\n" STAMP_SPACE "%.*s",
1648 l += weston_log_continue(" %.*s", len, p);
1649 for (p = end; isspace(*p); p++)
1652 weston_log_continue("\n");
1656 log_egl_gl_info(EGLDisplay egldpy)
1660 str = eglQueryString(egldpy, EGL_VERSION);
1661 weston_log("EGL version: %s\n", str ? str : "(null)");
1663 str = eglQueryString(egldpy, EGL_VENDOR);
1664 weston_log("EGL vendor: %s\n", str ? str : "(null)");
1666 str = eglQueryString(egldpy, EGL_CLIENT_APIS);
1667 weston_log("EGL client APIs: %s\n", str ? str : "(null)");
1669 str = eglQueryString(egldpy, EGL_EXTENSIONS);
1670 log_extensions("EGL extensions", str ? str : "(null)");
1672 str = (char *)glGetString(GL_VERSION);
1673 weston_log("GL version: %s\n", str ? str : "(null)");
1675 str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
1676 weston_log("GLSL version: %s\n", str ? str : "(null)");
1678 str = (char *)glGetString(GL_VENDOR);
1679 weston_log("GL vendor: %s\n", str ? str : "(null)");
1681 str = (char *)glGetString(GL_RENDERER);
1682 weston_log("GL renderer: %s\n", str ? str : "(null)");
1684 str = (char *)glGetString(GL_EXTENSIONS);
1685 log_extensions("GL extensions", str ? str : "(null)");
1689 log_egl_config_info(EGLDisplay egldpy, EGLConfig eglconfig)
1693 weston_log("Chosen EGL config details:\n");
1695 weston_log_continue(STAMP_SPACE "RGBA bits");
1696 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_RED_SIZE, &r) &&
1697 eglGetConfigAttrib(egldpy, eglconfig, EGL_GREEN_SIZE, &g) &&
1698 eglGetConfigAttrib(egldpy, eglconfig, EGL_BLUE_SIZE, &b) &&
1699 eglGetConfigAttrib(egldpy, eglconfig, EGL_ALPHA_SIZE, &a))
1700 weston_log_continue(": %d %d %d %d\n", r, g, b, a);
1702 weston_log_continue(" unknown\n");
1704 weston_log_continue(STAMP_SPACE "swap interval range");
1705 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_MIN_SWAP_INTERVAL, &a) &&
1706 eglGetConfigAttrib(egldpy, eglconfig, EGL_MAX_SWAP_INTERVAL, &b))
1707 weston_log_continue(": %d - %d\n", a, b);
1709 weston_log_continue(" unknown\n");
1713 egl_choose_config(struct gl_renderer *gr, const EGLint *attribs,
1714 const EGLint *visual_id,
1715 EGLConfig *config_out)
1722 if (!eglGetConfigs(gr->egl_display, NULL, 0, &count) || count < 1)
1725 configs = calloc(count, sizeof *configs);
1729 if (!eglChooseConfig(gr->egl_display, attribs, configs,
1733 for (i = 0; i < matched; ++i) {
1737 if (!eglGetConfigAttrib(gr->egl_display,
1738 configs[i], EGL_NATIVE_VISUAL_ID,
1742 if (id != 0 && id != *visual_id)
1746 *config_out = configs[i];
1758 gl_renderer_output_set_border(struct weston_output *output,
1759 enum gl_renderer_border_side side,
1760 int32_t width, int32_t height,
1761 int32_t tex_width, unsigned char *data)
1763 struct gl_output_state *go = get_output_state(output);
1765 if (go->borders[side].width != width ||
1766 go->borders[side].height != height)
1767 /* In this case, we have to blow everything and do a full
1769 go->border_status |= BORDER_SIZE_CHANGED | BORDER_ALL_DIRTY;
1776 go->borders[side].width = width;
1777 go->borders[side].height = height;
1778 go->borders[side].tex_width = tex_width;
1779 go->borders[side].data = data;
1780 go->border_status |= 1 << side;
1784 gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface);
1787 gl_renderer_output_create(struct weston_output *output,
1788 EGLNativeWindowType window,
1789 const EGLint *attribs,
1790 const EGLint *visual_id)
1792 struct weston_compositor *ec = output->compositor;
1793 struct gl_renderer *gr = get_renderer(ec);
1794 struct gl_output_state *go;
1795 EGLConfig egl_config;
1798 if (egl_choose_config(gr, attribs, visual_id, &egl_config) == -1) {
1799 weston_log("failed to choose EGL config for output\n");
1803 if (egl_config != gr->egl_config &&
1804 !gr->has_configless_context) {
1805 weston_log("attempted to use a different EGL config for an "
1806 "output but EGL_MESA_configless_context is not "
1811 go = calloc(1, sizeof *go);
1817 eglCreateWindowSurface(gr->egl_display,
1821 if (go->egl_surface == EGL_NO_SURFACE) {
1822 weston_log("failed to create egl surface\n");
1827 if (gr->egl_context == NULL)
1828 if (gl_renderer_setup(ec, go->egl_surface) < 0) {
1833 for (i = 0; i < BUFFER_DAMAGE_COUNT; i++)
1834 pixman_region32_init(&go->buffer_damage[i]);
1836 output->renderer_state = go;
1838 log_egl_config_info(gr->egl_display, egl_config);
1844 gl_renderer_output_destroy(struct weston_output *output)
1846 struct gl_renderer *gr = get_renderer(output->compositor);
1847 struct gl_output_state *go = get_output_state(output);
1850 for (i = 0; i < 2; i++)
1851 pixman_region32_fini(&go->buffer_damage[i]);
1853 eglDestroySurface(gr->egl_display, go->egl_surface);
1859 gl_renderer_output_surface(struct weston_output *output)
1861 return get_output_state(output)->egl_surface;
1865 gl_renderer_destroy(struct weston_compositor *ec)
1867 struct gl_renderer *gr = get_renderer(ec);
1869 wl_signal_emit(&gr->destroy_signal, gr);
1872 glDeleteFramebuffers(1, &gr->fbo);
1874 if (gr->has_bind_display)
1875 gr->unbind_display(gr->egl_display, ec->wl_display);
1877 /* Work around crash in egl_dri2.c's dri2_make_current() - when does this apply? */
1878 eglMakeCurrent(gr->egl_display,
1879 EGL_NO_SURFACE, EGL_NO_SURFACE,
1882 eglTerminate(gr->egl_display);
1885 wl_array_release(&gr->vertices);
1886 wl_array_release(&gr->vtxcnt);
1888 if (gr->fragment_binding)
1889 weston_binding_destroy(gr->fragment_binding);
1890 if (gr->fan_binding)
1891 weston_binding_destroy(gr->fan_binding);
1897 gl_renderer_setup_egl_extensions(struct weston_compositor *ec)
1899 struct gl_renderer *gr = get_renderer(ec);
1900 const char *extensions;
1903 gr->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1904 gr->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1906 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
1907 gr->unbind_display =
1908 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
1910 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
1913 (const char *) eglQueryString(gr->egl_display, EGL_EXTENSIONS);
1915 weston_log("Retrieving EGL extension string failed.\n");
1919 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
1920 gr->has_bind_display = 1;
1921 if (gr->has_bind_display) {
1922 ret = gr->bind_display(gr->egl_display, ec->wl_display);
1924 gr->has_bind_display = 0;
1927 if (strstr(extensions, "EGL_EXT_buffer_age"))
1928 gr->has_egl_buffer_age = 1;
1930 weston_log("warning: EGL_EXT_buffer_age not supported. "
1931 "Performance could be affected.\n");
1933 #ifdef EGL_EXT_swap_buffers_with_damage
1934 if (strstr(extensions, "EGL_EXT_swap_buffers_with_damage"))
1935 gr->swap_buffers_with_damage =
1936 (void *) eglGetProcAddress("eglSwapBuffersWithDamageEXT");
1938 weston_log("warning: EGL_EXT_swap_buffers_with_damage not "
1939 "supported. Performance could be affected.\n");
1942 #ifdef EGL_MESA_configless_context
1943 if (strstr(extensions, "EGL_MESA_configless_context"))
1944 gr->has_configless_context = 1;
1950 static const EGLint gl_renderer_opaque_attribs[] = {
1951 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1956 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1960 static const EGLint gl_renderer_alpha_attribs[] = {
1961 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1966 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1971 gl_renderer_create(struct weston_compositor *ec, EGLNativeDisplayType display,
1972 const EGLint *attribs, const EGLint *visual_id)
1974 struct gl_renderer *gr;
1975 EGLint major, minor;
1977 gr = calloc(1, sizeof *gr);
1982 gr->base.read_pixels = gl_renderer_read_pixels;
1983 gr->base.read_surface_pixels = gl_renderer_read_surface_pixels;
1984 gr->base.repaint_output = gl_renderer_repaint_output;
1985 gr->base.flush_damage = gl_renderer_flush_damage;
1986 gr->base.attach = gl_renderer_attach;
1987 gr->base.surface_set_color = gl_renderer_surface_set_color;
1988 gr->base.destroy = gl_renderer_destroy;
1990 gr->egl_display = eglGetDisplay(display);
1991 if (gr->egl_display == EGL_NO_DISPLAY) {
1992 weston_log("failed to create display\n");
1996 if (!eglInitialize(gr->egl_display, &major, &minor)) {
1997 weston_log("failed to initialize display\n");
2001 if (egl_choose_config(gr, attribs, visual_id, &gr->egl_config) < 0) {
2002 weston_log("failed to choose EGL config\n");
2006 ec->renderer = &gr->base;
2007 ec->capabilities |= WESTON_CAP_ROTATION_ANY;
2008 ec->capabilities |= WESTON_CAP_CAPTURE_YFLIP;
2010 if (gl_renderer_setup_egl_extensions(ec) < 0)
2013 wl_display_add_shm_format(ec->wl_display, WL_SHM_FORMAT_RGB565);
2015 wl_signal_init(&gr->destroy_signal);
2020 gl_renderer_print_egl_error_state();
2026 gl_renderer_display(struct weston_compositor *ec)
2028 return get_renderer(ec)->egl_display;
2032 compile_shaders(struct weston_compositor *ec)
2034 struct gl_renderer *gr = get_renderer(ec);
2036 gr->texture_shader_rgba.vertex_source = vertex_shader;
2037 gr->texture_shader_rgba.fragment_source = texture_fragment_shader_rgba;
2039 gr->texture_shader_rgbx.vertex_source = vertex_shader;
2040 gr->texture_shader_rgbx.fragment_source = texture_fragment_shader_rgbx;
2042 gr->texture_shader_egl_external.vertex_source = vertex_shader;
2043 gr->texture_shader_egl_external.fragment_source =
2044 texture_fragment_shader_egl_external;
2046 gr->texture_shader_y_uv.vertex_source = vertex_shader;
2047 gr->texture_shader_y_uv.fragment_source = texture_fragment_shader_y_uv;
2049 gr->texture_shader_y_u_v.vertex_source = vertex_shader;
2050 gr->texture_shader_y_u_v.fragment_source =
2051 texture_fragment_shader_y_u_v;
2053 gr->texture_shader_y_xuxv.vertex_source = vertex_shader;
2054 gr->texture_shader_y_xuxv.fragment_source =
2055 texture_fragment_shader_y_xuxv;
2057 gr->solid_shader.vertex_source = vertex_shader;
2058 gr->solid_shader.fragment_source = solid_fragment_shader;
2064 fragment_debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
2067 struct weston_compositor *ec = data;
2068 struct gl_renderer *gr = get_renderer(ec);
2069 struct weston_output *output;
2071 gr->fragment_shader_debug ^= 1;
2073 shader_release(&gr->texture_shader_rgba);
2074 shader_release(&gr->texture_shader_rgbx);
2075 shader_release(&gr->texture_shader_egl_external);
2076 shader_release(&gr->texture_shader_y_uv);
2077 shader_release(&gr->texture_shader_y_u_v);
2078 shader_release(&gr->texture_shader_y_xuxv);
2079 shader_release(&gr->solid_shader);
2081 /* Force use_shader() to call glUseProgram(), since we need to use
2082 * the recompiled version of the shader. */
2083 gr->current_shader = NULL;
2085 wl_list_for_each(output, &ec->output_list, link)
2086 weston_output_damage(output);
2090 fan_debug_repaint_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
2093 struct weston_compositor *compositor = data;
2094 struct gl_renderer *gr = get_renderer(compositor);
2096 gr->fan_debug = !gr->fan_debug;
2097 weston_compositor_damage_all(compositor);
2101 gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface)
2103 struct gl_renderer *gr = get_renderer(ec);
2104 const char *extensions;
2105 EGLConfig context_config;
2108 static const EGLint context_attribs[] = {
2109 EGL_CONTEXT_CLIENT_VERSION, 2,
2113 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
2114 weston_log("failed to bind EGL_OPENGL_ES_API\n");
2115 gl_renderer_print_egl_error_state();
2119 context_config = gr->egl_config;
2121 #ifdef EGL_MESA_configless_context
2122 if (gr->has_configless_context)
2123 context_config = EGL_NO_CONFIG_MESA;
2126 gr->egl_context = eglCreateContext(gr->egl_display, context_config,
2127 EGL_NO_CONTEXT, context_attribs);
2128 if (gr->egl_context == NULL) {
2129 weston_log("failed to create context\n");
2130 gl_renderer_print_egl_error_state();
2134 ret = eglMakeCurrent(gr->egl_display, egl_surface,
2135 egl_surface, gr->egl_context);
2136 if (ret == EGL_FALSE) {
2137 weston_log("Failed to make EGL context current.\n");
2138 gl_renderer_print_egl_error_state();
2142 log_egl_gl_info(gr->egl_display);
2144 gr->image_target_texture_2d =
2145 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2147 extensions = (const char *) glGetString(GL_EXTENSIONS);
2149 weston_log("Retrieving GL extension string failed.\n");
2153 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
2154 weston_log("GL_EXT_texture_format_BGRA8888 not available\n");
2158 if (strstr(extensions, "GL_EXT_read_format_bgra"))
2159 ec->read_format = PIXMAN_a8r8g8b8;
2161 ec->read_format = PIXMAN_a8b8g8r8;
2163 #ifdef GL_EXT_unpack_subimage
2164 if (strstr(extensions, "GL_EXT_unpack_subimage"))
2165 gr->has_unpack_subimage = 1;
2168 if (strstr(extensions, "GL_OES_EGL_image_external"))
2169 gr->has_egl_image_external = 1;
2171 glActiveTexture(GL_TEXTURE0);
2173 if (compile_shaders(ec))
2176 gr->fragment_binding =
2177 weston_compositor_add_debug_binding(ec, KEY_S,
2178 fragment_debug_binding,
2181 weston_compositor_add_debug_binding(ec, KEY_F,
2182 fan_debug_repaint_binding,
2185 weston_log("GL ES 2 renderer features:\n");
2186 weston_log_continue(STAMP_SPACE "read-back format: %s\n",
2187 ec->read_format == PIXMAN_a8r8g8b8 ? "BGRA" : "RGBA");
2188 weston_log_continue(STAMP_SPACE "wl_shm sub-image to texture: %s\n",
2189 gr->has_unpack_subimage ? "yes" : "no");
2190 weston_log_continue(STAMP_SPACE "EGL Wayland extension: %s\n",
2191 gr->has_bind_display ? "yes" : "no");
2197 WL_EXPORT struct gl_renderer_interface gl_renderer_interface = {
2198 .opaque_attribs = gl_renderer_opaque_attribs,
2199 .alpha_attribs = gl_renderer_alpha_attribs,
2201 .create = gl_renderer_create,
2202 .display = gl_renderer_display,
2203 .output_create = gl_renderer_output_create,
2204 .output_destroy = gl_renderer_output_destroy,
2205 .output_surface = gl_renderer_output_surface,
2206 .output_set_border = gl_renderer_output_set_border,
2207 .print_egl_error_state = gl_renderer_print_egl_error_state