blob: 818a1a328241e606391a0ee8e05afb64dc81e8d6 [file] [log] [blame]
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001/*
2 * Copyright © 2012 Intel Corporation
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 */
22
Kristian Høgsberg25894fc2012-09-05 22:06:26 -040023#define _GNU_SOURCE
24
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -040025#include <stdlib.h>
Kristian Høgsberg25894fc2012-09-05 22:06:26 -040026#include <string.h>
27#include <ctype.h>
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +030028#include <float.h>
29#include <assert.h>
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +020030#include <linux/input.h>
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -040031
Kristian Høgsbergd7c17262012-09-05 21:54:15 -040032#include "compositor.h"
33
John Kåre Alsaker94659272012-11-13 19:10:18 +010034struct gles2_output_state {
35 EGLSurface egl_surface;
36};
37
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +010038struct gles2_renderer {
39 struct weston_renderer base;
40 int fragment_shader_debug;
41
42 EGLDisplay egl_display;
43 EGLContext egl_context;
44 EGLConfig egl_config;
John Kåre Alsaker44154502012-11-13 19:10:20 +010045
46 struct {
47 int32_t top, bottom, left, right;
48 GLuint texture;
49 int32_t width, height;
50 } border;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +010051};
John Kåre Alsaker94659272012-11-13 19:10:18 +010052
53static inline struct gles2_output_state *
54get_output_state(struct weston_output *output)
55{
56 return (struct gles2_output_state *)output->renderer_state;
57}
58
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +010059static inline struct gles2_renderer *
60get_renderer(struct weston_compositor *ec)
61{
62 return (struct gles2_renderer *)ec->renderer;
63}
64
Kristian Høgsbergd7c17262012-09-05 21:54:15 -040065static const char *
66egl_error_string(EGLint code)
67{
68#define MYERRCODE(x) case x: return #x;
69 switch (code) {
70 MYERRCODE(EGL_SUCCESS)
71 MYERRCODE(EGL_NOT_INITIALIZED)
72 MYERRCODE(EGL_BAD_ACCESS)
73 MYERRCODE(EGL_BAD_ALLOC)
74 MYERRCODE(EGL_BAD_ATTRIBUTE)
75 MYERRCODE(EGL_BAD_CONTEXT)
76 MYERRCODE(EGL_BAD_CONFIG)
77 MYERRCODE(EGL_BAD_CURRENT_SURFACE)
78 MYERRCODE(EGL_BAD_DISPLAY)
79 MYERRCODE(EGL_BAD_SURFACE)
80 MYERRCODE(EGL_BAD_MATCH)
81 MYERRCODE(EGL_BAD_PARAMETER)
82 MYERRCODE(EGL_BAD_NATIVE_PIXMAP)
83 MYERRCODE(EGL_BAD_NATIVE_WINDOW)
84 MYERRCODE(EGL_CONTEXT_LOST)
85 default:
86 return "unknown";
87 }
88#undef MYERRCODE
89}
90
91static void
92print_egl_error_state(void)
93{
94 EGLint code;
95
96 code = eglGetError();
97 weston_log("EGL error state: %s (0x%04lx)\n",
98 egl_error_string(code), (long)code);
99}
100
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300101struct polygon8 {
102 GLfloat x[8];
103 GLfloat y[8];
104 int n;
105};
106
107struct clip_context {
108 struct {
109 GLfloat x;
110 GLfloat y;
111 } prev;
112
113 struct {
114 GLfloat x1, y1;
115 GLfloat x2, y2;
116 } clip;
117
118 struct {
119 GLfloat *x;
120 GLfloat *y;
121 } vertices;
122};
123
124static GLfloat
125float_difference(GLfloat a, GLfloat b)
126{
127 /* http://www.altdevblogaday.com/2012/02/22/comparing-floating-point-numbers-2012-edition/ */
128 static const GLfloat max_diff = 4.0f * FLT_MIN;
129 static const GLfloat max_rel_diff = 4.0e-5;
130 GLfloat diff = a - b;
131 GLfloat adiff = fabsf(diff);
132
133 if (adiff <= max_diff)
134 return 0.0f;
135
136 a = fabsf(a);
137 b = fabsf(b);
138 if (adiff <= (a > b ? a : b) * max_rel_diff)
139 return 0.0f;
140
141 return diff;
142}
143
144/* A line segment (p1x, p1y)-(p2x, p2y) intersects the line x = x_arg.
145 * Compute the y coordinate of the intersection.
146 */
147static GLfloat
148clip_intersect_y(GLfloat p1x, GLfloat p1y, GLfloat p2x, GLfloat p2y,
149 GLfloat x_arg)
150{
151 GLfloat a;
152 GLfloat diff = float_difference(p1x, p2x);
153
154 /* Practically vertical line segment, yet the end points have already
155 * been determined to be on different sides of the line. Therefore
156 * the line segment is part of the line and intersects everywhere.
157 * Return the end point, so we use the whole line segment.
158 */
159 if (diff == 0.0f)
160 return p2y;
161
162 a = (x_arg - p2x) / diff;
163 return p2y + (p1y - p2y) * a;
164}
165
166/* A line segment (p1x, p1y)-(p2x, p2y) intersects the line y = y_arg.
167 * Compute the x coordinate of the intersection.
168 */
169static GLfloat
170clip_intersect_x(GLfloat p1x, GLfloat p1y, GLfloat p2x, GLfloat p2y,
171 GLfloat y_arg)
172{
173 GLfloat a;
174 GLfloat diff = float_difference(p1y, p2y);
175
176 /* Practically horizontal line segment, yet the end points have already
177 * been determined to be on different sides of the line. Therefore
178 * the line segment is part of the line and intersects everywhere.
179 * Return the end point, so we use the whole line segment.
180 */
181 if (diff == 0.0f)
182 return p2x;
183
184 a = (y_arg - p2y) / diff;
185 return p2x + (p1x - p2x) * a;
186}
187
188enum path_transition {
189 PATH_TRANSITION_OUT_TO_OUT = 0,
190 PATH_TRANSITION_OUT_TO_IN = 1,
191 PATH_TRANSITION_IN_TO_OUT = 2,
192 PATH_TRANSITION_IN_TO_IN = 3,
193};
194
195static void
196clip_append_vertex(struct clip_context *ctx, GLfloat x, GLfloat y)
197{
198 *ctx->vertices.x++ = x;
199 *ctx->vertices.y++ = y;
200}
201
202static enum path_transition
203path_transition_left_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
204{
205 return ((ctx->prev.x >= ctx->clip.x1) << 1) | (x >= ctx->clip.x1);
206}
207
208static enum path_transition
209path_transition_right_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
210{
211 return ((ctx->prev.x < ctx->clip.x2) << 1) | (x < ctx->clip.x2);
212}
213
214static enum path_transition
215path_transition_top_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
216{
217 return ((ctx->prev.y >= ctx->clip.y1) << 1) | (y >= ctx->clip.y1);
218}
219
220static enum path_transition
221path_transition_bottom_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
222{
223 return ((ctx->prev.y < ctx->clip.y2) << 1) | (y < ctx->clip.y2);
224}
225
226static void
227clip_polygon_leftright(struct clip_context *ctx,
228 enum path_transition transition,
229 GLfloat x, GLfloat y, GLfloat clip_x)
230{
231 GLfloat yi;
232
233 switch (transition) {
234 case PATH_TRANSITION_IN_TO_IN:
235 clip_append_vertex(ctx, x, y);
236 break;
237 case PATH_TRANSITION_IN_TO_OUT:
238 yi = clip_intersect_y(ctx->prev.x, ctx->prev.y, x, y, clip_x);
239 clip_append_vertex(ctx, clip_x, yi);
240 break;
241 case PATH_TRANSITION_OUT_TO_IN:
242 yi = clip_intersect_y(ctx->prev.x, ctx->prev.y, x, y, clip_x);
243 clip_append_vertex(ctx, clip_x, yi);
244 clip_append_vertex(ctx, x, y);
245 break;
246 case PATH_TRANSITION_OUT_TO_OUT:
247 /* nothing */
248 break;
249 default:
250 assert(0 && "bad enum path_transition");
251 }
252
253 ctx->prev.x = x;
254 ctx->prev.y = y;
255}
256
257static void
258clip_polygon_topbottom(struct clip_context *ctx,
259 enum path_transition transition,
260 GLfloat x, GLfloat y, GLfloat clip_y)
261{
262 GLfloat xi;
263
264 switch (transition) {
265 case PATH_TRANSITION_IN_TO_IN:
266 clip_append_vertex(ctx, x, y);
267 break;
268 case PATH_TRANSITION_IN_TO_OUT:
269 xi = clip_intersect_x(ctx->prev.x, ctx->prev.y, x, y, clip_y);
270 clip_append_vertex(ctx, xi, clip_y);
271 break;
272 case PATH_TRANSITION_OUT_TO_IN:
273 xi = clip_intersect_x(ctx->prev.x, ctx->prev.y, x, y, clip_y);
274 clip_append_vertex(ctx, xi, clip_y);
275 clip_append_vertex(ctx, x, y);
276 break;
277 case PATH_TRANSITION_OUT_TO_OUT:
278 /* nothing */
279 break;
280 default:
281 assert(0 && "bad enum path_transition");
282 }
283
284 ctx->prev.x = x;
285 ctx->prev.y = y;
286}
287
288static void
289clip_context_prepare(struct clip_context *ctx, const struct polygon8 *src,
290 GLfloat *dst_x, GLfloat *dst_y)
291{
292 ctx->prev.x = src->x[src->n - 1];
293 ctx->prev.y = src->y[src->n - 1];
294 ctx->vertices.x = dst_x;
295 ctx->vertices.y = dst_y;
296}
297
298static int
299clip_polygon_left(struct clip_context *ctx, const struct polygon8 *src,
300 GLfloat *dst_x, GLfloat *dst_y)
301{
302 enum path_transition trans;
303 int i;
304
305 clip_context_prepare(ctx, src, dst_x, dst_y);
306 for (i = 0; i < src->n; i++) {
307 trans = path_transition_left_edge(ctx, src->x[i], src->y[i]);
308 clip_polygon_leftright(ctx, trans, src->x[i], src->y[i],
309 ctx->clip.x1);
310 }
311 return ctx->vertices.x - dst_x;
312}
313
314static int
315clip_polygon_right(struct clip_context *ctx, const struct polygon8 *src,
316 GLfloat *dst_x, GLfloat *dst_y)
317{
318 enum path_transition trans;
319 int i;
320
321 clip_context_prepare(ctx, src, dst_x, dst_y);
322 for (i = 0; i < src->n; i++) {
323 trans = path_transition_right_edge(ctx, src->x[i], src->y[i]);
324 clip_polygon_leftright(ctx, trans, src->x[i], src->y[i],
325 ctx->clip.x2);
326 }
327 return ctx->vertices.x - dst_x;
328}
329
330static int
331clip_polygon_top(struct clip_context *ctx, const struct polygon8 *src,
332 GLfloat *dst_x, GLfloat *dst_y)
333{
334 enum path_transition trans;
335 int i;
336
337 clip_context_prepare(ctx, src, dst_x, dst_y);
338 for (i = 0; i < src->n; i++) {
339 trans = path_transition_top_edge(ctx, src->x[i], src->y[i]);
340 clip_polygon_topbottom(ctx, trans, src->x[i], src->y[i],
341 ctx->clip.y1);
342 }
343 return ctx->vertices.x - dst_x;
344}
345
346static int
347clip_polygon_bottom(struct clip_context *ctx, const struct polygon8 *src,
348 GLfloat *dst_x, GLfloat *dst_y)
349{
350 enum path_transition trans;
351 int i;
352
353 clip_context_prepare(ctx, src, dst_x, dst_y);
354 for (i = 0; i < src->n; i++) {
355 trans = path_transition_bottom_edge(ctx, src->x[i], src->y[i]);
356 clip_polygon_topbottom(ctx, trans, src->x[i], src->y[i],
357 ctx->clip.y2);
358 }
359 return ctx->vertices.x - dst_x;
360}
361
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400362#define max(a, b) (((a) > (b)) ? (a) : (b))
363#define min(a, b) (((a) > (b)) ? (b) : (a))
364#define clip(x, a, b) min(max(x, a), b)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400365
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300366/*
367 * Compute the boundary vertices of the intersection of the global coordinate
368 * aligned rectangle 'rect', and an arbitrary quadrilateral produced from
369 * 'surf_rect' when transformed from surface coordinates into global coordinates.
370 * The vertices are written to 'ex' and 'ey', and the return value is the
371 * number of vertices. Vertices are produced in clockwise winding order.
372 * Guarantees to produce either zero vertices, or 3-8 vertices with non-zero
373 * polygon area.
374 */
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400375static int
376calculate_edges(struct weston_surface *es, pixman_box32_t *rect,
377 pixman_box32_t *surf_rect, GLfloat *ex, GLfloat *ey)
378{
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300379 struct polygon8 polygon;
380 struct clip_context ctx;
381 int i, n;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400382 GLfloat min_x, max_x, min_y, max_y;
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300383 struct polygon8 surf = {
384 { surf_rect->x1, surf_rect->x2, surf_rect->x2, surf_rect->x1 },
385 { surf_rect->y1, surf_rect->y1, surf_rect->y2, surf_rect->y2 },
386 4
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400387 };
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400388
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300389 ctx.clip.x1 = rect->x1;
390 ctx.clip.y1 = rect->y1;
391 ctx.clip.x2 = rect->x2;
392 ctx.clip.y2 = rect->y2;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400393
394 /* transform surface to screen space: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300395 for (i = 0; i < surf.n; i++)
396 weston_surface_to_global_float(es, surf.x[i], surf.y[i],
397 &surf.x[i], &surf.y[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400398
399 /* find bounding box: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300400 min_x = max_x = surf.x[0];
401 min_y = max_y = surf.y[0];
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400402
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300403 for (i = 1; i < surf.n; i++) {
404 min_x = min(min_x, surf.x[i]);
405 max_x = max(max_x, surf.x[i]);
406 min_y = min(min_y, surf.y[i]);
407 max_y = max(max_y, surf.y[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400408 }
409
410 /* First, simple bounding box check to discard early transformed
411 * surface rects that do not intersect with the clip region:
412 */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300413 if ((min_x >= ctx.clip.x2) || (max_x <= ctx.clip.x1) ||
414 (min_y >= ctx.clip.y2) || (max_y <= ctx.clip.y1))
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400415 return 0;
416
417 /* Simple case, bounding box edges are parallel to surface edges,
418 * there will be only four edges. We just need to clip the surface
419 * vertices to the clip rect bounds:
420 */
421 if (!es->transform.enabled) {
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300422 for (i = 0; i < surf.n; i++) {
423 ex[i] = clip(surf.x[i], ctx.clip.x1, ctx.clip.x2);
424 ey[i] = clip(surf.y[i], ctx.clip.y1, ctx.clip.y2);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400425 }
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300426 return surf.n;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400427 }
428
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300429 /* Transformed case: use a general polygon clipping algorithm to
430 * clip the surface rectangle with each side of 'rect'.
431 * The algorithm is Sutherland-Hodgman, as explained in
432 * http://www.codeguru.com/cpp/misc/misc/graphics/article.php/c8965/Polygon-Clipping.htm
433 * but without looking at any of that code.
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400434 */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300435 polygon.n = clip_polygon_left(&ctx, &surf, polygon.x, polygon.y);
436 surf.n = clip_polygon_right(&ctx, &polygon, surf.x, surf.y);
437 polygon.n = clip_polygon_top(&ctx, &surf, polygon.x, polygon.y);
438 surf.n = clip_polygon_bottom(&ctx, &polygon, surf.x, surf.y);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400439
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300440 /* Get rid of duplicate vertices */
441 ex[0] = surf.x[0];
442 ey[0] = surf.y[0];
443 n = 1;
444 for (i = 1; i < surf.n; i++) {
445 if (float_difference(ex[n - 1], surf.x[i]) == 0.0f &&
446 float_difference(ey[n - 1], surf.y[i]) == 0.0f)
447 continue;
448 ex[n] = surf.x[i];
449 ey[n] = surf.y[i];
450 n++;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400451 }
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300452 if (float_difference(ex[n - 1], surf.x[0]) == 0.0f &&
453 float_difference(ey[n - 1], surf.y[0]) == 0.0f)
454 n--;
455
456 if (n < 3)
457 return 0;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400458
459 return n;
460}
461
462static int
463texture_region(struct weston_surface *es, pixman_region32_t *region,
464 pixman_region32_t *surf_region)
465{
466 struct weston_compositor *ec = es->compositor;
467 GLfloat *v, inv_width, inv_height;
468 unsigned int *vtxcnt, nvtx = 0;
469 pixman_box32_t *rects, *surf_rects;
470 int i, j, k, nrects, nsurf;
471
472 rects = pixman_region32_rectangles(region, &nrects);
473 surf_rects = pixman_region32_rectangles(surf_region, &nsurf);
474
475 /* worst case we can have 8 vertices per rect (ie. clipped into
476 * an octagon):
477 */
478 v = wl_array_add(&ec->vertices, nrects * nsurf * 8 * 4 * sizeof *v);
479 vtxcnt = wl_array_add(&ec->vtxcnt, nrects * nsurf * sizeof *vtxcnt);
480
481 inv_width = 1.0 / es->pitch;
482 inv_height = 1.0 / es->geometry.height;
483
484 for (i = 0; i < nrects; i++) {
485 pixman_box32_t *rect = &rects[i];
486 for (j = 0; j < nsurf; j++) {
487 pixman_box32_t *surf_rect = &surf_rects[j];
488 GLfloat sx, sy;
489 GLfloat ex[8], ey[8]; /* edge points in screen space */
490 int n;
491
492 /* The transformed surface, after clipping to the clip region,
493 * can have as many as eight sides, emitted as a triangle-fan.
494 * The first vertex in the triangle fan can be chosen arbitrarily,
495 * since the area is guaranteed to be convex.
496 *
497 * If a corner of the transformed surface falls outside of the
498 * clip region, instead of emitting one vertex for the corner
499 * of the surface, up to two are emitted for two corresponding
500 * intersection point(s) between the surface and the clip region.
501 *
502 * To do this, we first calculate the (up to eight) points that
503 * form the intersection of the clip rect and the transformed
504 * surface.
505 */
506 n = calculate_edges(es, rect, surf_rect, ex, ey);
507 if (n < 3)
508 continue;
509
510 /* emit edge points: */
511 for (k = 0; k < n; k++) {
512 weston_surface_from_global_float(es, ex[k], ey[k], &sx, &sy);
513 /* position: */
514 *(v++) = ex[k];
515 *(v++) = ey[k];
516 /* texcoord: */
517 *(v++) = sx * inv_width;
518 *(v++) = sy * inv_height;
519 }
520
521 vtxcnt[nvtx++] = n;
522 }
523 }
524
525 return nvtx;
526}
527
528static void
529triangle_fan_debug(struct weston_surface *surface, int first, int count)
530{
531 struct weston_compositor *compositor = surface->compositor;
532 int i;
533 GLushort *buffer;
534 GLushort *index;
535 int nelems;
536 static int color_idx = 0;
537 static const GLfloat color[][4] = {
538 { 1.0, 0.0, 0.0, 1.0 },
539 { 0.0, 1.0, 0.0, 1.0 },
540 { 0.0, 0.0, 1.0, 1.0 },
541 { 1.0, 1.0, 1.0, 1.0 },
542 };
543
544 nelems = (count - 1 + count - 2) * 2;
545
546 buffer = malloc(sizeof(GLushort) * nelems);
547 index = buffer;
548
549 for (i = 1; i < count; i++) {
550 *index++ = first;
551 *index++ = first + i;
552 }
553
554 for (i = 2; i < count; i++) {
555 *index++ = first + i - 1;
556 *index++ = first + i;
557 }
558
559 glUseProgram(compositor->solid_shader.program);
560 glUniform4fv(compositor->solid_shader.color_uniform, 1,
561 color[color_idx++ % ARRAY_LENGTH(color)]);
562 glDrawElements(GL_LINES, nelems, GL_UNSIGNED_SHORT, buffer);
563 glUseProgram(compositor->current_shader->program);
564 free(buffer);
565}
566
567static void
568repaint_region(struct weston_surface *es, pixman_region32_t *region,
569 pixman_region32_t *surf_region)
570{
571 struct weston_compositor *ec = es->compositor;
572 GLfloat *v;
573 unsigned int *vtxcnt;
574 int i, first, nfans;
575
576 /* The final region to be painted is the intersection of
577 * 'region' and 'surf_region'. However, 'region' is in the global
578 * coordinates, and 'surf_region' is in the surface-local
579 * coordinates. texture_region() will iterate over all pairs of
580 * rectangles from both regions, compute the intersection
581 * polygon for each pair, and store it as a triangle fan if
582 * it has a non-zero area (at least 3 vertices, actually).
583 */
584 nfans = texture_region(es, region, surf_region);
585
586 v = ec->vertices.data;
587 vtxcnt = ec->vtxcnt.data;
588
589 /* position: */
590 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
591 glEnableVertexAttribArray(0);
592
593 /* texcoord: */
594 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
595 glEnableVertexAttribArray(1);
596
597 for (i = 0, first = 0; i < nfans; i++) {
598 glDrawArrays(GL_TRIANGLE_FAN, first, vtxcnt[i]);
599 if (ec->fan_debug)
600 triangle_fan_debug(es, first, vtxcnt[i]);
601 first += vtxcnt[i];
602 }
603
604 glDisableVertexAttribArray(1);
605 glDisableVertexAttribArray(0);
606
607 ec->vertices.size = 0;
608 ec->vtxcnt.size = 0;
609}
610
611static void
612weston_compositor_use_shader(struct weston_compositor *compositor,
613 struct weston_shader *shader)
614{
615 if (compositor->current_shader == shader)
616 return;
617
618 glUseProgram(shader->program);
619 compositor->current_shader = shader;
620}
621
622static void
623weston_shader_uniforms(struct weston_shader *shader,
624 struct weston_surface *surface,
625 struct weston_output *output)
626{
627 int i;
628
629 glUniformMatrix4fv(shader->proj_uniform,
630 1, GL_FALSE, output->matrix.d);
631 glUniform4fv(shader->color_uniform, 1, surface->color);
632 glUniform1f(shader->alpha_uniform, surface->alpha);
633
634 for (i = 0; i < surface->num_textures; i++)
635 glUniform1i(shader->tex_uniforms[i], i);
636}
637
638static void
639draw_surface(struct weston_surface *es, struct weston_output *output,
640 pixman_region32_t *damage) /* in global coordinates */
641{
642 struct weston_compositor *ec = es->compositor;
643 /* repaint bounding region in global coordinates: */
644 pixman_region32_t repaint;
645 /* non-opaque region in surface coordinates: */
646 pixman_region32_t surface_blend;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300647 pixman_region32_t *buffer_damage;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400648 GLint filter;
649 int i;
650
651 pixman_region32_init(&repaint);
652 pixman_region32_intersect(&repaint,
653 &es->transform.boundingbox, damage);
654 pixman_region32_subtract(&repaint, &repaint, &es->clip);
655
656 if (!pixman_region32_not_empty(&repaint))
657 goto out;
658
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300659 buffer_damage = &output->buffer_damage[output->current_buffer];
660 pixman_region32_subtract(buffer_damage, buffer_damage, &repaint);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400661
662 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
663
664 if (ec->fan_debug) {
665 weston_compositor_use_shader(ec, &ec->solid_shader);
666 weston_shader_uniforms(&ec->solid_shader, es, output);
667 }
668
669 weston_compositor_use_shader(ec, es->shader);
670 weston_shader_uniforms(es->shader, es, output);
671
672 if (es->transform.enabled || output->zoom.active)
673 filter = GL_LINEAR;
674 else
675 filter = GL_NEAREST;
676
677 for (i = 0; i < es->num_textures; i++) {
678 glActiveTexture(GL_TEXTURE0 + i);
679 glBindTexture(es->target, es->textures[i]);
680 glTexParameteri(es->target, GL_TEXTURE_MIN_FILTER, filter);
681 glTexParameteri(es->target, GL_TEXTURE_MAG_FILTER, filter);
682 }
683
684 /* blended region is whole surface minus opaque region: */
685 pixman_region32_init_rect(&surface_blend, 0, 0,
686 es->geometry.width, es->geometry.height);
687 pixman_region32_subtract(&surface_blend, &surface_blend, &es->opaque);
688
689 if (pixman_region32_not_empty(&es->opaque)) {
690 if (es->shader == &ec->texture_shader_rgba) {
691 /* Special case for RGBA textures with possibly
692 * bad data in alpha channel: use the shader
693 * that forces texture alpha = 1.0.
694 * Xwayland surfaces need this.
695 */
696 weston_compositor_use_shader(ec, &ec->texture_shader_rgbx);
697 weston_shader_uniforms(&ec->texture_shader_rgbx, es, output);
698 }
699
700 if (es->alpha < 1.0)
701 glEnable(GL_BLEND);
702 else
703 glDisable(GL_BLEND);
704
705 repaint_region(es, &repaint, &es->opaque);
706 }
707
708 if (pixman_region32_not_empty(&surface_blend)) {
709 weston_compositor_use_shader(ec, es->shader);
710 glEnable(GL_BLEND);
711 repaint_region(es, &repaint, &surface_blend);
712 }
713
714 pixman_region32_fini(&surface_blend);
715
716out:
717 pixman_region32_fini(&repaint);
718}
719
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400720static void
721repaint_surfaces(struct weston_output *output, pixman_region32_t *damage)
722{
723 struct weston_compositor *compositor = output->compositor;
724 struct weston_surface *surface;
725
726 wl_list_for_each_reverse(surface, &compositor->surface_list, link)
727 if (surface->plane == &compositor->primary_plane)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400728 draw_surface(surface, output, damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400729}
730
John Kåre Alsaker44154502012-11-13 19:10:20 +0100731
732static int
733texture_border(struct weston_output *output)
734{
735 struct weston_compositor *ec = output->compositor;
736 struct gles2_renderer *gr = get_renderer(ec);
737 GLfloat *d;
738 unsigned int *p;
739 int i, j, k, n;
740 GLfloat x[4], y[4], u[4], v[4];
741
742 x[0] = -gr->border.left;
743 x[1] = 0;
744 x[2] = output->current->width;
745 x[3] = output->current->width + gr->border.right;
746
747 y[0] = -gr->border.top;
748 y[1] = 0;
749 y[2] = output->current->height;
750 y[3] = output->current->height + gr->border.bottom;
751
752 u[0] = 0.0;
753 u[1] = (GLfloat) gr->border.left / gr->border.width;
754 u[2] = (GLfloat) (gr->border.width - gr->border.right) / gr->border.width;
755 u[3] = 1.0;
756
757 v[0] = 0.0;
758 v[1] = (GLfloat) gr->border.top / gr->border.height;
759 v[2] = (GLfloat) (gr->border.height - gr->border.bottom) / gr->border.height;
760 v[3] = 1.0;
761
762 n = 8;
763 d = wl_array_add(&ec->vertices, n * 16 * sizeof *d);
764 p = wl_array_add(&ec->indices, n * 6 * sizeof *p);
765
766 k = 0;
767 for (i = 0; i < 3; i++)
768 for (j = 0; j < 3; j++) {
769
770 if (i == 1 && j == 1)
771 continue;
772
773 d[ 0] = x[i];
774 d[ 1] = y[j];
775 d[ 2] = u[i];
776 d[ 3] = v[j];
777
778 d[ 4] = x[i];
779 d[ 5] = y[j + 1];
780 d[ 6] = u[i];
781 d[ 7] = v[j + 1];
782
783 d[ 8] = x[i + 1];
784 d[ 9] = y[j];
785 d[10] = u[i + 1];
786 d[11] = v[j];
787
788 d[12] = x[i + 1];
789 d[13] = y[j + 1];
790 d[14] = u[i + 1];
791 d[15] = v[j + 1];
792
793 p[0] = k + 0;
794 p[1] = k + 1;
795 p[2] = k + 2;
796 p[3] = k + 2;
797 p[4] = k + 1;
798 p[5] = k + 3;
799
800 d += 16;
801 p += 6;
802 k += 4;
803 }
804
805 return k / 4;
806}
807
808static void
809draw_border(struct weston_output *output)
810{
811 struct weston_compositor *ec = output->compositor;
812 struct gles2_renderer *gr = get_renderer(ec);
813 struct weston_shader *shader = &ec->texture_shader_rgba;
814 GLfloat *v;
815 int n;
816
817 glDisable(GL_BLEND);
818 glUseProgram(shader->program);
819 ec->current_shader = shader;
820
821 glUniformMatrix4fv(shader->proj_uniform,
822 1, GL_FALSE, output->matrix.d);
823
824 glUniform1i(shader->tex_uniforms[0], 0);
825 glUniform1f(shader->alpha_uniform, 1);
826
827 n = texture_border(output);
828
829 glActiveTexture(GL_TEXTURE0);
830 glBindTexture(GL_TEXTURE_2D, gr->border.texture);
831
832 v = ec->vertices.data;
833 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
834 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
835 glEnableVertexAttribArray(0);
836 glEnableVertexAttribArray(1);
837
838 glDrawElements(GL_TRIANGLES, n * 6,
839 GL_UNSIGNED_INT, ec->indices.data);
840
841 glDisableVertexAttribArray(1);
842 glDisableVertexAttribArray(0);
843
844 ec->vertices.size = 0;
845 ec->indices.size = 0;
846}
847
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400848static void
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400849gles2_renderer_repaint_output(struct weston_output *output,
850 pixman_region32_t *output_damage)
851{
John Kåre Alsaker94659272012-11-13 19:10:18 +0100852 struct gles2_output_state *go = get_output_state(output);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400853 struct weston_compositor *compositor = output->compositor;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100854 struct gles2_renderer *gr = get_renderer(compositor);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400855 EGLBoolean ret;
856 static int errored;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300857 int32_t width, height, i;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400858
859 width = output->current->width +
860 output->border.left + output->border.right;
861 height = output->current->height +
862 output->border.top + output->border.bottom;
863
864 glViewport(0, 0, width, height);
865
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100866 ret = eglMakeCurrent(gr->egl_display, go->egl_surface,
867 go->egl_surface, gr->egl_context);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400868 if (ret == EGL_FALSE) {
869 if (errored)
870 return;
871 errored = 1;
872 weston_log("Failed to make EGL context current.\n");
873 print_egl_error_state();
874 return;
875 }
876
877 /* if debugging, redraw everything outside the damage to clean up
878 * debug lines from the previous draw on this buffer:
879 */
880 if (compositor->fan_debug) {
881 pixman_region32_t undamaged;
882 pixman_region32_init(&undamaged);
883 pixman_region32_subtract(&undamaged, &output->region,
884 output_damage);
885 compositor->fan_debug = 0;
886 repaint_surfaces(output, &undamaged);
887 compositor->fan_debug = 1;
888 pixman_region32_fini(&undamaged);
889 }
890
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300891 for (i = 0; i < 2; i++)
892 pixman_region32_union(&output->buffer_damage[i],
893 &output->buffer_damage[i],
894 output_damage);
895
896 pixman_region32_union(output_damage, output_damage,
897 &output->buffer_damage[output->current_buffer]);
898
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400899 repaint_surfaces(output, output_damage);
900
John Kåre Alsaker44154502012-11-13 19:10:20 +0100901 if (gr->border.texture)
902 draw_border(output);
903
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400904 wl_signal_emit(&output->frame_signal, output);
905
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100906 ret = eglSwapBuffers(gr->egl_display, go->egl_surface);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400907 if (ret == EGL_FALSE && !errored) {
908 errored = 1;
909 weston_log("Failed in eglSwapBuffers.\n");
910 print_egl_error_state();
911 }
912
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300913 output->current_buffer ^= 1;
914
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400915}
Kristian Høgsberg25894fc2012-09-05 22:06:26 -0400916
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400917static void
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -0400918gles2_renderer_flush_damage(struct weston_surface *surface)
919{
920#ifdef GL_UNPACK_ROW_LENGTH
921 pixman_box32_t *rectangles;
922 void *data;
923 int i, n;
924#endif
925
Pekka Paalanenbcdd5792012-11-07 12:25:13 +0200926 pixman_region32_union(&surface->texture_damage,
927 &surface->texture_damage, &surface->damage);
928
929 /* Avoid upload, if the texture won't be used this time.
930 * We still accumulate the damage in texture_damage.
931 */
932 if (surface->plane != &surface->compositor->primary_plane)
933 return;
934
935 if (!pixman_region32_not_empty(&surface->texture_damage))
936 return;
937
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -0400938 glBindTexture(GL_TEXTURE_2D, surface->textures[0]);
939
940 if (!surface->compositor->has_unpack_subimage) {
941 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
942 surface->pitch, surface->buffer->height, 0,
943 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
944 wl_shm_buffer_get_data(surface->buffer));
945
Pekka Paalanenbcdd5792012-11-07 12:25:13 +0200946 goto done;
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -0400947 }
948
949#ifdef GL_UNPACK_ROW_LENGTH
950 /* Mesa does not define GL_EXT_unpack_subimage */
951 glPixelStorei(GL_UNPACK_ROW_LENGTH, surface->pitch);
952 data = wl_shm_buffer_get_data(surface->buffer);
Pekka Paalanenbcdd5792012-11-07 12:25:13 +0200953 rectangles = pixman_region32_rectangles(&surface->texture_damage, &n);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -0400954 for (i = 0; i < n; i++) {
955 glPixelStorei(GL_UNPACK_SKIP_PIXELS, rectangles[i].x1);
956 glPixelStorei(GL_UNPACK_SKIP_ROWS, rectangles[i].y1);
957 glTexSubImage2D(GL_TEXTURE_2D, 0,
958 rectangles[i].x1, rectangles[i].y1,
959 rectangles[i].x2 - rectangles[i].x1,
960 rectangles[i].y2 - rectangles[i].y1,
961 GL_BGRA_EXT, GL_UNSIGNED_BYTE, data);
962 }
963#endif
Pekka Paalanenbcdd5792012-11-07 12:25:13 +0200964
965done:
966 pixman_region32_fini(&surface->texture_damage);
967 pixman_region32_init(&surface->texture_damage);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -0400968}
969
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400970static void
971ensure_textures(struct weston_surface *es, int num_textures)
972{
973 int i;
974
975 if (num_textures <= es->num_textures)
976 return;
977
978 for (i = es->num_textures; i < num_textures; i++) {
979 glGenTextures(1, &es->textures[i]);
980 glBindTexture(es->target, es->textures[i]);
981 glTexParameteri(es->target,
982 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
983 glTexParameteri(es->target,
984 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
985 }
986 es->num_textures = num_textures;
987 glBindTexture(es->target, 0);
988}
989
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400990static void
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400991gles2_renderer_attach(struct weston_surface *es, struct wl_buffer *buffer)
992{
993 struct weston_compositor *ec = es->compositor;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100994 struct gles2_renderer *gr = get_renderer(ec);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400995 EGLint attribs[3], format;
996 int i, num_planes;
997
998 if (!buffer) {
999 for (i = 0; i < es->num_images; i++) {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001000 ec->destroy_image(gr->egl_display, es->images[i]);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001001 es->images[i] = NULL;
1002 }
1003 es->num_images = 0;
1004 glDeleteTextures(es->num_textures, es->textures);
1005 es->num_textures = 0;
1006 return;
1007 }
1008
1009 if (wl_buffer_is_shm(buffer)) {
1010 es->pitch = wl_shm_buffer_get_stride(buffer) / 4;
1011 es->target = GL_TEXTURE_2D;
1012
1013 ensure_textures(es, 1);
1014 glBindTexture(GL_TEXTURE_2D, es->textures[0]);
1015 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1016 es->pitch, buffer->height, 0,
1017 GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
1018 if (wl_shm_buffer_get_format(buffer) == WL_SHM_FORMAT_XRGB8888)
1019 es->shader = &ec->texture_shader_rgbx;
1020 else
1021 es->shader = &ec->texture_shader_rgba;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001022 } else if (ec->query_buffer(gr->egl_display, buffer,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001023 EGL_TEXTURE_FORMAT, &format)) {
1024 for (i = 0; i < es->num_images; i++)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001025 ec->destroy_image(gr->egl_display, es->images[i]);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001026 es->num_images = 0;
1027 es->target = GL_TEXTURE_2D;
1028 switch (format) {
1029 case EGL_TEXTURE_RGB:
1030 case EGL_TEXTURE_RGBA:
1031 default:
1032 num_planes = 1;
1033 es->shader = &ec->texture_shader_rgba;
1034 break;
1035 case EGL_TEXTURE_EXTERNAL_WL:
1036 num_planes = 1;
1037 es->target = GL_TEXTURE_EXTERNAL_OES;
1038 es->shader = &ec->texture_shader_egl_external;
1039 break;
1040 case EGL_TEXTURE_Y_UV_WL:
1041 num_planes = 2;
1042 es->shader = &ec->texture_shader_y_uv;
1043 break;
1044 case EGL_TEXTURE_Y_U_V_WL:
1045 num_planes = 3;
1046 es->shader = &ec->texture_shader_y_u_v;
1047 break;
1048 case EGL_TEXTURE_Y_XUXV_WL:
1049 num_planes = 2;
1050 es->shader = &ec->texture_shader_y_xuxv;
1051 break;
1052 }
1053
1054 ensure_textures(es, num_planes);
1055 for (i = 0; i < num_planes; i++) {
1056 attribs[0] = EGL_WAYLAND_PLANE_WL;
1057 attribs[1] = i;
1058 attribs[2] = EGL_NONE;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001059 es->images[i] = ec->create_image(gr->egl_display,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001060 NULL,
1061 EGL_WAYLAND_BUFFER_WL,
1062 buffer, attribs);
1063 if (!es->images[i]) {
1064 weston_log("failed to create img for plane %d\n", i);
1065 continue;
1066 }
1067 es->num_images++;
1068
1069 glActiveTexture(GL_TEXTURE0 + i);
1070 glBindTexture(es->target, es->textures[i]);
1071 ec->image_target_texture_2d(es->target,
1072 es->images[i]);
1073 }
1074
1075 es->pitch = buffer->width;
1076 } else {
1077 weston_log("unhandled buffer type!\n");
1078 }
1079}
1080
Kristian Høgsberg42263852012-09-06 21:59:29 -04001081static void
1082gles2_renderer_destroy_surface(struct weston_surface *surface)
1083{
1084 struct weston_compositor *ec = surface->compositor;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001085 struct gles2_renderer *gr = get_renderer(ec);
Kristian Høgsberg42263852012-09-06 21:59:29 -04001086 int i;
1087
1088 glDeleteTextures(surface->num_textures, surface->textures);
1089
1090 for (i = 0; i < surface->num_images; i++)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001091 ec->destroy_image(gr->egl_display, surface->images[i]);
Kristian Høgsberg42263852012-09-06 21:59:29 -04001092}
1093
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001094static const char vertex_shader[] =
1095 "uniform mat4 proj;\n"
1096 "attribute vec2 position;\n"
1097 "attribute vec2 texcoord;\n"
1098 "varying vec2 v_texcoord;\n"
1099 "void main()\n"
1100 "{\n"
1101 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
1102 " v_texcoord = texcoord;\n"
1103 "}\n";
1104
1105/* Declare common fragment shader uniforms */
1106#define FRAGMENT_CONVERT_YUV \
1107 " y *= alpha;\n" \
1108 " u *= alpha;\n" \
1109 " v *= alpha;\n" \
1110 " gl_FragColor.r = y + 1.59602678 * v;\n" \
1111 " gl_FragColor.g = y - 0.39176229 * u - 0.81296764 * v;\n" \
1112 " gl_FragColor.b = y + 2.01723214 * u;\n" \
1113 " gl_FragColor.a = alpha;\n"
1114
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001115static const char fragment_debug[] =
1116 " gl_FragColor = vec4(0.0, 0.3, 0.0, 0.2) + gl_FragColor * 0.8;\n";
1117
1118static const char fragment_brace[] =
1119 "}\n";
1120
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001121static const char texture_fragment_shader_rgba[] =
1122 "precision mediump float;\n"
1123 "varying vec2 v_texcoord;\n"
1124 "uniform sampler2D tex;\n"
1125 "uniform float alpha;\n"
1126 "void main()\n"
1127 "{\n"
1128 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001129 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001130
1131static const char texture_fragment_shader_rgbx[] =
1132 "precision mediump float;\n"
1133 "varying vec2 v_texcoord;\n"
1134 "uniform sampler2D tex;\n"
1135 "uniform float alpha;\n"
1136 "void main()\n"
1137 "{\n"
1138 " gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb\n;"
1139 " gl_FragColor.a = alpha;\n"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001140 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001141
1142static const char texture_fragment_shader_egl_external[] =
1143 "#extension GL_OES_EGL_image_external : require\n"
1144 "precision mediump float;\n"
1145 "varying vec2 v_texcoord;\n"
1146 "uniform samplerExternalOES tex;\n"
1147 "uniform float alpha;\n"
1148 "void main()\n"
1149 "{\n"
1150 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001151 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001152
1153static const char texture_fragment_shader_y_uv[] =
1154 "precision mediump float;\n"
1155 "uniform sampler2D tex;\n"
1156 "uniform sampler2D tex1;\n"
1157 "varying vec2 v_texcoord;\n"
1158 "uniform float alpha;\n"
1159 "void main() {\n"
1160 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1161 " float u = texture2D(tex1, v_texcoord).r - 0.5;\n"
1162 " float v = texture2D(tex1, v_texcoord).g - 0.5;\n"
1163 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001164 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001165
1166static const char texture_fragment_shader_y_u_v[] =
1167 "precision mediump float;\n"
1168 "uniform sampler2D tex;\n"
1169 "uniform sampler2D tex1;\n"
1170 "uniform sampler2D tex2;\n"
1171 "varying vec2 v_texcoord;\n"
1172 "uniform float alpha;\n"
1173 "void main() {\n"
1174 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1175 " float u = texture2D(tex1, v_texcoord).x - 0.5;\n"
1176 " float v = texture2D(tex2, v_texcoord).x - 0.5;\n"
1177 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001178 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001179
1180static const char texture_fragment_shader_y_xuxv[] =
1181 "precision mediump float;\n"
1182 "uniform sampler2D tex;\n"
1183 "uniform sampler2D tex1;\n"
1184 "varying vec2 v_texcoord;\n"
1185 "uniform float alpha;\n"
1186 "void main() {\n"
1187 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1188 " float u = texture2D(tex1, v_texcoord).g - 0.5;\n"
1189 " float v = texture2D(tex1, v_texcoord).a - 0.5;\n"
1190 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001191 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001192
1193static const char solid_fragment_shader[] =
1194 "precision mediump float;\n"
1195 "uniform vec4 color;\n"
1196 "uniform float alpha;\n"
1197 "void main()\n"
1198 "{\n"
1199 " gl_FragColor = alpha * color\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001200 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001201
1202static int
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001203compile_shader(GLenum type, int count, const char **sources)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001204{
1205 GLuint s;
1206 char msg[512];
1207 GLint status;
1208
1209 s = glCreateShader(type);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001210 glShaderSource(s, count, sources, NULL);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001211 glCompileShader(s);
1212 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
1213 if (!status) {
1214 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1215 weston_log("shader info: %s\n", msg);
1216 return GL_NONE;
1217 }
1218
1219 return s;
1220}
1221
1222static int
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001223weston_shader_init(struct weston_shader *shader, struct weston_compositor *ec,
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001224 const char *vertex_source, const char *fragment_source)
1225{
1226 char msg[512];
1227 GLint status;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001228 int count;
1229 const char *sources[3];
1230 struct gles2_renderer *renderer =
1231 (struct gles2_renderer *) ec->renderer;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001232
1233 shader->vertex_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001234 compile_shader(GL_VERTEX_SHADER, 1, &vertex_source);
1235
1236 if (renderer->fragment_shader_debug) {
1237 sources[0] = fragment_source;
1238 sources[1] = fragment_debug;
1239 sources[2] = fragment_brace;
1240 count = 3;
1241 } else {
1242 sources[0] = fragment_source;
1243 sources[1] = fragment_brace;
1244 count = 2;
1245 }
1246
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001247 shader->fragment_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001248 compile_shader(GL_FRAGMENT_SHADER, count, sources);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001249
1250 shader->program = glCreateProgram();
1251 glAttachShader(shader->program, shader->vertex_shader);
1252 glAttachShader(shader->program, shader->fragment_shader);
1253 glBindAttribLocation(shader->program, 0, "position");
1254 glBindAttribLocation(shader->program, 1, "texcoord");
1255
1256 glLinkProgram(shader->program);
1257 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1258 if (!status) {
1259 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1260 weston_log("link info: %s\n", msg);
1261 return -1;
1262 }
1263
1264 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1265 shader->tex_uniforms[0] = glGetUniformLocation(shader->program, "tex");
1266 shader->tex_uniforms[1] = glGetUniformLocation(shader->program, "tex1");
1267 shader->tex_uniforms[2] = glGetUniformLocation(shader->program, "tex2");
1268 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
1269 shader->color_uniform = glGetUniformLocation(shader->program, "color");
1270
1271 return 0;
1272}
1273
1274static void
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001275weston_shader_release(struct weston_shader *shader)
1276{
1277 glDeleteShader(shader->vertex_shader);
1278 glDeleteShader(shader->fragment_shader);
1279 glDeleteProgram(shader->program);
1280
1281 shader->vertex_shader = 0;
1282 shader->fragment_shader = 0;
1283 shader->program = 0;
1284}
1285
1286static void
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001287log_extensions(const char *name, const char *extensions)
1288{
1289 const char *p, *end;
1290 int l;
1291 int len;
1292
1293 l = weston_log("%s:", name);
1294 p = extensions;
1295 while (*p) {
1296 end = strchrnul(p, ' ');
1297 len = end - p;
1298 if (l + len > 78)
1299 l = weston_log_continue("\n" STAMP_SPACE "%.*s",
1300 len, p);
1301 else
1302 l += weston_log_continue(" %.*s", len, p);
1303 for (p = end; isspace(*p); p++)
1304 ;
1305 }
1306 weston_log_continue("\n");
1307}
1308
1309static void
1310log_egl_gl_info(EGLDisplay egldpy)
1311{
1312 const char *str;
1313
1314 str = eglQueryString(egldpy, EGL_VERSION);
1315 weston_log("EGL version: %s\n", str ? str : "(null)");
1316
1317 str = eglQueryString(egldpy, EGL_VENDOR);
1318 weston_log("EGL vendor: %s\n", str ? str : "(null)");
1319
1320 str = eglQueryString(egldpy, EGL_CLIENT_APIS);
1321 weston_log("EGL client APIs: %s\n", str ? str : "(null)");
1322
1323 str = eglQueryString(egldpy, EGL_EXTENSIONS);
1324 log_extensions("EGL extensions", str ? str : "(null)");
1325
1326 str = (char *)glGetString(GL_VERSION);
1327 weston_log("GL version: %s\n", str ? str : "(null)");
1328
1329 str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
1330 weston_log("GLSL version: %s\n", str ? str : "(null)");
1331
1332 str = (char *)glGetString(GL_VENDOR);
1333 weston_log("GL vendor: %s\n", str ? str : "(null)");
1334
1335 str = (char *)glGetString(GL_RENDERER);
1336 weston_log("GL renderer: %s\n", str ? str : "(null)");
1337
1338 str = (char *)glGetString(GL_EXTENSIONS);
1339 log_extensions("GL extensions", str ? str : "(null)");
1340}
1341
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001342static void
1343log_egl_config_info(EGLDisplay egldpy, EGLConfig eglconfig)
1344{
1345 EGLint r, g, b, a;
1346
1347 weston_log("Chosen EGL config details:\n");
1348
1349 weston_log_continue(STAMP_SPACE "RGBA bits");
1350 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_RED_SIZE, &r) &&
1351 eglGetConfigAttrib(egldpy, eglconfig, EGL_GREEN_SIZE, &g) &&
1352 eglGetConfigAttrib(egldpy, eglconfig, EGL_BLUE_SIZE, &b) &&
1353 eglGetConfigAttrib(egldpy, eglconfig, EGL_ALPHA_SIZE, &a))
1354 weston_log_continue(": %d %d %d %d\n", r, g, b, a);
1355 else
1356 weston_log_continue(" unknown\n");
1357
1358 weston_log_continue(STAMP_SPACE "swap interval range");
1359 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_MIN_SWAP_INTERVAL, &a) &&
1360 eglGetConfigAttrib(egldpy, eglconfig, EGL_MAX_SWAP_INTERVAL, &b))
1361 weston_log_continue(": %d - %d\n", a, b);
1362 else
1363 weston_log_continue(" unknown\n");
1364}
1365
John Kåre Alsaker44154502012-11-13 19:10:20 +01001366static void
1367output_apply_border(struct weston_output *output, struct gles2_renderer *gr)
1368{
1369 output->border.top = gr->border.top;
1370 output->border.bottom = gr->border.bottom;
1371 output->border.left = gr->border.left;
1372 output->border.right = gr->border.right;
1373}
1374
1375WL_EXPORT void
1376gles2_renderer_set_border(struct weston_compositor *ec, int32_t width, int32_t height, void *data,
1377 int32_t *edges)
1378{
1379 struct gles2_renderer *gr = get_renderer(ec);
1380 struct weston_output *output;
1381
1382 gr->border.left = edges[0];
1383 gr->border.right = edges[1];
1384 gr->border.top = edges[2];
1385 gr->border.bottom = edges[3];
1386
1387 gr->border.width = width;
1388 gr->border.height = height;
1389
1390 glGenTextures(1, &gr->border.texture);
1391 glBindTexture(GL_TEXTURE_2D, gr->border.texture);
1392 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1393 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1394 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1395 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1396
1397 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1398 width,
1399 height,
1400 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1401 data);
1402
1403 wl_list_for_each(output, &ec->output_list, link)
1404 output_apply_border(output, gr);
1405}
1406
John Kåre Alsaker94659272012-11-13 19:10:18 +01001407static int
1408gles2_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface);
1409
1410WL_EXPORT int
1411gles2_renderer_output_create(struct weston_output *output,
1412 EGLNativeWindowType window)
1413{
1414 struct weston_compositor *ec = output->compositor;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001415 struct gles2_renderer *gr = get_renderer(ec);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001416 struct gles2_output_state *go = calloc(1, sizeof *go);
1417
1418 if (!go)
1419 return -1;
1420
1421 go->egl_surface =
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001422 eglCreateWindowSurface(gr->egl_display,
1423 gr->egl_config,
John Kåre Alsaker94659272012-11-13 19:10:18 +01001424 window, NULL);
1425
1426 if (go->egl_surface == EGL_NO_SURFACE) {
1427 weston_log("failed to create egl surface\n");
1428 free(go);
1429 return -1;
1430 }
1431
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001432 if (gr->egl_context == NULL)
John Kåre Alsaker94659272012-11-13 19:10:18 +01001433 if (gles2_renderer_setup(ec, go->egl_surface) < 0) {
1434 free(go);
1435 return -1;
1436 }
1437
1438 output->renderer_state = go;
1439
John Kåre Alsaker44154502012-11-13 19:10:20 +01001440 output_apply_border(output, gr);
1441
John Kåre Alsaker94659272012-11-13 19:10:18 +01001442 return 0;
1443}
1444
1445WL_EXPORT void
1446gles2_renderer_output_destroy(struct weston_output *output)
1447{
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001448 struct gles2_renderer *gr = get_renderer(output->compositor);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001449 struct gles2_output_state *go = get_output_state(output);
1450
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001451 eglDestroySurface(gr->egl_display, go->egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001452
1453 free(go);
1454}
1455
1456WL_EXPORT EGLSurface
1457gles2_renderer_output_surface(struct weston_output *output)
1458{
1459 return get_output_state(output)->egl_surface;
1460}
1461
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001462WL_EXPORT void
1463gles2_renderer_destroy(struct weston_compositor *ec)
1464{
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001465 struct gles2_renderer *gr = get_renderer(ec);
1466
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001467 if (ec->has_bind_display)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001468 ec->unbind_display(gr->egl_display, ec->wl_display);
1469
1470 /* Work around crash in egl_dri2.c's dri2_make_current() - when does this apply? */
1471 eglMakeCurrent(gr->egl_display,
1472 EGL_NO_SURFACE, EGL_NO_SURFACE,
1473 EGL_NO_CONTEXT);
1474
1475 eglTerminate(gr->egl_display);
1476 eglReleaseThread();
1477}
1478
1479static int
1480egl_choose_config(struct gles2_renderer *gr, const EGLint *attribs,
1481 const EGLint *visual_id)
1482{
1483 EGLint count = 0;
1484 EGLint matched = 0;
1485 EGLConfig *configs;
1486 int i;
1487
1488 if (!eglGetConfigs(gr->egl_display, NULL, 0, &count) || count < 1)
1489 return -1;
1490
1491 configs = calloc(count, sizeof *configs);
1492 if (!configs)
1493 return -1;
1494
1495 if (!eglChooseConfig(gr->egl_display, attribs, configs,
1496 count, &matched))
1497 goto out;
1498
1499 for (i = 0; i < matched; ++i) {
1500 EGLint id;
1501
1502 if (visual_id) {
1503 if (!eglGetConfigAttrib(gr->egl_display,
1504 configs[i], EGL_NATIVE_VISUAL_ID,
1505 &id))
1506 continue;
1507
1508 if (id != *visual_id)
1509 continue;
1510 }
1511
1512 gr->egl_config = configs[i];
1513
1514 free(configs);
1515 return 0;
1516 }
1517
1518out:
1519 free(configs);
1520 return -1;
1521}
1522
1523WL_EXPORT const EGLint gles2_renderer_opaque_attribs[] = {
1524 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1525 EGL_RED_SIZE, 1,
1526 EGL_GREEN_SIZE, 1,
1527 EGL_BLUE_SIZE, 1,
1528 EGL_ALPHA_SIZE, 0,
1529 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1530 EGL_NONE
1531};
1532
1533WL_EXPORT const EGLint gles2_renderer_alpha_attribs[] = {
1534 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1535 EGL_RED_SIZE, 1,
1536 EGL_GREEN_SIZE, 1,
1537 EGL_BLUE_SIZE, 1,
1538 EGL_ALPHA_SIZE, 1,
1539 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1540 EGL_NONE
1541};
1542
1543WL_EXPORT int
1544gles2_renderer_create(struct weston_compositor *ec, EGLNativeDisplayType display,
1545 const EGLint *attribs, const EGLint *visual_id)
1546{
1547 struct gles2_renderer *gr;
1548 EGLint major, minor;
1549
1550 gr = calloc(1, sizeof *gr);
1551
1552 if (gr == NULL)
1553 return -1;
1554
1555 gr->base.repaint_output = gles2_renderer_repaint_output;
1556 gr->base.flush_damage = gles2_renderer_flush_damage;
1557 gr->base.attach = gles2_renderer_attach;
1558 gr->base.destroy_surface = gles2_renderer_destroy_surface;
1559
1560 gr->egl_display = eglGetDisplay(display);
1561 if (gr->egl_display == EGL_NO_DISPLAY) {
1562 weston_log("failed to create display\n");
1563 goto err_egl;
1564 }
1565
1566 if (!eglInitialize(gr->egl_display, &major, &minor)) {
1567 weston_log("failed to initialize display\n");
1568 goto err_egl;
1569 }
1570
1571 if (egl_choose_config(gr, attribs, visual_id) < 0) {
1572 weston_log("failed to choose EGL config\n");
1573 goto err_egl;
1574 }
1575
1576 ec->renderer = &gr->base;
1577
1578 return 0;
1579
1580err_egl:
1581 print_egl_error_state();
1582 free(gr);
1583 return -1;
1584}
1585
1586WL_EXPORT EGLDisplay
1587gles2_renderer_display(struct weston_compositor *ec)
1588{
1589 return get_renderer(ec)->egl_display;
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001590}
1591
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001592static int
1593compile_shaders(struct weston_compositor *ec)
1594{
1595 if (weston_shader_init(&ec->texture_shader_rgba, ec,
1596 vertex_shader, texture_fragment_shader_rgba) < 0)
1597 return -1;
1598 if (weston_shader_init(&ec->texture_shader_rgbx, ec,
1599 vertex_shader, texture_fragment_shader_rgbx) < 0)
1600 return -1;
1601 if (ec->has_egl_image_external &&
1602 weston_shader_init(&ec->texture_shader_egl_external, ec,
1603 vertex_shader, texture_fragment_shader_egl_external) < 0)
1604 return -1;
1605 if (weston_shader_init(&ec->texture_shader_y_uv, ec,
1606 vertex_shader, texture_fragment_shader_y_uv) < 0)
1607 return -1;
1608 if (weston_shader_init(&ec->texture_shader_y_u_v, ec,
1609 vertex_shader, texture_fragment_shader_y_u_v) < 0)
1610 return -1;
1611 if (weston_shader_init(&ec->texture_shader_y_xuxv, ec,
1612 vertex_shader, texture_fragment_shader_y_xuxv) < 0)
1613 return -1;
1614 if (weston_shader_init(&ec->solid_shader, ec,
1615 vertex_shader, solid_fragment_shader) < 0)
1616 return -1;
1617
1618 return 0;
1619}
1620
1621static void
1622fragment_debug_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
1623 void *data)
1624{
1625 struct weston_compositor *ec = data;
1626 struct gles2_renderer *renderer =
1627 (struct gles2_renderer *) ec->renderer;
1628 struct weston_output *output;
1629
1630 renderer->fragment_shader_debug ^= 1;
1631
1632 weston_shader_release(&ec->texture_shader_rgba);
1633 weston_shader_release(&ec->texture_shader_rgbx);
1634 weston_shader_release(&ec->texture_shader_egl_external);
1635 weston_shader_release(&ec->texture_shader_y_uv);
1636 weston_shader_release(&ec->texture_shader_y_u_v);
1637 weston_shader_release(&ec->texture_shader_y_xuxv);
1638 weston_shader_release(&ec->solid_shader);
1639
1640 compile_shaders(ec);
1641
1642 wl_list_for_each(output, &ec->output_list, link)
1643 weston_output_damage(output);
1644}
1645
John Kåre Alsaker94659272012-11-13 19:10:18 +01001646static int
1647gles2_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001648{
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001649 struct gles2_renderer *gr = get_renderer(ec);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001650 const char *extensions;
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001651 EGLBoolean ret;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001652
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001653 static const EGLint context_attribs[] = {
1654 EGL_CONTEXT_CLIENT_VERSION, 2,
1655 EGL_NONE
1656 };
1657
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001658 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
1659 weston_log("failed to bind EGL_OPENGL_ES_API\n");
1660 print_egl_error_state();
1661 return -1;
1662 }
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001663
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001664 log_egl_config_info(gr->egl_display, gr->egl_config);
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001665
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001666 gr->egl_context = eglCreateContext(gr->egl_display, gr->egl_config,
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001667 EGL_NO_CONTEXT, context_attribs);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001668 if (gr->egl_context == NULL) {
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001669 weston_log("failed to create context\n");
1670 print_egl_error_state();
1671 return -1;
1672 }
1673
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001674 ret = eglMakeCurrent(gr->egl_display, egl_surface,
1675 egl_surface, gr->egl_context);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001676 if (ret == EGL_FALSE) {
1677 weston_log("Failed to make EGL context current.\n");
1678 print_egl_error_state();
1679 return -1;
1680 }
1681
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001682 log_egl_gl_info(gr->egl_display);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001683
1684 ec->image_target_texture_2d =
1685 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
1686 ec->image_target_renderbuffer_storage = (void *)
1687 eglGetProcAddress("glEGLImageTargetRenderbufferStorageOES");
1688 ec->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1689 ec->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1690 ec->bind_display =
1691 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
1692 ec->unbind_display =
1693 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
1694 ec->query_buffer =
1695 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
1696
1697 extensions = (const char *) glGetString(GL_EXTENSIONS);
1698 if (!extensions) {
1699 weston_log("Retrieving GL extension string failed.\n");
1700 return -1;
1701 }
1702
1703 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
1704 weston_log("GL_EXT_texture_format_BGRA8888 not available\n");
1705 return -1;
1706 }
1707
1708 if (strstr(extensions, "GL_EXT_read_format_bgra"))
1709 ec->read_format = GL_BGRA_EXT;
1710 else
1711 ec->read_format = GL_RGBA;
1712
1713 if (strstr(extensions, "GL_EXT_unpack_subimage"))
1714 ec->has_unpack_subimage = 1;
1715
1716 if (strstr(extensions, "GL_OES_EGL_image_external"))
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001717 ec->has_egl_image_external = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001718
1719 extensions =
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001720 (const char *) eglQueryString(gr->egl_display, EGL_EXTENSIONS);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001721 if (!extensions) {
1722 weston_log("Retrieving EGL extension string failed.\n");
1723 return -1;
1724 }
1725
1726 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
1727 ec->has_bind_display = 1;
Pekka Paalanen035a0322012-10-24 09:43:06 +03001728 if (ec->has_bind_display) {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001729 ret = ec->bind_display(gr->egl_display, ec->wl_display);
Pekka Paalanen035a0322012-10-24 09:43:06 +03001730 if (!ret)
1731 ec->has_bind_display = 0;
1732 }
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001733
1734 glActiveTexture(GL_TEXTURE0);
1735
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001736 if (compile_shaders(ec))
1737 return -1;
1738
1739 weston_compositor_add_debug_binding(ec, KEY_S,
1740 fragment_debug_binding, ec);
1741
Pekka Paalanen035a0322012-10-24 09:43:06 +03001742 weston_log("GL ES 2 renderer features:\n");
1743 weston_log_continue(STAMP_SPACE "read-back format: %s\n",
1744 ec->read_format == GL_BGRA_EXT ? "BGRA" : "RGBA");
1745 weston_log_continue(STAMP_SPACE "wl_shm sub-image to texture: %s\n",
1746 ec->has_unpack_subimage ? "yes" : "no");
1747 weston_log_continue(STAMP_SPACE "EGL Wayland extension: %s\n",
1748 ec->has_bind_display ? "yes" : "no");
1749
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001750
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001751 return 0;
1752}