blob: 0e8b8cefcf74763d66ec8fd80b966088220c84df [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>
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -040030
Kristian Høgsbergd7c17262012-09-05 21:54:15 -040031#include "compositor.h"
32
33static const char *
34egl_error_string(EGLint code)
35{
36#define MYERRCODE(x) case x: return #x;
37 switch (code) {
38 MYERRCODE(EGL_SUCCESS)
39 MYERRCODE(EGL_NOT_INITIALIZED)
40 MYERRCODE(EGL_BAD_ACCESS)
41 MYERRCODE(EGL_BAD_ALLOC)
42 MYERRCODE(EGL_BAD_ATTRIBUTE)
43 MYERRCODE(EGL_BAD_CONTEXT)
44 MYERRCODE(EGL_BAD_CONFIG)
45 MYERRCODE(EGL_BAD_CURRENT_SURFACE)
46 MYERRCODE(EGL_BAD_DISPLAY)
47 MYERRCODE(EGL_BAD_SURFACE)
48 MYERRCODE(EGL_BAD_MATCH)
49 MYERRCODE(EGL_BAD_PARAMETER)
50 MYERRCODE(EGL_BAD_NATIVE_PIXMAP)
51 MYERRCODE(EGL_BAD_NATIVE_WINDOW)
52 MYERRCODE(EGL_CONTEXT_LOST)
53 default:
54 return "unknown";
55 }
56#undef MYERRCODE
57}
58
59static void
60print_egl_error_state(void)
61{
62 EGLint code;
63
64 code = eglGetError();
65 weston_log("EGL error state: %s (0x%04lx)\n",
66 egl_error_string(code), (long)code);
67}
68
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +030069struct polygon8 {
70 GLfloat x[8];
71 GLfloat y[8];
72 int n;
73};
74
75struct clip_context {
76 struct {
77 GLfloat x;
78 GLfloat y;
79 } prev;
80
81 struct {
82 GLfloat x1, y1;
83 GLfloat x2, y2;
84 } clip;
85
86 struct {
87 GLfloat *x;
88 GLfloat *y;
89 } vertices;
90};
91
92static GLfloat
93float_difference(GLfloat a, GLfloat b)
94{
95 /* http://www.altdevblogaday.com/2012/02/22/comparing-floating-point-numbers-2012-edition/ */
96 static const GLfloat max_diff = 4.0f * FLT_MIN;
97 static const GLfloat max_rel_diff = 4.0e-5;
98 GLfloat diff = a - b;
99 GLfloat adiff = fabsf(diff);
100
101 if (adiff <= max_diff)
102 return 0.0f;
103
104 a = fabsf(a);
105 b = fabsf(b);
106 if (adiff <= (a > b ? a : b) * max_rel_diff)
107 return 0.0f;
108
109 return diff;
110}
111
112/* A line segment (p1x, p1y)-(p2x, p2y) intersects the line x = x_arg.
113 * Compute the y coordinate of the intersection.
114 */
115static GLfloat
116clip_intersect_y(GLfloat p1x, GLfloat p1y, GLfloat p2x, GLfloat p2y,
117 GLfloat x_arg)
118{
119 GLfloat a;
120 GLfloat diff = float_difference(p1x, p2x);
121
122 /* Practically vertical line segment, yet the end points have already
123 * been determined to be on different sides of the line. Therefore
124 * the line segment is part of the line and intersects everywhere.
125 * Return the end point, so we use the whole line segment.
126 */
127 if (diff == 0.0f)
128 return p2y;
129
130 a = (x_arg - p2x) / diff;
131 return p2y + (p1y - p2y) * a;
132}
133
134/* A line segment (p1x, p1y)-(p2x, p2y) intersects the line y = y_arg.
135 * Compute the x coordinate of the intersection.
136 */
137static GLfloat
138clip_intersect_x(GLfloat p1x, GLfloat p1y, GLfloat p2x, GLfloat p2y,
139 GLfloat y_arg)
140{
141 GLfloat a;
142 GLfloat diff = float_difference(p1y, p2y);
143
144 /* Practically horizontal line segment, yet the end points have already
145 * been determined to be on different sides of the line. Therefore
146 * the line segment is part of the line and intersects everywhere.
147 * Return the end point, so we use the whole line segment.
148 */
149 if (diff == 0.0f)
150 return p2x;
151
152 a = (y_arg - p2y) / diff;
153 return p2x + (p1x - p2x) * a;
154}
155
156enum path_transition {
157 PATH_TRANSITION_OUT_TO_OUT = 0,
158 PATH_TRANSITION_OUT_TO_IN = 1,
159 PATH_TRANSITION_IN_TO_OUT = 2,
160 PATH_TRANSITION_IN_TO_IN = 3,
161};
162
163static void
164clip_append_vertex(struct clip_context *ctx, GLfloat x, GLfloat y)
165{
166 *ctx->vertices.x++ = x;
167 *ctx->vertices.y++ = y;
168}
169
170static enum path_transition
171path_transition_left_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
172{
173 return ((ctx->prev.x >= ctx->clip.x1) << 1) | (x >= ctx->clip.x1);
174}
175
176static enum path_transition
177path_transition_right_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
178{
179 return ((ctx->prev.x < ctx->clip.x2) << 1) | (x < ctx->clip.x2);
180}
181
182static enum path_transition
183path_transition_top_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
184{
185 return ((ctx->prev.y >= ctx->clip.y1) << 1) | (y >= ctx->clip.y1);
186}
187
188static enum path_transition
189path_transition_bottom_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
190{
191 return ((ctx->prev.y < ctx->clip.y2) << 1) | (y < ctx->clip.y2);
192}
193
194static void
195clip_polygon_leftright(struct clip_context *ctx,
196 enum path_transition transition,
197 GLfloat x, GLfloat y, GLfloat clip_x)
198{
199 GLfloat yi;
200
201 switch (transition) {
202 case PATH_TRANSITION_IN_TO_IN:
203 clip_append_vertex(ctx, x, y);
204 break;
205 case PATH_TRANSITION_IN_TO_OUT:
206 yi = clip_intersect_y(ctx->prev.x, ctx->prev.y, x, y, clip_x);
207 clip_append_vertex(ctx, clip_x, yi);
208 break;
209 case PATH_TRANSITION_OUT_TO_IN:
210 yi = clip_intersect_y(ctx->prev.x, ctx->prev.y, x, y, clip_x);
211 clip_append_vertex(ctx, clip_x, yi);
212 clip_append_vertex(ctx, x, y);
213 break;
214 case PATH_TRANSITION_OUT_TO_OUT:
215 /* nothing */
216 break;
217 default:
218 assert(0 && "bad enum path_transition");
219 }
220
221 ctx->prev.x = x;
222 ctx->prev.y = y;
223}
224
225static void
226clip_polygon_topbottom(struct clip_context *ctx,
227 enum path_transition transition,
228 GLfloat x, GLfloat y, GLfloat clip_y)
229{
230 GLfloat xi;
231
232 switch (transition) {
233 case PATH_TRANSITION_IN_TO_IN:
234 clip_append_vertex(ctx, x, y);
235 break;
236 case PATH_TRANSITION_IN_TO_OUT:
237 xi = clip_intersect_x(ctx->prev.x, ctx->prev.y, x, y, clip_y);
238 clip_append_vertex(ctx, xi, clip_y);
239 break;
240 case PATH_TRANSITION_OUT_TO_IN:
241 xi = clip_intersect_x(ctx->prev.x, ctx->prev.y, x, y, clip_y);
242 clip_append_vertex(ctx, xi, clip_y);
243 clip_append_vertex(ctx, x, y);
244 break;
245 case PATH_TRANSITION_OUT_TO_OUT:
246 /* nothing */
247 break;
248 default:
249 assert(0 && "bad enum path_transition");
250 }
251
252 ctx->prev.x = x;
253 ctx->prev.y = y;
254}
255
256static void
257clip_context_prepare(struct clip_context *ctx, const struct polygon8 *src,
258 GLfloat *dst_x, GLfloat *dst_y)
259{
260 ctx->prev.x = src->x[src->n - 1];
261 ctx->prev.y = src->y[src->n - 1];
262 ctx->vertices.x = dst_x;
263 ctx->vertices.y = dst_y;
264}
265
266static int
267clip_polygon_left(struct clip_context *ctx, const struct polygon8 *src,
268 GLfloat *dst_x, GLfloat *dst_y)
269{
270 enum path_transition trans;
271 int i;
272
273 clip_context_prepare(ctx, src, dst_x, dst_y);
274 for (i = 0; i < src->n; i++) {
275 trans = path_transition_left_edge(ctx, src->x[i], src->y[i]);
276 clip_polygon_leftright(ctx, trans, src->x[i], src->y[i],
277 ctx->clip.x1);
278 }
279 return ctx->vertices.x - dst_x;
280}
281
282static int
283clip_polygon_right(struct clip_context *ctx, const struct polygon8 *src,
284 GLfloat *dst_x, GLfloat *dst_y)
285{
286 enum path_transition trans;
287 int i;
288
289 clip_context_prepare(ctx, src, dst_x, dst_y);
290 for (i = 0; i < src->n; i++) {
291 trans = path_transition_right_edge(ctx, src->x[i], src->y[i]);
292 clip_polygon_leftright(ctx, trans, src->x[i], src->y[i],
293 ctx->clip.x2);
294 }
295 return ctx->vertices.x - dst_x;
296}
297
298static int
299clip_polygon_top(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_top_edge(ctx, src->x[i], src->y[i]);
308 clip_polygon_topbottom(ctx, trans, src->x[i], src->y[i],
309 ctx->clip.y1);
310 }
311 return ctx->vertices.x - dst_x;
312}
313
314static int
315clip_polygon_bottom(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_bottom_edge(ctx, src->x[i], src->y[i]);
324 clip_polygon_topbottom(ctx, trans, src->x[i], src->y[i],
325 ctx->clip.y2);
326 }
327 return ctx->vertices.x - dst_x;
328}
329
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400330#define max(a, b) (((a) > (b)) ? (a) : (b))
331#define min(a, b) (((a) > (b)) ? (b) : (a))
332#define clip(x, a, b) min(max(x, a), b)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400333
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300334/*
335 * Compute the boundary vertices of the intersection of the global coordinate
336 * aligned rectangle 'rect', and an arbitrary quadrilateral produced from
337 * 'surf_rect' when transformed from surface coordinates into global coordinates.
338 * The vertices are written to 'ex' and 'ey', and the return value is the
339 * number of vertices. Vertices are produced in clockwise winding order.
340 * Guarantees to produce either zero vertices, or 3-8 vertices with non-zero
341 * polygon area.
342 */
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400343static int
344calculate_edges(struct weston_surface *es, pixman_box32_t *rect,
345 pixman_box32_t *surf_rect, GLfloat *ex, GLfloat *ey)
346{
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300347 struct polygon8 polygon;
348 struct clip_context ctx;
349 int i, n;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400350 GLfloat min_x, max_x, min_y, max_y;
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300351 struct polygon8 surf = {
352 { surf_rect->x1, surf_rect->x2, surf_rect->x2, surf_rect->x1 },
353 { surf_rect->y1, surf_rect->y1, surf_rect->y2, surf_rect->y2 },
354 4
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400355 };
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400356
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300357 ctx.clip.x1 = rect->x1;
358 ctx.clip.y1 = rect->y1;
359 ctx.clip.x2 = rect->x2;
360 ctx.clip.y2 = rect->y2;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400361
362 /* transform surface to screen space: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300363 for (i = 0; i < surf.n; i++)
364 weston_surface_to_global_float(es, surf.x[i], surf.y[i],
365 &surf.x[i], &surf.y[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400366
367 /* find bounding box: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300368 min_x = max_x = surf.x[0];
369 min_y = max_y = surf.y[0];
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400370
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300371 for (i = 1; i < surf.n; i++) {
372 min_x = min(min_x, surf.x[i]);
373 max_x = max(max_x, surf.x[i]);
374 min_y = min(min_y, surf.y[i]);
375 max_y = max(max_y, surf.y[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400376 }
377
378 /* First, simple bounding box check to discard early transformed
379 * surface rects that do not intersect with the clip region:
380 */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300381 if ((min_x >= ctx.clip.x2) || (max_x <= ctx.clip.x1) ||
382 (min_y >= ctx.clip.y2) || (max_y <= ctx.clip.y1))
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400383 return 0;
384
385 /* Simple case, bounding box edges are parallel to surface edges,
386 * there will be only four edges. We just need to clip the surface
387 * vertices to the clip rect bounds:
388 */
389 if (!es->transform.enabled) {
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300390 for (i = 0; i < surf.n; i++) {
391 ex[i] = clip(surf.x[i], ctx.clip.x1, ctx.clip.x2);
392 ey[i] = clip(surf.y[i], ctx.clip.y1, ctx.clip.y2);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400393 }
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300394 return surf.n;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400395 }
396
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300397 /* Transformed case: use a general polygon clipping algorithm to
398 * clip the surface rectangle with each side of 'rect'.
399 * The algorithm is Sutherland-Hodgman, as explained in
400 * http://www.codeguru.com/cpp/misc/misc/graphics/article.php/c8965/Polygon-Clipping.htm
401 * but without looking at any of that code.
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400402 */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300403 polygon.n = clip_polygon_left(&ctx, &surf, polygon.x, polygon.y);
404 surf.n = clip_polygon_right(&ctx, &polygon, surf.x, surf.y);
405 polygon.n = clip_polygon_top(&ctx, &surf, polygon.x, polygon.y);
406 surf.n = clip_polygon_bottom(&ctx, &polygon, surf.x, surf.y);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400407
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300408 /* Get rid of duplicate vertices */
409 ex[0] = surf.x[0];
410 ey[0] = surf.y[0];
411 n = 1;
412 for (i = 1; i < surf.n; i++) {
413 if (float_difference(ex[n - 1], surf.x[i]) == 0.0f &&
414 float_difference(ey[n - 1], surf.y[i]) == 0.0f)
415 continue;
416 ex[n] = surf.x[i];
417 ey[n] = surf.y[i];
418 n++;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400419 }
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300420 if (float_difference(ex[n - 1], surf.x[0]) == 0.0f &&
421 float_difference(ey[n - 1], surf.y[0]) == 0.0f)
422 n--;
423
424 if (n < 3)
425 return 0;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400426
427 return n;
428}
429
430static int
431texture_region(struct weston_surface *es, pixman_region32_t *region,
432 pixman_region32_t *surf_region)
433{
434 struct weston_compositor *ec = es->compositor;
435 GLfloat *v, inv_width, inv_height;
436 unsigned int *vtxcnt, nvtx = 0;
437 pixman_box32_t *rects, *surf_rects;
438 int i, j, k, nrects, nsurf;
439
440 rects = pixman_region32_rectangles(region, &nrects);
441 surf_rects = pixman_region32_rectangles(surf_region, &nsurf);
442
443 /* worst case we can have 8 vertices per rect (ie. clipped into
444 * an octagon):
445 */
446 v = wl_array_add(&ec->vertices, nrects * nsurf * 8 * 4 * sizeof *v);
447 vtxcnt = wl_array_add(&ec->vtxcnt, nrects * nsurf * sizeof *vtxcnt);
448
449 inv_width = 1.0 / es->pitch;
450 inv_height = 1.0 / es->geometry.height;
451
452 for (i = 0; i < nrects; i++) {
453 pixman_box32_t *rect = &rects[i];
454 for (j = 0; j < nsurf; j++) {
455 pixman_box32_t *surf_rect = &surf_rects[j];
456 GLfloat sx, sy;
457 GLfloat ex[8], ey[8]; /* edge points in screen space */
458 int n;
459
460 /* The transformed surface, after clipping to the clip region,
461 * can have as many as eight sides, emitted as a triangle-fan.
462 * The first vertex in the triangle fan can be chosen arbitrarily,
463 * since the area is guaranteed to be convex.
464 *
465 * If a corner of the transformed surface falls outside of the
466 * clip region, instead of emitting one vertex for the corner
467 * of the surface, up to two are emitted for two corresponding
468 * intersection point(s) between the surface and the clip region.
469 *
470 * To do this, we first calculate the (up to eight) points that
471 * form the intersection of the clip rect and the transformed
472 * surface.
473 */
474 n = calculate_edges(es, rect, surf_rect, ex, ey);
475 if (n < 3)
476 continue;
477
478 /* emit edge points: */
479 for (k = 0; k < n; k++) {
480 weston_surface_from_global_float(es, ex[k], ey[k], &sx, &sy);
481 /* position: */
482 *(v++) = ex[k];
483 *(v++) = ey[k];
484 /* texcoord: */
485 *(v++) = sx * inv_width;
486 *(v++) = sy * inv_height;
487 }
488
489 vtxcnt[nvtx++] = n;
490 }
491 }
492
493 return nvtx;
494}
495
496static void
497triangle_fan_debug(struct weston_surface *surface, int first, int count)
498{
499 struct weston_compositor *compositor = surface->compositor;
500 int i;
501 GLushort *buffer;
502 GLushort *index;
503 int nelems;
504 static int color_idx = 0;
505 static const GLfloat color[][4] = {
506 { 1.0, 0.0, 0.0, 1.0 },
507 { 0.0, 1.0, 0.0, 1.0 },
508 { 0.0, 0.0, 1.0, 1.0 },
509 { 1.0, 1.0, 1.0, 1.0 },
510 };
511
512 nelems = (count - 1 + count - 2) * 2;
513
514 buffer = malloc(sizeof(GLushort) * nelems);
515 index = buffer;
516
517 for (i = 1; i < count; i++) {
518 *index++ = first;
519 *index++ = first + i;
520 }
521
522 for (i = 2; i < count; i++) {
523 *index++ = first + i - 1;
524 *index++ = first + i;
525 }
526
527 glUseProgram(compositor->solid_shader.program);
528 glUniform4fv(compositor->solid_shader.color_uniform, 1,
529 color[color_idx++ % ARRAY_LENGTH(color)]);
530 glDrawElements(GL_LINES, nelems, GL_UNSIGNED_SHORT, buffer);
531 glUseProgram(compositor->current_shader->program);
532 free(buffer);
533}
534
535static void
536repaint_region(struct weston_surface *es, pixman_region32_t *region,
537 pixman_region32_t *surf_region)
538{
539 struct weston_compositor *ec = es->compositor;
540 GLfloat *v;
541 unsigned int *vtxcnt;
542 int i, first, nfans;
543
544 /* The final region to be painted is the intersection of
545 * 'region' and 'surf_region'. However, 'region' is in the global
546 * coordinates, and 'surf_region' is in the surface-local
547 * coordinates. texture_region() will iterate over all pairs of
548 * rectangles from both regions, compute the intersection
549 * polygon for each pair, and store it as a triangle fan if
550 * it has a non-zero area (at least 3 vertices, actually).
551 */
552 nfans = texture_region(es, region, surf_region);
553
554 v = ec->vertices.data;
555 vtxcnt = ec->vtxcnt.data;
556
557 /* position: */
558 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
559 glEnableVertexAttribArray(0);
560
561 /* texcoord: */
562 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
563 glEnableVertexAttribArray(1);
564
565 for (i = 0, first = 0; i < nfans; i++) {
566 glDrawArrays(GL_TRIANGLE_FAN, first, vtxcnt[i]);
567 if (ec->fan_debug)
568 triangle_fan_debug(es, first, vtxcnt[i]);
569 first += vtxcnt[i];
570 }
571
572 glDisableVertexAttribArray(1);
573 glDisableVertexAttribArray(0);
574
575 ec->vertices.size = 0;
576 ec->vtxcnt.size = 0;
577}
578
579static void
580weston_compositor_use_shader(struct weston_compositor *compositor,
581 struct weston_shader *shader)
582{
583 if (compositor->current_shader == shader)
584 return;
585
586 glUseProgram(shader->program);
587 compositor->current_shader = shader;
588}
589
590static void
591weston_shader_uniforms(struct weston_shader *shader,
592 struct weston_surface *surface,
593 struct weston_output *output)
594{
595 int i;
596
597 glUniformMatrix4fv(shader->proj_uniform,
598 1, GL_FALSE, output->matrix.d);
599 glUniform4fv(shader->color_uniform, 1, surface->color);
600 glUniform1f(shader->alpha_uniform, surface->alpha);
601
602 for (i = 0; i < surface->num_textures; i++)
603 glUniform1i(shader->tex_uniforms[i], i);
604}
605
606static void
607draw_surface(struct weston_surface *es, struct weston_output *output,
608 pixman_region32_t *damage) /* in global coordinates */
609{
610 struct weston_compositor *ec = es->compositor;
611 /* repaint bounding region in global coordinates: */
612 pixman_region32_t repaint;
613 /* non-opaque region in surface coordinates: */
614 pixman_region32_t surface_blend;
615 GLint filter;
616 int i;
617
618 pixman_region32_init(&repaint);
619 pixman_region32_intersect(&repaint,
620 &es->transform.boundingbox, damage);
621 pixman_region32_subtract(&repaint, &repaint, &es->clip);
622
623 if (!pixman_region32_not_empty(&repaint))
624 goto out;
625
626 pixman_region32_subtract(&ec->primary_plane.damage,
627 &ec->primary_plane.damage, &repaint);
628
629 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
630
631 if (ec->fan_debug) {
632 weston_compositor_use_shader(ec, &ec->solid_shader);
633 weston_shader_uniforms(&ec->solid_shader, es, output);
634 }
635
636 weston_compositor_use_shader(ec, es->shader);
637 weston_shader_uniforms(es->shader, es, output);
638
639 if (es->transform.enabled || output->zoom.active)
640 filter = GL_LINEAR;
641 else
642 filter = GL_NEAREST;
643
644 for (i = 0; i < es->num_textures; i++) {
645 glActiveTexture(GL_TEXTURE0 + i);
646 glBindTexture(es->target, es->textures[i]);
647 glTexParameteri(es->target, GL_TEXTURE_MIN_FILTER, filter);
648 glTexParameteri(es->target, GL_TEXTURE_MAG_FILTER, filter);
649 }
650
651 /* blended region is whole surface minus opaque region: */
652 pixman_region32_init_rect(&surface_blend, 0, 0,
653 es->geometry.width, es->geometry.height);
654 pixman_region32_subtract(&surface_blend, &surface_blend, &es->opaque);
655
656 if (pixman_region32_not_empty(&es->opaque)) {
657 if (es->shader == &ec->texture_shader_rgba) {
658 /* Special case for RGBA textures with possibly
659 * bad data in alpha channel: use the shader
660 * that forces texture alpha = 1.0.
661 * Xwayland surfaces need this.
662 */
663 weston_compositor_use_shader(ec, &ec->texture_shader_rgbx);
664 weston_shader_uniforms(&ec->texture_shader_rgbx, es, output);
665 }
666
667 if (es->alpha < 1.0)
668 glEnable(GL_BLEND);
669 else
670 glDisable(GL_BLEND);
671
672 repaint_region(es, &repaint, &es->opaque);
673 }
674
675 if (pixman_region32_not_empty(&surface_blend)) {
676 weston_compositor_use_shader(ec, es->shader);
677 glEnable(GL_BLEND);
678 repaint_region(es, &repaint, &surface_blend);
679 }
680
681 pixman_region32_fini(&surface_blend);
682
683out:
684 pixman_region32_fini(&repaint);
685}
686
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400687static void
688repaint_surfaces(struct weston_output *output, pixman_region32_t *damage)
689{
690 struct weston_compositor *compositor = output->compositor;
691 struct weston_surface *surface;
692
693 wl_list_for_each_reverse(surface, &compositor->surface_list, link)
694 if (surface->plane == &compositor->primary_plane)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400695 draw_surface(surface, output, damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400696}
697
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400698static void
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400699gles2_renderer_repaint_output(struct weston_output *output,
700 pixman_region32_t *output_damage)
701{
702 struct weston_compositor *compositor = output->compositor;
703 EGLBoolean ret;
704 static int errored;
705 int32_t width, height;
706
707 width = output->current->width +
708 output->border.left + output->border.right;
709 height = output->current->height +
710 output->border.top + output->border.bottom;
711
712 glViewport(0, 0, width, height);
713
714 ret = eglMakeCurrent(compositor->egl_display, output->egl_surface,
715 output->egl_surface, compositor->egl_context);
716 if (ret == EGL_FALSE) {
717 if (errored)
718 return;
719 errored = 1;
720 weston_log("Failed to make EGL context current.\n");
721 print_egl_error_state();
722 return;
723 }
724
725 /* if debugging, redraw everything outside the damage to clean up
726 * debug lines from the previous draw on this buffer:
727 */
728 if (compositor->fan_debug) {
729 pixman_region32_t undamaged;
730 pixman_region32_init(&undamaged);
731 pixman_region32_subtract(&undamaged, &output->region,
732 output_damage);
733 compositor->fan_debug = 0;
734 repaint_surfaces(output, &undamaged);
735 compositor->fan_debug = 1;
736 pixman_region32_fini(&undamaged);
737 }
738
739 repaint_surfaces(output, output_damage);
740
741 wl_signal_emit(&output->frame_signal, output);
742
743 ret = eglSwapBuffers(compositor->egl_display, output->egl_surface);
744 if (ret == EGL_FALSE && !errored) {
745 errored = 1;
746 weston_log("Failed in eglSwapBuffers.\n");
747 print_egl_error_state();
748 }
749
750}
Kristian Høgsberg25894fc2012-09-05 22:06:26 -0400751
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400752static void
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -0400753gles2_renderer_flush_damage(struct weston_surface *surface)
754{
755#ifdef GL_UNPACK_ROW_LENGTH
756 pixman_box32_t *rectangles;
757 void *data;
758 int i, n;
759#endif
760
761 glBindTexture(GL_TEXTURE_2D, surface->textures[0]);
762
763 if (!surface->compositor->has_unpack_subimage) {
764 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
765 surface->pitch, surface->buffer->height, 0,
766 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
767 wl_shm_buffer_get_data(surface->buffer));
768
769 return;
770 }
771
772#ifdef GL_UNPACK_ROW_LENGTH
773 /* Mesa does not define GL_EXT_unpack_subimage */
774 glPixelStorei(GL_UNPACK_ROW_LENGTH, surface->pitch);
775 data = wl_shm_buffer_get_data(surface->buffer);
776 rectangles = pixman_region32_rectangles(&surface->damage, &n);
777 for (i = 0; i < n; i++) {
778 glPixelStorei(GL_UNPACK_SKIP_PIXELS, rectangles[i].x1);
779 glPixelStorei(GL_UNPACK_SKIP_ROWS, rectangles[i].y1);
780 glTexSubImage2D(GL_TEXTURE_2D, 0,
781 rectangles[i].x1, rectangles[i].y1,
782 rectangles[i].x2 - rectangles[i].x1,
783 rectangles[i].y2 - rectangles[i].y1,
784 GL_BGRA_EXT, GL_UNSIGNED_BYTE, data);
785 }
786#endif
787}
788
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400789static void
790ensure_textures(struct weston_surface *es, int num_textures)
791{
792 int i;
793
794 if (num_textures <= es->num_textures)
795 return;
796
797 for (i = es->num_textures; i < num_textures; i++) {
798 glGenTextures(1, &es->textures[i]);
799 glBindTexture(es->target, es->textures[i]);
800 glTexParameteri(es->target,
801 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
802 glTexParameteri(es->target,
803 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
804 }
805 es->num_textures = num_textures;
806 glBindTexture(es->target, 0);
807}
808
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400809static void
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400810gles2_renderer_attach(struct weston_surface *es, struct wl_buffer *buffer)
811{
812 struct weston_compositor *ec = es->compositor;
813 EGLint attribs[3], format;
814 int i, num_planes;
815
816 if (!buffer) {
817 for (i = 0; i < es->num_images; i++) {
818 ec->destroy_image(ec->egl_display, es->images[i]);
819 es->images[i] = NULL;
820 }
821 es->num_images = 0;
822 glDeleteTextures(es->num_textures, es->textures);
823 es->num_textures = 0;
824 return;
825 }
826
827 if (wl_buffer_is_shm(buffer)) {
828 es->pitch = wl_shm_buffer_get_stride(buffer) / 4;
829 es->target = GL_TEXTURE_2D;
830
831 ensure_textures(es, 1);
832 glBindTexture(GL_TEXTURE_2D, es->textures[0]);
833 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
834 es->pitch, buffer->height, 0,
835 GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
836 if (wl_shm_buffer_get_format(buffer) == WL_SHM_FORMAT_XRGB8888)
837 es->shader = &ec->texture_shader_rgbx;
838 else
839 es->shader = &ec->texture_shader_rgba;
840 } else if (ec->query_buffer(ec->egl_display, buffer,
841 EGL_TEXTURE_FORMAT, &format)) {
842 for (i = 0; i < es->num_images; i++)
843 ec->destroy_image(ec->egl_display, es->images[i]);
844 es->num_images = 0;
845 es->target = GL_TEXTURE_2D;
846 switch (format) {
847 case EGL_TEXTURE_RGB:
848 case EGL_TEXTURE_RGBA:
849 default:
850 num_planes = 1;
851 es->shader = &ec->texture_shader_rgba;
852 break;
853 case EGL_TEXTURE_EXTERNAL_WL:
854 num_planes = 1;
855 es->target = GL_TEXTURE_EXTERNAL_OES;
856 es->shader = &ec->texture_shader_egl_external;
857 break;
858 case EGL_TEXTURE_Y_UV_WL:
859 num_planes = 2;
860 es->shader = &ec->texture_shader_y_uv;
861 break;
862 case EGL_TEXTURE_Y_U_V_WL:
863 num_planes = 3;
864 es->shader = &ec->texture_shader_y_u_v;
865 break;
866 case EGL_TEXTURE_Y_XUXV_WL:
867 num_planes = 2;
868 es->shader = &ec->texture_shader_y_xuxv;
869 break;
870 }
871
872 ensure_textures(es, num_planes);
873 for (i = 0; i < num_planes; i++) {
874 attribs[0] = EGL_WAYLAND_PLANE_WL;
875 attribs[1] = i;
876 attribs[2] = EGL_NONE;
877 es->images[i] = ec->create_image(ec->egl_display,
878 NULL,
879 EGL_WAYLAND_BUFFER_WL,
880 buffer, attribs);
881 if (!es->images[i]) {
882 weston_log("failed to create img for plane %d\n", i);
883 continue;
884 }
885 es->num_images++;
886
887 glActiveTexture(GL_TEXTURE0 + i);
888 glBindTexture(es->target, es->textures[i]);
889 ec->image_target_texture_2d(es->target,
890 es->images[i]);
891 }
892
893 es->pitch = buffer->width;
894 } else {
895 weston_log("unhandled buffer type!\n");
896 }
897}
898
Kristian Høgsberg42263852012-09-06 21:59:29 -0400899static void
900gles2_renderer_destroy_surface(struct weston_surface *surface)
901{
902 struct weston_compositor *ec = surface->compositor;
903 int i;
904
905 glDeleteTextures(surface->num_textures, surface->textures);
906
907 for (i = 0; i < surface->num_images; i++)
908 ec->destroy_image(ec->egl_display, surface->images[i]);
909}
910
Kristian Høgsberg25894fc2012-09-05 22:06:26 -0400911static const char vertex_shader[] =
912 "uniform mat4 proj;\n"
913 "attribute vec2 position;\n"
914 "attribute vec2 texcoord;\n"
915 "varying vec2 v_texcoord;\n"
916 "void main()\n"
917 "{\n"
918 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
919 " v_texcoord = texcoord;\n"
920 "}\n";
921
922/* Declare common fragment shader uniforms */
923#define FRAGMENT_CONVERT_YUV \
924 " y *= alpha;\n" \
925 " u *= alpha;\n" \
926 " v *= alpha;\n" \
927 " gl_FragColor.r = y + 1.59602678 * v;\n" \
928 " gl_FragColor.g = y - 0.39176229 * u - 0.81296764 * v;\n" \
929 " gl_FragColor.b = y + 2.01723214 * u;\n" \
930 " gl_FragColor.a = alpha;\n"
931
932static const char texture_fragment_shader_rgba[] =
933 "precision mediump float;\n"
934 "varying vec2 v_texcoord;\n"
935 "uniform sampler2D tex;\n"
936 "uniform float alpha;\n"
937 "void main()\n"
938 "{\n"
939 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
940 "}\n";
941
942static const char texture_fragment_shader_rgbx[] =
943 "precision mediump float;\n"
944 "varying vec2 v_texcoord;\n"
945 "uniform sampler2D tex;\n"
946 "uniform float alpha;\n"
947 "void main()\n"
948 "{\n"
949 " gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb\n;"
950 " gl_FragColor.a = alpha;\n"
951 "}\n";
952
953static const char texture_fragment_shader_egl_external[] =
954 "#extension GL_OES_EGL_image_external : require\n"
955 "precision mediump float;\n"
956 "varying vec2 v_texcoord;\n"
957 "uniform samplerExternalOES tex;\n"
958 "uniform float alpha;\n"
959 "void main()\n"
960 "{\n"
961 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
962 "}\n";
963
964static const char texture_fragment_shader_y_uv[] =
965 "precision mediump float;\n"
966 "uniform sampler2D tex;\n"
967 "uniform sampler2D tex1;\n"
968 "varying vec2 v_texcoord;\n"
969 "uniform float alpha;\n"
970 "void main() {\n"
971 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
972 " float u = texture2D(tex1, v_texcoord).r - 0.5;\n"
973 " float v = texture2D(tex1, v_texcoord).g - 0.5;\n"
974 FRAGMENT_CONVERT_YUV
975 "}\n";
976
977static const char texture_fragment_shader_y_u_v[] =
978 "precision mediump float;\n"
979 "uniform sampler2D tex;\n"
980 "uniform sampler2D tex1;\n"
981 "uniform sampler2D tex2;\n"
982 "varying vec2 v_texcoord;\n"
983 "uniform float alpha;\n"
984 "void main() {\n"
985 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
986 " float u = texture2D(tex1, v_texcoord).x - 0.5;\n"
987 " float v = texture2D(tex2, v_texcoord).x - 0.5;\n"
988 FRAGMENT_CONVERT_YUV
989 "}\n";
990
991static const char texture_fragment_shader_y_xuxv[] =
992 "precision mediump float;\n"
993 "uniform sampler2D tex;\n"
994 "uniform sampler2D tex1;\n"
995 "varying vec2 v_texcoord;\n"
996 "uniform float alpha;\n"
997 "void main() {\n"
998 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
999 " float u = texture2D(tex1, v_texcoord).g - 0.5;\n"
1000 " float v = texture2D(tex1, v_texcoord).a - 0.5;\n"
1001 FRAGMENT_CONVERT_YUV
1002 "}\n";
1003
1004static const char solid_fragment_shader[] =
1005 "precision mediump float;\n"
1006 "uniform vec4 color;\n"
1007 "uniform float alpha;\n"
1008 "void main()\n"
1009 "{\n"
1010 " gl_FragColor = alpha * color\n;"
1011 "}\n";
1012
1013static int
1014compile_shader(GLenum type, const char *source)
1015{
1016 GLuint s;
1017 char msg[512];
1018 GLint status;
1019
1020 s = glCreateShader(type);
1021 glShaderSource(s, 1, &source, NULL);
1022 glCompileShader(s);
1023 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
1024 if (!status) {
1025 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1026 weston_log("shader info: %s\n", msg);
1027 return GL_NONE;
1028 }
1029
1030 return s;
1031}
1032
1033static int
1034weston_shader_init(struct weston_shader *shader,
1035 const char *vertex_source, const char *fragment_source)
1036{
1037 char msg[512];
1038 GLint status;
1039
1040 shader->vertex_shader =
1041 compile_shader(GL_VERTEX_SHADER, vertex_source);
1042 shader->fragment_shader =
1043 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
1044
1045 shader->program = glCreateProgram();
1046 glAttachShader(shader->program, shader->vertex_shader);
1047 glAttachShader(shader->program, shader->fragment_shader);
1048 glBindAttribLocation(shader->program, 0, "position");
1049 glBindAttribLocation(shader->program, 1, "texcoord");
1050
1051 glLinkProgram(shader->program);
1052 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1053 if (!status) {
1054 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1055 weston_log("link info: %s\n", msg);
1056 return -1;
1057 }
1058
1059 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1060 shader->tex_uniforms[0] = glGetUniformLocation(shader->program, "tex");
1061 shader->tex_uniforms[1] = glGetUniformLocation(shader->program, "tex1");
1062 shader->tex_uniforms[2] = glGetUniformLocation(shader->program, "tex2");
1063 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
1064 shader->color_uniform = glGetUniformLocation(shader->program, "color");
1065
1066 return 0;
1067}
1068
1069static void
1070log_extensions(const char *name, const char *extensions)
1071{
1072 const char *p, *end;
1073 int l;
1074 int len;
1075
1076 l = weston_log("%s:", name);
1077 p = extensions;
1078 while (*p) {
1079 end = strchrnul(p, ' ');
1080 len = end - p;
1081 if (l + len > 78)
1082 l = weston_log_continue("\n" STAMP_SPACE "%.*s",
1083 len, p);
1084 else
1085 l += weston_log_continue(" %.*s", len, p);
1086 for (p = end; isspace(*p); p++)
1087 ;
1088 }
1089 weston_log_continue("\n");
1090}
1091
1092static void
1093log_egl_gl_info(EGLDisplay egldpy)
1094{
1095 const char *str;
1096
1097 str = eglQueryString(egldpy, EGL_VERSION);
1098 weston_log("EGL version: %s\n", str ? str : "(null)");
1099
1100 str = eglQueryString(egldpy, EGL_VENDOR);
1101 weston_log("EGL vendor: %s\n", str ? str : "(null)");
1102
1103 str = eglQueryString(egldpy, EGL_CLIENT_APIS);
1104 weston_log("EGL client APIs: %s\n", str ? str : "(null)");
1105
1106 str = eglQueryString(egldpy, EGL_EXTENSIONS);
1107 log_extensions("EGL extensions", str ? str : "(null)");
1108
1109 str = (char *)glGetString(GL_VERSION);
1110 weston_log("GL version: %s\n", str ? str : "(null)");
1111
1112 str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
1113 weston_log("GLSL version: %s\n", str ? str : "(null)");
1114
1115 str = (char *)glGetString(GL_VENDOR);
1116 weston_log("GL vendor: %s\n", str ? str : "(null)");
1117
1118 str = (char *)glGetString(GL_RENDERER);
1119 weston_log("GL renderer: %s\n", str ? str : "(null)");
1120
1121 str = (char *)glGetString(GL_EXTENSIONS);
1122 log_extensions("GL extensions", str ? str : "(null)");
1123}
1124
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001125struct gles2_renderer {
1126 struct weston_renderer base;
1127};
1128
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001129WL_EXPORT void
1130gles2_renderer_destroy(struct weston_compositor *ec)
1131{
1132 if (ec->has_bind_display)
1133 ec->unbind_display(ec->egl_display, ec->wl_display);
1134}
1135
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001136WL_EXPORT int
1137gles2_renderer_init(struct weston_compositor *ec)
1138{
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001139 struct gles2_renderer *renderer;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001140 const char *extensions;
1141 int has_egl_image_external = 0;
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001142 struct weston_output *output;
1143 EGLBoolean ret;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001144
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001145 static const EGLint context_attribs[] = {
1146 EGL_CONTEXT_CLIENT_VERSION, 2,
1147 EGL_NONE
1148 };
1149
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001150 renderer = malloc(sizeof *renderer);
1151 if (renderer == NULL)
1152 return -1;
1153
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001154 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
1155 weston_log("failed to bind EGL_OPENGL_ES_API\n");
1156 print_egl_error_state();
1157 return -1;
1158 }
1159 ec->egl_context = eglCreateContext(ec->egl_display, ec->egl_config,
1160 EGL_NO_CONTEXT, context_attribs);
1161 if (ec->egl_context == NULL) {
1162 weston_log("failed to create context\n");
1163 print_egl_error_state();
1164 return -1;
1165 }
1166
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001167 output = container_of(ec->output_list.next,
1168 struct weston_output, link);
1169 ret = eglMakeCurrent(ec->egl_display, output->egl_surface,
1170 output->egl_surface, ec->egl_context);
1171 if (ret == EGL_FALSE) {
1172 weston_log("Failed to make EGL context current.\n");
1173 print_egl_error_state();
1174 return -1;
1175 }
1176
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001177 log_egl_gl_info(ec->egl_display);
1178
1179 ec->image_target_texture_2d =
1180 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
1181 ec->image_target_renderbuffer_storage = (void *)
1182 eglGetProcAddress("glEGLImageTargetRenderbufferStorageOES");
1183 ec->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1184 ec->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1185 ec->bind_display =
1186 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
1187 ec->unbind_display =
1188 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
1189 ec->query_buffer =
1190 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
1191
1192 extensions = (const char *) glGetString(GL_EXTENSIONS);
1193 if (!extensions) {
1194 weston_log("Retrieving GL extension string failed.\n");
1195 return -1;
1196 }
1197
1198 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
1199 weston_log("GL_EXT_texture_format_BGRA8888 not available\n");
1200 return -1;
1201 }
1202
1203 if (strstr(extensions, "GL_EXT_read_format_bgra"))
1204 ec->read_format = GL_BGRA_EXT;
1205 else
1206 ec->read_format = GL_RGBA;
1207
1208 if (strstr(extensions, "GL_EXT_unpack_subimage"))
1209 ec->has_unpack_subimage = 1;
1210
1211 if (strstr(extensions, "GL_OES_EGL_image_external"))
1212 has_egl_image_external = 1;
1213
1214 extensions =
1215 (const char *) eglQueryString(ec->egl_display, EGL_EXTENSIONS);
1216 if (!extensions) {
1217 weston_log("Retrieving EGL extension string failed.\n");
1218 return -1;
1219 }
1220
1221 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
1222 ec->has_bind_display = 1;
1223 if (ec->has_bind_display)
1224 ec->bind_display(ec->egl_display, ec->wl_display);
1225
1226 glActiveTexture(GL_TEXTURE0);
1227
1228 if (weston_shader_init(&ec->texture_shader_rgba,
1229 vertex_shader, texture_fragment_shader_rgba) < 0)
1230 return -1;
1231 if (weston_shader_init(&ec->texture_shader_rgbx,
1232 vertex_shader, texture_fragment_shader_rgbx) < 0)
1233 return -1;
1234 if (has_egl_image_external &&
1235 weston_shader_init(&ec->texture_shader_egl_external,
1236 vertex_shader, texture_fragment_shader_egl_external) < 0)
1237 return -1;
1238 if (weston_shader_init(&ec->texture_shader_y_uv,
1239 vertex_shader, texture_fragment_shader_y_uv) < 0)
1240 return -1;
1241 if (weston_shader_init(&ec->texture_shader_y_u_v,
1242 vertex_shader, texture_fragment_shader_y_u_v) < 0)
1243 return -1;
1244 if (weston_shader_init(&ec->texture_shader_y_xuxv,
1245 vertex_shader, texture_fragment_shader_y_xuxv) < 0)
1246 return -1;
1247 if (weston_shader_init(&ec->solid_shader,
1248 vertex_shader, solid_fragment_shader) < 0)
1249 return -1;
1250
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001251 renderer->base.repaint_output = gles2_renderer_repaint_output;
1252 renderer->base.flush_damage = gles2_renderer_flush_damage;
1253 renderer->base.attach = gles2_renderer_attach;
Kristian Høgsberg42263852012-09-06 21:59:29 -04001254 renderer->base.destroy_surface = gles2_renderer_destroy_surface;
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001255 ec->renderer = &renderer->base;
1256
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001257 return 0;
1258}