blob: a3785f84520702f37ba9394bd1ec7d079b45ac3a [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;
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +030047 const char *vertex_source, *fragment_source;
John Kåre Alsaker40684142012-11-13 19:10:25 +010048};
49
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +020050#define BUFFER_DAMAGE_COUNT 2
51
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010052struct gl_output_state {
John Kåre Alsaker94659272012-11-13 19:10:18 +010053 EGLSurface egl_surface;
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +020054 pixman_region32_t buffer_damage[BUFFER_DAMAGE_COUNT];
John Kåre Alsaker94659272012-11-13 19:10:18 +010055};
56
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010057struct gl_surface_state {
John Kåre Alsaker878f4492012-11-13 19:10:23 +010058 GLfloat color[4];
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010059 struct gl_shader *shader;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +010060
61 GLuint textures[3];
62 int num_textures;
Pekka Paalanen81ee3f52012-12-04 15:58:16 +020063 pixman_region32_t texture_damage;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +010064
65 EGLImageKHR images[3];
66 GLenum target;
67 int num_images;
Pekka Paalanenfb003d32012-12-04 15:58:13 +020068
69 struct weston_buffer_reference buffer_ref;
Pekka Paalanen68033ac2012-12-04 15:58:15 +020070 int pitch; /* in pixels */
Alexander Larsson4ea95522013-05-22 14:41:37 +020071 int height; /* in pixels */
John Kåre Alsaker878f4492012-11-13 19:10:23 +010072};
73
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010074struct gl_renderer {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +010075 struct weston_renderer base;
76 int fragment_shader_debug;
Kristian Høgsberg8799d412013-05-07 10:50:09 -040077 int fan_debug;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +010078
79 EGLDisplay egl_display;
80 EGLContext egl_context;
81 EGLConfig egl_config;
John Kåre Alsaker44154502012-11-13 19:10:20 +010082
83 struct {
84 int32_t top, bottom, left, right;
85 GLuint texture;
86 int32_t width, height;
87 } border;
John Kåre Alsaker40684142012-11-13 19:10:25 +010088
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -040089 struct wl_array vertices;
90 struct wl_array indices; /* only used in compositor-wayland */
91 struct wl_array vtxcnt;
92
John Kåre Alsaker320711d2012-11-13 19:10:27 +010093 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
94 PFNEGLCREATEIMAGEKHRPROC create_image;
95 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
96
97 int has_unpack_subimage;
98
99 PFNEGLBINDWAYLANDDISPLAYWL bind_display;
100 PFNEGLUNBINDWAYLANDDISPLAYWL unbind_display;
101 PFNEGLQUERYWAYLANDBUFFERWL query_buffer;
102 int has_bind_display;
103
104 int has_egl_image_external;
105
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +0200106 int has_egl_buffer_age;
107
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100108 struct gl_shader texture_shader_rgba;
109 struct gl_shader texture_shader_rgbx;
110 struct gl_shader texture_shader_egl_external;
111 struct gl_shader texture_shader_y_uv;
112 struct gl_shader texture_shader_y_u_v;
113 struct gl_shader texture_shader_y_xuxv;
114 struct gl_shader invert_color_shader;
115 struct gl_shader solid_shader;
116 struct gl_shader *current_shader;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100117};
John Kåre Alsaker94659272012-11-13 19:10:18 +0100118
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100119static inline struct gl_output_state *
John Kåre Alsaker94659272012-11-13 19:10:18 +0100120get_output_state(struct weston_output *output)
121{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100122 return (struct gl_output_state *)output->renderer_state;
John Kåre Alsaker94659272012-11-13 19:10:18 +0100123}
124
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100125static inline struct gl_surface_state *
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100126get_surface_state(struct weston_surface *surface)
127{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100128 return (struct gl_surface_state *)surface->renderer_state;
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100129}
130
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100131static inline struct gl_renderer *
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100132get_renderer(struct weston_compositor *ec)
133{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100134 return (struct gl_renderer *)ec->renderer;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100135}
136
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400137static const char *
138egl_error_string(EGLint code)
139{
140#define MYERRCODE(x) case x: return #x;
141 switch (code) {
142 MYERRCODE(EGL_SUCCESS)
143 MYERRCODE(EGL_NOT_INITIALIZED)
144 MYERRCODE(EGL_BAD_ACCESS)
145 MYERRCODE(EGL_BAD_ALLOC)
146 MYERRCODE(EGL_BAD_ATTRIBUTE)
147 MYERRCODE(EGL_BAD_CONTEXT)
148 MYERRCODE(EGL_BAD_CONFIG)
149 MYERRCODE(EGL_BAD_CURRENT_SURFACE)
150 MYERRCODE(EGL_BAD_DISPLAY)
151 MYERRCODE(EGL_BAD_SURFACE)
152 MYERRCODE(EGL_BAD_MATCH)
153 MYERRCODE(EGL_BAD_PARAMETER)
154 MYERRCODE(EGL_BAD_NATIVE_PIXMAP)
155 MYERRCODE(EGL_BAD_NATIVE_WINDOW)
156 MYERRCODE(EGL_CONTEXT_LOST)
157 default:
158 return "unknown";
159 }
160#undef MYERRCODE
161}
162
Pekka Paalanen326529f2012-11-27 12:25:25 +0200163WL_EXPORT void
164gl_renderer_print_egl_error_state(void)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400165{
166 EGLint code;
167
168 code = eglGetError();
169 weston_log("EGL error state: %s (0x%04lx)\n",
170 egl_error_string(code), (long)code);
171}
172
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300173struct polygon8 {
174 GLfloat x[8];
175 GLfloat y[8];
176 int n;
177};
178
179struct clip_context {
180 struct {
181 GLfloat x;
182 GLfloat y;
183 } prev;
184
185 struct {
186 GLfloat x1, y1;
187 GLfloat x2, y2;
188 } clip;
189
190 struct {
191 GLfloat *x;
192 GLfloat *y;
193 } vertices;
194};
195
196static GLfloat
197float_difference(GLfloat a, GLfloat b)
198{
199 /* http://www.altdevblogaday.com/2012/02/22/comparing-floating-point-numbers-2012-edition/ */
200 static const GLfloat max_diff = 4.0f * FLT_MIN;
201 static const GLfloat max_rel_diff = 4.0e-5;
202 GLfloat diff = a - b;
203 GLfloat adiff = fabsf(diff);
204
205 if (adiff <= max_diff)
206 return 0.0f;
207
208 a = fabsf(a);
209 b = fabsf(b);
210 if (adiff <= (a > b ? a : b) * max_rel_diff)
211 return 0.0f;
212
213 return diff;
214}
215
216/* A line segment (p1x, p1y)-(p2x, p2y) intersects the line x = x_arg.
217 * Compute the y coordinate of the intersection.
218 */
219static GLfloat
220clip_intersect_y(GLfloat p1x, GLfloat p1y, GLfloat p2x, GLfloat p2y,
221 GLfloat x_arg)
222{
223 GLfloat a;
224 GLfloat diff = float_difference(p1x, p2x);
225
226 /* Practically vertical line segment, yet the end points have already
227 * been determined to be on different sides of the line. Therefore
228 * the line segment is part of the line and intersects everywhere.
229 * Return the end point, so we use the whole line segment.
230 */
231 if (diff == 0.0f)
232 return p2y;
233
234 a = (x_arg - p2x) / diff;
235 return p2y + (p1y - p2y) * a;
236}
237
238/* A line segment (p1x, p1y)-(p2x, p2y) intersects the line y = y_arg.
239 * Compute the x coordinate of the intersection.
240 */
241static GLfloat
242clip_intersect_x(GLfloat p1x, GLfloat p1y, GLfloat p2x, GLfloat p2y,
243 GLfloat y_arg)
244{
245 GLfloat a;
246 GLfloat diff = float_difference(p1y, p2y);
247
248 /* Practically horizontal line segment, yet the end points have already
249 * been determined to be on different sides of the line. Therefore
250 * the line segment is part of the line and intersects everywhere.
251 * Return the end point, so we use the whole line segment.
252 */
253 if (diff == 0.0f)
254 return p2x;
255
256 a = (y_arg - p2y) / diff;
257 return p2x + (p1x - p2x) * a;
258}
259
260enum path_transition {
261 PATH_TRANSITION_OUT_TO_OUT = 0,
262 PATH_TRANSITION_OUT_TO_IN = 1,
263 PATH_TRANSITION_IN_TO_OUT = 2,
264 PATH_TRANSITION_IN_TO_IN = 3,
265};
266
267static void
268clip_append_vertex(struct clip_context *ctx, GLfloat x, GLfloat y)
269{
270 *ctx->vertices.x++ = x;
271 *ctx->vertices.y++ = y;
272}
273
274static enum path_transition
275path_transition_left_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
276{
277 return ((ctx->prev.x >= ctx->clip.x1) << 1) | (x >= ctx->clip.x1);
278}
279
280static enum path_transition
281path_transition_right_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
282{
283 return ((ctx->prev.x < ctx->clip.x2) << 1) | (x < ctx->clip.x2);
284}
285
286static enum path_transition
287path_transition_top_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
288{
289 return ((ctx->prev.y >= ctx->clip.y1) << 1) | (y >= ctx->clip.y1);
290}
291
292static enum path_transition
293path_transition_bottom_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
294{
295 return ((ctx->prev.y < ctx->clip.y2) << 1) | (y < ctx->clip.y2);
296}
297
298static void
299clip_polygon_leftright(struct clip_context *ctx,
300 enum path_transition transition,
301 GLfloat x, GLfloat y, GLfloat clip_x)
302{
303 GLfloat yi;
304
305 switch (transition) {
306 case PATH_TRANSITION_IN_TO_IN:
307 clip_append_vertex(ctx, x, y);
308 break;
309 case PATH_TRANSITION_IN_TO_OUT:
310 yi = clip_intersect_y(ctx->prev.x, ctx->prev.y, x, y, clip_x);
311 clip_append_vertex(ctx, clip_x, yi);
312 break;
313 case PATH_TRANSITION_OUT_TO_IN:
314 yi = clip_intersect_y(ctx->prev.x, ctx->prev.y, x, y, clip_x);
315 clip_append_vertex(ctx, clip_x, yi);
316 clip_append_vertex(ctx, x, y);
317 break;
318 case PATH_TRANSITION_OUT_TO_OUT:
319 /* nothing */
320 break;
321 default:
322 assert(0 && "bad enum path_transition");
323 }
324
325 ctx->prev.x = x;
326 ctx->prev.y = y;
327}
328
329static void
330clip_polygon_topbottom(struct clip_context *ctx,
331 enum path_transition transition,
332 GLfloat x, GLfloat y, GLfloat clip_y)
333{
334 GLfloat xi;
335
336 switch (transition) {
337 case PATH_TRANSITION_IN_TO_IN:
338 clip_append_vertex(ctx, x, y);
339 break;
340 case PATH_TRANSITION_IN_TO_OUT:
341 xi = clip_intersect_x(ctx->prev.x, ctx->prev.y, x, y, clip_y);
342 clip_append_vertex(ctx, xi, clip_y);
343 break;
344 case PATH_TRANSITION_OUT_TO_IN:
345 xi = clip_intersect_x(ctx->prev.x, ctx->prev.y, x, y, clip_y);
346 clip_append_vertex(ctx, xi, clip_y);
347 clip_append_vertex(ctx, x, y);
348 break;
349 case PATH_TRANSITION_OUT_TO_OUT:
350 /* nothing */
351 break;
352 default:
353 assert(0 && "bad enum path_transition");
354 }
355
356 ctx->prev.x = x;
357 ctx->prev.y = y;
358}
359
360static void
361clip_context_prepare(struct clip_context *ctx, const struct polygon8 *src,
362 GLfloat *dst_x, GLfloat *dst_y)
363{
364 ctx->prev.x = src->x[src->n - 1];
365 ctx->prev.y = src->y[src->n - 1];
366 ctx->vertices.x = dst_x;
367 ctx->vertices.y = dst_y;
368}
369
370static int
371clip_polygon_left(struct clip_context *ctx, const struct polygon8 *src,
372 GLfloat *dst_x, GLfloat *dst_y)
373{
374 enum path_transition trans;
375 int i;
376
377 clip_context_prepare(ctx, src, dst_x, dst_y);
378 for (i = 0; i < src->n; i++) {
379 trans = path_transition_left_edge(ctx, src->x[i], src->y[i]);
380 clip_polygon_leftright(ctx, trans, src->x[i], src->y[i],
381 ctx->clip.x1);
382 }
383 return ctx->vertices.x - dst_x;
384}
385
386static int
387clip_polygon_right(struct clip_context *ctx, const struct polygon8 *src,
388 GLfloat *dst_x, GLfloat *dst_y)
389{
390 enum path_transition trans;
391 int i;
392
393 clip_context_prepare(ctx, src, dst_x, dst_y);
394 for (i = 0; i < src->n; i++) {
395 trans = path_transition_right_edge(ctx, src->x[i], src->y[i]);
396 clip_polygon_leftright(ctx, trans, src->x[i], src->y[i],
397 ctx->clip.x2);
398 }
399 return ctx->vertices.x - dst_x;
400}
401
402static int
403clip_polygon_top(struct clip_context *ctx, const struct polygon8 *src,
404 GLfloat *dst_x, GLfloat *dst_y)
405{
406 enum path_transition trans;
407 int i;
408
409 clip_context_prepare(ctx, src, dst_x, dst_y);
410 for (i = 0; i < src->n; i++) {
411 trans = path_transition_top_edge(ctx, src->x[i], src->y[i]);
412 clip_polygon_topbottom(ctx, trans, src->x[i], src->y[i],
413 ctx->clip.y1);
414 }
415 return ctx->vertices.x - dst_x;
416}
417
418static int
419clip_polygon_bottom(struct clip_context *ctx, const struct polygon8 *src,
420 GLfloat *dst_x, GLfloat *dst_y)
421{
422 enum path_transition trans;
423 int i;
424
425 clip_context_prepare(ctx, src, dst_x, dst_y);
426 for (i = 0; i < src->n; i++) {
427 trans = path_transition_bottom_edge(ctx, src->x[i], src->y[i]);
428 clip_polygon_topbottom(ctx, trans, src->x[i], src->y[i],
429 ctx->clip.y2);
430 }
431 return ctx->vertices.x - dst_x;
432}
433
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400434#define max(a, b) (((a) > (b)) ? (a) : (b))
435#define min(a, b) (((a) > (b)) ? (b) : (a))
436#define clip(x, a, b) min(max(x, a), b)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400437
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300438/*
439 * Compute the boundary vertices of the intersection of the global coordinate
440 * aligned rectangle 'rect', and an arbitrary quadrilateral produced from
441 * 'surf_rect' when transformed from surface coordinates into global coordinates.
442 * The vertices are written to 'ex' and 'ey', and the return value is the
443 * number of vertices. Vertices are produced in clockwise winding order.
444 * Guarantees to produce either zero vertices, or 3-8 vertices with non-zero
445 * polygon area.
446 */
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400447static int
448calculate_edges(struct weston_surface *es, pixman_box32_t *rect,
449 pixman_box32_t *surf_rect, GLfloat *ex, GLfloat *ey)
450{
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300451 struct polygon8 polygon;
452 struct clip_context ctx;
453 int i, n;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400454 GLfloat min_x, max_x, min_y, max_y;
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300455 struct polygon8 surf = {
456 { surf_rect->x1, surf_rect->x2, surf_rect->x2, surf_rect->x1 },
457 { surf_rect->y1, surf_rect->y1, surf_rect->y2, surf_rect->y2 },
458 4
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400459 };
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400460
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300461 ctx.clip.x1 = rect->x1;
462 ctx.clip.y1 = rect->y1;
463 ctx.clip.x2 = rect->x2;
464 ctx.clip.y2 = rect->y2;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400465
466 /* transform surface to screen space: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300467 for (i = 0; i < surf.n; i++)
468 weston_surface_to_global_float(es, surf.x[i], surf.y[i],
469 &surf.x[i], &surf.y[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400470
471 /* find bounding box: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300472 min_x = max_x = surf.x[0];
473 min_y = max_y = surf.y[0];
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400474
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300475 for (i = 1; i < surf.n; i++) {
476 min_x = min(min_x, surf.x[i]);
477 max_x = max(max_x, surf.x[i]);
478 min_y = min(min_y, surf.y[i]);
479 max_y = max(max_y, surf.y[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400480 }
481
482 /* First, simple bounding box check to discard early transformed
483 * surface rects that do not intersect with the clip region:
484 */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300485 if ((min_x >= ctx.clip.x2) || (max_x <= ctx.clip.x1) ||
486 (min_y >= ctx.clip.y2) || (max_y <= ctx.clip.y1))
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400487 return 0;
488
489 /* Simple case, bounding box edges are parallel to surface edges,
490 * there will be only four edges. We just need to clip the surface
491 * vertices to the clip rect bounds:
492 */
493 if (!es->transform.enabled) {
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300494 for (i = 0; i < surf.n; i++) {
495 ex[i] = clip(surf.x[i], ctx.clip.x1, ctx.clip.x2);
496 ey[i] = clip(surf.y[i], ctx.clip.y1, ctx.clip.y2);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400497 }
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300498 return surf.n;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400499 }
500
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300501 /* Transformed case: use a general polygon clipping algorithm to
502 * clip the surface rectangle with each side of 'rect'.
503 * The algorithm is Sutherland-Hodgman, as explained in
504 * http://www.codeguru.com/cpp/misc/misc/graphics/article.php/c8965/Polygon-Clipping.htm
505 * but without looking at any of that code.
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400506 */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300507 polygon.n = clip_polygon_left(&ctx, &surf, polygon.x, polygon.y);
508 surf.n = clip_polygon_right(&ctx, &polygon, surf.x, surf.y);
509 polygon.n = clip_polygon_top(&ctx, &surf, polygon.x, polygon.y);
510 surf.n = clip_polygon_bottom(&ctx, &polygon, surf.x, surf.y);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400511
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300512 /* Get rid of duplicate vertices */
513 ex[0] = surf.x[0];
514 ey[0] = surf.y[0];
515 n = 1;
516 for (i = 1; i < surf.n; i++) {
517 if (float_difference(ex[n - 1], surf.x[i]) == 0.0f &&
518 float_difference(ey[n - 1], surf.y[i]) == 0.0f)
519 continue;
520 ex[n] = surf.x[i];
521 ey[n] = surf.y[i];
522 n++;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400523 }
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300524 if (float_difference(ex[n - 1], surf.x[0]) == 0.0f &&
525 float_difference(ey[n - 1], surf.y[0]) == 0.0f)
526 n--;
527
528 if (n < 3)
529 return 0;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400530
531 return n;
532}
533
534static int
535texture_region(struct weston_surface *es, pixman_region32_t *region,
536 pixman_region32_t *surf_region)
537{
Pekka Paalanen68033ac2012-12-04 15:58:15 +0200538 struct gl_surface_state *gs = get_surface_state(es);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400539 struct weston_compositor *ec = es->compositor;
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400540 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400541 GLfloat *v, inv_width, inv_height;
542 unsigned int *vtxcnt, nvtx = 0;
543 pixman_box32_t *rects, *surf_rects;
544 int i, j, k, nrects, nsurf;
545
546 rects = pixman_region32_rectangles(region, &nrects);
547 surf_rects = pixman_region32_rectangles(surf_region, &nsurf);
548
549 /* worst case we can have 8 vertices per rect (ie. clipped into
550 * an octagon):
551 */
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400552 v = wl_array_add(&gr->vertices, nrects * nsurf * 8 * 4 * sizeof *v);
553 vtxcnt = wl_array_add(&gr->vtxcnt, nrects * nsurf * sizeof *vtxcnt);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400554
Pekka Paalanen68033ac2012-12-04 15:58:15 +0200555 inv_width = 1.0 / gs->pitch;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200556 inv_height = 1.0 / gs->height;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400557
558 for (i = 0; i < nrects; i++) {
559 pixman_box32_t *rect = &rects[i];
560 for (j = 0; j < nsurf; j++) {
561 pixman_box32_t *surf_rect = &surf_rects[j];
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200562 GLfloat sx, sy, bx, by;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400563 GLfloat ex[8], ey[8]; /* edge points in screen space */
564 int n;
565
566 /* The transformed surface, after clipping to the clip region,
567 * can have as many as eight sides, emitted as a triangle-fan.
568 * The first vertex in the triangle fan can be chosen arbitrarily,
569 * since the area is guaranteed to be convex.
570 *
571 * If a corner of the transformed surface falls outside of the
572 * clip region, instead of emitting one vertex for the corner
573 * of the surface, up to two are emitted for two corresponding
574 * intersection point(s) between the surface and the clip region.
575 *
576 * To do this, we first calculate the (up to eight) points that
577 * form the intersection of the clip rect and the transformed
578 * surface.
579 */
580 n = calculate_edges(es, rect, surf_rect, ex, ey);
581 if (n < 3)
582 continue;
583
584 /* emit edge points: */
585 for (k = 0; k < n; k++) {
586 weston_surface_from_global_float(es, ex[k], ey[k], &sx, &sy);
587 /* position: */
588 *(v++) = ex[k];
589 *(v++) = ey[k];
590 /* texcoord: */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200591 weston_surface_to_buffer_float(es, sx, sy,
592 &bx, &by);
593 *(v++) = bx * inv_width;
594 *(v++) = by * inv_height;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400595 }
596
597 vtxcnt[nvtx++] = n;
598 }
599 }
600
601 return nvtx;
602}
603
604static void
605triangle_fan_debug(struct weston_surface *surface, int first, int count)
606{
607 struct weston_compositor *compositor = surface->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100608 struct gl_renderer *gr = get_renderer(compositor);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400609 int i;
610 GLushort *buffer;
611 GLushort *index;
612 int nelems;
613 static int color_idx = 0;
614 static const GLfloat color[][4] = {
615 { 1.0, 0.0, 0.0, 1.0 },
616 { 0.0, 1.0, 0.0, 1.0 },
617 { 0.0, 0.0, 1.0, 1.0 },
618 { 1.0, 1.0, 1.0, 1.0 },
619 };
620
621 nelems = (count - 1 + count - 2) * 2;
622
623 buffer = malloc(sizeof(GLushort) * nelems);
624 index = buffer;
625
626 for (i = 1; i < count; i++) {
627 *index++ = first;
628 *index++ = first + i;
629 }
630
631 for (i = 2; i < count; i++) {
632 *index++ = first + i - 1;
633 *index++ = first + i;
634 }
635
John Kåre Alsaker40684142012-11-13 19:10:25 +0100636 glUseProgram(gr->solid_shader.program);
637 glUniform4fv(gr->solid_shader.color_uniform, 1,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400638 color[color_idx++ % ARRAY_LENGTH(color)]);
639 glDrawElements(GL_LINES, nelems, GL_UNSIGNED_SHORT, buffer);
John Kåre Alsaker40684142012-11-13 19:10:25 +0100640 glUseProgram(gr->current_shader->program);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400641 free(buffer);
642}
643
644static void
645repaint_region(struct weston_surface *es, pixman_region32_t *region,
646 pixman_region32_t *surf_region)
647{
648 struct weston_compositor *ec = es->compositor;
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400649 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400650 GLfloat *v;
651 unsigned int *vtxcnt;
652 int i, first, nfans;
653
654 /* The final region to be painted is the intersection of
655 * 'region' and 'surf_region'. However, 'region' is in the global
656 * coordinates, and 'surf_region' is in the surface-local
657 * coordinates. texture_region() will iterate over all pairs of
658 * rectangles from both regions, compute the intersection
659 * polygon for each pair, and store it as a triangle fan if
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400660 * it has a non-zero area (at least 3 vertices1, actually).
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400661 */
662 nfans = texture_region(es, region, surf_region);
663
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400664 v = gr->vertices.data;
665 vtxcnt = gr->vtxcnt.data;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400666
667 /* position: */
668 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
669 glEnableVertexAttribArray(0);
670
671 /* texcoord: */
672 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
673 glEnableVertexAttribArray(1);
674
675 for (i = 0, first = 0; i < nfans; i++) {
676 glDrawArrays(GL_TRIANGLE_FAN, first, vtxcnt[i]);
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400677 if (gr->fan_debug)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400678 triangle_fan_debug(es, first, vtxcnt[i]);
679 first += vtxcnt[i];
680 }
681
682 glDisableVertexAttribArray(1);
683 glDisableVertexAttribArray(0);
684
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400685 gr->vertices.size = 0;
686 gr->vtxcnt.size = 0;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400687}
688
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100689static int
690use_output(struct weston_output *output)
691{
692 static int errored;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100693 struct gl_output_state *go = get_output_state(output);
694 struct gl_renderer *gr = get_renderer(output->compositor);
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100695 EGLBoolean ret;
696
697 ret = eglMakeCurrent(gr->egl_display, go->egl_surface,
698 go->egl_surface, gr->egl_context);
699
700 if (ret == EGL_FALSE) {
701 if (errored)
702 return -1;
703 errored = 1;
704 weston_log("Failed to make EGL context current.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +0200705 gl_renderer_print_egl_error_state();
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100706 return -1;
707 }
708
709 return 0;
710}
711
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +0300712static int
713shader_init(struct gl_shader *shader, struct gl_renderer *gr,
714 const char *vertex_source, const char *fragment_source);
715
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400716static void
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +0300717use_shader(struct gl_renderer *gr, struct gl_shader *shader)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400718{
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +0300719 if (!shader->program) {
720 int ret;
721
722 ret = shader_init(shader, gr,
723 shader->vertex_source,
724 shader->fragment_source);
725
726 if (ret < 0)
727 weston_log("warning: failed to compile shader\n");
728 }
729
John Kåre Alsaker40684142012-11-13 19:10:25 +0100730 if (gr->current_shader == shader)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400731 return;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400732 glUseProgram(shader->program);
John Kåre Alsaker40684142012-11-13 19:10:25 +0100733 gr->current_shader = shader;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400734}
735
736static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100737shader_uniforms(struct gl_shader *shader,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400738 struct weston_surface *surface,
739 struct weston_output *output)
740{
741 int i;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100742 struct gl_surface_state *gs = get_surface_state(surface);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400743
744 glUniformMatrix4fv(shader->proj_uniform,
745 1, GL_FALSE, output->matrix.d);
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100746 glUniform4fv(shader->color_uniform, 1, gs->color);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400747 glUniform1f(shader->alpha_uniform, surface->alpha);
748
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100749 for (i = 0; i < gs->num_textures; i++)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400750 glUniform1i(shader->tex_uniforms[i], i);
751}
752
753static void
754draw_surface(struct weston_surface *es, struct weston_output *output,
755 pixman_region32_t *damage) /* in global coordinates */
756{
757 struct weston_compositor *ec = es->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100758 struct gl_renderer *gr = get_renderer(ec);
759 struct gl_surface_state *gs = get_surface_state(es);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400760 /* repaint bounding region in global coordinates: */
761 pixman_region32_t repaint;
762 /* non-opaque region in surface coordinates: */
763 pixman_region32_t surface_blend;
764 GLint filter;
765 int i;
766
767 pixman_region32_init(&repaint);
768 pixman_region32_intersect(&repaint,
769 &es->transform.boundingbox, damage);
770 pixman_region32_subtract(&repaint, &repaint, &es->clip);
771
772 if (!pixman_region32_not_empty(&repaint))
773 goto out;
774
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400775 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
776
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400777 if (gr->fan_debug) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100778 use_shader(gr, &gr->solid_shader);
779 shader_uniforms(&gr->solid_shader, es, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400780 }
781
John Kåre Alsaker40684142012-11-13 19:10:25 +0100782 use_shader(gr, gs->shader);
783 shader_uniforms(gs->shader, es, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400784
Alexander Larsson4ea95522013-05-22 14:41:37 +0200785 if (es->transform.enabled || output->zoom.active || output->scale != es->buffer_scale)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400786 filter = GL_LINEAR;
787 else
788 filter = GL_NEAREST;
789
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100790 for (i = 0; i < gs->num_textures; i++) {
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400791 glActiveTexture(GL_TEXTURE0 + i);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100792 glBindTexture(gs->target, gs->textures[i]);
793 glTexParameteri(gs->target, GL_TEXTURE_MIN_FILTER, filter);
794 glTexParameteri(gs->target, GL_TEXTURE_MAG_FILTER, filter);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400795 }
796
797 /* blended region is whole surface minus opaque region: */
798 pixman_region32_init_rect(&surface_blend, 0, 0,
799 es->geometry.width, es->geometry.height);
800 pixman_region32_subtract(&surface_blend, &surface_blend, &es->opaque);
801
802 if (pixman_region32_not_empty(&es->opaque)) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100803 if (gs->shader == &gr->texture_shader_rgba) {
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400804 /* Special case for RGBA textures with possibly
805 * bad data in alpha channel: use the shader
806 * that forces texture alpha = 1.0.
807 * Xwayland surfaces need this.
808 */
John Kåre Alsaker40684142012-11-13 19:10:25 +0100809 use_shader(gr, &gr->texture_shader_rgbx);
810 shader_uniforms(&gr->texture_shader_rgbx, es, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400811 }
812
813 if (es->alpha < 1.0)
814 glEnable(GL_BLEND);
815 else
816 glDisable(GL_BLEND);
817
818 repaint_region(es, &repaint, &es->opaque);
819 }
820
821 if (pixman_region32_not_empty(&surface_blend)) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100822 use_shader(gr, gs->shader);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400823 glEnable(GL_BLEND);
824 repaint_region(es, &repaint, &surface_blend);
825 }
826
827 pixman_region32_fini(&surface_blend);
828
829out:
830 pixman_region32_fini(&repaint);
831}
832
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400833static void
834repaint_surfaces(struct weston_output *output, pixman_region32_t *damage)
835{
836 struct weston_compositor *compositor = output->compositor;
837 struct weston_surface *surface;
838
839 wl_list_for_each_reverse(surface, &compositor->surface_list, link)
840 if (surface->plane == &compositor->primary_plane)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400841 draw_surface(surface, output, damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400842}
843
John Kåre Alsaker44154502012-11-13 19:10:20 +0100844
845static int
846texture_border(struct weston_output *output)
847{
848 struct weston_compositor *ec = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100849 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker44154502012-11-13 19:10:20 +0100850 GLfloat *d;
851 unsigned int *p;
852 int i, j, k, n;
853 GLfloat x[4], y[4], u[4], v[4];
854
855 x[0] = -gr->border.left;
856 x[1] = 0;
857 x[2] = output->current->width;
858 x[3] = output->current->width + gr->border.right;
859
860 y[0] = -gr->border.top;
861 y[1] = 0;
862 y[2] = output->current->height;
863 y[3] = output->current->height + gr->border.bottom;
864
865 u[0] = 0.0;
866 u[1] = (GLfloat) gr->border.left / gr->border.width;
867 u[2] = (GLfloat) (gr->border.width - gr->border.right) / gr->border.width;
868 u[3] = 1.0;
869
870 v[0] = 0.0;
871 v[1] = (GLfloat) gr->border.top / gr->border.height;
872 v[2] = (GLfloat) (gr->border.height - gr->border.bottom) / gr->border.height;
873 v[3] = 1.0;
874
875 n = 8;
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400876 d = wl_array_add(&gr->vertices, n * 16 * sizeof *d);
877 p = wl_array_add(&gr->indices, n * 6 * sizeof *p);
John Kåre Alsaker44154502012-11-13 19:10:20 +0100878
879 k = 0;
880 for (i = 0; i < 3; i++)
881 for (j = 0; j < 3; j++) {
882
883 if (i == 1 && j == 1)
884 continue;
885
886 d[ 0] = x[i];
887 d[ 1] = y[j];
888 d[ 2] = u[i];
889 d[ 3] = v[j];
890
891 d[ 4] = x[i];
892 d[ 5] = y[j + 1];
893 d[ 6] = u[i];
894 d[ 7] = v[j + 1];
895
896 d[ 8] = x[i + 1];
897 d[ 9] = y[j];
898 d[10] = u[i + 1];
899 d[11] = v[j];
900
901 d[12] = x[i + 1];
902 d[13] = y[j + 1];
903 d[14] = u[i + 1];
904 d[15] = v[j + 1];
905
906 p[0] = k + 0;
907 p[1] = k + 1;
908 p[2] = k + 2;
909 p[3] = k + 2;
910 p[4] = k + 1;
911 p[5] = k + 3;
912
913 d += 16;
914 p += 6;
915 k += 4;
916 }
917
918 return k / 4;
919}
920
921static void
922draw_border(struct weston_output *output)
923{
924 struct weston_compositor *ec = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100925 struct gl_renderer *gr = get_renderer(ec);
926 struct gl_shader *shader = &gr->texture_shader_rgba;
John Kåre Alsaker44154502012-11-13 19:10:20 +0100927 GLfloat *v;
928 int n;
929
930 glDisable(GL_BLEND);
John Kåre Alsaker40684142012-11-13 19:10:25 +0100931 use_shader(gr, shader);
John Kåre Alsaker44154502012-11-13 19:10:20 +0100932
933 glUniformMatrix4fv(shader->proj_uniform,
934 1, GL_FALSE, output->matrix.d);
935
936 glUniform1i(shader->tex_uniforms[0], 0);
937 glUniform1f(shader->alpha_uniform, 1);
938
939 n = texture_border(output);
940
941 glActiveTexture(GL_TEXTURE0);
942 glBindTexture(GL_TEXTURE_2D, gr->border.texture);
943
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400944 v = gr->vertices.data;
John Kåre Alsaker44154502012-11-13 19:10:20 +0100945 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
946 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
947 glEnableVertexAttribArray(0);
948 glEnableVertexAttribArray(1);
949
950 glDrawElements(GL_TRIANGLES, n * 6,
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400951 GL_UNSIGNED_INT, gr->indices.data);
John Kåre Alsaker44154502012-11-13 19:10:20 +0100952
953 glDisableVertexAttribArray(1);
954 glDisableVertexAttribArray(0);
955
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400956 gr->vertices.size = 0;
957 gr->indices.size = 0;
John Kåre Alsaker44154502012-11-13 19:10:20 +0100958}
959
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400960static void
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +0200961output_get_buffer_damage(struct weston_output *output,
962 pixman_region32_t *buffer_damage)
963{
964 struct gl_output_state *go = get_output_state(output);
965 struct gl_renderer *gr = get_renderer(output->compositor);
966 EGLint buffer_age = 0;
967 EGLBoolean ret;
968 int i;
969
970 if (gr->has_egl_buffer_age) {
971 ret = eglQuerySurface(gr->egl_display, go->egl_surface,
972 EGL_BUFFER_AGE_EXT, &buffer_age);
973 if (ret == EGL_FALSE) {
974 weston_log("buffer age query failed.\n");
975 gl_renderer_print_egl_error_state();
976 }
977 }
978
979 if (buffer_age == 0 || buffer_age - 1 > BUFFER_DAMAGE_COUNT)
980 pixman_region32_copy(buffer_damage, &output->region);
981 else
982 for (i = 0; i < buffer_age - 1; i++)
983 pixman_region32_union(buffer_damage, buffer_damage,
984 &go->buffer_damage[i]);
985}
986
987static void
988output_rotate_damage(struct weston_output *output,
989 pixman_region32_t *output_damage)
990{
991 struct gl_output_state *go = get_output_state(output);
992 struct gl_renderer *gr = get_renderer(output->compositor);
993 int i;
994
995 if (!gr->has_egl_buffer_age)
996 return;
997
998 for (i = BUFFER_DAMAGE_COUNT - 1; i >= 1; i--)
999 pixman_region32_copy(&go->buffer_damage[i],
1000 &go->buffer_damage[i - 1]);
1001
1002 pixman_region32_copy(&go->buffer_damage[0], output_damage);
1003}
1004
1005static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001006gl_renderer_repaint_output(struct weston_output *output,
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001007 pixman_region32_t *output_damage)
1008{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001009 struct gl_output_state *go = get_output_state(output);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001010 struct weston_compositor *compositor = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001011 struct gl_renderer *gr = get_renderer(compositor);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001012 EGLBoolean ret;
1013 static int errored;
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001014 int32_t width, height;
1015 pixman_region32_t buffer_damage, total_damage;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001016
Alexander Larsson4ea95522013-05-22 14:41:37 +02001017 width = output->current->width * output->scale +
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001018 output->border.left + output->border.right;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001019 height = output->current->height * output->scale +
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001020 output->border.top + output->border.bottom;
1021
1022 glViewport(0, 0, width, height);
1023
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001024 if (use_output(output) < 0)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001025 return;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001026
1027 /* if debugging, redraw everything outside the damage to clean up
1028 * debug lines from the previous draw on this buffer:
1029 */
Kristian Høgsberg8799d412013-05-07 10:50:09 -04001030 if (gr->fan_debug) {
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001031 pixman_region32_t undamaged;
1032 pixman_region32_init(&undamaged);
1033 pixman_region32_subtract(&undamaged, &output->region,
1034 output_damage);
Kristian Høgsberg8799d412013-05-07 10:50:09 -04001035 gr->fan_debug = 0;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001036 repaint_surfaces(output, &undamaged);
Kristian Høgsberg8799d412013-05-07 10:50:09 -04001037 gr->fan_debug = 1;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001038 pixman_region32_fini(&undamaged);
1039 }
1040
Ander Conselvan de Oliveirab605c062013-03-05 17:30:28 +02001041 pixman_region32_init(&total_damage);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001042 pixman_region32_init(&buffer_damage);
1043
1044 output_get_buffer_damage(output, &buffer_damage);
1045 output_rotate_damage(output, output_damage);
1046
1047 pixman_region32_union(&total_damage, &buffer_damage, output_damage);
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03001048
Ander Conselvan de Oliveirab605c062013-03-05 17:30:28 +02001049 repaint_surfaces(output, &total_damage);
1050
1051 pixman_region32_fini(&total_damage);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001052 pixman_region32_fini(&buffer_damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001053
John Kåre Alsaker44154502012-11-13 19:10:20 +01001054 if (gr->border.texture)
1055 draw_border(output);
1056
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001057 pixman_region32_copy(&output->previous_damage, output_damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001058 wl_signal_emit(&output->frame_signal, output);
1059
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001060 ret = eglSwapBuffers(gr->egl_display, go->egl_surface);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001061 if (ret == EGL_FALSE && !errored) {
1062 errored = 1;
1063 weston_log("Failed in eglSwapBuffers.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02001064 gl_renderer_print_egl_error_state();
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001065 }
1066
1067}
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001068
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001069static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001070gl_renderer_read_pixels(struct weston_output *output,
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001071 pixman_format_code_t format, void *pixels,
1072 uint32_t x, uint32_t y,
1073 uint32_t width, uint32_t height)
1074{
1075 GLenum gl_format;
1076
1077 switch (format) {
1078 case PIXMAN_a8r8g8b8:
1079 gl_format = GL_BGRA_EXT;
1080 break;
1081 case PIXMAN_a8b8g8r8:
1082 gl_format = GL_RGBA;
1083 break;
1084 default:
1085 return -1;
1086 }
1087
1088 if (use_output(output) < 0)
1089 return -1;
1090
1091 glPixelStorei(GL_PACK_ALIGNMENT, 1);
1092 glReadPixels(x, y, width, height, gl_format,
1093 GL_UNSIGNED_BYTE, pixels);
1094
1095 return 0;
1096}
1097
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001098static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001099gl_renderer_flush_damage(struct weston_surface *surface)
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001100{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001101 struct gl_renderer *gr = get_renderer(surface->compositor);
1102 struct gl_surface_state *gs = get_surface_state(surface);
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001103 struct wl_buffer *buffer = gs->buffer_ref.buffer;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001104
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001105#ifdef GL_UNPACK_ROW_LENGTH
1106 pixman_box32_t *rectangles;
1107 void *data;
1108 int i, n;
1109#endif
1110
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001111 pixman_region32_union(&gs->texture_damage,
1112 &gs->texture_damage, &surface->damage);
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001113
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001114 if (!buffer)
1115 return;
1116
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001117 /* Avoid upload, if the texture won't be used this time.
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001118 * We still accumulate the damage in texture_damage, and
1119 * hold the reference to the buffer, in case the surface
1120 * migrates back to the primary plane.
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001121 */
1122 if (surface->plane != &surface->compositor->primary_plane)
1123 return;
1124
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001125 if (!pixman_region32_not_empty(&gs->texture_damage))
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001126 goto done;
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001127
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001128 glBindTexture(GL_TEXTURE_2D, gs->textures[0]);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001129
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001130 if (!gr->has_unpack_subimage) {
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001131 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001132 gs->pitch, buffer->height, 0,
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001133 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
Pekka Paalanende685b82012-12-04 15:58:12 +02001134 wl_shm_buffer_get_data(buffer));
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001135
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001136 goto done;
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001137 }
1138
1139#ifdef GL_UNPACK_ROW_LENGTH
1140 /* Mesa does not define GL_EXT_unpack_subimage */
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001141 glPixelStorei(GL_UNPACK_ROW_LENGTH, gs->pitch);
Pekka Paalanende685b82012-12-04 15:58:12 +02001142 data = wl_shm_buffer_get_data(buffer);
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001143 rectangles = pixman_region32_rectangles(&gs->texture_damage, &n);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001144 for (i = 0; i < n; i++) {
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001145 pixman_box32_t r;
1146
1147 r = weston_surface_to_buffer_rect(surface, rectangles[i]);
1148
1149 glPixelStorei(GL_UNPACK_SKIP_PIXELS, r.x1);
1150 glPixelStorei(GL_UNPACK_SKIP_ROWS, r.y1);
1151 glTexSubImage2D(GL_TEXTURE_2D, 0, r.x1, r.y1,
1152 r.x2 - r.x1, r.y2 - r.y1,
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001153 GL_BGRA_EXT, GL_UNSIGNED_BYTE, data);
1154 }
1155#endif
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001156
1157done:
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001158 pixman_region32_fini(&gs->texture_damage);
1159 pixman_region32_init(&gs->texture_damage);
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001160
1161 weston_buffer_reference(&gs->buffer_ref, NULL);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001162}
1163
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001164static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001165ensure_textures(struct gl_surface_state *gs, int num_textures)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001166{
1167 int i;
1168
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001169 if (num_textures <= gs->num_textures)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001170 return;
1171
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001172 for (i = gs->num_textures; i < num_textures; i++) {
1173 glGenTextures(1, &gs->textures[i]);
1174 glBindTexture(gs->target, gs->textures[i]);
1175 glTexParameteri(gs->target,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001176 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001177 glTexParameteri(gs->target,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001178 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1179 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001180 gs->num_textures = num_textures;
1181 glBindTexture(gs->target, 0);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001182}
1183
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001184static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001185gl_renderer_attach(struct weston_surface *es, struct wl_buffer *buffer)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001186{
1187 struct weston_compositor *ec = es->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001188 struct gl_renderer *gr = get_renderer(ec);
1189 struct gl_surface_state *gs = get_surface_state(es);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001190 EGLint attribs[3], format;
1191 int i, num_planes;
1192
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001193 weston_buffer_reference(&gs->buffer_ref, buffer);
1194
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001195 if (!buffer) {
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001196 for (i = 0; i < gs->num_images; i++) {
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001197 gr->destroy_image(gr->egl_display, gs->images[i]);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001198 gs->images[i] = NULL;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001199 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001200 gs->num_images = 0;
1201 glDeleteTextures(gs->num_textures, gs->textures);
1202 gs->num_textures = 0;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001203 return;
1204 }
1205
1206 if (wl_buffer_is_shm(buffer)) {
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001207 gs->pitch = wl_shm_buffer_get_stride(buffer) / 4;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001208 gs->height = wl_shm_buffer_get_height(buffer);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001209 gs->target = GL_TEXTURE_2D;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001210
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001211 ensure_textures(gs, 1);
1212 glBindTexture(GL_TEXTURE_2D, gs->textures[0]);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001213 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001214 gs->pitch, buffer->height, 0,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001215 GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
1216 if (wl_shm_buffer_get_format(buffer) == WL_SHM_FORMAT_XRGB8888)
John Kåre Alsaker40684142012-11-13 19:10:25 +01001217 gs->shader = &gr->texture_shader_rgbx;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001218 else
John Kåre Alsaker40684142012-11-13 19:10:25 +01001219 gs->shader = &gr->texture_shader_rgba;
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001220 } else if (gr->query_buffer(gr->egl_display, buffer,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001221 EGL_TEXTURE_FORMAT, &format)) {
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001222 for (i = 0; i < gs->num_images; i++)
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001223 gr->destroy_image(gr->egl_display, gs->images[i]);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001224 gs->num_images = 0;
1225 gs->target = GL_TEXTURE_2D;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001226 switch (format) {
1227 case EGL_TEXTURE_RGB:
1228 case EGL_TEXTURE_RGBA:
1229 default:
1230 num_planes = 1;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001231 gs->shader = &gr->texture_shader_rgba;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001232 break;
1233 case EGL_TEXTURE_EXTERNAL_WL:
1234 num_planes = 1;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001235 gs->target = GL_TEXTURE_EXTERNAL_OES;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001236 gs->shader = &gr->texture_shader_egl_external;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001237 break;
1238 case EGL_TEXTURE_Y_UV_WL:
1239 num_planes = 2;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001240 gs->shader = &gr->texture_shader_y_uv;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001241 break;
1242 case EGL_TEXTURE_Y_U_V_WL:
1243 num_planes = 3;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001244 gs->shader = &gr->texture_shader_y_u_v;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001245 break;
1246 case EGL_TEXTURE_Y_XUXV_WL:
1247 num_planes = 2;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001248 gs->shader = &gr->texture_shader_y_xuxv;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001249 break;
1250 }
1251
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001252 ensure_textures(gs, num_planes);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001253 for (i = 0; i < num_planes; i++) {
1254 attribs[0] = EGL_WAYLAND_PLANE_WL;
1255 attribs[1] = i;
1256 attribs[2] = EGL_NONE;
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001257 gs->images[i] = gr->create_image(gr->egl_display,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001258 NULL,
1259 EGL_WAYLAND_BUFFER_WL,
1260 buffer, attribs);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001261 if (!gs->images[i]) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001262 weston_log("failed to create img for plane %d\n", i);
1263 continue;
1264 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001265 gs->num_images++;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001266
1267 glActiveTexture(GL_TEXTURE0 + i);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001268 glBindTexture(gs->target, gs->textures[i]);
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001269 gr->image_target_texture_2d(gs->target,
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001270 gs->images[i]);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001271 }
1272
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001273 gs->pitch = buffer->width;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001274 gs->height = buffer->height;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001275 } else {
1276 weston_log("unhandled buffer type!\n");
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001277 weston_buffer_reference(&gs->buffer_ref, NULL);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001278 }
1279}
1280
Kristian Høgsberg42263852012-09-06 21:59:29 -04001281static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001282gl_renderer_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001283 float red, float green, float blue, float alpha)
1284{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001285 struct gl_surface_state *gs = get_surface_state(surface);
1286 struct gl_renderer *gr = get_renderer(surface->compositor);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001287
1288 gs->color[0] = red;
1289 gs->color[1] = green;
1290 gs->color[2] = blue;
1291 gs->color[3] = alpha;
1292
John Kåre Alsaker40684142012-11-13 19:10:25 +01001293 gs->shader = &gr->solid_shader;
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001294}
1295
1296static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001297gl_renderer_create_surface(struct weston_surface *surface)
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001298{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001299 struct gl_surface_state *gs;
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001300
1301 gs = calloc(1, sizeof *gs);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001302 if (!gs)
1303 return -1;
1304
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001305 /* A buffer is never attached to solid color surfaces, yet
1306 * they still go through texcoord computations. Do not divide
1307 * by zero there.
1308 */
1309 gs->pitch = 1;
1310
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001311 pixman_region32_init(&gs->texture_damage);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001312 surface->renderer_state = gs;
1313
1314 return 0;
1315}
1316
1317static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001318gl_renderer_destroy_surface(struct weston_surface *surface)
Kristian Høgsberg42263852012-09-06 21:59:29 -04001319{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001320 struct gl_surface_state *gs = get_surface_state(surface);
1321 struct gl_renderer *gr = get_renderer(surface->compositor);
Kristian Høgsberg42263852012-09-06 21:59:29 -04001322 int i;
1323
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001324 glDeleteTextures(gs->num_textures, gs->textures);
Kristian Høgsberg42263852012-09-06 21:59:29 -04001325
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001326 for (i = 0; i < gs->num_images; i++)
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001327 gr->destroy_image(gr->egl_display, gs->images[i]);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001328
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001329 weston_buffer_reference(&gs->buffer_ref, NULL);
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001330 pixman_region32_fini(&gs->texture_damage);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001331 free(gs);
Kristian Høgsberg42263852012-09-06 21:59:29 -04001332}
1333
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001334static const char vertex_shader[] =
1335 "uniform mat4 proj;\n"
1336 "attribute vec2 position;\n"
1337 "attribute vec2 texcoord;\n"
1338 "varying vec2 v_texcoord;\n"
1339 "void main()\n"
1340 "{\n"
1341 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
1342 " v_texcoord = texcoord;\n"
1343 "}\n";
1344
1345/* Declare common fragment shader uniforms */
1346#define FRAGMENT_CONVERT_YUV \
1347 " y *= alpha;\n" \
1348 " u *= alpha;\n" \
1349 " v *= alpha;\n" \
1350 " gl_FragColor.r = y + 1.59602678 * v;\n" \
1351 " gl_FragColor.g = y - 0.39176229 * u - 0.81296764 * v;\n" \
1352 " gl_FragColor.b = y + 2.01723214 * u;\n" \
1353 " gl_FragColor.a = alpha;\n"
1354
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001355static const char fragment_debug[] =
1356 " gl_FragColor = vec4(0.0, 0.3, 0.0, 0.2) + gl_FragColor * 0.8;\n";
1357
1358static const char fragment_brace[] =
1359 "}\n";
1360
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001361static const char texture_fragment_shader_rgba[] =
1362 "precision mediump float;\n"
1363 "varying vec2 v_texcoord;\n"
1364 "uniform sampler2D tex;\n"
1365 "uniform float alpha;\n"
1366 "void main()\n"
1367 "{\n"
1368 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001369 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001370
1371static const char texture_fragment_shader_rgbx[] =
1372 "precision mediump float;\n"
1373 "varying vec2 v_texcoord;\n"
1374 "uniform sampler2D tex;\n"
1375 "uniform float alpha;\n"
1376 "void main()\n"
1377 "{\n"
1378 " gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb\n;"
1379 " gl_FragColor.a = alpha;\n"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001380 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001381
1382static const char texture_fragment_shader_egl_external[] =
1383 "#extension GL_OES_EGL_image_external : require\n"
1384 "precision mediump float;\n"
1385 "varying vec2 v_texcoord;\n"
1386 "uniform samplerExternalOES tex;\n"
1387 "uniform float alpha;\n"
1388 "void main()\n"
1389 "{\n"
1390 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001391 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001392
1393static const char texture_fragment_shader_y_uv[] =
1394 "precision mediump float;\n"
1395 "uniform sampler2D tex;\n"
1396 "uniform sampler2D tex1;\n"
1397 "varying vec2 v_texcoord;\n"
1398 "uniform float alpha;\n"
1399 "void main() {\n"
1400 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1401 " float u = texture2D(tex1, v_texcoord).r - 0.5;\n"
1402 " float v = texture2D(tex1, v_texcoord).g - 0.5;\n"
1403 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001404 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001405
1406static const char texture_fragment_shader_y_u_v[] =
1407 "precision mediump float;\n"
1408 "uniform sampler2D tex;\n"
1409 "uniform sampler2D tex1;\n"
1410 "uniform sampler2D tex2;\n"
1411 "varying vec2 v_texcoord;\n"
1412 "uniform float alpha;\n"
1413 "void main() {\n"
1414 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1415 " float u = texture2D(tex1, v_texcoord).x - 0.5;\n"
1416 " float v = texture2D(tex2, v_texcoord).x - 0.5;\n"
1417 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001418 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001419
1420static const char texture_fragment_shader_y_xuxv[] =
1421 "precision mediump float;\n"
1422 "uniform sampler2D tex;\n"
1423 "uniform sampler2D tex1;\n"
1424 "varying vec2 v_texcoord;\n"
1425 "uniform float alpha;\n"
1426 "void main() {\n"
1427 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1428 " float u = texture2D(tex1, v_texcoord).g - 0.5;\n"
1429 " float v = texture2D(tex1, v_texcoord).a - 0.5;\n"
1430 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001431 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001432
1433static const char solid_fragment_shader[] =
1434 "precision mediump float;\n"
1435 "uniform vec4 color;\n"
1436 "uniform float alpha;\n"
1437 "void main()\n"
1438 "{\n"
1439 " gl_FragColor = alpha * color\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001440 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001441
1442static int
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001443compile_shader(GLenum type, int count, const char **sources)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001444{
1445 GLuint s;
1446 char msg[512];
1447 GLint status;
1448
1449 s = glCreateShader(type);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001450 glShaderSource(s, count, sources, NULL);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001451 glCompileShader(s);
1452 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
1453 if (!status) {
1454 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1455 weston_log("shader info: %s\n", msg);
1456 return GL_NONE;
1457 }
1458
1459 return s;
1460}
1461
1462static int
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03001463shader_init(struct gl_shader *shader, struct gl_renderer *renderer,
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001464 const char *vertex_source, const char *fragment_source)
1465{
1466 char msg[512];
1467 GLint status;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001468 int count;
1469 const char *sources[3];
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001470
1471 shader->vertex_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001472 compile_shader(GL_VERTEX_SHADER, 1, &vertex_source);
1473
1474 if (renderer->fragment_shader_debug) {
1475 sources[0] = fragment_source;
1476 sources[1] = fragment_debug;
1477 sources[2] = fragment_brace;
1478 count = 3;
1479 } else {
1480 sources[0] = fragment_source;
1481 sources[1] = fragment_brace;
1482 count = 2;
1483 }
1484
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001485 shader->fragment_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001486 compile_shader(GL_FRAGMENT_SHADER, count, sources);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001487
1488 shader->program = glCreateProgram();
1489 glAttachShader(shader->program, shader->vertex_shader);
1490 glAttachShader(shader->program, shader->fragment_shader);
1491 glBindAttribLocation(shader->program, 0, "position");
1492 glBindAttribLocation(shader->program, 1, "texcoord");
1493
1494 glLinkProgram(shader->program);
1495 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1496 if (!status) {
1497 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1498 weston_log("link info: %s\n", msg);
1499 return -1;
1500 }
1501
1502 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1503 shader->tex_uniforms[0] = glGetUniformLocation(shader->program, "tex");
1504 shader->tex_uniforms[1] = glGetUniformLocation(shader->program, "tex1");
1505 shader->tex_uniforms[2] = glGetUniformLocation(shader->program, "tex2");
1506 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
1507 shader->color_uniform = glGetUniformLocation(shader->program, "color");
1508
1509 return 0;
1510}
1511
1512static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001513shader_release(struct gl_shader *shader)
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001514{
1515 glDeleteShader(shader->vertex_shader);
1516 glDeleteShader(shader->fragment_shader);
1517 glDeleteProgram(shader->program);
1518
1519 shader->vertex_shader = 0;
1520 shader->fragment_shader = 0;
1521 shader->program = 0;
1522}
1523
1524static void
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001525log_extensions(const char *name, const char *extensions)
1526{
1527 const char *p, *end;
1528 int l;
1529 int len;
1530
1531 l = weston_log("%s:", name);
1532 p = extensions;
1533 while (*p) {
1534 end = strchrnul(p, ' ');
1535 len = end - p;
1536 if (l + len > 78)
1537 l = weston_log_continue("\n" STAMP_SPACE "%.*s",
1538 len, p);
1539 else
1540 l += weston_log_continue(" %.*s", len, p);
1541 for (p = end; isspace(*p); p++)
1542 ;
1543 }
1544 weston_log_continue("\n");
1545}
1546
1547static void
1548log_egl_gl_info(EGLDisplay egldpy)
1549{
1550 const char *str;
1551
1552 str = eglQueryString(egldpy, EGL_VERSION);
1553 weston_log("EGL version: %s\n", str ? str : "(null)");
1554
1555 str = eglQueryString(egldpy, EGL_VENDOR);
1556 weston_log("EGL vendor: %s\n", str ? str : "(null)");
1557
1558 str = eglQueryString(egldpy, EGL_CLIENT_APIS);
1559 weston_log("EGL client APIs: %s\n", str ? str : "(null)");
1560
1561 str = eglQueryString(egldpy, EGL_EXTENSIONS);
1562 log_extensions("EGL extensions", str ? str : "(null)");
1563
1564 str = (char *)glGetString(GL_VERSION);
1565 weston_log("GL version: %s\n", str ? str : "(null)");
1566
1567 str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
1568 weston_log("GLSL version: %s\n", str ? str : "(null)");
1569
1570 str = (char *)glGetString(GL_VENDOR);
1571 weston_log("GL vendor: %s\n", str ? str : "(null)");
1572
1573 str = (char *)glGetString(GL_RENDERER);
1574 weston_log("GL renderer: %s\n", str ? str : "(null)");
1575
1576 str = (char *)glGetString(GL_EXTENSIONS);
1577 log_extensions("GL extensions", str ? str : "(null)");
1578}
1579
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001580static void
1581log_egl_config_info(EGLDisplay egldpy, EGLConfig eglconfig)
1582{
1583 EGLint r, g, b, a;
1584
1585 weston_log("Chosen EGL config details:\n");
1586
1587 weston_log_continue(STAMP_SPACE "RGBA bits");
1588 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_RED_SIZE, &r) &&
1589 eglGetConfigAttrib(egldpy, eglconfig, EGL_GREEN_SIZE, &g) &&
1590 eglGetConfigAttrib(egldpy, eglconfig, EGL_BLUE_SIZE, &b) &&
1591 eglGetConfigAttrib(egldpy, eglconfig, EGL_ALPHA_SIZE, &a))
1592 weston_log_continue(": %d %d %d %d\n", r, g, b, a);
1593 else
1594 weston_log_continue(" unknown\n");
1595
1596 weston_log_continue(STAMP_SPACE "swap interval range");
1597 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_MIN_SWAP_INTERVAL, &a) &&
1598 eglGetConfigAttrib(egldpy, eglconfig, EGL_MAX_SWAP_INTERVAL, &b))
1599 weston_log_continue(": %d - %d\n", a, b);
1600 else
1601 weston_log_continue(" unknown\n");
1602}
1603
John Kåre Alsaker44154502012-11-13 19:10:20 +01001604static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001605output_apply_border(struct weston_output *output, struct gl_renderer *gr)
John Kåre Alsaker44154502012-11-13 19:10:20 +01001606{
1607 output->border.top = gr->border.top;
1608 output->border.bottom = gr->border.bottom;
1609 output->border.left = gr->border.left;
1610 output->border.right = gr->border.right;
1611}
1612
1613WL_EXPORT void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001614gl_renderer_set_border(struct weston_compositor *ec, int32_t width, int32_t height, void *data,
John Kåre Alsaker44154502012-11-13 19:10:20 +01001615 int32_t *edges)
1616{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001617 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker44154502012-11-13 19:10:20 +01001618 struct weston_output *output;
1619
1620 gr->border.left = edges[0];
1621 gr->border.right = edges[1];
1622 gr->border.top = edges[2];
1623 gr->border.bottom = edges[3];
1624
1625 gr->border.width = width;
1626 gr->border.height = height;
1627
1628 glGenTextures(1, &gr->border.texture);
1629 glBindTexture(GL_TEXTURE_2D, gr->border.texture);
1630 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1631 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1632 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1633 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1634
1635 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1636 width,
1637 height,
1638 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1639 data);
1640
1641 wl_list_for_each(output, &ec->output_list, link)
1642 output_apply_border(output, gr);
1643}
1644
John Kåre Alsaker94659272012-11-13 19:10:18 +01001645static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001646gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001647
1648WL_EXPORT int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001649gl_renderer_output_create(struct weston_output *output,
John Kåre Alsaker94659272012-11-13 19:10:18 +01001650 EGLNativeWindowType window)
1651{
1652 struct weston_compositor *ec = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001653 struct gl_renderer *gr = get_renderer(ec);
1654 struct gl_output_state *go = calloc(1, sizeof *go);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001655 int i;
John Kåre Alsaker94659272012-11-13 19:10:18 +01001656
1657 if (!go)
1658 return -1;
1659
1660 go->egl_surface =
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001661 eglCreateWindowSurface(gr->egl_display,
1662 gr->egl_config,
John Kåre Alsaker94659272012-11-13 19:10:18 +01001663 window, NULL);
1664
1665 if (go->egl_surface == EGL_NO_SURFACE) {
1666 weston_log("failed to create egl surface\n");
1667 free(go);
1668 return -1;
1669 }
1670
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001671 if (gr->egl_context == NULL)
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001672 if (gl_renderer_setup(ec, go->egl_surface) < 0) {
John Kåre Alsaker94659272012-11-13 19:10:18 +01001673 free(go);
1674 return -1;
1675 }
1676
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001677 for (i = 0; i < BUFFER_DAMAGE_COUNT; i++)
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001678 pixman_region32_init(&go->buffer_damage[i]);
1679
John Kåre Alsaker94659272012-11-13 19:10:18 +01001680 output->renderer_state = go;
1681
John Kåre Alsaker44154502012-11-13 19:10:20 +01001682 output_apply_border(output, gr);
1683
John Kåre Alsaker94659272012-11-13 19:10:18 +01001684 return 0;
1685}
1686
1687WL_EXPORT void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001688gl_renderer_output_destroy(struct weston_output *output)
John Kåre Alsaker94659272012-11-13 19:10:18 +01001689{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001690 struct gl_renderer *gr = get_renderer(output->compositor);
1691 struct gl_output_state *go = get_output_state(output);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001692 int i;
1693
1694 for (i = 0; i < 2; i++)
1695 pixman_region32_fini(&go->buffer_damage[i]);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001696
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001697 eglDestroySurface(gr->egl_display, go->egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001698
1699 free(go);
1700}
1701
1702WL_EXPORT EGLSurface
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001703gl_renderer_output_surface(struct weston_output *output)
John Kåre Alsaker94659272012-11-13 19:10:18 +01001704{
1705 return get_output_state(output)->egl_surface;
1706}
1707
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03001708static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001709gl_renderer_destroy(struct weston_compositor *ec)
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001710{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001711 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001712
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001713 if (gr->has_bind_display)
1714 gr->unbind_display(gr->egl_display, ec->wl_display);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001715
1716 /* Work around crash in egl_dri2.c's dri2_make_current() - when does this apply? */
1717 eglMakeCurrent(gr->egl_display,
1718 EGL_NO_SURFACE, EGL_NO_SURFACE,
1719 EGL_NO_CONTEXT);
1720
1721 eglTerminate(gr->egl_display);
1722 eglReleaseThread();
Scott Moreau976a0502013-03-07 10:15:17 -07001723
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -04001724 wl_array_release(&gr->vertices);
1725 wl_array_release(&gr->indices);
1726 wl_array_release(&gr->vtxcnt);
1727
Scott Moreau976a0502013-03-07 10:15:17 -07001728 free(gr);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001729}
1730
1731static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001732egl_choose_config(struct gl_renderer *gr, const EGLint *attribs,
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001733 const EGLint *visual_id)
1734{
1735 EGLint count = 0;
1736 EGLint matched = 0;
1737 EGLConfig *configs;
1738 int i;
1739
1740 if (!eglGetConfigs(gr->egl_display, NULL, 0, &count) || count < 1)
1741 return -1;
1742
1743 configs = calloc(count, sizeof *configs);
1744 if (!configs)
1745 return -1;
1746
1747 if (!eglChooseConfig(gr->egl_display, attribs, configs,
1748 count, &matched))
1749 goto out;
1750
1751 for (i = 0; i < matched; ++i) {
1752 EGLint id;
1753
1754 if (visual_id) {
1755 if (!eglGetConfigAttrib(gr->egl_display,
1756 configs[i], EGL_NATIVE_VISUAL_ID,
1757 &id))
1758 continue;
1759
1760 if (id != *visual_id)
1761 continue;
1762 }
1763
1764 gr->egl_config = configs[i];
1765
1766 free(configs);
1767 return 0;
1768 }
1769
1770out:
1771 free(configs);
1772 return -1;
1773}
1774
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001775WL_EXPORT const EGLint gl_renderer_opaque_attribs[] = {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001776 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1777 EGL_RED_SIZE, 1,
1778 EGL_GREEN_SIZE, 1,
1779 EGL_BLUE_SIZE, 1,
1780 EGL_ALPHA_SIZE, 0,
1781 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1782 EGL_NONE
1783};
1784
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001785WL_EXPORT const EGLint gl_renderer_alpha_attribs[] = {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001786 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1787 EGL_RED_SIZE, 1,
1788 EGL_GREEN_SIZE, 1,
1789 EGL_BLUE_SIZE, 1,
1790 EGL_ALPHA_SIZE, 1,
1791 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1792 EGL_NONE
1793};
1794
1795WL_EXPORT int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001796gl_renderer_create(struct weston_compositor *ec, EGLNativeDisplayType display,
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001797 const EGLint *attribs, const EGLint *visual_id)
1798{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001799 struct gl_renderer *gr;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001800 EGLint major, minor;
1801
1802 gr = calloc(1, sizeof *gr);
1803
1804 if (gr == NULL)
1805 return -1;
1806
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001807 gr->base.read_pixels = gl_renderer_read_pixels;
1808 gr->base.repaint_output = gl_renderer_repaint_output;
1809 gr->base.flush_damage = gl_renderer_flush_damage;
1810 gr->base.attach = gl_renderer_attach;
1811 gr->base.create_surface = gl_renderer_create_surface;
1812 gr->base.surface_set_color = gl_renderer_surface_set_color;
1813 gr->base.destroy_surface = gl_renderer_destroy_surface;
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03001814 gr->base.destroy = gl_renderer_destroy;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001815
1816 gr->egl_display = eglGetDisplay(display);
1817 if (gr->egl_display == EGL_NO_DISPLAY) {
1818 weston_log("failed to create display\n");
1819 goto err_egl;
1820 }
1821
1822 if (!eglInitialize(gr->egl_display, &major, &minor)) {
1823 weston_log("failed to initialize display\n");
1824 goto err_egl;
1825 }
1826
1827 if (egl_choose_config(gr, attribs, visual_id) < 0) {
1828 weston_log("failed to choose EGL config\n");
1829 goto err_egl;
1830 }
1831
1832 ec->renderer = &gr->base;
Pekka Paalanen7bb65102013-05-22 18:03:04 +03001833 ec->capabilities |= WESTON_CAP_ROTATION_ANY;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001834
1835 return 0;
1836
1837err_egl:
Pekka Paalanen326529f2012-11-27 12:25:25 +02001838 gl_renderer_print_egl_error_state();
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001839 free(gr);
1840 return -1;
1841}
1842
1843WL_EXPORT EGLDisplay
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001844gl_renderer_display(struct weston_compositor *ec)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001845{
1846 return get_renderer(ec)->egl_display;
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001847}
1848
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001849static int
1850compile_shaders(struct weston_compositor *ec)
1851{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001852 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker40684142012-11-13 19:10:25 +01001853
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03001854 gr->texture_shader_rgba.vertex_source = vertex_shader;
1855 gr->texture_shader_rgba.fragment_source = texture_fragment_shader_rgba;
1856
1857 gr->texture_shader_rgbx.vertex_source = vertex_shader;
1858 gr->texture_shader_rgbx.fragment_source = texture_fragment_shader_rgbx;
1859
1860 gr->texture_shader_egl_external.vertex_source = vertex_shader;
1861 gr->texture_shader_egl_external.fragment_source =
1862 texture_fragment_shader_egl_external;
1863
1864 gr->texture_shader_y_uv.vertex_source = vertex_shader;
1865 gr->texture_shader_y_uv.fragment_source = texture_fragment_shader_y_uv;
1866
1867 gr->texture_shader_y_u_v.vertex_source = vertex_shader;
1868 gr->texture_shader_y_u_v.fragment_source =
1869 texture_fragment_shader_y_u_v;
1870
1871 gr->texture_shader_y_u_v.vertex_source = vertex_shader;
1872 gr->texture_shader_y_xuxv.fragment_source =
1873 texture_fragment_shader_y_xuxv;
1874
1875 gr->solid_shader.vertex_source = vertex_shader;
1876 gr->solid_shader.fragment_source = solid_fragment_shader;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001877
1878 return 0;
1879}
1880
1881static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04001882fragment_debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001883 void *data)
1884{
1885 struct weston_compositor *ec = data;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001886 struct gl_renderer *gr = get_renderer(ec);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001887 struct weston_output *output;
1888
John Kåre Alsaker40684142012-11-13 19:10:25 +01001889 gr->fragment_shader_debug ^= 1;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001890
John Kåre Alsaker40684142012-11-13 19:10:25 +01001891 shader_release(&gr->texture_shader_rgba);
1892 shader_release(&gr->texture_shader_rgbx);
1893 shader_release(&gr->texture_shader_egl_external);
1894 shader_release(&gr->texture_shader_y_uv);
1895 shader_release(&gr->texture_shader_y_u_v);
1896 shader_release(&gr->texture_shader_y_xuxv);
1897 shader_release(&gr->solid_shader);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001898
Ander Conselvan de Oliveira03fb4ef2012-12-03 17:08:11 +02001899 /* Force use_shader() to call glUseProgram(), since we need to use
1900 * the recompiled version of the shader. */
1901 gr->current_shader = NULL;
1902
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001903 wl_list_for_each(output, &ec->output_list, link)
1904 weston_output_damage(output);
1905}
1906
Kristian Høgsberg8799d412013-05-07 10:50:09 -04001907static void
1908fan_debug_repaint_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
1909 void *data)
1910{
1911 struct weston_compositor *compositor = data;
1912 struct gl_renderer *gr = get_renderer(compositor);
1913
1914 gr->fan_debug = !gr->fan_debug;
1915 weston_compositor_damage_all(compositor);
1916}
1917
John Kåre Alsaker94659272012-11-13 19:10:18 +01001918static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001919gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001920{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001921 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001922 const char *extensions;
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001923 EGLBoolean ret;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001924
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001925 static const EGLint context_attribs[] = {
1926 EGL_CONTEXT_CLIENT_VERSION, 2,
1927 EGL_NONE
1928 };
1929
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001930 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
1931 weston_log("failed to bind EGL_OPENGL_ES_API\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02001932 gl_renderer_print_egl_error_state();
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001933 return -1;
1934 }
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001935
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001936 log_egl_config_info(gr->egl_display, gr->egl_config);
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001937
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001938 gr->egl_context = eglCreateContext(gr->egl_display, gr->egl_config,
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001939 EGL_NO_CONTEXT, context_attribs);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001940 if (gr->egl_context == NULL) {
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001941 weston_log("failed to create context\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02001942 gl_renderer_print_egl_error_state();
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001943 return -1;
1944 }
1945
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001946 ret = eglMakeCurrent(gr->egl_display, egl_surface,
1947 egl_surface, gr->egl_context);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001948 if (ret == EGL_FALSE) {
1949 weston_log("Failed to make EGL context current.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02001950 gl_renderer_print_egl_error_state();
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001951 return -1;
1952 }
1953
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001954 log_egl_gl_info(gr->egl_display);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001955
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001956 gr->image_target_texture_2d =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001957 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001958 gr->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1959 gr->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1960 gr->bind_display =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001961 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001962 gr->unbind_display =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001963 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001964 gr->query_buffer =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001965 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
1966
1967 extensions = (const char *) glGetString(GL_EXTENSIONS);
1968 if (!extensions) {
1969 weston_log("Retrieving GL extension string failed.\n");
1970 return -1;
1971 }
1972
1973 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
1974 weston_log("GL_EXT_texture_format_BGRA8888 not available\n");
1975 return -1;
1976 }
1977
1978 if (strstr(extensions, "GL_EXT_read_format_bgra"))
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01001979 ec->read_format = PIXMAN_a8r8g8b8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001980 else
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01001981 ec->read_format = PIXMAN_a8b8g8r8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001982
1983 if (strstr(extensions, "GL_EXT_unpack_subimage"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001984 gr->has_unpack_subimage = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001985
1986 if (strstr(extensions, "GL_OES_EGL_image_external"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001987 gr->has_egl_image_external = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001988
1989 extensions =
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001990 (const char *) eglQueryString(gr->egl_display, EGL_EXTENSIONS);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001991 if (!extensions) {
1992 weston_log("Retrieving EGL extension string failed.\n");
1993 return -1;
1994 }
1995
1996 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001997 gr->has_bind_display = 1;
1998 if (gr->has_bind_display) {
1999 ret = gr->bind_display(gr->egl_display, ec->wl_display);
Pekka Paalanen035a0322012-10-24 09:43:06 +03002000 if (!ret)
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002001 gr->has_bind_display = 0;
Pekka Paalanen035a0322012-10-24 09:43:06 +03002002 }
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002003
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02002004 if (strstr(extensions, "EGL_EXT_buffer_age"))
2005 gr->has_egl_buffer_age = 1;
2006 else
2007 weston_log("warning: EGL_EXT_buffer_age not supported. "
2008 "Performance could be affected.\n");
2009
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002010 glActiveTexture(GL_TEXTURE0);
2011
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002012 if (compile_shaders(ec))
2013 return -1;
2014
2015 weston_compositor_add_debug_binding(ec, KEY_S,
2016 fragment_debug_binding, ec);
Kristian Høgsberg8799d412013-05-07 10:50:09 -04002017 weston_compositor_add_debug_binding(ec, KEY_F,
2018 fan_debug_repaint_binding, ec);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002019
Pekka Paalanen035a0322012-10-24 09:43:06 +03002020 weston_log("GL ES 2 renderer features:\n");
2021 weston_log_continue(STAMP_SPACE "read-back format: %s\n",
Pekka Paalanenfe4eacf2013-01-10 16:50:42 +02002022 ec->read_format == PIXMAN_a8r8g8b8 ? "BGRA" : "RGBA");
Pekka Paalanen035a0322012-10-24 09:43:06 +03002023 weston_log_continue(STAMP_SPACE "wl_shm sub-image to texture: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002024 gr->has_unpack_subimage ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03002025 weston_log_continue(STAMP_SPACE "EGL Wayland extension: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002026 gr->has_bind_display ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03002027
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002028
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002029 return 0;
2030}