blob: d03bce6497aeb3d9c9c16318243b9818e8dc497c [file] [log] [blame]
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001/*
2 * Copyright © 2012 Intel Corporation
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 */
22
Daniel Stonec228e232013-05-22 18:03:19 +030023#include "config.h"
Kristian Høgsberg25894fc2012-09-05 22:06:26 -040024
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010025#include <GLES2/gl2.h>
26#include <GLES2/gl2ext.h>
27
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -040028#include <stdlib.h>
Kristian Høgsberg25894fc2012-09-05 22:06:26 -040029#include <string.h>
30#include <ctype.h>
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +030031#include <float.h>
32#include <assert.h>
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +020033#include <linux/input.h>
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -040034
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010035#include "gl-renderer.h"
Sam Spilsbury619859c2013-09-13 10:01:21 +080036#include "vertex-clipping.h"
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010037
38#include <EGL/eglext.h>
39#include "weston-egl-ext.h"
Kristian Høgsbergd7c17262012-09-05 21:54:15 -040040
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010041struct gl_shader {
John Kåre Alsaker40684142012-11-13 19:10:25 +010042 GLuint program;
43 GLuint vertex_shader, fragment_shader;
44 GLint proj_uniform;
45 GLint tex_uniforms[3];
46 GLint alpha_uniform;
47 GLint color_uniform;
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +030048 const char *vertex_source, *fragment_source;
John Kåre Alsaker40684142012-11-13 19:10:25 +010049};
50
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +020051#define BUFFER_DAMAGE_COUNT 2
52
Jason Ekstrande5512d42014-02-04 21:36:38 -060053enum 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
61};
62
Jason Ekstrand0b61bf42013-10-27 22:24:54 -050063struct gl_border_image {
64 GLuint tex;
65 int32_t width, height;
66 int32_t tex_width;
Jason Ekstrand0b61bf42013-10-27 22:24:54 -050067 void *data;
68};
69
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010070struct gl_output_state {
John Kåre Alsaker94659272012-11-13 19:10:18 +010071 EGLSurface egl_surface;
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +020072 pixman_region32_t buffer_damage[BUFFER_DAMAGE_COUNT];
Jason Ekstrande5512d42014-02-04 21:36:38 -060073 enum gl_border_status border_damage[BUFFER_DAMAGE_COUNT];
Jason Ekstrand0b61bf42013-10-27 22:24:54 -050074 struct gl_border_image borders[4];
Jason Ekstrande5512d42014-02-04 21:36:38 -060075 enum gl_border_status border_status;
John Kåre Alsaker94659272012-11-13 19:10:18 +010076};
77
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +030078enum buffer_type {
79 BUFFER_TYPE_NULL,
80 BUFFER_TYPE_SHM,
81 BUFFER_TYPE_EGL
82};
83
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010084struct gl_surface_state {
John Kåre Alsaker878f4492012-11-13 19:10:23 +010085 GLfloat color[4];
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010086 struct gl_shader *shader;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +010087
88 GLuint textures[3];
89 int num_textures;
Ander Conselvan de Oliveira6be5f432013-06-07 16:52:45 +030090 int needs_full_upload;
Pekka Paalanen81ee3f52012-12-04 15:58:16 +020091 pixman_region32_t texture_damage;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +010092
93 EGLImageKHR images[3];
94 GLenum target;
95 int num_images;
Pekka Paalanenfb003d32012-12-04 15:58:13 +020096
97 struct weston_buffer_reference buffer_ref;
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +030098 enum buffer_type buffer_type;
Pekka Paalanen68033ac2012-12-04 15:58:15 +020099 int pitch; /* in pixels */
Alexander Larsson4ea95522013-05-22 14:41:37 +0200100 int height; /* in pixels */
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +0400101 int y_inverted;
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300102
103 struct weston_surface *surface;
104
105 struct wl_listener surface_destroy_listener;
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +0300106 struct wl_listener renderer_destroy_listener;
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100107};
108
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100109struct gl_renderer {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100110 struct weston_renderer base;
111 int fragment_shader_debug;
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400112 int fan_debug;
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +0300113 struct weston_binding *fragment_binding;
114 struct weston_binding *fan_binding;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100115
116 EGLDisplay egl_display;
117 EGLContext egl_context;
118 EGLConfig egl_config;
John Kåre Alsaker44154502012-11-13 19:10:20 +0100119
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400120 struct wl_array vertices;
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400121 struct wl_array vtxcnt;
122
John Kåre Alsaker320711d2012-11-13 19:10:27 +0100123 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
124 PFNEGLCREATEIMAGEKHRPROC create_image;
125 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
126
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -0600127#ifdef EGL_EXT_swap_buffers_with_damage
128 PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC swap_buffers_with_damage;
129#endif
130
John Kåre Alsaker320711d2012-11-13 19:10:27 +0100131 int has_unpack_subimage;
132
133 PFNEGLBINDWAYLANDDISPLAYWL bind_display;
134 PFNEGLUNBINDWAYLANDDISPLAYWL unbind_display;
135 PFNEGLQUERYWAYLANDBUFFERWL query_buffer;
136 int has_bind_display;
137
138 int has_egl_image_external;
139
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +0200140 int has_egl_buffer_age;
141
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100142 struct gl_shader texture_shader_rgba;
143 struct gl_shader texture_shader_rgbx;
144 struct gl_shader texture_shader_egl_external;
145 struct gl_shader texture_shader_y_uv;
146 struct gl_shader texture_shader_y_u_v;
147 struct gl_shader texture_shader_y_xuxv;
148 struct gl_shader invert_color_shader;
149 struct gl_shader solid_shader;
150 struct gl_shader *current_shader;
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +0300151
152 struct wl_signal destroy_signal;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100153};
John Kåre Alsaker94659272012-11-13 19:10:18 +0100154
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100155static inline struct gl_output_state *
John Kåre Alsaker94659272012-11-13 19:10:18 +0100156get_output_state(struct weston_output *output)
157{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100158 return (struct gl_output_state *)output->renderer_state;
John Kåre Alsaker94659272012-11-13 19:10:18 +0100159}
160
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300161static int
162gl_renderer_create_surface(struct weston_surface *surface);
163
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100164static inline struct gl_surface_state *
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100165get_surface_state(struct weston_surface *surface)
166{
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300167 if (!surface->renderer_state)
168 gl_renderer_create_surface(surface);
169
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100170 return (struct gl_surface_state *)surface->renderer_state;
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100171}
172
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100173static inline struct gl_renderer *
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100174get_renderer(struct weston_compositor *ec)
175{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100176 return (struct gl_renderer *)ec->renderer;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100177}
178
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400179static const char *
180egl_error_string(EGLint code)
181{
182#define MYERRCODE(x) case x: return #x;
183 switch (code) {
184 MYERRCODE(EGL_SUCCESS)
185 MYERRCODE(EGL_NOT_INITIALIZED)
186 MYERRCODE(EGL_BAD_ACCESS)
187 MYERRCODE(EGL_BAD_ALLOC)
188 MYERRCODE(EGL_BAD_ATTRIBUTE)
189 MYERRCODE(EGL_BAD_CONTEXT)
190 MYERRCODE(EGL_BAD_CONFIG)
191 MYERRCODE(EGL_BAD_CURRENT_SURFACE)
192 MYERRCODE(EGL_BAD_DISPLAY)
193 MYERRCODE(EGL_BAD_SURFACE)
194 MYERRCODE(EGL_BAD_MATCH)
195 MYERRCODE(EGL_BAD_PARAMETER)
196 MYERRCODE(EGL_BAD_NATIVE_PIXMAP)
197 MYERRCODE(EGL_BAD_NATIVE_WINDOW)
198 MYERRCODE(EGL_CONTEXT_LOST)
199 default:
200 return "unknown";
201 }
202#undef MYERRCODE
203}
204
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300205static void
Pekka Paalanen326529f2012-11-27 12:25:25 +0200206gl_renderer_print_egl_error_state(void)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400207{
208 EGLint code;
209
210 code = eglGetError();
211 weston_log("EGL error state: %s (0x%04lx)\n",
212 egl_error_string(code), (long)code);
213}
214
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400215#define max(a, b) (((a) > (b)) ? (a) : (b))
216#define min(a, b) (((a) > (b)) ? (b) : (a))
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400217
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300218/*
219 * Compute the boundary vertices of the intersection of the global coordinate
220 * aligned rectangle 'rect', and an arbitrary quadrilateral produced from
221 * 'surf_rect' when transformed from surface coordinates into global coordinates.
222 * The vertices are written to 'ex' and 'ey', and the return value is the
223 * number of vertices. Vertices are produced in clockwise winding order.
224 * Guarantees to produce either zero vertices, or 3-8 vertices with non-zero
225 * polygon area.
226 */
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400227static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500228calculate_edges(struct weston_view *ev, pixman_box32_t *rect,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400229 pixman_box32_t *surf_rect, GLfloat *ex, GLfloat *ey)
230{
Sam Spilsbury619859c2013-09-13 10:01:21 +0800231
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300232 struct clip_context ctx;
233 int i, n;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400234 GLfloat min_x, max_x, min_y, max_y;
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300235 struct polygon8 surf = {
236 { surf_rect->x1, surf_rect->x2, surf_rect->x2, surf_rect->x1 },
237 { surf_rect->y1, surf_rect->y1, surf_rect->y2, surf_rect->y2 },
238 4
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400239 };
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400240
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300241 ctx.clip.x1 = rect->x1;
242 ctx.clip.y1 = rect->y1;
243 ctx.clip.x2 = rect->x2;
244 ctx.clip.y2 = rect->y2;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400245
246 /* transform surface to screen space: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300247 for (i = 0; i < surf.n; i++)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500248 weston_view_to_global_float(ev, surf.x[i], surf.y[i],
249 &surf.x[i], &surf.y[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400250
251 /* find bounding box: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300252 min_x = max_x = surf.x[0];
253 min_y = max_y = surf.y[0];
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400254
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300255 for (i = 1; i < surf.n; i++) {
256 min_x = min(min_x, surf.x[i]);
257 max_x = max(max_x, surf.x[i]);
258 min_y = min(min_y, surf.y[i]);
259 max_y = max(max_y, surf.y[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400260 }
261
262 /* First, simple bounding box check to discard early transformed
263 * surface rects that do not intersect with the clip region:
264 */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300265 if ((min_x >= ctx.clip.x2) || (max_x <= ctx.clip.x1) ||
266 (min_y >= ctx.clip.y2) || (max_y <= ctx.clip.y1))
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400267 return 0;
268
269 /* Simple case, bounding box edges are parallel to surface edges,
270 * there will be only four edges. We just need to clip the surface
271 * vertices to the clip rect bounds:
272 */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500273 if (!ev->transform.enabled)
Sam Spilsbury619859c2013-09-13 10:01:21 +0800274 return clip_simple(&ctx, &surf, ex, ey);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400275
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300276 /* Transformed case: use a general polygon clipping algorithm to
277 * clip the surface rectangle with each side of 'rect'.
278 * The algorithm is Sutherland-Hodgman, as explained in
279 * http://www.codeguru.com/cpp/misc/misc/graphics/article.php/c8965/Polygon-Clipping.htm
280 * but without looking at any of that code.
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400281 */
Sam Spilsbury619859c2013-09-13 10:01:21 +0800282 n = clip_transformed(&ctx, &surf, ex, ey);
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300283
284 if (n < 3)
285 return 0;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400286
287 return n;
288}
289
290static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500291texture_region(struct weston_view *ev, pixman_region32_t *region,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400292 pixman_region32_t *surf_region)
293{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500294 struct gl_surface_state *gs = get_surface_state(ev->surface);
295 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400296 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400297 GLfloat *v, inv_width, inv_height;
298 unsigned int *vtxcnt, nvtx = 0;
299 pixman_box32_t *rects, *surf_rects;
300 int i, j, k, nrects, nsurf;
301
302 rects = pixman_region32_rectangles(region, &nrects);
303 surf_rects = pixman_region32_rectangles(surf_region, &nsurf);
304
305 /* worst case we can have 8 vertices per rect (ie. clipped into
306 * an octagon):
307 */
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400308 v = wl_array_add(&gr->vertices, nrects * nsurf * 8 * 4 * sizeof *v);
309 vtxcnt = wl_array_add(&gr->vtxcnt, nrects * nsurf * sizeof *vtxcnt);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400310
Pekka Paalanen68033ac2012-12-04 15:58:15 +0200311 inv_width = 1.0 / gs->pitch;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200312 inv_height = 1.0 / gs->height;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400313
314 for (i = 0; i < nrects; i++) {
315 pixman_box32_t *rect = &rects[i];
316 for (j = 0; j < nsurf; j++) {
317 pixman_box32_t *surf_rect = &surf_rects[j];
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200318 GLfloat sx, sy, bx, by;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400319 GLfloat ex[8], ey[8]; /* edge points in screen space */
320 int n;
321
322 /* The transformed surface, after clipping to the clip region,
323 * can have as many as eight sides, emitted as a triangle-fan.
324 * The first vertex in the triangle fan can be chosen arbitrarily,
325 * since the area is guaranteed to be convex.
326 *
327 * If a corner of the transformed surface falls outside of the
328 * clip region, instead of emitting one vertex for the corner
329 * of the surface, up to two are emitted for two corresponding
330 * intersection point(s) between the surface and the clip region.
331 *
332 * To do this, we first calculate the (up to eight) points that
333 * form the intersection of the clip rect and the transformed
334 * surface.
335 */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500336 n = calculate_edges(ev, rect, surf_rect, ex, ey);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400337 if (n < 3)
338 continue;
339
340 /* emit edge points: */
341 for (k = 0; k < n; k++) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500342 weston_view_from_global_float(ev, ex[k], ey[k],
343 &sx, &sy);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400344 /* position: */
345 *(v++) = ex[k];
346 *(v++) = ey[k];
347 /* texcoord: */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500348 weston_surface_to_buffer_float(ev->surface,
349 sx, sy,
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200350 &bx, &by);
351 *(v++) = bx * inv_width;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +0400352 if (gs->y_inverted) {
353 *(v++) = by * inv_height;
354 } else {
355 *(v++) = (gs->height - by) * inv_height;
356 }
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400357 }
358
359 vtxcnt[nvtx++] = n;
360 }
361 }
362
363 return nvtx;
364}
365
366static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500367triangle_fan_debug(struct weston_view *view, int first, int count)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400368{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500369 struct weston_compositor *compositor = view->surface->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100370 struct gl_renderer *gr = get_renderer(compositor);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400371 int i;
372 GLushort *buffer;
373 GLushort *index;
374 int nelems;
375 static int color_idx = 0;
376 static const GLfloat color[][4] = {
377 { 1.0, 0.0, 0.0, 1.0 },
378 { 0.0, 1.0, 0.0, 1.0 },
379 { 0.0, 0.0, 1.0, 1.0 },
380 { 1.0, 1.0, 1.0, 1.0 },
381 };
382
383 nelems = (count - 1 + count - 2) * 2;
384
385 buffer = malloc(sizeof(GLushort) * nelems);
386 index = buffer;
387
388 for (i = 1; i < count; i++) {
389 *index++ = first;
390 *index++ = first + i;
391 }
392
393 for (i = 2; i < count; i++) {
394 *index++ = first + i - 1;
395 *index++ = first + i;
396 }
397
John Kåre Alsaker40684142012-11-13 19:10:25 +0100398 glUseProgram(gr->solid_shader.program);
399 glUniform4fv(gr->solid_shader.color_uniform, 1,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400400 color[color_idx++ % ARRAY_LENGTH(color)]);
401 glDrawElements(GL_LINES, nelems, GL_UNSIGNED_SHORT, buffer);
John Kåre Alsaker40684142012-11-13 19:10:25 +0100402 glUseProgram(gr->current_shader->program);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400403 free(buffer);
404}
405
406static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500407repaint_region(struct weston_view *ev, pixman_region32_t *region,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400408 pixman_region32_t *surf_region)
409{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500410 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400411 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400412 GLfloat *v;
413 unsigned int *vtxcnt;
414 int i, first, nfans;
415
416 /* The final region to be painted is the intersection of
417 * 'region' and 'surf_region'. However, 'region' is in the global
418 * coordinates, and 'surf_region' is in the surface-local
419 * coordinates. texture_region() will iterate over all pairs of
420 * rectangles from both regions, compute the intersection
421 * polygon for each pair, and store it as a triangle fan if
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400422 * it has a non-zero area (at least 3 vertices1, actually).
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400423 */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500424 nfans = texture_region(ev, region, surf_region);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400425
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400426 v = gr->vertices.data;
427 vtxcnt = gr->vtxcnt.data;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400428
429 /* position: */
430 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
431 glEnableVertexAttribArray(0);
432
433 /* texcoord: */
434 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
435 glEnableVertexAttribArray(1);
436
437 for (i = 0, first = 0; i < nfans; i++) {
438 glDrawArrays(GL_TRIANGLE_FAN, first, vtxcnt[i]);
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400439 if (gr->fan_debug)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500440 triangle_fan_debug(ev, first, vtxcnt[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400441 first += vtxcnt[i];
442 }
443
444 glDisableVertexAttribArray(1);
445 glDisableVertexAttribArray(0);
446
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400447 gr->vertices.size = 0;
448 gr->vtxcnt.size = 0;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400449}
450
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100451static int
452use_output(struct weston_output *output)
453{
454 static int errored;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100455 struct gl_output_state *go = get_output_state(output);
456 struct gl_renderer *gr = get_renderer(output->compositor);
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100457 EGLBoolean ret;
458
459 ret = eglMakeCurrent(gr->egl_display, go->egl_surface,
460 go->egl_surface, gr->egl_context);
461
462 if (ret == EGL_FALSE) {
463 if (errored)
464 return -1;
465 errored = 1;
466 weston_log("Failed to make EGL context current.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +0200467 gl_renderer_print_egl_error_state();
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100468 return -1;
469 }
470
471 return 0;
472}
473
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +0300474static int
475shader_init(struct gl_shader *shader, struct gl_renderer *gr,
476 const char *vertex_source, const char *fragment_source);
477
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400478static void
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +0300479use_shader(struct gl_renderer *gr, struct gl_shader *shader)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400480{
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +0300481 if (!shader->program) {
482 int ret;
483
484 ret = shader_init(shader, gr,
485 shader->vertex_source,
486 shader->fragment_source);
487
488 if (ret < 0)
489 weston_log("warning: failed to compile shader\n");
490 }
491
John Kåre Alsaker40684142012-11-13 19:10:25 +0100492 if (gr->current_shader == shader)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400493 return;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400494 glUseProgram(shader->program);
John Kåre Alsaker40684142012-11-13 19:10:25 +0100495 gr->current_shader = shader;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400496}
497
498static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100499shader_uniforms(struct gl_shader *shader,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500500 struct weston_view *view,
501 struct weston_output *output)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400502{
503 int i;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500504 struct gl_surface_state *gs = get_surface_state(view->surface);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400505
506 glUniformMatrix4fv(shader->proj_uniform,
507 1, GL_FALSE, output->matrix.d);
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100508 glUniform4fv(shader->color_uniform, 1, gs->color);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500509 glUniform1f(shader->alpha_uniform, view->alpha);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400510
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100511 for (i = 0; i < gs->num_textures; i++)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400512 glUniform1i(shader->tex_uniforms[i], i);
513}
514
515static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500516draw_view(struct weston_view *ev, struct weston_output *output,
517 pixman_region32_t *damage) /* in global coordinates */
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400518{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500519 struct weston_compositor *ec = ev->surface->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100520 struct gl_renderer *gr = get_renderer(ec);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500521 struct gl_surface_state *gs = get_surface_state(ev->surface);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400522 /* repaint bounding region in global coordinates: */
523 pixman_region32_t repaint;
524 /* non-opaque region in surface coordinates: */
525 pixman_region32_t surface_blend;
526 GLint filter;
527 int i;
528
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +0200529 /* In case of a runtime switch of renderers, we may not have received
530 * an attach for this surface since the switch. In that case we don't
531 * have a valid buffer or a proper shader set up so skip rendering. */
532 if (!gs->shader)
533 return;
534
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400535 pixman_region32_init(&repaint);
536 pixman_region32_intersect(&repaint,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500537 &ev->transform.boundingbox, damage);
538 pixman_region32_subtract(&repaint, &repaint, &ev->clip);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400539
540 if (!pixman_region32_not_empty(&repaint))
541 goto out;
542
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400543 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
544
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400545 if (gr->fan_debug) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100546 use_shader(gr, &gr->solid_shader);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500547 shader_uniforms(&gr->solid_shader, ev, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400548 }
549
John Kåre Alsaker40684142012-11-13 19:10:25 +0100550 use_shader(gr, gs->shader);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500551 shader_uniforms(gs->shader, ev, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400552
Jason Ekstranda7af7042013-10-12 22:38:11 -0500553 if (ev->transform.enabled || output->zoom.active ||
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +0100554 output->current_scale != ev->surface->buffer_viewport.scale)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400555 filter = GL_LINEAR;
556 else
557 filter = GL_NEAREST;
558
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100559 for (i = 0; i < gs->num_textures; i++) {
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400560 glActiveTexture(GL_TEXTURE0 + i);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100561 glBindTexture(gs->target, gs->textures[i]);
562 glTexParameteri(gs->target, GL_TEXTURE_MIN_FILTER, filter);
563 glTexParameteri(gs->target, GL_TEXTURE_MAG_FILTER, filter);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400564 }
565
566 /* blended region is whole surface minus opaque region: */
567 pixman_region32_init_rect(&surface_blend, 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600568 ev->surface->width, ev->surface->height);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500569 pixman_region32_subtract(&surface_blend, &surface_blend, &ev->surface->opaque);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400570
Jason Ekstranda7af7042013-10-12 22:38:11 -0500571 /* XXX: Should we be using ev->transform.opaque here? */
572 if (pixman_region32_not_empty(&ev->surface->opaque)) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100573 if (gs->shader == &gr->texture_shader_rgba) {
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400574 /* Special case for RGBA textures with possibly
575 * bad data in alpha channel: use the shader
576 * that forces texture alpha = 1.0.
577 * Xwayland surfaces need this.
578 */
John Kåre Alsaker40684142012-11-13 19:10:25 +0100579 use_shader(gr, &gr->texture_shader_rgbx);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500580 shader_uniforms(&gr->texture_shader_rgbx, ev, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400581 }
582
Jason Ekstranda7af7042013-10-12 22:38:11 -0500583 if (ev->alpha < 1.0)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400584 glEnable(GL_BLEND);
585 else
586 glDisable(GL_BLEND);
587
Jason Ekstranda7af7042013-10-12 22:38:11 -0500588 repaint_region(ev, &repaint, &ev->surface->opaque);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400589 }
590
591 if (pixman_region32_not_empty(&surface_blend)) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100592 use_shader(gr, gs->shader);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400593 glEnable(GL_BLEND);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500594 repaint_region(ev, &repaint, &surface_blend);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400595 }
596
597 pixman_region32_fini(&surface_blend);
598
599out:
600 pixman_region32_fini(&repaint);
601}
602
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400603static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500604repaint_views(struct weston_output *output, pixman_region32_t *damage)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400605{
606 struct weston_compositor *compositor = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500607 struct weston_view *view;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400608
Jason Ekstranda7af7042013-10-12 22:38:11 -0500609 wl_list_for_each_reverse(view, &compositor->view_list, link)
610 if (view->plane == &compositor->primary_plane)
611 draw_view(view, output, damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400612}
613
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500614static void
Jason Ekstrande5512d42014-02-04 21:36:38 -0600615draw_output_border_texture(struct gl_output_state *go,
616 enum gl_renderer_border_side side,
617 int32_t x, int32_t y,
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500618 int32_t width, int32_t height)
619{
Jason Ekstrande5512d42014-02-04 21:36:38 -0600620 struct gl_border_image *img = &go->borders[side];
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500621 static GLushort indices [] = { 0, 1, 3, 3, 1, 2 };
622
623 if (!img->data) {
624 if (img->tex) {
625 glDeleteTextures(1, &img->tex);
626 img->tex = 0;
627 }
628
629 return;
630 }
631
632 if (!img->tex) {
633 glGenTextures(1, &img->tex);
634 glBindTexture(GL_TEXTURE_2D, img->tex);
635
636 glTexParameteri(GL_TEXTURE_2D,
637 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
638 glTexParameteri(GL_TEXTURE_2D,
639 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
640 glTexParameteri(GL_TEXTURE_2D,
641 GL_TEXTURE_MIN_FILTER, GL_NEAREST);
642 glTexParameteri(GL_TEXTURE_2D,
643 GL_TEXTURE_MAG_FILTER, GL_NEAREST);
644 } else {
645 glBindTexture(GL_TEXTURE_2D, img->tex);
646 }
647
Jason Ekstrande5512d42014-02-04 21:36:38 -0600648 if (go->border_status & (1 << side)) {
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500649#ifdef GL_EXT_unpack_subimage
650 glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, 0);
651 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, 0);
652 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, 0);
653#endif
654 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
655 img->tex_width, img->height, 0,
656 GL_BGRA_EXT, GL_UNSIGNED_BYTE, img->data);
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500657 }
658
659 GLfloat texcoord[] = {
660 0.0f, 0.0f,
661 (GLfloat)img->width / (GLfloat)img->tex_width, 0.0f,
662 (GLfloat)img->width / (GLfloat)img->tex_width, 1.0f,
663 0.0f, 1.0f,
664 };
665
666 GLfloat verts[] = {
667 x, y,
668 x + width, y,
669 x + width, y + height,
670 x, y + height
671 };
672
673 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, verts);
674 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, texcoord);
675 glEnableVertexAttribArray(0);
676 glEnableVertexAttribArray(1);
677
678 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
679
680 glDisableVertexAttribArray(1);
681 glDisableVertexAttribArray(0);
682}
683
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -0600684static int
685output_has_borders(struct weston_output *output)
686{
687 struct gl_output_state *go = get_output_state(output);
688
689 return go->borders[GL_RENDERER_BORDER_TOP].data ||
690 go->borders[GL_RENDERER_BORDER_RIGHT].data ||
691 go->borders[GL_RENDERER_BORDER_BOTTOM].data ||
692 go->borders[GL_RENDERER_BORDER_LEFT].data;
693}
694
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500695static void
Jason Ekstrande5512d42014-02-04 21:36:38 -0600696draw_output_borders(struct weston_output *output,
697 enum gl_border_status border_status)
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500698{
699 struct gl_output_state *go = get_output_state(output);
700 struct gl_renderer *gr = get_renderer(output->compositor);
701 struct gl_shader *shader = &gr->texture_shader_rgba;
Jason Ekstrand00b84282013-10-27 22:24:59 -0500702 struct gl_border_image *top, *bottom, *left, *right;
703 struct weston_matrix matrix;
704 int full_width, full_height;
705
Jason Ekstrande5512d42014-02-04 21:36:38 -0600706 if (border_status == BORDER_STATUS_CLEAN)
707 return; /* Clean. Nothing to do. */
708
Jason Ekstrand00b84282013-10-27 22:24:59 -0500709 top = &go->borders[GL_RENDERER_BORDER_TOP];
710 bottom = &go->borders[GL_RENDERER_BORDER_BOTTOM];
711 left = &go->borders[GL_RENDERER_BORDER_LEFT];
712 right = &go->borders[GL_RENDERER_BORDER_RIGHT];
713
714 full_width = output->current_mode->width + left->width + right->width;
715 full_height = output->current_mode->height + top->height + bottom->height;
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500716
717 glDisable(GL_BLEND);
718 use_shader(gr, shader);
719
Jason Ekstrand00b84282013-10-27 22:24:59 -0500720 glViewport(0, 0, full_width, full_height);
721
722 weston_matrix_init(&matrix);
723 weston_matrix_translate(&matrix, -full_width/2.0, -full_height/2.0, 0);
724 weston_matrix_scale(&matrix, 2.0/full_width, -2.0/full_height, 1);
725 glUniformMatrix4fv(shader->proj_uniform, 1, GL_FALSE, matrix.d);
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500726
727 glUniform1i(shader->tex_uniforms[0], 0);
728 glUniform1f(shader->alpha_uniform, 1);
729 glActiveTexture(GL_TEXTURE0);
730
Jason Ekstrande5512d42014-02-04 21:36:38 -0600731 if (border_status & BORDER_TOP_DIRTY)
732 draw_output_border_texture(go, GL_RENDERER_BORDER_TOP,
733 0, 0,
734 full_width, top->height);
735 if (border_status & BORDER_LEFT_DIRTY)
736 draw_output_border_texture(go, GL_RENDERER_BORDER_LEFT,
737 0, top->height,
738 left->width, output->current_mode->height);
739 if (border_status & BORDER_RIGHT_DIRTY)
740 draw_output_border_texture(go, GL_RENDERER_BORDER_RIGHT,
741 full_width - right->width, top->height,
742 right->width, output->current_mode->height);
743 if (border_status & BORDER_BOTTOM_DIRTY)
744 draw_output_border_texture(go, GL_RENDERER_BORDER_BOTTOM,
745 0, full_height - bottom->height,
746 full_width, bottom->height);
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500747}
John Kåre Alsaker44154502012-11-13 19:10:20 +0100748
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400749static void
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -0600750output_get_border_damage(struct weston_output *output,
751 enum gl_border_status border_status,
752 pixman_region32_t *damage)
753{
754 struct gl_output_state *go = get_output_state(output);
755 struct gl_border_image *top, *bottom, *left, *right;
756 int full_width, full_height;
757
758 if (border_status == BORDER_STATUS_CLEAN)
759 return; /* Clean. Nothing to do. */
760
761 top = &go->borders[GL_RENDERER_BORDER_TOP];
762 bottom = &go->borders[GL_RENDERER_BORDER_BOTTOM];
763 left = &go->borders[GL_RENDERER_BORDER_LEFT];
764 right = &go->borders[GL_RENDERER_BORDER_RIGHT];
765
766 full_width = output->current_mode->width + left->width + right->width;
767 full_height = output->current_mode->height + top->height + bottom->height;
768 if (border_status & BORDER_TOP_DIRTY)
769 pixman_region32_union_rect(damage, damage,
770 0, 0,
771 full_width, top->height);
772 if (border_status & BORDER_LEFT_DIRTY)
773 pixman_region32_union_rect(damage, damage,
774 0, top->height,
775 left->width, output->current_mode->height);
776 if (border_status & BORDER_RIGHT_DIRTY)
777 pixman_region32_union_rect(damage, damage,
778 full_width - right->width, top->height,
779 right->width, output->current_mode->height);
780 if (border_status & BORDER_BOTTOM_DIRTY)
781 pixman_region32_union_rect(damage, damage,
782 0, full_height - bottom->height,
783 full_width, bottom->height);
784}
785
786static void
Jason Ekstrande5512d42014-02-04 21:36:38 -0600787output_get_damage(struct weston_output *output,
788 pixman_region32_t *buffer_damage, uint32_t *border_damage)
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +0200789{
790 struct gl_output_state *go = get_output_state(output);
791 struct gl_renderer *gr = get_renderer(output->compositor);
792 EGLint buffer_age = 0;
793 EGLBoolean ret;
794 int i;
795
796 if (gr->has_egl_buffer_age) {
797 ret = eglQuerySurface(gr->egl_display, go->egl_surface,
798 EGL_BUFFER_AGE_EXT, &buffer_age);
799 if (ret == EGL_FALSE) {
800 weston_log("buffer age query failed.\n");
801 gl_renderer_print_egl_error_state();
802 }
803 }
804
Jason Ekstrande5512d42014-02-04 21:36:38 -0600805 if (buffer_age == 0 || buffer_age - 1 > BUFFER_DAMAGE_COUNT) {
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +0200806 pixman_region32_copy(buffer_damage, &output->region);
Jason Ekstrande5512d42014-02-04 21:36:38 -0600807 *border_damage = BORDER_ALL_DIRTY;
808 } else {
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +0200809 for (i = 0; i < buffer_age - 1; i++)
Jason Ekstrande5512d42014-02-04 21:36:38 -0600810 *border_damage |= go->border_damage[i];
811
812 if (*border_damage & BORDER_SIZE_CHANGED) {
813 /* If we've had a resize, we have to do a full
814 * repaint. */
815 *border_damage |= BORDER_ALL_DIRTY;
816 pixman_region32_copy(buffer_damage, &output->region);
817 } else {
818 for (i = 0; i < buffer_age - 1; i++)
819 pixman_region32_union(buffer_damage,
820 buffer_damage,
821 &go->buffer_damage[i]);
822 }
823 }
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +0200824}
825
826static void
827output_rotate_damage(struct weston_output *output,
Jason Ekstrande5512d42014-02-04 21:36:38 -0600828 pixman_region32_t *output_damage,
829 enum gl_border_status border_status)
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +0200830{
831 struct gl_output_state *go = get_output_state(output);
832 struct gl_renderer *gr = get_renderer(output->compositor);
833 int i;
834
835 if (!gr->has_egl_buffer_age)
836 return;
837
Jason Ekstrande5512d42014-02-04 21:36:38 -0600838 for (i = BUFFER_DAMAGE_COUNT - 1; i >= 1; i--) {
839 go->border_damage[i] = go->border_damage[i - 1];
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +0200840 pixman_region32_copy(&go->buffer_damage[i],
841 &go->buffer_damage[i - 1]);
Jason Ekstrande5512d42014-02-04 21:36:38 -0600842 }
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +0200843
Jason Ekstrande5512d42014-02-04 21:36:38 -0600844 go->border_damage[0] = border_status;
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +0200845 pixman_region32_copy(&go->buffer_damage[0], output_damage);
846}
847
848static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100849gl_renderer_repaint_output(struct weston_output *output,
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400850 pixman_region32_t *output_damage)
851{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100852 struct gl_output_state *go = get_output_state(output);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400853 struct weston_compositor *compositor = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100854 struct gl_renderer *gr = get_renderer(compositor);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400855 EGLBoolean ret;
856 static int errored;
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -0600857#ifdef EGL_EXT_swap_buffers_with_damage
858 int i, nrects, buffer_height;
859 EGLint *egl_damage, *d;
860 pixman_box32_t *rects;
861#endif
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +0200862 pixman_region32_t buffer_damage, total_damage;
Jason Ekstrande5512d42014-02-04 21:36:38 -0600863 enum gl_border_status border_damage = BORDER_STATUS_CLEAN;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400864
Jason Ekstrand00b84282013-10-27 22:24:59 -0500865 /* Calculate the viewport */
866 glViewport(go->borders[GL_RENDERER_BORDER_LEFT].width,
867 go->borders[GL_RENDERER_BORDER_BOTTOM].height,
868 output->current_mode->width,
869 output->current_mode->height);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400870
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100871 if (use_output(output) < 0)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400872 return;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400873
874 /* if debugging, redraw everything outside the damage to clean up
875 * debug lines from the previous draw on this buffer:
876 */
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400877 if (gr->fan_debug) {
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400878 pixman_region32_t undamaged;
879 pixman_region32_init(&undamaged);
880 pixman_region32_subtract(&undamaged, &output->region,
881 output_damage);
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400882 gr->fan_debug = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500883 repaint_views(output, &undamaged);
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400884 gr->fan_debug = 1;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400885 pixman_region32_fini(&undamaged);
886 }
887
Ander Conselvan de Oliveirab605c062013-03-05 17:30:28 +0200888 pixman_region32_init(&total_damage);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +0200889 pixman_region32_init(&buffer_damage);
890
Jason Ekstrande5512d42014-02-04 21:36:38 -0600891 output_get_damage(output, &buffer_damage, &border_damage);
892 output_rotate_damage(output, output_damage, go->border_status);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +0200893
894 pixman_region32_union(&total_damage, &buffer_damage, output_damage);
Jason Ekstrande5512d42014-02-04 21:36:38 -0600895 border_damage |= go->border_status;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300896
Jason Ekstranda7af7042013-10-12 22:38:11 -0500897 repaint_views(output, &total_damage);
Ander Conselvan de Oliveirab605c062013-03-05 17:30:28 +0200898
899 pixman_region32_fini(&total_damage);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +0200900 pixman_region32_fini(&buffer_damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400901
Jason Ekstrande5512d42014-02-04 21:36:38 -0600902 draw_output_borders(output, border_damage);
John Kåre Alsaker44154502012-11-13 19:10:20 +0100903
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +0200904 pixman_region32_copy(&output->previous_damage, output_damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400905 wl_signal_emit(&output->frame_signal, output);
906
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -0600907#ifdef EGL_EXT_swap_buffers_with_damage
908 if (gr->swap_buffers_with_damage) {
909 pixman_region32_init(&buffer_damage);
910 weston_transformed_region(output->width, output->height,
911 output->transform,
912 output->current_scale,
913 output_damage, &buffer_damage);
914
915 if (output_has_borders(output)) {
916 pixman_region32_translate(&buffer_damage,
917 go->borders[GL_RENDERER_BORDER_LEFT].width,
918 go->borders[GL_RENDERER_BORDER_TOP].height);
919 output_get_border_damage(output, go->border_status,
920 &buffer_damage);
921 }
922
923 rects = pixman_region32_rectangles(&buffer_damage, &nrects);
924 egl_damage = malloc(nrects * 4 * sizeof(EGLint));
925
926 buffer_height = go->borders[GL_RENDERER_BORDER_TOP].height +
927 output->current_mode->height +
928 go->borders[GL_RENDERER_BORDER_BOTTOM].height;
929
930 d = egl_damage;
931 for (i = 0; i < nrects; ++i) {
932 *d++ = rects[i].x1;
933 *d++ = buffer_height - rects[i].y2;
934 *d++ = rects[i].x2 - rects[i].x1;
935 *d++ = rects[i].y2 - rects[i].y1;
936 }
937 ret = gr->swap_buffers_with_damage(gr->egl_display,
938 go->egl_surface,
939 egl_damage, nrects);
940 free(egl_damage);
941 pixman_region32_fini(&buffer_damage);
942 } else {
943 ret = eglSwapBuffers(gr->egl_display, go->egl_surface);
944 }
945#else /* ! defined EGL_EXT_swap_buffers_with_damage */
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100946 ret = eglSwapBuffers(gr->egl_display, go->egl_surface);
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -0600947#endif
948
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400949 if (ret == EGL_FALSE && !errored) {
950 errored = 1;
951 weston_log("Failed in eglSwapBuffers.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +0200952 gl_renderer_print_egl_error_state();
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400953 }
954
Jason Ekstrande5512d42014-02-04 21:36:38 -0600955 go->border_status = BORDER_STATUS_CLEAN;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400956}
Kristian Høgsberg25894fc2012-09-05 22:06:26 -0400957
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100958static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100959gl_renderer_read_pixels(struct weston_output *output,
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100960 pixman_format_code_t format, void *pixels,
961 uint32_t x, uint32_t y,
962 uint32_t width, uint32_t height)
963{
964 GLenum gl_format;
965
966 switch (format) {
967 case PIXMAN_a8r8g8b8:
968 gl_format = GL_BGRA_EXT;
969 break;
970 case PIXMAN_a8b8g8r8:
971 gl_format = GL_RGBA;
972 break;
973 default:
974 return -1;
975 }
976
977 if (use_output(output) < 0)
978 return -1;
979
980 glPixelStorei(GL_PACK_ALIGNMENT, 1);
981 glReadPixels(x, y, width, height, gl_format,
982 GL_UNSIGNED_BYTE, pixels);
983
984 return 0;
985}
986
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400987static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100988gl_renderer_flush_damage(struct weston_surface *surface)
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -0400989{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100990 struct gl_renderer *gr = get_renderer(surface->compositor);
991 struct gl_surface_state *gs = get_surface_state(surface);
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500992 struct weston_buffer *buffer = gs->buffer_ref.buffer;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500993 struct weston_view *view;
994 int texture_used;
Tomeu Vizoso12072b62013-08-06 20:05:55 +0200995 GLenum format;
996 int pixel_type;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100997
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -0700998#ifdef GL_EXT_unpack_subimage
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -0400999 pixman_box32_t *rectangles;
1000 void *data;
1001 int i, n;
1002#endif
1003
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001004 pixman_region32_union(&gs->texture_damage,
1005 &gs->texture_damage, &surface->damage);
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001006
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001007 if (!buffer)
1008 return;
1009
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001010 /* Avoid upload, if the texture won't be used this time.
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001011 * We still accumulate the damage in texture_damage, and
1012 * hold the reference to the buffer, in case the surface
1013 * migrates back to the primary plane.
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001014 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001015 texture_used = 0;
1016 wl_list_for_each(view, &surface->views, surface_link) {
1017 if (view->plane == &surface->compositor->primary_plane) {
1018 texture_used = 1;
1019 break;
1020 }
1021 }
1022 if (!texture_used)
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001023 return;
1024
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001025 if (!pixman_region32_not_empty(&gs->texture_damage) &&
1026 !gs->needs_full_upload)
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001027 goto done;
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001028
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001029 switch (wl_shm_buffer_get_format(buffer->shm_buffer)) {
1030 case WL_SHM_FORMAT_XRGB8888:
1031 case WL_SHM_FORMAT_ARGB8888:
1032 format = GL_BGRA_EXT;
1033 pixel_type = GL_UNSIGNED_BYTE;
1034 break;
1035 case WL_SHM_FORMAT_RGB565:
1036 format = GL_RGB;
1037 pixel_type = GL_UNSIGNED_SHORT_5_6_5;
1038 break;
1039 default:
1040 weston_log("warning: unknown shm buffer format\n");
1041 format = GL_BGRA_EXT;
1042 pixel_type = GL_UNSIGNED_BYTE;
1043 }
1044
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001045 glBindTexture(GL_TEXTURE_2D, gs->textures[0]);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001046
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001047 if (!gr->has_unpack_subimage) {
Neil Robertse5051712013-11-13 15:44:06 +00001048 wl_shm_buffer_begin_access(buffer->shm_buffer);
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001049 glTexImage2D(GL_TEXTURE_2D, 0, format,
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001050 gs->pitch, buffer->height, 0,
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001051 format, pixel_type,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001052 wl_shm_buffer_get_data(buffer->shm_buffer));
Neil Robertse5051712013-11-13 15:44:06 +00001053 wl_shm_buffer_end_access(buffer->shm_buffer);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001054
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001055 goto done;
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001056 }
1057
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -07001058#ifdef GL_EXT_unpack_subimage
1059 glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, gs->pitch);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001060 data = wl_shm_buffer_get_data(buffer->shm_buffer);
Ander Conselvan de Oliveira6be5f432013-06-07 16:52:45 +03001061
1062 if (gs->needs_full_upload) {
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -07001063 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, 0);
1064 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, 0);
Neil Robertse5051712013-11-13 15:44:06 +00001065 wl_shm_buffer_begin_access(buffer->shm_buffer);
Ander Conselvan de Oliveira6be5f432013-06-07 16:52:45 +03001066 glTexSubImage2D(GL_TEXTURE_2D, 0,
1067 0, 0, gs->pitch, buffer->height,
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001068 format, pixel_type, data);
Neil Robertse5051712013-11-13 15:44:06 +00001069 wl_shm_buffer_end_access(buffer->shm_buffer);
Ander Conselvan de Oliveira6be5f432013-06-07 16:52:45 +03001070 goto done;
1071 }
1072
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001073 rectangles = pixman_region32_rectangles(&gs->texture_damage, &n);
Neil Robertse5051712013-11-13 15:44:06 +00001074 wl_shm_buffer_begin_access(buffer->shm_buffer);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001075 for (i = 0; i < n; i++) {
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001076 pixman_box32_t r;
1077
1078 r = weston_surface_to_buffer_rect(surface, rectangles[i]);
1079
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -07001080 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, r.x1);
1081 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, r.y1);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001082 glTexSubImage2D(GL_TEXTURE_2D, 0, r.x1, r.y1,
1083 r.x2 - r.x1, r.y2 - r.y1,
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001084 format, pixel_type, data);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001085 }
Neil Robertse5051712013-11-13 15:44:06 +00001086 wl_shm_buffer_end_access(buffer->shm_buffer);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001087#endif
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001088
1089done:
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001090 pixman_region32_fini(&gs->texture_damage);
1091 pixman_region32_init(&gs->texture_damage);
Ander Conselvan de Oliveira6be5f432013-06-07 16:52:45 +03001092 gs->needs_full_upload = 0;
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001093
1094 weston_buffer_reference(&gs->buffer_ref, NULL);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001095}
1096
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001097static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001098ensure_textures(struct gl_surface_state *gs, int num_textures)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001099{
1100 int i;
1101
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001102 if (num_textures <= gs->num_textures)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001103 return;
1104
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001105 for (i = gs->num_textures; i < num_textures; i++) {
1106 glGenTextures(1, &gs->textures[i]);
1107 glBindTexture(gs->target, gs->textures[i]);
1108 glTexParameteri(gs->target,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001109 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001110 glTexParameteri(gs->target,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001111 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1112 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001113 gs->num_textures = num_textures;
1114 glBindTexture(gs->target, 0);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001115}
1116
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001117static void
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001118gl_renderer_attach_shm(struct weston_surface *es, struct weston_buffer *buffer,
1119 struct wl_shm_buffer *shm_buffer)
1120{
1121 struct weston_compositor *ec = es->compositor;
1122 struct gl_renderer *gr = get_renderer(ec);
1123 struct gl_surface_state *gs = get_surface_state(es);
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001124 int pitch;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001125
1126 buffer->shm_buffer = shm_buffer;
1127 buffer->width = wl_shm_buffer_get_width(shm_buffer);
1128 buffer->height = wl_shm_buffer_get_height(shm_buffer);
1129
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001130 switch (wl_shm_buffer_get_format(shm_buffer)) {
1131 case WL_SHM_FORMAT_XRGB8888:
1132 gs->shader = &gr->texture_shader_rgbx;
1133 pitch = wl_shm_buffer_get_stride(shm_buffer) / 4;
1134 break;
1135 case WL_SHM_FORMAT_ARGB8888:
1136 gs->shader = &gr->texture_shader_rgba;
1137 pitch = wl_shm_buffer_get_stride(shm_buffer) / 4;
1138 break;
1139 case WL_SHM_FORMAT_RGB565:
1140 gs->shader = &gr->texture_shader_rgbx;
1141 pitch = wl_shm_buffer_get_stride(shm_buffer) / 2;
1142 break;
1143 default:
1144 weston_log("warning: unknown shm buffer format\n");
1145 gs->shader = &gr->texture_shader_rgba;
1146 pitch = wl_shm_buffer_get_stride(shm_buffer) / 4;
1147 }
1148
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001149 /* Only allocate a texture if it doesn't match existing one.
1150 * If a switch from DRM allocated buffer to a SHM buffer is
1151 * happening, we need to allocate a new texture buffer. */
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001152 if (pitch != gs->pitch ||
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001153 buffer->height != gs->height ||
1154 gs->buffer_type != BUFFER_TYPE_SHM) {
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001155 gs->pitch = pitch;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001156 gs->height = buffer->height;
1157 gs->target = GL_TEXTURE_2D;
1158 gs->buffer_type = BUFFER_TYPE_SHM;
1159 gs->needs_full_upload = 1;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001160 gs->y_inverted = 1;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001161
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03001162 gs->surface = es;
1163
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001164 ensure_textures(gs, 1);
1165 glBindTexture(GL_TEXTURE_2D, gs->textures[0]);
1166 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1167 gs->pitch, buffer->height, 0,
1168 GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
1169 }
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001170}
1171
1172static void
1173gl_renderer_attach_egl(struct weston_surface *es, struct weston_buffer *buffer,
1174 uint32_t format)
1175{
1176 struct weston_compositor *ec = es->compositor;
1177 struct gl_renderer *gr = get_renderer(ec);
1178 struct gl_surface_state *gs = get_surface_state(es);
1179 EGLint attribs[3];
1180 int i, num_planes;
1181
1182 buffer->legacy_buffer = (struct wl_buffer *)buffer->resource;
1183 gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1184 EGL_WIDTH, &buffer->width);
1185 gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1186 EGL_HEIGHT, &buffer->height);
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001187 gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1188 EGL_WAYLAND_Y_INVERTED_WL, &buffer->y_inverted);
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001189
1190 for (i = 0; i < gs->num_images; i++)
1191 gr->destroy_image(gr->egl_display, gs->images[i]);
1192 gs->num_images = 0;
1193 gs->target = GL_TEXTURE_2D;
1194 switch (format) {
1195 case EGL_TEXTURE_RGB:
1196 case EGL_TEXTURE_RGBA:
1197 default:
1198 num_planes = 1;
1199 gs->shader = &gr->texture_shader_rgba;
1200 break;
1201 case EGL_TEXTURE_EXTERNAL_WL:
1202 num_planes = 1;
1203 gs->target = GL_TEXTURE_EXTERNAL_OES;
1204 gs->shader = &gr->texture_shader_egl_external;
1205 break;
1206 case EGL_TEXTURE_Y_UV_WL:
1207 num_planes = 2;
1208 gs->shader = &gr->texture_shader_y_uv;
1209 break;
1210 case EGL_TEXTURE_Y_U_V_WL:
1211 num_planes = 3;
1212 gs->shader = &gr->texture_shader_y_u_v;
1213 break;
1214 case EGL_TEXTURE_Y_XUXV_WL:
1215 num_planes = 2;
1216 gs->shader = &gr->texture_shader_y_xuxv;
1217 break;
1218 }
1219
1220 ensure_textures(gs, num_planes);
1221 for (i = 0; i < num_planes; i++) {
1222 attribs[0] = EGL_WAYLAND_PLANE_WL;
1223 attribs[1] = i;
1224 attribs[2] = EGL_NONE;
1225 gs->images[i] = gr->create_image(gr->egl_display,
1226 NULL,
1227 EGL_WAYLAND_BUFFER_WL,
1228 buffer->legacy_buffer,
1229 attribs);
1230 if (!gs->images[i]) {
1231 weston_log("failed to create img for plane %d\n", i);
1232 continue;
1233 }
1234 gs->num_images++;
1235
1236 glActiveTexture(GL_TEXTURE0 + i);
1237 glBindTexture(gs->target, gs->textures[i]);
1238 gr->image_target_texture_2d(gs->target,
1239 gs->images[i]);
1240 }
1241
1242 gs->pitch = buffer->width;
1243 gs->height = buffer->height;
1244 gs->buffer_type = BUFFER_TYPE_EGL;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001245 gs->y_inverted = buffer->y_inverted;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001246}
1247
1248static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001249gl_renderer_attach(struct weston_surface *es, struct weston_buffer *buffer)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001250{
1251 struct weston_compositor *ec = es->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001252 struct gl_renderer *gr = get_renderer(ec);
1253 struct gl_surface_state *gs = get_surface_state(es);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001254 struct wl_shm_buffer *shm_buffer;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001255 EGLint format;
1256 int i;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001257
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001258 weston_buffer_reference(&gs->buffer_ref, buffer);
1259
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001260 if (!buffer) {
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001261 for (i = 0; i < gs->num_images; i++) {
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001262 gr->destroy_image(gr->egl_display, gs->images[i]);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001263 gs->images[i] = NULL;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001264 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001265 gs->num_images = 0;
1266 glDeleteTextures(gs->num_textures, gs->textures);
1267 gs->num_textures = 0;
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +03001268 gs->buffer_type = BUFFER_TYPE_NULL;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001269 gs->y_inverted = 1;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001270 return;
1271 }
1272
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001273 shm_buffer = wl_shm_buffer_get(buffer->resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001274
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001275 if (shm_buffer)
1276 gl_renderer_attach_shm(es, buffer, shm_buffer);
Kristian Høgsberg47229392013-08-07 11:59:54 -07001277 else if (gr->query_buffer(gr->egl_display, (void *) buffer->resource,
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001278 EGL_TEXTURE_FORMAT, &format))
1279 gl_renderer_attach_egl(es, buffer, format);
1280 else {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001281 weston_log("unhandled buffer type!\n");
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001282 weston_buffer_reference(&gs->buffer_ref, NULL);
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +03001283 gs->buffer_type = BUFFER_TYPE_NULL;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001284 gs->y_inverted = 1;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001285 }
1286}
1287
Kristian Høgsberg42263852012-09-06 21:59:29 -04001288static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001289gl_renderer_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001290 float red, float green, float blue, float alpha)
1291{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001292 struct gl_surface_state *gs = get_surface_state(surface);
1293 struct gl_renderer *gr = get_renderer(surface->compositor);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001294
1295 gs->color[0] = red;
1296 gs->color[1] = green;
1297 gs->color[2] = blue;
1298 gs->color[3] = alpha;
1299
John Kåre Alsaker40684142012-11-13 19:10:25 +01001300 gs->shader = &gr->solid_shader;
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001301}
1302
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03001303static void
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03001304surface_state_destroy(struct gl_surface_state *gs, struct gl_renderer *gr)
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03001305{
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03001306 int i;
1307
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03001308 wl_list_remove(&gs->surface_destroy_listener.link);
1309 wl_list_remove(&gs->renderer_destroy_listener.link);
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03001310
1311 gs->surface->renderer_state = NULL;
1312
1313 glDeleteTextures(gs->num_textures, gs->textures);
1314
1315 for (i = 0; i < gs->num_images; i++)
1316 gr->destroy_image(gr->egl_display, gs->images[i]);
1317
1318 weston_buffer_reference(&gs->buffer_ref, NULL);
1319 pixman_region32_fini(&gs->texture_damage);
1320 free(gs);
1321}
1322
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03001323static void
1324surface_state_handle_surface_destroy(struct wl_listener *listener, void *data)
1325{
1326 struct gl_surface_state *gs;
1327 struct gl_renderer *gr;
1328
1329 gs = container_of(listener, struct gl_surface_state,
1330 surface_destroy_listener);
1331
1332 gr = get_renderer(gs->surface->compositor);
1333
1334 surface_state_destroy(gs, gr);
1335}
1336
1337static void
1338surface_state_handle_renderer_destroy(struct wl_listener *listener, void *data)
1339{
1340 struct gl_surface_state *gs;
1341 struct gl_renderer *gr;
1342
1343 gr = data;
1344
1345 gs = container_of(listener, struct gl_surface_state,
1346 renderer_destroy_listener);
1347
1348 surface_state_destroy(gs, gr);
1349}
1350
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001351static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001352gl_renderer_create_surface(struct weston_surface *surface)
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001353{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001354 struct gl_surface_state *gs;
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03001355 struct gl_renderer *gr = get_renderer(surface->compositor);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001356
1357 gs = calloc(1, sizeof *gs);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001358 if (!gs)
1359 return -1;
1360
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001361 /* A buffer is never attached to solid color surfaces, yet
1362 * they still go through texcoord computations. Do not divide
1363 * by zero there.
1364 */
1365 gs->pitch = 1;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001366 gs->y_inverted = 1;
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001367
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03001368 gs->surface = surface;
1369
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001370 pixman_region32_init(&gs->texture_damage);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001371 surface->renderer_state = gs;
1372
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03001373 gs->surface_destroy_listener.notify =
1374 surface_state_handle_surface_destroy;
1375 wl_signal_add(&surface->destroy_signal,
1376 &gs->surface_destroy_listener);
1377
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03001378 gs->renderer_destroy_listener.notify =
1379 surface_state_handle_renderer_destroy;
1380 wl_signal_add(&gr->destroy_signal,
1381 &gs->renderer_destroy_listener);
1382
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001383 if (surface->buffer_ref.buffer) {
1384 gl_renderer_attach(surface, surface->buffer_ref.buffer);
1385 gl_renderer_flush_damage(surface);
1386 }
1387
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001388 return 0;
1389}
1390
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001391static const char vertex_shader[] =
1392 "uniform mat4 proj;\n"
1393 "attribute vec2 position;\n"
1394 "attribute vec2 texcoord;\n"
1395 "varying vec2 v_texcoord;\n"
1396 "void main()\n"
1397 "{\n"
1398 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
1399 " v_texcoord = texcoord;\n"
1400 "}\n";
1401
1402/* Declare common fragment shader uniforms */
1403#define FRAGMENT_CONVERT_YUV \
1404 " y *= alpha;\n" \
1405 " u *= alpha;\n" \
1406 " v *= alpha;\n" \
1407 " gl_FragColor.r = y + 1.59602678 * v;\n" \
1408 " gl_FragColor.g = y - 0.39176229 * u - 0.81296764 * v;\n" \
1409 " gl_FragColor.b = y + 2.01723214 * u;\n" \
1410 " gl_FragColor.a = alpha;\n"
1411
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001412static const char fragment_debug[] =
1413 " gl_FragColor = vec4(0.0, 0.3, 0.0, 0.2) + gl_FragColor * 0.8;\n";
1414
1415static const char fragment_brace[] =
1416 "}\n";
1417
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001418static const char texture_fragment_shader_rgba[] =
1419 "precision mediump float;\n"
1420 "varying vec2 v_texcoord;\n"
1421 "uniform sampler2D tex;\n"
1422 "uniform float alpha;\n"
1423 "void main()\n"
1424 "{\n"
1425 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001426 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001427
1428static const char texture_fragment_shader_rgbx[] =
1429 "precision mediump float;\n"
1430 "varying vec2 v_texcoord;\n"
1431 "uniform sampler2D tex;\n"
1432 "uniform float alpha;\n"
1433 "void main()\n"
1434 "{\n"
1435 " gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb\n;"
1436 " gl_FragColor.a = alpha;\n"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001437 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001438
1439static const char texture_fragment_shader_egl_external[] =
1440 "#extension GL_OES_EGL_image_external : require\n"
1441 "precision mediump float;\n"
1442 "varying vec2 v_texcoord;\n"
1443 "uniform samplerExternalOES tex;\n"
1444 "uniform float alpha;\n"
1445 "void main()\n"
1446 "{\n"
1447 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001448 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001449
1450static const char texture_fragment_shader_y_uv[] =
1451 "precision mediump float;\n"
1452 "uniform sampler2D tex;\n"
1453 "uniform sampler2D tex1;\n"
1454 "varying vec2 v_texcoord;\n"
1455 "uniform float alpha;\n"
1456 "void main() {\n"
1457 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1458 " float u = texture2D(tex1, v_texcoord).r - 0.5;\n"
1459 " float v = texture2D(tex1, v_texcoord).g - 0.5;\n"
1460 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001461 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001462
1463static const char texture_fragment_shader_y_u_v[] =
1464 "precision mediump float;\n"
1465 "uniform sampler2D tex;\n"
1466 "uniform sampler2D tex1;\n"
1467 "uniform sampler2D tex2;\n"
1468 "varying vec2 v_texcoord;\n"
1469 "uniform float alpha;\n"
1470 "void main() {\n"
1471 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1472 " float u = texture2D(tex1, v_texcoord).x - 0.5;\n"
1473 " float v = texture2D(tex2, v_texcoord).x - 0.5;\n"
1474 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001475 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001476
1477static const char texture_fragment_shader_y_xuxv[] =
1478 "precision mediump float;\n"
1479 "uniform sampler2D tex;\n"
1480 "uniform sampler2D tex1;\n"
1481 "varying vec2 v_texcoord;\n"
1482 "uniform float alpha;\n"
1483 "void main() {\n"
1484 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1485 " float u = texture2D(tex1, v_texcoord).g - 0.5;\n"
1486 " float v = texture2D(tex1, v_texcoord).a - 0.5;\n"
1487 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001488 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001489
1490static const char solid_fragment_shader[] =
1491 "precision mediump float;\n"
1492 "uniform vec4 color;\n"
1493 "uniform float alpha;\n"
1494 "void main()\n"
1495 "{\n"
1496 " gl_FragColor = alpha * color\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001497 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001498
1499static int
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001500compile_shader(GLenum type, int count, const char **sources)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001501{
1502 GLuint s;
1503 char msg[512];
1504 GLint status;
1505
1506 s = glCreateShader(type);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001507 glShaderSource(s, count, sources, NULL);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001508 glCompileShader(s);
1509 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
1510 if (!status) {
1511 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1512 weston_log("shader info: %s\n", msg);
1513 return GL_NONE;
1514 }
1515
1516 return s;
1517}
1518
1519static int
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03001520shader_init(struct gl_shader *shader, struct gl_renderer *renderer,
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001521 const char *vertex_source, const char *fragment_source)
1522{
1523 char msg[512];
1524 GLint status;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001525 int count;
1526 const char *sources[3];
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001527
1528 shader->vertex_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001529 compile_shader(GL_VERTEX_SHADER, 1, &vertex_source);
1530
1531 if (renderer->fragment_shader_debug) {
1532 sources[0] = fragment_source;
1533 sources[1] = fragment_debug;
1534 sources[2] = fragment_brace;
1535 count = 3;
1536 } else {
1537 sources[0] = fragment_source;
1538 sources[1] = fragment_brace;
1539 count = 2;
1540 }
1541
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001542 shader->fragment_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001543 compile_shader(GL_FRAGMENT_SHADER, count, sources);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001544
1545 shader->program = glCreateProgram();
1546 glAttachShader(shader->program, shader->vertex_shader);
1547 glAttachShader(shader->program, shader->fragment_shader);
1548 glBindAttribLocation(shader->program, 0, "position");
1549 glBindAttribLocation(shader->program, 1, "texcoord");
1550
1551 glLinkProgram(shader->program);
1552 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1553 if (!status) {
1554 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1555 weston_log("link info: %s\n", msg);
1556 return -1;
1557 }
1558
1559 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1560 shader->tex_uniforms[0] = glGetUniformLocation(shader->program, "tex");
1561 shader->tex_uniforms[1] = glGetUniformLocation(shader->program, "tex1");
1562 shader->tex_uniforms[2] = glGetUniformLocation(shader->program, "tex2");
1563 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
1564 shader->color_uniform = glGetUniformLocation(shader->program, "color");
1565
1566 return 0;
1567}
1568
1569static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001570shader_release(struct gl_shader *shader)
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001571{
1572 glDeleteShader(shader->vertex_shader);
1573 glDeleteShader(shader->fragment_shader);
1574 glDeleteProgram(shader->program);
1575
1576 shader->vertex_shader = 0;
1577 shader->fragment_shader = 0;
1578 shader->program = 0;
1579}
1580
1581static void
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001582log_extensions(const char *name, const char *extensions)
1583{
1584 const char *p, *end;
1585 int l;
1586 int len;
1587
1588 l = weston_log("%s:", name);
1589 p = extensions;
1590 while (*p) {
1591 end = strchrnul(p, ' ');
1592 len = end - p;
1593 if (l + len > 78)
1594 l = weston_log_continue("\n" STAMP_SPACE "%.*s",
1595 len, p);
1596 else
1597 l += weston_log_continue(" %.*s", len, p);
1598 for (p = end; isspace(*p); p++)
1599 ;
1600 }
1601 weston_log_continue("\n");
1602}
1603
1604static void
1605log_egl_gl_info(EGLDisplay egldpy)
1606{
1607 const char *str;
1608
1609 str = eglQueryString(egldpy, EGL_VERSION);
1610 weston_log("EGL version: %s\n", str ? str : "(null)");
1611
1612 str = eglQueryString(egldpy, EGL_VENDOR);
1613 weston_log("EGL vendor: %s\n", str ? str : "(null)");
1614
1615 str = eglQueryString(egldpy, EGL_CLIENT_APIS);
1616 weston_log("EGL client APIs: %s\n", str ? str : "(null)");
1617
1618 str = eglQueryString(egldpy, EGL_EXTENSIONS);
1619 log_extensions("EGL extensions", str ? str : "(null)");
1620
1621 str = (char *)glGetString(GL_VERSION);
1622 weston_log("GL version: %s\n", str ? str : "(null)");
1623
1624 str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
1625 weston_log("GLSL version: %s\n", str ? str : "(null)");
1626
1627 str = (char *)glGetString(GL_VENDOR);
1628 weston_log("GL vendor: %s\n", str ? str : "(null)");
1629
1630 str = (char *)glGetString(GL_RENDERER);
1631 weston_log("GL renderer: %s\n", str ? str : "(null)");
1632
1633 str = (char *)glGetString(GL_EXTENSIONS);
1634 log_extensions("GL extensions", str ? str : "(null)");
1635}
1636
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001637static void
1638log_egl_config_info(EGLDisplay egldpy, EGLConfig eglconfig)
1639{
1640 EGLint r, g, b, a;
1641
1642 weston_log("Chosen EGL config details:\n");
1643
1644 weston_log_continue(STAMP_SPACE "RGBA bits");
1645 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_RED_SIZE, &r) &&
1646 eglGetConfigAttrib(egldpy, eglconfig, EGL_GREEN_SIZE, &g) &&
1647 eglGetConfigAttrib(egldpy, eglconfig, EGL_BLUE_SIZE, &b) &&
1648 eglGetConfigAttrib(egldpy, eglconfig, EGL_ALPHA_SIZE, &a))
1649 weston_log_continue(": %d %d %d %d\n", r, g, b, a);
1650 else
1651 weston_log_continue(" unknown\n");
1652
1653 weston_log_continue(STAMP_SPACE "swap interval range");
1654 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_MIN_SWAP_INTERVAL, &a) &&
1655 eglGetConfigAttrib(egldpy, eglconfig, EGL_MAX_SWAP_INTERVAL, &b))
1656 weston_log_continue(": %d - %d\n", a, b);
1657 else
1658 weston_log_continue(" unknown\n");
1659}
1660
John Kåre Alsaker44154502012-11-13 19:10:20 +01001661static void
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05001662gl_renderer_output_set_border(struct weston_output *output,
1663 enum gl_renderer_border_side side,
1664 int32_t width, int32_t height,
1665 int32_t tex_width, unsigned char *data)
1666{
1667 struct gl_output_state *go = get_output_state(output);
1668
Jason Ekstrande5512d42014-02-04 21:36:38 -06001669 if (go->borders[side].width != width ||
1670 go->borders[side].height != height)
1671 /* In this case, we have to blow everything and do a full
1672 * repaint. */
1673 go->border_status |= BORDER_SIZE_CHANGED | BORDER_ALL_DIRTY;
1674
1675 if (data == NULL) {
1676 width = 0;
1677 height = 0;
1678 }
1679
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05001680 go->borders[side].width = width;
1681 go->borders[side].height = height;
1682 go->borders[side].tex_width = tex_width;
1683 go->borders[side].data = data;
Jason Ekstrande5512d42014-02-04 21:36:38 -06001684 go->border_status |= 1 << side;
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05001685}
1686
John Kåre Alsaker94659272012-11-13 19:10:18 +01001687static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001688gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001689
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001690static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001691gl_renderer_output_create(struct weston_output *output,
John Kåre Alsaker94659272012-11-13 19:10:18 +01001692 EGLNativeWindowType window)
1693{
1694 struct weston_compositor *ec = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001695 struct gl_renderer *gr = get_renderer(ec);
1696 struct gl_output_state *go = calloc(1, sizeof *go);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001697 int i;
John Kåre Alsaker94659272012-11-13 19:10:18 +01001698
1699 if (!go)
1700 return -1;
1701
1702 go->egl_surface =
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001703 eglCreateWindowSurface(gr->egl_display,
1704 gr->egl_config,
John Kåre Alsaker94659272012-11-13 19:10:18 +01001705 window, NULL);
1706
1707 if (go->egl_surface == EGL_NO_SURFACE) {
1708 weston_log("failed to create egl surface\n");
1709 free(go);
1710 return -1;
1711 }
1712
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001713 if (gr->egl_context == NULL)
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001714 if (gl_renderer_setup(ec, go->egl_surface) < 0) {
John Kåre Alsaker94659272012-11-13 19:10:18 +01001715 free(go);
1716 return -1;
1717 }
1718
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001719 for (i = 0; i < BUFFER_DAMAGE_COUNT; i++)
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001720 pixman_region32_init(&go->buffer_damage[i]);
1721
John Kåre Alsaker94659272012-11-13 19:10:18 +01001722 output->renderer_state = go;
1723
1724 return 0;
1725}
1726
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001727static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001728gl_renderer_output_destroy(struct weston_output *output)
John Kåre Alsaker94659272012-11-13 19:10:18 +01001729{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001730 struct gl_renderer *gr = get_renderer(output->compositor);
1731 struct gl_output_state *go = get_output_state(output);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001732 int i;
1733
1734 for (i = 0; i < 2; i++)
1735 pixman_region32_fini(&go->buffer_damage[i]);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001736
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001737 eglDestroySurface(gr->egl_display, go->egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001738
1739 free(go);
1740}
1741
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001742static EGLSurface
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001743gl_renderer_output_surface(struct weston_output *output)
John Kåre Alsaker94659272012-11-13 19:10:18 +01001744{
1745 return get_output_state(output)->egl_surface;
1746}
1747
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03001748static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001749gl_renderer_destroy(struct weston_compositor *ec)
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001750{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001751 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001752
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03001753 wl_signal_emit(&gr->destroy_signal, gr);
1754
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001755 if (gr->has_bind_display)
1756 gr->unbind_display(gr->egl_display, ec->wl_display);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001757
1758 /* Work around crash in egl_dri2.c's dri2_make_current() - when does this apply? */
1759 eglMakeCurrent(gr->egl_display,
1760 EGL_NO_SURFACE, EGL_NO_SURFACE,
1761 EGL_NO_CONTEXT);
1762
1763 eglTerminate(gr->egl_display);
1764 eglReleaseThread();
Scott Moreau976a0502013-03-07 10:15:17 -07001765
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -04001766 wl_array_release(&gr->vertices);
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -04001767 wl_array_release(&gr->vtxcnt);
1768
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03001769 weston_binding_destroy(gr->fragment_binding);
1770 weston_binding_destroy(gr->fan_binding);
1771
Scott Moreau976a0502013-03-07 10:15:17 -07001772 free(gr);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001773}
1774
1775static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001776egl_choose_config(struct gl_renderer *gr, const EGLint *attribs,
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001777 const EGLint *visual_id)
1778{
1779 EGLint count = 0;
1780 EGLint matched = 0;
1781 EGLConfig *configs;
1782 int i;
1783
1784 if (!eglGetConfigs(gr->egl_display, NULL, 0, &count) || count < 1)
1785 return -1;
1786
1787 configs = calloc(count, sizeof *configs);
1788 if (!configs)
1789 return -1;
1790
1791 if (!eglChooseConfig(gr->egl_display, attribs, configs,
1792 count, &matched))
1793 goto out;
1794
1795 for (i = 0; i < matched; ++i) {
1796 EGLint id;
1797
1798 if (visual_id) {
1799 if (!eglGetConfigAttrib(gr->egl_display,
1800 configs[i], EGL_NATIVE_VISUAL_ID,
1801 &id))
1802 continue;
1803
Kristian Høgsbergc3ea26c2013-09-25 15:46:42 -07001804 if (id != 0 && id != *visual_id)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001805 continue;
1806 }
1807
1808 gr->egl_config = configs[i];
1809
1810 free(configs);
1811 return 0;
1812 }
1813
1814out:
1815 free(configs);
1816 return -1;
1817}
1818
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001819static const EGLint gl_renderer_opaque_attribs[] = {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001820 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1821 EGL_RED_SIZE, 1,
1822 EGL_GREEN_SIZE, 1,
1823 EGL_BLUE_SIZE, 1,
1824 EGL_ALPHA_SIZE, 0,
1825 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1826 EGL_NONE
1827};
1828
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001829static const EGLint gl_renderer_alpha_attribs[] = {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001830 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1831 EGL_RED_SIZE, 1,
1832 EGL_GREEN_SIZE, 1,
1833 EGL_BLUE_SIZE, 1,
1834 EGL_ALPHA_SIZE, 1,
1835 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1836 EGL_NONE
1837};
1838
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001839static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001840gl_renderer_create(struct weston_compositor *ec, EGLNativeDisplayType display,
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001841 const EGLint *attribs, const EGLint *visual_id)
1842{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001843 struct gl_renderer *gr;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001844 EGLint major, minor;
1845
1846 gr = calloc(1, sizeof *gr);
1847
1848 if (gr == NULL)
1849 return -1;
1850
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001851 gr->base.read_pixels = gl_renderer_read_pixels;
1852 gr->base.repaint_output = gl_renderer_repaint_output;
1853 gr->base.flush_damage = gl_renderer_flush_damage;
1854 gr->base.attach = gl_renderer_attach;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001855 gr->base.surface_set_color = gl_renderer_surface_set_color;
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03001856 gr->base.destroy = gl_renderer_destroy;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001857
1858 gr->egl_display = eglGetDisplay(display);
1859 if (gr->egl_display == EGL_NO_DISPLAY) {
1860 weston_log("failed to create display\n");
1861 goto err_egl;
1862 }
1863
1864 if (!eglInitialize(gr->egl_display, &major, &minor)) {
1865 weston_log("failed to initialize display\n");
1866 goto err_egl;
1867 }
1868
1869 if (egl_choose_config(gr, attribs, visual_id) < 0) {
1870 weston_log("failed to choose EGL config\n");
1871 goto err_egl;
1872 }
1873
1874 ec->renderer = &gr->base;
Pekka Paalanen7bb65102013-05-22 18:03:04 +03001875 ec->capabilities |= WESTON_CAP_ROTATION_ANY;
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03001876 ec->capabilities |= WESTON_CAP_CAPTURE_YFLIP;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001877
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001878 wl_display_add_shm_format(ec->wl_display, WL_SHM_FORMAT_RGB565);
1879
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03001880 wl_signal_init(&gr->destroy_signal);
1881
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001882 return 0;
1883
1884err_egl:
Pekka Paalanen326529f2012-11-27 12:25:25 +02001885 gl_renderer_print_egl_error_state();
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001886 free(gr);
1887 return -1;
1888}
1889
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001890static EGLDisplay
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001891gl_renderer_display(struct weston_compositor *ec)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001892{
1893 return get_renderer(ec)->egl_display;
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001894}
1895
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001896static int
1897compile_shaders(struct weston_compositor *ec)
1898{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001899 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker40684142012-11-13 19:10:25 +01001900
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03001901 gr->texture_shader_rgba.vertex_source = vertex_shader;
1902 gr->texture_shader_rgba.fragment_source = texture_fragment_shader_rgba;
1903
1904 gr->texture_shader_rgbx.vertex_source = vertex_shader;
1905 gr->texture_shader_rgbx.fragment_source = texture_fragment_shader_rgbx;
1906
1907 gr->texture_shader_egl_external.vertex_source = vertex_shader;
1908 gr->texture_shader_egl_external.fragment_source =
1909 texture_fragment_shader_egl_external;
1910
1911 gr->texture_shader_y_uv.vertex_source = vertex_shader;
1912 gr->texture_shader_y_uv.fragment_source = texture_fragment_shader_y_uv;
1913
1914 gr->texture_shader_y_u_v.vertex_source = vertex_shader;
1915 gr->texture_shader_y_u_v.fragment_source =
1916 texture_fragment_shader_y_u_v;
1917
Ander Conselvan de Oliveira41a50ea2013-11-27 17:43:51 +02001918 gr->texture_shader_y_xuxv.vertex_source = vertex_shader;
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03001919 gr->texture_shader_y_xuxv.fragment_source =
1920 texture_fragment_shader_y_xuxv;
1921
1922 gr->solid_shader.vertex_source = vertex_shader;
1923 gr->solid_shader.fragment_source = solid_fragment_shader;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001924
1925 return 0;
1926}
1927
1928static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04001929fragment_debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001930 void *data)
1931{
1932 struct weston_compositor *ec = data;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001933 struct gl_renderer *gr = get_renderer(ec);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001934 struct weston_output *output;
1935
John Kåre Alsaker40684142012-11-13 19:10:25 +01001936 gr->fragment_shader_debug ^= 1;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001937
John Kåre Alsaker40684142012-11-13 19:10:25 +01001938 shader_release(&gr->texture_shader_rgba);
1939 shader_release(&gr->texture_shader_rgbx);
1940 shader_release(&gr->texture_shader_egl_external);
1941 shader_release(&gr->texture_shader_y_uv);
1942 shader_release(&gr->texture_shader_y_u_v);
1943 shader_release(&gr->texture_shader_y_xuxv);
1944 shader_release(&gr->solid_shader);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001945
Ander Conselvan de Oliveira03fb4ef2012-12-03 17:08:11 +02001946 /* Force use_shader() to call glUseProgram(), since we need to use
1947 * the recompiled version of the shader. */
1948 gr->current_shader = NULL;
1949
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001950 wl_list_for_each(output, &ec->output_list, link)
1951 weston_output_damage(output);
1952}
1953
Kristian Høgsberg8799d412013-05-07 10:50:09 -04001954static void
1955fan_debug_repaint_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
1956 void *data)
1957{
1958 struct weston_compositor *compositor = data;
1959 struct gl_renderer *gr = get_renderer(compositor);
1960
1961 gr->fan_debug = !gr->fan_debug;
1962 weston_compositor_damage_all(compositor);
1963}
1964
John Kåre Alsaker94659272012-11-13 19:10:18 +01001965static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001966gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001967{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001968 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001969 const char *extensions;
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001970 EGLBoolean ret;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001971
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001972 static const EGLint context_attribs[] = {
1973 EGL_CONTEXT_CLIENT_VERSION, 2,
1974 EGL_NONE
1975 };
1976
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001977 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
1978 weston_log("failed to bind EGL_OPENGL_ES_API\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02001979 gl_renderer_print_egl_error_state();
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001980 return -1;
1981 }
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001982
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001983 log_egl_config_info(gr->egl_display, gr->egl_config);
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001984
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001985 gr->egl_context = eglCreateContext(gr->egl_display, gr->egl_config,
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001986 EGL_NO_CONTEXT, context_attribs);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001987 if (gr->egl_context == NULL) {
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001988 weston_log("failed to create context\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02001989 gl_renderer_print_egl_error_state();
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001990 return -1;
1991 }
1992
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001993 ret = eglMakeCurrent(gr->egl_display, egl_surface,
1994 egl_surface, gr->egl_context);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001995 if (ret == EGL_FALSE) {
1996 weston_log("Failed to make EGL context current.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02001997 gl_renderer_print_egl_error_state();
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001998 return -1;
1999 }
2000
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002001 log_egl_gl_info(gr->egl_display);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002002
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002003 gr->image_target_texture_2d =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002004 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002005 gr->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2006 gr->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2007 gr->bind_display =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002008 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002009 gr->unbind_display =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002010 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002011 gr->query_buffer =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002012 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
2013
2014 extensions = (const char *) glGetString(GL_EXTENSIONS);
2015 if (!extensions) {
2016 weston_log("Retrieving GL extension string failed.\n");
2017 return -1;
2018 }
2019
2020 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
2021 weston_log("GL_EXT_texture_format_BGRA8888 not available\n");
2022 return -1;
2023 }
2024
2025 if (strstr(extensions, "GL_EXT_read_format_bgra"))
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01002026 ec->read_format = PIXMAN_a8r8g8b8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002027 else
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01002028 ec->read_format = PIXMAN_a8b8g8r8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002029
Kristian Høgsberg1c4f1632013-08-07 12:11:27 -07002030#ifdef GL_EXT_unpack_subimage
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002031 if (strstr(extensions, "GL_EXT_unpack_subimage"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002032 gr->has_unpack_subimage = 1;
Kristian Høgsberg1c4f1632013-08-07 12:11:27 -07002033#endif
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002034
2035 if (strstr(extensions, "GL_OES_EGL_image_external"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002036 gr->has_egl_image_external = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002037
2038 extensions =
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002039 (const char *) eglQueryString(gr->egl_display, EGL_EXTENSIONS);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002040 if (!extensions) {
2041 weston_log("Retrieving EGL extension string failed.\n");
2042 return -1;
2043 }
2044
2045 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002046 gr->has_bind_display = 1;
2047 if (gr->has_bind_display) {
2048 ret = gr->bind_display(gr->egl_display, ec->wl_display);
Pekka Paalanen035a0322012-10-24 09:43:06 +03002049 if (!ret)
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002050 gr->has_bind_display = 0;
Pekka Paalanen035a0322012-10-24 09:43:06 +03002051 }
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002052
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02002053 if (strstr(extensions, "EGL_EXT_buffer_age"))
2054 gr->has_egl_buffer_age = 1;
2055 else
2056 weston_log("warning: EGL_EXT_buffer_age not supported. "
2057 "Performance could be affected.\n");
2058
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -06002059#ifdef EGL_EXT_swap_buffers_with_damage
2060 if (strstr(extensions, "EGL_EXT_swap_buffers_with_damage"))
2061 gr->swap_buffers_with_damage =
2062 (void *) eglGetProcAddress("eglSwapBuffersWithDamageEXT");
2063 else
2064 weston_log("warning: EGL_EXT_swap_buffers_with_damage not "
2065 "supported. Performance could be affected.\n");
2066#endif
2067
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002068 glActiveTexture(GL_TEXTURE0);
2069
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002070 if (compile_shaders(ec))
2071 return -1;
2072
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03002073 gr->fragment_binding =
2074 weston_compositor_add_debug_binding(ec, KEY_S,
2075 fragment_debug_binding,
2076 ec);
2077 gr->fan_binding =
2078 weston_compositor_add_debug_binding(ec, KEY_F,
2079 fan_debug_repaint_binding,
2080 ec);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002081
Pekka Paalanen035a0322012-10-24 09:43:06 +03002082 weston_log("GL ES 2 renderer features:\n");
2083 weston_log_continue(STAMP_SPACE "read-back format: %s\n",
Pekka Paalanenfe4eacf2013-01-10 16:50:42 +02002084 ec->read_format == PIXMAN_a8r8g8b8 ? "BGRA" : "RGBA");
Pekka Paalanen035a0322012-10-24 09:43:06 +03002085 weston_log_continue(STAMP_SPACE "wl_shm sub-image to texture: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002086 gr->has_unpack_subimage ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03002087 weston_log_continue(STAMP_SPACE "EGL Wayland extension: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002088 gr->has_bind_display ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03002089
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002090
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002091 return 0;
2092}
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002093
2094WL_EXPORT struct gl_renderer_interface gl_renderer_interface = {
2095 .opaque_attribs = gl_renderer_opaque_attribs,
2096 .alpha_attribs = gl_renderer_alpha_attribs,
2097
2098 .create = gl_renderer_create,
2099 .display = gl_renderer_display,
2100 .output_create = gl_renderer_output_create,
2101 .output_destroy = gl_renderer_output_destroy,
2102 .output_surface = gl_renderer_output_surface,
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05002103 .output_set_border = gl_renderer_output_set_border,
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002104 .print_egl_error_state = gl_renderer_print_egl_error_state
2105};