blob: 05647b7ec9dd6dc8399a3418104c8b9d454d2940 [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
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -040025#include <stdlib.h>
Kristian Høgsberg25894fc2012-09-05 22:06:26 -040026#include <string.h>
27#include <ctype.h>
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +030028#include <float.h>
29#include <assert.h>
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +020030#include <linux/input.h>
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -040031
Kristian Høgsbergd7c17262012-09-05 21:54:15 -040032#include "compositor.h"
33
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +020034struct gles2_renderer {
35 struct weston_renderer base;
36 int fragment_shader_debug;
37};
38
Kristian Høgsbergd7c17262012-09-05 21:54:15 -040039static const char *
40egl_error_string(EGLint code)
41{
42#define MYERRCODE(x) case x: return #x;
43 switch (code) {
44 MYERRCODE(EGL_SUCCESS)
45 MYERRCODE(EGL_NOT_INITIALIZED)
46 MYERRCODE(EGL_BAD_ACCESS)
47 MYERRCODE(EGL_BAD_ALLOC)
48 MYERRCODE(EGL_BAD_ATTRIBUTE)
49 MYERRCODE(EGL_BAD_CONTEXT)
50 MYERRCODE(EGL_BAD_CONFIG)
51 MYERRCODE(EGL_BAD_CURRENT_SURFACE)
52 MYERRCODE(EGL_BAD_DISPLAY)
53 MYERRCODE(EGL_BAD_SURFACE)
54 MYERRCODE(EGL_BAD_MATCH)
55 MYERRCODE(EGL_BAD_PARAMETER)
56 MYERRCODE(EGL_BAD_NATIVE_PIXMAP)
57 MYERRCODE(EGL_BAD_NATIVE_WINDOW)
58 MYERRCODE(EGL_CONTEXT_LOST)
59 default:
60 return "unknown";
61 }
62#undef MYERRCODE
63}
64
65static void
66print_egl_error_state(void)
67{
68 EGLint code;
69
70 code = eglGetError();
71 weston_log("EGL error state: %s (0x%04lx)\n",
72 egl_error_string(code), (long)code);
73}
74
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +030075struct polygon8 {
76 GLfloat x[8];
77 GLfloat y[8];
78 int n;
79};
80
81struct clip_context {
82 struct {
83 GLfloat x;
84 GLfloat y;
85 } prev;
86
87 struct {
88 GLfloat x1, y1;
89 GLfloat x2, y2;
90 } clip;
91
92 struct {
93 GLfloat *x;
94 GLfloat *y;
95 } vertices;
96};
97
98static GLfloat
99float_difference(GLfloat a, GLfloat b)
100{
101 /* http://www.altdevblogaday.com/2012/02/22/comparing-floating-point-numbers-2012-edition/ */
102 static const GLfloat max_diff = 4.0f * FLT_MIN;
103 static const GLfloat max_rel_diff = 4.0e-5;
104 GLfloat diff = a - b;
105 GLfloat adiff = fabsf(diff);
106
107 if (adiff <= max_diff)
108 return 0.0f;
109
110 a = fabsf(a);
111 b = fabsf(b);
112 if (adiff <= (a > b ? a : b) * max_rel_diff)
113 return 0.0f;
114
115 return diff;
116}
117
118/* A line segment (p1x, p1y)-(p2x, p2y) intersects the line x = x_arg.
119 * Compute the y coordinate of the intersection.
120 */
121static GLfloat
122clip_intersect_y(GLfloat p1x, GLfloat p1y, GLfloat p2x, GLfloat p2y,
123 GLfloat x_arg)
124{
125 GLfloat a;
126 GLfloat diff = float_difference(p1x, p2x);
127
128 /* Practically vertical line segment, yet the end points have already
129 * been determined to be on different sides of the line. Therefore
130 * the line segment is part of the line and intersects everywhere.
131 * Return the end point, so we use the whole line segment.
132 */
133 if (diff == 0.0f)
134 return p2y;
135
136 a = (x_arg - p2x) / diff;
137 return p2y + (p1y - p2y) * a;
138}
139
140/* A line segment (p1x, p1y)-(p2x, p2y) intersects the line y = y_arg.
141 * Compute the x coordinate of the intersection.
142 */
143static GLfloat
144clip_intersect_x(GLfloat p1x, GLfloat p1y, GLfloat p2x, GLfloat p2y,
145 GLfloat y_arg)
146{
147 GLfloat a;
148 GLfloat diff = float_difference(p1y, p2y);
149
150 /* Practically horizontal line segment, yet the end points have already
151 * been determined to be on different sides of the line. Therefore
152 * the line segment is part of the line and intersects everywhere.
153 * Return the end point, so we use the whole line segment.
154 */
155 if (diff == 0.0f)
156 return p2x;
157
158 a = (y_arg - p2y) / diff;
159 return p2x + (p1x - p2x) * a;
160}
161
162enum path_transition {
163 PATH_TRANSITION_OUT_TO_OUT = 0,
164 PATH_TRANSITION_OUT_TO_IN = 1,
165 PATH_TRANSITION_IN_TO_OUT = 2,
166 PATH_TRANSITION_IN_TO_IN = 3,
167};
168
169static void
170clip_append_vertex(struct clip_context *ctx, GLfloat x, GLfloat y)
171{
172 *ctx->vertices.x++ = x;
173 *ctx->vertices.y++ = y;
174}
175
176static enum path_transition
177path_transition_left_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
178{
179 return ((ctx->prev.x >= ctx->clip.x1) << 1) | (x >= ctx->clip.x1);
180}
181
182static enum path_transition
183path_transition_right_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
184{
185 return ((ctx->prev.x < ctx->clip.x2) << 1) | (x < ctx->clip.x2);
186}
187
188static enum path_transition
189path_transition_top_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
190{
191 return ((ctx->prev.y >= ctx->clip.y1) << 1) | (y >= ctx->clip.y1);
192}
193
194static enum path_transition
195path_transition_bottom_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
196{
197 return ((ctx->prev.y < ctx->clip.y2) << 1) | (y < ctx->clip.y2);
198}
199
200static void
201clip_polygon_leftright(struct clip_context *ctx,
202 enum path_transition transition,
203 GLfloat x, GLfloat y, GLfloat clip_x)
204{
205 GLfloat yi;
206
207 switch (transition) {
208 case PATH_TRANSITION_IN_TO_IN:
209 clip_append_vertex(ctx, x, y);
210 break;
211 case PATH_TRANSITION_IN_TO_OUT:
212 yi = clip_intersect_y(ctx->prev.x, ctx->prev.y, x, y, clip_x);
213 clip_append_vertex(ctx, clip_x, yi);
214 break;
215 case PATH_TRANSITION_OUT_TO_IN:
216 yi = clip_intersect_y(ctx->prev.x, ctx->prev.y, x, y, clip_x);
217 clip_append_vertex(ctx, clip_x, yi);
218 clip_append_vertex(ctx, x, y);
219 break;
220 case PATH_TRANSITION_OUT_TO_OUT:
221 /* nothing */
222 break;
223 default:
224 assert(0 && "bad enum path_transition");
225 }
226
227 ctx->prev.x = x;
228 ctx->prev.y = y;
229}
230
231static void
232clip_polygon_topbottom(struct clip_context *ctx,
233 enum path_transition transition,
234 GLfloat x, GLfloat y, GLfloat clip_y)
235{
236 GLfloat xi;
237
238 switch (transition) {
239 case PATH_TRANSITION_IN_TO_IN:
240 clip_append_vertex(ctx, x, y);
241 break;
242 case PATH_TRANSITION_IN_TO_OUT:
243 xi = clip_intersect_x(ctx->prev.x, ctx->prev.y, x, y, clip_y);
244 clip_append_vertex(ctx, xi, clip_y);
245 break;
246 case PATH_TRANSITION_OUT_TO_IN:
247 xi = clip_intersect_x(ctx->prev.x, ctx->prev.y, x, y, clip_y);
248 clip_append_vertex(ctx, xi, clip_y);
249 clip_append_vertex(ctx, x, y);
250 break;
251 case PATH_TRANSITION_OUT_TO_OUT:
252 /* nothing */
253 break;
254 default:
255 assert(0 && "bad enum path_transition");
256 }
257
258 ctx->prev.x = x;
259 ctx->prev.y = y;
260}
261
262static void
263clip_context_prepare(struct clip_context *ctx, const struct polygon8 *src,
264 GLfloat *dst_x, GLfloat *dst_y)
265{
266 ctx->prev.x = src->x[src->n - 1];
267 ctx->prev.y = src->y[src->n - 1];
268 ctx->vertices.x = dst_x;
269 ctx->vertices.y = dst_y;
270}
271
272static int
273clip_polygon_left(struct clip_context *ctx, const struct polygon8 *src,
274 GLfloat *dst_x, GLfloat *dst_y)
275{
276 enum path_transition trans;
277 int i;
278
279 clip_context_prepare(ctx, src, dst_x, dst_y);
280 for (i = 0; i < src->n; i++) {
281 trans = path_transition_left_edge(ctx, src->x[i], src->y[i]);
282 clip_polygon_leftright(ctx, trans, src->x[i], src->y[i],
283 ctx->clip.x1);
284 }
285 return ctx->vertices.x - dst_x;
286}
287
288static int
289clip_polygon_right(struct clip_context *ctx, const struct polygon8 *src,
290 GLfloat *dst_x, GLfloat *dst_y)
291{
292 enum path_transition trans;
293 int i;
294
295 clip_context_prepare(ctx, src, dst_x, dst_y);
296 for (i = 0; i < src->n; i++) {
297 trans = path_transition_right_edge(ctx, src->x[i], src->y[i]);
298 clip_polygon_leftright(ctx, trans, src->x[i], src->y[i],
299 ctx->clip.x2);
300 }
301 return ctx->vertices.x - dst_x;
302}
303
304static int
305clip_polygon_top(struct clip_context *ctx, const struct polygon8 *src,
306 GLfloat *dst_x, GLfloat *dst_y)
307{
308 enum path_transition trans;
309 int i;
310
311 clip_context_prepare(ctx, src, dst_x, dst_y);
312 for (i = 0; i < src->n; i++) {
313 trans = path_transition_top_edge(ctx, src->x[i], src->y[i]);
314 clip_polygon_topbottom(ctx, trans, src->x[i], src->y[i],
315 ctx->clip.y1);
316 }
317 return ctx->vertices.x - dst_x;
318}
319
320static int
321clip_polygon_bottom(struct clip_context *ctx, const struct polygon8 *src,
322 GLfloat *dst_x, GLfloat *dst_y)
323{
324 enum path_transition trans;
325 int i;
326
327 clip_context_prepare(ctx, src, dst_x, dst_y);
328 for (i = 0; i < src->n; i++) {
329 trans = path_transition_bottom_edge(ctx, src->x[i], src->y[i]);
330 clip_polygon_topbottom(ctx, trans, src->x[i], src->y[i],
331 ctx->clip.y2);
332 }
333 return ctx->vertices.x - dst_x;
334}
335
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400336#define max(a, b) (((a) > (b)) ? (a) : (b))
337#define min(a, b) (((a) > (b)) ? (b) : (a))
338#define clip(x, a, b) min(max(x, a), b)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400339
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300340/*
341 * Compute the boundary vertices of the intersection of the global coordinate
342 * aligned rectangle 'rect', and an arbitrary quadrilateral produced from
343 * 'surf_rect' when transformed from surface coordinates into global coordinates.
344 * The vertices are written to 'ex' and 'ey', and the return value is the
345 * number of vertices. Vertices are produced in clockwise winding order.
346 * Guarantees to produce either zero vertices, or 3-8 vertices with non-zero
347 * polygon area.
348 */
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400349static int
350calculate_edges(struct weston_surface *es, pixman_box32_t *rect,
351 pixman_box32_t *surf_rect, GLfloat *ex, GLfloat *ey)
352{
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300353 struct polygon8 polygon;
354 struct clip_context ctx;
355 int i, n;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400356 GLfloat min_x, max_x, min_y, max_y;
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300357 struct polygon8 surf = {
358 { surf_rect->x1, surf_rect->x2, surf_rect->x2, surf_rect->x1 },
359 { surf_rect->y1, surf_rect->y1, surf_rect->y2, surf_rect->y2 },
360 4
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400361 };
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400362
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300363 ctx.clip.x1 = rect->x1;
364 ctx.clip.y1 = rect->y1;
365 ctx.clip.x2 = rect->x2;
366 ctx.clip.y2 = rect->y2;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400367
368 /* transform surface to screen space: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300369 for (i = 0; i < surf.n; i++)
370 weston_surface_to_global_float(es, surf.x[i], surf.y[i],
371 &surf.x[i], &surf.y[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400372
373 /* find bounding box: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300374 min_x = max_x = surf.x[0];
375 min_y = max_y = surf.y[0];
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400376
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300377 for (i = 1; i < surf.n; i++) {
378 min_x = min(min_x, surf.x[i]);
379 max_x = max(max_x, surf.x[i]);
380 min_y = min(min_y, surf.y[i]);
381 max_y = max(max_y, surf.y[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400382 }
383
384 /* First, simple bounding box check to discard early transformed
385 * surface rects that do not intersect with the clip region:
386 */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300387 if ((min_x >= ctx.clip.x2) || (max_x <= ctx.clip.x1) ||
388 (min_y >= ctx.clip.y2) || (max_y <= ctx.clip.y1))
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400389 return 0;
390
391 /* Simple case, bounding box edges are parallel to surface edges,
392 * there will be only four edges. We just need to clip the surface
393 * vertices to the clip rect bounds:
394 */
395 if (!es->transform.enabled) {
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300396 for (i = 0; i < surf.n; i++) {
397 ex[i] = clip(surf.x[i], ctx.clip.x1, ctx.clip.x2);
398 ey[i] = clip(surf.y[i], ctx.clip.y1, ctx.clip.y2);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400399 }
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300400 return surf.n;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400401 }
402
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300403 /* Transformed case: use a general polygon clipping algorithm to
404 * clip the surface rectangle with each side of 'rect'.
405 * The algorithm is Sutherland-Hodgman, as explained in
406 * http://www.codeguru.com/cpp/misc/misc/graphics/article.php/c8965/Polygon-Clipping.htm
407 * but without looking at any of that code.
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400408 */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300409 polygon.n = clip_polygon_left(&ctx, &surf, polygon.x, polygon.y);
410 surf.n = clip_polygon_right(&ctx, &polygon, surf.x, surf.y);
411 polygon.n = clip_polygon_top(&ctx, &surf, polygon.x, polygon.y);
412 surf.n = clip_polygon_bottom(&ctx, &polygon, surf.x, surf.y);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400413
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300414 /* Get rid of duplicate vertices */
415 ex[0] = surf.x[0];
416 ey[0] = surf.y[0];
417 n = 1;
418 for (i = 1; i < surf.n; i++) {
419 if (float_difference(ex[n - 1], surf.x[i]) == 0.0f &&
420 float_difference(ey[n - 1], surf.y[i]) == 0.0f)
421 continue;
422 ex[n] = surf.x[i];
423 ey[n] = surf.y[i];
424 n++;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400425 }
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300426 if (float_difference(ex[n - 1], surf.x[0]) == 0.0f &&
427 float_difference(ey[n - 1], surf.y[0]) == 0.0f)
428 n--;
429
430 if (n < 3)
431 return 0;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400432
433 return n;
434}
435
436static int
437texture_region(struct weston_surface *es, pixman_region32_t *region,
438 pixman_region32_t *surf_region)
439{
440 struct weston_compositor *ec = es->compositor;
441 GLfloat *v, inv_width, inv_height;
442 unsigned int *vtxcnt, nvtx = 0;
443 pixman_box32_t *rects, *surf_rects;
444 int i, j, k, nrects, nsurf;
445
446 rects = pixman_region32_rectangles(region, &nrects);
447 surf_rects = pixman_region32_rectangles(surf_region, &nsurf);
448
449 /* worst case we can have 8 vertices per rect (ie. clipped into
450 * an octagon):
451 */
452 v = wl_array_add(&ec->vertices, nrects * nsurf * 8 * 4 * sizeof *v);
453 vtxcnt = wl_array_add(&ec->vtxcnt, nrects * nsurf * sizeof *vtxcnt);
454
455 inv_width = 1.0 / es->pitch;
456 inv_height = 1.0 / es->geometry.height;
457
458 for (i = 0; i < nrects; i++) {
459 pixman_box32_t *rect = &rects[i];
460 for (j = 0; j < nsurf; j++) {
461 pixman_box32_t *surf_rect = &surf_rects[j];
462 GLfloat sx, sy;
463 GLfloat ex[8], ey[8]; /* edge points in screen space */
464 int n;
465
466 /* The transformed surface, after clipping to the clip region,
467 * can have as many as eight sides, emitted as a triangle-fan.
468 * The first vertex in the triangle fan can be chosen arbitrarily,
469 * since the area is guaranteed to be convex.
470 *
471 * If a corner of the transformed surface falls outside of the
472 * clip region, instead of emitting one vertex for the corner
473 * of the surface, up to two are emitted for two corresponding
474 * intersection point(s) between the surface and the clip region.
475 *
476 * To do this, we first calculate the (up to eight) points that
477 * form the intersection of the clip rect and the transformed
478 * surface.
479 */
480 n = calculate_edges(es, rect, surf_rect, ex, ey);
481 if (n < 3)
482 continue;
483
484 /* emit edge points: */
485 for (k = 0; k < n; k++) {
486 weston_surface_from_global_float(es, ex[k], ey[k], &sx, &sy);
487 /* position: */
488 *(v++) = ex[k];
489 *(v++) = ey[k];
490 /* texcoord: */
491 *(v++) = sx * inv_width;
492 *(v++) = sy * inv_height;
493 }
494
495 vtxcnt[nvtx++] = n;
496 }
497 }
498
499 return nvtx;
500}
501
502static void
503triangle_fan_debug(struct weston_surface *surface, int first, int count)
504{
505 struct weston_compositor *compositor = surface->compositor;
506 int i;
507 GLushort *buffer;
508 GLushort *index;
509 int nelems;
510 static int color_idx = 0;
511 static const GLfloat color[][4] = {
512 { 1.0, 0.0, 0.0, 1.0 },
513 { 0.0, 1.0, 0.0, 1.0 },
514 { 0.0, 0.0, 1.0, 1.0 },
515 { 1.0, 1.0, 1.0, 1.0 },
516 };
517
518 nelems = (count - 1 + count - 2) * 2;
519
520 buffer = malloc(sizeof(GLushort) * nelems);
521 index = buffer;
522
523 for (i = 1; i < count; i++) {
524 *index++ = first;
525 *index++ = first + i;
526 }
527
528 for (i = 2; i < count; i++) {
529 *index++ = first + i - 1;
530 *index++ = first + i;
531 }
532
533 glUseProgram(compositor->solid_shader.program);
534 glUniform4fv(compositor->solid_shader.color_uniform, 1,
535 color[color_idx++ % ARRAY_LENGTH(color)]);
536 glDrawElements(GL_LINES, nelems, GL_UNSIGNED_SHORT, buffer);
537 glUseProgram(compositor->current_shader->program);
538 free(buffer);
539}
540
541static void
542repaint_region(struct weston_surface *es, pixman_region32_t *region,
543 pixman_region32_t *surf_region)
544{
545 struct weston_compositor *ec = es->compositor;
546 GLfloat *v;
547 unsigned int *vtxcnt;
548 int i, first, nfans;
549
550 /* The final region to be painted is the intersection of
551 * 'region' and 'surf_region'. However, 'region' is in the global
552 * coordinates, and 'surf_region' is in the surface-local
553 * coordinates. texture_region() will iterate over all pairs of
554 * rectangles from both regions, compute the intersection
555 * polygon for each pair, and store it as a triangle fan if
556 * it has a non-zero area (at least 3 vertices, actually).
557 */
558 nfans = texture_region(es, region, surf_region);
559
560 v = ec->vertices.data;
561 vtxcnt = ec->vtxcnt.data;
562
563 /* position: */
564 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
565 glEnableVertexAttribArray(0);
566
567 /* texcoord: */
568 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
569 glEnableVertexAttribArray(1);
570
571 for (i = 0, first = 0; i < nfans; i++) {
572 glDrawArrays(GL_TRIANGLE_FAN, first, vtxcnt[i]);
573 if (ec->fan_debug)
574 triangle_fan_debug(es, first, vtxcnt[i]);
575 first += vtxcnt[i];
576 }
577
578 glDisableVertexAttribArray(1);
579 glDisableVertexAttribArray(0);
580
581 ec->vertices.size = 0;
582 ec->vtxcnt.size = 0;
583}
584
585static void
586weston_compositor_use_shader(struct weston_compositor *compositor,
587 struct weston_shader *shader)
588{
589 if (compositor->current_shader == shader)
590 return;
591
592 glUseProgram(shader->program);
593 compositor->current_shader = shader;
594}
595
596static void
597weston_shader_uniforms(struct weston_shader *shader,
598 struct weston_surface *surface,
599 struct weston_output *output)
600{
601 int i;
602
603 glUniformMatrix4fv(shader->proj_uniform,
604 1, GL_FALSE, output->matrix.d);
605 glUniform4fv(shader->color_uniform, 1, surface->color);
606 glUniform1f(shader->alpha_uniform, surface->alpha);
607
608 for (i = 0; i < surface->num_textures; i++)
609 glUniform1i(shader->tex_uniforms[i], i);
610}
611
612static void
613draw_surface(struct weston_surface *es, struct weston_output *output,
614 pixman_region32_t *damage) /* in global coordinates */
615{
616 struct weston_compositor *ec = es->compositor;
617 /* repaint bounding region in global coordinates: */
618 pixman_region32_t repaint;
619 /* non-opaque region in surface coordinates: */
620 pixman_region32_t surface_blend;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300621 pixman_region32_t *buffer_damage;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400622 GLint filter;
623 int i;
624
625 pixman_region32_init(&repaint);
626 pixman_region32_intersect(&repaint,
627 &es->transform.boundingbox, damage);
628 pixman_region32_subtract(&repaint, &repaint, &es->clip);
629
630 if (!pixman_region32_not_empty(&repaint))
631 goto out;
632
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300633 buffer_damage = &output->buffer_damage[output->current_buffer];
634 pixman_region32_subtract(buffer_damage, buffer_damage, &repaint);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400635
636 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
637
638 if (ec->fan_debug) {
639 weston_compositor_use_shader(ec, &ec->solid_shader);
640 weston_shader_uniforms(&ec->solid_shader, es, output);
641 }
642
643 weston_compositor_use_shader(ec, es->shader);
644 weston_shader_uniforms(es->shader, es, output);
645
646 if (es->transform.enabled || output->zoom.active)
647 filter = GL_LINEAR;
648 else
649 filter = GL_NEAREST;
650
651 for (i = 0; i < es->num_textures; i++) {
652 glActiveTexture(GL_TEXTURE0 + i);
653 glBindTexture(es->target, es->textures[i]);
654 glTexParameteri(es->target, GL_TEXTURE_MIN_FILTER, filter);
655 glTexParameteri(es->target, GL_TEXTURE_MAG_FILTER, filter);
656 }
657
658 /* blended region is whole surface minus opaque region: */
659 pixman_region32_init_rect(&surface_blend, 0, 0,
660 es->geometry.width, es->geometry.height);
661 pixman_region32_subtract(&surface_blend, &surface_blend, &es->opaque);
662
663 if (pixman_region32_not_empty(&es->opaque)) {
664 if (es->shader == &ec->texture_shader_rgba) {
665 /* Special case for RGBA textures with possibly
666 * bad data in alpha channel: use the shader
667 * that forces texture alpha = 1.0.
668 * Xwayland surfaces need this.
669 */
670 weston_compositor_use_shader(ec, &ec->texture_shader_rgbx);
671 weston_shader_uniforms(&ec->texture_shader_rgbx, es, output);
672 }
673
674 if (es->alpha < 1.0)
675 glEnable(GL_BLEND);
676 else
677 glDisable(GL_BLEND);
678
679 repaint_region(es, &repaint, &es->opaque);
680 }
681
682 if (pixman_region32_not_empty(&surface_blend)) {
683 weston_compositor_use_shader(ec, es->shader);
684 glEnable(GL_BLEND);
685 repaint_region(es, &repaint, &surface_blend);
686 }
687
688 pixman_region32_fini(&surface_blend);
689
690out:
691 pixman_region32_fini(&repaint);
692}
693
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400694static void
695repaint_surfaces(struct weston_output *output, pixman_region32_t *damage)
696{
697 struct weston_compositor *compositor = output->compositor;
698 struct weston_surface *surface;
699
700 wl_list_for_each_reverse(surface, &compositor->surface_list, link)
701 if (surface->plane == &compositor->primary_plane)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400702 draw_surface(surface, output, damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400703}
704
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400705static void
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400706gles2_renderer_repaint_output(struct weston_output *output,
707 pixman_region32_t *output_damage)
708{
709 struct weston_compositor *compositor = output->compositor;
710 EGLBoolean ret;
711 static int errored;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300712 int32_t width, height, i;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400713
714 width = output->current->width +
715 output->border.left + output->border.right;
716 height = output->current->height +
717 output->border.top + output->border.bottom;
718
719 glViewport(0, 0, width, height);
720
721 ret = eglMakeCurrent(compositor->egl_display, output->egl_surface,
722 output->egl_surface, compositor->egl_context);
723 if (ret == EGL_FALSE) {
724 if (errored)
725 return;
726 errored = 1;
727 weston_log("Failed to make EGL context current.\n");
728 print_egl_error_state();
729 return;
730 }
731
732 /* if debugging, redraw everything outside the damage to clean up
733 * debug lines from the previous draw on this buffer:
734 */
735 if (compositor->fan_debug) {
736 pixman_region32_t undamaged;
737 pixman_region32_init(&undamaged);
738 pixman_region32_subtract(&undamaged, &output->region,
739 output_damage);
740 compositor->fan_debug = 0;
741 repaint_surfaces(output, &undamaged);
742 compositor->fan_debug = 1;
743 pixman_region32_fini(&undamaged);
744 }
745
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300746 for (i = 0; i < 2; i++)
747 pixman_region32_union(&output->buffer_damage[i],
748 &output->buffer_damage[i],
749 output_damage);
750
751 pixman_region32_union(output_damage, output_damage,
752 &output->buffer_damage[output->current_buffer]);
753
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400754 repaint_surfaces(output, output_damage);
755
756 wl_signal_emit(&output->frame_signal, output);
757
758 ret = eglSwapBuffers(compositor->egl_display, output->egl_surface);
759 if (ret == EGL_FALSE && !errored) {
760 errored = 1;
761 weston_log("Failed in eglSwapBuffers.\n");
762 print_egl_error_state();
763 }
764
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300765 output->current_buffer ^= 1;
766
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400767}
Kristian Høgsberg25894fc2012-09-05 22:06:26 -0400768
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400769static void
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -0400770gles2_renderer_flush_damage(struct weston_surface *surface)
771{
772#ifdef GL_UNPACK_ROW_LENGTH
773 pixman_box32_t *rectangles;
774 void *data;
775 int i, n;
776#endif
777
778 glBindTexture(GL_TEXTURE_2D, surface->textures[0]);
779
780 if (!surface->compositor->has_unpack_subimage) {
781 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
782 surface->pitch, surface->buffer->height, 0,
783 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
784 wl_shm_buffer_get_data(surface->buffer));
785
786 return;
787 }
788
789#ifdef GL_UNPACK_ROW_LENGTH
790 /* Mesa does not define GL_EXT_unpack_subimage */
791 glPixelStorei(GL_UNPACK_ROW_LENGTH, surface->pitch);
792 data = wl_shm_buffer_get_data(surface->buffer);
793 rectangles = pixman_region32_rectangles(&surface->damage, &n);
794 for (i = 0; i < n; i++) {
795 glPixelStorei(GL_UNPACK_SKIP_PIXELS, rectangles[i].x1);
796 glPixelStorei(GL_UNPACK_SKIP_ROWS, rectangles[i].y1);
797 glTexSubImage2D(GL_TEXTURE_2D, 0,
798 rectangles[i].x1, rectangles[i].y1,
799 rectangles[i].x2 - rectangles[i].x1,
800 rectangles[i].y2 - rectangles[i].y1,
801 GL_BGRA_EXT, GL_UNSIGNED_BYTE, data);
802 }
803#endif
804}
805
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400806static void
807ensure_textures(struct weston_surface *es, int num_textures)
808{
809 int i;
810
811 if (num_textures <= es->num_textures)
812 return;
813
814 for (i = es->num_textures; i < num_textures; i++) {
815 glGenTextures(1, &es->textures[i]);
816 glBindTexture(es->target, es->textures[i]);
817 glTexParameteri(es->target,
818 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
819 glTexParameteri(es->target,
820 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
821 }
822 es->num_textures = num_textures;
823 glBindTexture(es->target, 0);
824}
825
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400826static void
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400827gles2_renderer_attach(struct weston_surface *es, struct wl_buffer *buffer)
828{
829 struct weston_compositor *ec = es->compositor;
830 EGLint attribs[3], format;
831 int i, num_planes;
832
833 if (!buffer) {
834 for (i = 0; i < es->num_images; i++) {
835 ec->destroy_image(ec->egl_display, es->images[i]);
836 es->images[i] = NULL;
837 }
838 es->num_images = 0;
839 glDeleteTextures(es->num_textures, es->textures);
840 es->num_textures = 0;
841 return;
842 }
843
844 if (wl_buffer_is_shm(buffer)) {
845 es->pitch = wl_shm_buffer_get_stride(buffer) / 4;
846 es->target = GL_TEXTURE_2D;
847
848 ensure_textures(es, 1);
849 glBindTexture(GL_TEXTURE_2D, es->textures[0]);
850 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
851 es->pitch, buffer->height, 0,
852 GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
853 if (wl_shm_buffer_get_format(buffer) == WL_SHM_FORMAT_XRGB8888)
854 es->shader = &ec->texture_shader_rgbx;
855 else
856 es->shader = &ec->texture_shader_rgba;
857 } else if (ec->query_buffer(ec->egl_display, buffer,
858 EGL_TEXTURE_FORMAT, &format)) {
859 for (i = 0; i < es->num_images; i++)
860 ec->destroy_image(ec->egl_display, es->images[i]);
861 es->num_images = 0;
862 es->target = GL_TEXTURE_2D;
863 switch (format) {
864 case EGL_TEXTURE_RGB:
865 case EGL_TEXTURE_RGBA:
866 default:
867 num_planes = 1;
868 es->shader = &ec->texture_shader_rgba;
869 break;
870 case EGL_TEXTURE_EXTERNAL_WL:
871 num_planes = 1;
872 es->target = GL_TEXTURE_EXTERNAL_OES;
873 es->shader = &ec->texture_shader_egl_external;
874 break;
875 case EGL_TEXTURE_Y_UV_WL:
876 num_planes = 2;
877 es->shader = &ec->texture_shader_y_uv;
878 break;
879 case EGL_TEXTURE_Y_U_V_WL:
880 num_planes = 3;
881 es->shader = &ec->texture_shader_y_u_v;
882 break;
883 case EGL_TEXTURE_Y_XUXV_WL:
884 num_planes = 2;
885 es->shader = &ec->texture_shader_y_xuxv;
886 break;
887 }
888
889 ensure_textures(es, num_planes);
890 for (i = 0; i < num_planes; i++) {
891 attribs[0] = EGL_WAYLAND_PLANE_WL;
892 attribs[1] = i;
893 attribs[2] = EGL_NONE;
894 es->images[i] = ec->create_image(ec->egl_display,
895 NULL,
896 EGL_WAYLAND_BUFFER_WL,
897 buffer, attribs);
898 if (!es->images[i]) {
899 weston_log("failed to create img for plane %d\n", i);
900 continue;
901 }
902 es->num_images++;
903
904 glActiveTexture(GL_TEXTURE0 + i);
905 glBindTexture(es->target, es->textures[i]);
906 ec->image_target_texture_2d(es->target,
907 es->images[i]);
908 }
909
910 es->pitch = buffer->width;
911 } else {
912 weston_log("unhandled buffer type!\n");
913 }
914}
915
Kristian Høgsberg42263852012-09-06 21:59:29 -0400916static void
917gles2_renderer_destroy_surface(struct weston_surface *surface)
918{
919 struct weston_compositor *ec = surface->compositor;
920 int i;
921
922 glDeleteTextures(surface->num_textures, surface->textures);
923
924 for (i = 0; i < surface->num_images; i++)
925 ec->destroy_image(ec->egl_display, surface->images[i]);
926}
927
Kristian Høgsberg25894fc2012-09-05 22:06:26 -0400928static const char vertex_shader[] =
929 "uniform mat4 proj;\n"
930 "attribute vec2 position;\n"
931 "attribute vec2 texcoord;\n"
932 "varying vec2 v_texcoord;\n"
933 "void main()\n"
934 "{\n"
935 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
936 " v_texcoord = texcoord;\n"
937 "}\n";
938
939/* Declare common fragment shader uniforms */
940#define FRAGMENT_CONVERT_YUV \
941 " y *= alpha;\n" \
942 " u *= alpha;\n" \
943 " v *= alpha;\n" \
944 " gl_FragColor.r = y + 1.59602678 * v;\n" \
945 " gl_FragColor.g = y - 0.39176229 * u - 0.81296764 * v;\n" \
946 " gl_FragColor.b = y + 2.01723214 * u;\n" \
947 " gl_FragColor.a = alpha;\n"
948
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +0200949static const char fragment_debug[] =
950 " gl_FragColor = vec4(0.0, 0.3, 0.0, 0.2) + gl_FragColor * 0.8;\n";
951
952static const char fragment_brace[] =
953 "}\n";
954
Kristian Høgsberg25894fc2012-09-05 22:06:26 -0400955static const char texture_fragment_shader_rgba[] =
956 "precision mediump float;\n"
957 "varying vec2 v_texcoord;\n"
958 "uniform sampler2D tex;\n"
959 "uniform float alpha;\n"
960 "void main()\n"
961 "{\n"
962 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +0200963 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -0400964
965static const char texture_fragment_shader_rgbx[] =
966 "precision mediump float;\n"
967 "varying vec2 v_texcoord;\n"
968 "uniform sampler2D tex;\n"
969 "uniform float alpha;\n"
970 "void main()\n"
971 "{\n"
972 " gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb\n;"
973 " gl_FragColor.a = alpha;\n"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +0200974 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -0400975
976static const char texture_fragment_shader_egl_external[] =
977 "#extension GL_OES_EGL_image_external : require\n"
978 "precision mediump float;\n"
979 "varying vec2 v_texcoord;\n"
980 "uniform samplerExternalOES tex;\n"
981 "uniform float alpha;\n"
982 "void main()\n"
983 "{\n"
984 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +0200985 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -0400986
987static const char texture_fragment_shader_y_uv[] =
988 "precision mediump float;\n"
989 "uniform sampler2D tex;\n"
990 "uniform sampler2D tex1;\n"
991 "varying vec2 v_texcoord;\n"
992 "uniform float alpha;\n"
993 "void main() {\n"
994 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
995 " float u = texture2D(tex1, v_texcoord).r - 0.5;\n"
996 " float v = texture2D(tex1, v_texcoord).g - 0.5;\n"
997 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +0200998 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -0400999
1000static const char texture_fragment_shader_y_u_v[] =
1001 "precision mediump float;\n"
1002 "uniform sampler2D tex;\n"
1003 "uniform sampler2D tex1;\n"
1004 "uniform sampler2D tex2;\n"
1005 "varying vec2 v_texcoord;\n"
1006 "uniform float alpha;\n"
1007 "void main() {\n"
1008 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1009 " float u = texture2D(tex1, v_texcoord).x - 0.5;\n"
1010 " float v = texture2D(tex2, v_texcoord).x - 0.5;\n"
1011 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001012 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001013
1014static const char texture_fragment_shader_y_xuxv[] =
1015 "precision mediump float;\n"
1016 "uniform sampler2D tex;\n"
1017 "uniform sampler2D tex1;\n"
1018 "varying vec2 v_texcoord;\n"
1019 "uniform float alpha;\n"
1020 "void main() {\n"
1021 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1022 " float u = texture2D(tex1, v_texcoord).g - 0.5;\n"
1023 " float v = texture2D(tex1, v_texcoord).a - 0.5;\n"
1024 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001025 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001026
1027static const char solid_fragment_shader[] =
1028 "precision mediump float;\n"
1029 "uniform vec4 color;\n"
1030 "uniform float alpha;\n"
1031 "void main()\n"
1032 "{\n"
1033 " gl_FragColor = alpha * color\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001034 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001035
1036static int
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001037compile_shader(GLenum type, int count, const char **sources)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001038{
1039 GLuint s;
1040 char msg[512];
1041 GLint status;
1042
1043 s = glCreateShader(type);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001044 glShaderSource(s, count, sources, NULL);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001045 glCompileShader(s);
1046 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
1047 if (!status) {
1048 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1049 weston_log("shader info: %s\n", msg);
1050 return GL_NONE;
1051 }
1052
1053 return s;
1054}
1055
1056static int
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001057weston_shader_init(struct weston_shader *shader, struct weston_compositor *ec,
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001058 const char *vertex_source, const char *fragment_source)
1059{
1060 char msg[512];
1061 GLint status;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001062 int count;
1063 const char *sources[3];
1064 struct gles2_renderer *renderer =
1065 (struct gles2_renderer *) ec->renderer;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001066
1067 shader->vertex_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001068 compile_shader(GL_VERTEX_SHADER, 1, &vertex_source);
1069
1070 if (renderer->fragment_shader_debug) {
1071 sources[0] = fragment_source;
1072 sources[1] = fragment_debug;
1073 sources[2] = fragment_brace;
1074 count = 3;
1075 } else {
1076 sources[0] = fragment_source;
1077 sources[1] = fragment_brace;
1078 count = 2;
1079 }
1080
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001081 shader->fragment_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001082 compile_shader(GL_FRAGMENT_SHADER, count, sources);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001083
1084 shader->program = glCreateProgram();
1085 glAttachShader(shader->program, shader->vertex_shader);
1086 glAttachShader(shader->program, shader->fragment_shader);
1087 glBindAttribLocation(shader->program, 0, "position");
1088 glBindAttribLocation(shader->program, 1, "texcoord");
1089
1090 glLinkProgram(shader->program);
1091 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1092 if (!status) {
1093 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1094 weston_log("link info: %s\n", msg);
1095 return -1;
1096 }
1097
1098 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1099 shader->tex_uniforms[0] = glGetUniformLocation(shader->program, "tex");
1100 shader->tex_uniforms[1] = glGetUniformLocation(shader->program, "tex1");
1101 shader->tex_uniforms[2] = glGetUniformLocation(shader->program, "tex2");
1102 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
1103 shader->color_uniform = glGetUniformLocation(shader->program, "color");
1104
1105 return 0;
1106}
1107
1108static void
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001109weston_shader_release(struct weston_shader *shader)
1110{
1111 glDeleteShader(shader->vertex_shader);
1112 glDeleteShader(shader->fragment_shader);
1113 glDeleteProgram(shader->program);
1114
1115 shader->vertex_shader = 0;
1116 shader->fragment_shader = 0;
1117 shader->program = 0;
1118}
1119
1120static void
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001121log_extensions(const char *name, const char *extensions)
1122{
1123 const char *p, *end;
1124 int l;
1125 int len;
1126
1127 l = weston_log("%s:", name);
1128 p = extensions;
1129 while (*p) {
1130 end = strchrnul(p, ' ');
1131 len = end - p;
1132 if (l + len > 78)
1133 l = weston_log_continue("\n" STAMP_SPACE "%.*s",
1134 len, p);
1135 else
1136 l += weston_log_continue(" %.*s", len, p);
1137 for (p = end; isspace(*p); p++)
1138 ;
1139 }
1140 weston_log_continue("\n");
1141}
1142
1143static void
1144log_egl_gl_info(EGLDisplay egldpy)
1145{
1146 const char *str;
1147
1148 str = eglQueryString(egldpy, EGL_VERSION);
1149 weston_log("EGL version: %s\n", str ? str : "(null)");
1150
1151 str = eglQueryString(egldpy, EGL_VENDOR);
1152 weston_log("EGL vendor: %s\n", str ? str : "(null)");
1153
1154 str = eglQueryString(egldpy, EGL_CLIENT_APIS);
1155 weston_log("EGL client APIs: %s\n", str ? str : "(null)");
1156
1157 str = eglQueryString(egldpy, EGL_EXTENSIONS);
1158 log_extensions("EGL extensions", str ? str : "(null)");
1159
1160 str = (char *)glGetString(GL_VERSION);
1161 weston_log("GL version: %s\n", str ? str : "(null)");
1162
1163 str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
1164 weston_log("GLSL version: %s\n", str ? str : "(null)");
1165
1166 str = (char *)glGetString(GL_VENDOR);
1167 weston_log("GL vendor: %s\n", str ? str : "(null)");
1168
1169 str = (char *)glGetString(GL_RENDERER);
1170 weston_log("GL renderer: %s\n", str ? str : "(null)");
1171
1172 str = (char *)glGetString(GL_EXTENSIONS);
1173 log_extensions("GL extensions", str ? str : "(null)");
1174}
1175
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001176static void
1177log_egl_config_info(EGLDisplay egldpy, EGLConfig eglconfig)
1178{
1179 EGLint r, g, b, a;
1180
1181 weston_log("Chosen EGL config details:\n");
1182
1183 weston_log_continue(STAMP_SPACE "RGBA bits");
1184 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_RED_SIZE, &r) &&
1185 eglGetConfigAttrib(egldpy, eglconfig, EGL_GREEN_SIZE, &g) &&
1186 eglGetConfigAttrib(egldpy, eglconfig, EGL_BLUE_SIZE, &b) &&
1187 eglGetConfigAttrib(egldpy, eglconfig, EGL_ALPHA_SIZE, &a))
1188 weston_log_continue(": %d %d %d %d\n", r, g, b, a);
1189 else
1190 weston_log_continue(" unknown\n");
1191
1192 weston_log_continue(STAMP_SPACE "swap interval range");
1193 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_MIN_SWAP_INTERVAL, &a) &&
1194 eglGetConfigAttrib(egldpy, eglconfig, EGL_MAX_SWAP_INTERVAL, &b))
1195 weston_log_continue(": %d - %d\n", a, b);
1196 else
1197 weston_log_continue(" unknown\n");
1198}
1199
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001200WL_EXPORT void
1201gles2_renderer_destroy(struct weston_compositor *ec)
1202{
1203 if (ec->has_bind_display)
1204 ec->unbind_display(ec->egl_display, ec->wl_display);
1205}
1206
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001207static int
1208compile_shaders(struct weston_compositor *ec)
1209{
1210 if (weston_shader_init(&ec->texture_shader_rgba, ec,
1211 vertex_shader, texture_fragment_shader_rgba) < 0)
1212 return -1;
1213 if (weston_shader_init(&ec->texture_shader_rgbx, ec,
1214 vertex_shader, texture_fragment_shader_rgbx) < 0)
1215 return -1;
1216 if (ec->has_egl_image_external &&
1217 weston_shader_init(&ec->texture_shader_egl_external, ec,
1218 vertex_shader, texture_fragment_shader_egl_external) < 0)
1219 return -1;
1220 if (weston_shader_init(&ec->texture_shader_y_uv, ec,
1221 vertex_shader, texture_fragment_shader_y_uv) < 0)
1222 return -1;
1223 if (weston_shader_init(&ec->texture_shader_y_u_v, ec,
1224 vertex_shader, texture_fragment_shader_y_u_v) < 0)
1225 return -1;
1226 if (weston_shader_init(&ec->texture_shader_y_xuxv, ec,
1227 vertex_shader, texture_fragment_shader_y_xuxv) < 0)
1228 return -1;
1229 if (weston_shader_init(&ec->solid_shader, ec,
1230 vertex_shader, solid_fragment_shader) < 0)
1231 return -1;
1232
1233 return 0;
1234}
1235
1236static void
1237fragment_debug_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
1238 void *data)
1239{
1240 struct weston_compositor *ec = data;
1241 struct gles2_renderer *renderer =
1242 (struct gles2_renderer *) ec->renderer;
1243 struct weston_output *output;
1244
1245 renderer->fragment_shader_debug ^= 1;
1246
1247 weston_shader_release(&ec->texture_shader_rgba);
1248 weston_shader_release(&ec->texture_shader_rgbx);
1249 weston_shader_release(&ec->texture_shader_egl_external);
1250 weston_shader_release(&ec->texture_shader_y_uv);
1251 weston_shader_release(&ec->texture_shader_y_u_v);
1252 weston_shader_release(&ec->texture_shader_y_xuxv);
1253 weston_shader_release(&ec->solid_shader);
1254
1255 compile_shaders(ec);
1256
1257 wl_list_for_each(output, &ec->output_list, link)
1258 weston_output_damage(output);
1259}
1260
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001261WL_EXPORT int
1262gles2_renderer_init(struct weston_compositor *ec)
1263{
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001264 struct gles2_renderer *renderer;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001265 const char *extensions;
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001266 struct weston_output *output;
1267 EGLBoolean ret;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001268
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001269 static const EGLint context_attribs[] = {
1270 EGL_CONTEXT_CLIENT_VERSION, 2,
1271 EGL_NONE
1272 };
1273
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001274 renderer = calloc(1, sizeof *renderer);
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001275 if (renderer == NULL)
1276 return -1;
1277
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001278 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
1279 weston_log("failed to bind EGL_OPENGL_ES_API\n");
1280 print_egl_error_state();
1281 return -1;
1282 }
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001283
1284 log_egl_config_info(ec->egl_display, ec->egl_config);
1285
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001286 ec->egl_context = eglCreateContext(ec->egl_display, ec->egl_config,
1287 EGL_NO_CONTEXT, context_attribs);
1288 if (ec->egl_context == NULL) {
1289 weston_log("failed to create context\n");
1290 print_egl_error_state();
1291 return -1;
1292 }
1293
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001294 output = container_of(ec->output_list.next,
1295 struct weston_output, link);
1296 ret = eglMakeCurrent(ec->egl_display, output->egl_surface,
1297 output->egl_surface, ec->egl_context);
1298 if (ret == EGL_FALSE) {
1299 weston_log("Failed to make EGL context current.\n");
1300 print_egl_error_state();
1301 return -1;
1302 }
1303
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001304 log_egl_gl_info(ec->egl_display);
1305
1306 ec->image_target_texture_2d =
1307 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
1308 ec->image_target_renderbuffer_storage = (void *)
1309 eglGetProcAddress("glEGLImageTargetRenderbufferStorageOES");
1310 ec->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1311 ec->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1312 ec->bind_display =
1313 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
1314 ec->unbind_display =
1315 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
1316 ec->query_buffer =
1317 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
1318
1319 extensions = (const char *) glGetString(GL_EXTENSIONS);
1320 if (!extensions) {
1321 weston_log("Retrieving GL extension string failed.\n");
1322 return -1;
1323 }
1324
1325 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
1326 weston_log("GL_EXT_texture_format_BGRA8888 not available\n");
1327 return -1;
1328 }
1329
1330 if (strstr(extensions, "GL_EXT_read_format_bgra"))
1331 ec->read_format = GL_BGRA_EXT;
1332 else
1333 ec->read_format = GL_RGBA;
1334
1335 if (strstr(extensions, "GL_EXT_unpack_subimage"))
1336 ec->has_unpack_subimage = 1;
1337
1338 if (strstr(extensions, "GL_OES_EGL_image_external"))
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001339 ec->has_egl_image_external = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001340
1341 extensions =
1342 (const char *) eglQueryString(ec->egl_display, EGL_EXTENSIONS);
1343 if (!extensions) {
1344 weston_log("Retrieving EGL extension string failed.\n");
1345 return -1;
1346 }
1347
1348 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
1349 ec->has_bind_display = 1;
Pekka Paalanen035a0322012-10-24 09:43:06 +03001350 if (ec->has_bind_display) {
1351 ret = ec->bind_display(ec->egl_display, ec->wl_display);
1352 if (!ret)
1353 ec->has_bind_display = 0;
1354 }
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001355
1356 glActiveTexture(GL_TEXTURE0);
1357
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001358 renderer->base.repaint_output = gles2_renderer_repaint_output;
1359 renderer->base.flush_damage = gles2_renderer_flush_damage;
1360 renderer->base.attach = gles2_renderer_attach;
Kristian Høgsberg42263852012-09-06 21:59:29 -04001361 renderer->base.destroy_surface = gles2_renderer_destroy_surface;
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001362 ec->renderer = &renderer->base;
1363
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001364 if (compile_shaders(ec))
1365 return -1;
1366
1367 weston_compositor_add_debug_binding(ec, KEY_S,
1368 fragment_debug_binding, ec);
1369
Pekka Paalanen035a0322012-10-24 09:43:06 +03001370 weston_log("GL ES 2 renderer features:\n");
1371 weston_log_continue(STAMP_SPACE "read-back format: %s\n",
1372 ec->read_format == GL_BGRA_EXT ? "BGRA" : "RGBA");
1373 weston_log_continue(STAMP_SPACE "wl_shm sub-image to texture: %s\n",
1374 ec->has_unpack_subimage ? "yes" : "no");
1375 weston_log_continue(STAMP_SPACE "EGL Wayland extension: %s\n",
1376 ec->has_bind_display ? "yes" : "no");
1377
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001378 return 0;
1379}