blob: a74aef5f6e43315f4819161aad0ce46b8a578d2f [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 ||
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +0100539 output->current_scale != ev->surface->buffer_viewport.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
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +0200886 if (!pixman_region32_not_empty(&gs->texture_damage) &&
887 !gs->needs_full_upload)
Pekka Paalanenfb003d32012-12-04 15:58:13 +0200888 goto done;
Pekka Paalanenbcdd5792012-11-07 12:25:13 +0200889
Tomeu Vizoso12072b62013-08-06 20:05:55 +0200890 switch (wl_shm_buffer_get_format(buffer->shm_buffer)) {
891 case WL_SHM_FORMAT_XRGB8888:
892 case WL_SHM_FORMAT_ARGB8888:
893 format = GL_BGRA_EXT;
894 pixel_type = GL_UNSIGNED_BYTE;
895 break;
896 case WL_SHM_FORMAT_RGB565:
897 format = GL_RGB;
898 pixel_type = GL_UNSIGNED_SHORT_5_6_5;
899 break;
900 default:
901 weston_log("warning: unknown shm buffer format\n");
902 format = GL_BGRA_EXT;
903 pixel_type = GL_UNSIGNED_BYTE;
904 }
905
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100906 glBindTexture(GL_TEXTURE_2D, gs->textures[0]);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -0400907
John Kåre Alsaker320711d2012-11-13 19:10:27 +0100908 if (!gr->has_unpack_subimage) {
Neil Robertse5051712013-11-13 15:44:06 +0000909 wl_shm_buffer_begin_access(buffer->shm_buffer);
Tomeu Vizoso12072b62013-08-06 20:05:55 +0200910 glTexImage2D(GL_TEXTURE_2D, 0, format,
Pekka Paalanen68033ac2012-12-04 15:58:15 +0200911 gs->pitch, buffer->height, 0,
Tomeu Vizoso12072b62013-08-06 20:05:55 +0200912 format, pixel_type,
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500913 wl_shm_buffer_get_data(buffer->shm_buffer));
Neil Robertse5051712013-11-13 15:44:06 +0000914 wl_shm_buffer_end_access(buffer->shm_buffer);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -0400915
Pekka Paalanenbcdd5792012-11-07 12:25:13 +0200916 goto done;
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -0400917 }
918
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -0700919#ifdef GL_EXT_unpack_subimage
920 glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, gs->pitch);
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500921 data = wl_shm_buffer_get_data(buffer->shm_buffer);
Ander Conselvan de Oliveira6be5f432013-06-07 16:52:45 +0300922
923 if (gs->needs_full_upload) {
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -0700924 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, 0);
925 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, 0);
Neil Robertse5051712013-11-13 15:44:06 +0000926 wl_shm_buffer_begin_access(buffer->shm_buffer);
Ander Conselvan de Oliveira6be5f432013-06-07 16:52:45 +0300927 glTexSubImage2D(GL_TEXTURE_2D, 0,
928 0, 0, gs->pitch, buffer->height,
Tomeu Vizoso12072b62013-08-06 20:05:55 +0200929 format, pixel_type, data);
Neil Robertse5051712013-11-13 15:44:06 +0000930 wl_shm_buffer_end_access(buffer->shm_buffer);
Ander Conselvan de Oliveira6be5f432013-06-07 16:52:45 +0300931 goto done;
932 }
933
Pekka Paalanen81ee3f52012-12-04 15:58:16 +0200934 rectangles = pixman_region32_rectangles(&gs->texture_damage, &n);
Neil Robertse5051712013-11-13 15:44:06 +0000935 wl_shm_buffer_begin_access(buffer->shm_buffer);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -0400936 for (i = 0; i < n; i++) {
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200937 pixman_box32_t r;
938
939 r = weston_surface_to_buffer_rect(surface, rectangles[i]);
940
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -0700941 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, r.x1);
942 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, r.y1);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200943 glTexSubImage2D(GL_TEXTURE_2D, 0, r.x1, r.y1,
944 r.x2 - r.x1, r.y2 - r.y1,
Tomeu Vizoso12072b62013-08-06 20:05:55 +0200945 format, pixel_type, data);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -0400946 }
Neil Robertse5051712013-11-13 15:44:06 +0000947 wl_shm_buffer_end_access(buffer->shm_buffer);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -0400948#endif
Pekka Paalanenbcdd5792012-11-07 12:25:13 +0200949
950done:
Pekka Paalanen81ee3f52012-12-04 15:58:16 +0200951 pixman_region32_fini(&gs->texture_damage);
952 pixman_region32_init(&gs->texture_damage);
Ander Conselvan de Oliveira6be5f432013-06-07 16:52:45 +0300953 gs->needs_full_upload = 0;
Pekka Paalanenfb003d32012-12-04 15:58:13 +0200954
955 weston_buffer_reference(&gs->buffer_ref, NULL);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -0400956}
957
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400958static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100959ensure_textures(struct gl_surface_state *gs, int num_textures)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400960{
961 int i;
962
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100963 if (num_textures <= gs->num_textures)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400964 return;
965
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100966 for (i = gs->num_textures; i < num_textures; i++) {
967 glGenTextures(1, &gs->textures[i]);
968 glBindTexture(gs->target, gs->textures[i]);
969 glTexParameteri(gs->target,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400970 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100971 glTexParameteri(gs->target,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400972 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
973 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100974 gs->num_textures = num_textures;
975 glBindTexture(gs->target, 0);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400976}
977
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400978static void
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +0300979gl_renderer_attach_shm(struct weston_surface *es, struct weston_buffer *buffer,
980 struct wl_shm_buffer *shm_buffer)
981{
982 struct weston_compositor *ec = es->compositor;
983 struct gl_renderer *gr = get_renderer(ec);
984 struct gl_surface_state *gs = get_surface_state(es);
Tomeu Vizoso12072b62013-08-06 20:05:55 +0200985 int pitch;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +0300986
987 buffer->shm_buffer = shm_buffer;
988 buffer->width = wl_shm_buffer_get_width(shm_buffer);
989 buffer->height = wl_shm_buffer_get_height(shm_buffer);
990
Tomeu Vizoso12072b62013-08-06 20:05:55 +0200991 switch (wl_shm_buffer_get_format(shm_buffer)) {
992 case WL_SHM_FORMAT_XRGB8888:
993 gs->shader = &gr->texture_shader_rgbx;
994 pitch = wl_shm_buffer_get_stride(shm_buffer) / 4;
995 break;
996 case WL_SHM_FORMAT_ARGB8888:
997 gs->shader = &gr->texture_shader_rgba;
998 pitch = wl_shm_buffer_get_stride(shm_buffer) / 4;
999 break;
1000 case WL_SHM_FORMAT_RGB565:
1001 gs->shader = &gr->texture_shader_rgbx;
1002 pitch = wl_shm_buffer_get_stride(shm_buffer) / 2;
1003 break;
1004 default:
1005 weston_log("warning: unknown shm buffer format\n");
1006 gs->shader = &gr->texture_shader_rgba;
1007 pitch = wl_shm_buffer_get_stride(shm_buffer) / 4;
1008 }
1009
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001010 /* Only allocate a texture if it doesn't match existing one.
1011 * If a switch from DRM allocated buffer to a SHM buffer is
1012 * happening, we need to allocate a new texture buffer. */
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001013 if (pitch != gs->pitch ||
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001014 buffer->height != gs->height ||
1015 gs->buffer_type != BUFFER_TYPE_SHM) {
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001016 gs->pitch = pitch;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001017 gs->height = buffer->height;
1018 gs->target = GL_TEXTURE_2D;
1019 gs->buffer_type = BUFFER_TYPE_SHM;
1020 gs->needs_full_upload = 1;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001021 gs->y_inverted = 1;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001022
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03001023 gs->surface = es;
1024
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001025 ensure_textures(gs, 1);
1026 glBindTexture(GL_TEXTURE_2D, gs->textures[0]);
1027 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1028 gs->pitch, buffer->height, 0,
1029 GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
1030 }
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001031}
1032
1033static void
1034gl_renderer_attach_egl(struct weston_surface *es, struct weston_buffer *buffer,
1035 uint32_t format)
1036{
1037 struct weston_compositor *ec = es->compositor;
1038 struct gl_renderer *gr = get_renderer(ec);
1039 struct gl_surface_state *gs = get_surface_state(es);
1040 EGLint attribs[3];
1041 int i, num_planes;
1042
1043 buffer->legacy_buffer = (struct wl_buffer *)buffer->resource;
1044 gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1045 EGL_WIDTH, &buffer->width);
1046 gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1047 EGL_HEIGHT, &buffer->height);
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001048 gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1049 EGL_WAYLAND_Y_INVERTED_WL, &buffer->y_inverted);
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001050
1051 for (i = 0; i < gs->num_images; i++)
1052 gr->destroy_image(gr->egl_display, gs->images[i]);
1053 gs->num_images = 0;
1054 gs->target = GL_TEXTURE_2D;
1055 switch (format) {
1056 case EGL_TEXTURE_RGB:
1057 case EGL_TEXTURE_RGBA:
1058 default:
1059 num_planes = 1;
1060 gs->shader = &gr->texture_shader_rgba;
1061 break;
1062 case EGL_TEXTURE_EXTERNAL_WL:
1063 num_planes = 1;
1064 gs->target = GL_TEXTURE_EXTERNAL_OES;
1065 gs->shader = &gr->texture_shader_egl_external;
1066 break;
1067 case EGL_TEXTURE_Y_UV_WL:
1068 num_planes = 2;
1069 gs->shader = &gr->texture_shader_y_uv;
1070 break;
1071 case EGL_TEXTURE_Y_U_V_WL:
1072 num_planes = 3;
1073 gs->shader = &gr->texture_shader_y_u_v;
1074 break;
1075 case EGL_TEXTURE_Y_XUXV_WL:
1076 num_planes = 2;
1077 gs->shader = &gr->texture_shader_y_xuxv;
1078 break;
1079 }
1080
1081 ensure_textures(gs, num_planes);
1082 for (i = 0; i < num_planes; i++) {
1083 attribs[0] = EGL_WAYLAND_PLANE_WL;
1084 attribs[1] = i;
1085 attribs[2] = EGL_NONE;
1086 gs->images[i] = gr->create_image(gr->egl_display,
1087 NULL,
1088 EGL_WAYLAND_BUFFER_WL,
1089 buffer->legacy_buffer,
1090 attribs);
1091 if (!gs->images[i]) {
1092 weston_log("failed to create img for plane %d\n", i);
1093 continue;
1094 }
1095 gs->num_images++;
1096
1097 glActiveTexture(GL_TEXTURE0 + i);
1098 glBindTexture(gs->target, gs->textures[i]);
1099 gr->image_target_texture_2d(gs->target,
1100 gs->images[i]);
1101 }
1102
1103 gs->pitch = buffer->width;
1104 gs->height = buffer->height;
1105 gs->buffer_type = BUFFER_TYPE_EGL;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001106 gs->y_inverted = buffer->y_inverted;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001107}
1108
1109static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001110gl_renderer_attach(struct weston_surface *es, struct weston_buffer *buffer)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001111{
1112 struct weston_compositor *ec = es->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001113 struct gl_renderer *gr = get_renderer(ec);
1114 struct gl_surface_state *gs = get_surface_state(es);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001115 struct wl_shm_buffer *shm_buffer;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001116 EGLint format;
1117 int i;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001118
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001119 weston_buffer_reference(&gs->buffer_ref, buffer);
1120
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001121 if (!buffer) {
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001122 for (i = 0; i < gs->num_images; i++) {
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001123 gr->destroy_image(gr->egl_display, gs->images[i]);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001124 gs->images[i] = NULL;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001125 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001126 gs->num_images = 0;
1127 glDeleteTextures(gs->num_textures, gs->textures);
1128 gs->num_textures = 0;
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +03001129 gs->buffer_type = BUFFER_TYPE_NULL;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001130 gs->y_inverted = 1;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001131 return;
1132 }
1133
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001134 shm_buffer = wl_shm_buffer_get(buffer->resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001135
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001136 if (shm_buffer)
1137 gl_renderer_attach_shm(es, buffer, shm_buffer);
Kristian Høgsberg47229392013-08-07 11:59:54 -07001138 else if (gr->query_buffer(gr->egl_display, (void *) buffer->resource,
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001139 EGL_TEXTURE_FORMAT, &format))
1140 gl_renderer_attach_egl(es, buffer, format);
1141 else {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001142 weston_log("unhandled buffer type!\n");
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001143 weston_buffer_reference(&gs->buffer_ref, NULL);
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +03001144 gs->buffer_type = BUFFER_TYPE_NULL;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001145 gs->y_inverted = 1;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001146 }
1147}
1148
Kristian Høgsberg42263852012-09-06 21:59:29 -04001149static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001150gl_renderer_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001151 float red, float green, float blue, float alpha)
1152{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001153 struct gl_surface_state *gs = get_surface_state(surface);
1154 struct gl_renderer *gr = get_renderer(surface->compositor);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001155
1156 gs->color[0] = red;
1157 gs->color[1] = green;
1158 gs->color[2] = blue;
1159 gs->color[3] = alpha;
1160
John Kåre Alsaker40684142012-11-13 19:10:25 +01001161 gs->shader = &gr->solid_shader;
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001162}
1163
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03001164static void
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03001165surface_state_destroy(struct gl_surface_state *gs, struct gl_renderer *gr)
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03001166{
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03001167 int i;
1168
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03001169 wl_list_remove(&gs->surface_destroy_listener.link);
1170 wl_list_remove(&gs->renderer_destroy_listener.link);
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03001171
1172 gs->surface->renderer_state = NULL;
1173
1174 glDeleteTextures(gs->num_textures, gs->textures);
1175
1176 for (i = 0; i < gs->num_images; i++)
1177 gr->destroy_image(gr->egl_display, gs->images[i]);
1178
1179 weston_buffer_reference(&gs->buffer_ref, NULL);
1180 pixman_region32_fini(&gs->texture_damage);
1181 free(gs);
1182}
1183
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03001184static void
1185surface_state_handle_surface_destroy(struct wl_listener *listener, void *data)
1186{
1187 struct gl_surface_state *gs;
1188 struct gl_renderer *gr;
1189
1190 gs = container_of(listener, struct gl_surface_state,
1191 surface_destroy_listener);
1192
1193 gr = get_renderer(gs->surface->compositor);
1194
1195 surface_state_destroy(gs, gr);
1196}
1197
1198static void
1199surface_state_handle_renderer_destroy(struct wl_listener *listener, void *data)
1200{
1201 struct gl_surface_state *gs;
1202 struct gl_renderer *gr;
1203
1204 gr = data;
1205
1206 gs = container_of(listener, struct gl_surface_state,
1207 renderer_destroy_listener);
1208
1209 surface_state_destroy(gs, gr);
1210}
1211
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001212static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001213gl_renderer_create_surface(struct weston_surface *surface)
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001214{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001215 struct gl_surface_state *gs;
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03001216 struct gl_renderer *gr = get_renderer(surface->compositor);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001217
1218 gs = calloc(1, sizeof *gs);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001219 if (!gs)
1220 return -1;
1221
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001222 /* A buffer is never attached to solid color surfaces, yet
1223 * they still go through texcoord computations. Do not divide
1224 * by zero there.
1225 */
1226 gs->pitch = 1;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001227 gs->y_inverted = 1;
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001228
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03001229 gs->surface = surface;
1230
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001231 pixman_region32_init(&gs->texture_damage);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001232 surface->renderer_state = gs;
1233
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03001234 gs->surface_destroy_listener.notify =
1235 surface_state_handle_surface_destroy;
1236 wl_signal_add(&surface->destroy_signal,
1237 &gs->surface_destroy_listener);
1238
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03001239 gs->renderer_destroy_listener.notify =
1240 surface_state_handle_renderer_destroy;
1241 wl_signal_add(&gr->destroy_signal,
1242 &gs->renderer_destroy_listener);
1243
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001244 if (surface->buffer_ref.buffer) {
1245 gl_renderer_attach(surface, surface->buffer_ref.buffer);
1246 gl_renderer_flush_damage(surface);
1247 }
1248
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001249 return 0;
1250}
1251
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001252static const char vertex_shader[] =
1253 "uniform mat4 proj;\n"
1254 "attribute vec2 position;\n"
1255 "attribute vec2 texcoord;\n"
1256 "varying vec2 v_texcoord;\n"
1257 "void main()\n"
1258 "{\n"
1259 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
1260 " v_texcoord = texcoord;\n"
1261 "}\n";
1262
1263/* Declare common fragment shader uniforms */
1264#define FRAGMENT_CONVERT_YUV \
1265 " y *= alpha;\n" \
1266 " u *= alpha;\n" \
1267 " v *= alpha;\n" \
1268 " gl_FragColor.r = y + 1.59602678 * v;\n" \
1269 " gl_FragColor.g = y - 0.39176229 * u - 0.81296764 * v;\n" \
1270 " gl_FragColor.b = y + 2.01723214 * u;\n" \
1271 " gl_FragColor.a = alpha;\n"
1272
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001273static const char fragment_debug[] =
1274 " gl_FragColor = vec4(0.0, 0.3, 0.0, 0.2) + gl_FragColor * 0.8;\n";
1275
1276static const char fragment_brace[] =
1277 "}\n";
1278
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001279static const char texture_fragment_shader_rgba[] =
1280 "precision mediump float;\n"
1281 "varying vec2 v_texcoord;\n"
1282 "uniform sampler2D tex;\n"
1283 "uniform float alpha;\n"
1284 "void main()\n"
1285 "{\n"
1286 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001287 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001288
1289static const char texture_fragment_shader_rgbx[] =
1290 "precision mediump float;\n"
1291 "varying vec2 v_texcoord;\n"
1292 "uniform sampler2D tex;\n"
1293 "uniform float alpha;\n"
1294 "void main()\n"
1295 "{\n"
1296 " gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb\n;"
1297 " gl_FragColor.a = alpha;\n"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001298 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001299
1300static const char texture_fragment_shader_egl_external[] =
1301 "#extension GL_OES_EGL_image_external : require\n"
1302 "precision mediump float;\n"
1303 "varying vec2 v_texcoord;\n"
1304 "uniform samplerExternalOES tex;\n"
1305 "uniform float alpha;\n"
1306 "void main()\n"
1307 "{\n"
1308 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001309 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001310
1311static const char texture_fragment_shader_y_uv[] =
1312 "precision mediump float;\n"
1313 "uniform sampler2D tex;\n"
1314 "uniform sampler2D tex1;\n"
1315 "varying vec2 v_texcoord;\n"
1316 "uniform float alpha;\n"
1317 "void main() {\n"
1318 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1319 " float u = texture2D(tex1, v_texcoord).r - 0.5;\n"
1320 " float v = texture2D(tex1, v_texcoord).g - 0.5;\n"
1321 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001322 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001323
1324static const char texture_fragment_shader_y_u_v[] =
1325 "precision mediump float;\n"
1326 "uniform sampler2D tex;\n"
1327 "uniform sampler2D tex1;\n"
1328 "uniform sampler2D tex2;\n"
1329 "varying vec2 v_texcoord;\n"
1330 "uniform float alpha;\n"
1331 "void main() {\n"
1332 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1333 " float u = texture2D(tex1, v_texcoord).x - 0.5;\n"
1334 " float v = texture2D(tex2, v_texcoord).x - 0.5;\n"
1335 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001336 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001337
1338static const char texture_fragment_shader_y_xuxv[] =
1339 "precision mediump float;\n"
1340 "uniform sampler2D tex;\n"
1341 "uniform sampler2D tex1;\n"
1342 "varying vec2 v_texcoord;\n"
1343 "uniform float alpha;\n"
1344 "void main() {\n"
1345 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1346 " float u = texture2D(tex1, v_texcoord).g - 0.5;\n"
1347 " float v = texture2D(tex1, v_texcoord).a - 0.5;\n"
1348 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001349 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001350
1351static const char solid_fragment_shader[] =
1352 "precision mediump float;\n"
1353 "uniform vec4 color;\n"
1354 "uniform float alpha;\n"
1355 "void main()\n"
1356 "{\n"
1357 " gl_FragColor = alpha * color\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001358 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001359
1360static int
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001361compile_shader(GLenum type, int count, const char **sources)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001362{
1363 GLuint s;
1364 char msg[512];
1365 GLint status;
1366
1367 s = glCreateShader(type);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001368 glShaderSource(s, count, sources, NULL);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001369 glCompileShader(s);
1370 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
1371 if (!status) {
1372 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1373 weston_log("shader info: %s\n", msg);
1374 return GL_NONE;
1375 }
1376
1377 return s;
1378}
1379
1380static int
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03001381shader_init(struct gl_shader *shader, struct gl_renderer *renderer,
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001382 const char *vertex_source, const char *fragment_source)
1383{
1384 char msg[512];
1385 GLint status;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001386 int count;
1387 const char *sources[3];
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001388
1389 shader->vertex_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001390 compile_shader(GL_VERTEX_SHADER, 1, &vertex_source);
1391
1392 if (renderer->fragment_shader_debug) {
1393 sources[0] = fragment_source;
1394 sources[1] = fragment_debug;
1395 sources[2] = fragment_brace;
1396 count = 3;
1397 } else {
1398 sources[0] = fragment_source;
1399 sources[1] = fragment_brace;
1400 count = 2;
1401 }
1402
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001403 shader->fragment_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001404 compile_shader(GL_FRAGMENT_SHADER, count, sources);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001405
1406 shader->program = glCreateProgram();
1407 glAttachShader(shader->program, shader->vertex_shader);
1408 glAttachShader(shader->program, shader->fragment_shader);
1409 glBindAttribLocation(shader->program, 0, "position");
1410 glBindAttribLocation(shader->program, 1, "texcoord");
1411
1412 glLinkProgram(shader->program);
1413 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1414 if (!status) {
1415 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1416 weston_log("link info: %s\n", msg);
1417 return -1;
1418 }
1419
1420 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1421 shader->tex_uniforms[0] = glGetUniformLocation(shader->program, "tex");
1422 shader->tex_uniforms[1] = glGetUniformLocation(shader->program, "tex1");
1423 shader->tex_uniforms[2] = glGetUniformLocation(shader->program, "tex2");
1424 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
1425 shader->color_uniform = glGetUniformLocation(shader->program, "color");
1426
1427 return 0;
1428}
1429
1430static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001431shader_release(struct gl_shader *shader)
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001432{
1433 glDeleteShader(shader->vertex_shader);
1434 glDeleteShader(shader->fragment_shader);
1435 glDeleteProgram(shader->program);
1436
1437 shader->vertex_shader = 0;
1438 shader->fragment_shader = 0;
1439 shader->program = 0;
1440}
1441
1442static void
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001443log_extensions(const char *name, const char *extensions)
1444{
1445 const char *p, *end;
1446 int l;
1447 int len;
1448
1449 l = weston_log("%s:", name);
1450 p = extensions;
1451 while (*p) {
1452 end = strchrnul(p, ' ');
1453 len = end - p;
1454 if (l + len > 78)
1455 l = weston_log_continue("\n" STAMP_SPACE "%.*s",
1456 len, p);
1457 else
1458 l += weston_log_continue(" %.*s", len, p);
1459 for (p = end; isspace(*p); p++)
1460 ;
1461 }
1462 weston_log_continue("\n");
1463}
1464
1465static void
1466log_egl_gl_info(EGLDisplay egldpy)
1467{
1468 const char *str;
1469
1470 str = eglQueryString(egldpy, EGL_VERSION);
1471 weston_log("EGL version: %s\n", str ? str : "(null)");
1472
1473 str = eglQueryString(egldpy, EGL_VENDOR);
1474 weston_log("EGL vendor: %s\n", str ? str : "(null)");
1475
1476 str = eglQueryString(egldpy, EGL_CLIENT_APIS);
1477 weston_log("EGL client APIs: %s\n", str ? str : "(null)");
1478
1479 str = eglQueryString(egldpy, EGL_EXTENSIONS);
1480 log_extensions("EGL extensions", str ? str : "(null)");
1481
1482 str = (char *)glGetString(GL_VERSION);
1483 weston_log("GL version: %s\n", str ? str : "(null)");
1484
1485 str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
1486 weston_log("GLSL version: %s\n", str ? str : "(null)");
1487
1488 str = (char *)glGetString(GL_VENDOR);
1489 weston_log("GL vendor: %s\n", str ? str : "(null)");
1490
1491 str = (char *)glGetString(GL_RENDERER);
1492 weston_log("GL renderer: %s\n", str ? str : "(null)");
1493
1494 str = (char *)glGetString(GL_EXTENSIONS);
1495 log_extensions("GL extensions", str ? str : "(null)");
1496}
1497
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001498static void
1499log_egl_config_info(EGLDisplay egldpy, EGLConfig eglconfig)
1500{
1501 EGLint r, g, b, a;
1502
1503 weston_log("Chosen EGL config details:\n");
1504
1505 weston_log_continue(STAMP_SPACE "RGBA bits");
1506 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_RED_SIZE, &r) &&
1507 eglGetConfigAttrib(egldpy, eglconfig, EGL_GREEN_SIZE, &g) &&
1508 eglGetConfigAttrib(egldpy, eglconfig, EGL_BLUE_SIZE, &b) &&
1509 eglGetConfigAttrib(egldpy, eglconfig, EGL_ALPHA_SIZE, &a))
1510 weston_log_continue(": %d %d %d %d\n", r, g, b, a);
1511 else
1512 weston_log_continue(" unknown\n");
1513
1514 weston_log_continue(STAMP_SPACE "swap interval range");
1515 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_MIN_SWAP_INTERVAL, &a) &&
1516 eglGetConfigAttrib(egldpy, eglconfig, EGL_MAX_SWAP_INTERVAL, &b))
1517 weston_log_continue(": %d - %d\n", a, b);
1518 else
1519 weston_log_continue(" unknown\n");
1520}
1521
John Kåre Alsaker44154502012-11-13 19:10:20 +01001522static void
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05001523gl_renderer_output_set_border(struct weston_output *output,
1524 enum gl_renderer_border_side side,
1525 int32_t width, int32_t height,
1526 int32_t tex_width, unsigned char *data)
1527{
1528 struct gl_output_state *go = get_output_state(output);
1529
1530 go->borders[side].width = width;
1531 go->borders[side].height = height;
1532 go->borders[side].tex_width = tex_width;
1533 go->borders[side].data = data;
1534 go->borders[side].dirty = 1;
1535}
1536
John Kåre Alsaker94659272012-11-13 19:10:18 +01001537static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001538gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001539
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001540static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001541gl_renderer_output_create(struct weston_output *output,
John Kåre Alsaker94659272012-11-13 19:10:18 +01001542 EGLNativeWindowType window)
1543{
1544 struct weston_compositor *ec = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001545 struct gl_renderer *gr = get_renderer(ec);
1546 struct gl_output_state *go = calloc(1, sizeof *go);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001547 int i;
John Kåre Alsaker94659272012-11-13 19:10:18 +01001548
1549 if (!go)
1550 return -1;
1551
1552 go->egl_surface =
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001553 eglCreateWindowSurface(gr->egl_display,
1554 gr->egl_config,
John Kåre Alsaker94659272012-11-13 19:10:18 +01001555 window, NULL);
1556
1557 if (go->egl_surface == EGL_NO_SURFACE) {
1558 weston_log("failed to create egl surface\n");
1559 free(go);
1560 return -1;
1561 }
1562
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001563 if (gr->egl_context == NULL)
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001564 if (gl_renderer_setup(ec, go->egl_surface) < 0) {
John Kåre Alsaker94659272012-11-13 19:10:18 +01001565 free(go);
1566 return -1;
1567 }
1568
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001569 for (i = 0; i < BUFFER_DAMAGE_COUNT; i++)
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001570 pixman_region32_init(&go->buffer_damage[i]);
1571
John Kåre Alsaker94659272012-11-13 19:10:18 +01001572 output->renderer_state = go;
1573
1574 return 0;
1575}
1576
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001577static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001578gl_renderer_output_destroy(struct weston_output *output)
John Kåre Alsaker94659272012-11-13 19:10:18 +01001579{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001580 struct gl_renderer *gr = get_renderer(output->compositor);
1581 struct gl_output_state *go = get_output_state(output);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001582 int i;
1583
1584 for (i = 0; i < 2; i++)
1585 pixman_region32_fini(&go->buffer_damage[i]);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001586
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001587 eglDestroySurface(gr->egl_display, go->egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001588
1589 free(go);
1590}
1591
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001592static EGLSurface
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001593gl_renderer_output_surface(struct weston_output *output)
John Kåre Alsaker94659272012-11-13 19:10:18 +01001594{
1595 return get_output_state(output)->egl_surface;
1596}
1597
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03001598static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001599gl_renderer_destroy(struct weston_compositor *ec)
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001600{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001601 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001602
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03001603 wl_signal_emit(&gr->destroy_signal, gr);
1604
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001605 if (gr->has_bind_display)
1606 gr->unbind_display(gr->egl_display, ec->wl_display);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001607
1608 /* Work around crash in egl_dri2.c's dri2_make_current() - when does this apply? */
1609 eglMakeCurrent(gr->egl_display,
1610 EGL_NO_SURFACE, EGL_NO_SURFACE,
1611 EGL_NO_CONTEXT);
1612
1613 eglTerminate(gr->egl_display);
1614 eglReleaseThread();
Scott Moreau976a0502013-03-07 10:15:17 -07001615
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -04001616 wl_array_release(&gr->vertices);
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -04001617 wl_array_release(&gr->vtxcnt);
1618
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03001619 weston_binding_destroy(gr->fragment_binding);
1620 weston_binding_destroy(gr->fan_binding);
1621
Scott Moreau976a0502013-03-07 10:15:17 -07001622 free(gr);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001623}
1624
1625static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001626egl_choose_config(struct gl_renderer *gr, const EGLint *attribs,
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001627 const EGLint *visual_id)
1628{
1629 EGLint count = 0;
1630 EGLint matched = 0;
1631 EGLConfig *configs;
1632 int i;
1633
1634 if (!eglGetConfigs(gr->egl_display, NULL, 0, &count) || count < 1)
1635 return -1;
1636
1637 configs = calloc(count, sizeof *configs);
1638 if (!configs)
1639 return -1;
1640
1641 if (!eglChooseConfig(gr->egl_display, attribs, configs,
1642 count, &matched))
1643 goto out;
1644
1645 for (i = 0; i < matched; ++i) {
1646 EGLint id;
1647
1648 if (visual_id) {
1649 if (!eglGetConfigAttrib(gr->egl_display,
1650 configs[i], EGL_NATIVE_VISUAL_ID,
1651 &id))
1652 continue;
1653
Kristian Høgsbergc3ea26c2013-09-25 15:46:42 -07001654 if (id != 0 && id != *visual_id)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001655 continue;
1656 }
1657
1658 gr->egl_config = configs[i];
1659
1660 free(configs);
1661 return 0;
1662 }
1663
1664out:
1665 free(configs);
1666 return -1;
1667}
1668
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001669static const EGLint gl_renderer_opaque_attribs[] = {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001670 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1671 EGL_RED_SIZE, 1,
1672 EGL_GREEN_SIZE, 1,
1673 EGL_BLUE_SIZE, 1,
1674 EGL_ALPHA_SIZE, 0,
1675 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1676 EGL_NONE
1677};
1678
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001679static const EGLint gl_renderer_alpha_attribs[] = {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001680 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1681 EGL_RED_SIZE, 1,
1682 EGL_GREEN_SIZE, 1,
1683 EGL_BLUE_SIZE, 1,
1684 EGL_ALPHA_SIZE, 1,
1685 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1686 EGL_NONE
1687};
1688
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001689static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001690gl_renderer_create(struct weston_compositor *ec, EGLNativeDisplayType display,
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001691 const EGLint *attribs, const EGLint *visual_id)
1692{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001693 struct gl_renderer *gr;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001694 EGLint major, minor;
1695
1696 gr = calloc(1, sizeof *gr);
1697
1698 if (gr == NULL)
1699 return -1;
1700
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001701 gr->base.read_pixels = gl_renderer_read_pixels;
1702 gr->base.repaint_output = gl_renderer_repaint_output;
1703 gr->base.flush_damage = gl_renderer_flush_damage;
1704 gr->base.attach = gl_renderer_attach;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001705 gr->base.surface_set_color = gl_renderer_surface_set_color;
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03001706 gr->base.destroy = gl_renderer_destroy;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001707
1708 gr->egl_display = eglGetDisplay(display);
1709 if (gr->egl_display == EGL_NO_DISPLAY) {
1710 weston_log("failed to create display\n");
1711 goto err_egl;
1712 }
1713
1714 if (!eglInitialize(gr->egl_display, &major, &minor)) {
1715 weston_log("failed to initialize display\n");
1716 goto err_egl;
1717 }
1718
1719 if (egl_choose_config(gr, attribs, visual_id) < 0) {
1720 weston_log("failed to choose EGL config\n");
1721 goto err_egl;
1722 }
1723
1724 ec->renderer = &gr->base;
Pekka Paalanen7bb65102013-05-22 18:03:04 +03001725 ec->capabilities |= WESTON_CAP_ROTATION_ANY;
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03001726 ec->capabilities |= WESTON_CAP_CAPTURE_YFLIP;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001727
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001728 wl_display_add_shm_format(ec->wl_display, WL_SHM_FORMAT_RGB565);
1729
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03001730 wl_signal_init(&gr->destroy_signal);
1731
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001732 return 0;
1733
1734err_egl:
Pekka Paalanen326529f2012-11-27 12:25:25 +02001735 gl_renderer_print_egl_error_state();
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001736 free(gr);
1737 return -1;
1738}
1739
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001740static EGLDisplay
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001741gl_renderer_display(struct weston_compositor *ec)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001742{
1743 return get_renderer(ec)->egl_display;
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001744}
1745
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001746static int
1747compile_shaders(struct weston_compositor *ec)
1748{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001749 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker40684142012-11-13 19:10:25 +01001750
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03001751 gr->texture_shader_rgba.vertex_source = vertex_shader;
1752 gr->texture_shader_rgba.fragment_source = texture_fragment_shader_rgba;
1753
1754 gr->texture_shader_rgbx.vertex_source = vertex_shader;
1755 gr->texture_shader_rgbx.fragment_source = texture_fragment_shader_rgbx;
1756
1757 gr->texture_shader_egl_external.vertex_source = vertex_shader;
1758 gr->texture_shader_egl_external.fragment_source =
1759 texture_fragment_shader_egl_external;
1760
1761 gr->texture_shader_y_uv.vertex_source = vertex_shader;
1762 gr->texture_shader_y_uv.fragment_source = texture_fragment_shader_y_uv;
1763
1764 gr->texture_shader_y_u_v.vertex_source = vertex_shader;
1765 gr->texture_shader_y_u_v.fragment_source =
1766 texture_fragment_shader_y_u_v;
1767
Ander Conselvan de Oliveira41a50ea2013-11-27 17:43:51 +02001768 gr->texture_shader_y_xuxv.vertex_source = vertex_shader;
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03001769 gr->texture_shader_y_xuxv.fragment_source =
1770 texture_fragment_shader_y_xuxv;
1771
1772 gr->solid_shader.vertex_source = vertex_shader;
1773 gr->solid_shader.fragment_source = solid_fragment_shader;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001774
1775 return 0;
1776}
1777
1778static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04001779fragment_debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001780 void *data)
1781{
1782 struct weston_compositor *ec = data;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001783 struct gl_renderer *gr = get_renderer(ec);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001784 struct weston_output *output;
1785
John Kåre Alsaker40684142012-11-13 19:10:25 +01001786 gr->fragment_shader_debug ^= 1;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001787
John Kåre Alsaker40684142012-11-13 19:10:25 +01001788 shader_release(&gr->texture_shader_rgba);
1789 shader_release(&gr->texture_shader_rgbx);
1790 shader_release(&gr->texture_shader_egl_external);
1791 shader_release(&gr->texture_shader_y_uv);
1792 shader_release(&gr->texture_shader_y_u_v);
1793 shader_release(&gr->texture_shader_y_xuxv);
1794 shader_release(&gr->solid_shader);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001795
Ander Conselvan de Oliveira03fb4ef2012-12-03 17:08:11 +02001796 /* Force use_shader() to call glUseProgram(), since we need to use
1797 * the recompiled version of the shader. */
1798 gr->current_shader = NULL;
1799
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001800 wl_list_for_each(output, &ec->output_list, link)
1801 weston_output_damage(output);
1802}
1803
Kristian Høgsberg8799d412013-05-07 10:50:09 -04001804static void
1805fan_debug_repaint_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
1806 void *data)
1807{
1808 struct weston_compositor *compositor = data;
1809 struct gl_renderer *gr = get_renderer(compositor);
1810
1811 gr->fan_debug = !gr->fan_debug;
1812 weston_compositor_damage_all(compositor);
1813}
1814
John Kåre Alsaker94659272012-11-13 19:10:18 +01001815static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001816gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001817{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001818 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001819 const char *extensions;
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001820 EGLBoolean ret;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001821
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001822 static const EGLint context_attribs[] = {
1823 EGL_CONTEXT_CLIENT_VERSION, 2,
1824 EGL_NONE
1825 };
1826
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001827 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
1828 weston_log("failed to bind EGL_OPENGL_ES_API\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02001829 gl_renderer_print_egl_error_state();
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001830 return -1;
1831 }
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001832
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001833 log_egl_config_info(gr->egl_display, gr->egl_config);
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001834
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001835 gr->egl_context = eglCreateContext(gr->egl_display, gr->egl_config,
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001836 EGL_NO_CONTEXT, context_attribs);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001837 if (gr->egl_context == NULL) {
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001838 weston_log("failed to create context\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02001839 gl_renderer_print_egl_error_state();
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04001840 return -1;
1841 }
1842
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001843 ret = eglMakeCurrent(gr->egl_display, egl_surface,
1844 egl_surface, gr->egl_context);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001845 if (ret == EGL_FALSE) {
1846 weston_log("Failed to make EGL context current.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02001847 gl_renderer_print_egl_error_state();
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001848 return -1;
1849 }
1850
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001851 log_egl_gl_info(gr->egl_display);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001852
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001853 gr->image_target_texture_2d =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001854 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001855 gr->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1856 gr->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1857 gr->bind_display =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001858 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001859 gr->unbind_display =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001860 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001861 gr->query_buffer =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001862 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
1863
1864 extensions = (const char *) glGetString(GL_EXTENSIONS);
1865 if (!extensions) {
1866 weston_log("Retrieving GL extension string failed.\n");
1867 return -1;
1868 }
1869
1870 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
1871 weston_log("GL_EXT_texture_format_BGRA8888 not available\n");
1872 return -1;
1873 }
1874
1875 if (strstr(extensions, "GL_EXT_read_format_bgra"))
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01001876 ec->read_format = PIXMAN_a8r8g8b8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001877 else
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01001878 ec->read_format = PIXMAN_a8b8g8r8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001879
Kristian Høgsberg1c4f1632013-08-07 12:11:27 -07001880#ifdef GL_EXT_unpack_subimage
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001881 if (strstr(extensions, "GL_EXT_unpack_subimage"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001882 gr->has_unpack_subimage = 1;
Kristian Høgsberg1c4f1632013-08-07 12:11:27 -07001883#endif
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001884
1885 if (strstr(extensions, "GL_OES_EGL_image_external"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001886 gr->has_egl_image_external = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001887
1888 extensions =
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001889 (const char *) eglQueryString(gr->egl_display, EGL_EXTENSIONS);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001890 if (!extensions) {
1891 weston_log("Retrieving EGL extension string failed.\n");
1892 return -1;
1893 }
1894
1895 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001896 gr->has_bind_display = 1;
1897 if (gr->has_bind_display) {
1898 ret = gr->bind_display(gr->egl_display, ec->wl_display);
Pekka Paalanen035a0322012-10-24 09:43:06 +03001899 if (!ret)
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001900 gr->has_bind_display = 0;
Pekka Paalanen035a0322012-10-24 09:43:06 +03001901 }
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001902
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001903 if (strstr(extensions, "EGL_EXT_buffer_age"))
1904 gr->has_egl_buffer_age = 1;
1905 else
1906 weston_log("warning: EGL_EXT_buffer_age not supported. "
1907 "Performance could be affected.\n");
1908
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001909 glActiveTexture(GL_TEXTURE0);
1910
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001911 if (compile_shaders(ec))
1912 return -1;
1913
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03001914 gr->fragment_binding =
1915 weston_compositor_add_debug_binding(ec, KEY_S,
1916 fragment_debug_binding,
1917 ec);
1918 gr->fan_binding =
1919 weston_compositor_add_debug_binding(ec, KEY_F,
1920 fan_debug_repaint_binding,
1921 ec);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001922
Pekka Paalanen035a0322012-10-24 09:43:06 +03001923 weston_log("GL ES 2 renderer features:\n");
1924 weston_log_continue(STAMP_SPACE "read-back format: %s\n",
Pekka Paalanenfe4eacf2013-01-10 16:50:42 +02001925 ec->read_format == PIXMAN_a8r8g8b8 ? "BGRA" : "RGBA");
Pekka Paalanen035a0322012-10-24 09:43:06 +03001926 weston_log_continue(STAMP_SPACE "wl_shm sub-image to texture: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001927 gr->has_unpack_subimage ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03001928 weston_log_continue(STAMP_SPACE "EGL Wayland extension: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001929 gr->has_bind_display ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03001930
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001931
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001932 return 0;
1933}
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001934
1935WL_EXPORT struct gl_renderer_interface gl_renderer_interface = {
1936 .opaque_attribs = gl_renderer_opaque_attribs,
1937 .alpha_attribs = gl_renderer_alpha_attribs,
1938
1939 .create = gl_renderer_create,
1940 .display = gl_renderer_display,
1941 .output_create = gl_renderer_output_create,
1942 .output_destroy = gl_renderer_output_destroy,
1943 .output_surface = gl_renderer_output_surface,
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05001944 .output_set_border = gl_renderer_output_set_border,
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001945 .print_egl_error_state = gl_renderer_print_egl_error_state
1946};