blob: c12d671a721189ee50dae5900cd8dc9096789ac2 [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
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010025#include <GLES2/gl2.h>
26#include <GLES2/gl2ext.h>
27
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -040028#include <stdlib.h>
Kristian Høgsberg25894fc2012-09-05 22:06:26 -040029#include <string.h>
30#include <ctype.h>
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +030031#include <float.h>
32#include <assert.h>
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +020033#include <linux/input.h>
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -040034
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010035#include "gl-renderer.h"
36
37#include <EGL/eglext.h>
38#include "weston-egl-ext.h"
Kristian Høgsbergd7c17262012-09-05 21:54:15 -040039
John Kåre Alsaker40684142012-11-13 19:10:25 +010040struct gles2_shader {
41 GLuint program;
42 GLuint vertex_shader, fragment_shader;
43 GLint proj_uniform;
44 GLint tex_uniforms[3];
45 GLint alpha_uniform;
46 GLint color_uniform;
47};
48
John Kåre Alsaker94659272012-11-13 19:10:18 +010049struct gles2_output_state {
50 EGLSurface egl_surface;
51};
52
John Kåre Alsaker878f4492012-11-13 19:10:23 +010053struct gles2_surface_state {
54 GLfloat color[4];
John Kåre Alsaker40684142012-11-13 19:10:25 +010055 struct gles2_shader *shader;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +010056
57 GLuint textures[3];
58 int num_textures;
59
60 EGLImageKHR images[3];
61 GLenum target;
62 int num_images;
John Kåre Alsaker878f4492012-11-13 19:10:23 +010063};
64
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +010065struct gles2_renderer {
66 struct weston_renderer base;
67 int fragment_shader_debug;
68
69 EGLDisplay egl_display;
70 EGLContext egl_context;
71 EGLConfig egl_config;
John Kåre Alsaker44154502012-11-13 19:10:20 +010072
73 struct {
74 int32_t top, bottom, left, right;
75 GLuint texture;
76 int32_t width, height;
77 } border;
John Kåre Alsaker40684142012-11-13 19:10:25 +010078
John Kåre Alsaker320711d2012-11-13 19:10:27 +010079 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
80 PFNEGLCREATEIMAGEKHRPROC create_image;
81 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
82
83 int has_unpack_subimage;
84
85 PFNEGLBINDWAYLANDDISPLAYWL bind_display;
86 PFNEGLUNBINDWAYLANDDISPLAYWL unbind_display;
87 PFNEGLQUERYWAYLANDBUFFERWL query_buffer;
88 int has_bind_display;
89
90 int has_egl_image_external;
91
John Kåre Alsaker40684142012-11-13 19:10:25 +010092 struct gles2_shader texture_shader_rgba;
93 struct gles2_shader texture_shader_rgbx;
94 struct gles2_shader texture_shader_egl_external;
95 struct gles2_shader texture_shader_y_uv;
96 struct gles2_shader texture_shader_y_u_v;
97 struct gles2_shader texture_shader_y_xuxv;
98 struct gles2_shader invert_color_shader;
99 struct gles2_shader solid_shader;
100 struct gles2_shader *current_shader;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100101};
John Kåre Alsaker94659272012-11-13 19:10:18 +0100102
103static inline struct gles2_output_state *
104get_output_state(struct weston_output *output)
105{
106 return (struct gles2_output_state *)output->renderer_state;
107}
108
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100109static inline struct gles2_surface_state *
110get_surface_state(struct weston_surface *surface)
111{
112 return (struct gles2_surface_state *)surface->renderer_state;
113}
114
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100115static inline struct gles2_renderer *
116get_renderer(struct weston_compositor *ec)
117{
118 return (struct gles2_renderer *)ec->renderer;
119}
120
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400121static const char *
122egl_error_string(EGLint code)
123{
124#define MYERRCODE(x) case x: return #x;
125 switch (code) {
126 MYERRCODE(EGL_SUCCESS)
127 MYERRCODE(EGL_NOT_INITIALIZED)
128 MYERRCODE(EGL_BAD_ACCESS)
129 MYERRCODE(EGL_BAD_ALLOC)
130 MYERRCODE(EGL_BAD_ATTRIBUTE)
131 MYERRCODE(EGL_BAD_CONTEXT)
132 MYERRCODE(EGL_BAD_CONFIG)
133 MYERRCODE(EGL_BAD_CURRENT_SURFACE)
134 MYERRCODE(EGL_BAD_DISPLAY)
135 MYERRCODE(EGL_BAD_SURFACE)
136 MYERRCODE(EGL_BAD_MATCH)
137 MYERRCODE(EGL_BAD_PARAMETER)
138 MYERRCODE(EGL_BAD_NATIVE_PIXMAP)
139 MYERRCODE(EGL_BAD_NATIVE_WINDOW)
140 MYERRCODE(EGL_CONTEXT_LOST)
141 default:
142 return "unknown";
143 }
144#undef MYERRCODE
145}
146
147static void
148print_egl_error_state(void)
149{
150 EGLint code;
151
152 code = eglGetError();
153 weston_log("EGL error state: %s (0x%04lx)\n",
154 egl_error_string(code), (long)code);
155}
156
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300157struct polygon8 {
158 GLfloat x[8];
159 GLfloat y[8];
160 int n;
161};
162
163struct clip_context {
164 struct {
165 GLfloat x;
166 GLfloat y;
167 } prev;
168
169 struct {
170 GLfloat x1, y1;
171 GLfloat x2, y2;
172 } clip;
173
174 struct {
175 GLfloat *x;
176 GLfloat *y;
177 } vertices;
178};
179
180static GLfloat
181float_difference(GLfloat a, GLfloat b)
182{
183 /* http://www.altdevblogaday.com/2012/02/22/comparing-floating-point-numbers-2012-edition/ */
184 static const GLfloat max_diff = 4.0f * FLT_MIN;
185 static const GLfloat max_rel_diff = 4.0e-5;
186 GLfloat diff = a - b;
187 GLfloat adiff = fabsf(diff);
188
189 if (adiff <= max_diff)
190 return 0.0f;
191
192 a = fabsf(a);
193 b = fabsf(b);
194 if (adiff <= (a > b ? a : b) * max_rel_diff)
195 return 0.0f;
196
197 return diff;
198}
199
200/* A line segment (p1x, p1y)-(p2x, p2y) intersects the line x = x_arg.
201 * Compute the y coordinate of the intersection.
202 */
203static GLfloat
204clip_intersect_y(GLfloat p1x, GLfloat p1y, GLfloat p2x, GLfloat p2y,
205 GLfloat x_arg)
206{
207 GLfloat a;
208 GLfloat diff = float_difference(p1x, p2x);
209
210 /* Practically vertical line segment, yet the end points have already
211 * been determined to be on different sides of the line. Therefore
212 * the line segment is part of the line and intersects everywhere.
213 * Return the end point, so we use the whole line segment.
214 */
215 if (diff == 0.0f)
216 return p2y;
217
218 a = (x_arg - p2x) / diff;
219 return p2y + (p1y - p2y) * a;
220}
221
222/* A line segment (p1x, p1y)-(p2x, p2y) intersects the line y = y_arg.
223 * Compute the x coordinate of the intersection.
224 */
225static GLfloat
226clip_intersect_x(GLfloat p1x, GLfloat p1y, GLfloat p2x, GLfloat p2y,
227 GLfloat y_arg)
228{
229 GLfloat a;
230 GLfloat diff = float_difference(p1y, p2y);
231
232 /* Practically horizontal line segment, yet the end points have already
233 * been determined to be on different sides of the line. Therefore
234 * the line segment is part of the line and intersects everywhere.
235 * Return the end point, so we use the whole line segment.
236 */
237 if (diff == 0.0f)
238 return p2x;
239
240 a = (y_arg - p2y) / diff;
241 return p2x + (p1x - p2x) * a;
242}
243
244enum path_transition {
245 PATH_TRANSITION_OUT_TO_OUT = 0,
246 PATH_TRANSITION_OUT_TO_IN = 1,
247 PATH_TRANSITION_IN_TO_OUT = 2,
248 PATH_TRANSITION_IN_TO_IN = 3,
249};
250
251static void
252clip_append_vertex(struct clip_context *ctx, GLfloat x, GLfloat y)
253{
254 *ctx->vertices.x++ = x;
255 *ctx->vertices.y++ = y;
256}
257
258static enum path_transition
259path_transition_left_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
260{
261 return ((ctx->prev.x >= ctx->clip.x1) << 1) | (x >= ctx->clip.x1);
262}
263
264static enum path_transition
265path_transition_right_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
266{
267 return ((ctx->prev.x < ctx->clip.x2) << 1) | (x < ctx->clip.x2);
268}
269
270static enum path_transition
271path_transition_top_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
272{
273 return ((ctx->prev.y >= ctx->clip.y1) << 1) | (y >= ctx->clip.y1);
274}
275
276static enum path_transition
277path_transition_bottom_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
278{
279 return ((ctx->prev.y < ctx->clip.y2) << 1) | (y < ctx->clip.y2);
280}
281
282static void
283clip_polygon_leftright(struct clip_context *ctx,
284 enum path_transition transition,
285 GLfloat x, GLfloat y, GLfloat clip_x)
286{
287 GLfloat yi;
288
289 switch (transition) {
290 case PATH_TRANSITION_IN_TO_IN:
291 clip_append_vertex(ctx, x, y);
292 break;
293 case PATH_TRANSITION_IN_TO_OUT:
294 yi = clip_intersect_y(ctx->prev.x, ctx->prev.y, x, y, clip_x);
295 clip_append_vertex(ctx, clip_x, yi);
296 break;
297 case PATH_TRANSITION_OUT_TO_IN:
298 yi = clip_intersect_y(ctx->prev.x, ctx->prev.y, x, y, clip_x);
299 clip_append_vertex(ctx, clip_x, yi);
300 clip_append_vertex(ctx, x, y);
301 break;
302 case PATH_TRANSITION_OUT_TO_OUT:
303 /* nothing */
304 break;
305 default:
306 assert(0 && "bad enum path_transition");
307 }
308
309 ctx->prev.x = x;
310 ctx->prev.y = y;
311}
312
313static void
314clip_polygon_topbottom(struct clip_context *ctx,
315 enum path_transition transition,
316 GLfloat x, GLfloat y, GLfloat clip_y)
317{
318 GLfloat xi;
319
320 switch (transition) {
321 case PATH_TRANSITION_IN_TO_IN:
322 clip_append_vertex(ctx, x, y);
323 break;
324 case PATH_TRANSITION_IN_TO_OUT:
325 xi = clip_intersect_x(ctx->prev.x, ctx->prev.y, x, y, clip_y);
326 clip_append_vertex(ctx, xi, clip_y);
327 break;
328 case PATH_TRANSITION_OUT_TO_IN:
329 xi = clip_intersect_x(ctx->prev.x, ctx->prev.y, x, y, clip_y);
330 clip_append_vertex(ctx, xi, clip_y);
331 clip_append_vertex(ctx, x, y);
332 break;
333 case PATH_TRANSITION_OUT_TO_OUT:
334 /* nothing */
335 break;
336 default:
337 assert(0 && "bad enum path_transition");
338 }
339
340 ctx->prev.x = x;
341 ctx->prev.y = y;
342}
343
344static void
345clip_context_prepare(struct clip_context *ctx, const struct polygon8 *src,
346 GLfloat *dst_x, GLfloat *dst_y)
347{
348 ctx->prev.x = src->x[src->n - 1];
349 ctx->prev.y = src->y[src->n - 1];
350 ctx->vertices.x = dst_x;
351 ctx->vertices.y = dst_y;
352}
353
354static int
355clip_polygon_left(struct clip_context *ctx, const struct polygon8 *src,
356 GLfloat *dst_x, GLfloat *dst_y)
357{
358 enum path_transition trans;
359 int i;
360
361 clip_context_prepare(ctx, src, dst_x, dst_y);
362 for (i = 0; i < src->n; i++) {
363 trans = path_transition_left_edge(ctx, src->x[i], src->y[i]);
364 clip_polygon_leftright(ctx, trans, src->x[i], src->y[i],
365 ctx->clip.x1);
366 }
367 return ctx->vertices.x - dst_x;
368}
369
370static int
371clip_polygon_right(struct clip_context *ctx, const struct polygon8 *src,
372 GLfloat *dst_x, GLfloat *dst_y)
373{
374 enum path_transition trans;
375 int i;
376
377 clip_context_prepare(ctx, src, dst_x, dst_y);
378 for (i = 0; i < src->n; i++) {
379 trans = path_transition_right_edge(ctx, src->x[i], src->y[i]);
380 clip_polygon_leftright(ctx, trans, src->x[i], src->y[i],
381 ctx->clip.x2);
382 }
383 return ctx->vertices.x - dst_x;
384}
385
386static int
387clip_polygon_top(struct clip_context *ctx, const struct polygon8 *src,
388 GLfloat *dst_x, GLfloat *dst_y)
389{
390 enum path_transition trans;
391 int i;
392
393 clip_context_prepare(ctx, src, dst_x, dst_y);
394 for (i = 0; i < src->n; i++) {
395 trans = path_transition_top_edge(ctx, src->x[i], src->y[i]);
396 clip_polygon_topbottom(ctx, trans, src->x[i], src->y[i],
397 ctx->clip.y1);
398 }
399 return ctx->vertices.x - dst_x;
400}
401
402static int
403clip_polygon_bottom(struct clip_context *ctx, const struct polygon8 *src,
404 GLfloat *dst_x, GLfloat *dst_y)
405{
406 enum path_transition trans;
407 int i;
408
409 clip_context_prepare(ctx, src, dst_x, dst_y);
410 for (i = 0; i < src->n; i++) {
411 trans = path_transition_bottom_edge(ctx, src->x[i], src->y[i]);
412 clip_polygon_topbottom(ctx, trans, src->x[i], src->y[i],
413 ctx->clip.y2);
414 }
415 return ctx->vertices.x - dst_x;
416}
417
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400418#define max(a, b) (((a) > (b)) ? (a) : (b))
419#define min(a, b) (((a) > (b)) ? (b) : (a))
420#define clip(x, a, b) min(max(x, a), b)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400421
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300422/*
423 * Compute the boundary vertices of the intersection of the global coordinate
424 * aligned rectangle 'rect', and an arbitrary quadrilateral produced from
425 * 'surf_rect' when transformed from surface coordinates into global coordinates.
426 * The vertices are written to 'ex' and 'ey', and the return value is the
427 * number of vertices. Vertices are produced in clockwise winding order.
428 * Guarantees to produce either zero vertices, or 3-8 vertices with non-zero
429 * polygon area.
430 */
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400431static int
432calculate_edges(struct weston_surface *es, pixman_box32_t *rect,
433 pixman_box32_t *surf_rect, GLfloat *ex, GLfloat *ey)
434{
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300435 struct polygon8 polygon;
436 struct clip_context ctx;
437 int i, n;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400438 GLfloat min_x, max_x, min_y, max_y;
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300439 struct polygon8 surf = {
440 { surf_rect->x1, surf_rect->x2, surf_rect->x2, surf_rect->x1 },
441 { surf_rect->y1, surf_rect->y1, surf_rect->y2, surf_rect->y2 },
442 4
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400443 };
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400444
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300445 ctx.clip.x1 = rect->x1;
446 ctx.clip.y1 = rect->y1;
447 ctx.clip.x2 = rect->x2;
448 ctx.clip.y2 = rect->y2;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400449
450 /* transform surface to screen space: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300451 for (i = 0; i < surf.n; i++)
452 weston_surface_to_global_float(es, surf.x[i], surf.y[i],
453 &surf.x[i], &surf.y[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400454
455 /* find bounding box: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300456 min_x = max_x = surf.x[0];
457 min_y = max_y = surf.y[0];
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400458
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300459 for (i = 1; i < surf.n; i++) {
460 min_x = min(min_x, surf.x[i]);
461 max_x = max(max_x, surf.x[i]);
462 min_y = min(min_y, surf.y[i]);
463 max_y = max(max_y, surf.y[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400464 }
465
466 /* First, simple bounding box check to discard early transformed
467 * surface rects that do not intersect with the clip region:
468 */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300469 if ((min_x >= ctx.clip.x2) || (max_x <= ctx.clip.x1) ||
470 (min_y >= ctx.clip.y2) || (max_y <= ctx.clip.y1))
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400471 return 0;
472
473 /* Simple case, bounding box edges are parallel to surface edges,
474 * there will be only four edges. We just need to clip the surface
475 * vertices to the clip rect bounds:
476 */
477 if (!es->transform.enabled) {
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300478 for (i = 0; i < surf.n; i++) {
479 ex[i] = clip(surf.x[i], ctx.clip.x1, ctx.clip.x2);
480 ey[i] = clip(surf.y[i], ctx.clip.y1, ctx.clip.y2);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400481 }
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300482 return surf.n;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400483 }
484
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300485 /* Transformed case: use a general polygon clipping algorithm to
486 * clip the surface rectangle with each side of 'rect'.
487 * The algorithm is Sutherland-Hodgman, as explained in
488 * http://www.codeguru.com/cpp/misc/misc/graphics/article.php/c8965/Polygon-Clipping.htm
489 * but without looking at any of that code.
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400490 */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300491 polygon.n = clip_polygon_left(&ctx, &surf, polygon.x, polygon.y);
492 surf.n = clip_polygon_right(&ctx, &polygon, surf.x, surf.y);
493 polygon.n = clip_polygon_top(&ctx, &surf, polygon.x, polygon.y);
494 surf.n = clip_polygon_bottom(&ctx, &polygon, surf.x, surf.y);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400495
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300496 /* Get rid of duplicate vertices */
497 ex[0] = surf.x[0];
498 ey[0] = surf.y[0];
499 n = 1;
500 for (i = 1; i < surf.n; i++) {
501 if (float_difference(ex[n - 1], surf.x[i]) == 0.0f &&
502 float_difference(ey[n - 1], surf.y[i]) == 0.0f)
503 continue;
504 ex[n] = surf.x[i];
505 ey[n] = surf.y[i];
506 n++;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400507 }
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300508 if (float_difference(ex[n - 1], surf.x[0]) == 0.0f &&
509 float_difference(ey[n - 1], surf.y[0]) == 0.0f)
510 n--;
511
512 if (n < 3)
513 return 0;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400514
515 return n;
516}
517
518static int
519texture_region(struct weston_surface *es, pixman_region32_t *region,
520 pixman_region32_t *surf_region)
521{
522 struct weston_compositor *ec = es->compositor;
523 GLfloat *v, inv_width, inv_height;
524 unsigned int *vtxcnt, nvtx = 0;
525 pixman_box32_t *rects, *surf_rects;
526 int i, j, k, nrects, nsurf;
527
528 rects = pixman_region32_rectangles(region, &nrects);
529 surf_rects = pixman_region32_rectangles(surf_region, &nsurf);
530
531 /* worst case we can have 8 vertices per rect (ie. clipped into
532 * an octagon):
533 */
534 v = wl_array_add(&ec->vertices, nrects * nsurf * 8 * 4 * sizeof *v);
535 vtxcnt = wl_array_add(&ec->vtxcnt, nrects * nsurf * sizeof *vtxcnt);
536
537 inv_width = 1.0 / es->pitch;
538 inv_height = 1.0 / es->geometry.height;
539
540 for (i = 0; i < nrects; i++) {
541 pixman_box32_t *rect = &rects[i];
542 for (j = 0; j < nsurf; j++) {
543 pixman_box32_t *surf_rect = &surf_rects[j];
544 GLfloat sx, sy;
545 GLfloat ex[8], ey[8]; /* edge points in screen space */
546 int n;
547
548 /* The transformed surface, after clipping to the clip region,
549 * can have as many as eight sides, emitted as a triangle-fan.
550 * The first vertex in the triangle fan can be chosen arbitrarily,
551 * since the area is guaranteed to be convex.
552 *
553 * If a corner of the transformed surface falls outside of the
554 * clip region, instead of emitting one vertex for the corner
555 * of the surface, up to two are emitted for two corresponding
556 * intersection point(s) between the surface and the clip region.
557 *
558 * To do this, we first calculate the (up to eight) points that
559 * form the intersection of the clip rect and the transformed
560 * surface.
561 */
562 n = calculate_edges(es, rect, surf_rect, ex, ey);
563 if (n < 3)
564 continue;
565
566 /* emit edge points: */
567 for (k = 0; k < n; k++) {
568 weston_surface_from_global_float(es, ex[k], ey[k], &sx, &sy);
569 /* position: */
570 *(v++) = ex[k];
571 *(v++) = ey[k];
572 /* texcoord: */
573 *(v++) = sx * inv_width;
574 *(v++) = sy * inv_height;
575 }
576
577 vtxcnt[nvtx++] = n;
578 }
579 }
580
581 return nvtx;
582}
583
584static void
585triangle_fan_debug(struct weston_surface *surface, int first, int count)
586{
587 struct weston_compositor *compositor = surface->compositor;
John Kåre Alsaker40684142012-11-13 19:10:25 +0100588 struct gles2_renderer *gr = get_renderer(compositor);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400589 int i;
590 GLushort *buffer;
591 GLushort *index;
592 int nelems;
593 static int color_idx = 0;
594 static const GLfloat color[][4] = {
595 { 1.0, 0.0, 0.0, 1.0 },
596 { 0.0, 1.0, 0.0, 1.0 },
597 { 0.0, 0.0, 1.0, 1.0 },
598 { 1.0, 1.0, 1.0, 1.0 },
599 };
600
601 nelems = (count - 1 + count - 2) * 2;
602
603 buffer = malloc(sizeof(GLushort) * nelems);
604 index = buffer;
605
606 for (i = 1; i < count; i++) {
607 *index++ = first;
608 *index++ = first + i;
609 }
610
611 for (i = 2; i < count; i++) {
612 *index++ = first + i - 1;
613 *index++ = first + i;
614 }
615
John Kåre Alsaker40684142012-11-13 19:10:25 +0100616 glUseProgram(gr->solid_shader.program);
617 glUniform4fv(gr->solid_shader.color_uniform, 1,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400618 color[color_idx++ % ARRAY_LENGTH(color)]);
619 glDrawElements(GL_LINES, nelems, GL_UNSIGNED_SHORT, buffer);
John Kåre Alsaker40684142012-11-13 19:10:25 +0100620 glUseProgram(gr->current_shader->program);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400621 free(buffer);
622}
623
624static void
625repaint_region(struct weston_surface *es, pixman_region32_t *region,
626 pixman_region32_t *surf_region)
627{
628 struct weston_compositor *ec = es->compositor;
629 GLfloat *v;
630 unsigned int *vtxcnt;
631 int i, first, nfans;
632
633 /* The final region to be painted is the intersection of
634 * 'region' and 'surf_region'. However, 'region' is in the global
635 * coordinates, and 'surf_region' is in the surface-local
636 * coordinates. texture_region() will iterate over all pairs of
637 * rectangles from both regions, compute the intersection
638 * polygon for each pair, and store it as a triangle fan if
639 * it has a non-zero area (at least 3 vertices, actually).
640 */
641 nfans = texture_region(es, region, surf_region);
642
643 v = ec->vertices.data;
644 vtxcnt = ec->vtxcnt.data;
645
646 /* position: */
647 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
648 glEnableVertexAttribArray(0);
649
650 /* texcoord: */
651 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
652 glEnableVertexAttribArray(1);
653
654 for (i = 0, first = 0; i < nfans; i++) {
655 glDrawArrays(GL_TRIANGLE_FAN, first, vtxcnt[i]);
656 if (ec->fan_debug)
657 triangle_fan_debug(es, first, vtxcnt[i]);
658 first += vtxcnt[i];
659 }
660
661 glDisableVertexAttribArray(1);
662 glDisableVertexAttribArray(0);
663
664 ec->vertices.size = 0;
665 ec->vtxcnt.size = 0;
666}
667
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100668static int
669use_output(struct weston_output *output)
670{
671 static int errored;
672 struct gles2_output_state *go = get_output_state(output);
673 struct gles2_renderer *gr = get_renderer(output->compositor);
674 EGLBoolean ret;
675
676 ret = eglMakeCurrent(gr->egl_display, go->egl_surface,
677 go->egl_surface, gr->egl_context);
678
679 if (ret == EGL_FALSE) {
680 if (errored)
681 return -1;
682 errored = 1;
683 weston_log("Failed to make EGL context current.\n");
684 print_egl_error_state();
685 return -1;
686 }
687
688 return 0;
689}
690
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400691static void
John Kåre Alsaker40684142012-11-13 19:10:25 +0100692use_shader(struct gles2_renderer *gr,
693 struct gles2_shader *shader)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400694{
John Kåre Alsaker40684142012-11-13 19:10:25 +0100695 if (gr->current_shader == shader)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400696 return;
697
698 glUseProgram(shader->program);
John Kåre Alsaker40684142012-11-13 19:10:25 +0100699 gr->current_shader = shader;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400700}
701
702static void
John Kåre Alsaker40684142012-11-13 19:10:25 +0100703shader_uniforms(struct gles2_shader *shader,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400704 struct weston_surface *surface,
705 struct weston_output *output)
706{
707 int i;
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100708 struct gles2_surface_state *gs = get_surface_state(surface);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400709
710 glUniformMatrix4fv(shader->proj_uniform,
711 1, GL_FALSE, output->matrix.d);
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100712 glUniform4fv(shader->color_uniform, 1, gs->color);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400713 glUniform1f(shader->alpha_uniform, surface->alpha);
714
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100715 for (i = 0; i < gs->num_textures; i++)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400716 glUniform1i(shader->tex_uniforms[i], i);
717}
718
719static void
720draw_surface(struct weston_surface *es, struct weston_output *output,
721 pixman_region32_t *damage) /* in global coordinates */
722{
723 struct weston_compositor *ec = es->compositor;
John Kåre Alsaker40684142012-11-13 19:10:25 +0100724 struct gles2_renderer *gr = get_renderer(ec);
725 struct gles2_surface_state *gs = get_surface_state(es);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400726 /* repaint bounding region in global coordinates: */
727 pixman_region32_t repaint;
728 /* non-opaque region in surface coordinates: */
729 pixman_region32_t surface_blend;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300730 pixman_region32_t *buffer_damage;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400731 GLint filter;
732 int i;
733
734 pixman_region32_init(&repaint);
735 pixman_region32_intersect(&repaint,
736 &es->transform.boundingbox, damage);
737 pixman_region32_subtract(&repaint, &repaint, &es->clip);
738
739 if (!pixman_region32_not_empty(&repaint))
740 goto out;
741
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300742 buffer_damage = &output->buffer_damage[output->current_buffer];
743 pixman_region32_subtract(buffer_damage, buffer_damage, &repaint);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400744
745 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
746
747 if (ec->fan_debug) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100748 use_shader(gr, &gr->solid_shader);
749 shader_uniforms(&gr->solid_shader, es, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400750 }
751
John Kåre Alsaker40684142012-11-13 19:10:25 +0100752 use_shader(gr, gs->shader);
753 shader_uniforms(gs->shader, es, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400754
755 if (es->transform.enabled || output->zoom.active)
756 filter = GL_LINEAR;
757 else
758 filter = GL_NEAREST;
759
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100760 for (i = 0; i < gs->num_textures; i++) {
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400761 glActiveTexture(GL_TEXTURE0 + i);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100762 glBindTexture(gs->target, gs->textures[i]);
763 glTexParameteri(gs->target, GL_TEXTURE_MIN_FILTER, filter);
764 glTexParameteri(gs->target, GL_TEXTURE_MAG_FILTER, filter);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400765 }
766
767 /* blended region is whole surface minus opaque region: */
768 pixman_region32_init_rect(&surface_blend, 0, 0,
769 es->geometry.width, es->geometry.height);
770 pixman_region32_subtract(&surface_blend, &surface_blend, &es->opaque);
771
772 if (pixman_region32_not_empty(&es->opaque)) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100773 if (gs->shader == &gr->texture_shader_rgba) {
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400774 /* Special case for RGBA textures with possibly
775 * bad data in alpha channel: use the shader
776 * that forces texture alpha = 1.0.
777 * Xwayland surfaces need this.
778 */
John Kåre Alsaker40684142012-11-13 19:10:25 +0100779 use_shader(gr, &gr->texture_shader_rgbx);
780 shader_uniforms(&gr->texture_shader_rgbx, es, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400781 }
782
783 if (es->alpha < 1.0)
784 glEnable(GL_BLEND);
785 else
786 glDisable(GL_BLEND);
787
788 repaint_region(es, &repaint, &es->opaque);
789 }
790
791 if (pixman_region32_not_empty(&surface_blend)) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100792 use_shader(gr, gs->shader);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400793 glEnable(GL_BLEND);
794 repaint_region(es, &repaint, &surface_blend);
795 }
796
797 pixman_region32_fini(&surface_blend);
798
799out:
800 pixman_region32_fini(&repaint);
801}
802
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400803static void
804repaint_surfaces(struct weston_output *output, pixman_region32_t *damage)
805{
806 struct weston_compositor *compositor = output->compositor;
807 struct weston_surface *surface;
808
809 wl_list_for_each_reverse(surface, &compositor->surface_list, link)
810 if (surface->plane == &compositor->primary_plane)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400811 draw_surface(surface, output, damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400812}
813
John Kåre Alsaker44154502012-11-13 19:10:20 +0100814
815static int
816texture_border(struct weston_output *output)
817{
818 struct weston_compositor *ec = output->compositor;
819 struct gles2_renderer *gr = get_renderer(ec);
820 GLfloat *d;
821 unsigned int *p;
822 int i, j, k, n;
823 GLfloat x[4], y[4], u[4], v[4];
824
825 x[0] = -gr->border.left;
826 x[1] = 0;
827 x[2] = output->current->width;
828 x[3] = output->current->width + gr->border.right;
829
830 y[0] = -gr->border.top;
831 y[1] = 0;
832 y[2] = output->current->height;
833 y[3] = output->current->height + gr->border.bottom;
834
835 u[0] = 0.0;
836 u[1] = (GLfloat) gr->border.left / gr->border.width;
837 u[2] = (GLfloat) (gr->border.width - gr->border.right) / gr->border.width;
838 u[3] = 1.0;
839
840 v[0] = 0.0;
841 v[1] = (GLfloat) gr->border.top / gr->border.height;
842 v[2] = (GLfloat) (gr->border.height - gr->border.bottom) / gr->border.height;
843 v[3] = 1.0;
844
845 n = 8;
846 d = wl_array_add(&ec->vertices, n * 16 * sizeof *d);
847 p = wl_array_add(&ec->indices, n * 6 * sizeof *p);
848
849 k = 0;
850 for (i = 0; i < 3; i++)
851 for (j = 0; j < 3; j++) {
852
853 if (i == 1 && j == 1)
854 continue;
855
856 d[ 0] = x[i];
857 d[ 1] = y[j];
858 d[ 2] = u[i];
859 d[ 3] = v[j];
860
861 d[ 4] = x[i];
862 d[ 5] = y[j + 1];
863 d[ 6] = u[i];
864 d[ 7] = v[j + 1];
865
866 d[ 8] = x[i + 1];
867 d[ 9] = y[j];
868 d[10] = u[i + 1];
869 d[11] = v[j];
870
871 d[12] = x[i + 1];
872 d[13] = y[j + 1];
873 d[14] = u[i + 1];
874 d[15] = v[j + 1];
875
876 p[0] = k + 0;
877 p[1] = k + 1;
878 p[2] = k + 2;
879 p[3] = k + 2;
880 p[4] = k + 1;
881 p[5] = k + 3;
882
883 d += 16;
884 p += 6;
885 k += 4;
886 }
887
888 return k / 4;
889}
890
891static void
892draw_border(struct weston_output *output)
893{
894 struct weston_compositor *ec = output->compositor;
895 struct gles2_renderer *gr = get_renderer(ec);
John Kåre Alsaker40684142012-11-13 19:10:25 +0100896 struct gles2_shader *shader = &gr->texture_shader_rgba;
John Kåre Alsaker44154502012-11-13 19:10:20 +0100897 GLfloat *v;
898 int n;
899
900 glDisable(GL_BLEND);
John Kåre Alsaker40684142012-11-13 19:10:25 +0100901 use_shader(gr, shader);
John Kåre Alsaker44154502012-11-13 19:10:20 +0100902
903 glUniformMatrix4fv(shader->proj_uniform,
904 1, GL_FALSE, output->matrix.d);
905
906 glUniform1i(shader->tex_uniforms[0], 0);
907 glUniform1f(shader->alpha_uniform, 1);
908
909 n = texture_border(output);
910
911 glActiveTexture(GL_TEXTURE0);
912 glBindTexture(GL_TEXTURE_2D, gr->border.texture);
913
914 v = ec->vertices.data;
915 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
916 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
917 glEnableVertexAttribArray(0);
918 glEnableVertexAttribArray(1);
919
920 glDrawElements(GL_TRIANGLES, n * 6,
921 GL_UNSIGNED_INT, ec->indices.data);
922
923 glDisableVertexAttribArray(1);
924 glDisableVertexAttribArray(0);
925
926 ec->vertices.size = 0;
927 ec->indices.size = 0;
928}
929
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400930static void
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400931gles2_renderer_repaint_output(struct weston_output *output,
932 pixman_region32_t *output_damage)
933{
John Kåre Alsaker94659272012-11-13 19:10:18 +0100934 struct gles2_output_state *go = get_output_state(output);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400935 struct weston_compositor *compositor = output->compositor;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100936 struct gles2_renderer *gr = get_renderer(compositor);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400937 EGLBoolean ret;
938 static int errored;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300939 int32_t width, height, i;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400940
941 width = output->current->width +
942 output->border.left + output->border.right;
943 height = output->current->height +
944 output->border.top + output->border.bottom;
945
946 glViewport(0, 0, width, height);
947
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100948 if (use_output(output) < 0)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400949 return;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400950
951 /* if debugging, redraw everything outside the damage to clean up
952 * debug lines from the previous draw on this buffer:
953 */
954 if (compositor->fan_debug) {
955 pixman_region32_t undamaged;
956 pixman_region32_init(&undamaged);
957 pixman_region32_subtract(&undamaged, &output->region,
958 output_damage);
959 compositor->fan_debug = 0;
960 repaint_surfaces(output, &undamaged);
961 compositor->fan_debug = 1;
962 pixman_region32_fini(&undamaged);
963 }
964
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300965 for (i = 0; i < 2; i++)
966 pixman_region32_union(&output->buffer_damage[i],
967 &output->buffer_damage[i],
968 output_damage);
969
970 pixman_region32_union(output_damage, output_damage,
971 &output->buffer_damage[output->current_buffer]);
972
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400973 repaint_surfaces(output, output_damage);
974
John Kåre Alsaker44154502012-11-13 19:10:20 +0100975 if (gr->border.texture)
976 draw_border(output);
977
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400978 wl_signal_emit(&output->frame_signal, output);
979
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100980 ret = eglSwapBuffers(gr->egl_display, go->egl_surface);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400981 if (ret == EGL_FALSE && !errored) {
982 errored = 1;
983 weston_log("Failed in eglSwapBuffers.\n");
984 print_egl_error_state();
985 }
986
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300987 output->current_buffer ^= 1;
988
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400989}
Kristian Høgsberg25894fc2012-09-05 22:06:26 -0400990
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100991static int
992gles2_renderer_read_pixels(struct weston_output *output,
993 pixman_format_code_t format, void *pixels,
994 uint32_t x, uint32_t y,
995 uint32_t width, uint32_t height)
996{
997 GLenum gl_format;
998
999 switch (format) {
1000 case PIXMAN_a8r8g8b8:
1001 gl_format = GL_BGRA_EXT;
1002 break;
1003 case PIXMAN_a8b8g8r8:
1004 gl_format = GL_RGBA;
1005 break;
1006 default:
1007 return -1;
1008 }
1009
1010 if (use_output(output) < 0)
1011 return -1;
1012
1013 glPixelStorei(GL_PACK_ALIGNMENT, 1);
1014 glReadPixels(x, y, width, height, gl_format,
1015 GL_UNSIGNED_BYTE, pixels);
1016
1017 return 0;
1018}
1019
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001020static void
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001021gles2_renderer_flush_damage(struct weston_surface *surface)
1022{
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001023 struct gles2_renderer *gr = get_renderer(surface->compositor);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001024 struct gles2_surface_state *gs = get_surface_state(surface);
1025
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001026#ifdef GL_UNPACK_ROW_LENGTH
1027 pixman_box32_t *rectangles;
1028 void *data;
1029 int i, n;
1030#endif
1031
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001032 pixman_region32_union(&surface->texture_damage,
1033 &surface->texture_damage, &surface->damage);
1034
1035 /* Avoid upload, if the texture won't be used this time.
1036 * We still accumulate the damage in texture_damage.
1037 */
1038 if (surface->plane != &surface->compositor->primary_plane)
1039 return;
1040
1041 if (!pixman_region32_not_empty(&surface->texture_damage))
1042 return;
1043
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001044 glBindTexture(GL_TEXTURE_2D, gs->textures[0]);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001045
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001046 if (!gr->has_unpack_subimage) {
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001047 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1048 surface->pitch, surface->buffer->height, 0,
1049 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1050 wl_shm_buffer_get_data(surface->buffer));
1051
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001052 goto done;
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001053 }
1054
1055#ifdef GL_UNPACK_ROW_LENGTH
1056 /* Mesa does not define GL_EXT_unpack_subimage */
1057 glPixelStorei(GL_UNPACK_ROW_LENGTH, surface->pitch);
1058 data = wl_shm_buffer_get_data(surface->buffer);
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001059 rectangles = pixman_region32_rectangles(&surface->texture_damage, &n);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001060 for (i = 0; i < n; i++) {
1061 glPixelStorei(GL_UNPACK_SKIP_PIXELS, rectangles[i].x1);
1062 glPixelStorei(GL_UNPACK_SKIP_ROWS, rectangles[i].y1);
1063 glTexSubImage2D(GL_TEXTURE_2D, 0,
1064 rectangles[i].x1, rectangles[i].y1,
1065 rectangles[i].x2 - rectangles[i].x1,
1066 rectangles[i].y2 - rectangles[i].y1,
1067 GL_BGRA_EXT, GL_UNSIGNED_BYTE, data);
1068 }
1069#endif
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001070
1071done:
1072 pixman_region32_fini(&surface->texture_damage);
1073 pixman_region32_init(&surface->texture_damage);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001074}
1075
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001076static void
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001077ensure_textures(struct gles2_surface_state *gs, int num_textures)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001078{
1079 int i;
1080
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001081 if (num_textures <= gs->num_textures)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001082 return;
1083
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001084 for (i = gs->num_textures; i < num_textures; i++) {
1085 glGenTextures(1, &gs->textures[i]);
1086 glBindTexture(gs->target, gs->textures[i]);
1087 glTexParameteri(gs->target,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001088 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001089 glTexParameteri(gs->target,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001090 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1091 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001092 gs->num_textures = num_textures;
1093 glBindTexture(gs->target, 0);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001094}
1095
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001096static void
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001097gles2_renderer_attach(struct weston_surface *es, struct wl_buffer *buffer)
1098{
1099 struct weston_compositor *ec = es->compositor;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001100 struct gles2_renderer *gr = get_renderer(ec);
John Kåre Alsaker40684142012-11-13 19:10:25 +01001101 struct gles2_surface_state *gs = get_surface_state(es);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001102 EGLint attribs[3], format;
1103 int i, num_planes;
1104
1105 if (!buffer) {
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001106 for (i = 0; i < gs->num_images; i++) {
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001107 gr->destroy_image(gr->egl_display, gs->images[i]);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001108 gs->images[i] = NULL;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001109 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001110 gs->num_images = 0;
1111 glDeleteTextures(gs->num_textures, gs->textures);
1112 gs->num_textures = 0;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001113 return;
1114 }
1115
1116 if (wl_buffer_is_shm(buffer)) {
1117 es->pitch = wl_shm_buffer_get_stride(buffer) / 4;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001118 gs->target = GL_TEXTURE_2D;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001119
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001120 ensure_textures(gs, 1);
1121 glBindTexture(GL_TEXTURE_2D, gs->textures[0]);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001122 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1123 es->pitch, buffer->height, 0,
1124 GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
1125 if (wl_shm_buffer_get_format(buffer) == WL_SHM_FORMAT_XRGB8888)
John Kåre Alsaker40684142012-11-13 19:10:25 +01001126 gs->shader = &gr->texture_shader_rgbx;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001127 else
John Kåre Alsaker40684142012-11-13 19:10:25 +01001128 gs->shader = &gr->texture_shader_rgba;
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001129 } else if (gr->query_buffer(gr->egl_display, buffer,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001130 EGL_TEXTURE_FORMAT, &format)) {
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001131 for (i = 0; i < gs->num_images; i++)
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001132 gr->destroy_image(gr->egl_display, gs->images[i]);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001133 gs->num_images = 0;
1134 gs->target = GL_TEXTURE_2D;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001135 switch (format) {
1136 case EGL_TEXTURE_RGB:
1137 case EGL_TEXTURE_RGBA:
1138 default:
1139 num_planes = 1;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001140 gs->shader = &gr->texture_shader_rgba;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001141 break;
1142 case EGL_TEXTURE_EXTERNAL_WL:
1143 num_planes = 1;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001144 gs->target = GL_TEXTURE_EXTERNAL_OES;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001145 gs->shader = &gr->texture_shader_egl_external;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001146 break;
1147 case EGL_TEXTURE_Y_UV_WL:
1148 num_planes = 2;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001149 gs->shader = &gr->texture_shader_y_uv;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001150 break;
1151 case EGL_TEXTURE_Y_U_V_WL:
1152 num_planes = 3;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001153 gs->shader = &gr->texture_shader_y_u_v;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001154 break;
1155 case EGL_TEXTURE_Y_XUXV_WL:
1156 num_planes = 2;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001157 gs->shader = &gr->texture_shader_y_xuxv;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001158 break;
1159 }
1160
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001161 ensure_textures(gs, num_planes);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001162 for (i = 0; i < num_planes; i++) {
1163 attribs[0] = EGL_WAYLAND_PLANE_WL;
1164 attribs[1] = i;
1165 attribs[2] = EGL_NONE;
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001166 gs->images[i] = gr->create_image(gr->egl_display,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001167 NULL,
1168 EGL_WAYLAND_BUFFER_WL,
1169 buffer, attribs);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001170 if (!gs->images[i]) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001171 weston_log("failed to create img for plane %d\n", i);
1172 continue;
1173 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001174 gs->num_images++;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001175
1176 glActiveTexture(GL_TEXTURE0 + i);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001177 glBindTexture(gs->target, gs->textures[i]);
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001178 gr->image_target_texture_2d(gs->target,
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001179 gs->images[i]);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001180 }
1181
1182 es->pitch = buffer->width;
1183 } else {
1184 weston_log("unhandled buffer type!\n");
1185 }
1186}
1187
Kristian Høgsberg42263852012-09-06 21:59:29 -04001188static void
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001189gles2_renderer_surface_set_color(struct weston_surface *surface,
1190 float red, float green, float blue, float alpha)
1191{
1192 struct gles2_surface_state *gs = get_surface_state(surface);
John Kåre Alsaker40684142012-11-13 19:10:25 +01001193 struct gles2_renderer *gr = get_renderer(surface->compositor);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001194
1195 gs->color[0] = red;
1196 gs->color[1] = green;
1197 gs->color[2] = blue;
1198 gs->color[3] = alpha;
1199
John Kåre Alsaker40684142012-11-13 19:10:25 +01001200 gs->shader = &gr->solid_shader;
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001201}
1202
1203static int
1204gles2_renderer_create_surface(struct weston_surface *surface)
1205{
1206 struct gles2_surface_state *gs;
1207
1208 gs = calloc(1, sizeof *gs);
1209
1210 if (!gs)
1211 return -1;
1212
1213 surface->renderer_state = gs;
1214
1215 return 0;
1216}
1217
1218static void
Kristian Høgsberg42263852012-09-06 21:59:29 -04001219gles2_renderer_destroy_surface(struct weston_surface *surface)
1220{
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001221 struct gles2_surface_state *gs = get_surface_state(surface);
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001222 struct gles2_renderer *gr = get_renderer(surface->compositor);
Kristian Høgsberg42263852012-09-06 21:59:29 -04001223 int i;
1224
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001225 glDeleteTextures(gs->num_textures, gs->textures);
Kristian Høgsberg42263852012-09-06 21:59:29 -04001226
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001227 for (i = 0; i < gs->num_images; i++)
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001228 gr->destroy_image(gr->egl_display, gs->images[i]);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001229
1230 free(gs);
Kristian Høgsberg42263852012-09-06 21:59:29 -04001231}
1232
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001233static const char vertex_shader[] =
1234 "uniform mat4 proj;\n"
1235 "attribute vec2 position;\n"
1236 "attribute vec2 texcoord;\n"
1237 "varying vec2 v_texcoord;\n"
1238 "void main()\n"
1239 "{\n"
1240 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
1241 " v_texcoord = texcoord;\n"
1242 "}\n";
1243
1244/* Declare common fragment shader uniforms */
1245#define FRAGMENT_CONVERT_YUV \
1246 " y *= alpha;\n" \
1247 " u *= alpha;\n" \
1248 " v *= alpha;\n" \
1249 " gl_FragColor.r = y + 1.59602678 * v;\n" \
1250 " gl_FragColor.g = y - 0.39176229 * u - 0.81296764 * v;\n" \
1251 " gl_FragColor.b = y + 2.01723214 * u;\n" \
1252 " gl_FragColor.a = alpha;\n"
1253
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001254static const char fragment_debug[] =
1255 " gl_FragColor = vec4(0.0, 0.3, 0.0, 0.2) + gl_FragColor * 0.8;\n";
1256
1257static const char fragment_brace[] =
1258 "}\n";
1259
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001260static const char texture_fragment_shader_rgba[] =
1261 "precision mediump float;\n"
1262 "varying vec2 v_texcoord;\n"
1263 "uniform sampler2D tex;\n"
1264 "uniform float alpha;\n"
1265 "void main()\n"
1266 "{\n"
1267 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001268 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001269
1270static const char texture_fragment_shader_rgbx[] =
1271 "precision mediump float;\n"
1272 "varying vec2 v_texcoord;\n"
1273 "uniform sampler2D tex;\n"
1274 "uniform float alpha;\n"
1275 "void main()\n"
1276 "{\n"
1277 " gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb\n;"
1278 " gl_FragColor.a = alpha;\n"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001279 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001280
1281static const char texture_fragment_shader_egl_external[] =
1282 "#extension GL_OES_EGL_image_external : require\n"
1283 "precision mediump float;\n"
1284 "varying vec2 v_texcoord;\n"
1285 "uniform samplerExternalOES tex;\n"
1286 "uniform float alpha;\n"
1287 "void main()\n"
1288 "{\n"
1289 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001290 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001291
1292static const char texture_fragment_shader_y_uv[] =
1293 "precision mediump float;\n"
1294 "uniform sampler2D tex;\n"
1295 "uniform sampler2D tex1;\n"
1296 "varying vec2 v_texcoord;\n"
1297 "uniform float alpha;\n"
1298 "void main() {\n"
1299 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1300 " float u = texture2D(tex1, v_texcoord).r - 0.5;\n"
1301 " float v = texture2D(tex1, v_texcoord).g - 0.5;\n"
1302 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001303 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001304
1305static const char texture_fragment_shader_y_u_v[] =
1306 "precision mediump float;\n"
1307 "uniform sampler2D tex;\n"
1308 "uniform sampler2D tex1;\n"
1309 "uniform sampler2D tex2;\n"
1310 "varying vec2 v_texcoord;\n"
1311 "uniform float alpha;\n"
1312 "void main() {\n"
1313 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1314 " float u = texture2D(tex1, v_texcoord).x - 0.5;\n"
1315 " float v = texture2D(tex2, v_texcoord).x - 0.5;\n"
1316 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001317 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001318
1319static const char texture_fragment_shader_y_xuxv[] =
1320 "precision mediump float;\n"
1321 "uniform sampler2D tex;\n"
1322 "uniform sampler2D tex1;\n"
1323 "varying vec2 v_texcoord;\n"
1324 "uniform float alpha;\n"
1325 "void main() {\n"
1326 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1327 " float u = texture2D(tex1, v_texcoord).g - 0.5;\n"
1328 " float v = texture2D(tex1, v_texcoord).a - 0.5;\n"
1329 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001330 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001331
1332static const char solid_fragment_shader[] =
1333 "precision mediump float;\n"
1334 "uniform vec4 color;\n"
1335 "uniform float alpha;\n"
1336 "void main()\n"
1337 "{\n"
1338 " gl_FragColor = alpha * color\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001339 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001340
1341static int
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001342compile_shader(GLenum type, int count, const char **sources)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001343{
1344 GLuint s;
1345 char msg[512];
1346 GLint status;
1347
1348 s = glCreateShader(type);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001349 glShaderSource(s, count, sources, NULL);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001350 glCompileShader(s);
1351 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
1352 if (!status) {
1353 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1354 weston_log("shader info: %s\n", msg);
1355 return GL_NONE;
1356 }
1357
1358 return s;
1359}
1360
1361static int
John Kåre Alsaker40684142012-11-13 19:10:25 +01001362shader_init(struct gles2_shader *shader, struct weston_compositor *ec,
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001363 const char *vertex_source, const char *fragment_source)
1364{
1365 char msg[512];
1366 GLint status;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001367 int count;
1368 const char *sources[3];
1369 struct gles2_renderer *renderer =
1370 (struct gles2_renderer *) ec->renderer;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001371
1372 shader->vertex_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001373 compile_shader(GL_VERTEX_SHADER, 1, &vertex_source);
1374
1375 if (renderer->fragment_shader_debug) {
1376 sources[0] = fragment_source;
1377 sources[1] = fragment_debug;
1378 sources[2] = fragment_brace;
1379 count = 3;
1380 } else {
1381 sources[0] = fragment_source;
1382 sources[1] = fragment_brace;
1383 count = 2;
1384 }
1385
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001386 shader->fragment_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001387 compile_shader(GL_FRAGMENT_SHADER, count, sources);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001388
1389 shader->program = glCreateProgram();
1390 glAttachShader(shader->program, shader->vertex_shader);
1391 glAttachShader(shader->program, shader->fragment_shader);
1392 glBindAttribLocation(shader->program, 0, "position");
1393 glBindAttribLocation(shader->program, 1, "texcoord");
1394
1395 glLinkProgram(shader->program);
1396 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1397 if (!status) {
1398 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1399 weston_log("link info: %s\n", msg);
1400 return -1;
1401 }
1402
1403 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1404 shader->tex_uniforms[0] = glGetUniformLocation(shader->program, "tex");
1405 shader->tex_uniforms[1] = glGetUniformLocation(shader->program, "tex1");
1406 shader->tex_uniforms[2] = glGetUniformLocation(shader->program, "tex2");
1407 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
1408 shader->color_uniform = glGetUniformLocation(shader->program, "color");
1409
1410 return 0;
1411}
1412
1413static void
John Kåre Alsaker40684142012-11-13 19:10:25 +01001414shader_release(struct gles2_shader *shader)
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001415{
1416 glDeleteShader(shader->vertex_shader);
1417 glDeleteShader(shader->fragment_shader);
1418 glDeleteProgram(shader->program);
1419
1420 shader->vertex_shader = 0;
1421 shader->fragment_shader = 0;
1422 shader->program = 0;
1423}
1424
1425static void
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001426log_extensions(const char *name, const char *extensions)
1427{
1428 const char *p, *end;
1429 int l;
1430 int len;
1431
1432 l = weston_log("%s:", name);
1433 p = extensions;
1434 while (*p) {
1435 end = strchrnul(p, ' ');
1436 len = end - p;
1437 if (l + len > 78)
1438 l = weston_log_continue("\n" STAMP_SPACE "%.*s",
1439 len, p);
1440 else
1441 l += weston_log_continue(" %.*s", len, p);
1442 for (p = end; isspace(*p); p++)
1443 ;
1444 }
1445 weston_log_continue("\n");
1446}
1447
1448static void
1449log_egl_gl_info(EGLDisplay egldpy)
1450{
1451 const char *str;
1452
1453 str = eglQueryString(egldpy, EGL_VERSION);
1454 weston_log("EGL version: %s\n", str ? str : "(null)");
1455
1456 str = eglQueryString(egldpy, EGL_VENDOR);
1457 weston_log("EGL vendor: %s\n", str ? str : "(null)");
1458
1459 str = eglQueryString(egldpy, EGL_CLIENT_APIS);
1460 weston_log("EGL client APIs: %s\n", str ? str : "(null)");
1461
1462 str = eglQueryString(egldpy, EGL_EXTENSIONS);
1463 log_extensions("EGL extensions", str ? str : "(null)");
1464
1465 str = (char *)glGetString(GL_VERSION);
1466 weston_log("GL version: %s\n", str ? str : "(null)");
1467
1468 str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
1469 weston_log("GLSL version: %s\n", str ? str : "(null)");
1470
1471 str = (char *)glGetString(GL_VENDOR);
1472 weston_log("GL vendor: %s\n", str ? str : "(null)");
1473
1474 str = (char *)glGetString(GL_RENDERER);
1475 weston_log("GL renderer: %s\n", str ? str : "(null)");
1476
1477 str = (char *)glGetString(GL_EXTENSIONS);
1478 log_extensions("GL extensions", str ? str : "(null)");
1479}
1480
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001481static void
1482log_egl_config_info(EGLDisplay egldpy, EGLConfig eglconfig)
1483{
1484 EGLint r, g, b, a;
1485
1486 weston_log("Chosen EGL config details:\n");
1487
1488 weston_log_continue(STAMP_SPACE "RGBA bits");
1489 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_RED_SIZE, &r) &&
1490 eglGetConfigAttrib(egldpy, eglconfig, EGL_GREEN_SIZE, &g) &&
1491 eglGetConfigAttrib(egldpy, eglconfig, EGL_BLUE_SIZE, &b) &&
1492 eglGetConfigAttrib(egldpy, eglconfig, EGL_ALPHA_SIZE, &a))
1493 weston_log_continue(": %d %d %d %d\n", r, g, b, a);
1494 else
1495 weston_log_continue(" unknown\n");
1496
1497 weston_log_continue(STAMP_SPACE "swap interval range");
1498 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_MIN_SWAP_INTERVAL, &a) &&
1499 eglGetConfigAttrib(egldpy, eglconfig, EGL_MAX_SWAP_INTERVAL, &b))
1500 weston_log_continue(": %d - %d\n", a, b);
1501 else
1502 weston_log_continue(" unknown\n");
1503}
1504
John Kåre Alsaker44154502012-11-13 19:10:20 +01001505static void
1506output_apply_border(struct weston_output *output, struct gles2_renderer *gr)
1507{
1508 output->border.top = gr->border.top;
1509 output->border.bottom = gr->border.bottom;
1510 output->border.left = gr->border.left;
1511 output->border.right = gr->border.right;
1512}
1513
1514WL_EXPORT void
1515gles2_renderer_set_border(struct weston_compositor *ec, int32_t width, int32_t height, void *data,
1516 int32_t *edges)
1517{
1518 struct gles2_renderer *gr = get_renderer(ec);
1519 struct weston_output *output;
1520
1521 gr->border.left = edges[0];
1522 gr->border.right = edges[1];
1523 gr->border.top = edges[2];
1524 gr->border.bottom = edges[3];
1525
1526 gr->border.width = width;
1527 gr->border.height = height;
1528
1529 glGenTextures(1, &gr->border.texture);
1530 glBindTexture(GL_TEXTURE_2D, gr->border.texture);
1531 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1532 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1533 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1534 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1535
1536 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1537 width,
1538 height,
1539 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1540 data);
1541
1542 wl_list_for_each(output, &ec->output_list, link)
1543 output_apply_border(output, gr);
1544}
1545
John Kåre Alsaker94659272012-11-13 19:10:18 +01001546static int
1547gles2_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface);
1548
1549WL_EXPORT int
1550gles2_renderer_output_create(struct weston_output *output,
1551 EGLNativeWindowType window)
1552{
1553 struct weston_compositor *ec = output->compositor;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001554 struct gles2_renderer *gr = get_renderer(ec);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001555 struct gles2_output_state *go = calloc(1, sizeof *go);
1556
1557 if (!go)
1558 return -1;
1559
1560 go->egl_surface =
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001561 eglCreateWindowSurface(gr->egl_display,
1562 gr->egl_config,
John Kåre Alsaker94659272012-11-13 19:10:18 +01001563 window, NULL);
1564
1565 if (go->egl_surface == EGL_NO_SURFACE) {
1566 weston_log("failed to create egl surface\n");
1567 free(go);
1568 return -1;
1569 }
1570
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001571 if (gr->egl_context == NULL)
John Kåre Alsaker94659272012-11-13 19:10:18 +01001572 if (gles2_renderer_setup(ec, go->egl_surface) < 0) {
1573 free(go);
1574 return -1;
1575 }
1576
1577 output->renderer_state = go;
1578
John Kåre Alsaker44154502012-11-13 19:10:20 +01001579 output_apply_border(output, gr);
1580
John Kåre Alsaker94659272012-11-13 19:10:18 +01001581 return 0;
1582}
1583
1584WL_EXPORT void
1585gles2_renderer_output_destroy(struct weston_output *output)
1586{
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001587 struct gles2_renderer *gr = get_renderer(output->compositor);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001588 struct gles2_output_state *go = get_output_state(output);
1589
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001590 eglDestroySurface(gr->egl_display, go->egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001591
1592 free(go);
1593}
1594
1595WL_EXPORT EGLSurface
1596gles2_renderer_output_surface(struct weston_output *output)
1597{
1598 return get_output_state(output)->egl_surface;
1599}
1600
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001601WL_EXPORT void
1602gles2_renderer_destroy(struct weston_compositor *ec)
1603{
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001604 struct gles2_renderer *gr = get_renderer(ec);
1605
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001606 if (gr->has_bind_display)
1607 gr->unbind_display(gr->egl_display, ec->wl_display);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001608
1609 /* Work around crash in egl_dri2.c's dri2_make_current() - when does this apply? */
1610 eglMakeCurrent(gr->egl_display,
1611 EGL_NO_SURFACE, EGL_NO_SURFACE,
1612 EGL_NO_CONTEXT);
1613
1614 eglTerminate(gr->egl_display);
1615 eglReleaseThread();
1616}
1617
1618static int
1619egl_choose_config(struct gles2_renderer *gr, const EGLint *attribs,
1620 const EGLint *visual_id)
1621{
1622 EGLint count = 0;
1623 EGLint matched = 0;
1624 EGLConfig *configs;
1625 int i;
1626
1627 if (!eglGetConfigs(gr->egl_display, NULL, 0, &count) || count < 1)
1628 return -1;
1629
1630 configs = calloc(count, sizeof *configs);
1631 if (!configs)
1632 return -1;
1633
1634 if (!eglChooseConfig(gr->egl_display, attribs, configs,
1635 count, &matched))
1636 goto out;
1637
1638 for (i = 0; i < matched; ++i) {
1639 EGLint id;
1640
1641 if (visual_id) {
1642 if (!eglGetConfigAttrib(gr->egl_display,
1643 configs[i], EGL_NATIVE_VISUAL_ID,
1644 &id))
1645 continue;
1646
1647 if (id != *visual_id)
1648 continue;
1649 }
1650
1651 gr->egl_config = configs[i];
1652
1653 free(configs);
1654 return 0;
1655 }
1656
1657out:
1658 free(configs);
1659 return -1;
1660}
1661
1662WL_EXPORT const EGLint gles2_renderer_opaque_attribs[] = {
1663 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1664 EGL_RED_SIZE, 1,
1665 EGL_GREEN_SIZE, 1,
1666 EGL_BLUE_SIZE, 1,
1667 EGL_ALPHA_SIZE, 0,
1668 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1669 EGL_NONE
1670};
1671
1672WL_EXPORT const EGLint gles2_renderer_alpha_attribs[] = {
1673 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1674 EGL_RED_SIZE, 1,
1675 EGL_GREEN_SIZE, 1,
1676 EGL_BLUE_SIZE, 1,
1677 EGL_ALPHA_SIZE, 1,
1678 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1679 EGL_NONE
1680};
1681
1682WL_EXPORT int
1683gles2_renderer_create(struct weston_compositor *ec, EGLNativeDisplayType display,
1684 const EGLint *attribs, const EGLint *visual_id)
1685{
1686 struct gles2_renderer *gr;
1687 EGLint major, minor;
1688
1689 gr = calloc(1, sizeof *gr);
1690
1691 if (gr == NULL)
1692 return -1;
1693
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001694 gr->base.read_pixels = gles2_renderer_read_pixels;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001695 gr->base.repaint_output = gles2_renderer_repaint_output;
1696 gr->base.flush_damage = gles2_renderer_flush_damage;
1697 gr->base.attach = gles2_renderer_attach;
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001698 gr->base.create_surface = gles2_renderer_create_surface;
1699 gr->base.surface_set_color = gles2_renderer_surface_set_color;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001700 gr->base.destroy_surface = gles2_renderer_destroy_surface;
1701
1702 gr->egl_display = eglGetDisplay(display);
1703 if (gr->egl_display == EGL_NO_DISPLAY) {
1704 weston_log("failed to create display\n");
1705 goto err_egl;
1706 }
1707
1708 if (!eglInitialize(gr->egl_display, &major, &minor)) {
1709 weston_log("failed to initialize display\n");
1710 goto err_egl;
1711 }
1712
1713 if (egl_choose_config(gr, attribs, visual_id) < 0) {
1714 weston_log("failed to choose EGL config\n");
1715 goto err_egl;
1716 }
1717
1718 ec->renderer = &gr->base;
1719
1720 return 0;
1721
1722err_egl:
1723 print_egl_error_state();
1724 free(gr);
1725 return -1;
1726}
1727
1728WL_EXPORT EGLDisplay
1729gles2_renderer_display(struct weston_compositor *ec)
1730{
1731 return get_renderer(ec)->egl_display;
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001732}
1733
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001734static int
1735compile_shaders(struct weston_compositor *ec)
1736{
John Kåre Alsaker40684142012-11-13 19:10:25 +01001737 struct gles2_renderer *gr = get_renderer(ec);
1738
1739 if (shader_init(&gr->texture_shader_rgba, ec,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001740 vertex_shader, texture_fragment_shader_rgba) < 0)
1741 return -1;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001742 if (shader_init(&gr->texture_shader_rgbx, ec,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001743 vertex_shader, texture_fragment_shader_rgbx) < 0)
1744 return -1;
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001745 if (gr->has_egl_image_external &&
John Kåre Alsaker40684142012-11-13 19:10:25 +01001746 shader_init(&gr->texture_shader_egl_external, ec,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001747 vertex_shader, texture_fragment_shader_egl_external) < 0)
1748 return -1;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001749 if (shader_init(&gr->texture_shader_y_uv, ec,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001750 vertex_shader, texture_fragment_shader_y_uv) < 0)
1751 return -1;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001752 if (shader_init(&gr->texture_shader_y_u_v, ec,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001753 vertex_shader, texture_fragment_shader_y_u_v) < 0)
1754 return -1;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001755 if (shader_init(&gr->texture_shader_y_xuxv, ec,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001756 vertex_shader, texture_fragment_shader_y_xuxv) < 0)
1757 return -1;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001758 if (shader_init(&gr->solid_shader, ec,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001759 vertex_shader, solid_fragment_shader) < 0)
1760 return -1;
1761
1762 return 0;
1763}
1764
1765static void
1766fragment_debug_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
1767 void *data)
1768{
1769 struct weston_compositor *ec = data;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001770 struct gles2_renderer *gr = get_renderer(ec);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001771 struct weston_output *output;
1772
John Kåre Alsaker40684142012-11-13 19:10:25 +01001773 gr->fragment_shader_debug ^= 1;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001774
John Kåre Alsaker40684142012-11-13 19:10:25 +01001775 shader_release(&gr->texture_shader_rgba);
1776 shader_release(&gr->texture_shader_rgbx);
1777 shader_release(&gr->texture_shader_egl_external);
1778 shader_release(&gr->texture_shader_y_uv);
1779 shader_release(&gr->texture_shader_y_u_v);
1780 shader_release(&gr->texture_shader_y_xuxv);
1781 shader_release(&gr->solid_shader);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001782
1783 compile_shaders(ec);
1784
1785 wl_list_for_each(output, &ec->output_list, link)
1786 weston_output_damage(output);
1787}
1788
John Kåre Alsaker94659272012-11-13 19:10:18 +01001789static int
1790gles2_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001791{
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001792 struct gles2_renderer *gr = get_renderer(ec);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001793 const char *extensions;
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001794 EGLBoolean ret;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001795
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001796 static const EGLint context_attribs[] = {
1797 EGL_CONTEXT_CLIENT_VERSION, 2,
1798 EGL_NONE
1799 };
1800
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001801 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
1802 weston_log("failed to bind EGL_OPENGL_ES_API\n");
1803 print_egl_error_state();
1804 return -1;
1805 }
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001806
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001807 log_egl_config_info(gr->egl_display, gr->egl_config);
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001808
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001809 gr->egl_context = eglCreateContext(gr->egl_display, gr->egl_config,
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001810 EGL_NO_CONTEXT, context_attribs);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001811 if (gr->egl_context == NULL) {
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001812 weston_log("failed to create context\n");
1813 print_egl_error_state();
1814 return -1;
1815 }
1816
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001817 ret = eglMakeCurrent(gr->egl_display, egl_surface,
1818 egl_surface, gr->egl_context);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001819 if (ret == EGL_FALSE) {
1820 weston_log("Failed to make EGL context current.\n");
1821 print_egl_error_state();
1822 return -1;
1823 }
1824
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001825 log_egl_gl_info(gr->egl_display);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001826
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001827 gr->image_target_texture_2d =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001828 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001829 gr->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1830 gr->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1831 gr->bind_display =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001832 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001833 gr->unbind_display =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001834 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001835 gr->query_buffer =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001836 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
1837
1838 extensions = (const char *) glGetString(GL_EXTENSIONS);
1839 if (!extensions) {
1840 weston_log("Retrieving GL extension string failed.\n");
1841 return -1;
1842 }
1843
1844 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
1845 weston_log("GL_EXT_texture_format_BGRA8888 not available\n");
1846 return -1;
1847 }
1848
1849 if (strstr(extensions, "GL_EXT_read_format_bgra"))
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01001850 ec->read_format = PIXMAN_a8r8g8b8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001851 else
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01001852 ec->read_format = PIXMAN_a8b8g8r8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001853
1854 if (strstr(extensions, "GL_EXT_unpack_subimage"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001855 gr->has_unpack_subimage = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001856
1857 if (strstr(extensions, "GL_OES_EGL_image_external"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001858 gr->has_egl_image_external = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001859
1860 extensions =
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001861 (const char *) eglQueryString(gr->egl_display, EGL_EXTENSIONS);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001862 if (!extensions) {
1863 weston_log("Retrieving EGL extension string failed.\n");
1864 return -1;
1865 }
1866
1867 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001868 gr->has_bind_display = 1;
1869 if (gr->has_bind_display) {
1870 ret = gr->bind_display(gr->egl_display, ec->wl_display);
Pekka Paalanen035a0322012-10-24 09:43:06 +03001871 if (!ret)
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001872 gr->has_bind_display = 0;
Pekka Paalanen035a0322012-10-24 09:43:06 +03001873 }
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001874
1875 glActiveTexture(GL_TEXTURE0);
1876
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001877 if (compile_shaders(ec))
1878 return -1;
1879
1880 weston_compositor_add_debug_binding(ec, KEY_S,
1881 fragment_debug_binding, ec);
1882
Pekka Paalanen035a0322012-10-24 09:43:06 +03001883 weston_log("GL ES 2 renderer features:\n");
1884 weston_log_continue(STAMP_SPACE "read-back format: %s\n",
1885 ec->read_format == GL_BGRA_EXT ? "BGRA" : "RGBA");
1886 weston_log_continue(STAMP_SPACE "wl_shm sub-image to texture: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001887 gr->has_unpack_subimage ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03001888 weston_log_continue(STAMP_SPACE "EGL Wayland extension: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001889 gr->has_bind_display ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03001890
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001891
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001892 return 0;
1893}