blob: 4c1bc233ff9f0479e3b33689e74d3e3d97ac14b3 [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;
Ander Conselvan de Oliveirab605c062013-03-05 17:30:28 +0200960 pixman_region32_t total_damage;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400961
962 width = output->current->width +
963 output->border.left + output->border.right;
964 height = output->current->height +
965 output->border.top + output->border.bottom;
966
967 glViewport(0, 0, width, height);
968
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100969 if (use_output(output) < 0)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400970 return;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400971
972 /* if debugging, redraw everything outside the damage to clean up
973 * debug lines from the previous draw on this buffer:
974 */
975 if (compositor->fan_debug) {
976 pixman_region32_t undamaged;
977 pixman_region32_init(&undamaged);
978 pixman_region32_subtract(&undamaged, &output->region,
979 output_damage);
980 compositor->fan_debug = 0;
981 repaint_surfaces(output, &undamaged);
982 compositor->fan_debug = 1;
983 pixman_region32_fini(&undamaged);
984 }
985
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300986 for (i = 0; i < 2; i++)
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +0200987 pixman_region32_union(&go->buffer_damage[i],
988 &go->buffer_damage[i],
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300989 output_damage);
990
Ander Conselvan de Oliveirab605c062013-03-05 17:30:28 +0200991 pixman_region32_init(&total_damage);
992 pixman_region32_copy(&total_damage,
993 &go->buffer_damage[go->current_buffer]);
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300994
Ander Conselvan de Oliveirab605c062013-03-05 17:30:28 +0200995 repaint_surfaces(output, &total_damage);
996
997 pixman_region32_fini(&total_damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400998
John Kåre Alsaker44154502012-11-13 19:10:20 +0100999 if (gr->border.texture)
1000 draw_border(output);
1001
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001002 pixman_region32_copy(&output->previous_damage, output_damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001003 wl_signal_emit(&output->frame_signal, output);
1004
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001005 ret = eglSwapBuffers(gr->egl_display, go->egl_surface);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001006 if (ret == EGL_FALSE && !errored) {
1007 errored = 1;
1008 weston_log("Failed in eglSwapBuffers.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02001009 gl_renderer_print_egl_error_state();
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001010 }
1011
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001012 go->current_buffer ^= 1;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03001013
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001014}
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001015
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001016static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001017gl_renderer_read_pixels(struct weston_output *output,
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001018 pixman_format_code_t format, void *pixels,
1019 uint32_t x, uint32_t y,
1020 uint32_t width, uint32_t height)
1021{
1022 GLenum gl_format;
1023
1024 switch (format) {
1025 case PIXMAN_a8r8g8b8:
1026 gl_format = GL_BGRA_EXT;
1027 break;
1028 case PIXMAN_a8b8g8r8:
1029 gl_format = GL_RGBA;
1030 break;
1031 default:
1032 return -1;
1033 }
1034
1035 if (use_output(output) < 0)
1036 return -1;
1037
1038 glPixelStorei(GL_PACK_ALIGNMENT, 1);
1039 glReadPixels(x, y, width, height, gl_format,
1040 GL_UNSIGNED_BYTE, pixels);
1041
1042 return 0;
1043}
1044
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001045static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001046gl_renderer_flush_damage(struct weston_surface *surface)
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001047{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001048 struct gl_renderer *gr = get_renderer(surface->compositor);
1049 struct gl_surface_state *gs = get_surface_state(surface);
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001050 struct wl_buffer *buffer = gs->buffer_ref.buffer;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001051
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001052#ifdef GL_UNPACK_ROW_LENGTH
1053 pixman_box32_t *rectangles;
1054 void *data;
1055 int i, n;
1056#endif
1057
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001058 pixman_region32_union(&gs->texture_damage,
1059 &gs->texture_damage, &surface->damage);
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001060
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001061 if (!buffer)
1062 return;
1063
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001064 /* Avoid upload, if the texture won't be used this time.
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001065 * We still accumulate the damage in texture_damage, and
1066 * hold the reference to the buffer, in case the surface
1067 * migrates back to the primary plane.
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001068 */
1069 if (surface->plane != &surface->compositor->primary_plane)
1070 return;
1071
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001072 if (!pixman_region32_not_empty(&gs->texture_damage))
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001073 goto done;
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001074
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001075 glBindTexture(GL_TEXTURE_2D, gs->textures[0]);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001076
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001077 if (!gr->has_unpack_subimage) {
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001078 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001079 gs->pitch, buffer->height, 0,
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001080 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
Pekka Paalanende685b82012-12-04 15:58:12 +02001081 wl_shm_buffer_get_data(buffer));
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001082
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001083 goto done;
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001084 }
1085
1086#ifdef GL_UNPACK_ROW_LENGTH
1087 /* Mesa does not define GL_EXT_unpack_subimage */
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001088 glPixelStorei(GL_UNPACK_ROW_LENGTH, gs->pitch);
Pekka Paalanende685b82012-12-04 15:58:12 +02001089 data = wl_shm_buffer_get_data(buffer);
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001090 rectangles = pixman_region32_rectangles(&gs->texture_damage, &n);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001091 for (i = 0; i < n; i++) {
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001092 pixman_box32_t r;
1093
1094 r = weston_surface_to_buffer_rect(surface, rectangles[i]);
1095
1096 glPixelStorei(GL_UNPACK_SKIP_PIXELS, r.x1);
1097 glPixelStorei(GL_UNPACK_SKIP_ROWS, r.y1);
1098 glTexSubImage2D(GL_TEXTURE_2D, 0, r.x1, r.y1,
1099 r.x2 - r.x1, r.y2 - r.y1,
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001100 GL_BGRA_EXT, GL_UNSIGNED_BYTE, data);
1101 }
1102#endif
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001103
1104done:
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001105 pixman_region32_fini(&gs->texture_damage);
1106 pixman_region32_init(&gs->texture_damage);
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001107
1108 weston_buffer_reference(&gs->buffer_ref, NULL);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001109}
1110
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001111static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001112ensure_textures(struct gl_surface_state *gs, int num_textures)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001113{
1114 int i;
1115
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001116 if (num_textures <= gs->num_textures)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001117 return;
1118
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001119 for (i = gs->num_textures; i < num_textures; i++) {
1120 glGenTextures(1, &gs->textures[i]);
1121 glBindTexture(gs->target, gs->textures[i]);
1122 glTexParameteri(gs->target,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001123 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001124 glTexParameteri(gs->target,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001125 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1126 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001127 gs->num_textures = num_textures;
1128 glBindTexture(gs->target, 0);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001129}
1130
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001131static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001132gl_renderer_attach(struct weston_surface *es, struct wl_buffer *buffer)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001133{
1134 struct weston_compositor *ec = es->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001135 struct gl_renderer *gr = get_renderer(ec);
1136 struct gl_surface_state *gs = get_surface_state(es);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001137 EGLint attribs[3], format;
1138 int i, num_planes;
1139
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001140 weston_buffer_reference(&gs->buffer_ref, buffer);
1141
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001142 if (!buffer) {
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001143 for (i = 0; i < gs->num_images; i++) {
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001144 gr->destroy_image(gr->egl_display, gs->images[i]);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001145 gs->images[i] = NULL;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001146 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001147 gs->num_images = 0;
1148 glDeleteTextures(gs->num_textures, gs->textures);
1149 gs->num_textures = 0;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001150 return;
1151 }
1152
1153 if (wl_buffer_is_shm(buffer)) {
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001154 gs->pitch = wl_shm_buffer_get_stride(buffer) / 4;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001155 gs->target = GL_TEXTURE_2D;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001156
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001157 ensure_textures(gs, 1);
1158 glBindTexture(GL_TEXTURE_2D, gs->textures[0]);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001159 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001160 gs->pitch, buffer->height, 0,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001161 GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
1162 if (wl_shm_buffer_get_format(buffer) == WL_SHM_FORMAT_XRGB8888)
John Kåre Alsaker40684142012-11-13 19:10:25 +01001163 gs->shader = &gr->texture_shader_rgbx;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001164 else
John Kåre Alsaker40684142012-11-13 19:10:25 +01001165 gs->shader = &gr->texture_shader_rgba;
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001166 } else if (gr->query_buffer(gr->egl_display, buffer,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001167 EGL_TEXTURE_FORMAT, &format)) {
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001168 for (i = 0; i < gs->num_images; i++)
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001169 gr->destroy_image(gr->egl_display, gs->images[i]);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001170 gs->num_images = 0;
1171 gs->target = GL_TEXTURE_2D;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001172 switch (format) {
1173 case EGL_TEXTURE_RGB:
1174 case EGL_TEXTURE_RGBA:
1175 default:
1176 num_planes = 1;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001177 gs->shader = &gr->texture_shader_rgba;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001178 break;
1179 case EGL_TEXTURE_EXTERNAL_WL:
1180 num_planes = 1;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001181 gs->target = GL_TEXTURE_EXTERNAL_OES;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001182 gs->shader = &gr->texture_shader_egl_external;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001183 break;
1184 case EGL_TEXTURE_Y_UV_WL:
1185 num_planes = 2;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001186 gs->shader = &gr->texture_shader_y_uv;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001187 break;
1188 case EGL_TEXTURE_Y_U_V_WL:
1189 num_planes = 3;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001190 gs->shader = &gr->texture_shader_y_u_v;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001191 break;
1192 case EGL_TEXTURE_Y_XUXV_WL:
1193 num_planes = 2;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001194 gs->shader = &gr->texture_shader_y_xuxv;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001195 break;
1196 }
1197
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001198 ensure_textures(gs, num_planes);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001199 for (i = 0; i < num_planes; i++) {
1200 attribs[0] = EGL_WAYLAND_PLANE_WL;
1201 attribs[1] = i;
1202 attribs[2] = EGL_NONE;
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001203 gs->images[i] = gr->create_image(gr->egl_display,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001204 NULL,
1205 EGL_WAYLAND_BUFFER_WL,
1206 buffer, attribs);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001207 if (!gs->images[i]) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001208 weston_log("failed to create img for plane %d\n", i);
1209 continue;
1210 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001211 gs->num_images++;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001212
1213 glActiveTexture(GL_TEXTURE0 + i);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001214 glBindTexture(gs->target, gs->textures[i]);
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001215 gr->image_target_texture_2d(gs->target,
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001216 gs->images[i]);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001217 }
1218
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001219 gs->pitch = buffer->width;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001220 } else {
1221 weston_log("unhandled buffer type!\n");
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001222 weston_buffer_reference(&gs->buffer_ref, NULL);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001223 }
1224}
1225
Kristian Høgsberg42263852012-09-06 21:59:29 -04001226static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001227gl_renderer_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001228 float red, float green, float blue, float alpha)
1229{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001230 struct gl_surface_state *gs = get_surface_state(surface);
1231 struct gl_renderer *gr = get_renderer(surface->compositor);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001232
1233 gs->color[0] = red;
1234 gs->color[1] = green;
1235 gs->color[2] = blue;
1236 gs->color[3] = alpha;
1237
John Kåre Alsaker40684142012-11-13 19:10:25 +01001238 gs->shader = &gr->solid_shader;
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001239}
1240
1241static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001242gl_renderer_create_surface(struct weston_surface *surface)
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001243{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001244 struct gl_surface_state *gs;
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001245
1246 gs = calloc(1, sizeof *gs);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001247 if (!gs)
1248 return -1;
1249
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001250 /* A buffer is never attached to solid color surfaces, yet
1251 * they still go through texcoord computations. Do not divide
1252 * by zero there.
1253 */
1254 gs->pitch = 1;
1255
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001256 pixman_region32_init(&gs->texture_damage);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001257 surface->renderer_state = gs;
1258
1259 return 0;
1260}
1261
1262static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001263gl_renderer_destroy_surface(struct weston_surface *surface)
Kristian Høgsberg42263852012-09-06 21:59:29 -04001264{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001265 struct gl_surface_state *gs = get_surface_state(surface);
1266 struct gl_renderer *gr = get_renderer(surface->compositor);
Kristian Høgsberg42263852012-09-06 21:59:29 -04001267 int i;
1268
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001269 glDeleteTextures(gs->num_textures, gs->textures);
Kristian Høgsberg42263852012-09-06 21:59:29 -04001270
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001271 for (i = 0; i < gs->num_images; i++)
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001272 gr->destroy_image(gr->egl_display, gs->images[i]);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001273
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001274 weston_buffer_reference(&gs->buffer_ref, NULL);
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001275 pixman_region32_fini(&gs->texture_damage);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001276 free(gs);
Kristian Høgsberg42263852012-09-06 21:59:29 -04001277}
1278
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001279static const char vertex_shader[] =
1280 "uniform mat4 proj;\n"
1281 "attribute vec2 position;\n"
1282 "attribute vec2 texcoord;\n"
1283 "varying vec2 v_texcoord;\n"
1284 "void main()\n"
1285 "{\n"
1286 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
1287 " v_texcoord = texcoord;\n"
1288 "}\n";
1289
1290/* Declare common fragment shader uniforms */
1291#define FRAGMENT_CONVERT_YUV \
1292 " y *= alpha;\n" \
1293 " u *= alpha;\n" \
1294 " v *= alpha;\n" \
1295 " gl_FragColor.r = y + 1.59602678 * v;\n" \
1296 " gl_FragColor.g = y - 0.39176229 * u - 0.81296764 * v;\n" \
1297 " gl_FragColor.b = y + 2.01723214 * u;\n" \
1298 " gl_FragColor.a = alpha;\n"
1299
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001300static const char fragment_debug[] =
1301 " gl_FragColor = vec4(0.0, 0.3, 0.0, 0.2) + gl_FragColor * 0.8;\n";
1302
1303static const char fragment_brace[] =
1304 "}\n";
1305
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001306static const char texture_fragment_shader_rgba[] =
1307 "precision mediump float;\n"
1308 "varying vec2 v_texcoord;\n"
1309 "uniform sampler2D tex;\n"
1310 "uniform float alpha;\n"
1311 "void main()\n"
1312 "{\n"
1313 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001314 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001315
1316static const char texture_fragment_shader_rgbx[] =
1317 "precision mediump float;\n"
1318 "varying vec2 v_texcoord;\n"
1319 "uniform sampler2D tex;\n"
1320 "uniform float alpha;\n"
1321 "void main()\n"
1322 "{\n"
1323 " gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb\n;"
1324 " gl_FragColor.a = alpha;\n"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001325 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001326
1327static const char texture_fragment_shader_egl_external[] =
1328 "#extension GL_OES_EGL_image_external : require\n"
1329 "precision mediump float;\n"
1330 "varying vec2 v_texcoord;\n"
1331 "uniform samplerExternalOES tex;\n"
1332 "uniform float alpha;\n"
1333 "void main()\n"
1334 "{\n"
1335 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001336 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001337
1338static const char texture_fragment_shader_y_uv[] =
1339 "precision mediump float;\n"
1340 "uniform sampler2D tex;\n"
1341 "uniform sampler2D tex1;\n"
1342 "varying vec2 v_texcoord;\n"
1343 "uniform float alpha;\n"
1344 "void main() {\n"
1345 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1346 " float u = texture2D(tex1, v_texcoord).r - 0.5;\n"
1347 " float v = texture2D(tex1, v_texcoord).g - 0.5;\n"
1348 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001349 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001350
1351static const char texture_fragment_shader_y_u_v[] =
1352 "precision mediump float;\n"
1353 "uniform sampler2D tex;\n"
1354 "uniform sampler2D tex1;\n"
1355 "uniform sampler2D tex2;\n"
1356 "varying vec2 v_texcoord;\n"
1357 "uniform float alpha;\n"
1358 "void main() {\n"
1359 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1360 " float u = texture2D(tex1, v_texcoord).x - 0.5;\n"
1361 " float v = texture2D(tex2, v_texcoord).x - 0.5;\n"
1362 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001363 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001364
1365static const char texture_fragment_shader_y_xuxv[] =
1366 "precision mediump float;\n"
1367 "uniform sampler2D tex;\n"
1368 "uniform sampler2D tex1;\n"
1369 "varying vec2 v_texcoord;\n"
1370 "uniform float alpha;\n"
1371 "void main() {\n"
1372 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1373 " float u = texture2D(tex1, v_texcoord).g - 0.5;\n"
1374 " float v = texture2D(tex1, v_texcoord).a - 0.5;\n"
1375 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001376 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001377
1378static const char solid_fragment_shader[] =
1379 "precision mediump float;\n"
1380 "uniform vec4 color;\n"
1381 "uniform float alpha;\n"
1382 "void main()\n"
1383 "{\n"
1384 " gl_FragColor = alpha * color\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001385 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001386
1387static int
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001388compile_shader(GLenum type, int count, const char **sources)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001389{
1390 GLuint s;
1391 char msg[512];
1392 GLint status;
1393
1394 s = glCreateShader(type);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001395 glShaderSource(s, count, sources, NULL);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001396 glCompileShader(s);
1397 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
1398 if (!status) {
1399 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1400 weston_log("shader info: %s\n", msg);
1401 return GL_NONE;
1402 }
1403
1404 return s;
1405}
1406
1407static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001408shader_init(struct gl_shader *shader, struct weston_compositor *ec,
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001409 const char *vertex_source, const char *fragment_source)
1410{
1411 char msg[512];
1412 GLint status;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001413 int count;
1414 const char *sources[3];
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001415 struct gl_renderer *renderer = get_renderer(ec);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001416
1417 shader->vertex_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001418 compile_shader(GL_VERTEX_SHADER, 1, &vertex_source);
1419
1420 if (renderer->fragment_shader_debug) {
1421 sources[0] = fragment_source;
1422 sources[1] = fragment_debug;
1423 sources[2] = fragment_brace;
1424 count = 3;
1425 } else {
1426 sources[0] = fragment_source;
1427 sources[1] = fragment_brace;
1428 count = 2;
1429 }
1430
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001431 shader->fragment_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001432 compile_shader(GL_FRAGMENT_SHADER, count, sources);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001433
1434 shader->program = glCreateProgram();
1435 glAttachShader(shader->program, shader->vertex_shader);
1436 glAttachShader(shader->program, shader->fragment_shader);
1437 glBindAttribLocation(shader->program, 0, "position");
1438 glBindAttribLocation(shader->program, 1, "texcoord");
1439
1440 glLinkProgram(shader->program);
1441 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1442 if (!status) {
1443 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1444 weston_log("link info: %s\n", msg);
1445 return -1;
1446 }
1447
1448 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1449 shader->tex_uniforms[0] = glGetUniformLocation(shader->program, "tex");
1450 shader->tex_uniforms[1] = glGetUniformLocation(shader->program, "tex1");
1451 shader->tex_uniforms[2] = glGetUniformLocation(shader->program, "tex2");
1452 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
1453 shader->color_uniform = glGetUniformLocation(shader->program, "color");
1454
1455 return 0;
1456}
1457
1458static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001459shader_release(struct gl_shader *shader)
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001460{
1461 glDeleteShader(shader->vertex_shader);
1462 glDeleteShader(shader->fragment_shader);
1463 glDeleteProgram(shader->program);
1464
1465 shader->vertex_shader = 0;
1466 shader->fragment_shader = 0;
1467 shader->program = 0;
1468}
1469
1470static void
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001471log_extensions(const char *name, const char *extensions)
1472{
1473 const char *p, *end;
1474 int l;
1475 int len;
1476
1477 l = weston_log("%s:", name);
1478 p = extensions;
1479 while (*p) {
1480 end = strchrnul(p, ' ');
1481 len = end - p;
1482 if (l + len > 78)
1483 l = weston_log_continue("\n" STAMP_SPACE "%.*s",
1484 len, p);
1485 else
1486 l += weston_log_continue(" %.*s", len, p);
1487 for (p = end; isspace(*p); p++)
1488 ;
1489 }
1490 weston_log_continue("\n");
1491}
1492
1493static void
1494log_egl_gl_info(EGLDisplay egldpy)
1495{
1496 const char *str;
1497
1498 str = eglQueryString(egldpy, EGL_VERSION);
1499 weston_log("EGL version: %s\n", str ? str : "(null)");
1500
1501 str = eglQueryString(egldpy, EGL_VENDOR);
1502 weston_log("EGL vendor: %s\n", str ? str : "(null)");
1503
1504 str = eglQueryString(egldpy, EGL_CLIENT_APIS);
1505 weston_log("EGL client APIs: %s\n", str ? str : "(null)");
1506
1507 str = eglQueryString(egldpy, EGL_EXTENSIONS);
1508 log_extensions("EGL extensions", str ? str : "(null)");
1509
1510 str = (char *)glGetString(GL_VERSION);
1511 weston_log("GL version: %s\n", str ? str : "(null)");
1512
1513 str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
1514 weston_log("GLSL version: %s\n", str ? str : "(null)");
1515
1516 str = (char *)glGetString(GL_VENDOR);
1517 weston_log("GL vendor: %s\n", str ? str : "(null)");
1518
1519 str = (char *)glGetString(GL_RENDERER);
1520 weston_log("GL renderer: %s\n", str ? str : "(null)");
1521
1522 str = (char *)glGetString(GL_EXTENSIONS);
1523 log_extensions("GL extensions", str ? str : "(null)");
1524}
1525
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001526static void
1527log_egl_config_info(EGLDisplay egldpy, EGLConfig eglconfig)
1528{
1529 EGLint r, g, b, a;
1530
1531 weston_log("Chosen EGL config details:\n");
1532
1533 weston_log_continue(STAMP_SPACE "RGBA bits");
1534 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_RED_SIZE, &r) &&
1535 eglGetConfigAttrib(egldpy, eglconfig, EGL_GREEN_SIZE, &g) &&
1536 eglGetConfigAttrib(egldpy, eglconfig, EGL_BLUE_SIZE, &b) &&
1537 eglGetConfigAttrib(egldpy, eglconfig, EGL_ALPHA_SIZE, &a))
1538 weston_log_continue(": %d %d %d %d\n", r, g, b, a);
1539 else
1540 weston_log_continue(" unknown\n");
1541
1542 weston_log_continue(STAMP_SPACE "swap interval range");
1543 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_MIN_SWAP_INTERVAL, &a) &&
1544 eglGetConfigAttrib(egldpy, eglconfig, EGL_MAX_SWAP_INTERVAL, &b))
1545 weston_log_continue(": %d - %d\n", a, b);
1546 else
1547 weston_log_continue(" unknown\n");
1548}
1549
John Kåre Alsaker44154502012-11-13 19:10:20 +01001550static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001551output_apply_border(struct weston_output *output, struct gl_renderer *gr)
John Kåre Alsaker44154502012-11-13 19:10:20 +01001552{
1553 output->border.top = gr->border.top;
1554 output->border.bottom = gr->border.bottom;
1555 output->border.left = gr->border.left;
1556 output->border.right = gr->border.right;
1557}
1558
1559WL_EXPORT void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001560gl_renderer_set_border(struct weston_compositor *ec, int32_t width, int32_t height, void *data,
John Kåre Alsaker44154502012-11-13 19:10:20 +01001561 int32_t *edges)
1562{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001563 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker44154502012-11-13 19:10:20 +01001564 struct weston_output *output;
1565
1566 gr->border.left = edges[0];
1567 gr->border.right = edges[1];
1568 gr->border.top = edges[2];
1569 gr->border.bottom = edges[3];
1570
1571 gr->border.width = width;
1572 gr->border.height = height;
1573
1574 glGenTextures(1, &gr->border.texture);
1575 glBindTexture(GL_TEXTURE_2D, gr->border.texture);
1576 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1577 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1578 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1579 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1580
1581 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1582 width,
1583 height,
1584 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1585 data);
1586
1587 wl_list_for_each(output, &ec->output_list, link)
1588 output_apply_border(output, gr);
1589}
1590
John Kåre Alsaker94659272012-11-13 19:10:18 +01001591static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001592gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001593
1594WL_EXPORT int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001595gl_renderer_output_create(struct weston_output *output,
John Kåre Alsaker94659272012-11-13 19:10:18 +01001596 EGLNativeWindowType window)
1597{
1598 struct weston_compositor *ec = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001599 struct gl_renderer *gr = get_renderer(ec);
1600 struct gl_output_state *go = calloc(1, sizeof *go);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001601 int i;
John Kåre Alsaker94659272012-11-13 19:10:18 +01001602
1603 if (!go)
1604 return -1;
1605
1606 go->egl_surface =
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001607 eglCreateWindowSurface(gr->egl_display,
1608 gr->egl_config,
John Kåre Alsaker94659272012-11-13 19:10:18 +01001609 window, NULL);
1610
1611 if (go->egl_surface == EGL_NO_SURFACE) {
1612 weston_log("failed to create egl surface\n");
1613 free(go);
1614 return -1;
1615 }
1616
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001617 if (gr->egl_context == NULL)
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001618 if (gl_renderer_setup(ec, go->egl_surface) < 0) {
John Kåre Alsaker94659272012-11-13 19:10:18 +01001619 free(go);
1620 return -1;
1621 }
1622
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001623 go->current_buffer = 0;
1624 for (i = 0; i < 2; i++)
1625 pixman_region32_init(&go->buffer_damage[i]);
1626
John Kåre Alsaker94659272012-11-13 19:10:18 +01001627 output->renderer_state = go;
1628
John Kåre Alsaker44154502012-11-13 19:10:20 +01001629 output_apply_border(output, gr);
1630
John Kåre Alsaker94659272012-11-13 19:10:18 +01001631 return 0;
1632}
1633
1634WL_EXPORT void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001635gl_renderer_output_destroy(struct weston_output *output)
John Kåre Alsaker94659272012-11-13 19:10:18 +01001636{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001637 struct gl_renderer *gr = get_renderer(output->compositor);
1638 struct gl_output_state *go = get_output_state(output);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001639 int i;
1640
1641 for (i = 0; i < 2; i++)
1642 pixman_region32_fini(&go->buffer_damage[i]);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001643
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001644 eglDestroySurface(gr->egl_display, go->egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001645
1646 free(go);
1647}
1648
1649WL_EXPORT EGLSurface
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001650gl_renderer_output_surface(struct weston_output *output)
John Kåre Alsaker94659272012-11-13 19:10:18 +01001651{
1652 return get_output_state(output)->egl_surface;
1653}
1654
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03001655static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001656gl_renderer_destroy(struct weston_compositor *ec)
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001657{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001658 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001659
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001660 if (gr->has_bind_display)
1661 gr->unbind_display(gr->egl_display, ec->wl_display);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001662
1663 /* Work around crash in egl_dri2.c's dri2_make_current() - when does this apply? */
1664 eglMakeCurrent(gr->egl_display,
1665 EGL_NO_SURFACE, EGL_NO_SURFACE,
1666 EGL_NO_CONTEXT);
1667
1668 eglTerminate(gr->egl_display);
1669 eglReleaseThread();
1670}
1671
1672static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001673egl_choose_config(struct gl_renderer *gr, const EGLint *attribs,
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001674 const EGLint *visual_id)
1675{
1676 EGLint count = 0;
1677 EGLint matched = 0;
1678 EGLConfig *configs;
1679 int i;
1680
1681 if (!eglGetConfigs(gr->egl_display, NULL, 0, &count) || count < 1)
1682 return -1;
1683
1684 configs = calloc(count, sizeof *configs);
1685 if (!configs)
1686 return -1;
1687
1688 if (!eglChooseConfig(gr->egl_display, attribs, configs,
1689 count, &matched))
1690 goto out;
1691
1692 for (i = 0; i < matched; ++i) {
1693 EGLint id;
1694
1695 if (visual_id) {
1696 if (!eglGetConfigAttrib(gr->egl_display,
1697 configs[i], EGL_NATIVE_VISUAL_ID,
1698 &id))
1699 continue;
1700
1701 if (id != *visual_id)
1702 continue;
1703 }
1704
1705 gr->egl_config = configs[i];
1706
1707 free(configs);
1708 return 0;
1709 }
1710
1711out:
1712 free(configs);
1713 return -1;
1714}
1715
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001716WL_EXPORT const EGLint gl_renderer_opaque_attribs[] = {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001717 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1718 EGL_RED_SIZE, 1,
1719 EGL_GREEN_SIZE, 1,
1720 EGL_BLUE_SIZE, 1,
1721 EGL_ALPHA_SIZE, 0,
1722 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1723 EGL_NONE
1724};
1725
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001726WL_EXPORT const EGLint gl_renderer_alpha_attribs[] = {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001727 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1728 EGL_RED_SIZE, 1,
1729 EGL_GREEN_SIZE, 1,
1730 EGL_BLUE_SIZE, 1,
1731 EGL_ALPHA_SIZE, 1,
1732 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1733 EGL_NONE
1734};
1735
1736WL_EXPORT int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001737gl_renderer_create(struct weston_compositor *ec, EGLNativeDisplayType display,
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001738 const EGLint *attribs, const EGLint *visual_id)
1739{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001740 struct gl_renderer *gr;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001741 EGLint major, minor;
1742
1743 gr = calloc(1, sizeof *gr);
1744
1745 if (gr == NULL)
1746 return -1;
1747
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001748 gr->base.read_pixels = gl_renderer_read_pixels;
1749 gr->base.repaint_output = gl_renderer_repaint_output;
1750 gr->base.flush_damage = gl_renderer_flush_damage;
1751 gr->base.attach = gl_renderer_attach;
1752 gr->base.create_surface = gl_renderer_create_surface;
1753 gr->base.surface_set_color = gl_renderer_surface_set_color;
1754 gr->base.destroy_surface = gl_renderer_destroy_surface;
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03001755 gr->base.destroy = gl_renderer_destroy;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001756
1757 gr->egl_display = eglGetDisplay(display);
1758 if (gr->egl_display == EGL_NO_DISPLAY) {
1759 weston_log("failed to create display\n");
1760 goto err_egl;
1761 }
1762
1763 if (!eglInitialize(gr->egl_display, &major, &minor)) {
1764 weston_log("failed to initialize display\n");
1765 goto err_egl;
1766 }
1767
1768 if (egl_choose_config(gr, attribs, visual_id) < 0) {
1769 weston_log("failed to choose EGL config\n");
1770 goto err_egl;
1771 }
1772
1773 ec->renderer = &gr->base;
1774
1775 return 0;
1776
1777err_egl:
Pekka Paalanen326529f2012-11-27 12:25:25 +02001778 gl_renderer_print_egl_error_state();
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001779 free(gr);
1780 return -1;
1781}
1782
1783WL_EXPORT EGLDisplay
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001784gl_renderer_display(struct weston_compositor *ec)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001785{
1786 return get_renderer(ec)->egl_display;
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001787}
1788
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001789static int
1790compile_shaders(struct weston_compositor *ec)
1791{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001792 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker40684142012-11-13 19:10:25 +01001793
1794 if (shader_init(&gr->texture_shader_rgba, ec,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001795 vertex_shader, texture_fragment_shader_rgba) < 0)
1796 return -1;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001797 if (shader_init(&gr->texture_shader_rgbx, ec,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001798 vertex_shader, texture_fragment_shader_rgbx) < 0)
1799 return -1;
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001800 if (gr->has_egl_image_external &&
John Kåre Alsaker40684142012-11-13 19:10:25 +01001801 shader_init(&gr->texture_shader_egl_external, ec,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001802 vertex_shader, texture_fragment_shader_egl_external) < 0)
1803 return -1;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001804 if (shader_init(&gr->texture_shader_y_uv, ec,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001805 vertex_shader, texture_fragment_shader_y_uv) < 0)
1806 return -1;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001807 if (shader_init(&gr->texture_shader_y_u_v, ec,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001808 vertex_shader, texture_fragment_shader_y_u_v) < 0)
1809 return -1;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001810 if (shader_init(&gr->texture_shader_y_xuxv, ec,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001811 vertex_shader, texture_fragment_shader_y_xuxv) < 0)
1812 return -1;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001813 if (shader_init(&gr->solid_shader, ec,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001814 vertex_shader, solid_fragment_shader) < 0)
1815 return -1;
1816
1817 return 0;
1818}
1819
1820static void
1821fragment_debug_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
1822 void *data)
1823{
1824 struct weston_compositor *ec = data;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001825 struct gl_renderer *gr = get_renderer(ec);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001826 struct weston_output *output;
1827
John Kåre Alsaker40684142012-11-13 19:10:25 +01001828 gr->fragment_shader_debug ^= 1;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001829
John Kåre Alsaker40684142012-11-13 19:10:25 +01001830 shader_release(&gr->texture_shader_rgba);
1831 shader_release(&gr->texture_shader_rgbx);
1832 shader_release(&gr->texture_shader_egl_external);
1833 shader_release(&gr->texture_shader_y_uv);
1834 shader_release(&gr->texture_shader_y_u_v);
1835 shader_release(&gr->texture_shader_y_xuxv);
1836 shader_release(&gr->solid_shader);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001837
1838 compile_shaders(ec);
1839
Ander Conselvan de Oliveira03fb4ef2012-12-03 17:08:11 +02001840 /* Force use_shader() to call glUseProgram(), since we need to use
1841 * the recompiled version of the shader. */
1842 gr->current_shader = NULL;
1843
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001844 wl_list_for_each(output, &ec->output_list, link)
1845 weston_output_damage(output);
1846}
1847
John Kåre Alsaker94659272012-11-13 19:10:18 +01001848static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001849gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001850{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001851 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001852 const char *extensions;
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001853 EGLBoolean ret;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001854
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001855 static const EGLint context_attribs[] = {
1856 EGL_CONTEXT_CLIENT_VERSION, 2,
1857 EGL_NONE
1858 };
1859
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001860 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
1861 weston_log("failed to bind EGL_OPENGL_ES_API\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02001862 gl_renderer_print_egl_error_state();
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001863 return -1;
1864 }
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001865
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001866 log_egl_config_info(gr->egl_display, gr->egl_config);
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001867
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001868 gr->egl_context = eglCreateContext(gr->egl_display, gr->egl_config,
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001869 EGL_NO_CONTEXT, context_attribs);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001870 if (gr->egl_context == NULL) {
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001871 weston_log("failed to create context\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02001872 gl_renderer_print_egl_error_state();
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001873 return -1;
1874 }
1875
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001876 ret = eglMakeCurrent(gr->egl_display, egl_surface,
1877 egl_surface, gr->egl_context);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001878 if (ret == EGL_FALSE) {
1879 weston_log("Failed to make EGL context current.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02001880 gl_renderer_print_egl_error_state();
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001881 return -1;
1882 }
1883
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001884 log_egl_gl_info(gr->egl_display);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001885
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001886 gr->image_target_texture_2d =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001887 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001888 gr->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1889 gr->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1890 gr->bind_display =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001891 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001892 gr->unbind_display =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001893 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001894 gr->query_buffer =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001895 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
1896
1897 extensions = (const char *) glGetString(GL_EXTENSIONS);
1898 if (!extensions) {
1899 weston_log("Retrieving GL extension string failed.\n");
1900 return -1;
1901 }
1902
1903 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
1904 weston_log("GL_EXT_texture_format_BGRA8888 not available\n");
1905 return -1;
1906 }
1907
1908 if (strstr(extensions, "GL_EXT_read_format_bgra"))
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01001909 ec->read_format = PIXMAN_a8r8g8b8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001910 else
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01001911 ec->read_format = PIXMAN_a8b8g8r8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001912
1913 if (strstr(extensions, "GL_EXT_unpack_subimage"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001914 gr->has_unpack_subimage = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001915
1916 if (strstr(extensions, "GL_OES_EGL_image_external"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001917 gr->has_egl_image_external = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001918
1919 extensions =
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001920 (const char *) eglQueryString(gr->egl_display, EGL_EXTENSIONS);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001921 if (!extensions) {
1922 weston_log("Retrieving EGL extension string failed.\n");
1923 return -1;
1924 }
1925
1926 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001927 gr->has_bind_display = 1;
1928 if (gr->has_bind_display) {
1929 ret = gr->bind_display(gr->egl_display, ec->wl_display);
Pekka Paalanen035a0322012-10-24 09:43:06 +03001930 if (!ret)
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001931 gr->has_bind_display = 0;
Pekka Paalanen035a0322012-10-24 09:43:06 +03001932 }
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001933
1934 glActiveTexture(GL_TEXTURE0);
1935
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001936 if (compile_shaders(ec))
1937 return -1;
1938
1939 weston_compositor_add_debug_binding(ec, KEY_S,
1940 fragment_debug_binding, ec);
1941
Pekka Paalanen035a0322012-10-24 09:43:06 +03001942 weston_log("GL ES 2 renderer features:\n");
1943 weston_log_continue(STAMP_SPACE "read-back format: %s\n",
Pekka Paalanenfe4eacf2013-01-10 16:50:42 +02001944 ec->read_format == PIXMAN_a8r8g8b8 ? "BGRA" : "RGBA");
Pekka Paalanen035a0322012-10-24 09:43:06 +03001945 weston_log_continue(STAMP_SPACE "wl_shm sub-image to texture: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001946 gr->has_unpack_subimage ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03001947 weston_log_continue(STAMP_SPACE "EGL Wayland extension: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001948 gr->has_bind_display ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03001949
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001950
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001951 return 0;
1952}