blob: 27c23ebc8b0a8a2bc4d15d98ec5ab9bdfce7f9c2 [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
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100611static int
612use_output(struct weston_output *output)
613{
614 static int errored;
615 struct gles2_output_state *go = get_output_state(output);
616 struct gles2_renderer *gr = get_renderer(output->compositor);
617 EGLBoolean ret;
618
619 ret = eglMakeCurrent(gr->egl_display, go->egl_surface,
620 go->egl_surface, gr->egl_context);
621
622 if (ret == EGL_FALSE) {
623 if (errored)
624 return -1;
625 errored = 1;
626 weston_log("Failed to make EGL context current.\n");
627 print_egl_error_state();
628 return -1;
629 }
630
631 return 0;
632}
633
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400634static void
635weston_compositor_use_shader(struct weston_compositor *compositor,
636 struct weston_shader *shader)
637{
638 if (compositor->current_shader == shader)
639 return;
640
641 glUseProgram(shader->program);
642 compositor->current_shader = shader;
643}
644
645static void
646weston_shader_uniforms(struct weston_shader *shader,
647 struct weston_surface *surface,
648 struct weston_output *output)
649{
650 int i;
651
652 glUniformMatrix4fv(shader->proj_uniform,
653 1, GL_FALSE, output->matrix.d);
654 glUniform4fv(shader->color_uniform, 1, surface->color);
655 glUniform1f(shader->alpha_uniform, surface->alpha);
656
657 for (i = 0; i < surface->num_textures; i++)
658 glUniform1i(shader->tex_uniforms[i], i);
659}
660
661static void
662draw_surface(struct weston_surface *es, struct weston_output *output,
663 pixman_region32_t *damage) /* in global coordinates */
664{
665 struct weston_compositor *ec = es->compositor;
666 /* repaint bounding region in global coordinates: */
667 pixman_region32_t repaint;
668 /* non-opaque region in surface coordinates: */
669 pixman_region32_t surface_blend;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300670 pixman_region32_t *buffer_damage;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400671 GLint filter;
672 int i;
673
674 pixman_region32_init(&repaint);
675 pixman_region32_intersect(&repaint,
676 &es->transform.boundingbox, damage);
677 pixman_region32_subtract(&repaint, &repaint, &es->clip);
678
679 if (!pixman_region32_not_empty(&repaint))
680 goto out;
681
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300682 buffer_damage = &output->buffer_damage[output->current_buffer];
683 pixman_region32_subtract(buffer_damage, buffer_damage, &repaint);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400684
685 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
686
687 if (ec->fan_debug) {
688 weston_compositor_use_shader(ec, &ec->solid_shader);
689 weston_shader_uniforms(&ec->solid_shader, es, output);
690 }
691
692 weston_compositor_use_shader(ec, es->shader);
693 weston_shader_uniforms(es->shader, es, output);
694
695 if (es->transform.enabled || output->zoom.active)
696 filter = GL_LINEAR;
697 else
698 filter = GL_NEAREST;
699
700 for (i = 0; i < es->num_textures; i++) {
701 glActiveTexture(GL_TEXTURE0 + i);
702 glBindTexture(es->target, es->textures[i]);
703 glTexParameteri(es->target, GL_TEXTURE_MIN_FILTER, filter);
704 glTexParameteri(es->target, GL_TEXTURE_MAG_FILTER, filter);
705 }
706
707 /* blended region is whole surface minus opaque region: */
708 pixman_region32_init_rect(&surface_blend, 0, 0,
709 es->geometry.width, es->geometry.height);
710 pixman_region32_subtract(&surface_blend, &surface_blend, &es->opaque);
711
712 if (pixman_region32_not_empty(&es->opaque)) {
713 if (es->shader == &ec->texture_shader_rgba) {
714 /* Special case for RGBA textures with possibly
715 * bad data in alpha channel: use the shader
716 * that forces texture alpha = 1.0.
717 * Xwayland surfaces need this.
718 */
719 weston_compositor_use_shader(ec, &ec->texture_shader_rgbx);
720 weston_shader_uniforms(&ec->texture_shader_rgbx, es, output);
721 }
722
723 if (es->alpha < 1.0)
724 glEnable(GL_BLEND);
725 else
726 glDisable(GL_BLEND);
727
728 repaint_region(es, &repaint, &es->opaque);
729 }
730
731 if (pixman_region32_not_empty(&surface_blend)) {
732 weston_compositor_use_shader(ec, es->shader);
733 glEnable(GL_BLEND);
734 repaint_region(es, &repaint, &surface_blend);
735 }
736
737 pixman_region32_fini(&surface_blend);
738
739out:
740 pixman_region32_fini(&repaint);
741}
742
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400743static void
744repaint_surfaces(struct weston_output *output, pixman_region32_t *damage)
745{
746 struct weston_compositor *compositor = output->compositor;
747 struct weston_surface *surface;
748
749 wl_list_for_each_reverse(surface, &compositor->surface_list, link)
750 if (surface->plane == &compositor->primary_plane)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400751 draw_surface(surface, output, damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400752}
753
John Kåre Alsaker44154502012-11-13 19:10:20 +0100754
755static int
756texture_border(struct weston_output *output)
757{
758 struct weston_compositor *ec = output->compositor;
759 struct gles2_renderer *gr = get_renderer(ec);
760 GLfloat *d;
761 unsigned int *p;
762 int i, j, k, n;
763 GLfloat x[4], y[4], u[4], v[4];
764
765 x[0] = -gr->border.left;
766 x[1] = 0;
767 x[2] = output->current->width;
768 x[3] = output->current->width + gr->border.right;
769
770 y[0] = -gr->border.top;
771 y[1] = 0;
772 y[2] = output->current->height;
773 y[3] = output->current->height + gr->border.bottom;
774
775 u[0] = 0.0;
776 u[1] = (GLfloat) gr->border.left / gr->border.width;
777 u[2] = (GLfloat) (gr->border.width - gr->border.right) / gr->border.width;
778 u[3] = 1.0;
779
780 v[0] = 0.0;
781 v[1] = (GLfloat) gr->border.top / gr->border.height;
782 v[2] = (GLfloat) (gr->border.height - gr->border.bottom) / gr->border.height;
783 v[3] = 1.0;
784
785 n = 8;
786 d = wl_array_add(&ec->vertices, n * 16 * sizeof *d);
787 p = wl_array_add(&ec->indices, n * 6 * sizeof *p);
788
789 k = 0;
790 for (i = 0; i < 3; i++)
791 for (j = 0; j < 3; j++) {
792
793 if (i == 1 && j == 1)
794 continue;
795
796 d[ 0] = x[i];
797 d[ 1] = y[j];
798 d[ 2] = u[i];
799 d[ 3] = v[j];
800
801 d[ 4] = x[i];
802 d[ 5] = y[j + 1];
803 d[ 6] = u[i];
804 d[ 7] = v[j + 1];
805
806 d[ 8] = x[i + 1];
807 d[ 9] = y[j];
808 d[10] = u[i + 1];
809 d[11] = v[j];
810
811 d[12] = x[i + 1];
812 d[13] = y[j + 1];
813 d[14] = u[i + 1];
814 d[15] = v[j + 1];
815
816 p[0] = k + 0;
817 p[1] = k + 1;
818 p[2] = k + 2;
819 p[3] = k + 2;
820 p[4] = k + 1;
821 p[5] = k + 3;
822
823 d += 16;
824 p += 6;
825 k += 4;
826 }
827
828 return k / 4;
829}
830
831static void
832draw_border(struct weston_output *output)
833{
834 struct weston_compositor *ec = output->compositor;
835 struct gles2_renderer *gr = get_renderer(ec);
836 struct weston_shader *shader = &ec->texture_shader_rgba;
837 GLfloat *v;
838 int n;
839
840 glDisable(GL_BLEND);
841 glUseProgram(shader->program);
842 ec->current_shader = shader;
843
844 glUniformMatrix4fv(shader->proj_uniform,
845 1, GL_FALSE, output->matrix.d);
846
847 glUniform1i(shader->tex_uniforms[0], 0);
848 glUniform1f(shader->alpha_uniform, 1);
849
850 n = texture_border(output);
851
852 glActiveTexture(GL_TEXTURE0);
853 glBindTexture(GL_TEXTURE_2D, gr->border.texture);
854
855 v = ec->vertices.data;
856 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
857 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
858 glEnableVertexAttribArray(0);
859 glEnableVertexAttribArray(1);
860
861 glDrawElements(GL_TRIANGLES, n * 6,
862 GL_UNSIGNED_INT, ec->indices.data);
863
864 glDisableVertexAttribArray(1);
865 glDisableVertexAttribArray(0);
866
867 ec->vertices.size = 0;
868 ec->indices.size = 0;
869}
870
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400871static void
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400872gles2_renderer_repaint_output(struct weston_output *output,
873 pixman_region32_t *output_damage)
874{
John Kåre Alsaker94659272012-11-13 19:10:18 +0100875 struct gles2_output_state *go = get_output_state(output);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400876 struct weston_compositor *compositor = output->compositor;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100877 struct gles2_renderer *gr = get_renderer(compositor);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400878 EGLBoolean ret;
879 static int errored;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300880 int32_t width, height, i;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400881
882 width = output->current->width +
883 output->border.left + output->border.right;
884 height = output->current->height +
885 output->border.top + output->border.bottom;
886
887 glViewport(0, 0, width, height);
888
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100889 if (use_output(output) < 0)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400890 return;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400891
892 /* if debugging, redraw everything outside the damage to clean up
893 * debug lines from the previous draw on this buffer:
894 */
895 if (compositor->fan_debug) {
896 pixman_region32_t undamaged;
897 pixman_region32_init(&undamaged);
898 pixman_region32_subtract(&undamaged, &output->region,
899 output_damage);
900 compositor->fan_debug = 0;
901 repaint_surfaces(output, &undamaged);
902 compositor->fan_debug = 1;
903 pixman_region32_fini(&undamaged);
904 }
905
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300906 for (i = 0; i < 2; i++)
907 pixman_region32_union(&output->buffer_damage[i],
908 &output->buffer_damage[i],
909 output_damage);
910
911 pixman_region32_union(output_damage, output_damage,
912 &output->buffer_damage[output->current_buffer]);
913
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400914 repaint_surfaces(output, output_damage);
915
John Kåre Alsaker44154502012-11-13 19:10:20 +0100916 if (gr->border.texture)
917 draw_border(output);
918
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400919 wl_signal_emit(&output->frame_signal, output);
920
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100921 ret = eglSwapBuffers(gr->egl_display, go->egl_surface);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400922 if (ret == EGL_FALSE && !errored) {
923 errored = 1;
924 weston_log("Failed in eglSwapBuffers.\n");
925 print_egl_error_state();
926 }
927
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300928 output->current_buffer ^= 1;
929
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400930}
Kristian Høgsberg25894fc2012-09-05 22:06:26 -0400931
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100932static int
933gles2_renderer_read_pixels(struct weston_output *output,
934 pixman_format_code_t format, void *pixels,
935 uint32_t x, uint32_t y,
936 uint32_t width, uint32_t height)
937{
938 GLenum gl_format;
939
940 switch (format) {
941 case PIXMAN_a8r8g8b8:
942 gl_format = GL_BGRA_EXT;
943 break;
944 case PIXMAN_a8b8g8r8:
945 gl_format = GL_RGBA;
946 break;
947 default:
948 return -1;
949 }
950
951 if (use_output(output) < 0)
952 return -1;
953
954 glPixelStorei(GL_PACK_ALIGNMENT, 1);
955 glReadPixels(x, y, width, height, gl_format,
956 GL_UNSIGNED_BYTE, pixels);
957
958 return 0;
959}
960
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400961static void
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -0400962gles2_renderer_flush_damage(struct weston_surface *surface)
963{
964#ifdef GL_UNPACK_ROW_LENGTH
965 pixman_box32_t *rectangles;
966 void *data;
967 int i, n;
968#endif
969
Pekka Paalanenbcdd5792012-11-07 12:25:13 +0200970 pixman_region32_union(&surface->texture_damage,
971 &surface->texture_damage, &surface->damage);
972
973 /* Avoid upload, if the texture won't be used this time.
974 * We still accumulate the damage in texture_damage.
975 */
976 if (surface->plane != &surface->compositor->primary_plane)
977 return;
978
979 if (!pixman_region32_not_empty(&surface->texture_damage))
980 return;
981
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -0400982 glBindTexture(GL_TEXTURE_2D, surface->textures[0]);
983
984 if (!surface->compositor->has_unpack_subimage) {
985 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
986 surface->pitch, surface->buffer->height, 0,
987 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
988 wl_shm_buffer_get_data(surface->buffer));
989
Pekka Paalanenbcdd5792012-11-07 12:25:13 +0200990 goto done;
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -0400991 }
992
993#ifdef GL_UNPACK_ROW_LENGTH
994 /* Mesa does not define GL_EXT_unpack_subimage */
995 glPixelStorei(GL_UNPACK_ROW_LENGTH, surface->pitch);
996 data = wl_shm_buffer_get_data(surface->buffer);
Pekka Paalanenbcdd5792012-11-07 12:25:13 +0200997 rectangles = pixman_region32_rectangles(&surface->texture_damage, &n);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -0400998 for (i = 0; i < n; i++) {
999 glPixelStorei(GL_UNPACK_SKIP_PIXELS, rectangles[i].x1);
1000 glPixelStorei(GL_UNPACK_SKIP_ROWS, rectangles[i].y1);
1001 glTexSubImage2D(GL_TEXTURE_2D, 0,
1002 rectangles[i].x1, rectangles[i].y1,
1003 rectangles[i].x2 - rectangles[i].x1,
1004 rectangles[i].y2 - rectangles[i].y1,
1005 GL_BGRA_EXT, GL_UNSIGNED_BYTE, data);
1006 }
1007#endif
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001008
1009done:
1010 pixman_region32_fini(&surface->texture_damage);
1011 pixman_region32_init(&surface->texture_damage);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001012}
1013
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001014static void
1015ensure_textures(struct weston_surface *es, int num_textures)
1016{
1017 int i;
1018
1019 if (num_textures <= es->num_textures)
1020 return;
1021
1022 for (i = es->num_textures; i < num_textures; i++) {
1023 glGenTextures(1, &es->textures[i]);
1024 glBindTexture(es->target, es->textures[i]);
1025 glTexParameteri(es->target,
1026 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1027 glTexParameteri(es->target,
1028 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1029 }
1030 es->num_textures = num_textures;
1031 glBindTexture(es->target, 0);
1032}
1033
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001034static void
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001035gles2_renderer_attach(struct weston_surface *es, struct wl_buffer *buffer)
1036{
1037 struct weston_compositor *ec = es->compositor;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001038 struct gles2_renderer *gr = get_renderer(ec);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001039 EGLint attribs[3], format;
1040 int i, num_planes;
1041
1042 if (!buffer) {
1043 for (i = 0; i < es->num_images; i++) {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001044 ec->destroy_image(gr->egl_display, es->images[i]);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001045 es->images[i] = NULL;
1046 }
1047 es->num_images = 0;
1048 glDeleteTextures(es->num_textures, es->textures);
1049 es->num_textures = 0;
1050 return;
1051 }
1052
1053 if (wl_buffer_is_shm(buffer)) {
1054 es->pitch = wl_shm_buffer_get_stride(buffer) / 4;
1055 es->target = GL_TEXTURE_2D;
1056
1057 ensure_textures(es, 1);
1058 glBindTexture(GL_TEXTURE_2D, es->textures[0]);
1059 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1060 es->pitch, buffer->height, 0,
1061 GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
1062 if (wl_shm_buffer_get_format(buffer) == WL_SHM_FORMAT_XRGB8888)
1063 es->shader = &ec->texture_shader_rgbx;
1064 else
1065 es->shader = &ec->texture_shader_rgba;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001066 } else if (ec->query_buffer(gr->egl_display, buffer,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001067 EGL_TEXTURE_FORMAT, &format)) {
1068 for (i = 0; i < es->num_images; i++)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001069 ec->destroy_image(gr->egl_display, es->images[i]);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001070 es->num_images = 0;
1071 es->target = GL_TEXTURE_2D;
1072 switch (format) {
1073 case EGL_TEXTURE_RGB:
1074 case EGL_TEXTURE_RGBA:
1075 default:
1076 num_planes = 1;
1077 es->shader = &ec->texture_shader_rgba;
1078 break;
1079 case EGL_TEXTURE_EXTERNAL_WL:
1080 num_planes = 1;
1081 es->target = GL_TEXTURE_EXTERNAL_OES;
1082 es->shader = &ec->texture_shader_egl_external;
1083 break;
1084 case EGL_TEXTURE_Y_UV_WL:
1085 num_planes = 2;
1086 es->shader = &ec->texture_shader_y_uv;
1087 break;
1088 case EGL_TEXTURE_Y_U_V_WL:
1089 num_planes = 3;
1090 es->shader = &ec->texture_shader_y_u_v;
1091 break;
1092 case EGL_TEXTURE_Y_XUXV_WL:
1093 num_planes = 2;
1094 es->shader = &ec->texture_shader_y_xuxv;
1095 break;
1096 }
1097
1098 ensure_textures(es, num_planes);
1099 for (i = 0; i < num_planes; i++) {
1100 attribs[0] = EGL_WAYLAND_PLANE_WL;
1101 attribs[1] = i;
1102 attribs[2] = EGL_NONE;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001103 es->images[i] = ec->create_image(gr->egl_display,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001104 NULL,
1105 EGL_WAYLAND_BUFFER_WL,
1106 buffer, attribs);
1107 if (!es->images[i]) {
1108 weston_log("failed to create img for plane %d\n", i);
1109 continue;
1110 }
1111 es->num_images++;
1112
1113 glActiveTexture(GL_TEXTURE0 + i);
1114 glBindTexture(es->target, es->textures[i]);
1115 ec->image_target_texture_2d(es->target,
1116 es->images[i]);
1117 }
1118
1119 es->pitch = buffer->width;
1120 } else {
1121 weston_log("unhandled buffer type!\n");
1122 }
1123}
1124
Kristian Høgsberg42263852012-09-06 21:59:29 -04001125static void
1126gles2_renderer_destroy_surface(struct weston_surface *surface)
1127{
1128 struct weston_compositor *ec = surface->compositor;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001129 struct gles2_renderer *gr = get_renderer(ec);
Kristian Høgsberg42263852012-09-06 21:59:29 -04001130 int i;
1131
1132 glDeleteTextures(surface->num_textures, surface->textures);
1133
1134 for (i = 0; i < surface->num_images; i++)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001135 ec->destroy_image(gr->egl_display, surface->images[i]);
Kristian Høgsberg42263852012-09-06 21:59:29 -04001136}
1137
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001138static const char vertex_shader[] =
1139 "uniform mat4 proj;\n"
1140 "attribute vec2 position;\n"
1141 "attribute vec2 texcoord;\n"
1142 "varying vec2 v_texcoord;\n"
1143 "void main()\n"
1144 "{\n"
1145 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
1146 " v_texcoord = texcoord;\n"
1147 "}\n";
1148
1149/* Declare common fragment shader uniforms */
1150#define FRAGMENT_CONVERT_YUV \
1151 " y *= alpha;\n" \
1152 " u *= alpha;\n" \
1153 " v *= alpha;\n" \
1154 " gl_FragColor.r = y + 1.59602678 * v;\n" \
1155 " gl_FragColor.g = y - 0.39176229 * u - 0.81296764 * v;\n" \
1156 " gl_FragColor.b = y + 2.01723214 * u;\n" \
1157 " gl_FragColor.a = alpha;\n"
1158
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001159static const char fragment_debug[] =
1160 " gl_FragColor = vec4(0.0, 0.3, 0.0, 0.2) + gl_FragColor * 0.8;\n";
1161
1162static const char fragment_brace[] =
1163 "}\n";
1164
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001165static const char texture_fragment_shader_rgba[] =
1166 "precision mediump float;\n"
1167 "varying vec2 v_texcoord;\n"
1168 "uniform sampler2D tex;\n"
1169 "uniform float alpha;\n"
1170 "void main()\n"
1171 "{\n"
1172 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001173 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001174
1175static const char texture_fragment_shader_rgbx[] =
1176 "precision mediump float;\n"
1177 "varying vec2 v_texcoord;\n"
1178 "uniform sampler2D tex;\n"
1179 "uniform float alpha;\n"
1180 "void main()\n"
1181 "{\n"
1182 " gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb\n;"
1183 " gl_FragColor.a = alpha;\n"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001184 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001185
1186static const char texture_fragment_shader_egl_external[] =
1187 "#extension GL_OES_EGL_image_external : require\n"
1188 "precision mediump float;\n"
1189 "varying vec2 v_texcoord;\n"
1190 "uniform samplerExternalOES tex;\n"
1191 "uniform float alpha;\n"
1192 "void main()\n"
1193 "{\n"
1194 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001195 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001196
1197static const char texture_fragment_shader_y_uv[] =
1198 "precision mediump float;\n"
1199 "uniform sampler2D tex;\n"
1200 "uniform sampler2D tex1;\n"
1201 "varying vec2 v_texcoord;\n"
1202 "uniform float alpha;\n"
1203 "void main() {\n"
1204 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1205 " float u = texture2D(tex1, v_texcoord).r - 0.5;\n"
1206 " float v = texture2D(tex1, v_texcoord).g - 0.5;\n"
1207 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001208 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001209
1210static const char texture_fragment_shader_y_u_v[] =
1211 "precision mediump float;\n"
1212 "uniform sampler2D tex;\n"
1213 "uniform sampler2D tex1;\n"
1214 "uniform sampler2D tex2;\n"
1215 "varying vec2 v_texcoord;\n"
1216 "uniform float alpha;\n"
1217 "void main() {\n"
1218 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1219 " float u = texture2D(tex1, v_texcoord).x - 0.5;\n"
1220 " float v = texture2D(tex2, v_texcoord).x - 0.5;\n"
1221 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001222 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001223
1224static const char texture_fragment_shader_y_xuxv[] =
1225 "precision mediump float;\n"
1226 "uniform sampler2D tex;\n"
1227 "uniform sampler2D tex1;\n"
1228 "varying vec2 v_texcoord;\n"
1229 "uniform float alpha;\n"
1230 "void main() {\n"
1231 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1232 " float u = texture2D(tex1, v_texcoord).g - 0.5;\n"
1233 " float v = texture2D(tex1, v_texcoord).a - 0.5;\n"
1234 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001235 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001236
1237static const char solid_fragment_shader[] =
1238 "precision mediump float;\n"
1239 "uniform vec4 color;\n"
1240 "uniform float alpha;\n"
1241 "void main()\n"
1242 "{\n"
1243 " gl_FragColor = alpha * color\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001244 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001245
1246static int
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001247compile_shader(GLenum type, int count, const char **sources)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001248{
1249 GLuint s;
1250 char msg[512];
1251 GLint status;
1252
1253 s = glCreateShader(type);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001254 glShaderSource(s, count, sources, NULL);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001255 glCompileShader(s);
1256 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
1257 if (!status) {
1258 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1259 weston_log("shader info: %s\n", msg);
1260 return GL_NONE;
1261 }
1262
1263 return s;
1264}
1265
1266static int
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001267weston_shader_init(struct weston_shader *shader, struct weston_compositor *ec,
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001268 const char *vertex_source, const char *fragment_source)
1269{
1270 char msg[512];
1271 GLint status;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001272 int count;
1273 const char *sources[3];
1274 struct gles2_renderer *renderer =
1275 (struct gles2_renderer *) ec->renderer;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001276
1277 shader->vertex_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001278 compile_shader(GL_VERTEX_SHADER, 1, &vertex_source);
1279
1280 if (renderer->fragment_shader_debug) {
1281 sources[0] = fragment_source;
1282 sources[1] = fragment_debug;
1283 sources[2] = fragment_brace;
1284 count = 3;
1285 } else {
1286 sources[0] = fragment_source;
1287 sources[1] = fragment_brace;
1288 count = 2;
1289 }
1290
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001291 shader->fragment_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001292 compile_shader(GL_FRAGMENT_SHADER, count, sources);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001293
1294 shader->program = glCreateProgram();
1295 glAttachShader(shader->program, shader->vertex_shader);
1296 glAttachShader(shader->program, shader->fragment_shader);
1297 glBindAttribLocation(shader->program, 0, "position");
1298 glBindAttribLocation(shader->program, 1, "texcoord");
1299
1300 glLinkProgram(shader->program);
1301 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1302 if (!status) {
1303 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1304 weston_log("link info: %s\n", msg);
1305 return -1;
1306 }
1307
1308 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1309 shader->tex_uniforms[0] = glGetUniformLocation(shader->program, "tex");
1310 shader->tex_uniforms[1] = glGetUniformLocation(shader->program, "tex1");
1311 shader->tex_uniforms[2] = glGetUniformLocation(shader->program, "tex2");
1312 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
1313 shader->color_uniform = glGetUniformLocation(shader->program, "color");
1314
1315 return 0;
1316}
1317
1318static void
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001319weston_shader_release(struct weston_shader *shader)
1320{
1321 glDeleteShader(shader->vertex_shader);
1322 glDeleteShader(shader->fragment_shader);
1323 glDeleteProgram(shader->program);
1324
1325 shader->vertex_shader = 0;
1326 shader->fragment_shader = 0;
1327 shader->program = 0;
1328}
1329
1330static void
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001331log_extensions(const char *name, const char *extensions)
1332{
1333 const char *p, *end;
1334 int l;
1335 int len;
1336
1337 l = weston_log("%s:", name);
1338 p = extensions;
1339 while (*p) {
1340 end = strchrnul(p, ' ');
1341 len = end - p;
1342 if (l + len > 78)
1343 l = weston_log_continue("\n" STAMP_SPACE "%.*s",
1344 len, p);
1345 else
1346 l += weston_log_continue(" %.*s", len, p);
1347 for (p = end; isspace(*p); p++)
1348 ;
1349 }
1350 weston_log_continue("\n");
1351}
1352
1353static void
1354log_egl_gl_info(EGLDisplay egldpy)
1355{
1356 const char *str;
1357
1358 str = eglQueryString(egldpy, EGL_VERSION);
1359 weston_log("EGL version: %s\n", str ? str : "(null)");
1360
1361 str = eglQueryString(egldpy, EGL_VENDOR);
1362 weston_log("EGL vendor: %s\n", str ? str : "(null)");
1363
1364 str = eglQueryString(egldpy, EGL_CLIENT_APIS);
1365 weston_log("EGL client APIs: %s\n", str ? str : "(null)");
1366
1367 str = eglQueryString(egldpy, EGL_EXTENSIONS);
1368 log_extensions("EGL extensions", str ? str : "(null)");
1369
1370 str = (char *)glGetString(GL_VERSION);
1371 weston_log("GL version: %s\n", str ? str : "(null)");
1372
1373 str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
1374 weston_log("GLSL version: %s\n", str ? str : "(null)");
1375
1376 str = (char *)glGetString(GL_VENDOR);
1377 weston_log("GL vendor: %s\n", str ? str : "(null)");
1378
1379 str = (char *)glGetString(GL_RENDERER);
1380 weston_log("GL renderer: %s\n", str ? str : "(null)");
1381
1382 str = (char *)glGetString(GL_EXTENSIONS);
1383 log_extensions("GL extensions", str ? str : "(null)");
1384}
1385
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001386static void
1387log_egl_config_info(EGLDisplay egldpy, EGLConfig eglconfig)
1388{
1389 EGLint r, g, b, a;
1390
1391 weston_log("Chosen EGL config details:\n");
1392
1393 weston_log_continue(STAMP_SPACE "RGBA bits");
1394 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_RED_SIZE, &r) &&
1395 eglGetConfigAttrib(egldpy, eglconfig, EGL_GREEN_SIZE, &g) &&
1396 eglGetConfigAttrib(egldpy, eglconfig, EGL_BLUE_SIZE, &b) &&
1397 eglGetConfigAttrib(egldpy, eglconfig, EGL_ALPHA_SIZE, &a))
1398 weston_log_continue(": %d %d %d %d\n", r, g, b, a);
1399 else
1400 weston_log_continue(" unknown\n");
1401
1402 weston_log_continue(STAMP_SPACE "swap interval range");
1403 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_MIN_SWAP_INTERVAL, &a) &&
1404 eglGetConfigAttrib(egldpy, eglconfig, EGL_MAX_SWAP_INTERVAL, &b))
1405 weston_log_continue(": %d - %d\n", a, b);
1406 else
1407 weston_log_continue(" unknown\n");
1408}
1409
John Kåre Alsaker44154502012-11-13 19:10:20 +01001410static void
1411output_apply_border(struct weston_output *output, struct gles2_renderer *gr)
1412{
1413 output->border.top = gr->border.top;
1414 output->border.bottom = gr->border.bottom;
1415 output->border.left = gr->border.left;
1416 output->border.right = gr->border.right;
1417}
1418
1419WL_EXPORT void
1420gles2_renderer_set_border(struct weston_compositor *ec, int32_t width, int32_t height, void *data,
1421 int32_t *edges)
1422{
1423 struct gles2_renderer *gr = get_renderer(ec);
1424 struct weston_output *output;
1425
1426 gr->border.left = edges[0];
1427 gr->border.right = edges[1];
1428 gr->border.top = edges[2];
1429 gr->border.bottom = edges[3];
1430
1431 gr->border.width = width;
1432 gr->border.height = height;
1433
1434 glGenTextures(1, &gr->border.texture);
1435 glBindTexture(GL_TEXTURE_2D, gr->border.texture);
1436 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1437 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1438 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1439 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1440
1441 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1442 width,
1443 height,
1444 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1445 data);
1446
1447 wl_list_for_each(output, &ec->output_list, link)
1448 output_apply_border(output, gr);
1449}
1450
John Kåre Alsaker94659272012-11-13 19:10:18 +01001451static int
1452gles2_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface);
1453
1454WL_EXPORT int
1455gles2_renderer_output_create(struct weston_output *output,
1456 EGLNativeWindowType window)
1457{
1458 struct weston_compositor *ec = output->compositor;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001459 struct gles2_renderer *gr = get_renderer(ec);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001460 struct gles2_output_state *go = calloc(1, sizeof *go);
1461
1462 if (!go)
1463 return -1;
1464
1465 go->egl_surface =
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001466 eglCreateWindowSurface(gr->egl_display,
1467 gr->egl_config,
John Kåre Alsaker94659272012-11-13 19:10:18 +01001468 window, NULL);
1469
1470 if (go->egl_surface == EGL_NO_SURFACE) {
1471 weston_log("failed to create egl surface\n");
1472 free(go);
1473 return -1;
1474 }
1475
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001476 if (gr->egl_context == NULL)
John Kåre Alsaker94659272012-11-13 19:10:18 +01001477 if (gles2_renderer_setup(ec, go->egl_surface) < 0) {
1478 free(go);
1479 return -1;
1480 }
1481
1482 output->renderer_state = go;
1483
John Kåre Alsaker44154502012-11-13 19:10:20 +01001484 output_apply_border(output, gr);
1485
John Kåre Alsaker94659272012-11-13 19:10:18 +01001486 return 0;
1487}
1488
1489WL_EXPORT void
1490gles2_renderer_output_destroy(struct weston_output *output)
1491{
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001492 struct gles2_renderer *gr = get_renderer(output->compositor);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001493 struct gles2_output_state *go = get_output_state(output);
1494
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001495 eglDestroySurface(gr->egl_display, go->egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001496
1497 free(go);
1498}
1499
1500WL_EXPORT EGLSurface
1501gles2_renderer_output_surface(struct weston_output *output)
1502{
1503 return get_output_state(output)->egl_surface;
1504}
1505
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001506WL_EXPORT void
1507gles2_renderer_destroy(struct weston_compositor *ec)
1508{
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001509 struct gles2_renderer *gr = get_renderer(ec);
1510
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001511 if (ec->has_bind_display)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001512 ec->unbind_display(gr->egl_display, ec->wl_display);
1513
1514 /* Work around crash in egl_dri2.c's dri2_make_current() - when does this apply? */
1515 eglMakeCurrent(gr->egl_display,
1516 EGL_NO_SURFACE, EGL_NO_SURFACE,
1517 EGL_NO_CONTEXT);
1518
1519 eglTerminate(gr->egl_display);
1520 eglReleaseThread();
1521}
1522
1523static int
1524egl_choose_config(struct gles2_renderer *gr, const EGLint *attribs,
1525 const EGLint *visual_id)
1526{
1527 EGLint count = 0;
1528 EGLint matched = 0;
1529 EGLConfig *configs;
1530 int i;
1531
1532 if (!eglGetConfigs(gr->egl_display, NULL, 0, &count) || count < 1)
1533 return -1;
1534
1535 configs = calloc(count, sizeof *configs);
1536 if (!configs)
1537 return -1;
1538
1539 if (!eglChooseConfig(gr->egl_display, attribs, configs,
1540 count, &matched))
1541 goto out;
1542
1543 for (i = 0; i < matched; ++i) {
1544 EGLint id;
1545
1546 if (visual_id) {
1547 if (!eglGetConfigAttrib(gr->egl_display,
1548 configs[i], EGL_NATIVE_VISUAL_ID,
1549 &id))
1550 continue;
1551
1552 if (id != *visual_id)
1553 continue;
1554 }
1555
1556 gr->egl_config = configs[i];
1557
1558 free(configs);
1559 return 0;
1560 }
1561
1562out:
1563 free(configs);
1564 return -1;
1565}
1566
1567WL_EXPORT const EGLint gles2_renderer_opaque_attribs[] = {
1568 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1569 EGL_RED_SIZE, 1,
1570 EGL_GREEN_SIZE, 1,
1571 EGL_BLUE_SIZE, 1,
1572 EGL_ALPHA_SIZE, 0,
1573 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1574 EGL_NONE
1575};
1576
1577WL_EXPORT const EGLint gles2_renderer_alpha_attribs[] = {
1578 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1579 EGL_RED_SIZE, 1,
1580 EGL_GREEN_SIZE, 1,
1581 EGL_BLUE_SIZE, 1,
1582 EGL_ALPHA_SIZE, 1,
1583 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1584 EGL_NONE
1585};
1586
1587WL_EXPORT int
1588gles2_renderer_create(struct weston_compositor *ec, EGLNativeDisplayType display,
1589 const EGLint *attribs, const EGLint *visual_id)
1590{
1591 struct gles2_renderer *gr;
1592 EGLint major, minor;
1593
1594 gr = calloc(1, sizeof *gr);
1595
1596 if (gr == NULL)
1597 return -1;
1598
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001599 gr->base.read_pixels = gles2_renderer_read_pixels;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001600 gr->base.repaint_output = gles2_renderer_repaint_output;
1601 gr->base.flush_damage = gles2_renderer_flush_damage;
1602 gr->base.attach = gles2_renderer_attach;
1603 gr->base.destroy_surface = gles2_renderer_destroy_surface;
1604
1605 gr->egl_display = eglGetDisplay(display);
1606 if (gr->egl_display == EGL_NO_DISPLAY) {
1607 weston_log("failed to create display\n");
1608 goto err_egl;
1609 }
1610
1611 if (!eglInitialize(gr->egl_display, &major, &minor)) {
1612 weston_log("failed to initialize display\n");
1613 goto err_egl;
1614 }
1615
1616 if (egl_choose_config(gr, attribs, visual_id) < 0) {
1617 weston_log("failed to choose EGL config\n");
1618 goto err_egl;
1619 }
1620
1621 ec->renderer = &gr->base;
1622
1623 return 0;
1624
1625err_egl:
1626 print_egl_error_state();
1627 free(gr);
1628 return -1;
1629}
1630
1631WL_EXPORT EGLDisplay
1632gles2_renderer_display(struct weston_compositor *ec)
1633{
1634 return get_renderer(ec)->egl_display;
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001635}
1636
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001637static int
1638compile_shaders(struct weston_compositor *ec)
1639{
1640 if (weston_shader_init(&ec->texture_shader_rgba, ec,
1641 vertex_shader, texture_fragment_shader_rgba) < 0)
1642 return -1;
1643 if (weston_shader_init(&ec->texture_shader_rgbx, ec,
1644 vertex_shader, texture_fragment_shader_rgbx) < 0)
1645 return -1;
1646 if (ec->has_egl_image_external &&
1647 weston_shader_init(&ec->texture_shader_egl_external, ec,
1648 vertex_shader, texture_fragment_shader_egl_external) < 0)
1649 return -1;
1650 if (weston_shader_init(&ec->texture_shader_y_uv, ec,
1651 vertex_shader, texture_fragment_shader_y_uv) < 0)
1652 return -1;
1653 if (weston_shader_init(&ec->texture_shader_y_u_v, ec,
1654 vertex_shader, texture_fragment_shader_y_u_v) < 0)
1655 return -1;
1656 if (weston_shader_init(&ec->texture_shader_y_xuxv, ec,
1657 vertex_shader, texture_fragment_shader_y_xuxv) < 0)
1658 return -1;
1659 if (weston_shader_init(&ec->solid_shader, ec,
1660 vertex_shader, solid_fragment_shader) < 0)
1661 return -1;
1662
1663 return 0;
1664}
1665
1666static void
1667fragment_debug_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
1668 void *data)
1669{
1670 struct weston_compositor *ec = data;
1671 struct gles2_renderer *renderer =
1672 (struct gles2_renderer *) ec->renderer;
1673 struct weston_output *output;
1674
1675 renderer->fragment_shader_debug ^= 1;
1676
1677 weston_shader_release(&ec->texture_shader_rgba);
1678 weston_shader_release(&ec->texture_shader_rgbx);
1679 weston_shader_release(&ec->texture_shader_egl_external);
1680 weston_shader_release(&ec->texture_shader_y_uv);
1681 weston_shader_release(&ec->texture_shader_y_u_v);
1682 weston_shader_release(&ec->texture_shader_y_xuxv);
1683 weston_shader_release(&ec->solid_shader);
1684
1685 compile_shaders(ec);
1686
1687 wl_list_for_each(output, &ec->output_list, link)
1688 weston_output_damage(output);
1689}
1690
John Kåre Alsaker94659272012-11-13 19:10:18 +01001691static int
1692gles2_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001693{
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001694 struct gles2_renderer *gr = get_renderer(ec);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001695 const char *extensions;
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001696 EGLBoolean ret;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001697
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001698 static const EGLint context_attribs[] = {
1699 EGL_CONTEXT_CLIENT_VERSION, 2,
1700 EGL_NONE
1701 };
1702
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001703 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
1704 weston_log("failed to bind EGL_OPENGL_ES_API\n");
1705 print_egl_error_state();
1706 return -1;
1707 }
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001708
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001709 log_egl_config_info(gr->egl_display, gr->egl_config);
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001710
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001711 gr->egl_context = eglCreateContext(gr->egl_display, gr->egl_config,
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001712 EGL_NO_CONTEXT, context_attribs);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001713 if (gr->egl_context == NULL) {
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001714 weston_log("failed to create context\n");
1715 print_egl_error_state();
1716 return -1;
1717 }
1718
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001719 ret = eglMakeCurrent(gr->egl_display, egl_surface,
1720 egl_surface, gr->egl_context);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001721 if (ret == EGL_FALSE) {
1722 weston_log("Failed to make EGL context current.\n");
1723 print_egl_error_state();
1724 return -1;
1725 }
1726
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001727 log_egl_gl_info(gr->egl_display);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001728
1729 ec->image_target_texture_2d =
1730 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
1731 ec->image_target_renderbuffer_storage = (void *)
1732 eglGetProcAddress("glEGLImageTargetRenderbufferStorageOES");
1733 ec->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1734 ec->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1735 ec->bind_display =
1736 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
1737 ec->unbind_display =
1738 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
1739 ec->query_buffer =
1740 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
1741
1742 extensions = (const char *) glGetString(GL_EXTENSIONS);
1743 if (!extensions) {
1744 weston_log("Retrieving GL extension string failed.\n");
1745 return -1;
1746 }
1747
1748 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
1749 weston_log("GL_EXT_texture_format_BGRA8888 not available\n");
1750 return -1;
1751 }
1752
1753 if (strstr(extensions, "GL_EXT_read_format_bgra"))
1754 ec->read_format = GL_BGRA_EXT;
1755 else
1756 ec->read_format = GL_RGBA;
1757
1758 if (strstr(extensions, "GL_EXT_unpack_subimage"))
1759 ec->has_unpack_subimage = 1;
1760
1761 if (strstr(extensions, "GL_OES_EGL_image_external"))
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001762 ec->has_egl_image_external = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001763
1764 extensions =
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001765 (const char *) eglQueryString(gr->egl_display, EGL_EXTENSIONS);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001766 if (!extensions) {
1767 weston_log("Retrieving EGL extension string failed.\n");
1768 return -1;
1769 }
1770
1771 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
1772 ec->has_bind_display = 1;
Pekka Paalanen035a0322012-10-24 09:43:06 +03001773 if (ec->has_bind_display) {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001774 ret = ec->bind_display(gr->egl_display, ec->wl_display);
Pekka Paalanen035a0322012-10-24 09:43:06 +03001775 if (!ret)
1776 ec->has_bind_display = 0;
1777 }
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001778
1779 glActiveTexture(GL_TEXTURE0);
1780
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001781 if (compile_shaders(ec))
1782 return -1;
1783
1784 weston_compositor_add_debug_binding(ec, KEY_S,
1785 fragment_debug_binding, ec);
1786
Pekka Paalanen035a0322012-10-24 09:43:06 +03001787 weston_log("GL ES 2 renderer features:\n");
1788 weston_log_continue(STAMP_SPACE "read-back format: %s\n",
1789 ec->read_format == GL_BGRA_EXT ? "BGRA" : "RGBA");
1790 weston_log_continue(STAMP_SPACE "wl_shm sub-image to texture: %s\n",
1791 ec->has_unpack_subimage ? "yes" : "no");
1792 weston_log_continue(STAMP_SPACE "EGL Wayland extension: %s\n",
1793 ec->has_bind_display ? "yes" : "no");
1794
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001795
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001796 return 0;
1797}