blob: 20a34b3e0e71ffc9e0a583063f47fcab59f8efb0 [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
John Kåre Alsakerd6159802012-11-13 19:10:24 +0100645use_shader(struct weston_compositor *compositor,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400646 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
John Kåre Alsakerd6159802012-11-13 19:10:24 +0100656shader_uniforms(struct weston_shader *shader,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400657 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) {
John Kåre Alsakerd6159802012-11-13 19:10:24 +0100699 use_shader(ec, &ec->solid_shader);
700 shader_uniforms(&ec->solid_shader, es, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400701 }
702
John Kåre Alsakerd6159802012-11-13 19:10:24 +0100703 use_shader(ec, es->shader);
704 shader_uniforms(es->shader, es, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400705
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 */
John Kåre Alsakerd6159802012-11-13 19:10:24 +0100730 use_shader(ec, &ec->texture_shader_rgbx);
731 shader_uniforms(&ec->texture_shader_rgbx, es, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400732 }
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)) {
John Kåre Alsakerd6159802012-11-13 19:10:24 +0100743 use_shader(ec, es->shader);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400744 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);
John Kåre Alsakerd6159802012-11-13 19:10:24 +0100852 use_shader(ec, shader);
John Kåre Alsaker44154502012-11-13 19:10:20 +0100853
854 glUniformMatrix4fv(shader->proj_uniform,
855 1, GL_FALSE, output->matrix.d);
856
857 glUniform1i(shader->tex_uniforms[0], 0);
858 glUniform1f(shader->alpha_uniform, 1);
859
860 n = texture_border(output);
861
862 glActiveTexture(GL_TEXTURE0);
863 glBindTexture(GL_TEXTURE_2D, gr->border.texture);
864
865 v = ec->vertices.data;
866 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
867 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
868 glEnableVertexAttribArray(0);
869 glEnableVertexAttribArray(1);
870
871 glDrawElements(GL_TRIANGLES, n * 6,
872 GL_UNSIGNED_INT, ec->indices.data);
873
874 glDisableVertexAttribArray(1);
875 glDisableVertexAttribArray(0);
876
877 ec->vertices.size = 0;
878 ec->indices.size = 0;
879}
880
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400881static void
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400882gles2_renderer_repaint_output(struct weston_output *output,
883 pixman_region32_t *output_damage)
884{
John Kåre Alsaker94659272012-11-13 19:10:18 +0100885 struct gles2_output_state *go = get_output_state(output);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400886 struct weston_compositor *compositor = output->compositor;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100887 struct gles2_renderer *gr = get_renderer(compositor);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400888 EGLBoolean ret;
889 static int errored;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300890 int32_t width, height, i;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400891
892 width = output->current->width +
893 output->border.left + output->border.right;
894 height = output->current->height +
895 output->border.top + output->border.bottom;
896
897 glViewport(0, 0, width, height);
898
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100899 if (use_output(output) < 0)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400900 return;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400901
902 /* if debugging, redraw everything outside the damage to clean up
903 * debug lines from the previous draw on this buffer:
904 */
905 if (compositor->fan_debug) {
906 pixman_region32_t undamaged;
907 pixman_region32_init(&undamaged);
908 pixman_region32_subtract(&undamaged, &output->region,
909 output_damage);
910 compositor->fan_debug = 0;
911 repaint_surfaces(output, &undamaged);
912 compositor->fan_debug = 1;
913 pixman_region32_fini(&undamaged);
914 }
915
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300916 for (i = 0; i < 2; i++)
917 pixman_region32_union(&output->buffer_damage[i],
918 &output->buffer_damage[i],
919 output_damage);
920
921 pixman_region32_union(output_damage, output_damage,
922 &output->buffer_damage[output->current_buffer]);
923
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400924 repaint_surfaces(output, output_damage);
925
John Kåre Alsaker44154502012-11-13 19:10:20 +0100926 if (gr->border.texture)
927 draw_border(output);
928
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400929 wl_signal_emit(&output->frame_signal, output);
930
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100931 ret = eglSwapBuffers(gr->egl_display, go->egl_surface);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400932 if (ret == EGL_FALSE && !errored) {
933 errored = 1;
934 weston_log("Failed in eglSwapBuffers.\n");
935 print_egl_error_state();
936 }
937
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300938 output->current_buffer ^= 1;
939
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400940}
Kristian Høgsberg25894fc2012-09-05 22:06:26 -0400941
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100942static int
943gles2_renderer_read_pixels(struct weston_output *output,
944 pixman_format_code_t format, void *pixels,
945 uint32_t x, uint32_t y,
946 uint32_t width, uint32_t height)
947{
948 GLenum gl_format;
949
950 switch (format) {
951 case PIXMAN_a8r8g8b8:
952 gl_format = GL_BGRA_EXT;
953 break;
954 case PIXMAN_a8b8g8r8:
955 gl_format = GL_RGBA;
956 break;
957 default:
958 return -1;
959 }
960
961 if (use_output(output) < 0)
962 return -1;
963
964 glPixelStorei(GL_PACK_ALIGNMENT, 1);
965 glReadPixels(x, y, width, height, gl_format,
966 GL_UNSIGNED_BYTE, pixels);
967
968 return 0;
969}
970
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400971static void
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -0400972gles2_renderer_flush_damage(struct weston_surface *surface)
973{
974#ifdef GL_UNPACK_ROW_LENGTH
975 pixman_box32_t *rectangles;
976 void *data;
977 int i, n;
978#endif
979
Pekka Paalanenbcdd5792012-11-07 12:25:13 +0200980 pixman_region32_union(&surface->texture_damage,
981 &surface->texture_damage, &surface->damage);
982
983 /* Avoid upload, if the texture won't be used this time.
984 * We still accumulate the damage in texture_damage.
985 */
986 if (surface->plane != &surface->compositor->primary_plane)
987 return;
988
989 if (!pixman_region32_not_empty(&surface->texture_damage))
990 return;
991
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -0400992 glBindTexture(GL_TEXTURE_2D, surface->textures[0]);
993
994 if (!surface->compositor->has_unpack_subimage) {
995 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
996 surface->pitch, surface->buffer->height, 0,
997 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
998 wl_shm_buffer_get_data(surface->buffer));
999
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001000 goto done;
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001001 }
1002
1003#ifdef GL_UNPACK_ROW_LENGTH
1004 /* Mesa does not define GL_EXT_unpack_subimage */
1005 glPixelStorei(GL_UNPACK_ROW_LENGTH, surface->pitch);
1006 data = wl_shm_buffer_get_data(surface->buffer);
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001007 rectangles = pixman_region32_rectangles(&surface->texture_damage, &n);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001008 for (i = 0; i < n; i++) {
1009 glPixelStorei(GL_UNPACK_SKIP_PIXELS, rectangles[i].x1);
1010 glPixelStorei(GL_UNPACK_SKIP_ROWS, rectangles[i].y1);
1011 glTexSubImage2D(GL_TEXTURE_2D, 0,
1012 rectangles[i].x1, rectangles[i].y1,
1013 rectangles[i].x2 - rectangles[i].x1,
1014 rectangles[i].y2 - rectangles[i].y1,
1015 GL_BGRA_EXT, GL_UNSIGNED_BYTE, data);
1016 }
1017#endif
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001018
1019done:
1020 pixman_region32_fini(&surface->texture_damage);
1021 pixman_region32_init(&surface->texture_damage);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001022}
1023
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001024static void
1025ensure_textures(struct weston_surface *es, int num_textures)
1026{
1027 int i;
1028
1029 if (num_textures <= es->num_textures)
1030 return;
1031
1032 for (i = es->num_textures; i < num_textures; i++) {
1033 glGenTextures(1, &es->textures[i]);
1034 glBindTexture(es->target, es->textures[i]);
1035 glTexParameteri(es->target,
1036 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1037 glTexParameteri(es->target,
1038 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1039 }
1040 es->num_textures = num_textures;
1041 glBindTexture(es->target, 0);
1042}
1043
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001044static void
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001045gles2_renderer_attach(struct weston_surface *es, struct wl_buffer *buffer)
1046{
1047 struct weston_compositor *ec = es->compositor;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001048 struct gles2_renderer *gr = get_renderer(ec);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001049 EGLint attribs[3], format;
1050 int i, num_planes;
1051
1052 if (!buffer) {
1053 for (i = 0; i < es->num_images; i++) {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001054 ec->destroy_image(gr->egl_display, es->images[i]);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001055 es->images[i] = NULL;
1056 }
1057 es->num_images = 0;
1058 glDeleteTextures(es->num_textures, es->textures);
1059 es->num_textures = 0;
1060 return;
1061 }
1062
1063 if (wl_buffer_is_shm(buffer)) {
1064 es->pitch = wl_shm_buffer_get_stride(buffer) / 4;
1065 es->target = GL_TEXTURE_2D;
1066
1067 ensure_textures(es, 1);
1068 glBindTexture(GL_TEXTURE_2D, es->textures[0]);
1069 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1070 es->pitch, buffer->height, 0,
1071 GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
1072 if (wl_shm_buffer_get_format(buffer) == WL_SHM_FORMAT_XRGB8888)
1073 es->shader = &ec->texture_shader_rgbx;
1074 else
1075 es->shader = &ec->texture_shader_rgba;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001076 } else if (ec->query_buffer(gr->egl_display, buffer,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001077 EGL_TEXTURE_FORMAT, &format)) {
1078 for (i = 0; i < es->num_images; i++)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001079 ec->destroy_image(gr->egl_display, es->images[i]);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001080 es->num_images = 0;
1081 es->target = GL_TEXTURE_2D;
1082 switch (format) {
1083 case EGL_TEXTURE_RGB:
1084 case EGL_TEXTURE_RGBA:
1085 default:
1086 num_planes = 1;
1087 es->shader = &ec->texture_shader_rgba;
1088 break;
1089 case EGL_TEXTURE_EXTERNAL_WL:
1090 num_planes = 1;
1091 es->target = GL_TEXTURE_EXTERNAL_OES;
1092 es->shader = &ec->texture_shader_egl_external;
1093 break;
1094 case EGL_TEXTURE_Y_UV_WL:
1095 num_planes = 2;
1096 es->shader = &ec->texture_shader_y_uv;
1097 break;
1098 case EGL_TEXTURE_Y_U_V_WL:
1099 num_planes = 3;
1100 es->shader = &ec->texture_shader_y_u_v;
1101 break;
1102 case EGL_TEXTURE_Y_XUXV_WL:
1103 num_planes = 2;
1104 es->shader = &ec->texture_shader_y_xuxv;
1105 break;
1106 }
1107
1108 ensure_textures(es, num_planes);
1109 for (i = 0; i < num_planes; i++) {
1110 attribs[0] = EGL_WAYLAND_PLANE_WL;
1111 attribs[1] = i;
1112 attribs[2] = EGL_NONE;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001113 es->images[i] = ec->create_image(gr->egl_display,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001114 NULL,
1115 EGL_WAYLAND_BUFFER_WL,
1116 buffer, attribs);
1117 if (!es->images[i]) {
1118 weston_log("failed to create img for plane %d\n", i);
1119 continue;
1120 }
1121 es->num_images++;
1122
1123 glActiveTexture(GL_TEXTURE0 + i);
1124 glBindTexture(es->target, es->textures[i]);
1125 ec->image_target_texture_2d(es->target,
1126 es->images[i]);
1127 }
1128
1129 es->pitch = buffer->width;
1130 } else {
1131 weston_log("unhandled buffer type!\n");
1132 }
1133}
1134
Kristian Høgsberg42263852012-09-06 21:59:29 -04001135static void
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001136gles2_renderer_surface_set_color(struct weston_surface *surface,
1137 float red, float green, float blue, float alpha)
1138{
1139 struct gles2_surface_state *gs = get_surface_state(surface);
1140
1141 gs->color[0] = red;
1142 gs->color[1] = green;
1143 gs->color[2] = blue;
1144 gs->color[3] = alpha;
1145
1146 surface->shader = &surface->compositor->solid_shader;
1147}
1148
1149static int
1150gles2_renderer_create_surface(struct weston_surface *surface)
1151{
1152 struct gles2_surface_state *gs;
1153
1154 gs = calloc(1, sizeof *gs);
1155
1156 if (!gs)
1157 return -1;
1158
1159 surface->renderer_state = gs;
1160
1161 return 0;
1162}
1163
1164static void
Kristian Høgsberg42263852012-09-06 21:59:29 -04001165gles2_renderer_destroy_surface(struct weston_surface *surface)
1166{
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001167 struct gles2_surface_state *gs = get_surface_state(surface);
Kristian Høgsberg42263852012-09-06 21:59:29 -04001168 struct weston_compositor *ec = surface->compositor;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001169 struct gles2_renderer *gr = get_renderer(ec);
Kristian Høgsberg42263852012-09-06 21:59:29 -04001170 int i;
1171
1172 glDeleteTextures(surface->num_textures, surface->textures);
1173
1174 for (i = 0; i < surface->num_images; i++)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001175 ec->destroy_image(gr->egl_display, surface->images[i]);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001176
1177 free(gs);
Kristian Høgsberg42263852012-09-06 21:59:29 -04001178}
1179
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001180static const char vertex_shader[] =
1181 "uniform mat4 proj;\n"
1182 "attribute vec2 position;\n"
1183 "attribute vec2 texcoord;\n"
1184 "varying vec2 v_texcoord;\n"
1185 "void main()\n"
1186 "{\n"
1187 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
1188 " v_texcoord = texcoord;\n"
1189 "}\n";
1190
1191/* Declare common fragment shader uniforms */
1192#define FRAGMENT_CONVERT_YUV \
1193 " y *= alpha;\n" \
1194 " u *= alpha;\n" \
1195 " v *= alpha;\n" \
1196 " gl_FragColor.r = y + 1.59602678 * v;\n" \
1197 " gl_FragColor.g = y - 0.39176229 * u - 0.81296764 * v;\n" \
1198 " gl_FragColor.b = y + 2.01723214 * u;\n" \
1199 " gl_FragColor.a = alpha;\n"
1200
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001201static const char fragment_debug[] =
1202 " gl_FragColor = vec4(0.0, 0.3, 0.0, 0.2) + gl_FragColor * 0.8;\n";
1203
1204static const char fragment_brace[] =
1205 "}\n";
1206
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001207static const char texture_fragment_shader_rgba[] =
1208 "precision mediump float;\n"
1209 "varying vec2 v_texcoord;\n"
1210 "uniform sampler2D tex;\n"
1211 "uniform float alpha;\n"
1212 "void main()\n"
1213 "{\n"
1214 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001215 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001216
1217static const char texture_fragment_shader_rgbx[] =
1218 "precision mediump float;\n"
1219 "varying vec2 v_texcoord;\n"
1220 "uniform sampler2D tex;\n"
1221 "uniform float alpha;\n"
1222 "void main()\n"
1223 "{\n"
1224 " gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb\n;"
1225 " gl_FragColor.a = alpha;\n"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001226 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001227
1228static const char texture_fragment_shader_egl_external[] =
1229 "#extension GL_OES_EGL_image_external : require\n"
1230 "precision mediump float;\n"
1231 "varying vec2 v_texcoord;\n"
1232 "uniform samplerExternalOES tex;\n"
1233 "uniform float alpha;\n"
1234 "void main()\n"
1235 "{\n"
1236 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001237 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001238
1239static const char texture_fragment_shader_y_uv[] =
1240 "precision mediump float;\n"
1241 "uniform sampler2D tex;\n"
1242 "uniform sampler2D tex1;\n"
1243 "varying vec2 v_texcoord;\n"
1244 "uniform float alpha;\n"
1245 "void main() {\n"
1246 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1247 " float u = texture2D(tex1, v_texcoord).r - 0.5;\n"
1248 " float v = texture2D(tex1, v_texcoord).g - 0.5;\n"
1249 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001250 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001251
1252static const char texture_fragment_shader_y_u_v[] =
1253 "precision mediump float;\n"
1254 "uniform sampler2D tex;\n"
1255 "uniform sampler2D tex1;\n"
1256 "uniform sampler2D tex2;\n"
1257 "varying vec2 v_texcoord;\n"
1258 "uniform float alpha;\n"
1259 "void main() {\n"
1260 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1261 " float u = texture2D(tex1, v_texcoord).x - 0.5;\n"
1262 " float v = texture2D(tex2, v_texcoord).x - 0.5;\n"
1263 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001264 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001265
1266static const char texture_fragment_shader_y_xuxv[] =
1267 "precision mediump float;\n"
1268 "uniform sampler2D tex;\n"
1269 "uniform sampler2D tex1;\n"
1270 "varying vec2 v_texcoord;\n"
1271 "uniform float alpha;\n"
1272 "void main() {\n"
1273 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1274 " float u = texture2D(tex1, v_texcoord).g - 0.5;\n"
1275 " float v = texture2D(tex1, v_texcoord).a - 0.5;\n"
1276 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001277 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001278
1279static const char solid_fragment_shader[] =
1280 "precision mediump float;\n"
1281 "uniform vec4 color;\n"
1282 "uniform float alpha;\n"
1283 "void main()\n"
1284 "{\n"
1285 " gl_FragColor = alpha * color\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001286 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001287
1288static int
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001289compile_shader(GLenum type, int count, const char **sources)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001290{
1291 GLuint s;
1292 char msg[512];
1293 GLint status;
1294
1295 s = glCreateShader(type);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001296 glShaderSource(s, count, sources, NULL);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001297 glCompileShader(s);
1298 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
1299 if (!status) {
1300 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1301 weston_log("shader info: %s\n", msg);
1302 return GL_NONE;
1303 }
1304
1305 return s;
1306}
1307
1308static int
John Kåre Alsakerd6159802012-11-13 19:10:24 +01001309shader_init(struct weston_shader *shader, struct weston_compositor *ec,
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001310 const char *vertex_source, const char *fragment_source)
1311{
1312 char msg[512];
1313 GLint status;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001314 int count;
1315 const char *sources[3];
1316 struct gles2_renderer *renderer =
1317 (struct gles2_renderer *) ec->renderer;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001318
1319 shader->vertex_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001320 compile_shader(GL_VERTEX_SHADER, 1, &vertex_source);
1321
1322 if (renderer->fragment_shader_debug) {
1323 sources[0] = fragment_source;
1324 sources[1] = fragment_debug;
1325 sources[2] = fragment_brace;
1326 count = 3;
1327 } else {
1328 sources[0] = fragment_source;
1329 sources[1] = fragment_brace;
1330 count = 2;
1331 }
1332
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001333 shader->fragment_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001334 compile_shader(GL_FRAGMENT_SHADER, count, sources);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001335
1336 shader->program = glCreateProgram();
1337 glAttachShader(shader->program, shader->vertex_shader);
1338 glAttachShader(shader->program, shader->fragment_shader);
1339 glBindAttribLocation(shader->program, 0, "position");
1340 glBindAttribLocation(shader->program, 1, "texcoord");
1341
1342 glLinkProgram(shader->program);
1343 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1344 if (!status) {
1345 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1346 weston_log("link info: %s\n", msg);
1347 return -1;
1348 }
1349
1350 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1351 shader->tex_uniforms[0] = glGetUniformLocation(shader->program, "tex");
1352 shader->tex_uniforms[1] = glGetUniformLocation(shader->program, "tex1");
1353 shader->tex_uniforms[2] = glGetUniformLocation(shader->program, "tex2");
1354 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
1355 shader->color_uniform = glGetUniformLocation(shader->program, "color");
1356
1357 return 0;
1358}
1359
1360static void
John Kåre Alsakerd6159802012-11-13 19:10:24 +01001361shader_release(struct weston_shader *shader)
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001362{
1363 glDeleteShader(shader->vertex_shader);
1364 glDeleteShader(shader->fragment_shader);
1365 glDeleteProgram(shader->program);
1366
1367 shader->vertex_shader = 0;
1368 shader->fragment_shader = 0;
1369 shader->program = 0;
1370}
1371
1372static void
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001373log_extensions(const char *name, const char *extensions)
1374{
1375 const char *p, *end;
1376 int l;
1377 int len;
1378
1379 l = weston_log("%s:", name);
1380 p = extensions;
1381 while (*p) {
1382 end = strchrnul(p, ' ');
1383 len = end - p;
1384 if (l + len > 78)
1385 l = weston_log_continue("\n" STAMP_SPACE "%.*s",
1386 len, p);
1387 else
1388 l += weston_log_continue(" %.*s", len, p);
1389 for (p = end; isspace(*p); p++)
1390 ;
1391 }
1392 weston_log_continue("\n");
1393}
1394
1395static void
1396log_egl_gl_info(EGLDisplay egldpy)
1397{
1398 const char *str;
1399
1400 str = eglQueryString(egldpy, EGL_VERSION);
1401 weston_log("EGL version: %s\n", str ? str : "(null)");
1402
1403 str = eglQueryString(egldpy, EGL_VENDOR);
1404 weston_log("EGL vendor: %s\n", str ? str : "(null)");
1405
1406 str = eglQueryString(egldpy, EGL_CLIENT_APIS);
1407 weston_log("EGL client APIs: %s\n", str ? str : "(null)");
1408
1409 str = eglQueryString(egldpy, EGL_EXTENSIONS);
1410 log_extensions("EGL extensions", str ? str : "(null)");
1411
1412 str = (char *)glGetString(GL_VERSION);
1413 weston_log("GL version: %s\n", str ? str : "(null)");
1414
1415 str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
1416 weston_log("GLSL version: %s\n", str ? str : "(null)");
1417
1418 str = (char *)glGetString(GL_VENDOR);
1419 weston_log("GL vendor: %s\n", str ? str : "(null)");
1420
1421 str = (char *)glGetString(GL_RENDERER);
1422 weston_log("GL renderer: %s\n", str ? str : "(null)");
1423
1424 str = (char *)glGetString(GL_EXTENSIONS);
1425 log_extensions("GL extensions", str ? str : "(null)");
1426}
1427
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001428static void
1429log_egl_config_info(EGLDisplay egldpy, EGLConfig eglconfig)
1430{
1431 EGLint r, g, b, a;
1432
1433 weston_log("Chosen EGL config details:\n");
1434
1435 weston_log_continue(STAMP_SPACE "RGBA bits");
1436 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_RED_SIZE, &r) &&
1437 eglGetConfigAttrib(egldpy, eglconfig, EGL_GREEN_SIZE, &g) &&
1438 eglGetConfigAttrib(egldpy, eglconfig, EGL_BLUE_SIZE, &b) &&
1439 eglGetConfigAttrib(egldpy, eglconfig, EGL_ALPHA_SIZE, &a))
1440 weston_log_continue(": %d %d %d %d\n", r, g, b, a);
1441 else
1442 weston_log_continue(" unknown\n");
1443
1444 weston_log_continue(STAMP_SPACE "swap interval range");
1445 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_MIN_SWAP_INTERVAL, &a) &&
1446 eglGetConfigAttrib(egldpy, eglconfig, EGL_MAX_SWAP_INTERVAL, &b))
1447 weston_log_continue(": %d - %d\n", a, b);
1448 else
1449 weston_log_continue(" unknown\n");
1450}
1451
John Kåre Alsaker44154502012-11-13 19:10:20 +01001452static void
1453output_apply_border(struct weston_output *output, struct gles2_renderer *gr)
1454{
1455 output->border.top = gr->border.top;
1456 output->border.bottom = gr->border.bottom;
1457 output->border.left = gr->border.left;
1458 output->border.right = gr->border.right;
1459}
1460
1461WL_EXPORT void
1462gles2_renderer_set_border(struct weston_compositor *ec, int32_t width, int32_t height, void *data,
1463 int32_t *edges)
1464{
1465 struct gles2_renderer *gr = get_renderer(ec);
1466 struct weston_output *output;
1467
1468 gr->border.left = edges[0];
1469 gr->border.right = edges[1];
1470 gr->border.top = edges[2];
1471 gr->border.bottom = edges[3];
1472
1473 gr->border.width = width;
1474 gr->border.height = height;
1475
1476 glGenTextures(1, &gr->border.texture);
1477 glBindTexture(GL_TEXTURE_2D, gr->border.texture);
1478 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1479 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1480 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1481 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1482
1483 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1484 width,
1485 height,
1486 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1487 data);
1488
1489 wl_list_for_each(output, &ec->output_list, link)
1490 output_apply_border(output, gr);
1491}
1492
John Kåre Alsaker94659272012-11-13 19:10:18 +01001493static int
1494gles2_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface);
1495
1496WL_EXPORT int
1497gles2_renderer_output_create(struct weston_output *output,
1498 EGLNativeWindowType window)
1499{
1500 struct weston_compositor *ec = output->compositor;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001501 struct gles2_renderer *gr = get_renderer(ec);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001502 struct gles2_output_state *go = calloc(1, sizeof *go);
1503
1504 if (!go)
1505 return -1;
1506
1507 go->egl_surface =
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001508 eglCreateWindowSurface(gr->egl_display,
1509 gr->egl_config,
John Kåre Alsaker94659272012-11-13 19:10:18 +01001510 window, NULL);
1511
1512 if (go->egl_surface == EGL_NO_SURFACE) {
1513 weston_log("failed to create egl surface\n");
1514 free(go);
1515 return -1;
1516 }
1517
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001518 if (gr->egl_context == NULL)
John Kåre Alsaker94659272012-11-13 19:10:18 +01001519 if (gles2_renderer_setup(ec, go->egl_surface) < 0) {
1520 free(go);
1521 return -1;
1522 }
1523
1524 output->renderer_state = go;
1525
John Kåre Alsaker44154502012-11-13 19:10:20 +01001526 output_apply_border(output, gr);
1527
John Kåre Alsaker94659272012-11-13 19:10:18 +01001528 return 0;
1529}
1530
1531WL_EXPORT void
1532gles2_renderer_output_destroy(struct weston_output *output)
1533{
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001534 struct gles2_renderer *gr = get_renderer(output->compositor);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001535 struct gles2_output_state *go = get_output_state(output);
1536
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001537 eglDestroySurface(gr->egl_display, go->egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001538
1539 free(go);
1540}
1541
1542WL_EXPORT EGLSurface
1543gles2_renderer_output_surface(struct weston_output *output)
1544{
1545 return get_output_state(output)->egl_surface;
1546}
1547
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001548WL_EXPORT void
1549gles2_renderer_destroy(struct weston_compositor *ec)
1550{
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001551 struct gles2_renderer *gr = get_renderer(ec);
1552
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001553 if (ec->has_bind_display)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001554 ec->unbind_display(gr->egl_display, ec->wl_display);
1555
1556 /* Work around crash in egl_dri2.c's dri2_make_current() - when does this apply? */
1557 eglMakeCurrent(gr->egl_display,
1558 EGL_NO_SURFACE, EGL_NO_SURFACE,
1559 EGL_NO_CONTEXT);
1560
1561 eglTerminate(gr->egl_display);
1562 eglReleaseThread();
1563}
1564
1565static int
1566egl_choose_config(struct gles2_renderer *gr, const EGLint *attribs,
1567 const EGLint *visual_id)
1568{
1569 EGLint count = 0;
1570 EGLint matched = 0;
1571 EGLConfig *configs;
1572 int i;
1573
1574 if (!eglGetConfigs(gr->egl_display, NULL, 0, &count) || count < 1)
1575 return -1;
1576
1577 configs = calloc(count, sizeof *configs);
1578 if (!configs)
1579 return -1;
1580
1581 if (!eglChooseConfig(gr->egl_display, attribs, configs,
1582 count, &matched))
1583 goto out;
1584
1585 for (i = 0; i < matched; ++i) {
1586 EGLint id;
1587
1588 if (visual_id) {
1589 if (!eglGetConfigAttrib(gr->egl_display,
1590 configs[i], EGL_NATIVE_VISUAL_ID,
1591 &id))
1592 continue;
1593
1594 if (id != *visual_id)
1595 continue;
1596 }
1597
1598 gr->egl_config = configs[i];
1599
1600 free(configs);
1601 return 0;
1602 }
1603
1604out:
1605 free(configs);
1606 return -1;
1607}
1608
1609WL_EXPORT const EGLint gles2_renderer_opaque_attribs[] = {
1610 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1611 EGL_RED_SIZE, 1,
1612 EGL_GREEN_SIZE, 1,
1613 EGL_BLUE_SIZE, 1,
1614 EGL_ALPHA_SIZE, 0,
1615 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1616 EGL_NONE
1617};
1618
1619WL_EXPORT const EGLint gles2_renderer_alpha_attribs[] = {
1620 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1621 EGL_RED_SIZE, 1,
1622 EGL_GREEN_SIZE, 1,
1623 EGL_BLUE_SIZE, 1,
1624 EGL_ALPHA_SIZE, 1,
1625 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1626 EGL_NONE
1627};
1628
1629WL_EXPORT int
1630gles2_renderer_create(struct weston_compositor *ec, EGLNativeDisplayType display,
1631 const EGLint *attribs, const EGLint *visual_id)
1632{
1633 struct gles2_renderer *gr;
1634 EGLint major, minor;
1635
1636 gr = calloc(1, sizeof *gr);
1637
1638 if (gr == NULL)
1639 return -1;
1640
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001641 gr->base.read_pixels = gles2_renderer_read_pixels;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001642 gr->base.repaint_output = gles2_renderer_repaint_output;
1643 gr->base.flush_damage = gles2_renderer_flush_damage;
1644 gr->base.attach = gles2_renderer_attach;
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001645 gr->base.create_surface = gles2_renderer_create_surface;
1646 gr->base.surface_set_color = gles2_renderer_surface_set_color;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001647 gr->base.destroy_surface = gles2_renderer_destroy_surface;
1648
1649 gr->egl_display = eglGetDisplay(display);
1650 if (gr->egl_display == EGL_NO_DISPLAY) {
1651 weston_log("failed to create display\n");
1652 goto err_egl;
1653 }
1654
1655 if (!eglInitialize(gr->egl_display, &major, &minor)) {
1656 weston_log("failed to initialize display\n");
1657 goto err_egl;
1658 }
1659
1660 if (egl_choose_config(gr, attribs, visual_id) < 0) {
1661 weston_log("failed to choose EGL config\n");
1662 goto err_egl;
1663 }
1664
1665 ec->renderer = &gr->base;
1666
1667 return 0;
1668
1669err_egl:
1670 print_egl_error_state();
1671 free(gr);
1672 return -1;
1673}
1674
1675WL_EXPORT EGLDisplay
1676gles2_renderer_display(struct weston_compositor *ec)
1677{
1678 return get_renderer(ec)->egl_display;
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001679}
1680
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001681static int
1682compile_shaders(struct weston_compositor *ec)
1683{
John Kåre Alsakerd6159802012-11-13 19:10:24 +01001684 if (shader_init(&ec->texture_shader_rgba, ec,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001685 vertex_shader, texture_fragment_shader_rgba) < 0)
1686 return -1;
John Kåre Alsakerd6159802012-11-13 19:10:24 +01001687 if (shader_init(&ec->texture_shader_rgbx, ec,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001688 vertex_shader, texture_fragment_shader_rgbx) < 0)
1689 return -1;
1690 if (ec->has_egl_image_external &&
John Kåre Alsakerd6159802012-11-13 19:10:24 +01001691 shader_init(&ec->texture_shader_egl_external, ec,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001692 vertex_shader, texture_fragment_shader_egl_external) < 0)
1693 return -1;
John Kåre Alsakerd6159802012-11-13 19:10:24 +01001694 if (shader_init(&ec->texture_shader_y_uv, ec,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001695 vertex_shader, texture_fragment_shader_y_uv) < 0)
1696 return -1;
John Kåre Alsakerd6159802012-11-13 19:10:24 +01001697 if (shader_init(&ec->texture_shader_y_u_v, ec,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001698 vertex_shader, texture_fragment_shader_y_u_v) < 0)
1699 return -1;
John Kåre Alsakerd6159802012-11-13 19:10:24 +01001700 if (shader_init(&ec->texture_shader_y_xuxv, ec,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001701 vertex_shader, texture_fragment_shader_y_xuxv) < 0)
1702 return -1;
John Kåre Alsakerd6159802012-11-13 19:10:24 +01001703 if (shader_init(&ec->solid_shader, ec,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001704 vertex_shader, solid_fragment_shader) < 0)
1705 return -1;
1706
1707 return 0;
1708}
1709
1710static void
1711fragment_debug_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
1712 void *data)
1713{
1714 struct weston_compositor *ec = data;
1715 struct gles2_renderer *renderer =
1716 (struct gles2_renderer *) ec->renderer;
1717 struct weston_output *output;
1718
1719 renderer->fragment_shader_debug ^= 1;
1720
John Kåre Alsakerd6159802012-11-13 19:10:24 +01001721 shader_release(&ec->texture_shader_rgba);
1722 shader_release(&ec->texture_shader_rgbx);
1723 shader_release(&ec->texture_shader_egl_external);
1724 shader_release(&ec->texture_shader_y_uv);
1725 shader_release(&ec->texture_shader_y_u_v);
1726 shader_release(&ec->texture_shader_y_xuxv);
1727 shader_release(&ec->solid_shader);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001728
1729 compile_shaders(ec);
1730
1731 wl_list_for_each(output, &ec->output_list, link)
1732 weston_output_damage(output);
1733}
1734
John Kåre Alsaker94659272012-11-13 19:10:18 +01001735static int
1736gles2_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001737{
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001738 struct gles2_renderer *gr = get_renderer(ec);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001739 const char *extensions;
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001740 EGLBoolean ret;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001741
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001742 static const EGLint context_attribs[] = {
1743 EGL_CONTEXT_CLIENT_VERSION, 2,
1744 EGL_NONE
1745 };
1746
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001747 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
1748 weston_log("failed to bind EGL_OPENGL_ES_API\n");
1749 print_egl_error_state();
1750 return -1;
1751 }
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001752
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001753 log_egl_config_info(gr->egl_display, gr->egl_config);
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001754
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001755 gr->egl_context = eglCreateContext(gr->egl_display, gr->egl_config,
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001756 EGL_NO_CONTEXT, context_attribs);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001757 if (gr->egl_context == NULL) {
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001758 weston_log("failed to create context\n");
1759 print_egl_error_state();
1760 return -1;
1761 }
1762
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001763 ret = eglMakeCurrent(gr->egl_display, egl_surface,
1764 egl_surface, gr->egl_context);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001765 if (ret == EGL_FALSE) {
1766 weston_log("Failed to make EGL context current.\n");
1767 print_egl_error_state();
1768 return -1;
1769 }
1770
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001771 log_egl_gl_info(gr->egl_display);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001772
1773 ec->image_target_texture_2d =
1774 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
1775 ec->image_target_renderbuffer_storage = (void *)
1776 eglGetProcAddress("glEGLImageTargetRenderbufferStorageOES");
1777 ec->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1778 ec->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1779 ec->bind_display =
1780 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
1781 ec->unbind_display =
1782 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
1783 ec->query_buffer =
1784 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
1785
1786 extensions = (const char *) glGetString(GL_EXTENSIONS);
1787 if (!extensions) {
1788 weston_log("Retrieving GL extension string failed.\n");
1789 return -1;
1790 }
1791
1792 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
1793 weston_log("GL_EXT_texture_format_BGRA8888 not available\n");
1794 return -1;
1795 }
1796
1797 if (strstr(extensions, "GL_EXT_read_format_bgra"))
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01001798 ec->read_format = PIXMAN_a8r8g8b8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001799 else
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01001800 ec->read_format = PIXMAN_a8b8g8r8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001801
1802 if (strstr(extensions, "GL_EXT_unpack_subimage"))
1803 ec->has_unpack_subimage = 1;
1804
1805 if (strstr(extensions, "GL_OES_EGL_image_external"))
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001806 ec->has_egl_image_external = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001807
1808 extensions =
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001809 (const char *) eglQueryString(gr->egl_display, EGL_EXTENSIONS);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001810 if (!extensions) {
1811 weston_log("Retrieving EGL extension string failed.\n");
1812 return -1;
1813 }
1814
1815 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
1816 ec->has_bind_display = 1;
Pekka Paalanen035a0322012-10-24 09:43:06 +03001817 if (ec->has_bind_display) {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001818 ret = ec->bind_display(gr->egl_display, ec->wl_display);
Pekka Paalanen035a0322012-10-24 09:43:06 +03001819 if (!ret)
1820 ec->has_bind_display = 0;
1821 }
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001822
1823 glActiveTexture(GL_TEXTURE0);
1824
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001825 if (compile_shaders(ec))
1826 return -1;
1827
1828 weston_compositor_add_debug_binding(ec, KEY_S,
1829 fragment_debug_binding, ec);
1830
Pekka Paalanen035a0322012-10-24 09:43:06 +03001831 weston_log("GL ES 2 renderer features:\n");
1832 weston_log_continue(STAMP_SPACE "read-back format: %s\n",
1833 ec->read_format == GL_BGRA_EXT ? "BGRA" : "RGBA");
1834 weston_log_continue(STAMP_SPACE "wl_shm sub-image to texture: %s\n",
1835 ec->has_unpack_subimage ? "yes" : "no");
1836 weston_log_continue(STAMP_SPACE "EGL Wayland extension: %s\n",
1837 ec->has_bind_display ? "yes" : "no");
1838
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001839
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001840 return 0;
1841}