blob: 89e9a4d813af9608ec5afbffaf3e5530e2e7dbd4 [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 Alsaker779b52a2012-11-13 19:10:29 +010040struct gl_shader {
John Kåre Alsaker40684142012-11-13 19:10:25 +010041 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 Alsaker779b52a2012-11-13 19:10:29 +010049struct gl_output_state {
John Kåre Alsaker94659272012-11-13 19:10:18 +010050 EGLSurface egl_surface;
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +020051 int current_buffer;
52 pixman_region32_t buffer_damage[2];
John Kåre Alsaker94659272012-11-13 19:10:18 +010053};
54
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010055struct gl_surface_state {
John Kåre Alsaker878f4492012-11-13 19:10:23 +010056 GLfloat color[4];
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010057 struct gl_shader *shader;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +010058
59 GLuint textures[3];
60 int num_textures;
61
62 EGLImageKHR images[3];
63 GLenum target;
64 int num_images;
Pekka Paalanenfb003d32012-12-04 15:58:13 +020065
66 struct weston_buffer_reference buffer_ref;
Pekka Paalanen68033ac2012-12-04 15:58:15 +020067 int pitch; /* in pixels */
John Kåre Alsaker878f4492012-11-13 19:10:23 +010068};
69
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010070struct gl_renderer {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +010071 struct weston_renderer base;
72 int fragment_shader_debug;
73
74 EGLDisplay egl_display;
75 EGLContext egl_context;
76 EGLConfig egl_config;
John Kåre Alsaker44154502012-11-13 19:10:20 +010077
78 struct {
79 int32_t top, bottom, left, right;
80 GLuint texture;
81 int32_t width, height;
82 } border;
John Kåre Alsaker40684142012-11-13 19:10:25 +010083
John Kåre Alsaker320711d2012-11-13 19:10:27 +010084 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
85 PFNEGLCREATEIMAGEKHRPROC create_image;
86 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
87
88 int has_unpack_subimage;
89
90 PFNEGLBINDWAYLANDDISPLAYWL bind_display;
91 PFNEGLUNBINDWAYLANDDISPLAYWL unbind_display;
92 PFNEGLQUERYWAYLANDBUFFERWL query_buffer;
93 int has_bind_display;
94
95 int has_egl_image_external;
96
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010097 struct gl_shader texture_shader_rgba;
98 struct gl_shader texture_shader_rgbx;
99 struct gl_shader texture_shader_egl_external;
100 struct gl_shader texture_shader_y_uv;
101 struct gl_shader texture_shader_y_u_v;
102 struct gl_shader texture_shader_y_xuxv;
103 struct gl_shader invert_color_shader;
104 struct gl_shader solid_shader;
105 struct gl_shader *current_shader;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100106};
John Kåre Alsaker94659272012-11-13 19:10:18 +0100107
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100108static inline struct gl_output_state *
John Kåre Alsaker94659272012-11-13 19:10:18 +0100109get_output_state(struct weston_output *output)
110{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100111 return (struct gl_output_state *)output->renderer_state;
John Kåre Alsaker94659272012-11-13 19:10:18 +0100112}
113
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100114static inline struct gl_surface_state *
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100115get_surface_state(struct weston_surface *surface)
116{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100117 return (struct gl_surface_state *)surface->renderer_state;
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100118}
119
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100120static inline struct gl_renderer *
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100121get_renderer(struct weston_compositor *ec)
122{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100123 return (struct gl_renderer *)ec->renderer;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100124}
125
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400126static const char *
127egl_error_string(EGLint code)
128{
129#define MYERRCODE(x) case x: return #x;
130 switch (code) {
131 MYERRCODE(EGL_SUCCESS)
132 MYERRCODE(EGL_NOT_INITIALIZED)
133 MYERRCODE(EGL_BAD_ACCESS)
134 MYERRCODE(EGL_BAD_ALLOC)
135 MYERRCODE(EGL_BAD_ATTRIBUTE)
136 MYERRCODE(EGL_BAD_CONTEXT)
137 MYERRCODE(EGL_BAD_CONFIG)
138 MYERRCODE(EGL_BAD_CURRENT_SURFACE)
139 MYERRCODE(EGL_BAD_DISPLAY)
140 MYERRCODE(EGL_BAD_SURFACE)
141 MYERRCODE(EGL_BAD_MATCH)
142 MYERRCODE(EGL_BAD_PARAMETER)
143 MYERRCODE(EGL_BAD_NATIVE_PIXMAP)
144 MYERRCODE(EGL_BAD_NATIVE_WINDOW)
145 MYERRCODE(EGL_CONTEXT_LOST)
146 default:
147 return "unknown";
148 }
149#undef MYERRCODE
150}
151
Pekka Paalanen326529f2012-11-27 12:25:25 +0200152WL_EXPORT void
153gl_renderer_print_egl_error_state(void)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400154{
155 EGLint code;
156
157 code = eglGetError();
158 weston_log("EGL error state: %s (0x%04lx)\n",
159 egl_error_string(code), (long)code);
160}
161
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300162struct polygon8 {
163 GLfloat x[8];
164 GLfloat y[8];
165 int n;
166};
167
168struct clip_context {
169 struct {
170 GLfloat x;
171 GLfloat y;
172 } prev;
173
174 struct {
175 GLfloat x1, y1;
176 GLfloat x2, y2;
177 } clip;
178
179 struct {
180 GLfloat *x;
181 GLfloat *y;
182 } vertices;
183};
184
185static GLfloat
186float_difference(GLfloat a, GLfloat b)
187{
188 /* http://www.altdevblogaday.com/2012/02/22/comparing-floating-point-numbers-2012-edition/ */
189 static const GLfloat max_diff = 4.0f * FLT_MIN;
190 static const GLfloat max_rel_diff = 4.0e-5;
191 GLfloat diff = a - b;
192 GLfloat adiff = fabsf(diff);
193
194 if (adiff <= max_diff)
195 return 0.0f;
196
197 a = fabsf(a);
198 b = fabsf(b);
199 if (adiff <= (a > b ? a : b) * max_rel_diff)
200 return 0.0f;
201
202 return diff;
203}
204
205/* A line segment (p1x, p1y)-(p2x, p2y) intersects the line x = x_arg.
206 * Compute the y coordinate of the intersection.
207 */
208static GLfloat
209clip_intersect_y(GLfloat p1x, GLfloat p1y, GLfloat p2x, GLfloat p2y,
210 GLfloat x_arg)
211{
212 GLfloat a;
213 GLfloat diff = float_difference(p1x, p2x);
214
215 /* Practically vertical line segment, yet the end points have already
216 * been determined to be on different sides of the line. Therefore
217 * the line segment is part of the line and intersects everywhere.
218 * Return the end point, so we use the whole line segment.
219 */
220 if (diff == 0.0f)
221 return p2y;
222
223 a = (x_arg - p2x) / diff;
224 return p2y + (p1y - p2y) * a;
225}
226
227/* A line segment (p1x, p1y)-(p2x, p2y) intersects the line y = y_arg.
228 * Compute the x coordinate of the intersection.
229 */
230static GLfloat
231clip_intersect_x(GLfloat p1x, GLfloat p1y, GLfloat p2x, GLfloat p2y,
232 GLfloat y_arg)
233{
234 GLfloat a;
235 GLfloat diff = float_difference(p1y, p2y);
236
237 /* Practically horizontal line segment, yet the end points have already
238 * been determined to be on different sides of the line. Therefore
239 * the line segment is part of the line and intersects everywhere.
240 * Return the end point, so we use the whole line segment.
241 */
242 if (diff == 0.0f)
243 return p2x;
244
245 a = (y_arg - p2y) / diff;
246 return p2x + (p1x - p2x) * a;
247}
248
249enum path_transition {
250 PATH_TRANSITION_OUT_TO_OUT = 0,
251 PATH_TRANSITION_OUT_TO_IN = 1,
252 PATH_TRANSITION_IN_TO_OUT = 2,
253 PATH_TRANSITION_IN_TO_IN = 3,
254};
255
256static void
257clip_append_vertex(struct clip_context *ctx, GLfloat x, GLfloat y)
258{
259 *ctx->vertices.x++ = x;
260 *ctx->vertices.y++ = y;
261}
262
263static enum path_transition
264path_transition_left_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
265{
266 return ((ctx->prev.x >= ctx->clip.x1) << 1) | (x >= ctx->clip.x1);
267}
268
269static enum path_transition
270path_transition_right_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
271{
272 return ((ctx->prev.x < ctx->clip.x2) << 1) | (x < ctx->clip.x2);
273}
274
275static enum path_transition
276path_transition_top_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
277{
278 return ((ctx->prev.y >= ctx->clip.y1) << 1) | (y >= ctx->clip.y1);
279}
280
281static enum path_transition
282path_transition_bottom_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
283{
284 return ((ctx->prev.y < ctx->clip.y2) << 1) | (y < ctx->clip.y2);
285}
286
287static void
288clip_polygon_leftright(struct clip_context *ctx,
289 enum path_transition transition,
290 GLfloat x, GLfloat y, GLfloat clip_x)
291{
292 GLfloat yi;
293
294 switch (transition) {
295 case PATH_TRANSITION_IN_TO_IN:
296 clip_append_vertex(ctx, x, y);
297 break;
298 case PATH_TRANSITION_IN_TO_OUT:
299 yi = clip_intersect_y(ctx->prev.x, ctx->prev.y, x, y, clip_x);
300 clip_append_vertex(ctx, clip_x, yi);
301 break;
302 case PATH_TRANSITION_OUT_TO_IN:
303 yi = clip_intersect_y(ctx->prev.x, ctx->prev.y, x, y, clip_x);
304 clip_append_vertex(ctx, clip_x, yi);
305 clip_append_vertex(ctx, x, y);
306 break;
307 case PATH_TRANSITION_OUT_TO_OUT:
308 /* nothing */
309 break;
310 default:
311 assert(0 && "bad enum path_transition");
312 }
313
314 ctx->prev.x = x;
315 ctx->prev.y = y;
316}
317
318static void
319clip_polygon_topbottom(struct clip_context *ctx,
320 enum path_transition transition,
321 GLfloat x, GLfloat y, GLfloat clip_y)
322{
323 GLfloat xi;
324
325 switch (transition) {
326 case PATH_TRANSITION_IN_TO_IN:
327 clip_append_vertex(ctx, x, y);
328 break;
329 case PATH_TRANSITION_IN_TO_OUT:
330 xi = clip_intersect_x(ctx->prev.x, ctx->prev.y, x, y, clip_y);
331 clip_append_vertex(ctx, xi, clip_y);
332 break;
333 case PATH_TRANSITION_OUT_TO_IN:
334 xi = clip_intersect_x(ctx->prev.x, ctx->prev.y, x, y, clip_y);
335 clip_append_vertex(ctx, xi, clip_y);
336 clip_append_vertex(ctx, x, y);
337 break;
338 case PATH_TRANSITION_OUT_TO_OUT:
339 /* nothing */
340 break;
341 default:
342 assert(0 && "bad enum path_transition");
343 }
344
345 ctx->prev.x = x;
346 ctx->prev.y = y;
347}
348
349static void
350clip_context_prepare(struct clip_context *ctx, const struct polygon8 *src,
351 GLfloat *dst_x, GLfloat *dst_y)
352{
353 ctx->prev.x = src->x[src->n - 1];
354 ctx->prev.y = src->y[src->n - 1];
355 ctx->vertices.x = dst_x;
356 ctx->vertices.y = dst_y;
357}
358
359static int
360clip_polygon_left(struct clip_context *ctx, const struct polygon8 *src,
361 GLfloat *dst_x, GLfloat *dst_y)
362{
363 enum path_transition trans;
364 int i;
365
366 clip_context_prepare(ctx, src, dst_x, dst_y);
367 for (i = 0; i < src->n; i++) {
368 trans = path_transition_left_edge(ctx, src->x[i], src->y[i]);
369 clip_polygon_leftright(ctx, trans, src->x[i], src->y[i],
370 ctx->clip.x1);
371 }
372 return ctx->vertices.x - dst_x;
373}
374
375static int
376clip_polygon_right(struct clip_context *ctx, const struct polygon8 *src,
377 GLfloat *dst_x, GLfloat *dst_y)
378{
379 enum path_transition trans;
380 int i;
381
382 clip_context_prepare(ctx, src, dst_x, dst_y);
383 for (i = 0; i < src->n; i++) {
384 trans = path_transition_right_edge(ctx, src->x[i], src->y[i]);
385 clip_polygon_leftright(ctx, trans, src->x[i], src->y[i],
386 ctx->clip.x2);
387 }
388 return ctx->vertices.x - dst_x;
389}
390
391static int
392clip_polygon_top(struct clip_context *ctx, const struct polygon8 *src,
393 GLfloat *dst_x, GLfloat *dst_y)
394{
395 enum path_transition trans;
396 int i;
397
398 clip_context_prepare(ctx, src, dst_x, dst_y);
399 for (i = 0; i < src->n; i++) {
400 trans = path_transition_top_edge(ctx, src->x[i], src->y[i]);
401 clip_polygon_topbottom(ctx, trans, src->x[i], src->y[i],
402 ctx->clip.y1);
403 }
404 return ctx->vertices.x - dst_x;
405}
406
407static int
408clip_polygon_bottom(struct clip_context *ctx, const struct polygon8 *src,
409 GLfloat *dst_x, GLfloat *dst_y)
410{
411 enum path_transition trans;
412 int i;
413
414 clip_context_prepare(ctx, src, dst_x, dst_y);
415 for (i = 0; i < src->n; i++) {
416 trans = path_transition_bottom_edge(ctx, src->x[i], src->y[i]);
417 clip_polygon_topbottom(ctx, trans, src->x[i], src->y[i],
418 ctx->clip.y2);
419 }
420 return ctx->vertices.x - dst_x;
421}
422
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400423#define max(a, b) (((a) > (b)) ? (a) : (b))
424#define min(a, b) (((a) > (b)) ? (b) : (a))
425#define clip(x, a, b) min(max(x, a), b)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400426
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300427/*
428 * Compute the boundary vertices of the intersection of the global coordinate
429 * aligned rectangle 'rect', and an arbitrary quadrilateral produced from
430 * 'surf_rect' when transformed from surface coordinates into global coordinates.
431 * The vertices are written to 'ex' and 'ey', and the return value is the
432 * number of vertices. Vertices are produced in clockwise winding order.
433 * Guarantees to produce either zero vertices, or 3-8 vertices with non-zero
434 * polygon area.
435 */
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400436static int
437calculate_edges(struct weston_surface *es, pixman_box32_t *rect,
438 pixman_box32_t *surf_rect, GLfloat *ex, GLfloat *ey)
439{
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300440 struct polygon8 polygon;
441 struct clip_context ctx;
442 int i, n;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400443 GLfloat min_x, max_x, min_y, max_y;
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300444 struct polygon8 surf = {
445 { surf_rect->x1, surf_rect->x2, surf_rect->x2, surf_rect->x1 },
446 { surf_rect->y1, surf_rect->y1, surf_rect->y2, surf_rect->y2 },
447 4
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400448 };
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400449
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300450 ctx.clip.x1 = rect->x1;
451 ctx.clip.y1 = rect->y1;
452 ctx.clip.x2 = rect->x2;
453 ctx.clip.y2 = rect->y2;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400454
455 /* transform surface to screen space: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300456 for (i = 0; i < surf.n; i++)
457 weston_surface_to_global_float(es, surf.x[i], surf.y[i],
458 &surf.x[i], &surf.y[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400459
460 /* find bounding box: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300461 min_x = max_x = surf.x[0];
462 min_y = max_y = surf.y[0];
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400463
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300464 for (i = 1; i < surf.n; i++) {
465 min_x = min(min_x, surf.x[i]);
466 max_x = max(max_x, surf.x[i]);
467 min_y = min(min_y, surf.y[i]);
468 max_y = max(max_y, surf.y[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400469 }
470
471 /* First, simple bounding box check to discard early transformed
472 * surface rects that do not intersect with the clip region:
473 */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300474 if ((min_x >= ctx.clip.x2) || (max_x <= ctx.clip.x1) ||
475 (min_y >= ctx.clip.y2) || (max_y <= ctx.clip.y1))
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400476 return 0;
477
478 /* Simple case, bounding box edges are parallel to surface edges,
479 * there will be only four edges. We just need to clip the surface
480 * vertices to the clip rect bounds:
481 */
482 if (!es->transform.enabled) {
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300483 for (i = 0; i < surf.n; i++) {
484 ex[i] = clip(surf.x[i], ctx.clip.x1, ctx.clip.x2);
485 ey[i] = clip(surf.y[i], ctx.clip.y1, ctx.clip.y2);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400486 }
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300487 return surf.n;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400488 }
489
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300490 /* Transformed case: use a general polygon clipping algorithm to
491 * clip the surface rectangle with each side of 'rect'.
492 * The algorithm is Sutherland-Hodgman, as explained in
493 * http://www.codeguru.com/cpp/misc/misc/graphics/article.php/c8965/Polygon-Clipping.htm
494 * but without looking at any of that code.
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400495 */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300496 polygon.n = clip_polygon_left(&ctx, &surf, polygon.x, polygon.y);
497 surf.n = clip_polygon_right(&ctx, &polygon, surf.x, surf.y);
498 polygon.n = clip_polygon_top(&ctx, &surf, polygon.x, polygon.y);
499 surf.n = clip_polygon_bottom(&ctx, &polygon, surf.x, surf.y);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400500
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300501 /* Get rid of duplicate vertices */
502 ex[0] = surf.x[0];
503 ey[0] = surf.y[0];
504 n = 1;
505 for (i = 1; i < surf.n; i++) {
506 if (float_difference(ex[n - 1], surf.x[i]) == 0.0f &&
507 float_difference(ey[n - 1], surf.y[i]) == 0.0f)
508 continue;
509 ex[n] = surf.x[i];
510 ey[n] = surf.y[i];
511 n++;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400512 }
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300513 if (float_difference(ex[n - 1], surf.x[0]) == 0.0f &&
514 float_difference(ey[n - 1], surf.y[0]) == 0.0f)
515 n--;
516
517 if (n < 3)
518 return 0;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400519
520 return n;
521}
522
523static int
524texture_region(struct weston_surface *es, pixman_region32_t *region,
525 pixman_region32_t *surf_region)
526{
Pekka Paalanen68033ac2012-12-04 15:58:15 +0200527 struct gl_surface_state *gs = get_surface_state(es);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400528 struct weston_compositor *ec = es->compositor;
529 GLfloat *v, inv_width, inv_height;
530 unsigned int *vtxcnt, nvtx = 0;
531 pixman_box32_t *rects, *surf_rects;
532 int i, j, k, nrects, nsurf;
533
534 rects = pixman_region32_rectangles(region, &nrects);
535 surf_rects = pixman_region32_rectangles(surf_region, &nsurf);
536
537 /* worst case we can have 8 vertices per rect (ie. clipped into
538 * an octagon):
539 */
540 v = wl_array_add(&ec->vertices, nrects * nsurf * 8 * 4 * sizeof *v);
541 vtxcnt = wl_array_add(&ec->vtxcnt, nrects * nsurf * sizeof *vtxcnt);
542
Pekka Paalanen68033ac2012-12-04 15:58:15 +0200543 inv_width = 1.0 / gs->pitch;
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200544
545 switch (es->buffer_transform) {
546 case WL_OUTPUT_TRANSFORM_90:
547 case WL_OUTPUT_TRANSFORM_270:
548 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
549 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
550 inv_height = 1.0 / es->geometry.width;
551 break;
552 default:
553 inv_height = 1.0 / es->geometry.height;
554 }
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400555
556 for (i = 0; i < nrects; i++) {
557 pixman_box32_t *rect = &rects[i];
558 for (j = 0; j < nsurf; j++) {
559 pixman_box32_t *surf_rect = &surf_rects[j];
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200560 GLfloat sx, sy, bx, by;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400561 GLfloat ex[8], ey[8]; /* edge points in screen space */
562 int n;
563
564 /* The transformed surface, after clipping to the clip region,
565 * can have as many as eight sides, emitted as a triangle-fan.
566 * The first vertex in the triangle fan can be chosen arbitrarily,
567 * since the area is guaranteed to be convex.
568 *
569 * If a corner of the transformed surface falls outside of the
570 * clip region, instead of emitting one vertex for the corner
571 * of the surface, up to two are emitted for two corresponding
572 * intersection point(s) between the surface and the clip region.
573 *
574 * To do this, we first calculate the (up to eight) points that
575 * form the intersection of the clip rect and the transformed
576 * surface.
577 */
578 n = calculate_edges(es, rect, surf_rect, ex, ey);
579 if (n < 3)
580 continue;
581
582 /* emit edge points: */
583 for (k = 0; k < n; k++) {
584 weston_surface_from_global_float(es, ex[k], ey[k], &sx, &sy);
585 /* position: */
586 *(v++) = ex[k];
587 *(v++) = ey[k];
588 /* texcoord: */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200589 weston_surface_to_buffer_float(es, sx, sy,
590 &bx, &by);
591 *(v++) = bx * inv_width;
592 *(v++) = by * inv_height;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400593 }
594
595 vtxcnt[nvtx++] = n;
596 }
597 }
598
599 return nvtx;
600}
601
602static void
603triangle_fan_debug(struct weston_surface *surface, int first, int count)
604{
605 struct weston_compositor *compositor = surface->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100606 struct gl_renderer *gr = get_renderer(compositor);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400607 int i;
608 GLushort *buffer;
609 GLushort *index;
610 int nelems;
611 static int color_idx = 0;
612 static const GLfloat color[][4] = {
613 { 1.0, 0.0, 0.0, 1.0 },
614 { 0.0, 1.0, 0.0, 1.0 },
615 { 0.0, 0.0, 1.0, 1.0 },
616 { 1.0, 1.0, 1.0, 1.0 },
617 };
618
619 nelems = (count - 1 + count - 2) * 2;
620
621 buffer = malloc(sizeof(GLushort) * nelems);
622 index = buffer;
623
624 for (i = 1; i < count; i++) {
625 *index++ = first;
626 *index++ = first + i;
627 }
628
629 for (i = 2; i < count; i++) {
630 *index++ = first + i - 1;
631 *index++ = first + i;
632 }
633
John Kåre Alsaker40684142012-11-13 19:10:25 +0100634 glUseProgram(gr->solid_shader.program);
635 glUniform4fv(gr->solid_shader.color_uniform, 1,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400636 color[color_idx++ % ARRAY_LENGTH(color)]);
637 glDrawElements(GL_LINES, nelems, GL_UNSIGNED_SHORT, buffer);
John Kåre Alsaker40684142012-11-13 19:10:25 +0100638 glUseProgram(gr->current_shader->program);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400639 free(buffer);
640}
641
642static void
643repaint_region(struct weston_surface *es, pixman_region32_t *region,
644 pixman_region32_t *surf_region)
645{
646 struct weston_compositor *ec = es->compositor;
647 GLfloat *v;
648 unsigned int *vtxcnt;
649 int i, first, nfans;
650
651 /* The final region to be painted is the intersection of
652 * 'region' and 'surf_region'. However, 'region' is in the global
653 * coordinates, and 'surf_region' is in the surface-local
654 * coordinates. texture_region() will iterate over all pairs of
655 * rectangles from both regions, compute the intersection
656 * polygon for each pair, and store it as a triangle fan if
657 * it has a non-zero area (at least 3 vertices, actually).
658 */
659 nfans = texture_region(es, region, surf_region);
660
661 v = ec->vertices.data;
662 vtxcnt = ec->vtxcnt.data;
663
664 /* position: */
665 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
666 glEnableVertexAttribArray(0);
667
668 /* texcoord: */
669 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
670 glEnableVertexAttribArray(1);
671
672 for (i = 0, first = 0; i < nfans; i++) {
673 glDrawArrays(GL_TRIANGLE_FAN, first, vtxcnt[i]);
674 if (ec->fan_debug)
675 triangle_fan_debug(es, first, vtxcnt[i]);
676 first += vtxcnt[i];
677 }
678
679 glDisableVertexAttribArray(1);
680 glDisableVertexAttribArray(0);
681
682 ec->vertices.size = 0;
683 ec->vtxcnt.size = 0;
684}
685
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100686static int
687use_output(struct weston_output *output)
688{
689 static int errored;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100690 struct gl_output_state *go = get_output_state(output);
691 struct gl_renderer *gr = get_renderer(output->compositor);
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100692 EGLBoolean ret;
693
694 ret = eglMakeCurrent(gr->egl_display, go->egl_surface,
695 go->egl_surface, gr->egl_context);
696
697 if (ret == EGL_FALSE) {
698 if (errored)
699 return -1;
700 errored = 1;
701 weston_log("Failed to make EGL context current.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +0200702 gl_renderer_print_egl_error_state();
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100703 return -1;
704 }
705
706 return 0;
707}
708
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400709static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100710use_shader(struct gl_renderer *gr,
711 struct gl_shader *shader)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400712{
John Kåre Alsaker40684142012-11-13 19:10:25 +0100713 if (gr->current_shader == shader)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400714 return;
715
716 glUseProgram(shader->program);
John Kåre Alsaker40684142012-11-13 19:10:25 +0100717 gr->current_shader = shader;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400718}
719
720static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100721shader_uniforms(struct gl_shader *shader,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400722 struct weston_surface *surface,
723 struct weston_output *output)
724{
725 int i;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100726 struct gl_surface_state *gs = get_surface_state(surface);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400727
728 glUniformMatrix4fv(shader->proj_uniform,
729 1, GL_FALSE, output->matrix.d);
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100730 glUniform4fv(shader->color_uniform, 1, gs->color);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400731 glUniform1f(shader->alpha_uniform, surface->alpha);
732
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100733 for (i = 0; i < gs->num_textures; i++)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400734 glUniform1i(shader->tex_uniforms[i], i);
735}
736
737static void
738draw_surface(struct weston_surface *es, struct weston_output *output,
739 pixman_region32_t *damage) /* in global coordinates */
740{
741 struct weston_compositor *ec = es->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100742 struct gl_renderer *gr = get_renderer(ec);
743 struct gl_surface_state *gs = get_surface_state(es);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +0200744 struct gl_output_state *go = get_output_state(output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400745 /* repaint bounding region in global coordinates: */
746 pixman_region32_t repaint;
747 /* non-opaque region in surface coordinates: */
748 pixman_region32_t surface_blend;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300749 pixman_region32_t *buffer_damage;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400750 GLint filter;
751 int i;
752
753 pixman_region32_init(&repaint);
754 pixman_region32_intersect(&repaint,
755 &es->transform.boundingbox, damage);
756 pixman_region32_subtract(&repaint, &repaint, &es->clip);
757
758 if (!pixman_region32_not_empty(&repaint))
759 goto out;
760
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +0200761 buffer_damage = &go->buffer_damage[go->current_buffer];
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300762 pixman_region32_subtract(buffer_damage, buffer_damage, &repaint);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400763
764 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
765
766 if (ec->fan_debug) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100767 use_shader(gr, &gr->solid_shader);
768 shader_uniforms(&gr->solid_shader, es, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400769 }
770
John Kåre Alsaker40684142012-11-13 19:10:25 +0100771 use_shader(gr, gs->shader);
772 shader_uniforms(gs->shader, es, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400773
774 if (es->transform.enabled || output->zoom.active)
775 filter = GL_LINEAR;
776 else
777 filter = GL_NEAREST;
778
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100779 for (i = 0; i < gs->num_textures; i++) {
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400780 glActiveTexture(GL_TEXTURE0 + i);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100781 glBindTexture(gs->target, gs->textures[i]);
782 glTexParameteri(gs->target, GL_TEXTURE_MIN_FILTER, filter);
783 glTexParameteri(gs->target, GL_TEXTURE_MAG_FILTER, filter);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400784 }
785
786 /* blended region is whole surface minus opaque region: */
787 pixman_region32_init_rect(&surface_blend, 0, 0,
788 es->geometry.width, es->geometry.height);
789 pixman_region32_subtract(&surface_blend, &surface_blend, &es->opaque);
790
791 if (pixman_region32_not_empty(&es->opaque)) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100792 if (gs->shader == &gr->texture_shader_rgba) {
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400793 /* Special case for RGBA textures with possibly
794 * bad data in alpha channel: use the shader
795 * that forces texture alpha = 1.0.
796 * Xwayland surfaces need this.
797 */
John Kåre Alsaker40684142012-11-13 19:10:25 +0100798 use_shader(gr, &gr->texture_shader_rgbx);
799 shader_uniforms(&gr->texture_shader_rgbx, es, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400800 }
801
802 if (es->alpha < 1.0)
803 glEnable(GL_BLEND);
804 else
805 glDisable(GL_BLEND);
806
807 repaint_region(es, &repaint, &es->opaque);
808 }
809
810 if (pixman_region32_not_empty(&surface_blend)) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100811 use_shader(gr, gs->shader);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400812 glEnable(GL_BLEND);
813 repaint_region(es, &repaint, &surface_blend);
814 }
815
816 pixman_region32_fini(&surface_blend);
817
818out:
819 pixman_region32_fini(&repaint);
820}
821
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400822static void
823repaint_surfaces(struct weston_output *output, pixman_region32_t *damage)
824{
825 struct weston_compositor *compositor = output->compositor;
826 struct weston_surface *surface;
827
828 wl_list_for_each_reverse(surface, &compositor->surface_list, link)
829 if (surface->plane == &compositor->primary_plane)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400830 draw_surface(surface, output, damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400831}
832
John Kåre Alsaker44154502012-11-13 19:10:20 +0100833
834static int
835texture_border(struct weston_output *output)
836{
837 struct weston_compositor *ec = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100838 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker44154502012-11-13 19:10:20 +0100839 GLfloat *d;
840 unsigned int *p;
841 int i, j, k, n;
842 GLfloat x[4], y[4], u[4], v[4];
843
844 x[0] = -gr->border.left;
845 x[1] = 0;
846 x[2] = output->current->width;
847 x[3] = output->current->width + gr->border.right;
848
849 y[0] = -gr->border.top;
850 y[1] = 0;
851 y[2] = output->current->height;
852 y[3] = output->current->height + gr->border.bottom;
853
854 u[0] = 0.0;
855 u[1] = (GLfloat) gr->border.left / gr->border.width;
856 u[2] = (GLfloat) (gr->border.width - gr->border.right) / gr->border.width;
857 u[3] = 1.0;
858
859 v[0] = 0.0;
860 v[1] = (GLfloat) gr->border.top / gr->border.height;
861 v[2] = (GLfloat) (gr->border.height - gr->border.bottom) / gr->border.height;
862 v[3] = 1.0;
863
864 n = 8;
865 d = wl_array_add(&ec->vertices, n * 16 * sizeof *d);
866 p = wl_array_add(&ec->indices, n * 6 * sizeof *p);
867
868 k = 0;
869 for (i = 0; i < 3; i++)
870 for (j = 0; j < 3; j++) {
871
872 if (i == 1 && j == 1)
873 continue;
874
875 d[ 0] = x[i];
876 d[ 1] = y[j];
877 d[ 2] = u[i];
878 d[ 3] = v[j];
879
880 d[ 4] = x[i];
881 d[ 5] = y[j + 1];
882 d[ 6] = u[i];
883 d[ 7] = v[j + 1];
884
885 d[ 8] = x[i + 1];
886 d[ 9] = y[j];
887 d[10] = u[i + 1];
888 d[11] = v[j];
889
890 d[12] = x[i + 1];
891 d[13] = y[j + 1];
892 d[14] = u[i + 1];
893 d[15] = v[j + 1];
894
895 p[0] = k + 0;
896 p[1] = k + 1;
897 p[2] = k + 2;
898 p[3] = k + 2;
899 p[4] = k + 1;
900 p[5] = k + 3;
901
902 d += 16;
903 p += 6;
904 k += 4;
905 }
906
907 return k / 4;
908}
909
910static void
911draw_border(struct weston_output *output)
912{
913 struct weston_compositor *ec = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100914 struct gl_renderer *gr = get_renderer(ec);
915 struct gl_shader *shader = &gr->texture_shader_rgba;
John Kåre Alsaker44154502012-11-13 19:10:20 +0100916 GLfloat *v;
917 int n;
918
919 glDisable(GL_BLEND);
John Kåre Alsaker40684142012-11-13 19:10:25 +0100920 use_shader(gr, shader);
John Kåre Alsaker44154502012-11-13 19:10:20 +0100921
922 glUniformMatrix4fv(shader->proj_uniform,
923 1, GL_FALSE, output->matrix.d);
924
925 glUniform1i(shader->tex_uniforms[0], 0);
926 glUniform1f(shader->alpha_uniform, 1);
927
928 n = texture_border(output);
929
930 glActiveTexture(GL_TEXTURE0);
931 glBindTexture(GL_TEXTURE_2D, gr->border.texture);
932
933 v = ec->vertices.data;
934 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
935 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
936 glEnableVertexAttribArray(0);
937 glEnableVertexAttribArray(1);
938
939 glDrawElements(GL_TRIANGLES, n * 6,
940 GL_UNSIGNED_INT, ec->indices.data);
941
942 glDisableVertexAttribArray(1);
943 glDisableVertexAttribArray(0);
944
945 ec->vertices.size = 0;
946 ec->indices.size = 0;
947}
948
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400949static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100950gl_renderer_repaint_output(struct weston_output *output,
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400951 pixman_region32_t *output_damage)
952{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100953 struct gl_output_state *go = get_output_state(output);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400954 struct weston_compositor *compositor = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100955 struct gl_renderer *gr = get_renderer(compositor);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400956 EGLBoolean ret;
957 static int errored;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300958 int32_t width, height, i;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400959
960 width = output->current->width +
961 output->border.left + output->border.right;
962 height = output->current->height +
963 output->border.top + output->border.bottom;
964
965 glViewport(0, 0, width, height);
966
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100967 if (use_output(output) < 0)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400968 return;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400969
970 /* if debugging, redraw everything outside the damage to clean up
971 * debug lines from the previous draw on this buffer:
972 */
973 if (compositor->fan_debug) {
974 pixman_region32_t undamaged;
975 pixman_region32_init(&undamaged);
976 pixman_region32_subtract(&undamaged, &output->region,
977 output_damage);
978 compositor->fan_debug = 0;
979 repaint_surfaces(output, &undamaged);
980 compositor->fan_debug = 1;
981 pixman_region32_fini(&undamaged);
982 }
983
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300984 for (i = 0; i < 2; i++)
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +0200985 pixman_region32_union(&go->buffer_damage[i],
986 &go->buffer_damage[i],
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300987 output_damage);
988
989 pixman_region32_union(output_damage, output_damage,
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +0200990 &go->buffer_damage[go->current_buffer]);
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300991
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400992 repaint_surfaces(output, output_damage);
993
John Kåre Alsaker44154502012-11-13 19:10:20 +0100994 if (gr->border.texture)
995 draw_border(output);
996
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +0200997 pixman_region32_copy(&output->previous_damage, output_damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400998 wl_signal_emit(&output->frame_signal, output);
999
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001000 ret = eglSwapBuffers(gr->egl_display, go->egl_surface);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001001 if (ret == EGL_FALSE && !errored) {
1002 errored = 1;
1003 weston_log("Failed in eglSwapBuffers.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02001004 gl_renderer_print_egl_error_state();
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001005 }
1006
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001007 go->current_buffer ^= 1;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03001008
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001009}
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001010
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001011static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001012gl_renderer_read_pixels(struct weston_output *output,
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001013 pixman_format_code_t format, void *pixels,
1014 uint32_t x, uint32_t y,
1015 uint32_t width, uint32_t height)
1016{
1017 GLenum gl_format;
1018
1019 switch (format) {
1020 case PIXMAN_a8r8g8b8:
1021 gl_format = GL_BGRA_EXT;
1022 break;
1023 case PIXMAN_a8b8g8r8:
1024 gl_format = GL_RGBA;
1025 break;
1026 default:
1027 return -1;
1028 }
1029
1030 if (use_output(output) < 0)
1031 return -1;
1032
1033 glPixelStorei(GL_PACK_ALIGNMENT, 1);
1034 glReadPixels(x, y, width, height, gl_format,
1035 GL_UNSIGNED_BYTE, pixels);
1036
1037 return 0;
1038}
1039
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001040static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001041gl_renderer_flush_damage(struct weston_surface *surface)
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001042{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001043 struct gl_renderer *gr = get_renderer(surface->compositor);
1044 struct gl_surface_state *gs = get_surface_state(surface);
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001045 struct wl_buffer *buffer = gs->buffer_ref.buffer;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001046
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001047#ifdef GL_UNPACK_ROW_LENGTH
1048 pixman_box32_t *rectangles;
1049 void *data;
1050 int i, n;
1051#endif
1052
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001053 pixman_region32_union(&surface->texture_damage,
1054 &surface->texture_damage, &surface->damage);
1055
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001056 if (!buffer)
1057 return;
1058
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001059 /* Avoid upload, if the texture won't be used this time.
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001060 * We still accumulate the damage in texture_damage, and
1061 * hold the reference to the buffer, in case the surface
1062 * migrates back to the primary plane.
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001063 */
1064 if (surface->plane != &surface->compositor->primary_plane)
1065 return;
1066
1067 if (!pixman_region32_not_empty(&surface->texture_damage))
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001068 goto done;
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001069
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001070 glBindTexture(GL_TEXTURE_2D, gs->textures[0]);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001071
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001072 if (!gr->has_unpack_subimage) {
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001073 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001074 gs->pitch, buffer->height, 0,
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001075 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
Pekka Paalanende685b82012-12-04 15:58:12 +02001076 wl_shm_buffer_get_data(buffer));
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001077
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001078 goto done;
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001079 }
1080
1081#ifdef GL_UNPACK_ROW_LENGTH
1082 /* Mesa does not define GL_EXT_unpack_subimage */
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001083 glPixelStorei(GL_UNPACK_ROW_LENGTH, gs->pitch);
Pekka Paalanende685b82012-12-04 15:58:12 +02001084 data = wl_shm_buffer_get_data(buffer);
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001085 rectangles = pixman_region32_rectangles(&surface->texture_damage, &n);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001086 for (i = 0; i < n; i++) {
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001087 pixman_box32_t r;
1088
1089 r = weston_surface_to_buffer_rect(surface, rectangles[i]);
1090
1091 glPixelStorei(GL_UNPACK_SKIP_PIXELS, r.x1);
1092 glPixelStorei(GL_UNPACK_SKIP_ROWS, r.y1);
1093 glTexSubImage2D(GL_TEXTURE_2D, 0, r.x1, r.y1,
1094 r.x2 - r.x1, r.y2 - r.y1,
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001095 GL_BGRA_EXT, GL_UNSIGNED_BYTE, data);
1096 }
1097#endif
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001098
1099done:
1100 pixman_region32_fini(&surface->texture_damage);
1101 pixman_region32_init(&surface->texture_damage);
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001102
1103 weston_buffer_reference(&gs->buffer_ref, NULL);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001104}
1105
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001106static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001107ensure_textures(struct gl_surface_state *gs, int num_textures)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001108{
1109 int i;
1110
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001111 if (num_textures <= gs->num_textures)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001112 return;
1113
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001114 for (i = gs->num_textures; i < num_textures; i++) {
1115 glGenTextures(1, &gs->textures[i]);
1116 glBindTexture(gs->target, gs->textures[i]);
1117 glTexParameteri(gs->target,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001118 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001119 glTexParameteri(gs->target,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001120 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1121 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001122 gs->num_textures = num_textures;
1123 glBindTexture(gs->target, 0);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001124}
1125
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001126static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001127gl_renderer_attach(struct weston_surface *es, struct wl_buffer *buffer)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001128{
1129 struct weston_compositor *ec = es->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001130 struct gl_renderer *gr = get_renderer(ec);
1131 struct gl_surface_state *gs = get_surface_state(es);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001132 EGLint attribs[3], format;
1133 int i, num_planes;
1134
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001135 weston_buffer_reference(&gs->buffer_ref, buffer);
1136
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001137 if (!buffer) {
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001138 for (i = 0; i < gs->num_images; i++) {
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001139 gr->destroy_image(gr->egl_display, gs->images[i]);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001140 gs->images[i] = NULL;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001141 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001142 gs->num_images = 0;
1143 glDeleteTextures(gs->num_textures, gs->textures);
1144 gs->num_textures = 0;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001145 return;
1146 }
1147
1148 if (wl_buffer_is_shm(buffer)) {
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001149 gs->pitch = wl_shm_buffer_get_stride(buffer) / 4;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001150 gs->target = GL_TEXTURE_2D;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001151
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001152 ensure_textures(gs, 1);
1153 glBindTexture(GL_TEXTURE_2D, gs->textures[0]);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001154 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001155 gs->pitch, buffer->height, 0,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001156 GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
1157 if (wl_shm_buffer_get_format(buffer) == WL_SHM_FORMAT_XRGB8888)
John Kåre Alsaker40684142012-11-13 19:10:25 +01001158 gs->shader = &gr->texture_shader_rgbx;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001159 else
John Kåre Alsaker40684142012-11-13 19:10:25 +01001160 gs->shader = &gr->texture_shader_rgba;
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001161 } else if (gr->query_buffer(gr->egl_display, buffer,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001162 EGL_TEXTURE_FORMAT, &format)) {
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001163 for (i = 0; i < gs->num_images; i++)
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001164 gr->destroy_image(gr->egl_display, gs->images[i]);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001165 gs->num_images = 0;
1166 gs->target = GL_TEXTURE_2D;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001167 switch (format) {
1168 case EGL_TEXTURE_RGB:
1169 case EGL_TEXTURE_RGBA:
1170 default:
1171 num_planes = 1;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001172 gs->shader = &gr->texture_shader_rgba;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001173 break;
1174 case EGL_TEXTURE_EXTERNAL_WL:
1175 num_planes = 1;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001176 gs->target = GL_TEXTURE_EXTERNAL_OES;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001177 gs->shader = &gr->texture_shader_egl_external;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001178 break;
1179 case EGL_TEXTURE_Y_UV_WL:
1180 num_planes = 2;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001181 gs->shader = &gr->texture_shader_y_uv;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001182 break;
1183 case EGL_TEXTURE_Y_U_V_WL:
1184 num_planes = 3;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001185 gs->shader = &gr->texture_shader_y_u_v;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001186 break;
1187 case EGL_TEXTURE_Y_XUXV_WL:
1188 num_planes = 2;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001189 gs->shader = &gr->texture_shader_y_xuxv;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001190 break;
1191 }
1192
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001193 ensure_textures(gs, num_planes);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001194 for (i = 0; i < num_planes; i++) {
1195 attribs[0] = EGL_WAYLAND_PLANE_WL;
1196 attribs[1] = i;
1197 attribs[2] = EGL_NONE;
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001198 gs->images[i] = gr->create_image(gr->egl_display,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001199 NULL,
1200 EGL_WAYLAND_BUFFER_WL,
1201 buffer, attribs);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001202 if (!gs->images[i]) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001203 weston_log("failed to create img for plane %d\n", i);
1204 continue;
1205 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001206 gs->num_images++;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001207
1208 glActiveTexture(GL_TEXTURE0 + i);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001209 glBindTexture(gs->target, gs->textures[i]);
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001210 gr->image_target_texture_2d(gs->target,
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001211 gs->images[i]);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001212 }
1213
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001214 gs->pitch = buffer->width;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001215 } else {
1216 weston_log("unhandled buffer type!\n");
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001217 weston_buffer_reference(&gs->buffer_ref, NULL);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001218 }
1219}
1220
Kristian Høgsberg42263852012-09-06 21:59:29 -04001221static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001222gl_renderer_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001223 float red, float green, float blue, float alpha)
1224{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001225 struct gl_surface_state *gs = get_surface_state(surface);
1226 struct gl_renderer *gr = get_renderer(surface->compositor);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001227
1228 gs->color[0] = red;
1229 gs->color[1] = green;
1230 gs->color[2] = blue;
1231 gs->color[3] = alpha;
1232
John Kåre Alsaker40684142012-11-13 19:10:25 +01001233 gs->shader = &gr->solid_shader;
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001234}
1235
1236static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001237gl_renderer_create_surface(struct weston_surface *surface)
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001238{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001239 struct gl_surface_state *gs;
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001240
1241 gs = calloc(1, sizeof *gs);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001242 if (!gs)
1243 return -1;
1244
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001245 /* A buffer is never attached to solid color surfaces, yet
1246 * they still go through texcoord computations. Do not divide
1247 * by zero there.
1248 */
1249 gs->pitch = 1;
1250
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001251 surface->renderer_state = gs;
1252
1253 return 0;
1254}
1255
1256static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001257gl_renderer_destroy_surface(struct weston_surface *surface)
Kristian Høgsberg42263852012-09-06 21:59:29 -04001258{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001259 struct gl_surface_state *gs = get_surface_state(surface);
1260 struct gl_renderer *gr = get_renderer(surface->compositor);
Kristian Høgsberg42263852012-09-06 21:59:29 -04001261 int i;
1262
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001263 glDeleteTextures(gs->num_textures, gs->textures);
Kristian Høgsberg42263852012-09-06 21:59:29 -04001264
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001265 for (i = 0; i < gs->num_images; i++)
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001266 gr->destroy_image(gr->egl_display, gs->images[i]);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001267
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001268 weston_buffer_reference(&gs->buffer_ref, NULL);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001269 free(gs);
Kristian Høgsberg42263852012-09-06 21:59:29 -04001270}
1271
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001272static const char vertex_shader[] =
1273 "uniform mat4 proj;\n"
1274 "attribute vec2 position;\n"
1275 "attribute vec2 texcoord;\n"
1276 "varying vec2 v_texcoord;\n"
1277 "void main()\n"
1278 "{\n"
1279 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
1280 " v_texcoord = texcoord;\n"
1281 "}\n";
1282
1283/* Declare common fragment shader uniforms */
1284#define FRAGMENT_CONVERT_YUV \
1285 " y *= alpha;\n" \
1286 " u *= alpha;\n" \
1287 " v *= alpha;\n" \
1288 " gl_FragColor.r = y + 1.59602678 * v;\n" \
1289 " gl_FragColor.g = y - 0.39176229 * u - 0.81296764 * v;\n" \
1290 " gl_FragColor.b = y + 2.01723214 * u;\n" \
1291 " gl_FragColor.a = alpha;\n"
1292
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001293static const char fragment_debug[] =
1294 " gl_FragColor = vec4(0.0, 0.3, 0.0, 0.2) + gl_FragColor * 0.8;\n";
1295
1296static const char fragment_brace[] =
1297 "}\n";
1298
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001299static const char texture_fragment_shader_rgba[] =
1300 "precision mediump float;\n"
1301 "varying vec2 v_texcoord;\n"
1302 "uniform sampler2D tex;\n"
1303 "uniform float alpha;\n"
1304 "void main()\n"
1305 "{\n"
1306 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001307 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001308
1309static const char texture_fragment_shader_rgbx[] =
1310 "precision mediump float;\n"
1311 "varying vec2 v_texcoord;\n"
1312 "uniform sampler2D tex;\n"
1313 "uniform float alpha;\n"
1314 "void main()\n"
1315 "{\n"
1316 " gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb\n;"
1317 " gl_FragColor.a = alpha;\n"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001318 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001319
1320static const char texture_fragment_shader_egl_external[] =
1321 "#extension GL_OES_EGL_image_external : require\n"
1322 "precision mediump float;\n"
1323 "varying vec2 v_texcoord;\n"
1324 "uniform samplerExternalOES tex;\n"
1325 "uniform float alpha;\n"
1326 "void main()\n"
1327 "{\n"
1328 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001329 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001330
1331static const char texture_fragment_shader_y_uv[] =
1332 "precision mediump float;\n"
1333 "uniform sampler2D tex;\n"
1334 "uniform sampler2D tex1;\n"
1335 "varying vec2 v_texcoord;\n"
1336 "uniform float alpha;\n"
1337 "void main() {\n"
1338 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1339 " float u = texture2D(tex1, v_texcoord).r - 0.5;\n"
1340 " float v = texture2D(tex1, v_texcoord).g - 0.5;\n"
1341 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001342 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001343
1344static const char texture_fragment_shader_y_u_v[] =
1345 "precision mediump float;\n"
1346 "uniform sampler2D tex;\n"
1347 "uniform sampler2D tex1;\n"
1348 "uniform sampler2D tex2;\n"
1349 "varying vec2 v_texcoord;\n"
1350 "uniform float alpha;\n"
1351 "void main() {\n"
1352 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1353 " float u = texture2D(tex1, v_texcoord).x - 0.5;\n"
1354 " float v = texture2D(tex2, v_texcoord).x - 0.5;\n"
1355 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001356 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001357
1358static const char texture_fragment_shader_y_xuxv[] =
1359 "precision mediump float;\n"
1360 "uniform sampler2D tex;\n"
1361 "uniform sampler2D tex1;\n"
1362 "varying vec2 v_texcoord;\n"
1363 "uniform float alpha;\n"
1364 "void main() {\n"
1365 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1366 " float u = texture2D(tex1, v_texcoord).g - 0.5;\n"
1367 " float v = texture2D(tex1, v_texcoord).a - 0.5;\n"
1368 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001369 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001370
1371static const char solid_fragment_shader[] =
1372 "precision mediump float;\n"
1373 "uniform vec4 color;\n"
1374 "uniform float alpha;\n"
1375 "void main()\n"
1376 "{\n"
1377 " gl_FragColor = alpha * color\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001378 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001379
1380static int
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001381compile_shader(GLenum type, int count, const char **sources)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001382{
1383 GLuint s;
1384 char msg[512];
1385 GLint status;
1386
1387 s = glCreateShader(type);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001388 glShaderSource(s, count, sources, NULL);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001389 glCompileShader(s);
1390 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
1391 if (!status) {
1392 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1393 weston_log("shader info: %s\n", msg);
1394 return GL_NONE;
1395 }
1396
1397 return s;
1398}
1399
1400static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001401shader_init(struct gl_shader *shader, struct weston_compositor *ec,
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001402 const char *vertex_source, const char *fragment_source)
1403{
1404 char msg[512];
1405 GLint status;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001406 int count;
1407 const char *sources[3];
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001408 struct gl_renderer *renderer = get_renderer(ec);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001409
1410 shader->vertex_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001411 compile_shader(GL_VERTEX_SHADER, 1, &vertex_source);
1412
1413 if (renderer->fragment_shader_debug) {
1414 sources[0] = fragment_source;
1415 sources[1] = fragment_debug;
1416 sources[2] = fragment_brace;
1417 count = 3;
1418 } else {
1419 sources[0] = fragment_source;
1420 sources[1] = fragment_brace;
1421 count = 2;
1422 }
1423
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001424 shader->fragment_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001425 compile_shader(GL_FRAGMENT_SHADER, count, sources);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001426
1427 shader->program = glCreateProgram();
1428 glAttachShader(shader->program, shader->vertex_shader);
1429 glAttachShader(shader->program, shader->fragment_shader);
1430 glBindAttribLocation(shader->program, 0, "position");
1431 glBindAttribLocation(shader->program, 1, "texcoord");
1432
1433 glLinkProgram(shader->program);
1434 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1435 if (!status) {
1436 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1437 weston_log("link info: %s\n", msg);
1438 return -1;
1439 }
1440
1441 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1442 shader->tex_uniforms[0] = glGetUniformLocation(shader->program, "tex");
1443 shader->tex_uniforms[1] = glGetUniformLocation(shader->program, "tex1");
1444 shader->tex_uniforms[2] = glGetUniformLocation(shader->program, "tex2");
1445 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
1446 shader->color_uniform = glGetUniformLocation(shader->program, "color");
1447
1448 return 0;
1449}
1450
1451static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001452shader_release(struct gl_shader *shader)
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001453{
1454 glDeleteShader(shader->vertex_shader);
1455 glDeleteShader(shader->fragment_shader);
1456 glDeleteProgram(shader->program);
1457
1458 shader->vertex_shader = 0;
1459 shader->fragment_shader = 0;
1460 shader->program = 0;
1461}
1462
1463static void
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001464log_extensions(const char *name, const char *extensions)
1465{
1466 const char *p, *end;
1467 int l;
1468 int len;
1469
1470 l = weston_log("%s:", name);
1471 p = extensions;
1472 while (*p) {
1473 end = strchrnul(p, ' ');
1474 len = end - p;
1475 if (l + len > 78)
1476 l = weston_log_continue("\n" STAMP_SPACE "%.*s",
1477 len, p);
1478 else
1479 l += weston_log_continue(" %.*s", len, p);
1480 for (p = end; isspace(*p); p++)
1481 ;
1482 }
1483 weston_log_continue("\n");
1484}
1485
1486static void
1487log_egl_gl_info(EGLDisplay egldpy)
1488{
1489 const char *str;
1490
1491 str = eglQueryString(egldpy, EGL_VERSION);
1492 weston_log("EGL version: %s\n", str ? str : "(null)");
1493
1494 str = eglQueryString(egldpy, EGL_VENDOR);
1495 weston_log("EGL vendor: %s\n", str ? str : "(null)");
1496
1497 str = eglQueryString(egldpy, EGL_CLIENT_APIS);
1498 weston_log("EGL client APIs: %s\n", str ? str : "(null)");
1499
1500 str = eglQueryString(egldpy, EGL_EXTENSIONS);
1501 log_extensions("EGL extensions", str ? str : "(null)");
1502
1503 str = (char *)glGetString(GL_VERSION);
1504 weston_log("GL version: %s\n", str ? str : "(null)");
1505
1506 str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
1507 weston_log("GLSL version: %s\n", str ? str : "(null)");
1508
1509 str = (char *)glGetString(GL_VENDOR);
1510 weston_log("GL vendor: %s\n", str ? str : "(null)");
1511
1512 str = (char *)glGetString(GL_RENDERER);
1513 weston_log("GL renderer: %s\n", str ? str : "(null)");
1514
1515 str = (char *)glGetString(GL_EXTENSIONS);
1516 log_extensions("GL extensions", str ? str : "(null)");
1517}
1518
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001519static void
1520log_egl_config_info(EGLDisplay egldpy, EGLConfig eglconfig)
1521{
1522 EGLint r, g, b, a;
1523
1524 weston_log("Chosen EGL config details:\n");
1525
1526 weston_log_continue(STAMP_SPACE "RGBA bits");
1527 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_RED_SIZE, &r) &&
1528 eglGetConfigAttrib(egldpy, eglconfig, EGL_GREEN_SIZE, &g) &&
1529 eglGetConfigAttrib(egldpy, eglconfig, EGL_BLUE_SIZE, &b) &&
1530 eglGetConfigAttrib(egldpy, eglconfig, EGL_ALPHA_SIZE, &a))
1531 weston_log_continue(": %d %d %d %d\n", r, g, b, a);
1532 else
1533 weston_log_continue(" unknown\n");
1534
1535 weston_log_continue(STAMP_SPACE "swap interval range");
1536 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_MIN_SWAP_INTERVAL, &a) &&
1537 eglGetConfigAttrib(egldpy, eglconfig, EGL_MAX_SWAP_INTERVAL, &b))
1538 weston_log_continue(": %d - %d\n", a, b);
1539 else
1540 weston_log_continue(" unknown\n");
1541}
1542
John Kåre Alsaker44154502012-11-13 19:10:20 +01001543static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001544output_apply_border(struct weston_output *output, struct gl_renderer *gr)
John Kåre Alsaker44154502012-11-13 19:10:20 +01001545{
1546 output->border.top = gr->border.top;
1547 output->border.bottom = gr->border.bottom;
1548 output->border.left = gr->border.left;
1549 output->border.right = gr->border.right;
1550}
1551
1552WL_EXPORT void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001553gl_renderer_set_border(struct weston_compositor *ec, int32_t width, int32_t height, void *data,
John Kåre Alsaker44154502012-11-13 19:10:20 +01001554 int32_t *edges)
1555{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001556 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker44154502012-11-13 19:10:20 +01001557 struct weston_output *output;
1558
1559 gr->border.left = edges[0];
1560 gr->border.right = edges[1];
1561 gr->border.top = edges[2];
1562 gr->border.bottom = edges[3];
1563
1564 gr->border.width = width;
1565 gr->border.height = height;
1566
1567 glGenTextures(1, &gr->border.texture);
1568 glBindTexture(GL_TEXTURE_2D, gr->border.texture);
1569 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1570 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1571 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1572 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1573
1574 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1575 width,
1576 height,
1577 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1578 data);
1579
1580 wl_list_for_each(output, &ec->output_list, link)
1581 output_apply_border(output, gr);
1582}
1583
John Kåre Alsaker94659272012-11-13 19:10:18 +01001584static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001585gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001586
1587WL_EXPORT int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001588gl_renderer_output_create(struct weston_output *output,
John Kåre Alsaker94659272012-11-13 19:10:18 +01001589 EGLNativeWindowType window)
1590{
1591 struct weston_compositor *ec = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001592 struct gl_renderer *gr = get_renderer(ec);
1593 struct gl_output_state *go = calloc(1, sizeof *go);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001594 int i;
John Kåre Alsaker94659272012-11-13 19:10:18 +01001595
1596 if (!go)
1597 return -1;
1598
1599 go->egl_surface =
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001600 eglCreateWindowSurface(gr->egl_display,
1601 gr->egl_config,
John Kåre Alsaker94659272012-11-13 19:10:18 +01001602 window, NULL);
1603
1604 if (go->egl_surface == EGL_NO_SURFACE) {
1605 weston_log("failed to create egl surface\n");
1606 free(go);
1607 return -1;
1608 }
1609
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001610 if (gr->egl_context == NULL)
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001611 if (gl_renderer_setup(ec, go->egl_surface) < 0) {
John Kåre Alsaker94659272012-11-13 19:10:18 +01001612 free(go);
1613 return -1;
1614 }
1615
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001616 go->current_buffer = 0;
1617 for (i = 0; i < 2; i++)
1618 pixman_region32_init(&go->buffer_damage[i]);
1619
John Kåre Alsaker94659272012-11-13 19:10:18 +01001620 output->renderer_state = go;
1621
John Kåre Alsaker44154502012-11-13 19:10:20 +01001622 output_apply_border(output, gr);
1623
John Kåre Alsaker94659272012-11-13 19:10:18 +01001624 return 0;
1625}
1626
1627WL_EXPORT void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001628gl_renderer_output_destroy(struct weston_output *output)
John Kåre Alsaker94659272012-11-13 19:10:18 +01001629{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001630 struct gl_renderer *gr = get_renderer(output->compositor);
1631 struct gl_output_state *go = get_output_state(output);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001632 int i;
1633
1634 for (i = 0; i < 2; i++)
1635 pixman_region32_fini(&go->buffer_damage[i]);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001636
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001637 eglDestroySurface(gr->egl_display, go->egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001638
1639 free(go);
1640}
1641
1642WL_EXPORT EGLSurface
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001643gl_renderer_output_surface(struct weston_output *output)
John Kåre Alsaker94659272012-11-13 19:10:18 +01001644{
1645 return get_output_state(output)->egl_surface;
1646}
1647
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001648WL_EXPORT void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001649gl_renderer_destroy(struct weston_compositor *ec)
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001650{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001651 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001652
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001653 if (gr->has_bind_display)
1654 gr->unbind_display(gr->egl_display, ec->wl_display);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001655
1656 /* Work around crash in egl_dri2.c's dri2_make_current() - when does this apply? */
1657 eglMakeCurrent(gr->egl_display,
1658 EGL_NO_SURFACE, EGL_NO_SURFACE,
1659 EGL_NO_CONTEXT);
1660
1661 eglTerminate(gr->egl_display);
1662 eglReleaseThread();
1663}
1664
1665static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001666egl_choose_config(struct gl_renderer *gr, const EGLint *attribs,
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001667 const EGLint *visual_id)
1668{
1669 EGLint count = 0;
1670 EGLint matched = 0;
1671 EGLConfig *configs;
1672 int i;
1673
1674 if (!eglGetConfigs(gr->egl_display, NULL, 0, &count) || count < 1)
1675 return -1;
1676
1677 configs = calloc(count, sizeof *configs);
1678 if (!configs)
1679 return -1;
1680
1681 if (!eglChooseConfig(gr->egl_display, attribs, configs,
1682 count, &matched))
1683 goto out;
1684
1685 for (i = 0; i < matched; ++i) {
1686 EGLint id;
1687
1688 if (visual_id) {
1689 if (!eglGetConfigAttrib(gr->egl_display,
1690 configs[i], EGL_NATIVE_VISUAL_ID,
1691 &id))
1692 continue;
1693
1694 if (id != *visual_id)
1695 continue;
1696 }
1697
1698 gr->egl_config = configs[i];
1699
1700 free(configs);
1701 return 0;
1702 }
1703
1704out:
1705 free(configs);
1706 return -1;
1707}
1708
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001709WL_EXPORT const EGLint gl_renderer_opaque_attribs[] = {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001710 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1711 EGL_RED_SIZE, 1,
1712 EGL_GREEN_SIZE, 1,
1713 EGL_BLUE_SIZE, 1,
1714 EGL_ALPHA_SIZE, 0,
1715 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1716 EGL_NONE
1717};
1718
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001719WL_EXPORT const EGLint gl_renderer_alpha_attribs[] = {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001720 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1721 EGL_RED_SIZE, 1,
1722 EGL_GREEN_SIZE, 1,
1723 EGL_BLUE_SIZE, 1,
1724 EGL_ALPHA_SIZE, 1,
1725 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1726 EGL_NONE
1727};
1728
1729WL_EXPORT int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001730gl_renderer_create(struct weston_compositor *ec, EGLNativeDisplayType display,
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001731 const EGLint *attribs, const EGLint *visual_id)
1732{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001733 struct gl_renderer *gr;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001734 EGLint major, minor;
1735
1736 gr = calloc(1, sizeof *gr);
1737
1738 if (gr == NULL)
1739 return -1;
1740
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001741 gr->base.read_pixels = gl_renderer_read_pixels;
1742 gr->base.repaint_output = gl_renderer_repaint_output;
1743 gr->base.flush_damage = gl_renderer_flush_damage;
1744 gr->base.attach = gl_renderer_attach;
1745 gr->base.create_surface = gl_renderer_create_surface;
1746 gr->base.surface_set_color = gl_renderer_surface_set_color;
1747 gr->base.destroy_surface = gl_renderer_destroy_surface;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001748
1749 gr->egl_display = eglGetDisplay(display);
1750 if (gr->egl_display == EGL_NO_DISPLAY) {
1751 weston_log("failed to create display\n");
1752 goto err_egl;
1753 }
1754
1755 if (!eglInitialize(gr->egl_display, &major, &minor)) {
1756 weston_log("failed to initialize display\n");
1757 goto err_egl;
1758 }
1759
1760 if (egl_choose_config(gr, attribs, visual_id) < 0) {
1761 weston_log("failed to choose EGL config\n");
1762 goto err_egl;
1763 }
1764
1765 ec->renderer = &gr->base;
1766
1767 return 0;
1768
1769err_egl:
Pekka Paalanen326529f2012-11-27 12:25:25 +02001770 gl_renderer_print_egl_error_state();
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001771 free(gr);
1772 return -1;
1773}
1774
1775WL_EXPORT EGLDisplay
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001776gl_renderer_display(struct weston_compositor *ec)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001777{
1778 return get_renderer(ec)->egl_display;
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001779}
1780
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001781static int
1782compile_shaders(struct weston_compositor *ec)
1783{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001784 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker40684142012-11-13 19:10:25 +01001785
1786 if (shader_init(&gr->texture_shader_rgba, ec,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001787 vertex_shader, texture_fragment_shader_rgba) < 0)
1788 return -1;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001789 if (shader_init(&gr->texture_shader_rgbx, ec,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001790 vertex_shader, texture_fragment_shader_rgbx) < 0)
1791 return -1;
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001792 if (gr->has_egl_image_external &&
John Kåre Alsaker40684142012-11-13 19:10:25 +01001793 shader_init(&gr->texture_shader_egl_external, ec,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001794 vertex_shader, texture_fragment_shader_egl_external) < 0)
1795 return -1;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001796 if (shader_init(&gr->texture_shader_y_uv, ec,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001797 vertex_shader, texture_fragment_shader_y_uv) < 0)
1798 return -1;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001799 if (shader_init(&gr->texture_shader_y_u_v, ec,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001800 vertex_shader, texture_fragment_shader_y_u_v) < 0)
1801 return -1;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001802 if (shader_init(&gr->texture_shader_y_xuxv, ec,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001803 vertex_shader, texture_fragment_shader_y_xuxv) < 0)
1804 return -1;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001805 if (shader_init(&gr->solid_shader, ec,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001806 vertex_shader, solid_fragment_shader) < 0)
1807 return -1;
1808
1809 return 0;
1810}
1811
1812static void
1813fragment_debug_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
1814 void *data)
1815{
1816 struct weston_compositor *ec = data;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001817 struct gl_renderer *gr = get_renderer(ec);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001818 struct weston_output *output;
1819
John Kåre Alsaker40684142012-11-13 19:10:25 +01001820 gr->fragment_shader_debug ^= 1;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001821
John Kåre Alsaker40684142012-11-13 19:10:25 +01001822 shader_release(&gr->texture_shader_rgba);
1823 shader_release(&gr->texture_shader_rgbx);
1824 shader_release(&gr->texture_shader_egl_external);
1825 shader_release(&gr->texture_shader_y_uv);
1826 shader_release(&gr->texture_shader_y_u_v);
1827 shader_release(&gr->texture_shader_y_xuxv);
1828 shader_release(&gr->solid_shader);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001829
1830 compile_shaders(ec);
1831
Ander Conselvan de Oliveira03fb4ef2012-12-03 17:08:11 +02001832 /* Force use_shader() to call glUseProgram(), since we need to use
1833 * the recompiled version of the shader. */
1834 gr->current_shader = NULL;
1835
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001836 wl_list_for_each(output, &ec->output_list, link)
1837 weston_output_damage(output);
1838}
1839
John Kåre Alsaker94659272012-11-13 19:10:18 +01001840static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001841gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001842{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001843 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001844 const char *extensions;
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001845 EGLBoolean ret;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001846
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001847 static const EGLint context_attribs[] = {
1848 EGL_CONTEXT_CLIENT_VERSION, 2,
1849 EGL_NONE
1850 };
1851
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001852 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
1853 weston_log("failed to bind EGL_OPENGL_ES_API\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02001854 gl_renderer_print_egl_error_state();
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001855 return -1;
1856 }
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001857
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001858 log_egl_config_info(gr->egl_display, gr->egl_config);
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001859
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001860 gr->egl_context = eglCreateContext(gr->egl_display, gr->egl_config,
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001861 EGL_NO_CONTEXT, context_attribs);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001862 if (gr->egl_context == NULL) {
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001863 weston_log("failed to create context\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02001864 gl_renderer_print_egl_error_state();
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001865 return -1;
1866 }
1867
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001868 ret = eglMakeCurrent(gr->egl_display, egl_surface,
1869 egl_surface, gr->egl_context);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001870 if (ret == EGL_FALSE) {
1871 weston_log("Failed to make EGL context current.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02001872 gl_renderer_print_egl_error_state();
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001873 return -1;
1874 }
1875
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001876 log_egl_gl_info(gr->egl_display);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001877
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001878 gr->image_target_texture_2d =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001879 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001880 gr->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1881 gr->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1882 gr->bind_display =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001883 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001884 gr->unbind_display =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001885 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001886 gr->query_buffer =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001887 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
1888
1889 extensions = (const char *) glGetString(GL_EXTENSIONS);
1890 if (!extensions) {
1891 weston_log("Retrieving GL extension string failed.\n");
1892 return -1;
1893 }
1894
1895 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
1896 weston_log("GL_EXT_texture_format_BGRA8888 not available\n");
1897 return -1;
1898 }
1899
1900 if (strstr(extensions, "GL_EXT_read_format_bgra"))
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01001901 ec->read_format = PIXMAN_a8r8g8b8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001902 else
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01001903 ec->read_format = PIXMAN_a8b8g8r8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001904
1905 if (strstr(extensions, "GL_EXT_unpack_subimage"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001906 gr->has_unpack_subimage = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001907
1908 if (strstr(extensions, "GL_OES_EGL_image_external"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001909 gr->has_egl_image_external = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001910
1911 extensions =
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001912 (const char *) eglQueryString(gr->egl_display, EGL_EXTENSIONS);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001913 if (!extensions) {
1914 weston_log("Retrieving EGL extension string failed.\n");
1915 return -1;
1916 }
1917
1918 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001919 gr->has_bind_display = 1;
1920 if (gr->has_bind_display) {
1921 ret = gr->bind_display(gr->egl_display, ec->wl_display);
Pekka Paalanen035a0322012-10-24 09:43:06 +03001922 if (!ret)
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001923 gr->has_bind_display = 0;
Pekka Paalanen035a0322012-10-24 09:43:06 +03001924 }
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001925
1926 glActiveTexture(GL_TEXTURE0);
1927
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001928 if (compile_shaders(ec))
1929 return -1;
1930
1931 weston_compositor_add_debug_binding(ec, KEY_S,
1932 fragment_debug_binding, ec);
1933
Pekka Paalanen035a0322012-10-24 09:43:06 +03001934 weston_log("GL ES 2 renderer features:\n");
1935 weston_log_continue(STAMP_SPACE "read-back format: %s\n",
1936 ec->read_format == GL_BGRA_EXT ? "BGRA" : "RGBA");
1937 weston_log_continue(STAMP_SPACE "wl_shm sub-image to texture: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001938 gr->has_unpack_subimage ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03001939 weston_log_continue(STAMP_SPACE "EGL Wayland extension: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001940 gr->has_bind_display ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03001941
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001942
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001943 return 0;
1944}