blob: 12451f8fa9138e64e6d0e9a6787bb53d69fa827b [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
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +020049#define BUFFER_DAMAGE_COUNT 2
50
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010051struct gl_output_state {
John Kåre Alsaker94659272012-11-13 19:10:18 +010052 EGLSurface egl_surface;
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +020053 pixman_region32_t buffer_damage[BUFFER_DAMAGE_COUNT];
John Kåre Alsaker94659272012-11-13 19:10:18 +010054};
55
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010056struct gl_surface_state {
John Kåre Alsaker878f4492012-11-13 19:10:23 +010057 GLfloat color[4];
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010058 struct gl_shader *shader;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +010059
60 GLuint textures[3];
61 int num_textures;
Pekka Paalanen81ee3f52012-12-04 15:58:16 +020062 pixman_region32_t texture_damage;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +010063
64 EGLImageKHR images[3];
65 GLenum target;
66 int num_images;
Pekka Paalanenfb003d32012-12-04 15:58:13 +020067
68 struct weston_buffer_reference buffer_ref;
Pekka Paalanen68033ac2012-12-04 15:58:15 +020069 int pitch; /* in pixels */
John Kåre Alsaker878f4492012-11-13 19:10:23 +010070};
71
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010072struct gl_renderer {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +010073 struct weston_renderer base;
74 int fragment_shader_debug;
Kristian Høgsberg8799d412013-05-07 10:50:09 -040075 int fan_debug;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +010076
77 EGLDisplay egl_display;
78 EGLContext egl_context;
79 EGLConfig egl_config;
John Kåre Alsaker44154502012-11-13 19:10:20 +010080
81 struct {
82 int32_t top, bottom, left, right;
83 GLuint texture;
84 int32_t width, height;
85 } border;
John Kåre Alsaker40684142012-11-13 19:10:25 +010086
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -040087 struct wl_array vertices;
88 struct wl_array indices; /* only used in compositor-wayland */
89 struct wl_array vtxcnt;
90
John Kåre Alsaker320711d2012-11-13 19:10:27 +010091 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
92 PFNEGLCREATEIMAGEKHRPROC create_image;
93 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
94
95 int has_unpack_subimage;
96
97 PFNEGLBINDWAYLANDDISPLAYWL bind_display;
98 PFNEGLUNBINDWAYLANDDISPLAYWL unbind_display;
99 PFNEGLQUERYWAYLANDBUFFERWL query_buffer;
100 int has_bind_display;
101
102 int has_egl_image_external;
103
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +0200104 int has_egl_buffer_age;
105
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100106 struct gl_shader texture_shader_rgba;
107 struct gl_shader texture_shader_rgbx;
108 struct gl_shader texture_shader_egl_external;
109 struct gl_shader texture_shader_y_uv;
110 struct gl_shader texture_shader_y_u_v;
111 struct gl_shader texture_shader_y_xuxv;
112 struct gl_shader invert_color_shader;
113 struct gl_shader solid_shader;
114 struct gl_shader *current_shader;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100115};
John Kåre Alsaker94659272012-11-13 19:10:18 +0100116
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100117static inline struct gl_output_state *
John Kåre Alsaker94659272012-11-13 19:10:18 +0100118get_output_state(struct weston_output *output)
119{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100120 return (struct gl_output_state *)output->renderer_state;
John Kåre Alsaker94659272012-11-13 19:10:18 +0100121}
122
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100123static inline struct gl_surface_state *
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100124get_surface_state(struct weston_surface *surface)
125{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100126 return (struct gl_surface_state *)surface->renderer_state;
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100127}
128
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100129static inline struct gl_renderer *
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100130get_renderer(struct weston_compositor *ec)
131{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100132 return (struct gl_renderer *)ec->renderer;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100133}
134
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400135static const char *
136egl_error_string(EGLint code)
137{
138#define MYERRCODE(x) case x: return #x;
139 switch (code) {
140 MYERRCODE(EGL_SUCCESS)
141 MYERRCODE(EGL_NOT_INITIALIZED)
142 MYERRCODE(EGL_BAD_ACCESS)
143 MYERRCODE(EGL_BAD_ALLOC)
144 MYERRCODE(EGL_BAD_ATTRIBUTE)
145 MYERRCODE(EGL_BAD_CONTEXT)
146 MYERRCODE(EGL_BAD_CONFIG)
147 MYERRCODE(EGL_BAD_CURRENT_SURFACE)
148 MYERRCODE(EGL_BAD_DISPLAY)
149 MYERRCODE(EGL_BAD_SURFACE)
150 MYERRCODE(EGL_BAD_MATCH)
151 MYERRCODE(EGL_BAD_PARAMETER)
152 MYERRCODE(EGL_BAD_NATIVE_PIXMAP)
153 MYERRCODE(EGL_BAD_NATIVE_WINDOW)
154 MYERRCODE(EGL_CONTEXT_LOST)
155 default:
156 return "unknown";
157 }
158#undef MYERRCODE
159}
160
Pekka Paalanen326529f2012-11-27 12:25:25 +0200161WL_EXPORT void
162gl_renderer_print_egl_error_state(void)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400163{
164 EGLint code;
165
166 code = eglGetError();
167 weston_log("EGL error state: %s (0x%04lx)\n",
168 egl_error_string(code), (long)code);
169}
170
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300171struct polygon8 {
172 GLfloat x[8];
173 GLfloat y[8];
174 int n;
175};
176
177struct clip_context {
178 struct {
179 GLfloat x;
180 GLfloat y;
181 } prev;
182
183 struct {
184 GLfloat x1, y1;
185 GLfloat x2, y2;
186 } clip;
187
188 struct {
189 GLfloat *x;
190 GLfloat *y;
191 } vertices;
192};
193
194static GLfloat
195float_difference(GLfloat a, GLfloat b)
196{
197 /* http://www.altdevblogaday.com/2012/02/22/comparing-floating-point-numbers-2012-edition/ */
198 static const GLfloat max_diff = 4.0f * FLT_MIN;
199 static const GLfloat max_rel_diff = 4.0e-5;
200 GLfloat diff = a - b;
201 GLfloat adiff = fabsf(diff);
202
203 if (adiff <= max_diff)
204 return 0.0f;
205
206 a = fabsf(a);
207 b = fabsf(b);
208 if (adiff <= (a > b ? a : b) * max_rel_diff)
209 return 0.0f;
210
211 return diff;
212}
213
214/* A line segment (p1x, p1y)-(p2x, p2y) intersects the line x = x_arg.
215 * Compute the y coordinate of the intersection.
216 */
217static GLfloat
218clip_intersect_y(GLfloat p1x, GLfloat p1y, GLfloat p2x, GLfloat p2y,
219 GLfloat x_arg)
220{
221 GLfloat a;
222 GLfloat diff = float_difference(p1x, p2x);
223
224 /* Practically vertical line segment, yet the end points have already
225 * been determined to be on different sides of the line. Therefore
226 * the line segment is part of the line and intersects everywhere.
227 * Return the end point, so we use the whole line segment.
228 */
229 if (diff == 0.0f)
230 return p2y;
231
232 a = (x_arg - p2x) / diff;
233 return p2y + (p1y - p2y) * a;
234}
235
236/* A line segment (p1x, p1y)-(p2x, p2y) intersects the line y = y_arg.
237 * Compute the x coordinate of the intersection.
238 */
239static GLfloat
240clip_intersect_x(GLfloat p1x, GLfloat p1y, GLfloat p2x, GLfloat p2y,
241 GLfloat y_arg)
242{
243 GLfloat a;
244 GLfloat diff = float_difference(p1y, p2y);
245
246 /* Practically horizontal line segment, yet the end points have already
247 * been determined to be on different sides of the line. Therefore
248 * the line segment is part of the line and intersects everywhere.
249 * Return the end point, so we use the whole line segment.
250 */
251 if (diff == 0.0f)
252 return p2x;
253
254 a = (y_arg - p2y) / diff;
255 return p2x + (p1x - p2x) * a;
256}
257
258enum path_transition {
259 PATH_TRANSITION_OUT_TO_OUT = 0,
260 PATH_TRANSITION_OUT_TO_IN = 1,
261 PATH_TRANSITION_IN_TO_OUT = 2,
262 PATH_TRANSITION_IN_TO_IN = 3,
263};
264
265static void
266clip_append_vertex(struct clip_context *ctx, GLfloat x, GLfloat y)
267{
268 *ctx->vertices.x++ = x;
269 *ctx->vertices.y++ = y;
270}
271
272static enum path_transition
273path_transition_left_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
274{
275 return ((ctx->prev.x >= ctx->clip.x1) << 1) | (x >= ctx->clip.x1);
276}
277
278static enum path_transition
279path_transition_right_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
280{
281 return ((ctx->prev.x < ctx->clip.x2) << 1) | (x < ctx->clip.x2);
282}
283
284static enum path_transition
285path_transition_top_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
286{
287 return ((ctx->prev.y >= ctx->clip.y1) << 1) | (y >= ctx->clip.y1);
288}
289
290static enum path_transition
291path_transition_bottom_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
292{
293 return ((ctx->prev.y < ctx->clip.y2) << 1) | (y < ctx->clip.y2);
294}
295
296static void
297clip_polygon_leftright(struct clip_context *ctx,
298 enum path_transition transition,
299 GLfloat x, GLfloat y, GLfloat clip_x)
300{
301 GLfloat yi;
302
303 switch (transition) {
304 case PATH_TRANSITION_IN_TO_IN:
305 clip_append_vertex(ctx, x, y);
306 break;
307 case PATH_TRANSITION_IN_TO_OUT:
308 yi = clip_intersect_y(ctx->prev.x, ctx->prev.y, x, y, clip_x);
309 clip_append_vertex(ctx, clip_x, yi);
310 break;
311 case PATH_TRANSITION_OUT_TO_IN:
312 yi = clip_intersect_y(ctx->prev.x, ctx->prev.y, x, y, clip_x);
313 clip_append_vertex(ctx, clip_x, yi);
314 clip_append_vertex(ctx, x, y);
315 break;
316 case PATH_TRANSITION_OUT_TO_OUT:
317 /* nothing */
318 break;
319 default:
320 assert(0 && "bad enum path_transition");
321 }
322
323 ctx->prev.x = x;
324 ctx->prev.y = y;
325}
326
327static void
328clip_polygon_topbottom(struct clip_context *ctx,
329 enum path_transition transition,
330 GLfloat x, GLfloat y, GLfloat clip_y)
331{
332 GLfloat xi;
333
334 switch (transition) {
335 case PATH_TRANSITION_IN_TO_IN:
336 clip_append_vertex(ctx, x, y);
337 break;
338 case PATH_TRANSITION_IN_TO_OUT:
339 xi = clip_intersect_x(ctx->prev.x, ctx->prev.y, x, y, clip_y);
340 clip_append_vertex(ctx, xi, clip_y);
341 break;
342 case PATH_TRANSITION_OUT_TO_IN:
343 xi = clip_intersect_x(ctx->prev.x, ctx->prev.y, x, y, clip_y);
344 clip_append_vertex(ctx, xi, clip_y);
345 clip_append_vertex(ctx, x, y);
346 break;
347 case PATH_TRANSITION_OUT_TO_OUT:
348 /* nothing */
349 break;
350 default:
351 assert(0 && "bad enum path_transition");
352 }
353
354 ctx->prev.x = x;
355 ctx->prev.y = y;
356}
357
358static void
359clip_context_prepare(struct clip_context *ctx, const struct polygon8 *src,
360 GLfloat *dst_x, GLfloat *dst_y)
361{
362 ctx->prev.x = src->x[src->n - 1];
363 ctx->prev.y = src->y[src->n - 1];
364 ctx->vertices.x = dst_x;
365 ctx->vertices.y = dst_y;
366}
367
368static int
369clip_polygon_left(struct clip_context *ctx, const struct polygon8 *src,
370 GLfloat *dst_x, GLfloat *dst_y)
371{
372 enum path_transition trans;
373 int i;
374
375 clip_context_prepare(ctx, src, dst_x, dst_y);
376 for (i = 0; i < src->n; i++) {
377 trans = path_transition_left_edge(ctx, src->x[i], src->y[i]);
378 clip_polygon_leftright(ctx, trans, src->x[i], src->y[i],
379 ctx->clip.x1);
380 }
381 return ctx->vertices.x - dst_x;
382}
383
384static int
385clip_polygon_right(struct clip_context *ctx, const struct polygon8 *src,
386 GLfloat *dst_x, GLfloat *dst_y)
387{
388 enum path_transition trans;
389 int i;
390
391 clip_context_prepare(ctx, src, dst_x, dst_y);
392 for (i = 0; i < src->n; i++) {
393 trans = path_transition_right_edge(ctx, src->x[i], src->y[i]);
394 clip_polygon_leftright(ctx, trans, src->x[i], src->y[i],
395 ctx->clip.x2);
396 }
397 return ctx->vertices.x - dst_x;
398}
399
400static int
401clip_polygon_top(struct clip_context *ctx, const struct polygon8 *src,
402 GLfloat *dst_x, GLfloat *dst_y)
403{
404 enum path_transition trans;
405 int i;
406
407 clip_context_prepare(ctx, src, dst_x, dst_y);
408 for (i = 0; i < src->n; i++) {
409 trans = path_transition_top_edge(ctx, src->x[i], src->y[i]);
410 clip_polygon_topbottom(ctx, trans, src->x[i], src->y[i],
411 ctx->clip.y1);
412 }
413 return ctx->vertices.x - dst_x;
414}
415
416static int
417clip_polygon_bottom(struct clip_context *ctx, const struct polygon8 *src,
418 GLfloat *dst_x, GLfloat *dst_y)
419{
420 enum path_transition trans;
421 int i;
422
423 clip_context_prepare(ctx, src, dst_x, dst_y);
424 for (i = 0; i < src->n; i++) {
425 trans = path_transition_bottom_edge(ctx, src->x[i], src->y[i]);
426 clip_polygon_topbottom(ctx, trans, src->x[i], src->y[i],
427 ctx->clip.y2);
428 }
429 return ctx->vertices.x - dst_x;
430}
431
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400432#define max(a, b) (((a) > (b)) ? (a) : (b))
433#define min(a, b) (((a) > (b)) ? (b) : (a))
434#define clip(x, a, b) min(max(x, a), b)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400435
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300436/*
437 * Compute the boundary vertices of the intersection of the global coordinate
438 * aligned rectangle 'rect', and an arbitrary quadrilateral produced from
439 * 'surf_rect' when transformed from surface coordinates into global coordinates.
440 * The vertices are written to 'ex' and 'ey', and the return value is the
441 * number of vertices. Vertices are produced in clockwise winding order.
442 * Guarantees to produce either zero vertices, or 3-8 vertices with non-zero
443 * polygon area.
444 */
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400445static int
446calculate_edges(struct weston_surface *es, pixman_box32_t *rect,
447 pixman_box32_t *surf_rect, GLfloat *ex, GLfloat *ey)
448{
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300449 struct polygon8 polygon;
450 struct clip_context ctx;
451 int i, n;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400452 GLfloat min_x, max_x, min_y, max_y;
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300453 struct polygon8 surf = {
454 { surf_rect->x1, surf_rect->x2, surf_rect->x2, surf_rect->x1 },
455 { surf_rect->y1, surf_rect->y1, surf_rect->y2, surf_rect->y2 },
456 4
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400457 };
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400458
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300459 ctx.clip.x1 = rect->x1;
460 ctx.clip.y1 = rect->y1;
461 ctx.clip.x2 = rect->x2;
462 ctx.clip.y2 = rect->y2;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400463
464 /* transform surface to screen space: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300465 for (i = 0; i < surf.n; i++)
466 weston_surface_to_global_float(es, surf.x[i], surf.y[i],
467 &surf.x[i], &surf.y[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400468
469 /* find bounding box: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300470 min_x = max_x = surf.x[0];
471 min_y = max_y = surf.y[0];
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400472
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300473 for (i = 1; i < surf.n; i++) {
474 min_x = min(min_x, surf.x[i]);
475 max_x = max(max_x, surf.x[i]);
476 min_y = min(min_y, surf.y[i]);
477 max_y = max(max_y, surf.y[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400478 }
479
480 /* First, simple bounding box check to discard early transformed
481 * surface rects that do not intersect with the clip region:
482 */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300483 if ((min_x >= ctx.clip.x2) || (max_x <= ctx.clip.x1) ||
484 (min_y >= ctx.clip.y2) || (max_y <= ctx.clip.y1))
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400485 return 0;
486
487 /* Simple case, bounding box edges are parallel to surface edges,
488 * there will be only four edges. We just need to clip the surface
489 * vertices to the clip rect bounds:
490 */
491 if (!es->transform.enabled) {
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300492 for (i = 0; i < surf.n; i++) {
493 ex[i] = clip(surf.x[i], ctx.clip.x1, ctx.clip.x2);
494 ey[i] = clip(surf.y[i], ctx.clip.y1, ctx.clip.y2);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400495 }
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300496 return surf.n;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400497 }
498
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300499 /* Transformed case: use a general polygon clipping algorithm to
500 * clip the surface rectangle with each side of 'rect'.
501 * The algorithm is Sutherland-Hodgman, as explained in
502 * http://www.codeguru.com/cpp/misc/misc/graphics/article.php/c8965/Polygon-Clipping.htm
503 * but without looking at any of that code.
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400504 */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300505 polygon.n = clip_polygon_left(&ctx, &surf, polygon.x, polygon.y);
506 surf.n = clip_polygon_right(&ctx, &polygon, surf.x, surf.y);
507 polygon.n = clip_polygon_top(&ctx, &surf, polygon.x, polygon.y);
508 surf.n = clip_polygon_bottom(&ctx, &polygon, surf.x, surf.y);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400509
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300510 /* Get rid of duplicate vertices */
511 ex[0] = surf.x[0];
512 ey[0] = surf.y[0];
513 n = 1;
514 for (i = 1; i < surf.n; i++) {
515 if (float_difference(ex[n - 1], surf.x[i]) == 0.0f &&
516 float_difference(ey[n - 1], surf.y[i]) == 0.0f)
517 continue;
518 ex[n] = surf.x[i];
519 ey[n] = surf.y[i];
520 n++;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400521 }
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300522 if (float_difference(ex[n - 1], surf.x[0]) == 0.0f &&
523 float_difference(ey[n - 1], surf.y[0]) == 0.0f)
524 n--;
525
526 if (n < 3)
527 return 0;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400528
529 return n;
530}
531
532static int
533texture_region(struct weston_surface *es, pixman_region32_t *region,
534 pixman_region32_t *surf_region)
535{
Pekka Paalanen68033ac2012-12-04 15:58:15 +0200536 struct gl_surface_state *gs = get_surface_state(es);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400537 struct weston_compositor *ec = es->compositor;
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400538 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400539 GLfloat *v, inv_width, inv_height;
540 unsigned int *vtxcnt, nvtx = 0;
541 pixman_box32_t *rects, *surf_rects;
542 int i, j, k, nrects, nsurf;
543
544 rects = pixman_region32_rectangles(region, &nrects);
545 surf_rects = pixman_region32_rectangles(surf_region, &nsurf);
546
547 /* worst case we can have 8 vertices per rect (ie. clipped into
548 * an octagon):
549 */
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400550 v = wl_array_add(&gr->vertices, nrects * nsurf * 8 * 4 * sizeof *v);
551 vtxcnt = wl_array_add(&gr->vtxcnt, nrects * nsurf * sizeof *vtxcnt);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400552
Pekka Paalanen68033ac2012-12-04 15:58:15 +0200553 inv_width = 1.0 / gs->pitch;
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200554
555 switch (es->buffer_transform) {
556 case WL_OUTPUT_TRANSFORM_90:
557 case WL_OUTPUT_TRANSFORM_270:
558 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
559 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
560 inv_height = 1.0 / es->geometry.width;
561 break;
562 default:
563 inv_height = 1.0 / es->geometry.height;
564 }
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400565
566 for (i = 0; i < nrects; i++) {
567 pixman_box32_t *rect = &rects[i];
568 for (j = 0; j < nsurf; j++) {
569 pixman_box32_t *surf_rect = &surf_rects[j];
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200570 GLfloat sx, sy, bx, by;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400571 GLfloat ex[8], ey[8]; /* edge points in screen space */
572 int n;
573
574 /* The transformed surface, after clipping to the clip region,
575 * can have as many as eight sides, emitted as a triangle-fan.
576 * The first vertex in the triangle fan can be chosen arbitrarily,
577 * since the area is guaranteed to be convex.
578 *
579 * If a corner of the transformed surface falls outside of the
580 * clip region, instead of emitting one vertex for the corner
581 * of the surface, up to two are emitted for two corresponding
582 * intersection point(s) between the surface and the clip region.
583 *
584 * To do this, we first calculate the (up to eight) points that
585 * form the intersection of the clip rect and the transformed
586 * surface.
587 */
588 n = calculate_edges(es, rect, surf_rect, ex, ey);
589 if (n < 3)
590 continue;
591
592 /* emit edge points: */
593 for (k = 0; k < n; k++) {
594 weston_surface_from_global_float(es, ex[k], ey[k], &sx, &sy);
595 /* position: */
596 *(v++) = ex[k];
597 *(v++) = ey[k];
598 /* texcoord: */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200599 weston_surface_to_buffer_float(es, sx, sy,
600 &bx, &by);
601 *(v++) = bx * inv_width;
602 *(v++) = by * inv_height;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400603 }
604
605 vtxcnt[nvtx++] = n;
606 }
607 }
608
609 return nvtx;
610}
611
612static void
613triangle_fan_debug(struct weston_surface *surface, int first, int count)
614{
615 struct weston_compositor *compositor = surface->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100616 struct gl_renderer *gr = get_renderer(compositor);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400617 int i;
618 GLushort *buffer;
619 GLushort *index;
620 int nelems;
621 static int color_idx = 0;
622 static const GLfloat color[][4] = {
623 { 1.0, 0.0, 0.0, 1.0 },
624 { 0.0, 1.0, 0.0, 1.0 },
625 { 0.0, 0.0, 1.0, 1.0 },
626 { 1.0, 1.0, 1.0, 1.0 },
627 };
628
629 nelems = (count - 1 + count - 2) * 2;
630
631 buffer = malloc(sizeof(GLushort) * nelems);
632 index = buffer;
633
634 for (i = 1; i < count; i++) {
635 *index++ = first;
636 *index++ = first + i;
637 }
638
639 for (i = 2; i < count; i++) {
640 *index++ = first + i - 1;
641 *index++ = first + i;
642 }
643
John Kåre Alsaker40684142012-11-13 19:10:25 +0100644 glUseProgram(gr->solid_shader.program);
645 glUniform4fv(gr->solid_shader.color_uniform, 1,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400646 color[color_idx++ % ARRAY_LENGTH(color)]);
647 glDrawElements(GL_LINES, nelems, GL_UNSIGNED_SHORT, buffer);
John Kåre Alsaker40684142012-11-13 19:10:25 +0100648 glUseProgram(gr->current_shader->program);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400649 free(buffer);
650}
651
652static void
653repaint_region(struct weston_surface *es, pixman_region32_t *region,
654 pixman_region32_t *surf_region)
655{
656 struct weston_compositor *ec = es->compositor;
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400657 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400658 GLfloat *v;
659 unsigned int *vtxcnt;
660 int i, first, nfans;
661
662 /* The final region to be painted is the intersection of
663 * 'region' and 'surf_region'. However, 'region' is in the global
664 * coordinates, and 'surf_region' is in the surface-local
665 * coordinates. texture_region() will iterate over all pairs of
666 * rectangles from both regions, compute the intersection
667 * polygon for each pair, and store it as a triangle fan if
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400668 * it has a non-zero area (at least 3 vertices1, actually).
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400669 */
670 nfans = texture_region(es, region, surf_region);
671
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400672 v = gr->vertices.data;
673 vtxcnt = gr->vtxcnt.data;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400674
675 /* position: */
676 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
677 glEnableVertexAttribArray(0);
678
679 /* texcoord: */
680 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
681 glEnableVertexAttribArray(1);
682
683 for (i = 0, first = 0; i < nfans; i++) {
684 glDrawArrays(GL_TRIANGLE_FAN, first, vtxcnt[i]);
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400685 if (gr->fan_debug)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400686 triangle_fan_debug(es, first, vtxcnt[i]);
687 first += vtxcnt[i];
688 }
689
690 glDisableVertexAttribArray(1);
691 glDisableVertexAttribArray(0);
692
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400693 gr->vertices.size = 0;
694 gr->vtxcnt.size = 0;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400695}
696
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100697static int
698use_output(struct weston_output *output)
699{
700 static int errored;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100701 struct gl_output_state *go = get_output_state(output);
702 struct gl_renderer *gr = get_renderer(output->compositor);
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100703 EGLBoolean ret;
704
705 ret = eglMakeCurrent(gr->egl_display, go->egl_surface,
706 go->egl_surface, gr->egl_context);
707
708 if (ret == EGL_FALSE) {
709 if (errored)
710 return -1;
711 errored = 1;
712 weston_log("Failed to make EGL context current.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +0200713 gl_renderer_print_egl_error_state();
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100714 return -1;
715 }
716
717 return 0;
718}
719
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400720static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100721use_shader(struct gl_renderer *gr,
722 struct gl_shader *shader)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400723{
John Kåre Alsaker40684142012-11-13 19:10:25 +0100724 if (gr->current_shader == shader)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400725 return;
726
727 glUseProgram(shader->program);
John Kåre Alsaker40684142012-11-13 19:10:25 +0100728 gr->current_shader = shader;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400729}
730
731static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100732shader_uniforms(struct gl_shader *shader,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400733 struct weston_surface *surface,
734 struct weston_output *output)
735{
736 int i;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100737 struct gl_surface_state *gs = get_surface_state(surface);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400738
739 glUniformMatrix4fv(shader->proj_uniform,
740 1, GL_FALSE, output->matrix.d);
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100741 glUniform4fv(shader->color_uniform, 1, gs->color);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400742 glUniform1f(shader->alpha_uniform, surface->alpha);
743
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100744 for (i = 0; i < gs->num_textures; i++)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400745 glUniform1i(shader->tex_uniforms[i], i);
746}
747
748static void
749draw_surface(struct weston_surface *es, struct weston_output *output,
750 pixman_region32_t *damage) /* in global coordinates */
751{
752 struct weston_compositor *ec = es->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100753 struct gl_renderer *gr = get_renderer(ec);
754 struct gl_surface_state *gs = get_surface_state(es);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400755 /* repaint bounding region in global coordinates: */
756 pixman_region32_t repaint;
757 /* non-opaque region in surface coordinates: */
758 pixman_region32_t surface_blend;
759 GLint filter;
760 int i;
761
762 pixman_region32_init(&repaint);
763 pixman_region32_intersect(&repaint,
764 &es->transform.boundingbox, damage);
765 pixman_region32_subtract(&repaint, &repaint, &es->clip);
766
767 if (!pixman_region32_not_empty(&repaint))
768 goto out;
769
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400770 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
771
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400772 if (gr->fan_debug) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100773 use_shader(gr, &gr->solid_shader);
774 shader_uniforms(&gr->solid_shader, es, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400775 }
776
John Kåre Alsaker40684142012-11-13 19:10:25 +0100777 use_shader(gr, gs->shader);
778 shader_uniforms(gs->shader, es, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400779
780 if (es->transform.enabled || output->zoom.active)
781 filter = GL_LINEAR;
782 else
783 filter = GL_NEAREST;
784
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100785 for (i = 0; i < gs->num_textures; i++) {
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400786 glActiveTexture(GL_TEXTURE0 + i);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100787 glBindTexture(gs->target, gs->textures[i]);
788 glTexParameteri(gs->target, GL_TEXTURE_MIN_FILTER, filter);
789 glTexParameteri(gs->target, GL_TEXTURE_MAG_FILTER, filter);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400790 }
791
792 /* blended region is whole surface minus opaque region: */
793 pixman_region32_init_rect(&surface_blend, 0, 0,
794 es->geometry.width, es->geometry.height);
795 pixman_region32_subtract(&surface_blend, &surface_blend, &es->opaque);
796
797 if (pixman_region32_not_empty(&es->opaque)) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100798 if (gs->shader == &gr->texture_shader_rgba) {
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400799 /* Special case for RGBA textures with possibly
800 * bad data in alpha channel: use the shader
801 * that forces texture alpha = 1.0.
802 * Xwayland surfaces need this.
803 */
John Kåre Alsaker40684142012-11-13 19:10:25 +0100804 use_shader(gr, &gr->texture_shader_rgbx);
805 shader_uniforms(&gr->texture_shader_rgbx, es, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400806 }
807
808 if (es->alpha < 1.0)
809 glEnable(GL_BLEND);
810 else
811 glDisable(GL_BLEND);
812
813 repaint_region(es, &repaint, &es->opaque);
814 }
815
816 if (pixman_region32_not_empty(&surface_blend)) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100817 use_shader(gr, gs->shader);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400818 glEnable(GL_BLEND);
819 repaint_region(es, &repaint, &surface_blend);
820 }
821
822 pixman_region32_fini(&surface_blend);
823
824out:
825 pixman_region32_fini(&repaint);
826}
827
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400828static void
829repaint_surfaces(struct weston_output *output, pixman_region32_t *damage)
830{
831 struct weston_compositor *compositor = output->compositor;
832 struct weston_surface *surface;
833
834 wl_list_for_each_reverse(surface, &compositor->surface_list, link)
835 if (surface->plane == &compositor->primary_plane)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400836 draw_surface(surface, output, damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400837}
838
John Kåre Alsaker44154502012-11-13 19:10:20 +0100839
840static int
841texture_border(struct weston_output *output)
842{
843 struct weston_compositor *ec = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100844 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker44154502012-11-13 19:10:20 +0100845 GLfloat *d;
846 unsigned int *p;
847 int i, j, k, n;
848 GLfloat x[4], y[4], u[4], v[4];
849
850 x[0] = -gr->border.left;
851 x[1] = 0;
852 x[2] = output->current->width;
853 x[3] = output->current->width + gr->border.right;
854
855 y[0] = -gr->border.top;
856 y[1] = 0;
857 y[2] = output->current->height;
858 y[3] = output->current->height + gr->border.bottom;
859
860 u[0] = 0.0;
861 u[1] = (GLfloat) gr->border.left / gr->border.width;
862 u[2] = (GLfloat) (gr->border.width - gr->border.right) / gr->border.width;
863 u[3] = 1.0;
864
865 v[0] = 0.0;
866 v[1] = (GLfloat) gr->border.top / gr->border.height;
867 v[2] = (GLfloat) (gr->border.height - gr->border.bottom) / gr->border.height;
868 v[3] = 1.0;
869
870 n = 8;
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400871 d = wl_array_add(&gr->vertices, n * 16 * sizeof *d);
872 p = wl_array_add(&gr->indices, n * 6 * sizeof *p);
John Kåre Alsaker44154502012-11-13 19:10:20 +0100873
874 k = 0;
875 for (i = 0; i < 3; i++)
876 for (j = 0; j < 3; j++) {
877
878 if (i == 1 && j == 1)
879 continue;
880
881 d[ 0] = x[i];
882 d[ 1] = y[j];
883 d[ 2] = u[i];
884 d[ 3] = v[j];
885
886 d[ 4] = x[i];
887 d[ 5] = y[j + 1];
888 d[ 6] = u[i];
889 d[ 7] = v[j + 1];
890
891 d[ 8] = x[i + 1];
892 d[ 9] = y[j];
893 d[10] = u[i + 1];
894 d[11] = v[j];
895
896 d[12] = x[i + 1];
897 d[13] = y[j + 1];
898 d[14] = u[i + 1];
899 d[15] = v[j + 1];
900
901 p[0] = k + 0;
902 p[1] = k + 1;
903 p[2] = k + 2;
904 p[3] = k + 2;
905 p[4] = k + 1;
906 p[5] = k + 3;
907
908 d += 16;
909 p += 6;
910 k += 4;
911 }
912
913 return k / 4;
914}
915
916static void
917draw_border(struct weston_output *output)
918{
919 struct weston_compositor *ec = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100920 struct gl_renderer *gr = get_renderer(ec);
921 struct gl_shader *shader = &gr->texture_shader_rgba;
John Kåre Alsaker44154502012-11-13 19:10:20 +0100922 GLfloat *v;
923 int n;
924
925 glDisable(GL_BLEND);
John Kåre Alsaker40684142012-11-13 19:10:25 +0100926 use_shader(gr, shader);
John Kåre Alsaker44154502012-11-13 19:10:20 +0100927
928 glUniformMatrix4fv(shader->proj_uniform,
929 1, GL_FALSE, output->matrix.d);
930
931 glUniform1i(shader->tex_uniforms[0], 0);
932 glUniform1f(shader->alpha_uniform, 1);
933
934 n = texture_border(output);
935
936 glActiveTexture(GL_TEXTURE0);
937 glBindTexture(GL_TEXTURE_2D, gr->border.texture);
938
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400939 v = gr->vertices.data;
John Kåre Alsaker44154502012-11-13 19:10:20 +0100940 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
941 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
942 glEnableVertexAttribArray(0);
943 glEnableVertexAttribArray(1);
944
945 glDrawElements(GL_TRIANGLES, n * 6,
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400946 GL_UNSIGNED_INT, gr->indices.data);
John Kåre Alsaker44154502012-11-13 19:10:20 +0100947
948 glDisableVertexAttribArray(1);
949 glDisableVertexAttribArray(0);
950
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400951 gr->vertices.size = 0;
952 gr->indices.size = 0;
John Kåre Alsaker44154502012-11-13 19:10:20 +0100953}
954
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400955static void
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +0200956output_get_buffer_damage(struct weston_output *output,
957 pixman_region32_t *buffer_damage)
958{
959 struct gl_output_state *go = get_output_state(output);
960 struct gl_renderer *gr = get_renderer(output->compositor);
961 EGLint buffer_age = 0;
962 EGLBoolean ret;
963 int i;
964
965 if (gr->has_egl_buffer_age) {
966 ret = eglQuerySurface(gr->egl_display, go->egl_surface,
967 EGL_BUFFER_AGE_EXT, &buffer_age);
968 if (ret == EGL_FALSE) {
969 weston_log("buffer age query failed.\n");
970 gl_renderer_print_egl_error_state();
971 }
972 }
973
974 if (buffer_age == 0 || buffer_age - 1 > BUFFER_DAMAGE_COUNT)
975 pixman_region32_copy(buffer_damage, &output->region);
976 else
977 for (i = 0; i < buffer_age - 1; i++)
978 pixman_region32_union(buffer_damage, buffer_damage,
979 &go->buffer_damage[i]);
980}
981
982static void
983output_rotate_damage(struct weston_output *output,
984 pixman_region32_t *output_damage)
985{
986 struct gl_output_state *go = get_output_state(output);
987 struct gl_renderer *gr = get_renderer(output->compositor);
988 int i;
989
990 if (!gr->has_egl_buffer_age)
991 return;
992
993 for (i = BUFFER_DAMAGE_COUNT - 1; i >= 1; i--)
994 pixman_region32_copy(&go->buffer_damage[i],
995 &go->buffer_damage[i - 1]);
996
997 pixman_region32_copy(&go->buffer_damage[0], output_damage);
998}
999
1000static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001001gl_renderer_repaint_output(struct weston_output *output,
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001002 pixman_region32_t *output_damage)
1003{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001004 struct gl_output_state *go = get_output_state(output);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001005 struct weston_compositor *compositor = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001006 struct gl_renderer *gr = get_renderer(compositor);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001007 EGLBoolean ret;
1008 static int errored;
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001009 int32_t width, height;
1010 pixman_region32_t buffer_damage, total_damage;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001011
1012 width = output->current->width +
1013 output->border.left + output->border.right;
1014 height = output->current->height +
1015 output->border.top + output->border.bottom;
1016
1017 glViewport(0, 0, width, height);
1018
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001019 if (use_output(output) < 0)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001020 return;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001021
1022 /* if debugging, redraw everything outside the damage to clean up
1023 * debug lines from the previous draw on this buffer:
1024 */
Kristian Høgsberg8799d412013-05-07 10:50:09 -04001025 if (gr->fan_debug) {
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001026 pixman_region32_t undamaged;
1027 pixman_region32_init(&undamaged);
1028 pixman_region32_subtract(&undamaged, &output->region,
1029 output_damage);
Kristian Høgsberg8799d412013-05-07 10:50:09 -04001030 gr->fan_debug = 0;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001031 repaint_surfaces(output, &undamaged);
Kristian Høgsberg8799d412013-05-07 10:50:09 -04001032 gr->fan_debug = 1;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001033 pixman_region32_fini(&undamaged);
1034 }
1035
Ander Conselvan de Oliveirab605c062013-03-05 17:30:28 +02001036 pixman_region32_init(&total_damage);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001037 pixman_region32_init(&buffer_damage);
1038
1039 output_get_buffer_damage(output, &buffer_damage);
1040 output_rotate_damage(output, output_damage);
1041
1042 pixman_region32_union(&total_damage, &buffer_damage, output_damage);
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03001043
Ander Conselvan de Oliveirab605c062013-03-05 17:30:28 +02001044 repaint_surfaces(output, &total_damage);
1045
1046 pixman_region32_fini(&total_damage);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001047 pixman_region32_fini(&buffer_damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001048
John Kåre Alsaker44154502012-11-13 19:10:20 +01001049 if (gr->border.texture)
1050 draw_border(output);
1051
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001052 pixman_region32_copy(&output->previous_damage, output_damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001053 wl_signal_emit(&output->frame_signal, output);
1054
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001055 ret = eglSwapBuffers(gr->egl_display, go->egl_surface);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001056 if (ret == EGL_FALSE && !errored) {
1057 errored = 1;
1058 weston_log("Failed in eglSwapBuffers.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02001059 gl_renderer_print_egl_error_state();
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001060 }
1061
1062}
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001063
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001064static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001065gl_renderer_read_pixels(struct weston_output *output,
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001066 pixman_format_code_t format, void *pixels,
1067 uint32_t x, uint32_t y,
1068 uint32_t width, uint32_t height)
1069{
1070 GLenum gl_format;
1071
1072 switch (format) {
1073 case PIXMAN_a8r8g8b8:
1074 gl_format = GL_BGRA_EXT;
1075 break;
1076 case PIXMAN_a8b8g8r8:
1077 gl_format = GL_RGBA;
1078 break;
1079 default:
1080 return -1;
1081 }
1082
1083 if (use_output(output) < 0)
1084 return -1;
1085
1086 glPixelStorei(GL_PACK_ALIGNMENT, 1);
1087 glReadPixels(x, y, width, height, gl_format,
1088 GL_UNSIGNED_BYTE, pixels);
1089
1090 return 0;
1091}
1092
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001093static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001094gl_renderer_flush_damage(struct weston_surface *surface)
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001095{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001096 struct gl_renderer *gr = get_renderer(surface->compositor);
1097 struct gl_surface_state *gs = get_surface_state(surface);
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001098 struct wl_buffer *buffer = gs->buffer_ref.buffer;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001099
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001100#ifdef GL_UNPACK_ROW_LENGTH
1101 pixman_box32_t *rectangles;
1102 void *data;
1103 int i, n;
1104#endif
1105
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001106 pixman_region32_union(&gs->texture_damage,
1107 &gs->texture_damage, &surface->damage);
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001108
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001109 if (!buffer)
1110 return;
1111
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001112 /* Avoid upload, if the texture won't be used this time.
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001113 * We still accumulate the damage in texture_damage, and
1114 * hold the reference to the buffer, in case the surface
1115 * migrates back to the primary plane.
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001116 */
1117 if (surface->plane != &surface->compositor->primary_plane)
1118 return;
1119
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001120 if (!pixman_region32_not_empty(&gs->texture_damage))
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001121 goto done;
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001122
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001123 glBindTexture(GL_TEXTURE_2D, gs->textures[0]);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001124
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001125 if (!gr->has_unpack_subimage) {
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001126 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001127 gs->pitch, buffer->height, 0,
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001128 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
Pekka Paalanende685b82012-12-04 15:58:12 +02001129 wl_shm_buffer_get_data(buffer));
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001130
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001131 goto done;
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001132 }
1133
1134#ifdef GL_UNPACK_ROW_LENGTH
1135 /* Mesa does not define GL_EXT_unpack_subimage */
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001136 glPixelStorei(GL_UNPACK_ROW_LENGTH, gs->pitch);
Pekka Paalanende685b82012-12-04 15:58:12 +02001137 data = wl_shm_buffer_get_data(buffer);
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001138 rectangles = pixman_region32_rectangles(&gs->texture_damage, &n);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001139 for (i = 0; i < n; i++) {
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001140 pixman_box32_t r;
1141
1142 r = weston_surface_to_buffer_rect(surface, rectangles[i]);
1143
1144 glPixelStorei(GL_UNPACK_SKIP_PIXELS, r.x1);
1145 glPixelStorei(GL_UNPACK_SKIP_ROWS, r.y1);
1146 glTexSubImage2D(GL_TEXTURE_2D, 0, r.x1, r.y1,
1147 r.x2 - r.x1, r.y2 - r.y1,
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001148 GL_BGRA_EXT, GL_UNSIGNED_BYTE, data);
1149 }
1150#endif
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001151
1152done:
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001153 pixman_region32_fini(&gs->texture_damage);
1154 pixman_region32_init(&gs->texture_damage);
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001155
1156 weston_buffer_reference(&gs->buffer_ref, NULL);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001157}
1158
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001159static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001160ensure_textures(struct gl_surface_state *gs, int num_textures)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001161{
1162 int i;
1163
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001164 if (num_textures <= gs->num_textures)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001165 return;
1166
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001167 for (i = gs->num_textures; i < num_textures; i++) {
1168 glGenTextures(1, &gs->textures[i]);
1169 glBindTexture(gs->target, gs->textures[i]);
1170 glTexParameteri(gs->target,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001171 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001172 glTexParameteri(gs->target,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001173 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1174 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001175 gs->num_textures = num_textures;
1176 glBindTexture(gs->target, 0);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001177}
1178
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001179static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001180gl_renderer_attach(struct weston_surface *es, struct wl_buffer *buffer)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001181{
1182 struct weston_compositor *ec = es->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001183 struct gl_renderer *gr = get_renderer(ec);
1184 struct gl_surface_state *gs = get_surface_state(es);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001185 EGLint attribs[3], format;
1186 int i, num_planes;
1187
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001188 weston_buffer_reference(&gs->buffer_ref, buffer);
1189
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001190 if (!buffer) {
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001191 for (i = 0; i < gs->num_images; i++) {
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001192 gr->destroy_image(gr->egl_display, gs->images[i]);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001193 gs->images[i] = NULL;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001194 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001195 gs->num_images = 0;
1196 glDeleteTextures(gs->num_textures, gs->textures);
1197 gs->num_textures = 0;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001198 return;
1199 }
1200
1201 if (wl_buffer_is_shm(buffer)) {
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001202 gs->pitch = wl_shm_buffer_get_stride(buffer) / 4;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001203 gs->target = GL_TEXTURE_2D;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001204
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001205 ensure_textures(gs, 1);
1206 glBindTexture(GL_TEXTURE_2D, gs->textures[0]);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001207 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001208 gs->pitch, buffer->height, 0,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001209 GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
1210 if (wl_shm_buffer_get_format(buffer) == WL_SHM_FORMAT_XRGB8888)
John Kåre Alsaker40684142012-11-13 19:10:25 +01001211 gs->shader = &gr->texture_shader_rgbx;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001212 else
John Kåre Alsaker40684142012-11-13 19:10:25 +01001213 gs->shader = &gr->texture_shader_rgba;
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001214 } else if (gr->query_buffer(gr->egl_display, buffer,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001215 EGL_TEXTURE_FORMAT, &format)) {
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001216 for (i = 0; i < gs->num_images; i++)
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001217 gr->destroy_image(gr->egl_display, gs->images[i]);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001218 gs->num_images = 0;
1219 gs->target = GL_TEXTURE_2D;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001220 switch (format) {
1221 case EGL_TEXTURE_RGB:
1222 case EGL_TEXTURE_RGBA:
1223 default:
1224 num_planes = 1;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001225 gs->shader = &gr->texture_shader_rgba;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001226 break;
1227 case EGL_TEXTURE_EXTERNAL_WL:
1228 num_planes = 1;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001229 gs->target = GL_TEXTURE_EXTERNAL_OES;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001230 gs->shader = &gr->texture_shader_egl_external;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001231 break;
1232 case EGL_TEXTURE_Y_UV_WL:
1233 num_planes = 2;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001234 gs->shader = &gr->texture_shader_y_uv;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001235 break;
1236 case EGL_TEXTURE_Y_U_V_WL:
1237 num_planes = 3;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001238 gs->shader = &gr->texture_shader_y_u_v;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001239 break;
1240 case EGL_TEXTURE_Y_XUXV_WL:
1241 num_planes = 2;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001242 gs->shader = &gr->texture_shader_y_xuxv;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001243 break;
1244 }
1245
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001246 ensure_textures(gs, num_planes);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001247 for (i = 0; i < num_planes; i++) {
1248 attribs[0] = EGL_WAYLAND_PLANE_WL;
1249 attribs[1] = i;
1250 attribs[2] = EGL_NONE;
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001251 gs->images[i] = gr->create_image(gr->egl_display,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001252 NULL,
1253 EGL_WAYLAND_BUFFER_WL,
1254 buffer, attribs);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001255 if (!gs->images[i]) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001256 weston_log("failed to create img for plane %d\n", i);
1257 continue;
1258 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001259 gs->num_images++;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001260
1261 glActiveTexture(GL_TEXTURE0 + i);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001262 glBindTexture(gs->target, gs->textures[i]);
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001263 gr->image_target_texture_2d(gs->target,
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001264 gs->images[i]);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001265 }
1266
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001267 gs->pitch = buffer->width;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001268 } else {
1269 weston_log("unhandled buffer type!\n");
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001270 weston_buffer_reference(&gs->buffer_ref, NULL);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001271 }
1272}
1273
Kristian Høgsberg42263852012-09-06 21:59:29 -04001274static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001275gl_renderer_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001276 float red, float green, float blue, float alpha)
1277{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001278 struct gl_surface_state *gs = get_surface_state(surface);
1279 struct gl_renderer *gr = get_renderer(surface->compositor);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001280
1281 gs->color[0] = red;
1282 gs->color[1] = green;
1283 gs->color[2] = blue;
1284 gs->color[3] = alpha;
1285
John Kåre Alsaker40684142012-11-13 19:10:25 +01001286 gs->shader = &gr->solid_shader;
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001287}
1288
1289static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001290gl_renderer_create_surface(struct weston_surface *surface)
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001291{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001292 struct gl_surface_state *gs;
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001293
1294 gs = calloc(1, sizeof *gs);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001295 if (!gs)
1296 return -1;
1297
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001298 /* A buffer is never attached to solid color surfaces, yet
1299 * they still go through texcoord computations. Do not divide
1300 * by zero there.
1301 */
1302 gs->pitch = 1;
1303
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001304 pixman_region32_init(&gs->texture_damage);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001305 surface->renderer_state = gs;
1306
1307 return 0;
1308}
1309
1310static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001311gl_renderer_destroy_surface(struct weston_surface *surface)
Kristian Høgsberg42263852012-09-06 21:59:29 -04001312{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001313 struct gl_surface_state *gs = get_surface_state(surface);
1314 struct gl_renderer *gr = get_renderer(surface->compositor);
Kristian Høgsberg42263852012-09-06 21:59:29 -04001315 int i;
1316
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001317 glDeleteTextures(gs->num_textures, gs->textures);
Kristian Høgsberg42263852012-09-06 21:59:29 -04001318
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001319 for (i = 0; i < gs->num_images; i++)
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001320 gr->destroy_image(gr->egl_display, gs->images[i]);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001321
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001322 weston_buffer_reference(&gs->buffer_ref, NULL);
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001323 pixman_region32_fini(&gs->texture_damage);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001324 free(gs);
Kristian Høgsberg42263852012-09-06 21:59:29 -04001325}
1326
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001327static const char vertex_shader[] =
1328 "uniform mat4 proj;\n"
1329 "attribute vec2 position;\n"
1330 "attribute vec2 texcoord;\n"
1331 "varying vec2 v_texcoord;\n"
1332 "void main()\n"
1333 "{\n"
1334 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
1335 " v_texcoord = texcoord;\n"
1336 "}\n";
1337
1338/* Declare common fragment shader uniforms */
1339#define FRAGMENT_CONVERT_YUV \
1340 " y *= alpha;\n" \
1341 " u *= alpha;\n" \
1342 " v *= alpha;\n" \
1343 " gl_FragColor.r = y + 1.59602678 * v;\n" \
1344 " gl_FragColor.g = y - 0.39176229 * u - 0.81296764 * v;\n" \
1345 " gl_FragColor.b = y + 2.01723214 * u;\n" \
1346 " gl_FragColor.a = alpha;\n"
1347
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001348static const char fragment_debug[] =
1349 " gl_FragColor = vec4(0.0, 0.3, 0.0, 0.2) + gl_FragColor * 0.8;\n";
1350
1351static const char fragment_brace[] =
1352 "}\n";
1353
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001354static const char texture_fragment_shader_rgba[] =
1355 "precision mediump float;\n"
1356 "varying vec2 v_texcoord;\n"
1357 "uniform sampler2D tex;\n"
1358 "uniform float alpha;\n"
1359 "void main()\n"
1360 "{\n"
1361 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001362 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001363
1364static const char texture_fragment_shader_rgbx[] =
1365 "precision mediump float;\n"
1366 "varying vec2 v_texcoord;\n"
1367 "uniform sampler2D tex;\n"
1368 "uniform float alpha;\n"
1369 "void main()\n"
1370 "{\n"
1371 " gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb\n;"
1372 " gl_FragColor.a = alpha;\n"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001373 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001374
1375static const char texture_fragment_shader_egl_external[] =
1376 "#extension GL_OES_EGL_image_external : require\n"
1377 "precision mediump float;\n"
1378 "varying vec2 v_texcoord;\n"
1379 "uniform samplerExternalOES tex;\n"
1380 "uniform float alpha;\n"
1381 "void main()\n"
1382 "{\n"
1383 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001384 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001385
1386static const char texture_fragment_shader_y_uv[] =
1387 "precision mediump float;\n"
1388 "uniform sampler2D tex;\n"
1389 "uniform sampler2D tex1;\n"
1390 "varying vec2 v_texcoord;\n"
1391 "uniform float alpha;\n"
1392 "void main() {\n"
1393 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1394 " float u = texture2D(tex1, v_texcoord).r - 0.5;\n"
1395 " float v = texture2D(tex1, v_texcoord).g - 0.5;\n"
1396 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001397 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001398
1399static const char texture_fragment_shader_y_u_v[] =
1400 "precision mediump float;\n"
1401 "uniform sampler2D tex;\n"
1402 "uniform sampler2D tex1;\n"
1403 "uniform sampler2D tex2;\n"
1404 "varying vec2 v_texcoord;\n"
1405 "uniform float alpha;\n"
1406 "void main() {\n"
1407 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1408 " float u = texture2D(tex1, v_texcoord).x - 0.5;\n"
1409 " float v = texture2D(tex2, v_texcoord).x - 0.5;\n"
1410 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001411 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001412
1413static const char texture_fragment_shader_y_xuxv[] =
1414 "precision mediump float;\n"
1415 "uniform sampler2D tex;\n"
1416 "uniform sampler2D tex1;\n"
1417 "varying vec2 v_texcoord;\n"
1418 "uniform float alpha;\n"
1419 "void main() {\n"
1420 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1421 " float u = texture2D(tex1, v_texcoord).g - 0.5;\n"
1422 " float v = texture2D(tex1, v_texcoord).a - 0.5;\n"
1423 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001424 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001425
1426static const char solid_fragment_shader[] =
1427 "precision mediump float;\n"
1428 "uniform vec4 color;\n"
1429 "uniform float alpha;\n"
1430 "void main()\n"
1431 "{\n"
1432 " gl_FragColor = alpha * color\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001433 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001434
1435static int
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001436compile_shader(GLenum type, int count, const char **sources)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001437{
1438 GLuint s;
1439 char msg[512];
1440 GLint status;
1441
1442 s = glCreateShader(type);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001443 glShaderSource(s, count, sources, NULL);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001444 glCompileShader(s);
1445 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
1446 if (!status) {
1447 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1448 weston_log("shader info: %s\n", msg);
1449 return GL_NONE;
1450 }
1451
1452 return s;
1453}
1454
1455static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001456shader_init(struct gl_shader *shader, struct weston_compositor *ec,
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001457 const char *vertex_source, const char *fragment_source)
1458{
1459 char msg[512];
1460 GLint status;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001461 int count;
1462 const char *sources[3];
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001463 struct gl_renderer *renderer = get_renderer(ec);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001464
1465 shader->vertex_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001466 compile_shader(GL_VERTEX_SHADER, 1, &vertex_source);
1467
1468 if (renderer->fragment_shader_debug) {
1469 sources[0] = fragment_source;
1470 sources[1] = fragment_debug;
1471 sources[2] = fragment_brace;
1472 count = 3;
1473 } else {
1474 sources[0] = fragment_source;
1475 sources[1] = fragment_brace;
1476 count = 2;
1477 }
1478
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001479 shader->fragment_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001480 compile_shader(GL_FRAGMENT_SHADER, count, sources);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001481
1482 shader->program = glCreateProgram();
1483 glAttachShader(shader->program, shader->vertex_shader);
1484 glAttachShader(shader->program, shader->fragment_shader);
1485 glBindAttribLocation(shader->program, 0, "position");
1486 glBindAttribLocation(shader->program, 1, "texcoord");
1487
1488 glLinkProgram(shader->program);
1489 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1490 if (!status) {
1491 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1492 weston_log("link info: %s\n", msg);
1493 return -1;
1494 }
1495
1496 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1497 shader->tex_uniforms[0] = glGetUniformLocation(shader->program, "tex");
1498 shader->tex_uniforms[1] = glGetUniformLocation(shader->program, "tex1");
1499 shader->tex_uniforms[2] = glGetUniformLocation(shader->program, "tex2");
1500 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
1501 shader->color_uniform = glGetUniformLocation(shader->program, "color");
1502
1503 return 0;
1504}
1505
1506static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001507shader_release(struct gl_shader *shader)
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001508{
1509 glDeleteShader(shader->vertex_shader);
1510 glDeleteShader(shader->fragment_shader);
1511 glDeleteProgram(shader->program);
1512
1513 shader->vertex_shader = 0;
1514 shader->fragment_shader = 0;
1515 shader->program = 0;
1516}
1517
1518static void
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001519log_extensions(const char *name, const char *extensions)
1520{
1521 const char *p, *end;
1522 int l;
1523 int len;
1524
1525 l = weston_log("%s:", name);
1526 p = extensions;
1527 while (*p) {
1528 end = strchrnul(p, ' ');
1529 len = end - p;
1530 if (l + len > 78)
1531 l = weston_log_continue("\n" STAMP_SPACE "%.*s",
1532 len, p);
1533 else
1534 l += weston_log_continue(" %.*s", len, p);
1535 for (p = end; isspace(*p); p++)
1536 ;
1537 }
1538 weston_log_continue("\n");
1539}
1540
1541static void
1542log_egl_gl_info(EGLDisplay egldpy)
1543{
1544 const char *str;
1545
1546 str = eglQueryString(egldpy, EGL_VERSION);
1547 weston_log("EGL version: %s\n", str ? str : "(null)");
1548
1549 str = eglQueryString(egldpy, EGL_VENDOR);
1550 weston_log("EGL vendor: %s\n", str ? str : "(null)");
1551
1552 str = eglQueryString(egldpy, EGL_CLIENT_APIS);
1553 weston_log("EGL client APIs: %s\n", str ? str : "(null)");
1554
1555 str = eglQueryString(egldpy, EGL_EXTENSIONS);
1556 log_extensions("EGL extensions", str ? str : "(null)");
1557
1558 str = (char *)glGetString(GL_VERSION);
1559 weston_log("GL version: %s\n", str ? str : "(null)");
1560
1561 str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
1562 weston_log("GLSL version: %s\n", str ? str : "(null)");
1563
1564 str = (char *)glGetString(GL_VENDOR);
1565 weston_log("GL vendor: %s\n", str ? str : "(null)");
1566
1567 str = (char *)glGetString(GL_RENDERER);
1568 weston_log("GL renderer: %s\n", str ? str : "(null)");
1569
1570 str = (char *)glGetString(GL_EXTENSIONS);
1571 log_extensions("GL extensions", str ? str : "(null)");
1572}
1573
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001574static void
1575log_egl_config_info(EGLDisplay egldpy, EGLConfig eglconfig)
1576{
1577 EGLint r, g, b, a;
1578
1579 weston_log("Chosen EGL config details:\n");
1580
1581 weston_log_continue(STAMP_SPACE "RGBA bits");
1582 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_RED_SIZE, &r) &&
1583 eglGetConfigAttrib(egldpy, eglconfig, EGL_GREEN_SIZE, &g) &&
1584 eglGetConfigAttrib(egldpy, eglconfig, EGL_BLUE_SIZE, &b) &&
1585 eglGetConfigAttrib(egldpy, eglconfig, EGL_ALPHA_SIZE, &a))
1586 weston_log_continue(": %d %d %d %d\n", r, g, b, a);
1587 else
1588 weston_log_continue(" unknown\n");
1589
1590 weston_log_continue(STAMP_SPACE "swap interval range");
1591 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_MIN_SWAP_INTERVAL, &a) &&
1592 eglGetConfigAttrib(egldpy, eglconfig, EGL_MAX_SWAP_INTERVAL, &b))
1593 weston_log_continue(": %d - %d\n", a, b);
1594 else
1595 weston_log_continue(" unknown\n");
1596}
1597
John Kåre Alsaker44154502012-11-13 19:10:20 +01001598static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001599output_apply_border(struct weston_output *output, struct gl_renderer *gr)
John Kåre Alsaker44154502012-11-13 19:10:20 +01001600{
1601 output->border.top = gr->border.top;
1602 output->border.bottom = gr->border.bottom;
1603 output->border.left = gr->border.left;
1604 output->border.right = gr->border.right;
1605}
1606
1607WL_EXPORT void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001608gl_renderer_set_border(struct weston_compositor *ec, int32_t width, int32_t height, void *data,
John Kåre Alsaker44154502012-11-13 19:10:20 +01001609 int32_t *edges)
1610{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001611 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker44154502012-11-13 19:10:20 +01001612 struct weston_output *output;
1613
1614 gr->border.left = edges[0];
1615 gr->border.right = edges[1];
1616 gr->border.top = edges[2];
1617 gr->border.bottom = edges[3];
1618
1619 gr->border.width = width;
1620 gr->border.height = height;
1621
1622 glGenTextures(1, &gr->border.texture);
1623 glBindTexture(GL_TEXTURE_2D, gr->border.texture);
1624 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1625 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1626 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1627 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1628
1629 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1630 width,
1631 height,
1632 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1633 data);
1634
1635 wl_list_for_each(output, &ec->output_list, link)
1636 output_apply_border(output, gr);
1637}
1638
John Kåre Alsaker94659272012-11-13 19:10:18 +01001639static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001640gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001641
1642WL_EXPORT int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001643gl_renderer_output_create(struct weston_output *output,
John Kåre Alsaker94659272012-11-13 19:10:18 +01001644 EGLNativeWindowType window)
1645{
1646 struct weston_compositor *ec = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001647 struct gl_renderer *gr = get_renderer(ec);
1648 struct gl_output_state *go = calloc(1, sizeof *go);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001649 int i;
John Kåre Alsaker94659272012-11-13 19:10:18 +01001650
1651 if (!go)
1652 return -1;
1653
1654 go->egl_surface =
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001655 eglCreateWindowSurface(gr->egl_display,
1656 gr->egl_config,
John Kåre Alsaker94659272012-11-13 19:10:18 +01001657 window, NULL);
1658
1659 if (go->egl_surface == EGL_NO_SURFACE) {
1660 weston_log("failed to create egl surface\n");
1661 free(go);
1662 return -1;
1663 }
1664
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001665 if (gr->egl_context == NULL)
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001666 if (gl_renderer_setup(ec, go->egl_surface) < 0) {
John Kåre Alsaker94659272012-11-13 19:10:18 +01001667 free(go);
1668 return -1;
1669 }
1670
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001671 for (i = 0; i < BUFFER_DAMAGE_COUNT; i++)
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001672 pixman_region32_init(&go->buffer_damage[i]);
1673
John Kåre Alsaker94659272012-11-13 19:10:18 +01001674 output->renderer_state = go;
1675
John Kåre Alsaker44154502012-11-13 19:10:20 +01001676 output_apply_border(output, gr);
1677
John Kåre Alsaker94659272012-11-13 19:10:18 +01001678 return 0;
1679}
1680
1681WL_EXPORT void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001682gl_renderer_output_destroy(struct weston_output *output)
John Kåre Alsaker94659272012-11-13 19:10:18 +01001683{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001684 struct gl_renderer *gr = get_renderer(output->compositor);
1685 struct gl_output_state *go = get_output_state(output);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001686 int i;
1687
1688 for (i = 0; i < 2; i++)
1689 pixman_region32_fini(&go->buffer_damage[i]);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001690
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001691 eglDestroySurface(gr->egl_display, go->egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001692
1693 free(go);
1694}
1695
1696WL_EXPORT EGLSurface
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001697gl_renderer_output_surface(struct weston_output *output)
John Kåre Alsaker94659272012-11-13 19:10:18 +01001698{
1699 return get_output_state(output)->egl_surface;
1700}
1701
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03001702static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001703gl_renderer_destroy(struct weston_compositor *ec)
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001704{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001705 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001706
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001707 if (gr->has_bind_display)
1708 gr->unbind_display(gr->egl_display, ec->wl_display);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001709
1710 /* Work around crash in egl_dri2.c's dri2_make_current() - when does this apply? */
1711 eglMakeCurrent(gr->egl_display,
1712 EGL_NO_SURFACE, EGL_NO_SURFACE,
1713 EGL_NO_CONTEXT);
1714
1715 eglTerminate(gr->egl_display);
1716 eglReleaseThread();
Scott Moreau976a0502013-03-07 10:15:17 -07001717
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -04001718 wl_array_release(&gr->vertices);
1719 wl_array_release(&gr->indices);
1720 wl_array_release(&gr->vtxcnt);
1721
Scott Moreau976a0502013-03-07 10:15:17 -07001722 free(gr);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001723}
1724
1725static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001726egl_choose_config(struct gl_renderer *gr, const EGLint *attribs,
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001727 const EGLint *visual_id)
1728{
1729 EGLint count = 0;
1730 EGLint matched = 0;
1731 EGLConfig *configs;
1732 int i;
1733
1734 if (!eglGetConfigs(gr->egl_display, NULL, 0, &count) || count < 1)
1735 return -1;
1736
1737 configs = calloc(count, sizeof *configs);
1738 if (!configs)
1739 return -1;
1740
1741 if (!eglChooseConfig(gr->egl_display, attribs, configs,
1742 count, &matched))
1743 goto out;
1744
1745 for (i = 0; i < matched; ++i) {
1746 EGLint id;
1747
1748 if (visual_id) {
1749 if (!eglGetConfigAttrib(gr->egl_display,
1750 configs[i], EGL_NATIVE_VISUAL_ID,
1751 &id))
1752 continue;
1753
1754 if (id != *visual_id)
1755 continue;
1756 }
1757
1758 gr->egl_config = configs[i];
1759
1760 free(configs);
1761 return 0;
1762 }
1763
1764out:
1765 free(configs);
1766 return -1;
1767}
1768
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001769WL_EXPORT const EGLint gl_renderer_opaque_attribs[] = {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001770 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1771 EGL_RED_SIZE, 1,
1772 EGL_GREEN_SIZE, 1,
1773 EGL_BLUE_SIZE, 1,
1774 EGL_ALPHA_SIZE, 0,
1775 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1776 EGL_NONE
1777};
1778
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001779WL_EXPORT const EGLint gl_renderer_alpha_attribs[] = {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001780 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1781 EGL_RED_SIZE, 1,
1782 EGL_GREEN_SIZE, 1,
1783 EGL_BLUE_SIZE, 1,
1784 EGL_ALPHA_SIZE, 1,
1785 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1786 EGL_NONE
1787};
1788
1789WL_EXPORT int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001790gl_renderer_create(struct weston_compositor *ec, EGLNativeDisplayType display,
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001791 const EGLint *attribs, const EGLint *visual_id)
1792{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001793 struct gl_renderer *gr;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001794 EGLint major, minor;
1795
1796 gr = calloc(1, sizeof *gr);
1797
1798 if (gr == NULL)
1799 return -1;
1800
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001801 gr->base.read_pixels = gl_renderer_read_pixels;
1802 gr->base.repaint_output = gl_renderer_repaint_output;
1803 gr->base.flush_damage = gl_renderer_flush_damage;
1804 gr->base.attach = gl_renderer_attach;
1805 gr->base.create_surface = gl_renderer_create_surface;
1806 gr->base.surface_set_color = gl_renderer_surface_set_color;
1807 gr->base.destroy_surface = gl_renderer_destroy_surface;
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03001808 gr->base.destroy = gl_renderer_destroy;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001809
1810 gr->egl_display = eglGetDisplay(display);
1811 if (gr->egl_display == EGL_NO_DISPLAY) {
1812 weston_log("failed to create display\n");
1813 goto err_egl;
1814 }
1815
1816 if (!eglInitialize(gr->egl_display, &major, &minor)) {
1817 weston_log("failed to initialize display\n");
1818 goto err_egl;
1819 }
1820
1821 if (egl_choose_config(gr, attribs, visual_id) < 0) {
1822 weston_log("failed to choose EGL config\n");
1823 goto err_egl;
1824 }
1825
1826 ec->renderer = &gr->base;
1827
1828 return 0;
1829
1830err_egl:
Pekka Paalanen326529f2012-11-27 12:25:25 +02001831 gl_renderer_print_egl_error_state();
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001832 free(gr);
1833 return -1;
1834}
1835
1836WL_EXPORT EGLDisplay
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001837gl_renderer_display(struct weston_compositor *ec)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001838{
1839 return get_renderer(ec)->egl_display;
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001840}
1841
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001842static int
1843compile_shaders(struct weston_compositor *ec)
1844{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001845 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker40684142012-11-13 19:10:25 +01001846
1847 if (shader_init(&gr->texture_shader_rgba, ec,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001848 vertex_shader, texture_fragment_shader_rgba) < 0)
1849 return -1;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001850 if (shader_init(&gr->texture_shader_rgbx, ec,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001851 vertex_shader, texture_fragment_shader_rgbx) < 0)
1852 return -1;
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001853 if (gr->has_egl_image_external &&
John Kåre Alsaker40684142012-11-13 19:10:25 +01001854 shader_init(&gr->texture_shader_egl_external, ec,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001855 vertex_shader, texture_fragment_shader_egl_external) < 0)
1856 return -1;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001857 if (shader_init(&gr->texture_shader_y_uv, ec,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001858 vertex_shader, texture_fragment_shader_y_uv) < 0)
1859 return -1;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001860 if (shader_init(&gr->texture_shader_y_u_v, ec,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001861 vertex_shader, texture_fragment_shader_y_u_v) < 0)
1862 return -1;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001863 if (shader_init(&gr->texture_shader_y_xuxv, ec,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001864 vertex_shader, texture_fragment_shader_y_xuxv) < 0)
1865 return -1;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001866 if (shader_init(&gr->solid_shader, ec,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001867 vertex_shader, solid_fragment_shader) < 0)
1868 return -1;
1869
1870 return 0;
1871}
1872
1873static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04001874fragment_debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001875 void *data)
1876{
1877 struct weston_compositor *ec = data;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001878 struct gl_renderer *gr = get_renderer(ec);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001879 struct weston_output *output;
1880
John Kåre Alsaker40684142012-11-13 19:10:25 +01001881 gr->fragment_shader_debug ^= 1;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001882
John Kåre Alsaker40684142012-11-13 19:10:25 +01001883 shader_release(&gr->texture_shader_rgba);
1884 shader_release(&gr->texture_shader_rgbx);
1885 shader_release(&gr->texture_shader_egl_external);
1886 shader_release(&gr->texture_shader_y_uv);
1887 shader_release(&gr->texture_shader_y_u_v);
1888 shader_release(&gr->texture_shader_y_xuxv);
1889 shader_release(&gr->solid_shader);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001890
1891 compile_shaders(ec);
1892
Ander Conselvan de Oliveira03fb4ef2012-12-03 17:08:11 +02001893 /* Force use_shader() to call glUseProgram(), since we need to use
1894 * the recompiled version of the shader. */
1895 gr->current_shader = NULL;
1896
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001897 wl_list_for_each(output, &ec->output_list, link)
1898 weston_output_damage(output);
1899}
1900
Kristian Høgsberg8799d412013-05-07 10:50:09 -04001901static void
1902fan_debug_repaint_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
1903 void *data)
1904{
1905 struct weston_compositor *compositor = data;
1906 struct gl_renderer *gr = get_renderer(compositor);
1907
1908 gr->fan_debug = !gr->fan_debug;
1909 weston_compositor_damage_all(compositor);
1910}
1911
John Kåre Alsaker94659272012-11-13 19:10:18 +01001912static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001913gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001914{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001915 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001916 const char *extensions;
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001917 EGLBoolean ret;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001918
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001919 static const EGLint context_attribs[] = {
1920 EGL_CONTEXT_CLIENT_VERSION, 2,
1921 EGL_NONE
1922 };
1923
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001924 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
1925 weston_log("failed to bind EGL_OPENGL_ES_API\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02001926 gl_renderer_print_egl_error_state();
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001927 return -1;
1928 }
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001929
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001930 log_egl_config_info(gr->egl_display, gr->egl_config);
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001931
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001932 gr->egl_context = eglCreateContext(gr->egl_display, gr->egl_config,
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001933 EGL_NO_CONTEXT, context_attribs);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001934 if (gr->egl_context == NULL) {
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001935 weston_log("failed to create context\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02001936 gl_renderer_print_egl_error_state();
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001937 return -1;
1938 }
1939
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001940 ret = eglMakeCurrent(gr->egl_display, egl_surface,
1941 egl_surface, gr->egl_context);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001942 if (ret == EGL_FALSE) {
1943 weston_log("Failed to make EGL context current.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02001944 gl_renderer_print_egl_error_state();
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001945 return -1;
1946 }
1947
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001948 log_egl_gl_info(gr->egl_display);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001949
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001950 gr->image_target_texture_2d =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001951 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001952 gr->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1953 gr->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1954 gr->bind_display =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001955 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001956 gr->unbind_display =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001957 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001958 gr->query_buffer =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001959 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
1960
1961 extensions = (const char *) glGetString(GL_EXTENSIONS);
1962 if (!extensions) {
1963 weston_log("Retrieving GL extension string failed.\n");
1964 return -1;
1965 }
1966
1967 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
1968 weston_log("GL_EXT_texture_format_BGRA8888 not available\n");
1969 return -1;
1970 }
1971
1972 if (strstr(extensions, "GL_EXT_read_format_bgra"))
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01001973 ec->read_format = PIXMAN_a8r8g8b8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001974 else
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01001975 ec->read_format = PIXMAN_a8b8g8r8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001976
1977 if (strstr(extensions, "GL_EXT_unpack_subimage"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001978 gr->has_unpack_subimage = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001979
1980 if (strstr(extensions, "GL_OES_EGL_image_external"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001981 gr->has_egl_image_external = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001982
1983 extensions =
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001984 (const char *) eglQueryString(gr->egl_display, EGL_EXTENSIONS);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001985 if (!extensions) {
1986 weston_log("Retrieving EGL extension string failed.\n");
1987 return -1;
1988 }
1989
1990 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001991 gr->has_bind_display = 1;
1992 if (gr->has_bind_display) {
1993 ret = gr->bind_display(gr->egl_display, ec->wl_display);
Pekka Paalanen035a0322012-10-24 09:43:06 +03001994 if (!ret)
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001995 gr->has_bind_display = 0;
Pekka Paalanen035a0322012-10-24 09:43:06 +03001996 }
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001997
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001998 if (strstr(extensions, "EGL_EXT_buffer_age"))
1999 gr->has_egl_buffer_age = 1;
2000 else
2001 weston_log("warning: EGL_EXT_buffer_age not supported. "
2002 "Performance could be affected.\n");
2003
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002004 glActiveTexture(GL_TEXTURE0);
2005
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002006 if (compile_shaders(ec))
2007 return -1;
2008
2009 weston_compositor_add_debug_binding(ec, KEY_S,
2010 fragment_debug_binding, ec);
Kristian Høgsberg8799d412013-05-07 10:50:09 -04002011 weston_compositor_add_debug_binding(ec, KEY_F,
2012 fan_debug_repaint_binding, ec);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002013
Pekka Paalanen035a0322012-10-24 09:43:06 +03002014 weston_log("GL ES 2 renderer features:\n");
2015 weston_log_continue(STAMP_SPACE "read-back format: %s\n",
Pekka Paalanenfe4eacf2013-01-10 16:50:42 +02002016 ec->read_format == PIXMAN_a8r8g8b8 ? "BGRA" : "RGBA");
Pekka Paalanen035a0322012-10-24 09:43:06 +03002017 weston_log_continue(STAMP_SPACE "wl_shm sub-image to texture: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002018 gr->has_unpack_subimage ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03002019 weston_log_continue(STAMP_SPACE "EGL Wayland extension: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002020 gr->has_bind_display ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03002021
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002022
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002023 return 0;
2024}