blob: 43fa39d97299bdc7796dda2bfcce8d091dfb4f43 [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
Kristian Høgsberg25894fc2012-09-05 22:06:26 -040023#define _GNU_SOURCE
24
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"
36
37#include <EGL/eglext.h>
38#include "weston-egl-ext.h"
Kristian Høgsbergd7c17262012-09-05 21:54:15 -040039
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010040struct gl_shader {
John Kåre Alsaker40684142012-11-13 19:10:25 +010041 GLuint program;
42 GLuint vertex_shader, fragment_shader;
43 GLint proj_uniform;
44 GLint tex_uniforms[3];
45 GLint alpha_uniform;
46 GLint color_uniform;
47};
48
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +020049#define BUFFER_DAMAGE_COUNT 2
50
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010051struct gl_output_state {
John Kåre Alsaker94659272012-11-13 19:10:18 +010052 EGLSurface egl_surface;
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +020053 pixman_region32_t buffer_damage[BUFFER_DAMAGE_COUNT];
John Kåre Alsaker94659272012-11-13 19:10:18 +010054};
55
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010056struct gl_surface_state {
John Kåre Alsaker878f4492012-11-13 19:10:23 +010057 GLfloat color[4];
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010058 struct gl_shader *shader;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +010059
60 GLuint textures[3];
61 int num_textures;
Pekka Paalanen81ee3f52012-12-04 15:58:16 +020062 pixman_region32_t texture_damage;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +010063
64 EGLImageKHR images[3];
65 GLenum target;
66 int num_images;
Pekka Paalanenfb003d32012-12-04 15:58:13 +020067
68 struct weston_buffer_reference buffer_ref;
Pekka Paalanen68033ac2012-12-04 15:58:15 +020069 int pitch; /* in pixels */
John Kåre Alsaker878f4492012-11-13 19:10:23 +010070};
71
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010072struct gl_renderer {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +010073 struct weston_renderer base;
74 int fragment_shader_debug;
Kristian Høgsberg8799d412013-05-07 10:50:09 -040075 int fan_debug;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +010076
77 EGLDisplay egl_display;
78 EGLContext egl_context;
79 EGLConfig egl_config;
John Kåre Alsaker44154502012-11-13 19:10:20 +010080
81 struct {
82 int32_t top, bottom, left, right;
83 GLuint texture;
84 int32_t width, height;
85 } border;
John Kåre Alsaker40684142012-11-13 19:10:25 +010086
John Kåre Alsaker320711d2012-11-13 19:10:27 +010087 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
88 PFNEGLCREATEIMAGEKHRPROC create_image;
89 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
90
91 int has_unpack_subimage;
92
93 PFNEGLBINDWAYLANDDISPLAYWL bind_display;
94 PFNEGLUNBINDWAYLANDDISPLAYWL unbind_display;
95 PFNEGLQUERYWAYLANDBUFFERWL query_buffer;
96 int has_bind_display;
97
98 int has_egl_image_external;
99
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +0200100 int has_egl_buffer_age;
101
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100102 struct gl_shader texture_shader_rgba;
103 struct gl_shader texture_shader_rgbx;
104 struct gl_shader texture_shader_egl_external;
105 struct gl_shader texture_shader_y_uv;
106 struct gl_shader texture_shader_y_u_v;
107 struct gl_shader texture_shader_y_xuxv;
108 struct gl_shader invert_color_shader;
109 struct gl_shader solid_shader;
110 struct gl_shader *current_shader;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100111};
John Kåre Alsaker94659272012-11-13 19:10:18 +0100112
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100113static inline struct gl_output_state *
John Kåre Alsaker94659272012-11-13 19:10:18 +0100114get_output_state(struct weston_output *output)
115{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100116 return (struct gl_output_state *)output->renderer_state;
John Kåre Alsaker94659272012-11-13 19:10:18 +0100117}
118
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100119static inline struct gl_surface_state *
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100120get_surface_state(struct weston_surface *surface)
121{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100122 return (struct gl_surface_state *)surface->renderer_state;
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100123}
124
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100125static inline struct gl_renderer *
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100126get_renderer(struct weston_compositor *ec)
127{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100128 return (struct gl_renderer *)ec->renderer;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100129}
130
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400131static const char *
132egl_error_string(EGLint code)
133{
134#define MYERRCODE(x) case x: return #x;
135 switch (code) {
136 MYERRCODE(EGL_SUCCESS)
137 MYERRCODE(EGL_NOT_INITIALIZED)
138 MYERRCODE(EGL_BAD_ACCESS)
139 MYERRCODE(EGL_BAD_ALLOC)
140 MYERRCODE(EGL_BAD_ATTRIBUTE)
141 MYERRCODE(EGL_BAD_CONTEXT)
142 MYERRCODE(EGL_BAD_CONFIG)
143 MYERRCODE(EGL_BAD_CURRENT_SURFACE)
144 MYERRCODE(EGL_BAD_DISPLAY)
145 MYERRCODE(EGL_BAD_SURFACE)
146 MYERRCODE(EGL_BAD_MATCH)
147 MYERRCODE(EGL_BAD_PARAMETER)
148 MYERRCODE(EGL_BAD_NATIVE_PIXMAP)
149 MYERRCODE(EGL_BAD_NATIVE_WINDOW)
150 MYERRCODE(EGL_CONTEXT_LOST)
151 default:
152 return "unknown";
153 }
154#undef MYERRCODE
155}
156
Pekka Paalanen326529f2012-11-27 12:25:25 +0200157WL_EXPORT void
158gl_renderer_print_egl_error_state(void)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400159{
160 EGLint code;
161
162 code = eglGetError();
163 weston_log("EGL error state: %s (0x%04lx)\n",
164 egl_error_string(code), (long)code);
165}
166
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300167struct polygon8 {
168 GLfloat x[8];
169 GLfloat y[8];
170 int n;
171};
172
173struct clip_context {
174 struct {
175 GLfloat x;
176 GLfloat y;
177 } prev;
178
179 struct {
180 GLfloat x1, y1;
181 GLfloat x2, y2;
182 } clip;
183
184 struct {
185 GLfloat *x;
186 GLfloat *y;
187 } vertices;
188};
189
190static GLfloat
191float_difference(GLfloat a, GLfloat b)
192{
193 /* http://www.altdevblogaday.com/2012/02/22/comparing-floating-point-numbers-2012-edition/ */
194 static const GLfloat max_diff = 4.0f * FLT_MIN;
195 static const GLfloat max_rel_diff = 4.0e-5;
196 GLfloat diff = a - b;
197 GLfloat adiff = fabsf(diff);
198
199 if (adiff <= max_diff)
200 return 0.0f;
201
202 a = fabsf(a);
203 b = fabsf(b);
204 if (adiff <= (a > b ? a : b) * max_rel_diff)
205 return 0.0f;
206
207 return diff;
208}
209
210/* A line segment (p1x, p1y)-(p2x, p2y) intersects the line x = x_arg.
211 * Compute the y coordinate of the intersection.
212 */
213static GLfloat
214clip_intersect_y(GLfloat p1x, GLfloat p1y, GLfloat p2x, GLfloat p2y,
215 GLfloat x_arg)
216{
217 GLfloat a;
218 GLfloat diff = float_difference(p1x, p2x);
219
220 /* Practically vertical line segment, yet the end points have already
221 * been determined to be on different sides of the line. Therefore
222 * the line segment is part of the line and intersects everywhere.
223 * Return the end point, so we use the whole line segment.
224 */
225 if (diff == 0.0f)
226 return p2y;
227
228 a = (x_arg - p2x) / diff;
229 return p2y + (p1y - p2y) * a;
230}
231
232/* A line segment (p1x, p1y)-(p2x, p2y) intersects the line y = y_arg.
233 * Compute the x coordinate of the intersection.
234 */
235static GLfloat
236clip_intersect_x(GLfloat p1x, GLfloat p1y, GLfloat p2x, GLfloat p2y,
237 GLfloat y_arg)
238{
239 GLfloat a;
240 GLfloat diff = float_difference(p1y, p2y);
241
242 /* Practically horizontal line segment, yet the end points have already
243 * been determined to be on different sides of the line. Therefore
244 * the line segment is part of the line and intersects everywhere.
245 * Return the end point, so we use the whole line segment.
246 */
247 if (diff == 0.0f)
248 return p2x;
249
250 a = (y_arg - p2y) / diff;
251 return p2x + (p1x - p2x) * a;
252}
253
254enum path_transition {
255 PATH_TRANSITION_OUT_TO_OUT = 0,
256 PATH_TRANSITION_OUT_TO_IN = 1,
257 PATH_TRANSITION_IN_TO_OUT = 2,
258 PATH_TRANSITION_IN_TO_IN = 3,
259};
260
261static void
262clip_append_vertex(struct clip_context *ctx, GLfloat x, GLfloat y)
263{
264 *ctx->vertices.x++ = x;
265 *ctx->vertices.y++ = y;
266}
267
268static enum path_transition
269path_transition_left_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
270{
271 return ((ctx->prev.x >= ctx->clip.x1) << 1) | (x >= ctx->clip.x1);
272}
273
274static enum path_transition
275path_transition_right_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
276{
277 return ((ctx->prev.x < ctx->clip.x2) << 1) | (x < ctx->clip.x2);
278}
279
280static enum path_transition
281path_transition_top_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
282{
283 return ((ctx->prev.y >= ctx->clip.y1) << 1) | (y >= ctx->clip.y1);
284}
285
286static enum path_transition
287path_transition_bottom_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
288{
289 return ((ctx->prev.y < ctx->clip.y2) << 1) | (y < ctx->clip.y2);
290}
291
292static void
293clip_polygon_leftright(struct clip_context *ctx,
294 enum path_transition transition,
295 GLfloat x, GLfloat y, GLfloat clip_x)
296{
297 GLfloat yi;
298
299 switch (transition) {
300 case PATH_TRANSITION_IN_TO_IN:
301 clip_append_vertex(ctx, x, y);
302 break;
303 case PATH_TRANSITION_IN_TO_OUT:
304 yi = clip_intersect_y(ctx->prev.x, ctx->prev.y, x, y, clip_x);
305 clip_append_vertex(ctx, clip_x, yi);
306 break;
307 case PATH_TRANSITION_OUT_TO_IN:
308 yi = clip_intersect_y(ctx->prev.x, ctx->prev.y, x, y, clip_x);
309 clip_append_vertex(ctx, clip_x, yi);
310 clip_append_vertex(ctx, x, y);
311 break;
312 case PATH_TRANSITION_OUT_TO_OUT:
313 /* nothing */
314 break;
315 default:
316 assert(0 && "bad enum path_transition");
317 }
318
319 ctx->prev.x = x;
320 ctx->prev.y = y;
321}
322
323static void
324clip_polygon_topbottom(struct clip_context *ctx,
325 enum path_transition transition,
326 GLfloat x, GLfloat y, GLfloat clip_y)
327{
328 GLfloat xi;
329
330 switch (transition) {
331 case PATH_TRANSITION_IN_TO_IN:
332 clip_append_vertex(ctx, x, y);
333 break;
334 case PATH_TRANSITION_IN_TO_OUT:
335 xi = clip_intersect_x(ctx->prev.x, ctx->prev.y, x, y, clip_y);
336 clip_append_vertex(ctx, xi, clip_y);
337 break;
338 case PATH_TRANSITION_OUT_TO_IN:
339 xi = clip_intersect_x(ctx->prev.x, ctx->prev.y, x, y, clip_y);
340 clip_append_vertex(ctx, xi, clip_y);
341 clip_append_vertex(ctx, x, y);
342 break;
343 case PATH_TRANSITION_OUT_TO_OUT:
344 /* nothing */
345 break;
346 default:
347 assert(0 && "bad enum path_transition");
348 }
349
350 ctx->prev.x = x;
351 ctx->prev.y = y;
352}
353
354static void
355clip_context_prepare(struct clip_context *ctx, const struct polygon8 *src,
356 GLfloat *dst_x, GLfloat *dst_y)
357{
358 ctx->prev.x = src->x[src->n - 1];
359 ctx->prev.y = src->y[src->n - 1];
360 ctx->vertices.x = dst_x;
361 ctx->vertices.y = dst_y;
362}
363
364static int
365clip_polygon_left(struct clip_context *ctx, const struct polygon8 *src,
366 GLfloat *dst_x, GLfloat *dst_y)
367{
368 enum path_transition trans;
369 int i;
370
371 clip_context_prepare(ctx, src, dst_x, dst_y);
372 for (i = 0; i < src->n; i++) {
373 trans = path_transition_left_edge(ctx, src->x[i], src->y[i]);
374 clip_polygon_leftright(ctx, trans, src->x[i], src->y[i],
375 ctx->clip.x1);
376 }
377 return ctx->vertices.x - dst_x;
378}
379
380static int
381clip_polygon_right(struct clip_context *ctx, const struct polygon8 *src,
382 GLfloat *dst_x, GLfloat *dst_y)
383{
384 enum path_transition trans;
385 int i;
386
387 clip_context_prepare(ctx, src, dst_x, dst_y);
388 for (i = 0; i < src->n; i++) {
389 trans = path_transition_right_edge(ctx, src->x[i], src->y[i]);
390 clip_polygon_leftright(ctx, trans, src->x[i], src->y[i],
391 ctx->clip.x2);
392 }
393 return ctx->vertices.x - dst_x;
394}
395
396static int
397clip_polygon_top(struct clip_context *ctx, const struct polygon8 *src,
398 GLfloat *dst_x, GLfloat *dst_y)
399{
400 enum path_transition trans;
401 int i;
402
403 clip_context_prepare(ctx, src, dst_x, dst_y);
404 for (i = 0; i < src->n; i++) {
405 trans = path_transition_top_edge(ctx, src->x[i], src->y[i]);
406 clip_polygon_topbottom(ctx, trans, src->x[i], src->y[i],
407 ctx->clip.y1);
408 }
409 return ctx->vertices.x - dst_x;
410}
411
412static int
413clip_polygon_bottom(struct clip_context *ctx, const struct polygon8 *src,
414 GLfloat *dst_x, GLfloat *dst_y)
415{
416 enum path_transition trans;
417 int i;
418
419 clip_context_prepare(ctx, src, dst_x, dst_y);
420 for (i = 0; i < src->n; i++) {
421 trans = path_transition_bottom_edge(ctx, src->x[i], src->y[i]);
422 clip_polygon_topbottom(ctx, trans, src->x[i], src->y[i],
423 ctx->clip.y2);
424 }
425 return ctx->vertices.x - dst_x;
426}
427
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400428#define max(a, b) (((a) > (b)) ? (a) : (b))
429#define min(a, b) (((a) > (b)) ? (b) : (a))
430#define clip(x, a, b) min(max(x, a), b)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400431
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300432/*
433 * Compute the boundary vertices of the intersection of the global coordinate
434 * aligned rectangle 'rect', and an arbitrary quadrilateral produced from
435 * 'surf_rect' when transformed from surface coordinates into global coordinates.
436 * The vertices are written to 'ex' and 'ey', and the return value is the
437 * number of vertices. Vertices are produced in clockwise winding order.
438 * Guarantees to produce either zero vertices, or 3-8 vertices with non-zero
439 * polygon area.
440 */
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400441static int
442calculate_edges(struct weston_surface *es, pixman_box32_t *rect,
443 pixman_box32_t *surf_rect, GLfloat *ex, GLfloat *ey)
444{
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300445 struct polygon8 polygon;
446 struct clip_context ctx;
447 int i, n;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400448 GLfloat min_x, max_x, min_y, max_y;
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300449 struct polygon8 surf = {
450 { surf_rect->x1, surf_rect->x2, surf_rect->x2, surf_rect->x1 },
451 { surf_rect->y1, surf_rect->y1, surf_rect->y2, surf_rect->y2 },
452 4
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400453 };
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400454
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300455 ctx.clip.x1 = rect->x1;
456 ctx.clip.y1 = rect->y1;
457 ctx.clip.x2 = rect->x2;
458 ctx.clip.y2 = rect->y2;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400459
460 /* transform surface to screen space: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300461 for (i = 0; i < surf.n; i++)
462 weston_surface_to_global_float(es, surf.x[i], surf.y[i],
463 &surf.x[i], &surf.y[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400464
465 /* find bounding box: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300466 min_x = max_x = surf.x[0];
467 min_y = max_y = surf.y[0];
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400468
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300469 for (i = 1; i < surf.n; i++) {
470 min_x = min(min_x, surf.x[i]);
471 max_x = max(max_x, surf.x[i]);
472 min_y = min(min_y, surf.y[i]);
473 max_y = max(max_y, surf.y[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400474 }
475
476 /* First, simple bounding box check to discard early transformed
477 * surface rects that do not intersect with the clip region:
478 */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300479 if ((min_x >= ctx.clip.x2) || (max_x <= ctx.clip.x1) ||
480 (min_y >= ctx.clip.y2) || (max_y <= ctx.clip.y1))
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400481 return 0;
482
483 /* Simple case, bounding box edges are parallel to surface edges,
484 * there will be only four edges. We just need to clip the surface
485 * vertices to the clip rect bounds:
486 */
487 if (!es->transform.enabled) {
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300488 for (i = 0; i < surf.n; i++) {
489 ex[i] = clip(surf.x[i], ctx.clip.x1, ctx.clip.x2);
490 ey[i] = clip(surf.y[i], ctx.clip.y1, ctx.clip.y2);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400491 }
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300492 return surf.n;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400493 }
494
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300495 /* Transformed case: use a general polygon clipping algorithm to
496 * clip the surface rectangle with each side of 'rect'.
497 * The algorithm is Sutherland-Hodgman, as explained in
498 * http://www.codeguru.com/cpp/misc/misc/graphics/article.php/c8965/Polygon-Clipping.htm
499 * but without looking at any of that code.
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400500 */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300501 polygon.n = clip_polygon_left(&ctx, &surf, polygon.x, polygon.y);
502 surf.n = clip_polygon_right(&ctx, &polygon, surf.x, surf.y);
503 polygon.n = clip_polygon_top(&ctx, &surf, polygon.x, polygon.y);
504 surf.n = clip_polygon_bottom(&ctx, &polygon, surf.x, surf.y);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400505
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300506 /* Get rid of duplicate vertices */
507 ex[0] = surf.x[0];
508 ey[0] = surf.y[0];
509 n = 1;
510 for (i = 1; i < surf.n; i++) {
511 if (float_difference(ex[n - 1], surf.x[i]) == 0.0f &&
512 float_difference(ey[n - 1], surf.y[i]) == 0.0f)
513 continue;
514 ex[n] = surf.x[i];
515 ey[n] = surf.y[i];
516 n++;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400517 }
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300518 if (float_difference(ex[n - 1], surf.x[0]) == 0.0f &&
519 float_difference(ey[n - 1], surf.y[0]) == 0.0f)
520 n--;
521
522 if (n < 3)
523 return 0;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400524
525 return n;
526}
527
528static int
529texture_region(struct weston_surface *es, pixman_region32_t *region,
530 pixman_region32_t *surf_region)
531{
Pekka Paalanen68033ac2012-12-04 15:58:15 +0200532 struct gl_surface_state *gs = get_surface_state(es);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400533 struct weston_compositor *ec = es->compositor;
534 GLfloat *v, inv_width, inv_height;
535 unsigned int *vtxcnt, nvtx = 0;
536 pixman_box32_t *rects, *surf_rects;
537 int i, j, k, nrects, nsurf;
538
539 rects = pixman_region32_rectangles(region, &nrects);
540 surf_rects = pixman_region32_rectangles(surf_region, &nsurf);
541
542 /* worst case we can have 8 vertices per rect (ie. clipped into
543 * an octagon):
544 */
545 v = wl_array_add(&ec->vertices, nrects * nsurf * 8 * 4 * sizeof *v);
546 vtxcnt = wl_array_add(&ec->vtxcnt, nrects * nsurf * sizeof *vtxcnt);
547
Pekka Paalanen68033ac2012-12-04 15:58:15 +0200548 inv_width = 1.0 / gs->pitch;
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200549
550 switch (es->buffer_transform) {
551 case WL_OUTPUT_TRANSFORM_90:
552 case WL_OUTPUT_TRANSFORM_270:
553 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
554 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
555 inv_height = 1.0 / es->geometry.width;
556 break;
557 default:
558 inv_height = 1.0 / es->geometry.height;
559 }
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400560
561 for (i = 0; i < nrects; i++) {
562 pixman_box32_t *rect = &rects[i];
563 for (j = 0; j < nsurf; j++) {
564 pixman_box32_t *surf_rect = &surf_rects[j];
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200565 GLfloat sx, sy, bx, by;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400566 GLfloat ex[8], ey[8]; /* edge points in screen space */
567 int n;
568
569 /* The transformed surface, after clipping to the clip region,
570 * can have as many as eight sides, emitted as a triangle-fan.
571 * The first vertex in the triangle fan can be chosen arbitrarily,
572 * since the area is guaranteed to be convex.
573 *
574 * If a corner of the transformed surface falls outside of the
575 * clip region, instead of emitting one vertex for the corner
576 * of the surface, up to two are emitted for two corresponding
577 * intersection point(s) between the surface and the clip region.
578 *
579 * To do this, we first calculate the (up to eight) points that
580 * form the intersection of the clip rect and the transformed
581 * surface.
582 */
583 n = calculate_edges(es, rect, surf_rect, ex, ey);
584 if (n < 3)
585 continue;
586
587 /* emit edge points: */
588 for (k = 0; k < n; k++) {
589 weston_surface_from_global_float(es, ex[k], ey[k], &sx, &sy);
590 /* position: */
591 *(v++) = ex[k];
592 *(v++) = ey[k];
593 /* texcoord: */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200594 weston_surface_to_buffer_float(es, sx, sy,
595 &bx, &by);
596 *(v++) = bx * inv_width;
597 *(v++) = by * inv_height;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400598 }
599
600 vtxcnt[nvtx++] = n;
601 }
602 }
603
604 return nvtx;
605}
606
607static void
608triangle_fan_debug(struct weston_surface *surface, int first, int count)
609{
610 struct weston_compositor *compositor = surface->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100611 struct gl_renderer *gr = get_renderer(compositor);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400612 int i;
613 GLushort *buffer;
614 GLushort *index;
615 int nelems;
616 static int color_idx = 0;
617 static const GLfloat color[][4] = {
618 { 1.0, 0.0, 0.0, 1.0 },
619 { 0.0, 1.0, 0.0, 1.0 },
620 { 0.0, 0.0, 1.0, 1.0 },
621 { 1.0, 1.0, 1.0, 1.0 },
622 };
623
624 nelems = (count - 1 + count - 2) * 2;
625
626 buffer = malloc(sizeof(GLushort) * nelems);
627 index = buffer;
628
629 for (i = 1; i < count; i++) {
630 *index++ = first;
631 *index++ = first + i;
632 }
633
634 for (i = 2; i < count; i++) {
635 *index++ = first + i - 1;
636 *index++ = first + i;
637 }
638
John Kåre Alsaker40684142012-11-13 19:10:25 +0100639 glUseProgram(gr->solid_shader.program);
640 glUniform4fv(gr->solid_shader.color_uniform, 1,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400641 color[color_idx++ % ARRAY_LENGTH(color)]);
642 glDrawElements(GL_LINES, nelems, GL_UNSIGNED_SHORT, buffer);
John Kåre Alsaker40684142012-11-13 19:10:25 +0100643 glUseProgram(gr->current_shader->program);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400644 free(buffer);
645}
646
647static void
648repaint_region(struct weston_surface *es, pixman_region32_t *region,
649 pixman_region32_t *surf_region)
650{
651 struct weston_compositor *ec = es->compositor;
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400652 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400653 GLfloat *v;
654 unsigned int *vtxcnt;
655 int i, first, nfans;
656
657 /* The final region to be painted is the intersection of
658 * 'region' and 'surf_region'. However, 'region' is in the global
659 * coordinates, and 'surf_region' is in the surface-local
660 * coordinates. texture_region() will iterate over all pairs of
661 * rectangles from both regions, compute the intersection
662 * polygon for each pair, and store it as a triangle fan if
663 * it has a non-zero area (at least 3 vertices, actually).
664 */
665 nfans = texture_region(es, region, surf_region);
666
667 v = ec->vertices.data;
668 vtxcnt = ec->vtxcnt.data;
669
670 /* position: */
671 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
672 glEnableVertexAttribArray(0);
673
674 /* texcoord: */
675 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
676 glEnableVertexAttribArray(1);
677
678 for (i = 0, first = 0; i < nfans; i++) {
679 glDrawArrays(GL_TRIANGLE_FAN, first, vtxcnt[i]);
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400680 if (gr->fan_debug)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400681 triangle_fan_debug(es, first, vtxcnt[i]);
682 first += vtxcnt[i];
683 }
684
685 glDisableVertexAttribArray(1);
686 glDisableVertexAttribArray(0);
687
688 ec->vertices.size = 0;
689 ec->vtxcnt.size = 0;
690}
691
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100692static int
693use_output(struct weston_output *output)
694{
695 static int errored;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100696 struct gl_output_state *go = get_output_state(output);
697 struct gl_renderer *gr = get_renderer(output->compositor);
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100698 EGLBoolean ret;
699
700 ret = eglMakeCurrent(gr->egl_display, go->egl_surface,
701 go->egl_surface, gr->egl_context);
702
703 if (ret == EGL_FALSE) {
704 if (errored)
705 return -1;
706 errored = 1;
707 weston_log("Failed to make EGL context current.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +0200708 gl_renderer_print_egl_error_state();
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100709 return -1;
710 }
711
712 return 0;
713}
714
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400715static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100716use_shader(struct gl_renderer *gr,
717 struct gl_shader *shader)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400718{
John Kåre Alsaker40684142012-11-13 19:10:25 +0100719 if (gr->current_shader == shader)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400720 return;
721
722 glUseProgram(shader->program);
John Kåre Alsaker40684142012-11-13 19:10:25 +0100723 gr->current_shader = shader;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400724}
725
726static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100727shader_uniforms(struct gl_shader *shader,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400728 struct weston_surface *surface,
729 struct weston_output *output)
730{
731 int i;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100732 struct gl_surface_state *gs = get_surface_state(surface);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400733
734 glUniformMatrix4fv(shader->proj_uniform,
735 1, GL_FALSE, output->matrix.d);
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100736 glUniform4fv(shader->color_uniform, 1, gs->color);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400737 glUniform1f(shader->alpha_uniform, surface->alpha);
738
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100739 for (i = 0; i < gs->num_textures; i++)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400740 glUniform1i(shader->tex_uniforms[i], i);
741}
742
743static void
744draw_surface(struct weston_surface *es, struct weston_output *output,
745 pixman_region32_t *damage) /* in global coordinates */
746{
747 struct weston_compositor *ec = es->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100748 struct gl_renderer *gr = get_renderer(ec);
749 struct gl_surface_state *gs = get_surface_state(es);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400750 /* repaint bounding region in global coordinates: */
751 pixman_region32_t repaint;
752 /* non-opaque region in surface coordinates: */
753 pixman_region32_t surface_blend;
754 GLint filter;
755 int i;
756
757 pixman_region32_init(&repaint);
758 pixman_region32_intersect(&repaint,
759 &es->transform.boundingbox, damage);
760 pixman_region32_subtract(&repaint, &repaint, &es->clip);
761
762 if (!pixman_region32_not_empty(&repaint))
763 goto out;
764
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400765 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
766
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400767 if (gr->fan_debug) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100768 use_shader(gr, &gr->solid_shader);
769 shader_uniforms(&gr->solid_shader, es, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400770 }
771
John Kåre Alsaker40684142012-11-13 19:10:25 +0100772 use_shader(gr, gs->shader);
773 shader_uniforms(gs->shader, es, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400774
775 if (es->transform.enabled || output->zoom.active)
776 filter = GL_LINEAR;
777 else
778 filter = GL_NEAREST;
779
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100780 for (i = 0; i < gs->num_textures; i++) {
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400781 glActiveTexture(GL_TEXTURE0 + i);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100782 glBindTexture(gs->target, gs->textures[i]);
783 glTexParameteri(gs->target, GL_TEXTURE_MIN_FILTER, filter);
784 glTexParameteri(gs->target, GL_TEXTURE_MAG_FILTER, filter);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400785 }
786
787 /* blended region is whole surface minus opaque region: */
788 pixman_region32_init_rect(&surface_blend, 0, 0,
789 es->geometry.width, es->geometry.height);
790 pixman_region32_subtract(&surface_blend, &surface_blend, &es->opaque);
791
792 if (pixman_region32_not_empty(&es->opaque)) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100793 if (gs->shader == &gr->texture_shader_rgba) {
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400794 /* Special case for RGBA textures with possibly
795 * bad data in alpha channel: use the shader
796 * that forces texture alpha = 1.0.
797 * Xwayland surfaces need this.
798 */
John Kåre Alsaker40684142012-11-13 19:10:25 +0100799 use_shader(gr, &gr->texture_shader_rgbx);
800 shader_uniforms(&gr->texture_shader_rgbx, es, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400801 }
802
803 if (es->alpha < 1.0)
804 glEnable(GL_BLEND);
805 else
806 glDisable(GL_BLEND);
807
808 repaint_region(es, &repaint, &es->opaque);
809 }
810
811 if (pixman_region32_not_empty(&surface_blend)) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100812 use_shader(gr, gs->shader);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400813 glEnable(GL_BLEND);
814 repaint_region(es, &repaint, &surface_blend);
815 }
816
817 pixman_region32_fini(&surface_blend);
818
819out:
820 pixman_region32_fini(&repaint);
821}
822
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400823static void
824repaint_surfaces(struct weston_output *output, pixman_region32_t *damage)
825{
826 struct weston_compositor *compositor = output->compositor;
827 struct weston_surface *surface;
828
829 wl_list_for_each_reverse(surface, &compositor->surface_list, link)
830 if (surface->plane == &compositor->primary_plane)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400831 draw_surface(surface, output, damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400832}
833
John Kåre Alsaker44154502012-11-13 19:10:20 +0100834
835static int
836texture_border(struct weston_output *output)
837{
838 struct weston_compositor *ec = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100839 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker44154502012-11-13 19:10:20 +0100840 GLfloat *d;
841 unsigned int *p;
842 int i, j, k, n;
843 GLfloat x[4], y[4], u[4], v[4];
844
845 x[0] = -gr->border.left;
846 x[1] = 0;
847 x[2] = output->current->width;
848 x[3] = output->current->width + gr->border.right;
849
850 y[0] = -gr->border.top;
851 y[1] = 0;
852 y[2] = output->current->height;
853 y[3] = output->current->height + gr->border.bottom;
854
855 u[0] = 0.0;
856 u[1] = (GLfloat) gr->border.left / gr->border.width;
857 u[2] = (GLfloat) (gr->border.width - gr->border.right) / gr->border.width;
858 u[3] = 1.0;
859
860 v[0] = 0.0;
861 v[1] = (GLfloat) gr->border.top / gr->border.height;
862 v[2] = (GLfloat) (gr->border.height - gr->border.bottom) / gr->border.height;
863 v[3] = 1.0;
864
865 n = 8;
866 d = wl_array_add(&ec->vertices, n * 16 * sizeof *d);
867 p = wl_array_add(&ec->indices, n * 6 * sizeof *p);
868
869 k = 0;
870 for (i = 0; i < 3; i++)
871 for (j = 0; j < 3; j++) {
872
873 if (i == 1 && j == 1)
874 continue;
875
876 d[ 0] = x[i];
877 d[ 1] = y[j];
878 d[ 2] = u[i];
879 d[ 3] = v[j];
880
881 d[ 4] = x[i];
882 d[ 5] = y[j + 1];
883 d[ 6] = u[i];
884 d[ 7] = v[j + 1];
885
886 d[ 8] = x[i + 1];
887 d[ 9] = y[j];
888 d[10] = u[i + 1];
889 d[11] = v[j];
890
891 d[12] = x[i + 1];
892 d[13] = y[j + 1];
893 d[14] = u[i + 1];
894 d[15] = v[j + 1];
895
896 p[0] = k + 0;
897 p[1] = k + 1;
898 p[2] = k + 2;
899 p[3] = k + 2;
900 p[4] = k + 1;
901 p[5] = k + 3;
902
903 d += 16;
904 p += 6;
905 k += 4;
906 }
907
908 return k / 4;
909}
910
911static void
912draw_border(struct weston_output *output)
913{
914 struct weston_compositor *ec = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100915 struct gl_renderer *gr = get_renderer(ec);
916 struct gl_shader *shader = &gr->texture_shader_rgba;
John Kåre Alsaker44154502012-11-13 19:10:20 +0100917 GLfloat *v;
918 int n;
919
920 glDisable(GL_BLEND);
John Kåre Alsaker40684142012-11-13 19:10:25 +0100921 use_shader(gr, shader);
John Kåre Alsaker44154502012-11-13 19:10:20 +0100922
923 glUniformMatrix4fv(shader->proj_uniform,
924 1, GL_FALSE, output->matrix.d);
925
926 glUniform1i(shader->tex_uniforms[0], 0);
927 glUniform1f(shader->alpha_uniform, 1);
928
929 n = texture_border(output);
930
931 glActiveTexture(GL_TEXTURE0);
932 glBindTexture(GL_TEXTURE_2D, gr->border.texture);
933
934 v = ec->vertices.data;
935 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
936 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
937 glEnableVertexAttribArray(0);
938 glEnableVertexAttribArray(1);
939
940 glDrawElements(GL_TRIANGLES, n * 6,
941 GL_UNSIGNED_INT, ec->indices.data);
942
943 glDisableVertexAttribArray(1);
944 glDisableVertexAttribArray(0);
945
946 ec->vertices.size = 0;
947 ec->indices.size = 0;
948}
949
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400950static void
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +0200951output_get_buffer_damage(struct weston_output *output,
952 pixman_region32_t *buffer_damage)
953{
954 struct gl_output_state *go = get_output_state(output);
955 struct gl_renderer *gr = get_renderer(output->compositor);
956 EGLint buffer_age = 0;
957 EGLBoolean ret;
958 int i;
959
960 if (gr->has_egl_buffer_age) {
961 ret = eglQuerySurface(gr->egl_display, go->egl_surface,
962 EGL_BUFFER_AGE_EXT, &buffer_age);
963 if (ret == EGL_FALSE) {
964 weston_log("buffer age query failed.\n");
965 gl_renderer_print_egl_error_state();
966 }
967 }
968
969 if (buffer_age == 0 || buffer_age - 1 > BUFFER_DAMAGE_COUNT)
970 pixman_region32_copy(buffer_damage, &output->region);
971 else
972 for (i = 0; i < buffer_age - 1; i++)
973 pixman_region32_union(buffer_damage, buffer_damage,
974 &go->buffer_damage[i]);
975}
976
977static void
978output_rotate_damage(struct weston_output *output,
979 pixman_region32_t *output_damage)
980{
981 struct gl_output_state *go = get_output_state(output);
982 struct gl_renderer *gr = get_renderer(output->compositor);
983 int i;
984
985 if (!gr->has_egl_buffer_age)
986 return;
987
988 for (i = BUFFER_DAMAGE_COUNT - 1; i >= 1; i--)
989 pixman_region32_copy(&go->buffer_damage[i],
990 &go->buffer_damage[i - 1]);
991
992 pixman_region32_copy(&go->buffer_damage[0], output_damage);
993}
994
995static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100996gl_renderer_repaint_output(struct weston_output *output,
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400997 pixman_region32_t *output_damage)
998{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100999 struct gl_output_state *go = get_output_state(output);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001000 struct weston_compositor *compositor = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001001 struct gl_renderer *gr = get_renderer(compositor);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001002 EGLBoolean ret;
1003 static int errored;
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001004 int32_t width, height;
1005 pixman_region32_t buffer_damage, total_damage;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001006
1007 width = output->current->width +
1008 output->border.left + output->border.right;
1009 height = output->current->height +
1010 output->border.top + output->border.bottom;
1011
1012 glViewport(0, 0, width, height);
1013
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001014 if (use_output(output) < 0)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001015 return;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001016
1017 /* if debugging, redraw everything outside the damage to clean up
1018 * debug lines from the previous draw on this buffer:
1019 */
Kristian Høgsberg8799d412013-05-07 10:50:09 -04001020 if (gr->fan_debug) {
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001021 pixman_region32_t undamaged;
1022 pixman_region32_init(&undamaged);
1023 pixman_region32_subtract(&undamaged, &output->region,
1024 output_damage);
Kristian Høgsberg8799d412013-05-07 10:50:09 -04001025 gr->fan_debug = 0;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001026 repaint_surfaces(output, &undamaged);
Kristian Høgsberg8799d412013-05-07 10:50:09 -04001027 gr->fan_debug = 1;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001028 pixman_region32_fini(&undamaged);
1029 }
1030
Ander Conselvan de Oliveirab605c062013-03-05 17:30:28 +02001031 pixman_region32_init(&total_damage);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001032 pixman_region32_init(&buffer_damage);
1033
1034 output_get_buffer_damage(output, &buffer_damage);
1035 output_rotate_damage(output, output_damage);
1036
1037 pixman_region32_union(&total_damage, &buffer_damage, output_damage);
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03001038
Ander Conselvan de Oliveirab605c062013-03-05 17:30:28 +02001039 repaint_surfaces(output, &total_damage);
1040
1041 pixman_region32_fini(&total_damage);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001042 pixman_region32_fini(&buffer_damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001043
John Kåre Alsaker44154502012-11-13 19:10:20 +01001044 if (gr->border.texture)
1045 draw_border(output);
1046
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001047 pixman_region32_copy(&output->previous_damage, output_damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001048 wl_signal_emit(&output->frame_signal, output);
1049
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001050 ret = eglSwapBuffers(gr->egl_display, go->egl_surface);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001051 if (ret == EGL_FALSE && !errored) {
1052 errored = 1;
1053 weston_log("Failed in eglSwapBuffers.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02001054 gl_renderer_print_egl_error_state();
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001055 }
1056
1057}
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001058
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001059static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001060gl_renderer_read_pixels(struct weston_output *output,
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001061 pixman_format_code_t format, void *pixels,
1062 uint32_t x, uint32_t y,
1063 uint32_t width, uint32_t height)
1064{
1065 GLenum gl_format;
1066
1067 switch (format) {
1068 case PIXMAN_a8r8g8b8:
1069 gl_format = GL_BGRA_EXT;
1070 break;
1071 case PIXMAN_a8b8g8r8:
1072 gl_format = GL_RGBA;
1073 break;
1074 default:
1075 return -1;
1076 }
1077
1078 if (use_output(output) < 0)
1079 return -1;
1080
1081 glPixelStorei(GL_PACK_ALIGNMENT, 1);
1082 glReadPixels(x, y, width, height, gl_format,
1083 GL_UNSIGNED_BYTE, pixels);
1084
1085 return 0;
1086}
1087
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001088static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001089gl_renderer_flush_damage(struct weston_surface *surface)
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001090{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001091 struct gl_renderer *gr = get_renderer(surface->compositor);
1092 struct gl_surface_state *gs = get_surface_state(surface);
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001093 struct wl_buffer *buffer = gs->buffer_ref.buffer;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001094
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001095#ifdef GL_UNPACK_ROW_LENGTH
1096 pixman_box32_t *rectangles;
1097 void *data;
1098 int i, n;
1099#endif
1100
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001101 pixman_region32_union(&gs->texture_damage,
1102 &gs->texture_damage, &surface->damage);
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001103
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001104 if (!buffer)
1105 return;
1106
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001107 /* Avoid upload, if the texture won't be used this time.
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001108 * We still accumulate the damage in texture_damage, and
1109 * hold the reference to the buffer, in case the surface
1110 * migrates back to the primary plane.
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001111 */
1112 if (surface->plane != &surface->compositor->primary_plane)
1113 return;
1114
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001115 if (!pixman_region32_not_empty(&gs->texture_damage))
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001116 goto done;
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001117
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001118 glBindTexture(GL_TEXTURE_2D, gs->textures[0]);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001119
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001120 if (!gr->has_unpack_subimage) {
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001121 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001122 gs->pitch, buffer->height, 0,
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001123 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
Pekka Paalanende685b82012-12-04 15:58:12 +02001124 wl_shm_buffer_get_data(buffer));
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001125
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001126 goto done;
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001127 }
1128
1129#ifdef GL_UNPACK_ROW_LENGTH
1130 /* Mesa does not define GL_EXT_unpack_subimage */
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001131 glPixelStorei(GL_UNPACK_ROW_LENGTH, gs->pitch);
Pekka Paalanende685b82012-12-04 15:58:12 +02001132 data = wl_shm_buffer_get_data(buffer);
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001133 rectangles = pixman_region32_rectangles(&gs->texture_damage, &n);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001134 for (i = 0; i < n; i++) {
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001135 pixman_box32_t r;
1136
1137 r = weston_surface_to_buffer_rect(surface, rectangles[i]);
1138
1139 glPixelStorei(GL_UNPACK_SKIP_PIXELS, r.x1);
1140 glPixelStorei(GL_UNPACK_SKIP_ROWS, r.y1);
1141 glTexSubImage2D(GL_TEXTURE_2D, 0, r.x1, r.y1,
1142 r.x2 - r.x1, r.y2 - r.y1,
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001143 GL_BGRA_EXT, GL_UNSIGNED_BYTE, data);
1144 }
1145#endif
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001146
1147done:
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001148 pixman_region32_fini(&gs->texture_damage);
1149 pixman_region32_init(&gs->texture_damage);
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001150
1151 weston_buffer_reference(&gs->buffer_ref, NULL);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001152}
1153
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001154static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001155ensure_textures(struct gl_surface_state *gs, int num_textures)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001156{
1157 int i;
1158
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001159 if (num_textures <= gs->num_textures)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001160 return;
1161
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001162 for (i = gs->num_textures; i < num_textures; i++) {
1163 glGenTextures(1, &gs->textures[i]);
1164 glBindTexture(gs->target, gs->textures[i]);
1165 glTexParameteri(gs->target,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001166 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001167 glTexParameteri(gs->target,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001168 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1169 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001170 gs->num_textures = num_textures;
1171 glBindTexture(gs->target, 0);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001172}
1173
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001174static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001175gl_renderer_attach(struct weston_surface *es, struct wl_buffer *buffer)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001176{
1177 struct weston_compositor *ec = es->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001178 struct gl_renderer *gr = get_renderer(ec);
1179 struct gl_surface_state *gs = get_surface_state(es);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001180 EGLint attribs[3], format;
1181 int i, num_planes;
1182
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001183 weston_buffer_reference(&gs->buffer_ref, buffer);
1184
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001185 if (!buffer) {
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001186 for (i = 0; i < gs->num_images; i++) {
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001187 gr->destroy_image(gr->egl_display, gs->images[i]);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001188 gs->images[i] = NULL;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001189 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001190 gs->num_images = 0;
1191 glDeleteTextures(gs->num_textures, gs->textures);
1192 gs->num_textures = 0;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001193 return;
1194 }
1195
1196 if (wl_buffer_is_shm(buffer)) {
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001197 gs->pitch = wl_shm_buffer_get_stride(buffer) / 4;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001198 gs->target = GL_TEXTURE_2D;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001199
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001200 ensure_textures(gs, 1);
1201 glBindTexture(GL_TEXTURE_2D, gs->textures[0]);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001202 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001203 gs->pitch, buffer->height, 0,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001204 GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
1205 if (wl_shm_buffer_get_format(buffer) == WL_SHM_FORMAT_XRGB8888)
John Kåre Alsaker40684142012-11-13 19:10:25 +01001206 gs->shader = &gr->texture_shader_rgbx;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001207 else
John Kåre Alsaker40684142012-11-13 19:10:25 +01001208 gs->shader = &gr->texture_shader_rgba;
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001209 } else if (gr->query_buffer(gr->egl_display, buffer,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001210 EGL_TEXTURE_FORMAT, &format)) {
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001211 for (i = 0; i < gs->num_images; i++)
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001212 gr->destroy_image(gr->egl_display, gs->images[i]);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001213 gs->num_images = 0;
1214 gs->target = GL_TEXTURE_2D;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001215 switch (format) {
1216 case EGL_TEXTURE_RGB:
1217 case EGL_TEXTURE_RGBA:
1218 default:
1219 num_planes = 1;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001220 gs->shader = &gr->texture_shader_rgba;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001221 break;
1222 case EGL_TEXTURE_EXTERNAL_WL:
1223 num_planes = 1;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001224 gs->target = GL_TEXTURE_EXTERNAL_OES;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001225 gs->shader = &gr->texture_shader_egl_external;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001226 break;
1227 case EGL_TEXTURE_Y_UV_WL:
1228 num_planes = 2;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001229 gs->shader = &gr->texture_shader_y_uv;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001230 break;
1231 case EGL_TEXTURE_Y_U_V_WL:
1232 num_planes = 3;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001233 gs->shader = &gr->texture_shader_y_u_v;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001234 break;
1235 case EGL_TEXTURE_Y_XUXV_WL:
1236 num_planes = 2;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001237 gs->shader = &gr->texture_shader_y_xuxv;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001238 break;
1239 }
1240
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001241 ensure_textures(gs, num_planes);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001242 for (i = 0; i < num_planes; i++) {
1243 attribs[0] = EGL_WAYLAND_PLANE_WL;
1244 attribs[1] = i;
1245 attribs[2] = EGL_NONE;
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001246 gs->images[i] = gr->create_image(gr->egl_display,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001247 NULL,
1248 EGL_WAYLAND_BUFFER_WL,
1249 buffer, attribs);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001250 if (!gs->images[i]) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001251 weston_log("failed to create img for plane %d\n", i);
1252 continue;
1253 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001254 gs->num_images++;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001255
1256 glActiveTexture(GL_TEXTURE0 + i);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001257 glBindTexture(gs->target, gs->textures[i]);
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001258 gr->image_target_texture_2d(gs->target,
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001259 gs->images[i]);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001260 }
1261
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001262 gs->pitch = buffer->width;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001263 } else {
1264 weston_log("unhandled buffer type!\n");
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001265 weston_buffer_reference(&gs->buffer_ref, NULL);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001266 }
1267}
1268
Kristian Høgsberg42263852012-09-06 21:59:29 -04001269static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001270gl_renderer_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001271 float red, float green, float blue, float alpha)
1272{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001273 struct gl_surface_state *gs = get_surface_state(surface);
1274 struct gl_renderer *gr = get_renderer(surface->compositor);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001275
1276 gs->color[0] = red;
1277 gs->color[1] = green;
1278 gs->color[2] = blue;
1279 gs->color[3] = alpha;
1280
John Kåre Alsaker40684142012-11-13 19:10:25 +01001281 gs->shader = &gr->solid_shader;
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001282}
1283
1284static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001285gl_renderer_create_surface(struct weston_surface *surface)
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001286{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001287 struct gl_surface_state *gs;
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001288
1289 gs = calloc(1, sizeof *gs);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001290 if (!gs)
1291 return -1;
1292
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001293 /* A buffer is never attached to solid color surfaces, yet
1294 * they still go through texcoord computations. Do not divide
1295 * by zero there.
1296 */
1297 gs->pitch = 1;
1298
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001299 pixman_region32_init(&gs->texture_damage);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001300 surface->renderer_state = gs;
1301
1302 return 0;
1303}
1304
1305static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001306gl_renderer_destroy_surface(struct weston_surface *surface)
Kristian Høgsberg42263852012-09-06 21:59:29 -04001307{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001308 struct gl_surface_state *gs = get_surface_state(surface);
1309 struct gl_renderer *gr = get_renderer(surface->compositor);
Kristian Høgsberg42263852012-09-06 21:59:29 -04001310 int i;
1311
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001312 glDeleteTextures(gs->num_textures, gs->textures);
Kristian Høgsberg42263852012-09-06 21:59:29 -04001313
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001314 for (i = 0; i < gs->num_images; i++)
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001315 gr->destroy_image(gr->egl_display, gs->images[i]);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001316
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001317 weston_buffer_reference(&gs->buffer_ref, NULL);
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001318 pixman_region32_fini(&gs->texture_damage);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001319 free(gs);
Kristian Høgsberg42263852012-09-06 21:59:29 -04001320}
1321
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001322static const char vertex_shader[] =
1323 "uniform mat4 proj;\n"
1324 "attribute vec2 position;\n"
1325 "attribute vec2 texcoord;\n"
1326 "varying vec2 v_texcoord;\n"
1327 "void main()\n"
1328 "{\n"
1329 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
1330 " v_texcoord = texcoord;\n"
1331 "}\n";
1332
1333/* Declare common fragment shader uniforms */
1334#define FRAGMENT_CONVERT_YUV \
1335 " y *= alpha;\n" \
1336 " u *= alpha;\n" \
1337 " v *= alpha;\n" \
1338 " gl_FragColor.r = y + 1.59602678 * v;\n" \
1339 " gl_FragColor.g = y - 0.39176229 * u - 0.81296764 * v;\n" \
1340 " gl_FragColor.b = y + 2.01723214 * u;\n" \
1341 " gl_FragColor.a = alpha;\n"
1342
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001343static const char fragment_debug[] =
1344 " gl_FragColor = vec4(0.0, 0.3, 0.0, 0.2) + gl_FragColor * 0.8;\n";
1345
1346static const char fragment_brace[] =
1347 "}\n";
1348
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001349static const char texture_fragment_shader_rgba[] =
1350 "precision mediump float;\n"
1351 "varying vec2 v_texcoord;\n"
1352 "uniform sampler2D tex;\n"
1353 "uniform float alpha;\n"
1354 "void main()\n"
1355 "{\n"
1356 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001357 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001358
1359static const char texture_fragment_shader_rgbx[] =
1360 "precision mediump float;\n"
1361 "varying vec2 v_texcoord;\n"
1362 "uniform sampler2D tex;\n"
1363 "uniform float alpha;\n"
1364 "void main()\n"
1365 "{\n"
1366 " gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb\n;"
1367 " gl_FragColor.a = alpha;\n"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001368 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001369
1370static const char texture_fragment_shader_egl_external[] =
1371 "#extension GL_OES_EGL_image_external : require\n"
1372 "precision mediump float;\n"
1373 "varying vec2 v_texcoord;\n"
1374 "uniform samplerExternalOES tex;\n"
1375 "uniform float alpha;\n"
1376 "void main()\n"
1377 "{\n"
1378 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001379 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001380
1381static const char texture_fragment_shader_y_uv[] =
1382 "precision mediump float;\n"
1383 "uniform sampler2D tex;\n"
1384 "uniform sampler2D tex1;\n"
1385 "varying vec2 v_texcoord;\n"
1386 "uniform float alpha;\n"
1387 "void main() {\n"
1388 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1389 " float u = texture2D(tex1, v_texcoord).r - 0.5;\n"
1390 " float v = texture2D(tex1, v_texcoord).g - 0.5;\n"
1391 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001392 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001393
1394static const char texture_fragment_shader_y_u_v[] =
1395 "precision mediump float;\n"
1396 "uniform sampler2D tex;\n"
1397 "uniform sampler2D tex1;\n"
1398 "uniform sampler2D tex2;\n"
1399 "varying vec2 v_texcoord;\n"
1400 "uniform float alpha;\n"
1401 "void main() {\n"
1402 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1403 " float u = texture2D(tex1, v_texcoord).x - 0.5;\n"
1404 " float v = texture2D(tex2, v_texcoord).x - 0.5;\n"
1405 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001406 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001407
1408static const char texture_fragment_shader_y_xuxv[] =
1409 "precision mediump float;\n"
1410 "uniform sampler2D tex;\n"
1411 "uniform sampler2D tex1;\n"
1412 "varying vec2 v_texcoord;\n"
1413 "uniform float alpha;\n"
1414 "void main() {\n"
1415 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1416 " float u = texture2D(tex1, v_texcoord).g - 0.5;\n"
1417 " float v = texture2D(tex1, v_texcoord).a - 0.5;\n"
1418 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001419 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001420
1421static const char solid_fragment_shader[] =
1422 "precision mediump float;\n"
1423 "uniform vec4 color;\n"
1424 "uniform float alpha;\n"
1425 "void main()\n"
1426 "{\n"
1427 " gl_FragColor = alpha * color\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001428 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001429
1430static int
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001431compile_shader(GLenum type, int count, const char **sources)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001432{
1433 GLuint s;
1434 char msg[512];
1435 GLint status;
1436
1437 s = glCreateShader(type);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001438 glShaderSource(s, count, sources, NULL);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001439 glCompileShader(s);
1440 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
1441 if (!status) {
1442 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1443 weston_log("shader info: %s\n", msg);
1444 return GL_NONE;
1445 }
1446
1447 return s;
1448}
1449
1450static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001451shader_init(struct gl_shader *shader, struct weston_compositor *ec,
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001452 const char *vertex_source, const char *fragment_source)
1453{
1454 char msg[512];
1455 GLint status;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001456 int count;
1457 const char *sources[3];
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001458 struct gl_renderer *renderer = get_renderer(ec);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001459
1460 shader->vertex_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001461 compile_shader(GL_VERTEX_SHADER, 1, &vertex_source);
1462
1463 if (renderer->fragment_shader_debug) {
1464 sources[0] = fragment_source;
1465 sources[1] = fragment_debug;
1466 sources[2] = fragment_brace;
1467 count = 3;
1468 } else {
1469 sources[0] = fragment_source;
1470 sources[1] = fragment_brace;
1471 count = 2;
1472 }
1473
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001474 shader->fragment_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001475 compile_shader(GL_FRAGMENT_SHADER, count, sources);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001476
1477 shader->program = glCreateProgram();
1478 glAttachShader(shader->program, shader->vertex_shader);
1479 glAttachShader(shader->program, shader->fragment_shader);
1480 glBindAttribLocation(shader->program, 0, "position");
1481 glBindAttribLocation(shader->program, 1, "texcoord");
1482
1483 glLinkProgram(shader->program);
1484 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1485 if (!status) {
1486 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1487 weston_log("link info: %s\n", msg);
1488 return -1;
1489 }
1490
1491 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1492 shader->tex_uniforms[0] = glGetUniformLocation(shader->program, "tex");
1493 shader->tex_uniforms[1] = glGetUniformLocation(shader->program, "tex1");
1494 shader->tex_uniforms[2] = glGetUniformLocation(shader->program, "tex2");
1495 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
1496 shader->color_uniform = glGetUniformLocation(shader->program, "color");
1497
1498 return 0;
1499}
1500
1501static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001502shader_release(struct gl_shader *shader)
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001503{
1504 glDeleteShader(shader->vertex_shader);
1505 glDeleteShader(shader->fragment_shader);
1506 glDeleteProgram(shader->program);
1507
1508 shader->vertex_shader = 0;
1509 shader->fragment_shader = 0;
1510 shader->program = 0;
1511}
1512
1513static void
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001514log_extensions(const char *name, const char *extensions)
1515{
1516 const char *p, *end;
1517 int l;
1518 int len;
1519
1520 l = weston_log("%s:", name);
1521 p = extensions;
1522 while (*p) {
1523 end = strchrnul(p, ' ');
1524 len = end - p;
1525 if (l + len > 78)
1526 l = weston_log_continue("\n" STAMP_SPACE "%.*s",
1527 len, p);
1528 else
1529 l += weston_log_continue(" %.*s", len, p);
1530 for (p = end; isspace(*p); p++)
1531 ;
1532 }
1533 weston_log_continue("\n");
1534}
1535
1536static void
1537log_egl_gl_info(EGLDisplay egldpy)
1538{
1539 const char *str;
1540
1541 str = eglQueryString(egldpy, EGL_VERSION);
1542 weston_log("EGL version: %s\n", str ? str : "(null)");
1543
1544 str = eglQueryString(egldpy, EGL_VENDOR);
1545 weston_log("EGL vendor: %s\n", str ? str : "(null)");
1546
1547 str = eglQueryString(egldpy, EGL_CLIENT_APIS);
1548 weston_log("EGL client APIs: %s\n", str ? str : "(null)");
1549
1550 str = eglQueryString(egldpy, EGL_EXTENSIONS);
1551 log_extensions("EGL extensions", str ? str : "(null)");
1552
1553 str = (char *)glGetString(GL_VERSION);
1554 weston_log("GL version: %s\n", str ? str : "(null)");
1555
1556 str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
1557 weston_log("GLSL version: %s\n", str ? str : "(null)");
1558
1559 str = (char *)glGetString(GL_VENDOR);
1560 weston_log("GL vendor: %s\n", str ? str : "(null)");
1561
1562 str = (char *)glGetString(GL_RENDERER);
1563 weston_log("GL renderer: %s\n", str ? str : "(null)");
1564
1565 str = (char *)glGetString(GL_EXTENSIONS);
1566 log_extensions("GL extensions", str ? str : "(null)");
1567}
1568
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001569static void
1570log_egl_config_info(EGLDisplay egldpy, EGLConfig eglconfig)
1571{
1572 EGLint r, g, b, a;
1573
1574 weston_log("Chosen EGL config details:\n");
1575
1576 weston_log_continue(STAMP_SPACE "RGBA bits");
1577 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_RED_SIZE, &r) &&
1578 eglGetConfigAttrib(egldpy, eglconfig, EGL_GREEN_SIZE, &g) &&
1579 eglGetConfigAttrib(egldpy, eglconfig, EGL_BLUE_SIZE, &b) &&
1580 eglGetConfigAttrib(egldpy, eglconfig, EGL_ALPHA_SIZE, &a))
1581 weston_log_continue(": %d %d %d %d\n", r, g, b, a);
1582 else
1583 weston_log_continue(" unknown\n");
1584
1585 weston_log_continue(STAMP_SPACE "swap interval range");
1586 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_MIN_SWAP_INTERVAL, &a) &&
1587 eglGetConfigAttrib(egldpy, eglconfig, EGL_MAX_SWAP_INTERVAL, &b))
1588 weston_log_continue(": %d - %d\n", a, b);
1589 else
1590 weston_log_continue(" unknown\n");
1591}
1592
John Kåre Alsaker44154502012-11-13 19:10:20 +01001593static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001594output_apply_border(struct weston_output *output, struct gl_renderer *gr)
John Kåre Alsaker44154502012-11-13 19:10:20 +01001595{
1596 output->border.top = gr->border.top;
1597 output->border.bottom = gr->border.bottom;
1598 output->border.left = gr->border.left;
1599 output->border.right = gr->border.right;
1600}
1601
1602WL_EXPORT void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001603gl_renderer_set_border(struct weston_compositor *ec, int32_t width, int32_t height, void *data,
John Kåre Alsaker44154502012-11-13 19:10:20 +01001604 int32_t *edges)
1605{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001606 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker44154502012-11-13 19:10:20 +01001607 struct weston_output *output;
1608
1609 gr->border.left = edges[0];
1610 gr->border.right = edges[1];
1611 gr->border.top = edges[2];
1612 gr->border.bottom = edges[3];
1613
1614 gr->border.width = width;
1615 gr->border.height = height;
1616
1617 glGenTextures(1, &gr->border.texture);
1618 glBindTexture(GL_TEXTURE_2D, gr->border.texture);
1619 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1620 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1621 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1622 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1623
1624 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1625 width,
1626 height,
1627 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1628 data);
1629
1630 wl_list_for_each(output, &ec->output_list, link)
1631 output_apply_border(output, gr);
1632}
1633
John Kåre Alsaker94659272012-11-13 19:10:18 +01001634static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001635gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001636
1637WL_EXPORT int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001638gl_renderer_output_create(struct weston_output *output,
John Kåre Alsaker94659272012-11-13 19:10:18 +01001639 EGLNativeWindowType window)
1640{
1641 struct weston_compositor *ec = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001642 struct gl_renderer *gr = get_renderer(ec);
1643 struct gl_output_state *go = calloc(1, sizeof *go);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001644 int i;
John Kåre Alsaker94659272012-11-13 19:10:18 +01001645
1646 if (!go)
1647 return -1;
1648
1649 go->egl_surface =
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001650 eglCreateWindowSurface(gr->egl_display,
1651 gr->egl_config,
John Kåre Alsaker94659272012-11-13 19:10:18 +01001652 window, NULL);
1653
1654 if (go->egl_surface == EGL_NO_SURFACE) {
1655 weston_log("failed to create egl surface\n");
1656 free(go);
1657 return -1;
1658 }
1659
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001660 if (gr->egl_context == NULL)
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001661 if (gl_renderer_setup(ec, go->egl_surface) < 0) {
John Kåre Alsaker94659272012-11-13 19:10:18 +01001662 free(go);
1663 return -1;
1664 }
1665
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001666 for (i = 0; i < BUFFER_DAMAGE_COUNT; i++)
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001667 pixman_region32_init(&go->buffer_damage[i]);
1668
John Kåre Alsaker94659272012-11-13 19:10:18 +01001669 output->renderer_state = go;
1670
John Kåre Alsaker44154502012-11-13 19:10:20 +01001671 output_apply_border(output, gr);
1672
John Kåre Alsaker94659272012-11-13 19:10:18 +01001673 return 0;
1674}
1675
1676WL_EXPORT void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001677gl_renderer_output_destroy(struct weston_output *output)
John Kåre Alsaker94659272012-11-13 19:10:18 +01001678{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001679 struct gl_renderer *gr = get_renderer(output->compositor);
1680 struct gl_output_state *go = get_output_state(output);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001681 int i;
1682
1683 for (i = 0; i < 2; i++)
1684 pixman_region32_fini(&go->buffer_damage[i]);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001685
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001686 eglDestroySurface(gr->egl_display, go->egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001687
1688 free(go);
1689}
1690
1691WL_EXPORT EGLSurface
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001692gl_renderer_output_surface(struct weston_output *output)
John Kåre Alsaker94659272012-11-13 19:10:18 +01001693{
1694 return get_output_state(output)->egl_surface;
1695}
1696
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03001697static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001698gl_renderer_destroy(struct weston_compositor *ec)
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001699{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001700 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001701
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001702 if (gr->has_bind_display)
1703 gr->unbind_display(gr->egl_display, ec->wl_display);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001704
1705 /* Work around crash in egl_dri2.c's dri2_make_current() - when does this apply? */
1706 eglMakeCurrent(gr->egl_display,
1707 EGL_NO_SURFACE, EGL_NO_SURFACE,
1708 EGL_NO_CONTEXT);
1709
1710 eglTerminate(gr->egl_display);
1711 eglReleaseThread();
Scott Moreau976a0502013-03-07 10:15:17 -07001712
1713 free(gr);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001714}
1715
1716static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001717egl_choose_config(struct gl_renderer *gr, const EGLint *attribs,
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001718 const EGLint *visual_id)
1719{
1720 EGLint count = 0;
1721 EGLint matched = 0;
1722 EGLConfig *configs;
1723 int i;
1724
1725 if (!eglGetConfigs(gr->egl_display, NULL, 0, &count) || count < 1)
1726 return -1;
1727
1728 configs = calloc(count, sizeof *configs);
1729 if (!configs)
1730 return -1;
1731
1732 if (!eglChooseConfig(gr->egl_display, attribs, configs,
1733 count, &matched))
1734 goto out;
1735
1736 for (i = 0; i < matched; ++i) {
1737 EGLint id;
1738
1739 if (visual_id) {
1740 if (!eglGetConfigAttrib(gr->egl_display,
1741 configs[i], EGL_NATIVE_VISUAL_ID,
1742 &id))
1743 continue;
1744
1745 if (id != *visual_id)
1746 continue;
1747 }
1748
1749 gr->egl_config = configs[i];
1750
1751 free(configs);
1752 return 0;
1753 }
1754
1755out:
1756 free(configs);
1757 return -1;
1758}
1759
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001760WL_EXPORT const EGLint gl_renderer_opaque_attribs[] = {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001761 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1762 EGL_RED_SIZE, 1,
1763 EGL_GREEN_SIZE, 1,
1764 EGL_BLUE_SIZE, 1,
1765 EGL_ALPHA_SIZE, 0,
1766 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1767 EGL_NONE
1768};
1769
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001770WL_EXPORT const EGLint gl_renderer_alpha_attribs[] = {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001771 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1772 EGL_RED_SIZE, 1,
1773 EGL_GREEN_SIZE, 1,
1774 EGL_BLUE_SIZE, 1,
1775 EGL_ALPHA_SIZE, 1,
1776 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1777 EGL_NONE
1778};
1779
1780WL_EXPORT int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001781gl_renderer_create(struct weston_compositor *ec, EGLNativeDisplayType display,
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001782 const EGLint *attribs, const EGLint *visual_id)
1783{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001784 struct gl_renderer *gr;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001785 EGLint major, minor;
1786
1787 gr = calloc(1, sizeof *gr);
1788
1789 if (gr == NULL)
1790 return -1;
1791
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001792 gr->base.read_pixels = gl_renderer_read_pixels;
1793 gr->base.repaint_output = gl_renderer_repaint_output;
1794 gr->base.flush_damage = gl_renderer_flush_damage;
1795 gr->base.attach = gl_renderer_attach;
1796 gr->base.create_surface = gl_renderer_create_surface;
1797 gr->base.surface_set_color = gl_renderer_surface_set_color;
1798 gr->base.destroy_surface = gl_renderer_destroy_surface;
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03001799 gr->base.destroy = gl_renderer_destroy;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001800
1801 gr->egl_display = eglGetDisplay(display);
1802 if (gr->egl_display == EGL_NO_DISPLAY) {
1803 weston_log("failed to create display\n");
1804 goto err_egl;
1805 }
1806
1807 if (!eglInitialize(gr->egl_display, &major, &minor)) {
1808 weston_log("failed to initialize display\n");
1809 goto err_egl;
1810 }
1811
1812 if (egl_choose_config(gr, attribs, visual_id) < 0) {
1813 weston_log("failed to choose EGL config\n");
1814 goto err_egl;
1815 }
1816
1817 ec->renderer = &gr->base;
1818
1819 return 0;
1820
1821err_egl:
Pekka Paalanen326529f2012-11-27 12:25:25 +02001822 gl_renderer_print_egl_error_state();
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001823 free(gr);
1824 return -1;
1825}
1826
1827WL_EXPORT EGLDisplay
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001828gl_renderer_display(struct weston_compositor *ec)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001829{
1830 return get_renderer(ec)->egl_display;
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001831}
1832
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001833static int
1834compile_shaders(struct weston_compositor *ec)
1835{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001836 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker40684142012-11-13 19:10:25 +01001837
1838 if (shader_init(&gr->texture_shader_rgba, ec,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001839 vertex_shader, texture_fragment_shader_rgba) < 0)
1840 return -1;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001841 if (shader_init(&gr->texture_shader_rgbx, ec,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001842 vertex_shader, texture_fragment_shader_rgbx) < 0)
1843 return -1;
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001844 if (gr->has_egl_image_external &&
John Kåre Alsaker40684142012-11-13 19:10:25 +01001845 shader_init(&gr->texture_shader_egl_external, ec,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001846 vertex_shader, texture_fragment_shader_egl_external) < 0)
1847 return -1;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001848 if (shader_init(&gr->texture_shader_y_uv, ec,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001849 vertex_shader, texture_fragment_shader_y_uv) < 0)
1850 return -1;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001851 if (shader_init(&gr->texture_shader_y_u_v, ec,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001852 vertex_shader, texture_fragment_shader_y_u_v) < 0)
1853 return -1;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001854 if (shader_init(&gr->texture_shader_y_xuxv, ec,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001855 vertex_shader, texture_fragment_shader_y_xuxv) < 0)
1856 return -1;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001857 if (shader_init(&gr->solid_shader, ec,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001858 vertex_shader, solid_fragment_shader) < 0)
1859 return -1;
1860
1861 return 0;
1862}
1863
1864static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04001865fragment_debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001866 void *data)
1867{
1868 struct weston_compositor *ec = data;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001869 struct gl_renderer *gr = get_renderer(ec);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001870 struct weston_output *output;
1871
John Kåre Alsaker40684142012-11-13 19:10:25 +01001872 gr->fragment_shader_debug ^= 1;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001873
John Kåre Alsaker40684142012-11-13 19:10:25 +01001874 shader_release(&gr->texture_shader_rgba);
1875 shader_release(&gr->texture_shader_rgbx);
1876 shader_release(&gr->texture_shader_egl_external);
1877 shader_release(&gr->texture_shader_y_uv);
1878 shader_release(&gr->texture_shader_y_u_v);
1879 shader_release(&gr->texture_shader_y_xuxv);
1880 shader_release(&gr->solid_shader);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001881
1882 compile_shaders(ec);
1883
Ander Conselvan de Oliveira03fb4ef2012-12-03 17:08:11 +02001884 /* Force use_shader() to call glUseProgram(), since we need to use
1885 * the recompiled version of the shader. */
1886 gr->current_shader = NULL;
1887
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001888 wl_list_for_each(output, &ec->output_list, link)
1889 weston_output_damage(output);
1890}
1891
Kristian Høgsberg8799d412013-05-07 10:50:09 -04001892static void
1893fan_debug_repaint_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
1894 void *data)
1895{
1896 struct weston_compositor *compositor = data;
1897 struct gl_renderer *gr = get_renderer(compositor);
1898
1899 gr->fan_debug = !gr->fan_debug;
1900 weston_compositor_damage_all(compositor);
1901}
1902
John Kåre Alsaker94659272012-11-13 19:10:18 +01001903static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001904gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001905{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001906 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001907 const char *extensions;
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001908 EGLBoolean ret;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001909
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001910 static const EGLint context_attribs[] = {
1911 EGL_CONTEXT_CLIENT_VERSION, 2,
1912 EGL_NONE
1913 };
1914
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001915 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
1916 weston_log("failed to bind EGL_OPENGL_ES_API\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02001917 gl_renderer_print_egl_error_state();
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001918 return -1;
1919 }
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001920
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001921 log_egl_config_info(gr->egl_display, gr->egl_config);
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001922
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001923 gr->egl_context = eglCreateContext(gr->egl_display, gr->egl_config,
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001924 EGL_NO_CONTEXT, context_attribs);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001925 if (gr->egl_context == NULL) {
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001926 weston_log("failed to create context\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02001927 gl_renderer_print_egl_error_state();
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001928 return -1;
1929 }
1930
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001931 ret = eglMakeCurrent(gr->egl_display, egl_surface,
1932 egl_surface, gr->egl_context);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001933 if (ret == EGL_FALSE) {
1934 weston_log("Failed to make EGL context current.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02001935 gl_renderer_print_egl_error_state();
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001936 return -1;
1937 }
1938
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001939 log_egl_gl_info(gr->egl_display);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001940
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001941 gr->image_target_texture_2d =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001942 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001943 gr->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1944 gr->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1945 gr->bind_display =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001946 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001947 gr->unbind_display =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001948 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001949 gr->query_buffer =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001950 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
1951
1952 extensions = (const char *) glGetString(GL_EXTENSIONS);
1953 if (!extensions) {
1954 weston_log("Retrieving GL extension string failed.\n");
1955 return -1;
1956 }
1957
1958 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
1959 weston_log("GL_EXT_texture_format_BGRA8888 not available\n");
1960 return -1;
1961 }
1962
1963 if (strstr(extensions, "GL_EXT_read_format_bgra"))
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01001964 ec->read_format = PIXMAN_a8r8g8b8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001965 else
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01001966 ec->read_format = PIXMAN_a8b8g8r8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001967
1968 if (strstr(extensions, "GL_EXT_unpack_subimage"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001969 gr->has_unpack_subimage = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001970
1971 if (strstr(extensions, "GL_OES_EGL_image_external"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001972 gr->has_egl_image_external = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001973
1974 extensions =
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001975 (const char *) eglQueryString(gr->egl_display, EGL_EXTENSIONS);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001976 if (!extensions) {
1977 weston_log("Retrieving EGL extension string failed.\n");
1978 return -1;
1979 }
1980
1981 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001982 gr->has_bind_display = 1;
1983 if (gr->has_bind_display) {
1984 ret = gr->bind_display(gr->egl_display, ec->wl_display);
Pekka Paalanen035a0322012-10-24 09:43:06 +03001985 if (!ret)
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001986 gr->has_bind_display = 0;
Pekka Paalanen035a0322012-10-24 09:43:06 +03001987 }
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001988
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001989 if (strstr(extensions, "EGL_EXT_buffer_age"))
1990 gr->has_egl_buffer_age = 1;
1991 else
1992 weston_log("warning: EGL_EXT_buffer_age not supported. "
1993 "Performance could be affected.\n");
1994
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001995 glActiveTexture(GL_TEXTURE0);
1996
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001997 if (compile_shaders(ec))
1998 return -1;
1999
2000 weston_compositor_add_debug_binding(ec, KEY_S,
2001 fragment_debug_binding, ec);
Kristian Høgsberg8799d412013-05-07 10:50:09 -04002002 weston_compositor_add_debug_binding(ec, KEY_F,
2003 fan_debug_repaint_binding, ec);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002004
Pekka Paalanen035a0322012-10-24 09:43:06 +03002005 weston_log("GL ES 2 renderer features:\n");
2006 weston_log_continue(STAMP_SPACE "read-back format: %s\n",
Pekka Paalanenfe4eacf2013-01-10 16:50:42 +02002007 ec->read_format == PIXMAN_a8r8g8b8 ? "BGRA" : "RGBA");
Pekka Paalanen035a0322012-10-24 09:43:06 +03002008 weston_log_continue(STAMP_SPACE "wl_shm sub-image to texture: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002009 gr->has_unpack_subimage ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03002010 weston_log_continue(STAMP_SPACE "EGL Wayland extension: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002011 gr->has_bind_display ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03002012
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002013
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002014 return 0;
2015}