blob: 5e1b3961404b1991de3baeb09ed6744d556b77d5 [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"
Sam Spilsbury619859c2013-09-13 10:01:21 +080036#include "vertex-clipping.h"
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010037
38#include <EGL/eglext.h>
39#include "weston-egl-ext.h"
Kristian Høgsbergd7c17262012-09-05 21:54:15 -040040
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010041struct gl_shader {
John Kåre Alsaker40684142012-11-13 19:10:25 +010042 GLuint program;
43 GLuint vertex_shader, fragment_shader;
44 GLint proj_uniform;
45 GLint tex_uniforms[3];
46 GLint alpha_uniform;
47 GLint color_uniform;
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +030048 const char *vertex_source, *fragment_source;
John Kåre Alsaker40684142012-11-13 19:10:25 +010049};
50
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +020051#define BUFFER_DAMAGE_COUNT 2
52
Jason Ekstrand0b61bf42013-10-27 22:24:54 -050053struct gl_border_image {
54 GLuint tex;
55 int32_t width, height;
56 int32_t tex_width;
57 int dirty;
58 void *data;
59};
60
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010061struct gl_output_state {
John Kåre Alsaker94659272012-11-13 19:10:18 +010062 EGLSurface egl_surface;
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +020063 pixman_region32_t buffer_damage[BUFFER_DAMAGE_COUNT];
Jason Ekstrand0b61bf42013-10-27 22:24:54 -050064 struct gl_border_image borders[4];
John Kåre Alsaker94659272012-11-13 19:10:18 +010065};
66
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +030067enum buffer_type {
68 BUFFER_TYPE_NULL,
69 BUFFER_TYPE_SHM,
70 BUFFER_TYPE_EGL
71};
72
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010073struct gl_surface_state {
John Kåre Alsaker878f4492012-11-13 19:10:23 +010074 GLfloat color[4];
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010075 struct gl_shader *shader;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +010076
77 GLuint textures[3];
78 int num_textures;
Ander Conselvan de Oliveira6be5f432013-06-07 16:52:45 +030079 int needs_full_upload;
Pekka Paalanen81ee3f52012-12-04 15:58:16 +020080 pixman_region32_t texture_damage;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +010081
82 EGLImageKHR images[3];
83 GLenum target;
84 int num_images;
Pekka Paalanenfb003d32012-12-04 15:58:13 +020085
86 struct weston_buffer_reference buffer_ref;
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +030087 enum buffer_type buffer_type;
Pekka Paalanen68033ac2012-12-04 15:58:15 +020088 int pitch; /* in pixels */
Alexander Larsson4ea95522013-05-22 14:41:37 +020089 int height; /* in pixels */
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +040090 int y_inverted;
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +030091
92 struct weston_surface *surface;
93
94 struct wl_listener surface_destroy_listener;
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +030095 struct wl_listener renderer_destroy_listener;
John Kåre Alsaker878f4492012-11-13 19:10:23 +010096};
97
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010098struct gl_renderer {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +010099 struct weston_renderer base;
100 int fragment_shader_debug;
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400101 int fan_debug;
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +0300102 struct weston_binding *fragment_binding;
103 struct weston_binding *fan_binding;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100104
105 EGLDisplay egl_display;
106 EGLContext egl_context;
107 EGLConfig egl_config;
John Kåre Alsaker44154502012-11-13 19:10:20 +0100108
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400109 struct wl_array vertices;
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400110 struct wl_array vtxcnt;
111
John Kåre Alsaker320711d2012-11-13 19:10:27 +0100112 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
113 PFNEGLCREATEIMAGEKHRPROC create_image;
114 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
115
116 int has_unpack_subimage;
117
118 PFNEGLBINDWAYLANDDISPLAYWL bind_display;
119 PFNEGLUNBINDWAYLANDDISPLAYWL unbind_display;
120 PFNEGLQUERYWAYLANDBUFFERWL query_buffer;
121 int has_bind_display;
122
123 int has_egl_image_external;
124
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +0200125 int has_egl_buffer_age;
126
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100127 struct gl_shader texture_shader_rgba;
128 struct gl_shader texture_shader_rgbx;
129 struct gl_shader texture_shader_egl_external;
130 struct gl_shader texture_shader_y_uv;
131 struct gl_shader texture_shader_y_u_v;
132 struct gl_shader texture_shader_y_xuxv;
133 struct gl_shader invert_color_shader;
134 struct gl_shader solid_shader;
135 struct gl_shader *current_shader;
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +0300136
137 struct wl_signal destroy_signal;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100138};
John Kåre Alsaker94659272012-11-13 19:10:18 +0100139
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100140static inline struct gl_output_state *
John Kåre Alsaker94659272012-11-13 19:10:18 +0100141get_output_state(struct weston_output *output)
142{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100143 return (struct gl_output_state *)output->renderer_state;
John Kåre Alsaker94659272012-11-13 19:10:18 +0100144}
145
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300146static int
147gl_renderer_create_surface(struct weston_surface *surface);
148
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100149static inline struct gl_surface_state *
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100150get_surface_state(struct weston_surface *surface)
151{
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300152 if (!surface->renderer_state)
153 gl_renderer_create_surface(surface);
154
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100155 return (struct gl_surface_state *)surface->renderer_state;
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100156}
157
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100158static inline struct gl_renderer *
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100159get_renderer(struct weston_compositor *ec)
160{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100161 return (struct gl_renderer *)ec->renderer;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100162}
163
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400164static const char *
165egl_error_string(EGLint code)
166{
167#define MYERRCODE(x) case x: return #x;
168 switch (code) {
169 MYERRCODE(EGL_SUCCESS)
170 MYERRCODE(EGL_NOT_INITIALIZED)
171 MYERRCODE(EGL_BAD_ACCESS)
172 MYERRCODE(EGL_BAD_ALLOC)
173 MYERRCODE(EGL_BAD_ATTRIBUTE)
174 MYERRCODE(EGL_BAD_CONTEXT)
175 MYERRCODE(EGL_BAD_CONFIG)
176 MYERRCODE(EGL_BAD_CURRENT_SURFACE)
177 MYERRCODE(EGL_BAD_DISPLAY)
178 MYERRCODE(EGL_BAD_SURFACE)
179 MYERRCODE(EGL_BAD_MATCH)
180 MYERRCODE(EGL_BAD_PARAMETER)
181 MYERRCODE(EGL_BAD_NATIVE_PIXMAP)
182 MYERRCODE(EGL_BAD_NATIVE_WINDOW)
183 MYERRCODE(EGL_CONTEXT_LOST)
184 default:
185 return "unknown";
186 }
187#undef MYERRCODE
188}
189
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300190static void
Pekka Paalanen326529f2012-11-27 12:25:25 +0200191gl_renderer_print_egl_error_state(void)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400192{
193 EGLint code;
194
195 code = eglGetError();
196 weston_log("EGL error state: %s (0x%04lx)\n",
197 egl_error_string(code), (long)code);
198}
199
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400200#define max(a, b) (((a) > (b)) ? (a) : (b))
201#define min(a, b) (((a) > (b)) ? (b) : (a))
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400202
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300203/*
204 * Compute the boundary vertices of the intersection of the global coordinate
205 * aligned rectangle 'rect', and an arbitrary quadrilateral produced from
206 * 'surf_rect' when transformed from surface coordinates into global coordinates.
207 * The vertices are written to 'ex' and 'ey', and the return value is the
208 * number of vertices. Vertices are produced in clockwise winding order.
209 * Guarantees to produce either zero vertices, or 3-8 vertices with non-zero
210 * polygon area.
211 */
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400212static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500213calculate_edges(struct weston_view *ev, pixman_box32_t *rect,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400214 pixman_box32_t *surf_rect, GLfloat *ex, GLfloat *ey)
215{
Sam Spilsbury619859c2013-09-13 10:01:21 +0800216
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300217 struct clip_context ctx;
218 int i, n;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400219 GLfloat min_x, max_x, min_y, max_y;
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300220 struct polygon8 surf = {
221 { surf_rect->x1, surf_rect->x2, surf_rect->x2, surf_rect->x1 },
222 { surf_rect->y1, surf_rect->y1, surf_rect->y2, surf_rect->y2 },
223 4
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400224 };
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400225
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300226 ctx.clip.x1 = rect->x1;
227 ctx.clip.y1 = rect->y1;
228 ctx.clip.x2 = rect->x2;
229 ctx.clip.y2 = rect->y2;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400230
231 /* transform surface to screen space: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300232 for (i = 0; i < surf.n; i++)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500233 weston_view_to_global_float(ev, surf.x[i], surf.y[i],
234 &surf.x[i], &surf.y[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400235
236 /* find bounding box: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300237 min_x = max_x = surf.x[0];
238 min_y = max_y = surf.y[0];
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400239
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300240 for (i = 1; i < surf.n; i++) {
241 min_x = min(min_x, surf.x[i]);
242 max_x = max(max_x, surf.x[i]);
243 min_y = min(min_y, surf.y[i]);
244 max_y = max(max_y, surf.y[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400245 }
246
247 /* First, simple bounding box check to discard early transformed
248 * surface rects that do not intersect with the clip region:
249 */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300250 if ((min_x >= ctx.clip.x2) || (max_x <= ctx.clip.x1) ||
251 (min_y >= ctx.clip.y2) || (max_y <= ctx.clip.y1))
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400252 return 0;
253
254 /* Simple case, bounding box edges are parallel to surface edges,
255 * there will be only four edges. We just need to clip the surface
256 * vertices to the clip rect bounds:
257 */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500258 if (!ev->transform.enabled)
Sam Spilsbury619859c2013-09-13 10:01:21 +0800259 return clip_simple(&ctx, &surf, ex, ey);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400260
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300261 /* Transformed case: use a general polygon clipping algorithm to
262 * clip the surface rectangle with each side of 'rect'.
263 * The algorithm is Sutherland-Hodgman, as explained in
264 * http://www.codeguru.com/cpp/misc/misc/graphics/article.php/c8965/Polygon-Clipping.htm
265 * but without looking at any of that code.
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400266 */
Sam Spilsbury619859c2013-09-13 10:01:21 +0800267 n = clip_transformed(&ctx, &surf, ex, ey);
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300268
269 if (n < 3)
270 return 0;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400271
272 return n;
273}
274
275static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500276texture_region(struct weston_view *ev, pixman_region32_t *region,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400277 pixman_region32_t *surf_region)
278{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500279 struct gl_surface_state *gs = get_surface_state(ev->surface);
280 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400281 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400282 GLfloat *v, inv_width, inv_height;
283 unsigned int *vtxcnt, nvtx = 0;
284 pixman_box32_t *rects, *surf_rects;
285 int i, j, k, nrects, nsurf;
286
287 rects = pixman_region32_rectangles(region, &nrects);
288 surf_rects = pixman_region32_rectangles(surf_region, &nsurf);
289
290 /* worst case we can have 8 vertices per rect (ie. clipped into
291 * an octagon):
292 */
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400293 v = wl_array_add(&gr->vertices, nrects * nsurf * 8 * 4 * sizeof *v);
294 vtxcnt = wl_array_add(&gr->vtxcnt, nrects * nsurf * sizeof *vtxcnt);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400295
Pekka Paalanen68033ac2012-12-04 15:58:15 +0200296 inv_width = 1.0 / gs->pitch;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200297 inv_height = 1.0 / gs->height;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400298
299 for (i = 0; i < nrects; i++) {
300 pixman_box32_t *rect = &rects[i];
301 for (j = 0; j < nsurf; j++) {
302 pixman_box32_t *surf_rect = &surf_rects[j];
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200303 GLfloat sx, sy, bx, by;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400304 GLfloat ex[8], ey[8]; /* edge points in screen space */
305 int n;
306
307 /* The transformed surface, after clipping to the clip region,
308 * can have as many as eight sides, emitted as a triangle-fan.
309 * The first vertex in the triangle fan can be chosen arbitrarily,
310 * since the area is guaranteed to be convex.
311 *
312 * If a corner of the transformed surface falls outside of the
313 * clip region, instead of emitting one vertex for the corner
314 * of the surface, up to two are emitted for two corresponding
315 * intersection point(s) between the surface and the clip region.
316 *
317 * To do this, we first calculate the (up to eight) points that
318 * form the intersection of the clip rect and the transformed
319 * surface.
320 */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500321 n = calculate_edges(ev, rect, surf_rect, ex, ey);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400322 if (n < 3)
323 continue;
324
325 /* emit edge points: */
326 for (k = 0; k < n; k++) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500327 weston_view_from_global_float(ev, ex[k], ey[k],
328 &sx, &sy);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400329 /* position: */
330 *(v++) = ex[k];
331 *(v++) = ey[k];
332 /* texcoord: */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500333 weston_surface_to_buffer_float(ev->surface,
334 sx, sy,
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200335 &bx, &by);
336 *(v++) = bx * inv_width;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +0400337 if (gs->y_inverted) {
338 *(v++) = by * inv_height;
339 } else {
340 *(v++) = (gs->height - by) * inv_height;
341 }
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400342 }
343
344 vtxcnt[nvtx++] = n;
345 }
346 }
347
348 return nvtx;
349}
350
351static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500352triangle_fan_debug(struct weston_view *view, int first, int count)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400353{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500354 struct weston_compositor *compositor = view->surface->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100355 struct gl_renderer *gr = get_renderer(compositor);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400356 int i;
357 GLushort *buffer;
358 GLushort *index;
359 int nelems;
360 static int color_idx = 0;
361 static const GLfloat color[][4] = {
362 { 1.0, 0.0, 0.0, 1.0 },
363 { 0.0, 1.0, 0.0, 1.0 },
364 { 0.0, 0.0, 1.0, 1.0 },
365 { 1.0, 1.0, 1.0, 1.0 },
366 };
367
368 nelems = (count - 1 + count - 2) * 2;
369
370 buffer = malloc(sizeof(GLushort) * nelems);
371 index = buffer;
372
373 for (i = 1; i < count; i++) {
374 *index++ = first;
375 *index++ = first + i;
376 }
377
378 for (i = 2; i < count; i++) {
379 *index++ = first + i - 1;
380 *index++ = first + i;
381 }
382
John Kåre Alsaker40684142012-11-13 19:10:25 +0100383 glUseProgram(gr->solid_shader.program);
384 glUniform4fv(gr->solid_shader.color_uniform, 1,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400385 color[color_idx++ % ARRAY_LENGTH(color)]);
386 glDrawElements(GL_LINES, nelems, GL_UNSIGNED_SHORT, buffer);
John Kåre Alsaker40684142012-11-13 19:10:25 +0100387 glUseProgram(gr->current_shader->program);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400388 free(buffer);
389}
390
391static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500392repaint_region(struct weston_view *ev, pixman_region32_t *region,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400393 pixman_region32_t *surf_region)
394{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500395 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400396 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400397 GLfloat *v;
398 unsigned int *vtxcnt;
399 int i, first, nfans;
400
401 /* The final region to be painted is the intersection of
402 * 'region' and 'surf_region'. However, 'region' is in the global
403 * coordinates, and 'surf_region' is in the surface-local
404 * coordinates. texture_region() will iterate over all pairs of
405 * rectangles from both regions, compute the intersection
406 * polygon for each pair, and store it as a triangle fan if
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400407 * it has a non-zero area (at least 3 vertices1, actually).
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400408 */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500409 nfans = texture_region(ev, region, surf_region);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400410
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400411 v = gr->vertices.data;
412 vtxcnt = gr->vtxcnt.data;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400413
414 /* position: */
415 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
416 glEnableVertexAttribArray(0);
417
418 /* texcoord: */
419 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
420 glEnableVertexAttribArray(1);
421
422 for (i = 0, first = 0; i < nfans; i++) {
423 glDrawArrays(GL_TRIANGLE_FAN, first, vtxcnt[i]);
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400424 if (gr->fan_debug)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500425 triangle_fan_debug(ev, first, vtxcnt[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400426 first += vtxcnt[i];
427 }
428
429 glDisableVertexAttribArray(1);
430 glDisableVertexAttribArray(0);
431
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400432 gr->vertices.size = 0;
433 gr->vtxcnt.size = 0;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400434}
435
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100436static int
437use_output(struct weston_output *output)
438{
439 static int errored;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100440 struct gl_output_state *go = get_output_state(output);
441 struct gl_renderer *gr = get_renderer(output->compositor);
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100442 EGLBoolean ret;
443
444 ret = eglMakeCurrent(gr->egl_display, go->egl_surface,
445 go->egl_surface, gr->egl_context);
446
447 if (ret == EGL_FALSE) {
448 if (errored)
449 return -1;
450 errored = 1;
451 weston_log("Failed to make EGL context current.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +0200452 gl_renderer_print_egl_error_state();
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100453 return -1;
454 }
455
456 return 0;
457}
458
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +0300459static int
460shader_init(struct gl_shader *shader, struct gl_renderer *gr,
461 const char *vertex_source, const char *fragment_source);
462
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400463static void
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +0300464use_shader(struct gl_renderer *gr, struct gl_shader *shader)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400465{
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +0300466 if (!shader->program) {
467 int ret;
468
469 ret = shader_init(shader, gr,
470 shader->vertex_source,
471 shader->fragment_source);
472
473 if (ret < 0)
474 weston_log("warning: failed to compile shader\n");
475 }
476
John Kåre Alsaker40684142012-11-13 19:10:25 +0100477 if (gr->current_shader == shader)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400478 return;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400479 glUseProgram(shader->program);
John Kåre Alsaker40684142012-11-13 19:10:25 +0100480 gr->current_shader = shader;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400481}
482
483static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100484shader_uniforms(struct gl_shader *shader,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500485 struct weston_view *view,
486 struct weston_output *output)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400487{
488 int i;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500489 struct gl_surface_state *gs = get_surface_state(view->surface);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400490
491 glUniformMatrix4fv(shader->proj_uniform,
492 1, GL_FALSE, output->matrix.d);
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100493 glUniform4fv(shader->color_uniform, 1, gs->color);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500494 glUniform1f(shader->alpha_uniform, view->alpha);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400495
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100496 for (i = 0; i < gs->num_textures; i++)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400497 glUniform1i(shader->tex_uniforms[i], i);
498}
499
500static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500501draw_view(struct weston_view *ev, struct weston_output *output,
502 pixman_region32_t *damage) /* in global coordinates */
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400503{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500504 struct weston_compositor *ec = ev->surface->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100505 struct gl_renderer *gr = get_renderer(ec);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500506 struct gl_surface_state *gs = get_surface_state(ev->surface);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400507 /* repaint bounding region in global coordinates: */
508 pixman_region32_t repaint;
509 /* non-opaque region in surface coordinates: */
510 pixman_region32_t surface_blend;
511 GLint filter;
512 int i;
513
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +0200514 /* In case of a runtime switch of renderers, we may not have received
515 * an attach for this surface since the switch. In that case we don't
516 * have a valid buffer or a proper shader set up so skip rendering. */
517 if (!gs->shader)
518 return;
519
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400520 pixman_region32_init(&repaint);
521 pixman_region32_intersect(&repaint,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500522 &ev->transform.boundingbox, damage);
523 pixman_region32_subtract(&repaint, &repaint, &ev->clip);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400524
525 if (!pixman_region32_not_empty(&repaint))
526 goto out;
527
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400528 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
529
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400530 if (gr->fan_debug) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100531 use_shader(gr, &gr->solid_shader);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500532 shader_uniforms(&gr->solid_shader, ev, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400533 }
534
John Kåre Alsaker40684142012-11-13 19:10:25 +0100535 use_shader(gr, gs->shader);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500536 shader_uniforms(gs->shader, ev, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400537
Jason Ekstranda7af7042013-10-12 22:38:11 -0500538 if (ev->transform.enabled || output->zoom.active ||
539 output->current_scale != ev->surface->buffer_scale)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400540 filter = GL_LINEAR;
541 else
542 filter = GL_NEAREST;
543
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100544 for (i = 0; i < gs->num_textures; i++) {
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400545 glActiveTexture(GL_TEXTURE0 + i);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100546 glBindTexture(gs->target, gs->textures[i]);
547 glTexParameteri(gs->target, GL_TEXTURE_MIN_FILTER, filter);
548 glTexParameteri(gs->target, GL_TEXTURE_MAG_FILTER, filter);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400549 }
550
551 /* blended region is whole surface minus opaque region: */
552 pixman_region32_init_rect(&surface_blend, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500553 ev->geometry.width, ev->geometry.height);
554 pixman_region32_subtract(&surface_blend, &surface_blend, &ev->surface->opaque);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400555
Jason Ekstranda7af7042013-10-12 22:38:11 -0500556 /* XXX: Should we be using ev->transform.opaque here? */
557 if (pixman_region32_not_empty(&ev->surface->opaque)) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100558 if (gs->shader == &gr->texture_shader_rgba) {
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400559 /* Special case for RGBA textures with possibly
560 * bad data in alpha channel: use the shader
561 * that forces texture alpha = 1.0.
562 * Xwayland surfaces need this.
563 */
John Kåre Alsaker40684142012-11-13 19:10:25 +0100564 use_shader(gr, &gr->texture_shader_rgbx);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500565 shader_uniforms(&gr->texture_shader_rgbx, ev, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400566 }
567
Jason Ekstranda7af7042013-10-12 22:38:11 -0500568 if (ev->alpha < 1.0)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400569 glEnable(GL_BLEND);
570 else
571 glDisable(GL_BLEND);
572
Jason Ekstranda7af7042013-10-12 22:38:11 -0500573 repaint_region(ev, &repaint, &ev->surface->opaque);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400574 }
575
576 if (pixman_region32_not_empty(&surface_blend)) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100577 use_shader(gr, gs->shader);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400578 glEnable(GL_BLEND);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500579 repaint_region(ev, &repaint, &surface_blend);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400580 }
581
582 pixman_region32_fini(&surface_blend);
583
584out:
585 pixman_region32_fini(&repaint);
586}
587
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400588static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500589repaint_views(struct weston_output *output, pixman_region32_t *damage)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400590{
591 struct weston_compositor *compositor = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500592 struct weston_view *view;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400593
Jason Ekstranda7af7042013-10-12 22:38:11 -0500594 wl_list_for_each_reverse(view, &compositor->view_list, link)
595 if (view->plane == &compositor->primary_plane)
596 draw_view(view, output, damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400597}
598
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500599static void
600draw_output_border_texture(struct gl_border_image *img, int32_t x, int32_t y,
601 int32_t width, int32_t height)
602{
603 static GLushort indices [] = { 0, 1, 3, 3, 1, 2 };
604
605 if (!img->data) {
606 if (img->tex) {
607 glDeleteTextures(1, &img->tex);
608 img->tex = 0;
609 }
610
611 return;
612 }
613
614 if (!img->tex) {
615 glGenTextures(1, &img->tex);
616 glBindTexture(GL_TEXTURE_2D, img->tex);
617
618 glTexParameteri(GL_TEXTURE_2D,
619 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
620 glTexParameteri(GL_TEXTURE_2D,
621 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
622 glTexParameteri(GL_TEXTURE_2D,
623 GL_TEXTURE_MIN_FILTER, GL_NEAREST);
624 glTexParameteri(GL_TEXTURE_2D,
625 GL_TEXTURE_MAG_FILTER, GL_NEAREST);
626 } else {
627 glBindTexture(GL_TEXTURE_2D, img->tex);
628 }
629
630 if (img->dirty) {
631#ifdef GL_EXT_unpack_subimage
632 glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, 0);
633 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, 0);
634 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, 0);
635#endif
636 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
637 img->tex_width, img->height, 0,
638 GL_BGRA_EXT, GL_UNSIGNED_BYTE, img->data);
639 img->dirty = 0;
640 }
641
642 GLfloat texcoord[] = {
643 0.0f, 0.0f,
644 (GLfloat)img->width / (GLfloat)img->tex_width, 0.0f,
645 (GLfloat)img->width / (GLfloat)img->tex_width, 1.0f,
646 0.0f, 1.0f,
647 };
648
649 GLfloat verts[] = {
650 x, y,
651 x + width, y,
652 x + width, y + height,
653 x, y + height
654 };
655
656 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, verts);
657 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, texcoord);
658 glEnableVertexAttribArray(0);
659 glEnableVertexAttribArray(1);
660
661 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
662
663 glDisableVertexAttribArray(1);
664 glDisableVertexAttribArray(0);
665}
666
667static void
668draw_output_border(struct weston_output *output)
669{
670 struct gl_output_state *go = get_output_state(output);
671 struct gl_renderer *gr = get_renderer(output->compositor);
672 struct gl_shader *shader = &gr->texture_shader_rgba;
Jason Ekstrand00b84282013-10-27 22:24:59 -0500673 struct gl_border_image *top, *bottom, *left, *right;
674 struct weston_matrix matrix;
675 int full_width, full_height;
676
677 top = &go->borders[GL_RENDERER_BORDER_TOP];
678 bottom = &go->borders[GL_RENDERER_BORDER_BOTTOM];
679 left = &go->borders[GL_RENDERER_BORDER_LEFT];
680 right = &go->borders[GL_RENDERER_BORDER_RIGHT];
681
682 full_width = output->current_mode->width + left->width + right->width;
683 full_height = output->current_mode->height + top->height + bottom->height;
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500684
685 glDisable(GL_BLEND);
686 use_shader(gr, shader);
687
Jason Ekstrand00b84282013-10-27 22:24:59 -0500688 glViewport(0, 0, full_width, full_height);
689
690 weston_matrix_init(&matrix);
691 weston_matrix_translate(&matrix, -full_width/2.0, -full_height/2.0, 0);
692 weston_matrix_scale(&matrix, 2.0/full_width, -2.0/full_height, 1);
693 glUniformMatrix4fv(shader->proj_uniform, 1, GL_FALSE, matrix.d);
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500694
695 glUniform1i(shader->tex_uniforms[0], 0);
696 glUniform1f(shader->alpha_uniform, 1);
697 glActiveTexture(GL_TEXTURE0);
698
Jason Ekstrand00b84282013-10-27 22:24:59 -0500699 draw_output_border_texture(top,
700 0, 0,
701 full_width, top->height);
702 draw_output_border_texture(left,
703 0, top->height,
704 left->width, output->current_mode->height);
705 draw_output_border_texture(right,
706 full_width - right->width, top->height,
707 right->width, output->current_mode->height);
708 draw_output_border_texture(bottom,
709 0, full_height - bottom->height,
710 full_width, bottom->height);
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500711}
John Kåre Alsaker44154502012-11-13 19:10:20 +0100712
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400713static void
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +0200714output_get_buffer_damage(struct weston_output *output,
715 pixman_region32_t *buffer_damage)
716{
717 struct gl_output_state *go = get_output_state(output);
718 struct gl_renderer *gr = get_renderer(output->compositor);
719 EGLint buffer_age = 0;
720 EGLBoolean ret;
721 int i;
722
723 if (gr->has_egl_buffer_age) {
724 ret = eglQuerySurface(gr->egl_display, go->egl_surface,
725 EGL_BUFFER_AGE_EXT, &buffer_age);
726 if (ret == EGL_FALSE) {
727 weston_log("buffer age query failed.\n");
728 gl_renderer_print_egl_error_state();
729 }
730 }
731
732 if (buffer_age == 0 || buffer_age - 1 > BUFFER_DAMAGE_COUNT)
733 pixman_region32_copy(buffer_damage, &output->region);
734 else
735 for (i = 0; i < buffer_age - 1; i++)
736 pixman_region32_union(buffer_damage, buffer_damage,
737 &go->buffer_damage[i]);
738}
739
740static void
741output_rotate_damage(struct weston_output *output,
742 pixman_region32_t *output_damage)
743{
744 struct gl_output_state *go = get_output_state(output);
745 struct gl_renderer *gr = get_renderer(output->compositor);
746 int i;
747
748 if (!gr->has_egl_buffer_age)
749 return;
750
751 for (i = BUFFER_DAMAGE_COUNT - 1; i >= 1; i--)
752 pixman_region32_copy(&go->buffer_damage[i],
753 &go->buffer_damage[i - 1]);
754
755 pixman_region32_copy(&go->buffer_damage[0], output_damage);
756}
757
758static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100759gl_renderer_repaint_output(struct weston_output *output,
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400760 pixman_region32_t *output_damage)
761{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100762 struct gl_output_state *go = get_output_state(output);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400763 struct weston_compositor *compositor = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100764 struct gl_renderer *gr = get_renderer(compositor);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400765 EGLBoolean ret;
766 static int errored;
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +0200767 pixman_region32_t buffer_damage, total_damage;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400768
Jason Ekstrand00b84282013-10-27 22:24:59 -0500769 /* Calculate the viewport */
770 glViewport(go->borders[GL_RENDERER_BORDER_LEFT].width,
771 go->borders[GL_RENDERER_BORDER_BOTTOM].height,
772 output->current_mode->width,
773 output->current_mode->height);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400774
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100775 if (use_output(output) < 0)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400776 return;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400777
778 /* if debugging, redraw everything outside the damage to clean up
779 * debug lines from the previous draw on this buffer:
780 */
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400781 if (gr->fan_debug) {
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400782 pixman_region32_t undamaged;
783 pixman_region32_init(&undamaged);
784 pixman_region32_subtract(&undamaged, &output->region,
785 output_damage);
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400786 gr->fan_debug = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500787 repaint_views(output, &undamaged);
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400788 gr->fan_debug = 1;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400789 pixman_region32_fini(&undamaged);
790 }
791
Ander Conselvan de Oliveirab605c062013-03-05 17:30:28 +0200792 pixman_region32_init(&total_damage);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +0200793 pixman_region32_init(&buffer_damage);
794
795 output_get_buffer_damage(output, &buffer_damage);
796 output_rotate_damage(output, output_damage);
797
798 pixman_region32_union(&total_damage, &buffer_damage, output_damage);
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300799
Jason Ekstranda7af7042013-10-12 22:38:11 -0500800 repaint_views(output, &total_damage);
Ander Conselvan de Oliveirab605c062013-03-05 17:30:28 +0200801
802 pixman_region32_fini(&total_damage);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +0200803 pixman_region32_fini(&buffer_damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400804
Jason Ekstrand310382f2013-10-27 22:24:56 -0500805 draw_output_border(output);
John Kåre Alsaker44154502012-11-13 19:10:20 +0100806
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +0200807 pixman_region32_copy(&output->previous_damage, output_damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400808 wl_signal_emit(&output->frame_signal, output);
809
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100810 ret = eglSwapBuffers(gr->egl_display, go->egl_surface);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400811 if (ret == EGL_FALSE && !errored) {
812 errored = 1;
813 weston_log("Failed in eglSwapBuffers.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +0200814 gl_renderer_print_egl_error_state();
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400815 }
816
817}
Kristian Høgsberg25894fc2012-09-05 22:06:26 -0400818
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100819static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100820gl_renderer_read_pixels(struct weston_output *output,
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100821 pixman_format_code_t format, void *pixels,
822 uint32_t x, uint32_t y,
823 uint32_t width, uint32_t height)
824{
825 GLenum gl_format;
826
827 switch (format) {
828 case PIXMAN_a8r8g8b8:
829 gl_format = GL_BGRA_EXT;
830 break;
831 case PIXMAN_a8b8g8r8:
832 gl_format = GL_RGBA;
833 break;
834 default:
835 return -1;
836 }
837
838 if (use_output(output) < 0)
839 return -1;
840
841 glPixelStorei(GL_PACK_ALIGNMENT, 1);
842 glReadPixels(x, y, width, height, gl_format,
843 GL_UNSIGNED_BYTE, pixels);
844
845 return 0;
846}
847
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400848static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100849gl_renderer_flush_damage(struct weston_surface *surface)
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -0400850{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100851 struct gl_renderer *gr = get_renderer(surface->compositor);
852 struct gl_surface_state *gs = get_surface_state(surface);
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500853 struct weston_buffer *buffer = gs->buffer_ref.buffer;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500854 struct weston_view *view;
855 int texture_used;
Tomeu Vizoso12072b62013-08-06 20:05:55 +0200856 GLenum format;
857 int pixel_type;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100858
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -0700859#ifdef GL_EXT_unpack_subimage
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -0400860 pixman_box32_t *rectangles;
861 void *data;
862 int i, n;
863#endif
864
Pekka Paalanen81ee3f52012-12-04 15:58:16 +0200865 pixman_region32_union(&gs->texture_damage,
866 &gs->texture_damage, &surface->damage);
Pekka Paalanenbcdd5792012-11-07 12:25:13 +0200867
Pekka Paalanenfb003d32012-12-04 15:58:13 +0200868 if (!buffer)
869 return;
870
Pekka Paalanenbcdd5792012-11-07 12:25:13 +0200871 /* Avoid upload, if the texture won't be used this time.
Pekka Paalanenfb003d32012-12-04 15:58:13 +0200872 * We still accumulate the damage in texture_damage, and
873 * hold the reference to the buffer, in case the surface
874 * migrates back to the primary plane.
Pekka Paalanenbcdd5792012-11-07 12:25:13 +0200875 */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500876 texture_used = 0;
877 wl_list_for_each(view, &surface->views, surface_link) {
878 if (view->plane == &surface->compositor->primary_plane) {
879 texture_used = 1;
880 break;
881 }
882 }
883 if (!texture_used)
Pekka Paalanenbcdd5792012-11-07 12:25:13 +0200884 return;
885
Pekka Paalanen81ee3f52012-12-04 15:58:16 +0200886 if (!pixman_region32_not_empty(&gs->texture_damage))
Pekka Paalanenfb003d32012-12-04 15:58:13 +0200887 goto done;
Pekka Paalanenbcdd5792012-11-07 12:25:13 +0200888
Tomeu Vizoso12072b62013-08-06 20:05:55 +0200889 switch (wl_shm_buffer_get_format(buffer->shm_buffer)) {
890 case WL_SHM_FORMAT_XRGB8888:
891 case WL_SHM_FORMAT_ARGB8888:
892 format = GL_BGRA_EXT;
893 pixel_type = GL_UNSIGNED_BYTE;
894 break;
895 case WL_SHM_FORMAT_RGB565:
896 format = GL_RGB;
897 pixel_type = GL_UNSIGNED_SHORT_5_6_5;
898 break;
899 default:
900 weston_log("warning: unknown shm buffer format\n");
901 format = GL_BGRA_EXT;
902 pixel_type = GL_UNSIGNED_BYTE;
903 }
904
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100905 glBindTexture(GL_TEXTURE_2D, gs->textures[0]);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -0400906
John Kåre Alsaker320711d2012-11-13 19:10:27 +0100907 if (!gr->has_unpack_subimage) {
Neil Robertse5051712013-11-13 15:44:06 +0000908 wl_shm_buffer_begin_access(buffer->shm_buffer);
Tomeu Vizoso12072b62013-08-06 20:05:55 +0200909 glTexImage2D(GL_TEXTURE_2D, 0, format,
Pekka Paalanen68033ac2012-12-04 15:58:15 +0200910 gs->pitch, buffer->height, 0,
Tomeu Vizoso12072b62013-08-06 20:05:55 +0200911 format, pixel_type,
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500912 wl_shm_buffer_get_data(buffer->shm_buffer));
Neil Robertse5051712013-11-13 15:44:06 +0000913 wl_shm_buffer_end_access(buffer->shm_buffer);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -0400914
Pekka Paalanenbcdd5792012-11-07 12:25:13 +0200915 goto done;
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -0400916 }
917
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -0700918#ifdef GL_EXT_unpack_subimage
919 glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, gs->pitch);
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500920 data = wl_shm_buffer_get_data(buffer->shm_buffer);
Ander Conselvan de Oliveira6be5f432013-06-07 16:52:45 +0300921
922 if (gs->needs_full_upload) {
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -0700923 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, 0);
924 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, 0);
Neil Robertse5051712013-11-13 15:44:06 +0000925 wl_shm_buffer_begin_access(buffer->shm_buffer);
Ander Conselvan de Oliveira6be5f432013-06-07 16:52:45 +0300926 glTexSubImage2D(GL_TEXTURE_2D, 0,
927 0, 0, gs->pitch, buffer->height,
Tomeu Vizoso12072b62013-08-06 20:05:55 +0200928 format, pixel_type, data);
Neil Robertse5051712013-11-13 15:44:06 +0000929 wl_shm_buffer_end_access(buffer->shm_buffer);
Ander Conselvan de Oliveira6be5f432013-06-07 16:52:45 +0300930 goto done;
931 }
932
Pekka Paalanen81ee3f52012-12-04 15:58:16 +0200933 rectangles = pixman_region32_rectangles(&gs->texture_damage, &n);
Neil Robertse5051712013-11-13 15:44:06 +0000934 wl_shm_buffer_begin_access(buffer->shm_buffer);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -0400935 for (i = 0; i < n; i++) {
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200936 pixman_box32_t r;
937
938 r = weston_surface_to_buffer_rect(surface, rectangles[i]);
939
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -0700940 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, r.x1);
941 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, r.y1);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200942 glTexSubImage2D(GL_TEXTURE_2D, 0, r.x1, r.y1,
943 r.x2 - r.x1, r.y2 - r.y1,
Tomeu Vizoso12072b62013-08-06 20:05:55 +0200944 format, pixel_type, data);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -0400945 }
Neil Robertse5051712013-11-13 15:44:06 +0000946 wl_shm_buffer_end_access(buffer->shm_buffer);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -0400947#endif
Pekka Paalanenbcdd5792012-11-07 12:25:13 +0200948
949done:
Pekka Paalanen81ee3f52012-12-04 15:58:16 +0200950 pixman_region32_fini(&gs->texture_damage);
951 pixman_region32_init(&gs->texture_damage);
Ander Conselvan de Oliveira6be5f432013-06-07 16:52:45 +0300952 gs->needs_full_upload = 0;
Pekka Paalanenfb003d32012-12-04 15:58:13 +0200953
954 weston_buffer_reference(&gs->buffer_ref, NULL);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -0400955}
956
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400957static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100958ensure_textures(struct gl_surface_state *gs, int num_textures)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400959{
960 int i;
961
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100962 if (num_textures <= gs->num_textures)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400963 return;
964
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100965 for (i = gs->num_textures; i < num_textures; i++) {
966 glGenTextures(1, &gs->textures[i]);
967 glBindTexture(gs->target, gs->textures[i]);
968 glTexParameteri(gs->target,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400969 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100970 glTexParameteri(gs->target,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400971 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
972 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100973 gs->num_textures = num_textures;
974 glBindTexture(gs->target, 0);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400975}
976
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400977static void
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +0300978gl_renderer_attach_shm(struct weston_surface *es, struct weston_buffer *buffer,
979 struct wl_shm_buffer *shm_buffer)
980{
981 struct weston_compositor *ec = es->compositor;
982 struct gl_renderer *gr = get_renderer(ec);
983 struct gl_surface_state *gs = get_surface_state(es);
Tomeu Vizoso12072b62013-08-06 20:05:55 +0200984 int pitch;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +0300985
986 buffer->shm_buffer = shm_buffer;
987 buffer->width = wl_shm_buffer_get_width(shm_buffer);
988 buffer->height = wl_shm_buffer_get_height(shm_buffer);
989
Tomeu Vizoso12072b62013-08-06 20:05:55 +0200990 switch (wl_shm_buffer_get_format(shm_buffer)) {
991 case WL_SHM_FORMAT_XRGB8888:
992 gs->shader = &gr->texture_shader_rgbx;
993 pitch = wl_shm_buffer_get_stride(shm_buffer) / 4;
994 break;
995 case WL_SHM_FORMAT_ARGB8888:
996 gs->shader = &gr->texture_shader_rgba;
997 pitch = wl_shm_buffer_get_stride(shm_buffer) / 4;
998 break;
999 case WL_SHM_FORMAT_RGB565:
1000 gs->shader = &gr->texture_shader_rgbx;
1001 pitch = wl_shm_buffer_get_stride(shm_buffer) / 2;
1002 break;
1003 default:
1004 weston_log("warning: unknown shm buffer format\n");
1005 gs->shader = &gr->texture_shader_rgba;
1006 pitch = wl_shm_buffer_get_stride(shm_buffer) / 4;
1007 }
1008
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001009 /* Only allocate a texture if it doesn't match existing one.
1010 * If a switch from DRM allocated buffer to a SHM buffer is
1011 * happening, we need to allocate a new texture buffer. */
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001012 if (pitch != gs->pitch ||
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001013 buffer->height != gs->height ||
1014 gs->buffer_type != BUFFER_TYPE_SHM) {
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001015 gs->pitch = pitch;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001016 gs->height = buffer->height;
1017 gs->target = GL_TEXTURE_2D;
1018 gs->buffer_type = BUFFER_TYPE_SHM;
1019 gs->needs_full_upload = 1;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001020 gs->y_inverted = 1;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001021
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03001022 gs->surface = es;
1023
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001024 ensure_textures(gs, 1);
1025 glBindTexture(GL_TEXTURE_2D, gs->textures[0]);
1026 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1027 gs->pitch, buffer->height, 0,
1028 GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
1029 }
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001030}
1031
1032static void
1033gl_renderer_attach_egl(struct weston_surface *es, struct weston_buffer *buffer,
1034 uint32_t format)
1035{
1036 struct weston_compositor *ec = es->compositor;
1037 struct gl_renderer *gr = get_renderer(ec);
1038 struct gl_surface_state *gs = get_surface_state(es);
1039 EGLint attribs[3];
1040 int i, num_planes;
1041
1042 buffer->legacy_buffer = (struct wl_buffer *)buffer->resource;
1043 gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1044 EGL_WIDTH, &buffer->width);
1045 gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1046 EGL_HEIGHT, &buffer->height);
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001047 gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1048 EGL_WAYLAND_Y_INVERTED_WL, &buffer->y_inverted);
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001049
1050 for (i = 0; i < gs->num_images; i++)
1051 gr->destroy_image(gr->egl_display, gs->images[i]);
1052 gs->num_images = 0;
1053 gs->target = GL_TEXTURE_2D;
1054 switch (format) {
1055 case EGL_TEXTURE_RGB:
1056 case EGL_TEXTURE_RGBA:
1057 default:
1058 num_planes = 1;
1059 gs->shader = &gr->texture_shader_rgba;
1060 break;
1061 case EGL_TEXTURE_EXTERNAL_WL:
1062 num_planes = 1;
1063 gs->target = GL_TEXTURE_EXTERNAL_OES;
1064 gs->shader = &gr->texture_shader_egl_external;
1065 break;
1066 case EGL_TEXTURE_Y_UV_WL:
1067 num_planes = 2;
1068 gs->shader = &gr->texture_shader_y_uv;
1069 break;
1070 case EGL_TEXTURE_Y_U_V_WL:
1071 num_planes = 3;
1072 gs->shader = &gr->texture_shader_y_u_v;
1073 break;
1074 case EGL_TEXTURE_Y_XUXV_WL:
1075 num_planes = 2;
1076 gs->shader = &gr->texture_shader_y_xuxv;
1077 break;
1078 }
1079
1080 ensure_textures(gs, num_planes);
1081 for (i = 0; i < num_planes; i++) {
1082 attribs[0] = EGL_WAYLAND_PLANE_WL;
1083 attribs[1] = i;
1084 attribs[2] = EGL_NONE;
1085 gs->images[i] = gr->create_image(gr->egl_display,
1086 NULL,
1087 EGL_WAYLAND_BUFFER_WL,
1088 buffer->legacy_buffer,
1089 attribs);
1090 if (!gs->images[i]) {
1091 weston_log("failed to create img for plane %d\n", i);
1092 continue;
1093 }
1094 gs->num_images++;
1095
1096 glActiveTexture(GL_TEXTURE0 + i);
1097 glBindTexture(gs->target, gs->textures[i]);
1098 gr->image_target_texture_2d(gs->target,
1099 gs->images[i]);
1100 }
1101
1102 gs->pitch = buffer->width;
1103 gs->height = buffer->height;
1104 gs->buffer_type = BUFFER_TYPE_EGL;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001105 gs->y_inverted = buffer->y_inverted;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001106}
1107
1108static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001109gl_renderer_attach(struct weston_surface *es, struct weston_buffer *buffer)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001110{
1111 struct weston_compositor *ec = es->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001112 struct gl_renderer *gr = get_renderer(ec);
1113 struct gl_surface_state *gs = get_surface_state(es);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001114 struct wl_shm_buffer *shm_buffer;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001115 EGLint format;
1116 int i;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001117
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001118 weston_buffer_reference(&gs->buffer_ref, buffer);
1119
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001120 if (!buffer) {
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001121 for (i = 0; i < gs->num_images; i++) {
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001122 gr->destroy_image(gr->egl_display, gs->images[i]);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001123 gs->images[i] = NULL;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001124 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001125 gs->num_images = 0;
1126 glDeleteTextures(gs->num_textures, gs->textures);
1127 gs->num_textures = 0;
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +03001128 gs->buffer_type = BUFFER_TYPE_NULL;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001129 gs->y_inverted = 1;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001130 return;
1131 }
1132
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001133 shm_buffer = wl_shm_buffer_get(buffer->resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001134
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001135 if (shm_buffer)
1136 gl_renderer_attach_shm(es, buffer, shm_buffer);
Kristian Høgsberg47229392013-08-07 11:59:54 -07001137 else if (gr->query_buffer(gr->egl_display, (void *) buffer->resource,
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001138 EGL_TEXTURE_FORMAT, &format))
1139 gl_renderer_attach_egl(es, buffer, format);
1140 else {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001141 weston_log("unhandled buffer type!\n");
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001142 weston_buffer_reference(&gs->buffer_ref, NULL);
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +03001143 gs->buffer_type = BUFFER_TYPE_NULL;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001144 gs->y_inverted = 1;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001145 }
1146}
1147
Kristian Høgsberg42263852012-09-06 21:59:29 -04001148static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001149gl_renderer_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001150 float red, float green, float blue, float alpha)
1151{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001152 struct gl_surface_state *gs = get_surface_state(surface);
1153 struct gl_renderer *gr = get_renderer(surface->compositor);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001154
1155 gs->color[0] = red;
1156 gs->color[1] = green;
1157 gs->color[2] = blue;
1158 gs->color[3] = alpha;
1159
John Kåre Alsaker40684142012-11-13 19:10:25 +01001160 gs->shader = &gr->solid_shader;
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001161}
1162
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03001163static void
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03001164surface_state_destroy(struct gl_surface_state *gs, struct gl_renderer *gr)
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03001165{
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03001166 int i;
1167
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03001168 wl_list_remove(&gs->surface_destroy_listener.link);
1169 wl_list_remove(&gs->renderer_destroy_listener.link);
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03001170
1171 gs->surface->renderer_state = NULL;
1172
1173 glDeleteTextures(gs->num_textures, gs->textures);
1174
1175 for (i = 0; i < gs->num_images; i++)
1176 gr->destroy_image(gr->egl_display, gs->images[i]);
1177
1178 weston_buffer_reference(&gs->buffer_ref, NULL);
1179 pixman_region32_fini(&gs->texture_damage);
1180 free(gs);
1181}
1182
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03001183static void
1184surface_state_handle_surface_destroy(struct wl_listener *listener, void *data)
1185{
1186 struct gl_surface_state *gs;
1187 struct gl_renderer *gr;
1188
1189 gs = container_of(listener, struct gl_surface_state,
1190 surface_destroy_listener);
1191
1192 gr = get_renderer(gs->surface->compositor);
1193
1194 surface_state_destroy(gs, gr);
1195}
1196
1197static void
1198surface_state_handle_renderer_destroy(struct wl_listener *listener, void *data)
1199{
1200 struct gl_surface_state *gs;
1201 struct gl_renderer *gr;
1202
1203 gr = data;
1204
1205 gs = container_of(listener, struct gl_surface_state,
1206 renderer_destroy_listener);
1207
1208 surface_state_destroy(gs, gr);
1209}
1210
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001211static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001212gl_renderer_create_surface(struct weston_surface *surface)
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001213{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001214 struct gl_surface_state *gs;
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03001215 struct gl_renderer *gr = get_renderer(surface->compositor);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001216
1217 gs = calloc(1, sizeof *gs);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001218 if (!gs)
1219 return -1;
1220
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001221 /* A buffer is never attached to solid color surfaces, yet
1222 * they still go through texcoord computations. Do not divide
1223 * by zero there.
1224 */
1225 gs->pitch = 1;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001226 gs->y_inverted = 1;
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001227
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03001228 gs->surface = surface;
1229
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001230 pixman_region32_init(&gs->texture_damage);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001231 surface->renderer_state = gs;
1232
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03001233 gs->surface_destroy_listener.notify =
1234 surface_state_handle_surface_destroy;
1235 wl_signal_add(&surface->destroy_signal,
1236 &gs->surface_destroy_listener);
1237
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03001238 gs->renderer_destroy_listener.notify =
1239 surface_state_handle_renderer_destroy;
1240 wl_signal_add(&gr->destroy_signal,
1241 &gs->renderer_destroy_listener);
1242
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001243 return 0;
1244}
1245
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001246static const char vertex_shader[] =
1247 "uniform mat4 proj;\n"
1248 "attribute vec2 position;\n"
1249 "attribute vec2 texcoord;\n"
1250 "varying vec2 v_texcoord;\n"
1251 "void main()\n"
1252 "{\n"
1253 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
1254 " v_texcoord = texcoord;\n"
1255 "}\n";
1256
1257/* Declare common fragment shader uniforms */
1258#define FRAGMENT_CONVERT_YUV \
1259 " y *= alpha;\n" \
1260 " u *= alpha;\n" \
1261 " v *= alpha;\n" \
1262 " gl_FragColor.r = y + 1.59602678 * v;\n" \
1263 " gl_FragColor.g = y - 0.39176229 * u - 0.81296764 * v;\n" \
1264 " gl_FragColor.b = y + 2.01723214 * u;\n" \
1265 " gl_FragColor.a = alpha;\n"
1266
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001267static const char fragment_debug[] =
1268 " gl_FragColor = vec4(0.0, 0.3, 0.0, 0.2) + gl_FragColor * 0.8;\n";
1269
1270static const char fragment_brace[] =
1271 "}\n";
1272
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001273static const char texture_fragment_shader_rgba[] =
1274 "precision mediump float;\n"
1275 "varying vec2 v_texcoord;\n"
1276 "uniform sampler2D tex;\n"
1277 "uniform float alpha;\n"
1278 "void main()\n"
1279 "{\n"
1280 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001281 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001282
1283static const char texture_fragment_shader_rgbx[] =
1284 "precision mediump float;\n"
1285 "varying vec2 v_texcoord;\n"
1286 "uniform sampler2D tex;\n"
1287 "uniform float alpha;\n"
1288 "void main()\n"
1289 "{\n"
1290 " gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb\n;"
1291 " gl_FragColor.a = alpha;\n"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001292 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001293
1294static const char texture_fragment_shader_egl_external[] =
1295 "#extension GL_OES_EGL_image_external : require\n"
1296 "precision mediump float;\n"
1297 "varying vec2 v_texcoord;\n"
1298 "uniform samplerExternalOES tex;\n"
1299 "uniform float alpha;\n"
1300 "void main()\n"
1301 "{\n"
1302 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001303 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001304
1305static const char texture_fragment_shader_y_uv[] =
1306 "precision mediump float;\n"
1307 "uniform sampler2D tex;\n"
1308 "uniform sampler2D tex1;\n"
1309 "varying vec2 v_texcoord;\n"
1310 "uniform float alpha;\n"
1311 "void main() {\n"
1312 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1313 " float u = texture2D(tex1, v_texcoord).r - 0.5;\n"
1314 " float v = texture2D(tex1, v_texcoord).g - 0.5;\n"
1315 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001316 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001317
1318static const char texture_fragment_shader_y_u_v[] =
1319 "precision mediump float;\n"
1320 "uniform sampler2D tex;\n"
1321 "uniform sampler2D tex1;\n"
1322 "uniform sampler2D tex2;\n"
1323 "varying vec2 v_texcoord;\n"
1324 "uniform float alpha;\n"
1325 "void main() {\n"
1326 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1327 " float u = texture2D(tex1, v_texcoord).x - 0.5;\n"
1328 " float v = texture2D(tex2, v_texcoord).x - 0.5;\n"
1329 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001330 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001331
1332static const char texture_fragment_shader_y_xuxv[] =
1333 "precision mediump float;\n"
1334 "uniform sampler2D tex;\n"
1335 "uniform sampler2D tex1;\n"
1336 "varying vec2 v_texcoord;\n"
1337 "uniform float alpha;\n"
1338 "void main() {\n"
1339 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1340 " float u = texture2D(tex1, v_texcoord).g - 0.5;\n"
1341 " float v = texture2D(tex1, v_texcoord).a - 0.5;\n"
1342 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001343 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001344
1345static const char solid_fragment_shader[] =
1346 "precision mediump float;\n"
1347 "uniform vec4 color;\n"
1348 "uniform float alpha;\n"
1349 "void main()\n"
1350 "{\n"
1351 " gl_FragColor = alpha * color\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001352 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001353
1354static int
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001355compile_shader(GLenum type, int count, const char **sources)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001356{
1357 GLuint s;
1358 char msg[512];
1359 GLint status;
1360
1361 s = glCreateShader(type);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001362 glShaderSource(s, count, sources, NULL);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001363 glCompileShader(s);
1364 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
1365 if (!status) {
1366 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1367 weston_log("shader info: %s\n", msg);
1368 return GL_NONE;
1369 }
1370
1371 return s;
1372}
1373
1374static int
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03001375shader_init(struct gl_shader *shader, struct gl_renderer *renderer,
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001376 const char *vertex_source, const char *fragment_source)
1377{
1378 char msg[512];
1379 GLint status;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001380 int count;
1381 const char *sources[3];
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001382
1383 shader->vertex_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001384 compile_shader(GL_VERTEX_SHADER, 1, &vertex_source);
1385
1386 if (renderer->fragment_shader_debug) {
1387 sources[0] = fragment_source;
1388 sources[1] = fragment_debug;
1389 sources[2] = fragment_brace;
1390 count = 3;
1391 } else {
1392 sources[0] = fragment_source;
1393 sources[1] = fragment_brace;
1394 count = 2;
1395 }
1396
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001397 shader->fragment_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001398 compile_shader(GL_FRAGMENT_SHADER, count, sources);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001399
1400 shader->program = glCreateProgram();
1401 glAttachShader(shader->program, shader->vertex_shader);
1402 glAttachShader(shader->program, shader->fragment_shader);
1403 glBindAttribLocation(shader->program, 0, "position");
1404 glBindAttribLocation(shader->program, 1, "texcoord");
1405
1406 glLinkProgram(shader->program);
1407 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1408 if (!status) {
1409 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1410 weston_log("link info: %s\n", msg);
1411 return -1;
1412 }
1413
1414 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1415 shader->tex_uniforms[0] = glGetUniformLocation(shader->program, "tex");
1416 shader->tex_uniforms[1] = glGetUniformLocation(shader->program, "tex1");
1417 shader->tex_uniforms[2] = glGetUniformLocation(shader->program, "tex2");
1418 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
1419 shader->color_uniform = glGetUniformLocation(shader->program, "color");
1420
1421 return 0;
1422}
1423
1424static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001425shader_release(struct gl_shader *shader)
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001426{
1427 glDeleteShader(shader->vertex_shader);
1428 glDeleteShader(shader->fragment_shader);
1429 glDeleteProgram(shader->program);
1430
1431 shader->vertex_shader = 0;
1432 shader->fragment_shader = 0;
1433 shader->program = 0;
1434}
1435
1436static void
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001437log_extensions(const char *name, const char *extensions)
1438{
1439 const char *p, *end;
1440 int l;
1441 int len;
1442
1443 l = weston_log("%s:", name);
1444 p = extensions;
1445 while (*p) {
1446 end = strchrnul(p, ' ');
1447 len = end - p;
1448 if (l + len > 78)
1449 l = weston_log_continue("\n" STAMP_SPACE "%.*s",
1450 len, p);
1451 else
1452 l += weston_log_continue(" %.*s", len, p);
1453 for (p = end; isspace(*p); p++)
1454 ;
1455 }
1456 weston_log_continue("\n");
1457}
1458
1459static void
1460log_egl_gl_info(EGLDisplay egldpy)
1461{
1462 const char *str;
1463
1464 str = eglQueryString(egldpy, EGL_VERSION);
1465 weston_log("EGL version: %s\n", str ? str : "(null)");
1466
1467 str = eglQueryString(egldpy, EGL_VENDOR);
1468 weston_log("EGL vendor: %s\n", str ? str : "(null)");
1469
1470 str = eglQueryString(egldpy, EGL_CLIENT_APIS);
1471 weston_log("EGL client APIs: %s\n", str ? str : "(null)");
1472
1473 str = eglQueryString(egldpy, EGL_EXTENSIONS);
1474 log_extensions("EGL extensions", str ? str : "(null)");
1475
1476 str = (char *)glGetString(GL_VERSION);
1477 weston_log("GL version: %s\n", str ? str : "(null)");
1478
1479 str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
1480 weston_log("GLSL version: %s\n", str ? str : "(null)");
1481
1482 str = (char *)glGetString(GL_VENDOR);
1483 weston_log("GL vendor: %s\n", str ? str : "(null)");
1484
1485 str = (char *)glGetString(GL_RENDERER);
1486 weston_log("GL renderer: %s\n", str ? str : "(null)");
1487
1488 str = (char *)glGetString(GL_EXTENSIONS);
1489 log_extensions("GL extensions", str ? str : "(null)");
1490}
1491
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001492static void
1493log_egl_config_info(EGLDisplay egldpy, EGLConfig eglconfig)
1494{
1495 EGLint r, g, b, a;
1496
1497 weston_log("Chosen EGL config details:\n");
1498
1499 weston_log_continue(STAMP_SPACE "RGBA bits");
1500 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_RED_SIZE, &r) &&
1501 eglGetConfigAttrib(egldpy, eglconfig, EGL_GREEN_SIZE, &g) &&
1502 eglGetConfigAttrib(egldpy, eglconfig, EGL_BLUE_SIZE, &b) &&
1503 eglGetConfigAttrib(egldpy, eglconfig, EGL_ALPHA_SIZE, &a))
1504 weston_log_continue(": %d %d %d %d\n", r, g, b, a);
1505 else
1506 weston_log_continue(" unknown\n");
1507
1508 weston_log_continue(STAMP_SPACE "swap interval range");
1509 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_MIN_SWAP_INTERVAL, &a) &&
1510 eglGetConfigAttrib(egldpy, eglconfig, EGL_MAX_SWAP_INTERVAL, &b))
1511 weston_log_continue(": %d - %d\n", a, b);
1512 else
1513 weston_log_continue(" unknown\n");
1514}
1515
John Kåre Alsaker44154502012-11-13 19:10:20 +01001516static void
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05001517gl_renderer_output_set_border(struct weston_output *output,
1518 enum gl_renderer_border_side side,
1519 int32_t width, int32_t height,
1520 int32_t tex_width, unsigned char *data)
1521{
1522 struct gl_output_state *go = get_output_state(output);
1523
1524 go->borders[side].width = width;
1525 go->borders[side].height = height;
1526 go->borders[side].tex_width = tex_width;
1527 go->borders[side].data = data;
1528 go->borders[side].dirty = 1;
1529}
1530
John Kåre Alsaker94659272012-11-13 19:10:18 +01001531static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001532gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001533
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001534static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001535gl_renderer_output_create(struct weston_output *output,
John Kåre Alsaker94659272012-11-13 19:10:18 +01001536 EGLNativeWindowType window)
1537{
1538 struct weston_compositor *ec = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001539 struct gl_renderer *gr = get_renderer(ec);
1540 struct gl_output_state *go = calloc(1, sizeof *go);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001541 int i;
John Kåre Alsaker94659272012-11-13 19:10:18 +01001542
1543 if (!go)
1544 return -1;
1545
1546 go->egl_surface =
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001547 eglCreateWindowSurface(gr->egl_display,
1548 gr->egl_config,
John Kåre Alsaker94659272012-11-13 19:10:18 +01001549 window, NULL);
1550
1551 if (go->egl_surface == EGL_NO_SURFACE) {
1552 weston_log("failed to create egl surface\n");
1553 free(go);
1554 return -1;
1555 }
1556
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001557 if (gr->egl_context == NULL)
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001558 if (gl_renderer_setup(ec, go->egl_surface) < 0) {
John Kåre Alsaker94659272012-11-13 19:10:18 +01001559 free(go);
1560 return -1;
1561 }
1562
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001563 for (i = 0; i < BUFFER_DAMAGE_COUNT; i++)
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001564 pixman_region32_init(&go->buffer_damage[i]);
1565
John Kåre Alsaker94659272012-11-13 19:10:18 +01001566 output->renderer_state = go;
1567
1568 return 0;
1569}
1570
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001571static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001572gl_renderer_output_destroy(struct weston_output *output)
John Kåre Alsaker94659272012-11-13 19:10:18 +01001573{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001574 struct gl_renderer *gr = get_renderer(output->compositor);
1575 struct gl_output_state *go = get_output_state(output);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001576 int i;
1577
1578 for (i = 0; i < 2; i++)
1579 pixman_region32_fini(&go->buffer_damage[i]);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001580
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001581 eglDestroySurface(gr->egl_display, go->egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001582
1583 free(go);
1584}
1585
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001586static EGLSurface
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001587gl_renderer_output_surface(struct weston_output *output)
John Kåre Alsaker94659272012-11-13 19:10:18 +01001588{
1589 return get_output_state(output)->egl_surface;
1590}
1591
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03001592static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001593gl_renderer_destroy(struct weston_compositor *ec)
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001594{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001595 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001596
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03001597 wl_signal_emit(&gr->destroy_signal, gr);
1598
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001599 if (gr->has_bind_display)
1600 gr->unbind_display(gr->egl_display, ec->wl_display);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001601
1602 /* Work around crash in egl_dri2.c's dri2_make_current() - when does this apply? */
1603 eglMakeCurrent(gr->egl_display,
1604 EGL_NO_SURFACE, EGL_NO_SURFACE,
1605 EGL_NO_CONTEXT);
1606
1607 eglTerminate(gr->egl_display);
1608 eglReleaseThread();
Scott Moreau976a0502013-03-07 10:15:17 -07001609
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -04001610 wl_array_release(&gr->vertices);
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -04001611 wl_array_release(&gr->vtxcnt);
1612
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03001613 weston_binding_destroy(gr->fragment_binding);
1614 weston_binding_destroy(gr->fan_binding);
1615
Scott Moreau976a0502013-03-07 10:15:17 -07001616 free(gr);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001617}
1618
1619static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001620egl_choose_config(struct gl_renderer *gr, const EGLint *attribs,
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001621 const EGLint *visual_id)
1622{
1623 EGLint count = 0;
1624 EGLint matched = 0;
1625 EGLConfig *configs;
1626 int i;
1627
1628 if (!eglGetConfigs(gr->egl_display, NULL, 0, &count) || count < 1)
1629 return -1;
1630
1631 configs = calloc(count, sizeof *configs);
1632 if (!configs)
1633 return -1;
1634
1635 if (!eglChooseConfig(gr->egl_display, attribs, configs,
1636 count, &matched))
1637 goto out;
1638
1639 for (i = 0; i < matched; ++i) {
1640 EGLint id;
1641
1642 if (visual_id) {
1643 if (!eglGetConfigAttrib(gr->egl_display,
1644 configs[i], EGL_NATIVE_VISUAL_ID,
1645 &id))
1646 continue;
1647
Kristian Høgsbergc3ea26c2013-09-25 15:46:42 -07001648 if (id != 0 && id != *visual_id)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001649 continue;
1650 }
1651
1652 gr->egl_config = configs[i];
1653
1654 free(configs);
1655 return 0;
1656 }
1657
1658out:
1659 free(configs);
1660 return -1;
1661}
1662
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001663static const EGLint gl_renderer_opaque_attribs[] = {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001664 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1665 EGL_RED_SIZE, 1,
1666 EGL_GREEN_SIZE, 1,
1667 EGL_BLUE_SIZE, 1,
1668 EGL_ALPHA_SIZE, 0,
1669 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1670 EGL_NONE
1671};
1672
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001673static const EGLint gl_renderer_alpha_attribs[] = {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001674 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1675 EGL_RED_SIZE, 1,
1676 EGL_GREEN_SIZE, 1,
1677 EGL_BLUE_SIZE, 1,
1678 EGL_ALPHA_SIZE, 1,
1679 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1680 EGL_NONE
1681};
1682
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001683static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001684gl_renderer_create(struct weston_compositor *ec, EGLNativeDisplayType display,
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001685 const EGLint *attribs, const EGLint *visual_id)
1686{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001687 struct gl_renderer *gr;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001688 EGLint major, minor;
1689
1690 gr = calloc(1, sizeof *gr);
1691
1692 if (gr == NULL)
1693 return -1;
1694
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001695 gr->base.read_pixels = gl_renderer_read_pixels;
1696 gr->base.repaint_output = gl_renderer_repaint_output;
1697 gr->base.flush_damage = gl_renderer_flush_damage;
1698 gr->base.attach = gl_renderer_attach;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001699 gr->base.surface_set_color = gl_renderer_surface_set_color;
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03001700 gr->base.destroy = gl_renderer_destroy;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001701
1702 gr->egl_display = eglGetDisplay(display);
1703 if (gr->egl_display == EGL_NO_DISPLAY) {
1704 weston_log("failed to create display\n");
1705 goto err_egl;
1706 }
1707
1708 if (!eglInitialize(gr->egl_display, &major, &minor)) {
1709 weston_log("failed to initialize display\n");
1710 goto err_egl;
1711 }
1712
1713 if (egl_choose_config(gr, attribs, visual_id) < 0) {
1714 weston_log("failed to choose EGL config\n");
1715 goto err_egl;
1716 }
1717
1718 ec->renderer = &gr->base;
Pekka Paalanen7bb65102013-05-22 18:03:04 +03001719 ec->capabilities |= WESTON_CAP_ROTATION_ANY;
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03001720 ec->capabilities |= WESTON_CAP_CAPTURE_YFLIP;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001721
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001722 wl_display_add_shm_format(ec->wl_display, WL_SHM_FORMAT_RGB565);
1723
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03001724 wl_signal_init(&gr->destroy_signal);
1725
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001726 return 0;
1727
1728err_egl:
Pekka Paalanen326529f2012-11-27 12:25:25 +02001729 gl_renderer_print_egl_error_state();
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001730 free(gr);
1731 return -1;
1732}
1733
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001734static EGLDisplay
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001735gl_renderer_display(struct weston_compositor *ec)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001736{
1737 return get_renderer(ec)->egl_display;
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001738}
1739
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001740static int
1741compile_shaders(struct weston_compositor *ec)
1742{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001743 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker40684142012-11-13 19:10:25 +01001744
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03001745 gr->texture_shader_rgba.vertex_source = vertex_shader;
1746 gr->texture_shader_rgba.fragment_source = texture_fragment_shader_rgba;
1747
1748 gr->texture_shader_rgbx.vertex_source = vertex_shader;
1749 gr->texture_shader_rgbx.fragment_source = texture_fragment_shader_rgbx;
1750
1751 gr->texture_shader_egl_external.vertex_source = vertex_shader;
1752 gr->texture_shader_egl_external.fragment_source =
1753 texture_fragment_shader_egl_external;
1754
1755 gr->texture_shader_y_uv.vertex_source = vertex_shader;
1756 gr->texture_shader_y_uv.fragment_source = texture_fragment_shader_y_uv;
1757
1758 gr->texture_shader_y_u_v.vertex_source = vertex_shader;
1759 gr->texture_shader_y_u_v.fragment_source =
1760 texture_fragment_shader_y_u_v;
1761
1762 gr->texture_shader_y_u_v.vertex_source = vertex_shader;
1763 gr->texture_shader_y_xuxv.fragment_source =
1764 texture_fragment_shader_y_xuxv;
1765
1766 gr->solid_shader.vertex_source = vertex_shader;
1767 gr->solid_shader.fragment_source = solid_fragment_shader;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001768
1769 return 0;
1770}
1771
1772static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04001773fragment_debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001774 void *data)
1775{
1776 struct weston_compositor *ec = data;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001777 struct gl_renderer *gr = get_renderer(ec);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001778 struct weston_output *output;
1779
John Kåre Alsaker40684142012-11-13 19:10:25 +01001780 gr->fragment_shader_debug ^= 1;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001781
John Kåre Alsaker40684142012-11-13 19:10:25 +01001782 shader_release(&gr->texture_shader_rgba);
1783 shader_release(&gr->texture_shader_rgbx);
1784 shader_release(&gr->texture_shader_egl_external);
1785 shader_release(&gr->texture_shader_y_uv);
1786 shader_release(&gr->texture_shader_y_u_v);
1787 shader_release(&gr->texture_shader_y_xuxv);
1788 shader_release(&gr->solid_shader);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001789
Ander Conselvan de Oliveira03fb4ef2012-12-03 17:08:11 +02001790 /* Force use_shader() to call glUseProgram(), since we need to use
1791 * the recompiled version of the shader. */
1792 gr->current_shader = NULL;
1793
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001794 wl_list_for_each(output, &ec->output_list, link)
1795 weston_output_damage(output);
1796}
1797
Kristian Høgsberg8799d412013-05-07 10:50:09 -04001798static void
1799fan_debug_repaint_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
1800 void *data)
1801{
1802 struct weston_compositor *compositor = data;
1803 struct gl_renderer *gr = get_renderer(compositor);
1804
1805 gr->fan_debug = !gr->fan_debug;
1806 weston_compositor_damage_all(compositor);
1807}
1808
John Kåre Alsaker94659272012-11-13 19:10:18 +01001809static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001810gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001811{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001812 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001813 const char *extensions;
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001814 EGLBoolean ret;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001815
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001816 static const EGLint context_attribs[] = {
1817 EGL_CONTEXT_CLIENT_VERSION, 2,
1818 EGL_NONE
1819 };
1820
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001821 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
1822 weston_log("failed to bind EGL_OPENGL_ES_API\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02001823 gl_renderer_print_egl_error_state();
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001824 return -1;
1825 }
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001826
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001827 log_egl_config_info(gr->egl_display, gr->egl_config);
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001828
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001829 gr->egl_context = eglCreateContext(gr->egl_display, gr->egl_config,
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001830 EGL_NO_CONTEXT, context_attribs);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001831 if (gr->egl_context == NULL) {
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001832 weston_log("failed to create context\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02001833 gl_renderer_print_egl_error_state();
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001834 return -1;
1835 }
1836
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001837 ret = eglMakeCurrent(gr->egl_display, egl_surface,
1838 egl_surface, gr->egl_context);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001839 if (ret == EGL_FALSE) {
1840 weston_log("Failed to make EGL context current.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02001841 gl_renderer_print_egl_error_state();
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001842 return -1;
1843 }
1844
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001845 log_egl_gl_info(gr->egl_display);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001846
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001847 gr->image_target_texture_2d =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001848 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001849 gr->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1850 gr->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1851 gr->bind_display =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001852 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001853 gr->unbind_display =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001854 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001855 gr->query_buffer =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001856 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
1857
1858 extensions = (const char *) glGetString(GL_EXTENSIONS);
1859 if (!extensions) {
1860 weston_log("Retrieving GL extension string failed.\n");
1861 return -1;
1862 }
1863
1864 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
1865 weston_log("GL_EXT_texture_format_BGRA8888 not available\n");
1866 return -1;
1867 }
1868
1869 if (strstr(extensions, "GL_EXT_read_format_bgra"))
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01001870 ec->read_format = PIXMAN_a8r8g8b8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001871 else
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01001872 ec->read_format = PIXMAN_a8b8g8r8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001873
Kristian Høgsberg1c4f1632013-08-07 12:11:27 -07001874#ifdef GL_EXT_unpack_subimage
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001875 if (strstr(extensions, "GL_EXT_unpack_subimage"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001876 gr->has_unpack_subimage = 1;
Kristian Høgsberg1c4f1632013-08-07 12:11:27 -07001877#endif
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001878
1879 if (strstr(extensions, "GL_OES_EGL_image_external"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001880 gr->has_egl_image_external = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001881
1882 extensions =
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001883 (const char *) eglQueryString(gr->egl_display, EGL_EXTENSIONS);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001884 if (!extensions) {
1885 weston_log("Retrieving EGL extension string failed.\n");
1886 return -1;
1887 }
1888
1889 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001890 gr->has_bind_display = 1;
1891 if (gr->has_bind_display) {
1892 ret = gr->bind_display(gr->egl_display, ec->wl_display);
Pekka Paalanen035a0322012-10-24 09:43:06 +03001893 if (!ret)
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001894 gr->has_bind_display = 0;
Pekka Paalanen035a0322012-10-24 09:43:06 +03001895 }
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001896
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001897 if (strstr(extensions, "EGL_EXT_buffer_age"))
1898 gr->has_egl_buffer_age = 1;
1899 else
1900 weston_log("warning: EGL_EXT_buffer_age not supported. "
1901 "Performance could be affected.\n");
1902
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001903 glActiveTexture(GL_TEXTURE0);
1904
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001905 if (compile_shaders(ec))
1906 return -1;
1907
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03001908 gr->fragment_binding =
1909 weston_compositor_add_debug_binding(ec, KEY_S,
1910 fragment_debug_binding,
1911 ec);
1912 gr->fan_binding =
1913 weston_compositor_add_debug_binding(ec, KEY_F,
1914 fan_debug_repaint_binding,
1915 ec);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001916
Pekka Paalanen035a0322012-10-24 09:43:06 +03001917 weston_log("GL ES 2 renderer features:\n");
1918 weston_log_continue(STAMP_SPACE "read-back format: %s\n",
Pekka Paalanenfe4eacf2013-01-10 16:50:42 +02001919 ec->read_format == PIXMAN_a8r8g8b8 ? "BGRA" : "RGBA");
Pekka Paalanen035a0322012-10-24 09:43:06 +03001920 weston_log_continue(STAMP_SPACE "wl_shm sub-image to texture: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001921 gr->has_unpack_subimage ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03001922 weston_log_continue(STAMP_SPACE "EGL Wayland extension: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001923 gr->has_bind_display ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03001924
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001925
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001926 return 0;
1927}
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001928
1929WL_EXPORT struct gl_renderer_interface gl_renderer_interface = {
1930 .opaque_attribs = gl_renderer_opaque_attribs,
1931 .alpha_attribs = gl_renderer_alpha_attribs,
1932
1933 .create = gl_renderer_create,
1934 .display = gl_renderer_display,
1935 .output_create = gl_renderer_output_create,
1936 .output_destroy = gl_renderer_output_destroy,
1937 .output_surface = gl_renderer_output_surface,
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05001938 .output_set_border = gl_renderer_output_set_border,
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001939 .print_egl_error_state = gl_renderer_print_egl_error_state
1940};