blob: 49fe7b04a46ce4d0918cd94b0732583a35428221 [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
Pekka Paalanenbcdd5792012-11-07 12:25:13 +0200778 pixman_region32_union(&surface->texture_damage,
779 &surface->texture_damage, &surface->damage);
780
781 /* Avoid upload, if the texture won't be used this time.
782 * We still accumulate the damage in texture_damage.
783 */
784 if (surface->plane != &surface->compositor->primary_plane)
785 return;
786
787 if (!pixman_region32_not_empty(&surface->texture_damage))
788 return;
789
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -0400790 glBindTexture(GL_TEXTURE_2D, surface->textures[0]);
791
792 if (!surface->compositor->has_unpack_subimage) {
793 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
794 surface->pitch, surface->buffer->height, 0,
795 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
796 wl_shm_buffer_get_data(surface->buffer));
797
Pekka Paalanenbcdd5792012-11-07 12:25:13 +0200798 goto done;
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -0400799 }
800
801#ifdef GL_UNPACK_ROW_LENGTH
802 /* Mesa does not define GL_EXT_unpack_subimage */
803 glPixelStorei(GL_UNPACK_ROW_LENGTH, surface->pitch);
804 data = wl_shm_buffer_get_data(surface->buffer);
Pekka Paalanenbcdd5792012-11-07 12:25:13 +0200805 rectangles = pixman_region32_rectangles(&surface->texture_damage, &n);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -0400806 for (i = 0; i < n; i++) {
807 glPixelStorei(GL_UNPACK_SKIP_PIXELS, rectangles[i].x1);
808 glPixelStorei(GL_UNPACK_SKIP_ROWS, rectangles[i].y1);
809 glTexSubImage2D(GL_TEXTURE_2D, 0,
810 rectangles[i].x1, rectangles[i].y1,
811 rectangles[i].x2 - rectangles[i].x1,
812 rectangles[i].y2 - rectangles[i].y1,
813 GL_BGRA_EXT, GL_UNSIGNED_BYTE, data);
814 }
815#endif
Pekka Paalanenbcdd5792012-11-07 12:25:13 +0200816
817done:
818 pixman_region32_fini(&surface->texture_damage);
819 pixman_region32_init(&surface->texture_damage);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -0400820}
821
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400822static void
823ensure_textures(struct weston_surface *es, int num_textures)
824{
825 int i;
826
827 if (num_textures <= es->num_textures)
828 return;
829
830 for (i = es->num_textures; i < num_textures; i++) {
831 glGenTextures(1, &es->textures[i]);
832 glBindTexture(es->target, es->textures[i]);
833 glTexParameteri(es->target,
834 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
835 glTexParameteri(es->target,
836 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
837 }
838 es->num_textures = num_textures;
839 glBindTexture(es->target, 0);
840}
841
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400842static void
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400843gles2_renderer_attach(struct weston_surface *es, struct wl_buffer *buffer)
844{
845 struct weston_compositor *ec = es->compositor;
846 EGLint attribs[3], format;
847 int i, num_planes;
848
849 if (!buffer) {
850 for (i = 0; i < es->num_images; i++) {
851 ec->destroy_image(ec->egl_display, es->images[i]);
852 es->images[i] = NULL;
853 }
854 es->num_images = 0;
855 glDeleteTextures(es->num_textures, es->textures);
856 es->num_textures = 0;
857 return;
858 }
859
860 if (wl_buffer_is_shm(buffer)) {
861 es->pitch = wl_shm_buffer_get_stride(buffer) / 4;
862 es->target = GL_TEXTURE_2D;
863
864 ensure_textures(es, 1);
865 glBindTexture(GL_TEXTURE_2D, es->textures[0]);
866 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
867 es->pitch, buffer->height, 0,
868 GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
869 if (wl_shm_buffer_get_format(buffer) == WL_SHM_FORMAT_XRGB8888)
870 es->shader = &ec->texture_shader_rgbx;
871 else
872 es->shader = &ec->texture_shader_rgba;
873 } else if (ec->query_buffer(ec->egl_display, buffer,
874 EGL_TEXTURE_FORMAT, &format)) {
875 for (i = 0; i < es->num_images; i++)
876 ec->destroy_image(ec->egl_display, es->images[i]);
877 es->num_images = 0;
878 es->target = GL_TEXTURE_2D;
879 switch (format) {
880 case EGL_TEXTURE_RGB:
881 case EGL_TEXTURE_RGBA:
882 default:
883 num_planes = 1;
884 es->shader = &ec->texture_shader_rgba;
885 break;
886 case EGL_TEXTURE_EXTERNAL_WL:
887 num_planes = 1;
888 es->target = GL_TEXTURE_EXTERNAL_OES;
889 es->shader = &ec->texture_shader_egl_external;
890 break;
891 case EGL_TEXTURE_Y_UV_WL:
892 num_planes = 2;
893 es->shader = &ec->texture_shader_y_uv;
894 break;
895 case EGL_TEXTURE_Y_U_V_WL:
896 num_planes = 3;
897 es->shader = &ec->texture_shader_y_u_v;
898 break;
899 case EGL_TEXTURE_Y_XUXV_WL:
900 num_planes = 2;
901 es->shader = &ec->texture_shader_y_xuxv;
902 break;
903 }
904
905 ensure_textures(es, num_planes);
906 for (i = 0; i < num_planes; i++) {
907 attribs[0] = EGL_WAYLAND_PLANE_WL;
908 attribs[1] = i;
909 attribs[2] = EGL_NONE;
910 es->images[i] = ec->create_image(ec->egl_display,
911 NULL,
912 EGL_WAYLAND_BUFFER_WL,
913 buffer, attribs);
914 if (!es->images[i]) {
915 weston_log("failed to create img for plane %d\n", i);
916 continue;
917 }
918 es->num_images++;
919
920 glActiveTexture(GL_TEXTURE0 + i);
921 glBindTexture(es->target, es->textures[i]);
922 ec->image_target_texture_2d(es->target,
923 es->images[i]);
924 }
925
926 es->pitch = buffer->width;
927 } else {
928 weston_log("unhandled buffer type!\n");
929 }
930}
931
Kristian Høgsberg42263852012-09-06 21:59:29 -0400932static void
933gles2_renderer_destroy_surface(struct weston_surface *surface)
934{
935 struct weston_compositor *ec = surface->compositor;
936 int i;
937
938 glDeleteTextures(surface->num_textures, surface->textures);
939
940 for (i = 0; i < surface->num_images; i++)
941 ec->destroy_image(ec->egl_display, surface->images[i]);
942}
943
Kristian Høgsberg25894fc2012-09-05 22:06:26 -0400944static const char vertex_shader[] =
945 "uniform mat4 proj;\n"
946 "attribute vec2 position;\n"
947 "attribute vec2 texcoord;\n"
948 "varying vec2 v_texcoord;\n"
949 "void main()\n"
950 "{\n"
951 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
952 " v_texcoord = texcoord;\n"
953 "}\n";
954
955/* Declare common fragment shader uniforms */
956#define FRAGMENT_CONVERT_YUV \
957 " y *= alpha;\n" \
958 " u *= alpha;\n" \
959 " v *= alpha;\n" \
960 " gl_FragColor.r = y + 1.59602678 * v;\n" \
961 " gl_FragColor.g = y - 0.39176229 * u - 0.81296764 * v;\n" \
962 " gl_FragColor.b = y + 2.01723214 * u;\n" \
963 " gl_FragColor.a = alpha;\n"
964
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +0200965static const char fragment_debug[] =
966 " gl_FragColor = vec4(0.0, 0.3, 0.0, 0.2) + gl_FragColor * 0.8;\n";
967
968static const char fragment_brace[] =
969 "}\n";
970
Kristian Høgsberg25894fc2012-09-05 22:06:26 -0400971static const char texture_fragment_shader_rgba[] =
972 "precision mediump float;\n"
973 "varying vec2 v_texcoord;\n"
974 "uniform sampler2D tex;\n"
975 "uniform float alpha;\n"
976 "void main()\n"
977 "{\n"
978 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +0200979 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -0400980
981static const char texture_fragment_shader_rgbx[] =
982 "precision mediump float;\n"
983 "varying vec2 v_texcoord;\n"
984 "uniform sampler2D tex;\n"
985 "uniform float alpha;\n"
986 "void main()\n"
987 "{\n"
988 " gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb\n;"
989 " gl_FragColor.a = alpha;\n"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +0200990 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -0400991
992static const char texture_fragment_shader_egl_external[] =
993 "#extension GL_OES_EGL_image_external : require\n"
994 "precision mediump float;\n"
995 "varying vec2 v_texcoord;\n"
996 "uniform samplerExternalOES tex;\n"
997 "uniform float alpha;\n"
998 "void main()\n"
999 "{\n"
1000 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001001 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001002
1003static const char texture_fragment_shader_y_uv[] =
1004 "precision mediump float;\n"
1005 "uniform sampler2D tex;\n"
1006 "uniform sampler2D tex1;\n"
1007 "varying vec2 v_texcoord;\n"
1008 "uniform float alpha;\n"
1009 "void main() {\n"
1010 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1011 " float u = texture2D(tex1, v_texcoord).r - 0.5;\n"
1012 " float v = texture2D(tex1, v_texcoord).g - 0.5;\n"
1013 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001014 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001015
1016static const char texture_fragment_shader_y_u_v[] =
1017 "precision mediump float;\n"
1018 "uniform sampler2D tex;\n"
1019 "uniform sampler2D tex1;\n"
1020 "uniform sampler2D tex2;\n"
1021 "varying vec2 v_texcoord;\n"
1022 "uniform float alpha;\n"
1023 "void main() {\n"
1024 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1025 " float u = texture2D(tex1, v_texcoord).x - 0.5;\n"
1026 " float v = texture2D(tex2, v_texcoord).x - 0.5;\n"
1027 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001028 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001029
1030static const char texture_fragment_shader_y_xuxv[] =
1031 "precision mediump float;\n"
1032 "uniform sampler2D tex;\n"
1033 "uniform sampler2D tex1;\n"
1034 "varying vec2 v_texcoord;\n"
1035 "uniform float alpha;\n"
1036 "void main() {\n"
1037 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1038 " float u = texture2D(tex1, v_texcoord).g - 0.5;\n"
1039 " float v = texture2D(tex1, v_texcoord).a - 0.5;\n"
1040 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001041 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001042
1043static const char solid_fragment_shader[] =
1044 "precision mediump float;\n"
1045 "uniform vec4 color;\n"
1046 "uniform float alpha;\n"
1047 "void main()\n"
1048 "{\n"
1049 " gl_FragColor = alpha * color\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001050 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001051
1052static int
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001053compile_shader(GLenum type, int count, const char **sources)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001054{
1055 GLuint s;
1056 char msg[512];
1057 GLint status;
1058
1059 s = glCreateShader(type);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001060 glShaderSource(s, count, sources, NULL);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001061 glCompileShader(s);
1062 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
1063 if (!status) {
1064 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1065 weston_log("shader info: %s\n", msg);
1066 return GL_NONE;
1067 }
1068
1069 return s;
1070}
1071
1072static int
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001073weston_shader_init(struct weston_shader *shader, struct weston_compositor *ec,
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001074 const char *vertex_source, const char *fragment_source)
1075{
1076 char msg[512];
1077 GLint status;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001078 int count;
1079 const char *sources[3];
1080 struct gles2_renderer *renderer =
1081 (struct gles2_renderer *) ec->renderer;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001082
1083 shader->vertex_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001084 compile_shader(GL_VERTEX_SHADER, 1, &vertex_source);
1085
1086 if (renderer->fragment_shader_debug) {
1087 sources[0] = fragment_source;
1088 sources[1] = fragment_debug;
1089 sources[2] = fragment_brace;
1090 count = 3;
1091 } else {
1092 sources[0] = fragment_source;
1093 sources[1] = fragment_brace;
1094 count = 2;
1095 }
1096
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001097 shader->fragment_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001098 compile_shader(GL_FRAGMENT_SHADER, count, sources);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001099
1100 shader->program = glCreateProgram();
1101 glAttachShader(shader->program, shader->vertex_shader);
1102 glAttachShader(shader->program, shader->fragment_shader);
1103 glBindAttribLocation(shader->program, 0, "position");
1104 glBindAttribLocation(shader->program, 1, "texcoord");
1105
1106 glLinkProgram(shader->program);
1107 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1108 if (!status) {
1109 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1110 weston_log("link info: %s\n", msg);
1111 return -1;
1112 }
1113
1114 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1115 shader->tex_uniforms[0] = glGetUniformLocation(shader->program, "tex");
1116 shader->tex_uniforms[1] = glGetUniformLocation(shader->program, "tex1");
1117 shader->tex_uniforms[2] = glGetUniformLocation(shader->program, "tex2");
1118 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
1119 shader->color_uniform = glGetUniformLocation(shader->program, "color");
1120
1121 return 0;
1122}
1123
1124static void
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001125weston_shader_release(struct weston_shader *shader)
1126{
1127 glDeleteShader(shader->vertex_shader);
1128 glDeleteShader(shader->fragment_shader);
1129 glDeleteProgram(shader->program);
1130
1131 shader->vertex_shader = 0;
1132 shader->fragment_shader = 0;
1133 shader->program = 0;
1134}
1135
1136static void
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001137log_extensions(const char *name, const char *extensions)
1138{
1139 const char *p, *end;
1140 int l;
1141 int len;
1142
1143 l = weston_log("%s:", name);
1144 p = extensions;
1145 while (*p) {
1146 end = strchrnul(p, ' ');
1147 len = end - p;
1148 if (l + len > 78)
1149 l = weston_log_continue("\n" STAMP_SPACE "%.*s",
1150 len, p);
1151 else
1152 l += weston_log_continue(" %.*s", len, p);
1153 for (p = end; isspace(*p); p++)
1154 ;
1155 }
1156 weston_log_continue("\n");
1157}
1158
1159static void
1160log_egl_gl_info(EGLDisplay egldpy)
1161{
1162 const char *str;
1163
1164 str = eglQueryString(egldpy, EGL_VERSION);
1165 weston_log("EGL version: %s\n", str ? str : "(null)");
1166
1167 str = eglQueryString(egldpy, EGL_VENDOR);
1168 weston_log("EGL vendor: %s\n", str ? str : "(null)");
1169
1170 str = eglQueryString(egldpy, EGL_CLIENT_APIS);
1171 weston_log("EGL client APIs: %s\n", str ? str : "(null)");
1172
1173 str = eglQueryString(egldpy, EGL_EXTENSIONS);
1174 log_extensions("EGL extensions", str ? str : "(null)");
1175
1176 str = (char *)glGetString(GL_VERSION);
1177 weston_log("GL version: %s\n", str ? str : "(null)");
1178
1179 str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
1180 weston_log("GLSL version: %s\n", str ? str : "(null)");
1181
1182 str = (char *)glGetString(GL_VENDOR);
1183 weston_log("GL vendor: %s\n", str ? str : "(null)");
1184
1185 str = (char *)glGetString(GL_RENDERER);
1186 weston_log("GL renderer: %s\n", str ? str : "(null)");
1187
1188 str = (char *)glGetString(GL_EXTENSIONS);
1189 log_extensions("GL extensions", str ? str : "(null)");
1190}
1191
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001192static void
1193log_egl_config_info(EGLDisplay egldpy, EGLConfig eglconfig)
1194{
1195 EGLint r, g, b, a;
1196
1197 weston_log("Chosen EGL config details:\n");
1198
1199 weston_log_continue(STAMP_SPACE "RGBA bits");
1200 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_RED_SIZE, &r) &&
1201 eglGetConfigAttrib(egldpy, eglconfig, EGL_GREEN_SIZE, &g) &&
1202 eglGetConfigAttrib(egldpy, eglconfig, EGL_BLUE_SIZE, &b) &&
1203 eglGetConfigAttrib(egldpy, eglconfig, EGL_ALPHA_SIZE, &a))
1204 weston_log_continue(": %d %d %d %d\n", r, g, b, a);
1205 else
1206 weston_log_continue(" unknown\n");
1207
1208 weston_log_continue(STAMP_SPACE "swap interval range");
1209 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_MIN_SWAP_INTERVAL, &a) &&
1210 eglGetConfigAttrib(egldpy, eglconfig, EGL_MAX_SWAP_INTERVAL, &b))
1211 weston_log_continue(": %d - %d\n", a, b);
1212 else
1213 weston_log_continue(" unknown\n");
1214}
1215
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001216WL_EXPORT void
1217gles2_renderer_destroy(struct weston_compositor *ec)
1218{
1219 if (ec->has_bind_display)
1220 ec->unbind_display(ec->egl_display, ec->wl_display);
1221}
1222
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001223static int
1224compile_shaders(struct weston_compositor *ec)
1225{
1226 if (weston_shader_init(&ec->texture_shader_rgba, ec,
1227 vertex_shader, texture_fragment_shader_rgba) < 0)
1228 return -1;
1229 if (weston_shader_init(&ec->texture_shader_rgbx, ec,
1230 vertex_shader, texture_fragment_shader_rgbx) < 0)
1231 return -1;
1232 if (ec->has_egl_image_external &&
1233 weston_shader_init(&ec->texture_shader_egl_external, ec,
1234 vertex_shader, texture_fragment_shader_egl_external) < 0)
1235 return -1;
1236 if (weston_shader_init(&ec->texture_shader_y_uv, ec,
1237 vertex_shader, texture_fragment_shader_y_uv) < 0)
1238 return -1;
1239 if (weston_shader_init(&ec->texture_shader_y_u_v, ec,
1240 vertex_shader, texture_fragment_shader_y_u_v) < 0)
1241 return -1;
1242 if (weston_shader_init(&ec->texture_shader_y_xuxv, ec,
1243 vertex_shader, texture_fragment_shader_y_xuxv) < 0)
1244 return -1;
1245 if (weston_shader_init(&ec->solid_shader, ec,
1246 vertex_shader, solid_fragment_shader) < 0)
1247 return -1;
1248
1249 return 0;
1250}
1251
1252static void
1253fragment_debug_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
1254 void *data)
1255{
1256 struct weston_compositor *ec = data;
1257 struct gles2_renderer *renderer =
1258 (struct gles2_renderer *) ec->renderer;
1259 struct weston_output *output;
1260
1261 renderer->fragment_shader_debug ^= 1;
1262
1263 weston_shader_release(&ec->texture_shader_rgba);
1264 weston_shader_release(&ec->texture_shader_rgbx);
1265 weston_shader_release(&ec->texture_shader_egl_external);
1266 weston_shader_release(&ec->texture_shader_y_uv);
1267 weston_shader_release(&ec->texture_shader_y_u_v);
1268 weston_shader_release(&ec->texture_shader_y_xuxv);
1269 weston_shader_release(&ec->solid_shader);
1270
1271 compile_shaders(ec);
1272
1273 wl_list_for_each(output, &ec->output_list, link)
1274 weston_output_damage(output);
1275}
1276
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001277WL_EXPORT int
1278gles2_renderer_init(struct weston_compositor *ec)
1279{
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001280 struct gles2_renderer *renderer;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001281 const char *extensions;
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001282 struct weston_output *output;
1283 EGLBoolean ret;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001284
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001285 static const EGLint context_attribs[] = {
1286 EGL_CONTEXT_CLIENT_VERSION, 2,
1287 EGL_NONE
1288 };
1289
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001290 renderer = calloc(1, sizeof *renderer);
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001291 if (renderer == NULL)
1292 return -1;
1293
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001294 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
1295 weston_log("failed to bind EGL_OPENGL_ES_API\n");
1296 print_egl_error_state();
1297 return -1;
1298 }
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001299
1300 log_egl_config_info(ec->egl_display, ec->egl_config);
1301
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001302 ec->egl_context = eglCreateContext(ec->egl_display, ec->egl_config,
1303 EGL_NO_CONTEXT, context_attribs);
1304 if (ec->egl_context == NULL) {
1305 weston_log("failed to create context\n");
1306 print_egl_error_state();
1307 return -1;
1308 }
1309
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001310 output = container_of(ec->output_list.next,
1311 struct weston_output, link);
1312 ret = eglMakeCurrent(ec->egl_display, output->egl_surface,
1313 output->egl_surface, ec->egl_context);
1314 if (ret == EGL_FALSE) {
1315 weston_log("Failed to make EGL context current.\n");
1316 print_egl_error_state();
1317 return -1;
1318 }
1319
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001320 log_egl_gl_info(ec->egl_display);
1321
1322 ec->image_target_texture_2d =
1323 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
1324 ec->image_target_renderbuffer_storage = (void *)
1325 eglGetProcAddress("glEGLImageTargetRenderbufferStorageOES");
1326 ec->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1327 ec->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1328 ec->bind_display =
1329 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
1330 ec->unbind_display =
1331 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
1332 ec->query_buffer =
1333 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
1334
1335 extensions = (const char *) glGetString(GL_EXTENSIONS);
1336 if (!extensions) {
1337 weston_log("Retrieving GL extension string failed.\n");
1338 return -1;
1339 }
1340
1341 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
1342 weston_log("GL_EXT_texture_format_BGRA8888 not available\n");
1343 return -1;
1344 }
1345
1346 if (strstr(extensions, "GL_EXT_read_format_bgra"))
1347 ec->read_format = GL_BGRA_EXT;
1348 else
1349 ec->read_format = GL_RGBA;
1350
1351 if (strstr(extensions, "GL_EXT_unpack_subimage"))
1352 ec->has_unpack_subimage = 1;
1353
1354 if (strstr(extensions, "GL_OES_EGL_image_external"))
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001355 ec->has_egl_image_external = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001356
1357 extensions =
1358 (const char *) eglQueryString(ec->egl_display, EGL_EXTENSIONS);
1359 if (!extensions) {
1360 weston_log("Retrieving EGL extension string failed.\n");
1361 return -1;
1362 }
1363
1364 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
1365 ec->has_bind_display = 1;
Pekka Paalanen035a0322012-10-24 09:43:06 +03001366 if (ec->has_bind_display) {
1367 ret = ec->bind_display(ec->egl_display, ec->wl_display);
1368 if (!ret)
1369 ec->has_bind_display = 0;
1370 }
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001371
1372 glActiveTexture(GL_TEXTURE0);
1373
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001374 renderer->base.repaint_output = gles2_renderer_repaint_output;
1375 renderer->base.flush_damage = gles2_renderer_flush_damage;
1376 renderer->base.attach = gles2_renderer_attach;
Kristian Høgsberg42263852012-09-06 21:59:29 -04001377 renderer->base.destroy_surface = gles2_renderer_destroy_surface;
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001378 ec->renderer = &renderer->base;
1379
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001380 if (compile_shaders(ec))
1381 return -1;
1382
1383 weston_compositor_add_debug_binding(ec, KEY_S,
1384 fragment_debug_binding, ec);
1385
Pekka Paalanen035a0322012-10-24 09:43:06 +03001386 weston_log("GL ES 2 renderer features:\n");
1387 weston_log_continue(STAMP_SPACE "read-back format: %s\n",
1388 ec->read_format == GL_BGRA_EXT ? "BGRA" : "RGBA");
1389 weston_log_continue(STAMP_SPACE "wl_shm sub-image to texture: %s\n",
1390 ec->has_unpack_subimage ? "yes" : "no");
1391 weston_log_continue(STAMP_SPACE "EGL Wayland extension: %s\n",
1392 ec->has_bind_display ? "yes" : "no");
1393
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001394 return 0;
1395}