blob: 4381a2eaccb92b7a3dc615f5b4976c63675ad8f6 [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
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +030057enum buffer_type {
58 BUFFER_TYPE_NULL,
59 BUFFER_TYPE_SHM,
60 BUFFER_TYPE_EGL
61};
62
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010063struct gl_surface_state {
John Kåre Alsaker878f4492012-11-13 19:10:23 +010064 GLfloat color[4];
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010065 struct gl_shader *shader;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +010066
67 GLuint textures[3];
68 int num_textures;
Ander Conselvan de Oliveira6be5f432013-06-07 16:52:45 +030069 int needs_full_upload;
Pekka Paalanen81ee3f52012-12-04 15:58:16 +020070 pixman_region32_t texture_damage;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +010071
72 EGLImageKHR images[3];
73 GLenum target;
74 int num_images;
Pekka Paalanenfb003d32012-12-04 15:58:13 +020075
76 struct weston_buffer_reference buffer_ref;
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +030077 enum buffer_type buffer_type;
Pekka Paalanen68033ac2012-12-04 15:58:15 +020078 int pitch; /* in pixels */
Alexander Larsson4ea95522013-05-22 14:41:37 +020079 int height; /* in pixels */
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +040080 int y_inverted;
John Kåre Alsaker878f4492012-11-13 19:10:23 +010081};
82
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010083struct gl_renderer {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +010084 struct weston_renderer base;
85 int fragment_shader_debug;
Kristian Høgsberg8799d412013-05-07 10:50:09 -040086 int fan_debug;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +010087
88 EGLDisplay egl_display;
89 EGLContext egl_context;
90 EGLConfig egl_config;
John Kåre Alsaker44154502012-11-13 19:10:20 +010091
92 struct {
93 int32_t top, bottom, left, right;
94 GLuint texture;
95 int32_t width, height;
96 } border;
John Kåre Alsaker40684142012-11-13 19:10:25 +010097
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -040098 struct wl_array vertices;
99 struct wl_array indices; /* only used in compositor-wayland */
100 struct wl_array vtxcnt;
101
John Kåre Alsaker320711d2012-11-13 19:10:27 +0100102 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
103 PFNEGLCREATEIMAGEKHRPROC create_image;
104 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
105
106 int has_unpack_subimage;
107
108 PFNEGLBINDWAYLANDDISPLAYWL bind_display;
109 PFNEGLUNBINDWAYLANDDISPLAYWL unbind_display;
110 PFNEGLQUERYWAYLANDBUFFERWL query_buffer;
111 int has_bind_display;
112
113 int has_egl_image_external;
114
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +0200115 int has_egl_buffer_age;
116
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100117 struct gl_shader texture_shader_rgba;
118 struct gl_shader texture_shader_rgbx;
119 struct gl_shader texture_shader_egl_external;
120 struct gl_shader texture_shader_y_uv;
121 struct gl_shader texture_shader_y_u_v;
122 struct gl_shader texture_shader_y_xuxv;
123 struct gl_shader invert_color_shader;
124 struct gl_shader solid_shader;
125 struct gl_shader *current_shader;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100126};
John Kåre Alsaker94659272012-11-13 19:10:18 +0100127
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100128static inline struct gl_output_state *
John Kåre Alsaker94659272012-11-13 19:10:18 +0100129get_output_state(struct weston_output *output)
130{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100131 return (struct gl_output_state *)output->renderer_state;
John Kåre Alsaker94659272012-11-13 19:10:18 +0100132}
133
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100134static inline struct gl_surface_state *
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100135get_surface_state(struct weston_surface *surface)
136{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100137 return (struct gl_surface_state *)surface->renderer_state;
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100138}
139
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100140static inline struct gl_renderer *
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100141get_renderer(struct weston_compositor *ec)
142{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100143 return (struct gl_renderer *)ec->renderer;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100144}
145
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400146static const char *
147egl_error_string(EGLint code)
148{
149#define MYERRCODE(x) case x: return #x;
150 switch (code) {
151 MYERRCODE(EGL_SUCCESS)
152 MYERRCODE(EGL_NOT_INITIALIZED)
153 MYERRCODE(EGL_BAD_ACCESS)
154 MYERRCODE(EGL_BAD_ALLOC)
155 MYERRCODE(EGL_BAD_ATTRIBUTE)
156 MYERRCODE(EGL_BAD_CONTEXT)
157 MYERRCODE(EGL_BAD_CONFIG)
158 MYERRCODE(EGL_BAD_CURRENT_SURFACE)
159 MYERRCODE(EGL_BAD_DISPLAY)
160 MYERRCODE(EGL_BAD_SURFACE)
161 MYERRCODE(EGL_BAD_MATCH)
162 MYERRCODE(EGL_BAD_PARAMETER)
163 MYERRCODE(EGL_BAD_NATIVE_PIXMAP)
164 MYERRCODE(EGL_BAD_NATIVE_WINDOW)
165 MYERRCODE(EGL_CONTEXT_LOST)
166 default:
167 return "unknown";
168 }
169#undef MYERRCODE
170}
171
Pekka Paalanen326529f2012-11-27 12:25:25 +0200172WL_EXPORT void
173gl_renderer_print_egl_error_state(void)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400174{
175 EGLint code;
176
177 code = eglGetError();
178 weston_log("EGL error state: %s (0x%04lx)\n",
179 egl_error_string(code), (long)code);
180}
181
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300182struct polygon8 {
183 GLfloat x[8];
184 GLfloat y[8];
185 int n;
186};
187
188struct clip_context {
189 struct {
190 GLfloat x;
191 GLfloat y;
192 } prev;
193
194 struct {
195 GLfloat x1, y1;
196 GLfloat x2, y2;
197 } clip;
198
199 struct {
200 GLfloat *x;
201 GLfloat *y;
202 } vertices;
203};
204
205static GLfloat
206float_difference(GLfloat a, GLfloat b)
207{
208 /* http://www.altdevblogaday.com/2012/02/22/comparing-floating-point-numbers-2012-edition/ */
209 static const GLfloat max_diff = 4.0f * FLT_MIN;
210 static const GLfloat max_rel_diff = 4.0e-5;
211 GLfloat diff = a - b;
212 GLfloat adiff = fabsf(diff);
213
214 if (adiff <= max_diff)
215 return 0.0f;
216
217 a = fabsf(a);
218 b = fabsf(b);
219 if (adiff <= (a > b ? a : b) * max_rel_diff)
220 return 0.0f;
221
222 return diff;
223}
224
225/* A line segment (p1x, p1y)-(p2x, p2y) intersects the line x = x_arg.
226 * Compute the y coordinate of the intersection.
227 */
228static GLfloat
229clip_intersect_y(GLfloat p1x, GLfloat p1y, GLfloat p2x, GLfloat p2y,
230 GLfloat x_arg)
231{
232 GLfloat a;
233 GLfloat diff = float_difference(p1x, p2x);
234
235 /* Practically vertical line segment, yet the end points have already
236 * been determined to be on different sides of the line. Therefore
237 * the line segment is part of the line and intersects everywhere.
238 * Return the end point, so we use the whole line segment.
239 */
240 if (diff == 0.0f)
241 return p2y;
242
243 a = (x_arg - p2x) / diff;
244 return p2y + (p1y - p2y) * a;
245}
246
247/* A line segment (p1x, p1y)-(p2x, p2y) intersects the line y = y_arg.
248 * Compute the x coordinate of the intersection.
249 */
250static GLfloat
251clip_intersect_x(GLfloat p1x, GLfloat p1y, GLfloat p2x, GLfloat p2y,
252 GLfloat y_arg)
253{
254 GLfloat a;
255 GLfloat diff = float_difference(p1y, p2y);
256
257 /* Practically horizontal line segment, yet the end points have already
258 * been determined to be on different sides of the line. Therefore
259 * the line segment is part of the line and intersects everywhere.
260 * Return the end point, so we use the whole line segment.
261 */
262 if (diff == 0.0f)
263 return p2x;
264
265 a = (y_arg - p2y) / diff;
266 return p2x + (p1x - p2x) * a;
267}
268
269enum path_transition {
270 PATH_TRANSITION_OUT_TO_OUT = 0,
271 PATH_TRANSITION_OUT_TO_IN = 1,
272 PATH_TRANSITION_IN_TO_OUT = 2,
273 PATH_TRANSITION_IN_TO_IN = 3,
274};
275
276static void
277clip_append_vertex(struct clip_context *ctx, GLfloat x, GLfloat y)
278{
279 *ctx->vertices.x++ = x;
280 *ctx->vertices.y++ = y;
281}
282
283static enum path_transition
284path_transition_left_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
285{
286 return ((ctx->prev.x >= ctx->clip.x1) << 1) | (x >= ctx->clip.x1);
287}
288
289static enum path_transition
290path_transition_right_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
291{
292 return ((ctx->prev.x < ctx->clip.x2) << 1) | (x < ctx->clip.x2);
293}
294
295static enum path_transition
296path_transition_top_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
297{
298 return ((ctx->prev.y >= ctx->clip.y1) << 1) | (y >= ctx->clip.y1);
299}
300
301static enum path_transition
302path_transition_bottom_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
303{
304 return ((ctx->prev.y < ctx->clip.y2) << 1) | (y < ctx->clip.y2);
305}
306
307static void
308clip_polygon_leftright(struct clip_context *ctx,
309 enum path_transition transition,
310 GLfloat x, GLfloat y, GLfloat clip_x)
311{
312 GLfloat yi;
313
314 switch (transition) {
315 case PATH_TRANSITION_IN_TO_IN:
316 clip_append_vertex(ctx, x, y);
317 break;
318 case PATH_TRANSITION_IN_TO_OUT:
319 yi = clip_intersect_y(ctx->prev.x, ctx->prev.y, x, y, clip_x);
320 clip_append_vertex(ctx, clip_x, yi);
321 break;
322 case PATH_TRANSITION_OUT_TO_IN:
323 yi = clip_intersect_y(ctx->prev.x, ctx->prev.y, x, y, clip_x);
324 clip_append_vertex(ctx, clip_x, yi);
325 clip_append_vertex(ctx, x, y);
326 break;
327 case PATH_TRANSITION_OUT_TO_OUT:
328 /* nothing */
329 break;
330 default:
331 assert(0 && "bad enum path_transition");
332 }
333
334 ctx->prev.x = x;
335 ctx->prev.y = y;
336}
337
338static void
339clip_polygon_topbottom(struct clip_context *ctx,
340 enum path_transition transition,
341 GLfloat x, GLfloat y, GLfloat clip_y)
342{
343 GLfloat xi;
344
345 switch (transition) {
346 case PATH_TRANSITION_IN_TO_IN:
347 clip_append_vertex(ctx, x, y);
348 break;
349 case PATH_TRANSITION_IN_TO_OUT:
350 xi = clip_intersect_x(ctx->prev.x, ctx->prev.y, x, y, clip_y);
351 clip_append_vertex(ctx, xi, clip_y);
352 break;
353 case PATH_TRANSITION_OUT_TO_IN:
354 xi = clip_intersect_x(ctx->prev.x, ctx->prev.y, x, y, clip_y);
355 clip_append_vertex(ctx, xi, clip_y);
356 clip_append_vertex(ctx, x, y);
357 break;
358 case PATH_TRANSITION_OUT_TO_OUT:
359 /* nothing */
360 break;
361 default:
362 assert(0 && "bad enum path_transition");
363 }
364
365 ctx->prev.x = x;
366 ctx->prev.y = y;
367}
368
369static void
370clip_context_prepare(struct clip_context *ctx, const struct polygon8 *src,
371 GLfloat *dst_x, GLfloat *dst_y)
372{
373 ctx->prev.x = src->x[src->n - 1];
374 ctx->prev.y = src->y[src->n - 1];
375 ctx->vertices.x = dst_x;
376 ctx->vertices.y = dst_y;
377}
378
379static int
380clip_polygon_left(struct clip_context *ctx, const struct polygon8 *src,
381 GLfloat *dst_x, GLfloat *dst_y)
382{
383 enum path_transition trans;
384 int i;
385
386 clip_context_prepare(ctx, src, dst_x, dst_y);
387 for (i = 0; i < src->n; i++) {
388 trans = path_transition_left_edge(ctx, src->x[i], src->y[i]);
389 clip_polygon_leftright(ctx, trans, src->x[i], src->y[i],
390 ctx->clip.x1);
391 }
392 return ctx->vertices.x - dst_x;
393}
394
395static int
396clip_polygon_right(struct clip_context *ctx, const struct polygon8 *src,
397 GLfloat *dst_x, GLfloat *dst_y)
398{
399 enum path_transition trans;
400 int i;
401
402 clip_context_prepare(ctx, src, dst_x, dst_y);
403 for (i = 0; i < src->n; i++) {
404 trans = path_transition_right_edge(ctx, src->x[i], src->y[i]);
405 clip_polygon_leftright(ctx, trans, src->x[i], src->y[i],
406 ctx->clip.x2);
407 }
408 return ctx->vertices.x - dst_x;
409}
410
411static int
412clip_polygon_top(struct clip_context *ctx, const struct polygon8 *src,
413 GLfloat *dst_x, GLfloat *dst_y)
414{
415 enum path_transition trans;
416 int i;
417
418 clip_context_prepare(ctx, src, dst_x, dst_y);
419 for (i = 0; i < src->n; i++) {
420 trans = path_transition_top_edge(ctx, src->x[i], src->y[i]);
421 clip_polygon_topbottom(ctx, trans, src->x[i], src->y[i],
422 ctx->clip.y1);
423 }
424 return ctx->vertices.x - dst_x;
425}
426
427static int
428clip_polygon_bottom(struct clip_context *ctx, const struct polygon8 *src,
429 GLfloat *dst_x, GLfloat *dst_y)
430{
431 enum path_transition trans;
432 int i;
433
434 clip_context_prepare(ctx, src, dst_x, dst_y);
435 for (i = 0; i < src->n; i++) {
436 trans = path_transition_bottom_edge(ctx, src->x[i], src->y[i]);
437 clip_polygon_topbottom(ctx, trans, src->x[i], src->y[i],
438 ctx->clip.y2);
439 }
440 return ctx->vertices.x - dst_x;
441}
442
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400443#define max(a, b) (((a) > (b)) ? (a) : (b))
444#define min(a, b) (((a) > (b)) ? (b) : (a))
445#define clip(x, a, b) min(max(x, a), b)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400446
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300447/*
448 * Compute the boundary vertices of the intersection of the global coordinate
449 * aligned rectangle 'rect', and an arbitrary quadrilateral produced from
450 * 'surf_rect' when transformed from surface coordinates into global coordinates.
451 * The vertices are written to 'ex' and 'ey', and the return value is the
452 * number of vertices. Vertices are produced in clockwise winding order.
453 * Guarantees to produce either zero vertices, or 3-8 vertices with non-zero
454 * polygon area.
455 */
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400456static int
457calculate_edges(struct weston_surface *es, pixman_box32_t *rect,
458 pixman_box32_t *surf_rect, GLfloat *ex, GLfloat *ey)
459{
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300460 struct polygon8 polygon;
461 struct clip_context ctx;
462 int i, n;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400463 GLfloat min_x, max_x, min_y, max_y;
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300464 struct polygon8 surf = {
465 { surf_rect->x1, surf_rect->x2, surf_rect->x2, surf_rect->x1 },
466 { surf_rect->y1, surf_rect->y1, surf_rect->y2, surf_rect->y2 },
467 4
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400468 };
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400469
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300470 ctx.clip.x1 = rect->x1;
471 ctx.clip.y1 = rect->y1;
472 ctx.clip.x2 = rect->x2;
473 ctx.clip.y2 = rect->y2;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400474
475 /* transform surface to screen space: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300476 for (i = 0; i < surf.n; i++)
477 weston_surface_to_global_float(es, surf.x[i], surf.y[i],
478 &surf.x[i], &surf.y[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400479
480 /* find bounding box: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300481 min_x = max_x = surf.x[0];
482 min_y = max_y = surf.y[0];
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400483
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300484 for (i = 1; i < surf.n; i++) {
485 min_x = min(min_x, surf.x[i]);
486 max_x = max(max_x, surf.x[i]);
487 min_y = min(min_y, surf.y[i]);
488 max_y = max(max_y, surf.y[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400489 }
490
491 /* First, simple bounding box check to discard early transformed
492 * surface rects that do not intersect with the clip region:
493 */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300494 if ((min_x >= ctx.clip.x2) || (max_x <= ctx.clip.x1) ||
495 (min_y >= ctx.clip.y2) || (max_y <= ctx.clip.y1))
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400496 return 0;
497
498 /* Simple case, bounding box edges are parallel to surface edges,
499 * there will be only four edges. We just need to clip the surface
500 * vertices to the clip rect bounds:
501 */
502 if (!es->transform.enabled) {
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300503 for (i = 0; i < surf.n; i++) {
504 ex[i] = clip(surf.x[i], ctx.clip.x1, ctx.clip.x2);
505 ey[i] = clip(surf.y[i], ctx.clip.y1, ctx.clip.y2);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400506 }
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300507 return surf.n;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400508 }
509
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300510 /* Transformed case: use a general polygon clipping algorithm to
511 * clip the surface rectangle with each side of 'rect'.
512 * The algorithm is Sutherland-Hodgman, as explained in
513 * http://www.codeguru.com/cpp/misc/misc/graphics/article.php/c8965/Polygon-Clipping.htm
514 * but without looking at any of that code.
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400515 */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300516 polygon.n = clip_polygon_left(&ctx, &surf, polygon.x, polygon.y);
517 surf.n = clip_polygon_right(&ctx, &polygon, surf.x, surf.y);
518 polygon.n = clip_polygon_top(&ctx, &surf, polygon.x, polygon.y);
519 surf.n = clip_polygon_bottom(&ctx, &polygon, surf.x, surf.y);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400520
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300521 /* Get rid of duplicate vertices */
522 ex[0] = surf.x[0];
523 ey[0] = surf.y[0];
524 n = 1;
525 for (i = 1; i < surf.n; i++) {
526 if (float_difference(ex[n - 1], surf.x[i]) == 0.0f &&
527 float_difference(ey[n - 1], surf.y[i]) == 0.0f)
528 continue;
529 ex[n] = surf.x[i];
530 ey[n] = surf.y[i];
531 n++;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400532 }
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300533 if (float_difference(ex[n - 1], surf.x[0]) == 0.0f &&
534 float_difference(ey[n - 1], surf.y[0]) == 0.0f)
535 n--;
536
537 if (n < 3)
538 return 0;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400539
540 return n;
541}
542
543static int
544texture_region(struct weston_surface *es, pixman_region32_t *region,
545 pixman_region32_t *surf_region)
546{
Pekka Paalanen68033ac2012-12-04 15:58:15 +0200547 struct gl_surface_state *gs = get_surface_state(es);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400548 struct weston_compositor *ec = es->compositor;
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400549 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400550 GLfloat *v, inv_width, inv_height;
551 unsigned int *vtxcnt, nvtx = 0;
552 pixman_box32_t *rects, *surf_rects;
553 int i, j, k, nrects, nsurf;
554
555 rects = pixman_region32_rectangles(region, &nrects);
556 surf_rects = pixman_region32_rectangles(surf_region, &nsurf);
557
558 /* worst case we can have 8 vertices per rect (ie. clipped into
559 * an octagon):
560 */
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400561 v = wl_array_add(&gr->vertices, nrects * nsurf * 8 * 4 * sizeof *v);
562 vtxcnt = wl_array_add(&gr->vtxcnt, nrects * nsurf * sizeof *vtxcnt);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400563
Pekka Paalanen68033ac2012-12-04 15:58:15 +0200564 inv_width = 1.0 / gs->pitch;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200565 inv_height = 1.0 / gs->height;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400566
567 for (i = 0; i < nrects; i++) {
568 pixman_box32_t *rect = &rects[i];
569 for (j = 0; j < nsurf; j++) {
570 pixman_box32_t *surf_rect = &surf_rects[j];
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200571 GLfloat sx, sy, bx, by;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400572 GLfloat ex[8], ey[8]; /* edge points in screen space */
573 int n;
574
575 /* The transformed surface, after clipping to the clip region,
576 * can have as many as eight sides, emitted as a triangle-fan.
577 * The first vertex in the triangle fan can be chosen arbitrarily,
578 * since the area is guaranteed to be convex.
579 *
580 * If a corner of the transformed surface falls outside of the
581 * clip region, instead of emitting one vertex for the corner
582 * of the surface, up to two are emitted for two corresponding
583 * intersection point(s) between the surface and the clip region.
584 *
585 * To do this, we first calculate the (up to eight) points that
586 * form the intersection of the clip rect and the transformed
587 * surface.
588 */
589 n = calculate_edges(es, rect, surf_rect, ex, ey);
590 if (n < 3)
591 continue;
592
593 /* emit edge points: */
594 for (k = 0; k < n; k++) {
595 weston_surface_from_global_float(es, ex[k], ey[k], &sx, &sy);
596 /* position: */
597 *(v++) = ex[k];
598 *(v++) = ey[k];
599 /* texcoord: */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200600 weston_surface_to_buffer_float(es, sx, sy,
601 &bx, &by);
602 *(v++) = bx * inv_width;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +0400603 if (gs->y_inverted) {
604 *(v++) = by * inv_height;
605 } else {
606 *(v++) = (gs->height - by) * inv_height;
607 }
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400608 }
609
610 vtxcnt[nvtx++] = n;
611 }
612 }
613
614 return nvtx;
615}
616
617static void
618triangle_fan_debug(struct weston_surface *surface, int first, int count)
619{
620 struct weston_compositor *compositor = surface->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100621 struct gl_renderer *gr = get_renderer(compositor);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400622 int i;
623 GLushort *buffer;
624 GLushort *index;
625 int nelems;
626 static int color_idx = 0;
627 static const GLfloat color[][4] = {
628 { 1.0, 0.0, 0.0, 1.0 },
629 { 0.0, 1.0, 0.0, 1.0 },
630 { 0.0, 0.0, 1.0, 1.0 },
631 { 1.0, 1.0, 1.0, 1.0 },
632 };
633
634 nelems = (count - 1 + count - 2) * 2;
635
636 buffer = malloc(sizeof(GLushort) * nelems);
637 index = buffer;
638
639 for (i = 1; i < count; i++) {
640 *index++ = first;
641 *index++ = first + i;
642 }
643
644 for (i = 2; i < count; i++) {
645 *index++ = first + i - 1;
646 *index++ = first + i;
647 }
648
John Kåre Alsaker40684142012-11-13 19:10:25 +0100649 glUseProgram(gr->solid_shader.program);
650 glUniform4fv(gr->solid_shader.color_uniform, 1,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400651 color[color_idx++ % ARRAY_LENGTH(color)]);
652 glDrawElements(GL_LINES, nelems, GL_UNSIGNED_SHORT, buffer);
John Kåre Alsaker40684142012-11-13 19:10:25 +0100653 glUseProgram(gr->current_shader->program);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400654 free(buffer);
655}
656
657static void
658repaint_region(struct weston_surface *es, pixman_region32_t *region,
659 pixman_region32_t *surf_region)
660{
661 struct weston_compositor *ec = es->compositor;
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400662 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400663 GLfloat *v;
664 unsigned int *vtxcnt;
665 int i, first, nfans;
666
667 /* The final region to be painted is the intersection of
668 * 'region' and 'surf_region'. However, 'region' is in the global
669 * coordinates, and 'surf_region' is in the surface-local
670 * coordinates. texture_region() will iterate over all pairs of
671 * rectangles from both regions, compute the intersection
672 * polygon for each pair, and store it as a triangle fan if
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400673 * it has a non-zero area (at least 3 vertices1, actually).
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400674 */
675 nfans = texture_region(es, region, surf_region);
676
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400677 v = gr->vertices.data;
678 vtxcnt = gr->vtxcnt.data;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400679
680 /* position: */
681 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
682 glEnableVertexAttribArray(0);
683
684 /* texcoord: */
685 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
686 glEnableVertexAttribArray(1);
687
688 for (i = 0, first = 0; i < nfans; i++) {
689 glDrawArrays(GL_TRIANGLE_FAN, first, vtxcnt[i]);
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400690 if (gr->fan_debug)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400691 triangle_fan_debug(es, first, vtxcnt[i]);
692 first += vtxcnt[i];
693 }
694
695 glDisableVertexAttribArray(1);
696 glDisableVertexAttribArray(0);
697
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400698 gr->vertices.size = 0;
699 gr->vtxcnt.size = 0;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400700}
701
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100702static int
703use_output(struct weston_output *output)
704{
705 static int errored;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100706 struct gl_output_state *go = get_output_state(output);
707 struct gl_renderer *gr = get_renderer(output->compositor);
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100708 EGLBoolean ret;
709
710 ret = eglMakeCurrent(gr->egl_display, go->egl_surface,
711 go->egl_surface, gr->egl_context);
712
713 if (ret == EGL_FALSE) {
714 if (errored)
715 return -1;
716 errored = 1;
717 weston_log("Failed to make EGL context current.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +0200718 gl_renderer_print_egl_error_state();
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100719 return -1;
720 }
721
722 return 0;
723}
724
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +0300725static int
726shader_init(struct gl_shader *shader, struct gl_renderer *gr,
727 const char *vertex_source, const char *fragment_source);
728
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400729static void
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +0300730use_shader(struct gl_renderer *gr, struct gl_shader *shader)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400731{
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +0300732 if (!shader->program) {
733 int ret;
734
735 ret = shader_init(shader, gr,
736 shader->vertex_source,
737 shader->fragment_source);
738
739 if (ret < 0)
740 weston_log("warning: failed to compile shader\n");
741 }
742
John Kåre Alsaker40684142012-11-13 19:10:25 +0100743 if (gr->current_shader == shader)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400744 return;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400745 glUseProgram(shader->program);
John Kåre Alsaker40684142012-11-13 19:10:25 +0100746 gr->current_shader = shader;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400747}
748
749static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100750shader_uniforms(struct gl_shader *shader,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400751 struct weston_surface *surface,
752 struct weston_output *output)
753{
754 int i;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100755 struct gl_surface_state *gs = get_surface_state(surface);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400756
757 glUniformMatrix4fv(shader->proj_uniform,
758 1, GL_FALSE, output->matrix.d);
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100759 glUniform4fv(shader->color_uniform, 1, gs->color);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400760 glUniform1f(shader->alpha_uniform, surface->alpha);
761
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100762 for (i = 0; i < gs->num_textures; i++)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400763 glUniform1i(shader->tex_uniforms[i], i);
764}
765
766static void
767draw_surface(struct weston_surface *es, struct weston_output *output,
768 pixman_region32_t *damage) /* in global coordinates */
769{
770 struct weston_compositor *ec = es->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100771 struct gl_renderer *gr = get_renderer(ec);
772 struct gl_surface_state *gs = get_surface_state(es);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400773 /* repaint bounding region in global coordinates: */
774 pixman_region32_t repaint;
775 /* non-opaque region in surface coordinates: */
776 pixman_region32_t surface_blend;
777 GLint filter;
778 int i;
779
780 pixman_region32_init(&repaint);
781 pixman_region32_intersect(&repaint,
782 &es->transform.boundingbox, damage);
783 pixman_region32_subtract(&repaint, &repaint, &es->clip);
784
785 if (!pixman_region32_not_empty(&repaint))
786 goto out;
787
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400788 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
789
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400790 if (gr->fan_debug) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100791 use_shader(gr, &gr->solid_shader);
792 shader_uniforms(&gr->solid_shader, es, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400793 }
794
John Kåre Alsaker40684142012-11-13 19:10:25 +0100795 use_shader(gr, gs->shader);
796 shader_uniforms(gs->shader, es, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400797
Alexander Larsson4ea95522013-05-22 14:41:37 +0200798 if (es->transform.enabled || output->zoom.active || output->scale != es->buffer_scale)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400799 filter = GL_LINEAR;
800 else
801 filter = GL_NEAREST;
802
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100803 for (i = 0; i < gs->num_textures; i++) {
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400804 glActiveTexture(GL_TEXTURE0 + i);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100805 glBindTexture(gs->target, gs->textures[i]);
806 glTexParameteri(gs->target, GL_TEXTURE_MIN_FILTER, filter);
807 glTexParameteri(gs->target, GL_TEXTURE_MAG_FILTER, filter);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400808 }
809
810 /* blended region is whole surface minus opaque region: */
811 pixman_region32_init_rect(&surface_blend, 0, 0,
812 es->geometry.width, es->geometry.height);
813 pixman_region32_subtract(&surface_blend, &surface_blend, &es->opaque);
814
815 if (pixman_region32_not_empty(&es->opaque)) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100816 if (gs->shader == &gr->texture_shader_rgba) {
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400817 /* Special case for RGBA textures with possibly
818 * bad data in alpha channel: use the shader
819 * that forces texture alpha = 1.0.
820 * Xwayland surfaces need this.
821 */
John Kåre Alsaker40684142012-11-13 19:10:25 +0100822 use_shader(gr, &gr->texture_shader_rgbx);
823 shader_uniforms(&gr->texture_shader_rgbx, es, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400824 }
825
826 if (es->alpha < 1.0)
827 glEnable(GL_BLEND);
828 else
829 glDisable(GL_BLEND);
830
831 repaint_region(es, &repaint, &es->opaque);
832 }
833
834 if (pixman_region32_not_empty(&surface_blend)) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100835 use_shader(gr, gs->shader);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400836 glEnable(GL_BLEND);
837 repaint_region(es, &repaint, &surface_blend);
838 }
839
840 pixman_region32_fini(&surface_blend);
841
842out:
843 pixman_region32_fini(&repaint);
844}
845
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400846static void
847repaint_surfaces(struct weston_output *output, pixman_region32_t *damage)
848{
849 struct weston_compositor *compositor = output->compositor;
850 struct weston_surface *surface;
851
852 wl_list_for_each_reverse(surface, &compositor->surface_list, link)
853 if (surface->plane == &compositor->primary_plane)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400854 draw_surface(surface, output, damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400855}
856
John Kåre Alsaker44154502012-11-13 19:10:20 +0100857
858static int
859texture_border(struct weston_output *output)
860{
861 struct weston_compositor *ec = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100862 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker44154502012-11-13 19:10:20 +0100863 GLfloat *d;
Kristian Høgsberg73db9242013-08-28 23:05:29 -0700864 unsigned short *p;
John Kåre Alsaker44154502012-11-13 19:10:20 +0100865 int i, j, k, n;
866 GLfloat x[4], y[4], u[4], v[4];
867
868 x[0] = -gr->border.left;
869 x[1] = 0;
870 x[2] = output->current->width;
871 x[3] = output->current->width + gr->border.right;
872
873 y[0] = -gr->border.top;
874 y[1] = 0;
875 y[2] = output->current->height;
876 y[3] = output->current->height + gr->border.bottom;
877
878 u[0] = 0.0;
879 u[1] = (GLfloat) gr->border.left / gr->border.width;
880 u[2] = (GLfloat) (gr->border.width - gr->border.right) / gr->border.width;
881 u[3] = 1.0;
882
883 v[0] = 0.0;
884 v[1] = (GLfloat) gr->border.top / gr->border.height;
885 v[2] = (GLfloat) (gr->border.height - gr->border.bottom) / gr->border.height;
886 v[3] = 1.0;
887
888 n = 8;
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400889 d = wl_array_add(&gr->vertices, n * 16 * sizeof *d);
890 p = wl_array_add(&gr->indices, n * 6 * sizeof *p);
John Kåre Alsaker44154502012-11-13 19:10:20 +0100891
892 k = 0;
893 for (i = 0; i < 3; i++)
894 for (j = 0; j < 3; j++) {
895
896 if (i == 1 && j == 1)
897 continue;
898
899 d[ 0] = x[i];
900 d[ 1] = y[j];
901 d[ 2] = u[i];
902 d[ 3] = v[j];
903
904 d[ 4] = x[i];
905 d[ 5] = y[j + 1];
906 d[ 6] = u[i];
907 d[ 7] = v[j + 1];
908
909 d[ 8] = x[i + 1];
910 d[ 9] = y[j];
911 d[10] = u[i + 1];
912 d[11] = v[j];
913
914 d[12] = x[i + 1];
915 d[13] = y[j + 1];
916 d[14] = u[i + 1];
917 d[15] = v[j + 1];
918
919 p[0] = k + 0;
920 p[1] = k + 1;
921 p[2] = k + 2;
922 p[3] = k + 2;
923 p[4] = k + 1;
924 p[5] = k + 3;
925
926 d += 16;
927 p += 6;
928 k += 4;
929 }
930
931 return k / 4;
932}
933
934static void
935draw_border(struct weston_output *output)
936{
937 struct weston_compositor *ec = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100938 struct gl_renderer *gr = get_renderer(ec);
939 struct gl_shader *shader = &gr->texture_shader_rgba;
John Kåre Alsaker44154502012-11-13 19:10:20 +0100940 GLfloat *v;
941 int n;
942
943 glDisable(GL_BLEND);
John Kåre Alsaker40684142012-11-13 19:10:25 +0100944 use_shader(gr, shader);
John Kåre Alsaker44154502012-11-13 19:10:20 +0100945
946 glUniformMatrix4fv(shader->proj_uniform,
947 1, GL_FALSE, output->matrix.d);
948
949 glUniform1i(shader->tex_uniforms[0], 0);
950 glUniform1f(shader->alpha_uniform, 1);
951
952 n = texture_border(output);
953
954 glActiveTexture(GL_TEXTURE0);
955 glBindTexture(GL_TEXTURE_2D, gr->border.texture);
956
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400957 v = gr->vertices.data;
John Kåre Alsaker44154502012-11-13 19:10:20 +0100958 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
959 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
960 glEnableVertexAttribArray(0);
961 glEnableVertexAttribArray(1);
962
963 glDrawElements(GL_TRIANGLES, n * 6,
Kristian Høgsberg73db9242013-08-28 23:05:29 -0700964 GL_UNSIGNED_SHORT, gr->indices.data);
John Kåre Alsaker44154502012-11-13 19:10:20 +0100965
966 glDisableVertexAttribArray(1);
967 glDisableVertexAttribArray(0);
968
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400969 gr->vertices.size = 0;
970 gr->indices.size = 0;
John Kåre Alsaker44154502012-11-13 19:10:20 +0100971}
972
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400973static void
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +0200974output_get_buffer_damage(struct weston_output *output,
975 pixman_region32_t *buffer_damage)
976{
977 struct gl_output_state *go = get_output_state(output);
978 struct gl_renderer *gr = get_renderer(output->compositor);
979 EGLint buffer_age = 0;
980 EGLBoolean ret;
981 int i;
982
983 if (gr->has_egl_buffer_age) {
984 ret = eglQuerySurface(gr->egl_display, go->egl_surface,
985 EGL_BUFFER_AGE_EXT, &buffer_age);
986 if (ret == EGL_FALSE) {
987 weston_log("buffer age query failed.\n");
988 gl_renderer_print_egl_error_state();
989 }
990 }
991
992 if (buffer_age == 0 || buffer_age - 1 > BUFFER_DAMAGE_COUNT)
993 pixman_region32_copy(buffer_damage, &output->region);
994 else
995 for (i = 0; i < buffer_age - 1; i++)
996 pixman_region32_union(buffer_damage, buffer_damage,
997 &go->buffer_damage[i]);
998}
999
1000static void
1001output_rotate_damage(struct weston_output *output,
1002 pixman_region32_t *output_damage)
1003{
1004 struct gl_output_state *go = get_output_state(output);
1005 struct gl_renderer *gr = get_renderer(output->compositor);
1006 int i;
1007
1008 if (!gr->has_egl_buffer_age)
1009 return;
1010
1011 for (i = BUFFER_DAMAGE_COUNT - 1; i >= 1; i--)
1012 pixman_region32_copy(&go->buffer_damage[i],
1013 &go->buffer_damage[i - 1]);
1014
1015 pixman_region32_copy(&go->buffer_damage[0], output_damage);
1016}
1017
1018static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001019gl_renderer_repaint_output(struct weston_output *output,
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001020 pixman_region32_t *output_damage)
1021{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001022 struct gl_output_state *go = get_output_state(output);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001023 struct weston_compositor *compositor = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001024 struct gl_renderer *gr = get_renderer(compositor);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001025 EGLBoolean ret;
1026 static int errored;
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001027 int32_t width, height;
1028 pixman_region32_t buffer_damage, total_damage;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001029
Alexander Larsson0b135062013-05-28 16:23:36 +02001030 width = output->current->width +
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001031 output->border.left + output->border.right;
Alexander Larsson0b135062013-05-28 16:23:36 +02001032 height = output->current->height +
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001033 output->border.top + output->border.bottom;
1034
1035 glViewport(0, 0, width, height);
1036
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001037 if (use_output(output) < 0)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001038 return;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001039
1040 /* if debugging, redraw everything outside the damage to clean up
1041 * debug lines from the previous draw on this buffer:
1042 */
Kristian Høgsberg8799d412013-05-07 10:50:09 -04001043 if (gr->fan_debug) {
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001044 pixman_region32_t undamaged;
1045 pixman_region32_init(&undamaged);
1046 pixman_region32_subtract(&undamaged, &output->region,
1047 output_damage);
Kristian Høgsberg8799d412013-05-07 10:50:09 -04001048 gr->fan_debug = 0;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001049 repaint_surfaces(output, &undamaged);
Kristian Høgsberg8799d412013-05-07 10:50:09 -04001050 gr->fan_debug = 1;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001051 pixman_region32_fini(&undamaged);
1052 }
1053
Ander Conselvan de Oliveirab605c062013-03-05 17:30:28 +02001054 pixman_region32_init(&total_damage);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001055 pixman_region32_init(&buffer_damage);
1056
1057 output_get_buffer_damage(output, &buffer_damage);
1058 output_rotate_damage(output, output_damage);
1059
1060 pixman_region32_union(&total_damage, &buffer_damage, output_damage);
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03001061
Ander Conselvan de Oliveirab605c062013-03-05 17:30:28 +02001062 repaint_surfaces(output, &total_damage);
1063
1064 pixman_region32_fini(&total_damage);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001065 pixman_region32_fini(&buffer_damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001066
John Kåre Alsaker44154502012-11-13 19:10:20 +01001067 if (gr->border.texture)
1068 draw_border(output);
1069
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001070 pixman_region32_copy(&output->previous_damage, output_damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001071 wl_signal_emit(&output->frame_signal, output);
1072
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001073 ret = eglSwapBuffers(gr->egl_display, go->egl_surface);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001074 if (ret == EGL_FALSE && !errored) {
1075 errored = 1;
1076 weston_log("Failed in eglSwapBuffers.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02001077 gl_renderer_print_egl_error_state();
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001078 }
1079
1080}
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001081
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001082static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001083gl_renderer_read_pixels(struct weston_output *output,
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001084 pixman_format_code_t format, void *pixels,
1085 uint32_t x, uint32_t y,
1086 uint32_t width, uint32_t height)
1087{
1088 GLenum gl_format;
1089
1090 switch (format) {
1091 case PIXMAN_a8r8g8b8:
1092 gl_format = GL_BGRA_EXT;
1093 break;
1094 case PIXMAN_a8b8g8r8:
1095 gl_format = GL_RGBA;
1096 break;
1097 default:
1098 return -1;
1099 }
1100
1101 if (use_output(output) < 0)
1102 return -1;
1103
1104 glPixelStorei(GL_PACK_ALIGNMENT, 1);
1105 glReadPixels(x, y, width, height, gl_format,
1106 GL_UNSIGNED_BYTE, pixels);
1107
1108 return 0;
1109}
1110
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001111static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001112gl_renderer_flush_damage(struct weston_surface *surface)
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001113{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001114 struct gl_renderer *gr = get_renderer(surface->compositor);
1115 struct gl_surface_state *gs = get_surface_state(surface);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001116 struct weston_buffer *buffer = gs->buffer_ref.buffer;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001117 GLenum format;
1118 int pixel_type;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001119
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -07001120#ifdef GL_EXT_unpack_subimage
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001121 pixman_box32_t *rectangles;
1122 void *data;
1123 int i, n;
1124#endif
1125
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001126 pixman_region32_union(&gs->texture_damage,
1127 &gs->texture_damage, &surface->damage);
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001128
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001129 if (!buffer)
1130 return;
1131
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001132 /* Avoid upload, if the texture won't be used this time.
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001133 * We still accumulate the damage in texture_damage, and
1134 * hold the reference to the buffer, in case the surface
1135 * migrates back to the primary plane.
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001136 */
1137 if (surface->plane != &surface->compositor->primary_plane)
1138 return;
1139
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001140 if (!pixman_region32_not_empty(&gs->texture_damage))
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001141 goto done;
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001142
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001143 switch (wl_shm_buffer_get_format(buffer->shm_buffer)) {
1144 case WL_SHM_FORMAT_XRGB8888:
1145 case WL_SHM_FORMAT_ARGB8888:
1146 format = GL_BGRA_EXT;
1147 pixel_type = GL_UNSIGNED_BYTE;
1148 break;
1149 case WL_SHM_FORMAT_RGB565:
1150 format = GL_RGB;
1151 pixel_type = GL_UNSIGNED_SHORT_5_6_5;
1152 break;
1153 default:
1154 weston_log("warning: unknown shm buffer format\n");
1155 format = GL_BGRA_EXT;
1156 pixel_type = GL_UNSIGNED_BYTE;
1157 }
1158
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001159 glBindTexture(GL_TEXTURE_2D, gs->textures[0]);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001160
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001161 if (!gr->has_unpack_subimage) {
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001162 glTexImage2D(GL_TEXTURE_2D, 0, format,
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001163 gs->pitch, buffer->height, 0,
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001164 format, pixel_type,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001165 wl_shm_buffer_get_data(buffer->shm_buffer));
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001166
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001167 goto done;
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001168 }
1169
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -07001170#ifdef GL_EXT_unpack_subimage
1171 glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, gs->pitch);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001172 data = wl_shm_buffer_get_data(buffer->shm_buffer);
Ander Conselvan de Oliveira6be5f432013-06-07 16:52:45 +03001173
1174 if (gs->needs_full_upload) {
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -07001175 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, 0);
1176 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, 0);
Ander Conselvan de Oliveira6be5f432013-06-07 16:52:45 +03001177 glTexSubImage2D(GL_TEXTURE_2D, 0,
1178 0, 0, gs->pitch, buffer->height,
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001179 format, pixel_type, data);
Ander Conselvan de Oliveira6be5f432013-06-07 16:52:45 +03001180 goto done;
1181 }
1182
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001183 rectangles = pixman_region32_rectangles(&gs->texture_damage, &n);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001184 for (i = 0; i < n; i++) {
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001185 pixman_box32_t r;
1186
1187 r = weston_surface_to_buffer_rect(surface, rectangles[i]);
1188
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -07001189 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, r.x1);
1190 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, r.y1);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001191 glTexSubImage2D(GL_TEXTURE_2D, 0, r.x1, r.y1,
1192 r.x2 - r.x1, r.y2 - r.y1,
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001193 format, pixel_type, data);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001194 }
1195#endif
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001196
1197done:
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001198 pixman_region32_fini(&gs->texture_damage);
1199 pixman_region32_init(&gs->texture_damage);
Ander Conselvan de Oliveira6be5f432013-06-07 16:52:45 +03001200 gs->needs_full_upload = 0;
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001201
1202 weston_buffer_reference(&gs->buffer_ref, NULL);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001203}
1204
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001205static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001206ensure_textures(struct gl_surface_state *gs, int num_textures)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001207{
1208 int i;
1209
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001210 if (num_textures <= gs->num_textures)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001211 return;
1212
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001213 for (i = gs->num_textures; i < num_textures; i++) {
1214 glGenTextures(1, &gs->textures[i]);
1215 glBindTexture(gs->target, gs->textures[i]);
1216 glTexParameteri(gs->target,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001217 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001218 glTexParameteri(gs->target,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001219 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1220 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001221 gs->num_textures = num_textures;
1222 glBindTexture(gs->target, 0);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001223}
1224
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001225static void
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001226gl_renderer_attach_shm(struct weston_surface *es, struct weston_buffer *buffer,
1227 struct wl_shm_buffer *shm_buffer)
1228{
1229 struct weston_compositor *ec = es->compositor;
1230 struct gl_renderer *gr = get_renderer(ec);
1231 struct gl_surface_state *gs = get_surface_state(es);
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001232 int pitch;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001233
1234 buffer->shm_buffer = shm_buffer;
1235 buffer->width = wl_shm_buffer_get_width(shm_buffer);
1236 buffer->height = wl_shm_buffer_get_height(shm_buffer);
1237
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001238 switch (wl_shm_buffer_get_format(shm_buffer)) {
1239 case WL_SHM_FORMAT_XRGB8888:
1240 gs->shader = &gr->texture_shader_rgbx;
1241 pitch = wl_shm_buffer_get_stride(shm_buffer) / 4;
1242 break;
1243 case WL_SHM_FORMAT_ARGB8888:
1244 gs->shader = &gr->texture_shader_rgba;
1245 pitch = wl_shm_buffer_get_stride(shm_buffer) / 4;
1246 break;
1247 case WL_SHM_FORMAT_RGB565:
1248 gs->shader = &gr->texture_shader_rgbx;
1249 pitch = wl_shm_buffer_get_stride(shm_buffer) / 2;
1250 break;
1251 default:
1252 weston_log("warning: unknown shm buffer format\n");
1253 gs->shader = &gr->texture_shader_rgba;
1254 pitch = wl_shm_buffer_get_stride(shm_buffer) / 4;
1255 }
1256
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001257 /* Only allocate a texture if it doesn't match existing one.
1258 * If a switch from DRM allocated buffer to a SHM buffer is
1259 * happening, we need to allocate a new texture buffer. */
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001260 if (pitch != gs->pitch ||
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001261 buffer->height != gs->height ||
1262 gs->buffer_type != BUFFER_TYPE_SHM) {
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001263 gs->pitch = pitch;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001264 gs->height = buffer->height;
1265 gs->target = GL_TEXTURE_2D;
1266 gs->buffer_type = BUFFER_TYPE_SHM;
1267 gs->needs_full_upload = 1;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001268 gs->y_inverted = 1;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001269
1270 ensure_textures(gs, 1);
1271 glBindTexture(GL_TEXTURE_2D, gs->textures[0]);
1272 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1273 gs->pitch, buffer->height, 0,
1274 GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
1275 }
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001276}
1277
1278static void
1279gl_renderer_attach_egl(struct weston_surface *es, struct weston_buffer *buffer,
1280 uint32_t format)
1281{
1282 struct weston_compositor *ec = es->compositor;
1283 struct gl_renderer *gr = get_renderer(ec);
1284 struct gl_surface_state *gs = get_surface_state(es);
1285 EGLint attribs[3];
1286 int i, num_planes;
1287
1288 buffer->legacy_buffer = (struct wl_buffer *)buffer->resource;
1289 gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1290 EGL_WIDTH, &buffer->width);
1291 gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1292 EGL_HEIGHT, &buffer->height);
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001293 gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1294 EGL_WAYLAND_Y_INVERTED_WL, &buffer->y_inverted);
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001295
1296 for (i = 0; i < gs->num_images; i++)
1297 gr->destroy_image(gr->egl_display, gs->images[i]);
1298 gs->num_images = 0;
1299 gs->target = GL_TEXTURE_2D;
1300 switch (format) {
1301 case EGL_TEXTURE_RGB:
1302 case EGL_TEXTURE_RGBA:
1303 default:
1304 num_planes = 1;
1305 gs->shader = &gr->texture_shader_rgba;
1306 break;
1307 case EGL_TEXTURE_EXTERNAL_WL:
1308 num_planes = 1;
1309 gs->target = GL_TEXTURE_EXTERNAL_OES;
1310 gs->shader = &gr->texture_shader_egl_external;
1311 break;
1312 case EGL_TEXTURE_Y_UV_WL:
1313 num_planes = 2;
1314 gs->shader = &gr->texture_shader_y_uv;
1315 break;
1316 case EGL_TEXTURE_Y_U_V_WL:
1317 num_planes = 3;
1318 gs->shader = &gr->texture_shader_y_u_v;
1319 break;
1320 case EGL_TEXTURE_Y_XUXV_WL:
1321 num_planes = 2;
1322 gs->shader = &gr->texture_shader_y_xuxv;
1323 break;
1324 }
1325
1326 ensure_textures(gs, num_planes);
1327 for (i = 0; i < num_planes; i++) {
1328 attribs[0] = EGL_WAYLAND_PLANE_WL;
1329 attribs[1] = i;
1330 attribs[2] = EGL_NONE;
1331 gs->images[i] = gr->create_image(gr->egl_display,
1332 NULL,
1333 EGL_WAYLAND_BUFFER_WL,
1334 buffer->legacy_buffer,
1335 attribs);
1336 if (!gs->images[i]) {
1337 weston_log("failed to create img for plane %d\n", i);
1338 continue;
1339 }
1340 gs->num_images++;
1341
1342 glActiveTexture(GL_TEXTURE0 + i);
1343 glBindTexture(gs->target, gs->textures[i]);
1344 gr->image_target_texture_2d(gs->target,
1345 gs->images[i]);
1346 }
1347
1348 gs->pitch = buffer->width;
1349 gs->height = buffer->height;
1350 gs->buffer_type = BUFFER_TYPE_EGL;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001351 gs->y_inverted = buffer->y_inverted;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001352}
1353
1354static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001355gl_renderer_attach(struct weston_surface *es, struct weston_buffer *buffer)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001356{
1357 struct weston_compositor *ec = es->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001358 struct gl_renderer *gr = get_renderer(ec);
1359 struct gl_surface_state *gs = get_surface_state(es);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001360 struct wl_shm_buffer *shm_buffer;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001361 EGLint format;
1362 int i;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001363
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001364 weston_buffer_reference(&gs->buffer_ref, buffer);
1365
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001366 if (!buffer) {
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001367 for (i = 0; i < gs->num_images; i++) {
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001368 gr->destroy_image(gr->egl_display, gs->images[i]);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001369 gs->images[i] = NULL;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001370 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001371 gs->num_images = 0;
1372 glDeleteTextures(gs->num_textures, gs->textures);
1373 gs->num_textures = 0;
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +03001374 gs->buffer_type = BUFFER_TYPE_NULL;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001375 gs->y_inverted = 1;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001376 return;
1377 }
1378
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001379 shm_buffer = wl_shm_buffer_get(buffer->resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001380
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001381 if (shm_buffer)
1382 gl_renderer_attach_shm(es, buffer, shm_buffer);
Kristian Høgsberg47229392013-08-07 11:59:54 -07001383 else if (gr->query_buffer(gr->egl_display, (void *) buffer->resource,
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001384 EGL_TEXTURE_FORMAT, &format))
1385 gl_renderer_attach_egl(es, buffer, format);
1386 else {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001387 weston_log("unhandled buffer type!\n");
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001388 weston_buffer_reference(&gs->buffer_ref, NULL);
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +03001389 gs->buffer_type = BUFFER_TYPE_NULL;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001390 gs->y_inverted = 1;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001391 }
1392}
1393
Kristian Høgsberg42263852012-09-06 21:59:29 -04001394static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001395gl_renderer_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001396 float red, float green, float blue, float alpha)
1397{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001398 struct gl_surface_state *gs = get_surface_state(surface);
1399 struct gl_renderer *gr = get_renderer(surface->compositor);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001400
1401 gs->color[0] = red;
1402 gs->color[1] = green;
1403 gs->color[2] = blue;
1404 gs->color[3] = alpha;
1405
John Kåre Alsaker40684142012-11-13 19:10:25 +01001406 gs->shader = &gr->solid_shader;
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001407}
1408
1409static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001410gl_renderer_create_surface(struct weston_surface *surface)
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001411{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001412 struct gl_surface_state *gs;
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001413
1414 gs = calloc(1, sizeof *gs);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001415 if (!gs)
1416 return -1;
1417
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001418 /* A buffer is never attached to solid color surfaces, yet
1419 * they still go through texcoord computations. Do not divide
1420 * by zero there.
1421 */
1422 gs->pitch = 1;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001423 gs->y_inverted = 1;
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001424
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001425 pixman_region32_init(&gs->texture_damage);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001426 surface->renderer_state = gs;
1427
1428 return 0;
1429}
1430
1431static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001432gl_renderer_destroy_surface(struct weston_surface *surface)
Kristian Høgsberg42263852012-09-06 21:59:29 -04001433{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001434 struct gl_surface_state *gs = get_surface_state(surface);
1435 struct gl_renderer *gr = get_renderer(surface->compositor);
Kristian Høgsberg42263852012-09-06 21:59:29 -04001436 int i;
1437
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001438 glDeleteTextures(gs->num_textures, gs->textures);
Kristian Høgsberg42263852012-09-06 21:59:29 -04001439
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001440 for (i = 0; i < gs->num_images; i++)
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001441 gr->destroy_image(gr->egl_display, gs->images[i]);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001442
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001443 weston_buffer_reference(&gs->buffer_ref, NULL);
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001444 pixman_region32_fini(&gs->texture_damage);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001445 free(gs);
Kristian Høgsberg42263852012-09-06 21:59:29 -04001446}
1447
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001448static const char vertex_shader[] =
1449 "uniform mat4 proj;\n"
1450 "attribute vec2 position;\n"
1451 "attribute vec2 texcoord;\n"
1452 "varying vec2 v_texcoord;\n"
1453 "void main()\n"
1454 "{\n"
1455 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
1456 " v_texcoord = texcoord;\n"
1457 "}\n";
1458
1459/* Declare common fragment shader uniforms */
1460#define FRAGMENT_CONVERT_YUV \
1461 " y *= alpha;\n" \
1462 " u *= alpha;\n" \
1463 " v *= alpha;\n" \
1464 " gl_FragColor.r = y + 1.59602678 * v;\n" \
1465 " gl_FragColor.g = y - 0.39176229 * u - 0.81296764 * v;\n" \
1466 " gl_FragColor.b = y + 2.01723214 * u;\n" \
1467 " gl_FragColor.a = alpha;\n"
1468
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001469static const char fragment_debug[] =
1470 " gl_FragColor = vec4(0.0, 0.3, 0.0, 0.2) + gl_FragColor * 0.8;\n";
1471
1472static const char fragment_brace[] =
1473 "}\n";
1474
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001475static const char texture_fragment_shader_rgba[] =
1476 "precision mediump float;\n"
1477 "varying vec2 v_texcoord;\n"
1478 "uniform sampler2D tex;\n"
1479 "uniform float alpha;\n"
1480 "void main()\n"
1481 "{\n"
1482 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001483 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001484
1485static const char texture_fragment_shader_rgbx[] =
1486 "precision mediump float;\n"
1487 "varying vec2 v_texcoord;\n"
1488 "uniform sampler2D tex;\n"
1489 "uniform float alpha;\n"
1490 "void main()\n"
1491 "{\n"
1492 " gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb\n;"
1493 " gl_FragColor.a = alpha;\n"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001494 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001495
1496static const char texture_fragment_shader_egl_external[] =
1497 "#extension GL_OES_EGL_image_external : require\n"
1498 "precision mediump float;\n"
1499 "varying vec2 v_texcoord;\n"
1500 "uniform samplerExternalOES tex;\n"
1501 "uniform float alpha;\n"
1502 "void main()\n"
1503 "{\n"
1504 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001505 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001506
1507static const char texture_fragment_shader_y_uv[] =
1508 "precision mediump float;\n"
1509 "uniform sampler2D tex;\n"
1510 "uniform sampler2D tex1;\n"
1511 "varying vec2 v_texcoord;\n"
1512 "uniform float alpha;\n"
1513 "void main() {\n"
1514 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1515 " float u = texture2D(tex1, v_texcoord).r - 0.5;\n"
1516 " float v = texture2D(tex1, v_texcoord).g - 0.5;\n"
1517 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001518 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001519
1520static const char texture_fragment_shader_y_u_v[] =
1521 "precision mediump float;\n"
1522 "uniform sampler2D tex;\n"
1523 "uniform sampler2D tex1;\n"
1524 "uniform sampler2D tex2;\n"
1525 "varying vec2 v_texcoord;\n"
1526 "uniform float alpha;\n"
1527 "void main() {\n"
1528 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1529 " float u = texture2D(tex1, v_texcoord).x - 0.5;\n"
1530 " float v = texture2D(tex2, v_texcoord).x - 0.5;\n"
1531 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001532 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001533
1534static const char texture_fragment_shader_y_xuxv[] =
1535 "precision mediump float;\n"
1536 "uniform sampler2D tex;\n"
1537 "uniform sampler2D tex1;\n"
1538 "varying vec2 v_texcoord;\n"
1539 "uniform float alpha;\n"
1540 "void main() {\n"
1541 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1542 " float u = texture2D(tex1, v_texcoord).g - 0.5;\n"
1543 " float v = texture2D(tex1, v_texcoord).a - 0.5;\n"
1544 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001545 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001546
1547static const char solid_fragment_shader[] =
1548 "precision mediump float;\n"
1549 "uniform vec4 color;\n"
1550 "uniform float alpha;\n"
1551 "void main()\n"
1552 "{\n"
1553 " gl_FragColor = alpha * color\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001554 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001555
1556static int
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001557compile_shader(GLenum type, int count, const char **sources)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001558{
1559 GLuint s;
1560 char msg[512];
1561 GLint status;
1562
1563 s = glCreateShader(type);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001564 glShaderSource(s, count, sources, NULL);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001565 glCompileShader(s);
1566 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
1567 if (!status) {
1568 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1569 weston_log("shader info: %s\n", msg);
1570 return GL_NONE;
1571 }
1572
1573 return s;
1574}
1575
1576static int
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03001577shader_init(struct gl_shader *shader, struct gl_renderer *renderer,
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001578 const char *vertex_source, const char *fragment_source)
1579{
1580 char msg[512];
1581 GLint status;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001582 int count;
1583 const char *sources[3];
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001584
1585 shader->vertex_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001586 compile_shader(GL_VERTEX_SHADER, 1, &vertex_source);
1587
1588 if (renderer->fragment_shader_debug) {
1589 sources[0] = fragment_source;
1590 sources[1] = fragment_debug;
1591 sources[2] = fragment_brace;
1592 count = 3;
1593 } else {
1594 sources[0] = fragment_source;
1595 sources[1] = fragment_brace;
1596 count = 2;
1597 }
1598
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001599 shader->fragment_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001600 compile_shader(GL_FRAGMENT_SHADER, count, sources);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001601
1602 shader->program = glCreateProgram();
1603 glAttachShader(shader->program, shader->vertex_shader);
1604 glAttachShader(shader->program, shader->fragment_shader);
1605 glBindAttribLocation(shader->program, 0, "position");
1606 glBindAttribLocation(shader->program, 1, "texcoord");
1607
1608 glLinkProgram(shader->program);
1609 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1610 if (!status) {
1611 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1612 weston_log("link info: %s\n", msg);
1613 return -1;
1614 }
1615
1616 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1617 shader->tex_uniforms[0] = glGetUniformLocation(shader->program, "tex");
1618 shader->tex_uniforms[1] = glGetUniformLocation(shader->program, "tex1");
1619 shader->tex_uniforms[2] = glGetUniformLocation(shader->program, "tex2");
1620 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
1621 shader->color_uniform = glGetUniformLocation(shader->program, "color");
1622
1623 return 0;
1624}
1625
1626static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001627shader_release(struct gl_shader *shader)
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001628{
1629 glDeleteShader(shader->vertex_shader);
1630 glDeleteShader(shader->fragment_shader);
1631 glDeleteProgram(shader->program);
1632
1633 shader->vertex_shader = 0;
1634 shader->fragment_shader = 0;
1635 shader->program = 0;
1636}
1637
1638static void
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001639log_extensions(const char *name, const char *extensions)
1640{
1641 const char *p, *end;
1642 int l;
1643 int len;
1644
1645 l = weston_log("%s:", name);
1646 p = extensions;
1647 while (*p) {
1648 end = strchrnul(p, ' ');
1649 len = end - p;
1650 if (l + len > 78)
1651 l = weston_log_continue("\n" STAMP_SPACE "%.*s",
1652 len, p);
1653 else
1654 l += weston_log_continue(" %.*s", len, p);
1655 for (p = end; isspace(*p); p++)
1656 ;
1657 }
1658 weston_log_continue("\n");
1659}
1660
1661static void
1662log_egl_gl_info(EGLDisplay egldpy)
1663{
1664 const char *str;
1665
1666 str = eglQueryString(egldpy, EGL_VERSION);
1667 weston_log("EGL version: %s\n", str ? str : "(null)");
1668
1669 str = eglQueryString(egldpy, EGL_VENDOR);
1670 weston_log("EGL vendor: %s\n", str ? str : "(null)");
1671
1672 str = eglQueryString(egldpy, EGL_CLIENT_APIS);
1673 weston_log("EGL client APIs: %s\n", str ? str : "(null)");
1674
1675 str = eglQueryString(egldpy, EGL_EXTENSIONS);
1676 log_extensions("EGL extensions", str ? str : "(null)");
1677
1678 str = (char *)glGetString(GL_VERSION);
1679 weston_log("GL version: %s\n", str ? str : "(null)");
1680
1681 str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
1682 weston_log("GLSL version: %s\n", str ? str : "(null)");
1683
1684 str = (char *)glGetString(GL_VENDOR);
1685 weston_log("GL vendor: %s\n", str ? str : "(null)");
1686
1687 str = (char *)glGetString(GL_RENDERER);
1688 weston_log("GL renderer: %s\n", str ? str : "(null)");
1689
1690 str = (char *)glGetString(GL_EXTENSIONS);
1691 log_extensions("GL extensions", str ? str : "(null)");
1692}
1693
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001694static void
1695log_egl_config_info(EGLDisplay egldpy, EGLConfig eglconfig)
1696{
1697 EGLint r, g, b, a;
1698
1699 weston_log("Chosen EGL config details:\n");
1700
1701 weston_log_continue(STAMP_SPACE "RGBA bits");
1702 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_RED_SIZE, &r) &&
1703 eglGetConfigAttrib(egldpy, eglconfig, EGL_GREEN_SIZE, &g) &&
1704 eglGetConfigAttrib(egldpy, eglconfig, EGL_BLUE_SIZE, &b) &&
1705 eglGetConfigAttrib(egldpy, eglconfig, EGL_ALPHA_SIZE, &a))
1706 weston_log_continue(": %d %d %d %d\n", r, g, b, a);
1707 else
1708 weston_log_continue(" unknown\n");
1709
1710 weston_log_continue(STAMP_SPACE "swap interval range");
1711 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_MIN_SWAP_INTERVAL, &a) &&
1712 eglGetConfigAttrib(egldpy, eglconfig, EGL_MAX_SWAP_INTERVAL, &b))
1713 weston_log_continue(": %d - %d\n", a, b);
1714 else
1715 weston_log_continue(" unknown\n");
1716}
1717
John Kåre Alsaker44154502012-11-13 19:10:20 +01001718static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001719output_apply_border(struct weston_output *output, struct gl_renderer *gr)
John Kåre Alsaker44154502012-11-13 19:10:20 +01001720{
1721 output->border.top = gr->border.top;
1722 output->border.bottom = gr->border.bottom;
1723 output->border.left = gr->border.left;
1724 output->border.right = gr->border.right;
1725}
1726
1727WL_EXPORT void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001728gl_renderer_set_border(struct weston_compositor *ec, int32_t width, int32_t height, void *data,
John Kåre Alsaker44154502012-11-13 19:10:20 +01001729 int32_t *edges)
1730{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001731 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker44154502012-11-13 19:10:20 +01001732 struct weston_output *output;
1733
1734 gr->border.left = edges[0];
1735 gr->border.right = edges[1];
1736 gr->border.top = edges[2];
1737 gr->border.bottom = edges[3];
1738
1739 gr->border.width = width;
1740 gr->border.height = height;
1741
1742 glGenTextures(1, &gr->border.texture);
1743 glBindTexture(GL_TEXTURE_2D, gr->border.texture);
1744 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1745 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1746 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1747 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1748
1749 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1750 width,
1751 height,
1752 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1753 data);
1754
1755 wl_list_for_each(output, &ec->output_list, link)
1756 output_apply_border(output, gr);
1757}
1758
John Kåre Alsaker94659272012-11-13 19:10:18 +01001759static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001760gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001761
1762WL_EXPORT int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001763gl_renderer_output_create(struct weston_output *output,
John Kåre Alsaker94659272012-11-13 19:10:18 +01001764 EGLNativeWindowType window)
1765{
1766 struct weston_compositor *ec = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001767 struct gl_renderer *gr = get_renderer(ec);
1768 struct gl_output_state *go = calloc(1, sizeof *go);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001769 int i;
John Kåre Alsaker94659272012-11-13 19:10:18 +01001770
1771 if (!go)
1772 return -1;
1773
1774 go->egl_surface =
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001775 eglCreateWindowSurface(gr->egl_display,
1776 gr->egl_config,
John Kåre Alsaker94659272012-11-13 19:10:18 +01001777 window, NULL);
1778
1779 if (go->egl_surface == EGL_NO_SURFACE) {
1780 weston_log("failed to create egl surface\n");
1781 free(go);
1782 return -1;
1783 }
1784
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001785 if (gr->egl_context == NULL)
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001786 if (gl_renderer_setup(ec, go->egl_surface) < 0) {
John Kåre Alsaker94659272012-11-13 19:10:18 +01001787 free(go);
1788 return -1;
1789 }
1790
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001791 for (i = 0; i < BUFFER_DAMAGE_COUNT; i++)
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001792 pixman_region32_init(&go->buffer_damage[i]);
1793
John Kåre Alsaker94659272012-11-13 19:10:18 +01001794 output->renderer_state = go;
1795
John Kåre Alsaker44154502012-11-13 19:10:20 +01001796 output_apply_border(output, gr);
1797
John Kåre Alsaker94659272012-11-13 19:10:18 +01001798 return 0;
1799}
1800
1801WL_EXPORT void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001802gl_renderer_output_destroy(struct weston_output *output)
John Kåre Alsaker94659272012-11-13 19:10:18 +01001803{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001804 struct gl_renderer *gr = get_renderer(output->compositor);
1805 struct gl_output_state *go = get_output_state(output);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001806 int i;
1807
1808 for (i = 0; i < 2; i++)
1809 pixman_region32_fini(&go->buffer_damage[i]);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001810
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001811 eglDestroySurface(gr->egl_display, go->egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001812
1813 free(go);
1814}
1815
1816WL_EXPORT EGLSurface
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001817gl_renderer_output_surface(struct weston_output *output)
John Kåre Alsaker94659272012-11-13 19:10:18 +01001818{
1819 return get_output_state(output)->egl_surface;
1820}
1821
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03001822static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001823gl_renderer_destroy(struct weston_compositor *ec)
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001824{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001825 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001826
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001827 if (gr->has_bind_display)
1828 gr->unbind_display(gr->egl_display, ec->wl_display);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001829
1830 /* Work around crash in egl_dri2.c's dri2_make_current() - when does this apply? */
1831 eglMakeCurrent(gr->egl_display,
1832 EGL_NO_SURFACE, EGL_NO_SURFACE,
1833 EGL_NO_CONTEXT);
1834
1835 eglTerminate(gr->egl_display);
1836 eglReleaseThread();
Scott Moreau976a0502013-03-07 10:15:17 -07001837
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -04001838 wl_array_release(&gr->vertices);
1839 wl_array_release(&gr->indices);
1840 wl_array_release(&gr->vtxcnt);
1841
Scott Moreau976a0502013-03-07 10:15:17 -07001842 free(gr);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001843}
1844
1845static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001846egl_choose_config(struct gl_renderer *gr, const EGLint *attribs,
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001847 const EGLint *visual_id)
1848{
1849 EGLint count = 0;
1850 EGLint matched = 0;
1851 EGLConfig *configs;
1852 int i;
1853
1854 if (!eglGetConfigs(gr->egl_display, NULL, 0, &count) || count < 1)
1855 return -1;
1856
1857 configs = calloc(count, sizeof *configs);
1858 if (!configs)
1859 return -1;
1860
1861 if (!eglChooseConfig(gr->egl_display, attribs, configs,
1862 count, &matched))
1863 goto out;
1864
1865 for (i = 0; i < matched; ++i) {
1866 EGLint id;
1867
1868 if (visual_id) {
1869 if (!eglGetConfigAttrib(gr->egl_display,
1870 configs[i], EGL_NATIVE_VISUAL_ID,
1871 &id))
1872 continue;
1873
1874 if (id != *visual_id)
1875 continue;
1876 }
1877
1878 gr->egl_config = configs[i];
1879
1880 free(configs);
1881 return 0;
1882 }
1883
1884out:
1885 free(configs);
1886 return -1;
1887}
1888
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001889WL_EXPORT const EGLint gl_renderer_opaque_attribs[] = {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001890 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1891 EGL_RED_SIZE, 1,
1892 EGL_GREEN_SIZE, 1,
1893 EGL_BLUE_SIZE, 1,
1894 EGL_ALPHA_SIZE, 0,
1895 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1896 EGL_NONE
1897};
1898
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001899WL_EXPORT const EGLint gl_renderer_alpha_attribs[] = {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001900 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1901 EGL_RED_SIZE, 1,
1902 EGL_GREEN_SIZE, 1,
1903 EGL_BLUE_SIZE, 1,
1904 EGL_ALPHA_SIZE, 1,
1905 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1906 EGL_NONE
1907};
1908
1909WL_EXPORT int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001910gl_renderer_create(struct weston_compositor *ec, EGLNativeDisplayType display,
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001911 const EGLint *attribs, const EGLint *visual_id)
1912{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001913 struct gl_renderer *gr;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001914 EGLint major, minor;
1915
1916 gr = calloc(1, sizeof *gr);
1917
1918 if (gr == NULL)
1919 return -1;
1920
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001921 gr->base.read_pixels = gl_renderer_read_pixels;
1922 gr->base.repaint_output = gl_renderer_repaint_output;
1923 gr->base.flush_damage = gl_renderer_flush_damage;
1924 gr->base.attach = gl_renderer_attach;
1925 gr->base.create_surface = gl_renderer_create_surface;
1926 gr->base.surface_set_color = gl_renderer_surface_set_color;
1927 gr->base.destroy_surface = gl_renderer_destroy_surface;
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03001928 gr->base.destroy = gl_renderer_destroy;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001929
1930 gr->egl_display = eglGetDisplay(display);
1931 if (gr->egl_display == EGL_NO_DISPLAY) {
1932 weston_log("failed to create display\n");
1933 goto err_egl;
1934 }
1935
1936 if (!eglInitialize(gr->egl_display, &major, &minor)) {
1937 weston_log("failed to initialize display\n");
1938 goto err_egl;
1939 }
1940
1941 if (egl_choose_config(gr, attribs, visual_id) < 0) {
1942 weston_log("failed to choose EGL config\n");
1943 goto err_egl;
1944 }
1945
1946 ec->renderer = &gr->base;
Pekka Paalanen7bb65102013-05-22 18:03:04 +03001947 ec->capabilities |= WESTON_CAP_ROTATION_ANY;
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03001948 ec->capabilities |= WESTON_CAP_CAPTURE_YFLIP;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001949
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001950 wl_display_add_shm_format(ec->wl_display, WL_SHM_FORMAT_RGB565);
1951
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001952 return 0;
1953
1954err_egl:
Pekka Paalanen326529f2012-11-27 12:25:25 +02001955 gl_renderer_print_egl_error_state();
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001956 free(gr);
1957 return -1;
1958}
1959
1960WL_EXPORT EGLDisplay
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001961gl_renderer_display(struct weston_compositor *ec)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001962{
1963 return get_renderer(ec)->egl_display;
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001964}
1965
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001966static int
1967compile_shaders(struct weston_compositor *ec)
1968{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001969 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker40684142012-11-13 19:10:25 +01001970
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03001971 gr->texture_shader_rgba.vertex_source = vertex_shader;
1972 gr->texture_shader_rgba.fragment_source = texture_fragment_shader_rgba;
1973
1974 gr->texture_shader_rgbx.vertex_source = vertex_shader;
1975 gr->texture_shader_rgbx.fragment_source = texture_fragment_shader_rgbx;
1976
1977 gr->texture_shader_egl_external.vertex_source = vertex_shader;
1978 gr->texture_shader_egl_external.fragment_source =
1979 texture_fragment_shader_egl_external;
1980
1981 gr->texture_shader_y_uv.vertex_source = vertex_shader;
1982 gr->texture_shader_y_uv.fragment_source = texture_fragment_shader_y_uv;
1983
1984 gr->texture_shader_y_u_v.vertex_source = vertex_shader;
1985 gr->texture_shader_y_u_v.fragment_source =
1986 texture_fragment_shader_y_u_v;
1987
1988 gr->texture_shader_y_u_v.vertex_source = vertex_shader;
1989 gr->texture_shader_y_xuxv.fragment_source =
1990 texture_fragment_shader_y_xuxv;
1991
1992 gr->solid_shader.vertex_source = vertex_shader;
1993 gr->solid_shader.fragment_source = solid_fragment_shader;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001994
1995 return 0;
1996}
1997
1998static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04001999fragment_debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002000 void *data)
2001{
2002 struct weston_compositor *ec = data;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002003 struct gl_renderer *gr = get_renderer(ec);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002004 struct weston_output *output;
2005
John Kåre Alsaker40684142012-11-13 19:10:25 +01002006 gr->fragment_shader_debug ^= 1;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002007
John Kåre Alsaker40684142012-11-13 19:10:25 +01002008 shader_release(&gr->texture_shader_rgba);
2009 shader_release(&gr->texture_shader_rgbx);
2010 shader_release(&gr->texture_shader_egl_external);
2011 shader_release(&gr->texture_shader_y_uv);
2012 shader_release(&gr->texture_shader_y_u_v);
2013 shader_release(&gr->texture_shader_y_xuxv);
2014 shader_release(&gr->solid_shader);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002015
Ander Conselvan de Oliveira03fb4ef2012-12-03 17:08:11 +02002016 /* Force use_shader() to call glUseProgram(), since we need to use
2017 * the recompiled version of the shader. */
2018 gr->current_shader = NULL;
2019
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002020 wl_list_for_each(output, &ec->output_list, link)
2021 weston_output_damage(output);
2022}
2023
Kristian Høgsberg8799d412013-05-07 10:50:09 -04002024static void
2025fan_debug_repaint_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
2026 void *data)
2027{
2028 struct weston_compositor *compositor = data;
2029 struct gl_renderer *gr = get_renderer(compositor);
2030
2031 gr->fan_debug = !gr->fan_debug;
2032 weston_compositor_damage_all(compositor);
2033}
2034
John Kåre Alsaker94659272012-11-13 19:10:18 +01002035static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002036gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002037{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002038 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002039 const char *extensions;
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04002040 EGLBoolean ret;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002041
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04002042 static const EGLint context_attribs[] = {
2043 EGL_CONTEXT_CLIENT_VERSION, 2,
2044 EGL_NONE
2045 };
2046
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04002047 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
2048 weston_log("failed to bind EGL_OPENGL_ES_API\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02002049 gl_renderer_print_egl_error_state();
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04002050 return -1;
2051 }
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03002052
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002053 log_egl_config_info(gr->egl_display, gr->egl_config);
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03002054
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002055 gr->egl_context = eglCreateContext(gr->egl_display, gr->egl_config,
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04002056 EGL_NO_CONTEXT, context_attribs);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002057 if (gr->egl_context == NULL) {
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04002058 weston_log("failed to create context\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02002059 gl_renderer_print_egl_error_state();
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04002060 return -1;
2061 }
2062
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002063 ret = eglMakeCurrent(gr->egl_display, egl_surface,
2064 egl_surface, gr->egl_context);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04002065 if (ret == EGL_FALSE) {
2066 weston_log("Failed to make EGL context current.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02002067 gl_renderer_print_egl_error_state();
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04002068 return -1;
2069 }
2070
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002071 log_egl_gl_info(gr->egl_display);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002072
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002073 gr->image_target_texture_2d =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002074 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002075 gr->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2076 gr->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2077 gr->bind_display =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002078 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002079 gr->unbind_display =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002080 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002081 gr->query_buffer =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002082 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
2083
2084 extensions = (const char *) glGetString(GL_EXTENSIONS);
2085 if (!extensions) {
2086 weston_log("Retrieving GL extension string failed.\n");
2087 return -1;
2088 }
2089
2090 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
2091 weston_log("GL_EXT_texture_format_BGRA8888 not available\n");
2092 return -1;
2093 }
2094
2095 if (strstr(extensions, "GL_EXT_read_format_bgra"))
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01002096 ec->read_format = PIXMAN_a8r8g8b8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002097 else
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01002098 ec->read_format = PIXMAN_a8b8g8r8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002099
Kristian Høgsberg1c4f1632013-08-07 12:11:27 -07002100#ifdef GL_EXT_unpack_subimage
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002101 if (strstr(extensions, "GL_EXT_unpack_subimage"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002102 gr->has_unpack_subimage = 1;
Kristian Høgsberg1c4f1632013-08-07 12:11:27 -07002103#endif
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002104
2105 if (strstr(extensions, "GL_OES_EGL_image_external"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002106 gr->has_egl_image_external = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002107
2108 extensions =
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002109 (const char *) eglQueryString(gr->egl_display, EGL_EXTENSIONS);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002110 if (!extensions) {
2111 weston_log("Retrieving EGL extension string failed.\n");
2112 return -1;
2113 }
2114
2115 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002116 gr->has_bind_display = 1;
2117 if (gr->has_bind_display) {
2118 ret = gr->bind_display(gr->egl_display, ec->wl_display);
Pekka Paalanen035a0322012-10-24 09:43:06 +03002119 if (!ret)
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002120 gr->has_bind_display = 0;
Pekka Paalanen035a0322012-10-24 09:43:06 +03002121 }
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002122
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02002123 if (strstr(extensions, "EGL_EXT_buffer_age"))
2124 gr->has_egl_buffer_age = 1;
2125 else
2126 weston_log("warning: EGL_EXT_buffer_age not supported. "
2127 "Performance could be affected.\n");
2128
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002129 glActiveTexture(GL_TEXTURE0);
2130
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002131 if (compile_shaders(ec))
2132 return -1;
2133
2134 weston_compositor_add_debug_binding(ec, KEY_S,
2135 fragment_debug_binding, ec);
Kristian Høgsberg8799d412013-05-07 10:50:09 -04002136 weston_compositor_add_debug_binding(ec, KEY_F,
2137 fan_debug_repaint_binding, ec);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002138
Pekka Paalanen035a0322012-10-24 09:43:06 +03002139 weston_log("GL ES 2 renderer features:\n");
2140 weston_log_continue(STAMP_SPACE "read-back format: %s\n",
Pekka Paalanenfe4eacf2013-01-10 16:50:42 +02002141 ec->read_format == PIXMAN_a8r8g8b8 ? "BGRA" : "RGBA");
Pekka Paalanen035a0322012-10-24 09:43:06 +03002142 weston_log_continue(STAMP_SPACE "wl_shm sub-image to texture: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002143 gr->has_unpack_subimage ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03002144 weston_log_continue(STAMP_SPACE "EGL Wayland extension: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002145 gr->has_bind_display ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03002146
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002147
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002148 return 0;
2149}