blob: e598d1e940663eea96049db5339137e18534268a [file] [log] [blame]
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001/*
2 * Copyright © 2012 Intel Corporation
Pekka Paalaneneb35cbe2015-02-09 13:37:27 +02003 * Copyright © 2015 Collabora, Ltd.
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04004 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 */
23
Daniel Stonec228e232013-05-22 18:03:19 +030024#include "config.h"
Kristian Høgsberg25894fc2012-09-05 22:06:26 -040025
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010026#include <GLES2/gl2.h>
27#include <GLES2/gl2ext.h>
28
Derek Foremanf8180982014-10-16 16:37:02 -050029#include <stdbool.h>
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -040030#include <stdlib.h>
Kristian Høgsberg25894fc2012-09-05 22:06:26 -040031#include <string.h>
32#include <ctype.h>
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +030033#include <float.h>
34#include <assert.h>
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +020035#include <linux/input.h>
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -040036
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010037#include "gl-renderer.h"
Sam Spilsbury619859c2013-09-13 10:01:21 +080038#include "vertex-clipping.h"
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010039
40#include <EGL/eglext.h>
41#include "weston-egl-ext.h"
Kristian Høgsbergd7c17262012-09-05 21:54:15 -040042
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010043struct gl_shader {
John Kåre Alsaker40684142012-11-13 19:10:25 +010044 GLuint program;
45 GLuint vertex_shader, fragment_shader;
46 GLint proj_uniform;
47 GLint tex_uniforms[3];
48 GLint alpha_uniform;
49 GLint color_uniform;
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +030050 const char *vertex_source, *fragment_source;
John Kåre Alsaker40684142012-11-13 19:10:25 +010051};
52
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +020053#define BUFFER_DAMAGE_COUNT 2
54
Jason Ekstrande5512d42014-02-04 21:36:38 -060055enum gl_border_status {
56 BORDER_STATUS_CLEAN = 0,
57 BORDER_TOP_DIRTY = 1 << GL_RENDERER_BORDER_TOP,
58 BORDER_LEFT_DIRTY = 1 << GL_RENDERER_BORDER_LEFT,
59 BORDER_RIGHT_DIRTY = 1 << GL_RENDERER_BORDER_RIGHT,
60 BORDER_BOTTOM_DIRTY = 1 << GL_RENDERER_BORDER_BOTTOM,
61 BORDER_ALL_DIRTY = 0xf,
62 BORDER_SIZE_CHANGED = 0x10
63};
64
Jason Ekstrand0b61bf42013-10-27 22:24:54 -050065struct gl_border_image {
66 GLuint tex;
67 int32_t width, height;
68 int32_t tex_width;
Jason Ekstrand0b61bf42013-10-27 22:24:54 -050069 void *data;
70};
71
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010072struct gl_output_state {
John Kåre Alsaker94659272012-11-13 19:10:18 +010073 EGLSurface egl_surface;
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +020074 pixman_region32_t buffer_damage[BUFFER_DAMAGE_COUNT];
Derek Foreman4c582662014-10-09 18:39:44 -050075 int buffer_damage_index;
Jason Ekstrande5512d42014-02-04 21:36:38 -060076 enum gl_border_status border_damage[BUFFER_DAMAGE_COUNT];
Jason Ekstrand0b61bf42013-10-27 22:24:54 -050077 struct gl_border_image borders[4];
Jason Ekstrande5512d42014-02-04 21:36:38 -060078 enum gl_border_status border_status;
Jason Ekstrandfb23df72014-10-16 10:55:21 -050079
80 struct weston_matrix output_matrix;
John Kåre Alsaker94659272012-11-13 19:10:18 +010081};
82
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +030083enum buffer_type {
84 BUFFER_TYPE_NULL,
Pekka Paalanenaeb917e2015-02-09 13:56:56 +020085 BUFFER_TYPE_SOLID, /* internal solid color surfaces without a buffer */
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +030086 BUFFER_TYPE_SHM,
87 BUFFER_TYPE_EGL
88};
89
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010090struct gl_surface_state {
John Kåre Alsaker878f4492012-11-13 19:10:23 +010091 GLfloat color[4];
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010092 struct gl_shader *shader;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +010093
94 GLuint textures[3];
95 int num_textures;
Ander Conselvan de Oliveira6be5f432013-06-07 16:52:45 +030096 int needs_full_upload;
Pekka Paalanen81ee3f52012-12-04 15:58:16 +020097 pixman_region32_t texture_damage;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +010098
Neil Roberts4d085e72014-04-07 15:01:01 +010099 /* These are only used by SHM surfaces to detect when we need
100 * to do a full upload to specify a new internal texture
101 * format */
102 GLenum gl_format;
103 GLenum gl_pixel_type;
104
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100105 EGLImageKHR images[3];
106 GLenum target;
107 int num_images;
Pekka Paalanenfb003d32012-12-04 15:58:13 +0200108
109 struct weston_buffer_reference buffer_ref;
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +0300110 enum buffer_type buffer_type;
Pekka Paalanen68033ac2012-12-04 15:58:15 +0200111 int pitch; /* in pixels */
Alexander Larsson4ea95522013-05-22 14:41:37 +0200112 int height; /* in pixels */
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +0400113 int y_inverted;
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300114
115 struct weston_surface *surface;
116
117 struct wl_listener surface_destroy_listener;
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +0300118 struct wl_listener renderer_destroy_listener;
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100119};
120
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100121struct gl_renderer {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100122 struct weston_renderer base;
123 int fragment_shader_debug;
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400124 int fan_debug;
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +0300125 struct weston_binding *fragment_binding;
126 struct weston_binding *fan_binding;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100127
128 EGLDisplay egl_display;
129 EGLContext egl_context;
130 EGLConfig egl_config;
John Kåre Alsaker44154502012-11-13 19:10:20 +0100131
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400132 struct wl_array vertices;
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400133 struct wl_array vtxcnt;
134
John Kåre Alsaker320711d2012-11-13 19:10:27 +0100135 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
136 PFNEGLCREATEIMAGEKHRPROC create_image;
137 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
138
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -0600139#ifdef EGL_EXT_swap_buffers_with_damage
140 PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC swap_buffers_with_damage;
141#endif
142
John Kåre Alsaker320711d2012-11-13 19:10:27 +0100143 int has_unpack_subimage;
144
145 PFNEGLBINDWAYLANDDISPLAYWL bind_display;
146 PFNEGLUNBINDWAYLANDDISPLAYWL unbind_display;
147 PFNEGLQUERYWAYLANDBUFFERWL query_buffer;
148 int has_bind_display;
149
150 int has_egl_image_external;
151
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +0200152 int has_egl_buffer_age;
153
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000154 int has_configless_context;
155
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100156 struct gl_shader texture_shader_rgba;
157 struct gl_shader texture_shader_rgbx;
158 struct gl_shader texture_shader_egl_external;
159 struct gl_shader texture_shader_y_uv;
160 struct gl_shader texture_shader_y_u_v;
161 struct gl_shader texture_shader_y_xuxv;
162 struct gl_shader invert_color_shader;
163 struct gl_shader solid_shader;
164 struct gl_shader *current_shader;
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +0300165
166 struct wl_signal destroy_signal;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100167};
John Kåre Alsaker94659272012-11-13 19:10:18 +0100168
Jonny Lamb70eba3f2015-03-20 15:26:50 +0100169#ifdef EGL_EXT_platform_base
170static PFNEGLGETPLATFORMDISPLAYEXTPROC get_platform_display = NULL;
171#endif
172
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100173static inline struct gl_output_state *
John Kåre Alsaker94659272012-11-13 19:10:18 +0100174get_output_state(struct weston_output *output)
175{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100176 return (struct gl_output_state *)output->renderer_state;
John Kåre Alsaker94659272012-11-13 19:10:18 +0100177}
178
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300179static int
180gl_renderer_create_surface(struct weston_surface *surface);
181
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100182static inline struct gl_surface_state *
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100183get_surface_state(struct weston_surface *surface)
184{
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300185 if (!surface->renderer_state)
186 gl_renderer_create_surface(surface);
187
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100188 return (struct gl_surface_state *)surface->renderer_state;
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100189}
190
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100191static inline struct gl_renderer *
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100192get_renderer(struct weston_compositor *ec)
193{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100194 return (struct gl_renderer *)ec->renderer;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100195}
196
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400197static const char *
198egl_error_string(EGLint code)
199{
200#define MYERRCODE(x) case x: return #x;
201 switch (code) {
202 MYERRCODE(EGL_SUCCESS)
203 MYERRCODE(EGL_NOT_INITIALIZED)
204 MYERRCODE(EGL_BAD_ACCESS)
205 MYERRCODE(EGL_BAD_ALLOC)
206 MYERRCODE(EGL_BAD_ATTRIBUTE)
207 MYERRCODE(EGL_BAD_CONTEXT)
208 MYERRCODE(EGL_BAD_CONFIG)
209 MYERRCODE(EGL_BAD_CURRENT_SURFACE)
210 MYERRCODE(EGL_BAD_DISPLAY)
211 MYERRCODE(EGL_BAD_SURFACE)
212 MYERRCODE(EGL_BAD_MATCH)
213 MYERRCODE(EGL_BAD_PARAMETER)
214 MYERRCODE(EGL_BAD_NATIVE_PIXMAP)
215 MYERRCODE(EGL_BAD_NATIVE_WINDOW)
216 MYERRCODE(EGL_CONTEXT_LOST)
217 default:
218 return "unknown";
219 }
220#undef MYERRCODE
221}
222
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300223static void
Pekka Paalanen326529f2012-11-27 12:25:25 +0200224gl_renderer_print_egl_error_state(void)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400225{
226 EGLint code;
227
228 code = eglGetError();
229 weston_log("EGL error state: %s (0x%04lx)\n",
230 egl_error_string(code), (long)code);
231}
232
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400233#define max(a, b) (((a) > (b)) ? (a) : (b))
234#define min(a, b) (((a) > (b)) ? (b) : (a))
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400235
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300236/*
237 * Compute the boundary vertices of the intersection of the global coordinate
238 * aligned rectangle 'rect', and an arbitrary quadrilateral produced from
239 * 'surf_rect' when transformed from surface coordinates into global coordinates.
240 * The vertices are written to 'ex' and 'ey', and the return value is the
241 * number of vertices. Vertices are produced in clockwise winding order.
242 * Guarantees to produce either zero vertices, or 3-8 vertices with non-zero
243 * polygon area.
244 */
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400245static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500246calculate_edges(struct weston_view *ev, pixman_box32_t *rect,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400247 pixman_box32_t *surf_rect, GLfloat *ex, GLfloat *ey)
248{
Sam Spilsbury619859c2013-09-13 10:01:21 +0800249
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300250 struct clip_context ctx;
251 int i, n;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400252 GLfloat min_x, max_x, min_y, max_y;
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300253 struct polygon8 surf = {
254 { surf_rect->x1, surf_rect->x2, surf_rect->x2, surf_rect->x1 },
255 { surf_rect->y1, surf_rect->y1, surf_rect->y2, surf_rect->y2 },
256 4
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400257 };
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400258
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300259 ctx.clip.x1 = rect->x1;
260 ctx.clip.y1 = rect->y1;
261 ctx.clip.x2 = rect->x2;
262 ctx.clip.y2 = rect->y2;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400263
264 /* transform surface to screen space: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300265 for (i = 0; i < surf.n; i++)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500266 weston_view_to_global_float(ev, surf.x[i], surf.y[i],
267 &surf.x[i], &surf.y[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400268
269 /* find bounding box: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300270 min_x = max_x = surf.x[0];
271 min_y = max_y = surf.y[0];
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400272
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300273 for (i = 1; i < surf.n; i++) {
274 min_x = min(min_x, surf.x[i]);
275 max_x = max(max_x, surf.x[i]);
276 min_y = min(min_y, surf.y[i]);
277 max_y = max(max_y, surf.y[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400278 }
279
280 /* First, simple bounding box check to discard early transformed
281 * surface rects that do not intersect with the clip region:
282 */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300283 if ((min_x >= ctx.clip.x2) || (max_x <= ctx.clip.x1) ||
284 (min_y >= ctx.clip.y2) || (max_y <= ctx.clip.y1))
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400285 return 0;
286
287 /* Simple case, bounding box edges are parallel to surface edges,
288 * there will be only four edges. We just need to clip the surface
289 * vertices to the clip rect bounds:
290 */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500291 if (!ev->transform.enabled)
Sam Spilsbury619859c2013-09-13 10:01:21 +0800292 return clip_simple(&ctx, &surf, ex, ey);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400293
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300294 /* Transformed case: use a general polygon clipping algorithm to
295 * clip the surface rectangle with each side of 'rect'.
296 * The algorithm is Sutherland-Hodgman, as explained in
297 * http://www.codeguru.com/cpp/misc/misc/graphics/article.php/c8965/Polygon-Clipping.htm
298 * but without looking at any of that code.
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400299 */
Sam Spilsbury619859c2013-09-13 10:01:21 +0800300 n = clip_transformed(&ctx, &surf, ex, ey);
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300301
302 if (n < 3)
303 return 0;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400304
305 return n;
306}
307
Derek Foremanf8180982014-10-16 16:37:02 -0500308static bool
309merge_down(pixman_box32_t *a, pixman_box32_t *b, pixman_box32_t *merge)
310{
311 if (a->x1 == b->x1 && a->x2 == b->x2 && a->y1 == b->y2) {
312 merge->x1 = a->x1;
313 merge->x2 = a->x2;
314 merge->y1 = b->y1;
315 merge->y2 = a->y2;
316 return true;
317 }
318 return false;
319}
320
321static int
322compress_bands(pixman_box32_t *inrects, int nrects,
323 pixman_box32_t **outrects)
324{
325 bool merged;
326 pixman_box32_t *out, merge_rect;
327 int i, j, nout;
328
329 if (!nrects) {
330 *outrects = NULL;
331 return 0;
332 }
333
334 /* nrects is an upper bound - we're not too worried about
335 * allocating a little extra
336 */
337 out = malloc(sizeof(pixman_box32_t) * nrects);
338 out[0] = inrects[0];
339 nout = 1;
340 for (i = 1; i < nrects; i++) {
341 for (j = 0; j < nout; j++) {
342 merged = merge_down(&inrects[i], &out[j], &merge_rect);
343 if (merged) {
344 out[j] = merge_rect;
345 break;
346 }
347 }
348 if (!merged) {
349 out[nout] = inrects[i];
350 nout++;
351 }
352 }
353 *outrects = out;
354 return nout;
355}
356
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400357static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500358texture_region(struct weston_view *ev, pixman_region32_t *region,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400359 pixman_region32_t *surf_region)
360{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500361 struct gl_surface_state *gs = get_surface_state(ev->surface);
362 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400363 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400364 GLfloat *v, inv_width, inv_height;
365 unsigned int *vtxcnt, nvtx = 0;
366 pixman_box32_t *rects, *surf_rects;
Derek Foremanf8180982014-10-16 16:37:02 -0500367 pixman_box32_t *raw_rects;
368 int i, j, k, nrects, nsurf, raw_nrects;
369 bool used_band_compression;
370 raw_rects = pixman_region32_rectangles(region, &raw_nrects);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400371 surf_rects = pixman_region32_rectangles(surf_region, &nsurf);
372
Derek Foremanf8180982014-10-16 16:37:02 -0500373 if (raw_nrects < 4) {
374 used_band_compression = false;
375 nrects = raw_nrects;
376 rects = raw_rects;
377 } else {
378 nrects = compress_bands(raw_rects, raw_nrects, &rects);
379 used_band_compression = true;
380 }
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400381 /* worst case we can have 8 vertices per rect (ie. clipped into
382 * an octagon):
383 */
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400384 v = wl_array_add(&gr->vertices, nrects * nsurf * 8 * 4 * sizeof *v);
385 vtxcnt = wl_array_add(&gr->vtxcnt, nrects * nsurf * sizeof *vtxcnt);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400386
Pekka Paalanen68033ac2012-12-04 15:58:15 +0200387 inv_width = 1.0 / gs->pitch;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200388 inv_height = 1.0 / gs->height;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400389
390 for (i = 0; i < nrects; i++) {
391 pixman_box32_t *rect = &rects[i];
392 for (j = 0; j < nsurf; j++) {
393 pixman_box32_t *surf_rect = &surf_rects[j];
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200394 GLfloat sx, sy, bx, by;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400395 GLfloat ex[8], ey[8]; /* edge points in screen space */
396 int n;
397
398 /* The transformed surface, after clipping to the clip region,
399 * can have as many as eight sides, emitted as a triangle-fan.
400 * The first vertex in the triangle fan can be chosen arbitrarily,
401 * since the area is guaranteed to be convex.
402 *
403 * If a corner of the transformed surface falls outside of the
404 * clip region, instead of emitting one vertex for the corner
405 * of the surface, up to two are emitted for two corresponding
406 * intersection point(s) between the surface and the clip region.
407 *
408 * To do this, we first calculate the (up to eight) points that
409 * form the intersection of the clip rect and the transformed
410 * surface.
411 */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500412 n = calculate_edges(ev, rect, surf_rect, ex, ey);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400413 if (n < 3)
414 continue;
415
416 /* emit edge points: */
417 for (k = 0; k < n; k++) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500418 weston_view_from_global_float(ev, ex[k], ey[k],
419 &sx, &sy);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400420 /* position: */
421 *(v++) = ex[k];
422 *(v++) = ey[k];
423 /* texcoord: */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500424 weston_surface_to_buffer_float(ev->surface,
425 sx, sy,
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200426 &bx, &by);
427 *(v++) = bx * inv_width;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +0400428 if (gs->y_inverted) {
429 *(v++) = by * inv_height;
430 } else {
431 *(v++) = (gs->height - by) * inv_height;
432 }
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400433 }
434
435 vtxcnt[nvtx++] = n;
436 }
437 }
438
Derek Foremanf8180982014-10-16 16:37:02 -0500439 if (used_band_compression)
440 free(rects);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400441 return nvtx;
442}
443
444static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500445triangle_fan_debug(struct weston_view *view, int first, int count)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400446{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500447 struct weston_compositor *compositor = view->surface->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100448 struct gl_renderer *gr = get_renderer(compositor);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400449 int i;
450 GLushort *buffer;
451 GLushort *index;
452 int nelems;
453 static int color_idx = 0;
454 static const GLfloat color[][4] = {
455 { 1.0, 0.0, 0.0, 1.0 },
456 { 0.0, 1.0, 0.0, 1.0 },
457 { 0.0, 0.0, 1.0, 1.0 },
458 { 1.0, 1.0, 1.0, 1.0 },
459 };
460
461 nelems = (count - 1 + count - 2) * 2;
462
463 buffer = malloc(sizeof(GLushort) * nelems);
464 index = buffer;
465
466 for (i = 1; i < count; i++) {
467 *index++ = first;
468 *index++ = first + i;
469 }
470
471 for (i = 2; i < count; i++) {
472 *index++ = first + i - 1;
473 *index++ = first + i;
474 }
475
John Kåre Alsaker40684142012-11-13 19:10:25 +0100476 glUseProgram(gr->solid_shader.program);
477 glUniform4fv(gr->solid_shader.color_uniform, 1,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400478 color[color_idx++ % ARRAY_LENGTH(color)]);
479 glDrawElements(GL_LINES, nelems, GL_UNSIGNED_SHORT, buffer);
John Kåre Alsaker40684142012-11-13 19:10:25 +0100480 glUseProgram(gr->current_shader->program);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400481 free(buffer);
482}
483
484static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500485repaint_region(struct weston_view *ev, pixman_region32_t *region,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400486 pixman_region32_t *surf_region)
487{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500488 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400489 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400490 GLfloat *v;
491 unsigned int *vtxcnt;
492 int i, first, nfans;
493
494 /* The final region to be painted is the intersection of
495 * 'region' and 'surf_region'. However, 'region' is in the global
496 * coordinates, and 'surf_region' is in the surface-local
497 * coordinates. texture_region() will iterate over all pairs of
498 * rectangles from both regions, compute the intersection
499 * polygon for each pair, and store it as a triangle fan if
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400500 * it has a non-zero area (at least 3 vertices1, actually).
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400501 */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500502 nfans = texture_region(ev, region, surf_region);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400503
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400504 v = gr->vertices.data;
505 vtxcnt = gr->vtxcnt.data;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400506
507 /* position: */
508 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
509 glEnableVertexAttribArray(0);
510
511 /* texcoord: */
512 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
513 glEnableVertexAttribArray(1);
514
515 for (i = 0, first = 0; i < nfans; i++) {
516 glDrawArrays(GL_TRIANGLE_FAN, first, vtxcnt[i]);
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400517 if (gr->fan_debug)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500518 triangle_fan_debug(ev, first, vtxcnt[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400519 first += vtxcnt[i];
520 }
521
522 glDisableVertexAttribArray(1);
523 glDisableVertexAttribArray(0);
524
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400525 gr->vertices.size = 0;
526 gr->vtxcnt.size = 0;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400527}
528
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100529static int
530use_output(struct weston_output *output)
531{
532 static int errored;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100533 struct gl_output_state *go = get_output_state(output);
534 struct gl_renderer *gr = get_renderer(output->compositor);
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100535 EGLBoolean ret;
536
537 ret = eglMakeCurrent(gr->egl_display, go->egl_surface,
538 go->egl_surface, gr->egl_context);
539
540 if (ret == EGL_FALSE) {
541 if (errored)
542 return -1;
543 errored = 1;
544 weston_log("Failed to make EGL context current.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +0200545 gl_renderer_print_egl_error_state();
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100546 return -1;
547 }
548
549 return 0;
550}
551
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +0300552static int
553shader_init(struct gl_shader *shader, struct gl_renderer *gr,
554 const char *vertex_source, const char *fragment_source);
555
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400556static void
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +0300557use_shader(struct gl_renderer *gr, struct gl_shader *shader)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400558{
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +0300559 if (!shader->program) {
560 int ret;
561
562 ret = shader_init(shader, gr,
563 shader->vertex_source,
564 shader->fragment_source);
565
566 if (ret < 0)
567 weston_log("warning: failed to compile shader\n");
568 }
569
John Kåre Alsaker40684142012-11-13 19:10:25 +0100570 if (gr->current_shader == shader)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400571 return;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400572 glUseProgram(shader->program);
John Kåre Alsaker40684142012-11-13 19:10:25 +0100573 gr->current_shader = shader;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400574}
575
576static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100577shader_uniforms(struct gl_shader *shader,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500578 struct weston_view *view,
579 struct weston_output *output)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400580{
581 int i;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500582 struct gl_surface_state *gs = get_surface_state(view->surface);
Jason Ekstrandfb23df72014-10-16 10:55:21 -0500583 struct gl_output_state *go = get_output_state(output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400584
585 glUniformMatrix4fv(shader->proj_uniform,
Jason Ekstrandfb23df72014-10-16 10:55:21 -0500586 1, GL_FALSE, go->output_matrix.d);
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100587 glUniform4fv(shader->color_uniform, 1, gs->color);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500588 glUniform1f(shader->alpha_uniform, view->alpha);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400589
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100590 for (i = 0; i < gs->num_textures; i++)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400591 glUniform1i(shader->tex_uniforms[i], i);
592}
593
594static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500595draw_view(struct weston_view *ev, struct weston_output *output,
596 pixman_region32_t *damage) /* in global coordinates */
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400597{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500598 struct weston_compositor *ec = ev->surface->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100599 struct gl_renderer *gr = get_renderer(ec);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500600 struct gl_surface_state *gs = get_surface_state(ev->surface);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400601 /* repaint bounding region in global coordinates: */
602 pixman_region32_t repaint;
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +0200603 /* opaque region in surface coordinates: */
604 pixman_region32_t surface_opaque;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400605 /* non-opaque region in surface coordinates: */
606 pixman_region32_t surface_blend;
607 GLint filter;
608 int i;
609
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +0200610 /* In case of a runtime switch of renderers, we may not have received
611 * an attach for this surface since the switch. In that case we don't
612 * have a valid buffer or a proper shader set up so skip rendering. */
613 if (!gs->shader)
614 return;
615
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400616 pixman_region32_init(&repaint);
617 pixman_region32_intersect(&repaint,
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200618 &ev->transform.boundingbox, damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500619 pixman_region32_subtract(&repaint, &repaint, &ev->clip);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400620
621 if (!pixman_region32_not_empty(&repaint))
622 goto out;
623
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400624 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
625
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400626 if (gr->fan_debug) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100627 use_shader(gr, &gr->solid_shader);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500628 shader_uniforms(&gr->solid_shader, ev, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400629 }
630
John Kåre Alsaker40684142012-11-13 19:10:25 +0100631 use_shader(gr, gs->shader);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500632 shader_uniforms(gs->shader, ev, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400633
Jason Ekstranda7af7042013-10-12 22:38:11 -0500634 if (ev->transform.enabled || output->zoom.active ||
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200635 output->current_scale != ev->surface->buffer_viewport.buffer.scale)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400636 filter = GL_LINEAR;
637 else
638 filter = GL_NEAREST;
639
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100640 for (i = 0; i < gs->num_textures; i++) {
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400641 glActiveTexture(GL_TEXTURE0 + i);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100642 glBindTexture(gs->target, gs->textures[i]);
643 glTexParameteri(gs->target, GL_TEXTURE_MIN_FILTER, filter);
644 glTexParameteri(gs->target, GL_TEXTURE_MAG_FILTER, filter);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400645 }
646
647 /* blended region is whole surface minus opaque region: */
648 pixman_region32_init_rect(&surface_blend, 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600649 ev->surface->width, ev->surface->height);
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +0200650 if (ev->geometry.scissor_enabled)
651 pixman_region32_intersect(&surface_blend, &surface_blend,
652 &ev->geometry.scissor);
653 pixman_region32_subtract(&surface_blend, &surface_blend,
654 &ev->surface->opaque);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400655
Jason Ekstranda7af7042013-10-12 22:38:11 -0500656 /* XXX: Should we be using ev->transform.opaque here? */
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +0200657 pixman_region32_init(&surface_opaque);
658 if (ev->geometry.scissor_enabled)
659 pixman_region32_intersect(&surface_opaque,
660 &ev->surface->opaque,
661 &ev->geometry.scissor);
662 else
663 pixman_region32_copy(&surface_opaque, &ev->surface->opaque);
664
665 if (pixman_region32_not_empty(&surface_opaque)) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100666 if (gs->shader == &gr->texture_shader_rgba) {
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400667 /* Special case for RGBA textures with possibly
668 * bad data in alpha channel: use the shader
669 * that forces texture alpha = 1.0.
670 * Xwayland surfaces need this.
671 */
John Kåre Alsaker40684142012-11-13 19:10:25 +0100672 use_shader(gr, &gr->texture_shader_rgbx);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500673 shader_uniforms(&gr->texture_shader_rgbx, ev, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400674 }
675
Jason Ekstranda7af7042013-10-12 22:38:11 -0500676 if (ev->alpha < 1.0)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400677 glEnable(GL_BLEND);
678 else
679 glDisable(GL_BLEND);
680
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +0200681 repaint_region(ev, &repaint, &surface_opaque);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400682 }
683
684 if (pixman_region32_not_empty(&surface_blend)) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100685 use_shader(gr, gs->shader);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400686 glEnable(GL_BLEND);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500687 repaint_region(ev, &repaint, &surface_blend);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400688 }
689
690 pixman_region32_fini(&surface_blend);
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +0200691 pixman_region32_fini(&surface_opaque);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400692
693out:
694 pixman_region32_fini(&repaint);
695}
696
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400697static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500698repaint_views(struct weston_output *output, pixman_region32_t *damage)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400699{
700 struct weston_compositor *compositor = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500701 struct weston_view *view;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400702
Jason Ekstranda7af7042013-10-12 22:38:11 -0500703 wl_list_for_each_reverse(view, &compositor->view_list, link)
704 if (view->plane == &compositor->primary_plane)
705 draw_view(view, output, damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400706}
707
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500708static void
Jason Ekstrande5512d42014-02-04 21:36:38 -0600709draw_output_border_texture(struct gl_output_state *go,
710 enum gl_renderer_border_side side,
711 int32_t x, int32_t y,
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500712 int32_t width, int32_t height)
713{
Jason Ekstrande5512d42014-02-04 21:36:38 -0600714 struct gl_border_image *img = &go->borders[side];
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500715 static GLushort indices [] = { 0, 1, 3, 3, 1, 2 };
716
717 if (!img->data) {
718 if (img->tex) {
719 glDeleteTextures(1, &img->tex);
720 img->tex = 0;
721 }
722
723 return;
724 }
725
726 if (!img->tex) {
727 glGenTextures(1, &img->tex);
728 glBindTexture(GL_TEXTURE_2D, img->tex);
729
730 glTexParameteri(GL_TEXTURE_2D,
731 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
732 glTexParameteri(GL_TEXTURE_2D,
733 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
734 glTexParameteri(GL_TEXTURE_2D,
735 GL_TEXTURE_MIN_FILTER, GL_NEAREST);
736 glTexParameteri(GL_TEXTURE_2D,
737 GL_TEXTURE_MAG_FILTER, GL_NEAREST);
738 } else {
739 glBindTexture(GL_TEXTURE_2D, img->tex);
740 }
741
Jason Ekstrande5512d42014-02-04 21:36:38 -0600742 if (go->border_status & (1 << side)) {
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500743#ifdef GL_EXT_unpack_subimage
744 glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, 0);
745 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, 0);
746 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, 0);
747#endif
748 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
749 img->tex_width, img->height, 0,
750 GL_BGRA_EXT, GL_UNSIGNED_BYTE, img->data);
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500751 }
752
753 GLfloat texcoord[] = {
754 0.0f, 0.0f,
755 (GLfloat)img->width / (GLfloat)img->tex_width, 0.0f,
756 (GLfloat)img->width / (GLfloat)img->tex_width, 1.0f,
757 0.0f, 1.0f,
758 };
759
760 GLfloat verts[] = {
761 x, y,
762 x + width, y,
763 x + width, y + height,
764 x, y + height
765 };
766
767 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, verts);
768 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, texcoord);
769 glEnableVertexAttribArray(0);
770 glEnableVertexAttribArray(1);
771
772 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
773
774 glDisableVertexAttribArray(1);
775 glDisableVertexAttribArray(0);
776}
777
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -0600778static int
779output_has_borders(struct weston_output *output)
780{
781 struct gl_output_state *go = get_output_state(output);
782
783 return go->borders[GL_RENDERER_BORDER_TOP].data ||
784 go->borders[GL_RENDERER_BORDER_RIGHT].data ||
785 go->borders[GL_RENDERER_BORDER_BOTTOM].data ||
786 go->borders[GL_RENDERER_BORDER_LEFT].data;
787}
788
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500789static void
Jason Ekstrande5512d42014-02-04 21:36:38 -0600790draw_output_borders(struct weston_output *output,
791 enum gl_border_status border_status)
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500792{
793 struct gl_output_state *go = get_output_state(output);
794 struct gl_renderer *gr = get_renderer(output->compositor);
795 struct gl_shader *shader = &gr->texture_shader_rgba;
Jason Ekstrand00b84282013-10-27 22:24:59 -0500796 struct gl_border_image *top, *bottom, *left, *right;
797 struct weston_matrix matrix;
798 int full_width, full_height;
799
Jason Ekstrande5512d42014-02-04 21:36:38 -0600800 if (border_status == BORDER_STATUS_CLEAN)
801 return; /* Clean. Nothing to do. */
802
Jason Ekstrand00b84282013-10-27 22:24:59 -0500803 top = &go->borders[GL_RENDERER_BORDER_TOP];
804 bottom = &go->borders[GL_RENDERER_BORDER_BOTTOM];
805 left = &go->borders[GL_RENDERER_BORDER_LEFT];
806 right = &go->borders[GL_RENDERER_BORDER_RIGHT];
807
808 full_width = output->current_mode->width + left->width + right->width;
809 full_height = output->current_mode->height + top->height + bottom->height;
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500810
811 glDisable(GL_BLEND);
812 use_shader(gr, shader);
813
Jason Ekstrand00b84282013-10-27 22:24:59 -0500814 glViewport(0, 0, full_width, full_height);
815
816 weston_matrix_init(&matrix);
817 weston_matrix_translate(&matrix, -full_width/2.0, -full_height/2.0, 0);
818 weston_matrix_scale(&matrix, 2.0/full_width, -2.0/full_height, 1);
819 glUniformMatrix4fv(shader->proj_uniform, 1, GL_FALSE, matrix.d);
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500820
821 glUniform1i(shader->tex_uniforms[0], 0);
822 glUniform1f(shader->alpha_uniform, 1);
823 glActiveTexture(GL_TEXTURE0);
824
Jason Ekstrande5512d42014-02-04 21:36:38 -0600825 if (border_status & BORDER_TOP_DIRTY)
826 draw_output_border_texture(go, GL_RENDERER_BORDER_TOP,
827 0, 0,
828 full_width, top->height);
829 if (border_status & BORDER_LEFT_DIRTY)
830 draw_output_border_texture(go, GL_RENDERER_BORDER_LEFT,
831 0, top->height,
832 left->width, output->current_mode->height);
833 if (border_status & BORDER_RIGHT_DIRTY)
834 draw_output_border_texture(go, GL_RENDERER_BORDER_RIGHT,
835 full_width - right->width, top->height,
836 right->width, output->current_mode->height);
837 if (border_status & BORDER_BOTTOM_DIRTY)
838 draw_output_border_texture(go, GL_RENDERER_BORDER_BOTTOM,
839 0, full_height - bottom->height,
840 full_width, bottom->height);
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500841}
John Kåre Alsaker44154502012-11-13 19:10:20 +0100842
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400843static void
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -0600844output_get_border_damage(struct weston_output *output,
845 enum gl_border_status border_status,
846 pixman_region32_t *damage)
847{
848 struct gl_output_state *go = get_output_state(output);
849 struct gl_border_image *top, *bottom, *left, *right;
850 int full_width, full_height;
851
852 if (border_status == BORDER_STATUS_CLEAN)
853 return; /* Clean. Nothing to do. */
854
855 top = &go->borders[GL_RENDERER_BORDER_TOP];
856 bottom = &go->borders[GL_RENDERER_BORDER_BOTTOM];
857 left = &go->borders[GL_RENDERER_BORDER_LEFT];
858 right = &go->borders[GL_RENDERER_BORDER_RIGHT];
859
860 full_width = output->current_mode->width + left->width + right->width;
861 full_height = output->current_mode->height + top->height + bottom->height;
862 if (border_status & BORDER_TOP_DIRTY)
863 pixman_region32_union_rect(damage, damage,
864 0, 0,
865 full_width, top->height);
866 if (border_status & BORDER_LEFT_DIRTY)
867 pixman_region32_union_rect(damage, damage,
868 0, top->height,
869 left->width, output->current_mode->height);
870 if (border_status & BORDER_RIGHT_DIRTY)
871 pixman_region32_union_rect(damage, damage,
872 full_width - right->width, top->height,
873 right->width, output->current_mode->height);
874 if (border_status & BORDER_BOTTOM_DIRTY)
875 pixman_region32_union_rect(damage, damage,
876 0, full_height - bottom->height,
877 full_width, bottom->height);
878}
879
880static void
Jason Ekstrande5512d42014-02-04 21:36:38 -0600881output_get_damage(struct weston_output *output,
882 pixman_region32_t *buffer_damage, uint32_t *border_damage)
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +0200883{
884 struct gl_output_state *go = get_output_state(output);
885 struct gl_renderer *gr = get_renderer(output->compositor);
886 EGLint buffer_age = 0;
887 EGLBoolean ret;
888 int i;
889
890 if (gr->has_egl_buffer_age) {
891 ret = eglQuerySurface(gr->egl_display, go->egl_surface,
892 EGL_BUFFER_AGE_EXT, &buffer_age);
893 if (ret == EGL_FALSE) {
894 weston_log("buffer age query failed.\n");
895 gl_renderer_print_egl_error_state();
896 }
897 }
898
Jason Ekstrande5512d42014-02-04 21:36:38 -0600899 if (buffer_age == 0 || buffer_age - 1 > BUFFER_DAMAGE_COUNT) {
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +0200900 pixman_region32_copy(buffer_damage, &output->region);
Jason Ekstrande5512d42014-02-04 21:36:38 -0600901 *border_damage = BORDER_ALL_DIRTY;
902 } else {
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +0200903 for (i = 0; i < buffer_age - 1; i++)
Derek Foreman4c582662014-10-09 18:39:44 -0500904 *border_damage |= go->border_damage[(go->buffer_damage_index + i) % BUFFER_DAMAGE_COUNT];
Jason Ekstrande5512d42014-02-04 21:36:38 -0600905
906 if (*border_damage & BORDER_SIZE_CHANGED) {
907 /* If we've had a resize, we have to do a full
908 * repaint. */
909 *border_damage |= BORDER_ALL_DIRTY;
910 pixman_region32_copy(buffer_damage, &output->region);
911 } else {
912 for (i = 0; i < buffer_age - 1; i++)
913 pixman_region32_union(buffer_damage,
914 buffer_damage,
Derek Foreman4c582662014-10-09 18:39:44 -0500915 &go->buffer_damage[(go->buffer_damage_index + i) % BUFFER_DAMAGE_COUNT]);
Jason Ekstrande5512d42014-02-04 21:36:38 -0600916 }
917 }
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +0200918}
919
920static void
921output_rotate_damage(struct weston_output *output,
Jason Ekstrande5512d42014-02-04 21:36:38 -0600922 pixman_region32_t *output_damage,
923 enum gl_border_status border_status)
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +0200924{
925 struct gl_output_state *go = get_output_state(output);
926 struct gl_renderer *gr = get_renderer(output->compositor);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +0200927
928 if (!gr->has_egl_buffer_age)
929 return;
930
Derek Foreman4c582662014-10-09 18:39:44 -0500931 go->buffer_damage_index += BUFFER_DAMAGE_COUNT - 1;
932 go->buffer_damage_index %= BUFFER_DAMAGE_COUNT;
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +0200933
Derek Foreman4c582662014-10-09 18:39:44 -0500934 pixman_region32_copy(&go->buffer_damage[go->buffer_damage_index], output_damage);
935 go->border_damage[go->buffer_damage_index] = border_status;
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +0200936}
937
938static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100939gl_renderer_repaint_output(struct weston_output *output,
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400940 pixman_region32_t *output_damage)
941{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100942 struct gl_output_state *go = get_output_state(output);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400943 struct weston_compositor *compositor = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100944 struct gl_renderer *gr = get_renderer(compositor);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400945 EGLBoolean ret;
946 static int errored;
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -0600947#ifdef EGL_EXT_swap_buffers_with_damage
948 int i, nrects, buffer_height;
949 EGLint *egl_damage, *d;
950 pixman_box32_t *rects;
951#endif
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +0200952 pixman_region32_t buffer_damage, total_damage;
Jason Ekstrande5512d42014-02-04 21:36:38 -0600953 enum gl_border_status border_damage = BORDER_STATUS_CLEAN;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400954
Jason Ekstrandae0c6e32014-10-16 10:55:20 -0500955 if (use_output(output) < 0)
956 return;
957
Jason Ekstrand00b84282013-10-27 22:24:59 -0500958 /* Calculate the viewport */
959 glViewport(go->borders[GL_RENDERER_BORDER_LEFT].width,
960 go->borders[GL_RENDERER_BORDER_BOTTOM].height,
961 output->current_mode->width,
962 output->current_mode->height);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400963
Jason Ekstrandfb23df72014-10-16 10:55:21 -0500964 /* Calculate the global GL matrix */
965 go->output_matrix = output->matrix;
966 weston_matrix_translate(&go->output_matrix,
967 -(output->current_mode->width / 2.0),
968 -(output->current_mode->height / 2.0), 0);
969 weston_matrix_scale(&go->output_matrix,
970 2.0 / output->current_mode->width,
971 -2.0 / output->current_mode->height, 1);
972
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400973 /* if debugging, redraw everything outside the damage to clean up
974 * debug lines from the previous draw on this buffer:
975 */
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400976 if (gr->fan_debug) {
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400977 pixman_region32_t undamaged;
978 pixman_region32_init(&undamaged);
979 pixman_region32_subtract(&undamaged, &output->region,
980 output_damage);
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400981 gr->fan_debug = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500982 repaint_views(output, &undamaged);
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400983 gr->fan_debug = 1;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400984 pixman_region32_fini(&undamaged);
985 }
986
Ander Conselvan de Oliveirab605c062013-03-05 17:30:28 +0200987 pixman_region32_init(&total_damage);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +0200988 pixman_region32_init(&buffer_damage);
989
Jason Ekstrande5512d42014-02-04 21:36:38 -0600990 output_get_damage(output, &buffer_damage, &border_damage);
991 output_rotate_damage(output, output_damage, go->border_status);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +0200992
993 pixman_region32_union(&total_damage, &buffer_damage, output_damage);
Jason Ekstrande5512d42014-02-04 21:36:38 -0600994 border_damage |= go->border_status;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +0300995
Jason Ekstranda7af7042013-10-12 22:38:11 -0500996 repaint_views(output, &total_damage);
Ander Conselvan de Oliveirab605c062013-03-05 17:30:28 +0200997
998 pixman_region32_fini(&total_damage);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +0200999 pixman_region32_fini(&buffer_damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001000
Jason Ekstrande5512d42014-02-04 21:36:38 -06001001 draw_output_borders(output, border_damage);
John Kåre Alsaker44154502012-11-13 19:10:20 +01001002
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001003 pixman_region32_copy(&output->previous_damage, output_damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001004 wl_signal_emit(&output->frame_signal, output);
1005
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -06001006#ifdef EGL_EXT_swap_buffers_with_damage
1007 if (gr->swap_buffers_with_damage) {
1008 pixman_region32_init(&buffer_damage);
1009 weston_transformed_region(output->width, output->height,
1010 output->transform,
1011 output->current_scale,
1012 output_damage, &buffer_damage);
1013
1014 if (output_has_borders(output)) {
1015 pixman_region32_translate(&buffer_damage,
1016 go->borders[GL_RENDERER_BORDER_LEFT].width,
1017 go->borders[GL_RENDERER_BORDER_TOP].height);
1018 output_get_border_damage(output, go->border_status,
1019 &buffer_damage);
1020 }
1021
1022 rects = pixman_region32_rectangles(&buffer_damage, &nrects);
1023 egl_damage = malloc(nrects * 4 * sizeof(EGLint));
1024
1025 buffer_height = go->borders[GL_RENDERER_BORDER_TOP].height +
1026 output->current_mode->height +
1027 go->borders[GL_RENDERER_BORDER_BOTTOM].height;
1028
1029 d = egl_damage;
1030 for (i = 0; i < nrects; ++i) {
1031 *d++ = rects[i].x1;
1032 *d++ = buffer_height - rects[i].y2;
1033 *d++ = rects[i].x2 - rects[i].x1;
1034 *d++ = rects[i].y2 - rects[i].y1;
1035 }
1036 ret = gr->swap_buffers_with_damage(gr->egl_display,
1037 go->egl_surface,
1038 egl_damage, nrects);
1039 free(egl_damage);
1040 pixman_region32_fini(&buffer_damage);
1041 } else {
1042 ret = eglSwapBuffers(gr->egl_display, go->egl_surface);
1043 }
1044#else /* ! defined EGL_EXT_swap_buffers_with_damage */
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001045 ret = eglSwapBuffers(gr->egl_display, go->egl_surface);
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -06001046#endif
1047
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001048 if (ret == EGL_FALSE && !errored) {
1049 errored = 1;
1050 weston_log("Failed in eglSwapBuffers.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02001051 gl_renderer_print_egl_error_state();
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001052 }
1053
Jason Ekstrande5512d42014-02-04 21:36:38 -06001054 go->border_status = BORDER_STATUS_CLEAN;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001055}
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001056
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001057static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001058gl_renderer_read_pixels(struct weston_output *output,
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001059 pixman_format_code_t format, void *pixels,
1060 uint32_t x, uint32_t y,
1061 uint32_t width, uint32_t height)
1062{
1063 GLenum gl_format;
Jason Ekstrand701f6362014-04-02 19:53:59 -05001064 struct gl_output_state *go = get_output_state(output);
1065
1066 x += go->borders[GL_RENDERER_BORDER_LEFT].width;
1067 y += go->borders[GL_RENDERER_BORDER_BOTTOM].height;
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001068
1069 switch (format) {
1070 case PIXMAN_a8r8g8b8:
1071 gl_format = GL_BGRA_EXT;
1072 break;
1073 case PIXMAN_a8b8g8r8:
1074 gl_format = GL_RGBA;
1075 break;
1076 default:
1077 return -1;
1078 }
1079
1080 if (use_output(output) < 0)
1081 return -1;
1082
1083 glPixelStorei(GL_PACK_ALIGNMENT, 1);
1084 glReadPixels(x, y, width, height, gl_format,
1085 GL_UNSIGNED_BYTE, pixels);
1086
1087 return 0;
1088}
1089
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001090static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001091gl_renderer_flush_damage(struct weston_surface *surface)
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001092{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001093 struct gl_renderer *gr = get_renderer(surface->compositor);
1094 struct gl_surface_state *gs = get_surface_state(surface);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001095 struct weston_buffer *buffer = gs->buffer_ref.buffer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001096 struct weston_view *view;
1097 int texture_used;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001098
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -07001099#ifdef GL_EXT_unpack_subimage
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001100 pixman_box32_t *rectangles;
1101 void *data;
1102 int i, n;
1103#endif
1104
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001105 pixman_region32_union(&gs->texture_damage,
1106 &gs->texture_damage, &surface->damage);
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001107
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001108 if (!buffer)
1109 return;
1110
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001111 /* Avoid upload, if the texture won't be used this time.
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001112 * We still accumulate the damage in texture_damage, and
1113 * hold the reference to the buffer, in case the surface
1114 * migrates back to the primary plane.
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001115 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001116 texture_used = 0;
1117 wl_list_for_each(view, &surface->views, surface_link) {
1118 if (view->plane == &surface->compositor->primary_plane) {
1119 texture_used = 1;
1120 break;
1121 }
1122 }
1123 if (!texture_used)
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001124 return;
1125
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001126 if (!pixman_region32_not_empty(&gs->texture_damage) &&
1127 !gs->needs_full_upload)
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001128 goto done;
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001129
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001130 glBindTexture(GL_TEXTURE_2D, gs->textures[0]);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001131
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001132 if (!gr->has_unpack_subimage) {
Neil Robertse5051712013-11-13 15:44:06 +00001133 wl_shm_buffer_begin_access(buffer->shm_buffer);
Neil Roberts4d085e72014-04-07 15:01:01 +01001134 glTexImage2D(GL_TEXTURE_2D, 0, gs->gl_format,
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001135 gs->pitch, buffer->height, 0,
Neil Roberts4d085e72014-04-07 15:01:01 +01001136 gs->gl_format, gs->gl_pixel_type,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001137 wl_shm_buffer_get_data(buffer->shm_buffer));
Neil Robertse5051712013-11-13 15:44:06 +00001138 wl_shm_buffer_end_access(buffer->shm_buffer);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001139
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001140 goto done;
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001141 }
1142
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -07001143#ifdef GL_EXT_unpack_subimage
1144 glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, gs->pitch);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001145 data = wl_shm_buffer_get_data(buffer->shm_buffer);
Ander Conselvan de Oliveira6be5f432013-06-07 16:52:45 +03001146
1147 if (gs->needs_full_upload) {
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -07001148 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, 0);
1149 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, 0);
Neil Robertse5051712013-11-13 15:44:06 +00001150 wl_shm_buffer_begin_access(buffer->shm_buffer);
Neil Roberts4d085e72014-04-07 15:01:01 +01001151 glTexImage2D(GL_TEXTURE_2D, 0, gs->gl_format,
Neil Roberts39a443f2014-04-04 16:24:54 +01001152 gs->pitch, buffer->height, 0,
Neil Roberts4d085e72014-04-07 15:01:01 +01001153 gs->gl_format, gs->gl_pixel_type, data);
Neil Robertse5051712013-11-13 15:44:06 +00001154 wl_shm_buffer_end_access(buffer->shm_buffer);
Ander Conselvan de Oliveira6be5f432013-06-07 16:52:45 +03001155 goto done;
1156 }
1157
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001158 rectangles = pixman_region32_rectangles(&gs->texture_damage, &n);
Neil Robertse5051712013-11-13 15:44:06 +00001159 wl_shm_buffer_begin_access(buffer->shm_buffer);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001160 for (i = 0; i < n; i++) {
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001161 pixman_box32_t r;
1162
1163 r = weston_surface_to_buffer_rect(surface, rectangles[i]);
1164
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -07001165 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, r.x1);
1166 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, r.y1);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001167 glTexSubImage2D(GL_TEXTURE_2D, 0, r.x1, r.y1,
1168 r.x2 - r.x1, r.y2 - r.y1,
Neil Roberts4d085e72014-04-07 15:01:01 +01001169 gs->gl_format, gs->gl_pixel_type, data);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001170 }
Neil Robertse5051712013-11-13 15:44:06 +00001171 wl_shm_buffer_end_access(buffer->shm_buffer);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001172#endif
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001173
1174done:
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001175 pixman_region32_fini(&gs->texture_damage);
1176 pixman_region32_init(&gs->texture_damage);
Ander Conselvan de Oliveira6be5f432013-06-07 16:52:45 +03001177 gs->needs_full_upload = 0;
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001178
1179 weston_buffer_reference(&gs->buffer_ref, NULL);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001180}
1181
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001182static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001183ensure_textures(struct gl_surface_state *gs, int num_textures)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001184{
1185 int i;
1186
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001187 if (num_textures <= gs->num_textures)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001188 return;
1189
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001190 for (i = gs->num_textures; i < num_textures; i++) {
1191 glGenTextures(1, &gs->textures[i]);
1192 glBindTexture(gs->target, gs->textures[i]);
1193 glTexParameteri(gs->target,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001194 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001195 glTexParameteri(gs->target,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001196 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1197 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001198 gs->num_textures = num_textures;
1199 glBindTexture(gs->target, 0);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001200}
1201
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001202static void
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001203gl_renderer_attach_shm(struct weston_surface *es, struct weston_buffer *buffer,
1204 struct wl_shm_buffer *shm_buffer)
1205{
1206 struct weston_compositor *ec = es->compositor;
1207 struct gl_renderer *gr = get_renderer(ec);
1208 struct gl_surface_state *gs = get_surface_state(es);
Neil Roberts4d085e72014-04-07 15:01:01 +01001209 GLenum gl_format, gl_pixel_type;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001210 int pitch;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001211
1212 buffer->shm_buffer = shm_buffer;
1213 buffer->width = wl_shm_buffer_get_width(shm_buffer);
1214 buffer->height = wl_shm_buffer_get_height(shm_buffer);
1215
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001216 switch (wl_shm_buffer_get_format(shm_buffer)) {
1217 case WL_SHM_FORMAT_XRGB8888:
1218 gs->shader = &gr->texture_shader_rgbx;
1219 pitch = wl_shm_buffer_get_stride(shm_buffer) / 4;
Neil Roberts4d085e72014-04-07 15:01:01 +01001220 gl_format = GL_BGRA_EXT;
1221 gl_pixel_type = GL_UNSIGNED_BYTE;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001222 break;
1223 case WL_SHM_FORMAT_ARGB8888:
1224 gs->shader = &gr->texture_shader_rgba;
1225 pitch = wl_shm_buffer_get_stride(shm_buffer) / 4;
Neil Roberts4d085e72014-04-07 15:01:01 +01001226 gl_format = GL_BGRA_EXT;
1227 gl_pixel_type = GL_UNSIGNED_BYTE;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001228 break;
1229 case WL_SHM_FORMAT_RGB565:
1230 gs->shader = &gr->texture_shader_rgbx;
1231 pitch = wl_shm_buffer_get_stride(shm_buffer) / 2;
Neil Roberts4d085e72014-04-07 15:01:01 +01001232 gl_format = GL_RGB;
1233 gl_pixel_type = GL_UNSIGNED_SHORT_5_6_5;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001234 break;
1235 default:
Neil Roberts4d085e72014-04-07 15:01:01 +01001236 weston_log("warning: unknown shm buffer format: %08x\n",
1237 wl_shm_buffer_get_format(shm_buffer));
1238 return;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001239 }
1240
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001241 /* Only allocate a texture if it doesn't match existing one.
1242 * If a switch from DRM allocated buffer to a SHM buffer is
1243 * happening, we need to allocate a new texture buffer. */
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001244 if (pitch != gs->pitch ||
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001245 buffer->height != gs->height ||
Neil Roberts4d085e72014-04-07 15:01:01 +01001246 gl_format != gs->gl_format ||
1247 gl_pixel_type != gs->gl_pixel_type ||
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001248 gs->buffer_type != BUFFER_TYPE_SHM) {
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001249 gs->pitch = pitch;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001250 gs->height = buffer->height;
1251 gs->target = GL_TEXTURE_2D;
Neil Roberts4d085e72014-04-07 15:01:01 +01001252 gs->gl_format = gl_format;
1253 gs->gl_pixel_type = gl_pixel_type;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001254 gs->buffer_type = BUFFER_TYPE_SHM;
1255 gs->needs_full_upload = 1;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001256 gs->y_inverted = 1;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001257
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03001258 gs->surface = es;
1259
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001260 ensure_textures(gs, 1);
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001261 }
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001262}
1263
1264static void
1265gl_renderer_attach_egl(struct weston_surface *es, struct weston_buffer *buffer,
1266 uint32_t format)
1267{
1268 struct weston_compositor *ec = es->compositor;
1269 struct gl_renderer *gr = get_renderer(ec);
1270 struct gl_surface_state *gs = get_surface_state(es);
1271 EGLint attribs[3];
1272 int i, num_planes;
1273
1274 buffer->legacy_buffer = (struct wl_buffer *)buffer->resource;
1275 gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1276 EGL_WIDTH, &buffer->width);
1277 gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1278 EGL_HEIGHT, &buffer->height);
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001279 gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1280 EGL_WAYLAND_Y_INVERTED_WL, &buffer->y_inverted);
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001281
1282 for (i = 0; i < gs->num_images; i++)
1283 gr->destroy_image(gr->egl_display, gs->images[i]);
1284 gs->num_images = 0;
1285 gs->target = GL_TEXTURE_2D;
1286 switch (format) {
1287 case EGL_TEXTURE_RGB:
1288 case EGL_TEXTURE_RGBA:
1289 default:
1290 num_planes = 1;
1291 gs->shader = &gr->texture_shader_rgba;
1292 break;
1293 case EGL_TEXTURE_EXTERNAL_WL:
1294 num_planes = 1;
1295 gs->target = GL_TEXTURE_EXTERNAL_OES;
1296 gs->shader = &gr->texture_shader_egl_external;
1297 break;
1298 case EGL_TEXTURE_Y_UV_WL:
1299 num_planes = 2;
1300 gs->shader = &gr->texture_shader_y_uv;
1301 break;
1302 case EGL_TEXTURE_Y_U_V_WL:
1303 num_planes = 3;
1304 gs->shader = &gr->texture_shader_y_u_v;
1305 break;
1306 case EGL_TEXTURE_Y_XUXV_WL:
1307 num_planes = 2;
1308 gs->shader = &gr->texture_shader_y_xuxv;
1309 break;
1310 }
1311
1312 ensure_textures(gs, num_planes);
1313 for (i = 0; i < num_planes; i++) {
1314 attribs[0] = EGL_WAYLAND_PLANE_WL;
1315 attribs[1] = i;
1316 attribs[2] = EGL_NONE;
1317 gs->images[i] = gr->create_image(gr->egl_display,
1318 NULL,
1319 EGL_WAYLAND_BUFFER_WL,
1320 buffer->legacy_buffer,
1321 attribs);
1322 if (!gs->images[i]) {
1323 weston_log("failed to create img for plane %d\n", i);
1324 continue;
1325 }
1326 gs->num_images++;
1327
1328 glActiveTexture(GL_TEXTURE0 + i);
1329 glBindTexture(gs->target, gs->textures[i]);
1330 gr->image_target_texture_2d(gs->target,
1331 gs->images[i]);
1332 }
1333
1334 gs->pitch = buffer->width;
1335 gs->height = buffer->height;
1336 gs->buffer_type = BUFFER_TYPE_EGL;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001337 gs->y_inverted = buffer->y_inverted;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001338}
1339
1340static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001341gl_renderer_attach(struct weston_surface *es, struct weston_buffer *buffer)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001342{
1343 struct weston_compositor *ec = es->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001344 struct gl_renderer *gr = get_renderer(ec);
1345 struct gl_surface_state *gs = get_surface_state(es);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001346 struct wl_shm_buffer *shm_buffer;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001347 EGLint format;
1348 int i;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001349
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001350 weston_buffer_reference(&gs->buffer_ref, buffer);
1351
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001352 if (!buffer) {
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001353 for (i = 0; i < gs->num_images; i++) {
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001354 gr->destroy_image(gr->egl_display, gs->images[i]);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001355 gs->images[i] = NULL;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001356 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001357 gs->num_images = 0;
1358 glDeleteTextures(gs->num_textures, gs->textures);
1359 gs->num_textures = 0;
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +03001360 gs->buffer_type = BUFFER_TYPE_NULL;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001361 gs->y_inverted = 1;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001362 return;
1363 }
1364
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001365 shm_buffer = wl_shm_buffer_get(buffer->resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001366
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001367 if (shm_buffer)
1368 gl_renderer_attach_shm(es, buffer, shm_buffer);
Kristian Høgsberg47229392013-08-07 11:59:54 -07001369 else if (gr->query_buffer(gr->egl_display, (void *) buffer->resource,
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001370 EGL_TEXTURE_FORMAT, &format))
1371 gl_renderer_attach_egl(es, buffer, format);
1372 else {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001373 weston_log("unhandled buffer type!\n");
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001374 weston_buffer_reference(&gs->buffer_ref, NULL);
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +03001375 gs->buffer_type = BUFFER_TYPE_NULL;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001376 gs->y_inverted = 1;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001377 }
1378}
1379
Kristian Høgsberg42263852012-09-06 21:59:29 -04001380static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001381gl_renderer_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001382 float red, float green, float blue, float alpha)
1383{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001384 struct gl_surface_state *gs = get_surface_state(surface);
1385 struct gl_renderer *gr = get_renderer(surface->compositor);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001386
1387 gs->color[0] = red;
1388 gs->color[1] = green;
1389 gs->color[2] = blue;
1390 gs->color[3] = alpha;
Pekka Paalanenaeb917e2015-02-09 13:56:56 +02001391 gs->buffer_type = BUFFER_TYPE_SOLID;
1392 gs->pitch = 1;
1393 gs->height = 1;
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001394
John Kåre Alsaker40684142012-11-13 19:10:25 +01001395 gs->shader = &gr->solid_shader;
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001396}
1397
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03001398static void
Pekka Paalaneneb35cbe2015-02-09 13:37:27 +02001399gl_renderer_surface_get_content_size(struct weston_surface *surface,
1400 int *width, int *height)
1401{
1402 struct gl_surface_state *gs = get_surface_state(surface);
1403
1404 if (gs->buffer_type == BUFFER_TYPE_NULL) {
1405 *width = 0;
1406 *height = 0;
1407 } else {
1408 *width = gs->pitch;
1409 *height = gs->height;
1410 }
1411}
1412
1413static uint32_t
1414pack_color(pixman_format_code_t format, float *c)
1415{
1416 uint8_t r = round(c[0] * 255.0f);
1417 uint8_t g = round(c[1] * 255.0f);
1418 uint8_t b = round(c[2] * 255.0f);
1419 uint8_t a = round(c[3] * 255.0f);
1420
1421 switch (format) {
1422 case PIXMAN_a8b8g8r8:
1423 return (a << 24) | (b << 16) | (g << 8) | r;
1424 default:
1425 assert(0);
1426 return 0;
1427 }
1428}
1429
1430static int
1431gl_renderer_surface_copy_content(struct weston_surface *surface,
1432 void *target, size_t size,
1433 int src_x, int src_y,
1434 int width, int height)
1435{
1436 static const GLfloat verts[4 * 2] = {
1437 0.0f, 0.0f,
1438 1.0f, 0.0f,
1439 1.0f, 1.0f,
1440 0.0f, 1.0f
1441 };
1442 static const GLfloat projmat_normal[16] = { /* transpose */
1443 2.0f, 0.0f, 0.0f, 0.0f,
1444 0.0f, 2.0f, 0.0f, 0.0f,
1445 0.0f, 0.0f, 1.0f, 0.0f,
1446 -1.0f, -1.0f, 0.0f, 1.0f
1447 };
1448 static const GLfloat projmat_yinvert[16] = { /* transpose */
1449 2.0f, 0.0f, 0.0f, 0.0f,
1450 0.0f, -2.0f, 0.0f, 0.0f,
1451 0.0f, 0.0f, 1.0f, 0.0f,
1452 -1.0f, 1.0f, 0.0f, 1.0f
1453 };
1454 const pixman_format_code_t format = PIXMAN_a8b8g8r8;
1455 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
1456 const GLenum gl_format = GL_RGBA; /* PIXMAN_a8b8g8r8 little-endian */
1457 struct gl_renderer *gr = get_renderer(surface->compositor);
1458 struct gl_surface_state *gs = get_surface_state(surface);
1459 int cw, ch;
1460 GLuint fbo;
1461 GLuint tex;
1462 GLenum status;
1463 const GLfloat *proj;
1464 int i;
1465
1466 gl_renderer_surface_get_content_size(surface, &cw, &ch);
1467
1468 switch (gs->buffer_type) {
1469 case BUFFER_TYPE_NULL:
1470 return -1;
1471 case BUFFER_TYPE_SOLID:
1472 *(uint32_t *)target = pack_color(format, gs->color);
1473 return 0;
1474 case BUFFER_TYPE_SHM:
1475 gl_renderer_flush_damage(surface);
1476 /* fall through */
1477 case BUFFER_TYPE_EGL:
1478 break;
1479 }
1480
1481 glGenTextures(1, &tex);
1482 glBindTexture(GL_TEXTURE_2D, tex);
1483 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, cw, ch,
1484 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
1485 glBindTexture(GL_TEXTURE_2D, 0);
1486
1487 glGenFramebuffers(1, &fbo);
1488 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
1489 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
1490 GL_TEXTURE_2D, tex, 0);
1491
1492 status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
1493 if (status != GL_FRAMEBUFFER_COMPLETE) {
1494 weston_log("%s: fbo error: %#x\n", __func__, status);
1495 glDeleteFramebuffers(1, &fbo);
1496 glDeleteTextures(1, &tex);
1497 return -1;
1498 }
1499
1500 glViewport(0, 0, cw, ch);
1501 glDisable(GL_BLEND);
1502 use_shader(gr, gs->shader);
1503 if (gs->y_inverted)
1504 proj = projmat_normal;
1505 else
1506 proj = projmat_yinvert;
1507
1508 glUniformMatrix4fv(gs->shader->proj_uniform, 1, GL_FALSE, proj);
1509 glUniform1f(gs->shader->alpha_uniform, 1.0f);
1510
1511 for (i = 0; i < gs->num_textures; i++) {
1512 glUniform1i(gs->shader->tex_uniforms[i], i);
1513
1514 glActiveTexture(GL_TEXTURE0 + i);
1515 glBindTexture(gs->target, gs->textures[i]);
1516 glTexParameteri(gs->target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1517 glTexParameteri(gs->target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1518 }
1519
1520 /* position: */
1521 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, verts);
1522 glEnableVertexAttribArray(0);
1523
1524 /* texcoord: */
1525 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, verts);
1526 glEnableVertexAttribArray(1);
1527
1528 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1529
1530 glDisableVertexAttribArray(1);
1531 glDisableVertexAttribArray(0);
1532
1533 glPixelStorei(GL_PACK_ALIGNMENT, bytespp);
1534 glReadPixels(src_x, src_y, width, height, gl_format,
1535 GL_UNSIGNED_BYTE, target);
1536
1537 glDeleteFramebuffers(1, &fbo);
1538 glDeleteTextures(1, &tex);
1539
1540 return 0;
1541}
1542
1543static void
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03001544surface_state_destroy(struct gl_surface_state *gs, struct gl_renderer *gr)
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03001545{
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03001546 int i;
1547
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03001548 wl_list_remove(&gs->surface_destroy_listener.link);
1549 wl_list_remove(&gs->renderer_destroy_listener.link);
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03001550
1551 gs->surface->renderer_state = NULL;
1552
1553 glDeleteTextures(gs->num_textures, gs->textures);
1554
1555 for (i = 0; i < gs->num_images; i++)
1556 gr->destroy_image(gr->egl_display, gs->images[i]);
1557
1558 weston_buffer_reference(&gs->buffer_ref, NULL);
1559 pixman_region32_fini(&gs->texture_damage);
1560 free(gs);
1561}
1562
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03001563static void
1564surface_state_handle_surface_destroy(struct wl_listener *listener, void *data)
1565{
1566 struct gl_surface_state *gs;
1567 struct gl_renderer *gr;
1568
1569 gs = container_of(listener, struct gl_surface_state,
1570 surface_destroy_listener);
1571
1572 gr = get_renderer(gs->surface->compositor);
1573
1574 surface_state_destroy(gs, gr);
1575}
1576
1577static void
1578surface_state_handle_renderer_destroy(struct wl_listener *listener, void *data)
1579{
1580 struct gl_surface_state *gs;
1581 struct gl_renderer *gr;
1582
1583 gr = data;
1584
1585 gs = container_of(listener, struct gl_surface_state,
1586 renderer_destroy_listener);
1587
1588 surface_state_destroy(gs, gr);
1589}
1590
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001591static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001592gl_renderer_create_surface(struct weston_surface *surface)
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001593{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001594 struct gl_surface_state *gs;
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03001595 struct gl_renderer *gr = get_renderer(surface->compositor);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001596
Bryce Harringtonde16d892014-11-20 22:21:57 -08001597 gs = zalloc(sizeof *gs);
1598 if (gs == NULL)
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001599 return -1;
1600
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001601 /* A buffer is never attached to solid color surfaces, yet
1602 * they still go through texcoord computations. Do not divide
1603 * by zero there.
1604 */
1605 gs->pitch = 1;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001606 gs->y_inverted = 1;
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001607
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03001608 gs->surface = surface;
1609
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001610 pixman_region32_init(&gs->texture_damage);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001611 surface->renderer_state = gs;
1612
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03001613 gs->surface_destroy_listener.notify =
1614 surface_state_handle_surface_destroy;
1615 wl_signal_add(&surface->destroy_signal,
1616 &gs->surface_destroy_listener);
1617
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03001618 gs->renderer_destroy_listener.notify =
1619 surface_state_handle_renderer_destroy;
1620 wl_signal_add(&gr->destroy_signal,
1621 &gs->renderer_destroy_listener);
1622
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001623 if (surface->buffer_ref.buffer) {
1624 gl_renderer_attach(surface, surface->buffer_ref.buffer);
1625 gl_renderer_flush_damage(surface);
1626 }
1627
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001628 return 0;
1629}
1630
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001631static const char vertex_shader[] =
1632 "uniform mat4 proj;\n"
1633 "attribute vec2 position;\n"
1634 "attribute vec2 texcoord;\n"
1635 "varying vec2 v_texcoord;\n"
1636 "void main()\n"
1637 "{\n"
1638 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
1639 " v_texcoord = texcoord;\n"
1640 "}\n";
1641
1642/* Declare common fragment shader uniforms */
1643#define FRAGMENT_CONVERT_YUV \
1644 " y *= alpha;\n" \
1645 " u *= alpha;\n" \
1646 " v *= alpha;\n" \
1647 " gl_FragColor.r = y + 1.59602678 * v;\n" \
1648 " gl_FragColor.g = y - 0.39176229 * u - 0.81296764 * v;\n" \
1649 " gl_FragColor.b = y + 2.01723214 * u;\n" \
1650 " gl_FragColor.a = alpha;\n"
1651
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001652static const char fragment_debug[] =
1653 " gl_FragColor = vec4(0.0, 0.3, 0.0, 0.2) + gl_FragColor * 0.8;\n";
1654
1655static const char fragment_brace[] =
1656 "}\n";
1657
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001658static const char texture_fragment_shader_rgba[] =
1659 "precision mediump float;\n"
1660 "varying vec2 v_texcoord;\n"
1661 "uniform sampler2D tex;\n"
1662 "uniform float alpha;\n"
1663 "void main()\n"
1664 "{\n"
1665 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001666 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001667
1668static const char texture_fragment_shader_rgbx[] =
1669 "precision mediump float;\n"
1670 "varying vec2 v_texcoord;\n"
1671 "uniform sampler2D tex;\n"
1672 "uniform float alpha;\n"
1673 "void main()\n"
1674 "{\n"
1675 " gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb\n;"
1676 " gl_FragColor.a = alpha;\n"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001677 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001678
1679static const char texture_fragment_shader_egl_external[] =
1680 "#extension GL_OES_EGL_image_external : require\n"
1681 "precision mediump float;\n"
1682 "varying vec2 v_texcoord;\n"
1683 "uniform samplerExternalOES tex;\n"
1684 "uniform float alpha;\n"
1685 "void main()\n"
1686 "{\n"
1687 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001688 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001689
1690static const char texture_fragment_shader_y_uv[] =
1691 "precision mediump float;\n"
1692 "uniform sampler2D tex;\n"
1693 "uniform sampler2D tex1;\n"
1694 "varying vec2 v_texcoord;\n"
1695 "uniform float alpha;\n"
1696 "void main() {\n"
1697 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1698 " float u = texture2D(tex1, v_texcoord).r - 0.5;\n"
1699 " float v = texture2D(tex1, v_texcoord).g - 0.5;\n"
1700 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001701 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001702
1703static const char texture_fragment_shader_y_u_v[] =
1704 "precision mediump float;\n"
1705 "uniform sampler2D tex;\n"
1706 "uniform sampler2D tex1;\n"
1707 "uniform sampler2D tex2;\n"
1708 "varying vec2 v_texcoord;\n"
1709 "uniform float alpha;\n"
1710 "void main() {\n"
1711 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1712 " float u = texture2D(tex1, v_texcoord).x - 0.5;\n"
1713 " float v = texture2D(tex2, v_texcoord).x - 0.5;\n"
1714 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001715 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001716
1717static const char texture_fragment_shader_y_xuxv[] =
1718 "precision mediump float;\n"
1719 "uniform sampler2D tex;\n"
1720 "uniform sampler2D tex1;\n"
1721 "varying vec2 v_texcoord;\n"
1722 "uniform float alpha;\n"
1723 "void main() {\n"
1724 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
1725 " float u = texture2D(tex1, v_texcoord).g - 0.5;\n"
1726 " float v = texture2D(tex1, v_texcoord).a - 0.5;\n"
1727 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001728 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001729
1730static const char solid_fragment_shader[] =
1731 "precision mediump float;\n"
1732 "uniform vec4 color;\n"
1733 "uniform float alpha;\n"
1734 "void main()\n"
1735 "{\n"
1736 " gl_FragColor = alpha * color\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001737 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001738
1739static int
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001740compile_shader(GLenum type, int count, const char **sources)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001741{
1742 GLuint s;
1743 char msg[512];
1744 GLint status;
1745
1746 s = glCreateShader(type);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001747 glShaderSource(s, count, sources, NULL);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001748 glCompileShader(s);
1749 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
1750 if (!status) {
1751 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1752 weston_log("shader info: %s\n", msg);
1753 return GL_NONE;
1754 }
1755
1756 return s;
1757}
1758
1759static int
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03001760shader_init(struct gl_shader *shader, struct gl_renderer *renderer,
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001761 const char *vertex_source, const char *fragment_source)
1762{
1763 char msg[512];
1764 GLint status;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001765 int count;
1766 const char *sources[3];
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001767
1768 shader->vertex_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001769 compile_shader(GL_VERTEX_SHADER, 1, &vertex_source);
1770
1771 if (renderer->fragment_shader_debug) {
1772 sources[0] = fragment_source;
1773 sources[1] = fragment_debug;
1774 sources[2] = fragment_brace;
1775 count = 3;
1776 } else {
1777 sources[0] = fragment_source;
1778 sources[1] = fragment_brace;
1779 count = 2;
1780 }
1781
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001782 shader->fragment_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001783 compile_shader(GL_FRAGMENT_SHADER, count, sources);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001784
1785 shader->program = glCreateProgram();
1786 glAttachShader(shader->program, shader->vertex_shader);
1787 glAttachShader(shader->program, shader->fragment_shader);
1788 glBindAttribLocation(shader->program, 0, "position");
1789 glBindAttribLocation(shader->program, 1, "texcoord");
1790
1791 glLinkProgram(shader->program);
1792 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1793 if (!status) {
1794 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1795 weston_log("link info: %s\n", msg);
1796 return -1;
1797 }
1798
1799 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1800 shader->tex_uniforms[0] = glGetUniformLocation(shader->program, "tex");
1801 shader->tex_uniforms[1] = glGetUniformLocation(shader->program, "tex1");
1802 shader->tex_uniforms[2] = glGetUniformLocation(shader->program, "tex2");
1803 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
1804 shader->color_uniform = glGetUniformLocation(shader->program, "color");
1805
1806 return 0;
1807}
1808
1809static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001810shader_release(struct gl_shader *shader)
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02001811{
1812 glDeleteShader(shader->vertex_shader);
1813 glDeleteShader(shader->fragment_shader);
1814 glDeleteProgram(shader->program);
1815
1816 shader->vertex_shader = 0;
1817 shader->fragment_shader = 0;
1818 shader->program = 0;
1819}
1820
1821static void
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001822log_extensions(const char *name, const char *extensions)
1823{
1824 const char *p, *end;
1825 int l;
1826 int len;
1827
1828 l = weston_log("%s:", name);
1829 p = extensions;
1830 while (*p) {
1831 end = strchrnul(p, ' ');
1832 len = end - p;
1833 if (l + len > 78)
1834 l = weston_log_continue("\n" STAMP_SPACE "%.*s",
1835 len, p);
1836 else
1837 l += weston_log_continue(" %.*s", len, p);
1838 for (p = end; isspace(*p); p++)
1839 ;
1840 }
1841 weston_log_continue("\n");
1842}
1843
1844static void
1845log_egl_gl_info(EGLDisplay egldpy)
1846{
1847 const char *str;
1848
1849 str = eglQueryString(egldpy, EGL_VERSION);
1850 weston_log("EGL version: %s\n", str ? str : "(null)");
1851
1852 str = eglQueryString(egldpy, EGL_VENDOR);
1853 weston_log("EGL vendor: %s\n", str ? str : "(null)");
1854
1855 str = eglQueryString(egldpy, EGL_CLIENT_APIS);
1856 weston_log("EGL client APIs: %s\n", str ? str : "(null)");
1857
1858 str = eglQueryString(egldpy, EGL_EXTENSIONS);
1859 log_extensions("EGL extensions", str ? str : "(null)");
1860
1861 str = (char *)glGetString(GL_VERSION);
1862 weston_log("GL version: %s\n", str ? str : "(null)");
1863
1864 str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
1865 weston_log("GLSL version: %s\n", str ? str : "(null)");
1866
1867 str = (char *)glGetString(GL_VENDOR);
1868 weston_log("GL vendor: %s\n", str ? str : "(null)");
1869
1870 str = (char *)glGetString(GL_RENDERER);
1871 weston_log("GL renderer: %s\n", str ? str : "(null)");
1872
1873 str = (char *)glGetString(GL_EXTENSIONS);
1874 log_extensions("GL extensions", str ? str : "(null)");
1875}
1876
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03001877static void
1878log_egl_config_info(EGLDisplay egldpy, EGLConfig eglconfig)
1879{
1880 EGLint r, g, b, a;
1881
1882 weston_log("Chosen EGL config details:\n");
1883
1884 weston_log_continue(STAMP_SPACE "RGBA bits");
1885 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_RED_SIZE, &r) &&
1886 eglGetConfigAttrib(egldpy, eglconfig, EGL_GREEN_SIZE, &g) &&
1887 eglGetConfigAttrib(egldpy, eglconfig, EGL_BLUE_SIZE, &b) &&
1888 eglGetConfigAttrib(egldpy, eglconfig, EGL_ALPHA_SIZE, &a))
1889 weston_log_continue(": %d %d %d %d\n", r, g, b, a);
1890 else
1891 weston_log_continue(" unknown\n");
1892
1893 weston_log_continue(STAMP_SPACE "swap interval range");
1894 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_MIN_SWAP_INTERVAL, &a) &&
1895 eglGetConfigAttrib(egldpy, eglconfig, EGL_MAX_SWAP_INTERVAL, &b))
1896 weston_log_continue(": %d - %d\n", a, b);
1897 else
1898 weston_log_continue(" unknown\n");
1899}
1900
Neil Roberts77c1a5b2014-03-07 18:05:50 +00001901static int
1902egl_choose_config(struct gl_renderer *gr, const EGLint *attribs,
1903 const EGLint *visual_id,
1904 EGLConfig *config_out)
1905{
1906 EGLint count = 0;
1907 EGLint matched = 0;
1908 EGLConfig *configs;
1909 int i;
1910
1911 if (!eglGetConfigs(gr->egl_display, NULL, 0, &count) || count < 1)
1912 return -1;
1913
1914 configs = calloc(count, sizeof *configs);
1915 if (!configs)
1916 return -1;
1917
1918 if (!eglChooseConfig(gr->egl_display, attribs, configs,
1919 count, &matched))
1920 goto out;
1921
1922 for (i = 0; i < matched; ++i) {
1923 EGLint id;
1924
1925 if (visual_id) {
1926 if (!eglGetConfigAttrib(gr->egl_display,
1927 configs[i], EGL_NATIVE_VISUAL_ID,
1928 &id))
1929 continue;
1930
1931 if (id != 0 && id != *visual_id)
1932 continue;
1933 }
1934
1935 *config_out = configs[i];
1936
1937 free(configs);
1938 return 0;
1939 }
1940
1941out:
1942 free(configs);
1943 return -1;
1944}
1945
John Kåre Alsaker44154502012-11-13 19:10:20 +01001946static void
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05001947gl_renderer_output_set_border(struct weston_output *output,
1948 enum gl_renderer_border_side side,
1949 int32_t width, int32_t height,
1950 int32_t tex_width, unsigned char *data)
1951{
1952 struct gl_output_state *go = get_output_state(output);
1953
Jason Ekstrande5512d42014-02-04 21:36:38 -06001954 if (go->borders[side].width != width ||
1955 go->borders[side].height != height)
1956 /* In this case, we have to blow everything and do a full
1957 * repaint. */
1958 go->border_status |= BORDER_SIZE_CHANGED | BORDER_ALL_DIRTY;
1959
1960 if (data == NULL) {
1961 width = 0;
1962 height = 0;
1963 }
1964
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05001965 go->borders[side].width = width;
1966 go->borders[side].height = height;
1967 go->borders[side].tex_width = tex_width;
1968 go->borders[side].data = data;
Jason Ekstrande5512d42014-02-04 21:36:38 -06001969 go->border_status |= 1 << side;
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05001970}
1971
John Kåre Alsaker94659272012-11-13 19:10:18 +01001972static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001973gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01001974
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001975static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001976gl_renderer_output_create(struct weston_output *output,
Neil Roberts77c1a5b2014-03-07 18:05:50 +00001977 EGLNativeWindowType window,
1978 const EGLint *attribs,
1979 const EGLint *visual_id)
John Kåre Alsaker94659272012-11-13 19:10:18 +01001980{
1981 struct weston_compositor *ec = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001982 struct gl_renderer *gr = get_renderer(ec);
Neil Roberts77c1a5b2014-03-07 18:05:50 +00001983 struct gl_output_state *go;
1984 EGLConfig egl_config;
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001985 int i;
John Kåre Alsaker94659272012-11-13 19:10:18 +01001986
Neil Roberts77c1a5b2014-03-07 18:05:50 +00001987 if (egl_choose_config(gr, attribs, visual_id, &egl_config) == -1) {
1988 weston_log("failed to choose EGL config for output\n");
1989 return -1;
1990 }
1991
1992 if (egl_config != gr->egl_config &&
1993 !gr->has_configless_context) {
1994 weston_log("attempted to use a different EGL config for an "
1995 "output but EGL_MESA_configless_context is not "
1996 "supported\n");
1997 return -1;
1998 }
1999
Bryce Harringtonde16d892014-11-20 22:21:57 -08002000 go = zalloc(sizeof *go);
2001 if (go == NULL)
John Kåre Alsaker94659272012-11-13 19:10:18 +01002002 return -1;
2003
2004 go->egl_surface =
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002005 eglCreateWindowSurface(gr->egl_display,
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002006 egl_config,
John Kåre Alsaker94659272012-11-13 19:10:18 +01002007 window, NULL);
2008
2009 if (go->egl_surface == EGL_NO_SURFACE) {
2010 weston_log("failed to create egl surface\n");
2011 free(go);
2012 return -1;
2013 }
2014
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002015 if (gr->egl_context == NULL)
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002016 if (gl_renderer_setup(ec, go->egl_surface) < 0) {
John Kåre Alsaker94659272012-11-13 19:10:18 +01002017 free(go);
2018 return -1;
2019 }
2020
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02002021 for (i = 0; i < BUFFER_DAMAGE_COUNT; i++)
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002022 pixman_region32_init(&go->buffer_damage[i]);
2023
John Kåre Alsaker94659272012-11-13 19:10:18 +01002024 output->renderer_state = go;
2025
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002026 log_egl_config_info(gr->egl_display, egl_config);
2027
John Kåre Alsaker94659272012-11-13 19:10:18 +01002028 return 0;
2029}
2030
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002031static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002032gl_renderer_output_destroy(struct weston_output *output)
John Kåre Alsaker94659272012-11-13 19:10:18 +01002033{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002034 struct gl_renderer *gr = get_renderer(output->compositor);
2035 struct gl_output_state *go = get_output_state(output);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002036 int i;
2037
2038 for (i = 0; i < 2; i++)
2039 pixman_region32_fini(&go->buffer_damage[i]);
John Kåre Alsaker94659272012-11-13 19:10:18 +01002040
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002041 eglDestroySurface(gr->egl_display, go->egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01002042
2043 free(go);
2044}
2045
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002046static EGLSurface
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002047gl_renderer_output_surface(struct weston_output *output)
John Kåre Alsaker94659272012-11-13 19:10:18 +01002048{
2049 return get_output_state(output)->egl_surface;
2050}
2051
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03002052static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002053gl_renderer_destroy(struct weston_compositor *ec)
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04002054{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002055 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002056
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002057 wl_signal_emit(&gr->destroy_signal, gr);
2058
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002059 if (gr->has_bind_display)
2060 gr->unbind_display(gr->egl_display, ec->wl_display);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002061
2062 /* Work around crash in egl_dri2.c's dri2_make_current() - when does this apply? */
2063 eglMakeCurrent(gr->egl_display,
2064 EGL_NO_SURFACE, EGL_NO_SURFACE,
2065 EGL_NO_CONTEXT);
2066
2067 eglTerminate(gr->egl_display);
2068 eglReleaseThread();
Scott Moreau976a0502013-03-07 10:15:17 -07002069
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -04002070 wl_array_release(&gr->vertices);
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -04002071 wl_array_release(&gr->vtxcnt);
2072
Mariusz Ceiercbb91582014-02-08 20:11:24 +01002073 if (gr->fragment_binding)
2074 weston_binding_destroy(gr->fragment_binding);
2075 if (gr->fan_binding)
2076 weston_binding_destroy(gr->fan_binding);
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03002077
Scott Moreau976a0502013-03-07 10:15:17 -07002078 free(gr);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002079}
2080
2081static int
Neil Robertsb7f85332014-03-07 18:05:49 +00002082gl_renderer_setup_egl_extensions(struct weston_compositor *ec)
2083{
2084 struct gl_renderer *gr = get_renderer(ec);
2085 const char *extensions;
2086 EGLBoolean ret;
2087
2088 gr->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2089 gr->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2090 gr->bind_display =
2091 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
2092 gr->unbind_display =
2093 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
2094 gr->query_buffer =
2095 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
2096
2097 extensions =
2098 (const char *) eglQueryString(gr->egl_display, EGL_EXTENSIONS);
2099 if (!extensions) {
2100 weston_log("Retrieving EGL extension string failed.\n");
2101 return -1;
2102 }
2103
2104 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
2105 gr->has_bind_display = 1;
2106 if (gr->has_bind_display) {
2107 ret = gr->bind_display(gr->egl_display, ec->wl_display);
2108 if (!ret)
2109 gr->has_bind_display = 0;
2110 }
2111
2112 if (strstr(extensions, "EGL_EXT_buffer_age"))
2113 gr->has_egl_buffer_age = 1;
2114 else
2115 weston_log("warning: EGL_EXT_buffer_age not supported. "
2116 "Performance could be affected.\n");
2117
2118#ifdef EGL_EXT_swap_buffers_with_damage
2119 if (strstr(extensions, "EGL_EXT_swap_buffers_with_damage"))
2120 gr->swap_buffers_with_damage =
2121 (void *) eglGetProcAddress("eglSwapBuffersWithDamageEXT");
2122 else
2123 weston_log("warning: EGL_EXT_swap_buffers_with_damage not "
2124 "supported. Performance could be affected.\n");
2125#endif
2126
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002127#ifdef EGL_MESA_configless_context
2128 if (strstr(extensions, "EGL_MESA_configless_context"))
2129 gr->has_configless_context = 1;
2130#endif
2131
Neil Robertsb7f85332014-03-07 18:05:49 +00002132 return 0;
2133}
2134
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002135static const EGLint gl_renderer_opaque_attribs[] = {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002136 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
2137 EGL_RED_SIZE, 1,
2138 EGL_GREEN_SIZE, 1,
2139 EGL_BLUE_SIZE, 1,
2140 EGL_ALPHA_SIZE, 0,
2141 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
2142 EGL_NONE
2143};
2144
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002145static const EGLint gl_renderer_alpha_attribs[] = {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002146 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
2147 EGL_RED_SIZE, 1,
2148 EGL_GREEN_SIZE, 1,
2149 EGL_BLUE_SIZE, 1,
2150 EGL_ALPHA_SIZE, 1,
2151 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
2152 EGL_NONE
2153};
2154
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002155/** Checks whether a platform EGL client extension is supported
2156 *
2157 * \param ec The weston compositor
2158 * \param extension_suffix The EGL client extension suffix
2159 * \return 1 if supported, 0 if using fallbacks, -1 unsupported
2160 *
2161 * This function checks whether a specific platform_* extension is supported
2162 * by EGL.
2163 *
2164 * The extension suffix should be the suffix of the platform extension (that
2165 * specifies a <platform> argument as defined in EGL_EXT_platform_base). For
2166 * example, passing "foo" will check whether either "EGL_KHR_platform_foo",
2167 * "EGL_EXT_platform_foo", or "EGL_MESA_platform_foo" is supported.
2168 *
2169 * The return value is 1:
2170 * - if the supplied EGL client extension is supported.
2171 * The return value is 0:
2172 * - if the platform_base client extension isn't supported so will
2173 * fallback to eglGetDisplay and friends.
2174 * The return value is -1:
2175 * - if the supplied EGL client extension is not supported.
2176 */
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002177static int
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002178gl_renderer_supports(struct weston_compositor *ec,
2179 const char *extension_suffix)
2180{
2181 static const char *extensions = NULL;
2182 char s[64];
2183
2184 if (!extensions) {
2185 extensions = (const char *) eglQueryString(
2186 EGL_NO_DISPLAY, EGL_EXTENSIONS);
2187
2188 if (!extensions)
2189 return 0;
2190
2191 log_extensions("EGL client extensions",
2192 extensions);
2193 }
2194
2195 snprintf(s, sizeof s, "EGL_KHR_platform_%s", extension_suffix);
2196 if (strstr(extensions, s))
2197 return 1;
2198
2199 snprintf(s, sizeof s, "EGL_EXT_platform_%s", extension_suffix);
2200 if (strstr(extensions, s))
2201 return 1;
2202
2203 snprintf(s, sizeof s, "EGL_MESA_platform_%s", extension_suffix);
2204 if (strstr(extensions, s))
2205 return 1;
2206
2207 /* at this point we definitely have some client extensions but
2208 * haven't found the supplied client extension, so chances are it's
2209 * not supported. */
2210
2211 return -1;
2212}
2213
2214static int
2215gl_renderer_create(struct weston_compositor *ec, EGLenum platform,
2216 void *native_window, const EGLint *attribs,
2217 const EGLint *visual_id)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002218{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002219 struct gl_renderer *gr;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002220 EGLint major, minor;
2221
Bryce Harringtonde16d892014-11-20 22:21:57 -08002222 gr = zalloc(sizeof *gr);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002223 if (gr == NULL)
2224 return -1;
2225
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002226 gr->base.read_pixels = gl_renderer_read_pixels;
2227 gr->base.repaint_output = gl_renderer_repaint_output;
2228 gr->base.flush_damage = gl_renderer_flush_damage;
2229 gr->base.attach = gl_renderer_attach;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002230 gr->base.surface_set_color = gl_renderer_surface_set_color;
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03002231 gr->base.destroy = gl_renderer_destroy;
Pekka Paalaneneb35cbe2015-02-09 13:37:27 +02002232 gr->base.surface_get_content_size =
2233 gl_renderer_surface_get_content_size;
2234 gr->base.surface_copy_content = gl_renderer_surface_copy_content;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002235
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002236#ifdef EGL_EXT_platform_base
2237 if (!get_platform_display) {
2238 get_platform_display =
2239 (void *) eglGetProcAddress("eglGetPlatformDisplayEXT");
2240 }
2241
2242 if (get_platform_display && platform) {
2243 gr->egl_display =
2244 get_platform_display(platform, native_window,
2245 NULL);
2246 } else {
2247#endif
2248 weston_log("warning: either no EGL_EXT_platform_base "
2249 "support or specific platform support; "
2250 "falling back to eglGetDisplay.\n");
2251 gr->egl_display = eglGetDisplay(native_window);
2252#ifdef EGL_EXT_platform_base
2253 }
2254#endif
2255
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002256 if (gr->egl_display == EGL_NO_DISPLAY) {
2257 weston_log("failed to create display\n");
2258 goto err_egl;
2259 }
2260
2261 if (!eglInitialize(gr->egl_display, &major, &minor)) {
2262 weston_log("failed to initialize display\n");
2263 goto err_egl;
2264 }
2265
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002266 if (egl_choose_config(gr, attribs, visual_id, &gr->egl_config) < 0) {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002267 weston_log("failed to choose EGL config\n");
2268 goto err_egl;
2269 }
2270
2271 ec->renderer = &gr->base;
Pekka Paalanen7bb65102013-05-22 18:03:04 +03002272 ec->capabilities |= WESTON_CAP_ROTATION_ANY;
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03002273 ec->capabilities |= WESTON_CAP_CAPTURE_YFLIP;
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +02002274 ec->capabilities |= WESTON_CAP_VIEW_CLIP_MASK;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002275
Neil Robertsb7f85332014-03-07 18:05:49 +00002276 if (gl_renderer_setup_egl_extensions(ec) < 0)
2277 goto err_egl;
2278
Tomeu Vizoso12072b62013-08-06 20:05:55 +02002279 wl_display_add_shm_format(ec->wl_display, WL_SHM_FORMAT_RGB565);
2280
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002281 wl_signal_init(&gr->destroy_signal);
2282
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002283 return 0;
2284
2285err_egl:
Pekka Paalanen326529f2012-11-27 12:25:25 +02002286 gl_renderer_print_egl_error_state();
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002287 free(gr);
2288 return -1;
2289}
2290
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002291static EGLDisplay
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002292gl_renderer_display(struct weston_compositor *ec)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002293{
2294 return get_renderer(ec)->egl_display;
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04002295}
2296
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002297static int
2298compile_shaders(struct weston_compositor *ec)
2299{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002300 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker40684142012-11-13 19:10:25 +01002301
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03002302 gr->texture_shader_rgba.vertex_source = vertex_shader;
2303 gr->texture_shader_rgba.fragment_source = texture_fragment_shader_rgba;
2304
2305 gr->texture_shader_rgbx.vertex_source = vertex_shader;
2306 gr->texture_shader_rgbx.fragment_source = texture_fragment_shader_rgbx;
2307
2308 gr->texture_shader_egl_external.vertex_source = vertex_shader;
2309 gr->texture_shader_egl_external.fragment_source =
2310 texture_fragment_shader_egl_external;
2311
2312 gr->texture_shader_y_uv.vertex_source = vertex_shader;
2313 gr->texture_shader_y_uv.fragment_source = texture_fragment_shader_y_uv;
2314
2315 gr->texture_shader_y_u_v.vertex_source = vertex_shader;
2316 gr->texture_shader_y_u_v.fragment_source =
2317 texture_fragment_shader_y_u_v;
2318
Ander Conselvan de Oliveira41a50ea2013-11-27 17:43:51 +02002319 gr->texture_shader_y_xuxv.vertex_source = vertex_shader;
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03002320 gr->texture_shader_y_xuxv.fragment_source =
2321 texture_fragment_shader_y_xuxv;
2322
2323 gr->solid_shader.vertex_source = vertex_shader;
2324 gr->solid_shader.fragment_source = solid_fragment_shader;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002325
2326 return 0;
2327}
2328
2329static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002330fragment_debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002331 void *data)
2332{
2333 struct weston_compositor *ec = data;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002334 struct gl_renderer *gr = get_renderer(ec);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002335 struct weston_output *output;
2336
John Kåre Alsaker40684142012-11-13 19:10:25 +01002337 gr->fragment_shader_debug ^= 1;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002338
John Kåre Alsaker40684142012-11-13 19:10:25 +01002339 shader_release(&gr->texture_shader_rgba);
2340 shader_release(&gr->texture_shader_rgbx);
2341 shader_release(&gr->texture_shader_egl_external);
2342 shader_release(&gr->texture_shader_y_uv);
2343 shader_release(&gr->texture_shader_y_u_v);
2344 shader_release(&gr->texture_shader_y_xuxv);
2345 shader_release(&gr->solid_shader);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002346
Ander Conselvan de Oliveira03fb4ef2012-12-03 17:08:11 +02002347 /* Force use_shader() to call glUseProgram(), since we need to use
2348 * the recompiled version of the shader. */
2349 gr->current_shader = NULL;
2350
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002351 wl_list_for_each(output, &ec->output_list, link)
2352 weston_output_damage(output);
2353}
2354
Kristian Høgsberg8799d412013-05-07 10:50:09 -04002355static void
2356fan_debug_repaint_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
2357 void *data)
2358{
2359 struct weston_compositor *compositor = data;
2360 struct gl_renderer *gr = get_renderer(compositor);
2361
2362 gr->fan_debug = !gr->fan_debug;
2363 weston_compositor_damage_all(compositor);
2364}
2365
John Kåre Alsaker94659272012-11-13 19:10:18 +01002366static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002367gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002368{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002369 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002370 const char *extensions;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002371 EGLConfig context_config;
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04002372 EGLBoolean ret;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002373
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04002374 static const EGLint context_attribs[] = {
2375 EGL_CONTEXT_CLIENT_VERSION, 2,
2376 EGL_NONE
2377 };
2378
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04002379 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
2380 weston_log("failed to bind EGL_OPENGL_ES_API\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02002381 gl_renderer_print_egl_error_state();
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04002382 return -1;
2383 }
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03002384
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002385 context_config = gr->egl_config;
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03002386
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002387#ifdef EGL_MESA_configless_context
2388 if (gr->has_configless_context)
2389 context_config = EGL_NO_CONFIG_MESA;
2390#endif
2391
2392 gr->egl_context = eglCreateContext(gr->egl_display, context_config,
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04002393 EGL_NO_CONTEXT, context_attribs);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002394 if (gr->egl_context == NULL) {
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04002395 weston_log("failed to create context\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02002396 gl_renderer_print_egl_error_state();
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04002397 return -1;
2398 }
2399
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002400 ret = eglMakeCurrent(gr->egl_display, egl_surface,
2401 egl_surface, gr->egl_context);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04002402 if (ret == EGL_FALSE) {
2403 weston_log("Failed to make EGL context current.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02002404 gl_renderer_print_egl_error_state();
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04002405 return -1;
2406 }
2407
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002408 log_egl_gl_info(gr->egl_display);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002409
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002410 gr->image_target_texture_2d =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002411 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002412
2413 extensions = (const char *) glGetString(GL_EXTENSIONS);
2414 if (!extensions) {
2415 weston_log("Retrieving GL extension string failed.\n");
2416 return -1;
2417 }
2418
2419 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
2420 weston_log("GL_EXT_texture_format_BGRA8888 not available\n");
2421 return -1;
2422 }
2423
2424 if (strstr(extensions, "GL_EXT_read_format_bgra"))
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01002425 ec->read_format = PIXMAN_a8r8g8b8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002426 else
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01002427 ec->read_format = PIXMAN_a8b8g8r8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002428
Kristian Høgsberg1c4f1632013-08-07 12:11:27 -07002429#ifdef GL_EXT_unpack_subimage
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002430 if (strstr(extensions, "GL_EXT_unpack_subimage"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002431 gr->has_unpack_subimage = 1;
Kristian Høgsberg1c4f1632013-08-07 12:11:27 -07002432#endif
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002433
2434 if (strstr(extensions, "GL_OES_EGL_image_external"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002435 gr->has_egl_image_external = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002436
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002437 glActiveTexture(GL_TEXTURE0);
2438
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002439 if (compile_shaders(ec))
2440 return -1;
2441
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03002442 gr->fragment_binding =
2443 weston_compositor_add_debug_binding(ec, KEY_S,
2444 fragment_debug_binding,
2445 ec);
2446 gr->fan_binding =
2447 weston_compositor_add_debug_binding(ec, KEY_F,
2448 fan_debug_repaint_binding,
2449 ec);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002450
Pekka Paalanen035a0322012-10-24 09:43:06 +03002451 weston_log("GL ES 2 renderer features:\n");
2452 weston_log_continue(STAMP_SPACE "read-back format: %s\n",
Pekka Paalanenfe4eacf2013-01-10 16:50:42 +02002453 ec->read_format == PIXMAN_a8r8g8b8 ? "BGRA" : "RGBA");
Pekka Paalanen035a0322012-10-24 09:43:06 +03002454 weston_log_continue(STAMP_SPACE "wl_shm sub-image to texture: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002455 gr->has_unpack_subimage ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03002456 weston_log_continue(STAMP_SPACE "EGL Wayland extension: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002457 gr->has_bind_display ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03002458
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002459
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002460 return 0;
2461}
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002462
2463WL_EXPORT struct gl_renderer_interface gl_renderer_interface = {
2464 .opaque_attribs = gl_renderer_opaque_attribs,
2465 .alpha_attribs = gl_renderer_alpha_attribs,
2466
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002467 .supports = gl_renderer_supports,
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002468 .create = gl_renderer_create,
2469 .display = gl_renderer_display,
2470 .output_create = gl_renderer_output_create,
2471 .output_destroy = gl_renderer_output_destroy,
2472 .output_surface = gl_renderer_output_surface,
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05002473 .output_set_border = gl_renderer_output_set_border,
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002474 .print_egl_error_state = gl_renderer_print_egl_error_state
2475};