blob: a02cdacd995871a5147e7cad3191caf3b6f3e208 [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 Alsaker878f4492012-11-13 19:10:23 +010038struct gles2_surface_state {
39 GLfloat color[4];
40};
41
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +010042struct gles2_renderer {
43 struct weston_renderer base;
44 int fragment_shader_debug;
45
46 EGLDisplay egl_display;
47 EGLContext egl_context;
48 EGLConfig egl_config;
John Kåre Alsaker44154502012-11-13 19:10:20 +010049
50 struct {
51 int32_t top, bottom, left, right;
52 GLuint texture;
53 int32_t width, height;
54 } border;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +010055};
John Kåre Alsaker94659272012-11-13 19:10:18 +010056
57static inline struct gles2_output_state *
58get_output_state(struct weston_output *output)
59{
60 return (struct gles2_output_state *)output->renderer_state;
61}
62
John Kåre Alsaker878f4492012-11-13 19:10:23 +010063static inline struct gles2_surface_state *
64get_surface_state(struct weston_surface *surface)
65{
66 return (struct gles2_surface_state *)surface->renderer_state;
67}
68
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +010069static inline struct gles2_renderer *
70get_renderer(struct weston_compositor *ec)
71{
72 return (struct gles2_renderer *)ec->renderer;
73}
74
Kristian Høgsbergd7c17262012-09-05 21:54:15 -040075static const char *
76egl_error_string(EGLint code)
77{
78#define MYERRCODE(x) case x: return #x;
79 switch (code) {
80 MYERRCODE(EGL_SUCCESS)
81 MYERRCODE(EGL_NOT_INITIALIZED)
82 MYERRCODE(EGL_BAD_ACCESS)
83 MYERRCODE(EGL_BAD_ALLOC)
84 MYERRCODE(EGL_BAD_ATTRIBUTE)
85 MYERRCODE(EGL_BAD_CONTEXT)
86 MYERRCODE(EGL_BAD_CONFIG)
87 MYERRCODE(EGL_BAD_CURRENT_SURFACE)
88 MYERRCODE(EGL_BAD_DISPLAY)
89 MYERRCODE(EGL_BAD_SURFACE)
90 MYERRCODE(EGL_BAD_MATCH)
91 MYERRCODE(EGL_BAD_PARAMETER)
92 MYERRCODE(EGL_BAD_NATIVE_PIXMAP)
93 MYERRCODE(EGL_BAD_NATIVE_WINDOW)
94 MYERRCODE(EGL_CONTEXT_LOST)
95 default:
96 return "unknown";
97 }
98#undef MYERRCODE
99}
100
101static void
102print_egl_error_state(void)
103{
104 EGLint code;
105
106 code = eglGetError();
107 weston_log("EGL error state: %s (0x%04lx)\n",
108 egl_error_string(code), (long)code);
109}
110
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300111struct polygon8 {
112 GLfloat x[8];
113 GLfloat y[8];
114 int n;
115};
116
117struct clip_context {
118 struct {
119 GLfloat x;
120 GLfloat y;
121 } prev;
122
123 struct {
124 GLfloat x1, y1;
125 GLfloat x2, y2;
126 } clip;
127
128 struct {
129 GLfloat *x;
130 GLfloat *y;
131 } vertices;
132};
133
134static GLfloat
135float_difference(GLfloat a, GLfloat b)
136{
137 /* http://www.altdevblogaday.com/2012/02/22/comparing-floating-point-numbers-2012-edition/ */
138 static const GLfloat max_diff = 4.0f * FLT_MIN;
139 static const GLfloat max_rel_diff = 4.0e-5;
140 GLfloat diff = a - b;
141 GLfloat adiff = fabsf(diff);
142
143 if (adiff <= max_diff)
144 return 0.0f;
145
146 a = fabsf(a);
147 b = fabsf(b);
148 if (adiff <= (a > b ? a : b) * max_rel_diff)
149 return 0.0f;
150
151 return diff;
152}
153
154/* A line segment (p1x, p1y)-(p2x, p2y) intersects the line x = x_arg.
155 * Compute the y coordinate of the intersection.
156 */
157static GLfloat
158clip_intersect_y(GLfloat p1x, GLfloat p1y, GLfloat p2x, GLfloat p2y,
159 GLfloat x_arg)
160{
161 GLfloat a;
162 GLfloat diff = float_difference(p1x, p2x);
163
164 /* Practically vertical line segment, yet the end points have already
165 * been determined to be on different sides of the line. Therefore
166 * the line segment is part of the line and intersects everywhere.
167 * Return the end point, so we use the whole line segment.
168 */
169 if (diff == 0.0f)
170 return p2y;
171
172 a = (x_arg - p2x) / diff;
173 return p2y + (p1y - p2y) * a;
174}
175
176/* A line segment (p1x, p1y)-(p2x, p2y) intersects the line y = y_arg.
177 * Compute the x coordinate of the intersection.
178 */
179static GLfloat
180clip_intersect_x(GLfloat p1x, GLfloat p1y, GLfloat p2x, GLfloat p2y,
181 GLfloat y_arg)
182{
183 GLfloat a;
184 GLfloat diff = float_difference(p1y, p2y);
185
186 /* Practically horizontal line segment, yet the end points have already
187 * been determined to be on different sides of the line. Therefore
188 * the line segment is part of the line and intersects everywhere.
189 * Return the end point, so we use the whole line segment.
190 */
191 if (diff == 0.0f)
192 return p2x;
193
194 a = (y_arg - p2y) / diff;
195 return p2x + (p1x - p2x) * a;
196}
197
198enum path_transition {
199 PATH_TRANSITION_OUT_TO_OUT = 0,
200 PATH_TRANSITION_OUT_TO_IN = 1,
201 PATH_TRANSITION_IN_TO_OUT = 2,
202 PATH_TRANSITION_IN_TO_IN = 3,
203};
204
205static void
206clip_append_vertex(struct clip_context *ctx, GLfloat x, GLfloat y)
207{
208 *ctx->vertices.x++ = x;
209 *ctx->vertices.y++ = y;
210}
211
212static enum path_transition
213path_transition_left_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
214{
215 return ((ctx->prev.x >= ctx->clip.x1) << 1) | (x >= ctx->clip.x1);
216}
217
218static enum path_transition
219path_transition_right_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
220{
221 return ((ctx->prev.x < ctx->clip.x2) << 1) | (x < ctx->clip.x2);
222}
223
224static enum path_transition
225path_transition_top_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
226{
227 return ((ctx->prev.y >= ctx->clip.y1) << 1) | (y >= ctx->clip.y1);
228}
229
230static enum path_transition
231path_transition_bottom_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
232{
233 return ((ctx->prev.y < ctx->clip.y2) << 1) | (y < ctx->clip.y2);
234}
235
236static void
237clip_polygon_leftright(struct clip_context *ctx,
238 enum path_transition transition,
239 GLfloat x, GLfloat y, GLfloat clip_x)
240{
241 GLfloat yi;
242
243 switch (transition) {
244 case PATH_TRANSITION_IN_TO_IN:
245 clip_append_vertex(ctx, x, y);
246 break;
247 case PATH_TRANSITION_IN_TO_OUT:
248 yi = clip_intersect_y(ctx->prev.x, ctx->prev.y, x, y, clip_x);
249 clip_append_vertex(ctx, clip_x, yi);
250 break;
251 case PATH_TRANSITION_OUT_TO_IN:
252 yi = clip_intersect_y(ctx->prev.x, ctx->prev.y, x, y, clip_x);
253 clip_append_vertex(ctx, clip_x, yi);
254 clip_append_vertex(ctx, x, y);
255 break;
256 case PATH_TRANSITION_OUT_TO_OUT:
257 /* nothing */
258 break;
259 default:
260 assert(0 && "bad enum path_transition");
261 }
262
263 ctx->prev.x = x;
264 ctx->prev.y = y;
265}
266
267static void
268clip_polygon_topbottom(struct clip_context *ctx,
269 enum path_transition transition,
270 GLfloat x, GLfloat y, GLfloat clip_y)
271{
272 GLfloat xi;
273
274 switch (transition) {
275 case PATH_TRANSITION_IN_TO_IN:
276 clip_append_vertex(ctx, x, y);
277 break;
278 case PATH_TRANSITION_IN_TO_OUT:
279 xi = clip_intersect_x(ctx->prev.x, ctx->prev.y, x, y, clip_y);
280 clip_append_vertex(ctx, xi, clip_y);
281 break;
282 case PATH_TRANSITION_OUT_TO_IN:
283 xi = clip_intersect_x(ctx->prev.x, ctx->prev.y, x, y, clip_y);
284 clip_append_vertex(ctx, xi, clip_y);
285 clip_append_vertex(ctx, x, y);
286 break;
287 case PATH_TRANSITION_OUT_TO_OUT:
288 /* nothing */
289 break;
290 default:
291 assert(0 && "bad enum path_transition");
292 }
293
294 ctx->prev.x = x;
295 ctx->prev.y = y;
296}
297
298static void
299clip_context_prepare(struct clip_context *ctx, const struct polygon8 *src,
300 GLfloat *dst_x, GLfloat *dst_y)
301{
302 ctx->prev.x = src->x[src->n - 1];
303 ctx->prev.y = src->y[src->n - 1];
304 ctx->vertices.x = dst_x;
305 ctx->vertices.y = dst_y;
306}
307
308static int
309clip_polygon_left(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_left_edge(ctx, src->x[i], src->y[i]);
318 clip_polygon_leftright(ctx, trans, src->x[i], src->y[i],
319 ctx->clip.x1);
320 }
321 return ctx->vertices.x - dst_x;
322}
323
324static int
325clip_polygon_right(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_right_edge(ctx, src->x[i], src->y[i]);
334 clip_polygon_leftright(ctx, trans, src->x[i], src->y[i],
335 ctx->clip.x2);
336 }
337 return ctx->vertices.x - dst_x;
338}
339
340static int
341clip_polygon_top(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_top_edge(ctx, src->x[i], src->y[i]);
350 clip_polygon_topbottom(ctx, trans, src->x[i], src->y[i],
351 ctx->clip.y1);
352 }
353 return ctx->vertices.x - dst_x;
354}
355
356static int
357clip_polygon_bottom(struct clip_context *ctx, const struct polygon8 *src,
358 GLfloat *dst_x, GLfloat *dst_y)
359{
360 enum path_transition trans;
361 int i;
362
363 clip_context_prepare(ctx, src, dst_x, dst_y);
364 for (i = 0; i < src->n; i++) {
365 trans = path_transition_bottom_edge(ctx, src->x[i], src->y[i]);
366 clip_polygon_topbottom(ctx, trans, src->x[i], src->y[i],
367 ctx->clip.y2);
368 }
369 return ctx->vertices.x - dst_x;
370}
371
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400372#define max(a, b) (((a) > (b)) ? (a) : (b))
373#define min(a, b) (((a) > (b)) ? (b) : (a))
374#define clip(x, a, b) min(max(x, a), b)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400375
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300376/*
377 * Compute the boundary vertices of the intersection of the global coordinate
378 * aligned rectangle 'rect', and an arbitrary quadrilateral produced from
379 * 'surf_rect' when transformed from surface coordinates into global coordinates.
380 * The vertices are written to 'ex' and 'ey', and the return value is the
381 * number of vertices. Vertices are produced in clockwise winding order.
382 * Guarantees to produce either zero vertices, or 3-8 vertices with non-zero
383 * polygon area.
384 */
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400385static int
386calculate_edges(struct weston_surface *es, pixman_box32_t *rect,
387 pixman_box32_t *surf_rect, GLfloat *ex, GLfloat *ey)
388{
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300389 struct polygon8 polygon;
390 struct clip_context ctx;
391 int i, n;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400392 GLfloat min_x, max_x, min_y, max_y;
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300393 struct polygon8 surf = {
394 { surf_rect->x1, surf_rect->x2, surf_rect->x2, surf_rect->x1 },
395 { surf_rect->y1, surf_rect->y1, surf_rect->y2, surf_rect->y2 },
396 4
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400397 };
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400398
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300399 ctx.clip.x1 = rect->x1;
400 ctx.clip.y1 = rect->y1;
401 ctx.clip.x2 = rect->x2;
402 ctx.clip.y2 = rect->y2;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400403
404 /* transform surface to screen space: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300405 for (i = 0; i < surf.n; i++)
406 weston_surface_to_global_float(es, surf.x[i], surf.y[i],
407 &surf.x[i], &surf.y[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400408
409 /* find bounding box: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300410 min_x = max_x = surf.x[0];
411 min_y = max_y = surf.y[0];
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400412
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300413 for (i = 1; i < surf.n; i++) {
414 min_x = min(min_x, surf.x[i]);
415 max_x = max(max_x, surf.x[i]);
416 min_y = min(min_y, surf.y[i]);
417 max_y = max(max_y, surf.y[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400418 }
419
420 /* First, simple bounding box check to discard early transformed
421 * surface rects that do not intersect with the clip region:
422 */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300423 if ((min_x >= ctx.clip.x2) || (max_x <= ctx.clip.x1) ||
424 (min_y >= ctx.clip.y2) || (max_y <= ctx.clip.y1))
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400425 return 0;
426
427 /* Simple case, bounding box edges are parallel to surface edges,
428 * there will be only four edges. We just need to clip the surface
429 * vertices to the clip rect bounds:
430 */
431 if (!es->transform.enabled) {
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300432 for (i = 0; i < surf.n; i++) {
433 ex[i] = clip(surf.x[i], ctx.clip.x1, ctx.clip.x2);
434 ey[i] = clip(surf.y[i], ctx.clip.y1, ctx.clip.y2);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400435 }
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300436 return surf.n;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400437 }
438
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300439 /* Transformed case: use a general polygon clipping algorithm to
440 * clip the surface rectangle with each side of 'rect'.
441 * The algorithm is Sutherland-Hodgman, as explained in
442 * http://www.codeguru.com/cpp/misc/misc/graphics/article.php/c8965/Polygon-Clipping.htm
443 * but without looking at any of that code.
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400444 */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300445 polygon.n = clip_polygon_left(&ctx, &surf, polygon.x, polygon.y);
446 surf.n = clip_polygon_right(&ctx, &polygon, surf.x, surf.y);
447 polygon.n = clip_polygon_top(&ctx, &surf, polygon.x, polygon.y);
448 surf.n = clip_polygon_bottom(&ctx, &polygon, surf.x, surf.y);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400449
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300450 /* Get rid of duplicate vertices */
451 ex[0] = surf.x[0];
452 ey[0] = surf.y[0];
453 n = 1;
454 for (i = 1; i < surf.n; i++) {
455 if (float_difference(ex[n - 1], surf.x[i]) == 0.0f &&
456 float_difference(ey[n - 1], surf.y[i]) == 0.0f)
457 continue;
458 ex[n] = surf.x[i];
459 ey[n] = surf.y[i];
460 n++;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400461 }
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300462 if (float_difference(ex[n - 1], surf.x[0]) == 0.0f &&
463 float_difference(ey[n - 1], surf.y[0]) == 0.0f)
464 n--;
465
466 if (n < 3)
467 return 0;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400468
469 return n;
470}
471
472static int
473texture_region(struct weston_surface *es, pixman_region32_t *region,
474 pixman_region32_t *surf_region)
475{
476 struct weston_compositor *ec = es->compositor;
477 GLfloat *v, inv_width, inv_height;
478 unsigned int *vtxcnt, nvtx = 0;
479 pixman_box32_t *rects, *surf_rects;
480 int i, j, k, nrects, nsurf;
481
482 rects = pixman_region32_rectangles(region, &nrects);
483 surf_rects = pixman_region32_rectangles(surf_region, &nsurf);
484
485 /* worst case we can have 8 vertices per rect (ie. clipped into
486 * an octagon):
487 */
488 v = wl_array_add(&ec->vertices, nrects * nsurf * 8 * 4 * sizeof *v);
489 vtxcnt = wl_array_add(&ec->vtxcnt, nrects * nsurf * sizeof *vtxcnt);
490
491 inv_width = 1.0 / es->pitch;
492 inv_height = 1.0 / es->geometry.height;
493
494 for (i = 0; i < nrects; i++) {
495 pixman_box32_t *rect = &rects[i];
496 for (j = 0; j < nsurf; j++) {
497 pixman_box32_t *surf_rect = &surf_rects[j];
498 GLfloat sx, sy;
499 GLfloat ex[8], ey[8]; /* edge points in screen space */
500 int n;
501
502 /* The transformed surface, after clipping to the clip region,
503 * can have as many as eight sides, emitted as a triangle-fan.
504 * The first vertex in the triangle fan can be chosen arbitrarily,
505 * since the area is guaranteed to be convex.
506 *
507 * If a corner of the transformed surface falls outside of the
508 * clip region, instead of emitting one vertex for the corner
509 * of the surface, up to two are emitted for two corresponding
510 * intersection point(s) between the surface and the clip region.
511 *
512 * To do this, we first calculate the (up to eight) points that
513 * form the intersection of the clip rect and the transformed
514 * surface.
515 */
516 n = calculate_edges(es, rect, surf_rect, ex, ey);
517 if (n < 3)
518 continue;
519
520 /* emit edge points: */
521 for (k = 0; k < n; k++) {
522 weston_surface_from_global_float(es, ex[k], ey[k], &sx, &sy);
523 /* position: */
524 *(v++) = ex[k];
525 *(v++) = ey[k];
526 /* texcoord: */
527 *(v++) = sx * inv_width;
528 *(v++) = sy * inv_height;
529 }
530
531 vtxcnt[nvtx++] = n;
532 }
533 }
534
535 return nvtx;
536}
537
538static void
539triangle_fan_debug(struct weston_surface *surface, int first, int count)
540{
541 struct weston_compositor *compositor = surface->compositor;
542 int i;
543 GLushort *buffer;
544 GLushort *index;
545 int nelems;
546 static int color_idx = 0;
547 static const GLfloat color[][4] = {
548 { 1.0, 0.0, 0.0, 1.0 },
549 { 0.0, 1.0, 0.0, 1.0 },
550 { 0.0, 0.0, 1.0, 1.0 },
551 { 1.0, 1.0, 1.0, 1.0 },
552 };
553
554 nelems = (count - 1 + count - 2) * 2;
555
556 buffer = malloc(sizeof(GLushort) * nelems);
557 index = buffer;
558
559 for (i = 1; i < count; i++) {
560 *index++ = first;
561 *index++ = first + i;
562 }
563
564 for (i = 2; i < count; i++) {
565 *index++ = first + i - 1;
566 *index++ = first + i;
567 }
568
569 glUseProgram(compositor->solid_shader.program);
570 glUniform4fv(compositor->solid_shader.color_uniform, 1,
571 color[color_idx++ % ARRAY_LENGTH(color)]);
572 glDrawElements(GL_LINES, nelems, GL_UNSIGNED_SHORT, buffer);
573 glUseProgram(compositor->current_shader->program);
574 free(buffer);
575}
576
577static void
578repaint_region(struct weston_surface *es, pixman_region32_t *region,
579 pixman_region32_t *surf_region)
580{
581 struct weston_compositor *ec = es->compositor;
582 GLfloat *v;
583 unsigned int *vtxcnt;
584 int i, first, nfans;
585
586 /* The final region to be painted is the intersection of
587 * 'region' and 'surf_region'. However, 'region' is in the global
588 * coordinates, and 'surf_region' is in the surface-local
589 * coordinates. texture_region() will iterate over all pairs of
590 * rectangles from both regions, compute the intersection
591 * polygon for each pair, and store it as a triangle fan if
592 * it has a non-zero area (at least 3 vertices, actually).
593 */
594 nfans = texture_region(es, region, surf_region);
595
596 v = ec->vertices.data;
597 vtxcnt = ec->vtxcnt.data;
598
599 /* position: */
600 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
601 glEnableVertexAttribArray(0);
602
603 /* texcoord: */
604 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
605 glEnableVertexAttribArray(1);
606
607 for (i = 0, first = 0; i < nfans; i++) {
608 glDrawArrays(GL_TRIANGLE_FAN, first, vtxcnt[i]);
609 if (ec->fan_debug)
610 triangle_fan_debug(es, first, vtxcnt[i]);
611 first += vtxcnt[i];
612 }
613
614 glDisableVertexAttribArray(1);
615 glDisableVertexAttribArray(0);
616
617 ec->vertices.size = 0;
618 ec->vtxcnt.size = 0;
619}
620
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100621static int
622use_output(struct weston_output *output)
623{
624 static int errored;
625 struct gles2_output_state *go = get_output_state(output);
626 struct gles2_renderer *gr = get_renderer(output->compositor);
627 EGLBoolean ret;
628
629 ret = eglMakeCurrent(gr->egl_display, go->egl_surface,
630 go->egl_surface, gr->egl_context);
631
632 if (ret == EGL_FALSE) {
633 if (errored)
634 return -1;
635 errored = 1;
636 weston_log("Failed to make EGL context current.\n");
637 print_egl_error_state();
638 return -1;
639 }
640
641 return 0;
642}
643
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400644static void
645weston_compositor_use_shader(struct weston_compositor *compositor,
646 struct weston_shader *shader)
647{
648 if (compositor->current_shader == shader)
649 return;
650
651 glUseProgram(shader->program);
652 compositor->current_shader = shader;
653}
654
655static void
656weston_shader_uniforms(struct weston_shader *shader,
657 struct weston_surface *surface,
658 struct weston_output *output)
659{
660 int i;
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100661 struct gles2_surface_state *gs = get_surface_state(surface);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400662
663 glUniformMatrix4fv(shader->proj_uniform,
664 1, GL_FALSE, output->matrix.d);
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100665 glUniform4fv(shader->color_uniform, 1, gs->color);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400666 glUniform1f(shader->alpha_uniform, surface->alpha);
667
668 for (i = 0; i < surface->num_textures; i++)
669 glUniform1i(shader->tex_uniforms[i], i);
670}
671
672static void
673draw_surface(struct weston_surface *es, struct weston_output *output,
674 pixman_region32_t *damage) /* in global coordinates */
675{
676 struct weston_compositor *ec = es->compositor;
677 /* repaint bounding region in global coordinates: */
678 pixman_region32_t repaint;
679 /* non-opaque region in surface coordinates: */
680 pixman_region32_t surface_blend;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300681 pixman_region32_t *buffer_damage;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400682 GLint filter;
683 int i;
684
685 pixman_region32_init(&repaint);
686 pixman_region32_intersect(&repaint,
687 &es->transform.boundingbox, damage);
688 pixman_region32_subtract(&repaint, &repaint, &es->clip);
689
690 if (!pixman_region32_not_empty(&repaint))
691 goto out;
692
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300693 buffer_damage = &output->buffer_damage[output->current_buffer];
694 pixman_region32_subtract(buffer_damage, buffer_damage, &repaint);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400695
696 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
697
698 if (ec->fan_debug) {
699 weston_compositor_use_shader(ec, &ec->solid_shader);
700 weston_shader_uniforms(&ec->solid_shader, es, output);
701 }
702
703 weston_compositor_use_shader(ec, es->shader);
704 weston_shader_uniforms(es->shader, es, output);
705
706 if (es->transform.enabled || output->zoom.active)
707 filter = GL_LINEAR;
708 else
709 filter = GL_NEAREST;
710
711 for (i = 0; i < es->num_textures; i++) {
712 glActiveTexture(GL_TEXTURE0 + i);
713 glBindTexture(es->target, es->textures[i]);
714 glTexParameteri(es->target, GL_TEXTURE_MIN_FILTER, filter);
715 glTexParameteri(es->target, GL_TEXTURE_MAG_FILTER, filter);
716 }
717
718 /* blended region is whole surface minus opaque region: */
719 pixman_region32_init_rect(&surface_blend, 0, 0,
720 es->geometry.width, es->geometry.height);
721 pixman_region32_subtract(&surface_blend, &surface_blend, &es->opaque);
722
723 if (pixman_region32_not_empty(&es->opaque)) {
724 if (es->shader == &ec->texture_shader_rgba) {
725 /* Special case for RGBA textures with possibly
726 * bad data in alpha channel: use the shader
727 * that forces texture alpha = 1.0.
728 * Xwayland surfaces need this.
729 */
730 weston_compositor_use_shader(ec, &ec->texture_shader_rgbx);
731 weston_shader_uniforms(&ec->texture_shader_rgbx, es, output);
732 }
733
734 if (es->alpha < 1.0)
735 glEnable(GL_BLEND);
736 else
737 glDisable(GL_BLEND);
738
739 repaint_region(es, &repaint, &es->opaque);
740 }
741
742 if (pixman_region32_not_empty(&surface_blend)) {
743 weston_compositor_use_shader(ec, es->shader);
744 glEnable(GL_BLEND);
745 repaint_region(es, &repaint, &surface_blend);
746 }
747
748 pixman_region32_fini(&surface_blend);
749
750out:
751 pixman_region32_fini(&repaint);
752}
753
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400754static void
755repaint_surfaces(struct weston_output *output, pixman_region32_t *damage)
756{
757 struct weston_compositor *compositor = output->compositor;
758 struct weston_surface *surface;
759
760 wl_list_for_each_reverse(surface, &compositor->surface_list, link)
761 if (surface->plane == &compositor->primary_plane)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400762 draw_surface(surface, output, damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400763}
764
John Kåre Alsaker44154502012-11-13 19:10:20 +0100765
766static int
767texture_border(struct weston_output *output)
768{
769 struct weston_compositor *ec = output->compositor;
770 struct gles2_renderer *gr = get_renderer(ec);
771 GLfloat *d;
772 unsigned int *p;
773 int i, j, k, n;
774 GLfloat x[4], y[4], u[4], v[4];
775
776 x[0] = -gr->border.left;
777 x[1] = 0;
778 x[2] = output->current->width;
779 x[3] = output->current->width + gr->border.right;
780
781 y[0] = -gr->border.top;
782 y[1] = 0;
783 y[2] = output->current->height;
784 y[3] = output->current->height + gr->border.bottom;
785
786 u[0] = 0.0;
787 u[1] = (GLfloat) gr->border.left / gr->border.width;
788 u[2] = (GLfloat) (gr->border.width - gr->border.right) / gr->border.width;
789 u[3] = 1.0;
790
791 v[0] = 0.0;
792 v[1] = (GLfloat) gr->border.top / gr->border.height;
793 v[2] = (GLfloat) (gr->border.height - gr->border.bottom) / gr->border.height;
794 v[3] = 1.0;
795
796 n = 8;
797 d = wl_array_add(&ec->vertices, n * 16 * sizeof *d);
798 p = wl_array_add(&ec->indices, n * 6 * sizeof *p);
799
800 k = 0;
801 for (i = 0; i < 3; i++)
802 for (j = 0; j < 3; j++) {
803
804 if (i == 1 && j == 1)
805 continue;
806
807 d[ 0] = x[i];
808 d[ 1] = y[j];
809 d[ 2] = u[i];
810 d[ 3] = v[j];
811
812 d[ 4] = x[i];
813 d[ 5] = y[j + 1];
814 d[ 6] = u[i];
815 d[ 7] = v[j + 1];
816
817 d[ 8] = x[i + 1];
818 d[ 9] = y[j];
819 d[10] = u[i + 1];
820 d[11] = v[j];
821
822 d[12] = x[i + 1];
823 d[13] = y[j + 1];
824 d[14] = u[i + 1];
825 d[15] = v[j + 1];
826
827 p[0] = k + 0;
828 p[1] = k + 1;
829 p[2] = k + 2;
830 p[3] = k + 2;
831 p[4] = k + 1;
832 p[5] = k + 3;
833
834 d += 16;
835 p += 6;
836 k += 4;
837 }
838
839 return k / 4;
840}
841
842static void
843draw_border(struct weston_output *output)
844{
845 struct weston_compositor *ec = output->compositor;
846 struct gles2_renderer *gr = get_renderer(ec);
847 struct weston_shader *shader = &ec->texture_shader_rgba;
848 GLfloat *v;
849 int n;
850
851 glDisable(GL_BLEND);
852 glUseProgram(shader->program);
853 ec->current_shader = shader;
854
855 glUniformMatrix4fv(shader->proj_uniform,
856 1, GL_FALSE, output->matrix.d);
857
858 glUniform1i(shader->tex_uniforms[0], 0);
859 glUniform1f(shader->alpha_uniform, 1);
860
861 n = texture_border(output);
862
863 glActiveTexture(GL_TEXTURE0);
864 glBindTexture(GL_TEXTURE_2D, gr->border.texture);
865
866 v = ec->vertices.data;
867 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
868 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
869 glEnableVertexAttribArray(0);
870 glEnableVertexAttribArray(1);
871
872 glDrawElements(GL_TRIANGLES, n * 6,
873 GL_UNSIGNED_INT, ec->indices.data);
874
875 glDisableVertexAttribArray(1);
876 glDisableVertexAttribArray(0);
877
878 ec->vertices.size = 0;
879 ec->indices.size = 0;
880}
881
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400882static void
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400883gles2_renderer_repaint_output(struct weston_output *output,
884 pixman_region32_t *output_damage)
885{
John Kåre Alsaker94659272012-11-13 19:10:18 +0100886 struct gles2_output_state *go = get_output_state(output);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400887 struct weston_compositor *compositor = output->compositor;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100888 struct gles2_renderer *gr = get_renderer(compositor);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400889 EGLBoolean ret;
890 static int errored;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300891 int32_t width, height, i;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400892
893 width = output->current->width +
894 output->border.left + output->border.right;
895 height = output->current->height +
896 output->border.top + output->border.bottom;
897
898 glViewport(0, 0, width, height);
899
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100900 if (use_output(output) < 0)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400901 return;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400902
903 /* if debugging, redraw everything outside the damage to clean up
904 * debug lines from the previous draw on this buffer:
905 */
906 if (compositor->fan_debug) {
907 pixman_region32_t undamaged;
908 pixman_region32_init(&undamaged);
909 pixman_region32_subtract(&undamaged, &output->region,
910 output_damage);
911 compositor->fan_debug = 0;
912 repaint_surfaces(output, &undamaged);
913 compositor->fan_debug = 1;
914 pixman_region32_fini(&undamaged);
915 }
916
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300917 for (i = 0; i < 2; i++)
918 pixman_region32_union(&output->buffer_damage[i],
919 &output->buffer_damage[i],
920 output_damage);
921
922 pixman_region32_union(output_damage, output_damage,
923 &output->buffer_damage[output->current_buffer]);
924
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400925 repaint_surfaces(output, output_damage);
926
John Kåre Alsaker44154502012-11-13 19:10:20 +0100927 if (gr->border.texture)
928 draw_border(output);
929
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400930 wl_signal_emit(&output->frame_signal, output);
931
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100932 ret = eglSwapBuffers(gr->egl_display, go->egl_surface);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400933 if (ret == EGL_FALSE && !errored) {
934 errored = 1;
935 weston_log("Failed in eglSwapBuffers.\n");
936 print_egl_error_state();
937 }
938
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300939 output->current_buffer ^= 1;
940
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400941}
Kristian Høgsberg25894fc2012-09-05 22:06:26 -0400942
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100943static int
944gles2_renderer_read_pixels(struct weston_output *output,
945 pixman_format_code_t format, void *pixels,
946 uint32_t x, uint32_t y,
947 uint32_t width, uint32_t height)
948{
949 GLenum gl_format;
950
951 switch (format) {
952 case PIXMAN_a8r8g8b8:
953 gl_format = GL_BGRA_EXT;
954 break;
955 case PIXMAN_a8b8g8r8:
956 gl_format = GL_RGBA;
957 break;
958 default:
959 return -1;
960 }
961
962 if (use_output(output) < 0)
963 return -1;
964
965 glPixelStorei(GL_PACK_ALIGNMENT, 1);
966 glReadPixels(x, y, width, height, gl_format,
967 GL_UNSIGNED_BYTE, pixels);
968
969 return 0;
970}
971
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400972static void
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -0400973gles2_renderer_flush_damage(struct weston_surface *surface)
974{
975#ifdef GL_UNPACK_ROW_LENGTH
976 pixman_box32_t *rectangles;
977 void *data;
978 int i, n;
979#endif
980
Pekka Paalanenbcdd5792012-11-07 12:25:13 +0200981 pixman_region32_union(&surface->texture_damage,
982 &surface->texture_damage, &surface->damage);
983
984 /* Avoid upload, if the texture won't be used this time.
985 * We still accumulate the damage in texture_damage.
986 */
987 if (surface->plane != &surface->compositor->primary_plane)
988 return;
989
990 if (!pixman_region32_not_empty(&surface->texture_damage))
991 return;
992
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -0400993 glBindTexture(GL_TEXTURE_2D, surface->textures[0]);
994
995 if (!surface->compositor->has_unpack_subimage) {
996 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
997 surface->pitch, surface->buffer->height, 0,
998 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
999 wl_shm_buffer_get_data(surface->buffer));
1000
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001001 goto done;
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001002 }
1003
1004#ifdef GL_UNPACK_ROW_LENGTH
1005 /* Mesa does not define GL_EXT_unpack_subimage */
1006 glPixelStorei(GL_UNPACK_ROW_LENGTH, surface->pitch);
1007 data = wl_shm_buffer_get_data(surface->buffer);
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001008 rectangles = pixman_region32_rectangles(&surface->texture_damage, &n);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001009 for (i = 0; i < n; i++) {
1010 glPixelStorei(GL_UNPACK_SKIP_PIXELS, rectangles[i].x1);
1011 glPixelStorei(GL_UNPACK_SKIP_ROWS, rectangles[i].y1);
1012 glTexSubImage2D(GL_TEXTURE_2D, 0,
1013 rectangles[i].x1, rectangles[i].y1,
1014 rectangles[i].x2 - rectangles[i].x1,
1015 rectangles[i].y2 - rectangles[i].y1,
1016 GL_BGRA_EXT, GL_UNSIGNED_BYTE, data);
1017 }
1018#endif
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001019
1020done:
1021 pixman_region32_fini(&surface->texture_damage);
1022 pixman_region32_init(&surface->texture_damage);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001023}
1024
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001025static void
1026ensure_textures(struct weston_surface *es, int num_textures)
1027{
1028 int i;
1029
1030 if (num_textures <= es->num_textures)
1031 return;
1032
1033 for (i = es->num_textures; i < num_textures; i++) {
1034 glGenTextures(1, &es->textures[i]);
1035 glBindTexture(es->target, es->textures[i]);
1036 glTexParameteri(es->target,
1037 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1038 glTexParameteri(es->target,
1039 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1040 }
1041 es->num_textures = num_textures;
1042 glBindTexture(es->target, 0);
1043}
1044
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001045static void
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001046gles2_renderer_attach(struct weston_surface *es, struct wl_buffer *buffer)
1047{
1048 struct weston_compositor *ec = es->compositor;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001049 struct gles2_renderer *gr = get_renderer(ec);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001050 EGLint attribs[3], format;
1051 int i, num_planes;
1052
1053 if (!buffer) {
1054 for (i = 0; i < es->num_images; i++) {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001055 ec->destroy_image(gr->egl_display, es->images[i]);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001056 es->images[i] = NULL;
1057 }
1058 es->num_images = 0;
1059 glDeleteTextures(es->num_textures, es->textures);
1060 es->num_textures = 0;
1061 return;
1062 }
1063
1064 if (wl_buffer_is_shm(buffer)) {
1065 es->pitch = wl_shm_buffer_get_stride(buffer) / 4;
1066 es->target = GL_TEXTURE_2D;
1067
1068 ensure_textures(es, 1);
1069 glBindTexture(GL_TEXTURE_2D, es->textures[0]);
1070 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1071 es->pitch, buffer->height, 0,
1072 GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
1073 if (wl_shm_buffer_get_format(buffer) == WL_SHM_FORMAT_XRGB8888)
1074 es->shader = &ec->texture_shader_rgbx;
1075 else
1076 es->shader = &ec->texture_shader_rgba;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001077 } else if (ec->query_buffer(gr->egl_display, buffer,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001078 EGL_TEXTURE_FORMAT, &format)) {
1079 for (i = 0; i < es->num_images; i++)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001080 ec->destroy_image(gr->egl_display, es->images[i]);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001081 es->num_images = 0;
1082 es->target = GL_TEXTURE_2D;
1083 switch (format) {
1084 case EGL_TEXTURE_RGB:
1085 case EGL_TEXTURE_RGBA:
1086 default:
1087 num_planes = 1;
1088 es->shader = &ec->texture_shader_rgba;
1089 break;
1090 case EGL_TEXTURE_EXTERNAL_WL:
1091 num_planes = 1;
1092 es->target = GL_TEXTURE_EXTERNAL_OES;
1093 es->shader = &ec->texture_shader_egl_external;
1094 break;
1095 case EGL_TEXTURE_Y_UV_WL:
1096 num_planes = 2;
1097 es->shader = &ec->texture_shader_y_uv;
1098 break;
1099 case EGL_TEXTURE_Y_U_V_WL:
1100 num_planes = 3;
1101 es->shader = &ec->texture_shader_y_u_v;
1102 break;
1103 case EGL_TEXTURE_Y_XUXV_WL:
1104 num_planes = 2;
1105 es->shader = &ec->texture_shader_y_xuxv;
1106 break;
1107 }
1108
1109 ensure_textures(es, num_planes);
1110 for (i = 0; i < num_planes; i++) {
1111 attribs[0] = EGL_WAYLAND_PLANE_WL;
1112 attribs[1] = i;
1113 attribs[2] = EGL_NONE;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001114 es->images[i] = ec->create_image(gr->egl_display,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001115 NULL,
1116 EGL_WAYLAND_BUFFER_WL,
1117 buffer, attribs);
1118 if (!es->images[i]) {
1119 weston_log("failed to create img for plane %d\n", i);
1120 continue;
1121 }
1122 es->num_images++;
1123
1124 glActiveTexture(GL_TEXTURE0 + i);
1125 glBindTexture(es->target, es->textures[i]);
1126 ec->image_target_texture_2d(es->target,
1127 es->images[i]);
1128 }
1129
1130 es->pitch = buffer->width;
1131 } else {
1132 weston_log("unhandled buffer type!\n");
1133 }
1134}
1135
Kristian Høgsberg42263852012-09-06 21:59:29 -04001136static void
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001137gles2_renderer_surface_set_color(struct weston_surface *surface,
1138 float red, float green, float blue, float alpha)
1139{
1140 struct gles2_surface_state *gs = get_surface_state(surface);
1141
1142 gs->color[0] = red;
1143 gs->color[1] = green;
1144 gs->color[2] = blue;
1145 gs->color[3] = alpha;
1146
1147 surface->shader = &surface->compositor->solid_shader;
1148}
1149
1150static int
1151gles2_renderer_create_surface(struct weston_surface *surface)
1152{
1153 struct gles2_surface_state *gs;
1154
1155 gs = calloc(1, sizeof *gs);
1156
1157 if (!gs)
1158 return -1;
1159
1160 surface->renderer_state = gs;
1161
1162 return 0;
1163}
1164
1165static void
Kristian Høgsberg42263852012-09-06 21:59:29 -04001166gles2_renderer_destroy_surface(struct weston_surface *surface)
1167{
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001168 struct gles2_surface_state *gs = get_surface_state(surface);
Kristian Høgsberg42263852012-09-06 21:59:29 -04001169 struct weston_compositor *ec = surface->compositor;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001170 struct gles2_renderer *gr = get_renderer(ec);
Kristian Høgsberg42263852012-09-06 21:59:29 -04001171 int i;
1172
1173 glDeleteTextures(surface->num_textures, surface->textures);
1174
1175 for (i = 0; i < surface->num_images; i++)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001176 ec->destroy_image(gr->egl_display, surface->images[i]);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001177
1178 free(gs);
Kristian Høgsberg42263852012-09-06 21:59:29 -04001179}
1180
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001181static const char vertex_shader[] =
1182 "uniform mat4 proj;\n"
1183 "attribute vec2 position;\n"
1184 "attribute vec2 texcoord;\n"
1185 "varying vec2 v_texcoord;\n"
1186 "void main()\n"
1187 "{\n"
1188 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
1189 " v_texcoord = texcoord;\n"
1190 "}\n";
1191
1192/* Declare common fragment shader uniforms */
1193#define FRAGMENT_CONVERT_YUV \
1194 " y *= alpha;\n" \
1195 " u *= alpha;\n" \
1196 " v *= alpha;\n" \
1197 " gl_FragColor.r = y + 1.59602678 * v;\n" \
1198 " gl_FragColor.g = y - 0.39176229 * u - 0.81296764 * v;\n" \
1199 " gl_FragColor.b = y + 2.01723214 * u;\n" \
1200 " gl_FragColor.a = alpha;\n"
1201
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001202static const char fragment_debug[] =
1203 " gl_FragColor = vec4(0.0, 0.3, 0.0, 0.2) + gl_FragColor * 0.8;\n";
1204
1205static const char fragment_brace[] =
1206 "}\n";
1207
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001208static const char texture_fragment_shader_rgba[] =
1209 "precision mediump float;\n"
1210 "varying vec2 v_texcoord;\n"
1211 "uniform sampler2D tex;\n"
1212 "uniform float alpha;\n"
1213 "void main()\n"
1214 "{\n"
1215 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001216 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001217
1218static const char texture_fragment_shader_rgbx[] =
1219 "precision mediump float;\n"
1220 "varying vec2 v_texcoord;\n"
1221 "uniform sampler2D tex;\n"
1222 "uniform float alpha;\n"
1223 "void main()\n"
1224 "{\n"
1225 " gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb\n;"
1226 " gl_FragColor.a = alpha;\n"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001227 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001228
1229static const char texture_fragment_shader_egl_external[] =
1230 "#extension GL_OES_EGL_image_external : require\n"
1231 "precision mediump float;\n"
1232 "varying vec2 v_texcoord;\n"
1233 "uniform samplerExternalOES tex;\n"
1234 "uniform float alpha;\n"
1235 "void main()\n"
1236 "{\n"
1237 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001238 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001239
1240static const char texture_fragment_shader_y_uv[] =
1241 "precision mediump float;\n"
1242 "uniform sampler2D tex;\n"
1243 "uniform sampler2D tex1;\n"
1244 "varying vec2 v_texcoord;\n"
1245 "uniform float alpha;\n"
1246 "void main() {\n"
1247 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1248 " float u = texture2D(tex1, v_texcoord).r - 0.5;\n"
1249 " float v = texture2D(tex1, v_texcoord).g - 0.5;\n"
1250 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001251 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001252
1253static const char texture_fragment_shader_y_u_v[] =
1254 "precision mediump float;\n"
1255 "uniform sampler2D tex;\n"
1256 "uniform sampler2D tex1;\n"
1257 "uniform sampler2D tex2;\n"
1258 "varying vec2 v_texcoord;\n"
1259 "uniform float alpha;\n"
1260 "void main() {\n"
1261 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1262 " float u = texture2D(tex1, v_texcoord).x - 0.5;\n"
1263 " float v = texture2D(tex2, v_texcoord).x - 0.5;\n"
1264 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001265 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001266
1267static const char texture_fragment_shader_y_xuxv[] =
1268 "precision mediump float;\n"
1269 "uniform sampler2D tex;\n"
1270 "uniform sampler2D tex1;\n"
1271 "varying vec2 v_texcoord;\n"
1272 "uniform float alpha;\n"
1273 "void main() {\n"
1274 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1275 " float u = texture2D(tex1, v_texcoord).g - 0.5;\n"
1276 " float v = texture2D(tex1, v_texcoord).a - 0.5;\n"
1277 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001278 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001279
1280static const char solid_fragment_shader[] =
1281 "precision mediump float;\n"
1282 "uniform vec4 color;\n"
1283 "uniform float alpha;\n"
1284 "void main()\n"
1285 "{\n"
1286 " gl_FragColor = alpha * color\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001287 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001288
1289static int
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001290compile_shader(GLenum type, int count, const char **sources)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001291{
1292 GLuint s;
1293 char msg[512];
1294 GLint status;
1295
1296 s = glCreateShader(type);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001297 glShaderSource(s, count, sources, NULL);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001298 glCompileShader(s);
1299 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
1300 if (!status) {
1301 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1302 weston_log("shader info: %s\n", msg);
1303 return GL_NONE;
1304 }
1305
1306 return s;
1307}
1308
1309static int
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001310weston_shader_init(struct weston_shader *shader, struct weston_compositor *ec,
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001311 const char *vertex_source, const char *fragment_source)
1312{
1313 char msg[512];
1314 GLint status;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001315 int count;
1316 const char *sources[3];
1317 struct gles2_renderer *renderer =
1318 (struct gles2_renderer *) ec->renderer;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001319
1320 shader->vertex_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001321 compile_shader(GL_VERTEX_SHADER, 1, &vertex_source);
1322
1323 if (renderer->fragment_shader_debug) {
1324 sources[0] = fragment_source;
1325 sources[1] = fragment_debug;
1326 sources[2] = fragment_brace;
1327 count = 3;
1328 } else {
1329 sources[0] = fragment_source;
1330 sources[1] = fragment_brace;
1331 count = 2;
1332 }
1333
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001334 shader->fragment_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001335 compile_shader(GL_FRAGMENT_SHADER, count, sources);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001336
1337 shader->program = glCreateProgram();
1338 glAttachShader(shader->program, shader->vertex_shader);
1339 glAttachShader(shader->program, shader->fragment_shader);
1340 glBindAttribLocation(shader->program, 0, "position");
1341 glBindAttribLocation(shader->program, 1, "texcoord");
1342
1343 glLinkProgram(shader->program);
1344 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1345 if (!status) {
1346 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1347 weston_log("link info: %s\n", msg);
1348 return -1;
1349 }
1350
1351 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1352 shader->tex_uniforms[0] = glGetUniformLocation(shader->program, "tex");
1353 shader->tex_uniforms[1] = glGetUniformLocation(shader->program, "tex1");
1354 shader->tex_uniforms[2] = glGetUniformLocation(shader->program, "tex2");
1355 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
1356 shader->color_uniform = glGetUniformLocation(shader->program, "color");
1357
1358 return 0;
1359}
1360
1361static void
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001362weston_shader_release(struct weston_shader *shader)
1363{
1364 glDeleteShader(shader->vertex_shader);
1365 glDeleteShader(shader->fragment_shader);
1366 glDeleteProgram(shader->program);
1367
1368 shader->vertex_shader = 0;
1369 shader->fragment_shader = 0;
1370 shader->program = 0;
1371}
1372
1373static void
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001374log_extensions(const char *name, const char *extensions)
1375{
1376 const char *p, *end;
1377 int l;
1378 int len;
1379
1380 l = weston_log("%s:", name);
1381 p = extensions;
1382 while (*p) {
1383 end = strchrnul(p, ' ');
1384 len = end - p;
1385 if (l + len > 78)
1386 l = weston_log_continue("\n" STAMP_SPACE "%.*s",
1387 len, p);
1388 else
1389 l += weston_log_continue(" %.*s", len, p);
1390 for (p = end; isspace(*p); p++)
1391 ;
1392 }
1393 weston_log_continue("\n");
1394}
1395
1396static void
1397log_egl_gl_info(EGLDisplay egldpy)
1398{
1399 const char *str;
1400
1401 str = eglQueryString(egldpy, EGL_VERSION);
1402 weston_log("EGL version: %s\n", str ? str : "(null)");
1403
1404 str = eglQueryString(egldpy, EGL_VENDOR);
1405 weston_log("EGL vendor: %s\n", str ? str : "(null)");
1406
1407 str = eglQueryString(egldpy, EGL_CLIENT_APIS);
1408 weston_log("EGL client APIs: %s\n", str ? str : "(null)");
1409
1410 str = eglQueryString(egldpy, EGL_EXTENSIONS);
1411 log_extensions("EGL extensions", str ? str : "(null)");
1412
1413 str = (char *)glGetString(GL_VERSION);
1414 weston_log("GL version: %s\n", str ? str : "(null)");
1415
1416 str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
1417 weston_log("GLSL version: %s\n", str ? str : "(null)");
1418
1419 str = (char *)glGetString(GL_VENDOR);
1420 weston_log("GL vendor: %s\n", str ? str : "(null)");
1421
1422 str = (char *)glGetString(GL_RENDERER);
1423 weston_log("GL renderer: %s\n", str ? str : "(null)");
1424
1425 str = (char *)glGetString(GL_EXTENSIONS);
1426 log_extensions("GL extensions", str ? str : "(null)");
1427}
1428
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001429static void
1430log_egl_config_info(EGLDisplay egldpy, EGLConfig eglconfig)
1431{
1432 EGLint r, g, b, a;
1433
1434 weston_log("Chosen EGL config details:\n");
1435
1436 weston_log_continue(STAMP_SPACE "RGBA bits");
1437 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_RED_SIZE, &r) &&
1438 eglGetConfigAttrib(egldpy, eglconfig, EGL_GREEN_SIZE, &g) &&
1439 eglGetConfigAttrib(egldpy, eglconfig, EGL_BLUE_SIZE, &b) &&
1440 eglGetConfigAttrib(egldpy, eglconfig, EGL_ALPHA_SIZE, &a))
1441 weston_log_continue(": %d %d %d %d\n", r, g, b, a);
1442 else
1443 weston_log_continue(" unknown\n");
1444
1445 weston_log_continue(STAMP_SPACE "swap interval range");
1446 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_MIN_SWAP_INTERVAL, &a) &&
1447 eglGetConfigAttrib(egldpy, eglconfig, EGL_MAX_SWAP_INTERVAL, &b))
1448 weston_log_continue(": %d - %d\n", a, b);
1449 else
1450 weston_log_continue(" unknown\n");
1451}
1452
John Kåre Alsaker44154502012-11-13 19:10:20 +01001453static void
1454output_apply_border(struct weston_output *output, struct gles2_renderer *gr)
1455{
1456 output->border.top = gr->border.top;
1457 output->border.bottom = gr->border.bottom;
1458 output->border.left = gr->border.left;
1459 output->border.right = gr->border.right;
1460}
1461
1462WL_EXPORT void
1463gles2_renderer_set_border(struct weston_compositor *ec, int32_t width, int32_t height, void *data,
1464 int32_t *edges)
1465{
1466 struct gles2_renderer *gr = get_renderer(ec);
1467 struct weston_output *output;
1468
1469 gr->border.left = edges[0];
1470 gr->border.right = edges[1];
1471 gr->border.top = edges[2];
1472 gr->border.bottom = edges[3];
1473
1474 gr->border.width = width;
1475 gr->border.height = height;
1476
1477 glGenTextures(1, &gr->border.texture);
1478 glBindTexture(GL_TEXTURE_2D, gr->border.texture);
1479 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1480 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1481 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1482 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1483
1484 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1485 width,
1486 height,
1487 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1488 data);
1489
1490 wl_list_for_each(output, &ec->output_list, link)
1491 output_apply_border(output, gr);
1492}
1493
John Kåre Alsaker94659272012-11-13 19:10:18 +01001494static int
1495gles2_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface);
1496
1497WL_EXPORT int
1498gles2_renderer_output_create(struct weston_output *output,
1499 EGLNativeWindowType window)
1500{
1501 struct weston_compositor *ec = output->compositor;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001502 struct gles2_renderer *gr = get_renderer(ec);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001503 struct gles2_output_state *go = calloc(1, sizeof *go);
1504
1505 if (!go)
1506 return -1;
1507
1508 go->egl_surface =
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001509 eglCreateWindowSurface(gr->egl_display,
1510 gr->egl_config,
John Kåre Alsaker94659272012-11-13 19:10:18 +01001511 window, NULL);
1512
1513 if (go->egl_surface == EGL_NO_SURFACE) {
1514 weston_log("failed to create egl surface\n");
1515 free(go);
1516 return -1;
1517 }
1518
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001519 if (gr->egl_context == NULL)
John Kåre Alsaker94659272012-11-13 19:10:18 +01001520 if (gles2_renderer_setup(ec, go->egl_surface) < 0) {
1521 free(go);
1522 return -1;
1523 }
1524
1525 output->renderer_state = go;
1526
John Kåre Alsaker44154502012-11-13 19:10:20 +01001527 output_apply_border(output, gr);
1528
John Kåre Alsaker94659272012-11-13 19:10:18 +01001529 return 0;
1530}
1531
1532WL_EXPORT void
1533gles2_renderer_output_destroy(struct weston_output *output)
1534{
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001535 struct gles2_renderer *gr = get_renderer(output->compositor);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001536 struct gles2_output_state *go = get_output_state(output);
1537
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001538 eglDestroySurface(gr->egl_display, go->egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001539
1540 free(go);
1541}
1542
1543WL_EXPORT EGLSurface
1544gles2_renderer_output_surface(struct weston_output *output)
1545{
1546 return get_output_state(output)->egl_surface;
1547}
1548
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001549WL_EXPORT void
1550gles2_renderer_destroy(struct weston_compositor *ec)
1551{
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001552 struct gles2_renderer *gr = get_renderer(ec);
1553
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001554 if (ec->has_bind_display)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001555 ec->unbind_display(gr->egl_display, ec->wl_display);
1556
1557 /* Work around crash in egl_dri2.c's dri2_make_current() - when does this apply? */
1558 eglMakeCurrent(gr->egl_display,
1559 EGL_NO_SURFACE, EGL_NO_SURFACE,
1560 EGL_NO_CONTEXT);
1561
1562 eglTerminate(gr->egl_display);
1563 eglReleaseThread();
1564}
1565
1566static int
1567egl_choose_config(struct gles2_renderer *gr, const EGLint *attribs,
1568 const EGLint *visual_id)
1569{
1570 EGLint count = 0;
1571 EGLint matched = 0;
1572 EGLConfig *configs;
1573 int i;
1574
1575 if (!eglGetConfigs(gr->egl_display, NULL, 0, &count) || count < 1)
1576 return -1;
1577
1578 configs = calloc(count, sizeof *configs);
1579 if (!configs)
1580 return -1;
1581
1582 if (!eglChooseConfig(gr->egl_display, attribs, configs,
1583 count, &matched))
1584 goto out;
1585
1586 for (i = 0; i < matched; ++i) {
1587 EGLint id;
1588
1589 if (visual_id) {
1590 if (!eglGetConfigAttrib(gr->egl_display,
1591 configs[i], EGL_NATIVE_VISUAL_ID,
1592 &id))
1593 continue;
1594
1595 if (id != *visual_id)
1596 continue;
1597 }
1598
1599 gr->egl_config = configs[i];
1600
1601 free(configs);
1602 return 0;
1603 }
1604
1605out:
1606 free(configs);
1607 return -1;
1608}
1609
1610WL_EXPORT const EGLint gles2_renderer_opaque_attribs[] = {
1611 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1612 EGL_RED_SIZE, 1,
1613 EGL_GREEN_SIZE, 1,
1614 EGL_BLUE_SIZE, 1,
1615 EGL_ALPHA_SIZE, 0,
1616 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1617 EGL_NONE
1618};
1619
1620WL_EXPORT const EGLint gles2_renderer_alpha_attribs[] = {
1621 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1622 EGL_RED_SIZE, 1,
1623 EGL_GREEN_SIZE, 1,
1624 EGL_BLUE_SIZE, 1,
1625 EGL_ALPHA_SIZE, 1,
1626 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1627 EGL_NONE
1628};
1629
1630WL_EXPORT int
1631gles2_renderer_create(struct weston_compositor *ec, EGLNativeDisplayType display,
1632 const EGLint *attribs, const EGLint *visual_id)
1633{
1634 struct gles2_renderer *gr;
1635 EGLint major, minor;
1636
1637 gr = calloc(1, sizeof *gr);
1638
1639 if (gr == NULL)
1640 return -1;
1641
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001642 gr->base.read_pixels = gles2_renderer_read_pixels;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001643 gr->base.repaint_output = gles2_renderer_repaint_output;
1644 gr->base.flush_damage = gles2_renderer_flush_damage;
1645 gr->base.attach = gles2_renderer_attach;
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001646 gr->base.create_surface = gles2_renderer_create_surface;
1647 gr->base.surface_set_color = gles2_renderer_surface_set_color;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001648 gr->base.destroy_surface = gles2_renderer_destroy_surface;
1649
1650 gr->egl_display = eglGetDisplay(display);
1651 if (gr->egl_display == EGL_NO_DISPLAY) {
1652 weston_log("failed to create display\n");
1653 goto err_egl;
1654 }
1655
1656 if (!eglInitialize(gr->egl_display, &major, &minor)) {
1657 weston_log("failed to initialize display\n");
1658 goto err_egl;
1659 }
1660
1661 if (egl_choose_config(gr, attribs, visual_id) < 0) {
1662 weston_log("failed to choose EGL config\n");
1663 goto err_egl;
1664 }
1665
1666 ec->renderer = &gr->base;
1667
1668 return 0;
1669
1670err_egl:
1671 print_egl_error_state();
1672 free(gr);
1673 return -1;
1674}
1675
1676WL_EXPORT EGLDisplay
1677gles2_renderer_display(struct weston_compositor *ec)
1678{
1679 return get_renderer(ec)->egl_display;
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001680}
1681
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001682static int
1683compile_shaders(struct weston_compositor *ec)
1684{
1685 if (weston_shader_init(&ec->texture_shader_rgba, ec,
1686 vertex_shader, texture_fragment_shader_rgba) < 0)
1687 return -1;
1688 if (weston_shader_init(&ec->texture_shader_rgbx, ec,
1689 vertex_shader, texture_fragment_shader_rgbx) < 0)
1690 return -1;
1691 if (ec->has_egl_image_external &&
1692 weston_shader_init(&ec->texture_shader_egl_external, ec,
1693 vertex_shader, texture_fragment_shader_egl_external) < 0)
1694 return -1;
1695 if (weston_shader_init(&ec->texture_shader_y_uv, ec,
1696 vertex_shader, texture_fragment_shader_y_uv) < 0)
1697 return -1;
1698 if (weston_shader_init(&ec->texture_shader_y_u_v, ec,
1699 vertex_shader, texture_fragment_shader_y_u_v) < 0)
1700 return -1;
1701 if (weston_shader_init(&ec->texture_shader_y_xuxv, ec,
1702 vertex_shader, texture_fragment_shader_y_xuxv) < 0)
1703 return -1;
1704 if (weston_shader_init(&ec->solid_shader, ec,
1705 vertex_shader, solid_fragment_shader) < 0)
1706 return -1;
1707
1708 return 0;
1709}
1710
1711static void
1712fragment_debug_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
1713 void *data)
1714{
1715 struct weston_compositor *ec = data;
1716 struct gles2_renderer *renderer =
1717 (struct gles2_renderer *) ec->renderer;
1718 struct weston_output *output;
1719
1720 renderer->fragment_shader_debug ^= 1;
1721
1722 weston_shader_release(&ec->texture_shader_rgba);
1723 weston_shader_release(&ec->texture_shader_rgbx);
1724 weston_shader_release(&ec->texture_shader_egl_external);
1725 weston_shader_release(&ec->texture_shader_y_uv);
1726 weston_shader_release(&ec->texture_shader_y_u_v);
1727 weston_shader_release(&ec->texture_shader_y_xuxv);
1728 weston_shader_release(&ec->solid_shader);
1729
1730 compile_shaders(ec);
1731
1732 wl_list_for_each(output, &ec->output_list, link)
1733 weston_output_damage(output);
1734}
1735
John Kåre Alsaker94659272012-11-13 19:10:18 +01001736static int
1737gles2_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001738{
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001739 struct gles2_renderer *gr = get_renderer(ec);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001740 const char *extensions;
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001741 EGLBoolean ret;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001742
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001743 static const EGLint context_attribs[] = {
1744 EGL_CONTEXT_CLIENT_VERSION, 2,
1745 EGL_NONE
1746 };
1747
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001748 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
1749 weston_log("failed to bind EGL_OPENGL_ES_API\n");
1750 print_egl_error_state();
1751 return -1;
1752 }
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001753
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001754 log_egl_config_info(gr->egl_display, gr->egl_config);
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001755
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001756 gr->egl_context = eglCreateContext(gr->egl_display, gr->egl_config,
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001757 EGL_NO_CONTEXT, context_attribs);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001758 if (gr->egl_context == NULL) {
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001759 weston_log("failed to create context\n");
1760 print_egl_error_state();
1761 return -1;
1762 }
1763
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001764 ret = eglMakeCurrent(gr->egl_display, egl_surface,
1765 egl_surface, gr->egl_context);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001766 if (ret == EGL_FALSE) {
1767 weston_log("Failed to make EGL context current.\n");
1768 print_egl_error_state();
1769 return -1;
1770 }
1771
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001772 log_egl_gl_info(gr->egl_display);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001773
1774 ec->image_target_texture_2d =
1775 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
1776 ec->image_target_renderbuffer_storage = (void *)
1777 eglGetProcAddress("glEGLImageTargetRenderbufferStorageOES");
1778 ec->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1779 ec->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1780 ec->bind_display =
1781 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
1782 ec->unbind_display =
1783 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
1784 ec->query_buffer =
1785 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
1786
1787 extensions = (const char *) glGetString(GL_EXTENSIONS);
1788 if (!extensions) {
1789 weston_log("Retrieving GL extension string failed.\n");
1790 return -1;
1791 }
1792
1793 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
1794 weston_log("GL_EXT_texture_format_BGRA8888 not available\n");
1795 return -1;
1796 }
1797
1798 if (strstr(extensions, "GL_EXT_read_format_bgra"))
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01001799 ec->read_format = PIXMAN_a8r8g8b8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001800 else
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01001801 ec->read_format = PIXMAN_a8b8g8r8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001802
1803 if (strstr(extensions, "GL_EXT_unpack_subimage"))
1804 ec->has_unpack_subimage = 1;
1805
1806 if (strstr(extensions, "GL_OES_EGL_image_external"))
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001807 ec->has_egl_image_external = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001808
1809 extensions =
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001810 (const char *) eglQueryString(gr->egl_display, EGL_EXTENSIONS);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001811 if (!extensions) {
1812 weston_log("Retrieving EGL extension string failed.\n");
1813 return -1;
1814 }
1815
1816 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
1817 ec->has_bind_display = 1;
Pekka Paalanen035a0322012-10-24 09:43:06 +03001818 if (ec->has_bind_display) {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001819 ret = ec->bind_display(gr->egl_display, ec->wl_display);
Pekka Paalanen035a0322012-10-24 09:43:06 +03001820 if (!ret)
1821 ec->has_bind_display = 0;
1822 }
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001823
1824 glActiveTexture(GL_TEXTURE0);
1825
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001826 if (compile_shaders(ec))
1827 return -1;
1828
1829 weston_compositor_add_debug_binding(ec, KEY_S,
1830 fragment_debug_binding, ec);
1831
Pekka Paalanen035a0322012-10-24 09:43:06 +03001832 weston_log("GL ES 2 renderer features:\n");
1833 weston_log_continue(STAMP_SPACE "read-back format: %s\n",
1834 ec->read_format == GL_BGRA_EXT ? "BGRA" : "RGBA");
1835 weston_log_continue(STAMP_SPACE "wl_shm sub-image to texture: %s\n",
1836 ec->has_unpack_subimage ? "yes" : "no");
1837 weston_log_continue(STAMP_SPACE "EGL Wayland extension: %s\n",
1838 ec->has_bind_display ? "yes" : "no");
1839
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001840
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001841 return 0;
1842}