blob: 66533245fd1b5f0ab60c52ba36dd60383d197050 [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 */
John Kåre Alsaker878f4492012-11-13 19:10:23 +010080};
81
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010082struct gl_renderer {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +010083 struct weston_renderer base;
84 int fragment_shader_debug;
Kristian Høgsberg8799d412013-05-07 10:50:09 -040085 int fan_debug;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +010086
87 EGLDisplay egl_display;
88 EGLContext egl_context;
89 EGLConfig egl_config;
John Kåre Alsaker44154502012-11-13 19:10:20 +010090
91 struct {
92 int32_t top, bottom, left, right;
93 GLuint texture;
94 int32_t width, height;
95 } border;
John Kåre Alsaker40684142012-11-13 19:10:25 +010096
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -040097 struct wl_array vertices;
98 struct wl_array indices; /* only used in compositor-wayland */
99 struct wl_array vtxcnt;
100
John Kåre Alsaker320711d2012-11-13 19:10:27 +0100101 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
102 PFNEGLCREATEIMAGEKHRPROC create_image;
103 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
104
105 int has_unpack_subimage;
106
107 PFNEGLBINDWAYLANDDISPLAYWL bind_display;
108 PFNEGLUNBINDWAYLANDDISPLAYWL unbind_display;
109 PFNEGLQUERYWAYLANDBUFFERWL query_buffer;
110 int has_bind_display;
111
112 int has_egl_image_external;
113
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +0200114 int has_egl_buffer_age;
115
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100116 struct gl_shader texture_shader_rgba;
117 struct gl_shader texture_shader_rgbx;
118 struct gl_shader texture_shader_egl_external;
119 struct gl_shader texture_shader_y_uv;
120 struct gl_shader texture_shader_y_u_v;
121 struct gl_shader texture_shader_y_xuxv;
122 struct gl_shader invert_color_shader;
123 struct gl_shader solid_shader;
124 struct gl_shader *current_shader;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100125};
John Kåre Alsaker94659272012-11-13 19:10:18 +0100126
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100127static inline struct gl_output_state *
John Kåre Alsaker94659272012-11-13 19:10:18 +0100128get_output_state(struct weston_output *output)
129{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100130 return (struct gl_output_state *)output->renderer_state;
John Kåre Alsaker94659272012-11-13 19:10:18 +0100131}
132
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100133static inline struct gl_surface_state *
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100134get_surface_state(struct weston_surface *surface)
135{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100136 return (struct gl_surface_state *)surface->renderer_state;
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100137}
138
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100139static inline struct gl_renderer *
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100140get_renderer(struct weston_compositor *ec)
141{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100142 return (struct gl_renderer *)ec->renderer;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100143}
144
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400145static const char *
146egl_error_string(EGLint code)
147{
148#define MYERRCODE(x) case x: return #x;
149 switch (code) {
150 MYERRCODE(EGL_SUCCESS)
151 MYERRCODE(EGL_NOT_INITIALIZED)
152 MYERRCODE(EGL_BAD_ACCESS)
153 MYERRCODE(EGL_BAD_ALLOC)
154 MYERRCODE(EGL_BAD_ATTRIBUTE)
155 MYERRCODE(EGL_BAD_CONTEXT)
156 MYERRCODE(EGL_BAD_CONFIG)
157 MYERRCODE(EGL_BAD_CURRENT_SURFACE)
158 MYERRCODE(EGL_BAD_DISPLAY)
159 MYERRCODE(EGL_BAD_SURFACE)
160 MYERRCODE(EGL_BAD_MATCH)
161 MYERRCODE(EGL_BAD_PARAMETER)
162 MYERRCODE(EGL_BAD_NATIVE_PIXMAP)
163 MYERRCODE(EGL_BAD_NATIVE_WINDOW)
164 MYERRCODE(EGL_CONTEXT_LOST)
165 default:
166 return "unknown";
167 }
168#undef MYERRCODE
169}
170
Pekka Paalanen326529f2012-11-27 12:25:25 +0200171WL_EXPORT void
172gl_renderer_print_egl_error_state(void)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400173{
174 EGLint code;
175
176 code = eglGetError();
177 weston_log("EGL error state: %s (0x%04lx)\n",
178 egl_error_string(code), (long)code);
179}
180
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300181struct polygon8 {
182 GLfloat x[8];
183 GLfloat y[8];
184 int n;
185};
186
187struct clip_context {
188 struct {
189 GLfloat x;
190 GLfloat y;
191 } prev;
192
193 struct {
194 GLfloat x1, y1;
195 GLfloat x2, y2;
196 } clip;
197
198 struct {
199 GLfloat *x;
200 GLfloat *y;
201 } vertices;
202};
203
204static GLfloat
205float_difference(GLfloat a, GLfloat b)
206{
207 /* http://www.altdevblogaday.com/2012/02/22/comparing-floating-point-numbers-2012-edition/ */
208 static const GLfloat max_diff = 4.0f * FLT_MIN;
209 static const GLfloat max_rel_diff = 4.0e-5;
210 GLfloat diff = a - b;
211 GLfloat adiff = fabsf(diff);
212
213 if (adiff <= max_diff)
214 return 0.0f;
215
216 a = fabsf(a);
217 b = fabsf(b);
218 if (adiff <= (a > b ? a : b) * max_rel_diff)
219 return 0.0f;
220
221 return diff;
222}
223
224/* A line segment (p1x, p1y)-(p2x, p2y) intersects the line x = x_arg.
225 * Compute the y coordinate of the intersection.
226 */
227static GLfloat
228clip_intersect_y(GLfloat p1x, GLfloat p1y, GLfloat p2x, GLfloat p2y,
229 GLfloat x_arg)
230{
231 GLfloat a;
232 GLfloat diff = float_difference(p1x, p2x);
233
234 /* Practically vertical line segment, yet the end points have already
235 * been determined to be on different sides of the line. Therefore
236 * the line segment is part of the line and intersects everywhere.
237 * Return the end point, so we use the whole line segment.
238 */
239 if (diff == 0.0f)
240 return p2y;
241
242 a = (x_arg - p2x) / diff;
243 return p2y + (p1y - p2y) * a;
244}
245
246/* A line segment (p1x, p1y)-(p2x, p2y) intersects the line y = y_arg.
247 * Compute the x coordinate of the intersection.
248 */
249static GLfloat
250clip_intersect_x(GLfloat p1x, GLfloat p1y, GLfloat p2x, GLfloat p2y,
251 GLfloat y_arg)
252{
253 GLfloat a;
254 GLfloat diff = float_difference(p1y, p2y);
255
256 /* Practically horizontal line segment, yet the end points have already
257 * been determined to be on different sides of the line. Therefore
258 * the line segment is part of the line and intersects everywhere.
259 * Return the end point, so we use the whole line segment.
260 */
261 if (diff == 0.0f)
262 return p2x;
263
264 a = (y_arg - p2y) / diff;
265 return p2x + (p1x - p2x) * a;
266}
267
268enum path_transition {
269 PATH_TRANSITION_OUT_TO_OUT = 0,
270 PATH_TRANSITION_OUT_TO_IN = 1,
271 PATH_TRANSITION_IN_TO_OUT = 2,
272 PATH_TRANSITION_IN_TO_IN = 3,
273};
274
275static void
276clip_append_vertex(struct clip_context *ctx, GLfloat x, GLfloat y)
277{
278 *ctx->vertices.x++ = x;
279 *ctx->vertices.y++ = y;
280}
281
282static enum path_transition
283path_transition_left_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
284{
285 return ((ctx->prev.x >= ctx->clip.x1) << 1) | (x >= ctx->clip.x1);
286}
287
288static enum path_transition
289path_transition_right_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
290{
291 return ((ctx->prev.x < ctx->clip.x2) << 1) | (x < ctx->clip.x2);
292}
293
294static enum path_transition
295path_transition_top_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
296{
297 return ((ctx->prev.y >= ctx->clip.y1) << 1) | (y >= ctx->clip.y1);
298}
299
300static enum path_transition
301path_transition_bottom_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
302{
303 return ((ctx->prev.y < ctx->clip.y2) << 1) | (y < ctx->clip.y2);
304}
305
306static void
307clip_polygon_leftright(struct clip_context *ctx,
308 enum path_transition transition,
309 GLfloat x, GLfloat y, GLfloat clip_x)
310{
311 GLfloat yi;
312
313 switch (transition) {
314 case PATH_TRANSITION_IN_TO_IN:
315 clip_append_vertex(ctx, x, y);
316 break;
317 case PATH_TRANSITION_IN_TO_OUT:
318 yi = clip_intersect_y(ctx->prev.x, ctx->prev.y, x, y, clip_x);
319 clip_append_vertex(ctx, clip_x, yi);
320 break;
321 case PATH_TRANSITION_OUT_TO_IN:
322 yi = clip_intersect_y(ctx->prev.x, ctx->prev.y, x, y, clip_x);
323 clip_append_vertex(ctx, clip_x, yi);
324 clip_append_vertex(ctx, x, y);
325 break;
326 case PATH_TRANSITION_OUT_TO_OUT:
327 /* nothing */
328 break;
329 default:
330 assert(0 && "bad enum path_transition");
331 }
332
333 ctx->prev.x = x;
334 ctx->prev.y = y;
335}
336
337static void
338clip_polygon_topbottom(struct clip_context *ctx,
339 enum path_transition transition,
340 GLfloat x, GLfloat y, GLfloat clip_y)
341{
342 GLfloat xi;
343
344 switch (transition) {
345 case PATH_TRANSITION_IN_TO_IN:
346 clip_append_vertex(ctx, x, y);
347 break;
348 case PATH_TRANSITION_IN_TO_OUT:
349 xi = clip_intersect_x(ctx->prev.x, ctx->prev.y, x, y, clip_y);
350 clip_append_vertex(ctx, xi, clip_y);
351 break;
352 case PATH_TRANSITION_OUT_TO_IN:
353 xi = clip_intersect_x(ctx->prev.x, ctx->prev.y, x, y, clip_y);
354 clip_append_vertex(ctx, xi, clip_y);
355 clip_append_vertex(ctx, x, y);
356 break;
357 case PATH_TRANSITION_OUT_TO_OUT:
358 /* nothing */
359 break;
360 default:
361 assert(0 && "bad enum path_transition");
362 }
363
364 ctx->prev.x = x;
365 ctx->prev.y = y;
366}
367
368static void
369clip_context_prepare(struct clip_context *ctx, const struct polygon8 *src,
370 GLfloat *dst_x, GLfloat *dst_y)
371{
372 ctx->prev.x = src->x[src->n - 1];
373 ctx->prev.y = src->y[src->n - 1];
374 ctx->vertices.x = dst_x;
375 ctx->vertices.y = dst_y;
376}
377
378static int
379clip_polygon_left(struct clip_context *ctx, const struct polygon8 *src,
380 GLfloat *dst_x, GLfloat *dst_y)
381{
382 enum path_transition trans;
383 int i;
384
385 clip_context_prepare(ctx, src, dst_x, dst_y);
386 for (i = 0; i < src->n; i++) {
387 trans = path_transition_left_edge(ctx, src->x[i], src->y[i]);
388 clip_polygon_leftright(ctx, trans, src->x[i], src->y[i],
389 ctx->clip.x1);
390 }
391 return ctx->vertices.x - dst_x;
392}
393
394static int
395clip_polygon_right(struct clip_context *ctx, const struct polygon8 *src,
396 GLfloat *dst_x, GLfloat *dst_y)
397{
398 enum path_transition trans;
399 int i;
400
401 clip_context_prepare(ctx, src, dst_x, dst_y);
402 for (i = 0; i < src->n; i++) {
403 trans = path_transition_right_edge(ctx, src->x[i], src->y[i]);
404 clip_polygon_leftright(ctx, trans, src->x[i], src->y[i],
405 ctx->clip.x2);
406 }
407 return ctx->vertices.x - dst_x;
408}
409
410static int
411clip_polygon_top(struct clip_context *ctx, const struct polygon8 *src,
412 GLfloat *dst_x, GLfloat *dst_y)
413{
414 enum path_transition trans;
415 int i;
416
417 clip_context_prepare(ctx, src, dst_x, dst_y);
418 for (i = 0; i < src->n; i++) {
419 trans = path_transition_top_edge(ctx, src->x[i], src->y[i]);
420 clip_polygon_topbottom(ctx, trans, src->x[i], src->y[i],
421 ctx->clip.y1);
422 }
423 return ctx->vertices.x - dst_x;
424}
425
426static int
427clip_polygon_bottom(struct clip_context *ctx, const struct polygon8 *src,
428 GLfloat *dst_x, GLfloat *dst_y)
429{
430 enum path_transition trans;
431 int i;
432
433 clip_context_prepare(ctx, src, dst_x, dst_y);
434 for (i = 0; i < src->n; i++) {
435 trans = path_transition_bottom_edge(ctx, src->x[i], src->y[i]);
436 clip_polygon_topbottom(ctx, trans, src->x[i], src->y[i],
437 ctx->clip.y2);
438 }
439 return ctx->vertices.x - dst_x;
440}
441
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400442#define max(a, b) (((a) > (b)) ? (a) : (b))
443#define min(a, b) (((a) > (b)) ? (b) : (a))
444#define clip(x, a, b) min(max(x, a), b)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400445
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300446/*
447 * Compute the boundary vertices of the intersection of the global coordinate
448 * aligned rectangle 'rect', and an arbitrary quadrilateral produced from
449 * 'surf_rect' when transformed from surface coordinates into global coordinates.
450 * The vertices are written to 'ex' and 'ey', and the return value is the
451 * number of vertices. Vertices are produced in clockwise winding order.
452 * Guarantees to produce either zero vertices, or 3-8 vertices with non-zero
453 * polygon area.
454 */
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400455static int
456calculate_edges(struct weston_surface *es, pixman_box32_t *rect,
457 pixman_box32_t *surf_rect, GLfloat *ex, GLfloat *ey)
458{
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300459 struct polygon8 polygon;
460 struct clip_context ctx;
461 int i, n;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400462 GLfloat min_x, max_x, min_y, max_y;
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300463 struct polygon8 surf = {
464 { surf_rect->x1, surf_rect->x2, surf_rect->x2, surf_rect->x1 },
465 { surf_rect->y1, surf_rect->y1, surf_rect->y2, surf_rect->y2 },
466 4
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400467 };
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400468
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300469 ctx.clip.x1 = rect->x1;
470 ctx.clip.y1 = rect->y1;
471 ctx.clip.x2 = rect->x2;
472 ctx.clip.y2 = rect->y2;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400473
474 /* transform surface to screen space: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300475 for (i = 0; i < surf.n; i++)
476 weston_surface_to_global_float(es, surf.x[i], surf.y[i],
477 &surf.x[i], &surf.y[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400478
479 /* find bounding box: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300480 min_x = max_x = surf.x[0];
481 min_y = max_y = surf.y[0];
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400482
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300483 for (i = 1; i < surf.n; i++) {
484 min_x = min(min_x, surf.x[i]);
485 max_x = max(max_x, surf.x[i]);
486 min_y = min(min_y, surf.y[i]);
487 max_y = max(max_y, surf.y[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400488 }
489
490 /* First, simple bounding box check to discard early transformed
491 * surface rects that do not intersect with the clip region:
492 */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300493 if ((min_x >= ctx.clip.x2) || (max_x <= ctx.clip.x1) ||
494 (min_y >= ctx.clip.y2) || (max_y <= ctx.clip.y1))
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400495 return 0;
496
497 /* Simple case, bounding box edges are parallel to surface edges,
498 * there will be only four edges. We just need to clip the surface
499 * vertices to the clip rect bounds:
500 */
501 if (!es->transform.enabled) {
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300502 for (i = 0; i < surf.n; i++) {
503 ex[i] = clip(surf.x[i], ctx.clip.x1, ctx.clip.x2);
504 ey[i] = clip(surf.y[i], ctx.clip.y1, ctx.clip.y2);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400505 }
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300506 return surf.n;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400507 }
508
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300509 /* Transformed case: use a general polygon clipping algorithm to
510 * clip the surface rectangle with each side of 'rect'.
511 * The algorithm is Sutherland-Hodgman, as explained in
512 * http://www.codeguru.com/cpp/misc/misc/graphics/article.php/c8965/Polygon-Clipping.htm
513 * but without looking at any of that code.
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400514 */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300515 polygon.n = clip_polygon_left(&ctx, &surf, polygon.x, polygon.y);
516 surf.n = clip_polygon_right(&ctx, &polygon, surf.x, surf.y);
517 polygon.n = clip_polygon_top(&ctx, &surf, polygon.x, polygon.y);
518 surf.n = clip_polygon_bottom(&ctx, &polygon, surf.x, surf.y);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400519
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300520 /* Get rid of duplicate vertices */
521 ex[0] = surf.x[0];
522 ey[0] = surf.y[0];
523 n = 1;
524 for (i = 1; i < surf.n; i++) {
525 if (float_difference(ex[n - 1], surf.x[i]) == 0.0f &&
526 float_difference(ey[n - 1], surf.y[i]) == 0.0f)
527 continue;
528 ex[n] = surf.x[i];
529 ey[n] = surf.y[i];
530 n++;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400531 }
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300532 if (float_difference(ex[n - 1], surf.x[0]) == 0.0f &&
533 float_difference(ey[n - 1], surf.y[0]) == 0.0f)
534 n--;
535
536 if (n < 3)
537 return 0;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400538
539 return n;
540}
541
542static int
543texture_region(struct weston_surface *es, pixman_region32_t *region,
544 pixman_region32_t *surf_region)
545{
Pekka Paalanen68033ac2012-12-04 15:58:15 +0200546 struct gl_surface_state *gs = get_surface_state(es);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400547 struct weston_compositor *ec = es->compositor;
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400548 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400549 GLfloat *v, inv_width, inv_height;
550 unsigned int *vtxcnt, nvtx = 0;
551 pixman_box32_t *rects, *surf_rects;
552 int i, j, k, nrects, nsurf;
553
554 rects = pixman_region32_rectangles(region, &nrects);
555 surf_rects = pixman_region32_rectangles(surf_region, &nsurf);
556
557 /* worst case we can have 8 vertices per rect (ie. clipped into
558 * an octagon):
559 */
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400560 v = wl_array_add(&gr->vertices, nrects * nsurf * 8 * 4 * sizeof *v);
561 vtxcnt = wl_array_add(&gr->vtxcnt, nrects * nsurf * sizeof *vtxcnt);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400562
Pekka Paalanen68033ac2012-12-04 15:58:15 +0200563 inv_width = 1.0 / gs->pitch;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200564 inv_height = 1.0 / gs->height;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400565
566 for (i = 0; i < nrects; i++) {
567 pixman_box32_t *rect = &rects[i];
568 for (j = 0; j < nsurf; j++) {
569 pixman_box32_t *surf_rect = &surf_rects[j];
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200570 GLfloat sx, sy, bx, by;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400571 GLfloat ex[8], ey[8]; /* edge points in screen space */
572 int n;
573
574 /* The transformed surface, after clipping to the clip region,
575 * can have as many as eight sides, emitted as a triangle-fan.
576 * The first vertex in the triangle fan can be chosen arbitrarily,
577 * since the area is guaranteed to be convex.
578 *
579 * If a corner of the transformed surface falls outside of the
580 * clip region, instead of emitting one vertex for the corner
581 * of the surface, up to two are emitted for two corresponding
582 * intersection point(s) between the surface and the clip region.
583 *
584 * To do this, we first calculate the (up to eight) points that
585 * form the intersection of the clip rect and the transformed
586 * surface.
587 */
588 n = calculate_edges(es, rect, surf_rect, ex, ey);
589 if (n < 3)
590 continue;
591
592 /* emit edge points: */
593 for (k = 0; k < n; k++) {
594 weston_surface_from_global_float(es, ex[k], ey[k], &sx, &sy);
595 /* position: */
596 *(v++) = ex[k];
597 *(v++) = ey[k];
598 /* texcoord: */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200599 weston_surface_to_buffer_float(es, sx, sy,
600 &bx, &by);
601 *(v++) = bx * inv_width;
602 *(v++) = by * inv_height;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400603 }
604
605 vtxcnt[nvtx++] = n;
606 }
607 }
608
609 return nvtx;
610}
611
612static void
613triangle_fan_debug(struct weston_surface *surface, int first, int count)
614{
615 struct weston_compositor *compositor = surface->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100616 struct gl_renderer *gr = get_renderer(compositor);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400617 int i;
618 GLushort *buffer;
619 GLushort *index;
620 int nelems;
621 static int color_idx = 0;
622 static const GLfloat color[][4] = {
623 { 1.0, 0.0, 0.0, 1.0 },
624 { 0.0, 1.0, 0.0, 1.0 },
625 { 0.0, 0.0, 1.0, 1.0 },
626 { 1.0, 1.0, 1.0, 1.0 },
627 };
628
629 nelems = (count - 1 + count - 2) * 2;
630
631 buffer = malloc(sizeof(GLushort) * nelems);
632 index = buffer;
633
634 for (i = 1; i < count; i++) {
635 *index++ = first;
636 *index++ = first + i;
637 }
638
639 for (i = 2; i < count; i++) {
640 *index++ = first + i - 1;
641 *index++ = first + i;
642 }
643
John Kåre Alsaker40684142012-11-13 19:10:25 +0100644 glUseProgram(gr->solid_shader.program);
645 glUniform4fv(gr->solid_shader.color_uniform, 1,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400646 color[color_idx++ % ARRAY_LENGTH(color)]);
647 glDrawElements(GL_LINES, nelems, GL_UNSIGNED_SHORT, buffer);
John Kåre Alsaker40684142012-11-13 19:10:25 +0100648 glUseProgram(gr->current_shader->program);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400649 free(buffer);
650}
651
652static void
653repaint_region(struct weston_surface *es, pixman_region32_t *region,
654 pixman_region32_t *surf_region)
655{
656 struct weston_compositor *ec = es->compositor;
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400657 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400658 GLfloat *v;
659 unsigned int *vtxcnt;
660 int i, first, nfans;
661
662 /* The final region to be painted is the intersection of
663 * 'region' and 'surf_region'. However, 'region' is in the global
664 * coordinates, and 'surf_region' is in the surface-local
665 * coordinates. texture_region() will iterate over all pairs of
666 * rectangles from both regions, compute the intersection
667 * polygon for each pair, and store it as a triangle fan if
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400668 * it has a non-zero area (at least 3 vertices1, actually).
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400669 */
670 nfans = texture_region(es, region, surf_region);
671
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400672 v = gr->vertices.data;
673 vtxcnt = gr->vtxcnt.data;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400674
675 /* position: */
676 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
677 glEnableVertexAttribArray(0);
678
679 /* texcoord: */
680 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
681 glEnableVertexAttribArray(1);
682
683 for (i = 0, first = 0; i < nfans; i++) {
684 glDrawArrays(GL_TRIANGLE_FAN, first, vtxcnt[i]);
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400685 if (gr->fan_debug)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400686 triangle_fan_debug(es, first, vtxcnt[i]);
687 first += vtxcnt[i];
688 }
689
690 glDisableVertexAttribArray(1);
691 glDisableVertexAttribArray(0);
692
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400693 gr->vertices.size = 0;
694 gr->vtxcnt.size = 0;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400695}
696
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100697static int
698use_output(struct weston_output *output)
699{
700 static int errored;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100701 struct gl_output_state *go = get_output_state(output);
702 struct gl_renderer *gr = get_renderer(output->compositor);
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100703 EGLBoolean ret;
704
705 ret = eglMakeCurrent(gr->egl_display, go->egl_surface,
706 go->egl_surface, gr->egl_context);
707
708 if (ret == EGL_FALSE) {
709 if (errored)
710 return -1;
711 errored = 1;
712 weston_log("Failed to make EGL context current.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +0200713 gl_renderer_print_egl_error_state();
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100714 return -1;
715 }
716
717 return 0;
718}
719
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +0300720static int
721shader_init(struct gl_shader *shader, struct gl_renderer *gr,
722 const char *vertex_source, const char *fragment_source);
723
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400724static void
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +0300725use_shader(struct gl_renderer *gr, struct gl_shader *shader)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400726{
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +0300727 if (!shader->program) {
728 int ret;
729
730 ret = shader_init(shader, gr,
731 shader->vertex_source,
732 shader->fragment_source);
733
734 if (ret < 0)
735 weston_log("warning: failed to compile shader\n");
736 }
737
John Kåre Alsaker40684142012-11-13 19:10:25 +0100738 if (gr->current_shader == shader)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400739 return;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400740 glUseProgram(shader->program);
John Kåre Alsaker40684142012-11-13 19:10:25 +0100741 gr->current_shader = shader;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400742}
743
744static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100745shader_uniforms(struct gl_shader *shader,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400746 struct weston_surface *surface,
747 struct weston_output *output)
748{
749 int i;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100750 struct gl_surface_state *gs = get_surface_state(surface);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400751
752 glUniformMatrix4fv(shader->proj_uniform,
753 1, GL_FALSE, output->matrix.d);
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100754 glUniform4fv(shader->color_uniform, 1, gs->color);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400755 glUniform1f(shader->alpha_uniform, surface->alpha);
756
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100757 for (i = 0; i < gs->num_textures; i++)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400758 glUniform1i(shader->tex_uniforms[i], i);
759}
760
761static void
762draw_surface(struct weston_surface *es, struct weston_output *output,
763 pixman_region32_t *damage) /* in global coordinates */
764{
765 struct weston_compositor *ec = es->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100766 struct gl_renderer *gr = get_renderer(ec);
767 struct gl_surface_state *gs = get_surface_state(es);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400768 /* repaint bounding region in global coordinates: */
769 pixman_region32_t repaint;
770 /* non-opaque region in surface coordinates: */
771 pixman_region32_t surface_blend;
772 GLint filter;
773 int i;
774
775 pixman_region32_init(&repaint);
776 pixman_region32_intersect(&repaint,
777 &es->transform.boundingbox, damage);
778 pixman_region32_subtract(&repaint, &repaint, &es->clip);
779
780 if (!pixman_region32_not_empty(&repaint))
781 goto out;
782
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400783 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
784
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400785 if (gr->fan_debug) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100786 use_shader(gr, &gr->solid_shader);
787 shader_uniforms(&gr->solid_shader, es, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400788 }
789
John Kåre Alsaker40684142012-11-13 19:10:25 +0100790 use_shader(gr, gs->shader);
791 shader_uniforms(gs->shader, es, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400792
Alexander Larsson4ea95522013-05-22 14:41:37 +0200793 if (es->transform.enabled || output->zoom.active || output->scale != es->buffer_scale)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400794 filter = GL_LINEAR;
795 else
796 filter = GL_NEAREST;
797
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100798 for (i = 0; i < gs->num_textures; i++) {
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400799 glActiveTexture(GL_TEXTURE0 + i);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100800 glBindTexture(gs->target, gs->textures[i]);
801 glTexParameteri(gs->target, GL_TEXTURE_MIN_FILTER, filter);
802 glTexParameteri(gs->target, GL_TEXTURE_MAG_FILTER, filter);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400803 }
804
805 /* blended region is whole surface minus opaque region: */
806 pixman_region32_init_rect(&surface_blend, 0, 0,
807 es->geometry.width, es->geometry.height);
808 pixman_region32_subtract(&surface_blend, &surface_blend, &es->opaque);
809
810 if (pixman_region32_not_empty(&es->opaque)) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100811 if (gs->shader == &gr->texture_shader_rgba) {
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400812 /* Special case for RGBA textures with possibly
813 * bad data in alpha channel: use the shader
814 * that forces texture alpha = 1.0.
815 * Xwayland surfaces need this.
816 */
John Kåre Alsaker40684142012-11-13 19:10:25 +0100817 use_shader(gr, &gr->texture_shader_rgbx);
818 shader_uniforms(&gr->texture_shader_rgbx, es, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400819 }
820
821 if (es->alpha < 1.0)
822 glEnable(GL_BLEND);
823 else
824 glDisable(GL_BLEND);
825
826 repaint_region(es, &repaint, &es->opaque);
827 }
828
829 if (pixman_region32_not_empty(&surface_blend)) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100830 use_shader(gr, gs->shader);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400831 glEnable(GL_BLEND);
832 repaint_region(es, &repaint, &surface_blend);
833 }
834
835 pixman_region32_fini(&surface_blend);
836
837out:
838 pixman_region32_fini(&repaint);
839}
840
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400841static void
842repaint_surfaces(struct weston_output *output, pixman_region32_t *damage)
843{
844 struct weston_compositor *compositor = output->compositor;
845 struct weston_surface *surface;
846
847 wl_list_for_each_reverse(surface, &compositor->surface_list, link)
848 if (surface->plane == &compositor->primary_plane)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400849 draw_surface(surface, output, damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400850}
851
John Kåre Alsaker44154502012-11-13 19:10:20 +0100852
853static int
854texture_border(struct weston_output *output)
855{
856 struct weston_compositor *ec = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100857 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker44154502012-11-13 19:10:20 +0100858 GLfloat *d;
859 unsigned int *p;
860 int i, j, k, n;
861 GLfloat x[4], y[4], u[4], v[4];
862
863 x[0] = -gr->border.left;
864 x[1] = 0;
865 x[2] = output->current->width;
866 x[3] = output->current->width + gr->border.right;
867
868 y[0] = -gr->border.top;
869 y[1] = 0;
870 y[2] = output->current->height;
871 y[3] = output->current->height + gr->border.bottom;
872
873 u[0] = 0.0;
874 u[1] = (GLfloat) gr->border.left / gr->border.width;
875 u[2] = (GLfloat) (gr->border.width - gr->border.right) / gr->border.width;
876 u[3] = 1.0;
877
878 v[0] = 0.0;
879 v[1] = (GLfloat) gr->border.top / gr->border.height;
880 v[2] = (GLfloat) (gr->border.height - gr->border.bottom) / gr->border.height;
881 v[3] = 1.0;
882
883 n = 8;
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400884 d = wl_array_add(&gr->vertices, n * 16 * sizeof *d);
885 p = wl_array_add(&gr->indices, n * 6 * sizeof *p);
John Kåre Alsaker44154502012-11-13 19:10:20 +0100886
887 k = 0;
888 for (i = 0; i < 3; i++)
889 for (j = 0; j < 3; j++) {
890
891 if (i == 1 && j == 1)
892 continue;
893
894 d[ 0] = x[i];
895 d[ 1] = y[j];
896 d[ 2] = u[i];
897 d[ 3] = v[j];
898
899 d[ 4] = x[i];
900 d[ 5] = y[j + 1];
901 d[ 6] = u[i];
902 d[ 7] = v[j + 1];
903
904 d[ 8] = x[i + 1];
905 d[ 9] = y[j];
906 d[10] = u[i + 1];
907 d[11] = v[j];
908
909 d[12] = x[i + 1];
910 d[13] = y[j + 1];
911 d[14] = u[i + 1];
912 d[15] = v[j + 1];
913
914 p[0] = k + 0;
915 p[1] = k + 1;
916 p[2] = k + 2;
917 p[3] = k + 2;
918 p[4] = k + 1;
919 p[5] = k + 3;
920
921 d += 16;
922 p += 6;
923 k += 4;
924 }
925
926 return k / 4;
927}
928
929static void
930draw_border(struct weston_output *output)
931{
932 struct weston_compositor *ec = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100933 struct gl_renderer *gr = get_renderer(ec);
934 struct gl_shader *shader = &gr->texture_shader_rgba;
John Kåre Alsaker44154502012-11-13 19:10:20 +0100935 GLfloat *v;
936 int n;
937
938 glDisable(GL_BLEND);
John Kåre Alsaker40684142012-11-13 19:10:25 +0100939 use_shader(gr, shader);
John Kåre Alsaker44154502012-11-13 19:10:20 +0100940
941 glUniformMatrix4fv(shader->proj_uniform,
942 1, GL_FALSE, output->matrix.d);
943
944 glUniform1i(shader->tex_uniforms[0], 0);
945 glUniform1f(shader->alpha_uniform, 1);
946
947 n = texture_border(output);
948
949 glActiveTexture(GL_TEXTURE0);
950 glBindTexture(GL_TEXTURE_2D, gr->border.texture);
951
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400952 v = gr->vertices.data;
John Kåre Alsaker44154502012-11-13 19:10:20 +0100953 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
954 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
955 glEnableVertexAttribArray(0);
956 glEnableVertexAttribArray(1);
957
958 glDrawElements(GL_TRIANGLES, n * 6,
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400959 GL_UNSIGNED_INT, gr->indices.data);
John Kåre Alsaker44154502012-11-13 19:10:20 +0100960
961 glDisableVertexAttribArray(1);
962 glDisableVertexAttribArray(0);
963
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400964 gr->vertices.size = 0;
965 gr->indices.size = 0;
John Kåre Alsaker44154502012-11-13 19:10:20 +0100966}
967
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400968static void
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +0200969output_get_buffer_damage(struct weston_output *output,
970 pixman_region32_t *buffer_damage)
971{
972 struct gl_output_state *go = get_output_state(output);
973 struct gl_renderer *gr = get_renderer(output->compositor);
974 EGLint buffer_age = 0;
975 EGLBoolean ret;
976 int i;
977
978 if (gr->has_egl_buffer_age) {
979 ret = eglQuerySurface(gr->egl_display, go->egl_surface,
980 EGL_BUFFER_AGE_EXT, &buffer_age);
981 if (ret == EGL_FALSE) {
982 weston_log("buffer age query failed.\n");
983 gl_renderer_print_egl_error_state();
984 }
985 }
986
987 if (buffer_age == 0 || buffer_age - 1 > BUFFER_DAMAGE_COUNT)
988 pixman_region32_copy(buffer_damage, &output->region);
989 else
990 for (i = 0; i < buffer_age - 1; i++)
991 pixman_region32_union(buffer_damage, buffer_damage,
992 &go->buffer_damage[i]);
993}
994
995static void
996output_rotate_damage(struct weston_output *output,
997 pixman_region32_t *output_damage)
998{
999 struct gl_output_state *go = get_output_state(output);
1000 struct gl_renderer *gr = get_renderer(output->compositor);
1001 int i;
1002
1003 if (!gr->has_egl_buffer_age)
1004 return;
1005
1006 for (i = BUFFER_DAMAGE_COUNT - 1; i >= 1; i--)
1007 pixman_region32_copy(&go->buffer_damage[i],
1008 &go->buffer_damage[i - 1]);
1009
1010 pixman_region32_copy(&go->buffer_damage[0], output_damage);
1011}
1012
1013static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001014gl_renderer_repaint_output(struct weston_output *output,
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001015 pixman_region32_t *output_damage)
1016{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001017 struct gl_output_state *go = get_output_state(output);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001018 struct weston_compositor *compositor = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001019 struct gl_renderer *gr = get_renderer(compositor);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001020 EGLBoolean ret;
1021 static int errored;
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001022 int32_t width, height;
1023 pixman_region32_t buffer_damage, total_damage;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001024
Alexander Larsson0b135062013-05-28 16:23:36 +02001025 width = output->current->width +
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001026 output->border.left + output->border.right;
Alexander Larsson0b135062013-05-28 16:23:36 +02001027 height = output->current->height +
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001028 output->border.top + output->border.bottom;
1029
1030 glViewport(0, 0, width, height);
1031
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001032 if (use_output(output) < 0)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001033 return;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001034
1035 /* if debugging, redraw everything outside the damage to clean up
1036 * debug lines from the previous draw on this buffer:
1037 */
Kristian Høgsberg8799d412013-05-07 10:50:09 -04001038 if (gr->fan_debug) {
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001039 pixman_region32_t undamaged;
1040 pixman_region32_init(&undamaged);
1041 pixman_region32_subtract(&undamaged, &output->region,
1042 output_damage);
Kristian Høgsberg8799d412013-05-07 10:50:09 -04001043 gr->fan_debug = 0;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001044 repaint_surfaces(output, &undamaged);
Kristian Høgsberg8799d412013-05-07 10:50:09 -04001045 gr->fan_debug = 1;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001046 pixman_region32_fini(&undamaged);
1047 }
1048
Ander Conselvan de Oliveirab605c062013-03-05 17:30:28 +02001049 pixman_region32_init(&total_damage);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001050 pixman_region32_init(&buffer_damage);
1051
1052 output_get_buffer_damage(output, &buffer_damage);
1053 output_rotate_damage(output, output_damage);
1054
1055 pixman_region32_union(&total_damage, &buffer_damage, output_damage);
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03001056
Ander Conselvan de Oliveirab605c062013-03-05 17:30:28 +02001057 repaint_surfaces(output, &total_damage);
1058
1059 pixman_region32_fini(&total_damage);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001060 pixman_region32_fini(&buffer_damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001061
John Kåre Alsaker44154502012-11-13 19:10:20 +01001062 if (gr->border.texture)
1063 draw_border(output);
1064
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001065 pixman_region32_copy(&output->previous_damage, output_damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001066 wl_signal_emit(&output->frame_signal, output);
1067
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001068 ret = eglSwapBuffers(gr->egl_display, go->egl_surface);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001069 if (ret == EGL_FALSE && !errored) {
1070 errored = 1;
1071 weston_log("Failed in eglSwapBuffers.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02001072 gl_renderer_print_egl_error_state();
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001073 }
1074
1075}
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001076
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001077static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001078gl_renderer_read_pixels(struct weston_output *output,
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001079 pixman_format_code_t format, void *pixels,
1080 uint32_t x, uint32_t y,
1081 uint32_t width, uint32_t height)
1082{
1083 GLenum gl_format;
1084
1085 switch (format) {
1086 case PIXMAN_a8r8g8b8:
1087 gl_format = GL_BGRA_EXT;
1088 break;
1089 case PIXMAN_a8b8g8r8:
1090 gl_format = GL_RGBA;
1091 break;
1092 default:
1093 return -1;
1094 }
1095
1096 if (use_output(output) < 0)
1097 return -1;
1098
1099 glPixelStorei(GL_PACK_ALIGNMENT, 1);
1100 glReadPixels(x, y, width, height, gl_format,
1101 GL_UNSIGNED_BYTE, pixels);
1102
1103 return 0;
1104}
1105
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001106static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001107gl_renderer_flush_damage(struct weston_surface *surface)
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001108{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001109 struct gl_renderer *gr = get_renderer(surface->compositor);
1110 struct gl_surface_state *gs = get_surface_state(surface);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001111 struct weston_buffer *buffer = gs->buffer_ref.buffer;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001112 GLenum format;
1113 int pixel_type;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001114
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -07001115#ifdef GL_EXT_unpack_subimage
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001116 pixman_box32_t *rectangles;
1117 void *data;
1118 int i, n;
1119#endif
1120
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001121 pixman_region32_union(&gs->texture_damage,
1122 &gs->texture_damage, &surface->damage);
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001123
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001124 if (!buffer)
1125 return;
1126
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001127 /* Avoid upload, if the texture won't be used this time.
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001128 * We still accumulate the damage in texture_damage, and
1129 * hold the reference to the buffer, in case the surface
1130 * migrates back to the primary plane.
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001131 */
1132 if (surface->plane != &surface->compositor->primary_plane)
1133 return;
1134
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001135 if (!pixman_region32_not_empty(&gs->texture_damage))
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001136 goto done;
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001137
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001138 switch (wl_shm_buffer_get_format(buffer->shm_buffer)) {
1139 case WL_SHM_FORMAT_XRGB8888:
1140 case WL_SHM_FORMAT_ARGB8888:
1141 format = GL_BGRA_EXT;
1142 pixel_type = GL_UNSIGNED_BYTE;
1143 break;
1144 case WL_SHM_FORMAT_RGB565:
1145 format = GL_RGB;
1146 pixel_type = GL_UNSIGNED_SHORT_5_6_5;
1147 break;
1148 default:
1149 weston_log("warning: unknown shm buffer format\n");
1150 format = GL_BGRA_EXT;
1151 pixel_type = GL_UNSIGNED_BYTE;
1152 }
1153
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001154 glBindTexture(GL_TEXTURE_2D, gs->textures[0]);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001155
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001156 if (!gr->has_unpack_subimage) {
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001157 glTexImage2D(GL_TEXTURE_2D, 0, format,
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001158 gs->pitch, buffer->height, 0,
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001159 format, pixel_type,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001160 wl_shm_buffer_get_data(buffer->shm_buffer));
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001161
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001162 goto done;
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001163 }
1164
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -07001165#ifdef GL_EXT_unpack_subimage
1166 glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, gs->pitch);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001167 data = wl_shm_buffer_get_data(buffer->shm_buffer);
Ander Conselvan de Oliveira6be5f432013-06-07 16:52:45 +03001168
1169 if (gs->needs_full_upload) {
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -07001170 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, 0);
1171 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, 0);
Ander Conselvan de Oliveira6be5f432013-06-07 16:52:45 +03001172 glTexSubImage2D(GL_TEXTURE_2D, 0,
1173 0, 0, gs->pitch, buffer->height,
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001174 format, pixel_type, data);
Ander Conselvan de Oliveira6be5f432013-06-07 16:52:45 +03001175 goto done;
1176 }
1177
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001178 rectangles = pixman_region32_rectangles(&gs->texture_damage, &n);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001179 for (i = 0; i < n; i++) {
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001180 pixman_box32_t r;
1181
1182 r = weston_surface_to_buffer_rect(surface, rectangles[i]);
1183
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -07001184 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, r.x1);
1185 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, r.y1);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001186 glTexSubImage2D(GL_TEXTURE_2D, 0, r.x1, r.y1,
1187 r.x2 - r.x1, r.y2 - r.y1,
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001188 format, pixel_type, data);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001189 }
1190#endif
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001191
1192done:
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001193 pixman_region32_fini(&gs->texture_damage);
1194 pixman_region32_init(&gs->texture_damage);
Ander Conselvan de Oliveira6be5f432013-06-07 16:52:45 +03001195 gs->needs_full_upload = 0;
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001196
1197 weston_buffer_reference(&gs->buffer_ref, NULL);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001198}
1199
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001200static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001201ensure_textures(struct gl_surface_state *gs, int num_textures)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001202{
1203 int i;
1204
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001205 if (num_textures <= gs->num_textures)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001206 return;
1207
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001208 for (i = gs->num_textures; i < num_textures; i++) {
1209 glGenTextures(1, &gs->textures[i]);
1210 glBindTexture(gs->target, gs->textures[i]);
1211 glTexParameteri(gs->target,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001212 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001213 glTexParameteri(gs->target,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001214 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1215 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001216 gs->num_textures = num_textures;
1217 glBindTexture(gs->target, 0);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001218}
1219
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001220static void
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001221gl_renderer_attach_shm(struct weston_surface *es, struct weston_buffer *buffer,
1222 struct wl_shm_buffer *shm_buffer)
1223{
1224 struct weston_compositor *ec = es->compositor;
1225 struct gl_renderer *gr = get_renderer(ec);
1226 struct gl_surface_state *gs = get_surface_state(es);
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001227 int pitch;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001228
1229 buffer->shm_buffer = shm_buffer;
1230 buffer->width = wl_shm_buffer_get_width(shm_buffer);
1231 buffer->height = wl_shm_buffer_get_height(shm_buffer);
1232
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001233 switch (wl_shm_buffer_get_format(shm_buffer)) {
1234 case WL_SHM_FORMAT_XRGB8888:
1235 gs->shader = &gr->texture_shader_rgbx;
1236 pitch = wl_shm_buffer_get_stride(shm_buffer) / 4;
1237 break;
1238 case WL_SHM_FORMAT_ARGB8888:
1239 gs->shader = &gr->texture_shader_rgba;
1240 pitch = wl_shm_buffer_get_stride(shm_buffer) / 4;
1241 break;
1242 case WL_SHM_FORMAT_RGB565:
1243 gs->shader = &gr->texture_shader_rgbx;
1244 pitch = wl_shm_buffer_get_stride(shm_buffer) / 2;
1245 break;
1246 default:
1247 weston_log("warning: unknown shm buffer format\n");
1248 gs->shader = &gr->texture_shader_rgba;
1249 pitch = wl_shm_buffer_get_stride(shm_buffer) / 4;
1250 }
1251
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001252 /* Only allocate a texture if it doesn't match existing one.
1253 * If a switch from DRM allocated buffer to a SHM buffer is
1254 * happening, we need to allocate a new texture buffer. */
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001255 if (pitch != gs->pitch ||
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001256 buffer->height != gs->height ||
1257 gs->buffer_type != BUFFER_TYPE_SHM) {
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001258 gs->pitch = pitch;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001259 gs->height = buffer->height;
1260 gs->target = GL_TEXTURE_2D;
1261 gs->buffer_type = BUFFER_TYPE_SHM;
1262 gs->needs_full_upload = 1;
1263
1264 ensure_textures(gs, 1);
1265 glBindTexture(GL_TEXTURE_2D, gs->textures[0]);
1266 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1267 gs->pitch, buffer->height, 0,
1268 GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
1269 }
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001270}
1271
1272static void
1273gl_renderer_attach_egl(struct weston_surface *es, struct weston_buffer *buffer,
1274 uint32_t format)
1275{
1276 struct weston_compositor *ec = es->compositor;
1277 struct gl_renderer *gr = get_renderer(ec);
1278 struct gl_surface_state *gs = get_surface_state(es);
1279 EGLint attribs[3];
1280 int i, num_planes;
1281
1282 buffer->legacy_buffer = (struct wl_buffer *)buffer->resource;
1283 gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1284 EGL_WIDTH, &buffer->width);
1285 gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1286 EGL_HEIGHT, &buffer->height);
1287
1288 for (i = 0; i < gs->num_images; i++)
1289 gr->destroy_image(gr->egl_display, gs->images[i]);
1290 gs->num_images = 0;
1291 gs->target = GL_TEXTURE_2D;
1292 switch (format) {
1293 case EGL_TEXTURE_RGB:
1294 case EGL_TEXTURE_RGBA:
1295 default:
1296 num_planes = 1;
1297 gs->shader = &gr->texture_shader_rgba;
1298 break;
1299 case EGL_TEXTURE_EXTERNAL_WL:
1300 num_planes = 1;
1301 gs->target = GL_TEXTURE_EXTERNAL_OES;
1302 gs->shader = &gr->texture_shader_egl_external;
1303 break;
1304 case EGL_TEXTURE_Y_UV_WL:
1305 num_planes = 2;
1306 gs->shader = &gr->texture_shader_y_uv;
1307 break;
1308 case EGL_TEXTURE_Y_U_V_WL:
1309 num_planes = 3;
1310 gs->shader = &gr->texture_shader_y_u_v;
1311 break;
1312 case EGL_TEXTURE_Y_XUXV_WL:
1313 num_planes = 2;
1314 gs->shader = &gr->texture_shader_y_xuxv;
1315 break;
1316 }
1317
1318 ensure_textures(gs, num_planes);
1319 for (i = 0; i < num_planes; i++) {
1320 attribs[0] = EGL_WAYLAND_PLANE_WL;
1321 attribs[1] = i;
1322 attribs[2] = EGL_NONE;
1323 gs->images[i] = gr->create_image(gr->egl_display,
1324 NULL,
1325 EGL_WAYLAND_BUFFER_WL,
1326 buffer->legacy_buffer,
1327 attribs);
1328 if (!gs->images[i]) {
1329 weston_log("failed to create img for plane %d\n", i);
1330 continue;
1331 }
1332 gs->num_images++;
1333
1334 glActiveTexture(GL_TEXTURE0 + i);
1335 glBindTexture(gs->target, gs->textures[i]);
1336 gr->image_target_texture_2d(gs->target,
1337 gs->images[i]);
1338 }
1339
1340 gs->pitch = buffer->width;
1341 gs->height = buffer->height;
1342 gs->buffer_type = BUFFER_TYPE_EGL;
1343}
1344
1345static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001346gl_renderer_attach(struct weston_surface *es, struct weston_buffer *buffer)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001347{
1348 struct weston_compositor *ec = es->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001349 struct gl_renderer *gr = get_renderer(ec);
1350 struct gl_surface_state *gs = get_surface_state(es);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001351 struct wl_shm_buffer *shm_buffer;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001352 EGLint format;
1353 int i;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001354
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001355 weston_buffer_reference(&gs->buffer_ref, buffer);
1356
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001357 if (!buffer) {
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001358 for (i = 0; i < gs->num_images; i++) {
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001359 gr->destroy_image(gr->egl_display, gs->images[i]);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001360 gs->images[i] = NULL;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001361 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001362 gs->num_images = 0;
1363 glDeleteTextures(gs->num_textures, gs->textures);
1364 gs->num_textures = 0;
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +03001365 gs->buffer_type = BUFFER_TYPE_NULL;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001366 return;
1367 }
1368
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001369 shm_buffer = wl_shm_buffer_get(buffer->resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001370
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001371 if (shm_buffer)
1372 gl_renderer_attach_shm(es, buffer, shm_buffer);
1373 else if (gr->query_buffer(gr->egl_display,
1374 (struct wl_buffer *)buffer->resource,
1375 EGL_TEXTURE_FORMAT, &format))
1376 gl_renderer_attach_egl(es, buffer, format);
1377 else {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001378 weston_log("unhandled buffer type!\n");
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001379 weston_buffer_reference(&gs->buffer_ref, NULL);
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +03001380 gs->buffer_type = BUFFER_TYPE_NULL;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001381 }
1382}
1383
Kristian Høgsberg42263852012-09-06 21:59:29 -04001384static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001385gl_renderer_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001386 float red, float green, float blue, float alpha)
1387{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001388 struct gl_surface_state *gs = get_surface_state(surface);
1389 struct gl_renderer *gr = get_renderer(surface->compositor);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001390
1391 gs->color[0] = red;
1392 gs->color[1] = green;
1393 gs->color[2] = blue;
1394 gs->color[3] = alpha;
1395
John Kåre Alsaker40684142012-11-13 19:10:25 +01001396 gs->shader = &gr->solid_shader;
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001397}
1398
1399static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001400gl_renderer_create_surface(struct weston_surface *surface)
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001401{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001402 struct gl_surface_state *gs;
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001403
1404 gs = calloc(1, sizeof *gs);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001405 if (!gs)
1406 return -1;
1407
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001408 /* A buffer is never attached to solid color surfaces, yet
1409 * they still go through texcoord computations. Do not divide
1410 * by zero there.
1411 */
1412 gs->pitch = 1;
1413
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001414 pixman_region32_init(&gs->texture_damage);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001415 surface->renderer_state = gs;
1416
1417 return 0;
1418}
1419
1420static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001421gl_renderer_destroy_surface(struct weston_surface *surface)
Kristian Høgsberg42263852012-09-06 21:59:29 -04001422{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001423 struct gl_surface_state *gs = get_surface_state(surface);
1424 struct gl_renderer *gr = get_renderer(surface->compositor);
Kristian Høgsberg42263852012-09-06 21:59:29 -04001425 int i;
1426
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001427 glDeleteTextures(gs->num_textures, gs->textures);
Kristian Høgsberg42263852012-09-06 21:59:29 -04001428
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001429 for (i = 0; i < gs->num_images; i++)
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001430 gr->destroy_image(gr->egl_display, gs->images[i]);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001431
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001432 weston_buffer_reference(&gs->buffer_ref, NULL);
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001433 pixman_region32_fini(&gs->texture_damage);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001434 free(gs);
Kristian Høgsberg42263852012-09-06 21:59:29 -04001435}
1436
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001437static const char vertex_shader[] =
1438 "uniform mat4 proj;\n"
1439 "attribute vec2 position;\n"
1440 "attribute vec2 texcoord;\n"
1441 "varying vec2 v_texcoord;\n"
1442 "void main()\n"
1443 "{\n"
1444 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
1445 " v_texcoord = texcoord;\n"
1446 "}\n";
1447
1448/* Declare common fragment shader uniforms */
1449#define FRAGMENT_CONVERT_YUV \
1450 " y *= alpha;\n" \
1451 " u *= alpha;\n" \
1452 " v *= alpha;\n" \
1453 " gl_FragColor.r = y + 1.59602678 * v;\n" \
1454 " gl_FragColor.g = y - 0.39176229 * u - 0.81296764 * v;\n" \
1455 " gl_FragColor.b = y + 2.01723214 * u;\n" \
1456 " gl_FragColor.a = alpha;\n"
1457
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001458static const char fragment_debug[] =
1459 " gl_FragColor = vec4(0.0, 0.3, 0.0, 0.2) + gl_FragColor * 0.8;\n";
1460
1461static const char fragment_brace[] =
1462 "}\n";
1463
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001464static const char texture_fragment_shader_rgba[] =
1465 "precision mediump float;\n"
1466 "varying vec2 v_texcoord;\n"
1467 "uniform sampler2D tex;\n"
1468 "uniform float alpha;\n"
1469 "void main()\n"
1470 "{\n"
1471 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001472 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001473
1474static const char texture_fragment_shader_rgbx[] =
1475 "precision mediump float;\n"
1476 "varying vec2 v_texcoord;\n"
1477 "uniform sampler2D tex;\n"
1478 "uniform float alpha;\n"
1479 "void main()\n"
1480 "{\n"
1481 " gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb\n;"
1482 " gl_FragColor.a = alpha;\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_egl_external[] =
1486 "#extension GL_OES_EGL_image_external : require\n"
1487 "precision mediump float;\n"
1488 "varying vec2 v_texcoord;\n"
1489 "uniform samplerExternalOES tex;\n"
1490 "uniform float alpha;\n"
1491 "void main()\n"
1492 "{\n"
1493 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\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_y_uv[] =
1497 "precision mediump float;\n"
1498 "uniform sampler2D tex;\n"
1499 "uniform sampler2D tex1;\n"
1500 "varying vec2 v_texcoord;\n"
1501 "uniform float alpha;\n"
1502 "void main() {\n"
1503 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1504 " float u = texture2D(tex1, v_texcoord).r - 0.5;\n"
1505 " float v = texture2D(tex1, v_texcoord).g - 0.5;\n"
1506 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001507 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001508
1509static const char texture_fragment_shader_y_u_v[] =
1510 "precision mediump float;\n"
1511 "uniform sampler2D tex;\n"
1512 "uniform sampler2D tex1;\n"
1513 "uniform sampler2D tex2;\n"
1514 "varying vec2 v_texcoord;\n"
1515 "uniform float alpha;\n"
1516 "void main() {\n"
1517 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1518 " float u = texture2D(tex1, v_texcoord).x - 0.5;\n"
1519 " float v = texture2D(tex2, v_texcoord).x - 0.5;\n"
1520 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001521 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001522
1523static const char texture_fragment_shader_y_xuxv[] =
1524 "precision mediump float;\n"
1525 "uniform sampler2D tex;\n"
1526 "uniform sampler2D tex1;\n"
1527 "varying vec2 v_texcoord;\n"
1528 "uniform float alpha;\n"
1529 "void main() {\n"
1530 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1531 " float u = texture2D(tex1, v_texcoord).g - 0.5;\n"
1532 " float v = texture2D(tex1, v_texcoord).a - 0.5;\n"
1533 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001534 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001535
1536static const char solid_fragment_shader[] =
1537 "precision mediump float;\n"
1538 "uniform vec4 color;\n"
1539 "uniform float alpha;\n"
1540 "void main()\n"
1541 "{\n"
1542 " gl_FragColor = alpha * color\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001543 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001544
1545static int
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001546compile_shader(GLenum type, int count, const char **sources)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001547{
1548 GLuint s;
1549 char msg[512];
1550 GLint status;
1551
1552 s = glCreateShader(type);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001553 glShaderSource(s, count, sources, NULL);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001554 glCompileShader(s);
1555 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
1556 if (!status) {
1557 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1558 weston_log("shader info: %s\n", msg);
1559 return GL_NONE;
1560 }
1561
1562 return s;
1563}
1564
1565static int
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03001566shader_init(struct gl_shader *shader, struct gl_renderer *renderer,
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001567 const char *vertex_source, const char *fragment_source)
1568{
1569 char msg[512];
1570 GLint status;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001571 int count;
1572 const char *sources[3];
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001573
1574 shader->vertex_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001575 compile_shader(GL_VERTEX_SHADER, 1, &vertex_source);
1576
1577 if (renderer->fragment_shader_debug) {
1578 sources[0] = fragment_source;
1579 sources[1] = fragment_debug;
1580 sources[2] = fragment_brace;
1581 count = 3;
1582 } else {
1583 sources[0] = fragment_source;
1584 sources[1] = fragment_brace;
1585 count = 2;
1586 }
1587
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001588 shader->fragment_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001589 compile_shader(GL_FRAGMENT_SHADER, count, sources);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001590
1591 shader->program = glCreateProgram();
1592 glAttachShader(shader->program, shader->vertex_shader);
1593 glAttachShader(shader->program, shader->fragment_shader);
1594 glBindAttribLocation(shader->program, 0, "position");
1595 glBindAttribLocation(shader->program, 1, "texcoord");
1596
1597 glLinkProgram(shader->program);
1598 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1599 if (!status) {
1600 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1601 weston_log("link info: %s\n", msg);
1602 return -1;
1603 }
1604
1605 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1606 shader->tex_uniforms[0] = glGetUniformLocation(shader->program, "tex");
1607 shader->tex_uniforms[1] = glGetUniformLocation(shader->program, "tex1");
1608 shader->tex_uniforms[2] = glGetUniformLocation(shader->program, "tex2");
1609 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
1610 shader->color_uniform = glGetUniformLocation(shader->program, "color");
1611
1612 return 0;
1613}
1614
1615static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001616shader_release(struct gl_shader *shader)
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001617{
1618 glDeleteShader(shader->vertex_shader);
1619 glDeleteShader(shader->fragment_shader);
1620 glDeleteProgram(shader->program);
1621
1622 shader->vertex_shader = 0;
1623 shader->fragment_shader = 0;
1624 shader->program = 0;
1625}
1626
1627static void
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001628log_extensions(const char *name, const char *extensions)
1629{
1630 const char *p, *end;
1631 int l;
1632 int len;
1633
1634 l = weston_log("%s:", name);
1635 p = extensions;
1636 while (*p) {
1637 end = strchrnul(p, ' ');
1638 len = end - p;
1639 if (l + len > 78)
1640 l = weston_log_continue("\n" STAMP_SPACE "%.*s",
1641 len, p);
1642 else
1643 l += weston_log_continue(" %.*s", len, p);
1644 for (p = end; isspace(*p); p++)
1645 ;
1646 }
1647 weston_log_continue("\n");
1648}
1649
1650static void
1651log_egl_gl_info(EGLDisplay egldpy)
1652{
1653 const char *str;
1654
1655 str = eglQueryString(egldpy, EGL_VERSION);
1656 weston_log("EGL version: %s\n", str ? str : "(null)");
1657
1658 str = eglQueryString(egldpy, EGL_VENDOR);
1659 weston_log("EGL vendor: %s\n", str ? str : "(null)");
1660
1661 str = eglQueryString(egldpy, EGL_CLIENT_APIS);
1662 weston_log("EGL client APIs: %s\n", str ? str : "(null)");
1663
1664 str = eglQueryString(egldpy, EGL_EXTENSIONS);
1665 log_extensions("EGL extensions", str ? str : "(null)");
1666
1667 str = (char *)glGetString(GL_VERSION);
1668 weston_log("GL version: %s\n", str ? str : "(null)");
1669
1670 str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
1671 weston_log("GLSL version: %s\n", str ? str : "(null)");
1672
1673 str = (char *)glGetString(GL_VENDOR);
1674 weston_log("GL vendor: %s\n", str ? str : "(null)");
1675
1676 str = (char *)glGetString(GL_RENDERER);
1677 weston_log("GL renderer: %s\n", str ? str : "(null)");
1678
1679 str = (char *)glGetString(GL_EXTENSIONS);
1680 log_extensions("GL extensions", str ? str : "(null)");
1681}
1682
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001683static void
1684log_egl_config_info(EGLDisplay egldpy, EGLConfig eglconfig)
1685{
1686 EGLint r, g, b, a;
1687
1688 weston_log("Chosen EGL config details:\n");
1689
1690 weston_log_continue(STAMP_SPACE "RGBA bits");
1691 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_RED_SIZE, &r) &&
1692 eglGetConfigAttrib(egldpy, eglconfig, EGL_GREEN_SIZE, &g) &&
1693 eglGetConfigAttrib(egldpy, eglconfig, EGL_BLUE_SIZE, &b) &&
1694 eglGetConfigAttrib(egldpy, eglconfig, EGL_ALPHA_SIZE, &a))
1695 weston_log_continue(": %d %d %d %d\n", r, g, b, a);
1696 else
1697 weston_log_continue(" unknown\n");
1698
1699 weston_log_continue(STAMP_SPACE "swap interval range");
1700 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_MIN_SWAP_INTERVAL, &a) &&
1701 eglGetConfigAttrib(egldpy, eglconfig, EGL_MAX_SWAP_INTERVAL, &b))
1702 weston_log_continue(": %d - %d\n", a, b);
1703 else
1704 weston_log_continue(" unknown\n");
1705}
1706
John Kåre Alsaker44154502012-11-13 19:10:20 +01001707static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001708output_apply_border(struct weston_output *output, struct gl_renderer *gr)
John Kåre Alsaker44154502012-11-13 19:10:20 +01001709{
1710 output->border.top = gr->border.top;
1711 output->border.bottom = gr->border.bottom;
1712 output->border.left = gr->border.left;
1713 output->border.right = gr->border.right;
1714}
1715
1716WL_EXPORT void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001717gl_renderer_set_border(struct weston_compositor *ec, int32_t width, int32_t height, void *data,
John Kåre Alsaker44154502012-11-13 19:10:20 +01001718 int32_t *edges)
1719{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001720 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker44154502012-11-13 19:10:20 +01001721 struct weston_output *output;
1722
1723 gr->border.left = edges[0];
1724 gr->border.right = edges[1];
1725 gr->border.top = edges[2];
1726 gr->border.bottom = edges[3];
1727
1728 gr->border.width = width;
1729 gr->border.height = height;
1730
1731 glGenTextures(1, &gr->border.texture);
1732 glBindTexture(GL_TEXTURE_2D, gr->border.texture);
1733 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1734 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1735 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1736 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1737
1738 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1739 width,
1740 height,
1741 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1742 data);
1743
1744 wl_list_for_each(output, &ec->output_list, link)
1745 output_apply_border(output, gr);
1746}
1747
John Kåre Alsaker94659272012-11-13 19:10:18 +01001748static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001749gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001750
1751WL_EXPORT int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001752gl_renderer_output_create(struct weston_output *output,
John Kåre Alsaker94659272012-11-13 19:10:18 +01001753 EGLNativeWindowType window)
1754{
1755 struct weston_compositor *ec = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001756 struct gl_renderer *gr = get_renderer(ec);
1757 struct gl_output_state *go = calloc(1, sizeof *go);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001758 int i;
John Kåre Alsaker94659272012-11-13 19:10:18 +01001759
1760 if (!go)
1761 return -1;
1762
1763 go->egl_surface =
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001764 eglCreateWindowSurface(gr->egl_display,
1765 gr->egl_config,
John Kåre Alsaker94659272012-11-13 19:10:18 +01001766 window, NULL);
1767
1768 if (go->egl_surface == EGL_NO_SURFACE) {
1769 weston_log("failed to create egl surface\n");
1770 free(go);
1771 return -1;
1772 }
1773
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001774 if (gr->egl_context == NULL)
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001775 if (gl_renderer_setup(ec, go->egl_surface) < 0) {
John Kåre Alsaker94659272012-11-13 19:10:18 +01001776 free(go);
1777 return -1;
1778 }
1779
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001780 for (i = 0; i < BUFFER_DAMAGE_COUNT; i++)
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001781 pixman_region32_init(&go->buffer_damage[i]);
1782
John Kåre Alsaker94659272012-11-13 19:10:18 +01001783 output->renderer_state = go;
1784
John Kåre Alsaker44154502012-11-13 19:10:20 +01001785 output_apply_border(output, gr);
1786
John Kåre Alsaker94659272012-11-13 19:10:18 +01001787 return 0;
1788}
1789
1790WL_EXPORT void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001791gl_renderer_output_destroy(struct weston_output *output)
John Kåre Alsaker94659272012-11-13 19:10:18 +01001792{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001793 struct gl_renderer *gr = get_renderer(output->compositor);
1794 struct gl_output_state *go = get_output_state(output);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001795 int i;
1796
1797 for (i = 0; i < 2; i++)
1798 pixman_region32_fini(&go->buffer_damage[i]);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001799
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001800 eglDestroySurface(gr->egl_display, go->egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001801
1802 free(go);
1803}
1804
1805WL_EXPORT EGLSurface
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001806gl_renderer_output_surface(struct weston_output *output)
John Kåre Alsaker94659272012-11-13 19:10:18 +01001807{
1808 return get_output_state(output)->egl_surface;
1809}
1810
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03001811static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001812gl_renderer_destroy(struct weston_compositor *ec)
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001813{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001814 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001815
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001816 if (gr->has_bind_display)
1817 gr->unbind_display(gr->egl_display, ec->wl_display);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001818
1819 /* Work around crash in egl_dri2.c's dri2_make_current() - when does this apply? */
1820 eglMakeCurrent(gr->egl_display,
1821 EGL_NO_SURFACE, EGL_NO_SURFACE,
1822 EGL_NO_CONTEXT);
1823
1824 eglTerminate(gr->egl_display);
1825 eglReleaseThread();
Scott Moreau976a0502013-03-07 10:15:17 -07001826
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -04001827 wl_array_release(&gr->vertices);
1828 wl_array_release(&gr->indices);
1829 wl_array_release(&gr->vtxcnt);
1830
Scott Moreau976a0502013-03-07 10:15:17 -07001831 free(gr);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001832}
1833
1834static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001835egl_choose_config(struct gl_renderer *gr, const EGLint *attribs,
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001836 const EGLint *visual_id)
1837{
1838 EGLint count = 0;
1839 EGLint matched = 0;
1840 EGLConfig *configs;
1841 int i;
1842
1843 if (!eglGetConfigs(gr->egl_display, NULL, 0, &count) || count < 1)
1844 return -1;
1845
1846 configs = calloc(count, sizeof *configs);
1847 if (!configs)
1848 return -1;
1849
1850 if (!eglChooseConfig(gr->egl_display, attribs, configs,
1851 count, &matched))
1852 goto out;
1853
1854 for (i = 0; i < matched; ++i) {
1855 EGLint id;
1856
1857 if (visual_id) {
1858 if (!eglGetConfigAttrib(gr->egl_display,
1859 configs[i], EGL_NATIVE_VISUAL_ID,
1860 &id))
1861 continue;
1862
1863 if (id != *visual_id)
1864 continue;
1865 }
1866
1867 gr->egl_config = configs[i];
1868
1869 free(configs);
1870 return 0;
1871 }
1872
1873out:
1874 free(configs);
1875 return -1;
1876}
1877
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001878WL_EXPORT const EGLint gl_renderer_opaque_attribs[] = {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001879 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1880 EGL_RED_SIZE, 1,
1881 EGL_GREEN_SIZE, 1,
1882 EGL_BLUE_SIZE, 1,
1883 EGL_ALPHA_SIZE, 0,
1884 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1885 EGL_NONE
1886};
1887
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001888WL_EXPORT const EGLint gl_renderer_alpha_attribs[] = {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001889 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1890 EGL_RED_SIZE, 1,
1891 EGL_GREEN_SIZE, 1,
1892 EGL_BLUE_SIZE, 1,
1893 EGL_ALPHA_SIZE, 1,
1894 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1895 EGL_NONE
1896};
1897
1898WL_EXPORT int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001899gl_renderer_create(struct weston_compositor *ec, EGLNativeDisplayType display,
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001900 const EGLint *attribs, const EGLint *visual_id)
1901{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001902 struct gl_renderer *gr;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001903 EGLint major, minor;
1904
1905 gr = calloc(1, sizeof *gr);
1906
1907 if (gr == NULL)
1908 return -1;
1909
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001910 gr->base.read_pixels = gl_renderer_read_pixels;
1911 gr->base.repaint_output = gl_renderer_repaint_output;
1912 gr->base.flush_damage = gl_renderer_flush_damage;
1913 gr->base.attach = gl_renderer_attach;
1914 gr->base.create_surface = gl_renderer_create_surface;
1915 gr->base.surface_set_color = gl_renderer_surface_set_color;
1916 gr->base.destroy_surface = gl_renderer_destroy_surface;
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03001917 gr->base.destroy = gl_renderer_destroy;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001918
1919 gr->egl_display = eglGetDisplay(display);
1920 if (gr->egl_display == EGL_NO_DISPLAY) {
1921 weston_log("failed to create display\n");
1922 goto err_egl;
1923 }
1924
1925 if (!eglInitialize(gr->egl_display, &major, &minor)) {
1926 weston_log("failed to initialize display\n");
1927 goto err_egl;
1928 }
1929
1930 if (egl_choose_config(gr, attribs, visual_id) < 0) {
1931 weston_log("failed to choose EGL config\n");
1932 goto err_egl;
1933 }
1934
1935 ec->renderer = &gr->base;
Pekka Paalanen7bb65102013-05-22 18:03:04 +03001936 ec->capabilities |= WESTON_CAP_ROTATION_ANY;
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03001937 ec->capabilities |= WESTON_CAP_CAPTURE_YFLIP;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001938
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001939 wl_display_add_shm_format(ec->wl_display, WL_SHM_FORMAT_RGB565);
1940
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001941 return 0;
1942
1943err_egl:
Pekka Paalanen326529f2012-11-27 12:25:25 +02001944 gl_renderer_print_egl_error_state();
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001945 free(gr);
1946 return -1;
1947}
1948
1949WL_EXPORT EGLDisplay
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001950gl_renderer_display(struct weston_compositor *ec)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001951{
1952 return get_renderer(ec)->egl_display;
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001953}
1954
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001955static int
1956compile_shaders(struct weston_compositor *ec)
1957{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001958 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker40684142012-11-13 19:10:25 +01001959
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03001960 gr->texture_shader_rgba.vertex_source = vertex_shader;
1961 gr->texture_shader_rgba.fragment_source = texture_fragment_shader_rgba;
1962
1963 gr->texture_shader_rgbx.vertex_source = vertex_shader;
1964 gr->texture_shader_rgbx.fragment_source = texture_fragment_shader_rgbx;
1965
1966 gr->texture_shader_egl_external.vertex_source = vertex_shader;
1967 gr->texture_shader_egl_external.fragment_source =
1968 texture_fragment_shader_egl_external;
1969
1970 gr->texture_shader_y_uv.vertex_source = vertex_shader;
1971 gr->texture_shader_y_uv.fragment_source = texture_fragment_shader_y_uv;
1972
1973 gr->texture_shader_y_u_v.vertex_source = vertex_shader;
1974 gr->texture_shader_y_u_v.fragment_source =
1975 texture_fragment_shader_y_u_v;
1976
1977 gr->texture_shader_y_u_v.vertex_source = vertex_shader;
1978 gr->texture_shader_y_xuxv.fragment_source =
1979 texture_fragment_shader_y_xuxv;
1980
1981 gr->solid_shader.vertex_source = vertex_shader;
1982 gr->solid_shader.fragment_source = solid_fragment_shader;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001983
1984 return 0;
1985}
1986
1987static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04001988fragment_debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001989 void *data)
1990{
1991 struct weston_compositor *ec = data;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001992 struct gl_renderer *gr = get_renderer(ec);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001993 struct weston_output *output;
1994
John Kåre Alsaker40684142012-11-13 19:10:25 +01001995 gr->fragment_shader_debug ^= 1;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001996
John Kåre Alsaker40684142012-11-13 19:10:25 +01001997 shader_release(&gr->texture_shader_rgba);
1998 shader_release(&gr->texture_shader_rgbx);
1999 shader_release(&gr->texture_shader_egl_external);
2000 shader_release(&gr->texture_shader_y_uv);
2001 shader_release(&gr->texture_shader_y_u_v);
2002 shader_release(&gr->texture_shader_y_xuxv);
2003 shader_release(&gr->solid_shader);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002004
Ander Conselvan de Oliveira03fb4ef2012-12-03 17:08:11 +02002005 /* Force use_shader() to call glUseProgram(), since we need to use
2006 * the recompiled version of the shader. */
2007 gr->current_shader = NULL;
2008
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002009 wl_list_for_each(output, &ec->output_list, link)
2010 weston_output_damage(output);
2011}
2012
Kristian Høgsberg8799d412013-05-07 10:50:09 -04002013static void
2014fan_debug_repaint_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
2015 void *data)
2016{
2017 struct weston_compositor *compositor = data;
2018 struct gl_renderer *gr = get_renderer(compositor);
2019
2020 gr->fan_debug = !gr->fan_debug;
2021 weston_compositor_damage_all(compositor);
2022}
2023
John Kåre Alsaker94659272012-11-13 19:10:18 +01002024static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002025gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002026{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002027 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002028 const char *extensions;
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04002029 EGLBoolean ret;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002030
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04002031 static const EGLint context_attribs[] = {
2032 EGL_CONTEXT_CLIENT_VERSION, 2,
2033 EGL_NONE
2034 };
2035
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04002036 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
2037 weston_log("failed to bind EGL_OPENGL_ES_API\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02002038 gl_renderer_print_egl_error_state();
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04002039 return -1;
2040 }
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03002041
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002042 log_egl_config_info(gr->egl_display, gr->egl_config);
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03002043
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002044 gr->egl_context = eglCreateContext(gr->egl_display, gr->egl_config,
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04002045 EGL_NO_CONTEXT, context_attribs);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002046 if (gr->egl_context == NULL) {
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04002047 weston_log("failed to create context\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02002048 gl_renderer_print_egl_error_state();
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04002049 return -1;
2050 }
2051
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002052 ret = eglMakeCurrent(gr->egl_display, egl_surface,
2053 egl_surface, gr->egl_context);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04002054 if (ret == EGL_FALSE) {
2055 weston_log("Failed to make EGL context current.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02002056 gl_renderer_print_egl_error_state();
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04002057 return -1;
2058 }
2059
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002060 log_egl_gl_info(gr->egl_display);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002061
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002062 gr->image_target_texture_2d =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002063 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002064 gr->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2065 gr->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2066 gr->bind_display =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002067 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002068 gr->unbind_display =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002069 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002070 gr->query_buffer =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002071 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
2072
2073 extensions = (const char *) glGetString(GL_EXTENSIONS);
2074 if (!extensions) {
2075 weston_log("Retrieving GL extension string failed.\n");
2076 return -1;
2077 }
2078
2079 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
2080 weston_log("GL_EXT_texture_format_BGRA8888 not available\n");
2081 return -1;
2082 }
2083
2084 if (strstr(extensions, "GL_EXT_read_format_bgra"))
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01002085 ec->read_format = PIXMAN_a8r8g8b8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002086 else
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01002087 ec->read_format = PIXMAN_a8b8g8r8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002088
2089 if (strstr(extensions, "GL_EXT_unpack_subimage"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002090 gr->has_unpack_subimage = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002091
2092 if (strstr(extensions, "GL_OES_EGL_image_external"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002093 gr->has_egl_image_external = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002094
2095 extensions =
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002096 (const char *) eglQueryString(gr->egl_display, EGL_EXTENSIONS);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002097 if (!extensions) {
2098 weston_log("Retrieving EGL extension string failed.\n");
2099 return -1;
2100 }
2101
2102 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002103 gr->has_bind_display = 1;
2104 if (gr->has_bind_display) {
2105 ret = gr->bind_display(gr->egl_display, ec->wl_display);
Pekka Paalanen035a0322012-10-24 09:43:06 +03002106 if (!ret)
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002107 gr->has_bind_display = 0;
Pekka Paalanen035a0322012-10-24 09:43:06 +03002108 }
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002109
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02002110 if (strstr(extensions, "EGL_EXT_buffer_age"))
2111 gr->has_egl_buffer_age = 1;
2112 else
2113 weston_log("warning: EGL_EXT_buffer_age not supported. "
2114 "Performance could be affected.\n");
2115
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002116 glActiveTexture(GL_TEXTURE0);
2117
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002118 if (compile_shaders(ec))
2119 return -1;
2120
2121 weston_compositor_add_debug_binding(ec, KEY_S,
2122 fragment_debug_binding, ec);
Kristian Høgsberg8799d412013-05-07 10:50:09 -04002123 weston_compositor_add_debug_binding(ec, KEY_F,
2124 fan_debug_repaint_binding, ec);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002125
Pekka Paalanen035a0322012-10-24 09:43:06 +03002126 weston_log("GL ES 2 renderer features:\n");
2127 weston_log_continue(STAMP_SPACE "read-back format: %s\n",
Pekka Paalanenfe4eacf2013-01-10 16:50:42 +02002128 ec->read_format == PIXMAN_a8r8g8b8 ? "BGRA" : "RGBA");
Pekka Paalanen035a0322012-10-24 09:43:06 +03002129 weston_log_continue(STAMP_SPACE "wl_shm sub-image to texture: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002130 gr->has_unpack_subimage ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03002131 weston_log_continue(STAMP_SPACE "EGL Wayland extension: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002132 gr->has_bind_display ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03002133
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002134
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002135 return 0;
2136}