blob: 0f1873dfbe638fb0a5f208c1ee8cbabf5343cb00 [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
John Kåre Alsaker94659272012-11-13 19:10:18 +010034struct gles2_output_state {
35 EGLSurface egl_surface;
36};
37
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +010038struct gles2_renderer {
39 struct weston_renderer base;
40 int fragment_shader_debug;
41
42 EGLDisplay egl_display;
43 EGLContext egl_context;
44 EGLConfig egl_config;
45};
John Kåre Alsaker94659272012-11-13 19:10:18 +010046
47static inline struct gles2_output_state *
48get_output_state(struct weston_output *output)
49{
50 return (struct gles2_output_state *)output->renderer_state;
51}
52
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +010053static inline struct gles2_renderer *
54get_renderer(struct weston_compositor *ec)
55{
56 return (struct gles2_renderer *)ec->renderer;
57}
58
Kristian Høgsbergd7c17262012-09-05 21:54:15 -040059static const char *
60egl_error_string(EGLint code)
61{
62#define MYERRCODE(x) case x: return #x;
63 switch (code) {
64 MYERRCODE(EGL_SUCCESS)
65 MYERRCODE(EGL_NOT_INITIALIZED)
66 MYERRCODE(EGL_BAD_ACCESS)
67 MYERRCODE(EGL_BAD_ALLOC)
68 MYERRCODE(EGL_BAD_ATTRIBUTE)
69 MYERRCODE(EGL_BAD_CONTEXT)
70 MYERRCODE(EGL_BAD_CONFIG)
71 MYERRCODE(EGL_BAD_CURRENT_SURFACE)
72 MYERRCODE(EGL_BAD_DISPLAY)
73 MYERRCODE(EGL_BAD_SURFACE)
74 MYERRCODE(EGL_BAD_MATCH)
75 MYERRCODE(EGL_BAD_PARAMETER)
76 MYERRCODE(EGL_BAD_NATIVE_PIXMAP)
77 MYERRCODE(EGL_BAD_NATIVE_WINDOW)
78 MYERRCODE(EGL_CONTEXT_LOST)
79 default:
80 return "unknown";
81 }
82#undef MYERRCODE
83}
84
85static void
86print_egl_error_state(void)
87{
88 EGLint code;
89
90 code = eglGetError();
91 weston_log("EGL error state: %s (0x%04lx)\n",
92 egl_error_string(code), (long)code);
93}
94
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +030095struct polygon8 {
96 GLfloat x[8];
97 GLfloat y[8];
98 int n;
99};
100
101struct clip_context {
102 struct {
103 GLfloat x;
104 GLfloat y;
105 } prev;
106
107 struct {
108 GLfloat x1, y1;
109 GLfloat x2, y2;
110 } clip;
111
112 struct {
113 GLfloat *x;
114 GLfloat *y;
115 } vertices;
116};
117
118static GLfloat
119float_difference(GLfloat a, GLfloat b)
120{
121 /* http://www.altdevblogaday.com/2012/02/22/comparing-floating-point-numbers-2012-edition/ */
122 static const GLfloat max_diff = 4.0f * FLT_MIN;
123 static const GLfloat max_rel_diff = 4.0e-5;
124 GLfloat diff = a - b;
125 GLfloat adiff = fabsf(diff);
126
127 if (adiff <= max_diff)
128 return 0.0f;
129
130 a = fabsf(a);
131 b = fabsf(b);
132 if (adiff <= (a > b ? a : b) * max_rel_diff)
133 return 0.0f;
134
135 return diff;
136}
137
138/* A line segment (p1x, p1y)-(p2x, p2y) intersects the line x = x_arg.
139 * Compute the y coordinate of the intersection.
140 */
141static GLfloat
142clip_intersect_y(GLfloat p1x, GLfloat p1y, GLfloat p2x, GLfloat p2y,
143 GLfloat x_arg)
144{
145 GLfloat a;
146 GLfloat diff = float_difference(p1x, p2x);
147
148 /* Practically vertical line segment, yet the end points have already
149 * been determined to be on different sides of the line. Therefore
150 * the line segment is part of the line and intersects everywhere.
151 * Return the end point, so we use the whole line segment.
152 */
153 if (diff == 0.0f)
154 return p2y;
155
156 a = (x_arg - p2x) / diff;
157 return p2y + (p1y - p2y) * a;
158}
159
160/* A line segment (p1x, p1y)-(p2x, p2y) intersects the line y = y_arg.
161 * Compute the x coordinate of the intersection.
162 */
163static GLfloat
164clip_intersect_x(GLfloat p1x, GLfloat p1y, GLfloat p2x, GLfloat p2y,
165 GLfloat y_arg)
166{
167 GLfloat a;
168 GLfloat diff = float_difference(p1y, p2y);
169
170 /* Practically horizontal line segment, yet the end points have already
171 * been determined to be on different sides of the line. Therefore
172 * the line segment is part of the line and intersects everywhere.
173 * Return the end point, so we use the whole line segment.
174 */
175 if (diff == 0.0f)
176 return p2x;
177
178 a = (y_arg - p2y) / diff;
179 return p2x + (p1x - p2x) * a;
180}
181
182enum path_transition {
183 PATH_TRANSITION_OUT_TO_OUT = 0,
184 PATH_TRANSITION_OUT_TO_IN = 1,
185 PATH_TRANSITION_IN_TO_OUT = 2,
186 PATH_TRANSITION_IN_TO_IN = 3,
187};
188
189static void
190clip_append_vertex(struct clip_context *ctx, GLfloat x, GLfloat y)
191{
192 *ctx->vertices.x++ = x;
193 *ctx->vertices.y++ = y;
194}
195
196static enum path_transition
197path_transition_left_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
198{
199 return ((ctx->prev.x >= ctx->clip.x1) << 1) | (x >= ctx->clip.x1);
200}
201
202static enum path_transition
203path_transition_right_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
204{
205 return ((ctx->prev.x < ctx->clip.x2) << 1) | (x < ctx->clip.x2);
206}
207
208static enum path_transition
209path_transition_top_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
210{
211 return ((ctx->prev.y >= ctx->clip.y1) << 1) | (y >= ctx->clip.y1);
212}
213
214static enum path_transition
215path_transition_bottom_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
216{
217 return ((ctx->prev.y < ctx->clip.y2) << 1) | (y < ctx->clip.y2);
218}
219
220static void
221clip_polygon_leftright(struct clip_context *ctx,
222 enum path_transition transition,
223 GLfloat x, GLfloat y, GLfloat clip_x)
224{
225 GLfloat yi;
226
227 switch (transition) {
228 case PATH_TRANSITION_IN_TO_IN:
229 clip_append_vertex(ctx, x, y);
230 break;
231 case PATH_TRANSITION_IN_TO_OUT:
232 yi = clip_intersect_y(ctx->prev.x, ctx->prev.y, x, y, clip_x);
233 clip_append_vertex(ctx, clip_x, yi);
234 break;
235 case PATH_TRANSITION_OUT_TO_IN:
236 yi = clip_intersect_y(ctx->prev.x, ctx->prev.y, x, y, clip_x);
237 clip_append_vertex(ctx, clip_x, yi);
238 clip_append_vertex(ctx, x, y);
239 break;
240 case PATH_TRANSITION_OUT_TO_OUT:
241 /* nothing */
242 break;
243 default:
244 assert(0 && "bad enum path_transition");
245 }
246
247 ctx->prev.x = x;
248 ctx->prev.y = y;
249}
250
251static void
252clip_polygon_topbottom(struct clip_context *ctx,
253 enum path_transition transition,
254 GLfloat x, GLfloat y, GLfloat clip_y)
255{
256 GLfloat xi;
257
258 switch (transition) {
259 case PATH_TRANSITION_IN_TO_IN:
260 clip_append_vertex(ctx, x, y);
261 break;
262 case PATH_TRANSITION_IN_TO_OUT:
263 xi = clip_intersect_x(ctx->prev.x, ctx->prev.y, x, y, clip_y);
264 clip_append_vertex(ctx, xi, clip_y);
265 break;
266 case PATH_TRANSITION_OUT_TO_IN:
267 xi = clip_intersect_x(ctx->prev.x, ctx->prev.y, x, y, clip_y);
268 clip_append_vertex(ctx, xi, clip_y);
269 clip_append_vertex(ctx, x, y);
270 break;
271 case PATH_TRANSITION_OUT_TO_OUT:
272 /* nothing */
273 break;
274 default:
275 assert(0 && "bad enum path_transition");
276 }
277
278 ctx->prev.x = x;
279 ctx->prev.y = y;
280}
281
282static void
283clip_context_prepare(struct clip_context *ctx, const struct polygon8 *src,
284 GLfloat *dst_x, GLfloat *dst_y)
285{
286 ctx->prev.x = src->x[src->n - 1];
287 ctx->prev.y = src->y[src->n - 1];
288 ctx->vertices.x = dst_x;
289 ctx->vertices.y = dst_y;
290}
291
292static int
293clip_polygon_left(struct clip_context *ctx, const struct polygon8 *src,
294 GLfloat *dst_x, GLfloat *dst_y)
295{
296 enum path_transition trans;
297 int i;
298
299 clip_context_prepare(ctx, src, dst_x, dst_y);
300 for (i = 0; i < src->n; i++) {
301 trans = path_transition_left_edge(ctx, src->x[i], src->y[i]);
302 clip_polygon_leftright(ctx, trans, src->x[i], src->y[i],
303 ctx->clip.x1);
304 }
305 return ctx->vertices.x - dst_x;
306}
307
308static int
309clip_polygon_right(struct clip_context *ctx, const struct polygon8 *src,
310 GLfloat *dst_x, GLfloat *dst_y)
311{
312 enum path_transition trans;
313 int i;
314
315 clip_context_prepare(ctx, src, dst_x, dst_y);
316 for (i = 0; i < src->n; i++) {
317 trans = path_transition_right_edge(ctx, src->x[i], src->y[i]);
318 clip_polygon_leftright(ctx, trans, src->x[i], src->y[i],
319 ctx->clip.x2);
320 }
321 return ctx->vertices.x - dst_x;
322}
323
324static int
325clip_polygon_top(struct clip_context *ctx, const struct polygon8 *src,
326 GLfloat *dst_x, GLfloat *dst_y)
327{
328 enum path_transition trans;
329 int i;
330
331 clip_context_prepare(ctx, src, dst_x, dst_y);
332 for (i = 0; i < src->n; i++) {
333 trans = path_transition_top_edge(ctx, src->x[i], src->y[i]);
334 clip_polygon_topbottom(ctx, trans, src->x[i], src->y[i],
335 ctx->clip.y1);
336 }
337 return ctx->vertices.x - dst_x;
338}
339
340static int
341clip_polygon_bottom(struct clip_context *ctx, const struct polygon8 *src,
342 GLfloat *dst_x, GLfloat *dst_y)
343{
344 enum path_transition trans;
345 int i;
346
347 clip_context_prepare(ctx, src, dst_x, dst_y);
348 for (i = 0; i < src->n; i++) {
349 trans = path_transition_bottom_edge(ctx, src->x[i], src->y[i]);
350 clip_polygon_topbottom(ctx, trans, src->x[i], src->y[i],
351 ctx->clip.y2);
352 }
353 return ctx->vertices.x - dst_x;
354}
355
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400356#define max(a, b) (((a) > (b)) ? (a) : (b))
357#define min(a, b) (((a) > (b)) ? (b) : (a))
358#define clip(x, a, b) min(max(x, a), b)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400359
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300360/*
361 * Compute the boundary vertices of the intersection of the global coordinate
362 * aligned rectangle 'rect', and an arbitrary quadrilateral produced from
363 * 'surf_rect' when transformed from surface coordinates into global coordinates.
364 * The vertices are written to 'ex' and 'ey', and the return value is the
365 * number of vertices. Vertices are produced in clockwise winding order.
366 * Guarantees to produce either zero vertices, or 3-8 vertices with non-zero
367 * polygon area.
368 */
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400369static int
370calculate_edges(struct weston_surface *es, pixman_box32_t *rect,
371 pixman_box32_t *surf_rect, GLfloat *ex, GLfloat *ey)
372{
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300373 struct polygon8 polygon;
374 struct clip_context ctx;
375 int i, n;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400376 GLfloat min_x, max_x, min_y, max_y;
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300377 struct polygon8 surf = {
378 { surf_rect->x1, surf_rect->x2, surf_rect->x2, surf_rect->x1 },
379 { surf_rect->y1, surf_rect->y1, surf_rect->y2, surf_rect->y2 },
380 4
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400381 };
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400382
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300383 ctx.clip.x1 = rect->x1;
384 ctx.clip.y1 = rect->y1;
385 ctx.clip.x2 = rect->x2;
386 ctx.clip.y2 = rect->y2;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400387
388 /* transform surface to screen space: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300389 for (i = 0; i < surf.n; i++)
390 weston_surface_to_global_float(es, surf.x[i], surf.y[i],
391 &surf.x[i], &surf.y[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400392
393 /* find bounding box: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300394 min_x = max_x = surf.x[0];
395 min_y = max_y = surf.y[0];
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400396
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300397 for (i = 1; i < surf.n; i++) {
398 min_x = min(min_x, surf.x[i]);
399 max_x = max(max_x, surf.x[i]);
400 min_y = min(min_y, surf.y[i]);
401 max_y = max(max_y, surf.y[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400402 }
403
404 /* First, simple bounding box check to discard early transformed
405 * surface rects that do not intersect with the clip region:
406 */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300407 if ((min_x >= ctx.clip.x2) || (max_x <= ctx.clip.x1) ||
408 (min_y >= ctx.clip.y2) || (max_y <= ctx.clip.y1))
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400409 return 0;
410
411 /* Simple case, bounding box edges are parallel to surface edges,
412 * there will be only four edges. We just need to clip the surface
413 * vertices to the clip rect bounds:
414 */
415 if (!es->transform.enabled) {
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300416 for (i = 0; i < surf.n; i++) {
417 ex[i] = clip(surf.x[i], ctx.clip.x1, ctx.clip.x2);
418 ey[i] = clip(surf.y[i], ctx.clip.y1, ctx.clip.y2);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400419 }
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300420 return surf.n;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400421 }
422
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300423 /* Transformed case: use a general polygon clipping algorithm to
424 * clip the surface rectangle with each side of 'rect'.
425 * The algorithm is Sutherland-Hodgman, as explained in
426 * http://www.codeguru.com/cpp/misc/misc/graphics/article.php/c8965/Polygon-Clipping.htm
427 * but without looking at any of that code.
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400428 */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300429 polygon.n = clip_polygon_left(&ctx, &surf, polygon.x, polygon.y);
430 surf.n = clip_polygon_right(&ctx, &polygon, surf.x, surf.y);
431 polygon.n = clip_polygon_top(&ctx, &surf, polygon.x, polygon.y);
432 surf.n = clip_polygon_bottom(&ctx, &polygon, surf.x, surf.y);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400433
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300434 /* Get rid of duplicate vertices */
435 ex[0] = surf.x[0];
436 ey[0] = surf.y[0];
437 n = 1;
438 for (i = 1; i < surf.n; i++) {
439 if (float_difference(ex[n - 1], surf.x[i]) == 0.0f &&
440 float_difference(ey[n - 1], surf.y[i]) == 0.0f)
441 continue;
442 ex[n] = surf.x[i];
443 ey[n] = surf.y[i];
444 n++;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400445 }
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300446 if (float_difference(ex[n - 1], surf.x[0]) == 0.0f &&
447 float_difference(ey[n - 1], surf.y[0]) == 0.0f)
448 n--;
449
450 if (n < 3)
451 return 0;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400452
453 return n;
454}
455
456static int
457texture_region(struct weston_surface *es, pixman_region32_t *region,
458 pixman_region32_t *surf_region)
459{
460 struct weston_compositor *ec = es->compositor;
461 GLfloat *v, inv_width, inv_height;
462 unsigned int *vtxcnt, nvtx = 0;
463 pixman_box32_t *rects, *surf_rects;
464 int i, j, k, nrects, nsurf;
465
466 rects = pixman_region32_rectangles(region, &nrects);
467 surf_rects = pixman_region32_rectangles(surf_region, &nsurf);
468
469 /* worst case we can have 8 vertices per rect (ie. clipped into
470 * an octagon):
471 */
472 v = wl_array_add(&ec->vertices, nrects * nsurf * 8 * 4 * sizeof *v);
473 vtxcnt = wl_array_add(&ec->vtxcnt, nrects * nsurf * sizeof *vtxcnt);
474
475 inv_width = 1.0 / es->pitch;
476 inv_height = 1.0 / es->geometry.height;
477
478 for (i = 0; i < nrects; i++) {
479 pixman_box32_t *rect = &rects[i];
480 for (j = 0; j < nsurf; j++) {
481 pixman_box32_t *surf_rect = &surf_rects[j];
482 GLfloat sx, sy;
483 GLfloat ex[8], ey[8]; /* edge points in screen space */
484 int n;
485
486 /* The transformed surface, after clipping to the clip region,
487 * can have as many as eight sides, emitted as a triangle-fan.
488 * The first vertex in the triangle fan can be chosen arbitrarily,
489 * since the area is guaranteed to be convex.
490 *
491 * If a corner of the transformed surface falls outside of the
492 * clip region, instead of emitting one vertex for the corner
493 * of the surface, up to two are emitted for two corresponding
494 * intersection point(s) between the surface and the clip region.
495 *
496 * To do this, we first calculate the (up to eight) points that
497 * form the intersection of the clip rect and the transformed
498 * surface.
499 */
500 n = calculate_edges(es, rect, surf_rect, ex, ey);
501 if (n < 3)
502 continue;
503
504 /* emit edge points: */
505 for (k = 0; k < n; k++) {
506 weston_surface_from_global_float(es, ex[k], ey[k], &sx, &sy);
507 /* position: */
508 *(v++) = ex[k];
509 *(v++) = ey[k];
510 /* texcoord: */
511 *(v++) = sx * inv_width;
512 *(v++) = sy * inv_height;
513 }
514
515 vtxcnt[nvtx++] = n;
516 }
517 }
518
519 return nvtx;
520}
521
522static void
523triangle_fan_debug(struct weston_surface *surface, int first, int count)
524{
525 struct weston_compositor *compositor = surface->compositor;
526 int i;
527 GLushort *buffer;
528 GLushort *index;
529 int nelems;
530 static int color_idx = 0;
531 static const GLfloat color[][4] = {
532 { 1.0, 0.0, 0.0, 1.0 },
533 { 0.0, 1.0, 0.0, 1.0 },
534 { 0.0, 0.0, 1.0, 1.0 },
535 { 1.0, 1.0, 1.0, 1.0 },
536 };
537
538 nelems = (count - 1 + count - 2) * 2;
539
540 buffer = malloc(sizeof(GLushort) * nelems);
541 index = buffer;
542
543 for (i = 1; i < count; i++) {
544 *index++ = first;
545 *index++ = first + i;
546 }
547
548 for (i = 2; i < count; i++) {
549 *index++ = first + i - 1;
550 *index++ = first + i;
551 }
552
553 glUseProgram(compositor->solid_shader.program);
554 glUniform4fv(compositor->solid_shader.color_uniform, 1,
555 color[color_idx++ % ARRAY_LENGTH(color)]);
556 glDrawElements(GL_LINES, nelems, GL_UNSIGNED_SHORT, buffer);
557 glUseProgram(compositor->current_shader->program);
558 free(buffer);
559}
560
561static void
562repaint_region(struct weston_surface *es, pixman_region32_t *region,
563 pixman_region32_t *surf_region)
564{
565 struct weston_compositor *ec = es->compositor;
566 GLfloat *v;
567 unsigned int *vtxcnt;
568 int i, first, nfans;
569
570 /* The final region to be painted is the intersection of
571 * 'region' and 'surf_region'. However, 'region' is in the global
572 * coordinates, and 'surf_region' is in the surface-local
573 * coordinates. texture_region() will iterate over all pairs of
574 * rectangles from both regions, compute the intersection
575 * polygon for each pair, and store it as a triangle fan if
576 * it has a non-zero area (at least 3 vertices, actually).
577 */
578 nfans = texture_region(es, region, surf_region);
579
580 v = ec->vertices.data;
581 vtxcnt = ec->vtxcnt.data;
582
583 /* position: */
584 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
585 glEnableVertexAttribArray(0);
586
587 /* texcoord: */
588 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
589 glEnableVertexAttribArray(1);
590
591 for (i = 0, first = 0; i < nfans; i++) {
592 glDrawArrays(GL_TRIANGLE_FAN, first, vtxcnt[i]);
593 if (ec->fan_debug)
594 triangle_fan_debug(es, first, vtxcnt[i]);
595 first += vtxcnt[i];
596 }
597
598 glDisableVertexAttribArray(1);
599 glDisableVertexAttribArray(0);
600
601 ec->vertices.size = 0;
602 ec->vtxcnt.size = 0;
603}
604
605static void
606weston_compositor_use_shader(struct weston_compositor *compositor,
607 struct weston_shader *shader)
608{
609 if (compositor->current_shader == shader)
610 return;
611
612 glUseProgram(shader->program);
613 compositor->current_shader = shader;
614}
615
616static void
617weston_shader_uniforms(struct weston_shader *shader,
618 struct weston_surface *surface,
619 struct weston_output *output)
620{
621 int i;
622
623 glUniformMatrix4fv(shader->proj_uniform,
624 1, GL_FALSE, output->matrix.d);
625 glUniform4fv(shader->color_uniform, 1, surface->color);
626 glUniform1f(shader->alpha_uniform, surface->alpha);
627
628 for (i = 0; i < surface->num_textures; i++)
629 glUniform1i(shader->tex_uniforms[i], i);
630}
631
632static void
633draw_surface(struct weston_surface *es, struct weston_output *output,
634 pixman_region32_t *damage) /* in global coordinates */
635{
636 struct weston_compositor *ec = es->compositor;
637 /* repaint bounding region in global coordinates: */
638 pixman_region32_t repaint;
639 /* non-opaque region in surface coordinates: */
640 pixman_region32_t surface_blend;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300641 pixman_region32_t *buffer_damage;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400642 GLint filter;
643 int i;
644
645 pixman_region32_init(&repaint);
646 pixman_region32_intersect(&repaint,
647 &es->transform.boundingbox, damage);
648 pixman_region32_subtract(&repaint, &repaint, &es->clip);
649
650 if (!pixman_region32_not_empty(&repaint))
651 goto out;
652
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300653 buffer_damage = &output->buffer_damage[output->current_buffer];
654 pixman_region32_subtract(buffer_damage, buffer_damage, &repaint);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400655
656 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
657
658 if (ec->fan_debug) {
659 weston_compositor_use_shader(ec, &ec->solid_shader);
660 weston_shader_uniforms(&ec->solid_shader, es, output);
661 }
662
663 weston_compositor_use_shader(ec, es->shader);
664 weston_shader_uniforms(es->shader, es, output);
665
666 if (es->transform.enabled || output->zoom.active)
667 filter = GL_LINEAR;
668 else
669 filter = GL_NEAREST;
670
671 for (i = 0; i < es->num_textures; i++) {
672 glActiveTexture(GL_TEXTURE0 + i);
673 glBindTexture(es->target, es->textures[i]);
674 glTexParameteri(es->target, GL_TEXTURE_MIN_FILTER, filter);
675 glTexParameteri(es->target, GL_TEXTURE_MAG_FILTER, filter);
676 }
677
678 /* blended region is whole surface minus opaque region: */
679 pixman_region32_init_rect(&surface_blend, 0, 0,
680 es->geometry.width, es->geometry.height);
681 pixman_region32_subtract(&surface_blend, &surface_blend, &es->opaque);
682
683 if (pixman_region32_not_empty(&es->opaque)) {
684 if (es->shader == &ec->texture_shader_rgba) {
685 /* Special case for RGBA textures with possibly
686 * bad data in alpha channel: use the shader
687 * that forces texture alpha = 1.0.
688 * Xwayland surfaces need this.
689 */
690 weston_compositor_use_shader(ec, &ec->texture_shader_rgbx);
691 weston_shader_uniforms(&ec->texture_shader_rgbx, es, output);
692 }
693
694 if (es->alpha < 1.0)
695 glEnable(GL_BLEND);
696 else
697 glDisable(GL_BLEND);
698
699 repaint_region(es, &repaint, &es->opaque);
700 }
701
702 if (pixman_region32_not_empty(&surface_blend)) {
703 weston_compositor_use_shader(ec, es->shader);
704 glEnable(GL_BLEND);
705 repaint_region(es, &repaint, &surface_blend);
706 }
707
708 pixman_region32_fini(&surface_blend);
709
710out:
711 pixman_region32_fini(&repaint);
712}
713
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400714static void
715repaint_surfaces(struct weston_output *output, pixman_region32_t *damage)
716{
717 struct weston_compositor *compositor = output->compositor;
718 struct weston_surface *surface;
719
720 wl_list_for_each_reverse(surface, &compositor->surface_list, link)
721 if (surface->plane == &compositor->primary_plane)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400722 draw_surface(surface, output, damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400723}
724
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400725static void
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400726gles2_renderer_repaint_output(struct weston_output *output,
727 pixman_region32_t *output_damage)
728{
John Kåre Alsaker94659272012-11-13 19:10:18 +0100729 struct gles2_output_state *go = get_output_state(output);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400730 struct weston_compositor *compositor = output->compositor;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100731 struct gles2_renderer *gr = get_renderer(compositor);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400732 EGLBoolean ret;
733 static int errored;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300734 int32_t width, height, i;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400735
736 width = output->current->width +
737 output->border.left + output->border.right;
738 height = output->current->height +
739 output->border.top + output->border.bottom;
740
741 glViewport(0, 0, width, height);
742
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100743 ret = eglMakeCurrent(gr->egl_display, go->egl_surface,
744 go->egl_surface, gr->egl_context);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400745 if (ret == EGL_FALSE) {
746 if (errored)
747 return;
748 errored = 1;
749 weston_log("Failed to make EGL context current.\n");
750 print_egl_error_state();
751 return;
752 }
753
754 /* if debugging, redraw everything outside the damage to clean up
755 * debug lines from the previous draw on this buffer:
756 */
757 if (compositor->fan_debug) {
758 pixman_region32_t undamaged;
759 pixman_region32_init(&undamaged);
760 pixman_region32_subtract(&undamaged, &output->region,
761 output_damage);
762 compositor->fan_debug = 0;
763 repaint_surfaces(output, &undamaged);
764 compositor->fan_debug = 1;
765 pixman_region32_fini(&undamaged);
766 }
767
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300768 for (i = 0; i < 2; i++)
769 pixman_region32_union(&output->buffer_damage[i],
770 &output->buffer_damage[i],
771 output_damage);
772
773 pixman_region32_union(output_damage, output_damage,
774 &output->buffer_damage[output->current_buffer]);
775
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400776 repaint_surfaces(output, output_damage);
777
778 wl_signal_emit(&output->frame_signal, output);
779
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100780 ret = eglSwapBuffers(gr->egl_display, go->egl_surface);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400781 if (ret == EGL_FALSE && !errored) {
782 errored = 1;
783 weston_log("Failed in eglSwapBuffers.\n");
784 print_egl_error_state();
785 }
786
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300787 output->current_buffer ^= 1;
788
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400789}
Kristian Høgsberg25894fc2012-09-05 22:06:26 -0400790
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400791static void
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -0400792gles2_renderer_flush_damage(struct weston_surface *surface)
793{
794#ifdef GL_UNPACK_ROW_LENGTH
795 pixman_box32_t *rectangles;
796 void *data;
797 int i, n;
798#endif
799
Pekka Paalanenbcdd5792012-11-07 12:25:13 +0200800 pixman_region32_union(&surface->texture_damage,
801 &surface->texture_damage, &surface->damage);
802
803 /* Avoid upload, if the texture won't be used this time.
804 * We still accumulate the damage in texture_damage.
805 */
806 if (surface->plane != &surface->compositor->primary_plane)
807 return;
808
809 if (!pixman_region32_not_empty(&surface->texture_damage))
810 return;
811
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -0400812 glBindTexture(GL_TEXTURE_2D, surface->textures[0]);
813
814 if (!surface->compositor->has_unpack_subimage) {
815 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
816 surface->pitch, surface->buffer->height, 0,
817 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
818 wl_shm_buffer_get_data(surface->buffer));
819
Pekka Paalanenbcdd5792012-11-07 12:25:13 +0200820 goto done;
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -0400821 }
822
823#ifdef GL_UNPACK_ROW_LENGTH
824 /* Mesa does not define GL_EXT_unpack_subimage */
825 glPixelStorei(GL_UNPACK_ROW_LENGTH, surface->pitch);
826 data = wl_shm_buffer_get_data(surface->buffer);
Pekka Paalanenbcdd5792012-11-07 12:25:13 +0200827 rectangles = pixman_region32_rectangles(&surface->texture_damage, &n);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -0400828 for (i = 0; i < n; i++) {
829 glPixelStorei(GL_UNPACK_SKIP_PIXELS, rectangles[i].x1);
830 glPixelStorei(GL_UNPACK_SKIP_ROWS, rectangles[i].y1);
831 glTexSubImage2D(GL_TEXTURE_2D, 0,
832 rectangles[i].x1, rectangles[i].y1,
833 rectangles[i].x2 - rectangles[i].x1,
834 rectangles[i].y2 - rectangles[i].y1,
835 GL_BGRA_EXT, GL_UNSIGNED_BYTE, data);
836 }
837#endif
Pekka Paalanenbcdd5792012-11-07 12:25:13 +0200838
839done:
840 pixman_region32_fini(&surface->texture_damage);
841 pixman_region32_init(&surface->texture_damage);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -0400842}
843
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400844static void
845ensure_textures(struct weston_surface *es, int num_textures)
846{
847 int i;
848
849 if (num_textures <= es->num_textures)
850 return;
851
852 for (i = es->num_textures; i < num_textures; i++) {
853 glGenTextures(1, &es->textures[i]);
854 glBindTexture(es->target, es->textures[i]);
855 glTexParameteri(es->target,
856 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
857 glTexParameteri(es->target,
858 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
859 }
860 es->num_textures = num_textures;
861 glBindTexture(es->target, 0);
862}
863
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400864static void
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400865gles2_renderer_attach(struct weston_surface *es, struct wl_buffer *buffer)
866{
867 struct weston_compositor *ec = es->compositor;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100868 struct gles2_renderer *gr = get_renderer(ec);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400869 EGLint attribs[3], format;
870 int i, num_planes;
871
872 if (!buffer) {
873 for (i = 0; i < es->num_images; i++) {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100874 ec->destroy_image(gr->egl_display, es->images[i]);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400875 es->images[i] = NULL;
876 }
877 es->num_images = 0;
878 glDeleteTextures(es->num_textures, es->textures);
879 es->num_textures = 0;
880 return;
881 }
882
883 if (wl_buffer_is_shm(buffer)) {
884 es->pitch = wl_shm_buffer_get_stride(buffer) / 4;
885 es->target = GL_TEXTURE_2D;
886
887 ensure_textures(es, 1);
888 glBindTexture(GL_TEXTURE_2D, es->textures[0]);
889 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
890 es->pitch, buffer->height, 0,
891 GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
892 if (wl_shm_buffer_get_format(buffer) == WL_SHM_FORMAT_XRGB8888)
893 es->shader = &ec->texture_shader_rgbx;
894 else
895 es->shader = &ec->texture_shader_rgba;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100896 } else if (ec->query_buffer(gr->egl_display, buffer,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400897 EGL_TEXTURE_FORMAT, &format)) {
898 for (i = 0; i < es->num_images; i++)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100899 ec->destroy_image(gr->egl_display, es->images[i]);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400900 es->num_images = 0;
901 es->target = GL_TEXTURE_2D;
902 switch (format) {
903 case EGL_TEXTURE_RGB:
904 case EGL_TEXTURE_RGBA:
905 default:
906 num_planes = 1;
907 es->shader = &ec->texture_shader_rgba;
908 break;
909 case EGL_TEXTURE_EXTERNAL_WL:
910 num_planes = 1;
911 es->target = GL_TEXTURE_EXTERNAL_OES;
912 es->shader = &ec->texture_shader_egl_external;
913 break;
914 case EGL_TEXTURE_Y_UV_WL:
915 num_planes = 2;
916 es->shader = &ec->texture_shader_y_uv;
917 break;
918 case EGL_TEXTURE_Y_U_V_WL:
919 num_planes = 3;
920 es->shader = &ec->texture_shader_y_u_v;
921 break;
922 case EGL_TEXTURE_Y_XUXV_WL:
923 num_planes = 2;
924 es->shader = &ec->texture_shader_y_xuxv;
925 break;
926 }
927
928 ensure_textures(es, num_planes);
929 for (i = 0; i < num_planes; i++) {
930 attribs[0] = EGL_WAYLAND_PLANE_WL;
931 attribs[1] = i;
932 attribs[2] = EGL_NONE;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100933 es->images[i] = ec->create_image(gr->egl_display,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400934 NULL,
935 EGL_WAYLAND_BUFFER_WL,
936 buffer, attribs);
937 if (!es->images[i]) {
938 weston_log("failed to create img for plane %d\n", i);
939 continue;
940 }
941 es->num_images++;
942
943 glActiveTexture(GL_TEXTURE0 + i);
944 glBindTexture(es->target, es->textures[i]);
945 ec->image_target_texture_2d(es->target,
946 es->images[i]);
947 }
948
949 es->pitch = buffer->width;
950 } else {
951 weston_log("unhandled buffer type!\n");
952 }
953}
954
Kristian Høgsberg42263852012-09-06 21:59:29 -0400955static void
956gles2_renderer_destroy_surface(struct weston_surface *surface)
957{
958 struct weston_compositor *ec = surface->compositor;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100959 struct gles2_renderer *gr = get_renderer(ec);
Kristian Høgsberg42263852012-09-06 21:59:29 -0400960 int i;
961
962 glDeleteTextures(surface->num_textures, surface->textures);
963
964 for (i = 0; i < surface->num_images; i++)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100965 ec->destroy_image(gr->egl_display, surface->images[i]);
Kristian Høgsberg42263852012-09-06 21:59:29 -0400966}
967
Kristian Høgsberg25894fc2012-09-05 22:06:26 -0400968static const char vertex_shader[] =
969 "uniform mat4 proj;\n"
970 "attribute vec2 position;\n"
971 "attribute vec2 texcoord;\n"
972 "varying vec2 v_texcoord;\n"
973 "void main()\n"
974 "{\n"
975 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
976 " v_texcoord = texcoord;\n"
977 "}\n";
978
979/* Declare common fragment shader uniforms */
980#define FRAGMENT_CONVERT_YUV \
981 " y *= alpha;\n" \
982 " u *= alpha;\n" \
983 " v *= alpha;\n" \
984 " gl_FragColor.r = y + 1.59602678 * v;\n" \
985 " gl_FragColor.g = y - 0.39176229 * u - 0.81296764 * v;\n" \
986 " gl_FragColor.b = y + 2.01723214 * u;\n" \
987 " gl_FragColor.a = alpha;\n"
988
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +0200989static const char fragment_debug[] =
990 " gl_FragColor = vec4(0.0, 0.3, 0.0, 0.2) + gl_FragColor * 0.8;\n";
991
992static const char fragment_brace[] =
993 "}\n";
994
Kristian Høgsberg25894fc2012-09-05 22:06:26 -0400995static const char texture_fragment_shader_rgba[] =
996 "precision mediump float;\n"
997 "varying vec2 v_texcoord;\n"
998 "uniform sampler2D tex;\n"
999 "uniform float alpha;\n"
1000 "void main()\n"
1001 "{\n"
1002 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001003 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001004
1005static const char texture_fragment_shader_rgbx[] =
1006 "precision mediump float;\n"
1007 "varying vec2 v_texcoord;\n"
1008 "uniform sampler2D tex;\n"
1009 "uniform float alpha;\n"
1010 "void main()\n"
1011 "{\n"
1012 " gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb\n;"
1013 " gl_FragColor.a = alpha;\n"
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_egl_external[] =
1017 "#extension GL_OES_EGL_image_external : require\n"
1018 "precision mediump float;\n"
1019 "varying vec2 v_texcoord;\n"
1020 "uniform samplerExternalOES tex;\n"
1021 "uniform float alpha;\n"
1022 "void main()\n"
1023 "{\n"
1024 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001025 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001026
1027static const char texture_fragment_shader_y_uv[] =
1028 "precision mediump float;\n"
1029 "uniform sampler2D tex;\n"
1030 "uniform sampler2D tex1;\n"
1031 "varying vec2 v_texcoord;\n"
1032 "uniform float alpha;\n"
1033 "void main() {\n"
1034 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1035 " float u = texture2D(tex1, v_texcoord).r - 0.5;\n"
1036 " float v = texture2D(tex1, v_texcoord).g - 0.5;\n"
1037 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001038 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001039
1040static const char texture_fragment_shader_y_u_v[] =
1041 "precision mediump float;\n"
1042 "uniform sampler2D tex;\n"
1043 "uniform sampler2D tex1;\n"
1044 "uniform sampler2D tex2;\n"
1045 "varying vec2 v_texcoord;\n"
1046 "uniform float alpha;\n"
1047 "void main() {\n"
1048 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1049 " float u = texture2D(tex1, v_texcoord).x - 0.5;\n"
1050 " float v = texture2D(tex2, v_texcoord).x - 0.5;\n"
1051 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001052 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001053
1054static const char texture_fragment_shader_y_xuxv[] =
1055 "precision mediump float;\n"
1056 "uniform sampler2D tex;\n"
1057 "uniform sampler2D tex1;\n"
1058 "varying vec2 v_texcoord;\n"
1059 "uniform float alpha;\n"
1060 "void main() {\n"
1061 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1062 " float u = texture2D(tex1, v_texcoord).g - 0.5;\n"
1063 " float v = texture2D(tex1, v_texcoord).a - 0.5;\n"
1064 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001065 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001066
1067static const char solid_fragment_shader[] =
1068 "precision mediump float;\n"
1069 "uniform vec4 color;\n"
1070 "uniform float alpha;\n"
1071 "void main()\n"
1072 "{\n"
1073 " gl_FragColor = alpha * color\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001074 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001075
1076static int
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001077compile_shader(GLenum type, int count, const char **sources)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001078{
1079 GLuint s;
1080 char msg[512];
1081 GLint status;
1082
1083 s = glCreateShader(type);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001084 glShaderSource(s, count, sources, NULL);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001085 glCompileShader(s);
1086 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
1087 if (!status) {
1088 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1089 weston_log("shader info: %s\n", msg);
1090 return GL_NONE;
1091 }
1092
1093 return s;
1094}
1095
1096static int
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001097weston_shader_init(struct weston_shader *shader, struct weston_compositor *ec,
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001098 const char *vertex_source, const char *fragment_source)
1099{
1100 char msg[512];
1101 GLint status;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001102 int count;
1103 const char *sources[3];
1104 struct gles2_renderer *renderer =
1105 (struct gles2_renderer *) ec->renderer;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001106
1107 shader->vertex_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001108 compile_shader(GL_VERTEX_SHADER, 1, &vertex_source);
1109
1110 if (renderer->fragment_shader_debug) {
1111 sources[0] = fragment_source;
1112 sources[1] = fragment_debug;
1113 sources[2] = fragment_brace;
1114 count = 3;
1115 } else {
1116 sources[0] = fragment_source;
1117 sources[1] = fragment_brace;
1118 count = 2;
1119 }
1120
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001121 shader->fragment_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001122 compile_shader(GL_FRAGMENT_SHADER, count, sources);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001123
1124 shader->program = glCreateProgram();
1125 glAttachShader(shader->program, shader->vertex_shader);
1126 glAttachShader(shader->program, shader->fragment_shader);
1127 glBindAttribLocation(shader->program, 0, "position");
1128 glBindAttribLocation(shader->program, 1, "texcoord");
1129
1130 glLinkProgram(shader->program);
1131 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1132 if (!status) {
1133 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1134 weston_log("link info: %s\n", msg);
1135 return -1;
1136 }
1137
1138 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1139 shader->tex_uniforms[0] = glGetUniformLocation(shader->program, "tex");
1140 shader->tex_uniforms[1] = glGetUniformLocation(shader->program, "tex1");
1141 shader->tex_uniforms[2] = glGetUniformLocation(shader->program, "tex2");
1142 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
1143 shader->color_uniform = glGetUniformLocation(shader->program, "color");
1144
1145 return 0;
1146}
1147
1148static void
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001149weston_shader_release(struct weston_shader *shader)
1150{
1151 glDeleteShader(shader->vertex_shader);
1152 glDeleteShader(shader->fragment_shader);
1153 glDeleteProgram(shader->program);
1154
1155 shader->vertex_shader = 0;
1156 shader->fragment_shader = 0;
1157 shader->program = 0;
1158}
1159
1160static void
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001161log_extensions(const char *name, const char *extensions)
1162{
1163 const char *p, *end;
1164 int l;
1165 int len;
1166
1167 l = weston_log("%s:", name);
1168 p = extensions;
1169 while (*p) {
1170 end = strchrnul(p, ' ');
1171 len = end - p;
1172 if (l + len > 78)
1173 l = weston_log_continue("\n" STAMP_SPACE "%.*s",
1174 len, p);
1175 else
1176 l += weston_log_continue(" %.*s", len, p);
1177 for (p = end; isspace(*p); p++)
1178 ;
1179 }
1180 weston_log_continue("\n");
1181}
1182
1183static void
1184log_egl_gl_info(EGLDisplay egldpy)
1185{
1186 const char *str;
1187
1188 str = eglQueryString(egldpy, EGL_VERSION);
1189 weston_log("EGL version: %s\n", str ? str : "(null)");
1190
1191 str = eglQueryString(egldpy, EGL_VENDOR);
1192 weston_log("EGL vendor: %s\n", str ? str : "(null)");
1193
1194 str = eglQueryString(egldpy, EGL_CLIENT_APIS);
1195 weston_log("EGL client APIs: %s\n", str ? str : "(null)");
1196
1197 str = eglQueryString(egldpy, EGL_EXTENSIONS);
1198 log_extensions("EGL extensions", str ? str : "(null)");
1199
1200 str = (char *)glGetString(GL_VERSION);
1201 weston_log("GL version: %s\n", str ? str : "(null)");
1202
1203 str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
1204 weston_log("GLSL version: %s\n", str ? str : "(null)");
1205
1206 str = (char *)glGetString(GL_VENDOR);
1207 weston_log("GL vendor: %s\n", str ? str : "(null)");
1208
1209 str = (char *)glGetString(GL_RENDERER);
1210 weston_log("GL renderer: %s\n", str ? str : "(null)");
1211
1212 str = (char *)glGetString(GL_EXTENSIONS);
1213 log_extensions("GL extensions", str ? str : "(null)");
1214}
1215
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001216static void
1217log_egl_config_info(EGLDisplay egldpy, EGLConfig eglconfig)
1218{
1219 EGLint r, g, b, a;
1220
1221 weston_log("Chosen EGL config details:\n");
1222
1223 weston_log_continue(STAMP_SPACE "RGBA bits");
1224 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_RED_SIZE, &r) &&
1225 eglGetConfigAttrib(egldpy, eglconfig, EGL_GREEN_SIZE, &g) &&
1226 eglGetConfigAttrib(egldpy, eglconfig, EGL_BLUE_SIZE, &b) &&
1227 eglGetConfigAttrib(egldpy, eglconfig, EGL_ALPHA_SIZE, &a))
1228 weston_log_continue(": %d %d %d %d\n", r, g, b, a);
1229 else
1230 weston_log_continue(" unknown\n");
1231
1232 weston_log_continue(STAMP_SPACE "swap interval range");
1233 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_MIN_SWAP_INTERVAL, &a) &&
1234 eglGetConfigAttrib(egldpy, eglconfig, EGL_MAX_SWAP_INTERVAL, &b))
1235 weston_log_continue(": %d - %d\n", a, b);
1236 else
1237 weston_log_continue(" unknown\n");
1238}
1239
John Kåre Alsaker94659272012-11-13 19:10:18 +01001240static int
1241gles2_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface);
1242
1243WL_EXPORT int
1244gles2_renderer_output_create(struct weston_output *output,
1245 EGLNativeWindowType window)
1246{
1247 struct weston_compositor *ec = output->compositor;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001248 struct gles2_renderer *gr = get_renderer(ec);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001249 struct gles2_output_state *go = calloc(1, sizeof *go);
1250
1251 if (!go)
1252 return -1;
1253
1254 go->egl_surface =
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001255 eglCreateWindowSurface(gr->egl_display,
1256 gr->egl_config,
John Kåre Alsaker94659272012-11-13 19:10:18 +01001257 window, NULL);
1258
1259 if (go->egl_surface == EGL_NO_SURFACE) {
1260 weston_log("failed to create egl surface\n");
1261 free(go);
1262 return -1;
1263 }
1264
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001265 if (gr->egl_context == NULL)
John Kåre Alsaker94659272012-11-13 19:10:18 +01001266 if (gles2_renderer_setup(ec, go->egl_surface) < 0) {
1267 free(go);
1268 return -1;
1269 }
1270
1271 output->renderer_state = go;
1272
1273 return 0;
1274}
1275
1276WL_EXPORT void
1277gles2_renderer_output_destroy(struct weston_output *output)
1278{
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001279 struct gles2_renderer *gr = get_renderer(output->compositor);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001280 struct gles2_output_state *go = get_output_state(output);
1281
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001282 eglDestroySurface(gr->egl_display, go->egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001283
1284 free(go);
1285}
1286
1287WL_EXPORT EGLSurface
1288gles2_renderer_output_surface(struct weston_output *output)
1289{
1290 return get_output_state(output)->egl_surface;
1291}
1292
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001293WL_EXPORT void
1294gles2_renderer_destroy(struct weston_compositor *ec)
1295{
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001296 struct gles2_renderer *gr = get_renderer(ec);
1297
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001298 if (ec->has_bind_display)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001299 ec->unbind_display(gr->egl_display, ec->wl_display);
1300
1301 /* Work around crash in egl_dri2.c's dri2_make_current() - when does this apply? */
1302 eglMakeCurrent(gr->egl_display,
1303 EGL_NO_SURFACE, EGL_NO_SURFACE,
1304 EGL_NO_CONTEXT);
1305
1306 eglTerminate(gr->egl_display);
1307 eglReleaseThread();
1308}
1309
1310static int
1311egl_choose_config(struct gles2_renderer *gr, const EGLint *attribs,
1312 const EGLint *visual_id)
1313{
1314 EGLint count = 0;
1315 EGLint matched = 0;
1316 EGLConfig *configs;
1317 int i;
1318
1319 if (!eglGetConfigs(gr->egl_display, NULL, 0, &count) || count < 1)
1320 return -1;
1321
1322 configs = calloc(count, sizeof *configs);
1323 if (!configs)
1324 return -1;
1325
1326 if (!eglChooseConfig(gr->egl_display, attribs, configs,
1327 count, &matched))
1328 goto out;
1329
1330 for (i = 0; i < matched; ++i) {
1331 EGLint id;
1332
1333 if (visual_id) {
1334 if (!eglGetConfigAttrib(gr->egl_display,
1335 configs[i], EGL_NATIVE_VISUAL_ID,
1336 &id))
1337 continue;
1338
1339 if (id != *visual_id)
1340 continue;
1341 }
1342
1343 gr->egl_config = configs[i];
1344
1345 free(configs);
1346 return 0;
1347 }
1348
1349out:
1350 free(configs);
1351 return -1;
1352}
1353
1354WL_EXPORT const EGLint gles2_renderer_opaque_attribs[] = {
1355 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1356 EGL_RED_SIZE, 1,
1357 EGL_GREEN_SIZE, 1,
1358 EGL_BLUE_SIZE, 1,
1359 EGL_ALPHA_SIZE, 0,
1360 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1361 EGL_NONE
1362};
1363
1364WL_EXPORT const EGLint gles2_renderer_alpha_attribs[] = {
1365 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1366 EGL_RED_SIZE, 1,
1367 EGL_GREEN_SIZE, 1,
1368 EGL_BLUE_SIZE, 1,
1369 EGL_ALPHA_SIZE, 1,
1370 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1371 EGL_NONE
1372};
1373
1374WL_EXPORT int
1375gles2_renderer_create(struct weston_compositor *ec, EGLNativeDisplayType display,
1376 const EGLint *attribs, const EGLint *visual_id)
1377{
1378 struct gles2_renderer *gr;
1379 EGLint major, minor;
1380
1381 gr = calloc(1, sizeof *gr);
1382
1383 if (gr == NULL)
1384 return -1;
1385
1386 gr->base.repaint_output = gles2_renderer_repaint_output;
1387 gr->base.flush_damage = gles2_renderer_flush_damage;
1388 gr->base.attach = gles2_renderer_attach;
1389 gr->base.destroy_surface = gles2_renderer_destroy_surface;
1390
1391 gr->egl_display = eglGetDisplay(display);
1392 if (gr->egl_display == EGL_NO_DISPLAY) {
1393 weston_log("failed to create display\n");
1394 goto err_egl;
1395 }
1396
1397 if (!eglInitialize(gr->egl_display, &major, &minor)) {
1398 weston_log("failed to initialize display\n");
1399 goto err_egl;
1400 }
1401
1402 if (egl_choose_config(gr, attribs, visual_id) < 0) {
1403 weston_log("failed to choose EGL config\n");
1404 goto err_egl;
1405 }
1406
1407 ec->renderer = &gr->base;
1408
1409 return 0;
1410
1411err_egl:
1412 print_egl_error_state();
1413 free(gr);
1414 return -1;
1415}
1416
1417WL_EXPORT EGLDisplay
1418gles2_renderer_display(struct weston_compositor *ec)
1419{
1420 return get_renderer(ec)->egl_display;
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001421}
1422
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001423static int
1424compile_shaders(struct weston_compositor *ec)
1425{
1426 if (weston_shader_init(&ec->texture_shader_rgba, ec,
1427 vertex_shader, texture_fragment_shader_rgba) < 0)
1428 return -1;
1429 if (weston_shader_init(&ec->texture_shader_rgbx, ec,
1430 vertex_shader, texture_fragment_shader_rgbx) < 0)
1431 return -1;
1432 if (ec->has_egl_image_external &&
1433 weston_shader_init(&ec->texture_shader_egl_external, ec,
1434 vertex_shader, texture_fragment_shader_egl_external) < 0)
1435 return -1;
1436 if (weston_shader_init(&ec->texture_shader_y_uv, ec,
1437 vertex_shader, texture_fragment_shader_y_uv) < 0)
1438 return -1;
1439 if (weston_shader_init(&ec->texture_shader_y_u_v, ec,
1440 vertex_shader, texture_fragment_shader_y_u_v) < 0)
1441 return -1;
1442 if (weston_shader_init(&ec->texture_shader_y_xuxv, ec,
1443 vertex_shader, texture_fragment_shader_y_xuxv) < 0)
1444 return -1;
1445 if (weston_shader_init(&ec->solid_shader, ec,
1446 vertex_shader, solid_fragment_shader) < 0)
1447 return -1;
1448
1449 return 0;
1450}
1451
1452static void
1453fragment_debug_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
1454 void *data)
1455{
1456 struct weston_compositor *ec = data;
1457 struct gles2_renderer *renderer =
1458 (struct gles2_renderer *) ec->renderer;
1459 struct weston_output *output;
1460
1461 renderer->fragment_shader_debug ^= 1;
1462
1463 weston_shader_release(&ec->texture_shader_rgba);
1464 weston_shader_release(&ec->texture_shader_rgbx);
1465 weston_shader_release(&ec->texture_shader_egl_external);
1466 weston_shader_release(&ec->texture_shader_y_uv);
1467 weston_shader_release(&ec->texture_shader_y_u_v);
1468 weston_shader_release(&ec->texture_shader_y_xuxv);
1469 weston_shader_release(&ec->solid_shader);
1470
1471 compile_shaders(ec);
1472
1473 wl_list_for_each(output, &ec->output_list, link)
1474 weston_output_damage(output);
1475}
1476
John Kåre Alsaker94659272012-11-13 19:10:18 +01001477static int
1478gles2_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001479{
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001480 struct gles2_renderer *gr = get_renderer(ec);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001481 const char *extensions;
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001482 EGLBoolean ret;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001483
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001484 static const EGLint context_attribs[] = {
1485 EGL_CONTEXT_CLIENT_VERSION, 2,
1486 EGL_NONE
1487 };
1488
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001489 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
1490 weston_log("failed to bind EGL_OPENGL_ES_API\n");
1491 print_egl_error_state();
1492 return -1;
1493 }
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001494
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001495 log_egl_config_info(gr->egl_display, gr->egl_config);
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001496
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001497 gr->egl_context = eglCreateContext(gr->egl_display, gr->egl_config,
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001498 EGL_NO_CONTEXT, context_attribs);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001499 if (gr->egl_context == NULL) {
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001500 weston_log("failed to create context\n");
1501 print_egl_error_state();
1502 return -1;
1503 }
1504
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001505 ret = eglMakeCurrent(gr->egl_display, egl_surface,
1506 egl_surface, gr->egl_context);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001507 if (ret == EGL_FALSE) {
1508 weston_log("Failed to make EGL context current.\n");
1509 print_egl_error_state();
1510 return -1;
1511 }
1512
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001513 log_egl_gl_info(gr->egl_display);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001514
1515 ec->image_target_texture_2d =
1516 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
1517 ec->image_target_renderbuffer_storage = (void *)
1518 eglGetProcAddress("glEGLImageTargetRenderbufferStorageOES");
1519 ec->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1520 ec->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1521 ec->bind_display =
1522 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
1523 ec->unbind_display =
1524 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
1525 ec->query_buffer =
1526 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
1527
1528 extensions = (const char *) glGetString(GL_EXTENSIONS);
1529 if (!extensions) {
1530 weston_log("Retrieving GL extension string failed.\n");
1531 return -1;
1532 }
1533
1534 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
1535 weston_log("GL_EXT_texture_format_BGRA8888 not available\n");
1536 return -1;
1537 }
1538
1539 if (strstr(extensions, "GL_EXT_read_format_bgra"))
1540 ec->read_format = GL_BGRA_EXT;
1541 else
1542 ec->read_format = GL_RGBA;
1543
1544 if (strstr(extensions, "GL_EXT_unpack_subimage"))
1545 ec->has_unpack_subimage = 1;
1546
1547 if (strstr(extensions, "GL_OES_EGL_image_external"))
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001548 ec->has_egl_image_external = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001549
1550 extensions =
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001551 (const char *) eglQueryString(gr->egl_display, EGL_EXTENSIONS);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001552 if (!extensions) {
1553 weston_log("Retrieving EGL extension string failed.\n");
1554 return -1;
1555 }
1556
1557 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
1558 ec->has_bind_display = 1;
Pekka Paalanen035a0322012-10-24 09:43:06 +03001559 if (ec->has_bind_display) {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001560 ret = ec->bind_display(gr->egl_display, ec->wl_display);
Pekka Paalanen035a0322012-10-24 09:43:06 +03001561 if (!ret)
1562 ec->has_bind_display = 0;
1563 }
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001564
1565 glActiveTexture(GL_TEXTURE0);
1566
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001567 if (compile_shaders(ec))
1568 return -1;
1569
1570 weston_compositor_add_debug_binding(ec, KEY_S,
1571 fragment_debug_binding, ec);
1572
Pekka Paalanen035a0322012-10-24 09:43:06 +03001573 weston_log("GL ES 2 renderer features:\n");
1574 weston_log_continue(STAMP_SPACE "read-back format: %s\n",
1575 ec->read_format == GL_BGRA_EXT ? "BGRA" : "RGBA");
1576 weston_log_continue(STAMP_SPACE "wl_shm sub-image to texture: %s\n",
1577 ec->has_unpack_subimage ? "yes" : "no");
1578 weston_log_continue(STAMP_SPACE "EGL Wayland extension: %s\n",
1579 ec->has_bind_display ? "yes" : "no");
1580
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001581
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001582 return 0;
1583}