blob: 8da111e98b5529c8f73859fd8f3ea0182d2b2897 [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;
Pekka Paalanen81ee3f52012-12-04 15:58:16 +020061 pixman_region32_t texture_damage;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +010062
63 EGLImageKHR images[3];
64 GLenum target;
65 int num_images;
Pekka Paalanenfb003d32012-12-04 15:58:13 +020066
67 struct weston_buffer_reference buffer_ref;
Pekka Paalanen68033ac2012-12-04 15:58:15 +020068 int pitch; /* in pixels */
John Kåre Alsaker878f4492012-11-13 19:10:23 +010069};
70
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010071struct gl_renderer {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +010072 struct weston_renderer base;
73 int fragment_shader_debug;
74
75 EGLDisplay egl_display;
76 EGLContext egl_context;
77 EGLConfig egl_config;
John Kåre Alsaker44154502012-11-13 19:10:20 +010078
79 struct {
80 int32_t top, bottom, left, right;
81 GLuint texture;
82 int32_t width, height;
83 } border;
John Kåre Alsaker40684142012-11-13 19:10:25 +010084
John Kåre Alsaker320711d2012-11-13 19:10:27 +010085 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
86 PFNEGLCREATEIMAGEKHRPROC create_image;
87 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
88
89 int has_unpack_subimage;
90
91 PFNEGLBINDWAYLANDDISPLAYWL bind_display;
92 PFNEGLUNBINDWAYLANDDISPLAYWL unbind_display;
93 PFNEGLQUERYWAYLANDBUFFERWL query_buffer;
94 int has_bind_display;
95
96 int has_egl_image_external;
97
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010098 struct gl_shader texture_shader_rgba;
99 struct gl_shader texture_shader_rgbx;
100 struct gl_shader texture_shader_egl_external;
101 struct gl_shader texture_shader_y_uv;
102 struct gl_shader texture_shader_y_u_v;
103 struct gl_shader texture_shader_y_xuxv;
104 struct gl_shader invert_color_shader;
105 struct gl_shader solid_shader;
106 struct gl_shader *current_shader;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100107};
John Kåre Alsaker94659272012-11-13 19:10:18 +0100108
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100109static inline struct gl_output_state *
John Kåre Alsaker94659272012-11-13 19:10:18 +0100110get_output_state(struct weston_output *output)
111{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100112 return (struct gl_output_state *)output->renderer_state;
John Kåre Alsaker94659272012-11-13 19:10:18 +0100113}
114
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100115static inline struct gl_surface_state *
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100116get_surface_state(struct weston_surface *surface)
117{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100118 return (struct gl_surface_state *)surface->renderer_state;
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100119}
120
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100121static inline struct gl_renderer *
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100122get_renderer(struct weston_compositor *ec)
123{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100124 return (struct gl_renderer *)ec->renderer;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100125}
126
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400127static const char *
128egl_error_string(EGLint code)
129{
130#define MYERRCODE(x) case x: return #x;
131 switch (code) {
132 MYERRCODE(EGL_SUCCESS)
133 MYERRCODE(EGL_NOT_INITIALIZED)
134 MYERRCODE(EGL_BAD_ACCESS)
135 MYERRCODE(EGL_BAD_ALLOC)
136 MYERRCODE(EGL_BAD_ATTRIBUTE)
137 MYERRCODE(EGL_BAD_CONTEXT)
138 MYERRCODE(EGL_BAD_CONFIG)
139 MYERRCODE(EGL_BAD_CURRENT_SURFACE)
140 MYERRCODE(EGL_BAD_DISPLAY)
141 MYERRCODE(EGL_BAD_SURFACE)
142 MYERRCODE(EGL_BAD_MATCH)
143 MYERRCODE(EGL_BAD_PARAMETER)
144 MYERRCODE(EGL_BAD_NATIVE_PIXMAP)
145 MYERRCODE(EGL_BAD_NATIVE_WINDOW)
146 MYERRCODE(EGL_CONTEXT_LOST)
147 default:
148 return "unknown";
149 }
150#undef MYERRCODE
151}
152
Pekka Paalanen326529f2012-11-27 12:25:25 +0200153WL_EXPORT void
154gl_renderer_print_egl_error_state(void)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400155{
156 EGLint code;
157
158 code = eglGetError();
159 weston_log("EGL error state: %s (0x%04lx)\n",
160 egl_error_string(code), (long)code);
161}
162
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300163struct polygon8 {
164 GLfloat x[8];
165 GLfloat y[8];
166 int n;
167};
168
169struct clip_context {
170 struct {
171 GLfloat x;
172 GLfloat y;
173 } prev;
174
175 struct {
176 GLfloat x1, y1;
177 GLfloat x2, y2;
178 } clip;
179
180 struct {
181 GLfloat *x;
182 GLfloat *y;
183 } vertices;
184};
185
186static GLfloat
187float_difference(GLfloat a, GLfloat b)
188{
189 /* http://www.altdevblogaday.com/2012/02/22/comparing-floating-point-numbers-2012-edition/ */
190 static const GLfloat max_diff = 4.0f * FLT_MIN;
191 static const GLfloat max_rel_diff = 4.0e-5;
192 GLfloat diff = a - b;
193 GLfloat adiff = fabsf(diff);
194
195 if (adiff <= max_diff)
196 return 0.0f;
197
198 a = fabsf(a);
199 b = fabsf(b);
200 if (adiff <= (a > b ? a : b) * max_rel_diff)
201 return 0.0f;
202
203 return diff;
204}
205
206/* A line segment (p1x, p1y)-(p2x, p2y) intersects the line x = x_arg.
207 * Compute the y coordinate of the intersection.
208 */
209static GLfloat
210clip_intersect_y(GLfloat p1x, GLfloat p1y, GLfloat p2x, GLfloat p2y,
211 GLfloat x_arg)
212{
213 GLfloat a;
214 GLfloat diff = float_difference(p1x, p2x);
215
216 /* Practically vertical line segment, yet the end points have already
217 * been determined to be on different sides of the line. Therefore
218 * the line segment is part of the line and intersects everywhere.
219 * Return the end point, so we use the whole line segment.
220 */
221 if (diff == 0.0f)
222 return p2y;
223
224 a = (x_arg - p2x) / diff;
225 return p2y + (p1y - p2y) * a;
226}
227
228/* A line segment (p1x, p1y)-(p2x, p2y) intersects the line y = y_arg.
229 * Compute the x coordinate of the intersection.
230 */
231static GLfloat
232clip_intersect_x(GLfloat p1x, GLfloat p1y, GLfloat p2x, GLfloat p2y,
233 GLfloat y_arg)
234{
235 GLfloat a;
236 GLfloat diff = float_difference(p1y, p2y);
237
238 /* Practically horizontal line segment, yet the end points have already
239 * been determined to be on different sides of the line. Therefore
240 * the line segment is part of the line and intersects everywhere.
241 * Return the end point, so we use the whole line segment.
242 */
243 if (diff == 0.0f)
244 return p2x;
245
246 a = (y_arg - p2y) / diff;
247 return p2x + (p1x - p2x) * a;
248}
249
250enum path_transition {
251 PATH_TRANSITION_OUT_TO_OUT = 0,
252 PATH_TRANSITION_OUT_TO_IN = 1,
253 PATH_TRANSITION_IN_TO_OUT = 2,
254 PATH_TRANSITION_IN_TO_IN = 3,
255};
256
257static void
258clip_append_vertex(struct clip_context *ctx, GLfloat x, GLfloat y)
259{
260 *ctx->vertices.x++ = x;
261 *ctx->vertices.y++ = y;
262}
263
264static enum path_transition
265path_transition_left_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
266{
267 return ((ctx->prev.x >= ctx->clip.x1) << 1) | (x >= ctx->clip.x1);
268}
269
270static enum path_transition
271path_transition_right_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
272{
273 return ((ctx->prev.x < ctx->clip.x2) << 1) | (x < ctx->clip.x2);
274}
275
276static enum path_transition
277path_transition_top_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
278{
279 return ((ctx->prev.y >= ctx->clip.y1) << 1) | (y >= ctx->clip.y1);
280}
281
282static enum path_transition
283path_transition_bottom_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
284{
285 return ((ctx->prev.y < ctx->clip.y2) << 1) | (y < ctx->clip.y2);
286}
287
288static void
289clip_polygon_leftright(struct clip_context *ctx,
290 enum path_transition transition,
291 GLfloat x, GLfloat y, GLfloat clip_x)
292{
293 GLfloat yi;
294
295 switch (transition) {
296 case PATH_TRANSITION_IN_TO_IN:
297 clip_append_vertex(ctx, x, y);
298 break;
299 case PATH_TRANSITION_IN_TO_OUT:
300 yi = clip_intersect_y(ctx->prev.x, ctx->prev.y, x, y, clip_x);
301 clip_append_vertex(ctx, clip_x, yi);
302 break;
303 case PATH_TRANSITION_OUT_TO_IN:
304 yi = clip_intersect_y(ctx->prev.x, ctx->prev.y, x, y, clip_x);
305 clip_append_vertex(ctx, clip_x, yi);
306 clip_append_vertex(ctx, x, y);
307 break;
308 case PATH_TRANSITION_OUT_TO_OUT:
309 /* nothing */
310 break;
311 default:
312 assert(0 && "bad enum path_transition");
313 }
314
315 ctx->prev.x = x;
316 ctx->prev.y = y;
317}
318
319static void
320clip_polygon_topbottom(struct clip_context *ctx,
321 enum path_transition transition,
322 GLfloat x, GLfloat y, GLfloat clip_y)
323{
324 GLfloat xi;
325
326 switch (transition) {
327 case PATH_TRANSITION_IN_TO_IN:
328 clip_append_vertex(ctx, x, y);
329 break;
330 case PATH_TRANSITION_IN_TO_OUT:
331 xi = clip_intersect_x(ctx->prev.x, ctx->prev.y, x, y, clip_y);
332 clip_append_vertex(ctx, xi, clip_y);
333 break;
334 case PATH_TRANSITION_OUT_TO_IN:
335 xi = clip_intersect_x(ctx->prev.x, ctx->prev.y, x, y, clip_y);
336 clip_append_vertex(ctx, xi, clip_y);
337 clip_append_vertex(ctx, x, y);
338 break;
339 case PATH_TRANSITION_OUT_TO_OUT:
340 /* nothing */
341 break;
342 default:
343 assert(0 && "bad enum path_transition");
344 }
345
346 ctx->prev.x = x;
347 ctx->prev.y = y;
348}
349
350static void
351clip_context_prepare(struct clip_context *ctx, const struct polygon8 *src,
352 GLfloat *dst_x, GLfloat *dst_y)
353{
354 ctx->prev.x = src->x[src->n - 1];
355 ctx->prev.y = src->y[src->n - 1];
356 ctx->vertices.x = dst_x;
357 ctx->vertices.y = dst_y;
358}
359
360static int
361clip_polygon_left(struct clip_context *ctx, const struct polygon8 *src,
362 GLfloat *dst_x, GLfloat *dst_y)
363{
364 enum path_transition trans;
365 int i;
366
367 clip_context_prepare(ctx, src, dst_x, dst_y);
368 for (i = 0; i < src->n; i++) {
369 trans = path_transition_left_edge(ctx, src->x[i], src->y[i]);
370 clip_polygon_leftright(ctx, trans, src->x[i], src->y[i],
371 ctx->clip.x1);
372 }
373 return ctx->vertices.x - dst_x;
374}
375
376static int
377clip_polygon_right(struct clip_context *ctx, const struct polygon8 *src,
378 GLfloat *dst_x, GLfloat *dst_y)
379{
380 enum path_transition trans;
381 int i;
382
383 clip_context_prepare(ctx, src, dst_x, dst_y);
384 for (i = 0; i < src->n; i++) {
385 trans = path_transition_right_edge(ctx, src->x[i], src->y[i]);
386 clip_polygon_leftright(ctx, trans, src->x[i], src->y[i],
387 ctx->clip.x2);
388 }
389 return ctx->vertices.x - dst_x;
390}
391
392static int
393clip_polygon_top(struct clip_context *ctx, const struct polygon8 *src,
394 GLfloat *dst_x, GLfloat *dst_y)
395{
396 enum path_transition trans;
397 int i;
398
399 clip_context_prepare(ctx, src, dst_x, dst_y);
400 for (i = 0; i < src->n; i++) {
401 trans = path_transition_top_edge(ctx, src->x[i], src->y[i]);
402 clip_polygon_topbottom(ctx, trans, src->x[i], src->y[i],
403 ctx->clip.y1);
404 }
405 return ctx->vertices.x - dst_x;
406}
407
408static int
409clip_polygon_bottom(struct clip_context *ctx, const struct polygon8 *src,
410 GLfloat *dst_x, GLfloat *dst_y)
411{
412 enum path_transition trans;
413 int i;
414
415 clip_context_prepare(ctx, src, dst_x, dst_y);
416 for (i = 0; i < src->n; i++) {
417 trans = path_transition_bottom_edge(ctx, src->x[i], src->y[i]);
418 clip_polygon_topbottom(ctx, trans, src->x[i], src->y[i],
419 ctx->clip.y2);
420 }
421 return ctx->vertices.x - dst_x;
422}
423
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400424#define max(a, b) (((a) > (b)) ? (a) : (b))
425#define min(a, b) (((a) > (b)) ? (b) : (a))
426#define clip(x, a, b) min(max(x, a), b)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400427
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300428/*
429 * Compute the boundary vertices of the intersection of the global coordinate
430 * aligned rectangle 'rect', and an arbitrary quadrilateral produced from
431 * 'surf_rect' when transformed from surface coordinates into global coordinates.
432 * The vertices are written to 'ex' and 'ey', and the return value is the
433 * number of vertices. Vertices are produced in clockwise winding order.
434 * Guarantees to produce either zero vertices, or 3-8 vertices with non-zero
435 * polygon area.
436 */
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400437static int
438calculate_edges(struct weston_surface *es, pixman_box32_t *rect,
439 pixman_box32_t *surf_rect, GLfloat *ex, GLfloat *ey)
440{
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300441 struct polygon8 polygon;
442 struct clip_context ctx;
443 int i, n;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400444 GLfloat min_x, max_x, min_y, max_y;
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300445 struct polygon8 surf = {
446 { surf_rect->x1, surf_rect->x2, surf_rect->x2, surf_rect->x1 },
447 { surf_rect->y1, surf_rect->y1, surf_rect->y2, surf_rect->y2 },
448 4
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400449 };
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400450
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300451 ctx.clip.x1 = rect->x1;
452 ctx.clip.y1 = rect->y1;
453 ctx.clip.x2 = rect->x2;
454 ctx.clip.y2 = rect->y2;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400455
456 /* transform surface to screen space: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300457 for (i = 0; i < surf.n; i++)
458 weston_surface_to_global_float(es, surf.x[i], surf.y[i],
459 &surf.x[i], &surf.y[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400460
461 /* find bounding box: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300462 min_x = max_x = surf.x[0];
463 min_y = max_y = surf.y[0];
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400464
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300465 for (i = 1; i < surf.n; i++) {
466 min_x = min(min_x, surf.x[i]);
467 max_x = max(max_x, surf.x[i]);
468 min_y = min(min_y, surf.y[i]);
469 max_y = max(max_y, surf.y[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400470 }
471
472 /* First, simple bounding box check to discard early transformed
473 * surface rects that do not intersect with the clip region:
474 */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300475 if ((min_x >= ctx.clip.x2) || (max_x <= ctx.clip.x1) ||
476 (min_y >= ctx.clip.y2) || (max_y <= ctx.clip.y1))
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400477 return 0;
478
479 /* Simple case, bounding box edges are parallel to surface edges,
480 * there will be only four edges. We just need to clip the surface
481 * vertices to the clip rect bounds:
482 */
483 if (!es->transform.enabled) {
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300484 for (i = 0; i < surf.n; i++) {
485 ex[i] = clip(surf.x[i], ctx.clip.x1, ctx.clip.x2);
486 ey[i] = clip(surf.y[i], ctx.clip.y1, ctx.clip.y2);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400487 }
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300488 return surf.n;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400489 }
490
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300491 /* Transformed case: use a general polygon clipping algorithm to
492 * clip the surface rectangle with each side of 'rect'.
493 * The algorithm is Sutherland-Hodgman, as explained in
494 * http://www.codeguru.com/cpp/misc/misc/graphics/article.php/c8965/Polygon-Clipping.htm
495 * but without looking at any of that code.
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400496 */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300497 polygon.n = clip_polygon_left(&ctx, &surf, polygon.x, polygon.y);
498 surf.n = clip_polygon_right(&ctx, &polygon, surf.x, surf.y);
499 polygon.n = clip_polygon_top(&ctx, &surf, polygon.x, polygon.y);
500 surf.n = clip_polygon_bottom(&ctx, &polygon, surf.x, surf.y);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400501
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300502 /* Get rid of duplicate vertices */
503 ex[0] = surf.x[0];
504 ey[0] = surf.y[0];
505 n = 1;
506 for (i = 1; i < surf.n; i++) {
507 if (float_difference(ex[n - 1], surf.x[i]) == 0.0f &&
508 float_difference(ey[n - 1], surf.y[i]) == 0.0f)
509 continue;
510 ex[n] = surf.x[i];
511 ey[n] = surf.y[i];
512 n++;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400513 }
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300514 if (float_difference(ex[n - 1], surf.x[0]) == 0.0f &&
515 float_difference(ey[n - 1], surf.y[0]) == 0.0f)
516 n--;
517
518 if (n < 3)
519 return 0;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400520
521 return n;
522}
523
524static int
525texture_region(struct weston_surface *es, pixman_region32_t *region,
526 pixman_region32_t *surf_region)
527{
Pekka Paalanen68033ac2012-12-04 15:58:15 +0200528 struct gl_surface_state *gs = get_surface_state(es);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400529 struct weston_compositor *ec = es->compositor;
530 GLfloat *v, inv_width, inv_height;
531 unsigned int *vtxcnt, nvtx = 0;
532 pixman_box32_t *rects, *surf_rects;
533 int i, j, k, nrects, nsurf;
534
535 rects = pixman_region32_rectangles(region, &nrects);
536 surf_rects = pixman_region32_rectangles(surf_region, &nsurf);
537
538 /* worst case we can have 8 vertices per rect (ie. clipped into
539 * an octagon):
540 */
541 v = wl_array_add(&ec->vertices, nrects * nsurf * 8 * 4 * sizeof *v);
542 vtxcnt = wl_array_add(&ec->vtxcnt, nrects * nsurf * sizeof *vtxcnt);
543
Pekka Paalanen68033ac2012-12-04 15:58:15 +0200544 inv_width = 1.0 / gs->pitch;
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200545
546 switch (es->buffer_transform) {
547 case WL_OUTPUT_TRANSFORM_90:
548 case WL_OUTPUT_TRANSFORM_270:
549 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
550 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
551 inv_height = 1.0 / es->geometry.width;
552 break;
553 default:
554 inv_height = 1.0 / es->geometry.height;
555 }
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400556
557 for (i = 0; i < nrects; i++) {
558 pixman_box32_t *rect = &rects[i];
559 for (j = 0; j < nsurf; j++) {
560 pixman_box32_t *surf_rect = &surf_rects[j];
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200561 GLfloat sx, sy, bx, by;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400562 GLfloat ex[8], ey[8]; /* edge points in screen space */
563 int n;
564
565 /* The transformed surface, after clipping to the clip region,
566 * can have as many as eight sides, emitted as a triangle-fan.
567 * The first vertex in the triangle fan can be chosen arbitrarily,
568 * since the area is guaranteed to be convex.
569 *
570 * If a corner of the transformed surface falls outside of the
571 * clip region, instead of emitting one vertex for the corner
572 * of the surface, up to two are emitted for two corresponding
573 * intersection point(s) between the surface and the clip region.
574 *
575 * To do this, we first calculate the (up to eight) points that
576 * form the intersection of the clip rect and the transformed
577 * surface.
578 */
579 n = calculate_edges(es, rect, surf_rect, ex, ey);
580 if (n < 3)
581 continue;
582
583 /* emit edge points: */
584 for (k = 0; k < n; k++) {
585 weston_surface_from_global_float(es, ex[k], ey[k], &sx, &sy);
586 /* position: */
587 *(v++) = ex[k];
588 *(v++) = ey[k];
589 /* texcoord: */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200590 weston_surface_to_buffer_float(es, sx, sy,
591 &bx, &by);
592 *(v++) = bx * inv_width;
593 *(v++) = by * inv_height;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400594 }
595
596 vtxcnt[nvtx++] = n;
597 }
598 }
599
600 return nvtx;
601}
602
603static void
604triangle_fan_debug(struct weston_surface *surface, int first, int count)
605{
606 struct weston_compositor *compositor = surface->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100607 struct gl_renderer *gr = get_renderer(compositor);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400608 int i;
609 GLushort *buffer;
610 GLushort *index;
611 int nelems;
612 static int color_idx = 0;
613 static const GLfloat color[][4] = {
614 { 1.0, 0.0, 0.0, 1.0 },
615 { 0.0, 1.0, 0.0, 1.0 },
616 { 0.0, 0.0, 1.0, 1.0 },
617 { 1.0, 1.0, 1.0, 1.0 },
618 };
619
620 nelems = (count - 1 + count - 2) * 2;
621
622 buffer = malloc(sizeof(GLushort) * nelems);
623 index = buffer;
624
625 for (i = 1; i < count; i++) {
626 *index++ = first;
627 *index++ = first + i;
628 }
629
630 for (i = 2; i < count; i++) {
631 *index++ = first + i - 1;
632 *index++ = first + i;
633 }
634
John Kåre Alsaker40684142012-11-13 19:10:25 +0100635 glUseProgram(gr->solid_shader.program);
636 glUniform4fv(gr->solid_shader.color_uniform, 1,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400637 color[color_idx++ % ARRAY_LENGTH(color)]);
638 glDrawElements(GL_LINES, nelems, GL_UNSIGNED_SHORT, buffer);
John Kåre Alsaker40684142012-11-13 19:10:25 +0100639 glUseProgram(gr->current_shader->program);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400640 free(buffer);
641}
642
643static void
644repaint_region(struct weston_surface *es, pixman_region32_t *region,
645 pixman_region32_t *surf_region)
646{
647 struct weston_compositor *ec = es->compositor;
648 GLfloat *v;
649 unsigned int *vtxcnt;
650 int i, first, nfans;
651
652 /* The final region to be painted is the intersection of
653 * 'region' and 'surf_region'. However, 'region' is in the global
654 * coordinates, and 'surf_region' is in the surface-local
655 * coordinates. texture_region() will iterate over all pairs of
656 * rectangles from both regions, compute the intersection
657 * polygon for each pair, and store it as a triangle fan if
658 * it has a non-zero area (at least 3 vertices, actually).
659 */
660 nfans = texture_region(es, region, surf_region);
661
662 v = ec->vertices.data;
663 vtxcnt = ec->vtxcnt.data;
664
665 /* position: */
666 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
667 glEnableVertexAttribArray(0);
668
669 /* texcoord: */
670 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
671 glEnableVertexAttribArray(1);
672
673 for (i = 0, first = 0; i < nfans; i++) {
674 glDrawArrays(GL_TRIANGLE_FAN, first, vtxcnt[i]);
675 if (ec->fan_debug)
676 triangle_fan_debug(es, first, vtxcnt[i]);
677 first += vtxcnt[i];
678 }
679
680 glDisableVertexAttribArray(1);
681 glDisableVertexAttribArray(0);
682
683 ec->vertices.size = 0;
684 ec->vtxcnt.size = 0;
685}
686
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100687static int
688use_output(struct weston_output *output)
689{
690 static int errored;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100691 struct gl_output_state *go = get_output_state(output);
692 struct gl_renderer *gr = get_renderer(output->compositor);
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100693 EGLBoolean ret;
694
695 ret = eglMakeCurrent(gr->egl_display, go->egl_surface,
696 go->egl_surface, gr->egl_context);
697
698 if (ret == EGL_FALSE) {
699 if (errored)
700 return -1;
701 errored = 1;
702 weston_log("Failed to make EGL context current.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +0200703 gl_renderer_print_egl_error_state();
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100704 return -1;
705 }
706
707 return 0;
708}
709
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400710static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100711use_shader(struct gl_renderer *gr,
712 struct gl_shader *shader)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400713{
John Kåre Alsaker40684142012-11-13 19:10:25 +0100714 if (gr->current_shader == shader)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400715 return;
716
717 glUseProgram(shader->program);
John Kåre Alsaker40684142012-11-13 19:10:25 +0100718 gr->current_shader = shader;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400719}
720
721static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100722shader_uniforms(struct gl_shader *shader,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400723 struct weston_surface *surface,
724 struct weston_output *output)
725{
726 int i;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100727 struct gl_surface_state *gs = get_surface_state(surface);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400728
729 glUniformMatrix4fv(shader->proj_uniform,
730 1, GL_FALSE, output->matrix.d);
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100731 glUniform4fv(shader->color_uniform, 1, gs->color);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400732 glUniform1f(shader->alpha_uniform, surface->alpha);
733
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100734 for (i = 0; i < gs->num_textures; i++)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400735 glUniform1i(shader->tex_uniforms[i], i);
736}
737
738static void
739draw_surface(struct weston_surface *es, struct weston_output *output,
740 pixman_region32_t *damage) /* in global coordinates */
741{
742 struct weston_compositor *ec = es->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100743 struct gl_renderer *gr = get_renderer(ec);
744 struct gl_surface_state *gs = get_surface_state(es);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +0200745 struct gl_output_state *go = get_output_state(output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400746 /* repaint bounding region in global coordinates: */
747 pixman_region32_t repaint;
748 /* non-opaque region in surface coordinates: */
749 pixman_region32_t surface_blend;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300750 pixman_region32_t *buffer_damage;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400751 GLint filter;
752 int i;
753
754 pixman_region32_init(&repaint);
755 pixman_region32_intersect(&repaint,
756 &es->transform.boundingbox, damage);
757 pixman_region32_subtract(&repaint, &repaint, &es->clip);
758
759 if (!pixman_region32_not_empty(&repaint))
760 goto out;
761
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +0200762 buffer_damage = &go->buffer_damage[go->current_buffer];
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300763 pixman_region32_subtract(buffer_damage, buffer_damage, &repaint);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400764
765 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
766
767 if (ec->fan_debug) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100768 use_shader(gr, &gr->solid_shader);
769 shader_uniforms(&gr->solid_shader, es, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400770 }
771
John Kåre Alsaker40684142012-11-13 19:10:25 +0100772 use_shader(gr, gs->shader);
773 shader_uniforms(gs->shader, es, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400774
775 if (es->transform.enabled || output->zoom.active)
776 filter = GL_LINEAR;
777 else
778 filter = GL_NEAREST;
779
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100780 for (i = 0; i < gs->num_textures; i++) {
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400781 glActiveTexture(GL_TEXTURE0 + i);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100782 glBindTexture(gs->target, gs->textures[i]);
783 glTexParameteri(gs->target, GL_TEXTURE_MIN_FILTER, filter);
784 glTexParameteri(gs->target, GL_TEXTURE_MAG_FILTER, filter);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400785 }
786
787 /* blended region is whole surface minus opaque region: */
788 pixman_region32_init_rect(&surface_blend, 0, 0,
789 es->geometry.width, es->geometry.height);
790 pixman_region32_subtract(&surface_blend, &surface_blend, &es->opaque);
791
792 if (pixman_region32_not_empty(&es->opaque)) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100793 if (gs->shader == &gr->texture_shader_rgba) {
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400794 /* Special case for RGBA textures with possibly
795 * bad data in alpha channel: use the shader
796 * that forces texture alpha = 1.0.
797 * Xwayland surfaces need this.
798 */
John Kåre Alsaker40684142012-11-13 19:10:25 +0100799 use_shader(gr, &gr->texture_shader_rgbx);
800 shader_uniforms(&gr->texture_shader_rgbx, es, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400801 }
802
803 if (es->alpha < 1.0)
804 glEnable(GL_BLEND);
805 else
806 glDisable(GL_BLEND);
807
808 repaint_region(es, &repaint, &es->opaque);
809 }
810
811 if (pixman_region32_not_empty(&surface_blend)) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100812 use_shader(gr, gs->shader);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400813 glEnable(GL_BLEND);
814 repaint_region(es, &repaint, &surface_blend);
815 }
816
817 pixman_region32_fini(&surface_blend);
818
819out:
820 pixman_region32_fini(&repaint);
821}
822
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400823static void
824repaint_surfaces(struct weston_output *output, pixman_region32_t *damage)
825{
826 struct weston_compositor *compositor = output->compositor;
827 struct weston_surface *surface;
828
829 wl_list_for_each_reverse(surface, &compositor->surface_list, link)
830 if (surface->plane == &compositor->primary_plane)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400831 draw_surface(surface, output, damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400832}
833
John Kåre Alsaker44154502012-11-13 19:10:20 +0100834
835static int
836texture_border(struct weston_output *output)
837{
838 struct weston_compositor *ec = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100839 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker44154502012-11-13 19:10:20 +0100840 GLfloat *d;
841 unsigned int *p;
842 int i, j, k, n;
843 GLfloat x[4], y[4], u[4], v[4];
844
845 x[0] = -gr->border.left;
846 x[1] = 0;
847 x[2] = output->current->width;
848 x[3] = output->current->width + gr->border.right;
849
850 y[0] = -gr->border.top;
851 y[1] = 0;
852 y[2] = output->current->height;
853 y[3] = output->current->height + gr->border.bottom;
854
855 u[0] = 0.0;
856 u[1] = (GLfloat) gr->border.left / gr->border.width;
857 u[2] = (GLfloat) (gr->border.width - gr->border.right) / gr->border.width;
858 u[3] = 1.0;
859
860 v[0] = 0.0;
861 v[1] = (GLfloat) gr->border.top / gr->border.height;
862 v[2] = (GLfloat) (gr->border.height - gr->border.bottom) / gr->border.height;
863 v[3] = 1.0;
864
865 n = 8;
866 d = wl_array_add(&ec->vertices, n * 16 * sizeof *d);
867 p = wl_array_add(&ec->indices, n * 6 * sizeof *p);
868
869 k = 0;
870 for (i = 0; i < 3; i++)
871 for (j = 0; j < 3; j++) {
872
873 if (i == 1 && j == 1)
874 continue;
875
876 d[ 0] = x[i];
877 d[ 1] = y[j];
878 d[ 2] = u[i];
879 d[ 3] = v[j];
880
881 d[ 4] = x[i];
882 d[ 5] = y[j + 1];
883 d[ 6] = u[i];
884 d[ 7] = v[j + 1];
885
886 d[ 8] = x[i + 1];
887 d[ 9] = y[j];
888 d[10] = u[i + 1];
889 d[11] = v[j];
890
891 d[12] = x[i + 1];
892 d[13] = y[j + 1];
893 d[14] = u[i + 1];
894 d[15] = v[j + 1];
895
896 p[0] = k + 0;
897 p[1] = k + 1;
898 p[2] = k + 2;
899 p[3] = k + 2;
900 p[4] = k + 1;
901 p[5] = k + 3;
902
903 d += 16;
904 p += 6;
905 k += 4;
906 }
907
908 return k / 4;
909}
910
911static void
912draw_border(struct weston_output *output)
913{
914 struct weston_compositor *ec = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100915 struct gl_renderer *gr = get_renderer(ec);
916 struct gl_shader *shader = &gr->texture_shader_rgba;
John Kåre Alsaker44154502012-11-13 19:10:20 +0100917 GLfloat *v;
918 int n;
919
920 glDisable(GL_BLEND);
John Kåre Alsaker40684142012-11-13 19:10:25 +0100921 use_shader(gr, shader);
John Kåre Alsaker44154502012-11-13 19:10:20 +0100922
923 glUniformMatrix4fv(shader->proj_uniform,
924 1, GL_FALSE, output->matrix.d);
925
926 glUniform1i(shader->tex_uniforms[0], 0);
927 glUniform1f(shader->alpha_uniform, 1);
928
929 n = texture_border(output);
930
931 glActiveTexture(GL_TEXTURE0);
932 glBindTexture(GL_TEXTURE_2D, gr->border.texture);
933
934 v = ec->vertices.data;
935 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
936 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
937 glEnableVertexAttribArray(0);
938 glEnableVertexAttribArray(1);
939
940 glDrawElements(GL_TRIANGLES, n * 6,
941 GL_UNSIGNED_INT, ec->indices.data);
942
943 glDisableVertexAttribArray(1);
944 glDisableVertexAttribArray(0);
945
946 ec->vertices.size = 0;
947 ec->indices.size = 0;
948}
949
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400950static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100951gl_renderer_repaint_output(struct weston_output *output,
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400952 pixman_region32_t *output_damage)
953{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100954 struct gl_output_state *go = get_output_state(output);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400955 struct weston_compositor *compositor = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100956 struct gl_renderer *gr = get_renderer(compositor);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400957 EGLBoolean ret;
958 static int errored;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300959 int32_t width, height, i;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400960
961 width = output->current->width +
962 output->border.left + output->border.right;
963 height = output->current->height +
964 output->border.top + output->border.bottom;
965
966 glViewport(0, 0, width, height);
967
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100968 if (use_output(output) < 0)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400969 return;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400970
971 /* if debugging, redraw everything outside the damage to clean up
972 * debug lines from the previous draw on this buffer:
973 */
974 if (compositor->fan_debug) {
975 pixman_region32_t undamaged;
976 pixman_region32_init(&undamaged);
977 pixman_region32_subtract(&undamaged, &output->region,
978 output_damage);
979 compositor->fan_debug = 0;
980 repaint_surfaces(output, &undamaged);
981 compositor->fan_debug = 1;
982 pixman_region32_fini(&undamaged);
983 }
984
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300985 for (i = 0; i < 2; i++)
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +0200986 pixman_region32_union(&go->buffer_damage[i],
987 &go->buffer_damage[i],
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300988 output_damage);
989
990 pixman_region32_union(output_damage, output_damage,
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +0200991 &go->buffer_damage[go->current_buffer]);
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300992
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400993 repaint_surfaces(output, output_damage);
994
John Kåre Alsaker44154502012-11-13 19:10:20 +0100995 if (gr->border.texture)
996 draw_border(output);
997
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +0200998 pixman_region32_copy(&output->previous_damage, output_damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400999 wl_signal_emit(&output->frame_signal, output);
1000
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001001 ret = eglSwapBuffers(gr->egl_display, go->egl_surface);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001002 if (ret == EGL_FALSE && !errored) {
1003 errored = 1;
1004 weston_log("Failed in eglSwapBuffers.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02001005 gl_renderer_print_egl_error_state();
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001006 }
1007
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001008 go->current_buffer ^= 1;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03001009
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001010}
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001011
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001012static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001013gl_renderer_read_pixels(struct weston_output *output,
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001014 pixman_format_code_t format, void *pixels,
1015 uint32_t x, uint32_t y,
1016 uint32_t width, uint32_t height)
1017{
1018 GLenum gl_format;
1019
1020 switch (format) {
1021 case PIXMAN_a8r8g8b8:
1022 gl_format = GL_BGRA_EXT;
1023 break;
1024 case PIXMAN_a8b8g8r8:
1025 gl_format = GL_RGBA;
1026 break;
1027 default:
1028 return -1;
1029 }
1030
1031 if (use_output(output) < 0)
1032 return -1;
1033
1034 glPixelStorei(GL_PACK_ALIGNMENT, 1);
1035 glReadPixels(x, y, width, height, gl_format,
1036 GL_UNSIGNED_BYTE, pixels);
1037
1038 return 0;
1039}
1040
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001041static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001042gl_renderer_flush_damage(struct weston_surface *surface)
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001043{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001044 struct gl_renderer *gr = get_renderer(surface->compositor);
1045 struct gl_surface_state *gs = get_surface_state(surface);
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001046 struct wl_buffer *buffer = gs->buffer_ref.buffer;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001047
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001048#ifdef GL_UNPACK_ROW_LENGTH
1049 pixman_box32_t *rectangles;
1050 void *data;
1051 int i, n;
1052#endif
1053
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001054 pixman_region32_union(&gs->texture_damage,
1055 &gs->texture_damage, &surface->damage);
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001056
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001057 if (!buffer)
1058 return;
1059
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001060 /* Avoid upload, if the texture won't be used this time.
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001061 * We still accumulate the damage in texture_damage, and
1062 * hold the reference to the buffer, in case the surface
1063 * migrates back to the primary plane.
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001064 */
1065 if (surface->plane != &surface->compositor->primary_plane)
1066 return;
1067
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001068 if (!pixman_region32_not_empty(&gs->texture_damage))
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001069 goto done;
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001070
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001071 glBindTexture(GL_TEXTURE_2D, gs->textures[0]);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001072
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001073 if (!gr->has_unpack_subimage) {
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001074 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001075 gs->pitch, buffer->height, 0,
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001076 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
Pekka Paalanende685b82012-12-04 15:58:12 +02001077 wl_shm_buffer_get_data(buffer));
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001078
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001079 goto done;
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001080 }
1081
1082#ifdef GL_UNPACK_ROW_LENGTH
1083 /* Mesa does not define GL_EXT_unpack_subimage */
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001084 glPixelStorei(GL_UNPACK_ROW_LENGTH, gs->pitch);
Pekka Paalanende685b82012-12-04 15:58:12 +02001085 data = wl_shm_buffer_get_data(buffer);
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001086 rectangles = pixman_region32_rectangles(&gs->texture_damage, &n);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001087 for (i = 0; i < n; i++) {
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001088 pixman_box32_t r;
1089
1090 r = weston_surface_to_buffer_rect(surface, rectangles[i]);
1091
1092 glPixelStorei(GL_UNPACK_SKIP_PIXELS, r.x1);
1093 glPixelStorei(GL_UNPACK_SKIP_ROWS, r.y1);
1094 glTexSubImage2D(GL_TEXTURE_2D, 0, r.x1, r.y1,
1095 r.x2 - r.x1, r.y2 - r.y1,
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001096 GL_BGRA_EXT, GL_UNSIGNED_BYTE, data);
1097 }
1098#endif
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001099
1100done:
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001101 pixman_region32_fini(&gs->texture_damage);
1102 pixman_region32_init(&gs->texture_damage);
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001103
1104 weston_buffer_reference(&gs->buffer_ref, NULL);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001105}
1106
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001107static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001108ensure_textures(struct gl_surface_state *gs, int num_textures)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001109{
1110 int i;
1111
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001112 if (num_textures <= gs->num_textures)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001113 return;
1114
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001115 for (i = gs->num_textures; i < num_textures; i++) {
1116 glGenTextures(1, &gs->textures[i]);
1117 glBindTexture(gs->target, gs->textures[i]);
1118 glTexParameteri(gs->target,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001119 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001120 glTexParameteri(gs->target,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001121 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1122 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001123 gs->num_textures = num_textures;
1124 glBindTexture(gs->target, 0);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001125}
1126
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001127static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001128gl_renderer_attach(struct weston_surface *es, struct wl_buffer *buffer)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001129{
1130 struct weston_compositor *ec = es->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001131 struct gl_renderer *gr = get_renderer(ec);
1132 struct gl_surface_state *gs = get_surface_state(es);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001133 EGLint attribs[3], format;
1134 int i, num_planes;
1135
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001136 weston_buffer_reference(&gs->buffer_ref, buffer);
1137
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001138 if (!buffer) {
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001139 for (i = 0; i < gs->num_images; i++) {
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001140 gr->destroy_image(gr->egl_display, gs->images[i]);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001141 gs->images[i] = NULL;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001142 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001143 gs->num_images = 0;
1144 glDeleteTextures(gs->num_textures, gs->textures);
1145 gs->num_textures = 0;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001146 return;
1147 }
1148
1149 if (wl_buffer_is_shm(buffer)) {
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001150 gs->pitch = wl_shm_buffer_get_stride(buffer) / 4;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001151 gs->target = GL_TEXTURE_2D;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001152
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001153 ensure_textures(gs, 1);
1154 glBindTexture(GL_TEXTURE_2D, gs->textures[0]);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001155 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001156 gs->pitch, buffer->height, 0,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001157 GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
1158 if (wl_shm_buffer_get_format(buffer) == WL_SHM_FORMAT_XRGB8888)
John Kåre Alsaker40684142012-11-13 19:10:25 +01001159 gs->shader = &gr->texture_shader_rgbx;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001160 else
John Kåre Alsaker40684142012-11-13 19:10:25 +01001161 gs->shader = &gr->texture_shader_rgba;
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001162 } else if (gr->query_buffer(gr->egl_display, buffer,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001163 EGL_TEXTURE_FORMAT, &format)) {
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001164 for (i = 0; i < gs->num_images; i++)
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001165 gr->destroy_image(gr->egl_display, gs->images[i]);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001166 gs->num_images = 0;
1167 gs->target = GL_TEXTURE_2D;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001168 switch (format) {
1169 case EGL_TEXTURE_RGB:
1170 case EGL_TEXTURE_RGBA:
1171 default:
1172 num_planes = 1;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001173 gs->shader = &gr->texture_shader_rgba;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001174 break;
1175 case EGL_TEXTURE_EXTERNAL_WL:
1176 num_planes = 1;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001177 gs->target = GL_TEXTURE_EXTERNAL_OES;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001178 gs->shader = &gr->texture_shader_egl_external;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001179 break;
1180 case EGL_TEXTURE_Y_UV_WL:
1181 num_planes = 2;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001182 gs->shader = &gr->texture_shader_y_uv;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001183 break;
1184 case EGL_TEXTURE_Y_U_V_WL:
1185 num_planes = 3;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001186 gs->shader = &gr->texture_shader_y_u_v;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001187 break;
1188 case EGL_TEXTURE_Y_XUXV_WL:
1189 num_planes = 2;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001190 gs->shader = &gr->texture_shader_y_xuxv;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001191 break;
1192 }
1193
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001194 ensure_textures(gs, num_planes);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001195 for (i = 0; i < num_planes; i++) {
1196 attribs[0] = EGL_WAYLAND_PLANE_WL;
1197 attribs[1] = i;
1198 attribs[2] = EGL_NONE;
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001199 gs->images[i] = gr->create_image(gr->egl_display,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001200 NULL,
1201 EGL_WAYLAND_BUFFER_WL,
1202 buffer, attribs);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001203 if (!gs->images[i]) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001204 weston_log("failed to create img for plane %d\n", i);
1205 continue;
1206 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001207 gs->num_images++;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001208
1209 glActiveTexture(GL_TEXTURE0 + i);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001210 glBindTexture(gs->target, gs->textures[i]);
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001211 gr->image_target_texture_2d(gs->target,
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001212 gs->images[i]);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001213 }
1214
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001215 gs->pitch = buffer->width;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001216 } else {
1217 weston_log("unhandled buffer type!\n");
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001218 weston_buffer_reference(&gs->buffer_ref, NULL);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001219 }
1220}
1221
Kristian Høgsberg42263852012-09-06 21:59:29 -04001222static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001223gl_renderer_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001224 float red, float green, float blue, float alpha)
1225{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001226 struct gl_surface_state *gs = get_surface_state(surface);
1227 struct gl_renderer *gr = get_renderer(surface->compositor);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001228
1229 gs->color[0] = red;
1230 gs->color[1] = green;
1231 gs->color[2] = blue;
1232 gs->color[3] = alpha;
1233
John Kåre Alsaker40684142012-11-13 19:10:25 +01001234 gs->shader = &gr->solid_shader;
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001235}
1236
1237static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001238gl_renderer_create_surface(struct weston_surface *surface)
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001239{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001240 struct gl_surface_state *gs;
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001241
1242 gs = calloc(1, sizeof *gs);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001243 if (!gs)
1244 return -1;
1245
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001246 /* A buffer is never attached to solid color surfaces, yet
1247 * they still go through texcoord computations. Do not divide
1248 * by zero there.
1249 */
1250 gs->pitch = 1;
1251
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001252 pixman_region32_init(&gs->texture_damage);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001253 surface->renderer_state = gs;
1254
1255 return 0;
1256}
1257
1258static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001259gl_renderer_destroy_surface(struct weston_surface *surface)
Kristian Høgsberg42263852012-09-06 21:59:29 -04001260{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001261 struct gl_surface_state *gs = get_surface_state(surface);
1262 struct gl_renderer *gr = get_renderer(surface->compositor);
Kristian Høgsberg42263852012-09-06 21:59:29 -04001263 int i;
1264
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001265 glDeleteTextures(gs->num_textures, gs->textures);
Kristian Høgsberg42263852012-09-06 21:59:29 -04001266
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001267 for (i = 0; i < gs->num_images; i++)
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001268 gr->destroy_image(gr->egl_display, gs->images[i]);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001269
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001270 weston_buffer_reference(&gs->buffer_ref, NULL);
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001271 pixman_region32_fini(&gs->texture_damage);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001272 free(gs);
Kristian Høgsberg42263852012-09-06 21:59:29 -04001273}
1274
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001275static const char vertex_shader[] =
1276 "uniform mat4 proj;\n"
1277 "attribute vec2 position;\n"
1278 "attribute vec2 texcoord;\n"
1279 "varying vec2 v_texcoord;\n"
1280 "void main()\n"
1281 "{\n"
1282 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
1283 " v_texcoord = texcoord;\n"
1284 "}\n";
1285
1286/* Declare common fragment shader uniforms */
1287#define FRAGMENT_CONVERT_YUV \
1288 " y *= alpha;\n" \
1289 " u *= alpha;\n" \
1290 " v *= alpha;\n" \
1291 " gl_FragColor.r = y + 1.59602678 * v;\n" \
1292 " gl_FragColor.g = y - 0.39176229 * u - 0.81296764 * v;\n" \
1293 " gl_FragColor.b = y + 2.01723214 * u;\n" \
1294 " gl_FragColor.a = alpha;\n"
1295
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001296static const char fragment_debug[] =
1297 " gl_FragColor = vec4(0.0, 0.3, 0.0, 0.2) + gl_FragColor * 0.8;\n";
1298
1299static const char fragment_brace[] =
1300 "}\n";
1301
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001302static const char texture_fragment_shader_rgba[] =
1303 "precision mediump float;\n"
1304 "varying vec2 v_texcoord;\n"
1305 "uniform sampler2D tex;\n"
1306 "uniform float alpha;\n"
1307 "void main()\n"
1308 "{\n"
1309 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001310 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001311
1312static const char texture_fragment_shader_rgbx[] =
1313 "precision mediump float;\n"
1314 "varying vec2 v_texcoord;\n"
1315 "uniform sampler2D tex;\n"
1316 "uniform float alpha;\n"
1317 "void main()\n"
1318 "{\n"
1319 " gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb\n;"
1320 " gl_FragColor.a = alpha;\n"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001321 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001322
1323static const char texture_fragment_shader_egl_external[] =
1324 "#extension GL_OES_EGL_image_external : require\n"
1325 "precision mediump float;\n"
1326 "varying vec2 v_texcoord;\n"
1327 "uniform samplerExternalOES tex;\n"
1328 "uniform float alpha;\n"
1329 "void main()\n"
1330 "{\n"
1331 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001332 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001333
1334static const char texture_fragment_shader_y_uv[] =
1335 "precision mediump float;\n"
1336 "uniform sampler2D tex;\n"
1337 "uniform sampler2D tex1;\n"
1338 "varying vec2 v_texcoord;\n"
1339 "uniform float alpha;\n"
1340 "void main() {\n"
1341 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1342 " float u = texture2D(tex1, v_texcoord).r - 0.5;\n"
1343 " float v = texture2D(tex1, v_texcoord).g - 0.5;\n"
1344 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001345 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001346
1347static const char texture_fragment_shader_y_u_v[] =
1348 "precision mediump float;\n"
1349 "uniform sampler2D tex;\n"
1350 "uniform sampler2D tex1;\n"
1351 "uniform sampler2D tex2;\n"
1352 "varying vec2 v_texcoord;\n"
1353 "uniform float alpha;\n"
1354 "void main() {\n"
1355 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1356 " float u = texture2D(tex1, v_texcoord).x - 0.5;\n"
1357 " float v = texture2D(tex2, v_texcoord).x - 0.5;\n"
1358 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001359 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001360
1361static const char texture_fragment_shader_y_xuxv[] =
1362 "precision mediump float;\n"
1363 "uniform sampler2D tex;\n"
1364 "uniform sampler2D tex1;\n"
1365 "varying vec2 v_texcoord;\n"
1366 "uniform float alpha;\n"
1367 "void main() {\n"
1368 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1369 " float u = texture2D(tex1, v_texcoord).g - 0.5;\n"
1370 " float v = texture2D(tex1, v_texcoord).a - 0.5;\n"
1371 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001372 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001373
1374static const char solid_fragment_shader[] =
1375 "precision mediump float;\n"
1376 "uniform vec4 color;\n"
1377 "uniform float alpha;\n"
1378 "void main()\n"
1379 "{\n"
1380 " gl_FragColor = alpha * color\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001381 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001382
1383static int
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001384compile_shader(GLenum type, int count, const char **sources)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001385{
1386 GLuint s;
1387 char msg[512];
1388 GLint status;
1389
1390 s = glCreateShader(type);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001391 glShaderSource(s, count, sources, NULL);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001392 glCompileShader(s);
1393 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
1394 if (!status) {
1395 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1396 weston_log("shader info: %s\n", msg);
1397 return GL_NONE;
1398 }
1399
1400 return s;
1401}
1402
1403static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001404shader_init(struct gl_shader *shader, struct weston_compositor *ec,
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001405 const char *vertex_source, const char *fragment_source)
1406{
1407 char msg[512];
1408 GLint status;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001409 int count;
1410 const char *sources[3];
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001411 struct gl_renderer *renderer = get_renderer(ec);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001412
1413 shader->vertex_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001414 compile_shader(GL_VERTEX_SHADER, 1, &vertex_source);
1415
1416 if (renderer->fragment_shader_debug) {
1417 sources[0] = fragment_source;
1418 sources[1] = fragment_debug;
1419 sources[2] = fragment_brace;
1420 count = 3;
1421 } else {
1422 sources[0] = fragment_source;
1423 sources[1] = fragment_brace;
1424 count = 2;
1425 }
1426
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001427 shader->fragment_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001428 compile_shader(GL_FRAGMENT_SHADER, count, sources);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001429
1430 shader->program = glCreateProgram();
1431 glAttachShader(shader->program, shader->vertex_shader);
1432 glAttachShader(shader->program, shader->fragment_shader);
1433 glBindAttribLocation(shader->program, 0, "position");
1434 glBindAttribLocation(shader->program, 1, "texcoord");
1435
1436 glLinkProgram(shader->program);
1437 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1438 if (!status) {
1439 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1440 weston_log("link info: %s\n", msg);
1441 return -1;
1442 }
1443
1444 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1445 shader->tex_uniforms[0] = glGetUniformLocation(shader->program, "tex");
1446 shader->tex_uniforms[1] = glGetUniformLocation(shader->program, "tex1");
1447 shader->tex_uniforms[2] = glGetUniformLocation(shader->program, "tex2");
1448 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
1449 shader->color_uniform = glGetUniformLocation(shader->program, "color");
1450
1451 return 0;
1452}
1453
1454static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001455shader_release(struct gl_shader *shader)
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001456{
1457 glDeleteShader(shader->vertex_shader);
1458 glDeleteShader(shader->fragment_shader);
1459 glDeleteProgram(shader->program);
1460
1461 shader->vertex_shader = 0;
1462 shader->fragment_shader = 0;
1463 shader->program = 0;
1464}
1465
1466static void
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001467log_extensions(const char *name, const char *extensions)
1468{
1469 const char *p, *end;
1470 int l;
1471 int len;
1472
1473 l = weston_log("%s:", name);
1474 p = extensions;
1475 while (*p) {
1476 end = strchrnul(p, ' ');
1477 len = end - p;
1478 if (l + len > 78)
1479 l = weston_log_continue("\n" STAMP_SPACE "%.*s",
1480 len, p);
1481 else
1482 l += weston_log_continue(" %.*s", len, p);
1483 for (p = end; isspace(*p); p++)
1484 ;
1485 }
1486 weston_log_continue("\n");
1487}
1488
1489static void
1490log_egl_gl_info(EGLDisplay egldpy)
1491{
1492 const char *str;
1493
1494 str = eglQueryString(egldpy, EGL_VERSION);
1495 weston_log("EGL version: %s\n", str ? str : "(null)");
1496
1497 str = eglQueryString(egldpy, EGL_VENDOR);
1498 weston_log("EGL vendor: %s\n", str ? str : "(null)");
1499
1500 str = eglQueryString(egldpy, EGL_CLIENT_APIS);
1501 weston_log("EGL client APIs: %s\n", str ? str : "(null)");
1502
1503 str = eglQueryString(egldpy, EGL_EXTENSIONS);
1504 log_extensions("EGL extensions", str ? str : "(null)");
1505
1506 str = (char *)glGetString(GL_VERSION);
1507 weston_log("GL version: %s\n", str ? str : "(null)");
1508
1509 str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
1510 weston_log("GLSL version: %s\n", str ? str : "(null)");
1511
1512 str = (char *)glGetString(GL_VENDOR);
1513 weston_log("GL vendor: %s\n", str ? str : "(null)");
1514
1515 str = (char *)glGetString(GL_RENDERER);
1516 weston_log("GL renderer: %s\n", str ? str : "(null)");
1517
1518 str = (char *)glGetString(GL_EXTENSIONS);
1519 log_extensions("GL extensions", str ? str : "(null)");
1520}
1521
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001522static void
1523log_egl_config_info(EGLDisplay egldpy, EGLConfig eglconfig)
1524{
1525 EGLint r, g, b, a;
1526
1527 weston_log("Chosen EGL config details:\n");
1528
1529 weston_log_continue(STAMP_SPACE "RGBA bits");
1530 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_RED_SIZE, &r) &&
1531 eglGetConfigAttrib(egldpy, eglconfig, EGL_GREEN_SIZE, &g) &&
1532 eglGetConfigAttrib(egldpy, eglconfig, EGL_BLUE_SIZE, &b) &&
1533 eglGetConfigAttrib(egldpy, eglconfig, EGL_ALPHA_SIZE, &a))
1534 weston_log_continue(": %d %d %d %d\n", r, g, b, a);
1535 else
1536 weston_log_continue(" unknown\n");
1537
1538 weston_log_continue(STAMP_SPACE "swap interval range");
1539 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_MIN_SWAP_INTERVAL, &a) &&
1540 eglGetConfigAttrib(egldpy, eglconfig, EGL_MAX_SWAP_INTERVAL, &b))
1541 weston_log_continue(": %d - %d\n", a, b);
1542 else
1543 weston_log_continue(" unknown\n");
1544}
1545
John Kåre Alsaker44154502012-11-13 19:10:20 +01001546static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001547output_apply_border(struct weston_output *output, struct gl_renderer *gr)
John Kåre Alsaker44154502012-11-13 19:10:20 +01001548{
1549 output->border.top = gr->border.top;
1550 output->border.bottom = gr->border.bottom;
1551 output->border.left = gr->border.left;
1552 output->border.right = gr->border.right;
1553}
1554
1555WL_EXPORT void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001556gl_renderer_set_border(struct weston_compositor *ec, int32_t width, int32_t height, void *data,
John Kåre Alsaker44154502012-11-13 19:10:20 +01001557 int32_t *edges)
1558{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001559 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker44154502012-11-13 19:10:20 +01001560 struct weston_output *output;
1561
1562 gr->border.left = edges[0];
1563 gr->border.right = edges[1];
1564 gr->border.top = edges[2];
1565 gr->border.bottom = edges[3];
1566
1567 gr->border.width = width;
1568 gr->border.height = height;
1569
1570 glGenTextures(1, &gr->border.texture);
1571 glBindTexture(GL_TEXTURE_2D, gr->border.texture);
1572 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1573 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1574 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1575 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1576
1577 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1578 width,
1579 height,
1580 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1581 data);
1582
1583 wl_list_for_each(output, &ec->output_list, link)
1584 output_apply_border(output, gr);
1585}
1586
John Kåre Alsaker94659272012-11-13 19:10:18 +01001587static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001588gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001589
1590WL_EXPORT int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001591gl_renderer_output_create(struct weston_output *output,
John Kåre Alsaker94659272012-11-13 19:10:18 +01001592 EGLNativeWindowType window)
1593{
1594 struct weston_compositor *ec = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001595 struct gl_renderer *gr = get_renderer(ec);
1596 struct gl_output_state *go = calloc(1, sizeof *go);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001597 int i;
John Kåre Alsaker94659272012-11-13 19:10:18 +01001598
1599 if (!go)
1600 return -1;
1601
1602 go->egl_surface =
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001603 eglCreateWindowSurface(gr->egl_display,
1604 gr->egl_config,
John Kåre Alsaker94659272012-11-13 19:10:18 +01001605 window, NULL);
1606
1607 if (go->egl_surface == EGL_NO_SURFACE) {
1608 weston_log("failed to create egl surface\n");
1609 free(go);
1610 return -1;
1611 }
1612
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001613 if (gr->egl_context == NULL)
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001614 if (gl_renderer_setup(ec, go->egl_surface) < 0) {
John Kåre Alsaker94659272012-11-13 19:10:18 +01001615 free(go);
1616 return -1;
1617 }
1618
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001619 go->current_buffer = 0;
1620 for (i = 0; i < 2; i++)
1621 pixman_region32_init(&go->buffer_damage[i]);
1622
John Kåre Alsaker94659272012-11-13 19:10:18 +01001623 output->renderer_state = go;
1624
John Kåre Alsaker44154502012-11-13 19:10:20 +01001625 output_apply_border(output, gr);
1626
John Kåre Alsaker94659272012-11-13 19:10:18 +01001627 return 0;
1628}
1629
1630WL_EXPORT void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001631gl_renderer_output_destroy(struct weston_output *output)
John Kåre Alsaker94659272012-11-13 19:10:18 +01001632{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001633 struct gl_renderer *gr = get_renderer(output->compositor);
1634 struct gl_output_state *go = get_output_state(output);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001635 int i;
1636
1637 for (i = 0; i < 2; i++)
1638 pixman_region32_fini(&go->buffer_damage[i]);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001639
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001640 eglDestroySurface(gr->egl_display, go->egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001641
1642 free(go);
1643}
1644
1645WL_EXPORT EGLSurface
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001646gl_renderer_output_surface(struct weston_output *output)
John Kåre Alsaker94659272012-11-13 19:10:18 +01001647{
1648 return get_output_state(output)->egl_surface;
1649}
1650
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001651WL_EXPORT void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001652gl_renderer_destroy(struct weston_compositor *ec)
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001653{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001654 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001655
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001656 if (gr->has_bind_display)
1657 gr->unbind_display(gr->egl_display, ec->wl_display);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001658
1659 /* Work around crash in egl_dri2.c's dri2_make_current() - when does this apply? */
1660 eglMakeCurrent(gr->egl_display,
1661 EGL_NO_SURFACE, EGL_NO_SURFACE,
1662 EGL_NO_CONTEXT);
1663
1664 eglTerminate(gr->egl_display);
1665 eglReleaseThread();
1666}
1667
1668static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001669egl_choose_config(struct gl_renderer *gr, const EGLint *attribs,
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001670 const EGLint *visual_id)
1671{
1672 EGLint count = 0;
1673 EGLint matched = 0;
1674 EGLConfig *configs;
1675 int i;
1676
1677 if (!eglGetConfigs(gr->egl_display, NULL, 0, &count) || count < 1)
1678 return -1;
1679
1680 configs = calloc(count, sizeof *configs);
1681 if (!configs)
1682 return -1;
1683
1684 if (!eglChooseConfig(gr->egl_display, attribs, configs,
1685 count, &matched))
1686 goto out;
1687
1688 for (i = 0; i < matched; ++i) {
1689 EGLint id;
1690
1691 if (visual_id) {
1692 if (!eglGetConfigAttrib(gr->egl_display,
1693 configs[i], EGL_NATIVE_VISUAL_ID,
1694 &id))
1695 continue;
1696
1697 if (id != *visual_id)
1698 continue;
1699 }
1700
1701 gr->egl_config = configs[i];
1702
1703 free(configs);
1704 return 0;
1705 }
1706
1707out:
1708 free(configs);
1709 return -1;
1710}
1711
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001712WL_EXPORT const EGLint gl_renderer_opaque_attribs[] = {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001713 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1714 EGL_RED_SIZE, 1,
1715 EGL_GREEN_SIZE, 1,
1716 EGL_BLUE_SIZE, 1,
1717 EGL_ALPHA_SIZE, 0,
1718 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1719 EGL_NONE
1720};
1721
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001722WL_EXPORT const EGLint gl_renderer_alpha_attribs[] = {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001723 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1724 EGL_RED_SIZE, 1,
1725 EGL_GREEN_SIZE, 1,
1726 EGL_BLUE_SIZE, 1,
1727 EGL_ALPHA_SIZE, 1,
1728 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1729 EGL_NONE
1730};
1731
1732WL_EXPORT int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001733gl_renderer_create(struct weston_compositor *ec, EGLNativeDisplayType display,
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001734 const EGLint *attribs, const EGLint *visual_id)
1735{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001736 struct gl_renderer *gr;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001737 EGLint major, minor;
1738
1739 gr = calloc(1, sizeof *gr);
1740
1741 if (gr == NULL)
1742 return -1;
1743
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001744 gr->base.read_pixels = gl_renderer_read_pixels;
1745 gr->base.repaint_output = gl_renderer_repaint_output;
1746 gr->base.flush_damage = gl_renderer_flush_damage;
1747 gr->base.attach = gl_renderer_attach;
1748 gr->base.create_surface = gl_renderer_create_surface;
1749 gr->base.surface_set_color = gl_renderer_surface_set_color;
1750 gr->base.destroy_surface = gl_renderer_destroy_surface;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001751
1752 gr->egl_display = eglGetDisplay(display);
1753 if (gr->egl_display == EGL_NO_DISPLAY) {
1754 weston_log("failed to create display\n");
1755 goto err_egl;
1756 }
1757
1758 if (!eglInitialize(gr->egl_display, &major, &minor)) {
1759 weston_log("failed to initialize display\n");
1760 goto err_egl;
1761 }
1762
1763 if (egl_choose_config(gr, attribs, visual_id) < 0) {
1764 weston_log("failed to choose EGL config\n");
1765 goto err_egl;
1766 }
1767
1768 ec->renderer = &gr->base;
1769
1770 return 0;
1771
1772err_egl:
Pekka Paalanen326529f2012-11-27 12:25:25 +02001773 gl_renderer_print_egl_error_state();
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001774 free(gr);
1775 return -1;
1776}
1777
1778WL_EXPORT EGLDisplay
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001779gl_renderer_display(struct weston_compositor *ec)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001780{
1781 return get_renderer(ec)->egl_display;
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001782}
1783
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001784static int
1785compile_shaders(struct weston_compositor *ec)
1786{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001787 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker40684142012-11-13 19:10:25 +01001788
1789 if (shader_init(&gr->texture_shader_rgba, ec,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001790 vertex_shader, texture_fragment_shader_rgba) < 0)
1791 return -1;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001792 if (shader_init(&gr->texture_shader_rgbx, ec,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001793 vertex_shader, texture_fragment_shader_rgbx) < 0)
1794 return -1;
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001795 if (gr->has_egl_image_external &&
John Kåre Alsaker40684142012-11-13 19:10:25 +01001796 shader_init(&gr->texture_shader_egl_external, ec,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001797 vertex_shader, texture_fragment_shader_egl_external) < 0)
1798 return -1;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001799 if (shader_init(&gr->texture_shader_y_uv, ec,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001800 vertex_shader, texture_fragment_shader_y_uv) < 0)
1801 return -1;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001802 if (shader_init(&gr->texture_shader_y_u_v, ec,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001803 vertex_shader, texture_fragment_shader_y_u_v) < 0)
1804 return -1;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001805 if (shader_init(&gr->texture_shader_y_xuxv, ec,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001806 vertex_shader, texture_fragment_shader_y_xuxv) < 0)
1807 return -1;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001808 if (shader_init(&gr->solid_shader, ec,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001809 vertex_shader, solid_fragment_shader) < 0)
1810 return -1;
1811
1812 return 0;
1813}
1814
1815static void
1816fragment_debug_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
1817 void *data)
1818{
1819 struct weston_compositor *ec = data;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001820 struct gl_renderer *gr = get_renderer(ec);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001821 struct weston_output *output;
1822
John Kåre Alsaker40684142012-11-13 19:10:25 +01001823 gr->fragment_shader_debug ^= 1;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001824
John Kåre Alsaker40684142012-11-13 19:10:25 +01001825 shader_release(&gr->texture_shader_rgba);
1826 shader_release(&gr->texture_shader_rgbx);
1827 shader_release(&gr->texture_shader_egl_external);
1828 shader_release(&gr->texture_shader_y_uv);
1829 shader_release(&gr->texture_shader_y_u_v);
1830 shader_release(&gr->texture_shader_y_xuxv);
1831 shader_release(&gr->solid_shader);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001832
1833 compile_shaders(ec);
1834
Ander Conselvan de Oliveira03fb4ef2012-12-03 17:08:11 +02001835 /* Force use_shader() to call glUseProgram(), since we need to use
1836 * the recompiled version of the shader. */
1837 gr->current_shader = NULL;
1838
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001839 wl_list_for_each(output, &ec->output_list, link)
1840 weston_output_damage(output);
1841}
1842
John Kåre Alsaker94659272012-11-13 19:10:18 +01001843static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001844gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001845{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001846 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001847 const char *extensions;
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001848 EGLBoolean ret;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001849
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001850 static const EGLint context_attribs[] = {
1851 EGL_CONTEXT_CLIENT_VERSION, 2,
1852 EGL_NONE
1853 };
1854
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001855 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
1856 weston_log("failed to bind EGL_OPENGL_ES_API\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02001857 gl_renderer_print_egl_error_state();
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001858 return -1;
1859 }
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001860
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001861 log_egl_config_info(gr->egl_display, gr->egl_config);
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001862
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001863 gr->egl_context = eglCreateContext(gr->egl_display, gr->egl_config,
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001864 EGL_NO_CONTEXT, context_attribs);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001865 if (gr->egl_context == NULL) {
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001866 weston_log("failed to create context\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02001867 gl_renderer_print_egl_error_state();
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001868 return -1;
1869 }
1870
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001871 ret = eglMakeCurrent(gr->egl_display, egl_surface,
1872 egl_surface, gr->egl_context);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001873 if (ret == EGL_FALSE) {
1874 weston_log("Failed to make EGL context current.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02001875 gl_renderer_print_egl_error_state();
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001876 return -1;
1877 }
1878
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001879 log_egl_gl_info(gr->egl_display);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001880
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001881 gr->image_target_texture_2d =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001882 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001883 gr->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1884 gr->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1885 gr->bind_display =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001886 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001887 gr->unbind_display =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001888 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001889 gr->query_buffer =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001890 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
1891
1892 extensions = (const char *) glGetString(GL_EXTENSIONS);
1893 if (!extensions) {
1894 weston_log("Retrieving GL extension string failed.\n");
1895 return -1;
1896 }
1897
1898 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
1899 weston_log("GL_EXT_texture_format_BGRA8888 not available\n");
1900 return -1;
1901 }
1902
1903 if (strstr(extensions, "GL_EXT_read_format_bgra"))
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01001904 ec->read_format = PIXMAN_a8r8g8b8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001905 else
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01001906 ec->read_format = PIXMAN_a8b8g8r8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001907
1908 if (strstr(extensions, "GL_EXT_unpack_subimage"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001909 gr->has_unpack_subimage = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001910
1911 if (strstr(extensions, "GL_OES_EGL_image_external"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001912 gr->has_egl_image_external = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001913
1914 extensions =
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001915 (const char *) eglQueryString(gr->egl_display, EGL_EXTENSIONS);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001916 if (!extensions) {
1917 weston_log("Retrieving EGL extension string failed.\n");
1918 return -1;
1919 }
1920
1921 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001922 gr->has_bind_display = 1;
1923 if (gr->has_bind_display) {
1924 ret = gr->bind_display(gr->egl_display, ec->wl_display);
Pekka Paalanen035a0322012-10-24 09:43:06 +03001925 if (!ret)
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001926 gr->has_bind_display = 0;
Pekka Paalanen035a0322012-10-24 09:43:06 +03001927 }
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001928
1929 glActiveTexture(GL_TEXTURE0);
1930
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001931 if (compile_shaders(ec))
1932 return -1;
1933
1934 weston_compositor_add_debug_binding(ec, KEY_S,
1935 fragment_debug_binding, ec);
1936
Pekka Paalanen035a0322012-10-24 09:43:06 +03001937 weston_log("GL ES 2 renderer features:\n");
1938 weston_log_continue(STAMP_SPACE "read-back format: %s\n",
1939 ec->read_format == GL_BGRA_EXT ? "BGRA" : "RGBA");
1940 weston_log_continue(STAMP_SPACE "wl_shm sub-image to texture: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001941 gr->has_unpack_subimage ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03001942 weston_log_continue(STAMP_SPACE "EGL Wayland extension: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001943 gr->has_bind_display ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03001944
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001945
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001946 return 0;
1947}