blob: f0ae3450faf5723e03a7fe6d0a18c82129e51186 [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
Daniel Stonec228e232013-05-22 18:03:19 +030023#include "config.h"
Kristian Høgsberg25894fc2012-09-05 22:06:26 -040024
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 Larsson0b135062013-05-28 16:23:36 +02001017 width = output->current->width +
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001018 output->border.left + output->border.right;
Alexander Larsson0b135062013-05-28 16:23:36 +02001019 height = output->current->height +
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);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001103 struct weston_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,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001134 wl_shm_buffer_get_data(buffer->shm_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);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001142 data = wl_shm_buffer_get_data(buffer->shm_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
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001185gl_renderer_attach(struct weston_surface *es, struct weston_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);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001190 struct wl_shm_buffer *shm_buffer;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001191 EGLint attribs[3], format;
1192 int i, num_planes;
1193
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001194 weston_buffer_reference(&gs->buffer_ref, buffer);
1195
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001196 if (!buffer) {
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001197 for (i = 0; i < gs->num_images; i++) {
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001198 gr->destroy_image(gr->egl_display, gs->images[i]);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001199 gs->images[i] = NULL;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001200 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001201 gs->num_images = 0;
1202 glDeleteTextures(gs->num_textures, gs->textures);
1203 gs->num_textures = 0;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001204 return;
1205 }
1206
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001207 shm_buffer = wl_shm_buffer_get(buffer->resource);
1208 if (shm_buffer) {
1209 buffer->shm_buffer = shm_buffer;
1210 buffer->width = wl_shm_buffer_get_width(shm_buffer);
1211 buffer->height = wl_shm_buffer_get_height(shm_buffer);
1212
Sinclair Yeh2ada7482013-06-06 16:41:30 -07001213 /* Only allocate a texture if it doesn't match existing one.
1214 * If gs->num_images is not 0, then a switch from DRM allocated
1215 * buffer to a SHM buffer is happening, and we need to allocate
1216 * a new texture buffer. */
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001217 if (wl_shm_buffer_get_stride(shm_buffer) / 4 != gs->pitch ||
1218 buffer->height != gs->height ||
Sinclair Yeh2ada7482013-06-06 16:41:30 -07001219 gs->num_images > 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001220 gs->pitch = wl_shm_buffer_get_stride(shm_buffer) / 4;
1221 gs->height = buffer->height;
Sinclair Yeh2ada7482013-06-06 16:41:30 -07001222 gs->target = GL_TEXTURE_2D;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001223
Sinclair Yeh2ada7482013-06-06 16:41:30 -07001224 ensure_textures(gs, 1);
1225 glBindTexture(GL_TEXTURE_2D, gs->textures[0]);
1226 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1227 gs->pitch, buffer->height, 0,
1228 GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
1229 pixman_region32_union_rect(&gs->texture_damage,
1230 &gs->texture_damage,
1231 0, 0,
1232 gs->pitch / es->buffer_scale,
1233 gs->height / es->buffer_scale);
1234 }
1235
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001236 if (wl_shm_buffer_get_format(shm_buffer) == WL_SHM_FORMAT_XRGB8888)
John Kåre Alsaker40684142012-11-13 19:10:25 +01001237 gs->shader = &gr->texture_shader_rgbx;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001238 else
John Kåre Alsaker40684142012-11-13 19:10:25 +01001239 gs->shader = &gr->texture_shader_rgba;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001240 } else if (gr->query_buffer(gr->egl_display,
1241 (struct wl_buffer *)buffer->resource,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001242 EGL_TEXTURE_FORMAT, &format)) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001243 buffer->legacy_buffer = (struct wl_buffer *)buffer->resource;
1244 buffer->width = buffer->legacy_buffer->width;
1245 buffer->height = buffer->legacy_buffer->height;
1246
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001247 for (i = 0; i < gs->num_images; i++)
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001248 gr->destroy_image(gr->egl_display, gs->images[i]);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001249 gs->num_images = 0;
1250 gs->target = GL_TEXTURE_2D;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001251 switch (format) {
1252 case EGL_TEXTURE_RGB:
1253 case EGL_TEXTURE_RGBA:
1254 default:
1255 num_planes = 1;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001256 gs->shader = &gr->texture_shader_rgba;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001257 break;
1258 case EGL_TEXTURE_EXTERNAL_WL:
1259 num_planes = 1;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001260 gs->target = GL_TEXTURE_EXTERNAL_OES;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001261 gs->shader = &gr->texture_shader_egl_external;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001262 break;
1263 case EGL_TEXTURE_Y_UV_WL:
1264 num_planes = 2;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001265 gs->shader = &gr->texture_shader_y_uv;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001266 break;
1267 case EGL_TEXTURE_Y_U_V_WL:
1268 num_planes = 3;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001269 gs->shader = &gr->texture_shader_y_u_v;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001270 break;
1271 case EGL_TEXTURE_Y_XUXV_WL:
1272 num_planes = 2;
John Kåre Alsaker40684142012-11-13 19:10:25 +01001273 gs->shader = &gr->texture_shader_y_xuxv;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001274 break;
1275 }
1276
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001277 ensure_textures(gs, num_planes);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001278 for (i = 0; i < num_planes; i++) {
1279 attribs[0] = EGL_WAYLAND_PLANE_WL;
1280 attribs[1] = i;
1281 attribs[2] = EGL_NONE;
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001282 gs->images[i] = gr->create_image(gr->egl_display,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001283 NULL,
1284 EGL_WAYLAND_BUFFER_WL,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001285 buffer->legacy_buffer,
1286 attribs);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001287 if (!gs->images[i]) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001288 weston_log("failed to create img for plane %d\n", i);
1289 continue;
1290 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001291 gs->num_images++;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001292
1293 glActiveTexture(GL_TEXTURE0 + i);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001294 glBindTexture(gs->target, gs->textures[i]);
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001295 gr->image_target_texture_2d(gs->target,
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001296 gs->images[i]);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001297 }
1298
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001299 gs->pitch = buffer->width;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001300 gs->height = buffer->height;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001301 } else {
1302 weston_log("unhandled buffer type!\n");
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001303 weston_buffer_reference(&gs->buffer_ref, NULL);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001304 }
1305}
1306
Kristian Høgsberg42263852012-09-06 21:59:29 -04001307static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001308gl_renderer_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001309 float red, float green, float blue, float alpha)
1310{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001311 struct gl_surface_state *gs = get_surface_state(surface);
1312 struct gl_renderer *gr = get_renderer(surface->compositor);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001313
1314 gs->color[0] = red;
1315 gs->color[1] = green;
1316 gs->color[2] = blue;
1317 gs->color[3] = alpha;
1318
John Kåre Alsaker40684142012-11-13 19:10:25 +01001319 gs->shader = &gr->solid_shader;
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001320}
1321
1322static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001323gl_renderer_create_surface(struct weston_surface *surface)
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001324{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001325 struct gl_surface_state *gs;
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001326
1327 gs = calloc(1, sizeof *gs);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001328 if (!gs)
1329 return -1;
1330
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001331 /* A buffer is never attached to solid color surfaces, yet
1332 * they still go through texcoord computations. Do not divide
1333 * by zero there.
1334 */
1335 gs->pitch = 1;
1336
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001337 pixman_region32_init(&gs->texture_damage);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001338 surface->renderer_state = gs;
1339
1340 return 0;
1341}
1342
1343static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001344gl_renderer_destroy_surface(struct weston_surface *surface)
Kristian Høgsberg42263852012-09-06 21:59:29 -04001345{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001346 struct gl_surface_state *gs = get_surface_state(surface);
1347 struct gl_renderer *gr = get_renderer(surface->compositor);
Kristian Høgsberg42263852012-09-06 21:59:29 -04001348 int i;
1349
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001350 glDeleteTextures(gs->num_textures, gs->textures);
Kristian Høgsberg42263852012-09-06 21:59:29 -04001351
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001352 for (i = 0; i < gs->num_images; i++)
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001353 gr->destroy_image(gr->egl_display, gs->images[i]);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001354
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001355 weston_buffer_reference(&gs->buffer_ref, NULL);
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001356 pixman_region32_fini(&gs->texture_damage);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001357 free(gs);
Kristian Høgsberg42263852012-09-06 21:59:29 -04001358}
1359
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001360static const char vertex_shader[] =
1361 "uniform mat4 proj;\n"
1362 "attribute vec2 position;\n"
1363 "attribute vec2 texcoord;\n"
1364 "varying vec2 v_texcoord;\n"
1365 "void main()\n"
1366 "{\n"
1367 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
1368 " v_texcoord = texcoord;\n"
1369 "}\n";
1370
1371/* Declare common fragment shader uniforms */
1372#define FRAGMENT_CONVERT_YUV \
1373 " y *= alpha;\n" \
1374 " u *= alpha;\n" \
1375 " v *= alpha;\n" \
1376 " gl_FragColor.r = y + 1.59602678 * v;\n" \
1377 " gl_FragColor.g = y - 0.39176229 * u - 0.81296764 * v;\n" \
1378 " gl_FragColor.b = y + 2.01723214 * u;\n" \
1379 " gl_FragColor.a = alpha;\n"
1380
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001381static const char fragment_debug[] =
1382 " gl_FragColor = vec4(0.0, 0.3, 0.0, 0.2) + gl_FragColor * 0.8;\n";
1383
1384static const char fragment_brace[] =
1385 "}\n";
1386
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001387static const char texture_fragment_shader_rgba[] =
1388 "precision mediump float;\n"
1389 "varying vec2 v_texcoord;\n"
1390 "uniform sampler2D tex;\n"
1391 "uniform float alpha;\n"
1392 "void main()\n"
1393 "{\n"
1394 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001395 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001396
1397static const char texture_fragment_shader_rgbx[] =
1398 "precision mediump float;\n"
1399 "varying vec2 v_texcoord;\n"
1400 "uniform sampler2D tex;\n"
1401 "uniform float alpha;\n"
1402 "void main()\n"
1403 "{\n"
1404 " gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb\n;"
1405 " gl_FragColor.a = alpha;\n"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001406 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001407
1408static const char texture_fragment_shader_egl_external[] =
1409 "#extension GL_OES_EGL_image_external : require\n"
1410 "precision mediump float;\n"
1411 "varying vec2 v_texcoord;\n"
1412 "uniform samplerExternalOES tex;\n"
1413 "uniform float alpha;\n"
1414 "void main()\n"
1415 "{\n"
1416 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001417 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001418
1419static const char texture_fragment_shader_y_uv[] =
1420 "precision mediump float;\n"
1421 "uniform sampler2D tex;\n"
1422 "uniform sampler2D tex1;\n"
1423 "varying vec2 v_texcoord;\n"
1424 "uniform float alpha;\n"
1425 "void main() {\n"
1426 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1427 " float u = texture2D(tex1, v_texcoord).r - 0.5;\n"
1428 " float v = texture2D(tex1, v_texcoord).g - 0.5;\n"
1429 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001430 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001431
1432static const char texture_fragment_shader_y_u_v[] =
1433 "precision mediump float;\n"
1434 "uniform sampler2D tex;\n"
1435 "uniform sampler2D tex1;\n"
1436 "uniform sampler2D tex2;\n"
1437 "varying vec2 v_texcoord;\n"
1438 "uniform float alpha;\n"
1439 "void main() {\n"
1440 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1441 " float u = texture2D(tex1, v_texcoord).x - 0.5;\n"
1442 " float v = texture2D(tex2, v_texcoord).x - 0.5;\n"
1443 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001444 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001445
1446static const char texture_fragment_shader_y_xuxv[] =
1447 "precision mediump float;\n"
1448 "uniform sampler2D tex;\n"
1449 "uniform sampler2D tex1;\n"
1450 "varying vec2 v_texcoord;\n"
1451 "uniform float alpha;\n"
1452 "void main() {\n"
1453 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1454 " float u = texture2D(tex1, v_texcoord).g - 0.5;\n"
1455 " float v = texture2D(tex1, v_texcoord).a - 0.5;\n"
1456 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001457 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001458
1459static const char solid_fragment_shader[] =
1460 "precision mediump float;\n"
1461 "uniform vec4 color;\n"
1462 "uniform float alpha;\n"
1463 "void main()\n"
1464 "{\n"
1465 " gl_FragColor = alpha * color\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001466 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001467
1468static int
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001469compile_shader(GLenum type, int count, const char **sources)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001470{
1471 GLuint s;
1472 char msg[512];
1473 GLint status;
1474
1475 s = glCreateShader(type);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001476 glShaderSource(s, count, sources, NULL);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001477 glCompileShader(s);
1478 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
1479 if (!status) {
1480 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1481 weston_log("shader info: %s\n", msg);
1482 return GL_NONE;
1483 }
1484
1485 return s;
1486}
1487
1488static int
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03001489shader_init(struct gl_shader *shader, struct gl_renderer *renderer,
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001490 const char *vertex_source, const char *fragment_source)
1491{
1492 char msg[512];
1493 GLint status;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001494 int count;
1495 const char *sources[3];
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001496
1497 shader->vertex_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001498 compile_shader(GL_VERTEX_SHADER, 1, &vertex_source);
1499
1500 if (renderer->fragment_shader_debug) {
1501 sources[0] = fragment_source;
1502 sources[1] = fragment_debug;
1503 sources[2] = fragment_brace;
1504 count = 3;
1505 } else {
1506 sources[0] = fragment_source;
1507 sources[1] = fragment_brace;
1508 count = 2;
1509 }
1510
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001511 shader->fragment_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001512 compile_shader(GL_FRAGMENT_SHADER, count, sources);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001513
1514 shader->program = glCreateProgram();
1515 glAttachShader(shader->program, shader->vertex_shader);
1516 glAttachShader(shader->program, shader->fragment_shader);
1517 glBindAttribLocation(shader->program, 0, "position");
1518 glBindAttribLocation(shader->program, 1, "texcoord");
1519
1520 glLinkProgram(shader->program);
1521 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1522 if (!status) {
1523 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1524 weston_log("link info: %s\n", msg);
1525 return -1;
1526 }
1527
1528 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1529 shader->tex_uniforms[0] = glGetUniformLocation(shader->program, "tex");
1530 shader->tex_uniforms[1] = glGetUniformLocation(shader->program, "tex1");
1531 shader->tex_uniforms[2] = glGetUniformLocation(shader->program, "tex2");
1532 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
1533 shader->color_uniform = glGetUniformLocation(shader->program, "color");
1534
1535 return 0;
1536}
1537
1538static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001539shader_release(struct gl_shader *shader)
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001540{
1541 glDeleteShader(shader->vertex_shader);
1542 glDeleteShader(shader->fragment_shader);
1543 glDeleteProgram(shader->program);
1544
1545 shader->vertex_shader = 0;
1546 shader->fragment_shader = 0;
1547 shader->program = 0;
1548}
1549
1550static void
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001551log_extensions(const char *name, const char *extensions)
1552{
1553 const char *p, *end;
1554 int l;
1555 int len;
1556
1557 l = weston_log("%s:", name);
1558 p = extensions;
1559 while (*p) {
1560 end = strchrnul(p, ' ');
1561 len = end - p;
1562 if (l + len > 78)
1563 l = weston_log_continue("\n" STAMP_SPACE "%.*s",
1564 len, p);
1565 else
1566 l += weston_log_continue(" %.*s", len, p);
1567 for (p = end; isspace(*p); p++)
1568 ;
1569 }
1570 weston_log_continue("\n");
1571}
1572
1573static void
1574log_egl_gl_info(EGLDisplay egldpy)
1575{
1576 const char *str;
1577
1578 str = eglQueryString(egldpy, EGL_VERSION);
1579 weston_log("EGL version: %s\n", str ? str : "(null)");
1580
1581 str = eglQueryString(egldpy, EGL_VENDOR);
1582 weston_log("EGL vendor: %s\n", str ? str : "(null)");
1583
1584 str = eglQueryString(egldpy, EGL_CLIENT_APIS);
1585 weston_log("EGL client APIs: %s\n", str ? str : "(null)");
1586
1587 str = eglQueryString(egldpy, EGL_EXTENSIONS);
1588 log_extensions("EGL extensions", str ? str : "(null)");
1589
1590 str = (char *)glGetString(GL_VERSION);
1591 weston_log("GL version: %s\n", str ? str : "(null)");
1592
1593 str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
1594 weston_log("GLSL version: %s\n", str ? str : "(null)");
1595
1596 str = (char *)glGetString(GL_VENDOR);
1597 weston_log("GL vendor: %s\n", str ? str : "(null)");
1598
1599 str = (char *)glGetString(GL_RENDERER);
1600 weston_log("GL renderer: %s\n", str ? str : "(null)");
1601
1602 str = (char *)glGetString(GL_EXTENSIONS);
1603 log_extensions("GL extensions", str ? str : "(null)");
1604}
1605
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001606static void
1607log_egl_config_info(EGLDisplay egldpy, EGLConfig eglconfig)
1608{
1609 EGLint r, g, b, a;
1610
1611 weston_log("Chosen EGL config details:\n");
1612
1613 weston_log_continue(STAMP_SPACE "RGBA bits");
1614 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_RED_SIZE, &r) &&
1615 eglGetConfigAttrib(egldpy, eglconfig, EGL_GREEN_SIZE, &g) &&
1616 eglGetConfigAttrib(egldpy, eglconfig, EGL_BLUE_SIZE, &b) &&
1617 eglGetConfigAttrib(egldpy, eglconfig, EGL_ALPHA_SIZE, &a))
1618 weston_log_continue(": %d %d %d %d\n", r, g, b, a);
1619 else
1620 weston_log_continue(" unknown\n");
1621
1622 weston_log_continue(STAMP_SPACE "swap interval range");
1623 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_MIN_SWAP_INTERVAL, &a) &&
1624 eglGetConfigAttrib(egldpy, eglconfig, EGL_MAX_SWAP_INTERVAL, &b))
1625 weston_log_continue(": %d - %d\n", a, b);
1626 else
1627 weston_log_continue(" unknown\n");
1628}
1629
John Kåre Alsaker44154502012-11-13 19:10:20 +01001630static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001631output_apply_border(struct weston_output *output, struct gl_renderer *gr)
John Kåre Alsaker44154502012-11-13 19:10:20 +01001632{
1633 output->border.top = gr->border.top;
1634 output->border.bottom = gr->border.bottom;
1635 output->border.left = gr->border.left;
1636 output->border.right = gr->border.right;
1637}
1638
1639WL_EXPORT void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001640gl_renderer_set_border(struct weston_compositor *ec, int32_t width, int32_t height, void *data,
John Kåre Alsaker44154502012-11-13 19:10:20 +01001641 int32_t *edges)
1642{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001643 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker44154502012-11-13 19:10:20 +01001644 struct weston_output *output;
1645
1646 gr->border.left = edges[0];
1647 gr->border.right = edges[1];
1648 gr->border.top = edges[2];
1649 gr->border.bottom = edges[3];
1650
1651 gr->border.width = width;
1652 gr->border.height = height;
1653
1654 glGenTextures(1, &gr->border.texture);
1655 glBindTexture(GL_TEXTURE_2D, gr->border.texture);
1656 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1657 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1658 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1659 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1660
1661 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1662 width,
1663 height,
1664 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1665 data);
1666
1667 wl_list_for_each(output, &ec->output_list, link)
1668 output_apply_border(output, gr);
1669}
1670
John Kåre Alsaker94659272012-11-13 19:10:18 +01001671static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001672gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001673
1674WL_EXPORT int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001675gl_renderer_output_create(struct weston_output *output,
John Kåre Alsaker94659272012-11-13 19:10:18 +01001676 EGLNativeWindowType window)
1677{
1678 struct weston_compositor *ec = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001679 struct gl_renderer *gr = get_renderer(ec);
1680 struct gl_output_state *go = calloc(1, sizeof *go);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001681 int i;
John Kåre Alsaker94659272012-11-13 19:10:18 +01001682
1683 if (!go)
1684 return -1;
1685
1686 go->egl_surface =
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001687 eglCreateWindowSurface(gr->egl_display,
1688 gr->egl_config,
John Kåre Alsaker94659272012-11-13 19:10:18 +01001689 window, NULL);
1690
1691 if (go->egl_surface == EGL_NO_SURFACE) {
1692 weston_log("failed to create egl surface\n");
1693 free(go);
1694 return -1;
1695 }
1696
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001697 if (gr->egl_context == NULL)
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001698 if (gl_renderer_setup(ec, go->egl_surface) < 0) {
John Kåre Alsaker94659272012-11-13 19:10:18 +01001699 free(go);
1700 return -1;
1701 }
1702
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001703 for (i = 0; i < BUFFER_DAMAGE_COUNT; i++)
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001704 pixman_region32_init(&go->buffer_damage[i]);
1705
John Kåre Alsaker94659272012-11-13 19:10:18 +01001706 output->renderer_state = go;
1707
John Kåre Alsaker44154502012-11-13 19:10:20 +01001708 output_apply_border(output, gr);
1709
John Kåre Alsaker94659272012-11-13 19:10:18 +01001710 return 0;
1711}
1712
1713WL_EXPORT void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001714gl_renderer_output_destroy(struct weston_output *output)
John Kåre Alsaker94659272012-11-13 19:10:18 +01001715{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001716 struct gl_renderer *gr = get_renderer(output->compositor);
1717 struct gl_output_state *go = get_output_state(output);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001718 int i;
1719
1720 for (i = 0; i < 2; i++)
1721 pixman_region32_fini(&go->buffer_damage[i]);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001722
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001723 eglDestroySurface(gr->egl_display, go->egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001724
1725 free(go);
1726}
1727
1728WL_EXPORT EGLSurface
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001729gl_renderer_output_surface(struct weston_output *output)
John Kåre Alsaker94659272012-11-13 19:10:18 +01001730{
1731 return get_output_state(output)->egl_surface;
1732}
1733
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03001734static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001735gl_renderer_destroy(struct weston_compositor *ec)
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001736{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001737 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001738
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001739 if (gr->has_bind_display)
1740 gr->unbind_display(gr->egl_display, ec->wl_display);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001741
1742 /* Work around crash in egl_dri2.c's dri2_make_current() - when does this apply? */
1743 eglMakeCurrent(gr->egl_display,
1744 EGL_NO_SURFACE, EGL_NO_SURFACE,
1745 EGL_NO_CONTEXT);
1746
1747 eglTerminate(gr->egl_display);
1748 eglReleaseThread();
Scott Moreau976a0502013-03-07 10:15:17 -07001749
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -04001750 wl_array_release(&gr->vertices);
1751 wl_array_release(&gr->indices);
1752 wl_array_release(&gr->vtxcnt);
1753
Scott Moreau976a0502013-03-07 10:15:17 -07001754 free(gr);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001755}
1756
1757static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001758egl_choose_config(struct gl_renderer *gr, const EGLint *attribs,
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001759 const EGLint *visual_id)
1760{
1761 EGLint count = 0;
1762 EGLint matched = 0;
1763 EGLConfig *configs;
1764 int i;
1765
1766 if (!eglGetConfigs(gr->egl_display, NULL, 0, &count) || count < 1)
1767 return -1;
1768
1769 configs = calloc(count, sizeof *configs);
1770 if (!configs)
1771 return -1;
1772
1773 if (!eglChooseConfig(gr->egl_display, attribs, configs,
1774 count, &matched))
1775 goto out;
1776
1777 for (i = 0; i < matched; ++i) {
1778 EGLint id;
1779
1780 if (visual_id) {
1781 if (!eglGetConfigAttrib(gr->egl_display,
1782 configs[i], EGL_NATIVE_VISUAL_ID,
1783 &id))
1784 continue;
1785
1786 if (id != *visual_id)
1787 continue;
1788 }
1789
1790 gr->egl_config = configs[i];
1791
1792 free(configs);
1793 return 0;
1794 }
1795
1796out:
1797 free(configs);
1798 return -1;
1799}
1800
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001801WL_EXPORT const EGLint gl_renderer_opaque_attribs[] = {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001802 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1803 EGL_RED_SIZE, 1,
1804 EGL_GREEN_SIZE, 1,
1805 EGL_BLUE_SIZE, 1,
1806 EGL_ALPHA_SIZE, 0,
1807 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1808 EGL_NONE
1809};
1810
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001811WL_EXPORT const EGLint gl_renderer_alpha_attribs[] = {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001812 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1813 EGL_RED_SIZE, 1,
1814 EGL_GREEN_SIZE, 1,
1815 EGL_BLUE_SIZE, 1,
1816 EGL_ALPHA_SIZE, 1,
1817 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1818 EGL_NONE
1819};
1820
1821WL_EXPORT int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001822gl_renderer_create(struct weston_compositor *ec, EGLNativeDisplayType display,
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001823 const EGLint *attribs, const EGLint *visual_id)
1824{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001825 struct gl_renderer *gr;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001826 EGLint major, minor;
1827
1828 gr = calloc(1, sizeof *gr);
1829
1830 if (gr == NULL)
1831 return -1;
1832
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001833 gr->base.read_pixels = gl_renderer_read_pixels;
1834 gr->base.repaint_output = gl_renderer_repaint_output;
1835 gr->base.flush_damage = gl_renderer_flush_damage;
1836 gr->base.attach = gl_renderer_attach;
1837 gr->base.create_surface = gl_renderer_create_surface;
1838 gr->base.surface_set_color = gl_renderer_surface_set_color;
1839 gr->base.destroy_surface = gl_renderer_destroy_surface;
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03001840 gr->base.destroy = gl_renderer_destroy;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001841
1842 gr->egl_display = eglGetDisplay(display);
1843 if (gr->egl_display == EGL_NO_DISPLAY) {
1844 weston_log("failed to create display\n");
1845 goto err_egl;
1846 }
1847
1848 if (!eglInitialize(gr->egl_display, &major, &minor)) {
1849 weston_log("failed to initialize display\n");
1850 goto err_egl;
1851 }
1852
1853 if (egl_choose_config(gr, attribs, visual_id) < 0) {
1854 weston_log("failed to choose EGL config\n");
1855 goto err_egl;
1856 }
1857
1858 ec->renderer = &gr->base;
Pekka Paalanen7bb65102013-05-22 18:03:04 +03001859 ec->capabilities |= WESTON_CAP_ROTATION_ANY;
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03001860 ec->capabilities |= WESTON_CAP_CAPTURE_YFLIP;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001861
1862 return 0;
1863
1864err_egl:
Pekka Paalanen326529f2012-11-27 12:25:25 +02001865 gl_renderer_print_egl_error_state();
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001866 free(gr);
1867 return -1;
1868}
1869
1870WL_EXPORT EGLDisplay
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001871gl_renderer_display(struct weston_compositor *ec)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001872{
1873 return get_renderer(ec)->egl_display;
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001874}
1875
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001876static int
1877compile_shaders(struct weston_compositor *ec)
1878{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001879 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker40684142012-11-13 19:10:25 +01001880
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03001881 gr->texture_shader_rgba.vertex_source = vertex_shader;
1882 gr->texture_shader_rgba.fragment_source = texture_fragment_shader_rgba;
1883
1884 gr->texture_shader_rgbx.vertex_source = vertex_shader;
1885 gr->texture_shader_rgbx.fragment_source = texture_fragment_shader_rgbx;
1886
1887 gr->texture_shader_egl_external.vertex_source = vertex_shader;
1888 gr->texture_shader_egl_external.fragment_source =
1889 texture_fragment_shader_egl_external;
1890
1891 gr->texture_shader_y_uv.vertex_source = vertex_shader;
1892 gr->texture_shader_y_uv.fragment_source = texture_fragment_shader_y_uv;
1893
1894 gr->texture_shader_y_u_v.vertex_source = vertex_shader;
1895 gr->texture_shader_y_u_v.fragment_source =
1896 texture_fragment_shader_y_u_v;
1897
1898 gr->texture_shader_y_u_v.vertex_source = vertex_shader;
1899 gr->texture_shader_y_xuxv.fragment_source =
1900 texture_fragment_shader_y_xuxv;
1901
1902 gr->solid_shader.vertex_source = vertex_shader;
1903 gr->solid_shader.fragment_source = solid_fragment_shader;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001904
1905 return 0;
1906}
1907
1908static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04001909fragment_debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001910 void *data)
1911{
1912 struct weston_compositor *ec = data;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001913 struct gl_renderer *gr = get_renderer(ec);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001914 struct weston_output *output;
1915
John Kåre Alsaker40684142012-11-13 19:10:25 +01001916 gr->fragment_shader_debug ^= 1;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001917
John Kåre Alsaker40684142012-11-13 19:10:25 +01001918 shader_release(&gr->texture_shader_rgba);
1919 shader_release(&gr->texture_shader_rgbx);
1920 shader_release(&gr->texture_shader_egl_external);
1921 shader_release(&gr->texture_shader_y_uv);
1922 shader_release(&gr->texture_shader_y_u_v);
1923 shader_release(&gr->texture_shader_y_xuxv);
1924 shader_release(&gr->solid_shader);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001925
Ander Conselvan de Oliveira03fb4ef2012-12-03 17:08:11 +02001926 /* Force use_shader() to call glUseProgram(), since we need to use
1927 * the recompiled version of the shader. */
1928 gr->current_shader = NULL;
1929
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001930 wl_list_for_each(output, &ec->output_list, link)
1931 weston_output_damage(output);
1932}
1933
Kristian Høgsberg8799d412013-05-07 10:50:09 -04001934static void
1935fan_debug_repaint_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
1936 void *data)
1937{
1938 struct weston_compositor *compositor = data;
1939 struct gl_renderer *gr = get_renderer(compositor);
1940
1941 gr->fan_debug = !gr->fan_debug;
1942 weston_compositor_damage_all(compositor);
1943}
1944
John Kåre Alsaker94659272012-11-13 19:10:18 +01001945static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001946gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001947{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001948 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001949 const char *extensions;
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001950 EGLBoolean ret;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001951
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001952 static const EGLint context_attribs[] = {
1953 EGL_CONTEXT_CLIENT_VERSION, 2,
1954 EGL_NONE
1955 };
1956
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001957 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
1958 weston_log("failed to bind EGL_OPENGL_ES_API\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02001959 gl_renderer_print_egl_error_state();
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001960 return -1;
1961 }
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001962
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001963 log_egl_config_info(gr->egl_display, gr->egl_config);
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001964
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001965 gr->egl_context = eglCreateContext(gr->egl_display, gr->egl_config,
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001966 EGL_NO_CONTEXT, context_attribs);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001967 if (gr->egl_context == NULL) {
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001968 weston_log("failed to create context\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02001969 gl_renderer_print_egl_error_state();
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001970 return -1;
1971 }
1972
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001973 ret = eglMakeCurrent(gr->egl_display, egl_surface,
1974 egl_surface, gr->egl_context);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001975 if (ret == EGL_FALSE) {
1976 weston_log("Failed to make EGL context current.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02001977 gl_renderer_print_egl_error_state();
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001978 return -1;
1979 }
1980
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001981 log_egl_gl_info(gr->egl_display);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001982
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001983 gr->image_target_texture_2d =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001984 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001985 gr->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1986 gr->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1987 gr->bind_display =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001988 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001989 gr->unbind_display =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001990 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001991 gr->query_buffer =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001992 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
1993
1994 extensions = (const char *) glGetString(GL_EXTENSIONS);
1995 if (!extensions) {
1996 weston_log("Retrieving GL extension string failed.\n");
1997 return -1;
1998 }
1999
2000 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
2001 weston_log("GL_EXT_texture_format_BGRA8888 not available\n");
2002 return -1;
2003 }
2004
2005 if (strstr(extensions, "GL_EXT_read_format_bgra"))
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01002006 ec->read_format = PIXMAN_a8r8g8b8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002007 else
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01002008 ec->read_format = PIXMAN_a8b8g8r8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002009
2010 if (strstr(extensions, "GL_EXT_unpack_subimage"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002011 gr->has_unpack_subimage = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002012
2013 if (strstr(extensions, "GL_OES_EGL_image_external"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002014 gr->has_egl_image_external = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002015
2016 extensions =
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002017 (const char *) eglQueryString(gr->egl_display, EGL_EXTENSIONS);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002018 if (!extensions) {
2019 weston_log("Retrieving EGL extension string failed.\n");
2020 return -1;
2021 }
2022
2023 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002024 gr->has_bind_display = 1;
2025 if (gr->has_bind_display) {
2026 ret = gr->bind_display(gr->egl_display, ec->wl_display);
Pekka Paalanen035a0322012-10-24 09:43:06 +03002027 if (!ret)
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002028 gr->has_bind_display = 0;
Pekka Paalanen035a0322012-10-24 09:43:06 +03002029 }
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002030
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02002031 if (strstr(extensions, "EGL_EXT_buffer_age"))
2032 gr->has_egl_buffer_age = 1;
2033 else
2034 weston_log("warning: EGL_EXT_buffer_age not supported. "
2035 "Performance could be affected.\n");
2036
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002037 glActiveTexture(GL_TEXTURE0);
2038
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002039 if (compile_shaders(ec))
2040 return -1;
2041
2042 weston_compositor_add_debug_binding(ec, KEY_S,
2043 fragment_debug_binding, ec);
Kristian Høgsberg8799d412013-05-07 10:50:09 -04002044 weston_compositor_add_debug_binding(ec, KEY_F,
2045 fan_debug_repaint_binding, ec);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002046
Pekka Paalanen035a0322012-10-24 09:43:06 +03002047 weston_log("GL ES 2 renderer features:\n");
2048 weston_log_continue(STAMP_SPACE "read-back format: %s\n",
Pekka Paalanenfe4eacf2013-01-10 16:50:42 +02002049 ec->read_format == PIXMAN_a8r8g8b8 ? "BGRA" : "RGBA");
Pekka Paalanen035a0322012-10-24 09:43:06 +03002050 weston_log_continue(STAMP_SPACE "wl_shm sub-image to texture: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002051 gr->has_unpack_subimage ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03002052 weston_log_continue(STAMP_SPACE "EGL Wayland extension: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002053 gr->has_bind_display ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03002054
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002055
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002056 return 0;
2057}